aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/linux_game_smoke.gdb.py (5553B)


      1 """Loaded by GDB through verify_linux_game.py, on the native Linux client."""
      2 import json
      3 import os
      4 import pathlib
      5 import struct
      6 
      7 import gdb
      8 
      9 
     10 output = pathlib.Path('../local/client/linux-game-smoke')
     11 pending_files = {}
     12 opened_files = {}
     13 failed_files = {}
     14 last_blit = None
     15 
     16 
     17 def record_blit():
     18     global last_blit
     19     last_blit = (int(gdb.parse_and_eval('$rdi')), int(gdb.parse_and_eval('$rsi')))
     20 
     21 
     22 def record_file_open():
     23     # Pair syscall entry/exit stops and resolve successful opens through their descriptors.
     24     thread = gdb.selected_thread().global_num
     25     if thread not in pending_files:
     26         number = int(gdb.parse_and_eval('$orig_rax'))
     27         name = gdb.parse_and_eval('(char*)$rdi' if number == 2 else '(char*)$rsi').string()
     28         flags = int(gdb.parse_and_eval('$rsi' if number == 2 else '$rdx'))
     29         pending_files[thread] = (name, flags)
     30         return
     31     name, flags = pending_files.pop(thread)
     32     descriptor = int(gdb.parse_and_eval('(long)$rax'))
     33     if descriptor >= 0:
     34         path = os.readlink('/proc/%d/fd/%d' % (gdb.selected_inferior().pid, descriptor))
     35         opened_files.setdefault(path, set()).add((name, flags))
     36     else:
     37         failed_files.setdefault(name, set()).add(-descriptor)
     38 
     39 
     40 def save_file_audit():
     41     (output / 'opened-files.json').write_text(json.dumps(
     42         {path: sorted(opens) for path, opens in sorted(opened_files.items())}, indent=2))
     43     (output / 'failed-files.json').write_text(json.dumps(
     44         {path: sorted(errors) for path, errors in sorted(failed_files.items())}, indent=2))
     45 
     46 
     47 def send_key(symbol, down):
     48     event = int(gdb.parse_and_eval('(void*)malloc(56)'))
     49     window = int(gdb.parse_and_eval('(unsigned int)SDL_GetWindowID((void*)linux_graphics_window())'))
     50     data = struct.pack('<IIIBBxxiiH', 0x300 if down else 0x301, 0, window, int(down), 0, 0, ord(symbol), 0)
     51     gdb.selected_inferior().write_memory(event, data.ljust(56, b'\0'))
     52     result = int(gdb.parse_and_eval('(int)SDL_PushEvent((void*)%d)' % event))
     53     gdb.execute('call (void)free((void*)%d)' % event, to_string=True)
     54     assert result == 1
     55 
     56 
     57 def capture(label):
     58     # Read the completed game frame before desktop clipping of the primary surface.
     59     assert last_blit[1] == int(gdb.parse_and_eval('$rdi'))
     60     target = last_blit[0]
     61     inferior = gdb.selected_inferior()
     62     width, height = struct.unpack('<II', inferior.read_memory(target, 8))
     63     assert width > 0 and height > 0
     64     pixels = int(gdb.parse_and_eval('(void*)malloc(%d)' % (width * height * 4)))
     65     # Mesa may need its worker threads to complete synchronous GPU readback.
     66     # File tracing stays disabled during this debugger-injected call.
     67     gdb.execute('set scheduler-locking off')
     68     gdb.execute('call ((void (*)(void*, void*))gl_target_read)((void*)%d, (void*)%d)' % (target, pixels), to_string=True)
     69     gdb.execute('set scheduler-locking on')
     70     data = bytes(inferior.read_memory(pixels, width * height * 4))
     71     gdb.execute('call (void)free((void*)%d)' % pixels, to_string=True)
     72     (output / (label + '.rgba')).write_bytes(data)
     73     (output / (label + '.json')).write_text(json.dumps([width, height]))
     74     print('Captured', label)
     75 
     76 
     77 def on_present():
     78     frame = int(gdb.parse_and_eval('$ao_frames'))
     79     if frame == 700:
     80         save_file_audit()
     81     captures = {500: 'baseline', 530: 'inventory-open', 560: 'inventory-closed',
     82                 590: 'map-open', 620: 'map-closed', 680: 'moved'}
     83     events = {510: ('i', True), 511: ('i', False), 540: ('i', True), 541: ('i', False),
     84               570: ('p', True), 571: ('p', False), 600: ('p', True), 601: ('p', False),
     85               630: ('w', True), 660: ('w', False)}
     86     # Do not trace debugger-injected host calls; readback alone permits workers.
     87     if frame == 30 or frame in captures or frame in events:
     88         file_trace.enabled = False
     89         gdb.execute('set scheduler-locking on')
     90     if frame == 30:
     91         gdb.execute('call (void)SDL_WarpMouseInWindow((void*)linux_graphics_window(), 512, 384)', to_string=True)
     92     if frame in captures:
     93         capture(captures[frame])
     94     if frame in events:
     95         send_key(*events[frame])
     96     if frame == 30 or frame in captures or frame in events:
     97         file_trace.enabled = True
     98         gdb.execute('set scheduler-locking off')
     99 
    100 
    101 gdb.execute('set pagination off')
    102 gdb.execute('set confirm off')
    103 gdb.execute('set print thread-events off')
    104 gdb.execute('set python print-stack full')
    105 gdb.execute('''catch syscall open openat
    106 commands
    107 silent
    108 python record_file_open()
    109 continue
    110 end''')
    111 file_trace = gdb.breakpoints()[-1]
    112 gdb.execute('''break *gl_target_blit
    113 commands
    114 silent
    115 python record_blit()
    116 continue
    117 end''')
    118 for symbol in ['external_call', 'guest_memory_fault', 'unavailable_method']:
    119     gdb.Breakpoint(symbol, internal=True)
    120 gdb.execute('set $ao_frames = 0')
    121 gdb.execute('set $ao_cursor_calls = 0')
    122 gdb.execute('''break set_cursor
    123 commands
    124 silent
    125 set $ao_cursor_calls = $ao_cursor_calls + 1
    126 continue
    127 end''')
    128 gdb.execute('''break *gl_target_present
    129 commands
    130 silent
    131 set $ao_frames = $ao_frames + 1
    132 python on_present()
    133 if $ao_frames == 700
    134 if $ao_cursor_calls == 0
    135 printf "Cursor path was not exercised.\\n"
    136 quit 1
    137 end
    138 printf "SetCursor calls: %u\\n", $ao_cursor_calls
    139 printf "Smoke inputs complete at frame 700.\\n"
    140 quit
    141 end
    142 continue
    143 end''')
    144 try:
    145     gdb.execute('run')
    146 finally:
    147     save_file_audit()
    148 gdb.execute('thread apply all bt 10')
    149 gdb.execute('info registers rdi rip')
    150 print('Stopped after frame', gdb.parse_and_eval('$ao_frames'))
    151 raise RuntimeError('Client stopped before completing the input sequence')