#!/usr/bin/env python3 """The working frame reworked: the disassembly under the source in every split, the registers a scene does not move folded three to a row, the stack given what that frees, and the `blocks` lens's rings over the program column. python3 -B frame.py # 40-frame-beside, 41-frame-subject, 25-blocks-lens (.svg), 42-frame-layouts.png 40 and 41 are mocks 9 and 11 in the reworked frame (the same recording of targets/list, the same moments, each pane's minimap from the map as map.py draws it); 25 is sort with the `blocks` lens (graph.py's rings, the stop in sort-dbg.txt); 42 sets the three layouts weighed this round side by side, each as the list's `subject` and sort's `blocks`, at a third of their size: A (recommended) folds the quiet registers, B keeps all seventeen and gives the source's rows to the disassembly, C sets the registers in two columns. """ import math import re import subprocess from PIL import Image, ImageDraw, ImageFont import gen import dense as D import context as C import map as M import graph as G from context import (PAGE, RAISED, RULE, VELLUM, BRIGHT, DIM, FAINT, GOLD, HEAP, STACK, CODE, DATA, A, S, ROW, TAG, MONO, BOLD, b, ph, mix, Svg) HERE = G.HERE TOP, FOOT = 48, 888 WORDS = {6: "six", 8: "eight", 10: "ten", 11: "eleven", 12: "twelve", 13: "thirteen", 14: "fourteen", 15: "fifteen", 20: "twenty", 22: "twenty-two"} # ---- the splits ------------------------------------------------------------------------------------------------- # What a scene gives its lens, and the program column's rows under it: (drawing, view, source, disassembly). The # `blocks` lens has no view: its drawing (rings, 880 by 344) is over the program column, and the state column is # the registers' and the stack's from the top. SPLITS = {"none": (0, 0, 14, 17), "beside": (152, 152, 8, 17), "subject": (296, 200, 8, 11), "blocks": (344, 0, 6, 11)} LAYOUTS = { "A": dict(regs="fold", src=None, dis=None), # the splits above "B": dict(regs="all", src=6, dis=lambda d, s: d + s - 6), # all seventeen rows; the source gives to six "C": dict(regs="two", src=None, dis=None), # the registers in two columns of eight, rip across } def boxes(split, layout, quiet): """Every panel's box in a split: x, y, h (the width is the column's, or both for the band across).""" band, view, src, dis = SPLITS[split] L = LAYOUTS[layout] if L["src"] is not None and split != "none": src, dis = L["src"], L["dis"](dis, src) regs = {"fold": C.reg_rows(quiet), "all": 17, "two": 9}[L["regs"]] out = {} y = TOP + (band + 12 if band else 0) out["source"] = (64, y, ph(src)) out["disasm"] = (64, y + ph(src) + 12, ph(dis)) assert out["disasm"][1] + out["disasm"][2] == FOOT or split == "none", (split, layout, out) ys = TOP + (view + 12 if view else 0) out["registers"] = (976, ys, ph(regs)) yk = ys + ph(regs) + 12 words = (FOOT - yk - TAG - 8) // ROW out["stack"] = (976, FOOT - ph(words), ph(words)) # a panel is 32 + 24n: what is left over is above it if band: out["drawing"] = (64, TOP, band) if view: out["view"] = (976, TOP, view) return out, regs, words # ---- the registers in two columns (layout C) ------------------------------------------------------------------ def registers_two(g, y, regs, flags, marks): """C: sixteen in two columns of eight, each 440 wide, its chain cut to that; rip across the foot with the flags.""" legend = [("stack", STACK, False), ("heap", HEAP, False), ("code", CODE, False), ("data", DATA, False), ("rwx", DATA, True), ("read-only", VELLUM, False)] y0 = C.panel(g, C.RX, y, C.CW, 9, "registers", note=legend) where = {} for i, (name, links, end) in enumerate(regs[:16]): x, yy = C.RX + (i // 8) * 440, y0 + (i % 8) * ROW where[name] = yy g.t(x + 12, b(yy), name, S, DIM, MONO) if links: C.chain(g, x + 60, yy, links, end, marks.get(name), room=x + 428) else: C.integer(g, x + 60, yy, end[1]) yy = y0 + 8 * ROW name, links, end = regs[16] g.t(C.RX + 12, b(yy), name, S, C.current_row(g, C.RX + 2, yy, C.CW - 4, 54), BOLD) C.chain(g, C.RX + 72, yy, links, end, room=C.RX + C.CW - 12) x = C.RX + C.CW - 12 - len(flags) * 42 + 10 for fl, on, changed in flags: if on: g.rect(x, yy + 2, 2 * A + 8, 20, RAISED) g.t(x + 4, b(yy), fl, S, BRIGHT, BOLD) else: g.t(x + 4, b(yy), fl.lower(), S, mix(DIM, C.band(), 0.8), MONO) x += 42 where["rip"] = yy return where def draw_registers(g, layout, y, regs, flags, marks, quiet): C.RX = 976 if LAYOUTS[layout]["regs"] == "two": return registers_two(g, y, regs, flags, marks) return C.registers(g, y, regs, flags, marks, quiet if LAYOUTS[layout]["regs"] == "fold" else ()) # ---- a pane's minimap from rows we hold (sort's), as map.py draws one from the map ---------------------------- def strip(g, x, y, h, rows, w0, wn, marks=None, ticks=()): """rows: per row of the column, its runs as (x, width, ink) in the panel's units. The window framed; the marks at a row's left; a block's first row ticked at the strip's left; dots where the column goes on.""" marks = marks or {} g.rect(x, y, M.MW, h, C.WELL) cap, total = int((h - 6) // M.MR), len(rows) first = 0 if total <= cap else min(max(w0 + wn // 2 - cap // 2, 0), total - cap) for r in range(first, min(first + cap, total)): ry = y + 3 + (r - first) * M.MR for rx, rw, ink in rows[r]: g.rect(x + 2 + rx * M.KX, ry, max(rw * M.KX, 1), 3, mix(ink, PAGE, 0.65)) if r in ticks: g.rect(x, ry - 1, 2, 4, DIM) if r in marks: g.rect(x + 2, ry - 0.5, 4, 4, {"current": GOLD, "struck": gen.VERM, "read": gen.READ}[marks[r]]) g.frame(x, y + 3 + (w0 - first) * M.MR - 3, M.MW, wn * M.MR + 5, VELLUM, 2) for end, more in (("top", first > 0), ("foot", first + cap < total)): if more: yy = y + 1 if end == "top" else y + h - 3 for d in range(3): g.rect(x + M.MW / 2 - 7 + d * 6, yy, 2, 2, FAINT) def strip_at(box): x, y, h = box return x + 880 - 2 - M.MW, y + TAG, h - TAG - 2 # ---- the list: 40 (`beside`, at rest) and 41 (`subject`, mid-step) --------------------------------------------- LIST_QUIET = ("rbx", "rdx", "rsi", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15") # reverse moves rax rcx rdi def list_frame(g, split, step, layout="A", caption=None, strips=True): subject = split == "subject" bx, nregs, words = boxes(split, layout, LIST_QUIET) band, view, src, dis = SPLITS[split] src, dis = (bx["source"][2] - 32) // ROW, (bx["disasm"][2] - 32) // ROW # the map, its windows set for this split's rows: each pane's column for its minimap old = dict(C.SUBJECT), dict(C.BESIDE) sp = C.SUBJECT if subject else C.BESIDE sp.update(src=src, dis=dis, words=words) m, win = M.map_log(step, False, subject) M.stage(False) C.SUBJECT.update(old[0]) C.BESIDE.update(old[1]) D.put(64) M.strip(True) first = 18 if subject else 19 ys = C.source(g, bx["source"][1], src, D.SRC, first, 21, [], "list.c", 42) at = C.disasm(g, bx["disasm"][1], D.window(dis, step), "reverse", 21) M.strip(False) marks = {"rax": {0: "tick"}, "rcx": {0: "tick"}, "rdi": {0: "strike" if step else "tick"}} regs = draw_registers(g, layout, bx["registers"][1], D.regs(step), D.FLAGS, marks, LIST_QUIET) M.strip(True) C.RX = 976 C.stack(g, bx["stack"][1], M.slot_rows(M.UNDER, words), M.frames_from(M.UNDER, words), rsp=M.RSP, roles=M.roles_from(M.UNDER, words), note="%s words from rsp" % WORDS[words]) rows_shown = M.SUBJECT_VIEW if subject else M.BESIDE_VIEW vr = C.fields(g, TOP, view, "the heap, %x to %x" % (0x1004000 + 16 * rows_shown[0], 0x10040bf), 0x1004000, 512, D.heap_fields(0x1004000, 512, 0x1004098), D.MEM, HEAP, D.region, marks=(9,) if step else (), rows_shown=rows_shown, more_rows=False, touched={0x1004098} if step else ()) M.strip(False) D.put(64) C.thread(g, ys, [(at[0x1001926], at[0x1001926] + ROW)]) C.RX = 976 C.lens(g, TOP, band, view, D.NODES, M.pins(step), M.node_marks(step), 0x1004000, 32, link_from=(regs["rdi"], "rdi") if step else None, subject=subject, view_rows=vr) if strips: M.minimaps(g, m.log, {k: bx[k] for k in ("source", "disasm", "stack", "view")}) M.clock(g, 3 if step else 2, 4) gen.caption(g, caption) return bx, words CAP9 = [[("Stopped in ",), ("reverse", "mono"), (", about to load ",), ("head->next", "mono"), (".",)], [("rdi", "mono"), (" points at a node; the word after its value is the next node.",)]] CAP10 = [[("The load read the word at ",), ("1004098", "mono"), (": the address of the next node.",)], [("rdi", "mono"), (" holds it now, and points at the node whose value is 9.",)]] # ---- sort, stopped at the 11th compare (sort-dbg.txt) ---------------------------------------------------------- SORT_MEM = {} for line in open(HERE + "/sort-dbg.txt"): mm = re.match(r"^(7fffffff[0-9a-f]{4}) ((?:[0-9a-f]{2} ){16})", line) if mm: for i, v in enumerate(mm.group(2).split()): SORT_MEM[int(mm.group(1), 16) + i] = int(v, 16) SORT_NAMES = {0x401020: "_start", 0x4010e0: "__do_global_dtors_aux", 0x401120: "swap", 0x401150: "sort", 0x401200: "main"} RSP_S = 0x7fffffffecd0 def s_region(v): for lo, hi, ink in [(0x401000, 0x402000, CODE), (0x400000, 0x401000, VELLUM), (0x402000, 0x403000, VELLUM), (0x403000, 0x405000, DATA)]: if lo <= v < hi: return ink return D.region(v) def s_name(v): if 0x7ffff7fbe000 <= v < 0x7ffff7fec000: return "ld.so+%x" % (v - 0x7ffff7fbe000) if v in D.LIBNAMES: return D.LIBNAMES[v] f = max(a for a in SORT_NAMES if a <= v) return "%s+%d" % (SORT_NAMES[f], v - f) def s_word(a): return sum(SORT_MEM[a + i] << (8 * i) for i in range(8)) if all(a + i in SORT_MEM for i in range(8)) else None def s_follow(v): links = [] while True: ink = s_region(v) if ink == CODE: return links + [(v, CODE, s_name(v))], None links.append((v, ink)) s = "".join(chr(SORT_MEM[v + i]) for i in range(16) if v + i in SORT_MEM).split("\0")[0] if ink == STACK and len(s) >= 2 and s.isprintable() and SORT_MEM.get(v + len(s)) == 0: return links, ("str", s) w = s_word(v) if w is None or len(links) > 5: return links, None if w and s_region(w): v = w continue return links, ("int", w) def sort_slots(n): rows = [] for i in range(n): a = RSP_S + 8 * i w = s_word(a) links, end = s_follow(w) if w and s_region(w) else ([], ("int", w)) rows.append((a, {0: ["rsp"], 2: ["rbp"]}.get(i, []), links, end)) return rows E_ = [(0x7fffffffed00, STACK), (0x7fffffffedb0, STACK), (0x7fffffffee10, STACK)] SORT_REGS = [("rax", [], ("int", 1)), ("rbx", [], ("int", 0)), ("rcx", [(0x404010, DATA)], ("int", 0x900000004)), ("rdx", [], ("int", 4)), ("rsi", [(0x404024, DATA)], ("int", 0x1f0000001a)), ("rdi", [(0x404020, DATA)], ("int", 0x1a00000005)), ("rbp", [(0x7fffffffece0, STACK)] + E_, ("int", 0)), ("rsp", [(0x7fffffffecd0, STACK)], ("int", 0x401000000)), ("r8", [(0x7ffff7e19680, DATA)], None), ("r9", [(0x7ffff7e1afa0, DATA)], None), ("r10", [(0x7fffffffea60, STACK)], ("int", 0)), ("r11", [(0x7ffff7ffca90, DATA)], ("int", 0xba55a1098a503203)), ("r12", [(0x7fffffffee38, STACK), (0x7fffffffefea, STACK)], ("str", "./sort")), ("r13", [], ("int", 1)), ("r14", [(0x7ffff7ffd000, DATA), (0x7ffff7ffe3c0, DATA)], ("int", 0)), ("r15", [(0x403e10, DATA), (0x4010e0, CODE, "__do_global_dtors_aux")], None), ("rip", [(0x4011ab, CODE, "sort+91")], None)] SORT_FLAGS = [("CF", 0, 0), ("PF", 0, 1), ("AF", 0, 1), ("ZF", 0, 0), ("SF", 0, 0), ("IF", 1, 0), ("DF", 0, 0), ("OF", 0, 0)] SORT_QUIET = ("rbx", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15") # sort moves the other eight SORT_SRC = open(HERE + "/../../anim/tools/sort.c").read().split("\n") TEXT, FUNC = G.TEXT, G.FUNC def sort_strips(g, bx, lo, n, words): """The panes' minimaps from what the stop holds: the file, the program's text (69 rows round the pc), the stack from 16 words above rsp to as far as the dump goes.""" src_rows = [[(64 + (len(l) - len(l.lstrip())) * A, len(l.strip()) * A, VELLUM)] if l.strip() else [] for l in SORT_SRC[:22]] strip(g, *strip_at(bx["source"]), src_rows, 8, 8, {11: "current"}) i = [r[0] for r in TEXT].index(G.PC) col = TEXT[max(0, i - 34):i + 35] dis_rows = [[(40, 7 * A, CODE), (180, len(mn) * A, gen.KW), (264, len(ops) * A, VELLUM if FUNC(a) == "sort" else FAINT)] for a, ln, mn, ops, t, c in col] c0 = [r[0] for r in col] strip(g, *strip_at(bx["disasm"]), dis_rows, c0.index(TEXT[lo][0]), n, {c0.index(G.PC): "current"}, ticks={c0.index(a) for a in G.SORT_BL if a in c0}) stk = [] for k in range(-16, 110): a = RSP_S + 8 * k w = s_word(a) if w is None: break ink = s_region(w) if w else None stk.append([(184, 12 * A, STACK)] + ([(184 + 13 * A, 12 * A, ink or VELLUM)] if w else [(184 + 13 * A, A, FAINT)])) strip(g, *strip_at(bx["stack"]), stk, 16, words) def sort_frame(g, layout="A", strips=True): """Mock 25 in a layout: the `blocks` lens across the band, the source and the disassembly under it.""" bx, nregs, words = boxes("blocks", layout, SORT_QUIET) st = G.sort_state(G.UPTO, G.PC, G.TRAIL_ROWS, G.NOTES) G.rings(g, 64, TOP, 880, 344, G.SORT_BL, G.SORT_BASE, st, G.SORT_RINGS, "the blocks of sort") src, dis = (bx["source"][2] - 32) // ROW, (bx["disasm"][2] - 32) // ROW C.LX, C.RX = 64, 976 M.strip(strips) first = max(1, 12 - src // 2) ys = C.source(g, bx["source"][1], src, SORT_SRC, first, 12, [14, 13], "sort.c", 22) at, run, lo = G.sort_disasm(g, bx["disasm"][1], dis) M.strip(False) draw_registers(g, layout, bx["registers"][1], SORT_REGS, SORT_FLAGS, {}, SORT_QUIET) M.strip(strips) C.RX = 976 roles = {0: ("j, high half", VELLUM), 1: ("i, n", VELLUM), 2: ("saved rbp", DIM), 6: ("saved rbp", DIM)} C.stack(g, bx["stack"][1], sort_slots(words), [("sort", 0, 3), ("main", 4, 7), ("libc.so.6+27781", 8, 29), ("__libc_start_main+137", 30, 41)], rsp=RSP_S, roles={k: v for k, v in roles.items() if k < words}, note="%s words from rsp" % WORDS[words]) M.strip(False) C.thread(g, ys, [run]) if strips: sort_strips(g, bx, lo, dis, words) M.clock(g, 434, 1905) gen.caption(g, [[("The sixth pass, its third compare: ",), ("17 > 5", "mono"), (", so ",), ("jne", "mono"), (" takes the swap.",)], [("The swap has run 5 times so far, and each loop has turned 5.",)]]) return bx, words # ---- the frames ------------------------------------------------------------------------------------------------ def save(name, draw, *a, **k): gen.W, gen.H = 1920, 1080 M.stage(False) g = Svg() out = draw(g, *a, **k) g.save(name) return out def sheet(out): """Mock 42: A, B, C as columns; the list's `subject` and sort's `blocks` as rows; a third of their size.""" tiles, info = [], {} for key in "ABC": for kind in ("subject", "blocks"): name = "42-%s-%s" % (key, kind) if kind == "subject": bx, words = save(name, list_frame, "subject", True, key, CAP10) else: bx, words = save(name, sort_frame, key) src, dis = (bx["source"][2] - 32) // ROW, (bx["disasm"][2] - 32) // ROW regs = (bx["registers"][2] - 32) // ROW info[(key, kind)] = (src, dis, regs, words) subprocess.run(["resvg", "-w", "608", HERE + "/" + name + ".svg", HERE + "/" + name + ".png"], check=True) tiles.append((key, kind, Image.open(HERE + "/" + name + ".png").convert("RGB"))) W_, H_ = 64 + 3 * 608 + 2 * 32 + 64, 210 + 2 * (342 + 120) s = Image.new("RGB", (W_, H_), "#0a1030") d = ImageDraw.Draw(s) big = ImageFont.truetype("/usr/share/fonts/go/Go-Medium.ttf", 30) head = ImageFont.truetype("/usr/share/fonts/go/Go-Medium.ttf", 24) body = ImageFont.truetype("/usr/share/fonts/go/Go-Regular.ttf", 18) d.text((64, 28), "The frame, three ways: where the room for the stack and the disassembly comes from", font=big, fill="#fff8e8") d.text((64, 72), "Each the same two moments: the list as `subject` mid-step (top), sort with the `blocks` lens (foot). " "A third of their size: the layout, not the digits.", font=body, fill="#9aa6d9") titles = {"A": ("A fold the quiet registers (recommended)", "#e8c46a"), "B": ("B all seventeen; the source gives", "#eee6d2"), "C": ("C the registers in two columns", "#eee6d2")} subs = {"A": "the registers a scene moves keep their rows and chains; the rest three a row", "B": "rule 6 as it stood: every chain whole; the stack does not grow", "C": "two registers a row, 440 each: chains cut to a hop; the stack the most"} for i, (key, kind, im) in enumerate(tiles): col, row = "ABC".index(key), 0 if kind == "subject" else 1 x, y = 64 + col * (608 + 32), 210 + row * (342 + 120) if row == 0: d.text((x, 118), titles[key][0], font=head, fill=titles[key][1]) d.text((x, 152), subs[key], font=body, fill="#9aa6d9") s.paste(im, (x, y)) d.rectangle((x - 1, y - 1, x + 608, y + 342), outline="#e8c46a" if key == "A" else "#3a4a8a", width=2) src, dis, regs, words = info[(key, kind)] lab = "%s: source %d · disassembly %d · registers %d rows · stack %d" % ( "list, subject" if kind == "subject" else "sort, blocks", src, dis, regs, words) d.text((x, y + 352), lab, font=body, fill="#eee6d2") if kind == "blocks": d.text((x, y + 378), "rings over the program column, 880 by 344: whole", font=body, fill="#9aa6d9") else: d.text((x, y + 378), "the drawing 296 and its view of 7 over the state column", font=body, fill="#9aa6d9") s.save(HERE + "/" + out) for key in "ABC": for kind in ("subject", "blocks"): for ext in ("svg", "png"): subprocess.run(["rm", "-f", "%s/42-%s-%s.%s" % (HERE, key, kind, ext)], check=True) return info if __name__ == "__main__": print("40", save("40-frame-beside", list_frame, "beside", False, "A", CAP9)) print("41", save("41-frame-subject", list_frame, "subject", True, "A", CAP10)) print("25", save("25-blocks-lens", sort_frame, "A")) print("42", sheet("42-frame-layouts.png"))