gdb_bind_trace.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Material binds and blend state builds of a running WE under Wine, with caller, material, blending byte and device
  2. # blend mode/flags.
  3. # OUT=trace.txt COUNT=400 gdb -q -batch -p <wallpaper64.exe pid> -x gdb_bind_trace.py
  4. # IDA addresses work directly. gdb may crash on detach, WE keeps running; keep ulimit -c 0.
  5. import gdb, os
  6. gdb.execute('set pagination off')
  7. gdb.execute('set confirm off')
  8. inf = gdb.selected_inferior()
  9. def rd(addr, n): return bytes(inf.read_memory(addr, n))
  10. def q(addr): return int.from_bytes(rd(addr, 8), 'little')
  11. def name(mat):
  12. try:
  13. size = q(mat + 512 + 16); cap = q(mat + 512 + 24)
  14. p = q(mat + 512) if cap > 15 else mat + 512
  15. return rd(p, min(size, 80)).decode('latin1')
  16. except Exception as e: return '?'
  17. out = open(os.environ.get('OUT', 'bind_trace.txt'), 'w')
  18. count = [0]
  19. class B(gdb.Breakpoint):
  20. def __init__(self, addr, kind):
  21. super().__init__('*' + hex(addr), internal=False); self.kind = kind
  22. def stop(self):
  23. f = gdb.selected_frame()
  24. rcx = int(f.read_register('rcx')); rsp = int(f.read_register('rsp'))
  25. ra = q(rsp)
  26. if self.kind == 'bind':
  27. dev = q(q(rcx + 200) + 5400)
  28. line = 'BIND mat=%x ret=%x blend=%d name=%s dev38=%d dev40=%x' % (rcx, ra, rd(rcx + 496, 1)[0], name(rcx), rd(dev + 38, 1)[0], int.from_bytes(rd(dev + 40, 2), 'little'))
  29. else:
  30. line = 'BLEND ret=%x dev38=%d dev40=%x' % (ra, rd(rcx + 38, 1)[0], int.from_bytes(rd(rcx + 40, 2), 'little'))
  31. out.write(line + '\n'); out.flush()
  32. count[0] += 1
  33. if count[0] >= int(os.environ.get('COUNT', 400)):
  34. gdb.execute('delete'); out.close()
  35. gdb.post_event(lambda: gdb.execute('detach'))
  36. return False
  37. B(0x140155fc0, 'bind')
  38. B(0x140099f60, 'blend')
  39. gdb.execute('continue')