| 1 | from __future__ import print_function |
| 2 | |
| 3 | try: |
| 4 | import psyco |
| 5 | psyco.full() |
| 6 | except ImportError: |
| 7 | pass |
| 8 | |
| 9 | class LifeTime(object): |
| 10 | __slots__ = "typeid address size varsize birth death".split() |
| 11 | def __init__(self, typeid, address, size, varsize, birth, death=-1): |
| 12 | self.typeid = typeid |
| 13 | self.address = address |
| 14 | self.size = size |
| 15 | self.birth = birth |
| 16 | self.death = death |
| 17 | self.varsize = varsize |
| 18 | |
| 19 | def parse_file(f, callback): |
| 20 | unknown_lifetime = {} |
| 21 | current = 0 |
| 22 | for i, line in enumerate(f): |
| 23 | if i % 100000 == 0: |
| 24 | print(i) |
| 25 | line = line.split() |
| 26 | if line[0] == "free": |
| 27 | _, typeid, address = line |
| 28 | typeid = int(typeid) |
| 29 | address = int(address, 16) |
| 30 | unknown = unknown_lifetime.pop(address) |
| 31 | unknown.death = current |
| 32 | callback(unknown) |
| 33 | else: |
| 34 | if line[0] == "malloc_varsize": |
| 35 | varsize = True |
| 36 | else: |
| 37 | varsize = False |
| 38 | _, typeid, size, address = line |
| 39 | size = int(size) |
| 40 | typeid = int(typeid) |
| 41 | address = int(address, 16) |
| 42 | new = LifeTime(typeid, address, size, varsize, current) |
| 43 | unknown_lifetime[address] = new |
| 44 | current += size |
| 45 | for unknown in unknown_lifetime.itervalues(): |
| 46 | unknown.death = current |
| 47 | callback(unknown) |
| 48 | |
| 49 | def collect_all(f): |
| 50 | all = [] |
| 51 | def callback(obj): |
| 52 | all.append(obj) |
| 53 | parse_file(f, callback) |
| 54 | return all |