Skip to content

Commit

Permalink
test: add build option for debugging
Browse files Browse the repository at this point in the history
Signed-off-by: Tw <weii.tan>
  • Loading branch information
Tw committed May 9, 2023
1 parent bad505e commit aa1b356
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
7 changes: 6 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const builtin = @import("builtin");
const fs = std.fs;
const Builder = std.build.Builder;

const debugging = true;
const debugging = false;

fn create_bpf_prog(ctx: *const Ctx, src_path: ?[]const u8) *std.build.CompileStep {
const name = fs.path.stem(src_path orelse "?");
Expand Down Expand Up @@ -203,6 +203,11 @@ pub fn build(b: *Builder) !void {
});
}

// add debug option to test
const tests_options = b.addOptions();
exe_tests.addOptions("build_options", tests_options);
tests_options.addOption(bool, "debug", debugging);

const run_test_step = b.step("test", "Build unit tests");
run_test_step.dependOn(&install_test.step);
}
6 changes: 0 additions & 6 deletions src/tests/array.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ test "array" {
}
defer libbpf.bpf_object__close(obj);

var _map = libbpf.bpf_object__next_map(obj, null);
while (_map) |map| : (_map = libbpf.bpf_object__next_map(obj, _map)) {
print("find map: {s}\n", .{libbpf.bpf_map__name(map)});
}

var ret = libbpf.bpf_object__load(obj);
if (ret != 0) {
print("failed to load bpf object: {}\n", .{std.os.errno(-1)});
Expand All @@ -32,7 +27,6 @@ test "array" {

if (libbpf.bpf_object__next_program(obj, null)) |prog| {
var map = libbpf.bpf_object__next_map(obj, null).?;
print("find prog: {s}, map: {s}\n", .{ libbpf.bpf_program__name(prog), libbpf.bpf_map__name(map) });

// map[0] = 1
var k: u32 = 0;
Expand Down
1 change: 0 additions & 1 deletion src/tests/hash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ test "hash" {

if (libbpf.bpf_object__next_program(obj, null)) |prog| {
var map = libbpf.bpf_object__next_map(obj, null).?;
print("find prog: {s}, map: {s}\n", .{ libbpf.bpf_program__name(prog), libbpf.bpf_map__name(map) });

// map[0] = 1
var k: u32 = 0;
Expand Down
5 changes: 4 additions & 1 deletion src/tests/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ pub const libbpf = @cImport({
@cInclude("libbpf.h");
@cInclude("bpf.h");
});
const build_options = @import("build_options");

pub fn dbg_printf(level: libbpf.libbpf_print_level, fmt: [*c]const u8, args: [*c]libbpf.__va_list_tag) callconv(.C) c_int {
if (!build_options.debug and level == libbpf.LIBBPF_DEBUG) return 0;

pub fn dbg_printf(_: libbpf.libbpf_print_level, fmt: [*c]const u8, args: [*c]libbpf.__va_list_tag) callconv(.C) c_int {
return libbpf.vdprintf(std.io.getStdErr().handle, fmt, args);
}

Expand Down
1 change: 0 additions & 1 deletion src/tests/tracepoint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ test "tracepoint" {

if (libbpf.bpf_object__next_program(obj, null)) |prog| {
var map = libbpf.bpf_object__next_map(obj, null).?;
print("find prog: {s}, map: {s}\n", .{ libbpf.bpf_program__name(prog), libbpf.bpf_map__name(map) });

const link = libbpf.bpf_program__attach(prog) orelse {
print("failed to attach prog {s}: {}\n", .{ libbpf.bpf_program__name(prog), std.os.errno(-1) });
Expand Down

0 comments on commit aa1b356

Please sign in to comment.