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