toaruos/util/process_log.py

78 lines
2.6 KiB
Python
Raw Normal View History

import struct
import sys
addresses = {}
2018-12-11 17:10:43 +03:00
sources = {}
last_touched = {}
def find_nearby_allocations(addr,size):
results = []
2018-12-11 17:10:43 +03:00
for key in addresses.keys():
if abs(addr - key) < size*2 and addresses[key]:
results.append(key)
results = sorted(results)
for key in results:
if key == addr:
print(" self: 0x%x (size %d, allocated by 0x%x)" % (key, addresses[key], sources[key]))
else:
2018-12-11 17:10:43 +03:00
print(" nearby: 0x%x (size %d, allocated by 0x%x)" % (key, addresses[key], sources[key]))
2018-12-12 04:23:19 +03:00
count = 0
while 1:
2018-12-11 17:10:43 +03:00
data = sys.stdin.read(17)
t, = struct.unpack_from("c",data,0)
addr, = struct.unpack_from("I",data,1)
size, = struct.unpack_from("I",data,5)
extra, = struct.unpack_from("I",data,9)
2018-12-11 17:10:43 +03:00
fault, = struct.unpack_from("I",data,13)
2018-12-12 04:23:19 +03:00
count += 1
if count % 1000 == 0:
print(count)
if t == 'm':
addresses[addr] = size
2018-12-11 17:10:43 +03:00
sources[addr] = fault
last_touched[addr] = t
elif t == 'v':
addresses[addr] = size
2018-12-11 17:10:43 +03:00
sources[addr] = fault
last_touched[addr] = t
elif t == 'c':
addresses[addr] = 'c'
2018-12-11 17:10:43 +03:00
sources[addr] = 0
last_touched[addr] = t
elif t == 'r':
if addr not in addresses:
print("Bad realloc: 0x%x" % addr)
else:
addresses[addr] = None
addresses[extra] = size
2018-12-11 17:10:43 +03:00
sources[extra] = fault
last_touched[addr] = t
last_touched[extra] = t
elif t == 'f':
if addr not in addresses:
print("Bad free detected: 0x%x" % addr)
elif addresses[addr] is None:
print("Double free detected: 0x%x (allocated by 0x%x)" % (addr,sources[addr]))
elif addresses[addr] == 'c':
print("freeing something that was calloced...")
addresses[addr] = None
elif extra != 0xDEADBEEF:
2018-12-11 17:10:43 +03:00
print("Large buffer has bad value: 0x%x (0x%x) expected size is %d, supposed is %d" % (addr, extra, addresses[addr], size))
elif addresses[addr] != size:
print("Size on free is incorrect: 0x%x %d %d 0x%x allocated by 0x%x last touched by %c" % (addr,addresses[addr], size, fault, sources[addr], last_touched[addr]))
find_nearby_allocations(addr,addresses[addr])
else:
addresses[addr] = None
2018-12-11 17:10:43 +03:00
elif t == 'h':
if addr not in addresses:
print("Spurious halt: 0x%x" % (addr))
else:
print("Halting on suspected bug: 0x%x size was %d" % (addr, addresses[addr]))
else:
2018-12-11 17:10:43 +03:00
print("Garbage data detected: %c 0x%x %d 0xx" % (t, addr, size, extra))
break