summaryrefslogtreecommitdiff
path: root/src/cli
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli')
-rw-r--r--src/cli/dbg.zig320
-rw-r--r--src/cli/main.zig2
-rw-r--r--src/cli/record.zig158
3 files changed, 422 insertions, 58 deletions
diff --git a/src/cli/dbg.zig b/src/cli/dbg.zig
new file mode 100644
index 0000000..007b790
--- /dev/null
+++ b/src/cli/dbg.zig
@@ -0,0 +1,320 @@
+//! ztrace dbg: a program under the debugger, a command a line, every stop a
+//! moment of a recording that the same commands can look back into.
+const std = @import("std");
+const zt = @import("ztrace");
+const util = @import("util.zig");
+const fail = util.fail;
+const Moment = zt.store.Moment;
+
+pub const usage =
+ \\usage: ztrace dbg [-o FILE] [--stack BYTES] [-s BYTES] [--max-size BYTES] -- PROGRAM [ARGS]
+ \\ ztrace dbg [-o FILE] [--stack BYTES] [-s BYTES] [--max-size BYTES] -p PID
+ \\
+ \\Starts PROGRAM stopped at its first instruction (or attaches to PID)
+ \\and reads commands, from a script when stdin is no terminal:
+ \\
+ \\ run, r, continue, c run to a breakpoint, a signal or the exit
+ \\ step, s run to the next source line
+ \\ stepi, si run one instruction
+ \\ break, b WHERE stop at a function, file:line or *address
+ \\ syscalls on|off|NAME stop at each syscall's entry and exit, or at one's
+ \\ watch WHAT [LEN] record a region at every stop: an address or a symbol
+ \\ regs the registers
+ \\ x WHAT [LEN] memory at an address, a symbol or $register (64 bytes)
+ \\ bt the call chain, by frame pointer
+ \\ back [N] look N moments back in the recording: regs, x
+ \\ forward [N] and bt then show the program as it was. The
+ \\ goto MOMENT program itself stays where it stopped last
+ \\ quit, q
+ \\
+ \\Every stop is a moment of the recording (default out.ztrace), its
+ \\number in brackets where it is printed. A moment has the registers,
+ \\the words that changed in the --stack bytes above the stack pointer
+ \\(default 4K, what bt needs) and in the watched regions, and at most -s
+ \\bytes (default 256) of what a syscall points at. The session ends when
+ \\the file reaches --max-size (default 1G).
+ \\
+;
+
+pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
+ const arena = init.arena.allocator();
+ var out: []const u8 = "out.ztrace";
+ var pid: ?std.os.linux.pid_t = null;
+ // Enough of the stack in every moment for `bt` to work there.
+ var stack_bytes: usize = 4096;
+ var capture: ?usize = null;
+ var max_bytes: ?u64 = null;
+ var i: usize = 0;
+ while (i < args.len) : (i += 1) {
+ const arg = args[i];
+ if (std.mem.eql(u8, arg, "--")) {
+ i += 1;
+ break;
+ } else if (std.mem.eql(u8, arg, "-o") and i + 1 < args.len) {
+ i += 1;
+ out = args[i];
+ } else if (std.mem.eql(u8, arg, "-p") and i + 1 < args.len) {
+ i += 1;
+ pid = std.fmt.parseInt(std.os.linux.pid_t, args[i], 10) catch fail("-p needs a process id\n", .{});
+ } else if ((std.mem.eql(u8, arg, "--stack") or std.mem.eql(u8, arg, "-s") or std.mem.eql(u8, arg, "--max-size")) and i + 1 < args.len) {
+ i += 1;
+ const size = std.fmt.parseIntSizeSuffix(args[i], 10) catch fail("{s} needs a size\n", .{arg});
+ if (std.mem.eql(u8, arg, "-s")) capture = size else if (std.mem.eql(u8, arg, "--stack")) stack_bytes = size else max_bytes = size;
+ } else fail(usage, .{});
+ }
+ if ((pid == null) == (i >= args.len)) fail(usage, .{});
+
+ var out_buf: [4096]u8 = undefined;
+ var stdout = std.Io.File.stdout().writer(init.io, &out_buf);
+ var s: Session = .{ .gpa = init.gpa, .io = init.io, .out = out, .w = &stdout.interface, .rec = try .create(init.gpa, init.io, out), .d = undefined };
+ // A moment a stop, syscall entries too: their line comes with them.
+ s.rec.cut = (s.rec.cut | zt.Kind.syscall_enter.bit()) & ~zt.Kind.line.bit();
+ if (max_bytes) |n| s.rec.max_bytes = n;
+ const sink: zt.Sink = .{ .ctx = &s, .emitFn = Session.emit };
+ s.d = if (pid) |p|
+ zt.Debugger.attach(init.gpa, init.io, p, sink) catch |err| fail("cannot attach to {d}: {t}\n", .{ p, err })
+ else
+ try zt.Debugger.spawn(init.gpa, init.io, try util.resolve(init, args[i]), try util.targetArgv(arena, args[i..]), init.minimal.environ.block.slice.ptr, sink);
+ defer s.d.deinit();
+ defer s.stop_payload.deinit(init.gpa);
+ s.d.stack_watch = stack_bytes;
+ if (capture) |n| s.d.capture = n;
+ try s.showStop();
+
+ var in_buf: [4096]u8 = undefined;
+ var stdin = std.Io.File.stdin().reader(init.io, &in_buf);
+ const interactive = std.Io.File.stdin().isTty(init.io) catch false;
+ while (true) {
+ try s.w.writeAll("(ztrace) ");
+ try s.w.flush();
+ const line = (stdin.interface.takeDelimiter('\n') catch break) orelse break;
+ // A script's commands show in its transcript, as a terminal echoes them.
+ if (!interactive) try s.w.print("{s}\n", .{line});
+ try s.w.flush();
+ const more = s.command(line) catch |err| switch (err) {
+ error.WriteFailed => return,
+ // Nothing more can be recorded, so nothing more is run.
+ error.RecordingFull => {
+ try s.w.print("{s} is full, the program stops here: record less (fewer stops and watches, a smaller --stack or -s) or raise --max-size\n", .{out});
+ break;
+ },
+ else => more: {
+ try s.w.print("error: {t}\n", .{err});
+ break :more true;
+ },
+ };
+ if (!more) break;
+ }
+ if (s.trace) |*t| t.close();
+ const moments = s.rec.moments;
+ try s.rec.close();
+ try s.w.print("{s}: {d} moments\n", .{ out, moments });
+ try s.w.flush();
+}
+
+const Session = struct {
+ gpa: std.mem.Allocator,
+ io: std.Io,
+ out: []const u8,
+ w: *std.Io.Writer,
+ rec: zt.Recorder,
+ d: zt.Debugger,
+ /// The moment looked at. Null is the last: the program as it is now.
+ view: ?u64 = null,
+ /// The recording, opened to look into it.
+ trace: ?zt.Trace = null,
+ /// The one syscall to stop at, when `syscalls NAME` asked.
+ only_syscall: u16 = 0,
+ /// The last stop as the debugger sent it: showing it reads no recording.
+ stop: zt.Event = .{ .kind = .step },
+ stop_payload: std.ArrayList(u8) = .empty,
+
+ /// The debugger's sink: the recorder, with the last stop kept on the way.
+ fn emit(ctx: *anyopaque, ev: *const zt.Event, payload: []const u8) anyerror!void {
+ const s: *Session = @ptrCast(@alignCast(ctx));
+ if (s.rec.cut & ev.kind.bit() != 0) {
+ s.stop = ev.*;
+ s.stop_payload.clearRetainingCapacity();
+ try s.stop_payload.appendSlice(s.gpa, payload);
+ }
+ return s.rec.emit(ev.*, payload);
+ }
+
+ /// The syscall hook of `syscalls NAME`: the others are recorded and run through.
+ fn onlySyscall(ctx: ?*anyopaque, _: *zt.Debugger, stop: zt.Debugger.Stop) anyerror!zt.Debugger.Action {
+ const only: *const u16 = @ptrCast(@alignCast(ctx.?));
+ return switch (stop) {
+ .syscall_enter, .syscall_exit => |nr| if (nr == only.*) .stop else .go,
+ else => .go,
+ };
+ }
+
+ /// Runs a command. False to quit.
+ fn command(s: *Session, line: []const u8) !bool {
+ var words = std.mem.tokenizeAny(u8, line, " \t\r");
+ const cmd = words.next() orelse return true;
+ const arg = words.next();
+ if (is(cmd, "quit", "q")) return false;
+ if (is(cmd, "run", "r") or is(cmd, "continue", "c") or is(cmd, "step", "s") or is(cmd, "stepi", "si")) {
+ if (s.d.done) return error.Exited;
+ // Running leaves the past: the program goes on from where it is.
+ s.view = null;
+ _ = try if (is(cmd, "step", "s")) s.d.step() else if (is(cmd, "stepi", "si")) s.d.stepi() else s.d.cont();
+ try s.showStop();
+ } else if (is(cmd, "break", "b")) {
+ const addr = try s.d.resolve(arg orelse return error.MissingArgument);
+ try s.w.print("breakpoint {d} at ", .{try s.d.breakAt(addr, null)});
+ try s.d.describe(s.w, addr);
+ try s.w.writeAll("\n");
+ } else if (is(cmd, "syscalls", "sys")) {
+ const which = arg orelse "on";
+ s.d.syscalls = if (std.mem.eql(u8, which, "off")) .ignore else .stop;
+ s.d.on_syscall = null;
+ if (s.d.syscalls == .stop and !std.mem.eql(u8, which, "on")) {
+ s.only_syscall = @intCast(@backingInt(std.meta.stringToEnum(std.os.linux.SYS, which) orelse return error.NoSuchSyscall));
+ s.d.on_syscall = .{ .ctx = &s.only_syscall, .func = onlySyscall };
+ }
+ } else if (is(cmd, "watch", "w")) {
+ const what = s.d.place(arg orelse return error.MissingArgument) orelse return error.NoSuchSymbol;
+ const len = if (words.next()) |n| try std.fmt.parseInt(usize, n, 0) else @max(what.size, 8);
+ try s.d.watch(what.addr, len);
+ try s.w.print("watching {d} bytes at {x}\n", .{ len, what.addr });
+ } else if (is(cmd, "regs", "reg")) {
+ const r = try s.regs(try s.moment());
+ inline for (zt.Regs.names, 0..) |name, n| {
+ try s.w.print(" {s:<6} {x:0>16}{s}", .{ name, @field(r, name), if (n % 3 == 2) "\n" else "" });
+ }
+ } else if (is(cmd, "x", "x")) {
+ const m = try s.moment();
+ const text = arg orelse return error.MissingArgument;
+ const addr = if (text[0] == '$')
+ (try s.regs(m)).named(text[1..]) orelse return error.NoSuchRegister
+ else
+ (s.d.place(text) orelse return error.NoSuchSymbol).addr;
+ const len = if (words.next()) |n| try std.fmt.parseInt(usize, n, 0) else 64;
+ var at: usize = 0;
+ while (at < len) : (at += 16) {
+ var bytes: [16]u8 = undefined;
+ var known: [16]bool = undefined;
+ const n = @min(16, len - at);
+ try s.fetch(m, addr + at, bytes[0..n], known[0..n]);
+ try s.w.print("{x:0>12} ", .{addr + at});
+ for (bytes[0..n], known[0..n]) |byte, k| {
+ if (k) try s.w.print(" {x:0>2}", .{byte}) else try s.w.writeAll(" ??");
+ }
+ try s.w.splatByteAll(' ', (16 - n) * 3 + 2);
+ for (bytes[0..n], known[0..n]) |byte, k| try s.w.writeByte(if (k and std.ascii.isPrint(byte)) byte else '.');
+ try s.w.writeAll("\n");
+ }
+ } else if (is(cmd, "bt", "backtrace")) {
+ const m = try s.moment();
+ const r = try s.regs(m);
+ const at_entry = if (s.d.syms.symbolAt(r.rip)) |sym| sym.addr == r.rip else false;
+ var frames: [64]u64 = undefined;
+ for (zt.Debugger.backtrace(Memory{ .s = s, .m = m }, &r, at_entry, &frames), 0..) |pc, n| {
+ try s.w.print("#{d} ", .{n});
+ try s.d.describe(s.w, pc);
+ try s.w.writeAll("\n");
+ }
+ } else if (is(cmd, "back", "forward") or is(cmd, "goto", "goto")) {
+ const t = try s.recording();
+ const n = if (arg) |a| try std.fmt.parseInt(u64, a, 10) else if (is(cmd, "goto", "goto")) return error.MissingArgument else 1;
+ const now = s.view orelse t.len - 1;
+ const to = if (std.mem.eql(u8, cmd, "back")) now -| n else if (std.mem.eql(u8, cmd, "forward")) @min(now +| n, t.len - 1) else n;
+ if (to >= t.len) return error.NoSuchMoment;
+ s.view = if (to == t.len - 1) null else to;
+ try s.show(to);
+ } else return error.UnknownCommand;
+ return true;
+ }
+
+ fn is(cmd: []const u8, long: []const u8, short: []const u8) bool {
+ return std.mem.eql(u8, cmd, long) or std.mem.eql(u8, cmd, short);
+ }
+
+ /// The recording with everything so far in it.
+ fn recording(s: *Session) !*zt.Trace {
+ try s.rec.flush();
+ if (s.trace) |*t| t.close();
+ s.trace = null;
+ s.trace = try zt.Trace.open(s.gpa, s.io, s.out);
+ return &s.trace.?;
+ }
+
+ /// The moment commands look at: null for the running program itself.
+ fn moment(s: *Session) !?Moment {
+ if (s.view == null and !s.d.done) return null;
+ const t = try s.recording();
+ return try t.moment(s.view orelse t.len - 1);
+ }
+
+ /// The thread a moment stopped in, and its registers then.
+ fn regs(s: *Session, m: ?Moment) !zt.Regs {
+ const past = m orelse return s.d.regs;
+ var events = try past.events(s.gpa);
+ defer events.deinit(s.gpa);
+ var tid: u32 = @intCast(s.d.proc.pid);
+ while (events.next()) |item| tid = item.ev.tid;
+ return (try past.regs(tid)) orelse error.NoRegisters;
+ }
+
+ /// Memory as of a moment, or the program's: `known` tells the bytes there are.
+ fn fetch(s: *Session, m: ?Moment, addr: u64, buf: []u8, known: []bool) !void {
+ if (m) |past| return past.read(addr, buf, known);
+ const n = s.d.read(addr, buf);
+ @memset(known[0..n], true);
+ @memset(known[n..], false);
+ }
+
+ /// What `Debugger.backtrace` reads frames from.
+ const Memory = struct {
+ s: *Session,
+ m: ?Moment,
+
+ pub fn read(mem: Memory, addr: u64, buf: []u8) usize {
+ var known: [16]bool = undefined;
+ mem.s.fetch(mem.m, addr, buf, known[0..buf.len]) catch return 0;
+ return std.mem.indexOfScalar(bool, known[0..buf.len], false) orelse buf.len;
+ }
+ };
+
+ /// The stop the program is at now.
+ fn showStop(s: *Session) !void {
+ return s.print(s.rec.moments - 1, &s.stop, s.stop_payload.items, if (s.d.done) null else s.d.regs.rip);
+ }
+
+ /// The stop that ended a moment of the recording.
+ fn show(s: *Session, index: u64) !void {
+ const m = try (try s.recording()).moment(index);
+ var events = try m.events(s.gpa);
+ defer events.deinit(s.gpa);
+ var last: ?zt.store.Events.Item = null;
+ while (events.next()) |item| last = item;
+ const stop = last orelse return;
+ return s.print(index, &stop.ev, stop.payload, if (try m.regs(stop.ev.tid)) |r| r.rip else null);
+ }
+
+ /// A stop's line: its moment, what it was, and where.
+ fn print(s: *Session, index: u64, ev: *const zt.Event, payload: []const u8, pc: ?u64) !void {
+ try s.w.print("[{d}] ", .{index});
+ switch (ev.kind) {
+ .exit => return s.w.print("exited {d}\n", .{ev.a[0]}),
+ .breakpoint => try s.w.print("breakpoint {d}", .{ev.a[1]}),
+ .signal => try s.w.print("signal {t}", .{@as(std.os.linux.SIG, @fromBackingInt(@intCast(ev.aux)))}),
+ .syscall_enter => {
+ try s.w.writeAll("syscall ");
+ try zt.syscalls.formatEnter(s.w, ev, payload);
+ },
+ .syscall_exit => {
+ try s.w.print("syscall {s}", .{zt.syscalls.name(ev.aux) orelse "?"});
+ try zt.syscalls.formatExit(s.w, ev);
+ },
+ else => try s.w.print("{t}", .{ev.kind}),
+ }
+ if (pc) |at| {
+ try s.w.writeAll(" at ");
+ try s.d.describe(s.w, at);
+ }
+ try s.w.writeAll("\n");
+ }
+};
diff --git a/src/cli/main.zig b/src/cli/main.zig
index 3abea6e..72b894a 100644
--- a/src/cli/main.zig
+++ b/src/cli/main.zig
@@ -5,6 +5,7 @@ const util = @import("util.zig");
/// A command is a file with `usage` and `run(init, args)`.
const commands = .{
.record = @import("record.zig"),
+ .dbg = @import("dbg.zig"),
.run = @import("run.zig"),
.dump = @import("dump.zig"),
.show = @import("show.zig"),
@@ -14,6 +15,7 @@ const usage =
\\usage: ztrace COMMAND ...
\\
\\ record run a program under ptrace into a recording
+ \\ dbg debug a program, recording it: step, break, look, go back
\\ run record a program that carries a ring producer (agent, instrumentation)
\\ dump print a recording's events
\\ show print a recording's summary, or the program's state at a moment
diff --git a/src/cli/record.zig b/src/cli/record.zig
index ab617d4..7b44522 100644
--- a/src/cli/record.zig
+++ b/src/cli/record.zig
@@ -1,16 +1,31 @@
-//! ztrace record: a program under ptrace, every stop a moment.
+//! ztrace record: a program under the debugger, left to run, every stop a
+//! moment of the recording.
const std = @import("std");
const zt = @import("ztrace");
const util = @import("util.zig");
const fail = util.fail;
pub const usage =
- \\usage: ztrace record [-o FILE] [--steps N] [--syscalls] [--stack BYTES] -- PROGRAM [ARGS]
+ \\usage: ztrace record [-o FILE] [--by insn|line|syscall|break] [-b WHERE]... [-w WHAT[,LEN]]...
+ \\ [--stack BYTES] [-s BYTES] [--steps N] [--max-size BYTES] [--strace]
+ \\ -- PROGRAM [ARGS]
\\
- \\Runs PROGRAM under ptrace into a recording (default out.ztrace): every
- \\instruction, or with --syscalls every syscall. --steps stops after N
- \\moments. --stack records that many bytes above the stack pointer at each
- \\stop (default 128).
+ \\Runs PROGRAM under ptrace into a recording (default out.ztrace). --by
+ \\says where it stops, each stop a moment:
+ \\ insn every instruction (the default)
+ \\ line every source line, from the program's entry (its loader is run through)
+ \\ syscall every syscall (--syscalls is the same)
+ \\ break the breakpoints only
+ \\-b adds a breakpoint, in any of them: a function, file:line or *address.
+ \\Syscalls are recorded whatever the stops are, with at most -s bytes
+ \\(default 256) of the path or the buffer each points at; --strace also
+ \\prints them, a line each.
+ \\Of memory, only what is asked for is recorded, and of that the words
+ \\that changed since the stop before: the --stack bytes above the stack
+ \\pointer (default 128) and each -w region, an address or a symbol, as
+ \\long as the symbol unless LEN says.
+ \\--steps ends the recording after N moments, and --max-size when the
+ \\file is that large (default 1G). Sizes take a suffix: 64K, 4G.
\\
;
@@ -18,95 +33,122 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
const arena = init.arena.allocator();
var out: []const u8 = "out.ztrace";
var max_moments: u64 = std.math.maxInt(u64);
- var by_syscall = false;
+ var by: enum { insn, line, syscall, @"break" } = .insn;
var stack_bytes: usize = 128;
+ var capture: ?usize = null;
+ var max_bytes: ?u64 = null;
+ var strace = false;
+ var breaks: std.ArrayList([]const u8) = .empty;
+ var watches: std.ArrayList([]const u8) = .empty;
var i: usize = 0;
while (i < args.len) : (i += 1) {
const arg = args[i];
+ const value: ?[]const u8 = if (i + 1 < args.len) args[i + 1] else null;
if (std.mem.eql(u8, arg, "--")) {
i += 1;
break;
- } else if (std.mem.eql(u8, arg, "-o") and i + 1 < args.len) {
+ } else if (std.mem.eql(u8, arg, "--syscalls")) {
+ by = .syscall;
+ } else if (std.mem.eql(u8, arg, "--strace")) {
+ strace = true;
+ } else if (value == null) {
+ fail(usage, .{});
+ } else if (std.mem.eql(u8, arg, "-o")) {
+ out = value.?;
i += 1;
- out = args[i];
- } else if (std.mem.eql(u8, arg, "--steps") and i + 1 < args.len) {
+ } else if (std.mem.eql(u8, arg, "--by")) {
+ by = std.meta.stringToEnum(@TypeOf(by), value.?) orelse fail("--by needs insn, line, syscall or break\n", .{});
i += 1;
- max_moments = std.fmt.parseInt(u64, args[i], 0) catch fail("--steps needs a number\n", .{});
- } else if (std.mem.eql(u8, arg, "--stack") and i + 1 < args.len) {
+ } else if (std.mem.eql(u8, arg, "-b")) {
+ try breaks.append(arena, value.?);
+ i += 1;
+ } else if (std.mem.eql(u8, arg, "-w")) {
+ try watches.append(arena, value.?);
+ i += 1;
+ } else if (std.mem.eql(u8, arg, "--steps")) {
+ max_moments = std.fmt.parseInt(u64, value.?, 0) catch fail("--steps needs a number\n", .{});
+ i += 1;
+ } else if (std.mem.eql(u8, arg, "--stack") or std.mem.eql(u8, arg, "-s") or std.mem.eql(u8, arg, "--max-size")) {
+ const size = std.fmt.parseIntSizeSuffix(value.?, 10) catch fail("{s} needs a size\n", .{arg});
+ if (std.mem.eql(u8, arg, "-s")) capture = size else if (std.mem.eql(u8, arg, "--stack")) stack_bytes = size else max_bytes = size;
i += 1;
- stack_bytes = std.fmt.parseInt(usize, args[i], 0) catch fail("--stack needs a number\n", .{});
- } else if (std.mem.eql(u8, arg, "--syscalls")) {
- by_syscall = true;
} else fail(usage, .{});
}
if (i >= args.len) fail(usage, .{});
const path = try util.resolve(init, args[i]);
const argv = try util.targetArgv(arena, args[i..]);
- var proc = try zt.ptrace.Process.spawn(path, argv, init.minimal.environ.block.slice.ptr);
- errdefer proc.kill();
-
var rec = try zt.Recorder.create(init.gpa, init.io, out);
- const tid: u32 = @intCast(proc.pid);
- const stack = try arena.alloc(u8, stack_bytes);
- const stack_seen = try arena.alloc(u8, stack_bytes);
- var stack_at: u64 = 0;
+ // A stop is a moment: the line it is at comes with it, not as one more.
+ rec.cut &= ~zt.Kind.line.bit();
+ if (max_bytes) |n| rec.max_bytes = n;
+ var stderr_buf: [4096]u8 = undefined;
+ var stderr = std.Io.File.stderr().writer(init.io, &stderr_buf);
+ var printer: zt.syscalls.Printer = .{ .w = &stderr.interface, .next = rec.sink() };
+
+ var d = try zt.Debugger.spawn(init.gpa, init.io, path, argv, init.minimal.environ.block.slice.ptr, if (strace) printer.sink() else rec.sink());
+ defer d.deinit();
+ d.syscalls = if (by == .syscall) .stop else .record;
+ d.stack_watch = stack_bytes;
+ if (capture) |n| d.capture = n;
+ for (breaks.items) |where| _ = try d.breakAt(d.resolve(where) catch fail("{s}: no such place in the program\n", .{where}), null);
+ for (watches.items) |spec| {
+ const comma = std.mem.indexOfScalar(u8, spec, ',') orelse spec.len;
+ const what = d.place(spec[0..comma]) orelse fail("{s}: no such symbol\n", .{spec[0..comma]});
+ const len = if (comma < spec.len) std.fmt.parseInt(usize, spec[comma + 1 ..], 0) catch fail("{s}: the length is no number\n", .{spec}) else @max(what.size, 8);
+ try d.watch(what.addr, len);
+ }
+
+ // By line, the loader's hundred thousand instructions are run through
+ // to a breakpoint at the entry: there are no lines in them.
+ var entry: ?u32 = null;
+ if (by == .line) if (d.syms.entryPoint()) |at| if (at != d.regs.rip) {
+ entry = try d.breakAt(at, null);
+ };
var timer = zt.event.now();
var exit_code: u64 = 0;
+ var full = false;
while (rec.moments < max_moments) {
- const stop = if (by_syscall) try proc.syscall() else try proc.stepi();
+ const stop = (if (entry) |id| stop: {
+ defer d.clear(id);
+ entry = null;
+ break :stop d.cont();
+ } else switch (by) {
+ .insn => d.stepi(),
+ .line => d.step(),
+ .syscall, .@"break" => d.cont(),
+ }) catch |err| switch (err) {
+ error.RecordingFull => {
+ full = true;
+ break;
+ },
+ else => |e| return e,
+ };
switch (stop) {
.exited => |code| {
exit_code = code;
- try rec.emit(.{ .ts = zt.event.now(), .tid = tid, .kind = .exit, .a = .{ code, 0, 0, 0, 0, 0 } }, "");
break;
},
.killed => |sig| {
- exit_code = 128 + @as(u64, @intFromEnum(sig));
- try rec.emit(.{ .ts = zt.event.now(), .tid = tid, .kind = .exit, .a = .{ exit_code, 0, 0, 0, 0, 0 } }, "");
+ exit_code = 128 + @as(u64, @backingInt(sig));
break;
},
- .signal => |sig| try rec.emit(.{ .ts = zt.event.now(), .tid = tid, .kind = .signal, .aux = @intCast(@intFromEnum(sig)) }, ""),
- .trap, .syscall => {
- const regs = try proc.regs();
- // The stack above rsp: only the bytes that changed since the last stop.
- const n = proc.read(regs.rsp, stack);
- if (regs.rsp != stack_at) @memset(stack_seen, 0);
- var from: usize = 0;
- while (from < n) {
- if (regs.rsp == stack_at and stack[from] == stack_seen[from]) {
- from += 1;
- continue;
- }
- var to = from + 1;
- while (to < n and (regs.rsp != stack_at or stack[to] != stack_seen[to])) to += 1;
- try rec.emit(.{ .ts = zt.event.now(), .tid = tid, .kind = .mem_write, .a = .{ regs.rsp + from, to - from, regs.rip, 0, 0, 0 } }, stack[from..to]);
- from = to;
- }
- @memcpy(stack_seen[0..n], stack[0..n]);
- stack_at = regs.rsp;
-
- const ev: zt.Event = if (stop == .trap)
- .{ .ts = zt.event.now(), .tid = tid, .kind = .step, .a = .{ regs.rip, 0, 0, 0, 0, 0 } }
- else if (proc.in_syscall)
- .{ .ts = zt.event.now(), .tid = tid, .kind = .syscall_enter, .aux = @truncate(regs.orig_rax), .a = .{ regs.rdi, regs.rsi, regs.rdx, regs.r10, regs.r8, regs.r9 } }
- else
- .{ .ts = zt.event.now(), .tid = tid, .kind = .syscall_exit, .aux = @truncate(regs.orig_rax), .a = .{ regs.rax, 0, 0, 0, 0, 0 } };
- try rec.emit(ev, std.mem.asBytes(&regs));
- },
+ // A signal is delivered by the next resume.
+ else => {},
}
- } else proc.kill();
+ }
timer = zt.event.now() - timer;
const moments = rec.moments + @intFromBool(rec.delta.items.len > 0);
const events = rec.count;
try rec.close();
const size = (try std.Io.Dir.cwd().statFile(init.io, out, .{})).size;
+ if (full) std.debug.print("{s}: full at {d} bytes, the program stopped there: record less (--by line, syscall or break; fewer -w; a smaller --stack or -s) or raise --max-size\n", .{ out, size });
std.debug.print("{s}: {d} moments, {d} events, {d} bytes ({d} bytes/moment), {d:.1} ms, {d:.0} moments/s, exit {d}\n", .{
- out, moments,
- events, size,
- size / @max(moments, 1), @as(f64, @floatFromInt(timer)) / 1e6,
+ out, moments,
+ events, size,
+ size / @max(moments, 1), @as(f64, @floatFromInt(timer)) / 1e6,
@as(f64, @floatFromInt(moments)) / (@as(f64, @floatFromInt(timer)) / 1e9), exit_code,
});
}