//! The in-process agent: the library in its two forms, the header, and //! real targets recorded through it. const std = @import("std"); const Ctx = @import("../build.zig").Ctx; const Compile = std.Build.Step.Compile; /// Returns the library to preload, for what tests it with other backends. pub fn add(ctx: Ctx) *Compile { const b = ctx.b; const imports: []const std.Build.Module.Import = &.{.{ .name = "ztrace", .module = ctx.ztrace }}; // For Zig programs: the agent compiled in. const module = b.addModule("ztrace-agent", .{ .root_source_file = b.path("src/agent/agent.zig"), .target = ctx.target, .optimize = ctx.optimize, .imports = imports }); // `ztrace run` reads its child with the code the agent reads itself with. ctx.exe.root_module.addImport("ztrace-agent", module); // For everything else: libztrace-agent.so to preload, libztrace-agent.a to link in. var libs: [2]*Compile = undefined; for (&libs, [_]std.builtin.LinkMode{ .dynamic, .static }) |*lib, linkage| { // LLVM always: the agent has inline assembly the self-hosted backend does not take. lib.* = b.addLibrary(.{ .name = "ztrace-agent", .linkage = linkage, .use_llvm = true, .root_module = b.createModule(.{ .root_source_file = b.path("src/agent/lib.zig"), .target = ctx.target, .optimize = ctx.optimize, .imports = imports, .pic = true, // No stack probes: they live in Zig's runtime, which a program // linked by cc does not have (and should not get: with it would // come Zig's memcpy in place of libc's). .stack_check = false, }) }); b.installArtifact(lib.*); } b.getInstallStep().dependOn(&b.addInstallHeaderFile(b.path("include/ztrace.h"), "ztrace.h").step); const t: Tests = .{ .ctx = ctx, .step = ctx.areaTest("test-agent", "The in-process agent on real targets"), .preload = libs[0], .check = b.addExecutable(.{ .name = "agent-check", .root_module = b.createModule(.{ .root_source_file = b.path("src/agent/check.zig"), .target = ctx.target, .optimize = ctx.optimize, .imports = imports }) }), }; t.step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = module, .use_llvm = true })).step); // Programs that know nothing of the tracer, with the agent preloaded. const list = ctx.cTarget("agent_list", &.{"agent_list.c"}); const fib = ctx.cTarget("agent_fib", &.{"agent_fib.c"}); const sort = ctx.cTarget("agent_sort", &.{"agent_sort.c"}); const sys = ctx.cTarget("agent_sys", &.{"agent_sys.c"}); const bench = ctx.cTarget("agent_bench", &.{"agent_bench.c"}); for ([_]*Compile{ list, fib, sort, sys, bench }) |exe| b.installArtifact(exe); t.expect("list", "100", t.record(list.getEmittedBin(), true, &.{"--alloc"}, &.{"100"})); t.expect("fib", "10", t.record(fib.getEmittedBin(), true, &.{ "--hook", "fib" }, &.{"10"})); t.expect("sort", "numbers", t.record(sort.getEmittedBin(), true, &.{ "--hook", "swap", "--watch", "numbers" }, &.{})); t.expect("syscalls", "", t.record(sys.getEmittedBin(), true, &.{"--syscalls"}, &.{})); // Everything at once. t.expect("list", "100", t.record(list.getEmittedBin(), true, &.{ "--alloc", "--syscalls", "--hook", "main", "--watch", "stdout" }, &.{"100"})); // Stops: the target held at each return of swap while the tracer reads the array from outside. t.expect("sort", "numbers", t.record(sort.getEmittedBin(), true, &.{ "--hook", "swap", "--stop-at", "ret", "--read", "numbers" }, &.{})); const asked = t.record(fib.getEmittedBin(), true, &.{ "--hook", "fib", "--stop-at", "call", "-i" }, &.{"3"}).run; asked.setStdIn(.{ .bytes = "s\n\nx 8 4\nc\n" }); asked.expectStdOutMatch("call tid="); asked.expectStdOutMatch("not readable"); asked.expectStdOutMatch("fib(3) = 2"); // With no tracer the preloaded agent is not there. const alone = b.addSystemCommand(&.{"env"}); alone.addPrefixedArtifactArg("LD_PRELOAD=", libs[0]); alone.addArtifactArg(list); alone.expectStdOutEqual("sum 4950 of 100\n"); t.step.dependOn(&alone.step); // Programs that annotate themselves, with the agent linked in: against // glibc, statically against musl, and as a module of a Zig program. const api = ctx.cTarget("agent_api", &.{"agent_api.c"}); const musl = b.resolveTargetQuery(.{ .cpu_arch = ctx.target.result.cpu.arch, .os_tag = .linux, .abi = .musl }); const api_static_module = b.createModule(.{ .target = musl, .optimize = .Debug, .link_libc = true }); api_static_module.addCSourceFiles(.{ .root = b.path("targets"), .files = &.{"agent_api.c"}, .flags = &.{ "-O1", "-g" } }); const api_static = b.addExecutable(.{ .name = "agent_api_static", .root_module = api_static_module, .linkage = .static }); for ([_]*Compile{ api, api_static }) |exe| { exe.root_module.addIncludePath(b.path("include")); exe.root_module.linkLibrary(libs[1]); t.expect("api", "", t.record(exe.getEmittedBin(), false, &.{}, &.{})); } const api_zig = ctx.zigTarget("agent_zig", "targets/agent_zig.zig"); api_zig.root_module.addImport("ztrace-agent", module); api_zig.use_llvm = true; t.expect("api", "", t.record(api_zig.getEmittedBin(), false, &.{}, &.{})); // The allocator of a statically linked program, through --wrap: the build // system has no option for it, zig cc does. const wrap = b.addSystemCommand(&.{ b.graph.zig_exe, "cc", "-target", "x86_64-linux-musl", "-static", "-O1", "-g", "-o" }); const wrapped = wrap.addOutputFileArg("wrapped/agent_list"); wrap.addFileArg(b.path("targets/agent_list.c")); wrap.addArtifactArg(libs[1]); for ([_][]const u8{ "malloc", "calloc", "realloc", "free", "posix_memalign", "aligned_alloc", "memalign", "pthread_create", "fork" }) |name| wrap.addArg(b.fmt("-Wl,-wrap,{s}", .{name})); t.expect("list", "100", t.record(wrapped, false, &.{ "--alloc", "--syscalls" }, &.{"100"})); // A recording that reaches its size ends the run, closed and readable. const full = b.addRunArtifact(ctx.exe); full.addArgs(&.{ "run", "--max-size", "64K", "--alloc", "--preload" }); full.addArtifactArg(libs[0]); full.addArg("-o"); const full_file = full.addOutputFileArg("full.ztrace"); full.addArg("--"); full.addArtifactArg(list); full.addArg("200000"); full.expectStdErrMatch("full at 65536 bytes"); full.expectExitCode(1); const full_show = b.addRunArtifact(ctx.exe); full_show.addArg("show"); full_show.addFileArg(full_file); full_show.expectStdOutMatch(" alloc"); t.step.dependOn(&full_show.step); // zig build bench-agent -Doptimize=ReleaseFast: what each capability // costs the target (the loop prints its nanoseconds an iteration) and // what it writes (ztrace prints the recording's size), one after another. const bench_step = b.step("bench-agent", "What the agent costs a malloc, a syscall, a call, a watched region"); const P = struct { record: bool = false, preload: bool = true, options: []const []const u8 = &.{}, loop: []const u8, n: []const u8 }; var before: ?*std.Build.Step = null; for ([_]?P{ null, // the loops alone .{ .options = &.{"--alloc"}, .loop = "malloc", .n = "5000000" }, .{ .options = &.{ "--alloc", "--cut", "alloc,free" }, .loop = "malloc", .n = "1000000" }, .{ .options = &.{"--syscalls"}, .loop = "syscall", .n = "1000000" }, .{ .record = true, .preload = false, .options = &.{"--syscalls"}, .loop = "syscall", .n = "200000" }, .{ .options = &.{ "--hook", "work" }, .loop = "call", .n = "1000000" }, .{ .options = &.{ "--hook", "work", "--watch", "kb1" }, .loop = "call", .n = "200000" }, .{ .options = &.{ "--hook", "work", "--watch", "kb64" }, .loop = "call", .n = "200000" }, .{ .options = &.{ "--hook", "work", "--watch", "mb1" }, .loop = "call", .n = "100000" }, .{ .options = &.{ "--hook", "work", "--stop-at", "ret" }, .loop = "call", .n = "100000" }, .{ .options = &.{ "--hook", "work", "--stop-at", "ret", "--read", "kb1" }, .loop = "call", .n = "100000" }, .{ .options = &.{ "--hook", "work", "--stop-at", "ret", "--read", "kb64" }, .loop = "call", .n = "100000" }, .{ .options = &.{ "--hook", "work", "--stop-at", "ret", "--read", "mb1" }, .loop = "call", .n = "50000" }, }) |case| { const p = case orelse { for ([_][]const u8{ "malloc", "syscall", "call" }) |loop| { const native = b.addRunArtifact(bench); native.addArgs(&.{ loop, "5000000" }); native.has_side_effects = true; if (before) |step| native.step.dependOn(step); before = &native.step; } continue; }; const echo = b.addSystemCommand(&.{ "echo", if (p.record) "ztrace record" else "ztrace run" }); echo.addArgs(p.options); echo.step.dependOn(before.?); const run = b.addRunArtifact(ctx.exe); run.addArgs(&.{ if (p.record) "record" else "run", "-o" }); _ = run.addOutputFileArg("bench.ztrace"); if (p.preload) { run.addArg("--preload"); run.addArtifactArg(libs[0]); } run.addArgs(p.options); run.addArg("--"); run.addArtifactArg(bench); run.addArgs(&.{ p.loop, p.n }); run.has_side_effects = true; run.step.dependOn(&echo.step); before = &run.step; } bench_step.dependOn(before.?); for ([_]*Compile{ api, api_static, api_zig }) |exe| b.installArtifact(exe); return libs[0]; } const Recording = struct { run: *std.Build.Step.Run, file: std.Build.LazyPath }; const Tests = struct { ctx: Ctx, step: *std.Build.Step, preload: *Compile, check: *Compile, /// `ztrace run OPTIONS -- TARGET ARGS`, which has to end with the target's exit 0. fn record(t: Tests, target: std.Build.LazyPath, preload: bool, options: []const []const u8, args: []const []const u8) Recording { const run = t.ctx.b.addRunArtifact(t.ctx.exe); run.addArgs(&.{ "run", "-o" }); const file = run.addOutputFileArg("agent.ztrace"); if (preload) { run.addArg("--preload"); run.addArtifactArg(t.preload); } run.addArgs(options); run.addArg("--"); run.addFileArg(target); run.addArgs(args); run.expectStdErrMatch("(0 dropped)"); run.expectStdErrMatch("exit 0"); t.step.dependOn(&run.step); return .{ .run = run, .file = file }; } /// The recording `run` made holds what `scenario` says (src/agent/check.zig). fn expect(t: Tests, scenario: []const u8, argument: []const u8, recording: Recording) void { const check = t.ctx.b.addRunArtifact(t.check); check.addArg(scenario); check.addFileArg(recording.file); check.addArg(argument); check.expectExitCode(0); t.step.dependOn(&check.step); } };