简体   繁体   中英

LLDB Python/C++ bindings: Async Step instructions

I am trying to step through a thread. This works while I use debugger.SetAsync(False) , but I want to do this asynchronously. Here is a script to reproduce it. It steps when setting debugger.SetAsync (False) instead of True . I added time.sleep so that it has time to execute my instructions. I expect the next instruction in the frame.pc

import time
import sys
lldb_path = "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python"
sys.path = sys.path + [lldb_path]

import lldb
import os
exe = "./a.out"    
debugger = lldb.SBDebugger.Create()

debugger.SetAsync (True) # change this to False, to make it work


target = debugger.CreateTargetWithFileAndArch (exe, lldb.LLDB_ARCH_DEFAULT)

if target:
    main_bp = target.BreakpointCreateByName ("main", target.GetExecutable().GetFilename()) 
    print main_bp

    launch_info = lldb.SBLaunchInfo(None)
    launch_info.SetExecutableFile (lldb.SBFileSpec(exe), True)
    error = lldb.SBError()
    process = target.Launch (launch_info, error)
    time.sleep(1)
    # Make sure the launch went ok
    if process:
        # Print some simple process info
        state = process.GetState ()
        print 'process state'
        print state
        thread = process.GetThreadAtIndex(0)
        frame = thread.GetFrameAtIndex(0)
        print 'stop loc'
        print hex(frame.pc)
        print 'thread stop reason'
        print thread.stop_reason

        print 'stepping'
        thread.StepInstruction(False)

        time.sleep(1)

        print 'process state'
        print process.GetState ()

        print 'thread stop reason'
        print thread.stop_reason
        frame = thread.GetFrameAtIndex(0)
        print 'stop loc'
        print hex(frame.pc)  # invalid output?

Version: lldb-340.4.110 (Provided with Xcode)
Python: Python 2.7.10
Os: Mac Yosemite

The "async" version of the lldb API's uses an event based system. You can't wait for things to happen using sleep's - but rather using the WaitForEvent API's lldb provides. An example of how to do this is given at:

https://github.com/llvm/llvm-project/blob/main/lldb/examples/python/process_events.py

There's a bunch of stuff at the beginning of the example that shows how to load the lldb module and does argument parsing. The part you want to look at is the loop:

        listener = debugger.GetListener()
        # sign up for process state change events
        stop_idx = 0
        done = False
        while not done:
            event = lldb.SBEvent()
            if listener.WaitForEvent (options.event_timeout, event):

and below.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM