xref: /llvm-project/lldb/examples/python/disasm.py (revision 701edc154637cb6231db0d2615677ce6f19c458f)
1#!/usr/bin/python
2
3#----------------------------------------------------------------------
4# Be sure to add the python path that points to the LLDB shared library.
5# On MacOSX csh, tcsh:
6#   setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
7# On MacOSX sh, bash:
8#   export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
9#----------------------------------------------------------------------
10
11import lldb
12import os
13import sys
14import time
15
16def disassemble_instructions (insts):
17    for i in range(insts.GetSize()):
18        print insts.GetInstructionAtIndex(i)
19
20# Initialize LLDB so we can use it
21lldb.SBDebugger.Initialize()
22
23# Create a new debugger instance
24debugger = lldb.SBDebugger.Create()
25
26# When we step or continue, don't return from the function until the process
27# stops. We do this by setting the async mode to false.
28debugger.SetAsync (False)
29
30# Create a target from a file and arch
31print "Creating a target for '%s'" % sys.argv[1]
32
33target = debugger.CreateTargetWithFileAndArch (sys.argv[1], lldb.LLDB_ARCH_DEFAULT)
34
35if target.IsValid():
36    # If the target is valid set a breakpoint at main
37    main_bp = target.BreakpointCreateByName ("main", sys.argv[1]);
38
39    print main_bp
40
41    # Launch the process. Since we specified synchronous mode, we won't return
42    # from this function until we hit the breakpoint at main
43    process = target.LaunchProcess ([''], [''], "/dev/stdout", 0, False)
44
45    # Make sure the launch went ok
46    if process.IsValid():
47        # Print some simple process info
48        state = process.GetState ()
49        print process
50        if state == lldb.eStateStopped:
51            # Get the first thread
52            thread = process.GetThreadAtIndex (0)
53            if thread.IsValid():
54                # Print some simple thread info
55                print thread
56                # Get the first frame
57                frame = thread.GetFrameAtIndex (0)
58                if frame.IsValid():
59                    # Print some simple frame info
60                    print frame
61                    function = frame.GetFunction()
62                    # See if we have debug info (a function)
63                    if function.IsValid():
64                        # We do have a function, print some info for the function
65                        print function
66                        # Now get all instructions for this function and print them
67                        insts = function.GetInstructions(target)
68                        disassemble_instructions (insts)
69                    else:
70                        # See if we have a symbol in the symbol table for where we stopped
71                        symbol = frame.GetSymbol();
72                        if symbol.IsValid():
73                            # We do have a symbol, print some info for the symbol
74                            print symbol
75                            # Now get all instructions for this symbol and print them
76                            insts = symbol.GetInstructions(target)
77                            disassemble_instructions (insts)
78            print "Hit the breakpoint at main, continue and wait for program to exit..."
79            # Now continue to the program exit
80            process.Continue()
81            # When we return from the above function we will hopefully be at the
82            # program exit. Print out some process info
83            print process
84        elif state == lldb.eStateExited:
85            print "Didn't hit the breakpoint at main, program has exited..."
86        else:
87            print "Unexpected process state: %s, killing process..." % debugger.StateAsCString (state)
88            process.Kill()
89
90
91
92lldb.SBDebugger.Terminate()
93