LLDB mainline
StopInfo.cpp
Go to the documentation of this file.
1//===-- StopInfo.cpp ------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <string>
10
16#include "lldb/Core/Debugger.h"
18#include "lldb/Symbol/Block.h"
19#include "lldb/Target/Process.h"
21#include "lldb/Target/Target.h"
22#include "lldb/Target/Thread.h"
27#include "lldb/Utility/Log.h"
30
31using namespace lldb;
32using namespace lldb_private;
33
34StopInfo::StopInfo(Thread &thread, uint64_t value)
35 : m_thread_wp(thread.shared_from_this()),
36 m_stop_id(thread.GetProcess()->GetStopID()),
37 m_resume_id(thread.GetProcess()->GetResumeID()), m_value(value),
40
41bool StopInfo::IsValid() const {
42 ThreadSP thread_sp(m_thread_wp.lock());
43 if (thread_sp)
44 return thread_sp->GetProcess()->GetStopID() == m_stop_id;
45 return false;
46}
47
49 ThreadSP thread_sp(m_thread_wp.lock());
50 if (thread_sp) {
51 m_stop_id = thread_sp->GetProcess()->GetStopID();
52 m_resume_id = thread_sp->GetProcess()->GetResumeID();
53 }
54}
55
57 ThreadSP thread_sp(m_thread_wp.lock());
58
59 if (thread_sp) {
60 lldb::StateType ret_type = thread_sp->GetProcess()->GetPrivateState();
61 if (ret_type == eStateRunning) {
62 return true;
63 } else if (ret_type == eStateStopped) {
64 // This is a little tricky. We want to count "run and stopped again
65 // before you could ask this question as a "TRUE" answer to
66 // HasTargetRunSinceMe. But we don't want to include any running of the
67 // target done for expressions. So we track both resumes, and resumes
68 // caused by expressions, and check if there are any resumes
69 // NOT caused
70 // by expressions.
71
72 uint32_t curr_resume_id = thread_sp->GetProcess()->GetResumeID();
73 uint32_t last_user_expression_id =
74 thread_sp->GetProcess()->GetLastUserExpressionResumeID();
75 if (curr_resume_id == m_resume_id) {
76 return false;
77 } else if (curr_resume_id > last_user_expression_id) {
78 return true;
79 }
80 }
81 }
82 return false;
83}
84
85// StopInfoBreakpoint
86
87namespace lldb_private {
89public:
90 // We use a "breakpoint preserving BreakpointLocationCollection because we
91 // may need to hand out the "breakpoint hit" list as any point, potentially
92 // after the breakpoint has been deleted. But we still need to refer to them.
101
102 StopInfoBreakpoint(Thread &thread, break_id_t break_id, bool should_stop)
103 : StopInfo(thread, break_id), m_should_stop(should_stop),
106 m_was_all_internal(false), m_was_one_shot(false),
108 StoreBPInfo();
109 }
110
111 ~StopInfoBreakpoint() override = default;
112
113 void StoreBPInfo() {
114 ThreadSP thread_sp(m_thread_wp.lock());
115 if (thread_sp) {
117 if (bp_site_sp) {
118 uint32_t num_constituents = bp_site_sp->GetNumberOfConstituents();
119 if (num_constituents == 1) {
120 BreakpointLocationSP bp_loc_sp = bp_site_sp->GetConstituentAtIndex(0);
121 if (bp_loc_sp) {
122 Breakpoint & bkpt = bp_loc_sp->GetBreakpoint();
123 m_break_id = bkpt.GetID();
124 m_was_one_shot = bkpt.IsOneShot();
126 }
127 } else {
128 m_was_all_internal = true;
129 for (uint32_t i = 0; i < num_constituents; i++) {
130 if (!bp_site_sp->GetConstituentAtIndex(i)
131 ->GetBreakpoint()
132 .IsInternal()) {
133 m_was_all_internal = false;
134 break;
135 }
136 }
137 }
138 m_address = bp_site_sp->GetLoadAddress();
139 }
140 }
141 }
142
144 ProcessSP process_sp(thread.GetProcess());
145 if (process_sp) {
147 if (bp_site_sp)
148 return bp_site_sp->ValidForThisThread(thread);
149 }
150 return false;
151 }
152
153 StopReason GetStopReason() const override { return eStopReasonBreakpoint; }
154
155 bool ShouldStopSynchronous(Event *event_ptr) override {
156 ThreadSP thread_sp(m_thread_wp.lock());
157 if (thread_sp) {
159 // Only check once if we should stop at a breakpoint
161 if (bp_site_sp) {
162 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
163 StoppointCallbackContext context(event_ptr, exe_ctx, true);
164 bp_site_sp->BumpHitCounts();
166 bp_site_sp->ShouldStop(&context, m_async_stopped_locs);
167 } else {
169
170 LLDB_LOGF(log,
171 "Process::%s could not find breakpoint site id: %" PRId64
172 "...",
173 __FUNCTION__, m_value);
174
175 m_should_stop = true;
176 }
178 }
179 return m_should_stop;
180 }
181 return false;
182 }
183
184 bool DoShouldNotify(Event *event_ptr) override {
185 return !m_was_all_internal;
186 }
187
188 const char *GetDescription() override {
189 // FIXME: only print m_async_stopped_locs.
190 if (m_description.empty()) {
191 ThreadSP thread_sp(m_thread_wp.lock());
192 if (thread_sp) {
194 if (bp_site_sp) {
195 StreamString strm;
196 // If we have just hit an internal breakpoint, and it has a kind
197 // description, print that instead of the full breakpoint printing:
198 if (bp_site_sp->IsInternal()) {
199 size_t num_constituents = bp_site_sp->GetNumberOfConstituents();
200 for (size_t idx = 0; idx < num_constituents; idx++) {
201 const char *kind = bp_site_sp->GetConstituentAtIndex(idx)
202 ->GetBreakpoint()
203 .GetBreakpointKind();
204 if (kind != nullptr) {
205 m_description.assign(kind);
206 return kind;
207 }
208 }
209 }
210
211 strm.Printf("breakpoint ");
212 m_async_stopped_locs.GetDescription(&strm, eDescriptionLevelBrief);
213 m_description = std::string(strm.GetString());
214 } else {
215 StreamString strm;
217 BreakpointSP break_sp =
218 thread_sp->GetProcess()->GetTarget().GetBreakpointByID(
219 m_break_id);
220 if (break_sp) {
221 if (break_sp->IsInternal()) {
222 const char *kind = break_sp->GetBreakpointKind();
223 if (kind)
224 strm.Printf("internal %s breakpoint(%d).", kind, m_break_id);
225 else
226 strm.Printf("internal breakpoint(%d).", m_break_id);
227 } else {
228 strm.Printf("breakpoint %d.", m_break_id);
229 }
230 } else {
231 if (m_was_one_shot)
232 strm.Printf("one-shot breakpoint %d", m_break_id);
233 else
234 strm.Printf("breakpoint %d which has been deleted.",
235 m_break_id);
236 }
237 } else if (m_address == LLDB_INVALID_ADDRESS)
238 strm.Printf("breakpoint site %" PRIi64
239 " which has been deleted - unknown address",
240 m_value);
241 else
242 strm.Printf("breakpoint site %" PRIi64
243 " which has been deleted - was at 0x%" PRIx64,
245
246 m_description = std::string(strm.GetString());
247 }
248 }
249 }
250 return m_description.c_str();
251 }
252
253 uint32_t GetStopReasonDataCount() const override {
254 size_t num_async_locs = m_async_stopped_locs.GetSize();
255 // If we have async locations, they are the ones we should report:
256 if (num_async_locs > 0)
257 return num_async_locs * 2;
258
259 // Otherwise report the number of locations at this breakpoint's site.
261 if (bp_site_sp)
262 return bp_site_sp->GetNumberOfConstituents() * 2;
263 return 0; // Breakpoint must have cleared itself...
264 }
265
266 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
267 uint32_t bp_index = idx / 2;
268 BreakpointLocationSP loc_to_report_sp;
269
270 size_t num_async_locs = m_async_stopped_locs.GetSize();
271 if (num_async_locs > 0) {
272 // GetByIndex returns an empty SP if we ask past its contents:
273 loc_to_report_sp = m_async_stopped_locs.GetByIndex(bp_index);
274 } else {
276 if (bp_site_sp)
277 loc_to_report_sp = bp_site_sp->GetConstituentAtIndex(bp_index);
278 }
279 if (loc_to_report_sp) {
280 if (idx & 1) {
281 // Odd idx, return the breakpoint location ID
282 return loc_to_report_sp->GetID();
283 } else {
284 // Even idx, return the breakpoint ID
285 return loc_to_report_sp->GetBreakpoint().GetID();
286 }
287 }
289 }
290
291 std::optional<uint32_t>
292 GetSuggestedStackFrameIndex(bool inlined_stack) override {
293 if (!inlined_stack)
294 return {};
295
296 ThreadSP thread_sp(m_thread_wp.lock());
297 if (!thread_sp)
298 return {};
300 if (!bp_site_sp)
301 return {};
302
303 return bp_site_sp->GetSuggestedStackFrameIndex();
304 }
305
306 bool ShouldShow() const override { return !m_was_all_internal; }
307
308 bool ShouldSelect() const override { return !m_was_all_internal; }
309
310protected:
311 bool ShouldStop(Event *event_ptr) override {
312 // This just reports the work done by PerformAction or the synchronous
313 // stop. It should only ever get called after they have had a chance to
314 // run.
316 return m_should_stop;
317 }
318
319 void PerformAction(Event *event_ptr) override {
321 return;
323 bool all_stopping_locs_internal = true;
324
325 ThreadSP thread_sp(m_thread_wp.lock());
326
327 if (thread_sp) {
329
330 if (!thread_sp->IsValid()) {
331 // This shouldn't ever happen, but just in case, don't do more harm.
332 LLDB_LOGF(log, "PerformAction got called with an invalid thread.");
333 m_should_stop = true;
335 return;
336 }
337
339 std::unordered_set<break_id_t> precondition_breakpoints;
340 // Breakpoints that fail their condition check are not considered to
341 // have been hit. If the only locations at this site have failed their
342 // conditions, we should change the stop-info to none. Otherwise, if we
343 // hit another breakpoint on a different thread which does stop, users
344 // will see a breakpont hit with a failed condition, which is wrong.
345 // Use this variable to tell us if that is true.
346 bool actually_hit_any_locations = false;
347 if (bp_site_sp) {
348 // Let's copy the constituents list out of the site and store them in a
349 // local list. That way if one of the breakpoint actions changes the
350 // site, then we won't be operating on a bad list.
351 BreakpointLocationCollection site_locations;
352 size_t num_constituents = m_async_stopped_locs.GetSize();
353
354 if (num_constituents == 0) {
355 m_should_stop = true;
356 actually_hit_any_locations = true; // We're going to stop, don't
357 // change the stop info.
358 } else {
359 // We go through each location, and test first its precondition -
360 // this overrides everything. Note, we only do this once per
361 // breakpoint - not once per location... Then check the condition.
362 // If the condition says to stop, then we run the callback for that
363 // location. If that callback says to stop as well, then we set
364 // m_should_stop to true; we are going to stop. But we still want to
365 // give all the breakpoints whose conditions say we are going to stop
366 // a chance to run their callbacks. Of course if any callback
367 // restarts the target by putting "continue" in the callback, then
368 // we're going to restart, without running the rest of the callbacks.
369 // And in this case we will end up not stopping even if another
370 // location said we should stop. But that's better than not running
371 // all the callbacks.
372
373 // There's one other complication here. We may have run an async
374 // breakpoint callback that said we should stop. We only want to
375 // override that if another breakpoint action says we shouldn't
376 // stop. If nobody else has an opinion, then we should stop if the
377 // async callback says we should. An example of this is the async
378 // shared library load notification breakpoint and the setting
379 // stop-on-sharedlibrary-events.
380 // We'll keep the async value in async_should_stop, and track whether
381 // anyone said we should NOT stop in actually_said_continue.
382 bool async_should_stop = false;
384 async_should_stop = m_should_stop;
385 bool actually_said_continue = false;
386
387 m_should_stop = false;
388
389 // We don't select threads as we go through them testing breakpoint
390 // conditions and running commands. So we need to set the thread for
391 // expression evaluation here:
392 ThreadList::ExpressionExecutionThreadPusher thread_pusher(thread_sp);
393
394 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
395 Process *process = exe_ctx.GetProcessPtr();
396 if (process->GetModIDRef().IsRunningExpression()) {
397 // If we are in the middle of evaluating an expression, don't run
398 // asynchronous breakpoint commands or expressions. That could
399 // lead to infinite recursion if the command or condition re-calls
400 // the function with this breakpoint.
401 // TODO: We can keep a list of the breakpoints we've seen while
402 // running expressions in the nested
403 // PerformAction calls that can arise when the action runs a
404 // function that hits another breakpoint, and only stop running
405 // commands when we see the same breakpoint hit a second time.
406
408
409 // It is possible that the user has a breakpoint at the same site
410 // as the completed plan had (e.g. user has a breakpoint
411 // on a module entry point, and `ThreadPlanCallFunction` ends
412 // also there). We can't find an internal breakpoint in the loop
413 // later because it was already removed on the plan completion.
414 // So check if the plan was completed, and stop if so.
415 if (thread_sp->CompletedPlanOverridesBreakpoint()) {
416 m_should_stop = true;
417 thread_sp->ResetStopInfo();
418 return;
419 }
420
421 LLDB_LOGF(log, "StopInfoBreakpoint::PerformAction - Hit a "
422 "breakpoint while running an expression,"
423 " not running commands to avoid recursion.");
424 bool ignoring_breakpoints =
426 // Internal breakpoints should be allowed to do their job, we
427 // can make sure they don't do anything that would cause recursive
428 // command execution:
429 if (!m_was_all_internal) {
430 m_should_stop = !ignoring_breakpoints;
431 LLDB_LOGF(log,
432 "StopInfoBreakpoint::PerformAction - in expression, "
433 "continuing: %s.",
434 m_should_stop ? "true" : "false");
436 "hit breakpoint while running function, skipping commands "
437 "and conditions to prevent recursion",
438 process->GetTarget().GetDebugger().GetID());
439 return;
440 }
441 }
442
443 StoppointCallbackContext context(event_ptr, exe_ctx, false);
444
445 // For safety's sake let's also grab an extra reference to the
446 // breakpoint constituents of the locations we're going to examine,
447 // since the locations are going to have to get back to their
448 // breakpoints, and the locations don't keep their constituents alive.
449 // I'm just sticking the BreakpointSP's in a vector since I'm only
450 // using it to locally increment their retain counts.
451
452 // We are holding onto the breakpoint locations that were hit
453 // by this stop info between the "synchonous" ShouldStop and now.
454 // But an intervening action might have deleted one of the breakpoints
455 // we hit before we get here. So at the same time let's build a list
456 // of the still valid locations:
457 std::vector<lldb::BreakpointSP> location_constituents;
458
460 for (size_t j = 0; j < num_constituents; j++) {
461 BreakpointLocationSP loc_sp(m_async_stopped_locs.GetByIndex(j));
462 if (loc_sp->IsValid()) {
463 location_constituents.push_back(
464 loc_sp->GetBreakpoint().shared_from_this());
465 valid_locs.Add(loc_sp);
466 }
467 }
468
469 size_t num_valid_locs = valid_locs.GetSize();
470 for (size_t j = 0; j < num_valid_locs; j++) {
471 lldb::BreakpointLocationSP bp_loc_sp = valid_locs.GetByIndex(j);
472 StreamString loc_desc;
473 if (log) {
474 bp_loc_sp->GetDescription(&loc_desc, eDescriptionLevelBrief);
475 }
476 // If another action disabled this breakpoint or its location, then
477 // don't run the actions.
478 if (!bp_loc_sp->IsEnabled() ||
479 !bp_loc_sp->GetBreakpoint().IsEnabled())
480 continue;
481
482 // The breakpoint site may have many locations associated with it,
483 // not all of them valid for this thread. Skip the ones that
484 // aren't:
485 if (!bp_loc_sp->ValidForThisThread(*thread_sp)) {
486 LLDB_LOGF(log,
487 "Breakpoint %s hit on thread 0x%llx but it was not "
488 "for this thread, continuing.",
489 loc_desc.GetData(),
490 static_cast<unsigned long long>(thread_sp->GetID()));
491 continue;
492 }
493
494 // First run the precondition, but since the precondition is per
495 // breakpoint, only run it once per breakpoint.
496 std::pair<std::unordered_set<break_id_t>::iterator, bool> result =
497 precondition_breakpoints.insert(
498 bp_loc_sp->GetBreakpoint().GetID());
499 if (!result.second)
500 continue;
501
502 bool precondition_result =
503 bp_loc_sp->GetBreakpoint().EvaluatePrecondition(context);
504 if (!precondition_result) {
505 actually_said_continue = true;
506 continue;
507 }
508 // Next run the condition for the breakpoint. If that says we
509 // should stop, then we'll run the callback for the breakpoint. If
510 // the callback says we shouldn't stop that will win.
511
512 if (!bp_loc_sp->GetCondition())
513 actually_hit_any_locations = true;
514 else {
515 Status condition_error;
516 bool condition_says_stop =
517 bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
518
519 if (!condition_error.Success()) {
520 // If the condition fails to evaluate, we are going to stop
521 // at it, so the location was hit.
522 actually_hit_any_locations = true;
523 const char *err_str =
524 condition_error.AsCString("<unknown error>");
525 LLDB_LOGF(log, "Error evaluating condition: \"%s\"\n", err_str);
526
527 StreamString strm;
528 strm << "stopped due to an error evaluating condition of "
529 "breakpoint ";
530 bp_loc_sp->GetDescription(&strm, eDescriptionLevelBrief);
531 strm << ": \"" << bp_loc_sp->GetCondition().GetText() << "\"\n";
532 strm << err_str;
533
535 strm.GetString().str(),
536 exe_ctx.GetTargetRef().GetDebugger().GetID());
537 } else {
538 LLDB_LOGF(log,
539 "Condition evaluated for breakpoint %s on thread "
540 "0x%llx condition_says_stop: %i.",
541 loc_desc.GetData(),
542 static_cast<unsigned long long>(thread_sp->GetID()),
543 condition_says_stop);
544 if (condition_says_stop)
545 actually_hit_any_locations = true;
546 else {
547 // We don't want to increment the hit count of breakpoints if
548 // the condition fails. We've already bumped it by the time
549 // we get here, so undo the bump:
550 bp_loc_sp->UndoBumpHitCount();
551 actually_said_continue = true;
552 continue;
553 }
554 }
555 }
556
557 // We've done all the checks whose failure means "we consider lldb
558 // not to have hit the breakpoint". Now we're going to check for
559 // conditions that might continue after hitting. Start with the
560 // ignore count:
561 if (!bp_loc_sp->IgnoreCountShouldStop()) {
562 actually_said_continue = true;
563 continue;
564 }
565
566 // Check the auto-continue bit on the location, do this before the
567 // callback since it may change this, but that would be for the
568 // NEXT hit. Note, you might think you could check auto-continue
569 // before the condition, and not evaluate the condition if it says
570 // to continue. But failing the condition means the breakpoint was
571 // effectively NOT HIT. So these two states are different.
572 bool auto_continue_says_stop = true;
573 if (bp_loc_sp->IsAutoContinue())
574 {
575 LLDB_LOGF(log,
576 "Continuing breakpoint %s as AutoContinue was set.",
577 loc_desc.GetData());
578 // We want this stop reported, so you will know we auto-continued
579 // but only for external breakpoints:
580 if (!bp_loc_sp->GetBreakpoint().IsInternal())
581 thread_sp->SetShouldReportStop(eVoteYes);
582 auto_continue_says_stop = false;
583 }
584
585 bool callback_says_stop = true;
586
587 // FIXME: For now the callbacks have to run in async mode - the
588 // first time we restart we need
589 // to get out of there. So set it here.
590 // When we figure out how to nest breakpoint hits then this will
591 // change.
592
593 // Don't run async callbacks in PerformAction. They have already
594 // been taken into account with async_should_stop.
595 if (!bp_loc_sp->IsCallbackSynchronous()) {
596 Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
597 bool old_async = debugger.GetAsyncExecution();
598 debugger.SetAsyncExecution(true);
599
600 callback_says_stop = bp_loc_sp->InvokeCallback(&context);
601
602 debugger.SetAsyncExecution(old_async);
603
604 if (callback_says_stop && auto_continue_says_stop)
605 m_should_stop = true;
606 else
607 actually_said_continue = true;
608 }
609
610 if (m_should_stop && !bp_loc_sp->GetBreakpoint().IsInternal())
611 all_stopping_locs_internal = false;
612
613 // If we are going to stop for this breakpoint, then remove the
614 // breakpoint.
615 if (callback_says_stop && bp_loc_sp &&
616 bp_loc_sp->GetBreakpoint().IsOneShot()) {
617 thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID(
618 bp_loc_sp->GetBreakpoint().GetID());
619 }
620 // Also make sure that the callback hasn't continued the target. If
621 // it did, when we'll set m_should_start to false and get out of
622 // here.
623 if (HasTargetRunSinceMe()) {
624 m_should_stop = false;
625 actually_said_continue = true;
626 break;
627 }
628 }
629 // At this point if nobody actually told us to continue, we should
630 // give the async breakpoint callback a chance to weigh in:
631 if (!actually_said_continue && !m_should_stop) {
632 m_should_stop = async_should_stop;
633 }
634 }
635 // We've figured out what this stop wants to do, so mark it as valid so
636 // we don't compute it again.
638 } else {
639 m_should_stop = true;
641 actually_hit_any_locations = true;
642 Log *log_process(GetLog(LLDBLog::Process));
643
644 LLDB_LOGF(log_process,
645 "Process::%s could not find breakpoint site id: %" PRId64
646 "...",
647 __FUNCTION__, m_value);
648 }
649
650 if ((!m_should_stop || all_stopping_locs_internal) &&
651 thread_sp->CompletedPlanOverridesBreakpoint()) {
652
653 // Override should_stop decision when we have completed step plan
654 // additionally to the breakpoint
655 m_should_stop = true;
656
657 // We know we're stopping for a completed plan and we don't want to
658 // show the breakpoint stop, so compute the public stop info immediately
659 // here.
660 thread_sp->CalculatePublicStopInfo();
661 } else if (!actually_hit_any_locations) {
662 // In the end, we didn't actually have any locations that passed their
663 // "was I hit" checks. So say we aren't stopped.
664 GetThread()->ResetStopInfo();
665 LLDB_LOGF(log, "Process::%s all locations failed condition checks.",
666 __FUNCTION__);
667 }
668
669 LLDB_LOGF(log,
670 "Process::%s returning from action with m_should_stop: %d.",
671 __FUNCTION__, m_should_stop);
672 }
673 }
674
675private:
678 return {};
679
680 ThreadSP thread_sp = GetThread();
681 if (!thread_sp)
682 return {};
683 ProcessSP process_sp = thread_sp->GetProcess();
684 if (!process_sp)
685 return {};
686
687 return process_sp->GetBreakpointSiteList().FindByID(m_value);
688 }
689
692 bool m_should_perform_action; // Since we are trying to preserve the "state"
693 // of the system even if we run functions
694 // etc. behind the users backs, we need to make sure we only REALLY perform
695 // the action once.
696 lldb::addr_t m_address; // We use this to capture the breakpoint site address
697 // when we create the StopInfo,
698 // in case somebody deletes it between the time the StopInfo is made and the
699 // description is asked for.
703 /// The StopInfoBreakpoint lives after the stop, and could get queried
704 /// at any time so we need to make sure that it keeps the breakpoints for
705 /// each of the locations it records alive while it is around. That's what
706 /// The BreakpointPreservingLocationCollection does.
708};
709
710// StopInfoWatchpoint
711
713public:
714 // Make sure watchpoint is properly disabled and subsequently enabled while
715 // performing watchpoint actions.
717 public:
719 watchpoint_sp(w_sp) {
720 if (process_sp && watchpoint_sp) {
721 const bool notify = false;
722 watchpoint_sp->TurnOnEphemeralMode();
723 process_sp->DisableWatchpoint(watchpoint_sp, notify);
724 process_sp->AddPreResumeAction(SentryPreResumeAction, this);
725 }
726 }
727
728 void DoReenable() {
729 if (process_sp && watchpoint_sp) {
730 bool was_disabled = watchpoint_sp->IsDisabledDuringEphemeralMode();
731 watchpoint_sp->TurnOffEphemeralMode();
732 const bool notify = false;
733 if (was_disabled) {
734 process_sp->DisableWatchpoint(watchpoint_sp, notify);
735 } else {
736 process_sp->EnableWatchpoint(watchpoint_sp, notify);
737 }
738 }
739 }
740
742 DoReenable();
743 if (process_sp)
744 process_sp->ClearPreResumeAction(SentryPreResumeAction, this);
745 }
746
747 static bool SentryPreResumeAction(void *sentry_void) {
748 WatchpointSentry *sentry = (WatchpointSentry *) sentry_void;
749 sentry->DoReenable();
750 return true;
751 }
752
753 private:
756 };
757
758 StopInfoWatchpoint(Thread &thread, break_id_t watch_id, bool silently_skip_wp)
759 : StopInfo(thread, watch_id), m_silently_skip_wp(silently_skip_wp) {}
760
761 ~StopInfoWatchpoint() override = default;
762
763 StopReason GetStopReason() const override { return eStopReasonWatchpoint; }
764
765 uint32_t GetStopReasonDataCount() const override { return 1; }
766 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
767 if (idx == 0)
768 return GetValue();
769 return 0;
770 }
771
772 const char *GetDescription() override {
773 if (m_description.empty()) {
774 StreamString strm;
775 strm.Printf("watchpoint %" PRIi64, m_value);
776 m_description = std::string(strm.GetString());
777 }
778 return m_description.c_str();
779 }
780
781protected:
782 using StopInfoWatchpointSP = std::shared_ptr<StopInfoWatchpoint>;
783 // This plan is used to orchestrate stepping over the watchpoint for
784 // architectures (e.g. ARM) that report the watch before running the watched
785 // access. This is the sort of job you have to defer to the thread plans,
786 // if you try to do it directly in the stop info and there are other threads
787 // that needed to process this stop you will have yanked control away from
788 // them and they won't behave correctly.
790 public:
792 StopInfoWatchpointSP stop_info_sp,
793 WatchpointSP watch_sp)
794 : ThreadPlanStepInstruction(thread, false, true, eVoteNoOpinion,
796 m_stop_info_sp(stop_info_sp), m_watch_sp(watch_sp) {
797 assert(watch_sp);
798 }
799
800 bool DoWillResume(lldb::StateType resume_state,
801 bool current_plan) override {
802 if (resume_state == eStateSuspended)
803 return true;
804
805 if (!m_did_disable_wp) {
806 GetThread().GetProcess()->DisableWatchpoint(m_watch_sp, false);
807 m_did_disable_wp = true;
808 }
809 return true;
810 }
811
812 bool DoPlanExplainsStop(Event *event_ptr) override {
814 return true;
815 StopInfoSP stop_info_sp = GetThread().GetPrivateStopInfo();
816 // lldb-server resets the stop info for threads that didn't get to run,
817 // so we might have not gotten to run, but still have a watchpoint stop
818 // reason, in which case this will indeed be for us.
819 if (stop_info_sp
820 && stop_info_sp->GetStopReason() == eStopReasonWatchpoint)
821 return true;
822 return false;
823 }
824
825 void DidPop() override {
826 // Don't artifically keep the watchpoint alive.
827 m_watch_sp.reset();
828 }
829
830 bool ShouldStop(Event *event_ptr) override {
831 bool should_stop = ThreadPlanStepInstruction::ShouldStop(event_ptr);
832 bool plan_done = MischiefManaged();
833 if (plan_done) {
834 m_stop_info_sp->SetStepOverPlanComplete();
837 }
838 return should_stop;
839 }
840
842 return true;
843 }
844
845 protected:
847 if (!m_did_disable_wp)
848 return;
849 m_did_disable_wp = true;
850 GetThread().GetProcess()->EnableWatchpoint(m_watch_sp, true);
851 }
852
853 private:
856 bool m_did_disable_wp = false;
857 };
858
859 bool ShouldStopSynchronous(Event *event_ptr) override {
860 // If we are running our step-over the watchpoint plan, stop if it's done
861 // and continue if it's not:
863 return m_should_stop;
864
865 // If we are running our step over plan, then stop here and let the regular
866 // ShouldStop figure out what we should do: Otherwise, give our plan
867 // more time to get run:
870
872 ThreadSP thread_sp(m_thread_wp.lock());
873 assert(thread_sp);
874
875 if (thread_sp->GetTemporaryResumeState() == eStateSuspended) {
876 // This is the second firing of a watchpoint so don't process it again.
877 LLDB_LOG(log, "We didn't run but stopped with a StopInfoWatchpoint, we "
878 "have already handled this one, don't do it again.");
879 m_should_stop = false;
881 return m_should_stop;
882 }
883
884 WatchpointSP wp_sp(
885 thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
886 // If we can no longer find the watchpoint, we just have to stop:
887 if (!wp_sp) {
888
889 LLDB_LOGF(log,
890 "Process::%s could not find watchpoint location id: %" PRId64
891 "...",
892 __FUNCTION__, GetValue());
893
894 m_should_stop = true;
896 return true;
897 }
898
899 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
900 StoppointCallbackContext context(event_ptr, exe_ctx, true);
901 m_should_stop = wp_sp->ShouldStop(&context);
902 if (!m_should_stop) {
903 // This won't happen at present because we only allow one watchpoint per
904 // watched range. So we won't stop at a watched address with a disabled
905 // watchpoint. If we start allowing overlapping watchpoints, then we
906 // will have to make watchpoints be real "WatchpointSite" and delegate to
907 // all the watchpoints sharing the site. In that case, the code below
908 // would be the right thing to do.
910 return m_should_stop;
911 }
912 // If this is a system where we need to execute the watchpoint by hand
913 // after the hit, queue a thread plan to do that, and then say not to stop.
914 // Otherwise, let the async action figure out whether the watchpoint should
915 // stop
916
917 ProcessSP process_sp = exe_ctx.GetProcessSP();
918 bool wp_triggers_after = process_sp->GetWatchpointReportedAfter();
919
920 if (!wp_triggers_after) {
921 // We have to step over the watchpoint before we know what to do:
922 StopInfoWatchpointSP me_as_siwp_sp
923 = std::static_pointer_cast<StopInfoWatchpoint>(shared_from_this());
924 ThreadPlanSP step_over_wp_sp =
925 std::make_shared<ThreadPlanStepOverWatchpoint>(*(thread_sp.get()),
926 me_as_siwp_sp, wp_sp);
927 // When this plan is done we want to stop, so set this as a Controlling
928 // plan.
929 step_over_wp_sp->SetIsControllingPlan(true);
930 step_over_wp_sp->SetOkayToDiscard(false);
931
933 error = thread_sp->QueueThreadPlan(step_over_wp_sp, false);
934 // If we couldn't push the thread plan, just stop here:
935 if (!error.Success()) {
936 LLDB_LOGF(log, "Could not push our step over watchpoint plan: %s",
937 error.AsCString());
938
939 m_should_stop = true;
941 return true;
942 } else {
943 // Otherwise, don't set m_should_stop, we don't know that yet. Just
944 // say we should continue, and tell the thread we really should do so:
945 thread_sp->SetShouldRunBeforePublicStop(true);
947 return false;
948 }
949 } else {
950 // We didn't have to do anything special
952 return m_should_stop;
953 }
954
955 return m_should_stop;
956 }
957
958 bool ShouldStop(Event *event_ptr) override {
959 // This just reports the work done by PerformAction or the synchronous
960 // stop. It should only ever get called after they have had a chance to
961 // run.
963 return m_should_stop;
964 }
965
966 void PerformAction(Event *event_ptr) override {
968 // We're going to calculate if we should stop or not in some way during the
969 // course of this code. Also by default we're going to stop, so set that
970 // here.
971 m_should_stop = true;
972
973
974 ThreadSP thread_sp(m_thread_wp.lock());
975 if (thread_sp) {
976
977 WatchpointSP wp_sp(
978 thread_sp->CalculateTarget()->GetWatchpointList().FindByID(
979 GetValue()));
980 if (wp_sp) {
981 // This sentry object makes sure the current watchpoint is disabled
982 // while performing watchpoint actions, and it is then enabled after we
983 // are finished.
984 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
985 ProcessSP process_sp = exe_ctx.GetProcessSP();
986
987 WatchpointSentry sentry(process_sp, wp_sp);
988
989 if (m_silently_skip_wp) {
990 m_should_stop = false;
991 wp_sp->UndoHitCount();
992 }
993
994 if (wp_sp->GetHitCount() <= wp_sp->GetIgnoreCount()) {
995 m_should_stop = false;
997 }
998
999 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
1000
1001 if (m_should_stop && wp_sp->GetConditionText() != nullptr) {
1002 // We need to make sure the user sees any parse errors in their
1003 // condition, so we'll hook the constructor errors up to the
1004 // debugger's Async I/O.
1005 ExpressionResults result_code;
1006 EvaluateExpressionOptions expr_options;
1007 expr_options.SetUnwindOnError(true);
1008 expr_options.SetIgnoreBreakpoints(true);
1009 ValueObjectSP result_value_sp;
1010 result_code = UserExpression::Evaluate(
1011 exe_ctx, expr_options, wp_sp->GetConditionText(),
1012 llvm::StringRef(), result_value_sp);
1013
1014 if (result_code == eExpressionCompleted) {
1015 if (result_value_sp) {
1016 Scalar scalar_value;
1017 if (result_value_sp->ResolveValue(scalar_value)) {
1018 if (scalar_value.ULongLong(1) == 0) {
1019 // The condition failed, which we consider "not having hit
1020 // the watchpoint" so undo the hit count here.
1021 wp_sp->UndoHitCount();
1022 m_should_stop = false;
1023 } else
1024 m_should_stop = true;
1025 LLDB_LOGF(log,
1026 "Condition successfully evaluated, result is %s.\n",
1027 m_should_stop ? "true" : "false");
1028 } else {
1029 m_should_stop = true;
1030 LLDB_LOGF(
1031 log,
1032 "Failed to get an integer result from the expression.");
1033 }
1034 }
1035 } else {
1036 const char *err_str = "<unknown error>";
1037 if (result_value_sp)
1038 err_str = result_value_sp->GetError().AsCString();
1039
1040 LLDB_LOGF(log, "Error evaluating condition: \"%s\"\n", err_str);
1041
1042 StreamString strm;
1043 strm << "stopped due to an error evaluating condition of "
1044 "watchpoint ";
1045 wp_sp->GetDescription(&strm, eDescriptionLevelBrief);
1046 strm << ": \"" << wp_sp->GetConditionText() << "\"\n";
1047 strm << err_str;
1048
1049 Debugger::ReportError(strm.GetString().str(),
1050 exe_ctx.GetTargetRef().GetDebugger().GetID());
1051 }
1052 }
1053
1054 // If the condition says to stop, we run the callback to further decide
1055 // whether to stop.
1056 if (m_should_stop) {
1057 // FIXME: For now the callbacks have to run in async mode - the
1058 // first time we restart we need
1059 // to get out of there. So set it here.
1060 // When we figure out how to nest watchpoint hits then this will
1061 // change.
1062
1063 bool old_async = debugger.GetAsyncExecution();
1064 debugger.SetAsyncExecution(true);
1065
1066 StoppointCallbackContext context(event_ptr, exe_ctx, false);
1067 bool stop_requested = wp_sp->InvokeCallback(&context);
1068
1069 debugger.SetAsyncExecution(old_async);
1070
1071 // Also make sure that the callback hasn't continued the target. If
1072 // it did, when we'll set m_should_stop to false and get out of here.
1073 if (HasTargetRunSinceMe())
1074 m_should_stop = false;
1075
1076 if (m_should_stop && !stop_requested) {
1077 // We have been vetoed by the callback mechanism.
1078 m_should_stop = false;
1079 }
1080 }
1081
1082 // Don't stop if the watched region value is unmodified, and
1083 // this is a Modify-type watchpoint.
1084 if (m_should_stop && !wp_sp->WatchedValueReportable(exe_ctx)) {
1085 wp_sp->UndoHitCount();
1086 m_should_stop = false;
1087 }
1088
1089 // Finally, if we are going to stop, print out the new & old values:
1090 if (m_should_stop) {
1091 wp_sp->CaptureWatchedValue(exe_ctx);
1092
1093 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
1094 StreamUP output_up = debugger.GetAsyncOutputStream();
1095 if (wp_sp->DumpSnapshots(output_up.get()))
1096 output_up->EOL();
1097 }
1098
1099 } else {
1100 Log *log_process(GetLog(LLDBLog::Process));
1101
1102 LLDB_LOGF(log_process,
1103 "Process::%s could not find watchpoint id: %" PRId64 "...",
1104 __FUNCTION__, m_value);
1105 }
1106 LLDB_LOGF(log,
1107 "Process::%s returning from action with m_should_stop: %d.",
1108 __FUNCTION__, m_should_stop);
1109
1111 }
1112 }
1113
1114private:
1119
1120 bool m_should_stop = false;
1122 // A false watchpoint hit has happened -
1123 // the thread stopped with a watchpoint
1124 // hit notification, but the watched region
1125 // was not actually accessed (as determined
1126 // by the gdb stub we're talking to).
1127 // Continue past this watchpoint without
1128 // notifying the user; on some targets this
1129 // may mean disable wp, instruction step,
1130 // re-enable wp, continue.
1131 // On others, just continue.
1135};
1136
1137// StopInfoUnixSignal
1138
1140public:
1141 StopInfoUnixSignal(Thread &thread, int signo, const char *description,
1142 std::optional<int> code)
1143 : StopInfo(thread, signo), m_code(code) {
1144 SetDescription(description);
1145 }
1146
1147 ~StopInfoUnixSignal() override = default;
1148
1149 StopReason GetStopReason() const override { return eStopReasonSignal; }
1150
1151 bool ShouldStopSynchronous(Event *event_ptr) override {
1152 ThreadSP thread_sp(m_thread_wp.lock());
1153 if (thread_sp)
1154 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
1155 return false;
1156 }
1157
1158 bool ShouldStop(Event *event_ptr) override { return IsShouldStopSignal(); }
1159
1160 // If should stop returns false, check if we should notify of this event
1161 bool DoShouldNotify(Event *event_ptr) override {
1162 ThreadSP thread_sp(m_thread_wp.lock());
1163 if (thread_sp) {
1164 bool should_notify =
1165 thread_sp->GetProcess()->GetUnixSignals()->GetShouldNotify(m_value);
1166 if (should_notify) {
1167 StreamString strm;
1168 strm.Format(
1169 "thread {0:d} received signal: {1}", thread_sp->GetIndexID(),
1170 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsStringRef(
1171 m_value));
1173 strm.GetData());
1174 }
1175 return should_notify;
1176 }
1177 return true;
1178 }
1179
1180 void WillResume(lldb::StateType resume_state) override {
1181 ThreadSP thread_sp(m_thread_wp.lock());
1182 if (thread_sp) {
1183 if (!thread_sp->GetProcess()->GetUnixSignals()->GetShouldSuppress(
1184 m_value))
1185 thread_sp->SetResumeSignal(m_value);
1186 }
1187 }
1188
1189 const char *GetDescription() override {
1190 if (m_description.empty()) {
1191 ThreadSP thread_sp(m_thread_wp.lock());
1192 if (thread_sp) {
1193 UnixSignalsSP unix_signals = thread_sp->GetProcess()->GetUnixSignals();
1194 StreamString strm;
1195 strm << "signal ";
1196
1197 std::string signal_name =
1198 unix_signals->GetSignalDescription(m_value, m_code);
1199 if (signal_name.size())
1200 strm << signal_name;
1201 else
1202 strm.Printf("%" PRIi64, m_value);
1203
1204 m_description = std::string(strm.GetString());
1205 }
1206 }
1207 return m_description.c_str();
1208 }
1209
1210 bool ShouldSelect() const override { return IsShouldStopSignal(); }
1211
1212 uint32_t GetStopReasonDataCount() const override { return 1; }
1213 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1214 if (idx == 0)
1215 return GetValue();
1216 return 0;
1217 }
1218
1219private:
1220 // In siginfo_t terms, if m_value is si_signo, m_code is si_code.
1221 std::optional<int> m_code;
1222
1223 bool IsShouldStopSignal() const {
1224 if (ThreadSP thread_sp = m_thread_wp.lock())
1225 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
1226 return false;
1227 }
1228};
1229
1230// StopInfoInterrupt
1231
1233public:
1234 StopInfoInterrupt(Thread &thread, int signo, const char *description)
1235 : StopInfo(thread, signo) {
1236 SetDescription(description);
1237 }
1238
1239 ~StopInfoInterrupt() override = default;
1240
1241 StopReason GetStopReason() const override {
1243 }
1244
1245 const char *GetDescription() override {
1246 if (m_description.empty()) {
1247 m_description = "async interrupt";
1248 }
1249 return m_description.c_str();
1250 }
1251
1252 uint32_t GetStopReasonDataCount() const override { return 1; }
1253 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1254 if (idx == 0)
1255 return GetValue();
1256 else
1257 return 0;
1258 }
1259};
1260
1261// StopInfoTrace
1262
1263class StopInfoTrace : public StopInfo {
1264public:
1266
1267 ~StopInfoTrace() override = default;
1268
1269 StopReason GetStopReason() const override { return eStopReasonTrace; }
1270
1271 const char *GetDescription() override {
1272 if (m_description.empty())
1273 return "trace";
1274 else
1275 return m_description.c_str();
1276 }
1277
1278 std::optional<uint32_t>
1279 GetSuggestedStackFrameIndex(bool inlined_stack) override {
1280 // Trace only knows how to adjust inlined stacks:
1281 if (!inlined_stack)
1282 return {};
1283
1284 ThreadSP thread_sp = GetThread();
1285 StackFrameSP frame_0_sp = thread_sp->GetStackFrameAtIndex(0);
1286 if (!frame_0_sp)
1287 return {};
1288 if (!frame_0_sp->IsInlined())
1289 return {};
1290 Block *block_ptr = frame_0_sp->GetFrameBlock();
1291 if (!block_ptr)
1292 return {};
1293 Address pc_address = frame_0_sp->GetFrameCodeAddress();
1294 AddressRange containing_range;
1295 if (!block_ptr->GetRangeContainingAddress(pc_address, containing_range) ||
1296 pc_address != containing_range.GetBaseAddress())
1297 return {};
1298
1299 int num_inlined_functions = 0;
1300
1301 for (Block *container_ptr = block_ptr->GetInlinedParent();
1302 container_ptr != nullptr;
1303 container_ptr = container_ptr->GetInlinedParent()) {
1304 if (!container_ptr->GetRangeContainingAddress(pc_address,
1305 containing_range))
1306 break;
1307 if (pc_address != containing_range.GetBaseAddress())
1308 break;
1309
1310 num_inlined_functions++;
1311 }
1312 inlined_stack = true;
1313 return num_inlined_functions + 1;
1314 }
1315};
1316
1317// StopInfoException
1318
1320public:
1321 StopInfoException(Thread &thread, const char *description)
1322 : StopInfo(thread, LLDB_INVALID_UID) {
1323 if (description)
1324 SetDescription(description);
1325 }
1326
1327 ~StopInfoException() override = default;
1328
1329 StopReason GetStopReason() const override { return eStopReasonException; }
1330
1331 const char *GetDescription() override {
1332 if (m_description.empty())
1333 return "exception";
1334 else
1335 return m_description.c_str();
1336 }
1337 uint32_t GetStopReasonDataCount() const override { return 1; }
1338 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1339 if (idx == 0)
1340 return GetValue();
1341 else
1342 return 0;
1343 }
1344};
1345
1346// StopInfoProcessorTrace
1347
1349public:
1350 StopInfoProcessorTrace(Thread &thread, const char *description)
1351 : StopInfo(thread, LLDB_INVALID_UID) {
1352 if (description)
1353 SetDescription(description);
1354 }
1355
1356 ~StopInfoProcessorTrace() override = default;
1357
1358 StopReason GetStopReason() const override {
1360 }
1361
1362 const char *GetDescription() override {
1363 if (m_description.empty())
1364 return "processor trace event";
1365 else
1366 return m_description.c_str();
1367 }
1368};
1369
1370// StopInfoHistoryBoundary
1371
1373public:
1374 StopInfoHistoryBoundary(Thread &thread, const char *description)
1375 : StopInfo(thread, LLDB_INVALID_UID) {
1376 if (description)
1377 SetDescription(description);
1378 }
1379
1380 ~StopInfoHistoryBoundary() override = default;
1381
1382 StopReason GetStopReason() const override {
1384 }
1385
1386 const char *GetDescription() override {
1387 if (m_description.empty())
1388 return "history boundary";
1389 return m_description.c_str();
1390 }
1391};
1392
1393// StopInfoThreadPlan
1394
1396public:
1397 StopInfoThreadPlan(ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp,
1398 ExpressionVariableSP &expression_variable_sp)
1399 : StopInfo(plan_sp->GetThread(), LLDB_INVALID_UID), m_plan_sp(plan_sp),
1400 m_return_valobj_sp(return_valobj_sp),
1401 m_expression_variable_sp(expression_variable_sp) {}
1402
1403 ~StopInfoThreadPlan() override = default;
1404
1406
1407 const char *GetDescription() override {
1408 if (m_description.empty()) {
1409 StreamString strm;
1410 m_plan_sp->GetDescription(&strm, eDescriptionLevelBrief);
1411 m_description = std::string(strm.GetString());
1412 }
1413 return m_description.c_str();
1414 }
1415
1417
1421
1422protected:
1423 bool ShouldStop(Event *event_ptr) override {
1424 if (m_plan_sp)
1425 return m_plan_sp->ShouldStop(event_ptr);
1426 else
1427 return StopInfo::ShouldStop(event_ptr);
1428 }
1429
1430private:
1434};
1435
1436// StopInfoExec
1437
1438class StopInfoExec : public StopInfo {
1439public:
1441
1442 ~StopInfoExec() override = default;
1443
1444 bool ShouldStop(Event *event_ptr) override {
1445 ThreadSP thread_sp(m_thread_wp.lock());
1446 if (thread_sp)
1447 return thread_sp->GetProcess()->GetStopOnExec();
1448 return false;
1449 }
1450
1451 StopReason GetStopReason() const override { return eStopReasonExec; }
1452
1453 const char *GetDescription() override { return "exec"; }
1454
1455protected:
1456 void PerformAction(Event *event_ptr) override {
1457 // Only perform the action once
1459 return;
1460 m_performed_action = true;
1461 ThreadSP thread_sp(m_thread_wp.lock());
1462 if (thread_sp)
1463 thread_sp->GetProcess()->DidExec();
1464 }
1465
1467};
1468
1469// StopInfoFork
1470
1471class StopInfoFork : public StopInfo {
1472public:
1473 StopInfoFork(Thread &thread, lldb::pid_t child_pid, lldb::tid_t child_tid)
1474 : StopInfo(thread, child_pid), m_child_pid(child_pid),
1475 m_child_tid(child_tid) {}
1476
1477 ~StopInfoFork() override = default;
1478
1479 bool ShouldStop(Event *event_ptr) override { return false; }
1480
1481 StopReason GetStopReason() const override { return eStopReasonFork; }
1482
1483 const char *GetDescription() override { return "fork"; }
1484
1485 uint32_t GetStopReasonDataCount() const override { return 1; }
1486 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1487 if (idx == 0)
1488 return GetValue();
1489 else
1490 return 0;
1491 }
1492
1493protected:
1494 void PerformAction(Event *event_ptr) override {
1495 // Only perform the action once
1497 return;
1498 m_performed_action = true;
1499 ThreadSP thread_sp(m_thread_wp.lock());
1500 if (thread_sp)
1501 thread_sp->GetProcess()->DidFork(m_child_pid, m_child_tid);
1502 }
1503
1505
1506private:
1509};
1510
1511// StopInfoVFork
1512
1513class StopInfoVFork : public StopInfo {
1514public:
1515 StopInfoVFork(Thread &thread, lldb::pid_t child_pid, lldb::tid_t child_tid)
1516 : StopInfo(thread, child_pid), m_child_pid(child_pid),
1517 m_child_tid(child_tid) {}
1518
1519 ~StopInfoVFork() override = default;
1520
1521 bool ShouldStop(Event *event_ptr) override { return false; }
1522
1523 StopReason GetStopReason() const override { return eStopReasonVFork; }
1524
1525 const char *GetDescription() override { return "vfork"; }
1526
1527 uint32_t GetStopReasonDataCount() const override { return 1; }
1528 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1529 if (idx == 0)
1530 return GetValue();
1531 return 0;
1532 }
1533
1534protected:
1535 void PerformAction(Event *event_ptr) override {
1536 // Only perform the action once
1538 return;
1539 m_performed_action = true;
1540 ThreadSP thread_sp(m_thread_wp.lock());
1541 if (thread_sp)
1542 thread_sp->GetProcess()->DidVFork(m_child_pid, m_child_tid);
1543 }
1544
1546
1547private:
1550};
1551
1552// StopInfoVForkDone
1553
1555public:
1556 StopInfoVForkDone(Thread &thread) : StopInfo(thread, 0) {}
1557
1558 ~StopInfoVForkDone() override = default;
1559
1560 bool ShouldStop(Event *event_ptr) override { return false; }
1561
1562 StopReason GetStopReason() const override { return eStopReasonVForkDone; }
1563
1564 const char *GetDescription() override { return "vforkdone"; }
1565
1566protected:
1567 void PerformAction(Event *event_ptr) override {
1568 // Only perform the action once
1570 return;
1571 m_performed_action = true;
1572 ThreadSP thread_sp(m_thread_wp.lock());
1573 if (thread_sp)
1574 thread_sp->GetProcess()->DidVForkDone();
1575 }
1576
1578};
1579
1580} // namespace lldb_private
1581
1583 break_id_t break_id) {
1584 thread.SetThreadHitBreakpointSite();
1585
1586 return std::make_shared<StopInfoBreakpoint>(thread, break_id);
1587}
1588
1590 break_id_t break_id,
1591 bool should_stop) {
1592 return std::make_shared<StopInfoBreakpoint>(thread, break_id, should_stop);
1593}
1594
1595// LWP_TODO: We'll need a CreateStopReasonWithWatchpointResourceID akin
1596// to CreateStopReasonWithBreakpointSiteID
1598 break_id_t watch_id,
1599 bool silently_continue) {
1600 return std::make_shared<StopInfoWatchpoint>(thread, watch_id,
1601 silently_continue);
1602}
1603
1605 const char *description,
1606 std::optional<int> code) {
1607 thread.GetProcess()->GetUnixSignals()->IncrementSignalHitCount(signo);
1608 return std::make_shared<StopInfoUnixSignal>(thread, signo, description, code);
1609}
1610
1612 const char *description) {
1613 return std::make_shared<StopInfoInterrupt>(thread, signo, description);
1614}
1615
1617 return std::make_shared<StopInfoTrace>(thread);
1618}
1619
1621 ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp,
1622 ExpressionVariableSP expression_variable_sp) {
1623 return std::make_shared<StopInfoThreadPlan>(plan_sp, return_valobj_sp,
1624 expression_variable_sp);
1625}
1626
1628 const char *description) {
1629 return std::make_shared<StopInfoException>(thread, description);
1630}
1631
1633 const char *description) {
1634 return std::make_shared<StopInfoProcessorTrace>(thread, description);
1635}
1636
1638 const char *description) {
1639 return std::make_shared<StopInfoHistoryBoundary>(thread, description);
1640}
1641
1643 return std::make_shared<StopInfoExec>(thread);
1644}
1645
1647 lldb::pid_t child_pid,
1648 lldb::tid_t child_tid) {
1649 return std::make_shared<StopInfoFork>(thread, child_pid, child_tid);
1650}
1651
1652
1654 lldb::pid_t child_pid,
1655 lldb::tid_t child_tid) {
1656 return std::make_shared<StopInfoVFork>(thread, child_pid, child_tid);
1657}
1658
1660 return std::make_shared<StopInfoVForkDone>(thread);
1661}
1662
1664 if (stop_info_sp &&
1665 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1666 StopInfoThreadPlan *plan_stop_info =
1667 static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1668 return plan_stop_info->GetReturnValueObject();
1669 } else
1670 return ValueObjectSP();
1671}
1672
1674 if (stop_info_sp &&
1675 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1676 StopInfoThreadPlan *plan_stop_info =
1677 static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1678 return plan_stop_info->GetExpressionVariable();
1679 } else
1680 return ExpressionVariableSP();
1681}
1682
1685 lldb::addr_t *crashing_address) {
1686 if (!stop_info_sp) {
1687 return ValueObjectSP();
1688 }
1689
1690 const char *description = stop_info_sp->GetDescription();
1691 if (!description) {
1692 return ValueObjectSP();
1693 }
1694
1695 ThreadSP thread_sp = stop_info_sp->GetThread();
1696 if (!thread_sp) {
1697 return ValueObjectSP();
1698 }
1699
1700 StackFrameSP frame_sp =
1701 thread_sp->GetSelectedFrame(DoNoSelectMostRelevantFrame);
1702
1703 if (!frame_sp) {
1704 return ValueObjectSP();
1705 }
1706
1707 const char address_string[] = "address=";
1708
1709 const char *address_loc = strstr(description, address_string);
1710 if (!address_loc) {
1711 return ValueObjectSP();
1712 }
1713
1714 address_loc += (sizeof(address_string) - 1);
1715
1716 uint64_t address = strtoull(address_loc, nullptr, 0);
1717 if (crashing_address) {
1718 *crashing_address = address;
1719 }
1720
1721 return frame_sp->GuessValueForAddress(address);
1722}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
#define LLDB_LOGF(log,...)
Definition Log.h:383
A section + offset based address range class.
Address & GetBaseAddress()
Get accessor for the base address of the range.
A section + offset based address class.
Definition Address.h:62
A class that describes a single lexical block.
Definition Block.h:41
bool GetRangeContainingAddress(const Address &addr, AddressRange &range)
Definition Block.cpp:248
Block * GetInlinedParent()
Get the inlined parent block for this block.
Definition Block.cpp:212
lldb::BreakpointLocationSP GetByIndex(size_t i)
Returns a shared pointer to the breakpoint location with index i.
void Add(const lldb::BreakpointLocationSP &bp_loc_sp)
Add the breakpoint bp_loc_sp to the list.
size_t GetSize() const
Returns the number of elements in this breakpoint location list.
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition Breakpoint.h:81
bool IsOneShot() const
Check the OneShot state.
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
A class to manage flag bits.
Definition Debugger.h:101
void SetAsyncExecution(bool async)
static void ReportWarning(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report warning events.
static void ReportError(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report error events.
lldb::StreamUP GetAsyncOutputStream()
void SetUnwindOnError(bool unwind=false)
Definition Target.h:373
void SetIgnoreBreakpoints(bool ignore=false)
Definition Target.h:377
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
const lldb::ProcessSP & GetProcessSP() const
Get accessor to get the process shared pointer.
Target & GetTargetRef() const
Returns a reference to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
bool IsRunningExpression() const
Definition Process.h:279
bool GetIgnoreBreakpointsInExpressions() const
Definition Process.cpp:242
static void AddRestartedReason(Event *event_ptr, const char *reason)
Definition Process.cpp:4572
A plug-in interface definition class for debugging a process.
Definition Process.h:354
const ProcessModID & GetModIDRef() const
Definition Process.h:1453
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1250
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition Scalar.cpp:365
An error handling class.
Definition Status.h:118
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition Status.cpp:194
bool Success() const
Test for success condition.
Definition Status.cpp:303
void PerformAction(Event *event_ptr) override
Definition StopInfo.cpp:319
BreakpointSiteSP GetBreakpointSiteSP() const
Definition StopInfo.cpp:676
std::optional< uint32_t > GetSuggestedStackFrameIndex(bool inlined_stack) override
This gives the StopInfo a chance to suggest a stack frame to select.
Definition StopInfo.cpp:292
const char * GetDescription() override
Definition StopInfo.cpp:188
bool ShouldStopSynchronous(Event *event_ptr) override
Definition StopInfo.cpp:155
bool IsValidForOperatingSystemThread(Thread &thread) override
Definition StopInfo.cpp:143
bool DoShouldNotify(Event *event_ptr) override
Definition StopInfo.cpp:184
bool ShouldShow() const override
Returns true if this is a stop reason that should be shown to a user when viewing the thread with thi...
Definition StopInfo.cpp:306
uint32_t GetStopReasonDataCount() const override
Definition StopInfo.cpp:253
~StopInfoBreakpoint() override=default
bool ShouldSelect() const override
Returns true if this is a stop reason that should cause a thread to be selected when stopping.
Definition StopInfo.cpp:308
StopInfoBreakpoint(Thread &thread, break_id_t break_id, bool should_stop)
Definition StopInfo.cpp:102
StopInfoBreakpoint(Thread &thread, break_id_t break_id)
Definition StopInfo.cpp:93
BreakpointLocationCollection m_async_stopped_locs
The StopInfoBreakpoint lives after the stop, and could get queried at any time so we need to make sur...
Definition StopInfo.cpp:707
bool ShouldStop(Event *event_ptr) override
Definition StopInfo.cpp:311
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override
Definition StopInfo.cpp:266
StopReason GetStopReason() const override
Definition StopInfo.cpp:153
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override
const char * GetDescription() override
uint32_t GetStopReasonDataCount() const override
~StopInfoException() override=default
StopInfoException(Thread &thread, const char *description)
StopReason GetStopReason() const override
~StopInfoExec() override=default
StopInfoExec(Thread &thread)
const char * GetDescription() override
StopReason GetStopReason() const override
void PerformAction(Event *event_ptr) override
bool ShouldStop(Event *event_ptr) override
uint32_t GetStopReasonDataCount() const override
void PerformAction(Event *event_ptr) override
bool ShouldStop(Event *event_ptr) override
StopReason GetStopReason() const override
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override
StopInfoFork(Thread &thread, lldb::pid_t child_pid, lldb::tid_t child_tid)
~StopInfoFork() override=default
const char * GetDescription() override
StopReason GetStopReason() const override
const char * GetDescription() override
~StopInfoHistoryBoundary() override=default
StopInfoHistoryBoundary(Thread &thread, const char *description)
uint32_t GetStopReasonDataCount() const override
StopInfoInterrupt(Thread &thread, int signo, const char *description)
const char * GetDescription() override
StopReason GetStopReason() const override
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override
~StopInfoInterrupt() override=default
StopInfoProcessorTrace(Thread &thread, const char *description)
~StopInfoProcessorTrace() override=default
const char * GetDescription() override
StopReason GetStopReason() const override
bool ShouldStop(Event *event_ptr) override
~StopInfoThreadPlan() override=default
ExpressionVariableSP m_expression_variable_sp
ExpressionVariableSP GetExpressionVariable()
const char * GetDescription() override
StopReason GetStopReason() const override
StopInfoThreadPlan(ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp, ExpressionVariableSP &expression_variable_sp)
const char * GetDescription() override
~StopInfoTrace() override=default
StopReason GetStopReason() const override
StopInfoTrace(Thread &thread)
std::optional< uint32_t > GetSuggestedStackFrameIndex(bool inlined_stack) override
This gives the StopInfo a chance to suggest a stack frame to select.
StopInfoUnixSignal(Thread &thread, int signo, const char *description, std::optional< int > code)
void WillResume(lldb::StateType resume_state) override
bool DoShouldNotify(Event *event_ptr) override
~StopInfoUnixSignal() override=default
const char * GetDescription() override
std::optional< int > m_code
bool ShouldStopSynchronous(Event *event_ptr) override
uint32_t GetStopReasonDataCount() const override
bool ShouldStop(Event *event_ptr) override
StopReason GetStopReason() const override
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override
bool ShouldSelect() const override
Returns true if this is a stop reason that should cause a thread to be selected when stopping.
const char * GetDescription() override
~StopInfoVForkDone() override=default
bool ShouldStop(Event *event_ptr) override
StopReason GetStopReason() const override
void PerformAction(Event *event_ptr) override
bool ShouldStop(Event *event_ptr) override
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override
StopReason GetStopReason() const override
void PerformAction(Event *event_ptr) override
~StopInfoVFork() override=default
StopInfoVFork(Thread &thread, lldb::pid_t child_pid, lldb::tid_t child_tid)
const char * GetDescription() override
uint32_t GetStopReasonDataCount() const override
ThreadPlanStepOverWatchpoint(Thread &thread, StopInfoWatchpointSP stop_info_sp, WatchpointSP watch_sp)
Definition StopInfo.cpp:791
bool DoWillResume(lldb::StateType resume_state, bool current_plan) override
Definition StopInfo.cpp:800
WatchpointSentry(ProcessSP p_sp, WatchpointSP w_sp)
Definition StopInfo.cpp:718
static bool SentryPreResumeAction(void *sentry_void)
Definition StopInfo.cpp:747
~StopInfoWatchpoint() override=default
std::shared_ptr< StopInfoWatchpoint > StopInfoWatchpointSP
Definition StopInfo.cpp:782
StopInfoWatchpoint(Thread &thread, break_id_t watch_id, bool silently_skip_wp)
Definition StopInfo.cpp:758
uint32_t GetStopReasonDataCount() const override
Definition StopInfo.cpp:765
const char * GetDescription() override
Definition StopInfo.cpp:772
void PerformAction(Event *event_ptr) override
Definition StopInfo.cpp:966
bool ShouldStop(Event *event_ptr) override
Definition StopInfo.cpp:958
bool ShouldStopSynchronous(Event *event_ptr) override
Definition StopInfo.cpp:859
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override
Definition StopInfo.cpp:766
StopReason GetStopReason() const override
Definition StopInfo.cpp:763
std::string m_description
Definition StopInfo.h:227
static lldb::StopInfoSP CreateStopReasonWithPlan(lldb::ThreadPlanSP &plan, lldb::ValueObjectSP return_valobj_sp, lldb::ExpressionVariableSP expression_variable_sp)
uint64_t GetValue() const
Definition StopInfo.h:46
static lldb::ExpressionVariableSP GetExpressionVariable(lldb::StopInfoSP &stop_info_sp)
static lldb::ValueObjectSP GetReturnValueObject(lldb::StopInfoSP &stop_info_sp)
static lldb::StopInfoSP CreateStopReasonToTrace(Thread &thread)
static lldb::StopInfoSP CreateStopReasonVFork(Thread &thread, lldb::pid_t child_pid, lldb::tid_t child_tid)
static lldb::StopInfoSP CreateStopReasonWithInterrupt(Thread &thread, int signo, const char *description)
bool IsValid() const
Definition StopInfo.cpp:41
StructuredData::ObjectSP m_extended_info
Definition StopInfo.h:232
static lldb::StopInfoSP CreateStopReasonWithSignal(Thread &thread, int signo, const char *description=nullptr, std::optional< int > code=std::nullopt)
lldb::ThreadSP GetThread() const
Definition StopInfo.h:35
static lldb::StopInfoSP CreateStopReasonFork(Thread &thread, lldb::pid_t child_pid, lldb::tid_t child_tid)
static lldb::StopInfoSP CreateStopReasonVForkDone(Thread &thread)
static lldb::StopInfoSP CreateStopReasonWithWatchpointID(Thread &thread, lldb::break_id_t watch_id, bool silently_continue=false)
virtual void SetDescription(const char *desc_cstr)
Definition StopInfo.h:74
static lldb::StopInfoSP CreateStopReasonWithException(Thread &thread, const char *description)
static lldb::StopInfoSP CreateStopReasonWithBreakpointSiteID(Thread &thread, lldb::break_id_t break_id)
static lldb::StopInfoSP CreateStopReasonHistoryBoundary(Thread &thread, const char *description)
static lldb::ValueObjectSP GetCrashingDereference(lldb::StopInfoSP &stop_info_sp, lldb::addr_t *crashing_address=nullptr)
LazyBool m_override_should_notify
Definition StopInfo.h:228
static lldb::StopInfoSP CreateStopReasonProcessorTrace(Thread &thread, const char *description)
static lldb::StopInfoSP CreateStopReasonWithExec(Thread &thread)
friend class Thread
Definition StopInfo.h:245
StopInfo(Thread &thread, uint64_t value)
Definition StopInfo.cpp:34
lldb::ThreadWP m_thread_wp
Definition StopInfo.h:222
LazyBool m_override_should_stop
Definition StopInfo.h:229
virtual bool ShouldStop(Event *event_ptr)
Definition StopInfo.h:219
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
lldb::break_id_t GetID() const
Definition Stoppoint.cpp:22
const char * GetData() const
llvm::StringRef GetString() const
void Format(const char *format, Args &&... args)
Forwards the arguments to llvm::formatv and writes to the stream.
Definition Stream.h:376
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
Debugger & GetDebugger() const
Definition Target.h:1223
bool DoPlanExplainsStop(Event *event_ptr) override
ThreadPlanStepInstruction(Thread &thread, bool step_over, bool stop_others, Vote report_stop_vote, Vote report_run_vote)
Thread & GetThread()
Returns the Thread that is using this thread plan.
virtual lldb::StopInfoSP GetPrivateStopInfo(bool calculate=true)
Definition Thread.cpp:400
void SetStopInfo(const lldb::StopInfoSP &stop_info_sp)
Definition Thread.cpp:478
lldb::ProcessSP GetProcess() const
Definition Thread.h:161
static lldb::ExpressionResults Evaluate(ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options, llvm::StringRef expr_cstr, llvm::StringRef expr_prefix, lldb::ValueObjectSP &result_valobj_sp, std::string *fixed_expression=nullptr, ValueObject *ctx_obj=nullptr)
Evaluate one expression in the scratch context of the target passed in the exe_ctx and return its res...
#define LLDB_INVALID_BREAK_ID
#define LLDB_INVALID_UID
#define LLDB_INVALID_ADDRESS
@ DoNoSelectMostRelevantFrame
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::BreakpointSite > BreakpointSiteSP
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
@ eDescriptionLevelBrief
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::ExpressionVariable > ExpressionVariableSP
std::shared_ptr< lldb_private::UnixSignals > UnixSignalsSP
StateType
Process and Thread States.
@ eStateStopped
Process or thread is stopped and can be examined.
@ eStateSuspended
Process or thread is in a suspended state as far as the debugger is concerned while other processes o...
@ eStateRunning
Process or thread is running and can't be examined.
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
int32_t break_id_t
Definition lldb-types.h:87
std::shared_ptr< lldb_private::Process > ProcessSP
uint64_t pid_t
Definition lldb-types.h:83
std::shared_ptr< lldb_private::Watchpoint > WatchpointSP
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
uint64_t addr_t
Definition lldb-types.h:80
StopReason
Thread stop reasons.
@ eStopReasonPlanComplete
@ eStopReasonHistoryBoundary
@ eStopReasonBreakpoint
@ eStopReasonExec
Program was re-exec'ed.
@ eStopReasonVForkDone
@ eStopReasonInterrupt
Thread requested interrupt.
@ eStopReasonProcessorTrace
@ eStopReasonException
@ eStopReasonWatchpoint
std::unique_ptr< lldb_private::Stream > StreamUP
uint64_t tid_t
Definition lldb-types.h:84
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition UserID.h:47