Johnny Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | Run lldb disassembler on all the binaries specified by a combination of root dir |
| 5 | and path pattern. |
| 6 | """ |
| 7 | |
| 8 | import os, sys, subprocess |
| 9 | import re |
| 10 | from optparse import OptionParser |
| 11 | |
| 12 | # The directory of this Python script as well as the lldb-disasm.py workhorse. |
| 13 | scriptPath = None |
| 14 | |
| 15 | # The root directory for the SDK symbols. |
| 16 | root_dir = None |
| 17 | |
| 18 | # The regular expression pattern to match the desired pathname to the binaries. |
| 19 | path_pattern = None |
| 20 | |
| 21 | # And the re-compiled regular expression object. |
| 22 | path_regexp = None |
| 23 | |
| 24 | # If specified, number of symbols to disassemble for each qualified binary. |
| 25 | num_symbols = -1 |
| 26 | |
| 27 | # Command template of the invocation of lldb disassembler. |
| 28 | template = './lldb-disasm.py -C "platform select remote-ios" -o "-n" -q -e %s -n %s' |
| 29 | |
| 30 | # Regular expression for detecting file output for Mach-o binary. |
| 31 | mach_o = re.compile('\sMach-O.+binary') |
| 32 | def isbinary(path): |
| 33 | file_output = subprocess.Popen(["file", path], |
| 34 | stdout=subprocess.PIPE).stdout.read() |
| 35 | return (mach_o.search(file_output) is not None) |
| 36 | |
| 37 | def visit(suffix, dir, names): |
| 38 | """Look for matched file and invoke lldb disassembly on it.""" |
| 39 | global scriptPath |
| 40 | global root_dir |
| 41 | global path_pattern |
| 42 | global path_regexp |
| 43 | global num_symbols |
| 44 | |
| 45 | old_dir = os.getcwd() |
| 46 | for name in names: |
| 47 | path = os.path.join(dir, name) |
| 48 | if os.path.isdir(path): |
| 49 | continue |
| 50 | if name.endswith(".h"): |
| 51 | continue |
| 52 | |
| 53 | replaced_path = path.replace(root_dir, "", 1) |
| 54 | if not path_regexp.search(replaced_path): |
| 55 | continue |
| 56 | if suffix and not name.endswith(suffix): |
| 57 | continue |
| 58 | if not isbinary(path): |
| 59 | continue |
| 60 | |
| 61 | os.chdir(scriptPath) |
| 62 | command = template % (path, num_symbols if num_symbols > 0 else 1000) |
| 63 | print "Running %s" % (command) |
| 64 | os.system(command) |
| 65 | |
| 66 | os.chdir(old_dir) |
| 67 | |
| 68 | def main(): |
| 69 | """Read the root dir and the path spec, invoke lldb-disasm.py on the file.""" |
| 70 | global scriptPath |
| 71 | global root_dir |
| 72 | global path_pattern |
| 73 | global path_regexp |
| 74 | global num_symbols |
| 75 | |
| 76 | scriptPath = sys.path[0] |
| 77 | |
| 78 | parser = OptionParser(usage="""\ |
| 79 | Run lldb disassembler on all the binaries specified by a combination of root dir |
| 80 | and path pattern. |
| 81 | """) |
| 82 | parser.add_option('-r', '--root-dir', |
| 83 | type='string', action='store', |
| 84 | dest='root_dir', |
| 85 | help='Mandatory: the root directory for the SDK symbols.') |
| 86 | parser.add_option('-p', '--path-pattern', |
| 87 | type='string', action='store', |
| 88 | dest='path_pattern', |
| 89 | help='Mandatory: regular expression pattern for the desired binaries.') |
| 90 | parser.add_option('-s', '--suffix', |
| 91 | type='string', action='store', default=None, |
| 92 | dest='suffix', |
| 93 | help='Specify the suffix of the binaries to look for.') |
| 94 | parser.add_option('-n', '--num-symbols', |
| 95 | type='int', action='store', default=-1, |
| 96 | dest='num_symbols', |
| 97 | help="""The number of symbols to disassemble, if specified.""") |
| 98 | |
| 99 | |
| 100 | opts, args = parser.parse_args() |
| 101 | if not opts.root_dir or not opts.path_pattern: |
| 102 | parser.print_help() |
| 103 | sys.exit(1) |
| 104 | |
| 105 | # Sanity check the root directory. |
| 106 | root_dir = opts.root_dir |
| 107 | root_dir = os.path.abspath(root_dir) |
| 108 | if not os.path.isdir(root_dir): |
| 109 | parser.print_help() |
| 110 | sys.exit(1) |
| 111 | |
| 112 | path_pattern = opts.path_pattern |
| 113 | path_regexp = re.compile(path_pattern) |
| 114 | suffix = opts.suffix |
| 115 | num_symbols = opts.num_symbols |
| 116 | |
| 117 | print "Root directory for SDK symbols:", root_dir |
| 118 | print "Regular expression for the binaries:", path_pattern |
| 119 | print "Suffix of the binaries to look for:", suffix |
| 120 | print "num of symbols to disassemble:", num_symbols |
| 121 | |
| 122 | os.path.walk(root_dir, visit, suffix) |
| 123 | |
| 124 | |
| 125 | if __name__ == '__main__': |
| 126 | main() |