1#!/usr/bin/python
2
3import re, sys
4
5# 0x4460e2 malloc (17408) returns 0xd96fe0
6M = re.compile('.* malloc \((\\d+)\) returns (.*)')
7F = re.compile('freed (.*)')
8S = re.compile('========+ (.*)')
9
10alloc = {}
11section = ''
12
13def dump():
14    sections = {}
15    for addr in alloc:
16        sec = alloc[addr][1]
17        if sec in sections:
18            sections[sec] += alloc[addr][0]
19        else:
20            sections[sec] = alloc[addr][0]
21
22    print section
23    for key in sorted(sections):
24        print "   ", key, sections[key]
25
26hook = 'hook'
27if len(sys.argv) > 1:
28    hook = sys.argv[1]
29
30with open(hook) as f:
31    for line in f:
32        m = M.search(line)
33        if m:
34            # print 'alloc', m.group(1)
35            alloc[m.group(2)] = (int(m.group(1)), section)
36            continue
37        m = F.match(line)
38        if m:
39            if m.group(1) in alloc:
40                del alloc[m.group(1)]
41            else:
42                # print 'free', m.group(1)
43                pass
44            continue
45        m = S.match(line)
46        if m:
47            dump()
48            section = m.group(1)
49
50
51print alloc
52
53# after handshaking 0
54# {'SSL_handshake client': 7518, 'BIO_new_bio_pair 0': 35136, 'SSL_new client': 2128, 'SSL_handshake server': 146, 'SSL_new server': 3092, 'SSL_connect': 33680, 'SSL_accept': 36755}
55# client 7518 + 2128 + 33680 = 43326
56# server 146 + 3092 + 36755 = 39993
57
58
59# after handshaking 1
60# {'SSL_connect 0': 35432, 'SSL_accept 0': 40400, 'SSL_new server 0': 112, 'SSL_new server 1': 2964, 'BIO_new_bio_pair 1': 35152, 'SSL_connect 1': 405, 'SSL_accept 1': 760, 'SSL_new client 1': 2360, 'SSL_handshake server 1': 1792, 'SSL_handshake client 1': 8924, 'SSL_handshake client 0': 112}
61# client 2360 + 405 + 8924 = 11689
62# server 2964 + 760 + 1792 = 5516
63