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