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 
129b9c1b51eSKate Stone     Breakpoint *return_bp = m_thread.CalculateTarget()
130b9c1b51eSKate Stone                                 ->CreateBreakpoint(m_return_addr, true, false)
131b9c1b51eSKate Stone                                 .get();
132e103ae92SJonas Devlieghere 
133b9c1b51eSKate Stone     if (return_bp != nullptr) {
134e103ae92SJonas Devlieghere       if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
135e103ae92SJonas Devlieghere         m_could_not_resolve_hw_bp = true;
13630fdc8d8SChris Lattner       return_bp->SetThreadID(m_thread.GetID());
13730fdc8d8SChris Lattner       m_return_bp_id = return_bp->GetID();
1382995077dSJim Ingham       return_bp->SetBreakpointKind("step-out");
13930fdc8d8SChris Lattner     }
14073ca05a2SJim Ingham 
141b9c1b51eSKate Stone     if (immediate_return_from_sp) {
142b9c1b51eSKate Stone       const SymbolContext &sc =
143b9c1b51eSKate Stone           immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
144b9c1b51eSKate Stone       if (sc.function) {
14573ca05a2SJim Ingham         m_immediate_step_from_function = sc.function;
14673ca05a2SJim Ingham       }
14773ca05a2SJim Ingham     }
14830fdc8d8SChris Lattner   }
149a5ce6c88SJim Ingham }
150a5ce6c88SJim Ingham 
151b9c1b51eSKate Stone void ThreadPlanStepOut::SetupAvoidNoDebug(
152b9c1b51eSKate Stone     LazyBool step_out_avoids_code_without_debug_info) {
1534b4b2478SJim Ingham   bool avoid_nodebug = true;
154b9c1b51eSKate Stone   switch (step_out_avoids_code_without_debug_info) {
1554b4b2478SJim Ingham   case eLazyBoolYes:
1564b4b2478SJim Ingham     avoid_nodebug = true;
1574b4b2478SJim Ingham     break;
1584b4b2478SJim Ingham   case eLazyBoolNo:
1594b4b2478SJim Ingham     avoid_nodebug = false;
1604b4b2478SJim Ingham     break;
1614b4b2478SJim Ingham   case eLazyBoolCalculate:
1624b4b2478SJim Ingham     avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
1634b4b2478SJim Ingham     break;
1644b4b2478SJim Ingham   }
1654b4b2478SJim Ingham   if (avoid_nodebug)
1664b4b2478SJim Ingham     GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1674b4b2478SJim Ingham   else
1684b4b2478SJim Ingham     GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1694b4b2478SJim Ingham }
1704b4b2478SJim Ingham 
171b9c1b51eSKate Stone void ThreadPlanStepOut::DidPush() {
1724b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
1734b4b2478SJim Ingham     m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
174a5ce6c88SJim Ingham   else if (m_step_through_inline_plan_sp)
175a5ce6c88SJim Ingham     m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
17630fdc8d8SChris Lattner }
17730fdc8d8SChris Lattner 
178b9c1b51eSKate Stone ThreadPlanStepOut::~ThreadPlanStepOut() {
17930fdc8d8SChris Lattner   if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
1801ac04c30SGreg Clayton     m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
18130fdc8d8SChris Lattner }
18230fdc8d8SChris Lattner 
183b9c1b51eSKate Stone void ThreadPlanStepOut::GetDescription(Stream *s,
184b9c1b51eSKate Stone                                        lldb::DescriptionLevel level) {
18530fdc8d8SChris Lattner   if (level == lldb::eDescriptionLevelBrief)
18630fdc8d8SChris Lattner     s->Printf("step out");
187b9c1b51eSKate Stone   else {
1884b4b2478SJim Ingham     if (m_step_out_to_inline_plan_sp)
189b5c0d1ccSJim Ingham       s->Printf("Stepping out to inlined frame so we can walk through it.");
190a5ce6c88SJim Ingham     else if (m_step_through_inline_plan_sp)
191a5ce6c88SJim Ingham       s->Printf("Stepping out by stepping through inlined function.");
192b9c1b51eSKate Stone     else {
1932bdbfd50SJim Ingham       s->Printf("Stepping out from ");
1942bdbfd50SJim Ingham       Address tmp_address;
195b9c1b51eSKate Stone       if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) {
196b9c1b51eSKate Stone         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
197b9c1b51eSKate Stone                          Address::DumpStyleLoadAddress);
198b9c1b51eSKate Stone       } else {
1992bdbfd50SJim Ingham         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn);
2002bdbfd50SJim Ingham       }
2012bdbfd50SJim Ingham 
202b9c1b51eSKate Stone       // FIXME: find some useful way to present the m_return_id, since there may
203b9c1b51eSKate Stone       // be multiple copies of the
2042bdbfd50SJim Ingham       // same function on the stack.
2052bdbfd50SJim Ingham 
2062bdbfd50SJim Ingham       s->Printf(" returning to frame at ");
207b9c1b51eSKate Stone       if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) {
208b9c1b51eSKate Stone         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
209b9c1b51eSKate Stone                          Address::DumpStyleLoadAddress);
210b9c1b51eSKate Stone       } else {
2112bdbfd50SJim Ingham         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
2122bdbfd50SJim Ingham       }
2132bdbfd50SJim Ingham 
2142bdbfd50SJim Ingham       if (level == eDescriptionLevelVerbose)
2152bdbfd50SJim Ingham         s->Printf(" using breakpoint site %d", m_return_bp_id);
2162bdbfd50SJim Ingham     }
21730fdc8d8SChris Lattner   }
2184b36f791SVedant Kumar 
2194b36f791SVedant Kumar   s->Printf("\n");
2204b36f791SVedant Kumar   for (StackFrameSP frame_sp : m_stepped_past_frames) {
2214b36f791SVedant Kumar     s->Printf("Stepped out past: ");
2224b36f791SVedant Kumar     frame_sp->DumpUsingSettingsFormat(s);
2234b36f791SVedant Kumar   }
22430fdc8d8SChris Lattner }
22530fdc8d8SChris Lattner 
226b9c1b51eSKate Stone bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
2274b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
2284b4b2478SJim Ingham     return m_step_out_to_inline_plan_sp->ValidatePlan(error);
229e103ae92SJonas Devlieghere 
230e103ae92SJonas Devlieghere   if (m_step_through_inline_plan_sp)
231a5ce6c88SJim Ingham     return m_step_through_inline_plan_sp->ValidatePlan(error);
232e103ae92SJonas Devlieghere 
233e103ae92SJonas Devlieghere   if (m_could_not_resolve_hw_bp) {
234e103ae92SJonas Devlieghere     if (error)
235e103ae92SJonas Devlieghere       error->PutCString(
236e103ae92SJonas Devlieghere           "Could not create hardware breakpoint for thread plan.");
237e103ae92SJonas Devlieghere     return false;
238e103ae92SJonas Devlieghere   }
239e103ae92SJonas Devlieghere 
240e103ae92SJonas Devlieghere   if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
241708709c0SSean Callanan     if (error)
242a5ce6c88SJim Ingham       error->PutCString("Could not create return address breakpoint.");
24330fdc8d8SChris Lattner     return false;
244e103ae92SJonas Devlieghere   }
245e103ae92SJonas Devlieghere 
24630fdc8d8SChris Lattner   return true;
24730fdc8d8SChris Lattner }
24830fdc8d8SChris Lattner 
249b9c1b51eSKate Stone bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
25005097246SAdrian Prantl   // If the step out plan is done, then we just need to step through the
25105097246SAdrian Prantl   // inlined frame.
252b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
253e65b2cf2SEugene Zelenko     return m_step_out_to_inline_plan_sp->MischiefManaged();
254b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
255b9c1b51eSKate Stone     if (m_step_through_inline_plan_sp->MischiefManaged()) {
25673ca05a2SJim Ingham       CalculateReturnValue();
257a5ce6c88SJim Ingham       SetPlanComplete();
258a5ce6c88SJim Ingham       return true;
259b9c1b51eSKate Stone     } else
260a5ce6c88SJim Ingham       return false;
261b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
262e65b2cf2SEugene Zelenko     return m_step_out_further_plan_sp->MischiefManaged();
263a5ce6c88SJim Ingham   }
264a5ce6c88SJim Ingham 
265b9c1b51eSKate Stone   // We don't explain signals or breakpoints (breakpoints that handle stepping
26605097246SAdrian Prantl   // in or out will be handled by a child plan.
267a5ce6c88SJim Ingham 
26860c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
269b9c1b51eSKate Stone   if (stop_info_sp) {
270b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
271b9c1b51eSKate Stone     if (reason == eStopReasonBreakpoint) {
27205097246SAdrian Prantl       // If this is OUR breakpoint, we're fine, otherwise we don't know why
27305097246SAdrian Prantl       // this happened...
274b9c1b51eSKate Stone       BreakpointSiteSP site_sp(
275b9c1b51eSKate Stone           m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
276b9c1b51eSKate Stone               stop_info_sp->GetValue()));
277b9c1b51eSKate Stone       if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
278b5c0d1ccSJim Ingham         bool done;
279b5c0d1ccSJim Ingham 
280b5c0d1ccSJim Ingham         StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
281b5c0d1ccSJim Ingham 
282b5c0d1ccSJim Ingham         if (m_step_out_to_id == frame_zero_id)
283b5c0d1ccSJim Ingham           done = true;
284b9c1b51eSKate Stone         else if (m_step_out_to_id < frame_zero_id) {
285b5c0d1ccSJim Ingham           // Either we stepped past the breakpoint, or the stack ID calculation
286b5c0d1ccSJim Ingham           // was incorrect and we should probably stop.
287b5c0d1ccSJim Ingham           done = true;
288b9c1b51eSKate Stone         } else {
289e65b2cf2SEugene Zelenko           done = (m_immediate_step_from_id < frame_zero_id);
290b5c0d1ccSJim Ingham         }
291b5c0d1ccSJim Ingham 
292b9c1b51eSKate Stone         if (done) {
293e103ae92SJonas Devlieghere           if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
29473ca05a2SJim Ingham             CalculateReturnValue();
295481cef25SGreg Clayton             SetPlanComplete();
29673ca05a2SJim Ingham           }
2974b4b2478SJim Ingham         }
298481cef25SGreg Clayton 
299b9c1b51eSKate Stone         // If there was only one owner, then we're done.  But if we also hit
30005097246SAdrian Prantl         // some user breakpoint on our way out, we should mark ourselves as
30105097246SAdrian Prantl         // done, but also not claim to explain the stop, since it is more
30205097246SAdrian Prantl         // important to report the user breakpoint than the step out
30305097246SAdrian Prantl         // completion.
30430fdc8d8SChris Lattner 
305f4b47e15SGreg Clayton         if (site_sp->GetNumberOfOwners() == 1)
30630fdc8d8SChris Lattner           return true;
30730fdc8d8SChris Lattner       }
30830fdc8d8SChris Lattner       return false;
309b9c1b51eSKate Stone     } else if (IsUsuallyUnexplainedStopReason(reason))
31030fdc8d8SChris Lattner       return false;
3119b03fa0cSJim Ingham     else
31230fdc8d8SChris Lattner       return true;
31330fdc8d8SChris Lattner   }
31430fdc8d8SChris Lattner   return true;
31530fdc8d8SChris Lattner }
31630fdc8d8SChris Lattner 
317b9c1b51eSKate Stone bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
318a5ce6c88SJim Ingham   if (IsPlanComplete())
319a5ce6c88SJim Ingham     return true;
320b5c0d1ccSJim Ingham 
3214b4b2478SJim Ingham   bool done = false;
322b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
323b9c1b51eSKate Stone     if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
3244b4b2478SJim Ingham       // Now step through the inlined stack we are in:
325b9c1b51eSKate Stone       if (QueueInlinedStepPlan(true)) {
3264b4b2478SJim Ingham         // If we can't queue a plan to do this, then just call ourselves done.
3274b4b2478SJim Ingham         m_step_out_to_inline_plan_sp.reset();
3284b4b2478SJim Ingham         SetPlanComplete(false);
3294b4b2478SJim Ingham         return true;
330b9c1b51eSKate Stone       } else
3314b4b2478SJim Ingham         done = true;
332b9c1b51eSKate Stone     } else
3334b4b2478SJim Ingham       return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
334b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
3354b4b2478SJim Ingham     if (m_step_through_inline_plan_sp->MischiefManaged())
3364b4b2478SJim Ingham       done = true;
3374b4b2478SJim Ingham     else
3384b4b2478SJim Ingham       return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
339b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
3404b4b2478SJim Ingham     if (m_step_out_further_plan_sp->MischiefManaged())
3414b4b2478SJim Ingham       m_step_out_further_plan_sp.reset();
3424b4b2478SJim Ingham     else
3434b4b2478SJim Ingham       return m_step_out_further_plan_sp->ShouldStop(event_ptr);
3444b4b2478SJim Ingham   }
345b5c0d1ccSJim Ingham 
346b9c1b51eSKate Stone   if (!done) {
347b5c0d1ccSJim Ingham     StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
348e65b2cf2SEugene Zelenko     done = !(frame_zero_id < m_step_out_to_id);
3494b4b2478SJim Ingham   }
3504b4b2478SJim Ingham 
35105097246SAdrian Prantl   // The normal step out computations think we are done, so all we need to do
35205097246SAdrian Prantl   // is consult the ShouldStopHere, and we are done.
353b5c0d1ccSJim Ingham 
354b9c1b51eSKate Stone   if (done) {
355e103ae92SJonas Devlieghere     if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
35673ca05a2SJim Ingham       CalculateReturnValue();
357a5ce6c88SJim Ingham       SetPlanComplete();
358b9c1b51eSKate Stone     } else {
359b9c1b51eSKate Stone       m_step_out_further_plan_sp =
360e103ae92SJonas Devlieghere           QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder, m_status);
3614b4b2478SJim Ingham       done = false;
362a5ce6c88SJim Ingham     }
363a5ce6c88SJim Ingham   }
3644b4b2478SJim Ingham 
3654b4b2478SJim Ingham   return done;
366a5ce6c88SJim Ingham }
36730fdc8d8SChris Lattner 
368b9c1b51eSKate Stone bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
36930fdc8d8SChris Lattner 
370b9c1b51eSKate Stone StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
37130fdc8d8SChris Lattner 
372b9c1b51eSKate Stone bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
373b9c1b51eSKate Stone                                      bool current_plan) {
3744b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
375a5ce6c88SJim Ingham     return true;
376a5ce6c88SJim Ingham 
37730fdc8d8SChris Lattner   if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
37830fdc8d8SChris Lattner     return false;
37930fdc8d8SChris Lattner 
380b9c1b51eSKate Stone   if (current_plan) {
381b9c1b51eSKate Stone     Breakpoint *return_bp =
382b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
383e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
38430fdc8d8SChris Lattner       return_bp->SetEnabled(true);
38530fdc8d8SChris Lattner   }
38630fdc8d8SChris Lattner   return true;
38730fdc8d8SChris Lattner }
38830fdc8d8SChris Lattner 
389b9c1b51eSKate Stone bool ThreadPlanStepOut::WillStop() {
390b9c1b51eSKate Stone   if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
391b9c1b51eSKate Stone     Breakpoint *return_bp =
392b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
393e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
39430fdc8d8SChris Lattner       return_bp->SetEnabled(false);
395a5ce6c88SJim Ingham   }
396a5ce6c88SJim Ingham 
39730fdc8d8SChris Lattner   return true;
39830fdc8d8SChris Lattner }
39930fdc8d8SChris Lattner 
400b9c1b51eSKate Stone bool ThreadPlanStepOut::MischiefManaged() {
401b9c1b51eSKate Stone   if (IsPlanComplete()) {
40230fdc8d8SChris Lattner     // Did I reach my breakpoint?  If so I'm done.
40330fdc8d8SChris Lattner     //
404b9c1b51eSKate Stone     // I also check the stack depth, since if we've blown past the breakpoint
405b9c1b51eSKate Stone     // for some
406b9c1b51eSKate Stone     // reason and we're now stopping for some other reason altogether, then
40705097246SAdrian Prantl     // we're done with this step out operation.
40830fdc8d8SChris Lattner 
4095160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
41030fdc8d8SChris Lattner     if (log)
411*63e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Completed step out plan.");
412b9c1b51eSKate Stone     if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
4131ac04c30SGreg Clayton       m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
41430fdc8d8SChris Lattner       m_return_bp_id = LLDB_INVALID_BREAK_ID;
415a5ce6c88SJim Ingham     }
416a5ce6c88SJim Ingham 
41730fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
41830fdc8d8SChris Lattner     return true;
419b9c1b51eSKate Stone   } else {
42030fdc8d8SChris Lattner     return false;
42130fdc8d8SChris Lattner   }
42230fdc8d8SChris Lattner }
42330fdc8d8SChris Lattner 
424b9c1b51eSKate Stone bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
425b9c1b51eSKate Stone   // Now figure out the range of this inlined block, and set up a "step through
42605097246SAdrian Prantl   // range" plan for that.  If we've been provided with a context, then use the
42705097246SAdrian Prantl   // block in that context.
428b57e4a1bSJason Molenda   StackFrameSP immediate_return_from_sp(m_thread.GetStackFrameAtIndex(0));
429a5ce6c88SJim Ingham   if (!immediate_return_from_sp)
430a5ce6c88SJim Ingham     return false;
431a5ce6c88SJim Ingham 
4325160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
433b9c1b51eSKate Stone   if (log) {
434a5ce6c88SJim Ingham     StreamString s;
435a5ce6c88SJim Ingham     immediate_return_from_sp->Dump(&s, true, false);
436*63e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Queuing inlined frame to step past: %s.", s.GetData());
437a5ce6c88SJim Ingham   }
438a5ce6c88SJim Ingham 
439a5ce6c88SJim Ingham   Block *from_block = immediate_return_from_sp->GetFrameBlock();
440b9c1b51eSKate Stone   if (from_block) {
441a5ce6c88SJim Ingham     Block *inlined_block = from_block->GetContainingInlinedBlock();
442b9c1b51eSKate Stone     if (inlined_block) {
443a5ce6c88SJim Ingham       size_t num_ranges = inlined_block->GetNumRanges();
444a5ce6c88SJim Ingham       AddressRange inline_range;
445b9c1b51eSKate Stone       if (inlined_block->GetRangeAtIndex(0, inline_range)) {
446a5ce6c88SJim Ingham         SymbolContext inlined_sc;
447a5ce6c88SJim Ingham         inlined_block->CalculateSymbolContext(&inlined_sc);
4485f1a4e1fSJim Ingham         inlined_sc.target_sp = GetTarget().shared_from_this();
449b9c1b51eSKate Stone         RunMode run_mode =
450b9c1b51eSKate Stone             m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
4514b4b2478SJim Ingham         const LazyBool avoid_no_debug = eLazyBoolNo;
4522bdbfd50SJim Ingham 
453796ac80bSJonas Devlieghere         m_step_through_inline_plan_sp =
454796ac80bSJonas Devlieghere             std::make_shared<ThreadPlanStepOverRange>(
455796ac80bSJonas Devlieghere                 m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug);
456b9c1b51eSKate Stone         ThreadPlanStepOverRange *step_through_inline_plan_ptr =
457b9c1b51eSKate Stone             static_cast<ThreadPlanStepOverRange *>(
458b9c1b51eSKate Stone                 m_step_through_inline_plan_sp.get());
4592bdbfd50SJim Ingham         m_step_through_inline_plan_sp->SetPrivate(true);
4602bdbfd50SJim Ingham 
461a5ce6c88SJim Ingham         step_through_inline_plan_ptr->SetOkayToDiscard(true);
462a5ce6c88SJim Ingham         StreamString errors;
463b9c1b51eSKate Stone         if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
464a5ce6c88SJim Ingham           // FIXME: Log this failure.
465a5ce6c88SJim Ingham           delete step_through_inline_plan_ptr;
466a5ce6c88SJim Ingham           return false;
467a5ce6c88SJim Ingham         }
468a5ce6c88SJim Ingham 
469b9c1b51eSKate Stone         for (size_t i = 1; i < num_ranges; i++) {
470a5ce6c88SJim Ingham           if (inlined_block->GetRangeAtIndex(i, inline_range))
471a5ce6c88SJim Ingham             step_through_inline_plan_ptr->AddRange(inline_range);
472a5ce6c88SJim Ingham         }
4732bdbfd50SJim Ingham 
474a5ce6c88SJim Ingham         if (queue_now)
475a5ce6c88SJim Ingham           m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
476a5ce6c88SJim Ingham         return true;
477a5ce6c88SJim Ingham       }
478a5ce6c88SJim Ingham     }
479a5ce6c88SJim Ingham   }
480a5ce6c88SJim Ingham 
481a5ce6c88SJim Ingham   return false;
482a5ce6c88SJim Ingham }
48373ca05a2SJim Ingham 
484b9c1b51eSKate Stone void ThreadPlanStepOut::CalculateReturnValue() {
48573ca05a2SJim Ingham   if (m_return_valobj_sp)
48673ca05a2SJim Ingham     return;
48773ca05a2SJim Ingham 
488b612ac37SJim Ingham   if (!m_calculate_return_value)
489b612ac37SJim Ingham     return;
490b612ac37SJim Ingham 
491b9c1b51eSKate Stone   if (m_immediate_step_from_function != nullptr) {
492b9c1b51eSKate Stone     CompilerType return_compiler_type =
493b9c1b51eSKate Stone         m_immediate_step_from_function->GetCompilerType()
494b9c1b51eSKate Stone             .GetFunctionReturnType();
495b9c1b51eSKate Stone     if (return_compiler_type) {
4961ac04c30SGreg Clayton       lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
49773ca05a2SJim Ingham       if (abi_sp)
498b9c1b51eSKate Stone         m_return_valobj_sp =
499b9c1b51eSKate Stone             abi_sp->GetReturnValueObject(m_thread, return_compiler_type);
50073ca05a2SJim Ingham     }
50173ca05a2SJim Ingham   }
50273ca05a2SJim Ingham }
50364e7ead1SJim Ingham 
504b9c1b51eSKate Stone bool ThreadPlanStepOut::IsPlanStale() {
50505097246SAdrian Prantl   // If we are still lower on the stack than the frame we are returning to,
50605097246SAdrian Prantl   // then there's something for us to do.  Otherwise, we're stale.
50764e7ead1SJim Ingham 
50864e7ead1SJim Ingham   StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
509e65b2cf2SEugene Zelenko   return !(frame_zero_id < m_step_out_to_id);
51064e7ead1SJim Ingham }
511