diff options
author | Bjoern B. Brandenburg <bbb@cs.unc.edu> | 2008-09-11 11:54:16 -0400 |
---|---|---|
committer | Bjoern B. Brandenburg <bbb@cs.unc.edu> | 2008-09-11 11:54:16 -0400 |
commit | b65ea2396e5a77600958f73d6b8bb13c6e1042ad (patch) | |
tree | f95a84e2d14afec86a95784e35cc065dae07300f | |
parent | d0f9a1392cd658a9dae0dd04dac58ce0cdc3f366 (diff) |
add tool for showing disassembly of specific symbols
-rwxr-xr-x | show_asm | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/show_asm b/show_asm new file mode 100755 index 0000000..1ddbab4 --- /dev/null +++ b/show_asm | |||
@@ -0,0 +1,59 @@ | |||
1 | #!/usr/bin/env python | ||
2 | |||
3 | from subprocess import Popen, PIPE, call | ||
4 | import re | ||
5 | import sys | ||
6 | |||
7 | NM_FORMAT = "([0-9a-f]+) *([0-9a-f]*) *([AaBbCcDdGgIiNnRrSsTtUuVvWw])" + \ | ||
8 | " ([_a-zA-Z.0-9]+)" | ||
9 | |||
10 | nm_re = re.compile(NM_FORMAT) | ||
11 | |||
12 | def parse_nm_output(str): | ||
13 | "returns (start, length, type, name)" | ||
14 | m = nm_re.match(str) | ||
15 | if m: | ||
16 | start = int(m.group(1), 16) | ||
17 | if m.group(2) != '': | ||
18 | length = int(m.group(2), 16) | ||
19 | else: | ||
20 | length = 0 | ||
21 | return (start, length, m.group(3), m.group(4)) | ||
22 | else: | ||
23 | return None | ||
24 | |||
25 | def symbol_is_text(addr): | ||
26 | return addr[2] in "tT" | ||
27 | |||
28 | def nm(file): | ||
29 | cmd = "nm -S %s" % file | ||
30 | p = Popen(cmd, shell=True, stdout=PIPE) | ||
31 | return p.stdout | ||
32 | |||
33 | def nm_filtered(file, symbol): | ||
34 | cmd = "nm -S %s | egrep %s " % (file, symbol) | ||
35 | p = Popen(cmd, shell=True, stdout=PIPE) | ||
36 | return p.stdout | ||
37 | |||
38 | def objdump_symbol(file, addr): | ||
39 | objdump_cmd = "objdump -S -d --start-address=0x%x --stop-address=0x%x %s" \ | ||
40 | % (addr[0], addr[0] + addr[1], file) | ||
41 | filter_cmd = "egrep -v 'Disassembly of|file format|^$'" | ||
42 | pipe_cmd = "%s | %s" % (objdump_cmd, filter_cmd) | ||
43 | print "=====[ %s (0x%x - 0x%x, type: %s) ]=====" % \ | ||
44 | (addr[3], addr[0], addr[0] + addr[1], addr[2]) | ||
45 | sys.stdout.flush() | ||
46 | call(pipe_cmd, shell=True) | ||
47 | |||
48 | |||
49 | def get_address_ranges(file, symbol): | ||
50 | for line in nm_filtered(file, symbol): | ||
51 | addr = parse_nm_output(line) | ||
52 | if addr and addr[1] > 0 and symbol_is_text(addr): | ||
53 | objdump_symbol(file, addr) | ||
54 | |||
55 | if __name__ == "__main__": | ||
56 | if len(sys.argv) != 2: | ||
57 | print "Usage: %s <symbol pattern>" % sys.argv[0] | ||
58 | else: | ||
59 | get_address_ranges("vmlinux", sys.argv[1]) | ||