diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-13 09:28:38 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-13 09:28:38 -0300 |
| commit | 5c0230ea5a77f85de77ad4354788cd6c27aa1ef6 (patch) | |
| tree | e01a903bdee092965b86e118dfaab85c3bd60004 /gui/test/inspection.py | |
| parent | 5a40dca5adb6e9e650f55f3ec6f254a13bf50493 (diff) | |
| download | computeranim-5c0230ea5a77f85de77ad4354788cd6c27aa1ef6.tar.gz computeranim-5c0230ea5a77f85de77ad4354788cd6c27aa1ef6.zip | |
debugger: follow natural V8 deoptimization with adaptive live views
Diffstat (limited to 'gui/test/inspection.py')
| -rw-r--r-- | gui/test/inspection.py | 132 |
1 files changed, 49 insertions, 83 deletions
diff --git a/gui/test/inspection.py b/gui/test/inspection.py index 368b6aa..bdb90da 100644 --- a/gui/test/inspection.py +++ b/gui/test/inspection.py @@ -1,92 +1,58 @@ #!/usr/bin/env python3 -"""Real-target checks for read-only inspection and explicit GDB JIT ELF import.""" +"""Real-target checks for bounded read-only inspection, source maps, and natural deoptimization.""" import argparse -import json -import os from pathlib import Path -import re -import select import subprocess +import sys import tempfile +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'bench')) +from control import ROOT, Controller, natural, deoptimization -ROOT = Path(__file__).resolve().parents[2] -p = argparse.ArgumentParser(description=__doc__) -p.add_argument('--ztrace', type=Path, default=ROOT / 'gui/zig-out/bin/ztrace') -p.add_argument('--d8', type=Path) -a = p.parse_args() -a.ztrace = a.ztrace.resolve() -with tempfile.TemporaryDirectory(prefix='ztrace-inspect-') as directory: - work = Path(directory) - small = work / 'loop' - subprocess.run(['cc', '-g', '-O0', str(ROOT / 'gui/targets/loop.c'), '-o', str(small)], check=True) - programs = [('c', [str(small)])] - js = work / f'gui-inspect-{os.getpid()}.js' - if a.d8: - js.write_bytes((ROOT / 'gui/targets/jit.js').read_bytes()) - programs.append(('v8', [str(a.d8.resolve()), '--allow-natives-syntax', '--no-concurrent-recompilation', - '--no-maglev', '--gdbjit', '--gdbjit-dump', '--gdbjit-dump-filter=jitWork', - '--print-opt-code', '--print-opt-code-filter=jitWork', js.name])) - for name, argv in programs: - recording = work / (name + '.ztrace') - with (work / (name + '.log')).open('w+') as log: - target = subprocess.Popen([str(a.ztrace), 'serve', str(recording), '--', *argv], - cwd=work, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=log, bufsize=0) - pending = b'' - def read(): - global pending - while b'\n' not in pending: - assert select.select([target.stdout], [], [], 30)[0], 'controller timeout' - data = os.read(target.stdout.fileno(), 4096) - assert data, 'controller exited' - pending += data - line, pending = pending.split(b'\n', 1) - return json.loads(line) - def command(text, status='stopped'): - target.stdin.write((text + '\n').encode()) - while True: - answer = read() - if answer['status'] != 'running': - assert answer['status'] == status, answer - return answer - elf = None +def inspect(c, stopped): + for kind, count in [('code', 128), ('memory', 256)]: + assert c.command(f'inspect {kind} {stopped["pc"]} {count}')['pc'] == stopped['pc'] + for request in ['inspect code 0 0', 'inspect code 0 257', 'inspect memory 0 4097', 'inspect cheese 0 8', 'source-map /tmp']: + assert c.command(request, 'failed')['pc'] == stopped['pc'] + assert c.command('inspect memory 0 16')['pc'] == stopped['pc'] + assert 'State:\tt' in Path(f'/proc/{stopped["pid"]}/status').read_text() + assert c.command('stepi')['pc'] != stopped['pc'] + + +def main(): + p = argparse.ArgumentParser(description=__doc__) + p.add_argument('--ztrace', type=Path, default=ROOT / 'gui/zig-out/bin/ztrace') + p.add_argument('--d8', type=Path) + p.add_argument('--source', type=Path, default=Path('/tmp/computeranim-v8-source')) + a = p.parse_args() + with tempfile.TemporaryDirectory(prefix='ztrace-inspect-') as directory: + out = Path(directory) + small = out / 'loop' + subprocess.run(['cc', '-g', '-O0', str(ROOT / 'gui/targets/loop.c'), '-o', str(small)], check=True) + c = Controller(a.ztrace, out, 'c', [str(small)]) + try: + c.read() + c.command('break work') + stopped = c.command('continue') + inspect(c, stopped) + c.command('source-map ' + str(ROOT / 'gui/targets') + ' /mapped-source') + assert '/mapped-source/loop.c:' in c.show() + c.command('source-map ' + str(ROOT / 'gui/targets') + ' ' + str(ROOT / 'gui/targets')) + assert str(ROOT / 'gui/targets/loop.c') in c.show() + print('C: bounded inspection preserved PC; source prefix replacement and stepping passed') + finally: + c.close() + if a.d8: + c, js, elf, bounds, instructions, stopped = natural(a.ztrace, a.d8, out, a.source) try: - assert read()['status'] == 'stopped' - if name == 'c': - command('break work') - stopped = command('continue') - else: - command('continue') - log.seek(0) - assembly = log.read() - address = int(re.search(r'^(0x[0-9a-f]+)\s+[0-9a-f]+\s+[0-9a-f]+\s+addl r8,r11', assembly, re.M)[1], 16) - command('signal 0') - command('break *' + hex(address)) - stopped = command('continue') - assert stopped['pc'] == address - elf, = Path('/tmp').glob('elfdumpjitWork ' + js.name + ':*.o') - loaded = command('symbols ' + str(elf)) - assert loaded['pc'] == stopped['pc'], loaded - show = subprocess.check_output([str(a.ztrace), 'show', str(recording), '-1'], text=True) - assert str(js) + ':6:' in show, show[:2000] - assert '(gui-inspect-' in show, 'instruction source metadata missing' - for kind, count in [('code', 128), ('memory', 256)]: - seen = command(f'inspect {kind} {stopped["pc"]} {count}') - assert seen['pc'] == stopped['pc'], seen - for request in ['inspect code 0 0', 'inspect code 0 257', 'inspect memory 0 4097', 'inspect cheese 0 8']: - failed = command(request, 'failed') - assert failed['pc'] == stopped['pc'] - seen = command('inspect memory 0 16') - assert seen['pc'] == stopped['pc'] - assert 'State:\tt' in Path(f'/proc/{stopped["pid"]}/status').read_text() - moved = command('stepi') - assert moved['pc'] != stopped['pc'] - print(name + ': inspection preserved PC; bounds rejected; stepping still works' + ('; real JIT source line recorded' if name == 'v8' else '')) + inspect(c, stopped) + result = deoptimization(c, bounds, instructions, a.source) + assert result['reason'] == 'wrong map' + print('V8: natural optimization, generated exit, native source, wrong map and ' + result['fallback_tier'] + ' dispatch verified') finally: - if target.poll() is None: - target.stdin.write(b'quit\n') - target.wait(timeout=30) - target.stdin.close() - target.stdout.close() - if elf is not None: - elf.unlink(missing_ok=True) + c.close() + elf.unlink(missing_ok=True) + + +if __name__ == '__main__': + main() |
