summaryrefslogtreecommitdiff
path: root/src/trace
diff options
context:
space:
mode:
Diffstat (limited to 'src/trace')
-rw-r--r--src/trace/bpf.zig5
-rw-r--r--src/trace/kernel_test.zig11
-rw-r--r--src/trace/perf.zig39
3 files changed, 16 insertions, 39 deletions
diff --git a/src/trace/bpf.zig b/src/trace/bpf.zig
index 5dad4c6..a550419 100644
--- a/src/trace/bpf.zig
+++ b/src/trace/bpf.zig
@@ -482,7 +482,10 @@ pub const Tracer = struct {
}
/// A static probe of the file `path` (`p` from `usdt.probes` with no
- /// bias). The kernel raises its semaphore while we are attached.
+ /// bias). The kernel raises its semaphore in every process that maps
+ /// the file, while we are attached; where the same page of the file is
+ /// mapped writable twice it raises the wrong word, so for a target at
+ /// hand write the semaphore yourself and pass the probe without one.
pub fn usdtProbe(t: *Tracer, elf: usdt.Elf, path: [*:0]const u8, p: usdt.Probe, id: u64) !void {
const semaphore = if (p.semaphore == 0) 0 else elf.offset(p.semaphore) orelse return error.NoSuchProbe;
try t.attach("uprobe", path, elf.offset(p.pc) orelse return error.NoSuchProbe, semaphore, try usdtProgram(t.maps, @intCast(t.pid), id, p.args));
diff --git a/src/trace/kernel_test.zig b/src/trace/kernel_test.zig
index 1905771..8277e34 100644
--- a/src/trace/kernel_test.zig
+++ b/src/trace/kernel_test.zig
@@ -60,17 +60,20 @@ fn record(log: *Log, path: []const u8, args: []const []const u8, options: perf.O
const envp = [_:null]?[*:0]const u8{};
var proc = try zt.ptrace.Process.spawn(argv[0].?, argv, &envp);
- errdefer proc.kill();
+ errdefer proc.close();
var session = perf.Session.open(gpa, proc.pid, options, log.sink()) catch |err| switch (err) {
error.PerfAccess, error.PerfNoSuchEvent => {
- proc.kill();
+ proc.close();
return error.SkipZigTest;
},
else => |e| return e,
};
defer session.close();
+ var symbols: zt.Symbols = .{ .gpa = gpa, .io = std.testing.io };
+ defer symbols.deinit();
+ symbols.free(try symbols.refresh(proc.pid));
for (armed) |a| {
- const sym = (try perf.lookup(gpa, proc.pid, a.symbol)).?;
+ const sym = symbols.find(a.symbol).?;
try session.watch(.{ .addr = sym.addr, .len = @intCast(@min(sym.size, 8)), .mode = a.mode, .name = a.symbol });
}
perf.release(proc);
@@ -297,7 +300,7 @@ test "usdt: the notes say what readelf says; arguments are fetched at a probe" {
const argv = [_:null]?[*:0]const u8{ path, "4", null };
const envp = [_:null]?[*:0]const u8{};
var proc = try zt.ptrace.Process.spawn(path, &argv, &envp);
- errdefer proc.kill();
+ errdefer proc.close();
const maps = try perf.readMaps(gpa, proc.pid);
defer gpa.free(maps);
// The program itself is the first thing mapped.
diff --git a/src/trace/perf.zig b/src/trace/perf.zig
index 36eaaf8..962996a 100644
--- a/src/trace/perf.zig
+++ b/src/trace/perf.zig
@@ -21,7 +21,6 @@ const std = @import("std");
const linux = std.os.linux;
const event = @import("event.zig");
const ptrace = @import("ptrace.zig");
-const usdt = @import("usdt.zig");
const Event = event.Event;
const Regs = event.Regs;
const PERF = linux.PERF;
@@ -31,12 +30,6 @@ pub const reading: u16 = 1;
/// `aux` of a `fault` that needed I/O.
pub const fault_major: u16 = 1;
-/// The id a `string` event gives `text`: its hash, so that backends agree
-/// without a registry.
-pub fn stringId(text: []const u8) u64 {
- return std.hash.Wyhash.hash(0, text);
-}
-
/// What can be counted. Context switches and migrations are missing: their
/// software events fire in kernel mode, which paranoid 2 excludes, so they
/// always read 0 (`Options.switches` gives the switches as events instead).
@@ -236,7 +229,7 @@ pub const Session = struct {
var attr: linux.perf_event_attr = .{ .type = .BREAKPOINT, .sample_period_or_freq = 1, .bp_type = @backingInt(w.mode), .config1 = w.addr, .config2 = if (w.mode == .exec) 8 else w.len };
// Where it executes is known; what it was called with is not.
if (w.mode == .exec) attr.sample_type = PERF.SAMPLE.REGS_USER;
- try s.add(attr, .{ .watch = .{ .len = w.len, .mode = w.mode, .name = stringId(w.name) } });
+ try s.add(attr, .{ .watch = .{ .len = w.len, .mode = w.mode, .name = event.stringId(w.name) } });
if (w.mode == .exec) try s.name(w.name);
}
@@ -298,7 +291,7 @@ pub const Session = struct {
}
fn name(s: *Session, text: []const u8) !void {
- try s.sink.emit(.{ .ts = event.now(), .tid = @intCast(s.pid), .kind = .string, .a = .{ stringId(text), 0, 0, 0, 0, 0 } }, text);
+ try s.sink.emit(.{ .ts = event.now(), .tid = @intCast(s.pid), .kind = .string, .a = .{ event.stringId(text), 0, 0, 0, 0, 0 } }, text);
}
/// The mappings the target has now, from /proc: the kernel only reports
@@ -323,7 +316,7 @@ pub const Session = struct {
if (linux.read(fd, @ptrCast(&v), 24) != 24) continue;
// A counter that had to share its hardware counted part of the time: scale it.
count.* = if (v[2] != 0 and v[2] < v[1]) @intCast(@as(u128, v[0]) * v[1] / v[2]) else v[0];
- try s.sink.emit(.{ .ts = ts, .tid = @intCast(s.pid), .kind = .sample, .aux = reading, .a = .{ 0, stringId(@tagName(c)), count.*, 0, 0, 0 } }, "");
+ try s.sink.emit(.{ .ts = ts, .tid = @intCast(s.pid), .kind = .sample, .aux = reading, .a = .{ 0, event.stringId(@tagName(c)), count.*, 0, 0, 0 } }, "");
}
}
@@ -417,7 +410,7 @@ pub const Session = struct {
// The counter overflowed on the way into the kernel: not a place in the program.
if (pc >> 63 != 0) return;
ev.kind = .sample;
- ev.a = .{ pc, stringId("cycles"), word(body, 48), addr, 0, 0 };
+ ev.a = .{ pc, event.stringId("cycles"), word(body, 48), addr, 0, 0 };
var chain: []const u8 = "";
if (s.options.callchain) {
// The first entry says "user space from here", not a pc.
@@ -439,7 +432,7 @@ pub const Session = struct {
.write => {
// The value now: the target has run on since it wrote.
var bytes: [8]u8 = undefined;
- const n = (ptrace.Process{ .pid = s.pid }).read(addr, bytes[0..w.len]);
+ const n = (ptrace.Process{ .pid = s.pid, .tid = s.pid }).read(addr, bytes[0..w.len]);
ev.kind = .mem_write;
ev.a = .{ addr, w.len, pc, 0, 0, 0 };
try s.sink.emit(ev, bytes[0..n]);
@@ -573,28 +566,6 @@ pub fn release(p: ptrace.Process) void {
_ = linux.ptrace(linux.PTRACE.DETACH, p.pid, 0, 0, 0);
}
-pub const Symbol = struct { addr: u64, size: u64 };
-
-/// Where `pid` has the symbol `name` now, looking through every ELF file
-/// it has mapped, in address order. Shared libraries are there once the
-/// dynamic loader has run, not at the first instruction.
-pub fn lookup(gpa: std.mem.Allocator, pid: linux.pid_t, name: []const u8) !?Symbol {
- const text = try readMaps(gpa, pid);
- defer gpa.free(text);
- var lines = std.mem.tokenizeScalar(u8, text, '\n');
- while (lines.next()) |line| {
- const m = parseMap(line) orelse continue;
- if (m.offset != 0 or m.path.len == 0 or m.path[0] != '/') continue;
- const path = try gpa.dupeSentinel(u8, m.path, 0);
- defer gpa.free(path);
- const elf = usdt.Elf.open(path) catch continue;
- defer elf.close();
- const sym = elf.symbol(name) orelse continue;
- return .{ .addr = sym.value +% elf.bias(m.start), .size = sym.size };
- }
- return null;
-}
-
/// /proc/PID/maps, whole. Free with `gpa`.
pub fn readMaps(gpa: std.mem.Allocator, pid: linux.pid_t) ![]u8 {
var path_buf: [64]u8 = undefined;