Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 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 | |
| 11 | import lldb |
| 12 | import os |
| 13 | import sys |
| 14 | import time |
| 15 | |
| 16 | def disassemble_instructions (insts): |
| 17 | for i in range(0, insts.GetSize()): |
| 18 | print insts.GetInstructionAtIndex(i) |
| 19 | |
| 20 | # Initialize LLDB so we can use it |
| 21 | lldb.SBDebugger.Initialize() |
| 22 | |
| 23 | # Create a new debugger instance |
| 24 | debugger = 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. |
| 28 | debugger.SetAsync (False) |
| 29 | |
| 30 | # Create a target from a file and arch |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame^] | 31 | print "Creating a target for '%s'" % sys.argv[1] |
| 32 | |
| 33 | target = debugger.CreateTargetWithFileAndArch (sys.argv[1], lldb.LLDB_ARCH_DEFAULT) |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 34 | |
| 35 | if target.IsValid(): |
| 36 | # If the target is valid set a breakpoint at main |
Greg Clayton | 4fbfcda | 2010-10-06 17:33:30 +0000 | [diff] [blame] | 37 | main_bp = target.BreakpointCreateByName ("main", sys.argv[1]); |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame^] | 38 | |
| 39 | print main_bp |
| 40 | |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 41 | # Launch the process. Since we specified synchronous mode, we won't return |
| 42 | # from this function until we hit the breakpoint at main |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame^] | 43 | process = target.LaunchProcess ([''], [''], "/dev/stdout", 0, False) |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 44 | |
| 45 | # Make sure the launch went ok |
| 46 | if process.IsValid(): |
| 47 | # Print some simple process info |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame^] | 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) |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 68 | disassemble_instructions (insts) |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame^] | 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 | |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | lldb.SBDebugger.Terminate() |