blob: b635e8c62d8c4b01137f0de63ad6281a4951398c [file] [log] [blame]
Johnny Chen758f2882011-08-19 20:51:15 +00001#!/usr/bin/env python
2
3"""
4Run lldb disassembler on all the binaries specified by a combination of root dir
5and path pattern.
6"""
7
Kate Stoneb9c1b512016-09-06 20:57:50 +00008import os
9import sys
10import subprocess
Johnny Chen758f2882011-08-19 20:51:15 +000011import re
12from optparse import OptionParser
13
14# The directory of this Python script as well as the lldb-disasm.py workhorse.
15scriptPath = None
16
17# The root directory for the SDK symbols.
18root_dir = None
19
20# The regular expression pattern to match the desired pathname to the binaries.
21path_pattern = None
22
23# And the re-compiled regular expression object.
24path_regexp = None
25
26# If specified, number of symbols to disassemble for each qualified binary.
27num_symbols = -1
28
29# Command template of the invocation of lldb disassembler.
Johnny Chen8fff3362011-08-20 01:00:16 +000030template = '%s/lldb-disasm.py -C "platform select remote-ios" -o "-n" -q -e %s -n %s'
Johnny Chen758f2882011-08-19 20:51:15 +000031
32# Regular expression for detecting file output for Mach-o binary.
33mach_o = re.compile('\sMach-O.+binary')
Kate Stoneb9c1b512016-09-06 20:57:50 +000034
35
Johnny Chen758f2882011-08-19 20:51:15 +000036def isbinary(path):
Kate Stoneb9c1b512016-09-06 20:57:50 +000037 file_output = subprocess.Popen(["file", path],
Johnny Chen758f2882011-08-19 20:51:15 +000038 stdout=subprocess.PIPE).stdout.read()
39 return (mach_o.search(file_output) is not None)
40
Kate Stoneb9c1b512016-09-06 20:57:50 +000041
Johnny Chen8fff3362011-08-20 01:00:16 +000042def walk_and_invoke(sdk_root, path_regexp, suffix, num_symbols):
Johnny Chen758f2882011-08-19 20:51:15 +000043 """Look for matched file and invoke lldb disassembly on it."""
44 global scriptPath
Johnny Chen758f2882011-08-19 20:51:15 +000045
Johnny Chen8fff3362011-08-20 01:00:16 +000046 for root, dirs, files in os.walk(sdk_root, topdown=False):
47 for name in files:
48 path = os.path.join(root, name)
Johnny Chen758f2882011-08-19 20:51:15 +000049
Johnny Chen8fff3362011-08-20 01:00:16 +000050 # We're not interested in .h file.
51 if name.endswith(".h"):
52 continue
Johnny Chen1cd81bf2011-08-20 01:02:31 +000053 # Neither a symbolically linked file.
Johnny Chen8fff3362011-08-20 01:00:16 +000054 if os.path.islink(path):
55 continue
Johnny Chen758f2882011-08-19 20:51:15 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 # We'll be pattern matching based on the path relative to the SDK
58 # root.
Johnny Chen8fff3362011-08-20 01:00:16 +000059 replaced_path = path.replace(root_dir, "", 1)
60 # Check regular expression match for the replaced path.
61 if not path_regexp.search(replaced_path):
62 continue
63 # If a suffix is specified, check it, too.
64 if suffix and not name.endswith(suffix):
65 continue
66 if not isbinary(path):
67 continue
Johnny Chen758f2882011-08-19 20:51:15 +000068
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 command = template % (
70 scriptPath, path, num_symbols if num_symbols > 0 else 1000)
Johnny Chen8fff3362011-08-20 01:00:16 +000071 print "Running %s" % (command)
72 os.system(command)
Johnny Chen758f2882011-08-19 20:51:15 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074
Johnny Chen758f2882011-08-19 20:51:15 +000075def main():
76 """Read the root dir and the path spec, invoke lldb-disasm.py on the file."""
77 global scriptPath
78 global root_dir
79 global path_pattern
80 global path_regexp
81 global num_symbols
82
83 scriptPath = sys.path[0]
84
85 parser = OptionParser(usage="""\
86Run lldb disassembler on all the binaries specified by a combination of root dir
87and path pattern.
88""")
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 parser.add_option(
90 '-r',
91 '--root-dir',
92 type='string',
93 action='store',
94 dest='root_dir',
95 help='Mandatory: the root directory for the SDK symbols.')
96 parser.add_option(
97 '-p',
98 '--path-pattern',
99 type='string',
100 action='store',
101 dest='path_pattern',
102 help='Mandatory: regular expression pattern for the desired binaries.')
Johnny Chen758f2882011-08-19 20:51:15 +0000103 parser.add_option('-s', '--suffix',
104 type='string', action='store', default=None,
105 dest='suffix',
106 help='Specify the suffix of the binaries to look for.')
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 parser.add_option(
108 '-n',
109 '--num-symbols',
110 type='int',
111 action='store',
112 default=-1,
113 dest='num_symbols',
114 help="""The number of symbols to disassemble, if specified.""")
Johnny Chen758f2882011-08-19 20:51:15 +0000115
116 opts, args = parser.parse_args()
117 if not opts.root_dir or not opts.path_pattern:
118 parser.print_help()
119 sys.exit(1)
120
121 # Sanity check the root directory.
122 root_dir = opts.root_dir
123 root_dir = os.path.abspath(root_dir)
124 if not os.path.isdir(root_dir):
125 parser.print_help()
126 sys.exit(1)
127
128 path_pattern = opts.path_pattern
129 path_regexp = re.compile(path_pattern)
130 suffix = opts.suffix
131 num_symbols = opts.num_symbols
132
133 print "Root directory for SDK symbols:", root_dir
134 print "Regular expression for the binaries:", path_pattern
135 print "Suffix of the binaries to look for:", suffix
136 print "num of symbols to disassemble:", num_symbols
137
Johnny Chen8fff3362011-08-20 01:00:16 +0000138 walk_and_invoke(root_dir, path_regexp, suffix, num_symbols)
Johnny Chen758f2882011-08-19 20:51:15 +0000139
140
141if __name__ == '__main__':
142 main()