1#!/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(insts.GetSize()):
18        print insts.GetInstructionAtIndex(i)
19
20# 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
28print "Creating a target for '%s'" % sys.argv[1]
29
30target = debugger.CreateTargetWithFileAndArch (sys.argv[1], lldb.LLDB_ARCH_DEFAULT)
31
32if target.IsValid():
33    # If the target is valid set a breakpoint at main
34    main_bp = target.BreakpointCreateByName ("main", sys.argv[1]);
35
36    print main_bp
37
38    # Launch the process. Since we specified synchronous mode, we won't return
39    # from this function until we hit the breakpoint at main
40    process = target.LaunchProcess ([''], [''], "/dev/stdout", 0, False)
41
42    # Make sure the launch went ok
43    if process.IsValid():
44        # Print some simple process info
45        state = process.GetState ()
46        print process
47        if state == lldb.eStateStopped:
48            # Get the first thread
49            thread = process.GetThreadAtIndex (0)
50            if thread.IsValid():
51                # Print some simple thread info
52                print thread
53                # Get the first frame
54                frame = thread.GetFrameAtIndex (0)
55                if frame.IsValid():
56                    # Print some simple frame info
57                    print frame
58                    function = frame.GetFunction()
59                    # See if we have debug info (a function)
60                    if function.IsValid():
61                        # 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)
65                        disassemble_instructions (insts)
66                    else:
67                        # See if we have a symbol in the symbol table for where we stopped
68                        symbol = frame.GetSymbol();
69                        if symbol.IsValid():
70                            # 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)
75            print "Hit the breakpoint at main, continue and wait for program to exit..."
76            # Now continue to the program exit
77            process.Continue()
78            # When we return from the above function we will hopefully be at the
79            # program exit. Print out some process info
80            print process
81        elif state == lldb.eStateExited:
82            print "Didn't hit the breakpoint at main, program has exited..."
83        else:
84            print "Unexpected process state: %s, killing process..." % debugger.StateAsCString (state)
85            process.Kill()
86
87
88
89lldb.SBDebugger.Terminate()
90