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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8 | import os |
| 9 | import sys |
| 10 | import subprocess |
Johnny Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 11 | import re |
| 12 | from optparse import OptionParser |
| 13 | |
| 14 | # The directory of this Python script as well as the lldb-disasm.py workhorse. |
| 15 | scriptPath = None |
| 16 | |
| 17 | # The root directory for the SDK symbols. |
| 18 | root_dir = None |
| 19 | |
| 20 | # The regular expression pattern to match the desired pathname to the binaries. |
| 21 | path_pattern = None |
| 22 | |
| 23 | # And the re-compiled regular expression object. |
| 24 | path_regexp = None |
| 25 | |
| 26 | # If specified, number of symbols to disassemble for each qualified binary. |
| 27 | num_symbols = -1 |
| 28 | |
| 29 | # Command template of the invocation of lldb disassembler. |
Johnny Chen | 8fff336 | 2011-08-20 01:00:16 +0000 | [diff] [blame] | 30 | template = '%s/lldb-disasm.py -C "platform select remote-ios" -o "-n" -q -e %s -n %s' |
Johnny Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 31 | |
| 32 | # Regular expression for detecting file output for Mach-o binary. |
| 33 | mach_o = re.compile('\sMach-O.+binary') |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | |
| 35 | |
Johnny Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 36 | def isbinary(path): |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | file_output = subprocess.Popen(["file", path], |
Johnny Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 38 | stdout=subprocess.PIPE).stdout.read() |
| 39 | return (mach_o.search(file_output) is not None) |
| 40 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | |
Johnny Chen | 8fff336 | 2011-08-20 01:00:16 +0000 | [diff] [blame] | 42 | def walk_and_invoke(sdk_root, path_regexp, suffix, num_symbols): |
Johnny Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 43 | """Look for matched file and invoke lldb disassembly on it.""" |
| 44 | global scriptPath |
Johnny Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 45 | |
Johnny Chen | 8fff336 | 2011-08-20 01:00:16 +0000 | [diff] [blame] | 46 | for root, dirs, files in os.walk(sdk_root, topdown=False): |
| 47 | for name in files: |
| 48 | path = os.path.join(root, name) |
Johnny Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 49 | |
Johnny Chen | 8fff336 | 2011-08-20 01:00:16 +0000 | [diff] [blame] | 50 | # We're not interested in .h file. |
| 51 | if name.endswith(".h"): |
| 52 | continue |
Johnny Chen | 1cd81bf | 2011-08-20 01:02:31 +0000 | [diff] [blame] | 53 | # Neither a symbolically linked file. |
Johnny Chen | 8fff336 | 2011-08-20 01:00:16 +0000 | [diff] [blame] | 54 | if os.path.islink(path): |
| 55 | continue |
Johnny Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | # We'll be pattern matching based on the path relative to the SDK |
| 58 | # root. |
Johnny Chen | 8fff336 | 2011-08-20 01:00:16 +0000 | [diff] [blame] | 59 | 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 Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | command = template % ( |
| 70 | scriptPath, path, num_symbols if num_symbols > 0 else 1000) |
Johnny Chen | 8fff336 | 2011-08-20 01:00:16 +0000 | [diff] [blame] | 71 | print "Running %s" % (command) |
| 72 | os.system(command) |
Johnny Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | |
Johnny Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 75 | def 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="""\ |
| 86 | Run lldb disassembler on all the binaries specified by a combination of root dir |
| 87 | and path pattern. |
| 88 | """) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | 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 Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 103 | 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | 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 Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 115 | |
| 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 Chen | 8fff336 | 2011-08-20 01:00:16 +0000 | [diff] [blame] | 138 | walk_and_invoke(root_dir, path_regexp, suffix, num_symbols) |
Johnny Chen | 758f288 | 2011-08-19 20:51:15 +0000 | [diff] [blame] | 139 | |
| 140 | |
| 141 | if __name__ == '__main__': |
| 142 | main() |