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