blob: 9dd2b74f9cc38837b7c310462332671e659aebb5 [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):
Johnny Chen5949d282011-04-28 23:26:17 +000017 for i in insts:
18 print i
Greg Clayton1a3083a2010-10-06 03:53:16 +000019
Greg Clayton1a3083a2010-10-06 03:53:16 +000020# Create a new debugger instance
21debugger = 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.
25debugger.SetAsync (False)
26
27# Create a target from a file and arch
Greg Claytond8c62532010-10-07 04:19:01 +000028print "Creating a target for '%s'" % sys.argv[1]
29
30target = debugger.CreateTargetWithFileAndArch (sys.argv[1], lldb.LLDB_ARCH_DEFAULT)
Greg Clayton1a3083a2010-10-06 03:53:16 +000031
Johnny Chen528a9162011-05-25 20:48:29 +000032if target:
Greg Clayton1a3083a2010-10-06 03:53:16 +000033 # If the target is valid set a breakpoint at main
Jim Inghambb737102011-03-30 01:55:23 +000034 main_bp = target.BreakpointCreateByName ("main", target.GetExecutable().GetFilename());
Greg Claytond8c62532010-10-07 04:19:01 +000035
36 print main_bp
37
Greg Clayton1a3083a2010-10-06 03:53:16 +000038 # Launch the process. Since we specified synchronous mode, we won't return
39 # from this function until we hit the breakpoint at main
Johnny Chena6cec392011-05-25 20:56:32 +000040 process = target.LaunchSimple (None, None, os.getcwd())
Greg Clayton1a3083a2010-10-06 03:53:16 +000041
42 # Make sure the launch went ok
Johnny Chen528a9162011-05-25 20:48:29 +000043 if process:
Greg Clayton1a3083a2010-10-06 03:53:16 +000044 # Print some simple process info
Greg Claytond8c62532010-10-07 04:19:01 +000045 state = process.GetState ()
46 print process
47 if state == lldb.eStateStopped:
48 # Get the first thread
49 thread = process.GetThreadAtIndex (0)
Johnny Chen528a9162011-05-25 20:48:29 +000050 if thread:
Greg Claytond8c62532010-10-07 04:19:01 +000051 # Print some simple thread info
52 print thread
53 # Get the first frame
54 frame = thread.GetFrameAtIndex (0)
Johnny Chen528a9162011-05-25 20:48:29 +000055 if frame:
Greg Claytond8c62532010-10-07 04:19:01 +000056 # Print some simple frame info
57 print frame
58 function = frame.GetFunction()
59 # See if we have debug info (a function)
Johnny Chen528a9162011-05-25 20:48:29 +000060 if function:
Greg Claytond8c62532010-10-07 04:19:01 +000061 # 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 Clayton1a3083a2010-10-06 03:53:16 +000065 disassemble_instructions (insts)
Greg Claytond8c62532010-10-07 04:19:01 +000066 else:
67 # See if we have a symbol in the symbol table for where we stopped
68 symbol = frame.GetSymbol();
Johnny Chen528a9162011-05-25 20:48:29 +000069 if symbol:
Greg Claytond8c62532010-10-07 04:19:01 +000070 # 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 Inghambb737102011-03-30 01:55:23 +000075
Jim Inghambb737102011-03-30 01:55:23 +000076 registerList = frame.GetRegisters()
Johnny Chen5949d282011-04-28 23:26:17 +000077 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 Inghambb737102011-03-30 01:55:23 +000083
Greg Claytond8c62532010-10-07 04:19:01 +000084 print "Hit the breakpoint at main, continue and wait for program to exit..."
85 # Now continue to the program exit
86 process.Continue()
87 # When we return from the above function we will hopefully be at the
88 # program exit. Print out some process info
89 print process
90 elif state == lldb.eStateExited:
91 print "Didn't hit the breakpoint at main, program has exited..."
92 else:
93 print "Unexpected process state: %s, killing process..." % debugger.StateAsCString (state)
94 process.Kill()
95
Greg Clayton1a3083a2010-10-06 03:53:16 +000096
97
98lldb.SBDebugger.Terminate()