[lldb] Keep a stop private until it has been handled #234789
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
This is a tentative fix and the implementation is deliberately quite verbose as this is a complex problem to understand in the first place and solve.
DoOnRemoval runs several times in the life-cycle of an event. In simplistic terms, it always runs on the thread that pulls the event from the event queue. But that happens a couple of times with different contexts.
The stopped event is originally reported by the gdb-remote-async thread, and pulled from there as a "private event" by a listener on the Private State Thread. DoOnRemoval is first run in that "private" mode where only synchronous callbacks are run.
Then after running the event through the thread plan machinery, if the event will be broadcast publicly, it is made available to the Process Primary Listener, which pulls it off on whatever thread it is using for Event Handling. DoOnRemoval gets run AGAIN when the Primary Process Listener calls WaitForEvent, which is when the asynchronous callbacks are run.
Then DoOnRemoval (now a no-op) gets run again as any other secondary Process Listeners call WaitForEvent.
The complexity with the Private state thread is that sometimes it needs to run expressions while processing a stop, and it can't run itself as private state thread to do that, so it makes a "secondary private state thread" where the current PST plays the role of the async Event Handling Thread, and the secondary PST becomes the one to fetch events from the gdb-remote async thread.
This does not seem like it should be a public method. What would clients of this process do with this information?
What is this one for?
Should it take the Target API mutex? If I'm in synchronous mode, a call to "SBProcess::Continue" will still hold the Target API mutex, but the callback is very likely to also call API's that take the Target API mutex as well. The thread handling the async callback DoOnRemoval needs a waiver on the Target API mutex, I think.
This seems the wrong place to do this. Process::Halt should handle ensuring that the process gets done with (or interrupts) the DoOnRemoval handling and then either halts again (if DoOnRemoval restarted the process) or reports the halt state. There are other places that also want to Halt, and none of them should have to know about the details of the DoOnRemoval handling.
If I understand it correctly, I think this comment has the logic flipped:
The standard lldb driver program runs one Event Handling Thread that waits on all the Process Broadcasters. In that mode of using lldb, all the asynchronous DoOnRemoval calls happen on the same thread. So the thread managing events from different processes all have the same m_owner. This check wouldn't help anything, it would always pass. That's not a problem because in that scenario DoOnRemoval is always called ON the event handling thread which blocks that thread from fetching other process events - serializing the StopContext access.
I think this check would matter if I had the opposite use pattern, where there were several independent Event Handling Threads, one per process. Then you might be in the middle of running a DoOnRemoval on the EHT for process A on thread A, and Thread B might try to ask a question of process A. Thread B is not the one that pushed the StopHandling context, so it should see the old (running) state not the currently being handled state.
I don't understand when this would happen. When would "two threads pull this process's events from its primary listener"? Do you mean you have two threads each of which is calling "WaitForEvent" on the SAME listener. That seems to me clearly racy and not something we are interested in supporting. If we don't explicitly disallow it currently, we should. You can multiplex by having multiple Listeners for the same Broadcaster, but running concurrent WaitForEvent's on the same Listener seems wrong.
Is there a scenario that motivated this comment?
Someone said Halt while a stop callback was being run. Since an expression being run might spin forever, Halt needs to be able to interrupt that. I don't think it's right for the Process to just give up here.
BTW, since you return an error here, I don't think you need to check IsHandlingStopOnOtherThread in CommandInterpreter above. Either handle the failure to Halt directly, and/or have Halt actually effect a halt where it's appropriate.
This seems racy. If you don't interrupt or wait till the stop handling on another thread is done, you could have called "DiscardThreadPlans" and then the stop handling callback could push another plan, or set another breakpoint or call break enable after you said DisableAllBreakpoints.
I think you need to work out a way to interrupt the Callback, or to wait on its completion.
jimingham
medismailben
ProcessEventData::DoOnRemoval makes a stop public, by setting the public state to stopped and releasing the public run lock, before it handles the stop by running its StopInfo actions and stop hooks. While those run, any other thread sees a stopped process: it can take the run lock, run expressions, or resume the process underneath them. In an IDE, this is what happens when the user keeps clicking Continue while LLDB is still handling the previous stop.
Hold the stop back until it has been handled. The thread handling it pushes a new Policy::View::HandlingStop and gets a public state and run lock of its own, which read stopped, so it can still run expressions. Every other thread keeps seeing the process running, and the main run lock stays held. Once the stop is handled, it is made public, unless handling it left the process running. So after an auto-continue, the main run lock now stays held while the process runs, where before it was released and never taken again.
Halt now refuses while another thread handles a stop, since the process is already stopped and interrupting it would interrupt the expressions run while handling it. Destroy and Detach treat such a process as stopped instead of trying to halt it, and an interrupt in the command interpreter goes to the script interpreter, as it does when the process is stopped.
Expressions the private state thread runs for a stop before it broadcasts it, such as breakpoint conditions, aren't covered yet: see the FIXME in RunThreadPlan.
Co-authored-by: Jim Ingham <jingham@apple.com>
Assisted-by: Claude