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