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