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 if (log) {
333 LLDB_LOGF(log, "PerformAction got called with an invalid thread.");
334 }
335 m_should_stop = true;
337 return;
338 }
339
341 std::unordered_set<break_id_t> precondition_breakpoints;
342 // Breakpoints that fail their condition check are not considered to
343 // have been hit. If the only locations at this site have failed their
344 // conditions, we should change the stop-info to none. Otherwise, if we
345 // hit another breakpoint on a different thread which does stop, users
346 // will see a breakpont hit with a failed condition, which is wrong.
347 // Use this variable to tell us if that is true.
348 bool actually_hit_any_locations = false;
349 if (bp_site_sp) {
350 // Let's copy the constituents list out of the site and store them in a
351 // local list. That way if one of the breakpoint actions changes the
352 // site, then we won't be operating on a bad list.
353 BreakpointLocationCollection site_locations;
354 size_t num_constituents = m_async_stopped_locs.GetSize();
355
356 if (num_constituents == 0) {
357 m_should_stop = true;
358 actually_hit_any_locations = true; // We're going to stop, don't
359 // change the stop info.
360 } else {
361 // We go through each location, and test first its precondition -
362 // this overrides everything. Note, we only do this once per
363 // breakpoint - not once per location... Then check the condition.
364 // If the condition says to stop, then we run the callback for that
365 // location. If that callback says to stop as well, then we set
366 // m_should_stop to true; we are going to stop. But we still want to
367 // give all the breakpoints whose conditions say we are going to stop
368 // a chance to run their callbacks. Of course if any callback
369 // restarts the target by putting "continue" in the callback, then
370 // we're going to restart, without running the rest of the callbacks.
371 // And in this case we will end up not stopping even if another
372 // location said we should stop. But that's better than not running
373 // all the callbacks.
374
375 // There's one other complication here. We may have run an async
376 // breakpoint callback that said we should stop. We only want to
377 // override that if another breakpoint action says we shouldn't
378 // stop. If nobody else has an opinion, then we should stop if the
379 // async callback says we should. An example of this is the async
380 // shared library load notification breakpoint and the setting
381 // stop-on-sharedlibrary-events.
382 // We'll keep the async value in async_should_stop, and track whether
383 // anyone said we should NOT stop in actually_said_continue.
384 bool async_should_stop = false;
386 async_should_stop = m_should_stop;
387 bool actually_said_continue = false;
388
389 m_should_stop = false;
390
391 // We don't select threads as we go through them testing breakpoint
392 // conditions and running commands. So we need to set the thread for
393 // expression evaluation here:
394 ThreadList::ExpressionExecutionThreadPusher thread_pusher(thread_sp);
395
396 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
397 Process *process = exe_ctx.GetProcessPtr();
398 if (process->GetModIDRef().IsRunningExpression()) {
399 // If we are in the middle of evaluating an expression, don't run
400 // asynchronous breakpoint commands or expressions. That could
401 // lead to infinite recursion if the command or condition re-calls
402 // the function with this breakpoint.
403 // TODO: We can keep a list of the breakpoints we've seen while
404 // running expressions in the nested
405 // PerformAction calls that can arise when the action runs a
406 // function that hits another breakpoint, and only stop running
407 // commands when we see the same breakpoint hit a second time.
408
410
411 // It is possible that the user has a breakpoint at the same site
412 // as the completed plan had (e.g. user has a breakpoint
413 // on a module entry point, and `ThreadPlanCallFunction` ends
414 // also there). We can't find an internal breakpoint in the loop
415 // later because it was already removed on the plan completion.
416 // So check if the plan was completed, and stop if so.
417 if (thread_sp->CompletedPlanOverridesBreakpoint()) {
418 m_should_stop = true;
419 thread_sp->ResetStopInfo();
420 return;
421 }
422
423 LLDB_LOGF(log, "StopInfoBreakpoint::PerformAction - Hit a "
424 "breakpoint while running an expression,"
425 " not running commands to avoid recursion.");
426 bool ignoring_breakpoints =
428 // Internal breakpoints should be allowed to do their job, we
429 // can make sure they don't do anything that would cause recursive
430 // command execution:
431 if (!m_was_all_internal) {
432 m_should_stop = !ignoring_breakpoints;
433 LLDB_LOGF(log,
434 "StopInfoBreakpoint::PerformAction - in expression, "
435 "continuing: %s.",
436 m_should_stop ? "true" : "false");
438 "hit breakpoint while running function, skipping commands "
439 "and conditions to prevent recursion",
440 process->GetTarget().GetDebugger().GetID());
441 return;
442 }
443 }
444
445 StoppointCallbackContext context(event_ptr, exe_ctx, false);
446
447 // For safety's sake let's also grab an extra reference to the
448 // breakpoint constituents of the locations we're going to examine,
449 // since the locations are going to have to get back to their
450 // breakpoints, and the locations don't keep their constituents alive.
451 // I'm just sticking the BreakpointSP's in a vector since I'm only
452 // using it to locally increment their retain counts.
453
454 // We are holding onto the breakpoint locations that were hit
455 // by this stop info between the "synchonous" ShouldStop and now.
456 // But an intervening action might have deleted one of the breakpoints
457 // we hit before we get here. So at the same time let's build a list
458 // of the still valid locations:
459 std::vector<lldb::BreakpointSP> location_constituents;
460
462 for (size_t j = 0; j < num_constituents; j++) {
463 BreakpointLocationSP loc_sp(m_async_stopped_locs.GetByIndex(j));
464 if (loc_sp->IsValid()) {
465 location_constituents.push_back(
466 loc_sp->GetBreakpoint().shared_from_this());
467 valid_locs.Add(loc_sp);
468 }
469 }
470
471 size_t num_valid_locs = valid_locs.GetSize();
472 for (size_t j = 0; j < num_valid_locs; j++) {
473 lldb::BreakpointLocationSP bp_loc_sp = valid_locs.GetByIndex(j);
474 StreamString loc_desc;
475 if (log) {
476 bp_loc_sp->GetDescription(&loc_desc, eDescriptionLevelBrief);
477 }
478 // If another action disabled this breakpoint or its location, then
479 // don't run the actions.
480 if (!bp_loc_sp->IsEnabled() ||
481 !bp_loc_sp->GetBreakpoint().IsEnabled())
482 continue;
483
484 // The breakpoint site may have many locations associated with it,
485 // not all of them valid for this thread. Skip the ones that
486 // aren't:
487 if (!bp_loc_sp->ValidForThisThread(*thread_sp)) {
488 if (log) {
489 LLDB_LOGF(log,
490 "Breakpoint %s hit on thread 0x%llx but it was not "
491 "for this thread, continuing.",
492 loc_desc.GetData(),
493 static_cast<unsigned long long>(thread_sp->GetID()));
494 }
495 continue;
496 }
497
498 // First run the precondition, but since the precondition is per
499 // breakpoint, only run it once per breakpoint.
500 std::pair<std::unordered_set<break_id_t>::iterator, bool> result =
501 precondition_breakpoints.insert(
502 bp_loc_sp->GetBreakpoint().GetID());
503 if (!result.second)
504 continue;
505
506 bool precondition_result =
507 bp_loc_sp->GetBreakpoint().EvaluatePrecondition(context);
508 if (!precondition_result) {
509 actually_said_continue = true;
510 continue;
511 }
512 // Next run the condition for the breakpoint. If that says we
513 // should stop, then we'll run the callback for the breakpoint. If
514 // the callback says we shouldn't stop that will win.
515
516 if (!bp_loc_sp->GetCondition())
517 actually_hit_any_locations = true;
518 else {
519 Status condition_error;
520 bool condition_says_stop =
521 bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
522
523 if (!condition_error.Success()) {
524 // If the condition fails to evaluate, we are going to stop
525 // at it, so the location was hit.
526 actually_hit_any_locations = true;
527 const char *err_str =
528 condition_error.AsCString("<unknown error>");
529 LLDB_LOGF(log, "Error evaluating condition: \"%s\"\n", err_str);
530
531 StreamString strm;
532 strm << "stopped due to an error evaluating condition of "
533 "breakpoint ";
534 bp_loc_sp->GetDescription(&strm, eDescriptionLevelBrief);
535 strm << ": \"" << bp_loc_sp->GetCondition().GetText() << "\"\n";
536 strm << err_str;
537
539 strm.GetString().str(),
540 exe_ctx.GetTargetRef().GetDebugger().GetID());
541 } else {
542 LLDB_LOGF(log,
543 "Condition evaluated for breakpoint %s on thread "
544 "0x%llx condition_says_stop: %i.",
545 loc_desc.GetData(),
546 static_cast<unsigned long long>(thread_sp->GetID()),
547 condition_says_stop);
548 if (condition_says_stop)
549 actually_hit_any_locations = true;
550 else {
551 // We don't want to increment the hit count of breakpoints if
552 // the condition fails. We've already bumped it by the time
553 // we get here, so undo the bump:
554 bp_loc_sp->UndoBumpHitCount();
555 actually_said_continue = true;
556 continue;
557 }
558 }
559 }
560
561 // We've done all the checks whose failure means "we consider lldb
562 // not to have hit the breakpoint". Now we're going to check for
563 // conditions that might continue after hitting. Start with the
564 // ignore count:
565 if (!bp_loc_sp->IgnoreCountShouldStop()) {
566 actually_said_continue = true;
567 continue;
568 }
569
570 // Check the auto-continue bit on the location, do this before the
571 // callback since it may change this, but that would be for the
572 // NEXT hit. Note, you might think you could check auto-continue
573 // before the condition, and not evaluate the condition if it says
574 // to continue. But failing the condition means the breakpoint was
575 // effectively NOT HIT. So these two states are different.
576 bool auto_continue_says_stop = true;
577 if (bp_loc_sp->IsAutoContinue())
578 {
579 LLDB_LOGF(log,
580 "Continuing breakpoint %s as AutoContinue was set.",
581 loc_desc.GetData());
582 // We want this stop reported, so you will know we auto-continued
583 // but only for external breakpoints:
584 if (!bp_loc_sp->GetBreakpoint().IsInternal())
585 thread_sp->SetShouldReportStop(eVoteYes);
586 auto_continue_says_stop = false;
587 }
588
589 bool callback_says_stop = true;
590
591 // FIXME: For now the callbacks have to run in async mode - the
592 // first time we restart we need
593 // to get out of there. So set it here.
594 // When we figure out how to nest breakpoint hits then this will
595 // change.
596
597 // Don't run async callbacks in PerformAction. They have already
598 // been taken into account with async_should_stop.
599 if (!bp_loc_sp->IsCallbackSynchronous()) {
600 Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
601 bool old_async = debugger.GetAsyncExecution();
602 debugger.SetAsyncExecution(true);
603
604 callback_says_stop = bp_loc_sp->InvokeCallback(&context);
605
606 debugger.SetAsyncExecution(old_async);
607
608 if (callback_says_stop && auto_continue_says_stop)
609 m_should_stop = true;
610 else
611 actually_said_continue = true;
612 }
613
614 if (m_should_stop && !bp_loc_sp->GetBreakpoint().IsInternal())
615 all_stopping_locs_internal = false;
616
617 // If we are going to stop for this breakpoint, then remove the
618 // breakpoint.
619 if (callback_says_stop && bp_loc_sp &&
620 bp_loc_sp->GetBreakpoint().IsOneShot()) {
621 thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID(
622 bp_loc_sp->GetBreakpoint().GetID());
623 }
624 // Also make sure that the callback hasn't continued the target. If
625 // it did, when we'll set m_should_start to false and get out of
626 // here.
627 if (HasTargetRunSinceMe()) {
628 m_should_stop = false;
629 actually_said_continue = true;
630 break;
631 }
632 }
633 // At this point if nobody actually told us to continue, we should
634 // give the async breakpoint callback a chance to weigh in:
635 if (!actually_said_continue && !m_should_stop) {
636 m_should_stop = async_should_stop;
637 }
638 }
639 // We've figured out what this stop wants to do, so mark it as valid so
640 // we don't compute it again.
642 } else {
643 m_should_stop = true;
645 actually_hit_any_locations = true;
646 Log *log_process(GetLog(LLDBLog::Process));
647
648 LLDB_LOGF(log_process,
649 "Process::%s could not find breakpoint site id: %" PRId64
650 "...",
651 __FUNCTION__, m_value);
652 }
653
654 if ((!m_should_stop || all_stopping_locs_internal) &&
655 thread_sp->CompletedPlanOverridesBreakpoint()) {
656
657 // Override should_stop decision when we have completed step plan
658 // additionally to the breakpoint
659 m_should_stop = true;
660
661 // We know we're stopping for a completed plan and we don't want to
662 // show the breakpoint stop, so compute the public stop info immediately
663 // here.
664 thread_sp->CalculatePublicStopInfo();
665 } else if (!actually_hit_any_locations) {
666 // In the end, we didn't actually have any locations that passed their
667 // "was I hit" checks. So say we aren't stopped.
668 GetThread()->ResetStopInfo();
669 LLDB_LOGF(log, "Process::%s all locations failed condition checks.",
670 __FUNCTION__);
671 }
672
673 LLDB_LOGF(log,
674 "Process::%s returning from action with m_should_stop: %d.",
675 __FUNCTION__, m_should_stop);
676 }
677 }
678
679private:
682 return {};
683
684 ThreadSP thread_sp = GetThread();
685 if (!thread_sp)
686 return {};
687 ProcessSP process_sp = thread_sp->GetProcess();
688 if (!process_sp)
689 return {};
690
691 return process_sp->GetBreakpointSiteList().FindByID(m_value);
692 }
693
696 bool m_should_perform_action; // Since we are trying to preserve the "state"
697 // of the system even if we run functions
698 // etc. behind the users backs, we need to make sure we only REALLY perform
699 // the action once.
700 lldb::addr_t m_address; // We use this to capture the breakpoint site address
701 // when we create the StopInfo,
702 // in case somebody deletes it between the time the StopInfo is made and the
703 // description is asked for.
707 /// The StopInfoBreakpoint lives after the stop, and could get queried
708 /// at any time so we need to make sure that it keeps the breakpoints for
709 /// each of the locations it records alive while it is around. That's what
710 /// The BreakpointPreservingLocationCollection does.
712};
713
714// StopInfoWatchpoint
715
717public:
718 // Make sure watchpoint is properly disabled and subsequently enabled while
719 // performing watchpoint actions.
721 public:
723 watchpoint_sp(w_sp) {
724 if (process_sp && watchpoint_sp) {
725 const bool notify = false;
726 watchpoint_sp->TurnOnEphemeralMode();
727 process_sp->DisableWatchpoint(watchpoint_sp, notify);
728 process_sp->AddPreResumeAction(SentryPreResumeAction, this);
729 }
730 }
731
732 void DoReenable() {
733 if (process_sp && watchpoint_sp) {
734 bool was_disabled = watchpoint_sp->IsDisabledDuringEphemeralMode();
735 watchpoint_sp->TurnOffEphemeralMode();
736 const bool notify = false;
737 if (was_disabled) {
738 process_sp->DisableWatchpoint(watchpoint_sp, notify);
739 } else {
740 process_sp->EnableWatchpoint(watchpoint_sp, notify);
741 }
742 }
743 }
744
746 DoReenable();
747 if (process_sp)
748 process_sp->ClearPreResumeAction(SentryPreResumeAction, this);
749 }
750
751 static bool SentryPreResumeAction(void *sentry_void) {
752 WatchpointSentry *sentry = (WatchpointSentry *) sentry_void;
753 sentry->DoReenable();
754 return true;
755 }
756
757 private:
760 };
761
762 StopInfoWatchpoint(Thread &thread, break_id_t watch_id, bool silently_skip_wp)
763 : StopInfo(thread, watch_id), m_silently_skip_wp(silently_skip_wp) {}
764
765 ~StopInfoWatchpoint() override = default;
766
767 StopReason GetStopReason() const override { return eStopReasonWatchpoint; }
768
769 uint32_t GetStopReasonDataCount() const override { return 1; }
770 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
771 if (idx == 0)
772 return GetValue();
773 return 0;
774 }
775
776 const char *GetDescription() override {
777 if (m_description.empty()) {
778 StreamString strm;
779 strm.Printf("watchpoint %" PRIi64, m_value);
780 m_description = std::string(strm.GetString());
781 }
782 return m_description.c_str();
783 }
784
785protected:
786 using StopInfoWatchpointSP = std::shared_ptr<StopInfoWatchpoint>;
787 // This plan is used to orchestrate stepping over the watchpoint for
788 // architectures (e.g. ARM) that report the watch before running the watched
789 // access. This is the sort of job you have to defer to the thread plans,
790 // if you try to do it directly in the stop info and there are other threads
791 // that needed to process this stop you will have yanked control away from
792 // them and they won't behave correctly.
794 public:
796 StopInfoWatchpointSP stop_info_sp,
797 WatchpointSP watch_sp)
798 : ThreadPlanStepInstruction(thread, false, true, eVoteNoOpinion,
800 m_stop_info_sp(stop_info_sp), m_watch_sp(watch_sp) {
801 assert(watch_sp);
802 }
803
804 bool DoWillResume(lldb::StateType resume_state,
805 bool current_plan) override {
806 if (resume_state == eStateSuspended)
807 return true;
808
809 if (!m_did_disable_wp) {
810 GetThread().GetProcess()->DisableWatchpoint(m_watch_sp, false);
811 m_did_disable_wp = true;
812 }
813 return true;
814 }
815
816 bool DoPlanExplainsStop(Event *event_ptr) override {
818 return true;
819 StopInfoSP stop_info_sp = GetThread().GetPrivateStopInfo();
820 // lldb-server resets the stop info for threads that didn't get to run,
821 // so we might have not gotten to run, but still have a watchpoint stop
822 // reason, in which case this will indeed be for us.
823 if (stop_info_sp
824 && stop_info_sp->GetStopReason() == eStopReasonWatchpoint)
825 return true;
826 return false;
827 }
828
829 void DidPop() override {
830 // Don't artifically keep the watchpoint alive.
831 m_watch_sp.reset();
832 }
833
834 bool ShouldStop(Event *event_ptr) override {
835 bool should_stop = ThreadPlanStepInstruction::ShouldStop(event_ptr);
836 bool plan_done = MischiefManaged();
837 if (plan_done) {
838 m_stop_info_sp->SetStepOverPlanComplete();
841 }
842 return should_stop;
843 }
844
846 return true;
847 }
848
849 protected:
851 if (!m_did_disable_wp)
852 return;
853 m_did_disable_wp = true;
854 GetThread().GetProcess()->EnableWatchpoint(m_watch_sp, true);
855 }
856
857 private:
860 bool m_did_disable_wp = false;
861 };
862
863 bool ShouldStopSynchronous(Event *event_ptr) override {
864 // If we are running our step-over the watchpoint plan, stop if it's done
865 // and continue if it's not:
867 return m_should_stop;
868
869 // If we are running our step over plan, then stop here and let the regular
870 // ShouldStop figure out what we should do: Otherwise, give our plan
871 // more time to get run:
874
876 ThreadSP thread_sp(m_thread_wp.lock());
877 assert(thread_sp);
878
879 if (thread_sp->GetTemporaryResumeState() == eStateSuspended) {
880 // This is the second firing of a watchpoint so don't process it again.
881 LLDB_LOG(log, "We didn't run but stopped with a StopInfoWatchpoint, we "
882 "have already handled this one, don't do it again.");
883 m_should_stop = false;
885 return m_should_stop;
886 }
887
888 WatchpointSP wp_sp(
889 thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
890 // If we can no longer find the watchpoint, we just have to stop:
891 if (!wp_sp) {
892
893 LLDB_LOGF(log,
894 "Process::%s could not find watchpoint location id: %" PRId64
895 "...",
896 __FUNCTION__, GetValue());
897
898 m_should_stop = true;
900 return true;
901 }
902
903 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
904 StoppointCallbackContext context(event_ptr, exe_ctx, true);
905 m_should_stop = wp_sp->ShouldStop(&context);
906 if (!m_should_stop) {
907 // This won't happen at present because we only allow one watchpoint per
908 // watched range. So we won't stop at a watched address with a disabled
909 // watchpoint. If we start allowing overlapping watchpoints, then we
910 // will have to make watchpoints be real "WatchpointSite" and delegate to
911 // all the watchpoints sharing the site. In that case, the code below
912 // would be the right thing to do.
914 return m_should_stop;
915 }
916 // If this is a system where we need to execute the watchpoint by hand
917 // after the hit, queue a thread plan to do that, and then say not to stop.
918 // Otherwise, let the async action figure out whether the watchpoint should
919 // stop
920
921 ProcessSP process_sp = exe_ctx.GetProcessSP();
922 bool wp_triggers_after = process_sp->GetWatchpointReportedAfter();
923
924 if (!wp_triggers_after) {
925 // We have to step over the watchpoint before we know what to do:
926 StopInfoWatchpointSP me_as_siwp_sp
927 = std::static_pointer_cast<StopInfoWatchpoint>(shared_from_this());
928 ThreadPlanSP step_over_wp_sp =
929 std::make_shared<ThreadPlanStepOverWatchpoint>(*(thread_sp.get()),
930 me_as_siwp_sp, wp_sp);
931 // When this plan is done we want to stop, so set this as a Controlling
932 // plan.
933 step_over_wp_sp->SetIsControllingPlan(true);
934 step_over_wp_sp->SetOkayToDiscard(false);
935
937 error = thread_sp->QueueThreadPlan(step_over_wp_sp, false);
938 // If we couldn't push the thread plan, just stop here:
939 if (!error.Success()) {
940 LLDB_LOGF(log, "Could not push our step over watchpoint plan: %s",
941 error.AsCString());
942
943 m_should_stop = true;
945 return true;
946 } else {
947 // Otherwise, don't set m_should_stop, we don't know that yet. Just
948 // say we should continue, and tell the thread we really should do so:
949 thread_sp->SetShouldRunBeforePublicStop(true);
951 return false;
952 }
953 } else {
954 // We didn't have to do anything special
956 return m_should_stop;
957 }
958
959 return m_should_stop;
960 }
961
962 bool ShouldStop(Event *event_ptr) override {
963 // This just reports the work done by PerformAction or the synchronous
964 // stop. It should only ever get called after they have had a chance to
965 // run.
967 return m_should_stop;
968 }
969
970 void PerformAction(Event *event_ptr) override {
972 // We're going to calculate if we should stop or not in some way during the
973 // course of this code. Also by default we're going to stop, so set that
974 // here.
975 m_should_stop = true;
976
977
978 ThreadSP thread_sp(m_thread_wp.lock());
979 if (thread_sp) {
980
981 WatchpointSP wp_sp(
982 thread_sp->CalculateTarget()->GetWatchpointList().FindByID(
983 GetValue()));
984 if (wp_sp) {
985 // This sentry object makes sure the current watchpoint is disabled
986 // while performing watchpoint actions, and it is then enabled after we
987 // are finished.
988 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
989 ProcessSP process_sp = exe_ctx.GetProcessSP();
990
991 WatchpointSentry sentry(process_sp, wp_sp);
992
993 if (m_silently_skip_wp) {
994 m_should_stop = false;
995 wp_sp->UndoHitCount();
996 }
997
998 if (wp_sp->GetHitCount() <= wp_sp->GetIgnoreCount()) {
999 m_should_stop = false;
1001 }
1002
1003 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
1004
1005 if (m_should_stop && wp_sp->GetConditionText() != nullptr) {
1006 // We need to make sure the user sees any parse errors in their
1007 // condition, so we'll hook the constructor errors up to the
1008 // debugger's Async I/O.
1009 ExpressionResults result_code;
1010 EvaluateExpressionOptions expr_options;
1011 expr_options.SetUnwindOnError(true);
1012 expr_options.SetIgnoreBreakpoints(true);
1013 ValueObjectSP result_value_sp;
1014 result_code = UserExpression::Evaluate(
1015 exe_ctx, expr_options, wp_sp->GetConditionText(),
1016 llvm::StringRef(), result_value_sp);
1017
1018 if (result_code == eExpressionCompleted) {
1019 if (result_value_sp) {
1020 Scalar scalar_value;
1021 if (result_value_sp->ResolveValue(scalar_value)) {
1022 if (scalar_value.ULongLong(1) == 0) {
1023 // The condition failed, which we consider "not having hit
1024 // the watchpoint" so undo the hit count here.
1025 wp_sp->UndoHitCount();
1026 m_should_stop = false;
1027 } else
1028 m_should_stop = true;
1029 LLDB_LOGF(log,
1030 "Condition successfully evaluated, result is %s.\n",
1031 m_should_stop ? "true" : "false");
1032 } else {
1033 m_should_stop = true;
1034 LLDB_LOGF(
1035 log,
1036 "Failed to get an integer result from the expression.");
1037 }
1038 }
1039 } else {
1040 const char *err_str = "<unknown error>";
1041 if (result_value_sp)
1042 err_str = result_value_sp->GetError().AsCString();
1043
1044 LLDB_LOGF(log, "Error evaluating condition: \"%s\"\n", err_str);
1045
1046 StreamString strm;
1047 strm << "stopped due to an error evaluating condition of "
1048 "watchpoint ";
1049 wp_sp->GetDescription(&strm, eDescriptionLevelBrief);
1050 strm << ": \"" << wp_sp->GetConditionText() << "\"\n";
1051 strm << err_str;
1052
1053 Debugger::ReportError(strm.GetString().str(),
1054 exe_ctx.GetTargetRef().GetDebugger().GetID());
1055 }
1056 }
1057
1058 // If the condition says to stop, we run the callback to further decide
1059 // whether to stop.
1060 if (m_should_stop) {
1061 // FIXME: For now the callbacks have to run in async mode - the
1062 // first time we restart we need
1063 // to get out of there. So set it here.
1064 // When we figure out how to nest watchpoint hits then this will
1065 // change.
1066
1067 bool old_async = debugger.GetAsyncExecution();
1068 debugger.SetAsyncExecution(true);
1069
1070 StoppointCallbackContext context(event_ptr, exe_ctx, false);
1071 bool stop_requested = wp_sp->InvokeCallback(&context);
1072
1073 debugger.SetAsyncExecution(old_async);
1074
1075 // Also make sure that the callback hasn't continued the target. If
1076 // it did, when we'll set m_should_stop to false and get out of here.
1077 if (HasTargetRunSinceMe())
1078 m_should_stop = false;
1079
1080 if (m_should_stop && !stop_requested) {
1081 // We have been vetoed by the callback mechanism.
1082 m_should_stop = false;
1083 }
1084 }
1085
1086 // Don't stop if the watched region value is unmodified, and
1087 // this is a Modify-type watchpoint.
1088 if (m_should_stop && !wp_sp->WatchedValueReportable(exe_ctx)) {
1089 wp_sp->UndoHitCount();
1090 m_should_stop = false;
1091 }
1092
1093 // Finally, if we are going to stop, print out the new & old values:
1094 if (m_should_stop) {
1095 wp_sp->CaptureWatchedValue(exe_ctx);
1096
1097 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
1098 StreamUP output_up = debugger.GetAsyncOutputStream();
1099 if (wp_sp->DumpSnapshots(output_up.get()))
1100 output_up->EOL();
1101 }
1102
1103 } else {
1104 Log *log_process(GetLog(LLDBLog::Process));
1105
1106 LLDB_LOGF(log_process,
1107 "Process::%s could not find watchpoint id: %" PRId64 "...",
1108 __FUNCTION__, m_value);
1109 }
1110 LLDB_LOGF(log,
1111 "Process::%s returning from action with m_should_stop: %d.",
1112 __FUNCTION__, m_should_stop);
1113
1115 }
1116 }
1117
1118private:
1123
1124 bool m_should_stop = false;
1126 // A false watchpoint hit has happened -
1127 // the thread stopped with a watchpoint
1128 // hit notification, but the watched region
1129 // was not actually accessed (as determined
1130 // by the gdb stub we're talking to).
1131 // Continue past this watchpoint without
1132 // notifying the user; on some targets this
1133 // may mean disable wp, instruction step,
1134 // re-enable wp, continue.
1135 // On others, just continue.
1139};
1140
1141// StopInfoUnixSignal
1142
1144public:
1145 StopInfoUnixSignal(Thread &thread, int signo, const char *description,
1146 std::optional<int> code)
1147 : StopInfo(thread, signo), m_code(code) {
1148 SetDescription(description);
1149 }
1150
1151 ~StopInfoUnixSignal() override = default;
1152
1153 StopReason GetStopReason() const override { return eStopReasonSignal; }
1154
1155 bool ShouldStopSynchronous(Event *event_ptr) override {
1156 ThreadSP thread_sp(m_thread_wp.lock());
1157 if (thread_sp)
1158 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
1159 return false;
1160 }
1161
1162 bool ShouldStop(Event *event_ptr) override { return IsShouldStopSignal(); }
1163
1164 // If should stop returns false, check if we should notify of this event
1165 bool DoShouldNotify(Event *event_ptr) override {
1166 ThreadSP thread_sp(m_thread_wp.lock());
1167 if (thread_sp) {
1168 bool should_notify =
1169 thread_sp->GetProcess()->GetUnixSignals()->GetShouldNotify(m_value);
1170 if (should_notify) {
1171 StreamString strm;
1172 strm.Format(
1173 "thread {0:d} received signal: {1}", thread_sp->GetIndexID(),
1174 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsStringRef(
1175 m_value));
1177 strm.GetData());
1178 }
1179 return should_notify;
1180 }
1181 return true;
1182 }
1183
1184 void WillResume(lldb::StateType resume_state) override {
1185 ThreadSP thread_sp(m_thread_wp.lock());
1186 if (thread_sp) {
1187 if (!thread_sp->GetProcess()->GetUnixSignals()->GetShouldSuppress(
1188 m_value))
1189 thread_sp->SetResumeSignal(m_value);
1190 }
1191 }
1192
1193 const char *GetDescription() override {
1194 if (m_description.empty()) {
1195 ThreadSP thread_sp(m_thread_wp.lock());
1196 if (thread_sp) {
1197 UnixSignalsSP unix_signals = thread_sp->GetProcess()->GetUnixSignals();
1198 StreamString strm;
1199 strm << "signal ";
1200
1201 std::string signal_name =
1202 unix_signals->GetSignalDescription(m_value, m_code);
1203 if (signal_name.size())
1204 strm << signal_name;
1205 else
1206 strm.Printf("%" PRIi64, m_value);
1207
1208 m_description = std::string(strm.GetString());
1209 }
1210 }
1211 return m_description.c_str();
1212 }
1213
1214 bool ShouldSelect() const override { return IsShouldStopSignal(); }
1215
1216 uint32_t GetStopReasonDataCount() const override { return 1; }
1217 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1218 if (idx == 0)
1219 return GetValue();
1220 return 0;
1221 }
1222
1223private:
1224 // In siginfo_t terms, if m_value is si_signo, m_code is si_code.
1225 std::optional<int> m_code;
1226
1227 bool IsShouldStopSignal() const {
1228 if (ThreadSP thread_sp = m_thread_wp.lock())
1229 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
1230 return false;
1231 }
1232};
1233
1234// StopInfoInterrupt
1235
1237public:
1238 StopInfoInterrupt(Thread &thread, int signo, const char *description)
1239 : StopInfo(thread, signo) {
1240 SetDescription(description);
1241 }
1242
1243 ~StopInfoInterrupt() override = default;
1244
1245 StopReason GetStopReason() const override {
1247 }
1248
1249 const char *GetDescription() override {
1250 if (m_description.empty()) {
1251 m_description = "async interrupt";
1252 }
1253 return m_description.c_str();
1254 }
1255
1256 uint32_t GetStopReasonDataCount() const override { return 1; }
1257 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1258 if (idx == 0)
1259 return GetValue();
1260 else
1261 return 0;
1262 }
1263};
1264
1265// StopInfoTrace
1266
1267class StopInfoTrace : public StopInfo {
1268public:
1270
1271 ~StopInfoTrace() override = default;
1272
1273 StopReason GetStopReason() const override { return eStopReasonTrace; }
1274
1275 const char *GetDescription() override {
1276 if (m_description.empty())
1277 return "trace";
1278 else
1279 return m_description.c_str();
1280 }
1281
1282 std::optional<uint32_t>
1283 GetSuggestedStackFrameIndex(bool inlined_stack) override {
1284 // Trace only knows how to adjust inlined stacks:
1285 if (!inlined_stack)
1286 return {};
1287
1288 ThreadSP thread_sp = GetThread();
1289 StackFrameSP frame_0_sp = thread_sp->GetStackFrameAtIndex(0);
1290 if (!frame_0_sp)
1291 return {};
1292 if (!frame_0_sp->IsInlined())
1293 return {};
1294 Block *block_ptr = frame_0_sp->GetFrameBlock();
1295 if (!block_ptr)
1296 return {};
1297 Address pc_address = frame_0_sp->GetFrameCodeAddress();
1298 AddressRange containing_range;
1299 if (!block_ptr->GetRangeContainingAddress(pc_address, containing_range) ||
1300 pc_address != containing_range.GetBaseAddress())
1301 return {};
1302
1303 int num_inlined_functions = 0;
1304
1305 for (Block *container_ptr = block_ptr->GetInlinedParent();
1306 container_ptr != nullptr;
1307 container_ptr = container_ptr->GetInlinedParent()) {
1308 if (!container_ptr->GetRangeContainingAddress(pc_address,
1309 containing_range))
1310 break;
1311 if (pc_address != containing_range.GetBaseAddress())
1312 break;
1313
1314 num_inlined_functions++;
1315 }
1316 inlined_stack = true;
1317 return num_inlined_functions + 1;
1318 }
1319};
1320
1321// StopInfoException
1322
1324public:
1325 StopInfoException(Thread &thread, const char *description)
1326 : StopInfo(thread, LLDB_INVALID_UID) {
1327 if (description)
1328 SetDescription(description);
1329 }
1330
1331 ~StopInfoException() override = default;
1332
1333 StopReason GetStopReason() const override { return eStopReasonException; }
1334
1335 const char *GetDescription() override {
1336 if (m_description.empty())
1337 return "exception";
1338 else
1339 return m_description.c_str();
1340 }
1341 uint32_t GetStopReasonDataCount() const override { return 1; }
1342 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1343 if (idx == 0)
1344 return GetValue();
1345 else
1346 return 0;
1347 }
1348};
1349
1350// StopInfoProcessorTrace
1351
1353public:
1354 StopInfoProcessorTrace(Thread &thread, const char *description)
1355 : StopInfo(thread, LLDB_INVALID_UID) {
1356 if (description)
1357 SetDescription(description);
1358 }
1359
1360 ~StopInfoProcessorTrace() override = default;
1361
1362 StopReason GetStopReason() const override {
1364 }
1365
1366 const char *GetDescription() override {
1367 if (m_description.empty())
1368 return "processor trace event";
1369 else
1370 return m_description.c_str();
1371 }
1372};
1373
1374// StopInfoHistoryBoundary
1375
1377public:
1378 StopInfoHistoryBoundary(Thread &thread, const char *description)
1379 : StopInfo(thread, LLDB_INVALID_UID) {
1380 if (description)
1381 SetDescription(description);
1382 }
1383
1384 ~StopInfoHistoryBoundary() override = default;
1385
1386 StopReason GetStopReason() const override {
1388 }
1389
1390 const char *GetDescription() override {
1391 if (m_description.empty())
1392 return "history boundary";
1393 return m_description.c_str();
1394 }
1395};
1396
1397// StopInfoThreadPlan
1398
1400public:
1401 StopInfoThreadPlan(ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp,
1402 ExpressionVariableSP &expression_variable_sp)
1403 : StopInfo(plan_sp->GetThread(), LLDB_INVALID_UID), m_plan_sp(plan_sp),
1404 m_return_valobj_sp(return_valobj_sp),
1405 m_expression_variable_sp(expression_variable_sp) {}
1406
1407 ~StopInfoThreadPlan() override = default;
1408
1410
1411 const char *GetDescription() override {
1412 if (m_description.empty()) {
1413 StreamString strm;
1414 m_plan_sp->GetDescription(&strm, eDescriptionLevelBrief);
1415 m_description = std::string(strm.GetString());
1416 }
1417 return m_description.c_str();
1418 }
1419
1421
1425
1426protected:
1427 bool ShouldStop(Event *event_ptr) override {
1428 if (m_plan_sp)
1429 return m_plan_sp->ShouldStop(event_ptr);
1430 else
1431 return StopInfo::ShouldStop(event_ptr);
1432 }
1433
1434private:
1438};
1439
1440// StopInfoExec
1441
1442class StopInfoExec : public StopInfo {
1443public:
1445
1446 ~StopInfoExec() override = default;
1447
1448 bool ShouldStop(Event *event_ptr) override {
1449 ThreadSP thread_sp(m_thread_wp.lock());
1450 if (thread_sp)
1451 return thread_sp->GetProcess()->GetStopOnExec();
1452 return false;
1453 }
1454
1455 StopReason GetStopReason() const override { return eStopReasonExec; }
1456
1457 const char *GetDescription() override { return "exec"; }
1458
1459protected:
1460 void PerformAction(Event *event_ptr) override {
1461 // Only perform the action once
1463 return;
1464 m_performed_action = true;
1465 ThreadSP thread_sp(m_thread_wp.lock());
1466 if (thread_sp)
1467 thread_sp->GetProcess()->DidExec();
1468 }
1469
1471};
1472
1473// StopInfoFork
1474
1475class StopInfoFork : public StopInfo {
1476public:
1477 StopInfoFork(Thread &thread, lldb::pid_t child_pid, lldb::tid_t child_tid)
1478 : StopInfo(thread, child_pid), m_child_pid(child_pid),
1479 m_child_tid(child_tid) {}
1480
1481 ~StopInfoFork() override = default;
1482
1483 bool ShouldStop(Event *event_ptr) override { return false; }
1484
1485 StopReason GetStopReason() const override { return eStopReasonFork; }
1486
1487 const char *GetDescription() override { return "fork"; }
1488
1489 uint32_t GetStopReasonDataCount() const override { return 1; }
1490 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1491 if (idx == 0)
1492 return GetValue();
1493 else
1494 return 0;
1495 }
1496
1497protected:
1498 void PerformAction(Event *event_ptr) override {
1499 // Only perform the action once
1501 return;
1502 m_performed_action = true;
1503 ThreadSP thread_sp(m_thread_wp.lock());
1504 if (thread_sp)
1505 thread_sp->GetProcess()->DidFork(m_child_pid, m_child_tid);
1506 }
1507
1509
1510private:
1513};
1514
1515// StopInfoVFork
1516
1517class StopInfoVFork : public StopInfo {
1518public:
1519 StopInfoVFork(Thread &thread, lldb::pid_t child_pid, lldb::tid_t child_tid)
1520 : StopInfo(thread, child_pid), m_child_pid(child_pid),
1521 m_child_tid(child_tid) {}
1522
1523 ~StopInfoVFork() override = default;
1524
1525 bool ShouldStop(Event *event_ptr) override { return false; }
1526
1527 StopReason GetStopReason() const override { return eStopReasonVFork; }
1528
1529 const char *GetDescription() override { return "vfork"; }
1530
1531 uint32_t GetStopReasonDataCount() const override { return 1; }
1532 uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1533 if (idx == 0)
1534 return GetValue();
1535 return 0;
1536 }
1537
1538protected:
1539 void PerformAction(Event *event_ptr) override {
1540 // Only perform the action once
1542 return;
1543 m_performed_action = true;
1544 ThreadSP thread_sp(m_thread_wp.lock());
1545 if (thread_sp)
1546 thread_sp->GetProcess()->DidVFork(m_child_pid, m_child_tid);
1547 }
1548
1550
1551private:
1554};
1555
1556// StopInfoVForkDone
1557
1559public:
1560 StopInfoVForkDone(Thread &thread) : StopInfo(thread, 0) {}
1561
1562 ~StopInfoVForkDone() override = default;
1563
1564 bool ShouldStop(Event *event_ptr) override { return false; }
1565
1566 StopReason GetStopReason() const override { return eStopReasonVForkDone; }
1567
1568 const char *GetDescription() override { return "vforkdone"; }
1569
1570protected:
1571 void PerformAction(Event *event_ptr) override {
1572 // Only perform the action once
1574 return;
1575 m_performed_action = true;
1576 ThreadSP thread_sp(m_thread_wp.lock());
1577 if (thread_sp)
1578 thread_sp->GetProcess()->DidVForkDone();
1579 }
1580
1582};
1583
1584} // namespace lldb_private
1585
1587 break_id_t break_id) {
1588 thread.SetThreadHitBreakpointSite();
1589
1590 return std::make_shared<StopInfoBreakpoint>(thread, break_id);
1591}
1592
1594 break_id_t break_id,
1595 bool should_stop) {
1596 return std::make_shared<StopInfoBreakpoint>(thread, break_id, should_stop);
1597}
1598
1599// LWP_TODO: We'll need a CreateStopReasonWithWatchpointResourceID akin
1600// to CreateStopReasonWithBreakpointSiteID
1602 break_id_t watch_id,
1603 bool silently_continue) {
1604 return std::make_shared<StopInfoWatchpoint>(thread, watch_id,
1605 silently_continue);
1606}
1607
1609 const char *description,
1610 std::optional<int> code) {
1611 thread.GetProcess()->GetUnixSignals()->IncrementSignalHitCount(signo);
1612 return std::make_shared<StopInfoUnixSignal>(thread, signo, description, code);
1613}
1614
1616 const char *description) {
1617 return std::make_shared<StopInfoInterrupt>(thread, signo, description);
1618}
1619
1621 return std::make_shared<StopInfoTrace>(thread);
1622}
1623
1625 ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp,
1626 ExpressionVariableSP expression_variable_sp) {
1627 return std::make_shared<StopInfoThreadPlan>(plan_sp, return_valobj_sp,
1628 expression_variable_sp);
1629}
1630
1632 const char *description) {
1633 return std::make_shared<StopInfoException>(thread, description);
1634}
1635
1637 const char *description) {
1638 return std::make_shared<StopInfoProcessorTrace>(thread, description);
1639}
1640
1642 const char *description) {
1643 return std::make_shared<StopInfoHistoryBoundary>(thread, description);
1644}
1645
1647 return std::make_shared<StopInfoExec>(thread);
1648}
1649
1651 lldb::pid_t child_pid,
1652 lldb::tid_t child_tid) {
1653 return std::make_shared<StopInfoFork>(thread, child_pid, child_tid);
1654}
1655
1656
1658 lldb::pid_t child_pid,
1659 lldb::tid_t child_tid) {
1660 return std::make_shared<StopInfoVFork>(thread, child_pid, child_tid);
1661}
1662
1664 return std::make_shared<StopInfoVForkDone>(thread);
1665}
1666
1668 if (stop_info_sp &&
1669 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1670 StopInfoThreadPlan *plan_stop_info =
1671 static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1672 return plan_stop_info->GetReturnValueObject();
1673 } else
1674 return ValueObjectSP();
1675}
1676
1678 if (stop_info_sp &&
1679 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1680 StopInfoThreadPlan *plan_stop_info =
1681 static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1682 return plan_stop_info->GetExpressionVariable();
1683 } else
1684 return ExpressionVariableSP();
1685}
1686
1689 lldb::addr_t *crashing_address) {
1690 if (!stop_info_sp) {
1691 return ValueObjectSP();
1692 }
1693
1694 const char *description = stop_info_sp->GetDescription();
1695 if (!description) {
1696 return ValueObjectSP();
1697 }
1698
1699 ThreadSP thread_sp = stop_info_sp->GetThread();
1700 if (!thread_sp) {
1701 return ValueObjectSP();
1702 }
1703
1704 StackFrameSP frame_sp =
1705 thread_sp->GetSelectedFrame(DoNoSelectMostRelevantFrame);
1706
1707 if (!frame_sp) {
1708 return ValueObjectSP();
1709 }
1710
1711 const char address_string[] = "address=";
1712
1713 const char *address_loc = strstr(description, address_string);
1714 if (!address_loc) {
1715 return ValueObjectSP();
1716 }
1717
1718 address_loc += (sizeof(address_string) - 1);
1719
1720 uint64_t address = strtoull(address_loc, nullptr, 0);
1721 if (crashing_address) {
1722 *crashing_address = address;
1723 }
1724
1725 return frame_sp->GuessValueForAddress(address);
1726}
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:376
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:80
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:371
void SetIgnoreBreakpoints(bool ignore=false)
Definition Target.h:375
"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:282
bool GetIgnoreBreakpointsInExpressions() const
Definition Process.cpp:241
static void AddRestartedReason(Event *event_ptr, const char *reason)
Definition Process.cpp:4500
A plug-in interface definition class for debugging a process.
Definition Process.h:357
const ProcessModID & GetModIDRef() const
Definition Process.h:1473
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1270
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:195
bool Success() const
Test for success condition.
Definition Status.cpp:304
void PerformAction(Event *event_ptr) override
Definition StopInfo.cpp:319
BreakpointSiteSP GetBreakpointSiteSP() const
Definition StopInfo.cpp:680
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:711
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:795
bool DoWillResume(lldb::StateType resume_state, bool current_plan) override
Definition StopInfo.cpp:804
WatchpointSentry(ProcessSP p_sp, WatchpointSP w_sp)
Definition StopInfo.cpp:722
static bool SentryPreResumeAction(void *sentry_void)
Definition StopInfo.cpp:751
~StopInfoWatchpoint() override=default
std::shared_ptr< StopInfoWatchpoint > StopInfoWatchpointSP
Definition StopInfo.cpp:786
StopInfoWatchpoint(Thread &thread, break_id_t watch_id, bool silently_skip_wp)
Definition StopInfo.cpp:762
uint32_t GetStopReasonDataCount() const override
Definition StopInfo.cpp:769
const char * GetDescription() override
Definition StopInfo.cpp:776
void PerformAction(Event *event_ptr) override
Definition StopInfo.cpp:970
bool ShouldStop(Event *event_ptr) override
Definition StopInfo.cpp:962
bool ShouldStopSynchronous(Event *event_ptr) override
Definition StopInfo.cpp:863
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override
Definition StopInfo.cpp:770
StopReason GetStopReason() const override
Definition StopInfo.cpp:767
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)
Definition Stream.h:344
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:1108
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:386
void SetStopInfo(const lldb::StopInfoSP &stop_info_sp)
Definition Thread.cpp:464
lldb::ProcessSP GetProcess() const
Definition Thread.h:158
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:86
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