xref: /llvm-project/lldb/examples/python/disasm.py (revision cf4f56c4cbf1e32812830796657a765e796f32fd)
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(0, insts.GetSize()):
18        print insts.GetInstructionAtIndex(i)
19
20# Initialize LLDB so we can use it
21lldb.SBDebugger.Initialize()
22
23# Create a new debugger instance
24debugger = lldb.SBDebugger.Create()
25
26# When we step or continue, don't return from the function until the process
27# stops. We do this by setting the async mode to false.
28debugger.SetAsync (False)
29
30# Create a target from a file and arch
31target = debugger.CreateTargetWithFileAndArch (sys.argv[1], "x86_64")
32
33if target.IsValid():
34    # If the target is valid set a breakpoint at main
35    main_bp = target.BreakpointCreateByName ("main", sys.argv[1]);
36
37    # Launch the process. Since we specified synchronous mode, we won't return
38    # from this function until we hit the breakpoint at main
39    process = target.LaunchProcess (sys.argv[2:], [''], "/dev/stdout", 0, False)
40
41    # Make sure the launch went ok
42    if process.IsValid():
43        # Print some simple process info
44        print "process:", process, "\n"
45        # Get the first thread
46        thread = process.GetThreadAtIndex (0)
47        if thread.IsValid():
48            # Print some simple thread info
49            print "thread: ", thread
50            # Get the first frame
51            frame = thread.GetFrameAtIndex (0)
52            if frame.IsValid():
53                # Print some simple frame info
54                print "frame: ", frame
55                function = frame.GetFunction()
56                # See if we have debug info (a function)
57                if function.IsValid():
58                    # We do have a function, print some info for the function
59                    print "function: ", function, "\n"
60                    # Now get all instructions for this function and print them
61                    insts = function.GetInstructions(target)
62                    disassemble_instructions (insts)
63                else:
64                    # See if we have a symbol in the symbol table for where we stopped
65                    symbol = frame.GetSymbol();
66                    if symbol.IsValid():
67                        # We do have a symbol, print some info for the symbol
68                        print "symbol: ", symbol, "\n"
69                        # Now get all instructions for this symbol and print them
70                        insts = symbol.GetInstructions(target)
71                        disassemble_instructions (insts)
72        # Now continue to the program exit
73        process.Continue()
74        # When we return from the above function we will hopefully be at the
75        # program exit. Print out some process info
76        print "process:", process, "\n"
77
78
79lldb.SBDebugger.Terminate()
80