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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
|
//! A recording on disk, in xitdb: a list of immutable moments. Each moment
//! is the whole state of the program then, sharing everything unchanged with
//! the moment before, plus the events that led to it. Time travel is an
//! index.
//!
//! root = map { moments: list of moment }
//! moment = map {
//! delta the events of this moment: (u32 payload length, Event, payload)*
//! count events up to and including this moment
//! threads the set of tids seen
//! tid, regs / line / stack a thread's registers, source line, open calls
//! mem sorted map, big-endian (addr / 64) -> Chunk: the known bytes
//! heap sorted map, big-endian ptr -> Allocation: the live allocations
//! vars sorted map, big-endian addr -> Variable: the live named variables
//! maps sorted map, big-endian addr -> Mapping, path: the mapped regions
//! strings map id -> text
//! }
//!
//! The `Recorder` is a `Sink`: it takes events from any backend, folds them
//! into that state, and ends a moment at the kinds in `cut`. Moments are
//! written a batch a transaction, each frozen before the next begins.
const std = @import("std");
const xitdb = @import("xitdb");
const event = @import("event.zig");
const Event = event.Event;
const Regs = event.Regs;
const Kind = event.Kind;
const WriteDB = xitdb.Database(.buffered_file, u64);
const ReadDB = xitdb.Database(.file, u64);
const Key = enum(u64) { moments = 1, delta, count, threads, mem, heap, vars, maps, strings };
fn key(k: Key) u64 {
return @intFromEnum(k);
}
/// A thread's fields sit in the moment's own map, under the tid and a low
/// nibble no `Key` has: one block to copy when a register changes, not three.
const Field = enum(u64) { regs = 13, line, stack };
fn threadKey(tid: u64, f: Field) u64 {
return tid << 4 | @intFromEnum(f);
}
fn be(value: u64) [8]u8 {
var out: [8]u8 = undefined;
std.mem.writeInt(u64, &out, value, .big);
return out;
}
pub const chunk_bytes = 64;
const Chunk = extern struct {
/// Bit i set: byte i was recorded.
known: u64 = 0,
bytes: [chunk_bytes]u8 = @splat(0),
};
/// Where a thread is in its source.
pub const Line = extern struct { file: u64 = 0, line: u32 = 0, column: u32 = 0 };
/// A call not yet returned from.
pub const Frame = extern struct { function: u64, site: u64, sp: u64 };
pub const Allocation = extern struct {
ptr: u64,
size: u64,
/// The caller of malloc.
pc: u64,
/// The number of the `alloc` event, from 1.
born: u64,
};
pub const Variable = extern struct {
addr: u64,
size: u64,
/// String ids: resolve with `Moment.string`.
name: u64,
type: u64,
tid: u32,
/// Calls deep when it was declared; 0 for a global.
depth: u32,
};
pub const Mapping = extern struct { addr: u64, len: u64, prot: u64, offset: u64 };
/// What a moment changed, kept until its batch is written.
const Op = enum(u8) { delta, thread, regs, line, push, pop, chunk, heap_put, heap_del, var_put, var_del, map_put, map_del, string, end };
pub const Recorder = struct {
gpa: std.mem.Allocator,
io: std.Io,
file: std.Io.File,
/// Its own allocation: the database points at it.
buffer: *std.Io.Writer.Allocating,
db: WriteDB,
/// A moment ends after an event of one of these kinds.
cut: u64 = Kind.step.bit() | Kind.breakpoint.bit() | Kind.syscall_exit.bit() | Kind.signal.bit() | Kind.exit.bit() | Kind.line.bit() | Kind.mark.bit(),
/// ... or when its events pass this many bytes.
max_delta: usize = 1 << 20,
/// Moments a transaction. A crash loses the batch being gathered.
batch: usize = 256,
delta: std.ArrayList(u8) = .empty,
count: u64 = 0,
/// Moments ended, written or not.
moments: u64 = 0,
ops: std.ArrayList(u8) = .empty,
pending: usize = 0,
/// Every chunk recorded so far, so a partial write needs no read back.
mem: std.AutoHashMapUnmanaged(u64, Chunk) = .empty,
mem_dirty: std.AutoArrayHashMapUnmanaged(u64, void) = .empty,
/// Per thread seen: where each open call's variables begin in `declared`.
calls: std.AutoHashMapUnmanaged(u32, Calls) = .empty,
const Calls = struct { frames: std.ArrayList(usize) = .empty, declared: std.ArrayList(u64) = .empty };
/// A new recording at `path`, replacing any file there.
pub fn create(gpa: std.mem.Allocator, io: std.Io, path: []const u8) !Recorder {
const file = try std.Io.Dir.cwd().createFile(io, path, .{ .read = true, .truncate = true });
errdefer file.close(io);
const buffer = try gpa.create(std.Io.Writer.Allocating);
errdefer gpa.destroy(buffer);
buffer.* = .init(gpa);
errdefer buffer.deinit();
// No fsync: a recording is rewritten by running the program again.
const db = try WriteDB.init(io, .{ .file = file, .buffer = buffer, .fsync = false });
return .{ .gpa = gpa, .io = io, .file = file, .buffer = buffer, .db = db };
}
/// Ends the last moment, writes what is pending and closes the file.
pub fn close(r: *Recorder) !void {
defer {
r.delta.deinit(r.gpa);
r.ops.deinit(r.gpa);
r.mem.deinit(r.gpa);
r.mem_dirty.deinit(r.gpa);
var it = r.calls.valueIterator();
while (it.next()) |c| {
c.frames.deinit(r.gpa);
c.declared.deinit(r.gpa);
}
r.calls.deinit(r.gpa);
r.file.close(r.io);
r.buffer.deinit();
r.gpa.destroy(r.buffer);
}
if (r.delta.items.len > 0) try r.commit();
try r.flush();
try r.db.core.flush();
}
pub fn sink(r: *Recorder) event.Sink {
return .{ .ctx = r, .emitFn = sinkEmit };
}
fn sinkEmit(ctx: *anyopaque, ev: *const Event, payload: []const u8) anyerror!void {
const r: *Recorder = @ptrCast(@alignCast(ctx));
return r.emit(ev.*, payload);
}
fn op(r: *Recorder, tag: Op, fields: anytype, bytes: []const u8) !void {
try r.ops.append(r.gpa, @intFromEnum(tag));
inline for (fields) |f| try r.ops.appendSlice(r.gpa, std.mem.asBytes(&@as(u64, f)));
try r.ops.appendSlice(r.gpa, std.mem.asBytes(&@as(u32, @intCast(bytes.len))));
try r.ops.appendSlice(r.gpa, bytes);
}
pub fn emit(r: *Recorder, ev: Event, payload: []const u8) !void {
try r.delta.appendSlice(r.gpa, std.mem.asBytes(&@as(u32, @intCast(payload.len))));
try r.delta.appendSlice(r.gpa, std.mem.asBytes(&ev));
try r.delta.appendSlice(r.gpa, payload);
r.count += 1;
const a = ev.a;
const calls = try r.calls.getOrPut(r.gpa, ev.tid);
if (!calls.found_existing) {
calls.value_ptr.* = .{};
// Tid 0 is no thread: a string defined before any ran.
if (ev.tid != 0) try r.op(.thread, .{ev.tid}, "");
}
const c = calls.value_ptr;
switch (ev.kind) {
.mem_write => try r.write(a[0], payload),
.string => try r.op(.string, .{a[0]}, payload),
.line => try r.op(.line, .{ev.tid}, std.mem.asBytes(&Line{ .file = a[0], .line = @truncate(a[1]), .column = @truncate(a[2]) })),
.alloc => try r.op(.heap_put, .{a[0]}, std.mem.asBytes(&Allocation{ .ptr = a[0], .size = a[1], .pc = a[2], .born = r.count })),
.free => try r.op(.heap_del, .{a[0]}, ""),
.map => {
try r.op(.map_put, .{a[0]}, std.mem.asBytes(&Mapping{ .addr = a[0], .len = a[1], .prot = a[2], .offset = a[3] }));
try r.ops.appendSlice(r.gpa, std.mem.asBytes(&@as(u32, @intCast(payload.len))));
try r.ops.appendSlice(r.gpa, payload);
},
.unmap => try r.op(.map_del, .{a[0]}, ""),
.call => {
try c.frames.append(r.gpa, c.declared.items.len);
try r.op(.push, .{ev.tid}, std.mem.asBytes(&Frame{ .function = a[0], .site = a[1], .sp = a[2] }));
},
.ret => {
// A return with no call recorded (tracing began inside it): nothing to pop.
if (c.frames.pop()) |from| {
for (c.declared.items[from..]) |addr| try r.op(.var_del, .{addr}, "");
c.declared.shrinkRetainingCapacity(from);
try r.op(.pop, .{ev.tid}, "");
}
},
.variable => {
if (c.frames.items.len > 0) try c.declared.append(r.gpa, a[0]);
try r.op(.var_put, .{a[0]}, std.mem.asBytes(&Variable{ .addr = a[0], .size = a[1], .name = a[2], .type = a[3], .tid = ev.tid, .depth = @intCast(c.frames.items.len) }));
},
else => {},
}
// Any event may carry its thread's registers.
if (payload.len == @sizeOf(Regs) and ev.kind != .mem_write and ev.kind != .string) try r.op(.regs, .{ev.tid}, payload);
if (r.cut & ev.kind.bit() != 0 or r.delta.items.len >= r.max_delta) try r.commit();
}
fn write(r: *Recorder, addr: u64, bytes: []const u8) !void {
var at = addr;
var rest = bytes;
while (rest.len > 0) {
const index = at / chunk_bytes;
const off: usize = @intCast(at % chunk_bytes);
const n = @min(rest.len, chunk_bytes - off);
const mask = (if (n == 64) ~@as(u64, 0) else (@as(u64, 1) << @intCast(n)) - 1) << @intCast(off);
const gop = try r.mem.getOrPut(r.gpa, index);
if (!gop.found_existing) gop.value_ptr.* = .{};
const chunk = gop.value_ptr;
// A write of what is already there changes nothing worth storing.
if (chunk.known & mask != mask or !std.mem.eql(u8, chunk.bytes[off..][0..n], rest[0..n])) {
@memcpy(chunk.bytes[off..][0..n], rest[0..n]);
chunk.known |= mask;
try r.mem_dirty.put(r.gpa, index, {});
}
at += n;
rest = rest[n..];
}
}
/// Ends the moment. It reaches the file with its batch.
pub fn commit(r: *Recorder) !void {
for (r.mem_dirty.keys()) |index| try r.op(.chunk, .{index}, std.mem.asBytes(r.mem.getPtr(index).?));
r.mem_dirty.clearRetainingCapacity();
try r.op(.delta, .{r.count}, r.delta.items);
r.delta.clearRetainingCapacity();
try r.ops.append(r.gpa, @intFromEnum(Op.end));
r.moments += 1;
r.pending += 1;
if (r.pending >= r.batch) try r.flush();
}
/// Writes the moments ended so far: one transaction.
pub fn flush(r: *Recorder) !void {
if (r.pending == 0) return;
try r.db.runContext(Batch{ .ops = r.ops.items });
r.ops.clearRetainingCapacity();
r.pending = 0;
}
const Batch = struct {
ops: []const u8,
at: usize = 0,
const DB = WriteDB;
const Map = DB.HashMap(.read_write);
const Sorted = DB.SortedMap(.read_write);
const List = DB.ArrayList(.read_write);
fn take(b: *Batch, comptime T: type) T {
defer b.at += @sizeOf(T);
return std.mem.bytesToValue(T, b.ops[b.at..][0..@sizeOf(T)]);
}
fn bytes(b: *Batch) []const u8 {
const len = b.take(u32);
defer b.at += len;
return b.ops[b.at..][0..len];
}
pub fn run(ctx: Batch, cursor: *DB.Cursor(.read_write)) !void {
var b = ctx;
const root = try Map.init(cursor.*);
const moments = try List.init(try root.putCursor(key(.moments)));
while (b.at < b.ops.len) {
// The new moment starts as the last one, which is immutable
// from here: what this one changes it copies, the rest it
// shares. The list itself grows in place.
try moments.append(.{ .slot = try moments.getSlot(-1) });
var at = try moments.putCursor(-1);
try at.freeze();
const moment = try Map.init(at);
while (true) switch (@as(Op, @enumFromInt(b.take(u8)))) {
.end => break,
.delta => {
try moment.put(key(.count), .{ .uint = b.take(u64) });
try moment.put(key(.delta), .{ .bytes = b.bytes() });
},
.thread => {
try (try Map.init(try moment.putCursor(key(.threads)))).put(b.take(u64), .{ .uint = 1 });
_ = b.bytes();
},
.regs => try moment.put(threadKey(b.take(u64), .regs), .{ .bytes = b.bytes() }),
.line => try moment.put(threadKey(b.take(u64), .line), .{ .bytes = b.bytes() }),
.push => {
const stack = try List.init(try moment.putCursor(threadKey(b.take(u64), .stack)));
try stack.append(.{ .bytes = b.bytes() });
},
.pop => {
const stack = try List.init(try moment.putCursor(threadKey(b.take(u64), .stack)));
_ = b.bytes();
const n = try stack.count();
if (n > 0) try stack.slice(n - 1);
},
.chunk => try (try Sorted.init(try moment.putCursor(key(.mem)))).put(&be(b.take(u64)), .{ .bytes = b.bytes() }),
.heap_put => try (try Sorted.init(try moment.putCursor(key(.heap)))).put(&be(b.take(u64)), .{ .bytes = b.bytes() }),
.var_put => try (try Sorted.init(try moment.putCursor(key(.vars)))).put(&be(b.take(u64)), .{ .bytes = b.bytes() }),
.heap_del, .var_del, .map_del => |tag| {
const which: Key = switch (tag) {
.heap_del => .heap,
.var_del => .vars,
else => .maps,
};
const addr = be(b.take(u64));
_ = b.bytes();
// Freeing what was never seen allocated is not an error here.
_ = try (try Sorted.init(try moment.putCursor(key(which)))).remove(&addr);
},
.map_put => {
const addr = be(b.take(u64));
const mapping = b.bytes();
const path = b.bytes();
var value: [@sizeOf(Mapping) + std.Io.Dir.max_path_bytes]u8 = undefined;
const n = @min(path.len, std.Io.Dir.max_path_bytes);
@memcpy(value[0..mapping.len], mapping);
@memcpy(value[mapping.len..][0..n], path[0..n]);
try (try Sorted.init(try moment.putCursor(key(.maps)))).put(&addr, .{ .bytes = value[0 .. mapping.len + n] });
},
.string => try (try Map.init(try moment.putCursor(key(.strings)))).put(b.take(u64), .{ .bytes = b.bytes() }),
};
}
}
};
};
/// A recording, read. Any moment, in any order.
pub const Trace = struct {
gpa: std.mem.Allocator,
io: std.Io,
file: std.Io.File,
db: *ReadDB,
/// The number of moments.
len: u64,
const Map = ReadDB.HashMap(.read_only);
const Sorted = ReadDB.SortedMap(.read_only);
const List = ReadDB.ArrayList(.read_only);
pub fn open(gpa: std.mem.Allocator, io: std.Io, path: []const u8) !Trace {
const file = try std.Io.Dir.cwd().openFile(io, path, .{});
errdefer file.close(io);
const db = try gpa.create(ReadDB);
errdefer gpa.destroy(db);
db.* = try ReadDB.init(io, .{ .file = file, .fsync = false });
var t: Trace = .{ .gpa = gpa, .io = io, .file = file, .db = db, .len = 0 };
if (try t.list()) |moments| t.len = try moments.count();
return t;
}
pub fn close(t: *Trace) void {
t.file.close(t.io);
t.gpa.destroy(t.db);
}
fn list(t: *const Trace) !?List {
const root = try Map.init(t.db.rootCursor().readOnly());
return try List.init((try root.getCursor(key(.moments))) orelse return null);
}
pub fn moment(t: *const Trace, index: u64) !Moment {
const moments = (try t.list()) orelse return error.NoSuchMoment;
const cursor = (try moments.getCursor(@intCast(index))) orelse return error.NoSuchMoment;
return .{ .index = index, .map = try Map.init(cursor) };
}
/// The moment that holds event number `n` (from 1): the first whose
/// count reaches it.
pub fn momentOf(t: *const Trace, n: u64) !?u64 {
var lo: u64 = 0;
var hi = t.len;
while (lo < hi) {
const mid = lo + (hi - lo) / 2;
if (try (try t.moment(mid)).count() < n) lo = mid + 1 else hi = mid;
}
return if (lo < t.len) lo else null;
}
};
pub const Moment = struct {
index: u64,
map: Trace.Map,
const Map = Trace.Map;
const Sorted = Trace.Sorted;
const List = Trace.List;
/// Events up to and including this moment.
pub fn count(m: Moment) !u64 {
return (try m.map.getCursor(key(.count))).?.readUint();
}
fn value(comptime T: type, cursor: ReadDB.Cursor(.read_only)) !T {
var out: T = undefined;
_ = try cursor.readBytes(std.mem.asBytes(&out));
return out;
}
/// The threads seen so far, in no order.
pub fn threads(m: Moment, gpa: std.mem.Allocator) ![]u32 {
var out: std.ArrayList(u32) = .empty;
errdefer out.deinit(gpa);
if (try m.map.getCursor(key(.threads))) |cursor| {
var it = try cursor.iterator();
while (try it.next()) |entry| try out.append(gpa, @intCast((try entry.readKeyValuePair()).hash));
}
return out.toOwnedSlice(gpa);
}
pub fn regs(m: Moment, tid: u32) !?Regs {
return try value(Regs, (try m.map.getCursor(threadKey(tid, .regs))) orelse return null);
}
pub fn line(m: Moment, tid: u32) !?Line {
return try value(Line, (try m.map.getCursor(threadKey(tid, .line))) orelse return null);
}
/// The calls a thread is inside, outermost first.
pub fn stack(m: Moment, gpa: std.mem.Allocator, tid: u32) ![]Frame {
var out: std.ArrayList(Frame) = .empty;
errdefer out.deinit(gpa);
if (try m.map.getCursor(threadKey(tid, .stack))) |cursor| {
var it = try (try List.init(cursor)).iterator();
while (try it.next()) |frame| try out.append(gpa, try value(Frame, frame));
}
return out.toOwnedSlice(gpa);
}
/// The memory at `addr` as of this moment. Bytes never recorded read as
/// zero, and false in `known` when it is given.
pub fn read(m: Moment, addr: u64, buf: []u8, known: ?[]bool) !void {
@memset(buf, 0);
if (known) |k| @memset(k, false);
const mem = try Sorted.init((try m.map.getCursor(key(.mem))) orelse return);
var done: usize = 0;
while (done < buf.len) {
const at = addr + done;
const off: usize = @intCast(at % chunk_bytes);
const n = @min(buf.len - done, chunk_bytes - off);
if (try mem.getCursor(&be(at / chunk_bytes))) |cursor| {
const chunk = try value(Chunk, cursor);
@memcpy(buf[done..][0..n], chunk.bytes[off..][0..n]);
if (known) |k| for (k[done..][0..n], off..) |*bit, i| {
bit.* = chunk.known >> @intCast(i) & 1 != 0;
};
}
done += n;
}
}
fn sorted(m: Moment, comptime T: type, gpa: std.mem.Allocator, which: Key) ![]T {
var out: std.ArrayList(T) = .empty;
errdefer out.deinit(gpa);
if (try m.map.getCursor(key(which))) |cursor| {
var it = try (try Sorted.init(cursor)).iterator();
while (try it.next()) |entry| try out.append(gpa, try value(T, (try entry.readKeyValuePair()).value_cursor));
}
return out.toOwnedSlice(gpa);
}
/// The entry of a sorted map at the greatest key not above `addr`.
fn floor(m: Moment, comptime T: type, which: Key, addr: u64) !?T {
const map = try Sorted.init((try m.map.getCursor(key(which))) orelse return null);
const below = try map.rank(&be(addr +| 1));
if (below == 0) return null;
const pair = (try map.getIndexKeyValuePair(@intCast(below - 1))) orelse return null;
return try value(T, pair.value_cursor);
}
/// The live allocations, by address.
pub fn heap(m: Moment, gpa: std.mem.Allocator) ![]Allocation {
return m.sorted(Allocation, gpa, .heap);
}
/// The live allocation that holds `addr`.
pub fn allocation(m: Moment, addr: u64) !?Allocation {
const a = (try m.floor(Allocation, .heap, addr)) orelse return null;
return if (addr - a.ptr < @max(a.size, 1)) a else null;
}
/// The live named variables, by address.
pub fn variables(m: Moment, gpa: std.mem.Allocator) ![]Variable {
return m.sorted(Variable, gpa, .vars);
}
/// The mapped regions, by address.
pub fn mappings(m: Moment, gpa: std.mem.Allocator) ![]Mapping {
return m.sorted(Mapping, gpa, .maps);
}
/// The region that holds `addr`, and the file it maps into `path` (empty
/// when anonymous).
pub fn mapping(m: Moment, addr: u64, path: ?*[]const u8, buf: []u8) !?Mapping {
const map = try Sorted.init((try m.map.getCursor(key(.maps))) orelse return null);
const below = try map.rank(&be(addr +| 1));
if (below == 0) return null;
const pair = (try map.getIndexKeyValuePair(@intCast(below - 1))) orelse return null;
var raw: [@sizeOf(Mapping) + std.Io.Dir.max_path_bytes]u8 = undefined;
const bytes = try pair.value_cursor.readBytes(&raw);
const found = std.mem.bytesToValue(Mapping, bytes[0..@sizeOf(Mapping)]);
if (addr - found.addr >= found.len) return null;
if (path) |p| {
const n = @min(buf.len, bytes.len - @sizeOf(Mapping));
@memcpy(buf[0..n], bytes[@sizeOf(Mapping)..][0..n]);
p.* = buf[0..n];
}
return found;
}
pub fn string(m: Moment, id: u64, buf: []u8) !?[]const u8 {
const all = try Map.init((try m.map.getCursor(key(.strings))) orelse return null);
const cursor = (try all.getCursor(id)) orelse return null;
return try cursor.readBytes(buf);
}
/// The events that led from the moment before to this one. Free with
/// `Events.deinit`.
pub fn events(m: Moment, gpa: std.mem.Allocator) !Events {
const cursor = (try m.map.getCursor(key(.delta))).?;
return .{ .bytes = try cursor.readBytesAlloc(gpa, null) };
}
};
pub const Events = struct {
bytes: []const u8,
at: usize = 0,
pub const Item = struct { ev: Event, payload: []const u8 };
pub fn deinit(e: *Events, gpa: std.mem.Allocator) void {
gpa.free(e.bytes);
}
pub fn next(e: *Events) ?Item {
if (e.at >= e.bytes.len) return null;
const len = std.mem.readInt(u32, e.bytes[e.at..][0..4], .little);
const ev = std.mem.bytesToValue(Event, e.bytes[e.at + 4 ..][0..@sizeOf(Event)]);
const start = e.at + 4 + @sizeOf(Event);
e.at = start + len;
return .{ .ev = ev, .payload = e.bytes[start..][0..len] };
}
};
fn testPath(gpa: std.mem.Allocator, tmp: *std.testing.TmpDir) ![]u8 {
var buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
return std.fs.path.join(gpa, &.{ dir, "t.ztrace" });
}
fn testEvent(kind: Kind, tid: u32, a: [6]u64) Event {
return .{ .kind = kind, .tid = tid, .a = a };
}
test "registers, memory and strings are read back moment by moment" {
const gpa = std.testing.allocator;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const path = try testPath(gpa, &tmp);
defer gpa.free(path);
var rec = try Recorder.create(gpa, std.testing.io, path);
// Batches of two, so moments straddle transactions.
rec.batch = 2;
try rec.emit(testEvent(.string, 0, .{ 9, 0, 0, 0, 0, 0 }), "fib.c");
for (0..3) |i| {
// A write that straddles two chunks, then the step that ends the moment.
const bytes: [4]u8 = @splat(@intCast(i + 1));
try rec.emit(testEvent(.mem_write, 7, .{ 62 + i, 4, 0, 0, 0, 0 }), &bytes);
const regs: Regs = .{ .rip = 0x1000 + i, .rax = i };
try rec.emit(testEvent(.step, 7, .{ regs.rip, 0, 0, 0, 0, 0 }), std.mem.asBytes(®s));
}
try rec.close();
var trace = try Trace.open(gpa, std.testing.io, path);
defer trace.close();
try std.testing.expectEqual(@as(u64, 3), trace.len);
// The first moment still shows the first write only: nothing later leaked back.
const first = try trace.moment(0);
var buf: [8]u8 = undefined;
var known: [8]bool = undefined;
try first.read(60, &buf, &known);
try std.testing.expectEqualSlices(u8, &.{ 0, 0, 1, 1, 1, 1, 0, 0 }, &buf);
try std.testing.expectEqualSlices(bool, &.{ false, false, true, true, true, true, false, false }, &known);
try std.testing.expectEqual(@as(u64, 0x1000), (try first.regs(7)).?.rip);
try std.testing.expectEqual(@as(u64, 3), try first.count());
const last = try trace.moment(2);
try last.read(60, &buf, null);
try std.testing.expectEqualSlices(u8, &.{ 0, 0, 1, 2, 3, 3, 3, 3 }, &buf);
try std.testing.expectEqual(@as(u64, 2), (try last.regs(7)).?.rax);
try std.testing.expect((try last.regs(8)) == null);
var name: [16]u8 = undefined;
try std.testing.expectEqualStrings("fib.c", (try last.string(9, &name)).?);
const tids = try last.threads(gpa);
defer gpa.free(tids);
try std.testing.expectEqualSlices(u32, &.{7}, tids);
var events = try last.events(gpa);
defer events.deinit(gpa);
try std.testing.expectEqual(Kind.mem_write, events.next().?.ev.kind);
const step = events.next().?;
try std.testing.expectEqual(@as(u64, 0x1002), step.ev.a[0]);
try std.testing.expectEqual(@sizeOf(Regs), step.payload.len);
try std.testing.expect(events.next() == null);
try std.testing.expectEqual(@as(?u64, 1), try trace.momentOf(4));
try std.testing.expectEqual(@as(?u64, null), try trace.momentOf(8));
}
test "the heap, the call stack, variables, lines and mappings follow their events" {
const gpa = std.testing.allocator;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const path = try testPath(gpa, &tmp);
defer gpa.free(path);
var rec = try Recorder.create(gpa, std.testing.io, path);
rec.batch = 3;
const mark = testEvent(.mark, 1, @splat(0));
// Moment 0: a mapping, a global, two allocations, a call with a local.
try rec.emit(testEvent(.map, 1, .{ 0x400000, 0x2000, 5, 0, 0, 0 }), "/bin/prog");
try rec.emit(testEvent(.variable, 1, .{ 0x601000, 4, 1, 2, 0, 0 }), "");
try rec.emit(testEvent(.alloc, 1, .{ 0x1000, 16, 0x400123, 0, 0, 0 }), "");
try rec.emit(testEvent(.alloc, 1, .{ 0x2000, 32, 0x400456, 0, 0, 0 }), "");
try rec.emit(testEvent(.call, 1, .{ 0x400500, 0x400130, 0x7ffe0000, 0, 0, 0 }), "");
try rec.emit(testEvent(.variable, 1, .{ 0x7ffdfff8, 8, 3, 4, 0, 0 }), "");
try rec.emit(testEvent(.line, 1, .{ 9, 12, 5, 0, 0, 0 }), "");
// Moment 1: a nested call, a free.
try rec.emit(testEvent(.call, 1, .{ 0x400600, 0x400510, 0x7ffdffe0, 0, 0, 0 }), "");
try rec.emit(testEvent(.free, 1, .{ 0x1000, 0, 0, 0, 0, 0 }), "");
try rec.emit(mark, "");
// Moment 2: both calls return, the mapping goes.
try rec.emit(testEvent(.ret, 1, .{ 0x400600, 0, 0, 0, 0, 0 }), "");
try rec.emit(testEvent(.ret, 1, .{ 0x400500, 0, 0, 0, 0, 0 }), "");
try rec.emit(testEvent(.unmap, 1, .{ 0x400000, 0x2000, 0, 0, 0, 0 }), "");
try rec.emit(mark, "");
try rec.close();
var trace = try Trace.open(gpa, std.testing.io, path);
defer trace.close();
try std.testing.expectEqual(@as(u64, 3), trace.len);
const m0 = try trace.moment(0);
const heap0 = try m0.heap(gpa);
defer gpa.free(heap0);
try std.testing.expectEqual(@as(usize, 2), heap0.len);
try std.testing.expectEqual(@as(u64, 32), heap0[1].size);
try std.testing.expectEqual(@as(u64, 0x2000), (try m0.allocation(0x201f)).?.ptr);
try std.testing.expect((try m0.allocation(0x2020)) == null);
try std.testing.expect((try m0.allocation(0xfff)) == null);
const stack0 = try m0.stack(gpa, 1);
defer gpa.free(stack0);
try std.testing.expectEqual(@as(usize, 1), stack0.len);
try std.testing.expectEqual(@as(u64, 0x400500), stack0[0].function);
try std.testing.expectEqual(Line{ .file = 9, .line = 12, .column = 5 }, (try m0.line(1)).?);
const vars0 = try m0.variables(gpa);
defer gpa.free(vars0);
try std.testing.expectEqual(@as(usize, 2), vars0.len);
try std.testing.expectEqual(@as(u32, 0), vars0[0].depth);
try std.testing.expectEqual(@as(u32, 1), vars0[1].depth);
var path_buf: [64]u8 = undefined;
var mapped: []const u8 = "";
try std.testing.expectEqual(@as(u64, 0x400000), (try m0.mapping(0x401fff, &mapped, &path_buf)).?.addr);
try std.testing.expectEqualStrings("/bin/prog", mapped);
try std.testing.expect((try m0.mapping(0x402000, null, &path_buf)) == null);
const m1 = try trace.moment(1);
const stack1 = try m1.stack(gpa, 1);
defer gpa.free(stack1);
try std.testing.expectEqual(@as(usize, 2), stack1.len);
try std.testing.expect((try m1.allocation(0x1000)) == null);
// Returning took the local with its frame; the global stays.
const m2 = try trace.moment(2);
const stack2 = try m2.stack(gpa, 1);
defer gpa.free(stack2);
try std.testing.expectEqual(@as(usize, 0), stack2.len);
const vars2 = try m2.variables(gpa);
defer gpa.free(vars2);
try std.testing.expectEqual(@as(usize, 1), vars2.len);
try std.testing.expectEqual(@as(u64, 0x601000), vars2[0].addr);
try std.testing.expect((try m2.mapping(0x400000, null, &path_buf)) == null);
// ... and the first moment is as it was.
const again = try m0.stack(gpa, 1);
defer gpa.free(again);
try std.testing.expectEqual(@as(usize, 1), again.len);
}
|