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 |
Johnny Chen | d807c51 | 2011-05-25 22:01:16 +0000 | [diff] [blame^] | 14 | import signal |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 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 | |
Johnny Chen | 528a916 | 2011-05-25 20:48:29 +0000 | [diff] [blame] | 32 | if target: |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 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 |
Johnny Chen | a6cec39 | 2011-05-25 20:56:32 +0000 | [diff] [blame] | 40 | process = target.LaunchSimple (None, None, os.getcwd()) |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 41 | |
| 42 | # Make sure the launch went ok |
Johnny Chen | 528a916 | 2011-05-25 20:48:29 +0000 | [diff] [blame] | 43 | if process: |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 44 | # Print some simple process info |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 45 | state = process.GetState () |
| 46 | print process |
| 47 | if state == lldb.eStateStopped: |
| 48 | # Get the first thread |
| 49 | thread = process.GetThreadAtIndex (0) |
Johnny Chen | 528a916 | 2011-05-25 20:48:29 +0000 | [diff] [blame] | 50 | if thread: |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 51 | # Print some simple thread info |
| 52 | print thread |
| 53 | # Get the first frame |
| 54 | frame = thread.GetFrameAtIndex (0) |
Johnny Chen | 528a916 | 2011-05-25 20:48:29 +0000 | [diff] [blame] | 55 | if frame: |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 56 | # Print some simple frame info |
| 57 | print frame |
| 58 | function = frame.GetFunction() |
| 59 | # See if we have debug info (a function) |
Johnny Chen | 528a916 | 2011-05-25 20:48:29 +0000 | [diff] [blame] | 60 | if function: |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 61 | # We do have a function, print some info for the function |
| 62 | print function |
| 63 | # Now get all instructions for this function and print them |
| 64 | insts = function.GetInstructions(target) |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 65 | disassemble_instructions (insts) |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 66 | else: |
| 67 | # See if we have a symbol in the symbol table for where we stopped |
| 68 | symbol = frame.GetSymbol(); |
Johnny Chen | 528a916 | 2011-05-25 20:48:29 +0000 | [diff] [blame] | 69 | if symbol: |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 70 | # We do have a symbol, print some info for the symbol |
| 71 | print symbol |
| 72 | # Now get all instructions for this symbol and print them |
| 73 | insts = symbol.GetInstructions(target) |
| 74 | disassemble_instructions (insts) |
Jim Ingham | bb73710 | 2011-03-30 01:55:23 +0000 | [diff] [blame] | 75 | |
Jim Ingham | bb73710 | 2011-03-30 01:55:23 +0000 | [diff] [blame] | 76 | registerList = frame.GetRegisters() |
Johnny Chen | 5949d28 | 2011-04-28 23:26:17 +0000 | [diff] [blame] | 77 | print "Frame registers (size of register set = %d):" % registerList.GetSize() |
| 78 | for value in registerList: |
| 79 | #print value |
| 80 | print "%s (number of children = %d):" % (value.GetName(), value.GetNumChildren()) |
| 81 | for child in value: |
| 82 | print "Name: ", child.GetName(), " Value: ", child.GetValue(frame) |
Jim Ingham | bb73710 | 2011-03-30 01:55:23 +0000 | [diff] [blame] | 83 | |
Johnny Chen | d807c51 | 2011-05-25 22:01:16 +0000 | [diff] [blame^] | 84 | print "Hit the breakpoint at main, enter to continue and wait for program to exit or 'Ctrl-D'/'quit' to terminate the program" |
| 85 | next = sys.stdin.readline() |
| 86 | if not next or next.rstrip('\n') == 'quit': |
| 87 | print "Terminating the inferior process..." |
| 88 | process.Kill() |
| 89 | else: |
| 90 | # Now continue to the program exit |
| 91 | process.Continue() |
| 92 | # When we return from the above function we will hopefully be at the |
| 93 | # program exit. Print out some process info |
| 94 | print process |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 95 | elif state == lldb.eStateExited: |
| 96 | print "Didn't hit the breakpoint at main, program has exited..." |
| 97 | else: |
| 98 | print "Unexpected process state: %s, killing process..." % debugger.StateAsCString (state) |
| 99 | process.Kill() |
| 100 | |
Greg Clayton | 1a3083a | 2010-10-06 03:53:16 +0000 | [diff] [blame] | 101 | |
| 102 | |
| 103 | lldb.SBDebugger.Terminate() |