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 if (log) 333 log->Printf("StopInfoBreakpoint::PerformAction - Hit a " 334 "breakpoint while running an expression," 335 " not running commands to avoid recursion."); 336 bool ignoring_breakpoints = 337 process->GetIgnoreBreakpointsInExpressions(); 338 if (ignoring_breakpoints) { 339 m_should_stop = false; 340 // Internal breakpoints will always stop. 341 for (size_t j = 0; j < num_owners; j++) { 342 lldb::BreakpointLocationSP bp_loc_sp = 343 bp_site_sp->GetOwnerAtIndex(j); 344 if (bp_loc_sp->GetBreakpoint().IsInternal()) { 345 m_should_stop = true; 346 break; 347 } 348 } 349 } else { 350 m_should_stop = true; 351 } 352 if (log) 353 log->Printf("StopInfoBreakpoint::PerformAction - in expression, " 354 "continuing: %s.", 355 m_should_stop ? "true" : "false"); 356 process->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf( 357 "Warning: hit breakpoint while " 358 "running function, skipping commands and conditions to prevent " 359 "recursion."); 360 return; 361 } 362 363 StoppointCallbackContext context(event_ptr, exe_ctx, false); 364 365 // For safety's sake let's also grab an extra reference to the 366 // breakpoint owners of the locations we're going to examine, since 367 // the locations are going to have to get back to their breakpoints, 368 // and the locations don't keep their owners alive. I'm just 369 // sticking the BreakpointSP's in a vector since I'm only using it to 370 // locally increment their retain counts. 371 372 std::vector<lldb::BreakpointSP> location_owners; 373 374 for (size_t j = 0; j < num_owners; j++) { 375 BreakpointLocationSP loc(site_locations.GetByIndex(j)); 376 location_owners.push_back(loc->GetBreakpoint().shared_from_this()); 377 } 378 379 for (size_t j = 0; j < num_owners; j++) { 380 lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j); 381 StreamString loc_desc; 382 if (log) { 383 bp_loc_sp->GetDescription(&loc_desc, eDescriptionLevelBrief); 384 } 385 // If another action disabled this breakpoint or its location, then 386 // don't run the actions. 387 if (!bp_loc_sp->IsEnabled() || 388 !bp_loc_sp->GetBreakpoint().IsEnabled()) 389 continue; 390 391 // The breakpoint site may have many locations associated with it, 392 // not all of them valid for this thread. Skip the ones that 393 // aren't: 394 if (!bp_loc_sp->ValidForThisThread(thread_sp.get())) { 395 if (log) { 396 log->Printf("Breakpoint %s hit on thread 0x%llx but it was not " 397 "for this thread, continuing.", 398 loc_desc.GetData(), static_cast<unsigned long long>( 399 thread_sp->GetID())); 400 } 401 continue; 402 } 403 404 internal_breakpoint = bp_loc_sp->GetBreakpoint().IsInternal(); 405 406 // First run the precondition, but since the precondition is per 407 // breakpoint, only run it once per breakpoint. 408 std::pair<std::unordered_set<break_id_t>::iterator, bool> result = 409 precondition_breakpoints.insert( 410 bp_loc_sp->GetBreakpoint().GetID()); 411 if (!result.second) 412 continue; 413 414 bool precondition_result = 415 bp_loc_sp->GetBreakpoint().EvaluatePrecondition(context); 416 if (!precondition_result) 417 continue; 418 419 // Next run the condition for the breakpoint. If that says we 420 // should stop, then we'll run the callback for the breakpoint. If 421 // the callback says we shouldn't stop that will win. 422 423 if (bp_loc_sp->GetConditionText() != nullptr) { 424 Status condition_error; 425 bool condition_says_stop = 426 bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error); 427 428 if (!condition_error.Success()) { 429 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger(); 430 StreamSP error_sp = debugger.GetAsyncErrorStream(); 431 error_sp->Printf("Stopped due to an error evaluating condition " 432 "of breakpoint "); 433 bp_loc_sp->GetDescription(error_sp.get(), 434 eDescriptionLevelBrief); 435 error_sp->Printf(": \"%s\"", bp_loc_sp->GetConditionText()); 436 error_sp->EOL(); 437 const char *err_str = 438 condition_error.AsCString("<Unknown Error>"); 439 if (log) 440 log->Printf("Error evaluating condition: \"%s\"\n", err_str); 441 442 error_sp->PutCString(err_str); 443 error_sp->EOL(); 444 error_sp->Flush(); 445 } else { 446 if (log) { 447 log->Printf("Condition evaluated for breakpoint %s on thread " 448 "0x%llx conditon_says_stop: %i.", 449 loc_desc.GetData(), 450 static_cast<unsigned long long>( 451 thread_sp->GetID()), 452 condition_says_stop); 453 } 454 if (!condition_says_stop) { 455 // We don't want to increment the hit count of breakpoints if 456 // the condition fails. We've already bumped it by the time 457 // we get here, so undo the bump: 458 bp_loc_sp->UndoBumpHitCount(); 459 continue; 460 } 461 } 462 } 463 464 // Check the auto-continue bit on the location, do this before the 465 // callback since it may change this, but that would be for the 466 // NEXT hit. Note, you might think you could check auto-continue 467 // before the condition, and not evaluate the condition if it says 468 // to continue. But failing the condition means the breakpoint was 469 // effectively NOT HIT. So these two states are different. 470 bool auto_continue_says_stop = true; 471 if (bp_loc_sp->IsAutoContinue()) 472 { 473 if (log) 474 log->Printf("Continuing breakpoint %s as AutoContinue was set.", 475 loc_desc.GetData()); 476 // We want this stop reported, so you will know we auto-continued 477 // but only for external breakpoints: 478 if (!internal_breakpoint) 479 thread_sp->SetShouldReportStop(eVoteYes); 480 auto_continue_says_stop = false; 481 } 482 483 bool callback_says_stop = true; 484 485 // FIXME: For now the callbacks have to run in async mode - the 486 // first time we restart we need 487 // to get out of there. So set it here. 488 // When we figure out how to nest breakpoint hits then this will 489 // change. 490 491 Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger(); 492 bool old_async = debugger.GetAsyncExecution(); 493 debugger.SetAsyncExecution(true); 494 495 callback_says_stop = bp_loc_sp->InvokeCallback(&context); 496 497 debugger.SetAsyncExecution(old_async); 498 499 if (callback_says_stop && auto_continue_says_stop) 500 m_should_stop = true; 501 502 // If we are going to stop for this breakpoint, then remove the 503 // breakpoint. 504 if (callback_says_stop && bp_loc_sp && 505 bp_loc_sp->GetBreakpoint().IsOneShot()) { 506 thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID( 507 bp_loc_sp->GetBreakpoint().GetID()); 508 } 509 // Also make sure that the callback hasn't continued the target. If 510 // it did, when we'll set m_should_start to false and get out of 511 // here. 512 if (HasTargetRunSinceMe()) { 513 m_should_stop = false; 514 break; 515 } 516 } 517 } 518 // We've figured out what this stop wants to do, so mark it as valid so 519 // we don't compute it again. 520 m_should_stop_is_valid = true; 521 } else { 522 m_should_stop = true; 523 m_should_stop_is_valid = true; 524 Log *log_process( 525 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 526 527 if (log_process) 528 log_process->Printf( 529 "Process::%s could not find breakpoint site id: %" PRId64 "...", 530 __FUNCTION__, m_value); 531 } 532 533 if ((m_should_stop == false || internal_breakpoint) 534 && thread_sp->CompletedPlanOverridesBreakpoint()) { 535 536 // Override should_stop decision when we have completed step plan 537 // additionally to the breakpoint 538 m_should_stop = true; 539 540 // Here we clean the preset stop info so the next GetStopInfo call will 541 // find the appropriate stop info, which should be the stop info 542 // related to the completed plan 543 thread_sp->ResetStopInfo(); 544 } 545 546 if (log) 547 log->Printf("Process::%s returning from action with m_should_stop: %d.", 548 __FUNCTION__, m_should_stop); 549 } 550 } 551 552 private: 553 bool m_should_stop; 554 bool m_should_stop_is_valid; 555 bool m_should_perform_action; // Since we are trying to preserve the "state" 556 // of the system even if we run functions 557 // etc. behind the users backs, we need to make sure we only REALLY perform 558 // the action once. 559 lldb::addr_t m_address; // We use this to capture the breakpoint site address 560 // when we create the StopInfo, 561 // in case somebody deletes it between the time the StopInfo is made and the 562 // description is asked for. 563 lldb::break_id_t m_break_id; 564 bool m_was_one_shot; 565 }; 566 567 //---------------------------------------------------------------------- 568 // StopInfoWatchpoint 569 //---------------------------------------------------------------------- 570 571 class StopInfoWatchpoint : public StopInfo { 572 public: 573 // Make sure watchpoint is properly disabled and subsequently enabled while 574 // performing watchpoint actions. 575 class WatchpointSentry { 576 public: 577 WatchpointSentry(ProcessSP p_sp, WatchpointSP w_sp) : process_sp(p_sp), 578 watchpoint_sp(w_sp) { 579 if (process_sp && watchpoint_sp) { 580 const bool notify = false; 581 watchpoint_sp->TurnOnEphemeralMode(); 582 process_sp->DisableWatchpoint(watchpoint_sp.get(), notify); 583 process_sp->AddPreResumeAction(SentryPreResumeAction, this); 584 } 585 } 586 587 void DoReenable() { 588 if (process_sp && watchpoint_sp) { 589 bool was_disabled = watchpoint_sp->IsDisabledDuringEphemeralMode(); 590 watchpoint_sp->TurnOffEphemeralMode(); 591 const bool notify = false; 592 if (was_disabled) { 593 process_sp->DisableWatchpoint(watchpoint_sp.get(), notify); 594 } else { 595 process_sp->EnableWatchpoint(watchpoint_sp.get(), notify); 596 } 597 } 598 } 599 600 ~WatchpointSentry() { 601 DoReenable(); 602 if (process_sp) 603 process_sp->ClearPreResumeAction(SentryPreResumeAction, this); 604 } 605 606 static bool SentryPreResumeAction(void *sentry_void) { 607 WatchpointSentry *sentry = (WatchpointSentry *) sentry_void; 608 sentry->DoReenable(); 609 return true; 610 } 611 612 private: 613 ProcessSP process_sp; 614 WatchpointSP watchpoint_sp; 615 }; 616 617 StopInfoWatchpoint(Thread &thread, break_id_t watch_id, 618 lldb::addr_t watch_hit_addr) 619 : StopInfo(thread, watch_id), m_should_stop(false), 620 m_should_stop_is_valid(false), m_watch_hit_addr(watch_hit_addr) {} 621 622 ~StopInfoWatchpoint() override = default; 623 624 StopReason GetStopReason() const override { return eStopReasonWatchpoint; } 625 626 const char *GetDescription() override { 627 if (m_description.empty()) { 628 StreamString strm; 629 strm.Printf("watchpoint %" PRIi64, m_value); 630 m_description = strm.GetString(); 631 } 632 return m_description.c_str(); 633 } 634 635 protected: 636 bool ShouldStopSynchronous(Event *event_ptr) override { 637 // ShouldStop() method is idempotent and should not affect hit count. See 638 // Process::RunPrivateStateThread()->Process()->HandlePrivateEvent() 639 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()-> 640 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()-> 641 // StopInfoWatchpoint::ShouldStop() and 642 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()-> 643 // StopInfoWatchpoint::PerformAction(). 644 if (m_should_stop_is_valid) 645 return m_should_stop; 646 647 ThreadSP thread_sp(m_thread_wp.lock()); 648 if (thread_sp) { 649 WatchpointSP wp_sp( 650 thread_sp->CalculateTarget()->GetWatchpointList().FindByID( 651 GetValue())); 652 if (wp_sp) { 653 // Check if we should stop at a watchpoint. 654 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0)); 655 StoppointCallbackContext context(event_ptr, exe_ctx, true); 656 m_should_stop = wp_sp->ShouldStop(&context); 657 } else { 658 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 659 660 if (log) 661 log->Printf( 662 "Process::%s could not find watchpoint location id: %" PRId64 663 "...", 664 __FUNCTION__, GetValue()); 665 666 m_should_stop = true; 667 } 668 } 669 m_should_stop_is_valid = true; 670 return m_should_stop; 671 } 672 673 bool ShouldStop(Event *event_ptr) override { 674 // This just reports the work done by PerformAction or the synchronous 675 // stop. It should only ever get called after they have had a chance to 676 // run. 677 assert(m_should_stop_is_valid); 678 return m_should_stop; 679 } 680 681 void PerformAction(Event *event_ptr) override { 682 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS); 683 // We're going to calculate if we should stop or not in some way during the 684 // course of this code. Also by default we're going to stop, so set that 685 // here. 686 m_should_stop = true; 687 688 689 ThreadSP thread_sp(m_thread_wp.lock()); 690 if (thread_sp) { 691 692 WatchpointSP wp_sp( 693 thread_sp->CalculateTarget()->GetWatchpointList().FindByID( 694 GetValue())); 695 if (wp_sp) { 696 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0)); 697 ProcessSP process_sp = exe_ctx.GetProcessSP(); 698 699 { 700 // check if this process is running on an architecture where 701 // watchpoints trigger before the associated instruction runs. if so, 702 // disable the WP, single-step and then re-enable the watchpoint 703 if (process_sp) { 704 uint32_t num; 705 bool wp_triggers_after; 706 707 if (process_sp->GetWatchpointSupportInfo(num, wp_triggers_after) 708 .Success()) { 709 if (!wp_triggers_after) { 710 // We need to preserve the watch_index before watchpoint is 711 // disable. Since Watchpoint::SetEnabled will clear the watch 712 // index. This will fix TestWatchpointIter failure 713 Watchpoint *wp = wp_sp.get(); 714 uint32_t watch_index = wp->GetHardwareIndex(); 715 process_sp->DisableWatchpoint(wp, false); 716 StopInfoSP stored_stop_info_sp = thread_sp->GetStopInfo(); 717 assert(stored_stop_info_sp.get() == this); 718 719 ThreadPlanSP new_plan_sp( 720 thread_sp->QueueThreadPlanForStepSingleInstruction( 721 false, // step-over 722 false, // abort_other_plans 723 true)); // stop_other_threads 724 new_plan_sp->SetIsMasterPlan(true); 725 new_plan_sp->SetOkayToDiscard(false); 726 new_plan_sp->SetPrivate(true); 727 process_sp->GetThreadList().SetSelectedThreadByID( 728 thread_sp->GetID()); 729 process_sp->ResumeSynchronous(nullptr); 730 process_sp->GetThreadList().SetSelectedThreadByID( 731 thread_sp->GetID()); 732 thread_sp->SetStopInfo(stored_stop_info_sp); 733 process_sp->EnableWatchpoint(wp, false); 734 wp->SetHardwareIndex(watch_index); 735 } 736 } 737 } 738 } 739 740 // This sentry object makes sure the current watchpoint is disabled 741 // while performing watchpoint actions, and it is then enabled after we 742 // are finished. 743 WatchpointSentry sentry(process_sp, wp_sp); 744 745 /* 746 * MIPS: Last 3bits of the watchpoint address are masked by the kernel. 747 * For example: 748 * 'n' is at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is 749 * set at 'm', then 750 * watch exception is generated even when 'n' is read/written. To handle 751 * this case, 752 * server emulates the instruction at PC and finds the base address of 753 * the load/store 754 * instruction and appends it in the description of the stop-info 755 * packet. If watchpoint 756 * is not set on this address by user then this do not stop. 757 */ 758 if (m_watch_hit_addr != LLDB_INVALID_ADDRESS) { 759 WatchpointSP wp_hit_sp = 760 thread_sp->CalculateTarget()->GetWatchpointList().FindByAddress( 761 m_watch_hit_addr); 762 if (!wp_hit_sp) { 763 m_should_stop = false; 764 wp_sp->IncrementFalseAlarmsAndReviseHitCount(); 765 } 766 } 767 768 // TODO: This condition should be checked in the synchronous part of the 769 // watchpoint code 770 // (Watchpoint::ShouldStop), so that we avoid pulling an event even if 771 // the watchpoint fails the ignore count condition. It is moved here 772 // temporarily, because for archs with 773 // watchpoint_exceptions_received=before, the code in the previous 774 // lines takes care of moving the inferior to next PC. We have to check 775 // the ignore count condition after this is done, otherwise we will hit 776 // same watchpoint multiple times until we pass ignore condition, but 777 // we won't actually be ignoring them. 778 if (wp_sp->GetHitCount() <= wp_sp->GetIgnoreCount()) 779 m_should_stop = false; 780 781 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger(); 782 783 if (m_should_stop && wp_sp->GetConditionText() != nullptr) { 784 // We need to make sure the user sees any parse errors in their 785 // condition, so we'll hook the constructor errors up to the 786 // debugger's Async I/O. 787 ExpressionResults result_code; 788 EvaluateExpressionOptions expr_options; 789 expr_options.SetUnwindOnError(true); 790 expr_options.SetIgnoreBreakpoints(true); 791 ValueObjectSP result_value_sp; 792 Status error; 793 result_code = UserExpression::Evaluate( 794 exe_ctx, expr_options, wp_sp->GetConditionText(), 795 llvm::StringRef(), result_value_sp, error); 796 797 if (result_code == eExpressionCompleted) { 798 if (result_value_sp) { 799 Scalar scalar_value; 800 if (result_value_sp->ResolveValue(scalar_value)) { 801 if (scalar_value.ULongLong(1) == 0) { 802 // We have been vetoed. This takes precedence over querying 803 // the watchpoint whether it should stop (aka ignore count 804 // and friends). See also StopInfoWatchpoint::ShouldStop() 805 // as well as Process::ProcessEventData::DoOnRemoval(). 806 m_should_stop = false; 807 } else 808 m_should_stop = true; 809 if (log) 810 log->Printf( 811 "Condition successfully evaluated, result is %s.\n", 812 m_should_stop ? "true" : "false"); 813 } else { 814 m_should_stop = true; 815 if (log) 816 log->Printf( 817 "Failed to get an integer result from the expression."); 818 } 819 } 820 } else { 821 StreamSP error_sp = debugger.GetAsyncErrorStream(); 822 error_sp->Printf( 823 "Stopped due to an error evaluating condition of watchpoint "); 824 wp_sp->GetDescription(error_sp.get(), eDescriptionLevelBrief); 825 error_sp->Printf(": \"%s\"", wp_sp->GetConditionText()); 826 error_sp->EOL(); 827 const char *err_str = error.AsCString("<Unknown Error>"); 828 if (log) 829 log->Printf("Error evaluating condition: \"%s\"\n", err_str); 830 831 error_sp->PutCString(err_str); 832 error_sp->EOL(); 833 error_sp->Flush(); 834 // If the condition fails to be parsed or run, we should stop. 835 m_should_stop = true; 836 } 837 } 838 839 // If the condition says to stop, we run the callback to further decide 840 // whether to stop. 841 if (m_should_stop) { 842 // FIXME: For now the callbacks have to run in async mode - the 843 // first time we restart we need 844 // to get out of there. So set it here. 845 // When we figure out how to nest watchpoint hits then this will 846 // change. 847 848 bool old_async = debugger.GetAsyncExecution(); 849 debugger.SetAsyncExecution(true); 850 851 StoppointCallbackContext context(event_ptr, exe_ctx, false); 852 bool stop_requested = wp_sp->InvokeCallback(&context); 853 854 debugger.SetAsyncExecution(old_async); 855 856 // Also make sure that the callback hasn't continued the target. If 857 // it did, when we'll set m_should_stop to false and get out of here. 858 if (HasTargetRunSinceMe()) 859 m_should_stop = false; 860 861 if (m_should_stop && !stop_requested) { 862 // We have been vetoed by the callback mechanism. 863 m_should_stop = false; 864 } 865 } 866 // Finally, if we are going to stop, print out the new & old values: 867 if (m_should_stop) { 868 wp_sp->CaptureWatchedValue(exe_ctx); 869 870 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger(); 871 StreamSP output_sp = debugger.GetAsyncOutputStream(); 872 wp_sp->DumpSnapshots(output_sp.get()); 873 output_sp->EOL(); 874 output_sp->Flush(); 875 } 876 877 } else { 878 Log *log_process( 879 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 880 881 if (log_process) 882 log_process->Printf( 883 "Process::%s could not find watchpoint id: %" PRId64 "...", 884 __FUNCTION__, m_value); 885 } 886 if (log) 887 log->Printf("Process::%s returning from action with m_should_stop: %d.", 888 __FUNCTION__, m_should_stop); 889 890 m_should_stop_is_valid = true; 891 } 892 } 893 894 private: 895 bool m_should_stop; 896 bool m_should_stop_is_valid; 897 lldb::addr_t m_watch_hit_addr; 898 }; 899 900 //---------------------------------------------------------------------- 901 // StopInfoUnixSignal 902 //---------------------------------------------------------------------- 903 904 class StopInfoUnixSignal : public StopInfo { 905 public: 906 StopInfoUnixSignal(Thread &thread, int signo, const char *description) 907 : StopInfo(thread, signo) { 908 SetDescription(description); 909 } 910 911 ~StopInfoUnixSignal() override = default; 912 913 StopReason GetStopReason() const override { return eStopReasonSignal; } 914 915 bool ShouldStopSynchronous(Event *event_ptr) override { 916 ThreadSP thread_sp(m_thread_wp.lock()); 917 if (thread_sp) 918 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value); 919 return false; 920 } 921 922 bool ShouldStop(Event *event_ptr) override { 923 ThreadSP thread_sp(m_thread_wp.lock()); 924 if (thread_sp) 925 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value); 926 return false; 927 } 928 929 // If should stop returns false, check if we should notify of this event 930 bool DoShouldNotify(Event *event_ptr) override { 931 ThreadSP thread_sp(m_thread_wp.lock()); 932 if (thread_sp) { 933 bool should_notify = 934 thread_sp->GetProcess()->GetUnixSignals()->GetShouldNotify(m_value); 935 if (should_notify) { 936 StreamString strm; 937 strm.Printf( 938 "thread %d received signal: %s", thread_sp->GetIndexID(), 939 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString( 940 m_value)); 941 Process::ProcessEventData::AddRestartedReason(event_ptr, 942 strm.GetData()); 943 } 944 return should_notify; 945 } 946 return true; 947 } 948 949 void WillResume(lldb::StateType resume_state) override { 950 ThreadSP thread_sp(m_thread_wp.lock()); 951 if (thread_sp) { 952 if (!thread_sp->GetProcess()->GetUnixSignals()->GetShouldSuppress( 953 m_value)) 954 thread_sp->SetResumeSignal(m_value); 955 } 956 } 957 958 const char *GetDescription() override { 959 if (m_description.empty()) { 960 ThreadSP thread_sp(m_thread_wp.lock()); 961 if (thread_sp) { 962 StreamString strm; 963 const char *signal_name = 964 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString( 965 m_value); 966 if (signal_name) 967 strm.Printf("signal %s", signal_name); 968 else 969 strm.Printf("signal %" PRIi64, m_value); 970 m_description = strm.GetString(); 971 } 972 } 973 return m_description.c_str(); 974 } 975 }; 976 977 //---------------------------------------------------------------------- 978 // StopInfoTrace 979 //---------------------------------------------------------------------- 980 981 class StopInfoTrace : public StopInfo { 982 public: 983 StopInfoTrace(Thread &thread) : StopInfo(thread, LLDB_INVALID_UID) {} 984 985 ~StopInfoTrace() override = default; 986 987 StopReason GetStopReason() const override { return eStopReasonTrace; } 988 989 const char *GetDescription() override { 990 if (m_description.empty()) 991 return "trace"; 992 else 993 return m_description.c_str(); 994 } 995 }; 996 997 //---------------------------------------------------------------------- 998 // StopInfoException 999 //---------------------------------------------------------------------- 1000 1001 class StopInfoException : public StopInfo { 1002 public: 1003 StopInfoException(Thread &thread, const char *description) 1004 : StopInfo(thread, LLDB_INVALID_UID) { 1005 if (description) 1006 SetDescription(description); 1007 } 1008 1009 ~StopInfoException() override = default; 1010 1011 StopReason GetStopReason() const override { return eStopReasonException; } 1012 1013 const char *GetDescription() override { 1014 if (m_description.empty()) 1015 return "exception"; 1016 else 1017 return m_description.c_str(); 1018 } 1019 }; 1020 1021 //---------------------------------------------------------------------- 1022 // StopInfoThreadPlan 1023 //---------------------------------------------------------------------- 1024 1025 class StopInfoThreadPlan : public StopInfo { 1026 public: 1027 StopInfoThreadPlan(ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp, 1028 ExpressionVariableSP &expression_variable_sp) 1029 : StopInfo(plan_sp->GetThread(), LLDB_INVALID_UID), m_plan_sp(plan_sp), 1030 m_return_valobj_sp(return_valobj_sp), 1031 m_expression_variable_sp(expression_variable_sp) {} 1032 1033 ~StopInfoThreadPlan() override = default; 1034 1035 StopReason GetStopReason() const override { return eStopReasonPlanComplete; } 1036 1037 const char *GetDescription() override { 1038 if (m_description.empty()) { 1039 StreamString strm; 1040 m_plan_sp->GetDescription(&strm, eDescriptionLevelBrief); 1041 m_description = strm.GetString(); 1042 } 1043 return m_description.c_str(); 1044 } 1045 1046 ValueObjectSP GetReturnValueObject() { return m_return_valobj_sp; } 1047 1048 ExpressionVariableSP GetExpressionVariable() { 1049 return m_expression_variable_sp; 1050 } 1051 1052 protected: 1053 bool ShouldStop(Event *event_ptr) override { 1054 if (m_plan_sp) 1055 return m_plan_sp->ShouldStop(event_ptr); 1056 else 1057 return StopInfo::ShouldStop(event_ptr); 1058 } 1059 1060 private: 1061 ThreadPlanSP m_plan_sp; 1062 ValueObjectSP m_return_valobj_sp; 1063 ExpressionVariableSP m_expression_variable_sp; 1064 }; 1065 1066 //---------------------------------------------------------------------- 1067 // StopInfoExec 1068 //---------------------------------------------------------------------- 1069 1070 class StopInfoExec : public StopInfo { 1071 public: 1072 StopInfoExec(Thread &thread) 1073 : StopInfo(thread, LLDB_INVALID_UID), m_performed_action(false) {} 1074 1075 ~StopInfoExec() override = default; 1076 1077 bool ShouldStop(Event *event_ptr) override { 1078 ThreadSP thread_sp(m_thread_wp.lock()); 1079 if (thread_sp) 1080 return thread_sp->GetProcess()->GetStopOnExec(); 1081 return false; 1082 } 1083 1084 StopReason GetStopReason() const override { return eStopReasonExec; } 1085 1086 const char *GetDescription() override { return "exec"; } 1087 1088 protected: 1089 void PerformAction(Event *event_ptr) override { 1090 // Only perform the action once 1091 if (m_performed_action) 1092 return; 1093 m_performed_action = true; 1094 ThreadSP thread_sp(m_thread_wp.lock()); 1095 if (thread_sp) 1096 thread_sp->GetProcess()->DidExec(); 1097 } 1098 1099 bool m_performed_action; 1100 }; 1101 1102 } // namespace lldb_private 1103 1104 StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread, 1105 break_id_t break_id) { 1106 return StopInfoSP(new StopInfoBreakpoint(thread, break_id)); 1107 } 1108 1109 StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread, 1110 break_id_t break_id, 1111 bool should_stop) { 1112 return StopInfoSP(new StopInfoBreakpoint(thread, break_id, should_stop)); 1113 } 1114 1115 StopInfoSP 1116 StopInfo::CreateStopReasonWithWatchpointID(Thread &thread, break_id_t watch_id, 1117 lldb::addr_t watch_hit_addr) { 1118 return StopInfoSP(new StopInfoWatchpoint(thread, watch_id, watch_hit_addr)); 1119 } 1120 1121 StopInfoSP StopInfo::CreateStopReasonWithSignal(Thread &thread, int signo, 1122 const char *description) { 1123 return StopInfoSP(new StopInfoUnixSignal(thread, signo, description)); 1124 } 1125 1126 StopInfoSP StopInfo::CreateStopReasonToTrace(Thread &thread) { 1127 return StopInfoSP(new StopInfoTrace(thread)); 1128 } 1129 1130 StopInfoSP StopInfo::CreateStopReasonWithPlan( 1131 ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp, 1132 ExpressionVariableSP expression_variable_sp) { 1133 return StopInfoSP(new StopInfoThreadPlan(plan_sp, return_valobj_sp, 1134 expression_variable_sp)); 1135 } 1136 1137 StopInfoSP StopInfo::CreateStopReasonWithException(Thread &thread, 1138 const char *description) { 1139 return StopInfoSP(new StopInfoException(thread, description)); 1140 } 1141 1142 StopInfoSP StopInfo::CreateStopReasonWithExec(Thread &thread) { 1143 return StopInfoSP(new StopInfoExec(thread)); 1144 } 1145 1146 ValueObjectSP StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp) { 1147 if (stop_info_sp && 1148 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) { 1149 StopInfoThreadPlan *plan_stop_info = 1150 static_cast<StopInfoThreadPlan *>(stop_info_sp.get()); 1151 return plan_stop_info->GetReturnValueObject(); 1152 } else 1153 return ValueObjectSP(); 1154 } 1155 1156 ExpressionVariableSP StopInfo::GetExpressionVariable(StopInfoSP &stop_info_sp) { 1157 if (stop_info_sp && 1158 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) { 1159 StopInfoThreadPlan *plan_stop_info = 1160 static_cast<StopInfoThreadPlan *>(stop_info_sp.get()); 1161 return plan_stop_info->GetExpressionVariable(); 1162 } else 1163 return ExpressionVariableSP(); 1164 } 1165 1166 lldb::ValueObjectSP 1167 StopInfo::GetCrashingDereference(StopInfoSP &stop_info_sp, 1168 lldb::addr_t *crashing_address) { 1169 if (!stop_info_sp) { 1170 return ValueObjectSP(); 1171 } 1172 1173 const char *description = stop_info_sp->GetDescription(); 1174 if (!description) { 1175 return ValueObjectSP(); 1176 } 1177 1178 ThreadSP thread_sp = stop_info_sp->GetThread(); 1179 if (!thread_sp) { 1180 return ValueObjectSP(); 1181 } 1182 1183 StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); 1184 1185 if (!frame_sp) { 1186 return ValueObjectSP(); 1187 } 1188 1189 const char address_string[] = "address="; 1190 1191 const char *address_loc = strstr(description, address_string); 1192 if (!address_loc) { 1193 return ValueObjectSP(); 1194 } 1195 1196 address_loc += (sizeof(address_string) - 1); 1197 1198 uint64_t address = strtoull(address_loc, 0, 0); 1199 if (crashing_address) { 1200 *crashing_address = address; 1201 } 1202 1203 return frame_sp->GuessValueForAddress(address); 1204 } 1205