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