130fdc8d8SChris Lattner //===-- ThreadPlanStepOut.cpp -----------------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 630fdc8d8SChris Lattner // 730fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 830fdc8d8SChris Lattner 9e65b2cf2SEugene Zelenko #include "lldb/Target/ThreadPlanStepOut.h" 1030fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h" 1173ca05a2SJim Ingham #include "lldb/Core/Value.h" 1273ca05a2SJim Ingham #include "lldb/Core/ValueObjectConstResult.h" 131f746071SGreg Clayton #include "lldb/Symbol/Block.h" 141f746071SGreg Clayton #include "lldb/Symbol/Function.h" 15fd4cea53SJason Molenda #include "lldb/Symbol/Symbol.h" 161f746071SGreg Clayton #include "lldb/Symbol/Type.h" 1732abc6edSZachary Turner #include "lldb/Target/ABI.h" 1830fdc8d8SChris Lattner #include "lldb/Target/Process.h" 1930fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h" 20f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h" 2130fdc8d8SChris Lattner #include "lldb/Target/Target.h" 22a5ce6c88SJim Ingham #include "lldb/Target/ThreadPlanStepOverRange.h" 234b4b2478SJim Ingham #include "lldb/Target/ThreadPlanStepThrough.h" 246f9e6901SZachary Turner #include "lldb/Utility/Log.h" 2530fdc8d8SChris Lattner 26796ac80bSJonas Devlieghere #include <memory> 27796ac80bSJonas Devlieghere 2830fdc8d8SChris Lattner using namespace lldb; 2930fdc8d8SChris Lattner using namespace lldb_private; 3030fdc8d8SChris Lattner 314b4b2478SJim Ingham uint32_t ThreadPlanStepOut::s_default_flag_values = 0; 324b4b2478SJim Ingham 3330fdc8d8SChris Lattner // ThreadPlanStepOut: Step out of the current frame 34b9c1b51eSKate Stone ThreadPlanStepOut::ThreadPlanStepOut( 35b9c1b51eSKate Stone Thread &thread, SymbolContext *context, bool first_insn, bool stop_others, 36b9c1b51eSKate Stone Vote stop_vote, Vote run_vote, uint32_t frame_idx, 37fd4cea53SJason Molenda LazyBool step_out_avoids_code_without_debug_info, 38b9c1b51eSKate Stone bool continue_to_next_branch, bool gather_return_value) 39b9c1b51eSKate Stone : ThreadPlan(ThreadPlan::eKindStepOut, "Step out", thread, stop_vote, 40b9c1b51eSKate Stone run_vote), 41b9c1b51eSKate Stone ThreadPlanShouldStopHere(this), m_step_from_insn(LLDB_INVALID_ADDRESS), 421ee0d4f7SBenjamin Kramer m_return_bp_id(LLDB_INVALID_BREAK_ID), 43b9c1b51eSKate Stone m_return_addr(LLDB_INVALID_ADDRESS), m_stop_others(stop_others), 44b612ac37SJim Ingham m_immediate_step_from_function(nullptr), 45b9c1b51eSKate Stone m_calculate_return_value(gather_return_value) { 464b36f791SVedant Kumar Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 474b4b2478SJim Ingham SetFlagsToDefault(); 484b4b2478SJim Ingham SetupAvoidNoDebug(step_out_avoids_code_without_debug_info); 494b4b2478SJim Ingham 5030fdc8d8SChris Lattner m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0); 5130fdc8d8SChris Lattner 524b36f791SVedant Kumar uint32_t return_frame_index = frame_idx + 1; 534b36f791SVedant Kumar StackFrameSP return_frame_sp( 544b36f791SVedant Kumar m_thread.GetStackFrameAtIndex(return_frame_index)); 55b9c1b51eSKate Stone StackFrameSP immediate_return_from_sp( 56b9c1b51eSKate Stone m_thread.GetStackFrameAtIndex(frame_idx)); 57a5ce6c88SJim Ingham 58632d2f72SSean Callanan if (!return_frame_sp || !immediate_return_from_sp) 59632d2f72SSean Callanan return; // we can't do anything here. ValidatePlan() will return false. 60632d2f72SSean Callanan 614b36f791SVedant Kumar // While stepping out, behave as-if artificial frames are not present. 624b36f791SVedant Kumar while (return_frame_sp->IsArtificial()) { 634b36f791SVedant Kumar m_stepped_past_frames.push_back(return_frame_sp); 644b36f791SVedant Kumar 654b36f791SVedant Kumar ++return_frame_index; 664b36f791SVedant Kumar return_frame_sp = m_thread.GetStackFrameAtIndex(return_frame_index); 674b36f791SVedant Kumar 684b36f791SVedant Kumar // We never expect to see an artificial frame without a regular ancestor. 694b36f791SVedant Kumar // If this happens, log the issue and defensively refuse to step out. 704b36f791SVedant Kumar if (!return_frame_sp) { 714b36f791SVedant Kumar LLDB_LOG(log, "Can't step out of frame with artificial ancestors"); 724b36f791SVedant Kumar return; 734b36f791SVedant Kumar } 744b36f791SVedant Kumar } 754b36f791SVedant Kumar 76b5c0d1ccSJim Ingham m_step_out_to_id = return_frame_sp->GetStackID(); 77b5c0d1ccSJim Ingham m_immediate_step_from_id = immediate_return_from_sp->GetStackID(); 78a5ce6c88SJim Ingham 7905097246SAdrian Prantl // If the frame directly below the one we are returning to is inlined, we 8005097246SAdrian Prantl // have to be a little more careful. It is non-trivial to determine the real 8105097246SAdrian Prantl // "return code address" for an inlined frame, so we have to work our way to 8205097246SAdrian Prantl // that frame and then step out. 834b36f791SVedant Kumar if (immediate_return_from_sp->IsInlined()) { 84b9c1b51eSKate Stone if (frame_idx > 0) { 85b9c1b51eSKate Stone // First queue a plan that gets us to this inlined frame, and when we get 8605097246SAdrian Prantl // there we'll queue a second plan that walks us out of this frame. 87796ac80bSJonas Devlieghere m_step_out_to_inline_plan_sp = std::make_shared<ThreadPlanStepOut>( 88b9c1b51eSKate Stone m_thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion, 89796ac80bSJonas Devlieghere frame_idx - 1, eLazyBoolNo, continue_to_next_branch); 90b9c1b51eSKate Stone static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get()) 91b9c1b51eSKate Stone ->SetShouldStopHereCallbacks(nullptr, nullptr); 922bdbfd50SJim Ingham m_step_out_to_inline_plan_sp->SetPrivate(true); 93b9c1b51eSKate Stone } else { 9405097246SAdrian Prantl // If we're already at the inlined frame we're stepping through, then 9505097246SAdrian Prantl // just do that now. 96a5ce6c88SJim Ingham QueueInlinedStepPlan(false); 97a5ce6c88SJim Ingham } 984b36f791SVedant Kumar } else { 9930fdc8d8SChris Lattner // Find the return address and set a breakpoint there: 10030fdc8d8SChris Lattner // FIXME - can we do this more securely if we know first_insn? 10130fdc8d8SChris Lattner 102fd4cea53SJason Molenda Address return_address(return_frame_sp->GetFrameCodeAddress()); 103b9c1b51eSKate Stone if (continue_to_next_branch) { 104fd4cea53SJason Molenda SymbolContext return_address_sc; 105fd4cea53SJason Molenda AddressRange range; 106fd4cea53SJason Molenda Address return_address_decr_pc = return_address; 107fd4cea53SJason Molenda if (return_address_decr_pc.GetOffset() > 0) 108fd4cea53SJason Molenda return_address_decr_pc.Slide(-1); 109fd4cea53SJason Molenda 110b9c1b51eSKate Stone return_address_decr_pc.CalculateSymbolContext( 111b9c1b51eSKate Stone &return_address_sc, lldb::eSymbolContextLineEntry); 112b9c1b51eSKate Stone if (return_address_sc.line_entry.IsValid()) { 1138a777920SGreg Clayton const bool include_inlined_functions = false; 1148a777920SGreg Clayton range = return_address_sc.line_entry.GetSameLineContiguousAddressRange( 1158a777920SGreg Clayton include_inlined_functions); 116b9c1b51eSKate Stone if (range.GetByteSize() > 0) { 117b9c1b51eSKate Stone return_address = 118b9c1b51eSKate Stone m_thread.GetProcess()->AdvanceAddressToNextBranchInstruction( 119b9c1b51eSKate Stone return_address, range); 120fd4cea53SJason Molenda } 121fd4cea53SJason Molenda } 122fd4cea53SJason Molenda } 123b9c1b51eSKate Stone m_return_addr = 124b9c1b51eSKate Stone return_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget()); 125708709c0SSean Callanan 126708709c0SSean Callanan if (m_return_addr == LLDB_INVALID_ADDRESS) 127708709c0SSean Callanan return; 128708709c0SSean Callanan 129*2a42a5a2SJim Ingham // Perform some additional validation on the return address. 130*2a42a5a2SJim Ingham uint32_t permissions = 0; 131*2a42a5a2SJim Ingham if (!m_thread.GetProcess()->GetLoadAddressPermissions(m_return_addr, 132*2a42a5a2SJim Ingham permissions)) { 133*2a42a5a2SJim Ingham m_constructor_errors.Printf("Return address (0x%" PRIx64 134*2a42a5a2SJim Ingham ") permissions not found.", 135*2a42a5a2SJim Ingham m_return_addr); 136*2a42a5a2SJim Ingham LLDB_LOGF(log, "ThreadPlanStepOut(%p): %s", static_cast<void *>(this), 137*2a42a5a2SJim Ingham m_constructor_errors.GetData()); 138*2a42a5a2SJim Ingham return; 139*2a42a5a2SJim Ingham } else if (!(permissions & ePermissionsExecutable)) { 140*2a42a5a2SJim Ingham m_constructor_errors.Printf("Return address (0x%" PRIx64 141*2a42a5a2SJim Ingham ") did not point to executable memory.", 142*2a42a5a2SJim Ingham m_return_addr); 143*2a42a5a2SJim Ingham LLDB_LOGF(log, "ThreadPlanStepOut(%p): %s", static_cast<void *>(this), 144*2a42a5a2SJim Ingham m_constructor_errors.GetData()); 145*2a42a5a2SJim Ingham return; 146*2a42a5a2SJim Ingham } 147*2a42a5a2SJim Ingham 148b9c1b51eSKate Stone Breakpoint *return_bp = m_thread.CalculateTarget() 149b9c1b51eSKate Stone ->CreateBreakpoint(m_return_addr, true, false) 150b9c1b51eSKate Stone .get(); 151e103ae92SJonas Devlieghere 152b9c1b51eSKate Stone if (return_bp != nullptr) { 153e103ae92SJonas Devlieghere if (return_bp->IsHardware() && !return_bp->HasResolvedLocations()) 154e103ae92SJonas Devlieghere m_could_not_resolve_hw_bp = true; 15530fdc8d8SChris Lattner return_bp->SetThreadID(m_thread.GetID()); 15630fdc8d8SChris Lattner m_return_bp_id = return_bp->GetID(); 1572995077dSJim Ingham return_bp->SetBreakpointKind("step-out"); 15830fdc8d8SChris Lattner } 15973ca05a2SJim Ingham 160b9c1b51eSKate Stone if (immediate_return_from_sp) { 161b9c1b51eSKate Stone const SymbolContext &sc = 162b9c1b51eSKate Stone immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction); 163b9c1b51eSKate Stone if (sc.function) { 16473ca05a2SJim Ingham m_immediate_step_from_function = sc.function; 16573ca05a2SJim Ingham } 16673ca05a2SJim Ingham } 16730fdc8d8SChris Lattner } 168a5ce6c88SJim Ingham } 169a5ce6c88SJim Ingham 170b9c1b51eSKate Stone void ThreadPlanStepOut::SetupAvoidNoDebug( 171b9c1b51eSKate Stone LazyBool step_out_avoids_code_without_debug_info) { 1724b4b2478SJim Ingham bool avoid_nodebug = true; 173b9c1b51eSKate Stone switch (step_out_avoids_code_without_debug_info) { 1744b4b2478SJim Ingham case eLazyBoolYes: 1754b4b2478SJim Ingham avoid_nodebug = true; 1764b4b2478SJim Ingham break; 1774b4b2478SJim Ingham case eLazyBoolNo: 1784b4b2478SJim Ingham avoid_nodebug = false; 1794b4b2478SJim Ingham break; 1804b4b2478SJim Ingham case eLazyBoolCalculate: 1814b4b2478SJim Ingham avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug(); 1824b4b2478SJim Ingham break; 1834b4b2478SJim Ingham } 1844b4b2478SJim Ingham if (avoid_nodebug) 1854b4b2478SJim Ingham GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug); 1864b4b2478SJim Ingham else 1874b4b2478SJim Ingham GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug); 1884b4b2478SJim Ingham } 1894b4b2478SJim Ingham 190b9c1b51eSKate Stone void ThreadPlanStepOut::DidPush() { 1914b4b2478SJim Ingham if (m_step_out_to_inline_plan_sp) 1924b4b2478SJim Ingham m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false); 193a5ce6c88SJim Ingham else if (m_step_through_inline_plan_sp) 194a5ce6c88SJim Ingham m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false); 19530fdc8d8SChris Lattner } 19630fdc8d8SChris Lattner 197b9c1b51eSKate Stone ThreadPlanStepOut::~ThreadPlanStepOut() { 19830fdc8d8SChris Lattner if (m_return_bp_id != LLDB_INVALID_BREAK_ID) 1991ac04c30SGreg Clayton m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id); 20030fdc8d8SChris Lattner } 20130fdc8d8SChris Lattner 202b9c1b51eSKate Stone void ThreadPlanStepOut::GetDescription(Stream *s, 203b9c1b51eSKate Stone lldb::DescriptionLevel level) { 20430fdc8d8SChris Lattner if (level == lldb::eDescriptionLevelBrief) 20530fdc8d8SChris Lattner s->Printf("step out"); 206b9c1b51eSKate Stone else { 2074b4b2478SJim Ingham if (m_step_out_to_inline_plan_sp) 208b5c0d1ccSJim Ingham s->Printf("Stepping out to inlined frame so we can walk through it."); 209a5ce6c88SJim Ingham else if (m_step_through_inline_plan_sp) 210a5ce6c88SJim Ingham s->Printf("Stepping out by stepping through inlined function."); 211b9c1b51eSKate Stone else { 2122bdbfd50SJim Ingham s->Printf("Stepping out from "); 2132bdbfd50SJim Ingham Address tmp_address; 214b9c1b51eSKate Stone if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) { 215b9c1b51eSKate Stone tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription, 216b9c1b51eSKate Stone Address::DumpStyleLoadAddress); 217b9c1b51eSKate Stone } else { 2182bdbfd50SJim Ingham s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn); 2192bdbfd50SJim Ingham } 2202bdbfd50SJim Ingham 221b9c1b51eSKate Stone // FIXME: find some useful way to present the m_return_id, since there may 222b9c1b51eSKate Stone // be multiple copies of the 2232bdbfd50SJim Ingham // same function on the stack. 2242bdbfd50SJim Ingham 2252bdbfd50SJim Ingham s->Printf(" returning to frame at "); 226b9c1b51eSKate Stone if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) { 227b9c1b51eSKate Stone tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription, 228b9c1b51eSKate Stone Address::DumpStyleLoadAddress); 229b9c1b51eSKate Stone } else { 2302bdbfd50SJim Ingham s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr); 2312bdbfd50SJim Ingham } 2322bdbfd50SJim Ingham 2332bdbfd50SJim Ingham if (level == eDescriptionLevelVerbose) 2342bdbfd50SJim Ingham s->Printf(" using breakpoint site %d", m_return_bp_id); 2352bdbfd50SJim Ingham } 23630fdc8d8SChris Lattner } 2374b36f791SVedant Kumar 2384b36f791SVedant Kumar s->Printf("\n"); 2394b36f791SVedant Kumar for (StackFrameSP frame_sp : m_stepped_past_frames) { 2404b36f791SVedant Kumar s->Printf("Stepped out past: "); 2414b36f791SVedant Kumar frame_sp->DumpUsingSettingsFormat(s); 2424b36f791SVedant Kumar } 24330fdc8d8SChris Lattner } 24430fdc8d8SChris Lattner 245b9c1b51eSKate Stone bool ThreadPlanStepOut::ValidatePlan(Stream *error) { 2464b4b2478SJim Ingham if (m_step_out_to_inline_plan_sp) 2474b4b2478SJim Ingham return m_step_out_to_inline_plan_sp->ValidatePlan(error); 248e103ae92SJonas Devlieghere 249e103ae92SJonas Devlieghere if (m_step_through_inline_plan_sp) 250a5ce6c88SJim Ingham return m_step_through_inline_plan_sp->ValidatePlan(error); 251e103ae92SJonas Devlieghere 252e103ae92SJonas Devlieghere if (m_could_not_resolve_hw_bp) { 253e103ae92SJonas Devlieghere if (error) 254e103ae92SJonas Devlieghere error->PutCString( 255e103ae92SJonas Devlieghere "Could not create hardware breakpoint for thread plan."); 256e103ae92SJonas Devlieghere return false; 257e103ae92SJonas Devlieghere } 258e103ae92SJonas Devlieghere 259e103ae92SJonas Devlieghere if (m_return_bp_id == LLDB_INVALID_BREAK_ID) { 260*2a42a5a2SJim Ingham if (error) { 261a5ce6c88SJim Ingham error->PutCString("Could not create return address breakpoint."); 262*2a42a5a2SJim Ingham if (m_constructor_errors.GetSize() > 0) { 263*2a42a5a2SJim Ingham error->PutCString(" "); 264*2a42a5a2SJim Ingham error->PutCString(m_constructor_errors.GetString()); 265*2a42a5a2SJim Ingham } 266*2a42a5a2SJim Ingham } 26730fdc8d8SChris Lattner return false; 268e103ae92SJonas Devlieghere } 269e103ae92SJonas Devlieghere 27030fdc8d8SChris Lattner return true; 27130fdc8d8SChris Lattner } 27230fdc8d8SChris Lattner 273b9c1b51eSKate Stone bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) { 27405097246SAdrian Prantl // If the step out plan is done, then we just need to step through the 27505097246SAdrian Prantl // inlined frame. 276b9c1b51eSKate Stone if (m_step_out_to_inline_plan_sp) { 277e65b2cf2SEugene Zelenko return m_step_out_to_inline_plan_sp->MischiefManaged(); 278b9c1b51eSKate Stone } else if (m_step_through_inline_plan_sp) { 279b9c1b51eSKate Stone if (m_step_through_inline_plan_sp->MischiefManaged()) { 28073ca05a2SJim Ingham CalculateReturnValue(); 281a5ce6c88SJim Ingham SetPlanComplete(); 282a5ce6c88SJim Ingham return true; 283b9c1b51eSKate Stone } else 284a5ce6c88SJim Ingham return false; 285b9c1b51eSKate Stone } else if (m_step_out_further_plan_sp) { 286e65b2cf2SEugene Zelenko return m_step_out_further_plan_sp->MischiefManaged(); 287a5ce6c88SJim Ingham } 288a5ce6c88SJim Ingham 289b9c1b51eSKate Stone // We don't explain signals or breakpoints (breakpoints that handle stepping 29005097246SAdrian Prantl // in or out will be handled by a child plan. 291a5ce6c88SJim Ingham 29260c4118cSJim Ingham StopInfoSP stop_info_sp = GetPrivateStopInfo(); 293b9c1b51eSKate Stone if (stop_info_sp) { 294b15bfc75SJim Ingham StopReason reason = stop_info_sp->GetStopReason(); 295b9c1b51eSKate Stone if (reason == eStopReasonBreakpoint) { 29605097246SAdrian Prantl // If this is OUR breakpoint, we're fine, otherwise we don't know why 29705097246SAdrian Prantl // this happened... 298b9c1b51eSKate Stone BreakpointSiteSP site_sp( 299b9c1b51eSKate Stone m_thread.GetProcess()->GetBreakpointSiteList().FindByID( 300b9c1b51eSKate Stone stop_info_sp->GetValue())); 301b9c1b51eSKate Stone if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) { 302b5c0d1ccSJim Ingham bool done; 303b5c0d1ccSJim Ingham 304b5c0d1ccSJim Ingham StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); 305b5c0d1ccSJim Ingham 306b5c0d1ccSJim Ingham if (m_step_out_to_id == frame_zero_id) 307b5c0d1ccSJim Ingham done = true; 308b9c1b51eSKate Stone else if (m_step_out_to_id < frame_zero_id) { 309b5c0d1ccSJim Ingham // Either we stepped past the breakpoint, or the stack ID calculation 310b5c0d1ccSJim Ingham // was incorrect and we should probably stop. 311b5c0d1ccSJim Ingham done = true; 312b9c1b51eSKate Stone } else { 313e65b2cf2SEugene Zelenko done = (m_immediate_step_from_id < frame_zero_id); 314b5c0d1ccSJim Ingham } 315b5c0d1ccSJim Ingham 316b9c1b51eSKate Stone if (done) { 317e103ae92SJonas Devlieghere if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) { 31873ca05a2SJim Ingham CalculateReturnValue(); 319481cef25SGreg Clayton SetPlanComplete(); 32073ca05a2SJim Ingham } 3214b4b2478SJim Ingham } 322481cef25SGreg Clayton 323b9c1b51eSKate Stone // If there was only one owner, then we're done. But if we also hit 32405097246SAdrian Prantl // some user breakpoint on our way out, we should mark ourselves as 32505097246SAdrian Prantl // done, but also not claim to explain the stop, since it is more 32605097246SAdrian Prantl // important to report the user breakpoint than the step out 32705097246SAdrian Prantl // completion. 32830fdc8d8SChris Lattner 329f4b47e15SGreg Clayton if (site_sp->GetNumberOfOwners() == 1) 33030fdc8d8SChris Lattner return true; 33130fdc8d8SChris Lattner } 33230fdc8d8SChris Lattner return false; 333b9c1b51eSKate Stone } else if (IsUsuallyUnexplainedStopReason(reason)) 33430fdc8d8SChris Lattner return false; 3359b03fa0cSJim Ingham else 33630fdc8d8SChris Lattner return true; 33730fdc8d8SChris Lattner } 33830fdc8d8SChris Lattner return true; 33930fdc8d8SChris Lattner } 34030fdc8d8SChris Lattner 341b9c1b51eSKate Stone bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) { 342a5ce6c88SJim Ingham if (IsPlanComplete()) 343a5ce6c88SJim Ingham return true; 344b5c0d1ccSJim Ingham 3454b4b2478SJim Ingham bool done = false; 346b9c1b51eSKate Stone if (m_step_out_to_inline_plan_sp) { 347b9c1b51eSKate Stone if (m_step_out_to_inline_plan_sp->MischiefManaged()) { 3484b4b2478SJim Ingham // Now step through the inlined stack we are in: 349b9c1b51eSKate Stone if (QueueInlinedStepPlan(true)) { 3504b4b2478SJim Ingham // If we can't queue a plan to do this, then just call ourselves done. 3514b4b2478SJim Ingham m_step_out_to_inline_plan_sp.reset(); 3524b4b2478SJim Ingham SetPlanComplete(false); 3534b4b2478SJim Ingham return true; 354b9c1b51eSKate Stone } else 3554b4b2478SJim Ingham done = true; 356b9c1b51eSKate Stone } else 3574b4b2478SJim Ingham return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr); 358b9c1b51eSKate Stone } else if (m_step_through_inline_plan_sp) { 3594b4b2478SJim Ingham if (m_step_through_inline_plan_sp->MischiefManaged()) 3604b4b2478SJim Ingham done = true; 3614b4b2478SJim Ingham else 3624b4b2478SJim Ingham return m_step_through_inline_plan_sp->ShouldStop(event_ptr); 363b9c1b51eSKate Stone } else if (m_step_out_further_plan_sp) { 3644b4b2478SJim Ingham if (m_step_out_further_plan_sp->MischiefManaged()) 3654b4b2478SJim Ingham m_step_out_further_plan_sp.reset(); 3664b4b2478SJim Ingham else 3674b4b2478SJim Ingham return m_step_out_further_plan_sp->ShouldStop(event_ptr); 3684b4b2478SJim Ingham } 369b5c0d1ccSJim Ingham 370b9c1b51eSKate Stone if (!done) { 371b5c0d1ccSJim Ingham StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); 372e65b2cf2SEugene Zelenko done = !(frame_zero_id < m_step_out_to_id); 3734b4b2478SJim Ingham } 3744b4b2478SJim Ingham 37505097246SAdrian Prantl // The normal step out computations think we are done, so all we need to do 37605097246SAdrian Prantl // is consult the ShouldStopHere, and we are done. 377b5c0d1ccSJim Ingham 378b9c1b51eSKate Stone if (done) { 379e103ae92SJonas Devlieghere if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) { 38073ca05a2SJim Ingham CalculateReturnValue(); 381a5ce6c88SJim Ingham SetPlanComplete(); 382b9c1b51eSKate Stone } else { 383b9c1b51eSKate Stone m_step_out_further_plan_sp = 384e103ae92SJonas Devlieghere QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder, m_status); 3854b4b2478SJim Ingham done = false; 386a5ce6c88SJim Ingham } 387a5ce6c88SJim Ingham } 3884b4b2478SJim Ingham 3894b4b2478SJim Ingham return done; 390a5ce6c88SJim Ingham } 39130fdc8d8SChris Lattner 392b9c1b51eSKate Stone bool ThreadPlanStepOut::StopOthers() { return m_stop_others; } 39330fdc8d8SChris Lattner 394b9c1b51eSKate Stone StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; } 39530fdc8d8SChris Lattner 396b9c1b51eSKate Stone bool ThreadPlanStepOut::DoWillResume(StateType resume_state, 397b9c1b51eSKate Stone bool current_plan) { 3984b4b2478SJim Ingham if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp) 399a5ce6c88SJim Ingham return true; 400a5ce6c88SJim Ingham 40130fdc8d8SChris Lattner if (m_return_bp_id == LLDB_INVALID_BREAK_ID) 40230fdc8d8SChris Lattner return false; 40330fdc8d8SChris Lattner 404b9c1b51eSKate Stone if (current_plan) { 405b9c1b51eSKate Stone Breakpoint *return_bp = 406b9c1b51eSKate Stone m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get(); 407e65b2cf2SEugene Zelenko if (return_bp != nullptr) 40830fdc8d8SChris Lattner return_bp->SetEnabled(true); 40930fdc8d8SChris Lattner } 41030fdc8d8SChris Lattner return true; 41130fdc8d8SChris Lattner } 41230fdc8d8SChris Lattner 413b9c1b51eSKate Stone bool ThreadPlanStepOut::WillStop() { 414b9c1b51eSKate Stone if (m_return_bp_id != LLDB_INVALID_BREAK_ID) { 415b9c1b51eSKate Stone Breakpoint *return_bp = 416b9c1b51eSKate Stone m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get(); 417e65b2cf2SEugene Zelenko if (return_bp != nullptr) 41830fdc8d8SChris Lattner return_bp->SetEnabled(false); 419a5ce6c88SJim Ingham } 420a5ce6c88SJim Ingham 42130fdc8d8SChris Lattner return true; 42230fdc8d8SChris Lattner } 42330fdc8d8SChris Lattner 424b9c1b51eSKate Stone bool ThreadPlanStepOut::MischiefManaged() { 425b9c1b51eSKate Stone if (IsPlanComplete()) { 42630fdc8d8SChris Lattner // Did I reach my breakpoint? If so I'm done. 42730fdc8d8SChris Lattner // 428b9c1b51eSKate Stone // I also check the stack depth, since if we've blown past the breakpoint 429b9c1b51eSKate Stone // for some 430b9c1b51eSKate Stone // reason and we're now stopping for some other reason altogether, then 43105097246SAdrian Prantl // we're done with this step out operation. 43230fdc8d8SChris Lattner 4335160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 43430fdc8d8SChris Lattner if (log) 43563e5fb76SJonas Devlieghere LLDB_LOGF(log, "Completed step out plan."); 436b9c1b51eSKate Stone if (m_return_bp_id != LLDB_INVALID_BREAK_ID) { 4371ac04c30SGreg Clayton m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id); 43830fdc8d8SChris Lattner m_return_bp_id = LLDB_INVALID_BREAK_ID; 439a5ce6c88SJim Ingham } 440a5ce6c88SJim Ingham 44130fdc8d8SChris Lattner ThreadPlan::MischiefManaged(); 44230fdc8d8SChris Lattner return true; 443b9c1b51eSKate Stone } else { 44430fdc8d8SChris Lattner return false; 44530fdc8d8SChris Lattner } 44630fdc8d8SChris Lattner } 44730fdc8d8SChris Lattner 448b9c1b51eSKate Stone bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) { 449b9c1b51eSKate Stone // Now figure out the range of this inlined block, and set up a "step through 45005097246SAdrian Prantl // range" plan for that. If we've been provided with a context, then use the 45105097246SAdrian Prantl // block in that context. 452b57e4a1bSJason Molenda StackFrameSP immediate_return_from_sp(m_thread.GetStackFrameAtIndex(0)); 453a5ce6c88SJim Ingham if (!immediate_return_from_sp) 454a5ce6c88SJim Ingham return false; 455a5ce6c88SJim Ingham 4565160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 457b9c1b51eSKate Stone if (log) { 458a5ce6c88SJim Ingham StreamString s; 459a5ce6c88SJim Ingham immediate_return_from_sp->Dump(&s, true, false); 46063e5fb76SJonas Devlieghere LLDB_LOGF(log, "Queuing inlined frame to step past: %s.", s.GetData()); 461a5ce6c88SJim Ingham } 462a5ce6c88SJim Ingham 463a5ce6c88SJim Ingham Block *from_block = immediate_return_from_sp->GetFrameBlock(); 464b9c1b51eSKate Stone if (from_block) { 465a5ce6c88SJim Ingham Block *inlined_block = from_block->GetContainingInlinedBlock(); 466b9c1b51eSKate Stone if (inlined_block) { 467a5ce6c88SJim Ingham size_t num_ranges = inlined_block->GetNumRanges(); 468a5ce6c88SJim Ingham AddressRange inline_range; 469b9c1b51eSKate Stone if (inlined_block->GetRangeAtIndex(0, inline_range)) { 470a5ce6c88SJim Ingham SymbolContext inlined_sc; 471a5ce6c88SJim Ingham inlined_block->CalculateSymbolContext(&inlined_sc); 4725f1a4e1fSJim Ingham inlined_sc.target_sp = GetTarget().shared_from_this(); 473b9c1b51eSKate Stone RunMode run_mode = 474b9c1b51eSKate Stone m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads; 4754b4b2478SJim Ingham const LazyBool avoid_no_debug = eLazyBoolNo; 4762bdbfd50SJim Ingham 477796ac80bSJonas Devlieghere m_step_through_inline_plan_sp = 478796ac80bSJonas Devlieghere std::make_shared<ThreadPlanStepOverRange>( 479796ac80bSJonas Devlieghere m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug); 480b9c1b51eSKate Stone ThreadPlanStepOverRange *step_through_inline_plan_ptr = 481b9c1b51eSKate Stone static_cast<ThreadPlanStepOverRange *>( 482b9c1b51eSKate Stone m_step_through_inline_plan_sp.get()); 4832bdbfd50SJim Ingham m_step_through_inline_plan_sp->SetPrivate(true); 4842bdbfd50SJim Ingham 485a5ce6c88SJim Ingham step_through_inline_plan_ptr->SetOkayToDiscard(true); 486a5ce6c88SJim Ingham StreamString errors; 487b9c1b51eSKate Stone if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) { 488a5ce6c88SJim Ingham // FIXME: Log this failure. 489a5ce6c88SJim Ingham delete step_through_inline_plan_ptr; 490a5ce6c88SJim Ingham return false; 491a5ce6c88SJim Ingham } 492a5ce6c88SJim Ingham 493b9c1b51eSKate Stone for (size_t i = 1; i < num_ranges; i++) { 494a5ce6c88SJim Ingham if (inlined_block->GetRangeAtIndex(i, inline_range)) 495a5ce6c88SJim Ingham step_through_inline_plan_ptr->AddRange(inline_range); 496a5ce6c88SJim Ingham } 4972bdbfd50SJim Ingham 498a5ce6c88SJim Ingham if (queue_now) 499a5ce6c88SJim Ingham m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false); 500a5ce6c88SJim Ingham return true; 501a5ce6c88SJim Ingham } 502a5ce6c88SJim Ingham } 503a5ce6c88SJim Ingham } 504a5ce6c88SJim Ingham 505a5ce6c88SJim Ingham return false; 506a5ce6c88SJim Ingham } 50773ca05a2SJim Ingham 508b9c1b51eSKate Stone void ThreadPlanStepOut::CalculateReturnValue() { 50973ca05a2SJim Ingham if (m_return_valobj_sp) 51073ca05a2SJim Ingham return; 51173ca05a2SJim Ingham 512b612ac37SJim Ingham if (!m_calculate_return_value) 513b612ac37SJim Ingham return; 514b612ac37SJim Ingham 515b9c1b51eSKate Stone if (m_immediate_step_from_function != nullptr) { 516b9c1b51eSKate Stone CompilerType return_compiler_type = 517b9c1b51eSKate Stone m_immediate_step_from_function->GetCompilerType() 518b9c1b51eSKate Stone .GetFunctionReturnType(); 519b9c1b51eSKate Stone if (return_compiler_type) { 5201ac04c30SGreg Clayton lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI(); 52173ca05a2SJim Ingham if (abi_sp) 522b9c1b51eSKate Stone m_return_valobj_sp = 523b9c1b51eSKate Stone abi_sp->GetReturnValueObject(m_thread, return_compiler_type); 52473ca05a2SJim Ingham } 52573ca05a2SJim Ingham } 52673ca05a2SJim Ingham } 52764e7ead1SJim Ingham 528b9c1b51eSKate Stone bool ThreadPlanStepOut::IsPlanStale() { 52905097246SAdrian Prantl // If we are still lower on the stack than the frame we are returning to, 53005097246SAdrian Prantl // then there's something for us to do. Otherwise, we're stale. 53164e7ead1SJim Ingham 53264e7ead1SJim Ingham StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); 533e65b2cf2SEugene Zelenko return !(frame_zero_id < m_step_out_to_id); 53464e7ead1SJim Ingham } 535