@@ -3137,7 +3137,7 @@ void Target::SetAllStopHooksActiveState(bool active_state) {
31373137 }
31383138}
31393139
3140- bool Target::RunStopHooks () {
3140+ bool Target::RunStopHooks (bool at_initial_stop ) {
31413141 if (m_suppress_stop_hooks)
31423142 return false ;
31433143
@@ -3146,14 +3146,19 @@ bool Target::RunStopHooks() {
31463146
31473147 // Somebody might have restarted the process:
31483148 // Still return false, the return value is about US restarting the target.
3149- if (m_process_sp->GetState () != eStateStopped)
3149+ lldb::StateType state = m_process_sp->GetState ();
3150+ if (!(state == eStateStopped || state == eStateAttaching))
31503151 return false ;
31513152
31523153 if (m_stop_hooks.empty ())
31533154 return false ;
31543155
31553156 bool no_active_hooks =
3156- llvm::none_of (m_stop_hooks, [](auto &p) { return p.second ->IsActive (); });
3157+ llvm::none_of (m_stop_hooks, [at_initial_stop](auto &p) {
3158+ bool should_run_now =
3159+ !at_initial_stop || p.second ->GetRunAtInitialStop ();
3160+ return p.second ->IsActive () && should_run_now;
3161+ });
31573162 if (no_active_hooks)
31583163 return false ;
31593164
@@ -3183,9 +3188,22 @@ bool Target::RunStopHooks() {
31833188 }
31843189
31853190 // If no threads stopped for a reason, don't run the stop-hooks.
3191+ // However, if this is the FIRST stop for this process, then we are in the
3192+ // state where an attach or a core file load was completed without designating
3193+ // a particular thread as responsible for the stop. In that case, we do
3194+ // want to run the stop hooks, but do so just on one thread.
31863195 size_t num_exe_ctx = exc_ctx_with_reasons.size ();
3187- if (num_exe_ctx == 0 )
3188- return false ;
3196+ if (num_exe_ctx == 0 ) {
3197+ if (at_initial_stop && num_threads > 0 ) {
3198+ lldb::ThreadSP thread_to_use_sp = cur_threadlist.GetThreadAtIndex (0 );
3199+ exc_ctx_with_reasons.emplace_back (
3200+ m_process_sp.get (), thread_to_use_sp.get (),
3201+ thread_to_use_sp->GetStackFrameAtIndex (0 ).get ());
3202+ num_exe_ctx = 1 ;
3203+ } else {
3204+ return false ;
3205+ }
3206+ }
31893207
31903208 StreamSP output_sp = m_debugger.GetAsyncOutputStream ();
31913209 auto on_exit = llvm::make_scope_exit ([output_sp] { output_sp->Flush (); });
@@ -3199,6 +3217,8 @@ bool Target::RunStopHooks() {
31993217 StopHookSP cur_hook_sp = stop_entry.second ;
32003218 if (!cur_hook_sp->IsActive ())
32013219 continue ;
3220+ if (at_initial_stop && !cur_hook_sp->GetRunAtInitialStop ())
3221+ continue ;
32023222
32033223 bool any_thread_matched = false ;
32043224 for (auto exc_ctx : exc_ctx_with_reasons) {
@@ -3525,10 +3545,14 @@ Status Target::Launch(ProcessLaunchInfo &launch_info, Stream *stream) {
35253545 m_process_sp->RestoreProcessEvents ();
35263546
35273547 if (rebroadcast_first_stop) {
3548+ // We don't need to run the stop hooks by hand here, they will get
3549+ // triggered when this rebroadcast event gets fetched.
35283550 assert (first_stop_event_sp);
35293551 m_process_sp->BroadcastEvent (first_stop_event_sp);
35303552 return error;
35313553 }
3554+ // Run the stop hooks that want to run at entry.
3555+ RunStopHooks (true /* at entry point */ );
35323556
35333557 switch (state) {
35343558 case eStateStopped: {
@@ -3681,6 +3705,10 @@ Status Target::Attach(ProcessAttachInfo &attach_info, Stream *stream) {
36813705 true , SelectMostRelevantFrame);
36823706 process_sp->RestoreProcessEvents ();
36833707
3708+ // Run the stop hooks here. Since we were hijacking the events, they
3709+ // wouldn't have gotten run as part of event delivery.
3710+ RunStopHooks (/* at_initial_stop= */ true );
3711+
36843712 if (state != eStateStopped) {
36853713 const char *exit_desc = process_sp->GetExitDescription ();
36863714 if (exit_desc)
0 commit comments