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