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