3 #----------------------------------------------------------------------
4 # Be sure to add the python path that points to the LLDB shared library.
6 # setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
8 # export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python
9 #----------------------------------------------------------------------
15 def disassemble_instructions (insts):
20 print "Usage: disasm.py [-n name] executable-image"
21 print " By default, it breaks at and disassembles the 'main' function."
24 if len(sys.argv) == 2:
27 elif len(sys.argv) == 4:
28 if sys.argv[1] != '-n':
36 # Create a new debugger instance
37 debugger = lldb.SBDebugger.Create()
39 # When we step or continue, don't return from the function until the process
40 # stops. We do this by setting the async mode to false.
41 debugger.SetAsync (False)
43 # Create a target from a file and arch
44 print "Creating a target for '%s'" % exe
46 target = debugger.CreateTargetWithFileAndArch (exe, lldb.LLDB_ARCH_DEFAULT)
49 # If the target is valid set a breakpoint at main
50 main_bp = target.BreakpointCreateByName (fname, target.GetExecutable().GetFilename());
54 # Launch the process. Since we specified synchronous mode, we won't return
55 # from this function until we hit the breakpoint at main
56 process = target.LaunchSimple (None, None, os.getcwd())
58 # Make sure the launch went ok
60 # Print some simple process info
61 state = process.GetState ()
63 if state == lldb.eStateStopped:
64 # Get the first thread
65 thread = process.GetThreadAtIndex (0)
67 # Print some simple thread info
70 frame = thread.GetFrameAtIndex (0)
72 # Print some simple frame info
74 function = frame.GetFunction()
75 # See if we have debug info (a function)
77 # We do have a function, print some info for the function
79 # Now get all instructions for this function and print them
80 insts = function.GetInstructions(target)
81 disassemble_instructions (insts)
83 # See if we have a symbol in the symbol table for where we stopped
84 symbol = frame.GetSymbol();
86 # We do have a symbol, print some info for the symbol
88 # Now get all instructions for this symbol and print them
89 insts = symbol.GetInstructions(target)
90 disassemble_instructions (insts)
92 registerList = frame.GetRegisters()
93 print "Frame registers (size of register set = %d):" % registerList.GetSize()
94 for value in registerList:
96 print "%s (number of children = %d):" % (value.GetName(), value.GetNumChildren())
98 print "Name: ", child.GetName(), " Value: ", child.GetValue()
100 print "Hit the breakpoint at main, enter to continue and wait for program to exit or 'Ctrl-D'/'quit' to terminate the program"
101 next = sys.stdin.readline()
102 if not next or next.rstrip('\n') == 'quit':
103 print "Terminating the inferior process..."
106 # Now continue to the program exit
108 # When we return from the above function we will hopefully be at the
109 # program exit. Print out some process info
111 elif state == lldb.eStateExited:
112 print "Didn't hit the breakpoint at main, program has exited..."
114 print "Unexpected process state: %s, killing process..." % debugger.StateAsCString (state)
119 lldb.SBDebugger.Terminate()