1 //===-- ThreadPlanStepOut.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 // Other libraries and framework includes 13 // Project includes 14 #include "lldb/Target/ThreadPlanStepOut.h" 15 #include "lldb/Breakpoint/Breakpoint.h" 16 #include "lldb/Core/Log.h" 17 #include "lldb/Core/Value.h" 18 #include "lldb/Core/ValueObjectConstResult.h" 19 #include "lldb/Symbol/Block.h" 20 #include "lldb/Symbol/Function.h" 21 #include "lldb/Symbol/Symbol.h" 22 #include "lldb/Symbol/Type.h" 23 #include "lldb/Target/ABI.h" 24 #include "lldb/Target/Process.h" 25 #include "lldb/Target/RegisterContext.h" 26 #include "lldb/Target/StopInfo.h" 27 #include "lldb/Target/Target.h" 28 #include "lldb/Target/ThreadPlanStepOverRange.h" 29 #include "lldb/Target/ThreadPlanStepThrough.h" 30 31 using namespace lldb; 32 using namespace lldb_private; 33 34 uint32_t ThreadPlanStepOut::s_default_flag_values = 0; 35 36 //---------------------------------------------------------------------- 37 // ThreadPlanStepOut: Step out of the current frame 38 //---------------------------------------------------------------------- 39 ThreadPlanStepOut::ThreadPlanStepOut 40 ( 41 Thread &thread, 42 SymbolContext *context, 43 bool first_insn, 44 bool stop_others, 45 Vote stop_vote, 46 Vote run_vote, 47 uint32_t frame_idx, 48 LazyBool step_out_avoids_code_without_debug_info, 49 bool continue_to_next_branch 50 ) : 51 ThreadPlan (ThreadPlan::eKindStepOut, "Step out", thread, stop_vote, run_vote), 52 ThreadPlanShouldStopHere (this), 53 m_step_from_insn (LLDB_INVALID_ADDRESS), 54 m_return_bp_id (LLDB_INVALID_BREAK_ID), 55 m_return_addr (LLDB_INVALID_ADDRESS), 56 m_stop_others (stop_others), 57 m_immediate_step_from_function(nullptr) 58 { 59 SetFlagsToDefault(); 60 SetupAvoidNoDebug(step_out_avoids_code_without_debug_info); 61 62 m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0); 63 64 StackFrameSP return_frame_sp (m_thread.GetStackFrameAtIndex(frame_idx + 1)); 65 StackFrameSP immediate_return_from_sp (m_thread.GetStackFrameAtIndex (frame_idx)); 66 67 if (!return_frame_sp || !immediate_return_from_sp) 68 return; // we can't do anything here. ValidatePlan() will return false. 69 70 m_step_out_to_id = return_frame_sp->GetStackID(); 71 m_immediate_step_from_id = immediate_return_from_sp->GetStackID(); 72 73 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); 74 75 // If the frame directly below the one we are returning to is inlined, we have to be 76 // a little more careful. It is non-trivial to determine the real "return code address" for 77 // an inlined frame, so we have to work our way to that frame and then step out. 78 if (immediate_return_from_sp && immediate_return_from_sp->IsInlined()) 79 { 80 if (frame_idx > 0) 81 { 82 // First queue a plan that gets us to this inlined frame, and when we get there we'll queue a second 83 // plan that walks us out of this frame. 84 m_step_out_to_inline_plan_sp.reset(new ThreadPlanStepOut(m_thread, 85 nullptr, 86 false, 87 stop_others, 88 eVoteNoOpinion, 89 eVoteNoOpinion, 90 frame_idx - 1, 91 eLazyBoolNo, 92 continue_to_next_branch)); 93 static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())->SetShouldStopHereCallbacks(nullptr, nullptr); 94 m_step_out_to_inline_plan_sp->SetPrivate(true); 95 } 96 else 97 { 98 // If we're already at the inlined frame we're stepping through, then just do that now. 99 QueueInlinedStepPlan(false); 100 } 101 } 102 else if (return_frame_sp) 103 { 104 // Find the return address and set a breakpoint there: 105 // FIXME - can we do this more securely if we know first_insn? 106 107 Address return_address (return_frame_sp->GetFrameCodeAddress()); 108 if (continue_to_next_branch) 109 { 110 SymbolContext return_address_sc; 111 AddressRange range; 112 Address return_address_decr_pc = return_address; 113 if (return_address_decr_pc.GetOffset() > 0) 114 return_address_decr_pc.Slide (-1); 115 116 return_address_decr_pc.CalculateSymbolContext (&return_address_sc, lldb::eSymbolContextLineEntry | lldb::eSymbolContextFunction | lldb::eSymbolContextSymbol); 117 if (return_address_sc.line_entry.IsValid()) 118 { 119 range = return_address_sc.line_entry.GetSameLineContiguousAddressRange(); 120 } 121 else if (return_address_sc.function) 122 { 123 range = return_address_sc.function->GetAddressRange(); 124 } 125 else if (return_address_sc.symbol && return_address_sc.symbol->GetByteSizeIsValid()) 126 { 127 range.GetBaseAddress() = return_address_sc.symbol->GetAddress(); 128 range.SetByteSize (return_address_sc.symbol->GetByteSize()); 129 } 130 if (range.GetByteSize() > 0) 131 { 132 return_address = m_thread.GetProcess()->AdvanceAddressToNextBranchInstruction (return_address, range); 133 } 134 } 135 m_return_addr = return_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget()); 136 137 if (m_return_addr == LLDB_INVALID_ADDRESS) 138 return; 139 140 Breakpoint *return_bp = m_thread.CalculateTarget()->CreateBreakpoint (m_return_addr, true, false).get(); 141 if (return_bp != nullptr) 142 { 143 return_bp->SetThreadID(m_thread.GetID()); 144 m_return_bp_id = return_bp->GetID(); 145 return_bp->SetBreakpointKind ("step-out"); 146 } 147 148 if (immediate_return_from_sp) 149 { 150 const SymbolContext &sc = immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction); 151 if (sc.function) 152 { 153 m_immediate_step_from_function = sc.function; 154 } 155 } 156 } 157 } 158 159 void 160 ThreadPlanStepOut::SetupAvoidNoDebug(LazyBool step_out_avoids_code_without_debug_info) 161 { 162 bool avoid_nodebug = true; 163 switch (step_out_avoids_code_without_debug_info) 164 { 165 case eLazyBoolYes: 166 avoid_nodebug = true; 167 break; 168 case eLazyBoolNo: 169 avoid_nodebug = false; 170 break; 171 case eLazyBoolCalculate: 172 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug(); 173 break; 174 } 175 if (avoid_nodebug) 176 GetFlags().Set (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug); 177 else 178 GetFlags().Clear (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug); 179 } 180 181 void 182 ThreadPlanStepOut::DidPush() 183 { 184 if (m_step_out_to_inline_plan_sp) 185 m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false); 186 else if (m_step_through_inline_plan_sp) 187 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false); 188 } 189 190 ThreadPlanStepOut::~ThreadPlanStepOut () 191 { 192 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) 193 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id); 194 } 195 196 void 197 ThreadPlanStepOut::GetDescription (Stream *s, lldb::DescriptionLevel level) 198 { 199 if (level == lldb::eDescriptionLevelBrief) 200 s->Printf ("step out"); 201 else 202 { 203 if (m_step_out_to_inline_plan_sp) 204 s->Printf ("Stepping out to inlined frame so we can walk through it."); 205 else if (m_step_through_inline_plan_sp) 206 s->Printf ("Stepping out by stepping through inlined function."); 207 else 208 { 209 s->Printf ("Stepping out from "); 210 Address tmp_address; 211 if (tmp_address.SetLoadAddress (m_step_from_insn, &GetTarget())) 212 { 213 tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription, Address::DumpStyleLoadAddress); 214 } 215 else 216 { 217 s->Printf ("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn); 218 } 219 220 // FIXME: find some useful way to present the m_return_id, since there may be multiple copies of the 221 // same function on the stack. 222 223 s->Printf (" returning to frame at "); 224 if (tmp_address.SetLoadAddress (m_return_addr, &GetTarget())) 225 { 226 tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription, Address::DumpStyleLoadAddress); 227 } 228 else 229 { 230 s->Printf ("address 0x%" PRIx64 "", (uint64_t)m_return_addr); 231 } 232 233 if (level == eDescriptionLevelVerbose) 234 s->Printf(" using breakpoint site %d", m_return_bp_id); 235 } 236 } 237 } 238 239 bool 240 ThreadPlanStepOut::ValidatePlan (Stream *error) 241 { 242 if (m_step_out_to_inline_plan_sp) 243 return m_step_out_to_inline_plan_sp->ValidatePlan (error); 244 else if (m_step_through_inline_plan_sp) 245 return m_step_through_inline_plan_sp->ValidatePlan (error); 246 else if (m_return_bp_id == LLDB_INVALID_BREAK_ID) 247 { 248 if (error) 249 error->PutCString("Could not create return address breakpoint."); 250 return false; 251 } 252 else 253 return true; 254 } 255 256 bool 257 ThreadPlanStepOut::DoPlanExplainsStop (Event *event_ptr) 258 { 259 // If the step out plan is done, then we just need to step through the inlined frame. 260 if (m_step_out_to_inline_plan_sp) 261 { 262 return m_step_out_to_inline_plan_sp->MischiefManaged(); 263 } 264 else if (m_step_through_inline_plan_sp) 265 { 266 if (m_step_through_inline_plan_sp->MischiefManaged()) 267 { 268 CalculateReturnValue(); 269 SetPlanComplete(); 270 return true; 271 } 272 else 273 return false; 274 } 275 else if (m_step_out_further_plan_sp) 276 { 277 return m_step_out_further_plan_sp->MischiefManaged(); 278 } 279 280 // We don't explain signals or breakpoints (breakpoints that handle stepping in or 281 // out will be handled by a child plan. 282 283 StopInfoSP stop_info_sp = GetPrivateStopInfo (); 284 if (stop_info_sp) 285 { 286 StopReason reason = stop_info_sp->GetStopReason(); 287 if (reason == eStopReasonBreakpoint) 288 { 289 // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened... 290 BreakpointSiteSP site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue())); 291 if (site_sp && site_sp->IsBreakpointAtThisSite (m_return_bp_id)) 292 { 293 bool done; 294 295 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); 296 297 if (m_step_out_to_id == frame_zero_id) 298 done = true; 299 else if (m_step_out_to_id < frame_zero_id) 300 { 301 // Either we stepped past the breakpoint, or the stack ID calculation 302 // was incorrect and we should probably stop. 303 done = true; 304 } 305 else 306 { 307 done = (m_immediate_step_from_id < frame_zero_id); 308 } 309 310 if (done) 311 { 312 if (InvokeShouldStopHereCallback (eFrameCompareOlder)) 313 { 314 CalculateReturnValue(); 315 SetPlanComplete(); 316 } 317 } 318 319 // If there was only one owner, then we're done. But if we also hit some 320 // user breakpoint on our way out, we should mark ourselves as done, but 321 // also not claim to explain the stop, since it is more important to report 322 // the user breakpoint than the step out completion. 323 324 if (site_sp->GetNumberOfOwners() == 1) 325 return true; 326 } 327 return false; 328 } 329 else if (IsUsuallyUnexplainedStopReason(reason)) 330 return false; 331 else 332 return true; 333 } 334 return true; 335 } 336 337 bool 338 ThreadPlanStepOut::ShouldStop (Event *event_ptr) 339 { 340 if (IsPlanComplete()) 341 return true; 342 343 bool done = false; 344 if (m_step_out_to_inline_plan_sp) 345 { 346 if (m_step_out_to_inline_plan_sp->MischiefManaged()) 347 { 348 // Now step through the inlined stack we are in: 349 if (QueueInlinedStepPlan(true)) 350 { 351 // If we can't queue a plan to do this, then just call ourselves done. 352 m_step_out_to_inline_plan_sp.reset(); 353 SetPlanComplete (false); 354 return true; 355 } 356 else 357 done = true; 358 } 359 else 360 return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr); 361 } 362 else if (m_step_through_inline_plan_sp) 363 { 364 if (m_step_through_inline_plan_sp->MischiefManaged()) 365 done = true; 366 else 367 return m_step_through_inline_plan_sp->ShouldStop(event_ptr); 368 } 369 else if (m_step_out_further_plan_sp) 370 { 371 if (m_step_out_further_plan_sp->MischiefManaged()) 372 m_step_out_further_plan_sp.reset(); 373 else 374 return m_step_out_further_plan_sp->ShouldStop(event_ptr); 375 } 376 377 if (!done) 378 { 379 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); 380 done = !(frame_zero_id < m_step_out_to_id); 381 } 382 383 // The normal step out computations think we are done, so all we need to do is consult the ShouldStopHere, 384 // and we are done. 385 386 if (done) 387 { 388 if (InvokeShouldStopHereCallback(eFrameCompareOlder)) 389 { 390 CalculateReturnValue(); 391 SetPlanComplete(); 392 } 393 else 394 { 395 m_step_out_further_plan_sp = QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder); 396 done = false; 397 } 398 } 399 400 return done; 401 } 402 403 bool 404 ThreadPlanStepOut::StopOthers () 405 { 406 return m_stop_others; 407 } 408 409 StateType 410 ThreadPlanStepOut::GetPlanRunState () 411 { 412 return eStateRunning; 413 } 414 415 bool 416 ThreadPlanStepOut::DoWillResume (StateType resume_state, bool current_plan) 417 { 418 if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp) 419 return true; 420 421 if (m_return_bp_id == LLDB_INVALID_BREAK_ID) 422 return false; 423 424 if (current_plan) 425 { 426 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get(); 427 if (return_bp != nullptr) 428 return_bp->SetEnabled (true); 429 } 430 return true; 431 } 432 433 bool 434 ThreadPlanStepOut::WillStop () 435 { 436 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) 437 { 438 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get(); 439 if (return_bp != nullptr) 440 return_bp->SetEnabled (false); 441 } 442 443 return true; 444 } 445 446 bool 447 ThreadPlanStepOut::MischiefManaged () 448 { 449 if (IsPlanComplete()) 450 { 451 // Did I reach my breakpoint? If so I'm done. 452 // 453 // I also check the stack depth, since if we've blown past the breakpoint for some 454 // reason and we're now stopping for some other reason altogether, then we're done 455 // with this step out operation. 456 457 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 458 if (log) 459 log->Printf("Completed step out plan."); 460 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) 461 { 462 m_thread.CalculateTarget()->RemoveBreakpointByID (m_return_bp_id); 463 m_return_bp_id = LLDB_INVALID_BREAK_ID; 464 } 465 466 ThreadPlan::MischiefManaged (); 467 return true; 468 } 469 else 470 { 471 return false; 472 } 473 } 474 475 bool 476 ThreadPlanStepOut::QueueInlinedStepPlan (bool queue_now) 477 { 478 // Now figure out the range of this inlined block, and set up a "step through range" 479 // plan for that. If we've been provided with a context, then use the block in that 480 // context. 481 StackFrameSP immediate_return_from_sp (m_thread.GetStackFrameAtIndex (0)); 482 if (!immediate_return_from_sp) 483 return false; 484 485 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 486 if (log) 487 { 488 StreamString s; 489 immediate_return_from_sp->Dump(&s, true, false); 490 log->Printf("Queuing inlined frame to step past: %s.", s.GetData()); 491 } 492 493 Block *from_block = immediate_return_from_sp->GetFrameBlock(); 494 if (from_block) 495 { 496 Block *inlined_block = from_block->GetContainingInlinedBlock(); 497 if (inlined_block) 498 { 499 size_t num_ranges = inlined_block->GetNumRanges(); 500 AddressRange inline_range; 501 if (inlined_block->GetRangeAtIndex(0, inline_range)) 502 { 503 SymbolContext inlined_sc; 504 inlined_block->CalculateSymbolContext(&inlined_sc); 505 inlined_sc.target_sp = GetTarget().shared_from_this(); 506 RunMode run_mode = m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads; 507 const LazyBool avoid_no_debug = eLazyBoolNo; 508 509 m_step_through_inline_plan_sp.reset (new ThreadPlanStepOverRange(m_thread, 510 inline_range, 511 inlined_sc, 512 run_mode, 513 avoid_no_debug)); 514 ThreadPlanStepOverRange *step_through_inline_plan_ptr 515 = static_cast<ThreadPlanStepOverRange *>(m_step_through_inline_plan_sp.get()); 516 m_step_through_inline_plan_sp->SetPrivate(true); 517 518 step_through_inline_plan_ptr->SetOkayToDiscard(true); 519 StreamString errors; 520 if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) 521 { 522 //FIXME: Log this failure. 523 delete step_through_inline_plan_ptr; 524 return false; 525 } 526 527 for (size_t i = 1; i < num_ranges; i++) 528 { 529 if (inlined_block->GetRangeAtIndex (i, inline_range)) 530 step_through_inline_plan_ptr->AddRange (inline_range); 531 } 532 533 if (queue_now) 534 m_thread.QueueThreadPlan (m_step_through_inline_plan_sp, false); 535 return true; 536 } 537 } 538 } 539 540 return false; 541 } 542 543 void 544 ThreadPlanStepOut::CalculateReturnValue () 545 { 546 if (m_return_valobj_sp) 547 return; 548 549 if (m_immediate_step_from_function != nullptr) 550 { 551 CompilerType return_compiler_type = m_immediate_step_from_function->GetCompilerType().GetFunctionReturnType(); 552 if (return_compiler_type) 553 { 554 lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI(); 555 if (abi_sp) 556 m_return_valobj_sp = abi_sp->GetReturnValueObject(m_thread, return_compiler_type); 557 } 558 } 559 } 560 561 bool 562 ThreadPlanStepOut::IsPlanStale() 563 { 564 // If we are still lower on the stack than the frame we are returning to, then 565 // there's something for us to do. Otherwise, we're stale. 566 567 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); 568 return !(frame_zero_id < m_step_out_to_id); 569 } 570