11a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton#!/usr/bin/python 21a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton 31a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton#---------------------------------------------------------------------- 41a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton# Be sure to add the python path that points to the LLDB shared library. 51a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton# On MacOSX csh, tcsh: 61a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton# setenv PYTHONPATH /Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python 71a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton# On MacOSX sh, bash: 81a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton# export PYTHONPATH=/Developer/Library/PrivateFrameworks/LLDB.framework/Resources/Python 91a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton#---------------------------------------------------------------------- 101a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton 111a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Claytonimport lldb 121a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Claytonimport os 131a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Claytonimport sys 141a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton 151a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Claytondef disassemble_instructions (insts): 165949d280a934a839b957a5233b8bdb322ac7071cJohnny Chen for i in insts: 175949d280a934a839b957a5233b8bdb322ac7071cJohnny Chen print i 181a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton 19a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chendef usage(): 20a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen print "Usage: disasm.py [-n name] executable-image" 21a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen print " By default, it breaks at and disassembles the 'main' function." 22a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen sys.exit(0) 23a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen 24a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chenif len(sys.argv) == 2: 25a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen fname = 'main' 26a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen exe = sys.argv[1] 27a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chenelif len(sys.argv) == 4: 28a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen if sys.argv[1] != '-n': 29a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen usage() 30a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen else: 31a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen fname = sys.argv[2] 32a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen exe = sys.argv[3] 33a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chenelse: 34a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen usage() 35a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen 361a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton# Create a new debugger instance 371a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Claytondebugger = lldb.SBDebugger.Create() 381a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton 391a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton# When we step or continue, don't return from the function until the process 401a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton# stops. We do this by setting the async mode to false. 411a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Claytondebugger.SetAsync (False) 421a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton 431a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton# Create a target from a file and arch 44a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chenprint "Creating a target for '%s'" % exe 45d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton 46a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chentarget = debugger.CreateTargetWithFileAndArch (exe, lldb.LLDB_ARCH_DEFAULT) 471a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton 48528a9165d5863dea6b91266ab42f607c1c1b5c7dJohnny Chenif target: 491a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton # If the target is valid set a breakpoint at main 50a7ab5900ceefe329d773fa353313ff311517c2c3Johnny Chen main_bp = target.BreakpointCreateByName (fname, target.GetExecutable().GetFilename()); 51d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton 52d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton print main_bp 53d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton 541a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton # Launch the process. Since we specified synchronous mode, we won't return 551a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton # from this function until we hit the breakpoint at main 56a6cec39633b8548967391b4fabdf2066937e3413Johnny Chen process = target.LaunchSimple (None, None, os.getcwd()) 571a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton 581a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton # Make sure the launch went ok 59528a9165d5863dea6b91266ab42f607c1c1b5c7dJohnny Chen if process: 601a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton # Print some simple process info 61d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton state = process.GetState () 62d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton print process 63d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton if state == lldb.eStateStopped: 64d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton # Get the first thread 65d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton thread = process.GetThreadAtIndex (0) 66528a9165d5863dea6b91266ab42f607c1c1b5c7dJohnny Chen if thread: 67d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton # Print some simple thread info 68d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton print thread 69d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton # Get the first frame 70d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton frame = thread.GetFrameAtIndex (0) 71528a9165d5863dea6b91266ab42f607c1c1b5c7dJohnny Chen if frame: 72d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton # Print some simple frame info 73d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton print frame 74d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton function = frame.GetFunction() 75d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton # See if we have debug info (a function) 76528a9165d5863dea6b91266ab42f607c1c1b5c7dJohnny Chen if function: 77d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton # We do have a function, print some info for the function 78d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton print function 79d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton # Now get all instructions for this function and print them 80d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton insts = function.GetInstructions(target) 811a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton disassemble_instructions (insts) 82d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton else: 83d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton # See if we have a symbol in the symbol table for where we stopped 84d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton symbol = frame.GetSymbol(); 85528a9165d5863dea6b91266ab42f607c1c1b5c7dJohnny Chen if symbol: 86d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton # We do have a symbol, print some info for the symbol 87d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton print symbol 88d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton # Now get all instructions for this symbol and print them 89d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton insts = symbol.GetInstructions(target) 90d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton disassemble_instructions (insts) 91bb737104d66027385d9b0bc008402ae63578b41fJim Ingham 92bb737104d66027385d9b0bc008402ae63578b41fJim Ingham registerList = frame.GetRegisters() 935949d280a934a839b957a5233b8bdb322ac7071cJohnny Chen print "Frame registers (size of register set = %d):" % registerList.GetSize() 945949d280a934a839b957a5233b8bdb322ac7071cJohnny Chen for value in registerList: 955949d280a934a839b957a5233b8bdb322ac7071cJohnny Chen #print value 965949d280a934a839b957a5233b8bdb322ac7071cJohnny Chen print "%s (number of children = %d):" % (value.GetName(), value.GetNumChildren()) 975949d280a934a839b957a5233b8bdb322ac7071cJohnny Chen for child in value: 98e3a83d21d6f4fd813446fc7471c739507bbe11b5Johnny Chen print "Name: ", child.GetName(), " Value: ", child.GetValue() 99bb737104d66027385d9b0bc008402ae63578b41fJim Ingham 100d807c5145f1d9a650d58d0c86cde8d0b47b77a9dJohnny Chen print "Hit the breakpoint at main, enter to continue and wait for program to exit or 'Ctrl-D'/'quit' to terminate the program" 101d807c5145f1d9a650d58d0c86cde8d0b47b77a9dJohnny Chen next = sys.stdin.readline() 102d807c5145f1d9a650d58d0c86cde8d0b47b77a9dJohnny Chen if not next or next.rstrip('\n') == 'quit': 103d807c5145f1d9a650d58d0c86cde8d0b47b77a9dJohnny Chen print "Terminating the inferior process..." 104d807c5145f1d9a650d58d0c86cde8d0b47b77a9dJohnny Chen process.Kill() 105d807c5145f1d9a650d58d0c86cde8d0b47b77a9dJohnny Chen else: 106d807c5145f1d9a650d58d0c86cde8d0b47b77a9dJohnny Chen # Now continue to the program exit 107d807c5145f1d9a650d58d0c86cde8d0b47b77a9dJohnny Chen process.Continue() 108d807c5145f1d9a650d58d0c86cde8d0b47b77a9dJohnny Chen # When we return from the above function we will hopefully be at the 109d807c5145f1d9a650d58d0c86cde8d0b47b77a9dJohnny Chen # program exit. Print out some process info 110d807c5145f1d9a650d58d0c86cde8d0b47b77a9dJohnny Chen print process 111d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton elif state == lldb.eStateExited: 112d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton print "Didn't hit the breakpoint at main, program has exited..." 113d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton else: 114d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton print "Unexpected process state: %s, killing process..." % debugger.StateAsCString (state) 115d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton process.Kill() 116d8c625380b56759fc3fef8b9cf0389ae1a07f44dGreg Clayton 1171a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton 1181a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Clayton 1191a3083a04c20cc8e7298e03b731cab5d09aa7badGreg Claytonlldb.SBDebugger.Terminate() 120