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
|
const std = @import("std");
const sk = @import("sketch");
const canim = @import("canim");
const zt = @import("ztrace");
const ui = canim.ui;
const Client = @import("Client.zig");
const Navigation = @import("Navigation.zig");
const Sources = @import("Sources.zig");
var sources: Sources = .{};
pub const name = "ztrace debugger";
pub const width = 2240;
pub const height = 1260;
pub var io: std.Io = undefined;
pub var gpa: std.mem.Allocator = undefined;
var trace: ?zt.Trace = null;
var client: ?*Client = null;
var latest: Client.Update = .{};
var loaded: u64 = 0;
pub const State = struct {
moment: u64 = 0,
tid: u32 = 0,
following: bool = true,
status: zt.control.Status = .stopped,
stops: u64 = 0,
pc: u64 = 0,
action: sk.Request(Action) = .{},
nav: [3]Navigation = @splat(.{}),
focus: Pane = .code,
maps: [3]bool = @splat(true),
source: bool = true,
source_column: u16 = 0,
annotations: bool = true,
depth: u8 = 4,
ascii: bool = true,
lens: bool = false,
lens_address: u64 = 0,
lens_origin: Box = .{},
help: bool = false,
prefix: u8 = 0,
command_open: bool = false,
command_text: [512]u8 = @splat(0),
command_len: u16 = 0,
drag_map: ?Pane = null,
};
pub const Session = struct {
action: sk.Handled = .{},
scroll: [3]f32 = @splat(0),
base: [3]u64 = @splat(0),
mini: [3]ui.Collected = @splat(.{}),
mini_key: [3]u64 = @splat(0),
listing_at: ?u64 = null,
listing_base: u64 = 0,
code: [256]canim.disasm.Insn = undefined,
code_text: [256][1024]u8 = undefined,
code_lines: [256]?zt.store.Line = undefined,
code_count: usize = 0,
aligned: [512]canim.aligned.LiveRow = undefined,
aligned_count: usize = 0,
marker_key: u64 = 0,
marker_base: u64 = 0,
marker_from: f32 = 0,
marker_to: f32 = 0,
marker_shown: f32 = 0,
marker_at: u64 = 0,
lens_at: u64 = 0,
lens_previous: u64 = 0,
shown: ?u64 = null,
tid: u32 = 0,
changed_at: u64 = 0,
changed_regs: u32 = 0,
changed_stack: u16 = 0,
changed_flags: u64 = 0,
code_start: u64 = 0,
trail: [3]u64 = @splat(0),
result_at: ?u64 = null,
touch: ?canim.Recording.Touch = null,
branch: ?canim.Recording.Branch = null,
};
const Pane = enum { code, stack, memory };
const Kind = enum { cont, pause, stepi, step, previous, next, live, follow, back, forward, source, map, annotate, shallower, deeper, ascii, lens, close_lens, help, breakpoint, command, go, submit };
const Action = struct { kind: Kind, pane: Pane = .code, address: u64 = 0 };
const Box = sk.Rect;
const panes = [3]Box{
.{ .x = 24, .y = 88, .w = 1580, .h = 708 },
.{ .x = 24, .y = 812, .w = 1072, .h = 400 },
.{ .x = 1120, .y = 812, .w = 1096, .h = 400 },
};
const Hit = struct { address: u64, box: Box };
const Button = struct { action: Action, box: Box, label: []const u8, enabled: bool };
var hits: [2048]Hit = undefined;
var hit_count: usize = 0;
var buttons: [64]Button = undefined;
var button_count: usize = 0;
var extents: [3]f32 = @splat(0);
var bases: [3]u64 = @splat(0);
var tooltip: ?Button = null;
var lens_box: ?Box = null;
var lens_hits: usize = 0;
pub fn close() void {
sources.close(gpa);
if (trace) |*t| t.close();
trace = null;
if (client) |c| c.close();
client = null;
}
pub fn command(s: *State, c: *sk.Console, line: []const u8) !bool {
var words = std.mem.tokenizeScalar(u8, line, ' ');
const cmd = words.next() orelse return false;
if (std.mem.eql(u8, cmd, "launch")) {
if (client != null) return error.AlreadyLaunched;
var argv: std.ArrayList([]const u8) = .empty;
defer argv.deinit(gpa);
// shortcut: whitespace-separated argv; add quoting when launch needs arguments containing spaces.
while (words.next()) |word| try argv.append(gpa, word);
if (argv.items.len == 0) return error.MissingProgram;
client = try Client.start(gpa, io, argv.items);
s.following = true;
s.status = .running;
return true;
}
if (std.mem.eql(u8, cmd, "observed")) {
s.status = latest.status;
s.stops = latest.moments;
s.pc = latest.pc;
if (s.following) {
s.moment = latest.moments -| 1;
s.tid = latest.tid;
}
return true;
}
if (std.mem.eql(u8, cmd, "live")) {
s.following = true;
s.moment = latest.moments -| 1;
return true;
}
if (std.mem.eql(u8, cmd, "symbols") or std.mem.eql(u8, cmd, "break") or std.mem.eql(u8, cmd, "clear") or std.mem.eql(u8, cmd, "signal") or std.mem.eql(u8, cmd, "continue") or std.mem.eql(u8, cmd, "interrupt") or std.mem.eql(u8, cmd, "stepi") or std.mem.eql(u8, cmd, "nextline")) {
try (client orelse return error.NoTarget).send(if (std.mem.eql(u8, cmd, "interrupt")) "pause" else if (std.mem.eql(u8, cmd, "nextline")) "step" else line);
if (std.mem.eql(u8, cmd, "continue") or (std.mem.eql(u8, cmd, "stepi") or std.mem.eql(u8, cmd, "nextline"))) s.following = true;
return true;
}
if (std.mem.eql(u8, cmd, "goto")) {
const which = words.next() orelse return error.MissingPane;
const pane = std.meta.stringToEnum(Pane, which) orelse return error.InvalidPane;
const address = try std.fmt.parseInt(u64, words.next() orelse return error.MissingAddress, 0);
s.nav[@backingInt(pane)].visit(address);
s.focus = pane;
if (client != null and s.status == .stopped and s.following) {
var buf: [96]u8 = undefined;
try client.?.send(try std.fmt.bufPrint(&buf, "inspect {s} 0x{x} {d}", .{ if (pane == .code) "code" else "memory", address, @as(u32, if (pane == .code) 128 else 2048) }));
}
return true;
}
if (std.mem.eql(u8, cmd, "mouse-button")) {
// The headless host has no non-left click syntax; exercise the actual handler.
const code = try std.fmt.parseInt(u16, words.next() orelse return error.MissingButton, 10);
const x = try std.fmt.parseFloat(f32, words.next() orelse return error.MissingX);
const y = try std.fmt.parseFloat(f32, words.next() orelse return error.MissingY);
pointer(s, code, x, y);
return true;
}
if (!std.mem.eql(u8, cmd, "open")) return false;
if (client != null) return error.TargetActive;
const path = words.next() orelse return error.MissingPath;
var next = try zt.Trace.open(gpa, io, path);
errdefer next.close();
if (next.len == 0) return error.EmptyRecording;
var events = try (try next.moment(next.len - 1)).events(gpa);
defer events.deinit(gpa);
if (trace) |*t| t.close();
sources.close(gpa);
trace = next;
s.moment = next.len -| 1;
s.stops = next.len;
s.following = false;
while (events.next()) |e| if (e.ev.tid != 0) {
s.tid = e.ev.tid;
};
c.print("opened {d} moments", .{next.len});
return true;
}
fn contains(r: Box, x: f32, y: f32) bool {
return x >= r.x and y >= r.y and x < r.x + r.w and y < r.y + r.h;
}
fn paneAt(x: f32, y: f32) ?Pane {
for (panes, 0..) |box, i| if (contains(box, x, y)) return @fromBackingInt(@intCast(i));
return null;
}
fn addressAt(x: f32, y: f32) ?Hit {
var i = hit_count;
const floor = if (lens_box) |box| if (contains(box, x, y)) lens_hits else 0 else 0;
while (i > floor) {
i -= 1;
if (contains(hits[i].box, x, y)) return hits[i];
}
return null;
}
fn collectAddress(_: *anyopaque, address: u64, box: Box) void {
if (hit_count == hits.len) return;
hits[hit_count] = .{ .address = address, .box = box };
hit_count += 1;
}
fn scroll(s: *State, pane: Pane, delta: f32) void {
const p = @backingInt(pane);
const n = &s.nav[p];
if (n.place.follow) {
n.place.address = bases[p];
n.place.follow = false;
}
n.place.row = std.math.clamp(n.place.row + delta, 0, extents[p]);
}
fn act(s: *State, a: Action) void {
const p = @backingInt(a.pane);
switch (a.kind) {
.previous => {
s.following = false;
s.moment -|= 1;
},
.next => {
s.following = false;
s.moment = @min(s.moment +| 1, s.stops -| 1);
},
.live => {
s.following = true;
s.moment = s.stops -| 1;
},
.follow => {
s.nav[p].place = .{};
},
.back => s.nav[p].back(),
.forward => s.nav[p].forward(),
.source => s.source = !s.source,
.map => s.maps[p] = !s.maps[p],
.annotate => s.annotations = !s.annotations,
.shallower => s.depth = @max(1, s.depth -| 1),
.deeper => s.depth = @min(6, s.depth + 1),
.ascii => s.ascii = !s.ascii,
.lens => {
s.lens = !s.lens;
if (a.address != 0) {
s.lens_address = a.address;
for (hits[0..hit_count]) |hit| if (hit.address == a.address) {
s.lens_origin = hit.box;
break;
};
}
},
.close_lens => s.lens = false,
.help => s.help = !s.help,
.command => {
s.command_open = true;
s.command_len = 0;
},
else => s.action.send(a),
}
}
fn semantic(s: *State, address: u64) void {
const t = if (trace) |*t| t else return;
const m = t.moment(@min(s.moment, t.len -| 1)) catch return;
const region = canim.rows.regionOf(m, address);
const pane: Pane = switch (region) {
.code, .rwx => .code,
.stack => .stack,
else => blk: {
const value = (canim.Recording.word(m, address, 8) catch null) orelse break :blk .memory;
break :blk if (canim.rows.regionOf(m, value) != .none) .stack else .memory;
},
};
s.lens_address = address;
s.focus = pane;
s.action.send(.{ .kind = .go, .pane = pane, .address = address });
}
fn mapDrag(s: *State, pane: Pane, y: f32) void {
const p = @backingInt(pane);
const box = panes[p];
const visible = (box.h - 40) / 24;
const row = std.math.clamp((y - box.y - 32) / (box.h - 40), 0, 1) * (extents[p] + visible) - visible / 2;
scroll(s, pane, row - s.nav[p].place.row);
}
fn enabled(s: *const State, a: Action) bool {
return switch (a.kind) {
.cont, .stepi, .step, .breakpoint => client != null and s.status == .stopped,
.pause => client != null and s.status == .running,
.back => s.nav[@backingInt(a.pane)].cursor > 0,
.forward => s.nav[@backingInt(a.pane)].cursor + 1 < s.nav[@backingInt(a.pane)].len,
else => true,
};
}
fn pointer(s: *State, mouse_button: u16, x: f32, y: f32) void {
if (s.help) return;
if (paneAt(x, y)) |pane| s.focus = pane;
if (mouse_button == 4 or mouse_button == 5) {
act(s, .{ .kind = if (mouse_button == 4) .back else .forward, .pane = s.focus });
return;
}
if (mouse_button == 3) {
if (addressAt(x, y)) |hit| semantic(s, hit.address);
return;
}
if (mouse_button != 1) return;
var b = button_count;
while (b > 0) {
b -= 1;
if (contains(buttons[b].box, x, y)) {
if (enabled(s, buttons[b].action)) act(s, buttons[b].action);
return;
}
}
if (lens_box) |box| if (contains(box, x, y)) {
if (addressAt(x, y)) |hit| {
s.lens_address = hit.address;
s.lens_origin = hit.box;
s.lens = true;
}
return;
};
if (paneAt(x, y)) |pane| {
const p = @backingInt(pane);
if (s.maps[p] and x >= panes[p].x + panes[p].w - 54 and y >= panes[p].y + 32) {
s.drag_map = pane;
mapDrag(s, pane, y);
return;
}
}
if (addressAt(x, y)) |hit| {
s.lens_address = hit.address;
s.lens_origin = hit.box;
s.lens = true;
}
}
pub fn update(s: *State, in: *const sk.Input) void {
if (in.console) return;
for (in.events) |e| {
if (s.command_open) {
if (e.kind == .text and e.value >= 32 and e.value < 127 and s.command_len < s.command_text.len) {
if (s.command_len != 0 or e.value != ':') {
s.command_text[s.command_len] = @intCast(e.value);
s.command_len += 1;
}
}
if (e.kind == .key_down) switch (@as(sk.Key, @fromBackingInt(@intCast(@as(u8, @intCast(e.code)))))) {
.escape => s.command_open = false,
.backspace => s.command_len -|= 1,
.@"return" => {
s.command_open = false;
s.action.send(.{ .kind = .submit });
},
else => {},
};
continue;
}
switch (e.kind) {
.text => switch (e.value) {
':' => act(s, .{ .kind = .command }),
'?' => act(s, .{ .kind = .help }),
else => {},
},
.mouse_down => pointer(s, e.code, e.x, e.y),
.mouse_up, .cancel => s.drag_map = null,
.mouse_move => if (s.drag_map) |pane| {
mapDrag(s, pane, e.y);
},
.wheel => if (paneAt(e.x, e.y)) |pane| {
s.focus = pane;
scroll(s, pane, -e.wheel() * 3);
},
.key_down => {
const key: sk.Key = @fromBackingInt(@intCast(@as(u8, @intCast(e.code))));
const ctrl = e.value & sk.Key.Mod.ctrl != 0 or in.held(sk.Key.left_ctrl) or in.held(sk.Key.right_ctrl);
const shift = e.value & sk.Key.Mod.shift != 0 or in.held(sk.Key.left_shift) or in.held(sk.Key.right_shift);
if (ctrl) {
switch (key) {
.d => scroll(s, s.focus, (panes[@backingInt(s.focus)].h - 40) / 48),
.u => scroll(s, s.focus, -(panes[@backingInt(s.focus)].h - 40) / 48),
.o => act(s, .{ .kind = .back, .pane = s.focus }),
.i => act(s, .{ .kind = .forward, .pane = s.focus }),
.w => s.prefix = 'w',
.c => if (s.status == .running) {
act(s, .{ .kind = .pause });
},
else => {},
}
continue;
}
if (s.prefix == 'w') {
s.focus = switch (key) {
.h => .stack,
.l => .memory,
.k => .code,
.j => .stack,
else => s.focus,
};
s.prefix = 0;
continue;
}
const was_g = s.prefix == 'g';
s.prefix = 0;
switch (key) {
.h => s.source_column -|= 4,
.l => s.source_column = @min(s.source_column +| 4, 4096),
.j => scroll(s, s.focus, 1),
.k => scroll(s, s.focus, -1),
.g => if (shift) {
scroll(s, s.focus, extents[@backingInt(s.focus)]);
} else if (was_g) {
scroll(s, s.focus, -extents[@backingInt(s.focus)]);
} else {
s.prefix = 'g';
},
.s => if (s.status == .stopped) {
act(s, .{ .kind = .stepi });
},
.n => if (s.status == .stopped) {
act(s, .{ .kind = .step });
},
.c => if (s.status == .stopped) {
act(s, .{ .kind = .cont });
},
.escape => if (s.lens or s.help) {
s.lens = false;
s.help = false;
} else if (s.status == .running) {
act(s, .{ .kind = .pause });
},
.left_bracket => act(s, .{ .kind = .previous }),
.right_bracket => act(s, .{ .kind = .next }),
.f => if (shift) {
act(s, .{ .kind = .live });
} else {
act(s, .{ .kind = .follow, .pane = s.focus });
},
.b => act(s, .{ .kind = .breakpoint, .address = s.pc }),
.m => act(s, .{ .kind = .map, .pane = s.focus }),
.v => act(s, .{ .kind = .source }),
.a => act(s, .{ .kind = .annotate }),
.z => act(s, .{ .kind = .lens, .address = if (addressAt(in.mouse_x, in.mouse_y)) |h| h.address else s.pc }),
.tab => s.focus = @fromBackingInt(@intCast((@backingInt(s.focus) + 1) % 3)),
else => {},
}
},
else => {},
}
}
}
fn icon(u: ui.Ui, kind: Kind, active: bool) void {
const c = if (active) ui.theme.mark(.current).solid else ui.theme.color.text.primary;
const v = u.moved(6, 5);
switch (kind) {
.cont => sk.draw.polygon(u.f, &.{ .{ 3, 2 }, .{ 3, 20 }, .{ 18, 11 } }, v.at.pen(.{ .fill = ui.rgb(c) })),
.pause => {
v.fill(.{ .x = 4, .y = 3, .w = 4, .h = 16 }, c);
v.fill(.{ .x = 13, .y = 3, .w = 4, .h = 16 }, c);
},
.stepi, .step => {
v.route(&.{ .{ 2, 11 }, .{ 18, 11 } }, c, .{ .width = 2, .head = .{ 6, 6 } });
v.line(.{ 20, 3 }, .{ 20, 19 }, c, 2);
if (kind == .step) {
v.line(.{ 2, 2 }, .{ 12, 2 }, c, 2);
v.line(.{ 2, 20 }, .{ 12, 20 }, c, 2);
}
},
.previous, .back, .next, .forward => {
const left = kind == .previous or kind == .back;
v.route(&.{ .{ if (left) 19 else 3, 11 }, .{ if (left) 3 else 19, 11 } }, c, .{ .width = 2, .head = .{ 6, 7 } });
if (kind == .previous or kind == .next) v.disc(.{ if (left) 21 else 1, 11 }, 2, ui.rgb(c), null, 0);
},
.live, .follow => {
v.disc(.{ 11, 11 }, 7, null, ui.rgb(c), 2);
v.disc(.{ 11, 11 }, 2, ui.rgb(c), null, 0);
v.line(.{ 11, 0 }, .{ 11, 5 }, c, 1);
v.line(.{ 11, 17 }, .{ 11, 22 }, c, 1);
},
.breakpoint => v.disc(.{ 11, 11 }, 7, ui.rgb(ui.theme.mark(.write).solid), null, 0),
.source => {
v.line(.{ 10, 3 }, .{ 4, 11 }, c, 2);
v.line(.{ 4, 11 }, .{ 10, 19 }, c, 2);
v.line(.{ 14, 3 }, .{ 20, 11 }, c, 2);
v.line(.{ 20, 11 }, .{ 14, 19 }, c, 2);
},
.map => {
v.frame(.{ .x = 1, .y = 1, .w = 20, .h = 20 }, c, 1);
for (0..5) |i| {
const y = 4 + @as(f32, @floatFromInt(i)) * 3;
v.line(.{ 4, y }, .{ if (i % 2 == 0) 18 else 13, y }, c, 1);
}
v.frame(.{ .x = 2, .y = 8, .w = 18, .h = 7 }, c, 1);
},
.lens => {
v.disc(.{ 9, 9 }, 7, null, ui.rgb(c), 2);
v.line(.{ 14, 14 }, .{ 21, 21 }, c, 3);
},
.shallower, .deeper => {
v.line(.{ 3, 11 }, .{ 19, 11 }, c, 2);
if (kind == .deeper) v.line(.{ 11, 3 }, .{ 11, 19 }, c, 2);
},
.annotate => {
v.route(&.{ .{ 2, 18 }, .{ 2, 5 }, .{ 20, 5 } }, c, .{ .width = 2, .head = .{ 6, 6 } });
v.line(.{ 8, 14 }, .{ 20, 14 }, c, 2);
v.line(.{ 8, 19 }, .{ 17, 19 }, c, 2);
},
.ascii => v.text(.mono_bold, 20, 1, 19, "Aa", c),
.close_lens => {
v.line(.{ 3, 3 }, .{ 19, 19 }, c, 2);
v.line(.{ 19, 3 }, .{ 3, 19 }, c, 2);
},
.help => v.text(.mono_bold, 22, 5, 19, "?", c),
.command => {
v.route(&.{ .{ 2, 4 }, .{ 10, 11 }, .{ 2, 18 } }, c, .{ .width = 2, .head = null });
v.line(.{ 12, 18 }, .{ 21, 18 }, c, 2);
},
else => {},
}
}
fn button(u: ui.Ui, x: f32, y: f32, action: Action, label: []const u8, available: bool, active: bool) void {
const a = u.at.xf.apply(.{ x, y });
const box: Box = .{ .x = a[0], .y = a[1], .w = 34, .h = 32 };
const b: Button = .{ .action = action, .box = box, .label = label, .enabled = available };
if (button_count < buttons.len) {
buttons[button_count] = b;
button_count += 1;
}
const hover = !u.f.in.console and contains(box, u.f.in.mouse_x, u.f.in.mouse_y);
if (hover) tooltip = b;
const v = (if (available) u else u.faded(0.32)).moved(x, y);
v.fill(.{ .x = 0, .y = 0, .w = 34, .h = 32 }, if (hover or active) ui.theme.color.rule else ui.theme.color.raised);
if (hover or active) v.frame(.{ .x = 0, .y = 0, .w = 34, .h = 32 }, if (active) ui.theme.mark(.current).solid else ui.theme.color.text.dim, 1);
icon(v, action.kind, active);
}
fn paneButtons(u: ui.Ui, s: *const State, pane: Pane) void {
const p = @backingInt(pane);
const n = s.nav[p];
const w = panes[p].w;
button(u, w - 230, -3, .{ .kind = .back, .pane = pane }, "Back · Ctrl-o / Mouse4", n.cursor > 0, false);
button(u, w - 192, -3, .{ .kind = .forward, .pane = pane }, "Forward · Ctrl-i / Mouse5", n.cursor + 1 < n.len, false);
button(u, w - 154, -3, .{ .kind = .follow, .pane = pane }, "Follow live address · f", true, n.place.follow);
button(u, w - 116, -3, .{ .kind = .map, .pane = pane }, "Toggle content minimap · m", true, s.maps[p]);
button(u, w - 78, -3, .{ .kind = if (pane == .code) .source else if (pane == .stack) .shallower else .ascii, .pane = pane }, if (pane == .code) "Aligned source · v" else if (pane == .stack) "Fewer pointer hops" else "ASCII / pointer words", true, if (pane == .code) s.source else if (pane == .memory) s.ascii else false);
button(u, w - 40, -3, .{ .kind = if (pane == .stack) .deeper else .annotate, .pane = pane }, if (pane == .stack) "More pointer hops" else "Read / result annotations · a", true, if (pane == .stack) false else s.annotations);
}
pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
if (ss.action.take(s.action)) |action| switch (action.kind) {
.cont => f.run("continue"),
.pause => f.run("interrupt"),
.stepi => f.run("stepi"),
.step => f.run("nextline"),
.go => f.run(ui.Ui.init(f).print("goto {s} 0x{x}", .{ @tagName(action.pane), action.address })),
.breakpoint => f.run(ui.Ui.init(f).print("break *0x{x}", .{action.address})),
.submit => f.run(s.command_text[0..s.command_len]),
else => {},
};
if (client) |c| if (c.poll()) |update_| if (update_.generation != latest.generation) {
latest = update_;
if (latest.status != .running and latest.moments > loaded) {
if (zt.Trace.open(gpa, io, c.path)) |next| {
if (trace) |*t| t.close();
trace = next;
loaded = next.len;
} else |err| f.host.print("recording: {t}", .{err});
}
f.run("observed");
};
f.canvas(width, height);
f.clear(ui.theme.color.page);
const u = ui.Ui.init(f);
const ink = ui.theme.color.text;
const gold = ui.theme.mark(.current);
hit_count = 0;
button_count = 0;
tooltip = null;
lens_box = null;
ui.address_hit = .{ .ctx = ss, .emit = collectAddress };
defer ui.address_hit = null;
u.text(.sans_medium, 24, 24, 28, "ztrace / debugger", ink.primary);
const status = if (client == null) (if (trace == null) "READY" else "RECORDING") else @tagName(s.status);
u.fill(.{ .x = 300, .y = 9, .w = 118, .h = 24 }, ui.theme.color.raised);
u.text(.mono_bold, 17, 312, 27, status, if (s.status == .failed) ui.theme.mark(.write).solid else gold.solid);
u.text(.sans, 18, 434, 27, latest.reason(), ink.dim);
if (client != null) u.text(.mono, 17, 1904, 27, u.print("command {d:.3} ms", .{@as(f64, @floatFromInt(latest.elapsed_ns)) / 1e6}), ink.dim);
const kinds = [_]Kind{ if (s.status == .running) .pause else .cont, .stepi, .step, .breakpoint, .previous, .next, .live, .source, .map, .annotate, .lens, .command, .help };
const labels = [_][]const u8{ if (s.status == .running) "Pause · Esc / Ctrl-c" else "Continue · c", "Single instruction · s", "Next source line (enters calls) · n", "Breakpoint at PC · b", "Previous recorded stop · [", "Next recorded stop · ]", "Live stop · F (Shift-f)", "Aligned source · v", "Focused pane minimap · m", "Operand annotations · a", "Address lens · z", "Command line · :", "Keyboard help · ?" };
for (kinds, labels, 0..) |kind, label, i| {
const available = switch (kind) {
.cont, .pause, .stepi, .step, .breakpoint => client != null and s.status != .exited and (kind == .pause or s.status == .stopped),
else => true,
};
button(u, 24 + @as(f32, @floatFromInt(i)) * 42, 42, .{ .kind = kind, .pane = s.focus, .address = s.pc }, label, available, switch (kind) {
.source => s.source,
.map => s.maps[@backingInt(s.focus)],
.annotate => s.annotations,
.lens => s.lens,
.live => s.following,
else => false,
});
}
u.text(.sans, 17, 600, 63, "j k scroll s instruction n source c run : command ? help", ink.dim);
const t = if (trace) |*t| t else {
_ = u.moved(24, 88).panel(2192, 1124, "session", .{ .focus = true });
u.text(.sans_medium, 34, 72, 174, "Start with a real process.", ink.primary);
u.text(.mono, 22, 72, 226, "launch PROGRAM [ARGS]", gold.solid);
u.text(.sans, 22, 72, 268, "Open commands with : or backtick, then set a breakpoint and continue.", ink.dim);
u.text(.mono, 20, 72, 328, "break FUNCTION break *ADDRESS clear ID", ink.primary);
overlays(u, s, ss, null);
return;
};
const m = t.moment(@min(s.moment, t.len -| 1)) catch return;
const regs = (m.regs(s.tid) catch null) orelse return;
const low = (regs.rsp -| 24) & ~@as(u64, 7);
if (ss.shown != m.index or ss.tid != s.tid) selected(ss, t, m, regs, s.tid, low, f.in.now);
const seconds = @as(f32, @floatFromInt(f.in.now -| ss.changed_at)) / 1e9;
const heat = changedHeat(seconds);
const fire = if (seconds < ui.theme.time.fire) ui.theme.pulse(seconds / ui.theme.time.fire) else 0;
const travel = ui.theme.ease(.standard, std.math.clamp(seconds / ui.theme.time.step, 0, 1));
const mode = if (!s.following) "HISTORY" else if (s.status == .running) "LAST STOP" else "LIVE";
u.text(.mono_bold, 18, 1650, 63, mode, gold.solid);
u.text(.mono, 18, 1770, 63, u.print("moment {d}/{d} · tid {d}", .{ m.index, t.len -| 1, s.tid }), ink.dim);
const live_bases = [3]u64{ ss.code_start, low, (ss.touch orelse canim.Recording.Touch{ .addr = regs.rsp, .len = 8, .how = 1 }).addr & ~@as(u64, 15) };
for (0..3) |p| {
bases[p] = if (s.nav[p].place.follow) live_bases[p] else s.nav[p].place.address;
if (ss.base[p] != bases[p]) {
ss.scroll[p] = 0;
ss.base[p] = bases[p];
}
const target = if (s.nav[p].place.follow) 0 else s.nav[p].place.row;
const dt = @as(f32, @floatFromInt(f.in.dt)) / 1e9;
ss.scroll[p] = std.math.lerp(ss.scroll[p], target, 1 - @exp(-dt * 18));
if (@abs(ss.scroll[p] - target) < 0.001) ss.scroll[p] = target;
}
listing(ss, m, bases[0]);
drawCode(u.moved(24, 88), s, ss, m, regs, heat, fire, travel);
drawRegisters(u.moved(1628, 88), s, ss, m, regs, heat, seconds, fire);
drawStack(u.moved(24, 812), s, ss, m, regs, heat, seconds);
drawMemory(u.moved(1120, 812), s, ss, m, heat);
ties(u, s, ss, m, travel);
const place = if (t.len <= 1) 1 else @as(f32, @floatFromInt(m.index)) / @as(f32, @floatFromInt(t.len - 1));
u.fill(.{ .x = 24, .y = 1226, .w = 2192, .h = 2 }, ui.theme.color.rule);
u.fill(.{ .x = 24, .y = 1226, .w = 2192 * place, .h = 2 }, gold.solid);
u.fill(.{ .x = 22 + 2192 * place, .y = 1223, .w = 4, .h = 8 }, gold.solid);
u.text(.sans, 17, 24, 1251, if (s.following) "Live stop · right-click an address to follow its meaning · click for a lens" else "Recorded state · inspecting and navigating leave the target unchanged", ink.dim);
u.text(.sans, 17, 1730, 1251, "gold: execution vermilion: changed", ink.dim);
overlays(u, s, ss, m);
}
fn listing(ss: *Session, m: zt.store.Moment, base: u64) void {
if (ss.listing_at == m.index and ss.listing_base == base) return;
ss.listing_at = m.index;
ss.listing_base = base;
ss.code_count = 0;
var pc = base;
for (&ss.code, &ss.code_text, &ss.code_lines) |*row, *buf, *line| {
const insn = (zt.telescope.instruction(m, pc, buf) catch null) orelse break;
row.* = .{ .addr = pc, .len = insn.len, .text = insn.text, .line = if (insn.line) |l| l.line else null, .params = insn.params };
line.* = insn.line;
ss.code_count += 1;
pc +|= insn.len;
}
ss.aligned_count = canim.aligned.livePlan(ss.code_lines[0..ss.code_count], &ss.aligned).len;
}
fn sourceLine(u: ui.Ui, column: usize, m: zt.store.Moment, line: zt.store.Line, y: f32, w: f32, current: bool, fire: f32, again: bool) void {
const file = sources.get(gpa, io, m, line.file) orelse return;
const text = file.text orelse {
u.text(.mono, 18, 64, y + 18, "source file unavailable", ui.theme.color.text.faint);
return;
};
if (line.line == 0 or line.line > text.lines.len) return;
const range = text.lines[line.line - 1];
canim.rows.line(u, y, w, line.line, canim.rows.numberRoom(@intCast(text.lines.len)), text.bytes[@min(range[0] + column, range[1])..range[1]], text.styles[@min(range[0] + column, range[1])..range[1]], if (current) .{ .current = fire } else .plain);
if (again) u.text(.mono, 18, 3, y + 19, "↻", ui.theme.color.text.dim);
}
fn codeRows(u: ui.Ui, s: *const State, ss: *Session, m: zt.store.Moment, regs: zt.Regs, first: f32, heat: ui.Heat, fire: f32, travel: f32, miniature: bool) void {
const seam: f32 = if (s.source) 536 else 0;
const room: f32 = 1580 - seam - if (s.maps[0]) @as(f32, 58) else 6;
const count = if (s.source) ss.aligned_count else ss.code_count;
var current_group: ?usize = null;
var group_start: usize = 0;
for (ss.aligned[0..ss.aligned_count], 0..) |row, i| {
if (row.source != null) group_start = i;
if (row.instruction) |j| if (ss.code[j].addr == regs.rip) {
current_group = group_start;
};
}
var pc_row: ?f32 = null;
var branch_row: ?f32 = null;
for (0..count) |i| {
const r: canim.aligned.LiveRow = if (s.source) ss.aligned[i] else .{ .instruction = i };
const y = (if (miniature) @as(f32, 0) else 32) + (@as(f32, @floatFromInt(i)) - first) * 24;
if (!miniature and (y < 8 or y >= 706)) continue;
if (s.source) if (r.source) |line| {
const current = current_group == i;
sourceLine(if (r.instruction == null) u.faded(0.45) else u, if (miniature) 0 else s.source_column, m, line, y, seam - 32, current, fire, r.again);
if (r.instruction != null) u.route(&.{ .{ seam - 30, y + 12 }, .{ seam - 8, y + 12 }, .{ seam + 2, y + 12 } }, if (current) ui.theme.mark(.current).solid else ui.theme.color.rule, .{ .width = 1.5, .head = .{ 5, 6 }, .progress = if (current) travel else 1 });
};
const index = r.instruction orelse continue;
const row = ss.code[index];
const d = u.moved(seam, 0);
const current = row.addr == regs.rip;
if (current) {
const marker_y = if (miniature) y else 32 + (ss.marker_shown - first) * 24;
pc_row = marker_y;
d.current(.{ .x = 2, .y = marker_y, .w = room - 4, .h = 24 }, ui.theme.metric.plaque.pc, fire);
sk.draw.polygon(u.f, &.{ .{ 20, marker_y + 12 }, .{ 10, marker_y + 6 }, .{ 10, marker_y + 18 } }, d.at.pen(.{ .fill = ui.rgb(ui.theme.mark(.current).on_solid) }));
}
for (ss.trail, 0..) |old, k| if (old == row.addr and !current) {
d.trail(2, y, k);
break;
};
canim.disasm.liveRow(d, row, y, room, m, regs, .{ .current = current and s.annotations, .executed = ss.result_at == row.addr and s.annotations, .heat = heat, .lead = ui.shared(&.{ bases[0], bases[0] +| 512 }), .touch = if (current and s.annotations) ss.touch else null, .branch = if (current and s.annotations) ss.branch else null });
if (ss.branch) |branch| if (branch.to == row.addr) {
branch_row = y;
};
}
if (!miniature and s.source and pc_row != null and current_group != null) {
const from = 32 + (@as(f32, @floatFromInt(current_group.?)) - first) * 24 + 12;
if (@abs(from - pc_row.? - 12) > 1) u.route(&.{ .{ seam - 30, from }, .{ seam - 12, from }, .{ seam - 12, pc_row.? + 12 }, .{ seam + 2, pc_row.? + 12 } }, ui.theme.mark(.current).solid, .{ .width = 2, .head = .{ 6, 7 }, .progress = travel });
}
if (!miniature and pc_row != null and branch_row != null) if (ss.branch) |branch| if (branch.kind == .jump) {
u.route(&.{ .{ seam + 37, pc_row.? + 12 }, .{ seam + 28, pc_row.? + 12 }, .{ seam + 28, branch_row.? + 12 }, .{ seam + 38, branch_row.? + 12 } }, if (branch.taken) ui.theme.mark(.current).solid else ui.theme.color.text.faint, .{ .width = 2, .head = .{ 7, 8 }, .radius = 4, .dashed = !branch.taken, .progress = travel });
};
}
fn drawCode(u: ui.Ui, s: *const State, ss: *Session, m: zt.store.Moment, regs: zt.Regs, heat: ui.Heat, fire: f32, travel: f32) void {
const count = if (s.source) ss.aligned_count else ss.code_count;
const marker_key = (m.index + 1) *% 0x9e3779b97f4a7c15 ^ bases[0] ^ @as(u64, @intFromBool(s.source));
if (ss.marker_key != marker_key) {
for (0..count) |i| {
const index = if (s.source) ss.aligned[i].instruction else i;
if (index) |j| if (ss.code[j].addr == regs.rip) {
ss.marker_to = @floatFromInt(i);
ss.marker_from = if (ss.marker_base == bases[0]) ss.marker_shown else ss.marker_to;
};
}
ss.marker_key = marker_key;
ss.marker_base = bases[0];
ss.marker_at = u.f.in.now;
}
const marker_time = @as(f32, @floatFromInt(u.f.in.now -| ss.marker_at)) / 1e9;
ss.marker_shown = std.math.lerp(ss.marker_from, ss.marker_to, ui.theme.ease(.standard, std.math.clamp(marker_time / ui.theme.time.step, 0, 1)));
extents[0] = @max(0, @as(f32, @floatFromInt(count)) - 27);
_ = u.panel(1580, 708, if (s.source) "source / disassembly" else "disassembly", .{ .focus = s.focus == .code });
var file_name: []const u8 = "no recorded source mapping";
for (ss.code_lines[0..ss.code_count]) |line| if (line) |l| {
if (sources.get(gpa, io, m, l.file)) |file| file_name = file.path;
break;
};
u.text(.sans, 17, 250, 19, canim.rows.fitted(u, file_name, 1050), ui.theme.color.text.dim);
paneButtons(u, s, .code);
u.clip(.{ .x = 2, .y = 30, .w = 1576 - if (s.maps[0]) @as(f32, 56) else 0, .h = 676 });
if (s.source) u.line(.{ 530, 30 }, .{ 530, 706 }, ui.theme.color.rule, 1);
codeRows(u, s, ss, m, regs, ss.scroll[0], heat, fire, travel, false);
if (ss.code_count == 0) u.text(.sans, 20, if (s.source) 550 else 24, 64, "Code not captured · use goto code ADDRESS at a live stop", ui.theme.color.text.dim);
if (s.source and std.mem.eql(u8, file_name, "no recorded source mapping")) {
u.text(.sans_medium, 22, 28, 72, "Source mapping unavailable", ui.theme.color.text.dim);
u.text(.sans, 18, 28, 106, "This code has no recorded file / line table.", ui.theme.color.text.faint);
u.text(.sans, 18, 28, 136, "Machine instructions remain fully inspectable.", ui.theme.color.text.faint);
}
u.unclip();
if (s.maps[0]) {
const key = (m.index + 1) *% 0x9e3779b97f4a7c15 ^ bases[0] ^ (@as(u64, s.depth) << 32) ^ (@as(u64, @intFromBool(s.source)) << 40) ^ (@as(u64, @intFromBool(s.ascii)) << 41);
if (ss.mini_key[0] != key) {
ui.collected.reset();
ui.strokes = true;
u.clip(.{ .x = 0, .y = 0, .w = 0, .h = 0 });
// Identity coordinates keep the collected miniature local to its pane.
codeRows(ui.Ui.init(u.f), s, ss, m, regs, 0, .none, 0, 1, true);
ui.strokes = false;
u.unclip();
ss.mini[0] = ui.collected;
ss.mini_key[0] = key;
}
var current: ?f32 = null;
for (0..count) |i| {
const index = if (s.source) ss.aligned[i].instruction else i;
if (index) |j| if (ss.code[j].addr == regs.rip) {
current = @floatFromInt(i);
};
}
canim.minimap.liveStrip(u, ss.mini[0].runs[0..ss.mini[0].n_runs], .{ .x = 1528, .y = 32, .w = 46, .h = 668 }, 1520, count, ss.scroll[0], 27, current);
}
}
fn drawRegisters(u: ui.Ui, s: *const State, ss: *Session, m: zt.store.Moment, regs: zt.Regs, heat: ui.Heat, seconds: f32, fire: f32) void {
_ = u.panel(588, 708, "registers / call stack", .{ .note = u.print("{d} changed", .{@popCount(ss.changed_regs)}) });
u.clip(.{ .x = 2, .y = 30, .w = 584, .h = 674 });
for (zt.Regs.names[0..16], 0..) |reg, i| {
const changed = ss.changed_regs & (@as(u32, 1) << @intCast(i)) != 0;
_ = canim.rows.reg(u, 34 + @as(f32, @floatFromInt(i)) * 24, 588, reg, m, regs.named(reg).?, .{ .first = if (changed) heat else .none, .extend = if (changed) seconds else null, .most = s.depth });
}
u.current(.{ .x = 1, .y = 430, .w = 586, .h = 24 }, ui.theme.metric.plaque.rip, fire);
u.text(.mono_bold, 20, 14, canim.rows.baseline(430), "rip", ui.theme.mark(.current).on_solid);
_ = canim.rows.chain(u, 74, 430, m, regs.rip, 576, .{});
canim.rows.ripFlags(u, 224, 458, regs.eflags, ss.changed_flags, heat.corner, if (ss.branch != null) flagsReadAt(m, regs.rip) else 0, 1, .{ .moment = m, .pc = regs.rip, .eflags = regs.eflags });
u.line(.{ 12, 484 }, .{ 576, 484 }, ui.theme.color.rule, 1);
u.text(.sans_medium, 18, 14, 510, "call stack", ui.theme.color.text.primary);
var frame_buf: [10]zt.Debugger.Frame = undefined;
const frames = zt.telescope.frames(m, ®s, &frame_buf) catch &.{};
for (frames[0..@min(frames.len, 3)], 0..) |frame, i| {
const y = 522 + @as(f32, @floatFromInt(i)) * 52;
if (i == 0) canim.rows.callCurrent(u, y, 588);
u.text(.mono, 20, 12, canim.rows.baseline(y), u.print("{d}", .{i}), if (i == 0) ui.theme.mark(.current).on_solid else ui.theme.color.text.dim);
_ = u.address(60, canim.rows.baseline(y), frame.pc, .{ .ink = ui.rgb(ui.theme.color.region.code_) });
var buf: [256]u8 = undefined;
const name_ = (m.string(frame.pc, &buf) catch null) orelse "anonymous code";
u.text(.mono, 18, 60, y + 44, canim.rows.fitted(u, ui.symbol(u, name_), 510), ui.theme.color.text.dim);
}
u.unclip();
}
fn stackRows(u: ui.Ui, s: *const State, ss: *Session, m: zt.store.Moment, regs: zt.Regs, first: f32, heat: ui.Heat, seconds: f32, miniature: bool) void {
const start: usize = if (miniature) 0 else @intFromFloat(@floor(@max(0, first)));
const count: usize = if (miniature) 128 else 17;
const base = bases[1] & ~@as(u64, 7);
var slots: [128]zt.telescope.Slot = undefined;
_ = zt.telescope.slots(m, ®s, base +| start * 8, slots[0..@min(count, 128 - start)]) catch {
@memset(&slots, .{});
};
for (start..@min(start + count, 128)) |i| {
const addr = base +| i * 8;
const y = (if (miniature) @as(f32, 0) else 32) + (@as(f32, @floatFromInt(i)) - first) * 24;
const changed = addr >= regs.rsp -| 24 and i < 11 and base == ((regs.rsp -| 24) & ~@as(u64, 7)) and ss.changed_stack & (@as(u16, 1) << @as(u4, @intCast(i))) != 0;
const touched = if (ss.touch) |touch| touch.how & 1 != 0 and addr < touch.addr +| touch.len and addr +| 8 > touch.addr else false;
const pins: []const []const u8 = if (addr == regs.rsp and addr == regs.rbp) &.{ "rsp", "rbp" } else if (addr == regs.rsp) &.{"rsp"} else if (addr == regs.rbp) &.{"rbp"} else &.{};
const role = canim.Stack.slotName(u, slots[i - start], (canim.Recording.word(m, addr, 8) catch null) orelse 0);
_ = canim.rows.slot(u, y, if (s.maps[1]) 1012 else 1072, m, addr, @bitCast(addr -% regs.rsp), pins, 1, .{ .first = if (changed) heat else .none, .read = if (touched) 1 else null, .read_shows = 1, .lead = ui.shared(&.{ base, base +| 1024 }), .extend = if (changed) seconds else null, .most = s.depth }, role.name, role.local, null);
}
}
fn drawStack(u: ui.Ui, s: *const State, ss: *Session, m: zt.store.Moment, regs: zt.Regs, heat: ui.Heat, seconds: f32) void {
extents[1] = 113;
_ = u.panel(1072, 400, "stack / telescope", .{ .focus = s.focus == .stack });
u.text(.sans, 17, 235, 19, u.print("{d} pointer hops", .{s.depth}), ui.theme.color.text.dim);
paneButtons(u, s, .stack);
u.clip(.{ .x = 2, .y = 30, .w = if (s.maps[1]) 1012 else 1068, .h = 362 });
stackRows(u, s, ss, m, regs, ss.scroll[1], heat, seconds, false);
var frame_buf: [10]zt.Debugger.Frame = undefined;
const frames = zt.telescope.frames(m, ®s, &frame_buf) catch &.{};
const low = (bases[1] & ~@as(u64, 7)) + @as(u64, @intFromFloat(ss.scroll[1])) * 8;
for (frames, 0..) |frame, k| {
const from = @max(frame.low, low);
const to = @min(frame.high, low + 15 * 8);
if (from >= to) continue;
canim.rows.span(u, if (s.maps[1]) 1012 else 1072, 32 + @as(f32, @floatFromInt((from - low) / 8)) * 24, 32 + @as(f32, @floatFromInt((to - low) / 8)) * 24, u.print("#{d}", .{k}), k == 0, frame.high > low + 15 * 8);
}
u.unclip();
if (s.maps[1]) {
const key = (m.index + 1) *% 0x9e3779b97f4a7c15 ^ bases[1] ^ (@as(u64, s.depth) << 32) ^ (@as(u64, @intFromBool(s.source)) << 40) ^ (@as(u64, @intFromBool(s.ascii)) << 41);
if (ss.mini_key[1] != key) {
ui.collected.reset();
ui.strokes = true;
u.clip(.{ .x = 0, .y = 0, .w = 0, .h = 0 });
stackRows(ui.Ui.init(u.f), s, ss, m, regs, 0, .none, seconds, true);
ui.strokes = false;
u.unclip();
ss.mini[1] = ui.collected;
ss.mini_key[1] = key;
}
canim.minimap.liveStrip(u, ss.mini[1].runs[0..ss.mini[1].n_runs], .{ .x = 1020, .y = 32, .w = 46, .h = 360 }, 1012, 128, ss.scroll[1], 15, if (regs.rsp >= bases[1] and regs.rsp < bases[1] +| 1024) @as(f32, @floatFromInt((regs.rsp - bases[1]) / 8)) else null);
}
}
fn memoryRow(u: ui.Ui, m: zt.store.Moment, addr: u64, y: f32, room: f32, ascii: bool, touch: ?canim.Recording.Touch, heat: ui.Heat, before: ?zt.store.Moment) void {
var values: [16]u8 = @splat(0);
var known: [16]bool = @splat(false);
m.read(addr, &values, &known) catch {};
var previous: [16]u8 = @splat(0);
var was_known: [16]bool = @splat(false);
if (before) |b| b.read(addr, &previous, &was_known) catch {};
var runs: [2]canim.rows.Run = undefined;
for (&runs, 0..) |*run, i| {
const offset = i * 8;
const word = std.mem.readInt(u64, values[offset..][0..8], .little);
var changed: ?[2]u8 = null;
for (offset..offset + 8) |j| if (known[j] and was_known[j] and values[j] != previous[j]) {
if (changed == null) changed = .{ @intCast(j - offset), @intCast(j - offset) } else changed.?[1] = @intCast(j - offset);
};
const read: f32 = if (touch) |t| if (t.addr < addr +| offset +| 8 and t.addr +| t.len > addr +| offset) 1 else 0 else 0;
run.* = .{ .first = @intCast(offset), .len = 8, .kind = if (canim.rows.regionOf(m, word) != .none) .pointer else .number, .read = read, .heat = if (changed != null) heat else .none, .changed = changed };
}
const shift = canim.rows.addressRoom(addr);
canim.rows.bytesAt(u, y, addr, &values, &known, &runs, canim.rows.ink(canim.rows.regionOf(m, addr)), 0, false, m, shift);
if (ascii) {
var chars: [16]u8 = undefined;
for (&chars, values, known) |*c, v, k| c.* = if (!k) '?' else if (v >= 32 and v < 127) v else '.';
u.text(.mono, 20, 640 + shift, canim.rows.baseline(y), &chars, ui.theme.color.text.dim);
} else {
const fields = [_]canim.hexdump.Field{ .{ .at = 0, .len = 8, .reads = if (runs[0].kind == .pointer) .pointer else .number }, .{ .at = 8, .len = 8, .reads = if (runs[1].kind == .pointer) .pointer else .number } };
_ = canim.hexdump.readingIn(u, y, shift, m, &values, &known, &fields, room);
}
}
fn memoryRows(u: ui.Ui, s: *const State, ss: *Session, m: zt.store.Moment, first: f32, heat: ui.Heat, miniature: bool) void {
const start: usize = if (miniature) 0 else @intFromFloat(@floor(@max(0, first)));
const count: usize = if (miniature) 128 else 17;
const before = if (m.index > 0) trace.?.moment(m.index - 1) catch null else null;
for (start..@min(start + count, 128)) |i| {
const y = (if (miniature) @as(f32, 0) else 32) + (@as(f32, @floatFromInt(i)) - first) * 24;
memoryRow(u, m, bases[2] +| i * 16, y, if (s.maps[2]) 1036 else 1096, s.ascii, ss.touch, heat, if (miniature) null else before);
}
}
fn drawMemory(u: ui.Ui, s: *const State, ss: *Session, m: zt.store.Moment, heat: ui.Heat) void {
extents[2] = 113;
_ = u.panel(1096, 400, "memory / bytes", .{ .focus = s.focus == .memory });
u.text(.sans, 17, 220, 19, if (s.ascii) "hex · ASCII" else "hex · pointer words", ui.theme.color.text.dim);
paneButtons(u, s, .memory);
u.clip(.{ .x = 2, .y = 30, .w = if (s.maps[2]) 1036 else 1092, .h = 362 });
memoryRows(u, s, ss, m, ss.scroll[2], heat, false);
u.unclip();
if (s.maps[2]) {
const key = (m.index + 1) *% 0x9e3779b97f4a7c15 ^ bases[2] ^ (@as(u64, s.depth) << 32) ^ (@as(u64, @intFromBool(s.source)) << 40) ^ (@as(u64, @intFromBool(s.ascii)) << 41);
if (ss.mini_key[2] != key) {
ui.collected.reset();
ui.strokes = true;
u.clip(.{ .x = 0, .y = 0, .w = 0, .h = 0 });
memoryRows(ui.Ui.init(u.f), s, ss, m, 0, .none, true);
ui.strokes = false;
u.unclip();
ss.mini[2] = ui.collected;
ss.mini_key[2] = key;
}
canim.minimap.liveStrip(u, ss.mini[2].runs[0..ss.mini[2].n_runs], .{ .x = 1044, .y = 32, .w = 46, .h = 360 }, 1036, 128, ss.scroll[2], 15, if (ss.touch) |t| if (t.addr >= bases[2] and t.addr < bases[2] +| 2048) @as(f32, @floatFromInt((t.addr - bases[2]) / 16)) else null else null);
}
}
fn ties(u: ui.Ui, s: *const State, ss: *Session, m: zt.store.Moment, travel: f32) void {
_ = m;
const hovered = addressAt(u.f.in.mouse_x, u.f.in.mouse_y);
const address = if (hovered) |hit| hit.address else if (ss.touch) |touch| touch.addr else return;
var from: ?Box = if (hovered) |hit| hit.box else null;
for (hits[0..hit_count]) |hit| if (hit.address == address) {
if (from == null) {
from = hit.box;
continue;
}
const a = from.?;
const b = hit.box;
if (a.x == b.x and a.y == b.y) continue;
u.read(.{ .x = b.x, .y = b.y, .w = b.w, .h = b.h }, 1);
if (s.annotations and a.y < 796 and b.y > 812) {
const lane: f32 = 804;
u.route(&.{ .{ a.x + a.w, a.y + a.h / 2 }, .{ 1616, a.y + a.h / 2 }, .{ 1616, lane }, .{ if (b.x < 1100) 16 else 1112, lane }, .{ if (b.x < 1100) 16 else 1112, b.y + b.h / 2 }, .{ if (b.x < 1100) 26 else 1122, b.y + b.h / 2 } }, ui.theme.mark(.read).solid, .{ .width = 1.5, .head = .{ 6, 6 }, .progress = if (hovered != null) 1 else travel });
break;
}
};
if (hovered) |h| u.read(.{ .x = h.box.x, .y = h.box.y, .w = h.box.w, .h = h.box.h }, 1);
}
fn overlays(u: ui.Ui, s: *const State, ss: *Session, moment: ?zt.store.Moment) void {
const ink = ui.theme.color.text;
if (!s.lens) ss.lens_previous = 0;
if (s.lens and moment != null) {
const address = if (s.lens_address != 0) s.lens_address else s.pc;
if (ss.lens_previous != address) {
ss.lens_at = u.f.in.now;
ss.lens_previous = address;
}
const seconds = @as(f32, @floatFromInt(u.f.in.now -| ss.lens_at)) / 1e9;
const amount = ui.theme.ease(.standard, std.math.clamp(seconds / 0.2, 0, 1));
const resting_y: f32 = if (s.lens_origin.y >= 592 and s.lens_origin.y < 780 and s.lens_origin.x + s.lens_origin.w > 1090) 300 else 592;
const box: Box = .{ .x = 1090, .y = resting_y + 8 * (1 - amount), .w = 1108, .h = 188 };
lens_box = box;
lens_hits = hit_count;
const v = u.moved(box.x, box.y).faded(amount);
v.fill(.{ .x = 14 * amount, .y = 18 * amount, .w = box.w, .h = box.h }, ui.mix(ui.theme.color.page, 0x000000, 0.2));
v.fill(.{ .x = 0, .y = 0, .w = box.w, .h = box.h }, ui.theme.color.lift);
v.fill(.{ .x = 0, .y = 0, .w = box.w, .h = 28 }, ui.theme.color.lift_tag);
v.frame(.{ .x = 0, .y = 0, .w = box.w, .h = box.h }, ui.theme.color.rule, 1);
v.text(.sans_medium, 18, 14, 21, "address lens", ink.primary);
_ = v.address(158, 21, address, .{ .ink = ui.rgb(canim.rows.ink(canim.rows.regionOf(moment.?, address))) });
const first = (address & ~@as(u64, 15)) -| 32;
var bytes: [80]u8 = undefined;
var known: [80]bool = @splat(false);
moment.?.read(first, &bytes, &known) catch {};
v.text(.sans, 17, 370, 21, if (std.mem.indexOfScalar(bool, &known, true) == null) "Bytes were not captured at this stop · Esc closes" else "click a pointer to follow · right-click to navigate · Esc closes", ink.dim);
button(v, box.w - 40, -2, .{ .kind = .close_lens }, "Close address lens · Esc / z", true, true);
v.clip(.{ .x = 2, .y = 30, .w = box.w - 4, .h = box.h - 32 });
for (0..5) |i| memoryRow(v, moment.?, first +| i * 16, 34 + @as(f32, @floatFromInt(i)) * 24, box.w - 12, false, null, .none, null);
const row_y = 34 + @as(f32, @floatFromInt((address - first) / 16)) * 24;
const shift = canim.rows.addressRoom(address);
v.read(.{ .x = canim.rows.byteX(@intCast(address % 16)) + shift - 3, .y = row_y, .w = 28, .h = 24 }, amount);
v.unclip();
for (hits[0..hit_count]) |hit| if (hit.address == address and !contains(box, hit.box.x, hit.box.y)) {
u.read(.{ .x = hit.box.x, .y = hit.box.y, .w = hit.box.w, .h = hit.box.h }, amount);
if (hit.box.y > box.y + box.h and hit.box.y < 796) {
u.route(&.{ .{ hit.box.x, hit.box.y + hit.box.h }, .{ 1616, hit.box.y + hit.box.h }, .{ 1616, box.y + box.h } }, ui.theme.mark(.read).solid, .{ .width = 2, .head = null, .progress = amount });
} else if (hit.box.y > box.y + box.h) {
const lane: f32 = if (hit.box.x < 1100) 16 else 1112;
u.route(&.{ .{ hit.box.x, hit.box.y + hit.box.h }, .{ lane, hit.box.y + hit.box.h }, .{ lane, 804 }, .{ box.x + 176, 804 }, .{ box.x + 176, box.y + box.h } }, ui.theme.mark(.read).solid, .{ .width = 2, .head = null, .progress = amount });
} else {
const lane: f32 = if (hit.box.x >= 1628) 2228 else 1616;
u.route(&.{ .{ hit.box.x + hit.box.w, hit.box.y + hit.box.h }, .{ lane, hit.box.y + hit.box.h }, .{ lane, box.y - 8 }, .{ box.x + box.w - 24, box.y - 8 }, .{ box.x + box.w - 24, box.y } }, ui.theme.mark(.read).solid, .{ .width = 2, .head = null, .progress = amount });
}
break;
};
}
if (s.help) {
const v = u.moved(690, 190);
v.fill(.{ .x = 0, .y = 0, .w = 860, .h = 480 }, ui.theme.color.lift);
_ = v.panel(860, 480, "Vim keys / debugger", .{ .focus = true });
const lines = [_][]const u8{ "s single machine instruction n next source line", "c continue Esc / Ctrl-c pause", "j / k scroll h / l source columns gg / G ends", "Ctrl-d / u half page Ctrl-w h/j/k/l focus pane", "Ctrl-o / i address history Mouse4 / 5 back / forward", "[ / ] recorded stops Shift-f live stop f follow pane", "v source columns m minimap a operand annotations", "b breakpoint at PC z address lens Esc close lens", "Click address: lens Right-click: code / telescope / hex", ": commands goto code|stack|memory ADDRESS", "break FUNCTION break *ADDRESS clear ID signal 0" };
for (lines, 0..) |line, i| v.text(.mono, 18, 22, 64 + @as(f32, @floatFromInt(i)) * 36, line, ink.primary);
}
if (s.command_open) {
u.fill(.{ .x = 24, .y = 1218, .w = 2192, .h = 40 }, ui.theme.color.lift);
u.text(.mono, 22, 36, 1247, u.print(":{s}▏", .{s.command_text[0..s.command_len]}), ink.primary);
}
if (tooltip) |b| {
u.fill(.{ .x = 596, .y = 42, .w = 1020, .h = 32 }, ui.theme.color.page);
u.text(.sans_medium, 18, 608, 64, u.print("{s}{s}", .{ b.label, if (b.enabled) "" else " · unavailable in this state" }), if (b.enabled) ink.primary else ink.dim);
}
}
fn changedHeat(seconds: f32) ui.Heat {
const timing = ui.theme.time.strike;
const cool = std.math.clamp((seconds - timing.hold) / timing.cool, 0, 1);
return .{ .level = 1, .solid = 1 - ui.theme.ease(.standard, cool), .in = std.math.clamp(seconds / timing.in, 0, 1), .out = if (seconds < timing.pulse) ui.theme.pulse(seconds / timing.pulse) else 0, .corner = std.math.clamp((seconds - timing.hold - timing.cool + timing.corner) / timing.corner, 0, 1) };
}
fn flagsReadAt(m: zt.store.Moment, pc: u64) u16 {
var buf: [1024]u8 = undefined;
const insn = (zt.telescope.instruction(m, pc, &buf) catch null) orelse return 0;
const row: canim.disasm.Insn = .{ .addr = pc, .len = insn.len, .text = insn.text };
return canim.disasm.flagsRead(row.mnemonic());
}
fn rowOf(m: zt.store.Moment, start: u64, address: u64) ?usize {
var pc = start;
for (0..24) |i| {
if (pc == address) return i;
var buf: [1024]u8 = undefined;
const insn = (zt.telescope.instruction(m, pc, &buf) catch null) orelse return null;
pc += insn.len;
}
return null;
}
fn selected(ss: *Session, t: *zt.Trace, m: zt.store.Moment, regs: zt.Regs, tid: u32, low: u64, now: u64) void {
if (rowOf(m, ss.code_start, regs.rip) == null) {
ss.code_start = regs.rip;
for (0..6) |_| {
var buf: [1024]u8 = undefined;
const insn = (zt.telescope.instruction(m, ss.code_start, &buf) catch null) orelse break;
if (insn.back == 0) break;
ss.code_start -|= insn.back;
}
}
ss.shown = m.index;
ss.tid = tid;
ss.changed_at = now;
ss.changed_regs = 0;
ss.changed_stack = 0;
ss.changed_flags = 0;
ss.trail = @splat(0);
ss.result_at = null;
for (0..@min(m.index, 3)) |k| {
const previous = t.moment(m.index - 1 - k) catch continue;
const before = (previous.regs(tid) catch null) orelse continue;
ss.trail[k] = before.rip;
if (k != 0) continue;
var code: [1024]u8 = undefined;
if ((zt.telescope.instruction(previous, before.rip, &code) catch null)) |insn| {
if (before.rip + insn.len == regs.rip) ss.result_at = before.rip;
}
for (zt.Regs.names[0..16], 0..) |reg, i| if (regs.named(reg).? != before.named(reg).?) {
ss.changed_regs |= @as(u32, 1) << @intCast(i);
};
ss.changed_flags = regs.eflags ^ before.eflags;
for (0..11) |i| {
const addr = low + i * 8;
const a = (canim.Recording.word(previous, addr, 8) catch null) orelse continue;
const b = (canim.Recording.word(m, addr, 8) catch null) orelse continue;
if (a != b) ss.changed_stack |= @as(u16, 1) << @intCast(i);
}
}
ss.touch = null;
ss.branch = null;
var events = m.events(gpa) catch return;
defer events.deinit(gpa);
while (events.next()) |event| {
if (event.ev.tid != tid) continue;
const e = event.ev;
if (e.kind == .touch and ss.touch == null) ss.touch = .{ .addr = e.a[0], .len = e.a[1], .how = @intCast(e.aux & 3) };
if (e.kind == .branch) ss.branch = .{ .to = e.a[0], .taken = e.a[1] != 0, .kind = switch (e.aux) {
1 => .call,
2 => .jump,
3 => .ret,
else => .none,
} };
}
}
|