1 //===-- StopInfo.cpp ---------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/lldb-python.h"
11 
12 #include "lldb/Target/StopInfo.h"
13 
14 // C Includes
15 // C++ Includes
16 #include <string>
17 
18 // Other libraries and framework includes
19 // Project includes
20 #include "lldb/Core/Log.h"
21 #include "lldb/Breakpoint/Breakpoint.h"
22 #include "lldb/Breakpoint/BreakpointLocation.h"
23 #include "lldb/Breakpoint/StoppointCallbackContext.h"
24 #include "lldb/Breakpoint/Watchpoint.h"
25 #include "lldb/Core/Debugger.h"
26 #include "lldb/Core/StreamString.h"
27 #include "lldb/Expression/ClangUserExpression.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Target/Thread.h"
30 #include "lldb/Target/ThreadPlan.h"
31 #include "lldb/Target/Process.h"
32 #include "lldb/Target/UnixSignals.h"
33 
34 using namespace lldb;
35 using namespace lldb_private;
36 
37 StopInfo::StopInfo (Thread &thread, uint64_t value) :
38     m_thread_wp (thread.shared_from_this()),
39     m_stop_id (thread.GetProcess()->GetStopID()),
40     m_resume_id (thread.GetProcess()->GetResumeID()),
41     m_value (value),
42     m_override_should_notify (eLazyBoolCalculate),
43     m_override_should_stop (eLazyBoolCalculate),
44     m_extended_info()
45 {
46 }
47 
48 bool
49 StopInfo::IsValid () const
50 {
51     ThreadSP thread_sp (m_thread_wp.lock());
52     if (thread_sp)
53         return thread_sp->GetProcess()->GetStopID() == m_stop_id;
54     return false;
55 }
56 
57 void
58 StopInfo::MakeStopInfoValid ()
59 {
60     ThreadSP thread_sp (m_thread_wp.lock());
61     if (thread_sp)
62     {
63         m_stop_id = thread_sp->GetProcess()->GetStopID();
64         m_resume_id = thread_sp->GetProcess()->GetResumeID();
65     }
66 }
67 
68 bool
69 StopInfo::HasTargetRunSinceMe ()
70 {
71     ThreadSP thread_sp (m_thread_wp.lock());
72 
73     if (thread_sp)
74     {
75         lldb::StateType ret_type = thread_sp->GetProcess()->GetPrivateState();
76         if (ret_type == eStateRunning)
77         {
78             return true;
79         }
80         else if (ret_type == eStateStopped)
81         {
82             // This is a little tricky.  We want to count "run and stopped again before you could
83             // ask this question as a "TRUE" answer to HasTargetRunSinceMe.  But we don't want to
84             // include any running of the target done for expressions.  So we track both resumes,
85             // and resumes caused by expressions, and check if there are any resumes NOT caused
86             // by expressions.
87 
88             uint32_t curr_resume_id = thread_sp->GetProcess()->GetResumeID();
89             uint32_t last_user_expression_id = thread_sp->GetProcess()->GetLastUserExpressionResumeID ();
90             if (curr_resume_id == m_resume_id)
91             {
92                 return false;
93             }
94             else if (curr_resume_id > last_user_expression_id)
95             {
96                 return true;
97             }
98         }
99     }
100     return false;
101 }
102 
103 //----------------------------------------------------------------------
104 // StopInfoBreakpoint
105 //----------------------------------------------------------------------
106 
107 namespace lldb_private
108 {
109 class StopInfoBreakpoint : public StopInfo
110 {
111 public:
112     StopInfoBreakpoint (Thread &thread, break_id_t break_id) :
113         StopInfo (thread, break_id),
114         m_description(),
115         m_should_stop (false),
116         m_should_stop_is_valid (false),
117         m_should_perform_action (true),
118         m_address (LLDB_INVALID_ADDRESS),
119         m_break_id(LLDB_INVALID_BREAK_ID),
120         m_was_one_shot (false)
121     {
122         StoreBPInfo();
123     }
124 
125     StopInfoBreakpoint (Thread &thread, break_id_t break_id, bool should_stop) :
126         StopInfo (thread, break_id),
127         m_description(),
128         m_should_stop (should_stop),
129         m_should_stop_is_valid (true),
130         m_should_perform_action (true),
131         m_address (LLDB_INVALID_ADDRESS),
132         m_break_id(LLDB_INVALID_BREAK_ID),
133         m_was_one_shot (false)
134     {
135         StoreBPInfo();
136     }
137 
138     void
139     StoreBPInfo ()
140     {
141         ThreadSP thread_sp (m_thread_wp.lock());
142         if (thread_sp)
143         {
144             BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
145             if (bp_site_sp)
146             {
147                 if (bp_site_sp->GetNumberOfOwners() == 1)
148                 {
149                     BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(0);
150                     if (bp_loc_sp)
151                     {
152                         m_break_id = bp_loc_sp->GetBreakpoint().GetID();
153                         m_was_one_shot = bp_loc_sp->GetBreakpoint().IsOneShot();
154                     }
155                 }
156                 m_address = bp_site_sp->GetLoadAddress();
157             }
158         }
159     }
160 
161     virtual ~StopInfoBreakpoint ()
162     {
163     }
164 
165     virtual StopReason
166     GetStopReason () const
167     {
168         return eStopReasonBreakpoint;
169     }
170 
171     virtual bool
172     ShouldStopSynchronous (Event *event_ptr)
173     {
174         ThreadSP thread_sp (m_thread_wp.lock());
175         if (thread_sp)
176         {
177             if (!m_should_stop_is_valid)
178             {
179                 // Only check once if we should stop at a breakpoint
180                 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
181                 if (bp_site_sp)
182                 {
183                     ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
184                     StoppointCallbackContext context (event_ptr, exe_ctx, true);
185                     m_should_stop = bp_site_sp->ShouldStop (&context);
186                 }
187                 else
188                 {
189                     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
190 
191                     if (log)
192                         log->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
193 
194                     m_should_stop = true;
195                 }
196                 m_should_stop_is_valid = true;
197             }
198             return m_should_stop;
199         }
200         return false;
201     }
202 
203     virtual bool
204     DoShouldNotify (Event *event_ptr)
205     {
206         ThreadSP thread_sp (m_thread_wp.lock());
207         if (thread_sp)
208         {
209             BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
210             if (bp_site_sp)
211             {
212                 bool all_internal = true;
213 
214                 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++)
215                 {
216                     if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
217                     {
218                         all_internal = false;
219                         break;
220                     }
221                 }
222                 return all_internal == false;
223             }
224         }
225         return true;
226     }
227 
228     virtual const char *
229     GetDescription ()
230     {
231         if (m_description.empty())
232         {
233             ThreadSP thread_sp (m_thread_wp.lock());
234             if (thread_sp)
235             {
236                 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
237                 if (bp_site_sp)
238                 {
239                     StreamString strm;
240                     // If we have just hit an internal breakpoint, and it has a kind description, print that instead of the
241                     // full breakpoint printing:
242                     if (bp_site_sp->IsInternal())
243                     {
244                         size_t num_owners = bp_site_sp->GetNumberOfOwners();
245                         for (size_t idx = 0; idx < num_owners; idx++)
246                         {
247                             const char *kind = bp_site_sp->GetOwnerAtIndex(idx)->GetBreakpoint().GetBreakpointKind();
248                             if (kind != NULL)
249                             {
250                                 m_description.assign (kind);
251                                 return kind;
252                             }
253                         }
254                     }
255 
256                     strm.Printf("breakpoint ");
257                     bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
258                     m_description.swap (strm.GetString());
259                 }
260                 else
261                 {
262                     StreamString strm;
263                     if (m_break_id != LLDB_INVALID_BREAK_ID)
264                     {
265                         BreakpointSP break_sp = thread_sp->GetProcess()->GetTarget().GetBreakpointByID(m_break_id);
266                         if (break_sp)
267                         {
268                             if (break_sp->IsInternal())
269                             {
270                                 const char *kind = break_sp->GetBreakpointKind();
271                                 if (kind)
272                                     strm.Printf ("internal %s breakpoint(%d).", kind, m_break_id);
273                                 else
274                                     strm.Printf ("internal breakpoint(%d).", m_break_id);
275                             }
276                             else
277                             {
278                                 strm.Printf ("breakpoint %d.", m_break_id);
279                             }
280                         }
281                         else
282                         {
283                             if (m_was_one_shot)
284                                 strm.Printf ("one-shot breakpoint %d", m_break_id);
285                             else
286                                 strm.Printf ("breakpoint %d which has been deleted.", m_break_id);
287                         }
288                     }
289                     else if (m_address == LLDB_INVALID_ADDRESS)
290                         strm.Printf("breakpoint site %" PRIi64 " which has been deleted - unknown address", m_value);
291                     else
292                         strm.Printf("breakpoint site %" PRIi64 " which has been deleted - was at 0x%" PRIx64, m_value, m_address);
293 
294                     m_description.swap (strm.GetString());
295                 }
296             }
297         }
298         return m_description.c_str();
299     }
300 
301 protected:
302     bool
303     ShouldStop (Event *event_ptr)
304     {
305         // This just reports the work done by PerformAction or the synchronous stop.  It should
306         // only ever get called after they have had a chance to run.
307         assert (m_should_stop_is_valid);
308         return m_should_stop;
309     }
310 
311     virtual void
312     PerformAction (Event *event_ptr)
313     {
314         if (!m_should_perform_action)
315             return;
316         m_should_perform_action = false;
317 
318         ThreadSP thread_sp (m_thread_wp.lock());
319 
320         if (thread_sp)
321         {
322             Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS | LIBLLDB_LOG_STEP);
323 
324             if (!thread_sp->IsValid())
325             {
326                 // This shouldn't ever happen, but just in case, don't do more harm.
327                 if (log)
328                 {
329                     log->Printf ("PerformAction got called with an invalid thread.");
330                 }
331                 m_should_stop = true;
332                 m_should_stop_is_valid = true;
333                 return;
334             }
335 
336             BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
337 
338             if (bp_site_sp)
339             {
340                 size_t num_owners = bp_site_sp->GetNumberOfOwners();
341 
342                 if (num_owners == 0)
343                 {
344                     m_should_stop = true;
345                 }
346                 else
347                 {
348                     // We go through each location, and test first its condition.  If the condition says to stop,
349                     // then we run the callback for that location.  If that callback says to stop as well, then
350                     // we set m_should_stop to true; we are going to stop.
351                     // But we still want to give all the breakpoints whose conditions say we are going to stop a
352                     // chance to run their callbacks.
353                     // Of course if any callback restarts the target by putting "continue" in the callback, then
354                     // we're going to restart, without running the rest of the callbacks.  And in this case we will
355                     // end up not stopping even if another location said we should stop.  But that's better than not
356                     // running all the callbacks.
357 
358                     m_should_stop = false;
359 
360                     ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
361                     Process *process  = exe_ctx.GetProcessPtr();
362                     if (process->GetModIDRef().IsLastResumeForUserExpression())
363                     {
364                         // If we are in the middle of evaluating an expression, don't run asynchronous breakpoint commands or
365                         // expressions.  That could lead to infinite recursion if the command or condition re-calls the function
366                         // with this breakpoint.
367                         // TODO: We can keep a list of the breakpoints we've seen while running expressions in the nested
368                         // PerformAction calls that can arise when the action runs a function that hits another breakpoint,
369                         // and only stop running commands when we see the same breakpoint hit a second time.
370 
371                         m_should_stop_is_valid = true;
372                         if (log)
373                             log->Printf ("StopInfoBreakpoint::PerformAction - Hit a breakpoint while running an expression,"
374                                          " not running commands to avoid recursion.");
375                         bool ignoring_breakpoints = process->GetIgnoreBreakpointsInExpressions();
376                         if (ignoring_breakpoints)
377                         {
378                             m_should_stop = false;
379                             // Internal breakpoints will always stop.
380                             for (size_t j = 0; j < num_owners; j++)
381                             {
382                                 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
383                                 if (bp_loc_sp->GetBreakpoint().IsInternal())
384                                 {
385                                     m_should_stop = true;
386                                     break;
387                                 }
388                             }
389                         }
390                         else
391                         {
392                             m_should_stop = true;
393                         }
394                         if (log)
395                             log->Printf ("StopInfoBreakpoint::PerformAction - in expression, continuing: %s.",
396                                          m_should_stop ? "true" : "false");
397                         process->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: hit breakpoint while "
398                                                "running function, skipping commands and conditions to prevent recursion.");
399                         return;
400                     }
401 
402                     StoppointCallbackContext context (event_ptr, exe_ctx, false);
403 
404                     // Let's copy the breakpoint locations out of the site and store them in a local list.  That way if
405                     // one of the breakpoint actions changes the site, then we won't be operating on a bad list.
406 
407                     BreakpointLocationCollection site_locations;
408                     for (size_t j = 0; j < num_owners; j++)
409                         site_locations.Add(bp_site_sp->GetOwnerAtIndex(j));
410 
411                     for (size_t j = 0; j < num_owners; j++)
412                     {
413                         lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j);
414 
415                         // If another action disabled this breakpoint or its location, then don't run the actions.
416                         if (!bp_loc_sp->IsEnabled() || !bp_loc_sp->GetBreakpoint().IsEnabled())
417                             continue;
418 
419                         // The breakpoint site may have many locations associated with it, not all of them valid for
420                         // this thread.  Skip the ones that aren't:
421                         if (!bp_loc_sp->ValidForThisThread(thread_sp.get()))
422                         {
423                             if (log)
424                             {
425                                 StreamString s;
426                                 bp_loc_sp->GetDescription(&s, eDescriptionLevelBrief);
427                                 log->Printf ("Breakpoint %s hit on thread 0x%llx but it was not for this thread, continuing.",
428                                              s.GetData(),
429                                              static_cast<unsigned long long>(thread_sp->GetID()));
430                             }
431                             continue;
432                         }
433                         // First run the condition for the breakpoint.  If that says we should stop, then we'll run
434                         // the callback for the breakpoint.  If the callback says we shouldn't stop that will win.
435 
436                         if (bp_loc_sp->GetConditionText() != NULL)
437                         {
438                             Error condition_error;
439                             bool condition_says_stop = bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
440 
441                             if (!condition_error.Success())
442                             {
443                                 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
444                                 StreamSP error_sp = debugger.GetAsyncErrorStream ();
445                                 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
446                                 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
447                                 error_sp->Printf (": \"%s\"",
448                                                   bp_loc_sp->GetConditionText());
449                                 error_sp->EOL();
450                                 const char *err_str = condition_error.AsCString("<Unknown Error>");
451                                 if (log)
452                                     log->Printf("Error evaluating condition: \"%s\"\n", err_str);
453 
454                                 error_sp->PutCString (err_str);
455                                 error_sp->EOL();
456                                 error_sp->Flush();
457                             }
458                             else
459                             {
460                                 if (log)
461                                 {
462                                     StreamString s;
463                                     bp_loc_sp->GetDescription(&s, eDescriptionLevelBrief);
464                                     log->Printf ("Condition evaluated for breakpoint %s on thread 0x%llx conditon_says_stop: %i.",
465                                                  s.GetData(),
466                                                  static_cast<unsigned long long>(thread_sp->GetID()),
467                                                  condition_says_stop);
468                                 }
469                                 if (!condition_says_stop)
470                                     continue;
471                             }
472                         }
473 
474                         bool callback_says_stop;
475 
476                         // FIXME: For now the callbacks have to run in async mode - the first time we restart we need
477                         // to get out of there.  So set it here.
478                         // When we figure out how to nest breakpoint hits then this will change.
479 
480                         Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
481                         bool old_async = debugger.GetAsyncExecution();
482                         debugger.SetAsyncExecution (true);
483 
484                         callback_says_stop = bp_loc_sp->InvokeCallback (&context);
485 
486                         debugger.SetAsyncExecution (old_async);
487 
488                         if (callback_says_stop)
489                             m_should_stop = true;
490 
491                         // If we are going to stop for this breakpoint, then remove the breakpoint.
492                         if (callback_says_stop && bp_loc_sp && bp_loc_sp->GetBreakpoint().IsOneShot())
493                         {
494                             thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID (bp_loc_sp->GetBreakpoint().GetID());
495                         }
496 
497                         // Also make sure that the callback hasn't continued the target.
498                         // If it did, when we'll set m_should_start to false and get out of here.
499                         if (HasTargetRunSinceMe ())
500                         {
501                             m_should_stop = false;
502                             break;
503                         }
504                     }
505                 }
506                 // We've figured out what this stop wants to do, so mark it as valid so we don't compute it again.
507                 m_should_stop_is_valid = true;
508 
509             }
510             else
511             {
512                 m_should_stop = true;
513                 m_should_stop_is_valid = true;
514                 Log * log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
515 
516                 if (log_process)
517                     log_process->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
518             }
519             if (log)
520                 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
521         }
522     }
523 
524 private:
525     std::string m_description;
526     bool m_should_stop;
527     bool m_should_stop_is_valid;
528     bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
529                                   // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
530     lldb::addr_t m_address;       // We use this to capture the breakpoint site address when we create the StopInfo,
531                                   // in case somebody deletes it between the time the StopInfo is made and the
532                                   // description is asked for.
533     lldb::break_id_t m_break_id;
534     bool m_was_one_shot;
535 };
536 
537 
538 //----------------------------------------------------------------------
539 // StopInfoWatchpoint
540 //----------------------------------------------------------------------
541 
542 class StopInfoWatchpoint : public StopInfo
543 {
544 public:
545     // Make sure watchpoint is properly disabled and subsequently enabled while performing watchpoint actions.
546     class WatchpointSentry {
547     public:
548         WatchpointSentry(Process *p, Watchpoint *w):
549             process(p),
550             watchpoint(w)
551         {
552             if (process && watchpoint)
553             {
554                 const bool notify = false;
555                 watchpoint->TurnOnEphemeralMode();
556                 process->DisableWatchpoint(watchpoint, notify);
557             }
558         }
559         ~WatchpointSentry()
560         {
561             if (process && watchpoint)
562             {
563                 if (!watchpoint->IsDisabledDuringEphemeralMode())
564                 {
565                     const bool notify = false;
566                     process->EnableWatchpoint(watchpoint, notify);
567                 }
568                 watchpoint->TurnOffEphemeralMode();
569             }
570         }
571     private:
572         Process *process;
573         Watchpoint *watchpoint;
574     };
575 
576     StopInfoWatchpoint (Thread &thread, break_id_t watch_id) :
577         StopInfo(thread, watch_id),
578         m_description(),
579         m_should_stop(false),
580         m_should_stop_is_valid(false)
581     {
582     }
583 
584     virtual ~StopInfoWatchpoint ()
585     {
586     }
587 
588     virtual StopReason
589     GetStopReason () const
590     {
591         return eStopReasonWatchpoint;
592     }
593 
594     virtual const char *
595     GetDescription ()
596     {
597         if (m_description.empty())
598         {
599             StreamString strm;
600             strm.Printf("watchpoint %" PRIi64, m_value);
601             m_description.swap (strm.GetString());
602         }
603         return m_description.c_str();
604     }
605 
606 protected:
607     virtual bool
608     ShouldStopSynchronous (Event *event_ptr)
609     {
610         // ShouldStop() method is idempotent and should not affect hit count.
611         // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
612         // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
613         // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
614         // StopInfoWatchpoint::ShouldStop() and
615         // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
616         // StopInfoWatchpoint::PerformAction().
617         if (m_should_stop_is_valid)
618             return m_should_stop;
619 
620         ThreadSP thread_sp (m_thread_wp.lock());
621         if (thread_sp)
622         {
623             WatchpointSP wp_sp (thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
624             if (wp_sp)
625             {
626                 // Check if we should stop at a watchpoint.
627                 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
628                 StoppointCallbackContext context (event_ptr, exe_ctx, true);
629                 m_should_stop = wp_sp->ShouldStop (&context);
630             }
631             else
632             {
633                 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
634 
635                 if (log)
636                     log->Printf ("Process::%s could not find watchpoint location id: %" PRId64 "...",
637                                  __FUNCTION__, GetValue());
638 
639                 m_should_stop = true;
640             }
641         }
642         m_should_stop_is_valid = true;
643         return m_should_stop;
644     }
645 
646     bool
647     ShouldStop (Event *event_ptr)
648     {
649         // This just reports the work done by PerformAction or the synchronous stop.  It should
650         // only ever get called after they have had a chance to run.
651         assert (m_should_stop_is_valid);
652         return m_should_stop;
653     }
654 
655     virtual void
656     PerformAction (Event *event_ptr)
657     {
658         Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS);
659         // We're going to calculate if we should stop or not in some way during the course of
660         // this code.  Also by default we're going to stop, so set that here.
661         m_should_stop = true;
662 
663         ThreadSP thread_sp (m_thread_wp.lock());
664         if (thread_sp)
665         {
666 
667             WatchpointSP wp_sp (thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
668             if (wp_sp)
669             {
670                 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
671                 Process* process = exe_ctx.GetProcessPtr();
672 
673                 // This sentry object makes sure the current watchpoint is disabled while performing watchpoint actions,
674                 // and it is then enabled after we are finished.
675                 WatchpointSentry sentry(process, wp_sp.get());
676 
677                 {
678                     // check if this process is running on an architecture where watchpoints trigger
679                     // before the associated instruction runs. if so, disable the WP, single-step and then
680                     // re-enable the watchpoint
681                     if (process)
682                     {
683                         uint32_t num;
684                         bool wp_triggers_after;
685                         if (process->GetWatchpointSupportInfo(num, wp_triggers_after).Success())
686                         {
687                             if (!wp_triggers_after)
688                             {
689                                 StopInfoSP stored_stop_info_sp = thread_sp->GetStopInfo();
690                                 assert (stored_stop_info_sp.get() == this);
691 
692                                 ThreadPlanSP new_plan_sp(thread_sp->QueueThreadPlanForStepSingleInstruction(false, // step-over
693                                                                                                         false,     // abort_other_plans
694                                                                                                         true));    // stop_other_threads
695                                 new_plan_sp->SetIsMasterPlan (true);
696                                 new_plan_sp->SetOkayToDiscard (false);
697                                 new_plan_sp->SetPrivate (true);
698                                 process->GetThreadList().SetSelectedThreadByID (thread_sp->GetID());
699                                 process->Resume ();
700                                 process->WaitForProcessToStop (NULL);
701                                 process->GetThreadList().SetSelectedThreadByID (thread_sp->GetID());
702                                 thread_sp->SetStopInfo(stored_stop_info_sp);
703                             }
704                         }
705                     }
706                 }
707 
708                 if (m_should_stop && wp_sp->GetConditionText() != NULL)
709                 {
710                     // We need to make sure the user sees any parse errors in their condition, so we'll hook the
711                     // constructor errors up to the debugger's Async I/O.
712                     ExpressionResults result_code;
713                     EvaluateExpressionOptions expr_options;
714                     expr_options.SetUnwindOnError(true);
715                     expr_options.SetIgnoreBreakpoints(true);
716                     ValueObjectSP result_value_sp;
717                     Error error;
718                     result_code = ClangUserExpression::Evaluate (exe_ctx,
719                                                                  expr_options,
720                                                                  wp_sp->GetConditionText(),
721                                                                  NULL,
722                                                                  result_value_sp,
723                                                                  error);
724                     if (result_code == eExpressionCompleted)
725                     {
726                         if (result_value_sp)
727                         {
728                             Scalar scalar_value;
729                             if (result_value_sp->ResolveValue (scalar_value))
730                             {
731                                 if (scalar_value.ULongLong(1) == 0)
732                                 {
733                                     // We have been vetoed.  This takes precedence over querying
734                                     // the watchpoint whether it should stop (aka ignore count and
735                                     // friends).  See also StopInfoWatchpoint::ShouldStop() as well
736                                     // as Process::ProcessEventData::DoOnRemoval().
737                                     m_should_stop = false;
738                                 }
739                                 else
740                                     m_should_stop = true;
741                                 if (log)
742                                     log->Printf("Condition successfully evaluated, result is %s.\n",
743                                                 m_should_stop ? "true" : "false");
744                             }
745                             else
746                             {
747                                 m_should_stop = true;
748                                 if (log)
749                                     log->Printf("Failed to get an integer result from the expression.");
750                             }
751                         }
752                     }
753                     else
754                     {
755                         Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
756                         StreamSP error_sp = debugger.GetAsyncErrorStream ();
757                         error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
758                         wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
759                         error_sp->Printf (": \"%s\"",
760                                           wp_sp->GetConditionText());
761                         error_sp->EOL();
762                         const char *err_str = error.AsCString("<Unknown Error>");
763                         if (log)
764                             log->Printf("Error evaluating condition: \"%s\"\n", err_str);
765 
766                         error_sp->PutCString (err_str);
767                         error_sp->EOL();
768                         error_sp->Flush();
769                         // If the condition fails to be parsed or run, we should stop.
770                         m_should_stop = true;
771                     }
772                 }
773 
774                 // If the condition says to stop, we run the callback to further decide whether to stop.
775                 if (m_should_stop)
776                 {
777                     StoppointCallbackContext context (event_ptr, exe_ctx, false);
778                     bool stop_requested = wp_sp->InvokeCallback (&context);
779                     // Also make sure that the callback hasn't continued the target.
780                     // If it did, when we'll set m_should_stop to false and get out of here.
781                     if (HasTargetRunSinceMe ())
782                         m_should_stop = false;
783 
784                     if (m_should_stop && !stop_requested)
785                     {
786                         // We have been vetoed by the callback mechanism.
787                         m_should_stop = false;
788                     }
789                 }
790                 // Finally, if we are going to stop, print out the new & old values:
791                 if (m_should_stop)
792                 {
793                     wp_sp->CaptureWatchedValue(exe_ctx);
794 
795                     Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
796                     StreamSP output_sp = debugger.GetAsyncOutputStream ();
797                     wp_sp->DumpSnapshots(output_sp.get());
798                     output_sp->EOL();
799                     output_sp->Flush();
800                 }
801 
802             }
803             else
804             {
805                 Log * log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
806 
807                 if (log_process)
808                     log_process->Printf ("Process::%s could not find watchpoint id: %" PRId64 "...", __FUNCTION__, m_value);
809             }
810             if (log)
811                 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
812 
813             m_should_stop_is_valid = true;
814         }
815     }
816 
817 private:
818     std::string m_description;
819     bool m_should_stop;
820     bool m_should_stop_is_valid;
821 };
822 
823 
824 
825 //----------------------------------------------------------------------
826 // StopInfoUnixSignal
827 //----------------------------------------------------------------------
828 
829 class StopInfoUnixSignal : public StopInfo
830 {
831 public:
832 
833     StopInfoUnixSignal (Thread &thread, int signo) :
834         StopInfo (thread, signo)
835     {
836     }
837 
838     virtual ~StopInfoUnixSignal ()
839     {
840     }
841 
842 
843     virtual StopReason
844     GetStopReason () const
845     {
846         return eStopReasonSignal;
847     }
848 
849     virtual bool
850     ShouldStopSynchronous (Event *event_ptr)
851     {
852         ThreadSP thread_sp (m_thread_wp.lock());
853         if (thread_sp)
854             return thread_sp->GetProcess()->GetUnixSignals().GetShouldStop (m_value);
855         return false;
856     }
857 
858     virtual bool
859     ShouldStop (Event *event_ptr)
860     {
861         ThreadSP thread_sp (m_thread_wp.lock());
862         if (thread_sp)
863             return thread_sp->GetProcess()->GetUnixSignals().GetShouldStop (m_value);
864         return false;
865     }
866 
867 
868     // If should stop returns false, check if we should notify of this event
869     virtual bool
870     DoShouldNotify (Event *event_ptr)
871     {
872         ThreadSP thread_sp (m_thread_wp.lock());
873         if (thread_sp)
874         {
875             bool should_notify = thread_sp->GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
876             if (should_notify)
877             {
878                 StreamString strm;
879                 strm.Printf ("thread %d received signal: %s",
880                              thread_sp->GetIndexID(),
881                              thread_sp->GetProcess()->GetUnixSignals().GetSignalAsCString (m_value));
882                 Process::ProcessEventData::AddRestartedReason(event_ptr, strm.GetData());
883             }
884             return should_notify;
885         }
886         return true;
887     }
888 
889 
890     virtual void
891     WillResume (lldb::StateType resume_state)
892     {
893         ThreadSP thread_sp (m_thread_wp.lock());
894         if (thread_sp)
895         {
896             if (thread_sp->GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
897                 thread_sp->SetResumeSignal(m_value);
898         }
899     }
900 
901     virtual const char *
902     GetDescription ()
903     {
904         if (m_description.empty())
905         {
906             ThreadSP thread_sp (m_thread_wp.lock());
907             if (thread_sp)
908             {
909                 StreamString strm;
910                 const char *signal_name = thread_sp->GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
911                 if (signal_name)
912                     strm.Printf("signal %s", signal_name);
913                 else
914                     strm.Printf("signal %" PRIi64, m_value);
915                 m_description.swap (strm.GetString());
916             }
917         }
918         return m_description.c_str();
919     }
920 };
921 
922 //----------------------------------------------------------------------
923 // StopInfoTrace
924 //----------------------------------------------------------------------
925 
926 class StopInfoTrace : public StopInfo
927 {
928 public:
929 
930     StopInfoTrace (Thread &thread) :
931         StopInfo (thread, LLDB_INVALID_UID)
932     {
933     }
934 
935     virtual ~StopInfoTrace ()
936     {
937     }
938 
939     virtual StopReason
940     GetStopReason () const
941     {
942         return eStopReasonTrace;
943     }
944 
945     virtual const char *
946     GetDescription ()
947     {
948         if (m_description.empty())
949         return "trace";
950         else
951             return m_description.c_str();
952     }
953 };
954 
955 
956 //----------------------------------------------------------------------
957 // StopInfoException
958 //----------------------------------------------------------------------
959 
960 class StopInfoException : public StopInfo
961 {
962 public:
963 
964     StopInfoException (Thread &thread, const char *description) :
965         StopInfo (thread, LLDB_INVALID_UID)
966     {
967         if (description)
968             SetDescription (description);
969     }
970 
971     virtual
972     ~StopInfoException ()
973     {
974     }
975 
976     virtual StopReason
977     GetStopReason () const
978     {
979         return eStopReasonException;
980     }
981 
982     virtual const char *
983     GetDescription ()
984     {
985         if (m_description.empty())
986             return "exception";
987         else
988             return m_description.c_str();
989     }
990 };
991 
992 
993 //----------------------------------------------------------------------
994 // StopInfoThreadPlan
995 //----------------------------------------------------------------------
996 
997 class StopInfoThreadPlan : public StopInfo
998 {
999 public:
1000 
1001     StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp, ClangExpressionVariableSP &expression_variable_sp) :
1002         StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
1003         m_plan_sp (plan_sp),
1004         m_return_valobj_sp (return_valobj_sp),
1005         m_expression_variable_sp (expression_variable_sp)
1006     {
1007     }
1008 
1009     virtual ~StopInfoThreadPlan ()
1010     {
1011     }
1012 
1013     virtual StopReason
1014     GetStopReason () const
1015     {
1016         return eStopReasonPlanComplete;
1017     }
1018 
1019     virtual const char *
1020     GetDescription ()
1021     {
1022         if (m_description.empty())
1023         {
1024             StreamString strm;
1025             m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
1026             m_description.swap (strm.GetString());
1027         }
1028         return m_description.c_str();
1029     }
1030 
1031     ValueObjectSP
1032     GetReturnValueObject()
1033     {
1034         return m_return_valobj_sp;
1035     }
1036 
1037     ClangExpressionVariableSP
1038     GetExpressionVariable()
1039     {
1040         return m_expression_variable_sp;
1041     }
1042 
1043 protected:
1044     virtual bool
1045     ShouldStop (Event *event_ptr)
1046     {
1047         if (m_plan_sp)
1048             return m_plan_sp->ShouldStop(event_ptr);
1049         else
1050             return StopInfo::ShouldStop(event_ptr);
1051     }
1052 
1053 private:
1054     ThreadPlanSP m_plan_sp;
1055     ValueObjectSP m_return_valobj_sp;
1056     ClangExpressionVariableSP m_expression_variable_sp;
1057 };
1058 
1059 class StopInfoExec : public StopInfo
1060 {
1061 public:
1062 
1063     StopInfoExec (Thread &thread) :
1064         StopInfo (thread, LLDB_INVALID_UID),
1065         m_performed_action (false)
1066     {
1067     }
1068 
1069     virtual
1070     ~StopInfoExec ()
1071     {
1072     }
1073 
1074     virtual StopReason
1075     GetStopReason () const
1076     {
1077         return eStopReasonExec;
1078     }
1079 
1080     virtual const char *
1081     GetDescription ()
1082     {
1083         return "exec";
1084     }
1085 protected:
1086 
1087     virtual void
1088     PerformAction (Event *event_ptr)
1089     {
1090         // Only perform the action once
1091         if (m_performed_action)
1092             return;
1093         m_performed_action = true;
1094         ThreadSP thread_sp (m_thread_wp.lock());
1095         if (thread_sp)
1096             thread_sp->GetProcess()->DidExec();
1097     }
1098 
1099     bool m_performed_action;
1100 };
1101 
1102 } // namespace lldb_private
1103 
1104 StopInfoSP
1105 StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
1106 {
1107     return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
1108 }
1109 
1110 StopInfoSP
1111 StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
1112 {
1113     return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
1114 }
1115 
1116 StopInfoSP
1117 StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
1118 {
1119     return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
1120 }
1121 
1122 StopInfoSP
1123 StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo)
1124 {
1125     return StopInfoSP (new StopInfoUnixSignal (thread, signo));
1126 }
1127 
1128 StopInfoSP
1129 StopInfo::CreateStopReasonToTrace (Thread &thread)
1130 {
1131     return StopInfoSP (new StopInfoTrace (thread));
1132 }
1133 
1134 StopInfoSP
1135 StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp,
1136                                     ValueObjectSP return_valobj_sp,
1137                                     ClangExpressionVariableSP expression_variable_sp)
1138 {
1139     return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp, expression_variable_sp));
1140 }
1141 
1142 StopInfoSP
1143 StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
1144 {
1145     return StopInfoSP (new StopInfoException (thread, description));
1146 }
1147 
1148 StopInfoSP
1149 StopInfo::CreateStopReasonWithExec (Thread &thread)
1150 {
1151     return StopInfoSP (new StopInfoExec (thread));
1152 }
1153 
1154 ValueObjectSP
1155 StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
1156 {
1157     if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
1158     {
1159         StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1160         return plan_stop_info->GetReturnValueObject();
1161     }
1162     else
1163         return ValueObjectSP();
1164 }
1165 
1166 ClangExpressionVariableSP
1167 StopInfo::GetExpressionVariable(StopInfoSP &stop_info_sp)
1168 {
1169     if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
1170     {
1171         StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1172         return plan_stop_info->GetExpressionVariable();
1173     }
1174     else
1175         return ClangExpressionVariableSP();
1176 }
1177