Hi all, first post here. I’m writing a plugin for lldb using the python API, and I’m having a hard time understanding how to listen to events. I’m using lldb 14.0.0.
My goal is to be able to execute custom code when :
- a breakpoint it hit (so I can tell my plugin to update its data representation)
- the process is exited (so I can tell my plugin’s GUI process to stop)
- lldb exits (so I can tell my plugin’s GUI process to stop)
I was able to use a stop-hook for the breakpoint case, but I’m struggling to make it work for process and lldb exit.
Here is the code I tried to use :
import lldb
import threading
class LLDBEventHandler:
def __init__(self, debugger: lldb.SBDebugger):
self.__debugger = debugger
self.__listener = lldb.SBListener("dave_process_listener")
self.attached = False
# Attach to quit command
self.__debugger.GetCommandInterpreter().GetBroadcaster().AddListener(
self.__listener,
lldb.SBCommandInterpreter.eBroadcastBitQuitCommandReceived,
)
# Start listening thread
self.__thread = threading.Thread(target=self.__event_loop, daemon=True)
self.__thread.start()
def __try_to_attach_to_process(self):
process = (
self.__debugger.GetSelectedTarget().GetProcess()
) # type: lldb.SBProcess
if process.is_alive:
process.broadcaster.AddListener(
self.__listener,
lldb.SBProcess.eBroadcastBitStateChanged
| lldb.SBProcess.eBroadcastBitInterrupt,
)
self.attached = True
print("Successfully attached")
def __event_loop(self):
while True:
if not self.attached:
self.__try_to_attach_to_process()
event = lldb.SBEvent()
if self.__listener.WaitForEvent(1, event):
print("New event")
if lldb.SBProcess.EventIsProcessEvent(event):
print("process event")
if lldb.SBProcess.GetStateFromEvent(event) == lldb.eStateExited:
print("process exit")
# Do some stuff
elif lldb.SBCommandInterpreter.EventIsCommandInterpreterEvent(event):
if event.GetType() == lldb.SBCommandInterpreter.eBroadcastBitQuitCommandReceived :
print("debugger exit")
# Do some stuff
def __lldb_init_module(debugger: lldb.SBDebugger, internal_dict):
event_handler = LLDBEventHandler(debugger)
But I have several issues :
- When starting lldb, a
eBroadcastBitQuitCommandReceived
is immediately detected even though I did not typed any command - When explicitely typing the
quit
command, theeBroadcastBitQuitCommandReceived
event is detected twice
Is the behaviour expected ? How should I deal with these issues ?
Thanks a lot for your help and the great tools. Don’t hesitate to ask if you need more information from me.
Best regards