summaryrefslogtreecommitdiff
path: root/src/cli/run.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli/run.zig')
-rw-r--r--src/cli/run.zig43
1 files changed, 40 insertions, 3 deletions
diff --git a/src/cli/run.zig b/src/cli/run.zig
index 2483a21..db3529e 100644
--- a/src/cli/run.zig
+++ b/src/cli/run.zig
@@ -15,12 +15,15 @@ pub const usage =
\\Runs PROGRAM with a shared-memory ring in its environment and records
\\what it emits (default out.ztrace). --preload puts a library (the agent,
\\libztrace-agent.so) into it with LD_PRELOAD. --ring sizes the ring (a
- \\power of two, default 16 MiB).
+ \\power of two, default 16M). --max-size stops the run when the recording
+ \\reaches that size (default 1G). Sizes take K, M or G.
\\
\\What the agent records, preloaded or linked into PROGRAM:
\\ --alloc malloc, free and their family: alloc, free
\\ --syscalls every syscall, with paths and written bytes:
\\ syscall_enter, syscall_exit
+ \\ --capture BYTES how much of a path or a written buffer to keep
+ \\ (default 256, at most 2048)
\\ --hook SYM|ADDR a function's entry (with registers) and return
\\ (with its value): call, ret
\\ --watch SYM|ADDR:LEN memory: the bytes that changed in it, before each
@@ -48,6 +51,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
var hooks: []const u8 = "";
var watches: []const u8 = "";
var cut: ?u64 = null;
+ var max_bytes: ?u64 = null;
var stops: Stops = .{ .io = init.io, .arena = arena };
var i: usize = 0;
while (i < args.len) : (i += 1) {
@@ -72,8 +76,12 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
} else if (std.mem.eql(u8, arg, "--preload")) {
options.preload = value;
} else if (std.mem.eql(u8, arg, "--ring")) {
- options.ring_bytes = std.fmt.parseInt(usize, value, 0) catch 0;
+ options.ring_bytes = sized(value);
if (!std.math.isPowerOfTwo(options.ring_bytes) or options.ring_bytes < 4096) fail("--ring needs a power of two, 4096 or more\n", .{});
+ } else if (std.mem.eql(u8, arg, "--max-size")) {
+ max_bytes = sized(value);
+ } else if (std.mem.eql(u8, arg, "--capture")) {
+ try env.append(arena, try std.fmt.allocPrintSentinel(arena, "ZTRACE_CAPTURE={d}", .{sized(value)}, 0));
} else if (std.mem.eql(u8, arg, "--hook")) {
hooks = try std.mem.concat(arena, u8, &.{ hooks, ",", value });
} else if (std.mem.eql(u8, arg, "--watch")) {
@@ -92,6 +100,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
var rec = try zt.Recorder.create(init.gpa, init.io, out);
// A hooked call and its return are the steps of a run recorded from inside.
rec.cut = cut orelse rec.cut | zt.Kind.call.bit() | zt.Kind.ret.bit();
+ rec.max_bytes = max_bytes orelse rec.max_bytes;
stops.rec = &rec;
if (hooks.len > 0) try env.append(arena, try std.fmt.allocPrintSentinel(arena, "ZTRACE_HOOKS={s}", .{hooks}, 0));
if (watches.len > 0) try env.append(arena, try std.fmt.allocPrintSentinel(arena, "ZTRACE_WATCH={s}", .{watches}, 0));
@@ -109,12 +118,27 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
stops.session = &session;
const start = zt.event.now();
- while (try session.pump(stops.sink(), 10 * std.time.ns_per_ms)) {
+ var full = false;
+ while (session.pump(stops.sink(), 10 * std.time.ns_per_ms) catch |err| switch (err) {
+ error.RecordingFull => full: {
+ full = true;
+ break :full false;
+ },
+ else => |e| return e,
+ }) {
// Everything pumped is recorded and read: whoever waits at it may go on.
if (stops.held) session.ring.resumeAll();
stops.held = false;
}
const ns = zt.event.now() - start;
+ if (full) {
+ // Nobody empties the ring any more: the target would wait at it for ever.
+ _ = linux.kill(session.pid, .KILL);
+ var status: u32 = 0;
+ _ = linux.waitpid(session.pid, @ptrCast(&status), 0);
+ session.exit_code = 128 + @as(u8, @intFromEnum(linux.SIG.KILL));
+ std.debug.print("{s}: full at {d} bytes, the target was killed. Record less (fewer kinds in --cut, less --watch, a smaller --capture) or raise --max-size.\n", .{ out, rec.max_bytes });
+ }
const events = rec.count;
const moments = rec.moments + @intFromBool(rec.delta.items.len > 0);
@@ -127,6 +151,19 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
size, @as(f64, @floatFromInt(ns)) / 1e6,
@as(f64, @floatFromInt(events)) / (@as(f64, @floatFromInt(ns)) / 1e9), session.exit_code.?,
});
+ if (full) std.process.exit(1);
+}
+
+/// "4096", "64K", "16M", "1G".
+fn sized(text: []const u8) u64 {
+ const shift: u6 = switch (if (text.len > 0) text[text.len - 1] else 0) {
+ 'K' => 10,
+ 'M' => 20,
+ 'G' => 30,
+ else => 0,
+ };
+ const number = std.fmt.parseInt(u64, text[0 .. text.len - @intFromBool(shift != 0)], 0) catch fail("{s}: not a size\n", .{text});
+ return number << shift;
}
/// "call,ret" as a set of `Kind.bit`s.