#!/usr/bin/env python3 """Executable/data lenses, scroll bounds, display formats and native glass rendering.""" import argparse from pathlib import Path import re import subprocess import tempfile ROOT = Path(__file__).resolve().parents[2] p = argparse.ArgumentParser(description=__doc__) p.add_argument('--gui', type=Path, default=ROOT / 'gui/zig-out/bin/ztrace-gui') p.add_argument('--native', action='store_true') p.add_argument('--output', type=Path) a = p.parse_args() a.gui = a.gui.resolve() with tempfile.TemporaryDirectory(prefix='ztrace-lenses-') as directory: out = a.output.resolve() if a.output else Path(directory) out.mkdir(parents=True, exist_ok=True) target = out / 'segments' subprocess.run(['cc', '-g', '-O0', '-no-pie', str(ROOT / 'gui/targets/segments.c'), '-o', str(target)], check=True) symbols = {name: int(address, 16) for address, name in re.findall(r'^([0-9a-f]+) \w (\w+)$', subprocess.check_output(['nm', '-n', str(target)], text=True), re.M)} head, code = symbols['head'], symbols['checkpoint'] script = out / 'check.script' commands = f'''console launch {target} await stops 1 console break checkpoint console continue await execution 1 frames 40 console lens {code} expect lens_count 1 expect lenses[0].first {code} frames 40 screenshot {out / 'code-glass.png'} console set glass false frames 20 screenshot {out / 'code-opaque.png'} expect-different-screenshot {out / 'code-glass.png'} {out / 'code-opaque.png'} 1120 175 1060 140 console set glass true wheel 1600 220 -1 frames 40 print lenses[0].first expect execution 1 # Right-clicking an executable lens address opens memory, not code. console mouse-button 3 1230 161 expect focus memory expect nav[2].place.address {code} key escape # Data lens context is scrollable in both directions without altering its anchor. console lens {head} frames 40 expect lenses[0].address {head} expect lenses[0].first {(head & ~15) - 32} wheel 1600 220 -2 frames 40 expect lenses[0].first {(head & ~15) + 64} screenshot {out / 'memory-context.png'} wheel 1600 220 2 frames 40 expect lenses[0].first {(head & ~15) - 32} # Same-parent pointer recursion preserves ancestors and detects a cycle. click 1230 161 expect lens_count 2 expect lenses[1].cycle true frames 30 click 2180 350 expect lens_count 1 key escape console goto memory {head} frames 40 # Both controls are available directly on the memory pane. click 1240 822 expect unit 2 click 1240 822 expect unit 4 click 1240 822 expect unit 8 click 1278 822 expect number_format unsigned click 1278 822 expect number_format signed click 1278 822 expect number_format float click 1278 822 expect number_format hex ''' for unit in (1, 2, 4, 8): for fmt in ('hex', 'unsigned', 'signed') + (('float',) if unit >= 4 else ()): commands += f'console memory-format {unit} {fmt}\nexpect unit {unit}\nexpect number_format {fmt}\nframes 2\n' commands += f'''console memory-format 8 hex frames 10 screenshot {out / 'words.png'} # Historical browsing never requests bytes from a live target. key left_bracket expect following false print stops console lens {head} frames 30 wheel 1600 220 -20 frames 30 print stops expect execution 1 key escape expect-no-log CommandFailed expect-no-log backdrop shader ''' script.write_text(commands) args = [str(a.gui), '--fresh', '--size', '2240x1260', '--state', str(out / 'session.state'), '--script', str(script)] if not a.native: args.append('--headless') run = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) stdout, stderr = run.communicate(timeout=100) (out / 'run.log').write_text(stdout + stderr) assert run.returncode == 0, stderr first = int(re.search(r'lenses\[0\].first = (\d+)', stderr)[1]) assert first > code, (first, code) stops = re.findall(r'stops = (\d+)', stderr) assert len(stops) == 2 and stops[0] == stops[1], stops for recording in Path('/tmp').glob(f'ztrace-gui-{run.pid}-*.ztrace'): recording.unlink() print(('Native' if a.native else 'Headless') + ': code/data lenses, context scrolling, pointer recursion, memory formats and historical isolation passed')