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
11 #include "lldb/Breakpoint/Breakpoint.h"
12 #include "lldb/Breakpoint/BreakpointLocation.h"
13 #include "lldb/Breakpoint/StoppointCallbackContext.h"
14 #include "lldb/Breakpoint/Watchpoint.h"
15 #include "lldb/Breakpoint/WatchpointResource.h"
16 #include "lldb/Core/Debugger.h"
17 #include "lldb/Core/ValueObject.h"
18 #include "lldb/Expression/UserExpression.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/StopInfo.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Target/Thread.h"
23 #include "lldb/Target/ThreadPlan.h"
24 #include "lldb/Target/ThreadPlanStepInstruction.h"
25 #include "lldb/Target/UnixSignals.h"
26 #include "lldb/Utility/LLDBLog.h"
27 #include "lldb/Utility/Log.h"
28 #include "lldb/Utility/StreamString.h"
29
30 using namespace lldb;
31 using namespace lldb_private;
32
StopInfo(Thread & thread,uint64_t value)33 StopInfo::StopInfo(Thread &thread, uint64_t value)
34 : m_thread_wp(thread.shared_from_this()),
35 m_stop_id(thread.GetProcess()->GetStopID()),
36 m_resume_id(thread.GetProcess()->GetResumeID()), m_value(value),
37 m_description(), m_override_should_notify(eLazyBoolCalculate),
38 m_override_should_stop(eLazyBoolCalculate), m_extended_info() {}
39
IsValid() const40 bool StopInfo::IsValid() const {
41 ThreadSP thread_sp(m_thread_wp.lock());
42 if (thread_sp)
43 return thread_sp->GetProcess()->GetStopID() == m_stop_id;
44 return false;
45 }
46
MakeStopInfoValid()47 void StopInfo::MakeStopInfoValid() {
48 ThreadSP thread_sp(m_thread_wp.lock());
49 if (thread_sp) {
50 m_stop_id = thread_sp->GetProcess()->GetStopID();
51 m_resume_id = thread_sp->GetProcess()->GetResumeID();
52 }
53 }
54
HasTargetRunSinceMe()55 bool StopInfo::HasTargetRunSinceMe() {
56 ThreadSP thread_sp(m_thread_wp.lock());
57
58 if (thread_sp) {
59 lldb::StateType ret_type = thread_sp->GetProcess()->GetPrivateState();
60 if (ret_type == eStateRunning) {
61 return true;
62 } else if (ret_type == eStateStopped) {
63 // This is a little tricky. We want to count "run and stopped again
64 // before you could ask this question as a "TRUE" answer to
65 // HasTargetRunSinceMe. But we don't want to include any running of the
66 // target done for expressions. So we track both resumes, and resumes
67 // caused by expressions, and check if there are any resumes
68 // NOT caused
69 // by expressions.
70
71 uint32_t curr_resume_id = thread_sp->GetProcess()->GetResumeID();
72 uint32_t last_user_expression_id =
73 thread_sp->GetProcess()->GetLastUserExpressionResumeID();
74 if (curr_resume_id == m_resume_id) {
75 return false;
76 } else if (curr_resume_id > last_user_expression_id) {
77 return true;
78 }
79 }
80 }
81 return false;
82 }
83
84 // StopInfoBreakpoint
85
86 namespace lldb_private {
87 class StopInfoBreakpoint : public StopInfo {
88 public:
StopInfoBreakpoint(Thread & thread,break_id_t break_id)89 StopInfoBreakpoint(Thread &thread, break_id_t break_id)
90 : StopInfo(thread, break_id), m_should_stop(false),
91 m_should_stop_is_valid(false), m_should_perform_action(true),
92 m_address(LLDB_INVALID_ADDRESS), m_break_id(LLDB_INVALID_BREAK_ID),
93 m_was_all_internal(false), m_was_one_shot(false) {
94 StoreBPInfo();
95 }
96
StopInfoBreakpoint(Thread & thread,break_id_t break_id,bool should_stop)97 StopInfoBreakpoint(Thread &thread, break_id_t break_id, bool should_stop)
98 : StopInfo(thread, break_id), m_should_stop(should_stop),
99 m_should_stop_is_valid(true), m_should_perform_action(true),
100 m_address(LLDB_INVALID_ADDRESS), m_break_id(LLDB_INVALID_BREAK_ID),
101 m_was_all_internal(false), m_was_one_shot(false) {
102 StoreBPInfo();
103 }
104
105 ~StopInfoBreakpoint() override = default;
106
StoreBPInfo()107 void StoreBPInfo() {
108 ThreadSP thread_sp(m_thread_wp.lock());
109 if (thread_sp) {
110 BreakpointSiteSP bp_site_sp(
111 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
112 if (bp_site_sp) {
113 uint32_t num_constituents = bp_site_sp->GetNumberOfConstituents();
114 if (num_constituents == 1) {
115 BreakpointLocationSP bp_loc_sp = bp_site_sp->GetConstituentAtIndex(0);
116 if (bp_loc_sp) {
117 Breakpoint & bkpt = bp_loc_sp->GetBreakpoint();
118 m_break_id = bkpt.GetID();
119 m_was_one_shot = bkpt.IsOneShot();
120 m_was_all_internal = bkpt.IsInternal();
121 }
122 } else {
123 m_was_all_internal = true;
124 for (uint32_t i = 0; i < num_constituents; i++) {
125 if (!bp_site_sp->GetConstituentAtIndex(i)
126 ->GetBreakpoint()
127 .IsInternal()) {
128 m_was_all_internal = false;
129 break;
130 }
131 }
132 }
133 m_address = bp_site_sp->GetLoadAddress();
134 }
135 }
136 }
137
IsValidForOperatingSystemThread(Thread & thread)138 bool IsValidForOperatingSystemThread(Thread &thread) override {
139 ProcessSP process_sp(thread.GetProcess());
140 if (process_sp) {
141 BreakpointSiteSP bp_site_sp(
142 process_sp->GetBreakpointSiteList().FindByID(m_value));
143 if (bp_site_sp)
144 return bp_site_sp->ValidForThisThread(thread);
145 }
146 return false;
147 }
148
GetStopReason() const149 StopReason GetStopReason() const override { return eStopReasonBreakpoint; }
150
ShouldStopSynchronous(Event * event_ptr)151 bool ShouldStopSynchronous(Event *event_ptr) override {
152 ThreadSP thread_sp(m_thread_wp.lock());
153 if (thread_sp) {
154 if (!m_should_stop_is_valid) {
155 // Only check once if we should stop at a breakpoint
156 BreakpointSiteSP bp_site_sp(
157 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
158 if (bp_site_sp) {
159 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
160 StoppointCallbackContext context(event_ptr, exe_ctx, true);
161 bp_site_sp->BumpHitCounts();
162 m_should_stop = bp_site_sp->ShouldStop(&context);
163 } else {
164 Log *log = GetLog(LLDBLog::Process);
165
166 LLDB_LOGF(log,
167 "Process::%s could not find breakpoint site id: %" PRId64
168 "...",
169 __FUNCTION__, m_value);
170
171 m_should_stop = true;
172 }
173 m_should_stop_is_valid = true;
174 }
175 return m_should_stop;
176 }
177 return false;
178 }
179
DoShouldNotify(Event * event_ptr)180 bool DoShouldNotify(Event *event_ptr) override {
181 return !m_was_all_internal;
182 }
183
GetDescription()184 const char *GetDescription() override {
185 if (m_description.empty()) {
186 ThreadSP thread_sp(m_thread_wp.lock());
187 if (thread_sp) {
188 BreakpointSiteSP bp_site_sp(
189 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
190 if (bp_site_sp) {
191 StreamString strm;
192 // If we have just hit an internal breakpoint, and it has a kind
193 // description, print that instead of the full breakpoint printing:
194 if (bp_site_sp->IsInternal()) {
195 size_t num_constituents = bp_site_sp->GetNumberOfConstituents();
196 for (size_t idx = 0; idx < num_constituents; idx++) {
197 const char *kind = bp_site_sp->GetConstituentAtIndex(idx)
198 ->GetBreakpoint()
199 .GetBreakpointKind();
200 if (kind != nullptr) {
201 m_description.assign(kind);
202 return kind;
203 }
204 }
205 }
206
207 strm.Printf("breakpoint ");
208 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
209 m_description = std::string(strm.GetString());
210 } else {
211 StreamString strm;
212 if (m_break_id != LLDB_INVALID_BREAK_ID) {
213 BreakpointSP break_sp =
214 thread_sp->GetProcess()->GetTarget().GetBreakpointByID(
215 m_break_id);
216 if (break_sp) {
217 if (break_sp->IsInternal()) {
218 const char *kind = break_sp->GetBreakpointKind();
219 if (kind)
220 strm.Printf("internal %s breakpoint(%d).", kind, m_break_id);
221 else
222 strm.Printf("internal breakpoint(%d).", m_break_id);
223 } else {
224 strm.Printf("breakpoint %d.", m_break_id);
225 }
226 } else {
227 if (m_was_one_shot)
228 strm.Printf("one-shot breakpoint %d", m_break_id);
229 else
230 strm.Printf("breakpoint %d which has been deleted.",
231 m_break_id);
232 }
233 } else if (m_address == LLDB_INVALID_ADDRESS)
234 strm.Printf("breakpoint site %" PRIi64
235 " which has been deleted - unknown address",
236 m_value);
237 else
238 strm.Printf("breakpoint site %" PRIi64
239 " which has been deleted - was at 0x%" PRIx64,
240 m_value, m_address);
241
242 m_description = std::string(strm.GetString());
243 }
244 }
245 }
246 return m_description.c_str();
247 }
248
249 protected:
ShouldStop(Event * event_ptr)250 bool ShouldStop(Event *event_ptr) override {
251 // This just reports the work done by PerformAction or the synchronous
252 // stop. It should only ever get called after they have had a chance to
253 // run.
254 assert(m_should_stop_is_valid);
255 return m_should_stop;
256 }
257
PerformAction(Event * event_ptr)258 void PerformAction(Event *event_ptr) override {
259 if (!m_should_perform_action)
260 return;
261 m_should_perform_action = false;
262 bool all_stopping_locs_internal = true;
263
264 ThreadSP thread_sp(m_thread_wp.lock());
265
266 if (thread_sp) {
267 Log *log = GetLog(LLDBLog::Breakpoints | LLDBLog::Step);
268
269 if (!thread_sp->IsValid()) {
270 // This shouldn't ever happen, but just in case, don't do more harm.
271 if (log) {
272 LLDB_LOGF(log, "PerformAction got called with an invalid thread.");
273 }
274 m_should_stop = true;
275 m_should_stop_is_valid = true;
276 return;
277 }
278
279 BreakpointSiteSP bp_site_sp(
280 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
281 std::unordered_set<break_id_t> precondition_breakpoints;
282 // Breakpoints that fail their condition check are not considered to
283 // have been hit. If the only locations at this site have failed their
284 // conditions, we should change the stop-info to none. Otherwise, if we
285 // hit another breakpoint on a different thread which does stop, users
286 // will see a breakpont hit with a failed condition, which is wrong.
287 // Use this variable to tell us if that is true.
288 bool actually_hit_any_locations = false;
289 if (bp_site_sp) {
290 // Let's copy the constituents list out of the site and store them in a
291 // local list. That way if one of the breakpoint actions changes the
292 // site, then we won't be operating on a bad list.
293 BreakpointLocationCollection site_locations;
294 size_t num_constituents =
295 bp_site_sp->CopyConstituentsList(site_locations);
296
297 if (num_constituents == 0) {
298 m_should_stop = true;
299 actually_hit_any_locations = true; // We're going to stop, don't
300 // change the stop info.
301 } else {
302 // We go through each location, and test first its precondition -
303 // this overrides everything. Note, we only do this once per
304 // breakpoint - not once per location... Then check the condition.
305 // If the condition says to stop, then we run the callback for that
306 // location. If that callback says to stop as well, then we set
307 // m_should_stop to true; we are going to stop. But we still want to
308 // give all the breakpoints whose conditions say we are going to stop
309 // a chance to run their callbacks. Of course if any callback
310 // restarts the target by putting "continue" in the callback, then
311 // we're going to restart, without running the rest of the callbacks.
312 // And in this case we will end up not stopping even if another
313 // location said we should stop. But that's better than not running
314 // all the callbacks.
315
316 // There's one other complication here. We may have run an async
317 // breakpoint callback that said we should stop. We only want to
318 // override that if another breakpoint action says we shouldn't
319 // stop. If nobody else has an opinion, then we should stop if the
320 // async callback says we should. An example of this is the async
321 // shared library load notification breakpoint and the setting
322 // stop-on-sharedlibrary-events.
323 // We'll keep the async value in async_should_stop, and track whether
324 // anyone said we should NOT stop in actually_said_continue.
325 bool async_should_stop = false;
326 if (m_should_stop_is_valid)
327 async_should_stop = m_should_stop;
328 bool actually_said_continue = false;
329
330 m_should_stop = false;
331
332 // We don't select threads as we go through them testing breakpoint
333 // conditions and running commands. So we need to set the thread for
334 // expression evaluation here:
335 ThreadList::ExpressionExecutionThreadPusher thread_pusher(thread_sp);
336
337 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
338 Process *process = exe_ctx.GetProcessPtr();
339 if (process->GetModIDRef().IsRunningExpression()) {
340 // If we are in the middle of evaluating an expression, don't run
341 // asynchronous breakpoint commands or expressions. That could
342 // lead to infinite recursion if the command or condition re-calls
343 // the function with this breakpoint.
344 // TODO: We can keep a list of the breakpoints we've seen while
345 // running expressions in the nested
346 // PerformAction calls that can arise when the action runs a
347 // function that hits another breakpoint, and only stop running
348 // commands when we see the same breakpoint hit a second time.
349
350 m_should_stop_is_valid = true;
351
352 // It is possible that the user has a breakpoint at the same site
353 // as the completed plan had (e.g. user has a breakpoint
354 // on a module entry point, and `ThreadPlanCallFunction` ends
355 // also there). We can't find an internal breakpoint in the loop
356 // later because it was already removed on the plan completion.
357 // So check if the plan was completed, and stop if so.
358 if (thread_sp->CompletedPlanOverridesBreakpoint()) {
359 m_should_stop = true;
360 thread_sp->ResetStopInfo();
361 return;
362 }
363
364 LLDB_LOGF(log, "StopInfoBreakpoint::PerformAction - Hit a "
365 "breakpoint while running an expression,"
366 " not running commands to avoid recursion.");
367 bool ignoring_breakpoints =
368 process->GetIgnoreBreakpointsInExpressions();
369 // Internal breakpoints should be allowed to do their job, we
370 // can make sure they don't do anything that would cause recursive
371 // command execution:
372 if (!m_was_all_internal) {
373 m_should_stop = !ignoring_breakpoints;
374 LLDB_LOGF(log,
375 "StopInfoBreakpoint::PerformAction - in expression, "
376 "continuing: %s.",
377 m_should_stop ? "true" : "false");
378 Debugger::ReportWarning(
379 "hit breakpoint while running function, skipping commands "
380 "and conditions to prevent recursion",
381 process->GetTarget().GetDebugger().GetID());
382 return;
383 }
384 }
385
386 StoppointCallbackContext context(event_ptr, exe_ctx, false);
387
388 // For safety's sake let's also grab an extra reference to the
389 // breakpoint constituents of the locations we're going to examine,
390 // since the locations are going to have to get back to their
391 // breakpoints, and the locations don't keep their constituents alive.
392 // I'm just sticking the BreakpointSP's in a vector since I'm only
393 // using it to locally increment their retain counts.
394
395 std::vector<lldb::BreakpointSP> location_constituents;
396
397 for (size_t j = 0; j < num_constituents; j++) {
398 BreakpointLocationSP loc(site_locations.GetByIndex(j));
399 location_constituents.push_back(
400 loc->GetBreakpoint().shared_from_this());
401 }
402
403 for (size_t j = 0; j < num_constituents; j++) {
404 lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j);
405 StreamString loc_desc;
406 if (log) {
407 bp_loc_sp->GetDescription(&loc_desc, eDescriptionLevelBrief);
408 }
409 // If another action disabled this breakpoint or its location, then
410 // don't run the actions.
411 if (!bp_loc_sp->IsEnabled() ||
412 !bp_loc_sp->GetBreakpoint().IsEnabled())
413 continue;
414
415 // The breakpoint site may have many locations associated with it,
416 // not all of them valid for this thread. Skip the ones that
417 // aren't:
418 if (!bp_loc_sp->ValidForThisThread(*thread_sp)) {
419 if (log) {
420 LLDB_LOGF(log,
421 "Breakpoint %s hit on thread 0x%llx but it was not "
422 "for this thread, continuing.",
423 loc_desc.GetData(),
424 static_cast<unsigned long long>(thread_sp->GetID()));
425 }
426 continue;
427 }
428
429 // First run the precondition, but since the precondition is per
430 // breakpoint, only run it once per breakpoint.
431 std::pair<std::unordered_set<break_id_t>::iterator, bool> result =
432 precondition_breakpoints.insert(
433 bp_loc_sp->GetBreakpoint().GetID());
434 if (!result.second)
435 continue;
436
437 bool precondition_result =
438 bp_loc_sp->GetBreakpoint().EvaluatePrecondition(context);
439 if (!precondition_result) {
440 actually_said_continue = true;
441 continue;
442 }
443 // Next run the condition for the breakpoint. If that says we
444 // should stop, then we'll run the callback for the breakpoint. If
445 // the callback says we shouldn't stop that will win.
446
447 if (bp_loc_sp->GetConditionText() == nullptr)
448 actually_hit_any_locations = true;
449 else {
450 Status condition_error;
451 bool condition_says_stop =
452 bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
453
454 if (!condition_error.Success()) {
455 // If the condition fails to evaluate, we are going to stop
456 // at it, so the location was hit.
457 actually_hit_any_locations = true;
458 const char *err_str =
459 condition_error.AsCString("<unknown error>");
460 LLDB_LOGF(log, "Error evaluating condition: \"%s\"\n", err_str);
461
462 StreamString strm;
463 strm << "stopped due to an error evaluating condition of "
464 "breakpoint ";
465 bp_loc_sp->GetDescription(&strm, eDescriptionLevelBrief);
466 strm << ": \"" << bp_loc_sp->GetConditionText() << "\"\n";
467 strm << err_str;
468
469 Debugger::ReportError(
470 strm.GetString().str(),
471 exe_ctx.GetTargetRef().GetDebugger().GetID());
472 } else {
473 LLDB_LOGF(log,
474 "Condition evaluated for breakpoint %s on thread "
475 "0x%llx condition_says_stop: %i.",
476 loc_desc.GetData(),
477 static_cast<unsigned long long>(thread_sp->GetID()),
478 condition_says_stop);
479 if (condition_says_stop)
480 actually_hit_any_locations = true;
481 else {
482 // We don't want to increment the hit count of breakpoints if
483 // the condition fails. We've already bumped it by the time
484 // we get here, so undo the bump:
485 bp_loc_sp->UndoBumpHitCount();
486 actually_said_continue = true;
487 continue;
488 }
489 }
490 }
491
492 // We've done all the checks whose failure means "we consider lldb
493 // not to have hit the breakpoint". Now we're going to check for
494 // conditions that might continue after hitting. Start with the
495 // ignore count:
496 if (!bp_loc_sp->IgnoreCountShouldStop()) {
497 actually_said_continue = true;
498 continue;
499 }
500
501 // Check the auto-continue bit on the location, do this before the
502 // callback since it may change this, but that would be for the
503 // NEXT hit. Note, you might think you could check auto-continue
504 // before the condition, and not evaluate the condition if it says
505 // to continue. But failing the condition means the breakpoint was
506 // effectively NOT HIT. So these two states are different.
507 bool auto_continue_says_stop = true;
508 if (bp_loc_sp->IsAutoContinue())
509 {
510 LLDB_LOGF(log,
511 "Continuing breakpoint %s as AutoContinue was set.",
512 loc_desc.GetData());
513 // We want this stop reported, so you will know we auto-continued
514 // but only for external breakpoints:
515 if (!bp_loc_sp->GetBreakpoint().IsInternal())
516 thread_sp->SetShouldReportStop(eVoteYes);
517 auto_continue_says_stop = false;
518 }
519
520 bool callback_says_stop = true;
521
522 // FIXME: For now the callbacks have to run in async mode - the
523 // first time we restart we need
524 // to get out of there. So set it here.
525 // When we figure out how to nest breakpoint hits then this will
526 // change.
527
528 // Don't run async callbacks in PerformAction. They have already
529 // been taken into account with async_should_stop.
530 if (!bp_loc_sp->IsCallbackSynchronous()) {
531 Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
532 bool old_async = debugger.GetAsyncExecution();
533 debugger.SetAsyncExecution(true);
534
535 callback_says_stop = bp_loc_sp->InvokeCallback(&context);
536
537 debugger.SetAsyncExecution(old_async);
538
539 if (callback_says_stop && auto_continue_says_stop)
540 m_should_stop = true;
541 else
542 actually_said_continue = true;
543 }
544
545 if (m_should_stop && !bp_loc_sp->GetBreakpoint().IsInternal())
546 all_stopping_locs_internal = false;
547
548 // If we are going to stop for this breakpoint, then remove the
549 // breakpoint.
550 if (callback_says_stop && bp_loc_sp &&
551 bp_loc_sp->GetBreakpoint().IsOneShot()) {
552 thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID(
553 bp_loc_sp->GetBreakpoint().GetID());
554 }
555 // Also make sure that the callback hasn't continued the target. If
556 // it did, when we'll set m_should_start to false and get out of
557 // here.
558 if (HasTargetRunSinceMe()) {
559 m_should_stop = false;
560 actually_said_continue = true;
561 break;
562 }
563 }
564 // At this point if nobody actually told us to continue, we should
565 // give the async breakpoint callback a chance to weigh in:
566 if (!actually_said_continue && !m_should_stop) {
567 m_should_stop = async_should_stop;
568 }
569 }
570 // We've figured out what this stop wants to do, so mark it as valid so
571 // we don't compute it again.
572 m_should_stop_is_valid = true;
573 } else {
574 m_should_stop = true;
575 m_should_stop_is_valid = true;
576 actually_hit_any_locations = true;
577 Log *log_process(GetLog(LLDBLog::Process));
578
579 LLDB_LOGF(log_process,
580 "Process::%s could not find breakpoint site id: %" PRId64
581 "...",
582 __FUNCTION__, m_value);
583 }
584
585 if ((!m_should_stop || all_stopping_locs_internal) &&
586 thread_sp->CompletedPlanOverridesBreakpoint()) {
587
588 // Override should_stop decision when we have completed step plan
589 // additionally to the breakpoint
590 m_should_stop = true;
591
592 // We know we're stopping for a completed plan and we don't want to
593 // show the breakpoint stop, so compute the public stop info immediately
594 // here.
595 thread_sp->CalculatePublicStopInfo();
596 } else if (!actually_hit_any_locations) {
597 // In the end, we didn't actually have any locations that passed their
598 // "was I hit" checks. So say we aren't stopped.
599 GetThread()->ResetStopInfo();
600 LLDB_LOGF(log, "Process::%s all locations failed condition checks.",
601 __FUNCTION__);
602 }
603
604 LLDB_LOGF(log,
605 "Process::%s returning from action with m_should_stop: %d.",
606 __FUNCTION__, m_should_stop);
607 }
608 }
609
610 private:
611 bool m_should_stop;
612 bool m_should_stop_is_valid;
613 bool m_should_perform_action; // Since we are trying to preserve the "state"
614 // of the system even if we run functions
615 // etc. behind the users backs, we need to make sure we only REALLY perform
616 // the action once.
617 lldb::addr_t m_address; // We use this to capture the breakpoint site address
618 // when we create the StopInfo,
619 // in case somebody deletes it between the time the StopInfo is made and the
620 // description is asked for.
621 lldb::break_id_t m_break_id;
622 bool m_was_all_internal;
623 bool m_was_one_shot;
624 };
625
626 // StopInfoWatchpoint
627
628 class StopInfoWatchpoint : public StopInfo {
629 public:
630 // Make sure watchpoint is properly disabled and subsequently enabled while
631 // performing watchpoint actions.
632 class WatchpointSentry {
633 public:
WatchpointSentry(ProcessSP p_sp,WatchpointSP w_sp)634 WatchpointSentry(ProcessSP p_sp, WatchpointSP w_sp) : process_sp(p_sp),
635 watchpoint_sp(w_sp) {
636 if (process_sp && watchpoint_sp) {
637 const bool notify = false;
638 watchpoint_sp->TurnOnEphemeralMode();
639 process_sp->DisableWatchpoint(watchpoint_sp, notify);
640 process_sp->AddPreResumeAction(SentryPreResumeAction, this);
641 }
642 }
643
DoReenable()644 void DoReenable() {
645 if (process_sp && watchpoint_sp) {
646 bool was_disabled = watchpoint_sp->IsDisabledDuringEphemeralMode();
647 watchpoint_sp->TurnOffEphemeralMode();
648 const bool notify = false;
649 if (was_disabled) {
650 process_sp->DisableWatchpoint(watchpoint_sp, notify);
651 } else {
652 process_sp->EnableWatchpoint(watchpoint_sp, notify);
653 }
654 }
655 }
656
~WatchpointSentry()657 ~WatchpointSentry() {
658 DoReenable();
659 if (process_sp)
660 process_sp->ClearPreResumeAction(SentryPreResumeAction, this);
661 }
662
SentryPreResumeAction(void * sentry_void)663 static bool SentryPreResumeAction(void *sentry_void) {
664 WatchpointSentry *sentry = (WatchpointSentry *) sentry_void;
665 sentry->DoReenable();
666 return true;
667 }
668
669 private:
670 ProcessSP process_sp;
671 WatchpointSP watchpoint_sp;
672 };
673
StopInfoWatchpoint(Thread & thread,break_id_t watch_id,bool silently_skip_wp)674 StopInfoWatchpoint(Thread &thread, break_id_t watch_id, bool silently_skip_wp)
675 : StopInfo(thread, watch_id), m_silently_skip_wp(silently_skip_wp) {}
676
677 ~StopInfoWatchpoint() override = default;
678
GetStopReason() const679 StopReason GetStopReason() const override { return eStopReasonWatchpoint; }
680
GetDescription()681 const char *GetDescription() override {
682 if (m_description.empty()) {
683 StreamString strm;
684 strm.Printf("watchpoint %" PRIi64, m_value);
685 m_description = std::string(strm.GetString());
686 }
687 return m_description.c_str();
688 }
689
690 protected:
691 using StopInfoWatchpointSP = std::shared_ptr<StopInfoWatchpoint>;
692 // This plan is used to orchestrate stepping over the watchpoint for
693 // architectures (e.g. ARM) that report the watch before running the watched
694 // access. This is the sort of job you have to defer to the thread plans,
695 // if you try to do it directly in the stop info and there are other threads
696 // that needed to process this stop you will have yanked control away from
697 // them and they won't behave correctly.
698 class ThreadPlanStepOverWatchpoint : public ThreadPlanStepInstruction {
699 public:
ThreadPlanStepOverWatchpoint(Thread & thread,StopInfoWatchpointSP stop_info_sp,WatchpointSP watch_sp)700 ThreadPlanStepOverWatchpoint(Thread &thread,
701 StopInfoWatchpointSP stop_info_sp,
702 WatchpointSP watch_sp)
703 : ThreadPlanStepInstruction(thread, false, true, eVoteNoOpinion,
704 eVoteNoOpinion),
705 m_stop_info_sp(stop_info_sp), m_watch_sp(watch_sp) {
706 assert(watch_sp);
707 }
708
DoWillResume(lldb::StateType resume_state,bool current_plan)709 bool DoWillResume(lldb::StateType resume_state,
710 bool current_plan) override {
711 if (resume_state == eStateSuspended)
712 return true;
713
714 if (!m_did_disable_wp) {
715 GetThread().GetProcess()->DisableWatchpoint(m_watch_sp, false);
716 m_did_disable_wp = true;
717 }
718 return true;
719 }
720
DoPlanExplainsStop(Event * event_ptr)721 bool DoPlanExplainsStop(Event *event_ptr) override {
722 if (ThreadPlanStepInstruction::DoPlanExplainsStop(event_ptr))
723 return true;
724 StopInfoSP stop_info_sp = GetThread().GetPrivateStopInfo();
725 // lldb-server resets the stop info for threads that didn't get to run,
726 // so we might have not gotten to run, but still have a watchpoint stop
727 // reason, in which case this will indeed be for us.
728 if (stop_info_sp
729 && stop_info_sp->GetStopReason() == eStopReasonWatchpoint)
730 return true;
731 return false;
732 }
733
DidPop()734 void DidPop() override {
735 // Don't artifically keep the watchpoint alive.
736 m_watch_sp.reset();
737 }
738
ShouldStop(Event * event_ptr)739 bool ShouldStop(Event *event_ptr) override {
740 bool should_stop = ThreadPlanStepInstruction::ShouldStop(event_ptr);
741 bool plan_done = MischiefManaged();
742 if (plan_done) {
743 m_stop_info_sp->SetStepOverPlanComplete();
744 GetThread().SetStopInfo(m_stop_info_sp);
745 ResetWatchpoint();
746 }
747 return should_stop;
748 }
749
ShouldRunBeforePublicStop()750 bool ShouldRunBeforePublicStop() override {
751 return true;
752 }
753
754 protected:
ResetWatchpoint()755 void ResetWatchpoint() {
756 if (!m_did_disable_wp)
757 return;
758 m_did_disable_wp = true;
759 GetThread().GetProcess()->EnableWatchpoint(m_watch_sp, true);
760 }
761
762 private:
763 StopInfoWatchpointSP m_stop_info_sp;
764 WatchpointSP m_watch_sp;
765 bool m_did_disable_wp = false;
766 };
767
ShouldStopSynchronous(Event * event_ptr)768 bool ShouldStopSynchronous(Event *event_ptr) override {
769 // If we are running our step-over the watchpoint plan, stop if it's done
770 // and continue if it's not:
771 if (m_should_stop_is_valid)
772 return m_should_stop;
773
774 // If we are running our step over plan, then stop here and let the regular
775 // ShouldStop figure out what we should do: Otherwise, give our plan
776 // more time to get run:
777 if (m_using_step_over_plan)
778 return m_step_over_plan_complete;
779
780 Log *log = GetLog(LLDBLog::Process);
781 ThreadSP thread_sp(m_thread_wp.lock());
782 assert(thread_sp);
783
784 if (thread_sp->GetTemporaryResumeState() == eStateSuspended) {
785 // This is the second firing of a watchpoint so don't process it again.
786 LLDB_LOG(log, "We didn't run but stopped with a StopInfoWatchpoint, we "
787 "have already handled this one, don't do it again.");
788 m_should_stop = false;
789 m_should_stop_is_valid = true;
790 return m_should_stop;
791 }
792
793 WatchpointSP wp_sp(
794 thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
795 // If we can no longer find the watchpoint, we just have to stop:
796 if (!wp_sp) {
797
798 LLDB_LOGF(log,
799 "Process::%s could not find watchpoint location id: %" PRId64
800 "...",
801 __FUNCTION__, GetValue());
802
803 m_should_stop = true;
804 m_should_stop_is_valid = true;
805 return true;
806 }
807
808 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
809 StoppointCallbackContext context(event_ptr, exe_ctx, true);
810 m_should_stop = wp_sp->ShouldStop(&context);
811 if (!m_should_stop) {
812 // This won't happen at present because we only allow one watchpoint per
813 // watched range. So we won't stop at a watched address with a disabled
814 // watchpoint. If we start allowing overlapping watchpoints, then we
815 // will have to make watchpoints be real "WatchpointSite" and delegate to
816 // all the watchpoints sharing the site. In that case, the code below
817 // would be the right thing to do.
818 m_should_stop_is_valid = true;
819 return m_should_stop;
820 }
821 // If this is a system where we need to execute the watchpoint by hand
822 // after the hit, queue a thread plan to do that, and then say not to stop.
823 // Otherwise, let the async action figure out whether the watchpoint should
824 // stop
825
826 ProcessSP process_sp = exe_ctx.GetProcessSP();
827 bool wp_triggers_after = process_sp->GetWatchpointReportedAfter();
828
829 if (!wp_triggers_after) {
830 // We have to step over the watchpoint before we know what to do:
831 StopInfoWatchpointSP me_as_siwp_sp
832 = std::static_pointer_cast<StopInfoWatchpoint>(shared_from_this());
833 ThreadPlanSP step_over_wp_sp(new ThreadPlanStepOverWatchpoint(
834 *(thread_sp.get()), me_as_siwp_sp, wp_sp));
835 // When this plan is done we want to stop, so set this as a Controlling
836 // plan.
837 step_over_wp_sp->SetIsControllingPlan(true);
838 step_over_wp_sp->SetOkayToDiscard(false);
839
840 Status error;
841 error = thread_sp->QueueThreadPlan(step_over_wp_sp, false);
842 // If we couldn't push the thread plan, just stop here:
843 if (!error.Success()) {
844 LLDB_LOGF(log, "Could not push our step over watchpoint plan: %s",
845 error.AsCString());
846
847 m_should_stop = true;
848 m_should_stop_is_valid = true;
849 return true;
850 } else {
851 // Otherwise, don't set m_should_stop, we don't know that yet. Just
852 // say we should continue, and tell the thread we really should do so:
853 thread_sp->SetShouldRunBeforePublicStop(true);
854 m_using_step_over_plan = true;
855 return false;
856 }
857 } else {
858 // We didn't have to do anything special
859 m_should_stop_is_valid = true;
860 return m_should_stop;
861 }
862
863 return m_should_stop;
864 }
865
ShouldStop(Event * event_ptr)866 bool ShouldStop(Event *event_ptr) override {
867 // This just reports the work done by PerformAction or the synchronous
868 // stop. It should only ever get called after they have had a chance to
869 // run.
870 assert(m_should_stop_is_valid);
871 return m_should_stop;
872 }
873
PerformAction(Event * event_ptr)874 void PerformAction(Event *event_ptr) override {
875 Log *log = GetLog(LLDBLog::Watchpoints);
876 // We're going to calculate if we should stop or not in some way during the
877 // course of this code. Also by default we're going to stop, so set that
878 // here.
879 m_should_stop = true;
880
881
882 ThreadSP thread_sp(m_thread_wp.lock());
883 if (thread_sp) {
884
885 WatchpointSP wp_sp(
886 thread_sp->CalculateTarget()->GetWatchpointList().FindByID(
887 GetValue()));
888 if (wp_sp) {
889 // This sentry object makes sure the current watchpoint is disabled
890 // while performing watchpoint actions, and it is then enabled after we
891 // are finished.
892 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
893 ProcessSP process_sp = exe_ctx.GetProcessSP();
894
895 WatchpointSentry sentry(process_sp, wp_sp);
896
897 if (m_silently_skip_wp) {
898 m_should_stop = false;
899 wp_sp->UndoHitCount();
900 }
901
902 if (wp_sp->GetHitCount() <= wp_sp->GetIgnoreCount()) {
903 m_should_stop = false;
904 m_should_stop_is_valid = true;
905 }
906
907 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
908
909 if (m_should_stop && wp_sp->GetConditionText() != nullptr) {
910 // We need to make sure the user sees any parse errors in their
911 // condition, so we'll hook the constructor errors up to the
912 // debugger's Async I/O.
913 ExpressionResults result_code;
914 EvaluateExpressionOptions expr_options;
915 expr_options.SetUnwindOnError(true);
916 expr_options.SetIgnoreBreakpoints(true);
917 ValueObjectSP result_value_sp;
918 Status error;
919 result_code = UserExpression::Evaluate(
920 exe_ctx, expr_options, wp_sp->GetConditionText(),
921 llvm::StringRef(), result_value_sp, error);
922
923 if (result_code == eExpressionCompleted) {
924 if (result_value_sp) {
925 Scalar scalar_value;
926 if (result_value_sp->ResolveValue(scalar_value)) {
927 if (scalar_value.ULongLong(1) == 0) {
928 // The condition failed, which we consider "not having hit
929 // the watchpoint" so undo the hit count here.
930 wp_sp->UndoHitCount();
931 m_should_stop = false;
932 } else
933 m_should_stop = true;
934 LLDB_LOGF(log,
935 "Condition successfully evaluated, result is %s.\n",
936 m_should_stop ? "true" : "false");
937 } else {
938 m_should_stop = true;
939 LLDB_LOGF(
940 log,
941 "Failed to get an integer result from the expression.");
942 }
943 }
944 } else {
945 const char *err_str = error.AsCString("<unknown error>");
946 LLDB_LOGF(log, "Error evaluating condition: \"%s\"\n", err_str);
947
948 StreamString strm;
949 strm << "stopped due to an error evaluating condition of "
950 "watchpoint ";
951 wp_sp->GetDescription(&strm, eDescriptionLevelBrief);
952 strm << ": \"" << wp_sp->GetConditionText() << "\"\n";
953 strm << err_str;
954
955 Debugger::ReportError(strm.GetString().str(),
956 exe_ctx.GetTargetRef().GetDebugger().GetID());
957 }
958 }
959
960 // If the condition says to stop, we run the callback to further decide
961 // whether to stop.
962 if (m_should_stop) {
963 // FIXME: For now the callbacks have to run in async mode - the
964 // first time we restart we need
965 // to get out of there. So set it here.
966 // When we figure out how to nest watchpoint hits then this will
967 // change.
968
969 bool old_async = debugger.GetAsyncExecution();
970 debugger.SetAsyncExecution(true);
971
972 StoppointCallbackContext context(event_ptr, exe_ctx, false);
973 bool stop_requested = wp_sp->InvokeCallback(&context);
974
975 debugger.SetAsyncExecution(old_async);
976
977 // Also make sure that the callback hasn't continued the target. If
978 // it did, when we'll set m_should_stop to false and get out of here.
979 if (HasTargetRunSinceMe())
980 m_should_stop = false;
981
982 if (m_should_stop && !stop_requested) {
983 // We have been vetoed by the callback mechanism.
984 m_should_stop = false;
985 }
986 }
987
988 // Don't stop if the watched region value is unmodified, and
989 // this is a Modify-type watchpoint.
990 if (m_should_stop && !wp_sp->WatchedValueReportable(exe_ctx))
991 m_should_stop = false;
992
993 // Finally, if we are going to stop, print out the new & old values:
994 if (m_should_stop) {
995 wp_sp->CaptureWatchedValue(exe_ctx);
996
997 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
998 StreamSP output_sp = debugger.GetAsyncOutputStream();
999 if (wp_sp->DumpSnapshots(output_sp.get())) {
1000 output_sp->EOL();
1001 output_sp->Flush();
1002 }
1003 }
1004
1005 } else {
1006 Log *log_process(GetLog(LLDBLog::Process));
1007
1008 LLDB_LOGF(log_process,
1009 "Process::%s could not find watchpoint id: %" PRId64 "...",
1010 __FUNCTION__, m_value);
1011 }
1012 LLDB_LOGF(log,
1013 "Process::%s returning from action with m_should_stop: %d.",
1014 __FUNCTION__, m_should_stop);
1015
1016 m_should_stop_is_valid = true;
1017 }
1018 }
1019
1020 private:
SetStepOverPlanComplete()1021 void SetStepOverPlanComplete() {
1022 assert(m_using_step_over_plan);
1023 m_step_over_plan_complete = true;
1024 }
1025
1026 bool m_should_stop = false;
1027 bool m_should_stop_is_valid = false;
1028 // A false watchpoint hit has happened -
1029 // the thread stopped with a watchpoint
1030 // hit notification, but the watched region
1031 // was not actually accessed (as determined
1032 // by the gdb stub we're talking to).
1033 // Continue past this watchpoint without
1034 // notifying the user; on some targets this
1035 // may mean disable wp, instruction step,
1036 // re-enable wp, continue.
1037 // On others, just continue.
1038 bool m_silently_skip_wp = false;
1039 bool m_step_over_plan_complete = false;
1040 bool m_using_step_over_plan = false;
1041 };
1042
1043 // StopInfoUnixSignal
1044
1045 class StopInfoUnixSignal : public StopInfo {
1046 public:
StopInfoUnixSignal(Thread & thread,int signo,const char * description,std::optional<int> code)1047 StopInfoUnixSignal(Thread &thread, int signo, const char *description,
1048 std::optional<int> code)
1049 : StopInfo(thread, signo), m_code(code) {
1050 SetDescription(description);
1051 }
1052
1053 ~StopInfoUnixSignal() override = default;
1054
GetStopReason() const1055 StopReason GetStopReason() const override { return eStopReasonSignal; }
1056
ShouldStopSynchronous(Event * event_ptr)1057 bool ShouldStopSynchronous(Event *event_ptr) override {
1058 ThreadSP thread_sp(m_thread_wp.lock());
1059 if (thread_sp)
1060 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
1061 return false;
1062 }
1063
ShouldStop(Event * event_ptr)1064 bool ShouldStop(Event *event_ptr) override {
1065 ThreadSP thread_sp(m_thread_wp.lock());
1066 if (thread_sp)
1067 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
1068 return false;
1069 }
1070
1071 // If should stop returns false, check if we should notify of this event
DoShouldNotify(Event * event_ptr)1072 bool DoShouldNotify(Event *event_ptr) override {
1073 ThreadSP thread_sp(m_thread_wp.lock());
1074 if (thread_sp) {
1075 bool should_notify =
1076 thread_sp->GetProcess()->GetUnixSignals()->GetShouldNotify(m_value);
1077 if (should_notify) {
1078 StreamString strm;
1079 strm.Format(
1080 "thread {0:d} received signal: {1}", thread_sp->GetIndexID(),
1081 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsStringRef(
1082 m_value));
1083 Process::ProcessEventData::AddRestartedReason(event_ptr,
1084 strm.GetData());
1085 }
1086 return should_notify;
1087 }
1088 return true;
1089 }
1090
WillResume(lldb::StateType resume_state)1091 void WillResume(lldb::StateType resume_state) override {
1092 ThreadSP thread_sp(m_thread_wp.lock());
1093 if (thread_sp) {
1094 if (!thread_sp->GetProcess()->GetUnixSignals()->GetShouldSuppress(
1095 m_value))
1096 thread_sp->SetResumeSignal(m_value);
1097 }
1098 }
1099
GetDescription()1100 const char *GetDescription() override {
1101 if (m_description.empty()) {
1102 ThreadSP thread_sp(m_thread_wp.lock());
1103 if (thread_sp) {
1104 UnixSignalsSP unix_signals = thread_sp->GetProcess()->GetUnixSignals();
1105 StreamString strm;
1106 strm << "signal ";
1107
1108 std::string signal_name =
1109 unix_signals->GetSignalDescription(m_value, m_code);
1110 if (signal_name.size())
1111 strm << signal_name;
1112 else
1113 strm.Printf("%" PRIi64, m_value);
1114
1115 m_description = std::string(strm.GetString());
1116 }
1117 }
1118 return m_description.c_str();
1119 }
1120
1121 private:
1122 // In siginfo_t terms, if m_value is si_signo, m_code is si_code.
1123 std::optional<int> m_code;
1124 };
1125
1126 // StopInfoTrace
1127
1128 class StopInfoTrace : public StopInfo {
1129 public:
StopInfoTrace(Thread & thread)1130 StopInfoTrace(Thread &thread) : StopInfo(thread, LLDB_INVALID_UID) {}
1131
1132 ~StopInfoTrace() override = default;
1133
GetStopReason() const1134 StopReason GetStopReason() const override { return eStopReasonTrace; }
1135
GetDescription()1136 const char *GetDescription() override {
1137 if (m_description.empty())
1138 return "trace";
1139 else
1140 return m_description.c_str();
1141 }
1142 };
1143
1144 // StopInfoException
1145
1146 class StopInfoException : public StopInfo {
1147 public:
StopInfoException(Thread & thread,const char * description)1148 StopInfoException(Thread &thread, const char *description)
1149 : StopInfo(thread, LLDB_INVALID_UID) {
1150 if (description)
1151 SetDescription(description);
1152 }
1153
1154 ~StopInfoException() override = default;
1155
GetStopReason() const1156 StopReason GetStopReason() const override { return eStopReasonException; }
1157
GetDescription()1158 const char *GetDescription() override {
1159 if (m_description.empty())
1160 return "exception";
1161 else
1162 return m_description.c_str();
1163 }
1164 };
1165
1166 // StopInfoProcessorTrace
1167
1168 class StopInfoProcessorTrace : public StopInfo {
1169 public:
StopInfoProcessorTrace(Thread & thread,const char * description)1170 StopInfoProcessorTrace(Thread &thread, const char *description)
1171 : StopInfo(thread, LLDB_INVALID_UID) {
1172 if (description)
1173 SetDescription(description);
1174 }
1175
1176 ~StopInfoProcessorTrace() override = default;
1177
GetStopReason() const1178 StopReason GetStopReason() const override {
1179 return eStopReasonProcessorTrace;
1180 }
1181
GetDescription()1182 const char *GetDescription() override {
1183 if (m_description.empty())
1184 return "processor trace event";
1185 else
1186 return m_description.c_str();
1187 }
1188 };
1189
1190 // StopInfoThreadPlan
1191
1192 class StopInfoThreadPlan : public StopInfo {
1193 public:
StopInfoThreadPlan(ThreadPlanSP & plan_sp,ValueObjectSP & return_valobj_sp,ExpressionVariableSP & expression_variable_sp)1194 StopInfoThreadPlan(ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp,
1195 ExpressionVariableSP &expression_variable_sp)
1196 : StopInfo(plan_sp->GetThread(), LLDB_INVALID_UID), m_plan_sp(plan_sp),
1197 m_return_valobj_sp(return_valobj_sp),
1198 m_expression_variable_sp(expression_variable_sp) {}
1199
1200 ~StopInfoThreadPlan() override = default;
1201
GetStopReason() const1202 StopReason GetStopReason() const override { return eStopReasonPlanComplete; }
1203
GetDescription()1204 const char *GetDescription() override {
1205 if (m_description.empty()) {
1206 StreamString strm;
1207 m_plan_sp->GetDescription(&strm, eDescriptionLevelBrief);
1208 m_description = std::string(strm.GetString());
1209 }
1210 return m_description.c_str();
1211 }
1212
GetReturnValueObject()1213 ValueObjectSP GetReturnValueObject() { return m_return_valobj_sp; }
1214
GetExpressionVariable()1215 ExpressionVariableSP GetExpressionVariable() {
1216 return m_expression_variable_sp;
1217 }
1218
1219 protected:
ShouldStop(Event * event_ptr)1220 bool ShouldStop(Event *event_ptr) override {
1221 if (m_plan_sp)
1222 return m_plan_sp->ShouldStop(event_ptr);
1223 else
1224 return StopInfo::ShouldStop(event_ptr);
1225 }
1226
1227 private:
1228 ThreadPlanSP m_plan_sp;
1229 ValueObjectSP m_return_valobj_sp;
1230 ExpressionVariableSP m_expression_variable_sp;
1231 };
1232
1233 // StopInfoExec
1234
1235 class StopInfoExec : public StopInfo {
1236 public:
StopInfoExec(Thread & thread)1237 StopInfoExec(Thread &thread) : StopInfo(thread, LLDB_INVALID_UID) {}
1238
1239 ~StopInfoExec() override = default;
1240
ShouldStop(Event * event_ptr)1241 bool ShouldStop(Event *event_ptr) override {
1242 ThreadSP thread_sp(m_thread_wp.lock());
1243 if (thread_sp)
1244 return thread_sp->GetProcess()->GetStopOnExec();
1245 return false;
1246 }
1247
GetStopReason() const1248 StopReason GetStopReason() const override { return eStopReasonExec; }
1249
GetDescription()1250 const char *GetDescription() override { return "exec"; }
1251
1252 protected:
PerformAction(Event * event_ptr)1253 void PerformAction(Event *event_ptr) override {
1254 // Only perform the action once
1255 if (m_performed_action)
1256 return;
1257 m_performed_action = true;
1258 ThreadSP thread_sp(m_thread_wp.lock());
1259 if (thread_sp)
1260 thread_sp->GetProcess()->DidExec();
1261 }
1262
1263 bool m_performed_action = false;
1264 };
1265
1266 // StopInfoFork
1267
1268 class StopInfoFork : public StopInfo {
1269 public:
StopInfoFork(Thread & thread,lldb::pid_t child_pid,lldb::tid_t child_tid)1270 StopInfoFork(Thread &thread, lldb::pid_t child_pid, lldb::tid_t child_tid)
1271 : StopInfo(thread, child_pid), m_child_pid(child_pid),
1272 m_child_tid(child_tid) {}
1273
1274 ~StopInfoFork() override = default;
1275
ShouldStop(Event * event_ptr)1276 bool ShouldStop(Event *event_ptr) override { return false; }
1277
GetStopReason() const1278 StopReason GetStopReason() const override { return eStopReasonFork; }
1279
GetDescription()1280 const char *GetDescription() override { return "fork"; }
1281
1282 protected:
PerformAction(Event * event_ptr)1283 void PerformAction(Event *event_ptr) override {
1284 // Only perform the action once
1285 if (m_performed_action)
1286 return;
1287 m_performed_action = true;
1288 ThreadSP thread_sp(m_thread_wp.lock());
1289 if (thread_sp)
1290 thread_sp->GetProcess()->DidFork(m_child_pid, m_child_tid);
1291 }
1292
1293 bool m_performed_action = false;
1294
1295 private:
1296 lldb::pid_t m_child_pid;
1297 lldb::tid_t m_child_tid;
1298 };
1299
1300 // StopInfoVFork
1301
1302 class StopInfoVFork : public StopInfo {
1303 public:
StopInfoVFork(Thread & thread,lldb::pid_t child_pid,lldb::tid_t child_tid)1304 StopInfoVFork(Thread &thread, lldb::pid_t child_pid, lldb::tid_t child_tid)
1305 : StopInfo(thread, child_pid), m_child_pid(child_pid),
1306 m_child_tid(child_tid) {}
1307
1308 ~StopInfoVFork() override = default;
1309
ShouldStop(Event * event_ptr)1310 bool ShouldStop(Event *event_ptr) override { return false; }
1311
GetStopReason() const1312 StopReason GetStopReason() const override { return eStopReasonVFork; }
1313
GetDescription()1314 const char *GetDescription() override { return "vfork"; }
1315
1316 protected:
PerformAction(Event * event_ptr)1317 void PerformAction(Event *event_ptr) override {
1318 // Only perform the action once
1319 if (m_performed_action)
1320 return;
1321 m_performed_action = true;
1322 ThreadSP thread_sp(m_thread_wp.lock());
1323 if (thread_sp)
1324 thread_sp->GetProcess()->DidVFork(m_child_pid, m_child_tid);
1325 }
1326
1327 bool m_performed_action = false;
1328
1329 private:
1330 lldb::pid_t m_child_pid;
1331 lldb::tid_t m_child_tid;
1332 };
1333
1334 // StopInfoVForkDone
1335
1336 class StopInfoVForkDone : public StopInfo {
1337 public:
StopInfoVForkDone(Thread & thread)1338 StopInfoVForkDone(Thread &thread) : StopInfo(thread, 0) {}
1339
1340 ~StopInfoVForkDone() override = default;
1341
ShouldStop(Event * event_ptr)1342 bool ShouldStop(Event *event_ptr) override { return false; }
1343
GetStopReason() const1344 StopReason GetStopReason() const override { return eStopReasonVForkDone; }
1345
GetDescription()1346 const char *GetDescription() override { return "vforkdone"; }
1347
1348 protected:
PerformAction(Event * event_ptr)1349 void PerformAction(Event *event_ptr) override {
1350 // Only perform the action once
1351 if (m_performed_action)
1352 return;
1353 m_performed_action = true;
1354 ThreadSP thread_sp(m_thread_wp.lock());
1355 if (thread_sp)
1356 thread_sp->GetProcess()->DidVForkDone();
1357 }
1358
1359 bool m_performed_action = false;
1360 };
1361
1362 } // namespace lldb_private
1363
CreateStopReasonWithBreakpointSiteID(Thread & thread,break_id_t break_id)1364 StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
1365 break_id_t break_id) {
1366 return StopInfoSP(new StopInfoBreakpoint(thread, break_id));
1367 }
1368
CreateStopReasonWithBreakpointSiteID(Thread & thread,break_id_t break_id,bool should_stop)1369 StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
1370 break_id_t break_id,
1371 bool should_stop) {
1372 return StopInfoSP(new StopInfoBreakpoint(thread, break_id, should_stop));
1373 }
1374
1375 // LWP_TODO: We'll need a CreateStopReasonWithWatchpointResourceID akin
1376 // to CreateStopReasonWithBreakpointSiteID
CreateStopReasonWithWatchpointID(Thread & thread,break_id_t watch_id,bool silently_continue)1377 StopInfoSP StopInfo::CreateStopReasonWithWatchpointID(Thread &thread,
1378 break_id_t watch_id,
1379 bool silently_continue) {
1380 return StopInfoSP(
1381 new StopInfoWatchpoint(thread, watch_id, silently_continue));
1382 }
1383
CreateStopReasonWithSignal(Thread & thread,int signo,const char * description,std::optional<int> code)1384 StopInfoSP StopInfo::CreateStopReasonWithSignal(Thread &thread, int signo,
1385 const char *description,
1386 std::optional<int> code) {
1387 thread.GetProcess()->GetUnixSignals()->IncrementSignalHitCount(signo);
1388 return StopInfoSP(new StopInfoUnixSignal(thread, signo, description, code));
1389 }
1390
CreateStopReasonToTrace(Thread & thread)1391 StopInfoSP StopInfo::CreateStopReasonToTrace(Thread &thread) {
1392 return StopInfoSP(new StopInfoTrace(thread));
1393 }
1394
CreateStopReasonWithPlan(ThreadPlanSP & plan_sp,ValueObjectSP return_valobj_sp,ExpressionVariableSP expression_variable_sp)1395 StopInfoSP StopInfo::CreateStopReasonWithPlan(
1396 ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp,
1397 ExpressionVariableSP expression_variable_sp) {
1398 return StopInfoSP(new StopInfoThreadPlan(plan_sp, return_valobj_sp,
1399 expression_variable_sp));
1400 }
1401
CreateStopReasonWithException(Thread & thread,const char * description)1402 StopInfoSP StopInfo::CreateStopReasonWithException(Thread &thread,
1403 const char *description) {
1404 return StopInfoSP(new StopInfoException(thread, description));
1405 }
1406
CreateStopReasonProcessorTrace(Thread & thread,const char * description)1407 StopInfoSP StopInfo::CreateStopReasonProcessorTrace(Thread &thread,
1408 const char *description) {
1409 return StopInfoSP(new StopInfoProcessorTrace(thread, description));
1410 }
1411
CreateStopReasonWithExec(Thread & thread)1412 StopInfoSP StopInfo::CreateStopReasonWithExec(Thread &thread) {
1413 return StopInfoSP(new StopInfoExec(thread));
1414 }
1415
CreateStopReasonFork(Thread & thread,lldb::pid_t child_pid,lldb::tid_t child_tid)1416 StopInfoSP StopInfo::CreateStopReasonFork(Thread &thread,
1417 lldb::pid_t child_pid,
1418 lldb::tid_t child_tid) {
1419 return StopInfoSP(new StopInfoFork(thread, child_pid, child_tid));
1420 }
1421
1422
CreateStopReasonVFork(Thread & thread,lldb::pid_t child_pid,lldb::tid_t child_tid)1423 StopInfoSP StopInfo::CreateStopReasonVFork(Thread &thread,
1424 lldb::pid_t child_pid,
1425 lldb::tid_t child_tid) {
1426 return StopInfoSP(new StopInfoVFork(thread, child_pid, child_tid));
1427 }
1428
CreateStopReasonVForkDone(Thread & thread)1429 StopInfoSP StopInfo::CreateStopReasonVForkDone(Thread &thread) {
1430 return StopInfoSP(new StopInfoVForkDone(thread));
1431 }
1432
GetReturnValueObject(StopInfoSP & stop_info_sp)1433 ValueObjectSP StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp) {
1434 if (stop_info_sp &&
1435 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1436 StopInfoThreadPlan *plan_stop_info =
1437 static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1438 return plan_stop_info->GetReturnValueObject();
1439 } else
1440 return ValueObjectSP();
1441 }
1442
GetExpressionVariable(StopInfoSP & stop_info_sp)1443 ExpressionVariableSP StopInfo::GetExpressionVariable(StopInfoSP &stop_info_sp) {
1444 if (stop_info_sp &&
1445 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1446 StopInfoThreadPlan *plan_stop_info =
1447 static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1448 return plan_stop_info->GetExpressionVariable();
1449 } else
1450 return ExpressionVariableSP();
1451 }
1452
1453 lldb::ValueObjectSP
GetCrashingDereference(StopInfoSP & stop_info_sp,lldb::addr_t * crashing_address)1454 StopInfo::GetCrashingDereference(StopInfoSP &stop_info_sp,
1455 lldb::addr_t *crashing_address) {
1456 if (!stop_info_sp) {
1457 return ValueObjectSP();
1458 }
1459
1460 const char *description = stop_info_sp->GetDescription();
1461 if (!description) {
1462 return ValueObjectSP();
1463 }
1464
1465 ThreadSP thread_sp = stop_info_sp->GetThread();
1466 if (!thread_sp) {
1467 return ValueObjectSP();
1468 }
1469
1470 StackFrameSP frame_sp =
1471 thread_sp->GetSelectedFrame(DoNoSelectMostRelevantFrame);
1472
1473 if (!frame_sp) {
1474 return ValueObjectSP();
1475 }
1476
1477 const char address_string[] = "address=";
1478
1479 const char *address_loc = strstr(description, address_string);
1480 if (!address_loc) {
1481 return ValueObjectSP();
1482 }
1483
1484 address_loc += (sizeof(address_string) - 1);
1485
1486 uint64_t address = strtoull(address_loc, nullptr, 0);
1487 if (crashing_address) {
1488 *crashing_address = address;
1489 }
1490
1491 return frame_sp->GuessValueForAddress(address);
1492 }
1493