blob: 3de3730c422ca646bf722ab5a9e9a17bf6e3016f [file] [log] [blame]
Greg Clayton1a3083a2010-10-06 03:53:16 +00001#!/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
11import lldb
12import os
13import sys
14import time
15
16def 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
21lldb.SBDebugger.Initialize()
22
23# Create a new debugger instance
24debugger = 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.
28debugger.SetAsync (False)
29
30# Create a target from a file and arch
Greg Claytond8c62532010-10-07 04:19:01 +000031print "Creating a target for '%s'" % sys.argv[1]
32
33target = debugger.CreateTargetWithFileAndArch (sys.argv[1], lldb.LLDB_ARCH_DEFAULT)
Greg Clayton1a3083a2010-10-06 03:53:16 +000034
35if target.IsValid():
36 # If the target is valid set a breakpoint at main
Greg Clayton4fbfcda2010-10-06 17:33:30 +000037 main_bp = target.BreakpointCreateByName ("main", sys.argv[1]);
Greg Claytond8c62532010-10-07 04:19:01 +000038
39 print main_bp
40
Greg Clayton1a3083a2010-10-06 03:53:16 +000041 # Launch the process. Since we specified synchronous mode, we won't return
42 # from this function until we hit the breakpoint at main
Greg Claytond8c62532010-10-07 04:19:01 +000043 process = target.LaunchProcess ([''], [''], "/dev/stdout", 0, False)
Greg Clayton1a3083a2010-10-06 03:53:16 +000044
45 # Make sure the launch went ok
46 if process.IsValid():
47 # Print some simple process info
Greg Claytond8c62532010-10-07 04:19:01 +000048 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 Clayton1a3083a2010-10-06 03:53:16 +000068 disassemble_instructions (insts)
Greg Claytond8c62532010-10-07 04:19:01 +000069 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 Clayton1a3083a2010-10-06 03:53:16 +000090
91
92lldb.SBDebugger.Terminate()