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
|
//! The `syscalls` scene's `open`, built by Zig against musl, static: no
//! PLT and no GOT, and musl compiled from its source with the program, so
//! the source panel follows the pc all the way down: `main`'s line, musl's
//! `open.c`, `__syscall_cp.c`, and the one line of `syscall_arch.h` that
//! is the `syscall`; the kernel's window at the floor, and back up.
//!
//! How it moves: as `syscalls` (design/motion.md, "A call followed down",
//! "A system call"); each window opened by `descend`, the source fading
//! through to each file it comes to.
const std = @import("std");
const anim = @import("anim");
const canim = @import("canim");
const recordings = @import("recordings");
pub const background = canim.background;
pub const scene = anim.Live(frame, struct {});
/// The first moment from `from` whose instruction is `text`.
fn on(rec: *canim.Recording, from: u64, text: []const u8) u64 {
var buf: [256]u8 = undefined;
var i = from;
while (i < rec.end()) : (i += 1) if (rec.instruction(i, rec.register(i, "rip"), &buf)) |here| if (std.mem.startsWith(u8, here.text, text)) return i;
return rec.end();
}
/// The first moment from `from` on line `line` of a file not the program's own.
fn libLine(rec: *canim.Recording, from: u64, line: u32) u64 {
var i = from;
while (i < rec.end()) : (i += 1) if (rec.shown[i].here and rec.shown[i].src != 0 and rec.shown[i].line == line) return i;
return rec.end();
}
fn frame(s: *anim.Frame) void {
const rec = canim.open(recordings.musl);
const main = rec.call("main", 0);
var cur = canim.cursor(s, rec, main.enter);
const call_open = on(rec, main.enter, "call");
const call_cp = on(rec, call_open + 1, "call");
const jump = on(rec, call_cp, "jmp");
const crossing = on(rec, jump, "syscall");
const ret_cp = on(rec, crossing, "ret");
const ret_open = on(rec, ret_cp + 1, "ret");
const done = ret_open + 1;
cur.journey(.moment, done);
cur.levels = .{ .source_rows = 8, .fire = .disasm };
const ctx = canim.context(s, cur, .{ .split = canim.layout.split.beside, .syscalls = true, .descent = true });
const log = canim.crossing(s, .{ .cursor = cur, .stage = ctx.stage, .focus = true });
ctx.map.add(s, canim.Crossing{ .cursor = cur, .stage = ctx.map.stage });
var says: canim.Narrator = .{};
const t = canim.theme.time;
const joins = ctx.ties(s);
canim.establish(s, .{ ctx.clock, ctx.registers, ctx.stack, ctx.source, ctx.disasm, log, joins });
says.line(s, "The same `hello`, built by Zig against musl, static.");
cur.walk(s, .line, rec.lineMoment(6, main.enter));
says.line(s, "`main` calls `open` itself: no PLT stub, no GOT.\nmusl is compiled from its source with the program.");
cur.walk(s, .moment, call_open);
canim.descent.step(cur, s, .moment);
s.wait(t.phrase_rest);
says.line(s, "musl's `open` is open.c: the source follows it.");
cur.hurry(s, .moment, libLine(rec, call_open, 9), .{});
says.line(s, "Its line 16 calls `__syscall_cp`,\nin __syscall_cp.c.");
cur.hurry(s, .moment, call_cp, .{});
canim.descent.step(cur, s, .moment);
s.wait(t.phrase_rest);
says.line(s, "Which jumps on to `__syscall_cp_c`, in the same file.");
// __syscall_cp's few rows, and its jump on.
while (cur.moment(s) <= jump) canim.descent.step(cur, s, .moment);
s.wait(t.phrase_rest);
says.line(s, "The `syscall` is one line of C, in syscall_arch.h:\nrax holds 2, open's number, and rdi the path.");
cur.hurry(s, .moment, crossing - 2, .{});
cur.walk(s, .moment, crossing);
says.line(s, "The kernel opens the file and answers in rax: 3.");
canim.descent.step(cur, s, .moment);
s.wait(t.phrase_rest);
cur.hurry(s, .moment, ret_cp, .{});
canim.descent.step(cur, s, .moment);
s.wait(t.phrase_rest);
says.line(s, "Back in open.c, which returns the 3 to `main`.");
cur.hurry(s, .moment, ret_open, .{});
canim.descent.step(cur, s, .moment);
s.wait(t.phrase_rest);
says.close(s);
}
|