@@ -3038,7 +3038,7 @@ void Target::SetAllStopHooksActiveState(bool active_state) {
30383038 }
30393039}
30403040
3041- bool Target::RunStopHooks () {
3041+ bool Target::RunStopHooks (bool at_initial_stop ) {
30423042 if (m_suppress_stop_hooks)
30433043 return false ;
30443044
@@ -3047,14 +3047,18 @@ bool Target::RunStopHooks() {
30473047
30483048 // Somebody might have restarted the process:
30493049 // Still return false, the return value is about US restarting the target.
3050- if (m_process_sp->GetState () != eStateStopped)
3050+ lldb::StateType state = m_process_sp->GetState ();
3051+ if (!(state == eStateStopped || state == eStateAttaching))
30513052 return false ;
30523053
30533054 if (m_stop_hooks.empty ())
30543055 return false ;
30553056
30563057 bool no_active_hooks =
3057- llvm::none_of (m_stop_hooks, [](auto &p) { return p.second ->IsActive (); });
3058+ llvm::none_of (m_stop_hooks, [at_initial_stop](auto &p) {
3059+ bool should_run_now = !at_initial_stop || p.second ->GetRunAtFirstStop ();
3060+ return p.second ->IsActive () && should_run_now;
3061+ });
30583062 if (no_active_hooks)
30593063 return false ;
30603064
@@ -3084,9 +3088,22 @@ bool Target::RunStopHooks() {
30843088 }
30853089
30863090 // If no threads stopped for a reason, don't run the stop-hooks.
3091+ // However, if this is the FIRST stop for this process, then we are in the
3092+ // state where an attach or a core file load was completed without designating
3093+ // a particular thread as responsible for the stop. In that case, we do
3094+ // want to run the stop hooks, but do so just on one thread.
30873095 size_t num_exe_ctx = exc_ctx_with_reasons.size ();
3088- if (num_exe_ctx == 0 )
3089- return false ;
3096+ if (num_exe_ctx == 0 ) {
3097+ if (at_initial_stop && num_threads > 0 ) {
3098+ lldb::ThreadSP thread_to_use_sp = cur_threadlist.GetThreadAtIndex (0 );
3099+ exc_ctx_with_reasons.emplace_back (m_process_sp.get (),
3100+ thread_to_use_sp.get (),
3101+ thread_to_use_sp->GetStackFrameAtIndex (0 ).get ());
3102+ num_exe_ctx = 1 ;
3103+ } else
3104+ return false ;
3105+ }
3106+
30903107
30913108 StreamSP output_sp = m_debugger.GetAsyncOutputStream ();
30923109 auto on_exit = llvm::make_scope_exit ([output_sp] { output_sp->Flush (); });
@@ -3100,6 +3117,8 @@ bool Target::RunStopHooks() {
31003117 StopHookSP cur_hook_sp = stop_entry.second ;
31013118 if (!cur_hook_sp->IsActive ())
31023119 continue ;
3120+ if (at_initial_stop && !cur_hook_sp->GetRunAtFirstStop ())
3121+ continue ;
31033122
31043123 bool any_thread_matched = false ;
31053124 for (auto exc_ctx : exc_ctx_with_reasons) {
@@ -3426,10 +3445,14 @@ Status Target::Launch(ProcessLaunchInfo &launch_info, Stream *stream) {
34263445 m_process_sp->RestoreProcessEvents ();
34273446
34283447 if (rebroadcast_first_stop) {
3448+ // We don't need to run the stop hooks by hand here, they will get
3449+ // triggered when this rebroadcast event gets fetched.
34293450 assert (first_stop_event_sp);
34303451 m_process_sp->BroadcastEvent (first_stop_event_sp);
34313452 return error;
34323453 }
3454+ // Run the stop hooks that want to run at entry.
3455+ RunStopHooks (true /* at entry point */ );
34333456
34343457 switch (state) {
34353458 case eStateStopped: {
@@ -3581,6 +3604,10 @@ Status Target::Attach(ProcessAttachInfo &attach_info, Stream *stream) {
35813604 std::nullopt , nullptr , false , attach_info.GetHijackListener (), stream,
35823605 true , SelectMostRelevantFrame);
35833606 process_sp->RestoreProcessEvents ();
3607+
3608+ // Run the stop hooks here. Since we were hijacking the events, they
3609+ // wouldn't have gotten run as part of event delivery.
3610+ RunStopHooks (true /* at_initial_stop */ );
35843611
35853612 if (state != eStateStopped) {
35863613 const char *exit_desc = process_sp->GetExitDescription ();
0 commit comments