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