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