3

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

My debugger interactions run on a thread that must not block. So when I call thread.StepInstruction(False) I dont care about the next state, I just want to fire the event and later (seconds) look again at the process and detect its state. So before I look at the process again I have to WaitForEvent for every event I fired?
Yes, that's right. That way the event queue and the process state stay in sync.
It seems sad to turn a nice wait loop into polling, but anyway, for polling you want GetNextEvent, which will get an event if there is one, and return an empty event if there isn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.