summaryrefslogtreecommitdiff
path: root/src/cli/perf.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 12:23:33 -0300
committerGabriel Schneider <[email protected]>2026-11-10 12:23:41 -0300
commit8008a2655c0f0c9b9890fb4866f774c9b50e170c (patch)
treeec54af73611e686751dd74dd3f4a585198a91de5 /src/cli/perf.zig
parent2e01933dc5c0c1903c1b7f86e453829fee9a541b (diff)
downloadcomputeranim-8008a2655c0f0c9b9890fb4866f774c9b50e170c.tar.gz
computeranim-8008a2655c0f0c9b9890fb4866f774c9b50e170c.zip
kernel: on today's main
Symbols come from Symbols (perf.lookup is gone), string ids from event.stringId, and ptrace.Process is used as it is now. A USDT semaphore of the program itself is raised by the tracer: the kernel's counter landed on the wrong word of a small lld-linked target, whose page of the file is mapped writable twice. test-kernel also runs the library tests of perf, usdt and bpf. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'src/cli/perf.zig')
-rw-r--r--src/cli/perf.zig53
1 files changed, 24 insertions, 29 deletions
diff --git a/src/cli/perf.zig b/src/cli/perf.zig
index 58f7d77..ea211fe 100644
--- a/src/cli/perf.zig
+++ b/src/cli/perf.zig
@@ -115,7 +115,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
const argv = try util.targetArgv(arena, args[i..]);
// Held at its first instruction until the events are on it.
var proc = try zt.ptrace.Process.spawn(path, argv, init.minimal.environ.block.slice.ptr);
- errdefer proc.kill();
+ errdefer proc.close();
var rec = try zt.Recorder.create(init.gpa, init.io, out);
if (max_bytes) |bytes| rec.max_bytes = bytes;
// What changes the state a moment holds (registers, memory) ends one.
@@ -127,19 +127,26 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
};
defer session.close();
+ // Symbols: the program's own from the start, a library's once the
+ // loader has mapped it, which is at the program's entry point.
+ var symbols: zt.Symbols = .{ .gpa = init.gpa, .io = init.io };
+ defer symbols.deinit();
+ symbols.free(try symbols.refresh(proc.pid));
var at_entry = false;
for (specs.items) |spec| {
- const sym: zt.perf.Symbol = if (std.fmt.parseInt(u64, spec.what, 0)) |addr| .{ .addr = addr, .size = 8 } else |_| found: {
- if (try zt.perf.lookup(init.gpa, proc.pid, spec.what)) |sym| break :found sym;
- // A library's symbol: there once the loader has mapped it.
- if (!at_entry) try runToEntry(&proc);
- at_entry = true;
- break :found try zt.perf.lookup(init.gpa, proc.pid, spec.what) orelse fail("{s}: no such symbol in what the program has mapped\n", .{spec.what});
+ const addr, const size = if (std.fmt.parseInt(u64, spec.what, 0)) |addr| .{ addr, 8 } else |_| found: {
+ if (symbols.find(spec.what) == null and !at_entry) {
+ try runTo(&proc, symbols.entryPoint() orelse fail("{s}: no symbols to be had from the program\n", .{spec.what}));
+ at_entry = true;
+ symbols.free(try symbols.refresh(proc.pid));
+ }
+ const sym = symbols.find(spec.what) orelse fail("{s}: no such symbol in what the program has mapped\n", .{spec.what});
+ break :found .{ sym.addr, sym.size };
};
- const len = spec.len orelse if (std.mem.indexOfScalar(u64, &.{ 1, 2, 4 }, sym.size)) |_| @as(u8, @intCast(sym.size)) else 8;
- session.watch(.{ .addr = sym.addr, .len = len, .mode = spec.mode, .name = spec.what }) catch |err| switch (err) {
+ const len = spec.len orelse if (std.mem.indexOfScalar(u64, &.{ 1, 2, 4 }, size)) |_| @as(u8, @intCast(size)) else 8;
+ session.watch(.{ .addr = addr, .len = len, .mode = spec.mode, .name = spec.what }) catch |err| switch (err) {
error.NoFreeBreakpoint => fail("{s}: no debug register left, there are four\n", .{spec.what}),
- error.PerfNoSuchEvent => fail("{s}: a watchpoint covers 1, 2, 4 or 8 bytes at an address aligned to that, not {d} at {x}\n", .{ spec.what, len, sym.addr }),
+ error.PerfNoSuchEvent => fail("{s}: a watchpoint covers 1, 2, 4 or 8 bytes at an address aligned to that, not {d} at {x}\n", .{ spec.what, len, addr }),
else => |e| return e,
};
}
@@ -150,7 +157,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
error.RecordingFull => null,
else => |e| return e,
};
- if (exit_code == null) proc.kill();
+ if (exit_code == null) proc.close();
const ns = zt.event.now() - start;
try finish(init, &rec, out, exit_code, session.lost, ns, options.counters, session.counts);
}
@@ -202,30 +209,18 @@ pub fn finish(init: std.process.Init, rec: *zt.Recorder, out: []const u8, exit_c
});
}
-/// Runs the held target up to its own entry point, where the dynamic
-/// loader is done and its libraries are mapped.
-fn runToEntry(proc: *zt.ptrace.Process) !void {
- var path_buf: [64]u8 = undefined;
- const fd = linux.open(try std.fmt.bufPrintSentinel(&path_buf, "/proc/{d}/auxv", .{proc.pid}, 0), .{}, 0);
- if (linux.errno(fd) != .SUCCESS) return error.NoSuchProcess;
- defer _ = linux.close(@intCast(fd));
- var auxv: [64][2]u64 = undefined;
- const n = linux.read(@intCast(fd), @ptrCast(&auxv), @sizeOf(@TypeOf(auxv)));
- if (linux.errno(n) != .SUCCESS) return error.NoSuchProcess;
- const entry = for (auxv[0 .. n / 16]) |pair| {
- if (pair[0] == std.elf.AT.ENTRY) break pair[1];
- } else return error.NoEntryPoint;
-
+/// Runs the held target up to `addr`, through a breakpoint there.
+fn runTo(proc: *zt.ptrace.Process, addr: u64) !void {
var saved: [1]u8 = undefined;
- if (proc.read(entry, &saved) != 1) return error.BadAddress;
- try proc.write(entry, &.{0xcc});
+ if (proc.read(addr, &saved) != 1) return error.BadAddress;
+ try proc.write(addr, &.{0xcc});
while (true) switch (try proc.cont()) {
.trap => break,
.exited, .killed => fail("the program ended before its entry point\n", .{}),
else => {},
};
- try proc.write(entry, &saved);
+ try proc.write(addr, &saved);
var regs = try proc.regs();
- regs.rip = entry;
+ regs.rip = addr;
try proc.setRegs(&regs);
}