summaryrefslogtreecommitdiff
path: root/src/trace/Debugger.zig
blob: 9368a429fe211ac1b1211cee69778bfd0095eff9 (plain) (blame)
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
//! A debugger over `ptrace.Process`, for a program to drive (the `dbg` and
//! `record` commands are two): continue, step an instruction or a source
//! line, breakpoints, hooks at addresses and at syscalls, watched memory.
//! What it stops at goes to a `Sink`:
//!
//!   at a stop    `line` when the source line changed (its file a `string`
//!                first), a `mem_write` for each run of watched words that
//!                changed, then the stop itself with the registers: `step`,
//!                `breakpoint`, `syscall_enter`, `syscall_exit`, `signal`
//!   in between   `syscall_enter` and `syscall_exit` when syscalls are
//!                recorded but not stopped at, `thread_start`,
//!                `thread_exit`, `map` and `unmap`
//!   at the end   `exit`
//!
//! A syscall's entry carries the argument it points at (a path, the bytes
//! written) in place of the registers when it has one: they come with its
//! exit, after a `mem_write` of what a read put in its buffer. A `map`'s
//! payload is the region's name, also the kernel's own for a region that is
//! no file ("[stack]").
//!
//! Memory is never recorded wholesale: only the watched regions, the stack
//! window, the live allocations a ring tells of and what syscalls point
//! at, cut at `capture`, and of those only the words that changed since
//! they were last sent.
const Debugger = @This();

const std = @import("std");
const linux = std.os.linux;
const event = @import("event.zig");
const Event = event.Event;
const Regs = event.Regs;
const ptrace = @import("ptrace.zig");
const Ring = @import("Ring.zig");
const notes = @import("usdt.zig");
const Symbols = @import("Symbols.zig");
const syscalls_table = @import("syscalls.zig");
const Line = @import("store.zig").Line;
const P = linux.PTRACE;

gpa: std.mem.Allocator,
proc: ptrace.Process,
syms: Symbols,
sink: ?event.Sink,
/// The target's ring, when it carries a producer too (instrumentation, the
/// agent): drained into the sink while the target runs and before a stop
/// is reported, so its events and the stops are one recording, in order.
ring: ?*Ring = null,
/// What a syscall does to the running target: nothing (the fastest), an
/// event, or a stop as well.
syscalls: enum { ignore, record, stop } = .ignore,
/// Called at each syscall's entry and exit unless they are ignored; it
/// decides whether the target stops there.
on_syscall: ?Hook = null,
/// A controller's mailbox, serviced while waiting without moving ptrace to another thread.
on_wait: ?struct { ctx: *anyopaque, func: *const fn (*anyopaque, *Debugger) anyerror!void } = null,
interrupt_sent: bool = false,
/// Bytes above the stack pointer watched at every stop.
stack_watch: usize = 0,
/// The most captured of what a syscall points at: a path, the buffer of a
/// write or a read (strace's -s).
capture: usize = 256,
captured: std.ArrayList(u8) = .empty,
watches: std.ArrayList(Watch) = .empty,
/// The variables the target declared through its ring, watched like them.
declared: std.ArrayList(Watch) = .empty,
/// The live allocations the target tells of through its ring (`--alloc`),
/// by address: watched too, each with the allocator's header before it.
allocations: std.ArrayList(Watch) = .empty,
/// The most read of them at a stop, in bytes: a stop's cost must not grow
/// with the target's heap.
allocation_watch: usize = 256 << 10,
/// ASan's shadow memory, recorded beside every region recorded (`--asan`):
/// its byte for each 8 bytes, at `(addr >> 3) + asan_shadow`. The offset
/// is the runtime's, found at the first stop after it set it.
asan: bool = false,
asan_shadow: ?u64 = null,
/// glibc's heap, recorded at every stop as `heap.zig` reads it (`--heap`): null for not at all.
heap: ?@import("heap.zig").Watch = null,
breakpoints: std.ArrayList(Breakpoint) = .empty,
/// The current thread's registers at its last stop.
regs: Regs = .{},
/// The target has exited.
done: bool = false,
next_id: u32 = 1,
/// What the sink was sent of the target's memory, by page.
shadow: std.AutoHashMapUnmanaged(u64, *Page) = .empty,
scratch: std.ArrayList(u8) = .empty,
/// Decodes one instruction into `text`; null where there is none. Whoever
/// has a disassembler sets it (the ztrace command links capstone): the
/// library has none of its own.
disasm: ?*const fn (code: []const u8, addr: u64, text: []u8, regs: ?*const Regs, touches: []Touch) ?Insn = null,
/// What the instruction of the last stop was about to write, to be read
/// back at the next: an instruction's writes, with no region watched.
written: [2]Touch = undefined,
written_count: usize = 0,
/// The instructions above and below the pc whose text a stop records.
disasm_around: usize = 8,
/// The most instructions of a function recorded whole with its blocks
/// (`emitBlocks`): a larger one's blocks are recorded without their text.
function_text: usize = 2048,
/// Each function met, as where its instructions begin (`instructions`).
functions: std.AutoHashMapUnmanaged(u64, []const u32) = .empty,
/// How far a stop follows pointers, from each register and from each of
/// the first `telescope_words` words of the stack: the memory they lead to
/// is recorded, and each that leads into code is named. 0, the library's
/// default: not at all (the commands ask for 3).
telescope: u8 = 0,
telescope_words: usize = 16,
/// The bytes recorded each side of a place the telescope reaches (beside
/// the 32 at it) or an instruction touches, no further than its mapping:
/// the region a viewer shows round what a pointer leads to.
telescope_around: usize = 0,
/// The addresses already named (and the data addresses looked up that have
/// no name), and the instructions already recorded with the length of each.
described: std.AutoHashMapUnmanaged(u64, void) = .empty,
code_len: std.AutoHashMapUnmanaged(u64, u8) = .empty,
/// The call frame information at each pc a recorded walk met, its
/// function's frame recorded (null: none there); forgotten when the
/// mappings change.
unwound: std.AutoHashMapUnmanaged(u64, ?Symbols.Cfi) = .empty,
/// The static probes armed (`usdt`): what each one's hook reads.
sites: std.ArrayList(*Site) = .empty,
/// The threads that have stopped before: a first stop is a thread's birth.
threads: std.AutoHashMapUnmanaged(linux.pid_t, void) = .empty,
/// String ids of source files: by path, and by line-table entry.
files: std.StringHashMapUnmanaged(u64) = .empty,
file_ids: std.AutoHashMapUnmanaged(usize, u64) = .empty,
last_line: struct { file: u64 = 0, line: u32 = 0, tid: linux.pid_t = 0 } = .{},

pub const Stop = union(enum) {
    /// `stepi` or `step` finished.
    step,
    /// The breakpoint with this id was reached, and not yet run.
    breakpoint: u32,
    /// At a syscall, by number: its arguments are in rdi, rsi, rdx, r10, r8, r9.
    syscall_enter: u16,
    /// Its result is in rax.
    syscall_exit: u16,
    /// A signal the next resume delivers (`proc.resume_signal`: clear it to swallow the signal).
    signal: linux.SIG,
    exited: u8,
    killed: linux.SIG,
};

/// A callback at a breakpoint or a syscall. It may look at the target and
/// change it (`regs`, `setRegs`, `read`, `write`), and says whether the
/// target stops there or goes on.
pub const Hook = struct {
    ctx: ?*anyopaque = null,
    func: *const fn (ctx: ?*anyopaque, d: *Debugger, stop: Stop) anyerror!Action,
};
pub const Action = enum { go, stop };

pub const Breakpoint = struct { id: u32, addr: u64, saved: u8, hook: ?Hook };
/// An instruction decoded: its length, its text ("mov rbp, rsp"), and with
/// the registers given, the memory it is about to touch.
pub const Insn = struct { len: u8, text: []const u8, touches: []const Touch = &.{}, branch: Branch = .{} };
/// Where a call, a jump or a return goes. `kind`: 0 not one, 1 call, 2
/// jump, 3 return. With `indirect` the target is read from the
/// instruction's memory operand; a return's is on the stack. `conditional`:
/// a jump on a condition.
pub const Branch = extern struct { target: u64 = 0, kind: u8 = 0, taken: u8 = 1, indirect: u8 = 0, conditional: u8 = 0 };
/// Memory an instruction touches: `how` is 1 for a read, 2 a write, 3 both.
/// The memory its operands name, and what the stack's own instructions
/// touch without naming it: a push's word and a call's return address
/// under rsp, a pop's and a return's at rsp, leave's saved rbp at rbp. A
/// `rep` string instruction's is the range its count covers.
pub const Touch = extern struct { addr: u64, size: u32, how: u8 };
pub const Watch = struct { addr: u64, len: usize };

const Page = struct {
    /// Bit i set: word i was sent.
    known: [8]u64 = @splat(0),
    words: [512]u64 = undefined,
};

/// The longest `mem_write` sent: longer runs go out in pieces.
const write_max = 1 << 16;

/// Runs `argv` stopped at its first instruction. `path` is the executable.
pub fn spawn(gpa: std.mem.Allocator, io: std.Io, path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8, envp: [*:null]const ?[*:0]const u8, sink: ?event.Sink) !Debugger {
    return begin(gpa, io, try .spawn(path, argv, envp), sink);
}

/// Stops a running process and debugs it from there. `deinit` lets it run on.
pub fn attach(gpa: std.mem.Allocator, io: std.Io, pid: linux.pid_t, sink: ?event.Sink) !Debugger {
    return begin(gpa, io, try .attach(pid), sink);
}

fn begin(gpa: std.mem.Allocator, io: std.Io, proc: ptrace.Process, sink: ?event.Sink) !Debugger {
    var d: Debugger = .{ .gpa = gpa, .proc = proc, .syms = .{ .gpa = gpa, .io = io }, .sink = sink };
    errdefer d.deinit();
    try d.threads.put(gpa, proc.pid, {});
    d.regs = try d.proc.regs();
    try d.refreshMaps();
    try d.emitStart();
    // Where the target is now: the first stop.
    _ = try d.report(.step);
    return d;
}

pub fn deinit(d: *Debugger) void {
    // An attached target runs on, without our breakpoints.
    if (d.proc.attached and !d.proc.gone) for (d.breakpoints.items) |bp| d.proc.write(bp.addr, &.{bp.saved}) catch {};
    d.proc.close();
    d.syms.deinit();
    d.watches.deinit(d.gpa);
    d.declared.deinit(d.gpa);
    d.allocations.deinit(d.gpa);
    d.breakpoints.deinit(d.gpa);
    for (d.sites.items) |site| d.gpa.destroy(site);
    d.sites.deinit(d.gpa);
    d.described.deinit(d.gpa);
    d.code_len.deinit(d.gpa);
    d.unwound.deinit(d.gpa);
    var decoded = d.functions.valueIterator();
    while (decoded.next()) |starts| d.gpa.free(starts.*);
    d.functions.deinit(d.gpa);
    var pages = d.shadow.valueIterator();
    while (pages.next()) |page| d.gpa.destroy(page.*);
    d.shadow.deinit(d.gpa);
    d.scratch.deinit(d.gpa);
    d.captured.deinit(d.gpa);
    d.threads.deinit(d.gpa);
    var paths = d.files.keyIterator();
    while (paths.next()) |path| d.gpa.free(path.*);
    d.files.deinit(d.gpa);
    d.file_ids.deinit(d.gpa);
}

// Running.

/// Until the next breakpoint, signal or exit, or syscall when they stop.
pub fn cont(d: *Debugger) !Stop {
    return d.report(try d.run(d.free()));
}

/// One instruction; a call or jump into the vDSO runs to its return, as
/// one (`stepOne`).
pub fn stepi(d: *Debugger) !Stop {
    return d.report(try d.stepOne());
}

/// One instruction, but one that enters the vDSO (the kernel's code mapped
/// into the process: `clock_gettime`, `gettimeofday`) from outside runs it
/// through to where it returns, the return address on top of the stack:
/// its loops retry while the kernel's time data changes under them, which
/// a stop at each instruction makes them do for as long as stops are slow.
fn stepOne(d: *Debugger) !Stop {
    const from = d.regs.rip;
    const stop = try d.run(P.SINGLESTEP);
    if (stop != .step or !d.inVdso(d.regs.rip) or d.inVdso(from)) return stop;
    var word: [8]u8 = undefined;
    if (d.read(d.regs.rsp, &word) != 8) return stop;
    return d.runTo(std.mem.readInt(u64, &word, .little), d.regs.rsp);
}

fn inVdso(d: *const Debugger, addr: u64) bool {
    return if (d.syms.mappingAt(addr)) |m| std.mem.eql(u8, m.name, "[vdso]") else false;
}

/// To the next source line: instructions until the line is another, a call
/// into code without lines (a library) run through as one.
pub fn step(d: *Debugger) !Stop {
    const from = d.lineAt(d.regs.rip) orelse return d.stepi();
    const tid = d.proc.tid;
    while (true) {
        const before = d.regs;
        var stop = try d.stepOne();
        if (stop == .step and d.proc.tid == tid) {
            if (d.lineAt(d.regs.rip)) |here| {
                if (from.key != here.key or from.line != here.line or d.breakpointAt(d.regs.rip) != null) return d.report(.step);
                continue;
            }
            // In code without lines. A call there from code with lines
            // pushed the address to run to: the one after the call.
            var word: [8]u8 = undefined;
            if (d.regs.rsp != before.rsp -% 8 or d.read(d.regs.rsp, &word) != 8) continue;
            const back = std.mem.readInt(u64, &word, .little);
            if (back <= before.rip or back - before.rip > 15 or d.lineAt(before.rip) == null) continue;
            stop = try d.runTo(back, d.regs.rsp);
            if (stop == .step) continue;
        }
        return d.report(stop);
    }
}

/// Until the current thread is at `addr` with its stack above `sp` (the
/// call returned, not a deeper one), or something else stops the target.
fn runTo(d: *Debugger, addr: u64, sp: u64) !Stop {
    // A breakpoint of the caller's already there stops the target by itself.
    const mine = d.breakpointAt(addr) == null;
    if (mine) try d.insert(.{ .id = 0, .addr = addr, .saved = 0, .hook = null });
    defer if (mine) d.clear(0);
    const tid = d.proc.tid;
    while (true) {
        const stop = try d.run(d.free());
        if (stop != .breakpoint or stop.breakpoint != 0) return stop;
        if (d.proc.tid == tid and d.regs.rsp > sp) return .step;
    }
}

/// The request that lets the target run: syscalls are stops to ptrace only when wanted.
fn free(d: *const Debugger) u32 {
    return if (d.syscalls == .ignore) P.CONT else P.SYSCALL;
}

/// Resumes the current thread and waits for the next stop of any. A
/// breakpoint under its pc is lifted for one instruction first.
// shortcut: the other threads run while it is lifted and can pass it, upgrade by stopping them or by stepping a copy of the instruction.
fn resumeThread(d: *Debugger, request: u32) !ptrace.Stop {
    if (d.breakpointAt(d.regs.rip)) |bp| {
        try d.proc.write(bp.addr, &.{bp.saved});
        try d.proc.run(P.SINGLESTEP);
        const stop = try d.wait(d.proc.tid);
        if (!d.proc.gone) try d.proc.write(bp.addr, &.{0xcc});
        if (request == P.SINGLESTEP or stop != .trap) return stop;
    }
    try d.proc.run(request);
    return d.waitAny();
}

fn waitAny(d: *Debugger) !ptrace.Stop {
    return d.wait(if (d.proc.threads > 1) -1 else d.proc.pid);
}

/// The next stop. With a ring it is polled for and the ring drained
/// meanwhile: a target waiting for room in a full ring would never stop.
fn wait(d: *Debugger, tid: linux.pid_t) !ptrace.Stop {
    if (d.ring == null and d.on_wait == null) return d.proc.wait(tid);
    const tapped: event.Sink = .{ .ctx = d, .emitFn = tap };
    var idle: u32 = 0;
    while (true) {
        if (d.on_wait) |hook| try hook.func(hook.ctx, d);
        if (try d.proc.poll(tid)) |stop| {
            if (d.ring) |ring| _ = try ring.drain(tapped);
            return stop;
        }
        // A step is back in microseconds: look again before sleeping.
        const drained = if (d.ring) |ring| try ring.drain(tapped) else 0;
        idle = if (drained > 0) 0 else idle +| 1;
        if (idle > 64) try std.Io.sleep(d.syms.io, .fromMicroseconds(200), .awake);
    }
}

/// Request a stop from the owning controller's wait hook. Our SIGSTOP is not redelivered.
pub fn interrupt(d: *Debugger) !void {
    if (d.done or d.interrupt_sent) return;
    if (linux.errno(linux.tgkill(d.proc.pid, d.proc.tid, .STOP)) != .SUCCESS) return error.InterruptFailed;
    d.interrupt_sent = true;
}

/// What the target says of itself goes to the sink as it is; and a
/// variable it declares (`ztrace cc --instr vars`, the agent's
/// `ztrace_watch`) is watched from then on, so its bytes are recorded at
/// every stop with no store instrumented: names and types from the
/// compiler, values from the debugger.
fn tap(ctx: *anyopaque, ev: *const Event, payload: []const u8) anyerror!void {
    const d: *Debugger = @ptrCast(@alignCast(ctx));
    if (ev.kind == .variable and ev.a[1] > 0) declared: {
        const w: Watch = .{ .addr = ev.a[0], .len = @intCast(@min(ev.a[1], 4096)) };
        for (d.declared.items) |have| if (have.addr == w.addr and have.len == w.len) break :declared;
        // A stack's addresses come round again: the oldest make room.
        if (d.declared.items.len >= 256) _ = d.declared.orderedRemove(0);
        try d.declared.append(d.gpa, w);
    }
    // An allocation is watched from its birth to its free, kept in address order.
    if (ev.kind == .alloc or ev.kind == .free) {
        const items = d.allocations.items;
        const at = std.sort.lowerBound(Watch, items, ev.a[0], struct {
            fn order(want: u64, have: Watch) std.math.Order {
                return std.math.order(want, have.addr);
            }
        }.order);
        const found = at < items.len and items[at].addr == ev.a[0];
        if (ev.kind == .free) {
            if (found) _ = d.allocations.orderedRemove(at);
        } else if (found) items[at].len = @intCast(ev.a[1]) else try d.allocations.insert(d.gpa, at, .{ .addr = ev.a[0], .len = @intCast(ev.a[1]) });
    }
    return d.sink.?.emit(ev.*, payload);
}

/// The live allocations as they are now, each with the 16 bytes before it
/// (glibc's chunk header, musl's), and of a large one its first 4 KiB, as
/// a declared variable: neighbours are read as one region, and what is
/// sent of them is what changed.
fn snapshotAllocations(d: *Debugger) !void {
    var from: u64 = 0;
    var to: u64 = 0;
    var room = d.allocation_watch;
    for (d.allocations.items) |a| {
        const start = a.addr -| 16;
        const end = a.addr + @min(a.len, 4096);
        // shortcut: past `allocation_watch` the allocations highest in memory go unread, upgrade by reading only what the instructions' `touch`es wrote.
        if (end - start > room) break;
        room -= end - start;
        if (start <= to) {
            to = @max(to, end);
            continue;
        }
        if (to > from) try d.snapshot(from, to - from);
        from = start;
        to = end;
    }
    if (to > from) try d.snapshot(from, to - from);
}

/// Runs the target with `request` until something stops it for the caller.
/// What does not (a recorded syscall, a thread's birth, a hook that says
/// go) is sent as an event here; the stop itself is for `report`.
fn run(d: *Debugger, request: u32) !Stop {
    if (d.done) return error.Exited;
    var resumes = true;
    while (true) {
        const before = d.regs;
        const tid = d.proc.tid;
        const raw = if (resumes) try d.resumeThread(request) else try d.waitAny();
        resumes = true;
        switch (raw) {
            .exited, .killed => {
                _ = d.threads.remove(d.proc.tid);
                if (d.proc.gone) {
                    d.done = true;
                    return if (raw == .exited) .{ .exited = raw.exited } else .{ .killed = raw.killed };
                }
                // A thread ended: there is nothing of it to resume.
                try d.emit(.{ .kind = .thread_exit }, "");
                resumes = false;
                continue;
            },
            else => {},
        }
        d.regs = try d.proc.regs();
        const born = !(try d.threads.getOrPut(d.gpa, d.proc.tid)).found_existing;
        switch (raw) {
            .exited, .killed => unreachable,
            .clone => |child| try d.emit(.{ .kind = .thread_start, .a = .{ @intCast(child), 0, 0, 0, 0, 0 } }, std.mem.asBytes(&d.regs)),
            .signal => |sig| {
                if (sig == .STOP and d.interrupt_sent) {
                    d.interrupt_sent = false;
                    d.proc.resume_signal = 0;
                    return .{ .signal = sig };
                }
                // A thread's first stop is the SIGSTOP it is born with under tracing.
                if (!born or sig != .STOP) return .{ .signal = sig };
                d.proc.resume_signal = 0;
            },
            .syscall => {
                const nr: u16 = @truncate(d.regs.orig_rax);
                const stop: Stop = if (try d.proc.inSyscall()) .{ .syscall_enter = nr } else .{ .syscall_exit = nr };
                if (try d.atSyscall(stop)) return stop;
                try d.emitStop(stop);
            },
            .trap => {
                if (request == P.SINGLESTEP and d.proc.tid == tid) {
                    if (d.syscalls != .ignore) try d.steppedSyscall(before);
                    return .step;
                }
                // Not the step just asked for: another thread's that ended, or a breakpoint instruction.
                if (!d.proc.atBreakpoint()) return .step;
                const at = d.regs.rip -% 1;
                const bp = d.breakpointAt(at) orelse {
                    var byte: [1]u8 = undefined;
                    if (d.proc.read(at, &byte) == 1 and byte[0] == 0xcc) {
                        // The program's own breakpoint instruction: its signal to take.
                        d.proc.resume_signal = @backingInt(linux.SIG.TRAP);
                        return .{ .signal = .TRAP };
                    }
                    // One of ours, removed after this thread ran into it: back
                    // onto the instruction it stood for.
                    d.regs.rip = at;
                    try d.proc.setRegs(&d.regs);
                    continue;
                };
                d.regs.rip = at;
                try d.proc.setRegs(&d.regs);
                if (bp.hook) |hook| if (try hook.func(hook.ctx, d, .{ .breakpoint = bp.id }) == .go) continue;
                return .{ .breakpoint = bp.id };
            },
        }
    }
}

/// A single step is no syscall stop to ptrace. When it went over a
/// `syscall` instruction (which leaves its return address in rcx), the
/// entry and the exit are told from the registers on either side.
// shortcut: the hook sees the entry after the call ran, and exit_group never returns to be seen; upgrade by reading the instruction before each step.
fn steppedSyscall(d: *Debugger, before: Regs) !void {
    const after = d.regs;
    var insn: [2]u8 = undefined;
    if (after.rip != before.rip +% 2 or after.rcx != after.rip or d.read(before.rip, &insn) != 2 or insn[0] != 0x0f or insn[1] != 0x05) return;
    const nr: u16 = @truncate(before.rax);
    d.regs = before;
    d.regs.orig_rax = nr;
    _ = try d.atSyscall(.{ .syscall_enter = nr });
    try d.emitStop(.{ .syscall_enter = nr });
    d.regs = after;
    d.regs.orig_rax = nr;
    _ = try d.atSyscall(.{ .syscall_exit = nr });
    try d.emitStop(.{ .syscall_exit = nr });
    d.regs = after;
}

/// A syscall's entry or exit: true when the target stops there.
fn atSyscall(d: *Debugger, stop: Stop) !bool {
    // What the mappings are is known as they change, when syscalls are seen at all.
    // shortcut: all of /proc/pid/maps is read at each of these, upgrade by applying the syscall's own arguments.
    if (stop == .syscall_exit) switch (std.enums.fromInt(linux.SYS, stop.syscall_exit) orelse .close) {
        .mmap, .munmap, .mremap, .mprotect, .brk => try d.refreshMaps(),
        // What the kernel put in the buffer is a write to the target's memory.
        .read, .pread64 => if (@as(i64, @bitCast(d.regs.rax)) > 0) try d.snapshot(d.regs.rsi, @min(d.regs.rax, d.capture)),
        else => {},
    };
    if (stop == .syscall_enter) switch (std.enums.fromInt(linux.SYS, stop.syscall_enter) orelse .close) {
        // What the kernel is given is the target's memory as it is now: whatever was sent of it before may be stale.
        .write, .pwrite64 => try d.snapshot(d.regs.rsi, @min(d.regs.rdx, d.capture)),
        else => {},
    };
    if (d.on_syscall) |hook| return try hook.func(hook.ctx, d, stop) == .stop;
    return d.syscalls == .stop;
}

// Events.

fn emit(d: *Debugger, ev: Event, payload: []const u8) !void {
    const sink = d.sink orelse return;
    var e = ev;
    e.ts = event.now();
    e.tid = @intCast(d.proc.tid);
    try sink.emit(e, payload);
}

/// A stop the caller is about to see, as events: the line if it changed,
/// the watched memory that changed, then the stop.
fn report(d: *Debugger, stop: Stop) !Stop {
    if (d.sink == null) return stop;
    if (!d.done) {
        // Code loaded or JIT-compiled while running without syscall stops.
        if (d.syms.mappingAt(d.regs.rip) == null) try d.refreshMaps();
        try d.emitLine();
        if (d.asan and d.asan_shadow == null) try d.findShadow();
        // With the 128 bytes under the stack pointer: the red zone, a leaf's locals.
        if (d.stack_watch > 0) try d.snapshot(d.regs.rsp -| 128, d.stack_watch + 128);
        for (d.watches.items) |w| try d.snapshot(w.addr, w.len);
        for (d.declared.items) |w| try d.snapshot(w.addr, w.len);
        try d.snapshotAllocations();
        if (d.heap) |*h| try h.atStop(d);
        try d.emitCode();
        try d.emitTelescope();
    }
    try d.emitStop(stop);
    return stop;
}

/// `snapshot` of what an instruction touches, with the region round it
/// (`telescope_around`) as far as its mapping goes.
fn snapshotRound(d: *Debugger, addr: u64, size: usize) !void {
    const m = d.syms.mappingAt(addr) orelse return d.snapshot(addr, size);
    const lo = @max(m.start, addr -| d.telescope_around);
    try d.snapshot(lo, @min(m.end, addr + size + d.telescope_around) - lo);
}

/// The most of one touch a stop records: a `rep` string instruction's
/// range can be megabytes, and stepped by instruction it does one element
/// a step.
const touch_max = 4096;

/// `snapshotRound` of a touch, `touch_max` of it at most from where a
/// `rep` begins: its start, or its end with the direction flag set.
// shortcut: of a `rep` past 4 KiB run between two stops (by line, by breakpoint) the rest of what it wrote is not recorded, upgrade by reading the range in pages at the stop after when someone records memset by line.
fn snapshotTouch(d: *Debugger, t: Touch) !void {
    const n = @min(t.size, touch_max);
    try d.snapshotRound(if (d.regs.eflags & 0x400 != 0) t.addr + t.size - n else t.addr, n);
}

/// The instructions at the pc and after it, each a string (`event.codeId`)
/// recorded once: what a viewer shows as the disassembly.
fn emitCode(d: *Debugger) !void {
    const insnAt = d.disasm orelse return;
    // What the instruction before wrote is there to read now.
    for (d.written[0..d.written_count]) |t| try d.snapshotTouch(t);
    d.written_count = 0;
    var code: [256]u8 = undefined;
    var text: [192]u8 = undefined;
    // The instruction at the pc, against the registers it will run with:
    // what it reads is recorded as it is, what it writes is kept for the
    // stop after.
    if (d.read(d.regs.rip, code[0..15]) > 0) {
        var touches: [2]Touch = undefined;
        if (insnAt(code[0..15], d.regs.rip, text[1..], &d.regs, &touches)) |insn| {
            for (insn.touches) |t| {
                try d.snapshotTouch(t);
                try d.nameData(t.addr);
                try d.emit(.{ .kind = .touch, .aux = t.how, .a = .{ t.addr, t.size, d.regs.rip, 0, 0, 0 } }, "");
                if (t.how & 2 != 0) {
                    d.written[d.written_count] = t;
                    d.written_count += 1;
                }
            }
            // Where a call, a jump or a return goes, and whether it will.
            const b = insn.branch;
            const from: ?u64 = if (b.kind == 3) d.regs.rsp else if (b.indirect != 0 and insn.touches.len > 0) insn.touches[0].addr else null;
            var word: [8]u8 = undefined;
            const target: ?u64 = if (from) |addr| (if (d.read(addr, &word) == 8) std.mem.readInt(u64, &word, .little) else null) else b.target;
            if (b.kind != 0) if (target) |to| {
                try d.nameAddress(to);
                // Through memory: the slot it reads, where it has a name ("open@got").
                if (b.indirect != 0 and b.kind != 3) if (from) |slot| if (d.syms.symbolAt(slot) != null) try d.nameAddress(slot);
                try d.emit(.{ .kind = .branch, .aux = b.kind, .a = .{ to, b.taken, d.regs.rip, 0, 0, 0 } }, "");
            };
        }
    }
    // The instructions round the pc, in address order. Inside a function
    // they are known both ways, the function decoded once from its start,
    // and its blocks are recorded; elsewhere (no bounds: a JIT's code) only
    // from the pc on.
    const around = d.disasm_around;
    if (d.bounds(d.regs.rip)) |sym| if (sym.size > 0 and sym.size <= 1 << 20) {
        const starts = try d.instructions(sym, insnAt);
        try d.emitBlocks(insnAt, sym, starts);
        const here: u32 = @intCast(d.regs.rip - sym.addr);
        const at = std.sort.binarySearch(u32, starts, here, struct {
            fn order(want: u32, have: u32) std.math.Order {
                return std.math.order(want, have);
            }
        }.order);
        if (at) |k| {
            for (k -| around..@min(starts.len, k + around + 1)) |n| {
                _ = try d.emitInstruction(insnAt, sym.addr + starts[n], if (n > 0) @intCast(starts[n] - starts[n - 1]) else 0);
            }
            return;
        }
    };
    var addr = d.regs.rip;
    var back: u8 = 0;
    for (0..around + 1) |_| {
        back = (try d.emitInstruction(insnAt, addr, back)) orelse break;
        addr += back;
    }
}

/// Records the instruction at `addr` as a string (`event.codeId`), once: a
/// byte for its length, a byte for the length of the one before it (0:
/// not known), then its text; and when either is known, a zero, its source
/// line (`store.Line`) and a byte for how many parameters a direct call's
/// callee takes, plus one (0: not known). Returns its length; null for no
/// instruction.
fn emitInstruction(d: *Debugger, insnAt: @typeInfo(@TypeOf(d.disasm)).optional.child, addr: u64, back: u8) !?u8 {
    if (d.code_len.get(addr)) |len| return len;
    var code: [15]u8 = undefined;
    var text: [2 + 192 + 2 + @sizeOf(Line)]u8 = undefined;
    const n = d.read(addr, &code);
    const insn = insnAt(code[0..n], addr, text[2..][0..192], null, &.{}) orelse return null;
    text[0] = insn.len;
    text[1] = back;
    var len = 2 + insn.text.len;
    const b = insn.branch;
    const params: u8 = if (b.kind == 1 and b.indirect == 0 and b.target != 0) (if (d.syms.parameters(b.target)) |p| p +| 1 else 0) else 0;
    const line: Line = if (d.lineAt(addr)) |l| .{ .file = try d.fileId(l), .line = l.line, .column = l.column } else .{};
    if (line.line != 0 or params != 0) {
        text[len] = 0;
        @memcpy(text[len + 1 ..][0..@sizeOf(Line)], std.mem.asBytes(&line));
        text[len + 1 + @sizeOf(Line)] = params;
        len += 2 + @sizeOf(Line);
    }
    try d.emit(.{ .kind = .string, .a = .{ event.codeId(addr), 0, 0, 0, 0, 0 } }, text[0..len]);
    // Its bytes too, as memory: a viewer can show what the text was decoded from.
    try d.snapshot(addr, insn.len);
    try d.code_len.put(d.gpa, addr, insn.len);
    return insn.len;
}

/// Where each instruction of a function begins, as offsets from its start:
/// decoded once, from the first byte on, which is the only way to know
/// where the instruction before another begins.
fn instructions(d: *Debugger, sym: Symbols.Symbol, insnAt: @typeInfo(@TypeOf(d.disasm)).optional.child) ![]const u32 {
    const entry = try d.functions.getOrPut(d.gpa, sym.addr);
    if (entry.found_existing) return entry.value_ptr.*;
    errdefer d.functions.removeByPtr(entry.key_ptr);
    const code = try d.gpa.alloc(u8, sym.size);
    defer d.gpa.free(code);
    const n = d.read(sym.addr, code);
    var starts: std.ArrayList(u32) = .empty;
    errdefer starts.deinit(d.gpa);
    var text: [192]u8 = undefined;
    var off: usize = 0;
    while (off < n) {
        try starts.append(d.gpa, @intCast(off));
        // Bytes that are no instruction (padding, a table) are stepped over one at a time.
        off += if (insnAt(code[off..n], sym.addr + off, &text, null, &.{})) |insn| insn.len else 1;
    }
    entry.value_ptr.* = try starts.toOwnedSlice(d.gpa);
    return entry.value_ptr.*;
}

/// Where the function that holds `addr` begins and how long it is: by its
/// symbol, else by its call frame information (a stripped library's).
fn bounds(d: *Debugger, addr: u64) ?Symbols.Symbol {
    if (d.syms.symbolAt(addr)) |sym| if (sym.size > 0) return sym;
    const f = d.syms.functionAt(addr) orelse return null;
    return .{ .name = "", .addr = f.start, .size = f.len };
}

/// How a basic block ends.
pub const End = enum(u8) {
    /// Into the block after it, which something jumps to.
    fall,
    /// An unconditional jump in the function, to `to`.
    jump,
    /// A conditional jump: to `to` when taken, else into the block after it.
    branch,
    ret,
    /// A jump through a register or memory, to where is not known.
    indirect,
    /// A jump out of the function, to `to`: a tail call.
    tail,
    /// A call that does not return, to `to` (`no_target`: through a
    /// register or memory): the function's last instruction, or a call of
    /// one that never returns (`noreturn_names`).
    noreturn,
};

/// A basic block as `emitBlocks` records it: `offset` bytes into its
/// function, `len` long, how it ends, and where to (`to`, from the
/// function's start).
pub const Block = extern struct { offset: u32, len: u32, to: i32 = 0, end: End, pad: [3]u8 = @splat(0) };
pub const no_target = std.math.minInt(i32);

/// Functions that never return: a call of one ends its block.
const noreturn_names = [_][]const u8{ "abort", "exit", "_exit", "_Exit", "__stack_chk_fail", "__assert_fail", "__fortify_fail", "__chk_fail", "longjmp", "siglongjmp", "__longjmp_chk", "pthread_exit", "__cxa_throw", "__cxa_rethrow", "_Unwind_Resume" };

/// A function's basic blocks, recorded once as a string (`event.blocksId`
/// of where it begins): its length (4 bytes), a byte that is 1 when every
/// instruction of it is recorded too (`function_text` of them at most:
/// strings by `event.codeId`, as `emitInstruction` writes them; and the
/// names of where its calls and jumps out of it go), then its
/// blocks (`Block`). Decoded from its instructions: a block begins at the
/// function's start, at each jump's target in it, and after each jump,
/// return and call that does not return; a call that returns ends none.
fn emitBlocks(d: *Debugger, insnAt: @typeInfo(@TypeOf(d.disasm)).optional.child, sym: Symbols.Symbol, starts: []const u32) !void {
    if ((try d.described.getOrPut(d.gpa, event.blocksId(sym.addr))).found_existing) return;
    const code = try d.gpa.alloc(u8, sym.size);
    defer d.gpa.free(code);
    const n = d.read(sym.addr, code);
    const branches = try d.gpa.alloc(Branch, starts.len);
    defer d.gpa.free(branches);
    // By instruction: the ones a block begins at, and the calls that do not return.
    var leaders = try std.DynamicBitSetUnmanaged.initEmpty(d.gpa, starts.len + 1);
    defer leaders.deinit(d.gpa);
    var stops = try std.DynamicBitSetUnmanaged.initEmpty(d.gpa, starts.len);
    defer stops.deinit(d.gpa);
    leaders.set(0);
    var text: [192]u8 = undefined;
    for (starts, branches, 0..) |off, *b, k| {
        b.* = if (off < n) if (insnAt(code[off..n], sym.addr + off, &text, null, &.{})) |insn| insn.branch else .{} else .{};
        if (b.kind == 1 and noReturn(d, b.*, k + 1 == starts.len)) stops.set(k);
        if (b.kind == 2 or b.kind == 3 or stops.isSet(k)) leaders.set(k + 1);
        if (b.kind == 2 and b.indirect == 0 and b.target -% sym.addr < sym.size) {
            if (std.sort.binarySearch(u32, starts, @as(u32, @intCast(b.target - sym.addr)), struct {
                fn order(want: u32, have: u32) std.math.Order {
                    return std.math.order(want, have);
                }
            }.order)) |target| leaders.set(target);
        }
    }
    var out: std.Io.Writer.Allocating = .init(d.gpa);
    defer out.deinit();
    const w = &out.writer;
    try w.writeInt(u32, @intCast(sym.size), .little);
    try w.writeByte(@intFromBool(starts.len <= d.function_text));
    var first: usize = 0;
    for (branches, 0..) |b, k| {
        if (!leaders.isSet(k + 1) and k + 1 < starts.len) continue;
        const end: u32 = if (k + 1 < starts.len) starts[k + 1] else @intCast(sym.size);
        var block: Block = .{ .offset = starts[first], .len = end - starts[first], .end = .fall };
        const to = std.math.cast(i32, @as(i64, @bitCast(b.target -% sym.addr)));
        switch (b.kind) {
            2 => if (b.indirect != 0 or b.target == 0 or to == null) {
                block.end = .indirect;
            } else {
                block.to = to.?;
                block.end = if (b.conditional != 0) .branch else if (b.target -% sym.addr < sym.size) .jump else .tail;
            },
            3 => block.end = .ret,
            1 => if (stops.isSet(k)) {
                block.end = .noreturn;
                block.to = if (b.indirect != 0 or b.target == 0) no_target else to orelse no_target;
            },
            else => {},
        }
        try w.writeAll(std.mem.asBytes(&block));
        first = k + 1;
    }
    // The text of each instruction, when there are few enough, and the
    // name of each place its calls and its jumps out of it go.
    if (starts.len <= d.function_text) for (starts, branches, 0..) |off, b, k| {
        _ = try d.emitInstruction(insnAt, sym.addr + off, if (k > 0) @intCast(starts[k] - starts[k - 1]) else 0);
        if ((b.kind == 1 or b.kind == 2) and b.indirect == 0 and b.target != 0 and b.target -% sym.addr >= sym.size) try d.nameAddress(b.target);
    };
    try d.emit(.{ .kind = .string, .a = .{ event.blocksId(sym.addr), 0, 0, 0, 0, 0 } }, out.written());
}

/// Whether a call never returns: it is its function's last instruction,
/// or its callee is one of `noreturn_names` (through its PLT stub too).
fn noReturn(d: *Debugger, b: Branch, last: bool) bool {
    if (last) return true;
    if (b.indirect != 0 or b.target == 0) return false;
    const callee = (d.syms.symbolAt(b.target) orelse return false).name;
    const name = if (std.mem.endsWith(u8, callee, "@plt")) callee[0 .. callee.len - "@plt".len] else callee;
    for (noreturn_names) |known| if (std.mem.eql(u8, known, name)) return true;
    return false;
}

/// What a viewer follows from the registers and the top of the stack, as
/// pwndbg's telescope does: recorded here, while the memory is there to read.
fn emitTelescope(d: *Debugger) !void {
    if (d.telescope == 0) return;
    const r = &d.regs;
    for ([_]u64{ r.rip, r.rax, r.rbx, r.rcx, r.rdx, r.rsi, r.rdi, r.rbp, r.rsp, r.r8, r.r9, r.r10, r.r11, r.r12, r.r13, r.r14, r.r15 }) |value| try d.chase(value);
    var words: [256]u64 = undefined;
    const n = d.read(r.rsp, std.mem.sliceAsBytes(words[0..@min(words.len, d.telescope_words)])) / 8;
    for (words[0..n]) |value| try d.chase(value);
    // The call chain, walked as a reader will walk it again (`Recorded`),
    // each return address named.
    var rec: Recorded = .{ .d = d };
    var chain: [32]Frame = undefined;
    const at_entry = if (d.syms.symbolAt(r.rip)) |sym| sym.addr == r.rip else false;
    const found = frames(&rec, r, at_entry, &chain);
    if (rec.failed) |err| return err;
    for (found[@min(1, found.len)..]) |f| {
        try d.chase(f.pc);
        try d.emitCall(f.pc);
    }
}

/// A module's build id and where its debug info came from, recorded once as
/// the string `event.moduleId` of its path: a byte (`Symbols.Debug`), then
/// the id: which library a recording is of, and whether its names and
/// lines come from debug info that is its own.
fn emitModule(d: *Debugger, m: *const Symbols.Module) !void {
    const id = event.moduleId(m.path);
    if ((try d.described.getOrPut(d.gpa, id)).found_existing) return;
    var text: [1 + 64]u8 = undefined;
    text[0] = @intFromEnum(m.debug);
    const n = @min(m.build_id.len, 64);
    @memcpy(text[1..][0..n], m.build_id[0..n]);
    try d.emit(.{ .kind = .string, .a = .{ id, 0, 0, 0, 0, 0 } }, text[0 .. 1 + n]);
}

/// The call that returns to `ret`, the instruction before it and the one
/// at `ret`, recorded as instructions, once: a caller's rows in a descent,
/// and an outer frame is on its call's line, `back` from `ret`.
fn emitCall(d: *Debugger, ret: u64) !void {
    const insnAt = d.disasm orelse return;
    if ((try d.described.getOrPut(d.gpa, event.codeId(ret))).found_existing) return;
    const sym = d.bounds(ret -% 1) orelse return;
    if (sym.size == 0 or sym.size > 1 << 16 or ret == sym.addr) return;
    const starts = try d.instructions(sym, insnAt);
    const k = std.sort.binarySearch(u32, starts, @as(u32, @intCast(ret - sym.addr)), struct {
        fn order(want: u32, have: u32) std.math.Order {
            return std.math.order(want, have);
        }
    }.order) orelse return;
    if (k == 0) return;
    for (k -| 2..k + 1) |n| _ = try d.emitInstruction(insnAt, sym.addr + starts[n], if (n > 0) @intCast(starts[n] - starts[n - 1]) else 0);
}

/// Follows `value` as a pointer: a pointer into code gets its name and
/// ends the chain, any other readable target is recorded (32 bytes: a word
/// to follow, and enough to tell a string) and followed.
fn chase(d: *Debugger, value: u64) !void {
    var v = value;
    for (0..d.telescope) |_| {
        const m = d.syms.mappingAt(v) orelse return;
        if (m.prot & 1 == 0) return;
        if (m.prot & 4 != 0) return d.nameAddress(v);
        // A file's data, or the zeroes after it; not "[stack]" nor "[heap]".
        if (m.name.len == 0 or m.name[0] == '/') try d.nameData(v);
        const lo = @max(m.start, v -| d.telescope_around) & ~@as(u64, 7);
        const bytes = try d.snap(lo, @min(m.end, v + 32 + d.telescope_around) - lo);
        const off: usize = @intCast(v - lo);
        if (bytes.len < off + 8) return;
        v = std.mem.readInt(u64, bytes[off..][0..8], .little);
    }
}

/// The string whose id is `addr`: its name ("main+4"), recorded once. An
/// address in a mapping not read yet (the syscalls that made it not
/// followed) has its mappings read first, and one with no name is tried
/// again the next time.
fn nameAddress(d: *Debugger, addr: u64) !void {
    if (d.described.contains(addr)) return;
    d.know(addr);
    var buf: [256]u8 = undefined;
    var w: std.Io.Writer = .fixed(&buf);
    if (!(d.syms.label(&w, addr) catch false)) return;
    try d.described.put(d.gpa, addr, {});
    try d.emit(.{ .kind = .string, .a = .{ addr, 0, 0, 0, 0, 0 } }, w.buffered());
}

/// `nameAddress` of a data address that a global or static object of the
/// symbol tables holds: "head+0", "libc.so.6!main_arena+96". One a module's
/// symbols do not hold (a string literal, the GOT's header) is looked up
/// once; one outside every module (the stack, the heap) never.
fn nameData(d: *Debugger, addr: u64) !void {
    if (d.syms.moduleAt(addr) == null or d.described.contains(addr)) return;
    if (d.syms.symbolAt(addr) != null) return d.nameAddress(addr);
    try d.described.put(d.gpa, addr, {});
}

fn emitStop(d: *Debugger, stop: Stop) !void {
    const r = &d.regs;
    const regs = std.mem.asBytes(r);
    switch (stop) {
        .step => try d.emit(.{ .kind = .step, .a = .{ r.rip, 0, 0, 0, 0, 0 } }, regs),
        .breakpoint => |id| try d.emit(.{ .kind = .breakpoint, .a = .{ r.rip, id, 0, 0, 0, 0 } }, regs),
        .syscall_enter => |nr| {
            const text = try d.decode(nr);
            try d.emit(.{ .kind = .syscall_enter, .aux = nr, .a = .{ r.rdi, r.rsi, r.rdx, r.r10, r.r8, r.r9 } }, if (text.len > 0) text else regs);
        },
        .syscall_exit => |nr| try d.emit(.{ .kind = .syscall_exit, .aux = nr, .a = .{ r.rax, 0, 0, 0, 0, 0 } }, regs),
        .signal => |sig| try d.emit(.{ .kind = .signal, .aux = @intCast(@backingInt(sig)), .a = .{ d.proc.faultAddress(), r.rip, 0, 0, 0, 0 } }, regs),
        .exited => |code| try d.emit(.{ .kind = .exit, .a = .{ code, 0, 0, 0, 0, 0 } }, ""),
        .killed => |sig| try d.emit(.{ .kind = .exit, .a = .{ 128 + @as(u64, @backingInt(sig)), 0, 0, 0, 0, 0 } }, ""),
    }
}

/// At the entry of syscall `nr`: the string or the buffer its arguments
/// point at, cut at `capture`. Empty when it takes none. Good until the
/// next call.
// shortcut: the first such argument only (rename has two), upgrade when a consumer wants the second.
/// What a vector of buffers holds (writev's), end to end, as far as `buf` goes.
fn gather(d: *Debugger, vec: u64, count: u64, buf: []u8) []u8 {
    var len: usize = 0;
    for (0..@min(count, 64)) |i| {
        var iov: [2]u64 = undefined;
        if (d.proc.read(vec + i * 16, std.mem.asBytes(&iov)) != 16) break;
        len += d.proc.read(iov[0], buf[len..][0..@min(buf.len - len, iov[1])]);
        if (len == buf.len) break;
    }
    return buf[0..len];
}

pub fn decode(d: *Debugger, nr: u16) ![]const u8 {
    const args = [_]u64{ d.regs.rdi, d.regs.rsi, d.regs.rdx, d.regs.r10, d.regs.r8, d.regs.r9 };
    try d.captured.resize(d.gpa, d.capture);
    const buf = d.captured.items;
    const text = for (syscalls_table.signature(nr), 0..) |kind, i| switch (kind) {
        's' => break d.proc.readString(args[i], buf),
        'b' => break buf[0..d.proc.read(args[i], buf[0..@min(buf.len, args[i + 1])])],
        'v' => break d.gather(args[i], args[i + 1], buf),
        else => {},
    } else return "";
    // A payload as long as the registers is taken for them: a byte less is not.
    return text[0 .. text.len - @intFromBool(text.len == @sizeOf(Regs))];
}

/// The string id of a line's file: its path, recorded the first time.
fn fileId(d: *Debugger, l: Symbols.Line) !u64 {
    if (d.file_ids.get(l.key)) |id| return id;
    // As the compilation saw it: its directory, the file's, its name, each
    // relative to the one before (glibc's: /usr/src/debug/glibc/glibc/csu/../sysdeps/...).
    const path = try std.fs.path.resolveAlloc(d.gpa, &.{ l.base, l.dir, l.name });
    const named = d.files.getOrPut(d.gpa, path) catch |err| {
        d.gpa.free(path);
        return err;
    };
    if (named.found_existing) d.gpa.free(path) else {
        named.value_ptr.* = event.stringId(path);
        try d.emit(.{ .kind = .string, .a = .{ named.value_ptr.*, 0, 0, 0, 0, 0 } }, path);
    }
    try d.file_ids.put(d.gpa, l.key, named.value_ptr.*);
    return named.value_ptr.*;
}

fn emitLine(d: *Debugger) !void {
    const l = d.lineAt(d.regs.rip);
    const file = if (l) |loc| try d.fileId(loc) else 0;
    const line = if (l) |loc| loc.line else 0;
    if (file == d.last_line.file and line == d.last_line.line and (d.proc.tid == d.last_line.tid or d.last_line.tid == 0)) return;
    d.last_line = .{ .file = file, .line = line, .tid = d.proc.tid };
    // File zero clears a native source location when entering code without lines (including JIT code).
    try d.emit(.{ .kind = .line, .a = .{ file, line, if (l) |loc| loc.column else 0, d.regs.rip, 0, 0 } }, "");
}

/// Reads the target's mappings again: what changed goes out as `map` and
/// `unmap`, and the modules that are new are loaded.
pub fn refreshMaps(d: *Debugger) !void {
    const old = try d.syms.refresh(d.proc.pid);
    defer d.syms.free(old);
    d.unwound.clearRetainingCapacity();
    if (d.sink == null) return;
    for (d.syms.modules.items) |*m| try d.emitModule(m);
    const now = d.syms.maps.list;
    for (old.list) |o| {
        for (now) |m| {
            if (m.start == o.start) break;
        } else try d.emit(.{ .kind = .unmap, .a = .{ o.start, o.end - o.start, 0, 0, 0, 0 } }, "");
    }
    for (now) |m| {
        for (old.list) |o| {
            if (m.start == o.start and m.end == o.end and m.prot == o.prot and m.offset == o.offset and std.mem.eql(u8, m.name, o.name)) break;
        } else try d.emit(.{ .kind = .map, .a = .{ m.start, m.end - m.start, m.prot, m.offset, 0, 0 } }, m.name);
    }
}

// Memory.

/// The target's memory as the program has it: breakpoints do not show.
pub fn read(d: *Debugger, addr: u64, buf: []u8) usize {
    const n = d.proc.read(addr, buf);
    for (d.breakpoints.items) |bp| {
        if (bp.addr >= addr and bp.addr - addr < n) buf[bp.addr - addr] = bp.saved;
    }
    return n;
}

pub fn write(d: *Debugger, addr: u64, bytes: []const u8) !void {
    return d.proc.write(addr, bytes);
}

pub fn setRegs(d: *Debugger, regs: Regs) !void {
    try d.proc.setRegs(&regs);
    d.regs = regs;
}

/// Watches a region: from now on, each stop sends the words of it that changed.
pub fn watch(d: *Debugger, addr: u64, len: usize) !void {
    try d.watches.append(d.gpa, .{ .addr = addr, .len = len });
}

/// Sends a region as it is now, in `mem_write` events: one read of the
/// target, then only the words (8 bytes, aligned) the sink was not sent
/// before or that changed since.
pub fn snapshot(d: *Debugger, addr: u64, len: usize) !void {
    _ = try d.snap(addr, len);
}

/// Where ASan's shadow is (`--asan`), recorded once as the string
/// `asan_id`: a byte of shadow for 2^`scale` bytes of memory, at
/// `(addr >> scale) + offset`.
pub const Asan = extern struct { offset: u64, scale: u64 };
pub const asan_id = event.stringId("asan shadow");

/// ASan's shadow offset, as its runtime has it: the value it exports in
/// `__asan_shadow_memory_dynamic_address`, set at its start (0x7fff8000 on
/// x86-64 Linux), and a mapping there (its low shadow). Its scale is 3 on
/// x86-64 (the runtime keeps it in no variable).
fn findShadow(d: *Debugger) !void {
    const sym = d.syms.find("__asan_shadow_memory_dynamic_address") orelse return;
    var word: [8]u8 = undefined;
    if (d.read(sym.addr, &word) != 8) return;
    const offset = std.mem.readInt(u64, &word, .little);
    if (offset == 0 or d.syms.mappingAt(offset) == null) return;
    d.asan_shadow = offset;
    try d.emit(.{ .kind = .string, .a = .{ asan_id, 0, 0, 0, 0, 0 } }, std.mem.asBytes(&Asan{ .offset = offset, .scale = 3 }));
}

/// `snapshot`, returning what was read: from `addr` rounded down to a word,
/// valid until the next read. With ASan's shadow, its bytes for the region
/// are sent first.
fn snap(d: *Debugger, addr: u64, len: usize) ![]const u8 {
    if (d.asan_shadow) |offset| _ = try d.snapOnly((addr >> 3) + offset, ((addr + len + 7) >> 3) - (addr >> 3));
    return d.snapOnly(addr, len);
}

fn snapOnly(d: *Debugger, addr: u64, len: usize) ![]const u8 {
    const start = addr & ~@as(u64, 7);
    try d.scratch.resize(d.gpa, std.mem.alignForward(usize, len + (addr - start), 8));
    const bytes = d.scratch.items[0 .. d.read(start, d.scratch.items) & ~@as(usize, 7)];
    try d.send(start, bytes);
    return bytes;
}

/// Sends the words of `bytes`, read at `start` (a word's address), that
/// the sink was not sent before or that changed since.
fn send(d: *Debugger, start: u64, bytes: []const u8) !void {
    var page: *Page = undefined;
    var page_index: u64 = std.math.maxInt(u64);
    // Where the run of changed words being gathered began.
    var run_from: ?usize = null;
    var at: usize = 0;
    while (at < bytes.len) : (at += 8) {
        const a = start + at;
        if (a / 4096 != page_index) {
            page_index = a / 4096;
            const entry = try d.shadow.getOrPut(d.gpa, page_index);
            if (!entry.found_existing) {
                errdefer d.shadow.removeByPtr(entry.key_ptr);
                entry.value_ptr.* = try d.gpa.create(Page);
                entry.value_ptr.*.* = .{};
            }
            page = entry.value_ptr.*;
        }
        const slot = a % 4096 / 8;
        const bit = @as(u64, 1) << @intCast(slot % 64);
        const word = std.mem.readInt(u64, bytes[at..][0..8], .little);
        if (page.known[slot / 64] & bit == 0 or page.words[slot] != word) {
            page.known[slot / 64] |= bit;
            page.words[slot] = word;
            if (run_from == null) run_from = at;
        } else if (run_from) |from| {
            try d.emitWrite(start + from, bytes[from..at]);
            run_from = null;
        }
    }
    if (run_from) |from| try d.emitWrite(start + from, bytes[from..]);
}

fn emitWrite(d: *Debugger, addr: u64, bytes: []const u8) !void {
    var at: usize = 0;
    while (at < bytes.len) : (at += write_max) {
        const n = @min(write_max, bytes.len - at);
        try d.emit(.{ .kind = .mem_write, .a = .{ addr + at, n, 0, 0, 0, 0 } }, bytes[at..][0..n]);
    }
}

// Breakpoints.

/// A breakpoint at `addr`. With a hook, the hook decides whether the
/// target stops there. Returns its id: the one already at `addr` if any.
pub fn breakAt(d: *Debugger, addr: u64, hook: ?Hook) !u32 {
    if (d.breakpointAt(addr)) |bp| return bp.id;
    try d.insert(.{ .id = d.next_id, .addr = addr, .saved = 0, .hook = hook });
    d.next_id += 1;
    return d.next_id - 1;
}

fn insert(d: *Debugger, breakpoint: Breakpoint) !void {
    var bp = breakpoint;
    if (d.proc.read(bp.addr, (&bp.saved)[0..1]) != 1) return error.BadAddress;
    try d.breakpoints.ensureUnusedCapacity(d.gpa, 1);
    try d.proc.write(bp.addr, &.{0xcc});
    d.breakpoints.appendAssumeCapacity(bp);
}

/// Removes a breakpoint by id.
pub fn clear(d: *Debugger, id: u32) void {
    for (d.breakpoints.items, 0..) |bp, i| {
        if (bp.id != id) continue;
        if (!d.proc.gone) d.proc.write(bp.addr, &.{bp.saved}) catch {};
        _ = d.breakpoints.orderedRemove(i);
        return;
    }
}

fn breakpointAt(d: *const Debugger, addr: u64) ?Breakpoint {
    for (d.breakpoints.items) |bp| {
        if (bp.addr == addr) return bp;
    }
    return null;
}

// Addresses.

/// An address from "*0x401000", "file.c:12" (the line's first code, or the
/// next line's that has any) or a function's name: past `push rbp; mov rbp,
/// rsp` when it begins so, where its frame is set up for `backtrace` and
/// its arguments are still in their registers.
/// Runs the loader through to the program's entry point, where its
/// libraries are mapped: their symbols and probes can be found from there.
pub fn toEntry(d: *Debugger) !void {
    const at = d.syms.entryPoint() orelse return;
    if (at == d.regs.rip) return;
    const id = try d.breakAt(at, null);
    defer d.clear(id);
    while (true) switch (try d.cont()) {
        .breakpoint => |hit| if (hit == id) return,
        .exited, .killed => return error.Exited,
        else => {},
    };
}

const Site = struct { name: u64, args: [5]?notes.Arg };

/// Arms the static probes (USDT, DTrace's) that `spec` names, "provider:name"
/// with "*" for any of either, in every module mapped now. Each hit is a
/// `probe` event with its first five arguments and the registers, and the
/// program runs on: what `ztrace probe` does with a uprobe and root, done
/// with a breakpoint and neither. Returns how many places were armed.
pub fn usdt(d: *Debugger, spec: []const u8) !usize {
    const colon = std.mem.indexOfScalar(u8, spec, ':') orelse return error.BadLocation;
    var armed: usize = 0;
    for (d.syms.modules.items) |m| {
        const path = try d.gpa.dupeSentinel(u8, m.path, 0);
        defer d.gpa.free(path);
        const elf = notes.Elf.open(path) catch continue;
        defer elf.close();
        const found = try notes.probes(d.gpa, elf, m.bias);
        defer d.gpa.free(found);
        for (found) |p| {
            if (!probeNamed(spec[0..colon], p.provider) or !probeNamed(spec[colon + 1 ..], p.name)) continue;
            var buf: [256]u8 = undefined;
            const label = std.fmt.bufPrint(&buf, "{s}:{s}", .{ p.provider, p.name }) catch continue;
            const site = try d.gpa.create(Site);
            site.* = .{ .name = event.stringId(label), .args = @splat(null) };
            try d.sites.append(d.gpa, site);
            var specs = std.mem.tokenizeScalar(u8, p.args, ' ');
            for (&site.args) |*arg| arg.* = notes.Arg.parse(specs.next() orelse break) catch null;
            try d.emit(.{ .kind = .string, .a = .{ site.name, 0, 0, 0, 0, 0 } }, label);
            // A program may test the semaphore before working out a probe's arguments.
            if (p.semaphore != 0) try d.write(p.semaphore, &.{ 1, 0 });
            _ = try d.breakAt(p.pc, .{ .ctx = site, .func = atProbe });
            armed += 1;
        }
    }
    return armed;
}

fn probeNamed(want: []const u8, have: []const u8) bool {
    return std.mem.eql(u8, want, "*") or std.mem.eql(u8, want, have);
}

fn atProbe(ctx: ?*anyopaque, d: *Debugger, _: Stop) anyerror!Action {
    const site: *const Site = @ptrCast(@alignCast(ctx.?));
    var ev: Event = .{ .kind = .probe, .a = .{ site.name, 0, 0, 0, 0, 0 } };
    for (site.args, ev.a[1..]) |arg, *slot| slot.* = if (arg) |a| a.fetch(&d.regs, &d.proc) orelse 0 else 0;
    try d.emit(ev, std.mem.asBytes(&d.regs));
    return .go;
}

pub fn resolve(d: *Debugger, spec: []const u8) !u64 {
    if (std.mem.startsWith(u8, spec, "*")) return std.fmt.parseInt(u64, spec[1..], 0) catch error.BadLocation;
    if (std.mem.lastIndexOfScalar(u8, spec, ':')) |colon| {
        if (std.fmt.parseInt(u32, spec[colon + 1 ..], 10)) |line| {
            return d.syms.lineAddress(spec[0..colon], line) orelse error.NoSuchLine;
        } else |_| {}
    }
    const sym = d.symbol(spec) orelse return error.NoSuchSymbol;
    var code: [8]u8 = @splat(0);
    _ = d.read(sym.addr, &code);
    // endbr64 may come first.
    const at: usize = if (std.mem.eql(u8, code[0..4], &.{ 0xf3, 0x0f, 0x1e, 0xfa })) 4 else 0;
    return sym.addr + if (std.mem.eql(u8, code[at..][0..4], &.{ 0x55, 0x48, 0x89, 0xe5 })) at + 4 else 0;
}

/// A symbol by name. The mappings are read again when it is not found: a
/// library may have been loaded since.
pub fn symbol(d: *Debugger, name: []const u8) ?Symbols.Symbol {
    if (d.syms.find(name)) |sym| return sym;
    if (!d.done) d.refreshMaps() catch {};
    return d.syms.find(name);
}

/// A place in memory from a number or a symbol's name: `size` is the
/// symbol's, 0 for a number.
pub fn place(d: *Debugger, text: []const u8) ?Symbols.Symbol {
    if (std.fmt.parseInt(u64, text, 0)) |addr| return .{ .name = "", .addr = addr, .size = 0 } else |_| {}
    return d.symbol(text);
}

/// The source line of the code at `addr`.
pub fn lineAt(d: *Debugger, addr: u64) ?Symbols.Line {
    d.know(addr);
    return d.syms.lineAt(addr);
}

/// `addr` for a reader: see `Symbols.describe`.
pub fn describe(d: *Debugger, w: *std.Io.Writer, addr: u64) !void {
    d.know(addr);
    return d.syms.describe(w, addr);
}

/// An address in no mapping known is in one made since.
fn know(d: *Debugger, addr: u64) void {
    if (d.done or d.syms.moduleAt(addr) != null or d.syms.mappingAt(addr) != null) return;
    d.refreshMaps() catch {};
}

/// The call chain: the pc, then each return address outwards. `mem` is
/// what has the stack: anything with `read(addr, buf) usize`, the debugger
/// or a moment of a recording. Where it also has `cfi(pc) ?Symbols.Cfi`
/// (both do) a frame is walked through by its call frame information,
/// which is right inside a function that keeps no frame and inside a
/// prologue; where there is none for the pc (code without unwind tables, a
/// JIT's, a PLT entry, a signal frame), by the frame pointer. `at_entry`
/// says the pc is a function's first instruction, where its caller's
/// address is still on top of the stack: the frame pointer's walk needs
/// telling.
pub fn backtrace(mem: anytype, regs: *const Regs, at_entry: bool, out: []u64) []u64 {
    return walk(u64, mem, regs, at_entry, out);
}

/// A frame of the call chain: its pc, and the stack it holds, from `low`
/// (the stack pointer, for the newest) up to `high`, just past its return
/// address: where the caller's stack pointer was before the call (the
/// CFA). `high` is `low` where the frame's end is not known.
pub const Frame = struct { pc: u64, low: u64, high: u64 };

/// `backtrace`, each frame with its extent on the stack.
pub fn frames(mem: anytype, regs: *const Regs, at_entry: bool, out: []Frame) []Frame {
    return walk(Frame, mem, regs, at_entry, out);
}

/// A frame as the walk found it: where its function begins (0: not known)
/// and the row of call frame information it was walked through by (null:
/// by the frame pointer, its saved rbp at `high - 16`).
pub const Unwound = struct { frame: Frame, function: u64 = 0, row: ?Symbols.Row = null };

/// `frames`, each with how it was walked: what tells which of its words
/// are saved registers and its return address.
pub fn unwind(mem: anytype, regs: *const Regs, at_entry: bool, out: []Unwound) []Unwound {
    return walk(Unwound, mem, regs, at_entry, out);
}

fn walk(comptime T: type, mem: anytype, regs: *const Regs, at_entry: bool, out: []T) []T {
    const Mem = switch (@typeInfo(@TypeOf(mem))) {
        .pointer => |p| p.child,
        else => @TypeOf(mem),
    };
    // The registers known in the frame walked, by DWARF's number: rax rdx
    // rcx rbx rsi rdi rbp rsp, r8 to r15, the pc. In a caller only those a
    // call keeps are, and of them the ones its callee saved or left alone.
    var r: [17]?u64 = .{ regs.rax, regs.rdx, regs.rcx, regs.rbx, regs.rsi, regs.rdi, regs.rbp, regs.rsp, regs.r8, regs.r9, regs.r10, regs.r11, regs.r12, regs.r13, regs.r14, regs.r15, regs.rip };
    var n: usize = 0;
    while (n < out.len) : (n += 1) {
        const pc = r[16] orelse break;
        if (pc == 0 and n > 0) break;
        const sp = r[7].?;
        var next: [17]?u64 = @splat(null);
        for ([_]u8{ 3, 6, 12, 13, 14, 15 }) |kept| next[kept] = r[kept];
        // A caller's pc is a return address, which may be past the call's function.
        const info: ?Symbols.Cfi = if (@hasDecl(Mem, "cfi")) mem.cfi(if (n == 0) pc else pc -% 1) else null;
        by_cfi: {
            const c = info orelse break :by_cfi;
            const base = (if (c.row.cfa_reg < r.len) r[c.row.cfa_reg] else null) orelse break :by_cfi;
            const cfa = base +% @as(u64, @bitCast(@as(i64, c.row.cfa)));
            if (cfa <= sp or c.row.saved[6] == Symbols.lost) break :by_cfi;
            set(T, &out[n], .{ .frame = .{ .pc = pc, .low = sp, .high = cfa }, .function = c.function, .row = c.row });
            next[7] = cfa;
            for (Symbols.saved_registers, c.row.saved) |reg, at| {
                if (at != 0) next[reg] = if (at == Symbols.lost) null else wordAt(mem, cfa +% @as(u64, @bitCast(@as(i64, at) * 8)));
            }
            r = next;
            continue;
        }
        if (n == 0 and at_entry) if (wordAt(mem, sp)) |ret| {
            // Its frame is the return address alone.
            set(T, &out[n], .{ .frame = .{ .pc = pc, .low = sp, .high = sp + 8 } });
            next[7] = sp + 8;
            next[16] = ret;
            r = next;
            continue;
        };
        // By the frame pointer: the caller's rbp and pc are at rbp, and
        // where its other registers are is not known.
        const fp = r[6] orelse 0;
        var frame: [16]u8 = undefined;
        const known = fp != 0 and mem.read(fp, &frame) == 16;
        set(T, &out[n], .{ .frame = .{ .pc = pc, .low = sp, .high = if (known and fp >= sp) fp + 16 else sp } });
        if (!known) {
            n += 1;
            break;
        }
        const up = std.mem.readInt(u64, frame[0..8], .little);
        const ret = std.mem.readInt(u64, frame[8..16], .little);
        // Frames lie up the stack: one that does not is not a frame.
        if (ret == 0 or (up != 0 and up <= fp)) {
            n += 1;
            break;
        }
        next = @splat(null);
        next[6] = up;
        next[7] = @max(sp, fp + 16);
        next[16] = ret;
        r = next;
    }
    return out[0..n];
}

fn set(comptime T: type, slot: *T, found: Unwound) void {
    slot.* = switch (T) {
        u64 => found.frame.pc,
        Frame => found.frame,
        else => found,
    };
}

/// The word at `addr`, where `mem` has it.
fn wordAt(mem: anytype, addr: u64) ?u64 {
    var bytes: [8]u8 = undefined;
    return if (mem.read(addr, &bytes) == 8) std.mem.readInt(u64, &bytes, .little) else null;
}

/// The call frame information at `pc`: what makes the debugger a `mem` the
/// call chain is walked through by it.
pub fn cfi(d: *Debugger, pc: u64) ?Symbols.Cfi {
    return d.syms.cfi(pc);
}

/// The target's memory as a walk of the call chain reads it, every word it
/// reads recorded, and the frame of every function it goes through
/// (`emitFunction`): so a reader walks the same chain from the recording.
pub const Recorded = struct {
    d: *Debugger,
    failed: ?anyerror = null,
    /// The stack as last read, a page at a time: a walk goes up it.
    window: [4096]u8 = undefined,
    from: u64 = 0,
    len: usize = 0,

    pub fn read(rec: *Recorded, addr: u64, buf: []u8) usize {
        if (addr < rec.from or addr + buf.len > rec.from + rec.len) {
            rec.from = addr & ~@as(u64, 7);
            rec.len = rec.d.read(rec.from, &rec.window) & ~@as(usize, 7);
            if (addr + buf.len > rec.from + rec.len) return 0;
        }
        const start = addr & ~@as(u64, 7);
        const end = std.mem.alignForward(u64, addr + buf.len, 8);
        rec.d.send(start, rec.window[start - rec.from .. end - rec.from]) catch |err| {
            rec.failed = err;
            return 0;
        };
        @memcpy(buf, rec.window[addr - rec.from ..][0..buf.len]);
        return buf.len;
    }

    pub fn cfi(rec: *Recorded, pc: u64) ?Symbols.Cfi {
        const d = rec.d;
        if (d.unwound.get(pc)) |known| return known;
        const found: ?Symbols.Cfi = if (d.syms.functionAt(pc)) |f| found: {
            d.emitFunction(f) catch |err| {
                rec.failed = err;
                return null;
            };
            break :found .{ .function = f.start, .row = f.rowAt(pc) };
        } else null;
        d.unwound.put(d.gpa, pc, found) catch |err| {
            rec.failed = err;
        };
        return found;
    }
};

/// A function's frame as a reader needs it to walk through it and to name
/// its stack's words, recorded once as a string (`event.frameId` of where
/// it begins): its length (4 bytes), what its locals are from (a byte,
/// `Symbols.Function.base`), how many rows (2 bytes), the rows
/// (`Symbols.Row`), then each local (`Symbols.Local`): its offset from the
/// base, its size, the stretch of the function's code it is in, from and
/// to (4 bytes each), its name and a zero. The copy of the stack
/// protector's value it keeps (`canary`) is a local with no name, from its
/// store on; a function with no debug info then has the CFA for its base.
fn emitFunction(d: *Debugger, f: Symbols.Function) !void {
    if ((try d.described.getOrPut(d.gpa, event.frameId(f.start))).found_existing) return;
    const own = d.canary(f, if (f.base == Symbols.no_base) Symbols.base_cfa else f.base);
    const base = if (own != null and f.base == Symbols.no_base) Symbols.base_cfa else f.base;
    var text: std.Io.Writer.Allocating = .init(d.gpa);
    defer text.deinit();
    const w = &text.writer;
    const len = std.math.cast(u32, f.len) orelse 0;
    try w.writeInt(u32, len, .little);
    try w.writeByte(base);
    try w.writeInt(u16, @intCast(@min(f.rows.len, std.math.maxInt(u16))), .little);
    try w.writeAll(std.mem.sliceAsBytes(f.rows[0..@min(f.rows.len, std.math.maxInt(u16))]));
    const kept: [1]Symbols.Local = .{.{ .name = "", .offset = if (own) |c| c.offset else 0, .size = 8, .from = if (own) |c| c.from else 0, .to = len }};
    // Locals by a base that is not the one written are no use to a reader.
    for ([_][]const Symbols.Local{ if (base == f.base) f.locals else &.{}, kept[0..@intFromBool(own != null)] }) |list| for (list) |local| {
        try w.writeInt(i32, local.offset, .little);
        try w.writeInt(u32, local.size, .little);
        try w.writeInt(u32, local.from, .little);
        try w.writeInt(u32, local.to, .little);
        try w.writeAll(local.name);
        try w.writeByte(0);
    };
    try d.emit(.{ .kind = .string, .a = .{ event.frameId(f.start), 0, 0, 0, 0, 0 } }, text.written());
}

/// Where a function keeps its copy of the stack protector's value: the
/// word its prologue stores what it read at `fs:[0x28]` into (`mov rax,
/// fs:[0x28]` then `mov [rbp - 8], rax`), seen by decoding its first 64
/// instructions, once. Its offset from the frame base `base` (a register,
/// or the CFA), and how far into the function the store is. Null without
/// one, or where a reader could not place it: stored by a register that is
/// neither the base nor, with the CFA for base, the one the CFA is kept in
/// (rsp in a function that keeps rbp).
fn canary(d: *Debugger, f: Symbols.Function, base: u8) ?struct { offset: i32, from: u32 } {
    const insnAt = d.disasm orelse return null;
    var code: [1024]u8 = undefined;
    const n = d.read(f.start, code[0..@min(code.len, f.len)]);
    // Registers whose values tell, in a touch's address, which it was made from.
    const by_rbp: u64 = 1 << 44;
    const by_rsp: u64 = 2 << 44;
    const by_fs: u64 = 3 << 44;
    const regs: Regs = .{ .rbp = by_rbp, .rsp = by_rsp, .fs_base = by_fs };
    var text: [192]u8 = undefined;
    var touches: [2]Touch = undefined;
    // The register the value was read into, as the text names it.
    var held: [8]u8 = undefined;
    var held_len: usize = 0;
    var off: usize = 0;
    for (0..64) |_| {
        const at = off;
        const insn = insnAt(code[off..n], f.start + off, &text, &regs, &touches) orelse return null;
        off += insn.len;
        if (held_len == 0) {
            const comma = std.mem.indexOfScalar(u8, insn.text, ',') orelse continue;
            for (insn.touches) |t| if (t.addr == by_fs + 0x28 and t.size == 8 and t.how == 1 and std.mem.startsWith(u8, insn.text, "mov ") and comma - 4 <= held.len) {
                held_len = comma - 4;
                @memcpy(held[0..held_len], insn.text[4..comma]);
            };
            continue;
        }
        // The next store of that register.
        if (insn.touches.len == 0 or insn.touches[0].how != 2 or insn.text.len < held_len + 2) continue;
        const tail = insn.text[insn.text.len - held_len - 2 ..];
        if (!std.mem.eql(u8, tail[2..], held[0..held_len]) or !std.mem.eql(u8, tail[0..2], ", ")) continue;
        const t = insn.touches[0];
        const from_rbp: i64 = @bitCast(t.addr -% by_rbp);
        const from_rsp: i64 = @bitCast(t.addr -% by_rsp);
        const reg: u8, const disp: i64 = if (@abs(from_rbp) < 1 << 32) .{ 6, from_rbp } else if (@abs(from_rsp) < 1 << 32) .{ 7, from_rsp } else return null;
        const row = f.rowAt(f.start + at);
        const offset = if (base == Symbols.base_cfa and row.cfa_reg == reg) disp - row.cfa else if (base == reg) disp else return null;
        return .{ .offset = std.math.cast(i32, offset) orelse return null, .from = @intCast(at) };
    }
    return null;
}

/// Where the process's first stack is: the stack pointer at its exec, where
/// argc is, then the `argc` pointers of argv and a zero, the `envc` of envp
/// and a zero, and the `auxc` entries of the auxiliary vector (two words
/// each) and the zero entry that ends it. Recorded as the string
/// `event.frameId(0)`.
pub const Start = extern struct { sp: u64, argc: u64, envc: u64, auxc: u64 };

fn emitStart(d: *Debugger) !void {
    if (d.sink == null) return;
    // The kernel keeps it, also for a process attached to: /proc/pid/stat's
    // 28th field, after the name in parentheses.
    var path_buf: [32]u8 = undefined;
    const path = std.fmt.bufPrint(&path_buf, "/proc/{d}/stat", .{d.proc.pid}) catch unreachable;
    const file = std.Io.Dir.cwd().openFile(d.syms.io, path, .{}) catch return;
    defer file.close(d.syms.io);
    var read_buf: [256]u8 = undefined;
    var reader = file.readerStreaming(d.syms.io, &read_buf);
    var text: [1024]u8 = undefined;
    const n = reader.interface.readSliceShort(&text) catch return;
    const name_end = std.mem.lastIndexOfScalar(u8, text[0..n], ')') orelse return;
    var fields = std.mem.tokenizeScalar(u8, text[name_end + 1 .. n], ' ');
    for (0..25) |_| _ = fields.next();
    const sp = std.fmt.parseInt(u64, fields.next() orelse return, 10) catch return;
    var start: Start = .{ .sp = sp, .argc = wordAt(d, sp) orelse return, .envc = 0, .auxc = 0 };
    const envp = sp + 8 * (start.argc + 2);
    while (start.envc < 1 << 16 and (wordAt(d, envp + 8 * start.envc) orelse 0) != 0) start.envc += 1;
    const auxv = envp + 8 * (start.envc + 1);
    while (start.auxc < 256 and (wordAt(d, auxv + 16 * start.auxc) orelse 0) != 0) start.auxc += 1;
    try d.emit(.{ .kind = .string, .a = .{ event.frameId(0), 0, 0, 0, 0, 0 } }, std.mem.asBytes(&start));
}