1116e48deSShuo Chen#!/usr/bin/python
2116e48deSShuo Chen
325943c9eSShuo Chenimport re, sys
4116e48deSShuo Chen
5116e48deSShuo Chen# 0x4460e2 malloc (17408) returns 0xd96fe0
6116e48deSShuo ChenM = re.compile('.* malloc \((\\d+)\) returns (.*)')
7116e48deSShuo ChenF = re.compile('freed (.*)')
8116e48deSShuo ChenS = re.compile('========+ (.*)')
9116e48deSShuo Chen
10116e48deSShuo Chenalloc = {}
11116e48deSShuo Chensection = ''
12116e48deSShuo Chen
13116e48deSShuo Chendef dump():
14116e48deSShuo Chen    sections = {}
15116e48deSShuo Chen    for addr in alloc:
16116e48deSShuo Chen        sec = alloc[addr][1]
17116e48deSShuo Chen        if sec in sections:
18116e48deSShuo Chen            sections[sec] += alloc[addr][0]
19116e48deSShuo Chen        else:
20116e48deSShuo Chen            sections[sec] = alloc[addr][0]
21116e48deSShuo Chen
22116e48deSShuo Chen    print section
23116e48deSShuo Chen    for key in sorted(sections):
24116e48deSShuo Chen        print "   ", key, sections[key]
25116e48deSShuo Chen
2625943c9eSShuo Chenhook = 'hook'
2725943c9eSShuo Chenif len(sys.argv) > 1:
2825943c9eSShuo Chen    hook = sys.argv[1]
2925943c9eSShuo Chen
3025943c9eSShuo Chenwith open(hook) as f:
31116e48deSShuo Chen    for line in f:
32116e48deSShuo Chen        m = M.search(line)
33116e48deSShuo Chen        if m:
34116e48deSShuo Chen            # print 'alloc', m.group(1)
35116e48deSShuo Chen            alloc[m.group(2)] = (int(m.group(1)), section)
36116e48deSShuo Chen            continue
37116e48deSShuo Chen        m = F.match(line)
38116e48deSShuo Chen        if m:
39116e48deSShuo Chen            if m.group(1) in alloc:
40116e48deSShuo Chen                del alloc[m.group(1)]
41116e48deSShuo Chen            else:
42116e48deSShuo Chen                # print 'free', m.group(1)
43116e48deSShuo Chen                pass
44116e48deSShuo Chen            continue
45116e48deSShuo Chen        m = S.match(line)
46116e48deSShuo Chen        if m:
47116e48deSShuo Chen            dump()
48116e48deSShuo Chen            section = m.group(1)
49116e48deSShuo Chen
50116e48deSShuo Chen
51116e48deSShuo Chenprint alloc
52116e48deSShuo Chen
53116e48deSShuo Chen# after handshaking 0
54116e48deSShuo Chen# {'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}
55116e48deSShuo Chen# client 7518 + 2128 + 33680 = 43326
56116e48deSShuo Chen# server 146 + 3092 + 36755 = 39993
57116e48deSShuo Chen
58116e48deSShuo Chen
59116e48deSShuo Chen# after handshaking 1
60116e48deSShuo Chen# {'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}
61116e48deSShuo Chen# client 2360 + 405 + 8924 = 11689
62116e48deSShuo Chen# server 2964 + 760 + 1792 = 5516
63