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): |
Johnny Chen | 5949d28 | 2011-04-28 23:26:17 +0000 | [diff] [blame^] | 17 | for i in insts: |
| 18 | print i |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 19 | |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 20 | # Create a new debugger instance |
| 21 | debugger = lldb.SBDebugger.Create() |
| 22 | |
| 23 | # When we step or continue, don't return from the function until the process |
| 24 | # stops. We do this by setting the async mode to false. |
| 25 | debugger.SetAsync (False) |
| 26 | |
| 27 | # Create a target from a file and arch |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 28 | print "Creating a target for '%s'" % sys.argv[1] |
| 29 | |
| 30 | target = debugger.CreateTargetWithFileAndArch (sys.argv[1], lldb.LLDB_ARCH_DEFAULT) |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 31 | |
| 32 | if target.IsValid(): |
| 33 | # If the target is valid set a breakpoint at main |
Jim Ingham | bb73710 | 2011-03-30 01:55:23 +0000 | [diff] [blame] | 34 | main_bp = target.BreakpointCreateByName ("main", target.GetExecutable().GetFilename()); |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 35 | |
| 36 | print main_bp |
| 37 | |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 38 | # Launch the process. Since we specified synchronous mode, we won't return |
| 39 | # from this function until we hit the breakpoint at main |
Jim Ingham | bb73710 | 2011-03-30 01:55:23 +0000 | [diff] [blame] | 40 | error = lldb.SBError() |
| 41 | process = target.Launch (debugger.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error) |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 42 | |
| 43 | # Make sure the launch went ok |
| 44 | if process.IsValid(): |
| 45 | # Print some simple process info |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 46 | state = process.GetState () |
| 47 | print process |
| 48 | if state == lldb.eStateStopped: |
| 49 | # Get the first thread |
| 50 | thread = process.GetThreadAtIndex (0) |
| 51 | if thread.IsValid(): |
| 52 | # Print some simple thread info |
| 53 | print thread |
| 54 | # Get the first frame |
| 55 | frame = thread.GetFrameAtIndex (0) |
| 56 | if frame.IsValid(): |
| 57 | # Print some simple frame info |
| 58 | print frame |
| 59 | function = frame.GetFunction() |
| 60 | # See if we have debug info (a function) |
| 61 | if function.IsValid(): |
| 62 | # We do have a function, print some info for the function |
| 63 | print function |
| 64 | # Now get all instructions for this function and print them |
| 65 | insts = function.GetInstructions(target) |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 66 | disassemble_instructions (insts) |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 67 | else: |
| 68 | # See if we have a symbol in the symbol table for where we stopped |
| 69 | symbol = frame.GetSymbol(); |
| 70 | if symbol.IsValid(): |
| 71 | # We do have a symbol, print some info for the symbol |
| 72 | print symbol |
| 73 | # Now get all instructions for this symbol and print them |
| 74 | insts = symbol.GetInstructions(target) |
| 75 | disassemble_instructions (insts) |
Jim Ingham | bb73710 | 2011-03-30 01:55:23 +0000 | [diff] [blame] | 76 | |
Jim Ingham | bb73710 | 2011-03-30 01:55:23 +0000 | [diff] [blame] | 77 | registerList = frame.GetRegisters() |
Johnny Chen | 5949d28 | 2011-04-28 23:26:17 +0000 | [diff] [blame^] | 78 | print "Frame registers (size of register set = %d):" % registerList.GetSize() |
| 79 | for value in registerList: |
| 80 | #print value |
| 81 | print "%s (number of children = %d):" % (value.GetName(), value.GetNumChildren()) |
| 82 | for child in value: |
| 83 | print "Name: ", child.GetName(), " Value: ", child.GetValue(frame) |
Jim Ingham | bb73710 | 2011-03-30 01:55:23 +0000 | [diff] [blame] | 84 | |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 85 | print "Hit the breakpoint at main, continue and wait for program to exit..." |
| 86 | # Now continue to the program exit |
| 87 | process.Continue() |
| 88 | # When we return from the above function we will hopefully be at the |
| 89 | # program exit. Print out some process info |
| 90 | print process |
| 91 | elif state == lldb.eStateExited: |
| 92 | print "Didn't hit the breakpoint at main, program has exited..." |
| 93 | else: |
| 94 | print "Unexpected process state: %s, killing process..." % debugger.StateAsCString (state) |
| 95 | process.Kill() |
| 96 | |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | lldb.SBDebugger.Terminate() |