LLDB Plugin: Listen for SBEvents when SBTarget is created from Xcode

Forgive me if this was asked in the past. I did some combing through the archives with a "site:lists.llvm.org/pipermail/lldb-dev/ plugininitialize | Xcode | sbtarget” google query, but didn’t find anything relevant

My ultimate goal is to build a helper tool which compliments Xcode when debugging something.

To do this: I am using the C++ bool lldb::PluginInitialize(lldb::SBDebugger dbg) function call to initialize my plugin within LLDB’s process. From there, I am spawning a mach XPC anonymous service to coordinate talking with my LLDB plugin and a macOS GUI application. This code is working great!

My problem is that at the time of my code being initialized (via Xcode calling into it via XPC), the SBDebugger is valid, but no SBTarget is valid (even the dummy one). This means I can not listen for events when the process starts up (i.e. SBProcess’s eBroadCastBitStateChanged), nor monitor for any events being from the SBTarget.

Since I am running code inside the LLDB.framework, I can of course do some wild and crazy stuff by injecting code to intercept when an SBTarget is being created, but I’d really rather avoid that in case you change the lldb_private APIs around. So my question is: do you have a recommended way to be notified via code in a LLDB plugin when a target is initialized through Xcode?

PS. I’ve read through the examples where a target is created directly via the SB APIs, but I can’t use those when Xcode is responsible for launching the SBTarget and SBProcess.

Thanks! Y’all are awesome

Did you try getting the broadcaster class from SBTarget (SBTarget::GetBroadcasterClassName) and then in your Listener call StartListeningForEventClass?

When you listen to an Event Class you get signed up for events for each new object of that class as it gets created.

Jim

Here is example code showing what Jim was talking about:

auto listener = debugger.GetListener();
const uint32_t target_event_mask = lldb::SBTarget::eBroadcastBitModulesLoaded | lldb::SBTarget::eBroadcastBitModulesUnloaded | ...;
const uint32_t process_event_mask = lldb::SBProcess::eBroadcastBitStateChanged | ...;
const uint32_t target_mask = listener.StartListeningForEventClass(debugger, lldb::SBTarget::GetBroadcasterClassName(), target_event_mask);
const uint32_t process_mask = listener.StartListeningForEventClass(debugger, lldb::SBProcess::GetBroadcasterClassName(), process_event_mask);

Fill in an extra event mask bits you want where you see "..." and you should be good to go. Note that the return value from SBListener::StartListeningForEventClass(...) will return the actual mask you were able to grab. Some event bits can only be listened to by a single source and if someone else is already listening to this bit, you might not be able to listen to all of the bits you request. So check "target_event_mask" against "target_mask" and "process_event_mask" against "process_mask".

Greg

Here is example code showing what Jim was talking about:

auto listener = debugger.GetListener();
const uint32_t target_event_mask = lldb::SBTarget::eBroadcastBitModulesLoaded | lldb::SBTarget::eBroadcastBitModulesUnloaded | ...;
const uint32_t process_event_mask = lldb::SBProcess::eBroadcastBitStateChanged | ...;
const uint32_t target_mask = listener.StartListeningForEventClass(debugger, lldb::SBTarget::GetBroadcasterClassName(), target_event_mask);
const uint32_t process_mask = listener.StartListeningForEventClass(debugger, lldb::SBProcess::GetBroadcasterClassName(), process_event_mask);

Fill in an extra event mask bits you want where you see "..." and you should be good to go. Note that the return value from SBListener::StartListeningForEventClass(...) will return the actual mask you were able to grab. Some event bits can only be listened to by a single source and if someone else is already listening to this bit, you might not be able to listen to all of the bits you request. So check "target_event_mask" against "target_mask" and "process_event_mask" against "process_mask".

Most particularly, lldb doesn’t currently allow multiple listeners for the SBProcess StateChanged event. If you aren’t the creator of a process, don’t listen for this event or things will go poorly.

Jim