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