Improvements to checkstack.py tool.
[seabios.git] / tools / checkstack.py
1 #!/usr/bin/env python
2 # Script that tries to find how much stack space each function in an
3 # object is using.
4 #
5 # Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
6 #
7 # This file may be distributed under the terms of the GNU GPLv3 license.
8
9 # Usage:
10 #   objdump -m i386 -M i8086 -M suffix -d out/rom16.o | tools/checkstack.py
11
12 import sys
13 import re
14
15 # List of functions we can assume are never called.
16 #IGNORE = ['screenc', 'BX_PANIC', '__dprintf']
17 IGNORE = ['screenc', 'BX_PANIC']
18
19 # Find out maximum stack usage for a function
20 def calcmaxstack(funcs, func):
21     info = funcs[func]
22     if func.split('.')[0] in IGNORE:
23         # Function is hardcoded to report 0 stack usage
24         info[1] = 0
25         return
26     # Find max of all nested calls.
27     max = info[0]
28     for addr, callfname, usage in info[2]:
29         callinfo = funcs[callfname]
30         if callinfo[1] is None:
31             calcmaxstack(funcs, callfname)
32         totusage = usage + callinfo[1]
33         if totusage > max:
34             max = totusage
35     info[1] = max
36
37 hex_s = r'[0-9a-f]+'
38 re_func = re.compile(r'^' + hex_s + r' <(?P<func>.*)>:$')
39 re_asm = re.compile(
40     r'^[ ]*(?P<addr>' + hex_s + r'):\t.*\t'
41     r'(addr32 )?(?P<insn>[a-z0-9]+ [^<]*)( <(?P<ref>.*)>)?$')
42 re_usestack = re.compile(
43     r'^(push.*)|(sub.* [$](?P<num>0x' + hex_s + r'),%esp)$')
44
45 def calc():
46     # funcs = {funcname: [basicstackusage, maxstackusage
47     #                     , [(addr, callfname, stackusage), ...]] }
48     funcs = {'<indirect>': [0, 0, []]}
49     cur = None
50     atstart = 0
51     stackusage = 0
52
53     # Parse input lines
54     for line in sys.stdin.readlines():
55         m = re_func.match(line)
56         if m is not None:
57             # Found function
58             cur = [0, None, []]
59             funcs[m.group('func')] = cur
60             stackusage = 0
61             atstart = 1
62             subfuncs = {}
63             continue
64         m = re_asm.match(line)
65         if m is not None:
66             insn = m.group('insn')
67
68             im = re_usestack.match(insn)
69             if im is not None:
70                 if insn[:5] == 'pushl' or insn[:6] == 'pushfl':
71                     stackusage += 4
72                     continue
73                 elif insn[:5] == 'pushw' or insn[:6] == 'pushfw':
74                     stackusage += 2
75                     continue
76                 stackusage += int(im.group('num'), 16)
77
78             if atstart:
79                 cur[0] = stackusage
80                 atstart = 0
81
82             ref = m.group('ref')
83             if ref is None:
84                 if insn[:6] == 'lcallw':
85                     stackusage += 4
86                     ref = '<indirect>'
87                 else:
88                     # misc instruction - just ignore
89                     continue
90             else:
91                 # Jump or call insn
92                 if '+' in ref:
93                     # Inter-function jump - reset stack usage to
94                     # preamble usage
95                     stackusage = cur[0]
96                     continue
97                 if ref.split('.')[0] in IGNORE:
98                     # Call ignored - list only for informational purposes
99                     stackusage = 0
100                 elif insn[:1] == 'j':
101                     # Tail call
102                     stackusage = 0
103                 elif insn[:5] == 'calll':
104                     stackusage += 4
105                 else:
106                     print "unknown call", ref
107             if (ref, stackusage) not in subfuncs:
108                 cur[2].append((m.group('addr'), ref, stackusage))
109                 subfuncs[(ref, stackusage)] = 1
110             # Reset stack usage to preamble usage
111             stackusage = cur[0]
112
113             continue
114
115         #print "other", repr(line)
116
117     # Calculate maxstackusage
118     for func, info in funcs.items():
119         if info[1] is not None:
120             continue
121         calcmaxstack(funcs, func)
122
123     # Show all functions
124     funcnames = funcs.keys()
125     funcnames.sort()
126     for func in funcnames:
127         basicusage, maxusage, calls = funcs[func]
128         if maxusage == 0:
129             continue
130         print "\n%s[%d,%d]:" % (func, basicusage, maxusage)
131         for addr, callfname, stackusage in calls:
132             callinfo = funcs[callfname]
133             print "    %04s:%-40s [%d+%d,%d]" % (
134                 addr, callfname, stackusage, callinfo[0], stackusage+callinfo[1])
135
136 def main():
137     calc()
138
139 if __name__ == '__main__':
140     main()