Skip to content

Commit

Permalink
make compatible the latest zig
Browse files Browse the repository at this point in the history
Signed-off-by: Tw <[email protected]>
  • Loading branch information
tw4452852 committed Sep 12, 2024
1 parent 23ada2c commit 2caf5b8
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 49 deletions.
64 changes: 32 additions & 32 deletions src/bpf/args.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn Ctx(comptime func_name: []const u8) type {
const f = @typeInfo(@TypeOf(@field(vmlinux, "_zig_" ++ func_name)));
comptime var fields: []const StructField = &.{};

for (0.., f.Fn.params) |i, arg| {
for (0.., f.@"fn".params) |i, arg| {
fields = fields ++ [_]StructField{
.{
.name = std.fmt.comptimePrint("arg{}", .{i}),
Expand All @@ -44,15 +44,15 @@ pub fn Ctx(comptime func_name: []const u8) type {
fields = fields ++ [_]StructField{
.{
.name = "ret",
.type = f.Fn.return_type.?,
.type = f.@"fn".return_type.?,
.default_value = null,
.is_comptime = false,
.alignment = @sizeOf(u64),
},
};

return @Type(.{
.Struct = .{
.@"struct" = .{
.layout = .@"extern",
.is_tuple = false,
.fields = fields,
Expand Down Expand Up @@ -140,15 +140,15 @@ pub const REGS = extern struct {
/// Check if the type is ?*T or *T.
pub inline fn is_pointer(comptime typ: type) bool {
const ti = @typeInfo(typ);
return ti == .Pointer or (ti == .Optional and @typeInfo(ti.Optional.child) == .Pointer);
return ti == .pointer or (ti == .optional and @typeInfo(ti.optional.child) == .pointer);
}

/// Get the pointee type
pub inline fn deref_pointer(comptime typ: type) type {
const ti = @typeInfo(typ);
return switch (ti) {
inline .Pointer => |info| return info.child,
inline .Optional => |info| return @typeInfo(info.child).Pointer.child,
inline .pointer => |info| return info.child,
inline .optional => |info| return @typeInfo(info.child).pointer.child,
else => @compileLog(ti),
};
}
Expand All @@ -158,8 +158,8 @@ pub fn cast(comptime T: type, rc: c_ulong) T {
if (is_pointer(T)) return @ptrFromInt(rc);

const ti = @typeInfo(T);
if (ti == .Int) {
if (ti.Int.signedness == .signed) {
if (ti == .int) {
if (ti.int.signedness == .signed) {
return @truncate(@as(c_long, @bitCast(rc)));
}
return @truncate(rc);
Expand All @@ -184,8 +184,8 @@ pub fn PT_REGS(comptime func_name: []const u8, comptime for_syscall: bool) type
return @alignCast(@ptrCast(self));
}

pub usingnamespace if (f.Fn.params.len < 1) struct {} else struct {
const RET = f.Fn.params[0].type.?;
pub usingnamespace if (f.@"fn".params.len < 1) struct {} else struct {
const RET = f.@"fn".params[0].type.?;

pub fn arg0(self: *Self) RET {
if (!in_bpf_program) {
Expand All @@ -199,8 +199,8 @@ pub fn PT_REGS(comptime func_name: []const u8, comptime for_syscall: bool) type
}
};

pub usingnamespace if (f.Fn.params.len < 2) struct {} else struct {
const RET = f.Fn.params[1].type.?;
pub usingnamespace if (f.@"fn".params.len < 2) struct {} else struct {
const RET = f.@"fn".params[1].type.?;

pub fn arg1(self: *Self) RET {
if (!in_bpf_program) {
Expand All @@ -214,8 +214,8 @@ pub fn PT_REGS(comptime func_name: []const u8, comptime for_syscall: bool) type
}
};

pub usingnamespace if (f.Fn.params.len < 3) struct {} else struct {
const RET = f.Fn.params[2].type.?;
pub usingnamespace if (f.@"fn".params.len < 3) struct {} else struct {
const RET = f.@"fn".params[2].type.?;

pub fn arg2(self: *Self) RET {
if (!in_bpf_program) {
Expand All @@ -229,8 +229,8 @@ pub fn PT_REGS(comptime func_name: []const u8, comptime for_syscall: bool) type
}
};

pub usingnamespace if (f.Fn.params.len < 4) struct {} else struct {
const RET = f.Fn.params[3].type.?;
pub usingnamespace if (f.@"fn".params.len < 4) struct {} else struct {
const RET = f.@"fn".params[3].type.?;

pub fn arg3(self: *Self) RET {
if (!in_bpf_program) {
Expand All @@ -244,8 +244,8 @@ pub fn PT_REGS(comptime func_name: []const u8, comptime for_syscall: bool) type
}
};

pub usingnamespace if (f.Fn.params.len < 5) struct {} else struct {
const RET = f.Fn.params[4].type.?;
pub usingnamespace if (f.@"fn".params.len < 5) struct {} else struct {
const RET = f.@"fn".params[4].type.?;

pub fn arg4(self: *Self) RET {
if (!in_bpf_program) {
Expand All @@ -259,8 +259,8 @@ pub fn PT_REGS(comptime func_name: []const u8, comptime for_syscall: bool) type
}
};

pub usingnamespace if (f.Fn.return_type.? == void) struct {} else struct {
const RET = f.Fn.return_type.?;
pub usingnamespace if (f.@"fn".return_type.? == void) struct {} else struct {
const RET = f.@"fn".return_type.?;

pub fn ret(self: *Self) RET {
if (!in_bpf_program) {
Expand Down Expand Up @@ -297,48 +297,48 @@ pub fn SYSCALL(comptime name: []const u8) type {
@ptrCast(self);
}

pub usingnamespace if (f.Fn.params.len < 1) struct {} else struct {
const RET = f.Fn.params[0].type.?;
pub usingnamespace if (f.@"fn".params.len < 1) struct {} else struct {
const RET = f.@"fn".params[0].type.?;

pub fn arg0(self: *Self) RET {
return self.get_arg_ctx().arg0();
}
};

pub usingnamespace if (f.Fn.params.len < 2) struct {} else struct {
const RET = f.Fn.params[1].type.?;
pub usingnamespace if (f.@"fn".params.len < 2) struct {} else struct {
const RET = f.@"fn".params[1].type.?;

pub fn arg1(self: *Self) RET {
return self.get_arg_ctx().arg1();
}
};

pub usingnamespace if (f.Fn.params.len < 3) struct {} else struct {
const RET = f.Fn.params[2].type.?;
pub usingnamespace if (f.@"fn".params.len < 3) struct {} else struct {
const RET = f.@"fn".params[2].type.?;

pub fn arg2(self: *Self) RET {
return self.get_arg_ctx().arg2();
}
};

pub usingnamespace if (f.Fn.params.len < 4) struct {} else struct {
const RET = f.Fn.params[3].type.?;
pub usingnamespace if (f.@"fn".params.len < 4) struct {} else struct {
const RET = f.@"fn".params[3].type.?;

pub fn arg3(self: *Self) RET {
return self.get_arg_ctx().arg3();
}
};

pub usingnamespace if (f.Fn.params.len < 5) struct {} else struct {
const RET = f.Fn.params[4].type.?;
pub usingnamespace if (f.@"fn".params.len < 5) struct {} else struct {
const RET = f.@"fn".params[4].type.?;

pub fn arg4(self: *Self) RET {
return self.get_arg_ctx().arg4();
}
};

pub usingnamespace if (f.Fn.return_type.? == void) struct {} else struct {
const RET = f.Fn.return_type.?;
pub usingnamespace if (f.@"fn".return_type.? == void) struct {} else struct {
const RET = f.@"fn".return_type.?;

pub fn ret(self: *Self) RET {
return @as(*T, @ptrCast(self)).ret();
Expand Down
6 changes: 4 additions & 2 deletions src/bpf/map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,22 @@ fn Map(
}} else [_]StructField{});

const Def = @Type(.{
.Struct = .{
.@"struct" = .{
.layout = .@"extern",
.is_tuple = false,
.fields = &fields,
.decls = &[_]Declaration{},
},
});

return struct {
var def: Def = undefined;
const p: *Def = &def;

const Self = @This();

comptime {
@export(Self.def, .{ .name = name, .section = ".maps" });
@export(p, .{ .name = name, .section = ".maps" });
}

/// Perform a lookup in *map* for an entry associated to *key*.
Expand Down
2 changes: 1 addition & 1 deletion src/btf_sanitizer/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn dbg_print(comptime fmt: []const u8, args: anytype) void {
}
}

fn libbpf_dbg_printf(level: c.libbpf_print_level, fmt: [*c]const u8, args: @typeInfo(@typeInfo(@typeInfo(c.libbpf_print_fn_t).Optional.child).Pointer.child).Fn.params[2].type.?) callconv(.C) c_int {
fn libbpf_dbg_printf(level: c.libbpf_print_level, fmt: [*c]const u8, args: @typeInfo(@typeInfo(@typeInfo(c.libbpf_print_fn_t).optional.child).pointer.child).@"fn".params[2].type.?) callconv(.C) c_int {
if (!debug and level == c.LIBBPF_DEBUG) return 0;

return c.vdprintf(std.io.getStdErr().handle, fmt, args);
Expand Down
2 changes: 1 addition & 1 deletion src/tests/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub const libbpf = @cImport({
});
const build_options = @import("@build_options");

pub fn dbg_printf(level: libbpf.libbpf_print_level, fmt: [*c]const u8, args: @typeInfo(@typeInfo(@typeInfo(libbpf.libbpf_print_fn_t).Optional.child).Pointer.child).Fn.params[2].type.?) callconv(.C) c_int {
pub fn dbg_printf(level: libbpf.libbpf_print_level, fmt: [*c]const u8, args: @typeInfo(@typeInfo(@typeInfo(libbpf.libbpf_print_fn_t).optional.child).pointer.child).@"fn".params[2].type.?) callconv(.C) c_int {
if (!build_options.debug and level == libbpf.LIBBPF_DEBUG) return 0;

return libbpf.vdprintf(std.io.getStdErr().handle, fmt, args);
Expand Down
16 changes: 8 additions & 8 deletions src/tools/trace/comm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ pub fn Arg(comptime name: []const u8, comptime is_syscall: bool) type {

var ti = @typeInfo(FT);
inline while (comptime it.next()) |field_name| : (ti = @typeInfo(FT)) {
if (ti == .Optional) {
FT = ti.Optional.child;
if (ti == .optional) {
FT = ti.optional.child;
ti = @typeInfo(FT);
}

Expand All @@ -81,8 +81,8 @@ pub fn Arg(comptime name: []const u8, comptime is_syscall: bool) type {
if (is_string(FT)) return String;
}
return switch (ti) {
.Pointer => |info| info.child,
.Optional => |info| info.child,
.pointer => |info| info.child,
.optional => |info| info.child,
else => FT,
};
}
Expand Down Expand Up @@ -115,14 +115,14 @@ pub fn Arg(comptime name: []const u8, comptime is_syscall: bool) type {
}

comptime var ti = @typeInfo(FT);
var src: usize = if (ti == .Pointer) @intFromPtr(arg) else @intFromPtr(&arg);
var src: usize = if (ti == .pointer) @intFromPtr(arg) else @intFromPtr(&arg);

inline while (comptime it.next()) |field_name| {
if (ti == .Optional) {
FT = ti.Optional.child;
if (ti == .pptional) {
FT = ti.optional.child;
ti = @typeInfo(FT);
}
if (ti == .Pointer) {
if (ti == .pointer) {
FT = ti.Pointer.child;
ti = @typeInfo(FT);
}
Expand Down
4 changes: 2 additions & 2 deletions src/tools/trace/trace.bpf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn generate(comptime name: []const u8, comptime id: u32, comptime with_stack: bo
}

comptime {
@export(_entry, .{ .name = name ++ if (is_syscall) "_syscall_entry" else "_kprobe_entry" });
@export(&_entry, .{ .name = name ++ if (is_syscall) "_syscall_entry" else "_kprobe_entry" });
}

fn _exit(ctx: *F.Ctx()) linksection(F.exit_section()) callconv(.C) c_long {
Expand Down Expand Up @@ -95,7 +95,7 @@ fn generate(comptime name: []const u8, comptime id: u32, comptime with_stack: bo
}
}
if (want_ret) {
@export(_exit, .{ .name = name ++ if (is_syscall) "_syscall_exit" else "_kprobe_exit" });
@export(&_exit, .{ .name = name ++ if (is_syscall) "_syscall_exit" else "_kprobe_exit" });
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/tools/trace/trace.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TF = @TypeOf(tracing_funcs[0]);
var exiting = false;
var debug = false;

fn dbg_printf(level: libbpf.libbpf_print_level, fmt: [*c]const u8, args: @typeInfo(@typeInfo(@typeInfo(libbpf.libbpf_print_fn_t).Optional.child).Pointer.child).Fn.params[2].type.?) callconv(.C) c_int {
fn dbg_printf(level: libbpf.libbpf_print_level, fmt: [*c]const u8, args: @typeInfo(@typeInfo(@typeInfo(libbpf.libbpf_print_fn_t).optional.child).pointer.child).@"fn".params[2].type.?) callconv(.C) c_int {
if (!debug and level == libbpf.LIBBPF_DEBUG) return 0;

return libbpf.vdprintf(std.io.getStdErr().handle, fmt, args);
Expand Down
4 changes: 2 additions & 2 deletions src/vmlinux_dumper/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn dbg_print(comptime fmt: []const u8, args: anytype) void {
}
}

fn btf_dump_printf(ctx: ?*anyopaque, fmt: [*c]const u8, args: @typeInfo(@typeInfo(@typeInfo(libbpf.btf_dump_printf_fn_t).Optional.child).Pointer.child).Fn.params[2].type.?) callconv(.C) void {
fn btf_dump_printf(ctx: ?*anyopaque, fmt: [*c]const u8, args: @typeInfo(@typeInfo(@typeInfo(libbpf.btf_dump_printf_fn_t).optional.child).pointer.child).@"fn".params[2].type.?) callconv(.C) void {
const fd = @intFromPtr(ctx);
_ = libbpf.vdprintf(@intCast(fd), fmt, args);
}
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn main() !void {
}
const output = try std.fs.createFileAbsolute(output_arg.?, .{});
defer output.close();
if (debug) print("vmlinux_dumper: dump vmlinux.h to {s}\n", .{output_arg.?});
dbg_print("dump vmlinux.zig to {s}\n", .{output_arg.?});

const d = libbpf.btf_dump__new(btf, btf_dump_printf, @ptrFromInt(@as(usize, @intCast(output.handle))), null);
if (d == null) {
Expand Down

0 comments on commit 2caf5b8

Please sign in to comment.