I am cross compiling libbpf and using it my project. As part of that, one can enroll a logger to report errors when loading and validating ebpf for instance. This can be done easily with zig (x86) by defining a function such as:
So, it seems that the C-imported names are different depending on the architecture. This all feels a bit brittle, as though the type is arbitrary somehow. I realize it may be a bit “cursed” what I’m trying here, but what are the best ways of working when it comes to issues such as this one?
That’s a nice approach yes. I have something similar in my code base as a band-aid fix. Perhaps this stuff is variable-args specific, I wonder I there’s an existing abstraction that handles this built-in?
You can also do fn(...) like you can in c (requires c calling convention).
But that’s for the function that receives the var args in the first place which is not the log function you are defining. rather it is getting the var args handle which is how var args are propagated to other functions.
nix/store/wwjxwysa0igp0kjryg6bsnvzq7b0byi1-zig-0.15.1/lib/std/builtin.zig:899:29: error: disabled due to miscompilations
.stage2_llvm => @compileError("disabled due to miscompilations"),
x86_64
src/bpf.zig:8:36: error: expected type '*const fn (c_uint, [*c]const u8, [*c]cimport.struct___va_list_tag_1) callconv(.c) c_int', found '*const fn (c_uint, [*c]const u8, builtin.VaListX86_64) callconv(.c) c_int'
.zig => c.libbpf_set_print(log),
^~~
src/bpf.zig:8:36: note: pointer type child 'fn (c_uint, [*c]const u8, builtin.VaListX86_64) callconv(.c) c_int' cannot cast into pointer type child 'fn (c_uint, [*c]const u8, [*c]cimport.struct___va_list_tag_1) callconv(.c) c_int'
src/bpf.zig:8:36: note: parameter 2 'builtin.VaListX86_64' cannot cast into '[*c]cimport.struct___va_list_tag_1'
/nix/store/wwjxwysa0igp0kjryg6bsnvzq7b0byi1-zig-0.15.1/lib/std/builtin.zig:876:33: note: struct declared here
pub const VaListX86_64 = extern struct {
~~~~~~~^~~~~~
referenced by:
init: src/app.zig:40:35
main: src/main.zig:67:36
4 reference(s) hidden; use '-freference-trace=6' to see all references
src/bpf.zig:175:51: error: expected type '[*c]cimport.struct___va_list_tag_1', found 'builtin.VaListX86_64'
const len_c = c.vsnprintf(&buf, buf.len, fmt, ap);
Might be that my case is a degenerate one or that I’m using the API wrong, though . Perhaps its also a zig 0.15.1 thing.