Skip to content

Instantly share code, notes, and snippets.

@HansKristian-Work
Last active April 11, 2023 11:35
Show Gist options
  • Select an option

  • Save HansKristian-Work/41f661278eb07beb6c6121a00c10edb8 to your computer and use it in GitHub Desktop.

Select an option

Save HansKristian-Work/41f661278eb07beb6c6121a00c10edb8 to your computer and use it in GitHub Desktop.
Trace Rtl allocations from Wine log
#!/usr/bin/env python3
import sys
import re
def main():
allocations = {}
total_high_watermark = 0
size_match = re.compile('[0-9a-fA-F]+\)')
ptr_match = re.compile('ptr ([0-9a-fA-F]+)')
realloc_match = re.compile('([0-9a-fA-F]+),([0-9a-fA-F]+)\)')
realloc_return_match = re.compile('returning ([0-9a-fA-F]+)')
with open(sys.argv[1], 'r') as f:
while True:
line = f.readline()
if not line:
break
if 'trace:heap:RtlAllocateHeap' in line:
size = int(re.findall(size_match, line)[0][:-1], 16)
ptr = int(re.findall(ptr_match, line)[0], 16)
print('Allocate: ptr = {}, size = {}'.format(hex(ptr), size))
allocations[ptr] = size
total_high_watermark += size
print('Watermark: {} KiB'.format(total_high_watermark // 1024))
elif 'trace:heap:RtlFreeHeap' in line:
ptr = int(re.findall(size_match, line)[0][:-1], 16)
print('Free: ptr = {}'.format(hex(ptr)))
if ptr not in allocations:
print('Error: unknown pointer')
else:
total_high_watermark -= allocations[ptr]
print('Watermark: {} KiB'.format(total_high_watermark // 1024))
allocations.pop(ptr)
elif 'trace:heap:RtlReAllocateHeap' in line:
ptr_size = re.findall(realloc_match, line)[0]
out_ptr = int(re.findall(realloc_return_match, line)[0], 16)
in_ptr = int(ptr_size[0], 16)
new_size = int(ptr_size[1], 16)
print('Realloc: old ptr = {}, new size = {}, new ptr = {}'.format(hex(in_ptr), hex(new_size), hex(out_ptr)))
if in_ptr not in allocations:
print('Error: unknown pointer')
else:
total_high_watermark -= allocations[in_ptr]
allocations.pop(in_ptr)
allocations[out_ptr] = size
total_high_watermark += new_size
print('Watermark: {} KiB'.format(total_high_watermark // 1024))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment