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 #----------------------------------------------------------------------
16 def disassemble_instructions(insts
):
22 print("Usage: disasm.py [-n name] executable-image")
23 print(" By default, it breaks at and disassembles the 'main' function.")
26 if len(sys
.argv
) == 2:
29 elif len(sys
.argv
) == 4:
30 if sys
.argv
[1] != '-n':
38 # Create a new debugger instance
39 debugger
= lldb
.SBDebugger
.Create()
41 # When we step or continue, don't return from the function until the process
42 # stops. We do this by setting the async mode to false.
43 debugger
.SetAsync(False)
45 # Create a target from a file and arch
46 print("Creating a target for '%s'" % exe
)
48 target
= debugger
.CreateTargetWithFileAndArch(exe
, lldb
.LLDB_ARCH_DEFAULT
)
51 # If the target is valid set a breakpoint at main
52 main_bp
= target
.BreakpointCreateByName(
53 fname
, target
.GetExecutable().GetFilename())
57 # Launch the process. Since we specified synchronous mode, we won't return
58 # from this function until we hit the breakpoint at main
59 process
= target
.LaunchSimple(None, None, os
.getcwd())
61 # Make sure the launch went ok
63 # Print some simple process info
64 state
= process
.GetState()
66 if state
== lldb
.eStateStopped
:
67 # Get the first thread
68 thread
= process
.GetThreadAtIndex(0)
70 # Print some simple thread info
73 frame
= thread
.GetFrameAtIndex(0)
75 # Print some simple frame info
77 function
= frame
.GetFunction()
78 # See if we have debug info (a function)
80 # We do have a function, print some info for the
83 # Now get all instructions for this function and print
85 insts
= function
.GetInstructions(target
)
86 disassemble_instructions(insts
)
88 # See if we have a symbol in the symbol table for where
90 symbol
= frame
.GetSymbol()
92 # We do have a symbol, print some info for the
95 # Now get all instructions for this symbol and
97 insts
= symbol
.GetInstructions(target
)
98 disassemble_instructions(insts
)
100 registerList
= frame
.GetRegisters()
101 print("Frame registers (size of register set = %d):" % registerList
.GetSize())
102 for value
in registerList
:
104 print("%s (number of children = %d):" % (value
.GetName(), value
.GetNumChildren()))
106 print("Name: ", child
.GetName(), " Value: ", child
.GetValue())
108 print("Hit the breakpoint at main, enter to continue and wait for program to exit or 'Ctrl-D'/'quit' to terminate the program")
109 next
= sys
.stdin
.readline()
110 if not next
or next
.rstrip('\n') == 'quit':
111 print("Terminating the inferior process...")
114 # Now continue to the program exit
116 # When we return from the above function we will hopefully be at the
117 # program exit. Print out some process info
119 elif state
== lldb
.eStateExited
:
120 print("Didn't hit the breakpoint at main, program has exited...")
122 print("Unexpected process state: %s, killing process..." % debugger
.StateAsCString(state
))
126 lldb
.SBDebugger
.Terminate()