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