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