@@ -3027,7 +3027,7 @@ void Target::SetAllStopHooksActiveState(bool active_state) {
30273027 }
30283028}
30293029
3030- bool Target::RunStopHooks () {
3030+ bool Target::RunStopHooks (bool at_initial_stop ) {
30313031 if (m_suppress_stop_hooks)
30323032 return false ;
30333033
@@ -3036,21 +3036,20 @@ bool Target::RunStopHooks() {
30363036
30373037 // Somebody might have restarted the process:
30383038 // Still return false, the return value is about US restarting the target.
3039- if (m_process_sp->GetState () != eStateStopped)
3039+ lldb::StateType state = m_process_sp->GetState ();
3040+ if (!(state == eStateStopped || state == eStateAttaching))
30403041 return false ;
30413042
30423043 if (m_stop_hooks.empty ())
30433044 return false ;
30443045
3045- // If there aren't any active stop hooks, don't bother either.
3046- bool any_active_hooks = false ;
3047- for (auto hook : m_stop_hooks) {
3048- if (hook.second ->IsActive ()) {
3049- any_active_hooks = true ;
3050- break ;
3051- }
3052- }
3053- if (!any_active_hooks)
3046+ bool no_active_hooks =
3047+ llvm::none_of (m_stop_hooks, [at_initial_stop](auto &p) {
3048+ bool should_run_now =
3049+ !at_initial_stop || p.second ->GetRunAtInitialStop ();
3050+ return p.second ->IsActive () && should_run_now;
3051+ });
3052+ if (no_active_hooks)
30543053 return false ;
30553054
30563055 // Make sure we check that we are not stopped because of us running a user
@@ -3079,9 +3078,22 @@ bool Target::RunStopHooks() {
30793078 }
30803079
30813080 // If no threads stopped for a reason, don't run the stop-hooks.
3081+ // However, if this is the FIRST stop for this process, then we are in the
3082+ // state where an attach or a core file load was completed without designating
3083+ // a particular thread as responsible for the stop. In that case, we do
3084+ // want to run the stop hooks, but do so just on one thread.
30823085 size_t num_exe_ctx = exc_ctx_with_reasons.size ();
3083- if (num_exe_ctx == 0 )
3084- return false ;
3086+ if (num_exe_ctx == 0 ) {
3087+ if (at_initial_stop && num_threads > 0 ) {
3088+ lldb::ThreadSP thread_to_use_sp = cur_threadlist.GetThreadAtIndex (0 );
3089+ exc_ctx_with_reasons.emplace_back (
3090+ m_process_sp.get (), thread_to_use_sp.get (),
3091+ thread_to_use_sp->GetStackFrameAtIndex (0 ).get ());
3092+ num_exe_ctx = 1 ;
3093+ } else {
3094+ return false ;
3095+ }
3096+ }
30853097
30863098 StreamSP output_sp = m_debugger.GetAsyncOutputStream ();
30873099
@@ -3096,6 +3108,8 @@ bool Target::RunStopHooks() {
30963108 StopHookSP cur_hook_sp = stop_entry.second ;
30973109 if (!cur_hook_sp->IsActive ())
30983110 continue ;
3111+ if (at_initial_stop && !cur_hook_sp->GetRunAtInitialStop ())
3112+ continue ;
30993113
31003114 bool any_thread_matched = false ;
31013115 for (auto exc_ctx : exc_ctx_with_reasons) {
@@ -3472,10 +3486,14 @@ Status Target::Launch(ProcessLaunchInfo &launch_info, Stream *stream) {
34723486 m_process_sp->RestoreProcessEvents ();
34733487
34743488 if (rebroadcast_first_stop) {
3489+ // We don't need to run the stop hooks by hand here, they will get
3490+ // triggered when this rebroadcast event gets fetched.
34753491 assert (first_stop_event_sp);
34763492 m_process_sp->BroadcastEvent (first_stop_event_sp);
34773493 return error;
34783494 }
3495+ // Run the stop hooks that want to run at entry.
3496+ RunStopHooks (true /* at entry point */ );
34793497
34803498 switch (state) {
34813499 case eStateStopped: {
@@ -3628,6 +3646,10 @@ Status Target::Attach(ProcessAttachInfo &attach_info, Stream *stream) {
36283646 true , SelectMostRelevantFrame);
36293647 process_sp->RestoreProcessEvents ();
36303648
3649+ // Run the stop hooks here. Since we were hijacking the events, they
3650+ // wouldn't have gotten run as part of event delivery.
3651+ RunStopHooks (/* at_initial_stop= */ true );
3652+
36313653 if (state != eStateStopped) {
36323654 const char *exit_desc = process_sp->GetExitDescription ();
36333655 if (exit_desc)
0 commit comments