4 Run lldb disassembler on all the binaries specified by a combination of root dir
8 from __future__
import print_function
14 from optparse
import OptionParser
16 # The directory of this Python script as well as the lldb-disasm.py workhorse.
19 # The root directory for the SDK symbols.
22 # The regular expression pattern to match the desired pathname to the binaries.
25 # And the re-compiled regular expression object.
28 # If specified, number of symbols to disassemble for each qualified binary.
31 # Command template of the invocation of lldb disassembler.
32 template
= '%s/lldb-disasm.py -C "platform select remote-ios" -o "-n" -q -e %s -n %s'
34 # Regular expression for detecting file output for Mach-o binary.
35 mach_o
= re
.compile('\sMach-O.+binary')
39 file_output
= subprocess
.Popen(["file", path
],
40 stdout
=subprocess
.PIPE
).stdout
.read()
41 return (mach_o
.search(file_output
) is not None)
44 def walk_and_invoke(sdk_root
, path_regexp
, suffix
, num_symbols
):
45 """Look for matched file and invoke lldb disassembly on it."""
48 for root
, dirs
, files
in os
.walk(sdk_root
, topdown
=False):
50 path
= os
.path
.join(root
, name
)
52 # We're not interested in .h file.
53 if name
.endswith(".h"):
55 # Neither a symbolically linked file.
56 if os
.path
.islink(path
):
59 # We'll be pattern matching based on the path relative to the SDK
61 replaced_path
= path
.replace(root_dir
, "", 1)
62 # Check regular expression match for the replaced path.
63 if not path_regexp
.search(replaced_path
):
65 # If a suffix is specified, check it, too.
66 if suffix
and not name
.endswith(suffix
):
68 if not isbinary(path
):
71 command
= template
% (
72 scriptPath
, path
, num_symbols
if num_symbols
> 0 else 1000)
73 print("Running %s" % (command
))
78 """Read the root dir and the path spec, invoke lldb-disasm.py on the file."""
85 scriptPath
= sys
.path
[0]
87 parser
= OptionParser(usage
="""\
88 Run lldb disassembler on all the binaries specified by a combination of root dir
97 help='Mandatory: the root directory for the SDK symbols.')
104 help='Mandatory: regular expression pattern for the desired binaries.')
105 parser
.add_option('-s', '--suffix',
106 type='string', action
='store', default
=None,
108 help='Specify the suffix of the binaries to look for.')
116 help="""The number of symbols to disassemble, if specified.""")
118 opts
, args
= parser
.parse_args()
119 if not opts
.root_dir
or not opts
.path_pattern
:
123 # Sanity check the root directory.
124 root_dir
= opts
.root_dir
125 root_dir
= os
.path
.abspath(root_dir
)
126 if not os
.path
.isdir(root_dir
):
130 path_pattern
= opts
.path_pattern
131 path_regexp
= re
.compile(path_pattern
)
133 num_symbols
= opts
.num_symbols
135 print("Root directory for SDK symbols:", root_dir
)
136 print("Regular expression for the binaries:", path_pattern
)
137 print("Suffix of the binaries to look for:", suffix
)
138 print("num of symbols to disassemble:", num_symbols
)
140 walk_and_invoke(root_dir
, path_regexp
, suffix
, num_symbols
)
143 if __name__
== '__main__':