From 4b4113a0217ee27c78e7efddb66ff5a455d49083 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 12 Nov 2026 13:19:32 -0300 Subject: debugger: add a native GUI with live V8 JIT debugging --- gui/bench/steps.py | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 gui/bench/steps.py (limited to 'gui/bench/steps.py') diff --git a/gui/bench/steps.py b/gui/bench/steps.py new file mode 100644 index 0000000..f97efed --- /dev/null +++ b/gui/bench/steps.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python3 +"""Compare the same controller's recorded instruction steps on C and debug V8.""" +import argparse +import json +import math +import os +from pathlib import Path +import re +import select +import statistics +import subprocess +import time + +ROOT = Path(__file__).resolve().parents[2] +p = argparse.ArgumentParser(description=__doc__) +p.add_argument('--d8', type=Path, required=True) +p.add_argument('--ztrace', type=Path, default=ROOT / 'gui/zig-out/bin/ztrace') +p.add_argument('--out', type=Path, default=Path('/tmp/ztrace-step-bench')) +p.add_argument('--steps', type=int, default=1000) +a = p.parse_args() +assert a.steps > 0 +a.out.mkdir(parents=True, exist_ok=True) +a.out = a.out.resolve() +a.d8 = a.d8.resolve() +small = a.out / 'loop' +subprocess.run(['cc', '-g', '-O0', str(ROOT / 'gui/targets/loop.c'), '-o', str(small)], check=True) + + +class Controller: + def __init__(self, name, argv): + self.log = (a.out / (name + '.log')).open('w+') + self.process = subprocess.Popen( + [str(a.ztrace.resolve()), 'serve', str(a.out / (name + '.ztrace')), '--', *argv], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=self.log, bufsize=0) + self.pending = b'' + + def read(self): + deadline = time.monotonic() + 30 + while b'\n' not in self.pending: + if not select.select([self.process.stdout], [], [], max(0, deadline - time.monotonic()))[0]: + raise TimeoutError('controller did not reply within 30 seconds') + data = os.read(self.process.stdout.fileno(), 4096) + if not data: + raise RuntimeError('controller exited; see ' + self.log.name) + self.pending += data + line, self.pending = self.pending.split(b'\n', 1) + return json.loads(line) + + def command(self, command): + self.process.stdin.write((command + '\n').encode()) + while True: + reply = self.read() + assert reply['status'] not in ('failed', 'exited'), reply + if reply['status'] == 'stopped': + return reply + + def close(self): + try: + if self.process.poll() is None: + self.process.stdin.write(b'quit\n') + self.process.wait(timeout=30) + finally: + if self.process.poll() is None: + self.process.kill() + self.process.wait() + self.process.stdin.close() + self.process.stdout.close() + self.log.close() + + +def summary(ns): + ordered = sorted(v / 1000 for v in ns) + return dict(median_us=statistics.median(ordered), p95_us=ordered[math.ceil(len(ns) * .95) - 1], max_us=max(ordered)) + + +results = {} +for name, argv in [('c', [str(small)]), ('v8', [str(a.d8), '--allow-natives-syntax', + '--no-concurrent-recompilation', '--no-maglev', '--print-opt-code', + '--print-opt-code-filter=jitWork', str(ROOT / 'gui/targets/jit.js')])]: + start = time.perf_counter_ns() + c = Controller(name, argv) + try: + assert c.read()['status'] == 'stopped' + entry_ns = time.perf_counter_ns() - start + if name == 'c': + c.command('break work') + else: + stopped = c.command('continue') # Native %SystemBreak, before the final JS call. + assert stopped['reason'].startswith('signal TRAP'), stopped + c.log.seek(0) + assembly = c.log.read() + assert 'kind = TURBOFAN_JS' in assembly + # shortcut: this fixture's x64 loop instruction; fail loudly if V8 changes its register allocation. + match = re.search(r'^(0x[0-9a-f]+)\s+[0-9a-f]+\s+[0-9a-f]+\s+addl r8,r11', assembly, re.M) + assert match, 'could not find jitWork loop body; inspect ' + c.log.name + address = int(match[1], 16) + c.command('signal 0') + c.command('break *' + hex(address)) + hit = c.command('continue') + assert hit['reason'].startswith('breakpoint '), hit + if name == 'v8': + assert hit['pc'] == address, hit + maps = Path(f"/proc/{hit['pid']}/maps").read_text().splitlines() + mapping = next(line for line in maps if int(line.split('-')[0], 16) <= address < int(line.split('-')[1].split()[0], 16)) + assert 'x' in mapping.split()[1] and len(mapping.split()) == 5, mapping + first_break_ns = time.perf_counter_ns() - start + if name == 'v8': + stepped = c.command('step') + assert stepped['reason'] == 'step' and stepped['pc'] != address, stepped + core, wall = [], [] + for _ in range(a.steps): + start_step = time.perf_counter_ns() + reply = c.command('stepi') + wall.append(time.perf_counter_ns() - start_step) + core.append(reply['elapsed_ns']) + assert reply['reason'] == 'step', reply + results[name] = dict(steps=a.steps, entry_ms=entry_ns / 1e6, first_break_ms=first_break_ns / 1e6, + capture=summary(core), roundtrip=summary(wall)) + print(name, json.dumps(results[name]), flush=True) + finally: + c.close() + if name == 'v8': + shown = subprocess.run([str(a.ztrace.resolve()), 'show', str(a.out / 'v8.ztrace'), '-1'], + check=True, capture_output=True, text=True) + assert f"thread {reply['tid']}" in shown.stdout.splitlines(), 'JIT stop inherited a stale native source line' +(a.out / 'results.json').write_text(json.dumps(results, indent=2) + '\n') -- cgit v1.3