#!/usr/bin/env python3 """The default frame: the machine's whole context, pwndbg's way, and the scene's lens. python3 context.py # writes 09-context.svg, 10-context-step.svg, 11-context-lens.svg The moment is real: `ztrace record --by line -- targets/list`, moments 71 and 72 (`ztrace show FILE 71`): every register and its chain, the instructions at the pc, the stack. The lens (the list, and the heap words that are its nodes) is what those chains and the program say the heap holds; a recording made under ptrace has no allocations. """ import re import gen from gen import (PAGE, RAISED, RULE, VELLUM, BRIGHT, DIM, FAINT, INK, GOLD, VERM, READ, ALLOC, EMBER, TRAIL, STACK, HEAP, CODE, DATA, KW, LIT, COMMENT, W, H, Svg, tw) S = 20 # the context's mono size: an advance of 12 A = 12 ROW = 24 TAG = 24 MONO, BOLD, ITAL = "monom", "monob", "monoi" LX, RX, CW = 64, 976, 880 # the two columns TOP, BOTTOM = 48, 888 # the stage def b(y): """Baseline of a row at y.""" return y + ROW / 2 + 0.36 * S def panel(g, x, y, w, rows, title, focus=False, note=None, h=None): """A one-row tag and `rows` rows under it. Returns the y of the first row.""" h = h or TAG + 12 + rows * ROW g.rect(x, y, w, TAG, RAISED) g.frame(x, y, w, h, DIM if focus else RULE, 2) if focus: g.rect(x + 8, y + 6, 12, 12, GOLD) else: g.frame(x + 8, y + 6, 12, 12, FAINT, 2) g.t(x + 28, y + 18, title, 20, VELLUM if focus else DIM, "sansm") if isinstance(note, str): g.t(x + w - 10, y + 18, note, 18, FAINT, "sans", "end") elif note: # runs, each in its own ink: the legend nx = x + w - 10 - sum(tw(s, 18, "sansm") + 14 for s, _, _ in note) + 14 for s, ink, under in note: g.t(nx, y + 18, s, 18, ink, "sansm") if under: g.rect(nx, y + 21, tw(s, 18, "sansm"), 2, ink) nx += tw(s, 18, "sansm") + 14 return y + TAG + 6 # ---- a value, by what it is --------------------------------------------------- def groups(n): """Hex at its natural length, in fours from the right.""" d = "%x" % n head = len(d) % 4 return ([d[:head]] if head else []) + [d[i:i + 4] for i in range(head, len(d), 4)] def addr_w(n): gs = groups(n) return sum(len(s) for s in gs) * A + (len(gs) - 1) * (A // 2) def addr(g, x, y, n, ink, face=MONO, under=False): for s in groups(n): g.t(x, b(y), s, S, ink, face) x += len(s) * A + A // 2 if under: # writable and executable g.rect(x - addr_w(n) - A // 2, y + ROW - 3, addr_w(n), 2, ink) return x - A // 2 def link_arrow(g, x, y, ink=FAINT): """A pointer to: 30 wide.""" g.rect(x + 6, y + 11, 11, 2, ink) g.path(f"M{x + 24},{y + 12} l-8,-5 v10 Z", fill=ink) return x + 30 def value_arrow(g, x, y, ink=FAINT): """Which holds: 30 wide, pointing back.""" g.path(f"M{x + 6},{y + 12} l8,-5 v10 Z", fill=ink) g.rect(x + 13, y + 11, 11, 2, ink) return x + 30 def integer(g, x, y, v, ink=VELLUM): """A number that is not an address: one digit as it is; more, hex with 0x and its decimal after, faint.""" if v < 10: g.t(x, b(y), str(v), S, ink, MONO) return x + A s = "0x%x" % v g.t(x, b(y), s, S, ink, MONO) x += len(s) * A if v <= 0xffff: g.t(x + 8, b(y), str(v), S, FAINT, MONO) x += 8 + len(str(v)) * A return x def chain(g, x, y, links, end, marks=None, room=None): """A telescope: links are (address, ink[, name]); end is ("int", v), ("str", s), ("more",) or None. marks: {index: "strike" | "tick" | "read" | "land"} on a link, index len(links) being the end. Returns each link's (x, width).""" marks = marks or {} where = [] for i, link in enumerate(links): n, ink = link[0], link[1] if i: x = link_arrow(g, x, y) w = addr_w(n) m = marks.get(i) if m == "strike": g.rect(x - 5, y + 1, w + 10, ROW - 2, VERM) addr(g, x, y, n, INK, BOLD) else: if m == "tick": g.rect(x - 9, y + 3, 4, ROW - 6, VERM) if m == "read": g.frame(x - 6, y, w + 12, ROW, READ, 2) if m == "land": g.frame(x - 6, y, w + 12, ROW, BRIGHT, 2) addr(g, x, y, n, ink) where.append((x, w)) x += w if len(link) > 2: # code: its name is what a reader reads g.t(x + 10, b(y), link[2], S, VELLUM, MONO) x += 10 + len(link[2]) * A if end is None: return where if end[0] == "more": x = link_arrow(g, x, y) for d in range(3): g.rect(x + 2 + d * 8, y + 14, 3, 3, FAINT) return where x = value_arrow(g, x, y) if links else x m = marks.get(len(links)) if m == "tick": g.rect(x - 3, y + 3, 4, ROW - 6, VERM) x += 6 if end[0] == "int": integer(g, x, y, end[1]) else: s = end[1] if room is not None and len(s) + 2 > (room - x) // A: s = s[:max(int((room - x) // A) - 2, 1)] + "…" g.t(x, b(y), '"' + s, S, LIT, MONO) else: g.t(x, b(y), '"' + s + '"', S, LIT, MONO) return where def plaque(g, x, y, s, fill=RAISED, ink=VELLUM, stroke=None, pad=4): """A register's name where it points: 20 tall in a 24 row.""" w = len(s) * A + 2 * pad g.rect(x, y + 2, w, ROW - 4, fill) if stroke: g.frame(x, y + 2, w, ROW - 4, stroke, 2) g.t(x + pad, b(y) - 1, s, S, ink, MONO) return w # ---- the context's five panels --------------------------------------------------- def registers(g, y, regs, flags, marks): legend = [("stack", STACK, False), ("heap", HEAP, False), ("code", CODE, False), ("data", DATA, False), ("rwx", DATA, True), ("read-only", VELLUM, False)] y0 = panel(g, RX, y, CW, 18, "registers", note=legend) where = {} for i, (name, links, end) in enumerate(regs): yy = y0 + i * ROW if name == "rip": g.rect(RX + 2, yy, CW - 4, ROW, EMBER[GOLD]) g.rect(RX + 2, yy, 54, ROW, GOLD) g.t(RX + 12, b(yy), name, S, INK, BOLD) else: g.t(RX + 12, b(yy), name, S, DIM, MONO) where[name] = (yy, chain(g, RX + 72, yy, links, end, marks.get(name), room=RX + CW - 12) if links else [(RX + 72, integer(g, RX + 72, yy, end[1]) - RX - 72)]) if not links and marks.get(name, {}).get(0) == "tick": g.rect(RX + 63, yy + 3, 4, ROW - 6, VERM) yy = y0 + 17 * ROW g.t(RX + 12, b(yy), "flags", S, DIM, MONO) g.t(RX + 84, b(yy), "0x%x" % flags[0], S, VELLUM, MONO) x = RX + 84 + 6 * A + 12 for name, on, changed in flags[1]: if changed: g.rect(x - 7, yy + 3, 4, ROW - 6, VERM) if on: g.rect(x, yy + 2, 2 * A + 8, 20, RAISED) g.t(x + 4, b(yy), name, S, BRIGHT, BOLD) else: g.t(x + 4, b(yy), name.lower(), S, FAINT, MONO) x += 2 * A + 8 + 10 return where def stack(g, y, rows): y0 = panel(g, RX, y, CW, len(rows), "stack", note="eight words from rsp · low addresses on top") where = {} for i, (slot, names, links, end, note) in enumerate(rows): yy = y0 + i * ROW x = RX + 10 for n in names: x += plaque(g, x, yy, n, stroke=STACK, pad=2) + 2 where[slot] = yy chain(g, RX + 104, yy, [(slot, STACK)] + links, end, room=RX + CW - 8) if note: g.t(RX + CW - 12, b(yy) - 1, note, 18, FAINT, "sans", "end") return where def backtrace(g, y, frames): y0 = panel(g, RX, y, CW, len(frames), "backtrace", note="newest on top") for i, (name, where_) in enumerate(frames): yy = y0 + i * ROW if i == 0: g.rect(RX + 2, yy, CW - 4, ROW, EMBER[GOLD]) g.rect(RX + 2, yy, 42, ROW, GOLD) g.t(RX + 12, b(yy), "#%d" % i, S, INK if i == 0 else FAINT, BOLD if i == 0 else MONO) g.t(RX + 60, b(yy), name, S, VELLUM, MONO) g.t(RX + 60 + 18 * A, b(yy), where_, S, DIM, MONO) C_WORDS = {"struct", "long", "static", "return", "while", "for", "int", "char", "sizeof", "if", "else", "void", "NULL", "__attribute__", "noinline"} def c_line(g, x, y, text, limit): """One line of C in the four inks. Cut, with a faint mark, where the panel ends.""" cut = len(text) > limit text = text[:limit - 1] if cut else text 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): s = m.group() if not s.strip(): continue ink, face = VELLUM, MONO if s in C_WORDS or s.startswith("#"): ink, face = KW, BOLD elif s[0] == '"' or s[0].isdigit() or (s[0] == "<" and len(s) > 2): ink = LIT g.t(x + m.start() * A, b(y), s, S, ink, face) if cut: g.t(x + (limit - 1) * A, b(y), "…", S, FAINT, MONO) def source(g, y, rows, lines, first, cur, trail, name, 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") 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) def disasm(g, y, rows, func): """rows: (address, offset, mnemonic, operands, state) with state in cur | trail0 | trail1 | None, or ("note", parts) for the row under the pc that says what its operand is.""" y0 = panel(g, LX, y, CW, len(rows), "disassembly", note=func) where = {} for i, r in enumerate(rows): yy = y0 + i * ROW if r[0] == "note": g.rect(LX + 2, yy, CW - 4, ROW, EMBER[GOLD]) g.t(LX + 198, b(yy), r[1], S, DIM, MONO) x = LX + 198 + (len(r[1]) + 1) * A where["note"] = (yy, chain(g, x, yy, r[2], r[3], r[4] if len(r) > 4 else None)) continue a, off, mn, ops, st = r if st == "cur": g.rect(LX + 2, yy, CW - 4, ROW, EMBER[GOLD]) g.rect(LX + 2, yy, 26, ROW, GOLD) g.path(f"M{LX + 21},{yy + 12} l-10,-6 v12 Z", fill=INK) elif st: g.rect(LX + 2, yy + 1, 4, ROW - 2, TRAIL[int(st[-1])]) addr(g, LX + 36, yy, a, CODE) g.t(LX + 138, b(yy), "+%d" % off, S, FAINT, MONO) g.t(LX + 198, b(yy), mn, S, KW, BOLD) x = LX + 198 + 6 * A for part, ink in ops: if isinstance(part, int): x = addr(g, x, yy, part, ink) + 0 else: g.t(x, b(yy), part, S, ink, MONO) x += len(part) * A where[a] = yy return where # ---- the lens: a list, and the heap words that are its nodes ---------------------- def lens(g, y, nodes, pointing, marks, h=312, subject=False): """nodes: (address, value, next address or 0), in the list's first order. pointing: {address: [registers]}. marks: {"read": address of a word, "tick": [...], "land": address of a node}. As the subject it is one size up: mono 25 on rows of 30.""" global S, A, ROW keep = (S, A, ROW) panel(g, LX, y, CW, 0, "the list", focus=True, note="each node, and under it the heap words that are that node", h=h) if subject: S, A, ROW = 25, 15, 30 NW, NH, GAP = (124, 64, 22) if subject else (116, 44, 28) VW = NW - 48 # the value's half; the pointer's is 48 x0 = LX + (CW - len(nodes) * NW - (len(nodes) - 1) * GAP) / 2 ny = y + TAG + (22 if subject else 14) xs = {a: x0 + i * (NW + GAP) for i, (a, v, nx) in enumerate(nodes)} for a, v, nx in nodes: x = xs[a] g.rect(x, ny, VW - 2, NH, RAISED) vs = 35 if subject else 25 g.t(x + VW / 2 - 1, ny + NH / 2 + 0.36 * vs, str(v), vs, VELLUM, MONO if vs < 30 else "mono", "middle") g.rect(x + VW, ny, NW - VW, NH, "#101a48") if marks.get("read") == a + 8: g.frame(x + VW - 2, ny - 2, NW - VW + 4, NH + 4, READ, 2) if a + 8 in marks.get("tick", ()): g.rect(x + VW, ny + NH - 4, NW - VW, 4, VERM) if marks.get("land") == a: g.frame(x - 4, ny - 4, NW + 8, NH + 8, BRIGHT, 2) cx, cy = x + VW + (NW - VW) / 2, ny + NH / 2 if nx: g.rect(cx - 5, cy - 5, 10, 10, HEAP) tx = xs[nx] if tx > x: gen.arrow(g, [(cx + 8, cy), (tx - 3, cy)], HEAP, 3, hl=12, hw=10) else: # a link turned round: under the row gen.arrow(g, [(cx, cy + 8), (cx, ny + NH + 12), (tx + 34, ny + NH + 12), (tx + 34, ny + NH + 3)], HEAP, 3, r=8, hl=12, hw=10) else: g.path(f"M{x + VW + 10},{ny + NH - 8} L{x + NW - 10},{ny + 8}", DIM, 2) addr(g, x + (NW - addr_w(a)) / 2, ny + NH + 2, a, HEAP) px = x + 2 for name in pointing.get(a, ()): px += plaque(g, px, ny + NH + ROW + 2, name, stroke=HEAP) + 4 # the words wy = ny + NH + 2 * ROW + (46 if subject else 34) c0, c1, c2 = (LX + 190, LX + 380, LX + 590) if subject else (LX + 150, LX + 300, LX + 470) g.t(c0, wy - 6, "address", 18, FAINT, "sansm") g.t(c1, wy - 6, "value", 18, FAINT, "sansm") g.t(c2, wy - 6, "next", 18, FAINT, "sansm") if not subject: g.t(LX + CW - 12, wy - 6, "the heap, sixteen bytes a node", 18, FAINT, "sans", "end") rows = {} for i, (a, v, nx) in enumerate(sorted(nodes)): yy = wy + i * ROW rows[a] = yy px = LX + 12 for name in pointing.get(a, ()): px += plaque(g, px, yy, name, stroke=HEAP) + 4 if marks.get("land") == a: g.frame(c0 - 6, yy, c2 - c0 + addr_w(a) + 12, ROW, BRIGHT, 2) addr(g, c0, yy, a, HEAP) integer(g, c1, yy, v) if a + 8 in marks.get("tick", ()): g.rect(c2 - 9, yy + 3, 4, ROW - 6, VERM) if marks.get("read") == a + 8: g.frame(c2 - 6, yy, addr_w(a) + 12, ROW, READ, 2) if nx: addr(g, c2, yy, nx, HEAP) else: g.t(c2, b(yy), "0", S, VELLUM, MONO) g.t(c2 + 2 * A, b(yy), "NULL", S, FAINT, MONO) land_x = c2 + addr_w(nodes[0][0]) + 10 S, A, ROW = keep return xs, rows, land_x # ---- the data: moments 71 and 72 of the recording ----------------------------------- SRC = open(__file__.replace("context.py", "list.c")).read().split("\n") S_, H_, C_, D_ = STACK, HEAP, CODE, DATA RBP = [(0x7ffe970f17b0, S_), (0x7ffe970f17d0, S_), (0x7ffe970f1880, S_), (0x7ffe970f18e0, S_)] def regs_at(rdi): return [ ("rax", [(0x28b41090, H_)], ("int", 4)), ("rbx", [], ("int", 0)), ("rcx", [(0x28b410b0, H_)], ("int", 1)), ("rdx", [], ("int", 0)), ("rsi", [(0x28b410c0, H_)], ("int", 0)), ("rdi", [(rdi[0], H_)], ("int", rdi[1])), ("rbp", RBP, ("int", 0)), ("rsp", RBP, ("int", 0)), ("r8", [(0x7f2ba3019ac0, D_)], ("int", 0)), ("r9", [], ("int", 0x20)), ("r10", [], ("int", 0x10)), ("r11", [(0x7f2ba3019ac8, D_), (0x28b410c0, H_)], ("int", 0)), ("r12", [(0x7ffe970f1908, S_), (0x7ffe970f2fce, S_)], ("str", "zig-out/targets/list")), ("r13", [], ("int", 1)), ("r14", [(0x7f2ba32ac000, D_), (0x7f2ba32ad3c0, D_)], ("int", 0)), ("r15", [(0x10029c0, VELLUM)], ("int", 1)), ("rip", [(0x1001926, C_, "reverse+22")], None), ] STACK_ROWS = [ (0x7ffe970f17b0, ["rsp", "rbp"], RBP[1:], ("int", 0), ""), (0x7ffe970f17b8, [], [(0x10018b0, C_, "main+80")], None, "return address"), (0x7ffe970f17c0, [], [(0x7f2ba3020de8, D_), (0x7ffe970f1918, S_)], ("more",), ""), (0x7ffe970f17c8, [], [], ("int", 0), ""), (0x7ffe970f17d0, [], RBP[2:], ("int", 0), "main's saved rbp"), (0x7ffe970f17d8, [], [(0x7f2ba2e27781, C_, "libc+27781")], None, "return address"), (0x7ffe970f17e0, [], [(0x7f2ba328fc60, C_, "ld.so+22c60")], None, ""), (0x7ffe970f17e8, [], [(0x7ffe970f1908, S_), (0x7ffe970f2fce, S_)], ("str", "zig-out/targets/list"), ""), ] FRAMES = [("reverse+22", "list.c:21"), ("main+80", "list.c:39"), ("libc+27781", "libc.so.6")] NODES = [(0x28b410b0, 1, 0), (0x28b41090, 4, 0x28b41070), (0x28b41070, 9, 0x28b41050), (0x28b41050, 16, 0x28b41030), (0x28b41030, 25, 0x28b41010), (0x28b41010, 36, 0)] P = VELLUM DIS = [ (0x1001920, 16, "mov", [("rcx, rax", P)], "trail1"), (0x1001923, 19, "mov", [("rax, rdi", P)], "trail0"), (0x1001926, 22, "mov", [("rdi, qword ptr [rdi + 8]", P)], "cur"), None, # the note, filled in by each frame (0x100192a, 26, "mov", [("qword ptr [rax + 8], rcx", P)], None), (0x100192e, 30, "test", [("rdi, rdi", P)], None), ] def frame(name, step, subject=False): """subject: the lens is what the scene is about, and takes rows from the source and the disassembly.""" g = Svg() rdi = (0x28b41070, 9) if step else (0x28b41090, 4) # what the last three steps changed, and (mid-step) what this one has reg_marks = {"rax": {0: "tick"}, "rcx": {0: "tick"}} lens_marks = {"tick": [0x28b410b8]} pointing = {0x28b410b0: ["rcx"], 0x28b41090: ["rax"] + ([] if step else ["rdi"]), 0x28b41070: ["rdi"] if step else []} note_marks = None if step: reg_marks["rdi"] = {0: "strike"} lens_marks.update(read=0x28b41098, land=0x28b41070) note_marks = {0: "read"} else: reg_marks["rdi"] = {0: "tick"} lens_h, src_rows, src_first = (432, 8, 17) if subject else (312, 12, 15) xs, rows, land_x = lens(g, TOP, NODES, pointing, lens_marks, lens_h, subject) y = TOP + lens_h + 12 source(g, y, src_rows, SRC, src_first, 21, [20, 22], "list.c", len(SRC) - 1) y += TAG + 12 + src_rows * ROW + 12 dis = list(DIS) dis[3] = ("note", "rdi + 8 =", [(0x28b41098, H_), (0x28b41070, H_)], ("int", 9), note_marks) disasm(g, y, dis[1:] if subject else dis, "reverse") where = registers(g, TOP, regs_at(rdi), (0x206, [("CF", 0, 0), ("PF", 1, 0), ("AF", 0, 0), ("ZF", 0, 1), ("SF", 0, 0), ("IF", 1, 0), ("DF", 0, 0), ("OF", 0, 0)]), reg_marks) stack(g, TOP + 468 + 12, STACK_ROWS) backtrace(g, TOP + 468 + 12 + 228 + 12, FRAMES) if step: # the one link drawn: from the register just struck to the node it now holds, in heap's ink, for this step yy, links = where["rdi"] ty = rows[0x28b41070] + (15 if subject else 12) gen.arrow(g, [(RX, yy + 12), (RX - 16, yy + 12), (RX - 16, ty), (land_x, ty)], HEAP, 3, r=8, hl=12, hw=10) # the scene's clock: how far through the recording, on the rule over the caption g.rect(LX, 899, W - 2 * LX, 2, RULE) at = 71.5 if step else 71 g.rect(LX, 898, (W - 2 * LX) * at / 95, 4, GOLD) g.t(W - LX, 934, "moment %d of 95" % (72 if step else 71), S, FAINT, MONO, "end") if step: gen.caption(g, [[("The load read the word at ",), ("28b4 1098", "mono"), (": the address of the next node.",)], [("rdi", "mono"), (" holds it now, and follows it to the node whose value is 9.",)]]) else: gen.caption(g, [[("Stopped in ",), ("reverse", "mono"), (", about to load ",), ("head->next", "mono"), (".",)], [("rdi", "mono"), (" holds a node's address; the word after its value is the next node.",)]]) g.save(name) if __name__ == "__main__": frame("09-context", False) frame("10-context-step", True) frame("11-context-lens", True, subject=True)