1 //===-- StopInfo.cpp ------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <string>
10 
11 #include "lldb/Breakpoint/Breakpoint.h"
12 #include "lldb/Breakpoint/BreakpointLocation.h"
13 #include "lldb/Breakpoint/StoppointCallbackContext.h"
14 #include "lldb/Breakpoint/Watchpoint.h"
15 #include "lldb/Core/Debugger.h"
16 #include "lldb/Core/ValueObject.h"
17 #include "lldb/Expression/UserExpression.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Target/StopInfo.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Target/Thread.h"
22 #include "lldb/Target/ThreadPlan.h"
23 #include "lldb/Target/ThreadPlanStepInstruction.h"
24 #include "lldb/Target/UnixSignals.h"
25 #include "lldb/Utility/LLDBLog.h"
26 #include "lldb/Utility/Log.h"
27 #include "lldb/Utility/StreamString.h"
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 StopInfo::StopInfo(Thread &thread, uint64_t value)
33     : m_thread_wp(thread.shared_from_this()),
34       m_stop_id(thread.GetProcess()->GetStopID()),
35       m_resume_id(thread.GetProcess()->GetResumeID()), m_value(value),
36       m_description(), m_override_should_notify(eLazyBoolCalculate),
37       m_override_should_stop(eLazyBoolCalculate), m_extended_info() {}
38 
39 bool StopInfo::IsValid() const {
40   ThreadSP thread_sp(m_thread_wp.lock());
41   if (thread_sp)
42     return thread_sp->GetProcess()->GetStopID() == m_stop_id;
43   return false;
44 }
45 
46 void StopInfo::MakeStopInfoValid() {
47   ThreadSP thread_sp(m_thread_wp.lock());
48   if (thread_sp) {
49     m_stop_id = thread_sp->GetProcess()->GetStopID();
50     m_resume_id = thread_sp->GetProcess()->GetResumeID();
51   }
52 }
53 
54 bool StopInfo::HasTargetRunSinceMe() {
55   ThreadSP thread_sp(m_thread_wp.lock());
56 
57   if (thread_sp) {
58     lldb::StateType ret_type = thread_sp->GetProcess()->GetPrivateState();
59     if (ret_type == eStateRunning) {
60       return true;
61     } else if (ret_type == eStateStopped) {
62       // This is a little tricky.  We want to count "run and stopped again
63       // before you could ask this question as a "TRUE" answer to
64       // HasTargetRunSinceMe.  But we don't want to include any running of the
65       // target done for expressions.  So we track both resumes, and resumes
66       // caused by expressions, and check if there are any resumes
67       // NOT caused
68       // by expressions.
69 
70       uint32_t curr_resume_id = thread_sp->GetProcess()->GetResumeID();
71       uint32_t last_user_expression_id =
72           thread_sp->GetProcess()->GetLastUserExpressionResumeID();
73       if (curr_resume_id == m_resume_id) {
74         return false;
75       } else if (curr_resume_id > last_user_expression_id) {
76         return true;
77       }
78     }
79   }
80   return false;
81 }
82 
83 // StopInfoBreakpoint
84 
85 namespace lldb_private {
86 class StopInfoBreakpoint : public StopInfo {
87 public:
88   StopInfoBreakpoint(Thread &thread, break_id_t break_id)
89       : StopInfo(thread, break_id), m_should_stop(false),
90         m_should_stop_is_valid(false), m_should_perform_action(true),
91         m_address(LLDB_INVALID_ADDRESS), m_break_id(LLDB_INVALID_BREAK_ID),
92         m_was_all_internal(false), m_was_one_shot(false) {
93     StoreBPInfo();
94   }
95 
96   StopInfoBreakpoint(Thread &thread, break_id_t break_id, bool should_stop)
97       : StopInfo(thread, break_id), m_should_stop(should_stop),
98         m_should_stop_is_valid(true), m_should_perform_action(true),
99         m_address(LLDB_INVALID_ADDRESS), m_break_id(LLDB_INVALID_BREAK_ID),
100         m_was_all_internal(false), m_was_one_shot(false) {
101     StoreBPInfo();
102   }
103 
104   ~StopInfoBreakpoint() override = default;
105 
106   void StoreBPInfo() {
107     ThreadSP thread_sp(m_thread_wp.lock());
108     if (thread_sp) {
109       BreakpointSiteSP bp_site_sp(
110           thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
111       if (bp_site_sp) {
112         uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
113         if (num_owners == 1) {
114           BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(0);
115           if (bp_loc_sp) {
116             Breakpoint & bkpt = bp_loc_sp->GetBreakpoint();
117             m_break_id = bkpt.GetID();
118             m_was_one_shot = bkpt.IsOneShot();
119             m_was_all_internal = bkpt.IsInternal();
120           }
121         } else {
122           m_was_all_internal = true;
123           for (uint32_t i = 0; i < num_owners; i++) {
124             if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal()) {
125               m_was_all_internal = false;
126               break;
127             }
128           }
129         }
130         m_address = bp_site_sp->GetLoadAddress();
131       }
132     }
133   }
134 
135   bool IsValidForOperatingSystemThread(Thread &thread) override {
136     ProcessSP process_sp(thread.GetProcess());
137     if (process_sp) {
138       BreakpointSiteSP bp_site_sp(
139           process_sp->GetBreakpointSiteList().FindByID(m_value));
140       if (bp_site_sp)
141         return bp_site_sp->ValidForThisThread(thread);
142     }
143     return false;
144   }
145 
146   StopReason GetStopReason() const override { return eStopReasonBreakpoint; }
147 
148   bool ShouldStopSynchronous(Event *event_ptr) override {
149     ThreadSP thread_sp(m_thread_wp.lock());
150     if (thread_sp) {
151       if (!m_should_stop_is_valid) {
152         // Only check once if we should stop at a breakpoint
153         BreakpointSiteSP bp_site_sp(
154             thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
155         if (bp_site_sp) {
156           ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
157           StoppointCallbackContext context(event_ptr, exe_ctx, true);
158           bp_site_sp->BumpHitCounts();
159           m_should_stop = bp_site_sp->ShouldStop(&context);
160         } else {
161           Log *log = GetLog(LLDBLog::Process);
162 
163           LLDB_LOGF(log,
164                     "Process::%s could not find breakpoint site id: %" PRId64
165                     "...",
166                     __FUNCTION__, m_value);
167 
168           m_should_stop = true;
169         }
170         m_should_stop_is_valid = true;
171       }
172       return m_should_stop;
173     }
174     return false;
175   }
176 
177   bool DoShouldNotify(Event *event_ptr) override {
178     return !m_was_all_internal;
179   }
180 
181   const char *GetDescription() override {
182     if (m_description.empty()) {
183       ThreadSP thread_sp(m_thread_wp.lock());
184       if (thread_sp) {
185         BreakpointSiteSP bp_site_sp(
186             thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
187         if (bp_site_sp) {
188           StreamString strm;
189           // If we have just hit an internal breakpoint, and it has a kind
190           // description, print that instead of the full breakpoint printing:
191           if (bp_site_sp->IsInternal()) {
192             size_t num_owners = bp_site_sp->GetNumberOfOwners();
193             for (size_t idx = 0; idx < num_owners; idx++) {
194               const char *kind = bp_site_sp->GetOwnerAtIndex(idx)
195                                      ->GetBreakpoint()
196                                      .GetBreakpointKind();
197               if (kind != nullptr) {
198                 m_description.assign(kind);
199                 return kind;
200               }
201             }
202           }
203 
204           strm.Printf("breakpoint ");
205           bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
206           m_description = std::string(strm.GetString());
207         } else {
208           StreamString strm;
209           if (m_break_id != LLDB_INVALID_BREAK_ID) {
210             BreakpointSP break_sp =
211                 thread_sp->GetProcess()->GetTarget().GetBreakpointByID(
212                     m_break_id);
213             if (break_sp) {
214               if (break_sp->IsInternal()) {
215                 const char *kind = break_sp->GetBreakpointKind();
216                 if (kind)
217                   strm.Printf("internal %s breakpoint(%d).", kind, m_break_id);
218                 else
219                   strm.Printf("internal breakpoint(%d).", m_break_id);
220               } else {
221                 strm.Printf("breakpoint %d.", m_break_id);
222               }
223             } else {
224               if (m_was_one_shot)
225                 strm.Printf("one-shot breakpoint %d", m_break_id);
226               else
227                 strm.Printf("breakpoint %d which has been deleted.",
228                             m_break_id);
229             }
230           } else if (m_address == LLDB_INVALID_ADDRESS)
231             strm.Printf("breakpoint site %" PRIi64
232                         " which has been deleted - unknown address",
233                         m_value);
234           else
235             strm.Printf("breakpoint site %" PRIi64
236                         " which has been deleted - was at 0x%" PRIx64,
237                         m_value, m_address);
238 
239           m_description = std::string(strm.GetString());
240         }
241       }
242     }
243     return m_description.c_str();
244   }
245 
246 protected:
247   bool ShouldStop(Event *event_ptr) override {
248     // This just reports the work done by PerformAction or the synchronous
249     // stop. It should only ever get called after they have had a chance to
250     // run.
251     assert(m_should_stop_is_valid);
252     return m_should_stop;
253   }
254 
255   void PerformAction(Event *event_ptr) override {
256     if (!m_should_perform_action)
257       return;
258     m_should_perform_action = false;
259     bool internal_breakpoint = true;
260 
261     ThreadSP thread_sp(m_thread_wp.lock());
262 
263     if (thread_sp) {
264       Log *log = GetLog(LLDBLog::Breakpoints | LLDBLog::Step);
265 
266       if (!thread_sp->IsValid()) {
267         // This shouldn't ever happen, but just in case, don't do more harm.
268         if (log) {
269           LLDB_LOGF(log, "PerformAction got called with an invalid thread.");
270         }
271         m_should_stop = true;
272         m_should_stop_is_valid = true;
273         return;
274       }
275 
276       BreakpointSiteSP bp_site_sp(
277           thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
278       std::unordered_set<break_id_t> precondition_breakpoints;
279       // Breakpoints that fail their condition check are not considered to
280       // have been hit.  If the only locations at this site have failed their
281       // conditions, we should change the stop-info to none.  Otherwise, if we
282       // hit another breakpoint on a different thread which does stop, users
283       // will see a breakpont hit with a failed condition, which is wrong.
284       // Use this variable to tell us if that is true.
285       bool actually_hit_any_locations = false;
286       if (bp_site_sp) {
287         // Let's copy the owners list out of the site and store them in a local
288         // list.  That way if one of the breakpoint actions changes the site,
289         // then we won't be operating on a bad list.
290         BreakpointLocationCollection site_locations;
291         size_t num_owners = bp_site_sp->CopyOwnersList(site_locations);
292 
293         if (num_owners == 0) {
294           m_should_stop = true;
295           actually_hit_any_locations = true;  // We're going to stop, don't
296                                               // change the stop info.
297         } else {
298           // We go through each location, and test first its precondition -
299           // this overrides everything.  Note, we only do this once per
300           // breakpoint - not once per location... Then check the condition.
301           // If the condition says to stop, then we run the callback for that
302           // location.  If that callback says to stop as well, then we set
303           // m_should_stop to true; we are going to stop. But we still want to
304           // give all the breakpoints whose conditions say we are going to stop
305           // a chance to run their callbacks. Of course if any callback
306           // restarts the target by putting "continue" in the callback, then
307           // we're going to restart, without running the rest of the callbacks.
308           // And in this case we will end up not stopping even if another
309           // location said we should stop. But that's better than not running
310           // all the callbacks.
311 
312           // There's one other complication here.  We may have run an async
313           // breakpoint callback that said we should stop.  We only want to
314           // override that if another breakpoint action says we shouldn't
315           // stop.  If nobody else has an opinion, then we should stop if the
316           // async callback says we should.  An example of this is the async
317           // shared library load notification breakpoint and the setting
318           // stop-on-sharedlibrary-events.
319           // We'll keep the async value in async_should_stop, and track whether
320           // anyone said we should NOT stop in actually_said_continue.
321           bool async_should_stop = false;
322           if (m_should_stop_is_valid)
323             async_should_stop = m_should_stop;
324           bool actually_said_continue = false;
325 
326           m_should_stop = false;
327 
328           // We don't select threads as we go through them testing breakpoint
329           // conditions and running commands. So we need to set the thread for
330           // expression evaluation here:
331           ThreadList::ExpressionExecutionThreadPusher thread_pusher(thread_sp);
332 
333           ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
334           Process *process = exe_ctx.GetProcessPtr();
335           if (process->GetModIDRef().IsLastResumeForUserExpression()) {
336             // If we are in the middle of evaluating an expression, don't run
337             // asynchronous breakpoint commands or expressions.  That could
338             // lead to infinite recursion if the command or condition re-calls
339             // the function with this breakpoint.
340             // TODO: We can keep a list of the breakpoints we've seen while
341             // running expressions in the nested
342             // PerformAction calls that can arise when the action runs a
343             // function that hits another breakpoint, and only stop running
344             // commands when we see the same breakpoint hit a second time.
345 
346             m_should_stop_is_valid = true;
347 
348             // It is possible that the user has a breakpoint at the same site
349             // as the completed plan had (e.g. user has a breakpoint
350             // on a module entry point, and `ThreadPlanCallFunction` ends
351             // also there). We can't find an internal breakpoint in the loop
352             // later because it was already removed on the plan completion.
353             // So check if the plan was completed, and stop if so.
354             if (thread_sp->CompletedPlanOverridesBreakpoint()) {
355               m_should_stop = true;
356               thread_sp->ResetStopInfo();
357               return;
358             }
359 
360             LLDB_LOGF(log, "StopInfoBreakpoint::PerformAction - Hit a "
361                            "breakpoint while running an expression,"
362                            " not running commands to avoid recursion.");
363             bool ignoring_breakpoints =
364                 process->GetIgnoreBreakpointsInExpressions();
365             if (ignoring_breakpoints) {
366               m_should_stop = false;
367               // Internal breakpoints will always stop.
368               for (size_t j = 0; j < num_owners; j++) {
369                 lldb::BreakpointLocationSP bp_loc_sp =
370                     bp_site_sp->GetOwnerAtIndex(j);
371                 if (bp_loc_sp->GetBreakpoint().IsInternal()) {
372                   m_should_stop = true;
373                   break;
374                 }
375               }
376             } else {
377               m_should_stop = true;
378             }
379             LLDB_LOGF(log,
380                       "StopInfoBreakpoint::PerformAction - in expression, "
381                       "continuing: %s.",
382                       m_should_stop ? "true" : "false");
383             Debugger::ReportWarning(
384                 "hit breakpoint while running function, skipping commands and "
385                 "conditions to prevent recursion",
386                 process->GetTarget().GetDebugger().GetID());
387             return;
388           }
389 
390           StoppointCallbackContext context(event_ptr, exe_ctx, false);
391 
392           // For safety's sake let's also grab an extra reference to the
393           // breakpoint owners of the locations we're going to examine, since
394           // the locations are going to have to get back to their breakpoints,
395           // and the locations don't keep their owners alive.  I'm just
396           // sticking the BreakpointSP's in a vector since I'm only using it to
397           // locally increment their retain counts.
398 
399           std::vector<lldb::BreakpointSP> location_owners;
400 
401           for (size_t j = 0; j < num_owners; j++) {
402             BreakpointLocationSP loc(site_locations.GetByIndex(j));
403             location_owners.push_back(loc->GetBreakpoint().shared_from_this());
404           }
405 
406           for (size_t j = 0; j < num_owners; j++) {
407             lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j);
408             StreamString loc_desc;
409             if (log) {
410               bp_loc_sp->GetDescription(&loc_desc, eDescriptionLevelBrief);
411             }
412             // If another action disabled this breakpoint or its location, then
413             // don't run the actions.
414             if (!bp_loc_sp->IsEnabled() ||
415                 !bp_loc_sp->GetBreakpoint().IsEnabled())
416               continue;
417 
418             // The breakpoint site may have many locations associated with it,
419             // not all of them valid for this thread.  Skip the ones that
420             // aren't:
421             if (!bp_loc_sp->ValidForThisThread(*thread_sp)) {
422               if (log) {
423                 LLDB_LOGF(log,
424                           "Breakpoint %s hit on thread 0x%llx but it was not "
425                           "for this thread, continuing.",
426                           loc_desc.GetData(),
427                           static_cast<unsigned long long>(thread_sp->GetID()));
428               }
429               continue;
430             }
431 
432             internal_breakpoint = bp_loc_sp->GetBreakpoint().IsInternal();
433 
434             // First run the precondition, but since the precondition is per
435             // breakpoint, only run it once per breakpoint.
436             std::pair<std::unordered_set<break_id_t>::iterator, bool> result =
437                 precondition_breakpoints.insert(
438                     bp_loc_sp->GetBreakpoint().GetID());
439             if (!result.second)
440               continue;
441 
442             bool precondition_result =
443                 bp_loc_sp->GetBreakpoint().EvaluatePrecondition(context);
444             if (!precondition_result) {
445               actually_said_continue = true;
446               continue;
447             }
448             // Next run the condition for the breakpoint.  If that says we
449             // should stop, then we'll run the callback for the breakpoint.  If
450             // the callback says we shouldn't stop that will win.
451 
452             if (bp_loc_sp->GetConditionText() == nullptr)
453               actually_hit_any_locations = true;
454             else {
455               Status condition_error;
456               bool condition_says_stop =
457                   bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
458 
459               if (!condition_error.Success()) {
460                 // If the condition fails to evaluate, we are going to stop
461                 // at it, so the location was hit.
462                 actually_hit_any_locations = true;
463                 const char *err_str =
464                     condition_error.AsCString("<unknown error>");
465                 LLDB_LOGF(log, "Error evaluating condition: \"%s\"\n", err_str);
466 
467                 StreamString strm;
468                 strm << "stopped due to an error evaluating condition of "
469                         "breakpoint ";
470                 bp_loc_sp->GetDescription(&strm, eDescriptionLevelBrief);
471                 strm << ": \"" << bp_loc_sp->GetConditionText() << "\"\n";
472                 strm << err_str;
473 
474                 Debugger::ReportError(
475                     strm.GetString().str(),
476                     exe_ctx.GetTargetRef().GetDebugger().GetID());
477               } else {
478                 LLDB_LOGF(log,
479                           "Condition evaluated for breakpoint %s on thread "
480                           "0x%llx condition_says_stop: %i.",
481                           loc_desc.GetData(),
482                           static_cast<unsigned long long>(thread_sp->GetID()),
483                           condition_says_stop);
484                 if (condition_says_stop)
485                   actually_hit_any_locations = true;
486                 else {
487                   // We don't want to increment the hit count of breakpoints if
488                   // the condition fails. We've already bumped it by the time
489                   // we get here, so undo the bump:
490                   bp_loc_sp->UndoBumpHitCount();
491                   actually_said_continue = true;
492                   continue;
493                 }
494               }
495             }
496 
497             // We've done all the checks whose failure means "we consider lldb
498             // not to have hit the breakpoint".  Now we're going to check for
499             // conditions that might continue after hitting.  Start with the
500             // ignore count:
501             if (!bp_loc_sp->IgnoreCountShouldStop()) {
502               actually_said_continue = true;
503               continue;
504             }
505 
506             // Check the auto-continue bit on the location, do this before the
507             // callback since it may change this, but that would be for the
508             // NEXT hit.  Note, you might think you could check auto-continue
509             // before the condition, and not evaluate the condition if it says
510             // to continue.  But failing the condition means the breakpoint was
511             // effectively NOT HIT.  So these two states are different.
512             bool auto_continue_says_stop = true;
513             if (bp_loc_sp->IsAutoContinue())
514             {
515               LLDB_LOGF(log,
516                         "Continuing breakpoint %s as AutoContinue was set.",
517                         loc_desc.GetData());
518               // We want this stop reported, so you will know we auto-continued
519               // but only for external breakpoints:
520               if (!internal_breakpoint)
521                 thread_sp->SetShouldReportStop(eVoteYes);
522               auto_continue_says_stop = false;
523             }
524 
525             bool callback_says_stop = true;
526 
527             // FIXME: For now the callbacks have to run in async mode - the
528             // first time we restart we need
529             // to get out of there.  So set it here.
530             // When we figure out how to nest breakpoint hits then this will
531             // change.
532 
533             // Don't run async callbacks in PerformAction.  They have already
534             // been taken into account with async_should_stop.
535             if (!bp_loc_sp->IsCallbackSynchronous()) {
536               Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
537               bool old_async = debugger.GetAsyncExecution();
538               debugger.SetAsyncExecution(true);
539 
540               callback_says_stop = bp_loc_sp->InvokeCallback(&context);
541 
542               debugger.SetAsyncExecution(old_async);
543 
544               if (callback_says_stop && auto_continue_says_stop)
545                 m_should_stop = true;
546               else
547                 actually_said_continue = true;
548             }
549 
550             // If we are going to stop for this breakpoint, then remove the
551             // breakpoint.
552             if (callback_says_stop && bp_loc_sp &&
553                 bp_loc_sp->GetBreakpoint().IsOneShot()) {
554               thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID(
555                   bp_loc_sp->GetBreakpoint().GetID());
556             }
557             // Also make sure that the callback hasn't continued the target. If
558             // it did, when we'll set m_should_start to false and get out of
559             // here.
560             if (HasTargetRunSinceMe()) {
561               m_should_stop = false;
562               actually_said_continue = true;
563               break;
564             }
565           }
566           // At this point if nobody actually told us to continue, we should
567           // give the async breakpoint callback a chance to weigh in:
568           if (!actually_said_continue && !m_should_stop) {
569             m_should_stop = async_should_stop;
570           }
571         }
572         // We've figured out what this stop wants to do, so mark it as valid so
573         // we don't compute it again.
574         m_should_stop_is_valid = true;
575       } else {
576         m_should_stop = true;
577         m_should_stop_is_valid = true;
578         actually_hit_any_locations = true;
579         Log *log_process(GetLog(LLDBLog::Process));
580 
581         LLDB_LOGF(log_process,
582                   "Process::%s could not find breakpoint site id: %" PRId64
583                   "...",
584                   __FUNCTION__, m_value);
585       }
586 
587       if ((!m_should_stop || internal_breakpoint) &&
588           thread_sp->CompletedPlanOverridesBreakpoint()) {
589 
590         // Override should_stop decision when we have completed step plan
591         // additionally to the breakpoint
592         m_should_stop = true;
593 
594         // We know we're stopping for a completed plan and we don't want to
595         // show the breakpoint stop, so compute the public stop info immediately
596         // here.
597         thread_sp->CalculatePublicStopInfo();
598       } else if (!actually_hit_any_locations) {
599         // In the end, we didn't actually have any locations that passed their
600         // "was I hit" checks.  So say we aren't stopped.
601         GetThread()->ResetStopInfo();
602         LLDB_LOGF(log, "Process::%s all locations failed condition checks.",
603           __FUNCTION__);
604       }
605 
606       LLDB_LOGF(log,
607                 "Process::%s returning from action with m_should_stop: %d.",
608                 __FUNCTION__, m_should_stop);
609     }
610   }
611 
612 private:
613   bool m_should_stop;
614   bool m_should_stop_is_valid;
615   bool m_should_perform_action; // Since we are trying to preserve the "state"
616                                 // of the system even if we run functions
617   // etc. behind the users backs, we need to make sure we only REALLY perform
618   // the action once.
619   lldb::addr_t m_address; // We use this to capture the breakpoint site address
620                           // when we create the StopInfo,
621   // in case somebody deletes it between the time the StopInfo is made and the
622   // description is asked for.
623   lldb::break_id_t m_break_id;
624   bool m_was_all_internal;
625   bool m_was_one_shot;
626 };
627 
628 // StopInfoWatchpoint
629 
630 class StopInfoWatchpoint : public StopInfo {
631 public:
632   // Make sure watchpoint is properly disabled and subsequently enabled while
633   // performing watchpoint actions.
634   class WatchpointSentry {
635   public:
636     WatchpointSentry(ProcessSP p_sp, WatchpointSP w_sp) : process_sp(p_sp),
637                      watchpoint_sp(w_sp) {
638       if (process_sp && watchpoint_sp) {
639         const bool notify = false;
640         watchpoint_sp->TurnOnEphemeralMode();
641         process_sp->DisableWatchpoint(watchpoint_sp.get(), notify);
642         process_sp->AddPreResumeAction(SentryPreResumeAction, this);
643       }
644     }
645 
646     void DoReenable() {
647       if (process_sp && watchpoint_sp) {
648         bool was_disabled = watchpoint_sp->IsDisabledDuringEphemeralMode();
649         watchpoint_sp->TurnOffEphemeralMode();
650         const bool notify = false;
651         if (was_disabled) {
652           process_sp->DisableWatchpoint(watchpoint_sp.get(), notify);
653         } else {
654           process_sp->EnableWatchpoint(watchpoint_sp.get(), notify);
655         }
656       }
657     }
658 
659     ~WatchpointSentry() {
660         DoReenable();
661         if (process_sp)
662             process_sp->ClearPreResumeAction(SentryPreResumeAction, this);
663     }
664 
665     static bool SentryPreResumeAction(void *sentry_void) {
666         WatchpointSentry *sentry = (WatchpointSentry *) sentry_void;
667         sentry->DoReenable();
668         return true;
669     }
670 
671   private:
672     ProcessSP process_sp;
673     WatchpointSP watchpoint_sp;
674   };
675 
676   StopInfoWatchpoint(Thread &thread, break_id_t watch_id,
677                      lldb::addr_t watch_hit_addr)
678       : StopInfo(thread, watch_id), m_watch_hit_addr(watch_hit_addr) {}
679 
680   ~StopInfoWatchpoint() override = default;
681 
682   StopReason GetStopReason() const override { return eStopReasonWatchpoint; }
683 
684   const char *GetDescription() override {
685     if (m_description.empty()) {
686       StreamString strm;
687       strm.Printf("watchpoint %" PRIi64, m_value);
688       m_description = std::string(strm.GetString());
689     }
690     return m_description.c_str();
691   }
692 
693 protected:
694   using StopInfoWatchpointSP = std::shared_ptr<StopInfoWatchpoint>;
695   // This plan is used to orchestrate stepping over the watchpoint for
696   // architectures (e.g. ARM) that report the watch before running the watched
697   // access.  This is the sort of job you have to defer to the thread plans,
698   // if you try to do it directly in the stop info and there are other threads
699   // that needed to process this stop you will have yanked control away from
700   // them and they won't behave correctly.
701   class ThreadPlanStepOverWatchpoint : public ThreadPlanStepInstruction {
702   public:
703     ThreadPlanStepOverWatchpoint(Thread &thread,
704                                  StopInfoWatchpointSP stop_info_sp,
705                                  WatchpointSP watch_sp)
706         : ThreadPlanStepInstruction(thread, false, true, eVoteNoOpinion,
707                                     eVoteNoOpinion),
708           m_stop_info_sp(stop_info_sp), m_watch_sp(watch_sp) {
709       assert(watch_sp);
710       m_watch_index = watch_sp->GetHardwareIndex();
711     }
712 
713     bool DoWillResume(lldb::StateType resume_state,
714                       bool current_plan) override {
715       if (m_did_reset_watchpoint) {
716         GetThread().GetProcess()->DisableWatchpoint(m_watch_sp.get(), false);
717         m_did_reset_watchpoint = false;
718       }
719       return true;
720     }
721 
722     void DidPop() override {
723       // Don't artifically keep the watchpoint alive.
724       m_watch_sp.reset();
725     }
726 
727     bool ShouldStop(Event *event_ptr) override {
728       bool should_stop = ThreadPlanStepInstruction::ShouldStop(event_ptr);
729       bool plan_done = MischiefManaged();
730       if (plan_done) {
731         m_stop_info_sp->SetStepOverPlanComplete();
732         GetThread().SetStopInfo(m_stop_info_sp);
733         ResetWatchpoint();
734       }
735       return should_stop;
736     }
737 
738   protected:
739     void ResetWatchpoint() {
740       if (m_did_reset_watchpoint)
741         return;
742       m_did_reset_watchpoint = true;
743       GetThread().GetProcess()->EnableWatchpoint(m_watch_sp.get(), false);
744       m_watch_sp->SetHardwareIndex(m_watch_index);
745     }
746 
747   private:
748     StopInfoWatchpointSP m_stop_info_sp;
749     WatchpointSP m_watch_sp;
750     uint32_t m_watch_index = LLDB_INVALID_INDEX32;
751     bool m_did_reset_watchpoint = false;
752   };
753 
754   bool ShouldStopSynchronous(Event *event_ptr) override {
755     // If we are running our step-over the watchpoint plan, stop if it's done
756     // and continue if it's not:
757     if (m_should_stop_is_valid)
758       return m_should_stop;
759 
760     // If we are running our step over plan, then stop here and let the regular
761     // ShouldStop figure out what we should do:  Otherwise, give our plan
762     // more time to get run:
763     if (m_using_step_over_plan)
764       return m_step_over_plan_complete;
765 
766     Log *log = GetLog(LLDBLog::Process);
767     ThreadSP thread_sp(m_thread_wp.lock());
768     assert(thread_sp);
769     WatchpointSP wp_sp(
770         thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
771     // If we can no longer find the watchpoint, we just have to stop:
772     if (!wp_sp) {
773 
774       LLDB_LOGF(log,
775                 "Process::%s could not find watchpoint location id: %" PRId64
776                 "...",
777                 __FUNCTION__, GetValue());
778 
779       m_should_stop = true;
780       m_should_stop_is_valid = true;
781       return true;
782     }
783 
784     ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
785     StoppointCallbackContext context(event_ptr, exe_ctx, true);
786     m_should_stop = wp_sp->ShouldStop(&context);
787     if (!m_should_stop) {
788       // This won't happen at present because we only allow one watchpoint per
789       // watched range.  So we won't stop at a watched address with a disabled
790       // watchpoint.  If we start allowing overlapping watchpoints, then we
791       // will have to make watchpoints be real "WatchpointSite" and delegate to
792       // all the watchpoints sharing the site.  In that case, the code below
793       // would be the right thing to do.
794       m_should_stop_is_valid = true;
795       return m_should_stop;
796     }
797     // If this is a system where we need to execute the watchpoint by hand
798     // after the hit, queue a thread plan to do that, and then say not to stop.
799     // Otherwise, let the async action figure out whether the watchpoint should
800     // stop
801 
802     ProcessSP process_sp = exe_ctx.GetProcessSP();
803     uint32_t num;
804     bool wp_triggers_after;
805 
806     if (!process_sp->GetWatchpointSupportInfo(num, wp_triggers_after)
807             .Success()) {
808       m_should_stop_is_valid = true;
809       m_should_stop = true;
810       return m_should_stop;
811     }
812 
813     if (!wp_triggers_after) {
814       // We have to step over the breakpoint before we know what to do:
815       StopInfoWatchpointSP me_as_siwp_sp
816           = std::static_pointer_cast<StopInfoWatchpoint>(shared_from_this());
817       ThreadPlanSP step_over_wp_sp(new ThreadPlanStepOverWatchpoint(
818           *(thread_sp.get()), me_as_siwp_sp, wp_sp));
819       Status error;
820       error = thread_sp->QueueThreadPlan(step_over_wp_sp, false);
821       // If we couldn't push the thread plan, just stop here:
822       if (!error.Success()) {
823         LLDB_LOGF(log, "Could not push our step over watchpoint plan: %s",
824             error.AsCString());
825 
826         m_should_stop = true;
827         m_should_stop_is_valid = true;
828         return true;
829       } else {
830       // Otherwise, don't set m_should_stop, we don't know that yet.  Just
831       // say we should continue:
832         m_using_step_over_plan = true;
833         return false;
834       }
835     } else {
836       // We didn't have to do anything special here, so just return our value:
837       m_should_stop_is_valid = true;
838       return m_should_stop;
839     }
840 
841     return m_should_stop;
842   }
843 
844   bool ShouldStop(Event *event_ptr) override {
845     // This just reports the work done by PerformAction or the synchronous
846     // stop. It should only ever get called after they have had a chance to
847     // run.
848     assert(m_should_stop_is_valid);
849     return m_should_stop;
850   }
851 
852   void PerformAction(Event *event_ptr) override {
853     Log *log = GetLog(LLDBLog::Watchpoints);
854     // We're going to calculate if we should stop or not in some way during the
855     // course of this code.  Also by default we're going to stop, so set that
856     // here.
857     m_should_stop = true;
858 
859 
860     ThreadSP thread_sp(m_thread_wp.lock());
861     if (thread_sp) {
862 
863       WatchpointSP wp_sp(
864           thread_sp->CalculateTarget()->GetWatchpointList().FindByID(
865               GetValue()));
866       if (wp_sp) {
867         // This sentry object makes sure the current watchpoint is disabled
868         // while performing watchpoint actions, and it is then enabled after we
869         // are finished.
870         ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
871         ProcessSP process_sp = exe_ctx.GetProcessSP();
872 
873         WatchpointSentry sentry(process_sp, wp_sp);
874 
875         /*
876          * MIPS: Last 3bits of the watchpoint address are masked by the kernel.
877          * For example:
878          * 'n' is at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is
879          * set at 'm', then
880          * watch exception is generated even when 'n' is read/written. To handle
881          * this case,
882          * server emulates the instruction at PC and finds the base address of
883          * the load/store
884          * instruction and appends it in the description of the stop-info
885          * packet. If watchpoint
886          * is not set on this address by user then this do not stop.
887         */
888         if (m_watch_hit_addr != LLDB_INVALID_ADDRESS) {
889           WatchpointSP wp_hit_sp =
890               thread_sp->CalculateTarget()->GetWatchpointList().FindByAddress(
891                   m_watch_hit_addr);
892           if (!wp_hit_sp) {
893             m_should_stop = false;
894             wp_sp->IncrementFalseAlarmsAndReviseHitCount();
895           }
896         }
897 
898         if (wp_sp->GetHitCount() <= wp_sp->GetIgnoreCount()) {
899           m_should_stop = false;
900           m_should_stop_is_valid = true;
901         }
902 
903         Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
904 
905         if (m_should_stop && wp_sp->GetConditionText() != nullptr) {
906           // We need to make sure the user sees any parse errors in their
907           // condition, so we'll hook the constructor errors up to the
908           // debugger's Async I/O.
909           ExpressionResults result_code;
910           EvaluateExpressionOptions expr_options;
911           expr_options.SetUnwindOnError(true);
912           expr_options.SetIgnoreBreakpoints(true);
913           ValueObjectSP result_value_sp;
914           Status error;
915           result_code = UserExpression::Evaluate(
916               exe_ctx, expr_options, wp_sp->GetConditionText(),
917               llvm::StringRef(), result_value_sp, error);
918 
919           if (result_code == eExpressionCompleted) {
920             if (result_value_sp) {
921               Scalar scalar_value;
922               if (result_value_sp->ResolveValue(scalar_value)) {
923                 if (scalar_value.ULongLong(1) == 0) {
924                   // The condition failed, which we consider "not having hit
925                   // the watchpoint" so undo the hit count here.
926                   wp_sp->UndoHitCount();
927                   m_should_stop = false;
928                 } else
929                   m_should_stop = true;
930                 LLDB_LOGF(log,
931                           "Condition successfully evaluated, result is %s.\n",
932                           m_should_stop ? "true" : "false");
933               } else {
934                 m_should_stop = true;
935                 LLDB_LOGF(
936                     log,
937                     "Failed to get an integer result from the expression.");
938               }
939             }
940           } else {
941             const char *err_str = error.AsCString("<unknown error>");
942             LLDB_LOGF(log, "Error evaluating condition: \"%s\"\n", err_str);
943 
944             StreamString strm;
945             strm << "stopped due to an error evaluating condition of "
946                     "watchpoint ";
947             wp_sp->GetDescription(&strm, eDescriptionLevelBrief);
948             strm << ": \"" << wp_sp->GetConditionText() << "\"\n";
949             strm << err_str;
950 
951             Debugger::ReportError(strm.GetString().str(),
952                                   exe_ctx.GetTargetRef().GetDebugger().GetID());
953           }
954         }
955 
956         // If the condition says to stop, we run the callback to further decide
957         // whether to stop.
958         if (m_should_stop) {
959             // FIXME: For now the callbacks have to run in async mode - the
960             // first time we restart we need
961             // to get out of there.  So set it here.
962             // When we figure out how to nest watchpoint hits then this will
963             // change.
964 
965           bool old_async = debugger.GetAsyncExecution();
966           debugger.SetAsyncExecution(true);
967 
968           StoppointCallbackContext context(event_ptr, exe_ctx, false);
969           bool stop_requested = wp_sp->InvokeCallback(&context);
970 
971           debugger.SetAsyncExecution(old_async);
972 
973           // Also make sure that the callback hasn't continued the target. If
974           // it did, when we'll set m_should_stop to false and get out of here.
975           if (HasTargetRunSinceMe())
976             m_should_stop = false;
977 
978           if (m_should_stop && !stop_requested) {
979             // We have been vetoed by the callback mechanism.
980             m_should_stop = false;
981           }
982         }
983         // Finally, if we are going to stop, print out the new & old values:
984         if (m_should_stop) {
985           wp_sp->CaptureWatchedValue(exe_ctx);
986 
987           Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
988           StreamSP output_sp = debugger.GetAsyncOutputStream();
989           wp_sp->DumpSnapshots(output_sp.get());
990           output_sp->EOL();
991           output_sp->Flush();
992         }
993 
994       } else {
995         Log *log_process(GetLog(LLDBLog::Process));
996 
997         LLDB_LOGF(log_process,
998                   "Process::%s could not find watchpoint id: %" PRId64 "...",
999                   __FUNCTION__, m_value);
1000       }
1001       LLDB_LOGF(log,
1002                 "Process::%s returning from action with m_should_stop: %d.",
1003                 __FUNCTION__, m_should_stop);
1004 
1005       m_should_stop_is_valid = true;
1006     }
1007   }
1008 
1009 private:
1010   void SetStepOverPlanComplete() {
1011     assert(m_using_step_over_plan);
1012     m_step_over_plan_complete = true;
1013   }
1014 
1015   bool m_should_stop = false;
1016   bool m_should_stop_is_valid = false;
1017   lldb::addr_t m_watch_hit_addr;
1018   bool m_step_over_plan_complete = false;
1019   bool m_using_step_over_plan = false;
1020 };
1021 
1022 // StopInfoUnixSignal
1023 
1024 class StopInfoUnixSignal : public StopInfo {
1025 public:
1026   StopInfoUnixSignal(Thread &thread, int signo, const char *description)
1027       : StopInfo(thread, signo) {
1028     SetDescription(description);
1029   }
1030 
1031   ~StopInfoUnixSignal() override = default;
1032 
1033   StopReason GetStopReason() const override { return eStopReasonSignal; }
1034 
1035   bool ShouldStopSynchronous(Event *event_ptr) override {
1036     ThreadSP thread_sp(m_thread_wp.lock());
1037     if (thread_sp)
1038       return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
1039     return false;
1040   }
1041 
1042   bool ShouldStop(Event *event_ptr) override {
1043     ThreadSP thread_sp(m_thread_wp.lock());
1044     if (thread_sp)
1045       return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
1046     return false;
1047   }
1048 
1049   // If should stop returns false, check if we should notify of this event
1050   bool DoShouldNotify(Event *event_ptr) override {
1051     ThreadSP thread_sp(m_thread_wp.lock());
1052     if (thread_sp) {
1053       bool should_notify =
1054           thread_sp->GetProcess()->GetUnixSignals()->GetShouldNotify(m_value);
1055       if (should_notify) {
1056         StreamString strm;
1057         strm.Printf(
1058             "thread %d received signal: %s", thread_sp->GetIndexID(),
1059             thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString(
1060                 m_value));
1061         Process::ProcessEventData::AddRestartedReason(event_ptr,
1062                                                       strm.GetData());
1063       }
1064       return should_notify;
1065     }
1066     return true;
1067   }
1068 
1069   void WillResume(lldb::StateType resume_state) override {
1070     ThreadSP thread_sp(m_thread_wp.lock());
1071     if (thread_sp) {
1072       if (!thread_sp->GetProcess()->GetUnixSignals()->GetShouldSuppress(
1073               m_value))
1074         thread_sp->SetResumeSignal(m_value);
1075     }
1076   }
1077 
1078   const char *GetDescription() override {
1079     if (m_description.empty()) {
1080       ThreadSP thread_sp(m_thread_wp.lock());
1081       if (thread_sp) {
1082         StreamString strm;
1083         const char *signal_name =
1084             thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString(
1085                 m_value);
1086         if (signal_name)
1087           strm.Printf("signal %s", signal_name);
1088         else
1089           strm.Printf("signal %" PRIi64, m_value);
1090         m_description = std::string(strm.GetString());
1091       }
1092     }
1093     return m_description.c_str();
1094   }
1095 };
1096 
1097 // StopInfoTrace
1098 
1099 class StopInfoTrace : public StopInfo {
1100 public:
1101   StopInfoTrace(Thread &thread) : StopInfo(thread, LLDB_INVALID_UID) {}
1102 
1103   ~StopInfoTrace() override = default;
1104 
1105   StopReason GetStopReason() const override { return eStopReasonTrace; }
1106 
1107   const char *GetDescription() override {
1108     if (m_description.empty())
1109       return "trace";
1110     else
1111       return m_description.c_str();
1112   }
1113 };
1114 
1115 // StopInfoException
1116 
1117 class StopInfoException : public StopInfo {
1118 public:
1119   StopInfoException(Thread &thread, const char *description)
1120       : StopInfo(thread, LLDB_INVALID_UID) {
1121     if (description)
1122       SetDescription(description);
1123   }
1124 
1125   ~StopInfoException() override = default;
1126 
1127   StopReason GetStopReason() const override { return eStopReasonException; }
1128 
1129   const char *GetDescription() override {
1130     if (m_description.empty())
1131       return "exception";
1132     else
1133       return m_description.c_str();
1134   }
1135 };
1136 
1137 // StopInfoProcessorTrace
1138 
1139 class StopInfoProcessorTrace : public StopInfo {
1140 public:
1141   StopInfoProcessorTrace(Thread &thread, const char *description)
1142       : StopInfo(thread, LLDB_INVALID_UID) {
1143     if (description)
1144       SetDescription(description);
1145   }
1146 
1147   ~StopInfoProcessorTrace() override = default;
1148 
1149   StopReason GetStopReason() const override {
1150     return eStopReasonProcessorTrace;
1151   }
1152 
1153   const char *GetDescription() override {
1154     if (m_description.empty())
1155       return "processor trace event";
1156     else
1157       return m_description.c_str();
1158   }
1159 };
1160 
1161 // StopInfoThreadPlan
1162 
1163 class StopInfoThreadPlan : public StopInfo {
1164 public:
1165   StopInfoThreadPlan(ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp,
1166                      ExpressionVariableSP &expression_variable_sp)
1167       : StopInfo(plan_sp->GetThread(), LLDB_INVALID_UID), m_plan_sp(plan_sp),
1168         m_return_valobj_sp(return_valobj_sp),
1169         m_expression_variable_sp(expression_variable_sp) {}
1170 
1171   ~StopInfoThreadPlan() override = default;
1172 
1173   StopReason GetStopReason() const override { return eStopReasonPlanComplete; }
1174 
1175   const char *GetDescription() override {
1176     if (m_description.empty()) {
1177       StreamString strm;
1178       m_plan_sp->GetDescription(&strm, eDescriptionLevelBrief);
1179       m_description = std::string(strm.GetString());
1180     }
1181     return m_description.c_str();
1182   }
1183 
1184   ValueObjectSP GetReturnValueObject() { return m_return_valobj_sp; }
1185 
1186   ExpressionVariableSP GetExpressionVariable() {
1187     return m_expression_variable_sp;
1188   }
1189 
1190 protected:
1191   bool ShouldStop(Event *event_ptr) override {
1192     if (m_plan_sp)
1193       return m_plan_sp->ShouldStop(event_ptr);
1194     else
1195       return StopInfo::ShouldStop(event_ptr);
1196   }
1197 
1198 private:
1199   ThreadPlanSP m_plan_sp;
1200   ValueObjectSP m_return_valobj_sp;
1201   ExpressionVariableSP m_expression_variable_sp;
1202 };
1203 
1204 // StopInfoExec
1205 
1206 class StopInfoExec : public StopInfo {
1207 public:
1208   StopInfoExec(Thread &thread) : StopInfo(thread, LLDB_INVALID_UID) {}
1209 
1210   ~StopInfoExec() override = default;
1211 
1212   bool ShouldStop(Event *event_ptr) override {
1213     ThreadSP thread_sp(m_thread_wp.lock());
1214     if (thread_sp)
1215       return thread_sp->GetProcess()->GetStopOnExec();
1216     return false;
1217   }
1218 
1219   StopReason GetStopReason() const override { return eStopReasonExec; }
1220 
1221   const char *GetDescription() override { return "exec"; }
1222 
1223 protected:
1224   void PerformAction(Event *event_ptr) override {
1225     // Only perform the action once
1226     if (m_performed_action)
1227       return;
1228     m_performed_action = true;
1229     ThreadSP thread_sp(m_thread_wp.lock());
1230     if (thread_sp)
1231       thread_sp->GetProcess()->DidExec();
1232   }
1233 
1234   bool m_performed_action = false;
1235 };
1236 
1237 // StopInfoFork
1238 
1239 class StopInfoFork : public StopInfo {
1240 public:
1241   StopInfoFork(Thread &thread, lldb::pid_t child_pid, lldb::tid_t child_tid)
1242       : StopInfo(thread, child_pid), m_child_pid(child_pid),
1243         m_child_tid(child_tid) {}
1244 
1245   ~StopInfoFork() override = default;
1246 
1247   bool ShouldStop(Event *event_ptr) override { return false; }
1248 
1249   StopReason GetStopReason() const override { return eStopReasonFork; }
1250 
1251   const char *GetDescription() override { return "fork"; }
1252 
1253 protected:
1254   void PerformAction(Event *event_ptr) override {
1255     // Only perform the action once
1256     if (m_performed_action)
1257       return;
1258     m_performed_action = true;
1259     ThreadSP thread_sp(m_thread_wp.lock());
1260     if (thread_sp)
1261       thread_sp->GetProcess()->DidFork(m_child_pid, m_child_tid);
1262   }
1263 
1264   bool m_performed_action = false;
1265 
1266 private:
1267   lldb::pid_t m_child_pid;
1268   lldb::tid_t m_child_tid;
1269 };
1270 
1271 // StopInfoVFork
1272 
1273 class StopInfoVFork : public StopInfo {
1274 public:
1275   StopInfoVFork(Thread &thread, lldb::pid_t child_pid, lldb::tid_t child_tid)
1276       : StopInfo(thread, child_pid), m_child_pid(child_pid),
1277         m_child_tid(child_tid) {}
1278 
1279   ~StopInfoVFork() override = default;
1280 
1281   bool ShouldStop(Event *event_ptr) override { return false; }
1282 
1283   StopReason GetStopReason() const override { return eStopReasonVFork; }
1284 
1285   const char *GetDescription() override { return "vfork"; }
1286 
1287 protected:
1288   void PerformAction(Event *event_ptr) override {
1289     // Only perform the action once
1290     if (m_performed_action)
1291       return;
1292     m_performed_action = true;
1293     ThreadSP thread_sp(m_thread_wp.lock());
1294     if (thread_sp)
1295       thread_sp->GetProcess()->DidVFork(m_child_pid, m_child_tid);
1296   }
1297 
1298   bool m_performed_action = false;
1299 
1300 private:
1301   lldb::pid_t m_child_pid;
1302   lldb::tid_t m_child_tid;
1303 };
1304 
1305 // StopInfoVForkDone
1306 
1307 class StopInfoVForkDone : public StopInfo {
1308 public:
1309   StopInfoVForkDone(Thread &thread) : StopInfo(thread, 0) {}
1310 
1311   ~StopInfoVForkDone() override = default;
1312 
1313   bool ShouldStop(Event *event_ptr) override { return false; }
1314 
1315   StopReason GetStopReason() const override { return eStopReasonVForkDone; }
1316 
1317   const char *GetDescription() override { return "vforkdone"; }
1318 
1319 protected:
1320   void PerformAction(Event *event_ptr) override {
1321     // Only perform the action once
1322     if (m_performed_action)
1323       return;
1324     m_performed_action = true;
1325     ThreadSP thread_sp(m_thread_wp.lock());
1326     if (thread_sp)
1327       thread_sp->GetProcess()->DidVForkDone();
1328   }
1329 
1330   bool m_performed_action = false;
1331 };
1332 
1333 } // namespace lldb_private
1334 
1335 StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
1336                                                           break_id_t break_id) {
1337   return StopInfoSP(new StopInfoBreakpoint(thread, break_id));
1338 }
1339 
1340 StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
1341                                                           break_id_t break_id,
1342                                                           bool should_stop) {
1343   return StopInfoSP(new StopInfoBreakpoint(thread, break_id, should_stop));
1344 }
1345 
1346 StopInfoSP
1347 StopInfo::CreateStopReasonWithWatchpointID(Thread &thread, break_id_t watch_id,
1348                                            lldb::addr_t watch_hit_addr) {
1349   return StopInfoSP(new StopInfoWatchpoint(thread, watch_id, watch_hit_addr));
1350 }
1351 
1352 StopInfoSP StopInfo::CreateStopReasonWithSignal(Thread &thread, int signo,
1353                                                 const char *description) {
1354   thread.GetProcess()->GetUnixSignals()->IncrementSignalHitCount(signo);
1355   return StopInfoSP(new StopInfoUnixSignal(thread, signo, description));
1356 }
1357 
1358 StopInfoSP StopInfo::CreateStopReasonToTrace(Thread &thread) {
1359   return StopInfoSP(new StopInfoTrace(thread));
1360 }
1361 
1362 StopInfoSP StopInfo::CreateStopReasonWithPlan(
1363     ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp,
1364     ExpressionVariableSP expression_variable_sp) {
1365   return StopInfoSP(new StopInfoThreadPlan(plan_sp, return_valobj_sp,
1366                                            expression_variable_sp));
1367 }
1368 
1369 StopInfoSP StopInfo::CreateStopReasonWithException(Thread &thread,
1370                                                    const char *description) {
1371   return StopInfoSP(new StopInfoException(thread, description));
1372 }
1373 
1374 StopInfoSP StopInfo::CreateStopReasonProcessorTrace(Thread &thread,
1375                                                     const char *description) {
1376   return StopInfoSP(new StopInfoProcessorTrace(thread, description));
1377 }
1378 
1379 StopInfoSP StopInfo::CreateStopReasonWithExec(Thread &thread) {
1380   return StopInfoSP(new StopInfoExec(thread));
1381 }
1382 
1383 StopInfoSP StopInfo::CreateStopReasonFork(Thread &thread,
1384                                           lldb::pid_t child_pid,
1385                                           lldb::tid_t child_tid) {
1386   return StopInfoSP(new StopInfoFork(thread, child_pid, child_tid));
1387 }
1388 
1389 
1390 StopInfoSP StopInfo::CreateStopReasonVFork(Thread &thread,
1391                                            lldb::pid_t child_pid,
1392                                            lldb::tid_t child_tid) {
1393   return StopInfoSP(new StopInfoVFork(thread, child_pid, child_tid));
1394 }
1395 
1396 StopInfoSP StopInfo::CreateStopReasonVForkDone(Thread &thread) {
1397   return StopInfoSP(new StopInfoVForkDone(thread));
1398 }
1399 
1400 ValueObjectSP StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp) {
1401   if (stop_info_sp &&
1402       stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1403     StopInfoThreadPlan *plan_stop_info =
1404         static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1405     return plan_stop_info->GetReturnValueObject();
1406   } else
1407     return ValueObjectSP();
1408 }
1409 
1410 ExpressionVariableSP StopInfo::GetExpressionVariable(StopInfoSP &stop_info_sp) {
1411   if (stop_info_sp &&
1412       stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1413     StopInfoThreadPlan *plan_stop_info =
1414         static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1415     return plan_stop_info->GetExpressionVariable();
1416   } else
1417     return ExpressionVariableSP();
1418 }
1419 
1420 lldb::ValueObjectSP
1421 StopInfo::GetCrashingDereference(StopInfoSP &stop_info_sp,
1422                                  lldb::addr_t *crashing_address) {
1423   if (!stop_info_sp) {
1424     return ValueObjectSP();
1425   }
1426 
1427   const char *description = stop_info_sp->GetDescription();
1428   if (!description) {
1429     return ValueObjectSP();
1430   }
1431 
1432   ThreadSP thread_sp = stop_info_sp->GetThread();
1433   if (!thread_sp) {
1434     return ValueObjectSP();
1435   }
1436 
1437   StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
1438 
1439   if (!frame_sp) {
1440     return ValueObjectSP();
1441   }
1442 
1443   const char address_string[] = "address=";
1444 
1445   const char *address_loc = strstr(description, address_string);
1446   if (!address_loc) {
1447     return ValueObjectSP();
1448   }
1449 
1450   address_loc += (sizeof(address_string) - 1);
1451 
1452   uint64_t address = strtoull(address_loc, nullptr, 0);
1453   if (crashing_address) {
1454     *crashing_address = address;
1455   }
1456 
1457   return frame_sp->GuessValueForAddress(address);
1458 }
1459