1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
//! 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;
pub fn add(ctx: Ctx) void {
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,
}) });
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"});
for ([_]*Compile{ list, fib, sort, sys }) |exe| b.installArtifact(exe);
t.expect("list", "100", t.record(list, true, &.{"--alloc"}, &.{"100"}));
t.expect("fib", "10", t.record(fib, true, &.{ "--hook", "fib" }, &.{"10"}));
t.expect("sort", "numbers", t.record(sort, true, &.{ "--hook", "swap", "--watch", "numbers" }, &.{}));
t.expect("syscalls", "", t.record(sys, true, &.{"--syscalls"}, &.{}));
// Everything at once.
t.expect("list", "100", t.record(list, 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, true, &.{ "--hook", "swap", "--stop-at", "ret", "--read", "numbers" }, &.{}));
const asked = t.record(fib, 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, 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, false, &.{}, &.{}));
for ([_]*Compile{ api, api_static, api_zig }) |exe| b.installArtifact(exe);
}
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: *Compile, 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.addArtifactArg(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);
}
};
|