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
|
const std = @import("std");
/// What each area's build file (`build/*.zig`) is handed.
pub const Ctx = struct {
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
/// The tracing library: events, ring, recorder, backends.
ztrace: *std.Build.Module,
/// The `ztrace` command.
exe: *std.Build.Step.Compile,
/// `zig build test`: everything.
test_step: *std.Build.Step,
/// A C program to trace, from `targets/`: -O1 with debug info, frame
/// pointers kept, so its code is short and still maps to its source; no
/// UBSan (Zig's default in Debug), whose checks would be traced too.
pub fn cTarget(ctx: Ctx, name: []const u8, files: []const []const u8) *std.Build.Step.Compile {
const mod = ctx.b.createModule(.{ .target = ctx.target, .optimize = .Debug, .link_libc = true, .omit_frame_pointer = false, .sanitize_c = .off });
mod.addCSourceFiles(.{ .root = ctx.b.path("targets"), .files = files, .flags = &.{ "-O1", "-g", "-fno-omit-frame-pointer" } });
return ctx.b.addExecutable(.{ .name = name, .root_module = mod });
}
/// A Zig program to trace, from `targets/`, with the library importable
/// as `ztrace`.
pub fn zigTarget(ctx: Ctx, name: []const u8, file: []const u8) *std.Build.Step.Compile {
return ctx.b.addExecutable(.{ .name = name, .root_module = ctx.b.createModule(.{
.root_source_file = ctx.b.path(file),
.target = ctx.target,
.optimize = ctx.optimize,
.imports = &.{.{ .name = "ztrace", .module = ctx.ztrace }},
}) });
}
/// A step of its own for one area's tests, also part of `test`.
pub fn areaTest(ctx: Ctx, name: []const u8, description: []const u8) *std.Build.Step {
const step = ctx.b.step(name, description);
ctx.test_step.dependOn(step);
return step;
}
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const xitdb = b.dependency("xitdb", .{}).module("xitdb");
const ztrace = b.addModule("ztrace", .{
.root_source_file = b.path("src/trace/root.zig"),
.target = target,
.optimize = optimize,
.imports = &.{.{ .name = "xitdb", .module = xitdb }},
});
const exe = b.addExecutable(.{ .name = "ztrace", .root_module = b.createModule(.{
.root_source_file = b.path("src/cli/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{.{ .name = "ztrace", .module = ztrace }},
}) });
b.installArtifact(exe);
const ctx: Ctx = .{ .b = b, .target = target, .optimize = optimize, .ztrace = ztrace, .exe = exe, .test_step = b.step("test", "Run every test") };
// The library's own tests, and a target with its own producer recorded
// through the ring.
const core = ctx.areaTest("test-core", "Events, ring, recorder, ptrace basics");
core.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = ztrace })).step);
const emit = b.addRunArtifact(exe);
emit.addArgs(&.{ "run", "-o" });
_ = emit.addOutputFileArg("emit.ztrace");
emit.addArg("--");
emit.addArtifactArg(ctx.zigTarget("emit", "targets/emit.zig"));
emit.addArg("5000");
emit.expectStdErrMatch("5001 events (0 dropped)");
emit.expectStdErrMatch("exit 7");
core.dependOn(&emit.step);
const bench = b.addExecutable(.{ .name = "bench-store", .root_module = b.createModule(.{
.root_source_file = b.path("bench/store.zig"),
.target = target,
.optimize = .ReleaseFast,
.imports = &.{.{ .name = "ztrace", .module = b.createModule(.{
.root_source_file = b.path("src/trace/root.zig"),
.target = target,
.optimize = .ReleaseFast,
.imports = &.{.{ .name = "xitdb", .module = xitdb }},
}) }},
}) });
b.step("bench-store", "What a recording costs a moment").dependOn(&b.addRunArtifact(bench).step);
// Each area adds its own artifacts, targets and test step.
@import("build/debugger.zig").add(ctx);
const agent = @import("build/agent.zig").add(ctx);
const instrumented_list = @import("build/llvm.zig").add(ctx);
// The backends at once: a program compiled by `ztrace cc`, the agent
// preloaded into it, the debugger stopping it at breakpoints. One
// recording holds the stops, the allocations and the program's own calls.
const together = b.addRunArtifact(exe);
together.addArgs(&.{ "record", "--by", "break", "-b", "push", "-b", "reverse", "--alloc", "--preload" });
together.addArtifactArg(agent);
together.addArg("-o");
const recording = together.addOutputFileArg("together.ztrace");
together.addArg("--");
together.addFileArg(instrumented_list);
together.expectStdErrMatch("exit 55");
const shown = b.addRunArtifact(exe);
shown.addArg("show");
shown.addFileArg(recording);
inline for (.{ "6 breakpoint", "5 alloc", "7 call", "7 ret", " variable", " line" }) |part| shown.expectStdOutMatch(part);
ctx.areaTest("test-together", "A debugger, the agent and instrumentation on one program").dependOn(&shown.step);
@import("build/kernel.zig").add(ctx);
}
|