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 {
45 }
46 
47 bool
48 StopInfo::IsValid () const
49 {
50     ThreadSP thread_sp (m_thread_wp.lock());
51     if (thread_sp)
52         return thread_sp->GetProcess()->GetStopID() == m_stop_id;
53     return false;
54 }
55 
56 void
57 StopInfo::MakeStopInfoValid ()
58 {
59     ThreadSP thread_sp (m_thread_wp.lock());
60     if (thread_sp)
61     {
62         m_stop_id = thread_sp->GetProcess()->GetStopID();
63         m_resume_id = thread_sp->GetProcess()->GetResumeID();
64     }
65 }
66 
67 bool
68 StopInfo::HasTargetRunSinceMe ()
69 {
70     ThreadSP thread_sp (m_thread_wp.lock());
71 
72     if (thread_sp)
73     {
74         lldb::StateType ret_type = thread_sp->GetProcess()->GetPrivateState();
75         if (ret_type == eStateRunning)
76         {
77             return true;
78         }
79         else if (ret_type == eStateStopped)
80         {
81             // This is a little tricky.  We want to count "run and stopped again before you could
82             // ask this question as a "TRUE" answer to HasTargetRunSinceMe.  But we don't want to
83             // include any running of the target done for expressions.  So we track both resumes,
84             // and resumes caused by expressions, and check if there are any resumes NOT caused
85             // by expressions.
86 
87             uint32_t curr_resume_id = thread_sp->GetProcess()->GetResumeID();
88             uint32_t last_user_expression_id = thread_sp->GetProcess()->GetLastUserExpressionResumeID ();
89             if (curr_resume_id == m_resume_id)
90             {
91                 return false;
92             }
93             else if (curr_resume_id > last_user_expression_id)
94             {
95                 return true;
96             }
97         }
98     }
99     return false;
100 }
101 
102 //----------------------------------------------------------------------
103 // StopInfoBreakpoint
104 //----------------------------------------------------------------------
105 
106 namespace lldb_private
107 {
108 class StopInfoBreakpoint : public StopInfo
109 {
110 public:
111 
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::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
323 
324             if (!thread_sp->IsValid())
325             {
326                 // This shouldn't ever happen, but just in case, don't do more harm.
327                 log->Printf ("PerformAction got called with an invalid thread.");
328                 m_should_stop = true;
329                 m_should_stop_is_valid = true;
330                 return;
331             }
332 
333             BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
334 
335             if (bp_site_sp)
336             {
337                 size_t num_owners = bp_site_sp->GetNumberOfOwners();
338 
339                 if (num_owners == 0)
340                 {
341                     m_should_stop = true;
342                 }
343                 else
344                 {
345                     // We go through each location, and test first its condition.  If the condition says to stop,
346                     // then we run the callback for that location.  If that callback says to stop as well, then
347                     // we set m_should_stop to true; we are going to stop.
348                     // But we still want to give all the breakpoints whose conditions say we are going to stop a
349                     // chance to run their callbacks.
350                     // Of course if any callback restarts the target by putting "continue" in the callback, then
351                     // we're going to restart, without running the rest of the callbacks.  And in this case we will
352                     // end up not stopping even if another location said we should stop.  But that's better than not
353                     // running all the callbacks.
354 
355                     m_should_stop = false;
356 
357                     ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
358                     Process *process  = exe_ctx.GetProcessPtr();
359                     if (process->GetModIDRef().IsLastResumeForUserExpression())
360                     {
361                         // If we are in the middle of evaluating an expression, don't run asynchronous breakpoint commands or
362                         // expressions.  That could lead to infinite recursion if the command or condition re-calls the function
363                         // with this breakpoint.
364                         // TODO: We can keep a list of the breakpoints we've seen while running expressions in the nested
365                         // PerformAction calls that can arise when the action runs a function that hits another breakpoint,
366                         // and only stop running commands when we see the same breakpoint hit a second time.
367 
368                         m_should_stop_is_valid = true;
369                         if (log)
370                             log->Printf ("StopInfoBreakpoint::PerformAction - Hit a breakpoint while running an expression,"
371                                          " not running commands to avoid recursion.");
372                         bool ignoring_breakpoints = process->GetIgnoreBreakpointsInExpressions();
373                         if (ignoring_breakpoints)
374                         {
375                             m_should_stop = false;
376                             // Internal breakpoints will always stop.
377                             for (size_t j = 0; j < num_owners; j++)
378                             {
379                                 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
380                                 if (bp_loc_sp->GetBreakpoint().IsInternal())
381                                 {
382                                     m_should_stop = true;
383                                     break;
384                                 }
385                             }
386                         }
387                         else
388                         {
389                             m_should_stop = true;
390                         }
391                         if (log)
392                             log->Printf ("StopInfoBreakpoint::PerformAction - in expression, continuing: %s.",
393                                          m_should_stop ? "true" : "false");
394                         process->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: hit breakpoint while "
395                                                "running function, skipping commands and conditions to prevent recursion.");
396                         return;
397                     }
398 
399                     StoppointCallbackContext context (event_ptr, exe_ctx, false);
400 
401                     // Let's copy the breakpoint locations out of the site and store them in a local list.  That way if
402                     // one of the breakpoint actions changes the site, then we won't be operating on a bad list.
403 
404                     BreakpointLocationCollection site_locations;
405                     for (size_t j = 0; j < num_owners; j++)
406                         site_locations.Add(bp_site_sp->GetOwnerAtIndex(j));
407 
408                     for (size_t j = 0; j < num_owners; j++)
409                     {
410                         lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j);
411 
412                         // If another action disabled this breakpoint or its location, then don't run the actions.
413                         if (!bp_loc_sp->IsEnabled() || !bp_loc_sp->GetBreakpoint().IsEnabled())
414                             continue;
415 
416                         // The breakpoint site may have many locations associated with it, not all of them valid for
417                         // this thread.  Skip the ones that aren't:
418                         if (!bp_loc_sp->ValidForThisThread(thread_sp.get()))
419                             continue;
420 
421                         // First run the condition for the breakpoint.  If that says we should stop, then we'll run
422                         // the callback for the breakpoint.  If the callback says we shouldn't stop that will win.
423 
424                         if (bp_loc_sp->GetConditionText() != NULL)
425                         {
426                             Error condition_error;
427                             bool condition_says_stop = bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
428 
429                             if (!condition_error.Success())
430                             {
431                                 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
432                                 StreamSP error_sp = debugger.GetAsyncErrorStream ();
433                                 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
434                                 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
435                                 error_sp->Printf (": \"%s\"",
436                                                   bp_loc_sp->GetConditionText());
437                                 error_sp->EOL();
438                                 const char *err_str = condition_error.AsCString("<Unknown Error>");
439                                 if (log)
440                                     log->Printf("Error evaluating condition: \"%s\"\n", err_str);
441 
442                                 error_sp->PutCString (err_str);
443                                 error_sp->EOL();
444                                 error_sp->Flush();
445                                 // If the condition fails to be parsed or run, we should stop.
446                                 condition_says_stop = true;
447                             }
448                             else
449                             {
450                                 if (!condition_says_stop)
451                                     continue;
452                             }
453                         }
454 
455                         bool callback_says_stop;
456 
457                         // FIXME: For now the callbacks have to run in async mode - the first time we restart we need
458                         // to get out of there.  So set it here.
459                         // When we figure out how to nest breakpoint hits then this will change.
460 
461                         Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
462                         bool old_async = debugger.GetAsyncExecution();
463                         debugger.SetAsyncExecution (true);
464 
465                         callback_says_stop = bp_loc_sp->InvokeCallback (&context);
466 
467                         debugger.SetAsyncExecution (old_async);
468 
469                         if (callback_says_stop)
470                             m_should_stop = true;
471 
472                         // If we are going to stop for this breakpoint, then remove the breakpoint.
473                         if (callback_says_stop && bp_loc_sp && bp_loc_sp->GetBreakpoint().IsOneShot())
474                         {
475                             thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID (bp_loc_sp->GetBreakpoint().GetID());
476                         }
477 
478                         // Also make sure that the callback hasn't continued the target.
479                         // If it did, when we'll set m_should_start to false and get out of here.
480                         if (HasTargetRunSinceMe ())
481                         {
482                             m_should_stop = false;
483                             break;
484                         }
485                     }
486                 }
487                 // We've figured out what this stop wants to do, so mark it as valid so we don't compute it again.
488                 m_should_stop_is_valid = true;
489 
490             }
491             else
492             {
493                 m_should_stop = true;
494                 m_should_stop_is_valid = true;
495                 Log * log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
496 
497                 if (log_process)
498                     log_process->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
499             }
500             if (log)
501                 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
502         }
503     }
504 
505 private:
506     std::string m_description;
507     bool m_should_stop;
508     bool m_should_stop_is_valid;
509     bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
510                                   // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
511     lldb::addr_t m_address;       // We use this to capture the breakpoint site address when we create the StopInfo,
512                                   // in case somebody deletes it between the time the StopInfo is made and the
513                                   // description is asked for.
514     lldb::break_id_t m_break_id;
515     bool m_was_one_shot;
516 };
517 
518 
519 //----------------------------------------------------------------------
520 // StopInfoWatchpoint
521 //----------------------------------------------------------------------
522 
523 class StopInfoWatchpoint : public StopInfo
524 {
525 public:
526     // Make sure watchpoint is properly disabled and subsequently enabled while performing watchpoint actions.
527     class WatchpointSentry {
528     public:
529         WatchpointSentry(Process *p, Watchpoint *w):
530             process(p),
531             watchpoint(w)
532         {
533             if (process && watchpoint)
534             {
535                 const bool notify = false;
536                 watchpoint->TurnOnEphemeralMode();
537                 process->DisableWatchpoint(watchpoint, notify);
538             }
539         }
540         ~WatchpointSentry()
541         {
542             if (process && watchpoint)
543             {
544                 if (!watchpoint->IsDisabledDuringEphemeralMode())
545                 {
546                     const bool notify = false;
547                     process->EnableWatchpoint(watchpoint, notify);
548                 }
549                 watchpoint->TurnOffEphemeralMode();
550             }
551         }
552     private:
553         Process *process;
554         Watchpoint *watchpoint;
555     };
556 
557     StopInfoWatchpoint (Thread &thread, break_id_t watch_id) :
558         StopInfo(thread, watch_id),
559         m_description(),
560         m_should_stop(false),
561         m_should_stop_is_valid(false)
562     {
563     }
564 
565     virtual ~StopInfoWatchpoint ()
566     {
567     }
568 
569     virtual StopReason
570     GetStopReason () const
571     {
572         return eStopReasonWatchpoint;
573     }
574 
575     virtual const char *
576     GetDescription ()
577     {
578         if (m_description.empty())
579         {
580             StreamString strm;
581             strm.Printf("watchpoint %" PRIi64, m_value);
582             m_description.swap (strm.GetString());
583         }
584         return m_description.c_str();
585     }
586 
587 protected:
588     virtual bool
589     ShouldStopSynchronous (Event *event_ptr)
590     {
591         // ShouldStop() method is idempotent and should not affect hit count.
592         // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
593         // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
594         // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
595         // StopInfoWatchpoint::ShouldStop() and
596         // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
597         // StopInfoWatchpoint::PerformAction().
598         if (m_should_stop_is_valid)
599             return m_should_stop;
600 
601         ThreadSP thread_sp (m_thread_wp.lock());
602         if (thread_sp)
603         {
604             WatchpointSP wp_sp (thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
605             if (wp_sp)
606             {
607                 // Check if we should stop at a watchpoint.
608                 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
609                 StoppointCallbackContext context (event_ptr, exe_ctx, true);
610                 m_should_stop = wp_sp->ShouldStop (&context);
611             }
612             else
613             {
614                 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
615 
616                 if (log)
617                     log->Printf ("Process::%s could not find watchpoint location id: %" PRId64 "...",
618                                  __FUNCTION__, GetValue());
619 
620                 m_should_stop = true;
621             }
622         }
623         m_should_stop_is_valid = true;
624         return m_should_stop;
625     }
626 
627     bool
628     ShouldStop (Event *event_ptr)
629     {
630         // This just reports the work done by PerformAction or the synchronous stop.  It should
631         // only ever get called after they have had a chance to run.
632         assert (m_should_stop_is_valid);
633         return m_should_stop;
634     }
635 
636     virtual void
637     PerformAction (Event *event_ptr)
638     {
639         Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS);
640         // We're going to calculate if we should stop or not in some way during the course of
641         // this code.  Also by default we're going to stop, so set that here.
642         m_should_stop = true;
643 
644         ThreadSP thread_sp (m_thread_wp.lock());
645         if (thread_sp)
646         {
647 
648             WatchpointSP wp_sp (thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
649             if (wp_sp)
650             {
651                 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
652                 Process* process = exe_ctx.GetProcessPtr();
653 
654                 // This sentry object makes sure the current watchpoint is disabled while performing watchpoint actions,
655                 // and it is then enabled after we are finished.
656                 WatchpointSentry sentry(process, wp_sp.get());
657 
658                 {
659                     // check if this process is running on an architecture where watchpoints trigger
660                     // before the associated instruction runs. if so, disable the WP, single-step and then
661                     // re-enable the watchpoint
662                     if (process)
663                     {
664                         uint32_t num;
665                         bool wp_triggers_after;
666                         if (process->GetWatchpointSupportInfo(num, wp_triggers_after).Success())
667                         {
668                             if (!wp_triggers_after)
669                             {
670                                 ThreadPlan *new_plan = thread_sp->QueueThreadPlanForStepSingleInstruction(false, // step-over
671                                                                                                         false, // abort_other_plans
672                                                                                                         true); // stop_other_threads
673                                 new_plan->SetIsMasterPlan (true);
674                                 new_plan->SetOkayToDiscard (false);
675                                 process->GetThreadList().SetSelectedThreadByID (thread_sp->GetID());
676                                 process->Resume ();
677                                 process->WaitForProcessToStop (NULL);
678                                 process->GetThreadList().SetSelectedThreadByID (thread_sp->GetID());
679                                 MakeStopInfoValid(); // make sure we do not fail to stop because of the single-step taken above
680                             }
681                         }
682                     }
683                 }
684 
685                 if (m_should_stop && wp_sp->GetConditionText() != NULL)
686                 {
687                     // We need to make sure the user sees any parse errors in their condition, so we'll hook the
688                     // constructor errors up to the debugger's Async I/O.
689                     ExecutionResults result_code;
690                     ValueObjectSP result_value_sp;
691                     const bool unwind_on_error = true;
692                     const bool ignore_breakpoints = true;
693                     Error error;
694                     result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
695                                                                           eExecutionPolicyOnlyWhenNeeded,
696                                                                           lldb::eLanguageTypeUnknown,
697                                                                           ClangUserExpression::eResultTypeAny,
698                                                                           unwind_on_error,
699                                                                           ignore_breakpoints,
700                                                                           wp_sp->GetConditionText(),
701                                                                           NULL,
702                                                                           result_value_sp,
703                                                                           error,
704                                                                           true,
705                                                                           ClangUserExpression::kDefaultTimeout);
706                     if (result_code == eExecutionCompleted)
707                     {
708                         if (result_value_sp)
709                         {
710                             Scalar scalar_value;
711                             if (result_value_sp->ResolveValue (scalar_value))
712                             {
713                                 if (scalar_value.ULongLong(1) == 0)
714                                 {
715                                     // We have been vetoed.  This takes precedence over querying
716                                     // the watchpoint whether it should stop (aka ignore count and
717                                     // friends).  See also StopInfoWatchpoint::ShouldStop() as well
718                                     // as Process::ProcessEventData::DoOnRemoval().
719                                     m_should_stop = false;
720                                 }
721                                 else
722                                     m_should_stop = true;
723                                 if (log)
724                                     log->Printf("Condition successfully evaluated, result is %s.\n",
725                                                 m_should_stop ? "true" : "false");
726                             }
727                             else
728                             {
729                                 m_should_stop = true;
730                                 if (log)
731                                     log->Printf("Failed to get an integer result from the expression.");
732                             }
733                         }
734                     }
735                     else
736                     {
737                         Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
738                         StreamSP error_sp = debugger.GetAsyncErrorStream ();
739                         error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
740                         wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
741                         error_sp->Printf (": \"%s\"",
742                                           wp_sp->GetConditionText());
743                         error_sp->EOL();
744                         const char *err_str = error.AsCString("<Unknown Error>");
745                         if (log)
746                             log->Printf("Error evaluating condition: \"%s\"\n", err_str);
747 
748                         error_sp->PutCString (err_str);
749                         error_sp->EOL();
750                         error_sp->Flush();
751                         // If the condition fails to be parsed or run, we should stop.
752                         m_should_stop = true;
753                     }
754                 }
755 
756                 // If the condition says to stop, we run the callback to further decide whether to stop.
757                 if (m_should_stop)
758                 {
759                     StoppointCallbackContext context (event_ptr, exe_ctx, false);
760                     bool stop_requested = wp_sp->InvokeCallback (&context);
761                     // Also make sure that the callback hasn't continued the target.
762                     // If it did, when we'll set m_should_stop to false and get out of here.
763                     if (HasTargetRunSinceMe ())
764                         m_should_stop = false;
765 
766                     if (m_should_stop && !stop_requested)
767                     {
768                         // We have been vetoed by the callback mechanism.
769                         m_should_stop = false;
770                     }
771                 }
772                 // Finally, if we are going to stop, print out the new & old values:
773                 if (m_should_stop)
774                 {
775                     wp_sp->CaptureWatchedValue(exe_ctx);
776 
777                     Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
778                     StreamSP output_sp = debugger.GetAsyncOutputStream ();
779                     wp_sp->DumpSnapshots(output_sp.get());
780                     output_sp->EOL();
781                     output_sp->Flush();
782                 }
783 
784             }
785             else
786             {
787                 Log * log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
788 
789                 if (log_process)
790                     log_process->Printf ("Process::%s could not find watchpoint id: %" PRId64 "...", __FUNCTION__, m_value);
791             }
792             if (log)
793                 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
794 
795             m_should_stop_is_valid = true;
796         }
797     }
798 
799 private:
800     std::string m_description;
801     bool m_should_stop;
802     bool m_should_stop_is_valid;
803 };
804 
805 
806 
807 //----------------------------------------------------------------------
808 // StopInfoUnixSignal
809 //----------------------------------------------------------------------
810 
811 class StopInfoUnixSignal : public StopInfo
812 {
813 public:
814 
815     StopInfoUnixSignal (Thread &thread, int signo) :
816         StopInfo (thread, signo)
817     {
818     }
819 
820     virtual ~StopInfoUnixSignal ()
821     {
822     }
823 
824 
825     virtual StopReason
826     GetStopReason () const
827     {
828         return eStopReasonSignal;
829     }
830 
831     virtual bool
832     ShouldStopSynchronous (Event *event_ptr)
833     {
834         ThreadSP thread_sp (m_thread_wp.lock());
835         if (thread_sp)
836             return thread_sp->GetProcess()->GetUnixSignals().GetShouldStop (m_value);
837         return false;
838     }
839 
840     virtual bool
841     ShouldStop (Event *event_ptr)
842     {
843         ThreadSP thread_sp (m_thread_wp.lock());
844         if (thread_sp)
845             return thread_sp->GetProcess()->GetUnixSignals().GetShouldStop (m_value);
846         return false;
847     }
848 
849 
850     // If should stop returns false, check if we should notify of this event
851     virtual bool
852     DoShouldNotify (Event *event_ptr)
853     {
854         ThreadSP thread_sp (m_thread_wp.lock());
855         if (thread_sp)
856         {
857             bool should_notify = thread_sp->GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
858             if (should_notify)
859             {
860                 StreamString strm;
861                 strm.Printf ("thread %d received signal: %s",
862                              thread_sp->GetIndexID(),
863                              thread_sp->GetProcess()->GetUnixSignals().GetSignalAsCString (m_value));
864                 Process::ProcessEventData::AddRestartedReason(event_ptr, strm.GetData());
865             }
866             return should_notify;
867         }
868         return true;
869     }
870 
871 
872     virtual void
873     WillResume (lldb::StateType resume_state)
874     {
875         ThreadSP thread_sp (m_thread_wp.lock());
876         if (thread_sp)
877         {
878             if (thread_sp->GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
879                 thread_sp->SetResumeSignal(m_value);
880         }
881     }
882 
883     virtual const char *
884     GetDescription ()
885     {
886         if (m_description.empty())
887         {
888             ThreadSP thread_sp (m_thread_wp.lock());
889             if (thread_sp)
890             {
891                 StreamString strm;
892                 const char *signal_name = thread_sp->GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
893                 if (signal_name)
894                     strm.Printf("signal %s", signal_name);
895                 else
896                     strm.Printf("signal %" PRIi64, m_value);
897                 m_description.swap (strm.GetString());
898             }
899         }
900         return m_description.c_str();
901     }
902 };
903 
904 //----------------------------------------------------------------------
905 // StopInfoTrace
906 //----------------------------------------------------------------------
907 
908 class StopInfoTrace : public StopInfo
909 {
910 public:
911 
912     StopInfoTrace (Thread &thread) :
913         StopInfo (thread, LLDB_INVALID_UID)
914     {
915     }
916 
917     virtual ~StopInfoTrace ()
918     {
919     }
920 
921     virtual StopReason
922     GetStopReason () const
923     {
924         return eStopReasonTrace;
925     }
926 
927     virtual const char *
928     GetDescription ()
929     {
930         if (m_description.empty())
931         return "trace";
932         else
933             return m_description.c_str();
934     }
935 };
936 
937 
938 //----------------------------------------------------------------------
939 // StopInfoException
940 //----------------------------------------------------------------------
941 
942 class StopInfoException : public StopInfo
943 {
944 public:
945 
946     StopInfoException (Thread &thread, const char *description) :
947         StopInfo (thread, LLDB_INVALID_UID)
948     {
949         if (description)
950             SetDescription (description);
951     }
952 
953     virtual
954     ~StopInfoException ()
955     {
956     }
957 
958     virtual StopReason
959     GetStopReason () const
960     {
961         return eStopReasonException;
962     }
963 
964     virtual const char *
965     GetDescription ()
966     {
967         if (m_description.empty())
968             return "exception";
969         else
970             return m_description.c_str();
971     }
972 };
973 
974 
975 //----------------------------------------------------------------------
976 // StopInfoThreadPlan
977 //----------------------------------------------------------------------
978 
979 class StopInfoThreadPlan : public StopInfo
980 {
981 public:
982 
983     StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp) :
984         StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
985         m_plan_sp (plan_sp),
986         m_return_valobj_sp (return_valobj_sp)
987     {
988     }
989 
990     virtual ~StopInfoThreadPlan ()
991     {
992     }
993 
994     virtual StopReason
995     GetStopReason () const
996     {
997         return eStopReasonPlanComplete;
998     }
999 
1000     virtual const char *
1001     GetDescription ()
1002     {
1003         if (m_description.empty())
1004         {
1005             StreamString strm;
1006             m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
1007             m_description.swap (strm.GetString());
1008         }
1009         return m_description.c_str();
1010     }
1011 
1012     ValueObjectSP
1013     GetReturnValueObject()
1014     {
1015         return m_return_valobj_sp;
1016     }
1017 
1018 protected:
1019     virtual bool
1020     ShouldStop (Event *event_ptr)
1021     {
1022         if (m_plan_sp)
1023             return m_plan_sp->ShouldStop(event_ptr);
1024         else
1025             return StopInfo::ShouldStop(event_ptr);
1026     }
1027 
1028 private:
1029     ThreadPlanSP m_plan_sp;
1030     ValueObjectSP m_return_valobj_sp;
1031 };
1032 
1033 class StopInfoExec : public StopInfo
1034 {
1035 public:
1036 
1037     StopInfoExec (Thread &thread) :
1038         StopInfo (thread, LLDB_INVALID_UID),
1039         m_performed_action (false)
1040     {
1041     }
1042 
1043     virtual
1044     ~StopInfoExec ()
1045     {
1046     }
1047 
1048     virtual StopReason
1049     GetStopReason () const
1050     {
1051         return eStopReasonExec;
1052     }
1053 
1054     virtual const char *
1055     GetDescription ()
1056     {
1057         return "exec";
1058     }
1059 protected:
1060 
1061     virtual void
1062     PerformAction (Event *event_ptr)
1063     {
1064         // Only perform the action once
1065         if (m_performed_action)
1066             return;
1067         m_performed_action = true;
1068         ThreadSP thread_sp (m_thread_wp.lock());
1069         if (thread_sp)
1070             thread_sp->GetProcess()->DidExec();
1071     }
1072 
1073     bool m_performed_action;
1074 };
1075 
1076 } // namespace lldb_private
1077 
1078 StopInfoSP
1079 StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
1080 {
1081     return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
1082 }
1083 
1084 StopInfoSP
1085 StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
1086 {
1087     return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
1088 }
1089 
1090 StopInfoSP
1091 StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
1092 {
1093     return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
1094 }
1095 
1096 StopInfoSP
1097 StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo)
1098 {
1099     return StopInfoSP (new StopInfoUnixSignal (thread, signo));
1100 }
1101 
1102 StopInfoSP
1103 StopInfo::CreateStopReasonToTrace (Thread &thread)
1104 {
1105     return StopInfoSP (new StopInfoTrace (thread));
1106 }
1107 
1108 StopInfoSP
1109 StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp)
1110 {
1111     return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp));
1112 }
1113 
1114 StopInfoSP
1115 StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
1116 {
1117     return StopInfoSP (new StopInfoException (thread, description));
1118 }
1119 
1120 StopInfoSP
1121 StopInfo::CreateStopReasonWithExec (Thread &thread)
1122 {
1123     return StopInfoSP (new StopInfoExec (thread));
1124 }
1125 
1126 ValueObjectSP
1127 StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
1128 {
1129     if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
1130     {
1131         StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1132         return plan_stop_info->GetReturnValueObject();
1133     }
1134     else
1135         return ValueObjectSP();
1136 }
1137