1afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen#!/usr/bin/env python
2afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
3afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen"""
4afd19042fa577feabe14c1b97dd5653e3af567d6Johnny ChenRun lldb to disassemble all the available functions for an executable image.
5afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
6afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen"""
7afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
8afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chenimport os
98ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chenimport re
10afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chenimport sys
11afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chenfrom optparse import OptionParser
12afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
13afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chendef setupSysPath():
14afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    """
152efc7c6c4e791ed64adf037e4908b598d1f0ec8bJohnny Chen    Add LLDB.framework/Resources/Python and the test dir to the sys.path.
16afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    """
17afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # Get the directory containing the current script.
18afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    scriptPath = sys.path[0]
19afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    if not scriptPath.endswith(os.path.join('utils', 'test')):
20afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        print "This script expects to reside in lldb's utils/test directory."
21afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        sys.exit(-1)
22afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
23afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # This is our base name component.
24afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    base = os.path.abspath(os.path.join(scriptPath, os.pardir, os.pardir))
25afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
26afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # This is for the goodies in the test directory under base.
27afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    sys.path.append(os.path.join(base,'test'))
28afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
29afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # These are for xcode build directories.
30afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    xcode3_build_dir = ['build']
31afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    xcode4_build_dir = ['build', 'lldb', 'Build', 'Products']
32afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    dbg = ['Debug']
33afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    rel = ['Release']
34afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    bai = ['BuildAndIntegration']
35afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    python_resource_dir = ['LLDB.framework', 'Resources', 'Python']
36afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
37afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    dbgPath  = os.path.join(base, *(xcode3_build_dir + dbg + python_resource_dir))
38afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    dbgPath2 = os.path.join(base, *(xcode4_build_dir + dbg + python_resource_dir))
39afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    relPath  = os.path.join(base, *(xcode3_build_dir + rel + python_resource_dir))
40afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    relPath2 = os.path.join(base, *(xcode4_build_dir + rel + python_resource_dir))
41afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    baiPath  = os.path.join(base, *(xcode3_build_dir + bai + python_resource_dir))
42afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    baiPath2 = os.path.join(base, *(xcode4_build_dir + bai + python_resource_dir))
43afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
44afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    lldbPath = None
45afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    if os.path.isfile(os.path.join(dbgPath, 'lldb.py')):
46afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        lldbPath = dbgPath
47afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    elif os.path.isfile(os.path.join(dbgPath2, 'lldb.py')):
48afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        lldbPath = dbgPath2
49afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    elif os.path.isfile(os.path.join(relPath, 'lldb.py')):
50afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        lldbPath = relPath
51afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    elif os.path.isfile(os.path.join(relPath2, 'lldb.py')):
52afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        lldbPath = relPath2
53afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    elif os.path.isfile(os.path.join(baiPath, 'lldb.py')):
54afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        lldbPath = baiPath
55afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    elif os.path.isfile(os.path.join(baiPath2, 'lldb.py')):
56afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        lldbPath = baiPath2
57afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
58afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    if not lldbPath:
59afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        print 'This script requires lldb.py to be in either ' + dbgPath + ',',
60afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        print relPath + ', or ' + baiPath
61afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        sys.exit(-1)
62afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
63afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # This is to locate the lldb.py module.  Insert it right after sys.path[0].
64afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    sys.path[1:1] = [lldbPath]
6540ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen    #print "sys.path:", sys.path
66afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
67afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
6840ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chendef run_command(ci, cmd, res, echo=True):
6940ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen    if echo:
709aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen        print "run command:", cmd
71afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    ci.HandleCommand(cmd, res)
72afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    if res.Succeeded():
7340ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen        if echo:
749aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen            print "run_command output:", res.GetOutput()
75afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    else:
7640ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen        if echo:
779aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen            print "run command failed!"
789aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen            print "run_command error:", res.GetError()
79afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
808ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chendef do_lldb_disassembly(lldb_commands, exe, disassemble_options, num_symbols,
818ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                        symbols_to_disassemble,
828ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                        re_symbol_pattern,
838ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                        quiet_disassembly):
847df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen    import lldb, atexit, re
85afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
86afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # Create the debugger instance now.
87afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    dbg = lldb.SBDebugger.Create()
88234ebabe6218127a39cbe98dbad7b53f06907becJohnny Chen    if not dbg:
89afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen            raise Exception('Invalid debugger instance')
90afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
91afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # Register an exit callback.
92afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    atexit.register(lambda: lldb.SBDebugger.Terminate())
93afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
94afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # We want our debugger to be synchronous.
95afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    dbg.SetAsync(False)
96afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
97afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # Get the command interpreter from the debugger.
98afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    ci = dbg.GetCommandInterpreter()
99afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    if not ci:
100afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        raise Exception('Could not get the command interpreter')
101afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
102afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # And the associated result object.
103afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    res = lldb.SBCommandReturnObject()
104afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
105afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # See if there any extra command(s) to execute before we issue the file command.
106afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    for cmd in lldb_commands:
10740ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen        run_command(ci, cmd, res, not quiet_disassembly)
108afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
109563ce6abb47005e3829e5d8fb33c4ed45bda6430Johnny Chen    # Now issue the file command.
11040ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen    run_command(ci, 'file %s' % exe, res, not quiet_disassembly)
111563ce6abb47005e3829e5d8fb33c4ed45bda6430Johnny Chen
1127df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen    # Create a target.
113563ce6abb47005e3829e5d8fb33c4ed45bda6430Johnny Chen    #target = dbg.CreateTarget(exe)
114563ce6abb47005e3829e5d8fb33c4ed45bda6430Johnny Chen    target = dbg.GetSelectedTarget()
1157df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen    stream = lldb.SBStream()
1167df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen
117563ce6abb47005e3829e5d8fb33c4ed45bda6430Johnny Chen    def IsCodeType(symbol):
118563ce6abb47005e3829e5d8fb33c4ed45bda6430Johnny Chen        """Check whether an SBSymbol represents code."""
119563ce6abb47005e3829e5d8fb33c4ed45bda6430Johnny Chen        return symbol.GetType() == lldb.eSymbolTypeCode
120563ce6abb47005e3829e5d8fb33c4ed45bda6430Johnny Chen
1217df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen    # Define a generator for the symbols to disassemble.
1228ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen    def symbol_iter(num, symbols, re_symbol_pattern, target, verbose):
1237df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen        # If we specify the symbols to disassemble, ignore symbol table dump.
1247df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen        if symbols:
1257df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen            for i in range(len(symbols)):
12640ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen                if verbose:
12740ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen                    print "symbol:", symbols[i]
1287df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen                yield symbols[i]
1297df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen        else:
1307df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen            limited = True if num != -1 else False
1317df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen            if limited:
1327df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen                count = 0
133b66b112452ea0fc3b920f244912eae2790cfa8b1Johnny Chen            if re_symbol_pattern:
134b66b112452ea0fc3b920f244912eae2790cfa8b1Johnny Chen                pattern = re.compile(re_symbol_pattern)
1357df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen            stream = lldb.SBStream()
136a3cd9479fb6ffca90d335cb4ca802f4686dd0e10Johnny Chen            for m in target.module_iter():
13740ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen                if verbose:
13840ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen                    print "module:", m
139a3cd9479fb6ffca90d335cb4ca802f4686dd0e10Johnny Chen                for s in m:
1407df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen                    if limited and count >= num:
1417df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen                        return
1428ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                    # If a regexp symbol pattern is supplied, consult it.
1438ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                    if re_symbol_pattern:
1448ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                        # If the pattern does not match, look for the next symbol.
1458ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                        if not pattern.match(s.GetName()):
1468ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                            continue
1478ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen
1488ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                    # If we come here, we're ready to disassemble the symbol.
14940ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen                    if verbose:
15040ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen                        print "symbol:", s.GetName()
1517df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen                    if IsCodeType(s):
1527df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen                        if limited:
1537df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen                            count = count + 1
154d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                            if verbose:
155d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                                print "returning symbol:", s.GetName()
1567df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen                        yield s.GetName()
157d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                    if verbose:
158d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                        print "start address:", s.GetStartAddress()
159d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                        print "end address:", s.GetEndAddress()
160d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                        s.GetDescription(stream)
161d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                        print "symbol description:", stream.GetData()
162d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                        stream.Clear()
1637df6fc423f1922ba0700d38e990d8422493184c2Johnny Chen
164337836bbe11dd3543b6c6e236fa161d072181328Johnny Chen    # Disassembly time.
1658ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen    for symbol in symbol_iter(num_symbols, symbols_to_disassemble, re_symbol_pattern, target, not quiet_disassembly):
166337836bbe11dd3543b6c6e236fa161d072181328Johnny Chen        cmd = "disassemble %s '%s'" % (disassemble_options, symbol)
16740ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen        run_command(ci, cmd, res, not quiet_disassembly)
1689aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen
169afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
170afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chendef main():
171afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # This is to set up the Python path to include the pexpect-2.4 dir.
172afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # Remember to update this when/if things change.
173afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    scriptPath = sys.path[0]
174afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    sys.path.append(os.path.join(scriptPath, os.pardir, os.pardir, 'test', 'pexpect-2.4'))
175afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
176afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    parser = OptionParser(usage="""\
177afd19042fa577feabe14c1b97dd5653e3af567d6Johnny ChenRun lldb to disassemble all the available functions for an executable image.
178afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
179afd19042fa577feabe14c1b97dd5653e3af567d6Johnny ChenUsage: %prog [options]
180afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen""")
181afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    parser.add_option('-C', '--lldb-command',
182afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen                      type='string', action='append', metavar='COMMAND',
183afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen                      default=[], dest='lldb_commands',
184afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen                      help='Command(s) lldb executes after starting up (can be empty)')
185afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    parser.add_option('-e', '--executable',
186afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen                      type='string', action='store',
187afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen                      dest='executable',
1889aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen                      help="""Mandatory: the executable to do disassembly on.""")
1899aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen    parser.add_option('-o', '--options',
1909aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen                      type='string', action='store',
1919aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen                      dest='disassemble_options',
1929aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen                      help="""Mandatory: the options passed to lldb's 'disassemble' command.""")
193d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen    parser.add_option('-q', '--quiet-disassembly',
194d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                      action='store_true', default=False,
195d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                      dest='quiet_disassembly',
196d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                      help="""The symbol(s) to invoke lldb's 'disassemble' command on, if specified.""")
1978ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen    parser.add_option('-n', '--num-symbols',
1988ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                      type='int', action='store', default=-1,
1998ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                      dest='num_symbols',
2008ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                      help="""The number of symbols to disassemble, if specified.""")
2018ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen    parser.add_option('-p', '--symbol_pattern',
2028ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                      type='string', action='store',
2038ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                      dest='re_symbol_pattern',
2048ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                      help="""The regular expression of symbols to invoke lldb's 'disassemble' command.""")
205337836bbe11dd3543b6c6e236fa161d072181328Johnny Chen    parser.add_option('-s', '--symbol',
206337836bbe11dd3543b6c6e236fa161d072181328Johnny Chen                      type='string', action='append', metavar='SYMBOL', default=[],
207337836bbe11dd3543b6c6e236fa161d072181328Johnny Chen                      dest='symbols_to_disassemble',
208337836bbe11dd3543b6c6e236fa161d072181328Johnny Chen                      help="""The symbol(s) to invoke lldb's 'disassemble' command on, if specified.""")
209337836bbe11dd3543b6c6e236fa161d072181328Johnny Chen
210afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    opts, args = parser.parse_args()
211afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
212afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    lldb_commands = opts.lldb_commands
213afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
2149aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen    if not opts.executable or not opts.disassemble_options:
215afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        parser.print_help()
216afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen        sys.exit(1)
2179aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen
218afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    executable = opts.executable
2199aaceb1bed6a4f83389c76b1cb7a71f8d22db547Johnny Chen    disassemble_options = opts.disassemble_options
220d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen    quiet_disassembly = opts.quiet_disassembly
2218ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen    num_symbols = opts.num_symbols
222337836bbe11dd3543b6c6e236fa161d072181328Johnny Chen    symbols_to_disassemble = opts.symbols_to_disassemble
2238ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen    re_symbol_pattern = opts.re_symbol_pattern
224afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
225afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    # We have parsed the options.
22640ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen    if not quiet_disassembly:
22740ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen        print "lldb commands:", lldb_commands
22840ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen        print "executable:", executable
22940ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen        print "disassemble options:", disassemble_options
23040ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen        print "quiet disassembly output:", quiet_disassembly
23140ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen        print "num of symbols to disassemble:", num_symbols
23240ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen        print "symbols to disassemble:", symbols_to_disassemble
23340ad91fb8708dbfbcf5a16cdf4c2faa92f4e83f9Johnny Chen        print "regular expression of symbols to disassemble:", re_symbol_pattern
234afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
235afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    setupSysPath()
236d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen    do_lldb_disassembly(lldb_commands, executable, disassemble_options,
237d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                        num_symbols,
238d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                        symbols_to_disassemble,
2398ddc8020b9ce98a426cc1bc6a1a56d3a3baeb94cJohnny Chen                        re_symbol_pattern,
240d7e532921a938378cceea96a60d4235abe719a1fJohnny Chen                        quiet_disassembly)
241afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen
242afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chenif __name__ == '__main__':
243afd19042fa577feabe14c1b97dd5653e3af567d6Johnny Chen    main()
244