blob: 732cf106b11d94fbfd3a06e373569c7f8e5bf857 [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
Greg Clayton1a3083a2010-10-06 03:53:16 +000014
15def disassemble_instructions (insts):
Johnny Chen5949d282011-04-28 23:26:17 +000016 for i in insts:
17 print i
Greg Clayton1a3083a2010-10-06 03:53:16 +000018
Johnny Chena7ab5902011-05-25 22:29:23 +000019def usage():
20 print "Usage: disasm.py [-n name] executable-image"
21 print " By default, it breaks at and disassembles the 'main' function."
22 sys.exit(0)
23
24if len(sys.argv) == 2:
25 fname = 'main'
26 exe = sys.argv[1]
27elif len(sys.argv) == 4:
28 if sys.argv[1] != '-n':
29 usage()
30 else:
31 fname = sys.argv[2]
32 exe = sys.argv[3]
33else:
34 usage()
35
Greg Clayton1a3083a2010-10-06 03:53:16 +000036# Create a new debugger instance
37debugger = lldb.SBDebugger.Create()
38
39# When we step or continue, don't return from the function until the process
40# stops. We do this by setting the async mode to false.
41debugger.SetAsync (False)
42
43# Create a target from a file and arch
Johnny Chena7ab5902011-05-25 22:29:23 +000044print "Creating a target for '%s'" % exe
Greg Claytond8c62532010-10-07 04:19:01 +000045
Johnny Chena7ab5902011-05-25 22:29:23 +000046target = debugger.CreateTargetWithFileAndArch (exe, lldb.LLDB_ARCH_DEFAULT)
Greg Clayton1a3083a2010-10-06 03:53:16 +000047
Johnny Chen528a9162011-05-25 20:48:29 +000048if target:
Greg Clayton1a3083a2010-10-06 03:53:16 +000049 # If the target is valid set a breakpoint at main
Johnny Chena7ab5902011-05-25 22:29:23 +000050 main_bp = target.BreakpointCreateByName (fname, target.GetExecutable().GetFilename());
Greg Claytond8c62532010-10-07 04:19:01 +000051
52 print main_bp
53
Greg Clayton1a3083a2010-10-06 03:53:16 +000054 # Launch the process. Since we specified synchronous mode, we won't return
55 # from this function until we hit the breakpoint at main
Johnny Chena6cec392011-05-25 20:56:32 +000056 process = target.LaunchSimple (None, None, os.getcwd())
Greg Clayton1a3083a2010-10-06 03:53:16 +000057
58 # Make sure the launch went ok
Johnny Chen528a9162011-05-25 20:48:29 +000059 if process:
Greg Clayton1a3083a2010-10-06 03:53:16 +000060 # Print some simple process info
Greg Claytond8c62532010-10-07 04:19:01 +000061 state = process.GetState ()
62 print process
63 if state == lldb.eStateStopped:
64 # Get the first thread
65 thread = process.GetThreadAtIndex (0)
Johnny Chen528a9162011-05-25 20:48:29 +000066 if thread:
Greg Claytond8c62532010-10-07 04:19:01 +000067 # Print some simple thread info
68 print thread
69 # Get the first frame
70 frame = thread.GetFrameAtIndex (0)
Johnny Chen528a9162011-05-25 20:48:29 +000071 if frame:
Greg Claytond8c62532010-10-07 04:19:01 +000072 # Print some simple frame info
73 print frame
74 function = frame.GetFunction()
75 # See if we have debug info (a function)
Johnny Chen528a9162011-05-25 20:48:29 +000076 if function:
Greg Claytond8c62532010-10-07 04:19:01 +000077 # We do have a function, print some info for the function
78 print function
79 # Now get all instructions for this function and print them
80 insts = function.GetInstructions(target)
Greg Clayton1a3083a2010-10-06 03:53:16 +000081 disassemble_instructions (insts)
Greg Claytond8c62532010-10-07 04:19:01 +000082 else:
83 # See if we have a symbol in the symbol table for where we stopped
84 symbol = frame.GetSymbol();
Johnny Chen528a9162011-05-25 20:48:29 +000085 if symbol:
Greg Claytond8c62532010-10-07 04:19:01 +000086 # We do have a symbol, print some info for the symbol
87 print symbol
88 # Now get all instructions for this symbol and print them
89 insts = symbol.GetInstructions(target)
90 disassemble_instructions (insts)
Jim Inghambb737102011-03-30 01:55:23 +000091
Jim Inghambb737102011-03-30 01:55:23 +000092 registerList = frame.GetRegisters()
Johnny Chen5949d282011-04-28 23:26:17 +000093 print "Frame registers (size of register set = %d):" % registerList.GetSize()
94 for value in registerList:
95 #print value
96 print "%s (number of children = %d):" % (value.GetName(), value.GetNumChildren())
97 for child in value:
Johnny Chene3a83d22011-07-15 00:27:47 +000098 print "Name: ", child.GetName(), " Value: ", child.GetValue()
Jim Inghambb737102011-03-30 01:55:23 +000099
Johnny Chend807c512011-05-25 22:01:16 +0000100 print "Hit the breakpoint at main, enter to continue and wait for program to exit or 'Ctrl-D'/'quit' to terminate the program"
101 next = sys.stdin.readline()
102 if not next or next.rstrip('\n') == 'quit':
103 print "Terminating the inferior process..."
104 process.Kill()
105 else:
106 # Now continue to the program exit
107 process.Continue()
108 # When we return from the above function we will hopefully be at the
109 # program exit. Print out some process info
110 print process
Greg Claytond8c62532010-10-07 04:19:01 +0000111 elif state == lldb.eStateExited:
112 print "Didn't hit the breakpoint at main, program has exited..."
113 else:
114 print "Unexpected process state: %s, killing process..." % debugger.StateAsCString (state)
115 process.Kill()
116
Greg Clayton1a3083a2010-10-06 03:53:16 +0000117
118
119lldb.SBDebugger.Terminate()