diff options
Diffstat (limited to 'design/mock/context.py')
| -rw-r--r-- | design/mock/context.py | 369 |
1 files changed, 330 insertions, 39 deletions
diff --git a/design/mock/context.py b/design/mock/context.py index 96d4484..64603a2 100644 --- a/design/mock/context.py +++ b/design/mock/context.py @@ -161,6 +161,9 @@ def chain(g, x, y, links, end, marks=None, room=None, lead=0): x += named if end is None: return where + if end[0] == "int" and links and room is not None and x + 30 + len("%x" % end[1]) * A + 24 > room: + more(g, x, y) # the value does not fit: the chain ends early, and says so + return where x = value_arrow(g, x, y) if links else x if marks.get("end") == "tick": # the value it holds changed within three steps g.rect(x - 7, y + 3, 4, ROW - 6, VERM) @@ -238,39 +241,50 @@ def registers(g, y, regs, flags, marks): return where -def stack(g, y, rows, frames): +def stack(g, y, rows, frames, note=None): """rows: (slot, pins, links, end[, marks]). frames: (name, first row, last row), newest first: the - backtrace, drawn on the words that are each frame. The window is the newest frame's, from rsp. - Returns each row's y.""" - y0 = panel(g, RX, y, CW, len(rows), "stack", note="%s words from rsp · the frames they belong to" % - {6: "six", 8: "eight"}[len(rows)]) + backtrace, drawn on the words that are each frame; a first row under 0 runs above the window. The window + is the newest frame's. A frame's name is never cut for a chain: the chains on the rows it sits on end 12 + before it. Returns each row's y.""" + y0 = panel(g, RX, y, CW, len(rows), "stack", note=(note or "%s words from rsp" % {6: "six", 8: "eight"}[len(rows)]) + + " · the frames they belong to") lead = shared([r[0] for r in rows]) + room = {} # a row's chain ends here, where a frame's name begins + spans = [] + for i, (name, first, last) in enumerate(frames): + if first >= len(rows): + break + open_top, open_ = first < 0, last >= len(rows) + first, last = max(first, 0), min(last, len(rows) - 1) + x, ya, yb = RX + CW - 14, y0 + first * ROW + (0 if open_top else 3), y0 + (last + 1) * ROW - (0 if open_ else 3) + ny = (ya + yb) / 2 + 6 # the name's baseline, and the rows its letters cover + nx = x - 14 - tw(name, 18, "sansm") + for r in range(len(rows)): + if y0 + r * ROW < ny + 4 and y0 + (r + 1) * ROW > ny - 14: + room[r] = min(room.get(r, RX + CW - 136), nx - 12) + spans.append((i, name, x, ya, yb, ny, open_top, open_)) ys = [] for i, (slot, pins, links, end, *marks) in enumerate(rows): yy = y0 + i * ROW ys.append(yy) if pins: pin(g, RX + 10, yy, pins, STACK) - chain(g, RX + 122, yy, [(slot, STACK)] + links, end, marks[0] if marks else None, room=RX + CW - 136, - lead=lead) - for i, (name, first, last) in enumerate(frames): - if first >= len(rows): - break + chain(g, RX + 122, yy, [(slot, STACK)] + links, end, marks[0] if marks else None, + room=room.get(i, RX + CW - 136), lead=lead) + for i, name, x, ya, yb, ny, open_top, open_ in spans: ink = GOLD if i == 0 else RULE - open_ = last >= len(rows) - last = min(last, len(rows) - 1) - x, ya, yb = RX + CW - 14, y0 + first * ROW + 3, y0 + (last + 1) * ROW - (0 if open_ else 3) g.rect(x - 1, ya, 2, yb - ya, ink) - g.rect(x - 7, ya, 8, 2, ink) + if not open_top: + g.rect(x - 7, ya, 8, 2, ink) if not open_: g.rect(x - 7, yb - 2, 8, 2, ink) - g.t(x - 14, (ya + yb) / 2 + 6, name, 18, BRIGHT if i == 0 else DIM, "sansm", "end") + g.t(x - 14, ny, name, 18, BRIGHT if i == 0 else DIM, "sansm", "end") return ys # ---- the program column ------------------------------------------------------------- C_WORDS = {"struct", "long", "static", "return", "while", "for", "int", "char", "sizeof", "if", "else", "void", "NULL", - "__attribute__", "noinline"} + "__attribute__", "noinline", "const", "unsigned", "do", "elif", "defined"} def c_line(g, x, y, text, limit): @@ -280,10 +294,13 @@ def c_line(g, x, y, text, limit): if text.lstrip().startswith("//"): g.t(x, b(y), text, S, COMMENT, ITAL) return - for m in re.finditer(r'"[^"]*"?|#\w+|<[\w.]+>|\w+|\s+|.', text): + for m in re.finditer(r'/\*.*?(\*/|$)|"[^"]*"?|#\w+|<[\w.]+>|\w+|\s+|.', text): s = m.group() if not s.strip(): continue + if s.startswith("/*"): # a comment, to the line's end or its close + g.t(x + m.start() * A, b(y), s, S, COMMENT, ITAL) + continue ink, face = VELLUM, MONO if s in C_WORDS or s.startswith("#"): ink, face = KW, BOLD @@ -294,32 +311,39 @@ def c_line(g, x, y, text, limit): g.t(x + (limit - 1) * A, b(y), "…", S, FAINT, MONO) +def wide(total): + """How much wider than three characters the program's largest line number is: the same in every window.""" + return max(len(str(total)) - 3, 0) * A + + def source(g, y, rows, lines, first, cur, trail, name, total): - """Returns the y of the current line's row.""" + """Returns the y of the current line's row. Numbers as wide as the file's last: the text moves by the extra.""" + w = wide(total) y0 = panel(g, LX, y, CW, rows, name, note=f"lines {first} to {first + rows - 1} of {total}") for i in range(rows): n = first + i yy = y0 + i * ROW if n == cur: g.rect(LX + 2, yy, CW - 4, ROW, EMBER[GOLD]) - g.rect(LX + 2, yy, 50, ROW, GOLD) - g.t(LX + 46, b(yy), str(n), S, INK, BOLD, "end") + g.rect(LX + 2, yy, 50 + w, ROW, GOLD) + g.t(LX + 46 + w, b(yy), str(n), S, INK, BOLD, "end") else: if n in trail: g.rect(LX + 2, yy + 1, 4, ROW - 2, TRAIL[min(trail.index(n), 2)]) - g.t(LX + 46, b(yy), str(n), S, FAINT, MONO, "end") - c_line(g, LX + 64, yy, lines[n - 1], (CW - 64 - 12) // A) + g.t(LX + 46 + w, b(yy), str(n), S, FAINT, MONO, "end") + c_line(g, LX + 64 + w, yy, lines[n - 1], (CW - 64 - w - 12) // A) return y0 + (cur - first) * ROW DX_ADDR, DX_LINE, DX_MN, DX_OPS, DX_NOTE = 40, 134, 180, 264, 564 -def disasm(g, y, rows, func, cur_line, jump=None): +def disasm(g, y, rows, func, cur_line, jump=None, w=0): """rows: (address, line, mnemonic, operands, state, note), in address order: the pc's row and as many above as below. state: cur | trail0..2 (it ran just now) | out (past the function's end) | None. note, the row's annotation: ("chain", links, end, marks) | ("set", register, links, end) | ("text", s). - jump: (from, to): a jump that was taken, drawn in the lane. + jump: (from, to): a jump that was taken, drawn in the lane. w: how much wider than three characters the + program's line numbers are; every column right of the line's moves by it. Returns each address's row y.""" y0 = panel(g, LX, y, CW, len(rows), "disassembly", note=func) lead = shared([r[0] for r in rows if r[4] != "out"]) @@ -339,10 +363,10 @@ def disasm(g, y, rows, func, cur_line, jump=None): addr(g, LX + DX_ADDR, yy, a, mix(CODE, PAGE, 0.6) if dim else CODE, lead=0 if dim else lead) if line != last_line and not dim: # the line this run of instructions was compiled from on = line == cur_line - g.t(LX + DX_LINE + 3 * A, b(yy), str(line), S, GOLD if on else FAINT, BOLD if on else MONO, "end") + g.t(LX + DX_LINE + 3 * A + w, b(yy), str(line), S, GOLD if on else FAINT, BOLD if on else MONO, "end") last_line = line - g.t(LX + DX_MN, b(yy), mn, S, FAINT if dim else KW, MONO if dim else BOLD) - x = LX + DX_OPS + g.t(LX + DX_MN + w, b(yy), mn, S, FAINT if dim else KW, MONO if dim else BOLD) + x = LX + DX_OPS + w for part, ink in ops: if isinstance(part, int): x = addr(g, x, yy, part, ink) @@ -351,7 +375,7 @@ def disasm(g, y, rows, func, cur_line, jump=None): x += len(part) * A if not note: continue - x = max(LX + DX_NOTE, x + A) # operands are never cut where the row has room + x = max(LX + DX_NOTE + w, x + A) # operands are never cut where the row has room if note[0] == "text": g.t(x, b(yy), note[1], S, note[2] if len(note) > 2 else DIM, MONO) elif note[0] == "set": # what an instruction that ran left in a register: a register row, small @@ -376,6 +400,117 @@ def thread(g, ys, spans): # ---- the lens: a list, and beside it the bytes it is drawn from ------------------------- +def fields(g, y, h, about, base, length, flist, mem, region, region_of, marks=()): + """`hexdump` by `fields`: the region a scene names, read by the fields it gives it, in the band's view. + flist: (at, len, reads, big, mark) with reads in number | pointer | text | bytes. mem: {address: byte} of what + the stop shown saw (or the kernel took); a byte not in it is unread. marks: the field marks framed as read. + Rows that hold no field are one `more` row and their count. The bytes begin 12 after the widest address.""" + tag(g, RX, y, CW, h, "its bytes") + g.t(RX + 28 + tw("its bytes", 20, "sansm") + 12, y + 18, about, 18, FAINT, "sans") + total = (length + 15) // 16 + holds = lambda r: any(base + at < base + 16 * (r + 1) and base + at + n > base + 16 * r for at, n, *_ in flist) + shown = [r for r in range(total) if holds(r) or total <= (h - TAG - 2 * PAD) // ROW] + digits = max(len("%x" % (base + 16 * r)) for r in shown) + shift = max(18 + digits * A + 12 - 132, 0) + bx = lambda i: RX + 132 + shift + i * 30 + (12 if i >= 8 else 0) + lead = shared([base + 16 * r for r in shown]) if len(shown) > 1 else 0 + yy, last = y + TAG + PAD, None + for r in shown: + if last is not None and r > last + 1: # the rows left out: one row, and how many + more(g, RX + 18 - 6, yy) + g.t(RX + 18 + 30 + 36, b(yy), "%d rows" % (r - last - 1), S, FAINT, MONO) + yy += ROW + last = r + a0 = base + 16 * r + bracket(g, RX + 7, yy + 3, yy + ROW - 3, mix(region, PAGE, 0.6)) + addr(g, RX + 18, yy, a0, region, lead=lead) + runs, i = [], 0 + while i < 16 and a0 + i < base + length: + f = next((f for f in flist if base + f[0] <= a0 + i < base + f[0] + f[1]), None) + if f: + n = min(base + f[0] + f[1] - (a0 + i), 16 - i) + else: + n = min([base + o[0] - a0 for o in flist if a0 + i < base + o[0] < a0 + 16] + [16, base + length - a0]) - i + runs.append((i, n, f)) + i += n + x = RX + 640 + shift # the reading: a list, a character apart + for i0, n, f in runs: + vals = [mem.get(a0 + k) for k in range(i0, i0 + n)] + reads = f[2] if f else "loose" + x0, x1 = bx(i0) - 4, bx(i0 + n - 1) + 28 + if f: + g.rect(x0, yy + 1, x1 - x0, ROW - 2, RAISED) + if f[4] is not None and f[4] in marks: + g.frame(x0 - 3, yy - 1, x1 - x0 + 6, ROW + 2, READ, 2) + known = None not in vals + word = 0 + if known: + order = vals[::-1] if not (f and f[3]) else vals + for v in order: + word = word << 8 | v + ink = {"number": VELLUM, "bytes": VELLUM, "text": LIT, "loose": DIM, + "pointer": region_of(word) if known else VELLUM}[reads] + nz = [k for k, v in enumerate(vals) if v] + for k, v in enumerate(vals): + xx = bx(i0 + k) + if v is None: # not read at this stop: two dots, on the field's paper + g.rect(xx + 5, yy + 11, 4, 4, FAINT) + g.rect(xx + 15, yy + 11, 4, 4, FAINT) + continue + padded = reads in ("number", "pointer") and nz and (k < nz[0] if f[3] else k > nz[-1]) + g.t(xx, b(yy), "%02x" % v, S, mix(ink, PAGE, 0.45) if padded else ink, MONO) + if not known or reads in ("bytes", "loose"): + continue + if reads == "pointer": + if word == 0: + s_, sink = "NULL", DIM + else: + x = addr(g, link_arrow(g, x, yy), yy, word, region_of(word)) + A + continue + elif reads == "number": + s_, sink = str(word), VELLUM + else: + s_, sink = '"' + bytes(vals).split(b"\0")[0].decode("latin1") + '"', LIT + if x + len(s_) * A > RX + CW - 12: + s_ = s_[:max(int((RX + CW - 12 - x) // A) - 1, 0)] + "…" + g.t(x, b(yy), s_, S, sink, MONO) + x += (len(s_) + 1) * A + yy += ROW + + +def kernel(g, y, h, calls, name, nr, rows, given): + """`syscalls` in the band: the kernel's side, on vellum. calls: (name, result), newest first (the first is + the call in hand, gold: where execution is); rows: (register, argument, value, note); given: the bytes it took.""" + tag(g, LX, y, CW, h, "kernel", note="the other side of a system call") + g.rect(LX + 2, y + TAG, CW - 4, h - TAG - 2, gen.KERNEL) + g.t(LX + 12, y + TAG + 20, "calls so far", 18, gen.KERNEL_DIM, "sans") + for i, (c, ret) in enumerate(calls[:(h - TAG - 40) // 28]): + yy = y + TAG + 28 + i * 28 + if i == 0: + g.rect(LX + 12, yy, 196, 24, GOLD) + g.frame(LX + 12, yy, 196, 24, PAGE, 2) + g.t(LX + 20, b(yy), c, S, PAGE, MONO) + g.t(LX + 200, b(yy), str(ret), S, PAGE, MONO, "end") + x = LX + 236 + g.t(x, y + TAG + 46, name, 35, PAGE, "monob") + g.t(x + len(name) * 21 + 16, y + TAG + 44, "system call %d" % nr, 18, gen.KERNEL_DIM, "sans") + for i, (reg, arg, val, note) in enumerate(rows): + yy = y + TAG + 60 + i * 28 + g.rect(x, yy, 300, 2, gen.KERNEL_RULE) + g.t(x, b(yy + 2), reg, S, gen.KERNEL_DIM, MONO) + g.t(x + 52, b(yy + 2), arg, 20, PAGE, "sansm") + g.t(x + 130, b(yy + 2), val, S, PAGE, MONO) + if note: + g.t(x + 130 + (len(val) + 1) * A, b(yy + 2), note, S, PAGE, MONO) + x = LX + 564 + g.t(x, y + TAG + 20, "the bytes it was given", 18, gen.KERNEL_DIM, "sans") + for i, v in enumerate(given): + xx, yy = x + (i % 10) * 28, y + TAG + 28 + (i // 10) * 30 + g.rect(xx, yy, 26, 26, gen.KERNEL_HELD) + g.frame(xx, yy, 26, 26, PAGE, 2) + g.t(xx + 1, b(yy + 1), "%02x" % v, S, PAGE, MONO) + + def dump_row(g, y, a, runs, lead, mark=None, node=False): """A hexdump row: sixteen bytes and what they read as. runs: (first byte, bytes, kind, value) with kind in value | next | malloc | unread. mark: {"read": run index, "tick": run index}.""" @@ -703,21 +838,26 @@ def calls(g, y, h, tree, made, caller, project_from=None): return cx, cy -def backtrace(g, y, h, frames): - """The view of the calls: the call chain as rows, newest on top. frames: (name, args, file, line).""" - y0 = panel(g, RX, y, CW, (h - TAG - 2 * PAD) // ROW, "backtrace", note="the calls waiting · newest on top") - for i, (name, args, file, line) in enumerate(frames): +def backtrace(g, y, h, frames, x=RX, note="the calls waiting · newest on top", focus=False): + """The call chain as rows, newest on top: the view of the calls, or a lens's drawing (x = LX). frames: (name, + args, file, line), file None where the recording does not know it. More frames than rows: the oldest go, counted.""" + rows = (h - TAG - 2 * PAD) // ROW + if len(frames) > rows: + note += " · %d more" % (len(frames) - rows) + y0 = panel(g, x, y, CW, rows, "backtrace", focus=focus, note=note) + for i, (name, args, file, line) in enumerate(frames[:rows]): yy = y0 + i * ROW if i == 0: - g.rect(RX + 2, yy, CW - 4, ROW, EMBER[GOLD]) - g.rect(RX + 2, yy, 40, ROW, GOLD) - g.t(RX + 12, b(yy), "#0", S, INK, BOLD) + g.rect(x + 2, yy, CW - 4, ROW, EMBER[GOLD]) + g.rect(x + 2, yy, 40, ROW, GOLD) + g.t(x + 12, b(yy), "#0", S, INK, BOLD) else: - g.t(RX + 12, b(yy), "#%d" % i, S, FAINT, MONO) - g.t(RX + 60, b(yy), name, S, VELLUM, MONO) + g.t(x + 12, b(yy), "#%d" % i, S, FAINT, MONO) + g.t(x + 60, b(yy), name, S, VELLUM, MONO) if args: - g.t(RX + 384, b(yy), args, S, VELLUM, MONO) - g.t(RX + CW - 12, b(yy), "%s:%d" % (file, line), S, DIM, MONO, "end") + g.t(x + 384, b(yy), args, S, VELLUM, MONO) + if file: + g.t(x + CW - 12, b(yy), "%s:%d" % (file, line), S, DIM, MONO, "end") # ---- the data: moments 752 and 753 of the fib scene (anim/zig-out/recordings/steps.ztrace) ----- @@ -792,6 +932,155 @@ def frame_calls(name): g.save(name) +# ---- the data: the sqlite scene (anim/scenes/sqlite.zig), its stops 120 and 169 --------------------- +SQ = "/home/goblin/05-genizah/sqlite/sqlite-amalgamation-3500400/sqlite3.c" +SQ_LINES = {n: l for n, l in enumerate(open(SQ, errors="replace").read().split("\n"), 1) if 42280 <= n <= 42300 or 80885 <= n <= 80905} +SQ_TOTAL = 262899 + + +class Lines(dict): + """The source's lines a mock shows, by number (sqlite3.c is 262 899 of them).""" + def __getitem__(self, i): + return SQ_LINES.get(i + 1, "") + + +def sq_region(v): + """Where a value of the sqlite recording points, by its mappings: the ink of its digits.""" + if 0x7ffffffde000 <= v < 0x7ffffffff000: + return S_ + if 0x517000 <= v < 0x55b000: + return H_ + if 0x510000 <= v < 0x517000: + return D_ + if 0x402000 <= v < 0x4ed000 or 0x7ffff7c24000 <= v < 0x7ffff7d9f000: + return C_ + return VELLUM + + +SQ_FLAGS_120 = [("CF", 0, 0), ("PF", 0, 1), ("AF", 0, 0), ("ZF", 0, 1), ("SF", 0, 0), ("IF", 1, 0), ("DF", 0, 0), ("OF", 0, 0)] +SQ_FLAGS_169 = [("CF", 1, 1), ("PF", 1, 1), ("AF", 0, 0), ("ZF", 0, 0), ("SF", 0, 0), ("IF", 1, 0), ("DF", 0, 0), ("OF", 0, 0)] +E980 = [(0x7fffffffe980, S_), (0x7fffffffeb90, S_), (0x7fffffffec60, S_), (0x7fffffffece0, S_), (0x7fffffffed00, S_)] +E7E0 = [(0x7fffffffe7e0, S_), (0x7fffffffe820, S_), (0x7fffffffe860, S_), (0x7fffffffe8d0, S_), (0x7fffffffe940, S_)] +SQ_REGS_120 = [ + ("rax", [(0x5226d0, H_)], ("int", 0)), ("rbx", [], ("int", 1)), ("rcx", [], ("int", 0)), ("rdx", [], ("int", 8)), + ("rsi", [(0x7fffffffea60, S_)], ("int", 0)), ("rdi", [(0x521978, H_)], ("int", 0x101)), + ("rbp", E980, None), ("rsp", E980, None), ("r8", [], ("int", 0x38)), ("r9", [], ("int", 0)), + ("r10", [], ("int", 7)), ("r11", [], ("int", 0xffffffff)), ("r12", [], ("int", 0)), + ("r13", [(0x5222d0, H_)], ("int", 0x39fb80)), ("r14", [(0x5218f8, H_)], ("int", 0x40100010000)), + ("r15", [], ("int", 0)), ("rip", [(0x448d84, C_, "sqlite3BtreeInsert+4")], None), +] +PAGE_W = 0xe60f030000000d +SQ_REGS_169 = [ + ("rax", [], ("int", 0x1000)), ("rbx", [], ("int", 3)), ("rcx", [(0x7ffff7cf1a7d, C_, "__pwrite64+173")], None), + ("rdx", [], ("int", 0x1000)), ("rsi", [(0x538b28, H_)], ("int", PAGE_W)), ("rdi", [], ("int", 3)), + ("rbp", E7E0, None), ("rsp", [(0x7fffffffe7a0, S_)], ("int", 0)), ("r8", [], ("int", 0)), ("r9", [], ("int", 0)), + ("r10", [], ("int", 0x1000)), ("r11", [], ("int", 0x246)), ("r12", [], ("int", 0x1000)), + ("r13", [(0x538b28, H_)], ("int", PAGE_W)), ("r14", [], ("int", 0x1000)), ("r15", [(0x538b28, H_)], ("int", PAGE_W)), + ("rip", [(0x422051, C_, "unixWrite+65")], None), +] +EA18 = [(0x7fffffffea18, S_), (0x517378, H_), (0x513010, D_)] +SQ_STACK_120 = [ # from rsp: sqlite3BtreeInsert's saved rbp and return address, then its caller's words + (0x7fffffffe980, ["rbp", "rsp"], E980[1:], None), + (0x7fffffffe988, [], [(0x43e029, C_, "sqlite3VdbeExec+18905")], None), + (0x7fffffffe990, [], [(0x7ffff7c0ed60, VELLUM)], ("int", 0xd001200000f05)), + (0x7fffffffe998, [], EA18, ("int", 0x7800000003)), + (0x7fffffffe9a0, [], [(0x521ed0, H_)], ("int", 0)), + (0x7fffffffe9a8, [], EA18, ("int", 0x7800000003)), +] +SQ_STACK_169 = [ # rsp+20 to rsp+48: the words unixWrite's prologue pushed, its saved rbp, its way back + (0x7fffffffe7c0, [], [], ("int", 0), {"end": "tick"}), + (0x7fffffffe7c8, [], [(0x538b28, H_)], ("int", PAGE_W), {1: "tick"}), + (0x7fffffffe7d0, [], [(0x517e38, H_), (0x513010, D_)], ("int", 0x7800000003), {1: "tick"}), + (0x7fffffffe7d8, [], [], ("int", 2), {"end": "tick"}), + (0x7fffffffe7e0, ["rbp"], E7E0[1:], None), + (0x7fffffffe7e8, [], [(0x42fa75, C_, "pager_write_pagelist+245")], None), +] +SQ_DIS_120 = [(a, 80895, mn, [(ops, P)], "cur" if a == 0x448d84 else None, None) for a, mn, ops in [ + (0x448d80, "push", "rbp"), (0x448d81, "mov", "rbp, rsp"), (0x448d84, "push", "r15"), (0x448d86, "push", "r14"), + (0x448d88, "push", "r13"), (0x448d8a, "push", "r12"), (0x448d8c, "push", "rbx"), (0x448d8d, "sub", "rsp, 0xa8"), + (0x448d94, "mov", "r14d, ecx")]] +SQ_DIS_169 = [(a, 42290, mn, ops, "cur" if a == 0x422051 else None, None) for a, mn, ops in [ + (0x422042, "mov", [("rsi, r15", P)]), (0x422045, "mov", [("rdx, r12", P)]), (0x422048, "mov", [("rcx, r14", P)]), + (0x42204b, "call", [("qword ptr [rip + 0xf1567]", P)]), (0x422051, "mov", [("r13, rax", P)]), + (0x422054, "test", [("r13d, r13d", P)]), (0x422057, "jns", [(0x422070, C_)]), (0x422059, "call", [(0x4021c0, C_)]), + (0x42205e, "mov", [("ecx, dword ptr [rax]", P)])]] +SQ_BT_120 = [("sqlite3BtreeInsert+4", "", "sqlite3.c", 80895), ("sqlite3VdbeExec+18905", "", None, 0), + ("sqlite3_step+910", "", None, 0), ("sqlite3_exec+394", "", None, 0), ("main+56", "", None, 0), + ("libc.so.6+27781", "", None, 0), ("__libc_start_main+137", "", None, 0), ("_start+37", "", None, 0)] +# BtreePayload (the source's lines 16740 to 16748), as the scene gives it: (at, len, reads, big, mark) +PAYLOAD = [(0, 8, "pointer", False, None), (8, 8, "number", False, 0), (16, 8, "pointer", False, None), + (24, 8, "pointer", False, None), (32, 2, "number", False, None), (36, 4, "number", False, 1), + (40, 4, "number", False, None)] +ROW_BYTES = bytes.fromhex("0000000000000000 0100000000000000 884d520000000000 0000000000000000" + "0000000007000000 0000000001000000".replace(" ", "")) +PAGE0 = bytes.fromhex("0d000000030fe6000ff70fef0fe60000") +PAGE_END = bytes.fromhex("0000000000000703021767616d6d61060202156265746107010217616c706861") + + +def leaf(page): + """A table's leaf page, read by SQLite's file format (the scene's `leaf`): the header, the cell pointers, + each cell's length, rowid, record header and text. Every number big-endian, every varint here a byte.""" + out = [(0, 1, "number", False, 0), (1, 2, "bytes", True, 0), (3, 2, "number", True, 0), (5, 2, "bytes", True, 0), + (7, 1, "bytes", False, 0)] + cells = page[3] << 8 | page[4] + out += [(8 + 2 * k, 2, "number", True, 1) for k in range(cells)] + for k in range(cells): + at = page[8 + 2 * k] << 8 | page[9 + 2 * k] + out += [(at, 1, "bytes", False, 2 + k), (at + 1, 1, "number", False, 2 + k), (at + 2, 2, "bytes", False, 2 + k), + (at + 4, page[at] - 2, "text", False, 2 + k)] + return out + + +def frame_sqlite(name, late): + """The sqlite scene at rest. Early (stop 120): the backtrace is the band's drawing, and the row the B-tree + is handed, a struct on the stack, its view. Late (stop 169): pwrite64 has come back; the kernel's side is the + drawing, and the page it took the view, read by SQLite's file format.""" + g = Svg() + sp = SUBJECT + y = TOP + sp["band"] + 12 + yr = TOP + sp["view"] + 12 + lines = Lines() + w = wide(SQ_TOTAL) + if not late: + source(g, y, sp["src"], lines, 80890, 80895, [], "sqlite3.c", SQ_TOTAL) + disasm(g, y + ph(sp["src"]) + 12, SQ_DIS_120, "sqlite3BtreeInsert", 80895, w=w) + ys = y + TAG + PAD + 5 * ROW + changed = {r: {0: "tick"} for r, _, _ in SQ_REGS_120 if r not in ("r12", "rip")} + registers(g, yr, SQ_REGS_120, SQ_FLAGS_120, changed) + stack(g, yr + ph(17) + 12, SQ_STACK_120, [("sqlite3BtreeInsert", 0, 1), ("sqlite3VdbeExec", 2, 9)]) + backtrace(g, TOP, sp["band"], SQ_BT_120, x=LX, note="newest on top", focus=True) + base = 0x7fffffffea60 + fields(g, TOP, sp["view"], "pX, the row: a BtreePayload, at rsi", base, 48, PAYLOAD, + {base + i: v for i, v in enumerate(ROW_BYTES)}, STACK, sq_region, marks=(0, 1)) + clock(g, 9, 58) + gen.caption(g, [[("sqlite3VdbeExec", "mono"), (", the bytecode's loop, calls the B-tree: ",), ("rdi", "mono"), (" is",)], + [("the table's cursor, ",), ("rsi", "mono"), (" the row: rowid 1, a record of 7 bytes.",)]]) + else: + source(g, y, sp["src"], lines, 42286, 42290, [], "sqlite3.c", SQ_TOTAL) + disasm(g, y + ph(sp["src"]) + 12, SQ_DIS_169, "unixWrite", 42290, w=w) + ys = y + TAG + PAD + 4 * ROW + changed = {r: {0: "tick"} for r, _, _ in SQ_REGS_169 if r not in ("r9", "rip")} + registers(g, yr, SQ_REGS_169, SQ_FLAGS_169, changed) + stack(g, yr + ph(17) + 12, SQ_STACK_169, [("unixWrite", -1, 5)], note="rsp+20 to rsp+48") + page = bytes(4096 - len(PAGE_END) - len(PAGE0)) # the middle of the page is not drawn: `more` + page = PAGE0 + page + PAGE_END + kernel(g, TOP, sp["band"], [("pwrite64", 4096), ("pwrite64", 4096), ("fsync", 0), ("pwrite64", 12), + ("close", 0), ("fsync", 0), ("openat", 5), ("fsync", 0)], + "pwrite64", 18, [("rdi", "fd", "3", '"t.db"'), ("rsi", "buf", "538b28", None), + ("rdx", "count", "4096", None), ("r10", "offset", "4096", None), + ("rax", "returns", "4096", None)], PAGE0 + bytes(4)) + base = 0x538b28 + took = {base + i: v for i, v in enumerate(PAGE0)} + took.update({base + 0xfe0 + i: v for i, v in enumerate(PAGE_END)}) + fields(g, TOP, sp["view"], "pBuf: page 2 of t.db, at rsi", base, 4096, leaf(page), took, HEAP, sq_region, + marks=(0, 1)) + clock(g, 58, 58) + gen.caption(g, [[("Page 2 as the kernel took it: ",), ("0d", "mono"), (", a table's leaf; 3 cells;",)], + [("their offsets, ",), ("0ff7 0fef 0fe6", "mono"), (", each a row at the page's end.",)]]) + thread(g, ys, [(y + ph(sp["src"]) + 12 + TAG + PAD, y + ph(sp["src"]) + 12 + TAG + PAD + 9 * ROW)]) + g.save(name) + + if __name__ == "__main__": assert ph(8) + 12 + ph(17) == ph(17) + 12 + ph(8) == BOTTOM - TOP - 152 - 12 # lens beside assert ph(10) + 12 + ph(9) == BOTTOM - TOP - 296 - 12 # lens as subject: the drawing @@ -802,3 +1091,5 @@ if __name__ == "__main__": frame("10-context-step", True) frame("11-context-lens", True, subject=True) frame_calls("12-calls-subject") + frame_sqlite("13-sqlite-row", False) + frame_sqlite("14-sqlite-page", True) |
