defdisassemble_instructions(insts): for i in insts: print i
# Set the path to the executable to debug exe = "./a.out"
# Create a new debugger instance debugger = lldb.SBDebugger.Create()
# When we step or continue, don't return from the function until the process # stops. Otherwise we would have to handle the process events ourselves which, while doable is #a little tricky. We do this by setting the async mode to false. debugger.SetAsync (False)
# Create a target from a file and arch print"Creating a target for '%s'" % exe
if target: # If the target is valid set a breakpoint at main main_bp = target.BreakpointCreateByName ("main", target.GetExecutable().GetFilename());
print main_bp
# Launch the process. Since we specified synchronous mode, we won't return # from this function until we hit the breakpoint at main process = target.LaunchSimple (None, None, os.getcwd()) # Make sure the launch went ok if process: # Print some simple process info state = process.GetState () print process if state == lldb.eStateStopped: # Get the first thread thread = process.GetThreadAtIndex (0) if thread: # Print some simple thread info print thread # Get the first frame frame = thread.GetFrameAtIndex (0) if frame: # Print some simple frame info print frame function = frame.GetFunction() # See if we have debug info (a function) if function: # We do have a function, print some info for the function print function # Now get all instructions for this function and print them insts = function.GetInstructions(target) disassemble_instructions (insts) else: # See if we have a symbol in the symbol table for where we stopped symbol = frame.GetSymbol(); if symbol: # We do have a symbol, print some info for the symbol print symbol
可以看出这些主要类的相互调用关系
回调函数
在lldb的command中的可以实现回调,比如当某个断点命中时自动执行函数
1 2 3 4 5 6 7 8 9 10 11
(lldb) breakpoint set --func-regex <regular-expression> (lldb) breakpoint command add -s python 1 Enter your Python command(s). Type 'DONE' to end. def function (frame, bp_loc, internal_dict): """frame: the lldb.SBFrame for the location at which you stopped bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information internal_dict: an LLDB support object not to be used""" name = frame.GetFunctionName() print"function name: %s" % name return False DONE
Process 2447 launched: '/Users/penguin/Test/a.out' (x86_64) function name: func100 function name: c100 9Process 2447 exited with status = 0 (0x00000000)
defexplains_stop(self, event): # We are stepping, so if we stop for any other reason, it isn't # because of us. print'explains_stop' ifself.thread_plan.GetThread().GetStopReason() == lldb.eStopReasonTrace: print'true' returnTrue else: returnFalse
defcommand_function(debugger, command, result, internal_dict): """This command takes a lot of options and does many fancy things""" # Your code goes here
第一个参数是debugger实例,第二个参数是命令参数,
第三个参数为__lldb.SBCommandReturnObjec__类型,包含命令执行结果信息
最后是嵌入的脚本的集合
也可以使用Python类实现命令添加
1 2 3 4 5 6 7 8 9
classCommandObjectType: def__init__(self, debugger, session_dict): this call should initialize the command with respect to the command interpreter for the passed-in debugger def__call__(self, debugger, command, exe_ctx, result): this is the actual bulk of the command, akin to Python command functions defget_short_help(self): this call should return the short help text for this command[1] defget_long_help(self): this call should return the long help text for this command[1]
对于一个Python脚本,可以通过定义
1 2
def__lldb_init_module(debugger, internal_dict): # Command Initialization code goes here
# And the initialization code to add your commands def__lldb_init_module(debugger, internal_dict): debugger.HandleCommand('command script add -f script_step.ls ls') print'The "ls" python command has been installed and is ready for use.'
如上述脚本,执行命令
1
(lldb) command script import ~/script_step.py
就可以直接在LLDB中使用命令ls
1 2 3 4 5 6
(lldb) ls / Applications Library Network System ...