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 
26*796ac80bSJonas Devlieghere #include <memory>
27*796ac80bSJonas 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 //----------------------------------------------------------------------
3430fdc8d8SChris Lattner // ThreadPlanStepOut: Step out of the current frame
3530fdc8d8SChris Lattner //----------------------------------------------------------------------
36b9c1b51eSKate Stone ThreadPlanStepOut::ThreadPlanStepOut(
37b9c1b51eSKate Stone     Thread &thread, SymbolContext *context, bool first_insn, bool stop_others,
38b9c1b51eSKate Stone     Vote stop_vote, Vote run_vote, uint32_t frame_idx,
39fd4cea53SJason Molenda     LazyBool step_out_avoids_code_without_debug_info,
40b9c1b51eSKate Stone     bool continue_to_next_branch, bool gather_return_value)
41b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindStepOut, "Step out", thread, stop_vote,
42b9c1b51eSKate Stone                  run_vote),
43b9c1b51eSKate Stone       ThreadPlanShouldStopHere(this), m_step_from_insn(LLDB_INVALID_ADDRESS),
441ee0d4f7SBenjamin Kramer       m_return_bp_id(LLDB_INVALID_BREAK_ID),
45b9c1b51eSKate Stone       m_return_addr(LLDB_INVALID_ADDRESS), m_stop_others(stop_others),
46b612ac37SJim Ingham       m_immediate_step_from_function(nullptr),
47b9c1b51eSKate Stone       m_calculate_return_value(gather_return_value) {
484b36f791SVedant Kumar   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
494b4b2478SJim Ingham   SetFlagsToDefault();
504b4b2478SJim Ingham   SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
514b4b2478SJim Ingham 
5230fdc8d8SChris Lattner   m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
5330fdc8d8SChris Lattner 
544b36f791SVedant Kumar   uint32_t return_frame_index = frame_idx + 1;
554b36f791SVedant Kumar   StackFrameSP return_frame_sp(
564b36f791SVedant Kumar       m_thread.GetStackFrameAtIndex(return_frame_index));
57b9c1b51eSKate Stone   StackFrameSP immediate_return_from_sp(
58b9c1b51eSKate Stone       m_thread.GetStackFrameAtIndex(frame_idx));
59a5ce6c88SJim Ingham 
60632d2f72SSean Callanan   if (!return_frame_sp || !immediate_return_from_sp)
61632d2f72SSean Callanan     return; // we can't do anything here.  ValidatePlan() will return false.
62632d2f72SSean Callanan 
634b36f791SVedant Kumar   // While stepping out, behave as-if artificial frames are not present.
644b36f791SVedant Kumar   while (return_frame_sp->IsArtificial()) {
654b36f791SVedant Kumar     m_stepped_past_frames.push_back(return_frame_sp);
664b36f791SVedant Kumar 
674b36f791SVedant Kumar     ++return_frame_index;
684b36f791SVedant Kumar     return_frame_sp = m_thread.GetStackFrameAtIndex(return_frame_index);
694b36f791SVedant Kumar 
704b36f791SVedant Kumar     // We never expect to see an artificial frame without a regular ancestor.
714b36f791SVedant Kumar     // If this happens, log the issue and defensively refuse to step out.
724b36f791SVedant Kumar     if (!return_frame_sp) {
734b36f791SVedant Kumar       LLDB_LOG(log, "Can't step out of frame with artificial ancestors");
744b36f791SVedant Kumar       return;
754b36f791SVedant Kumar     }
764b36f791SVedant Kumar   }
774b36f791SVedant Kumar 
78b5c0d1ccSJim Ingham   m_step_out_to_id = return_frame_sp->GetStackID();
79b5c0d1ccSJim Ingham   m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
80a5ce6c88SJim Ingham 
8105097246SAdrian Prantl   // If the frame directly below the one we are returning to is inlined, we
8205097246SAdrian Prantl   // have to be a little more careful.  It is non-trivial to determine the real
8305097246SAdrian Prantl   // "return code address" for an inlined frame, so we have to work our way to
8405097246SAdrian Prantl   // that frame and then step out.
854b36f791SVedant Kumar   if (immediate_return_from_sp->IsInlined()) {
86b9c1b51eSKate Stone     if (frame_idx > 0) {
87b9c1b51eSKate Stone       // First queue a plan that gets us to this inlined frame, and when we get
8805097246SAdrian Prantl       // there we'll queue a second plan that walks us out of this frame.
89*796ac80bSJonas Devlieghere       m_step_out_to_inline_plan_sp = std::make_shared<ThreadPlanStepOut>(
90b9c1b51eSKate Stone           m_thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
91*796ac80bSJonas Devlieghere           frame_idx - 1, eLazyBoolNo, continue_to_next_branch);
92b9c1b51eSKate Stone       static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())
93b9c1b51eSKate Stone           ->SetShouldStopHereCallbacks(nullptr, nullptr);
942bdbfd50SJim Ingham       m_step_out_to_inline_plan_sp->SetPrivate(true);
95b9c1b51eSKate Stone     } else {
9605097246SAdrian Prantl       // If we're already at the inlined frame we're stepping through, then
9705097246SAdrian Prantl       // just do that now.
98a5ce6c88SJim Ingham       QueueInlinedStepPlan(false);
99a5ce6c88SJim Ingham     }
1004b36f791SVedant Kumar   } else {
10130fdc8d8SChris Lattner     // Find the return address and set a breakpoint there:
10230fdc8d8SChris Lattner     // FIXME - can we do this more securely if we know first_insn?
10330fdc8d8SChris Lattner 
104fd4cea53SJason Molenda     Address return_address(return_frame_sp->GetFrameCodeAddress());
105b9c1b51eSKate Stone     if (continue_to_next_branch) {
106fd4cea53SJason Molenda       SymbolContext return_address_sc;
107fd4cea53SJason Molenda       AddressRange range;
108fd4cea53SJason Molenda       Address return_address_decr_pc = return_address;
109fd4cea53SJason Molenda       if (return_address_decr_pc.GetOffset() > 0)
110fd4cea53SJason Molenda         return_address_decr_pc.Slide(-1);
111fd4cea53SJason Molenda 
112b9c1b51eSKate Stone       return_address_decr_pc.CalculateSymbolContext(
113b9c1b51eSKate Stone           &return_address_sc, lldb::eSymbolContextLineEntry);
114b9c1b51eSKate Stone       if (return_address_sc.line_entry.IsValid()) {
115b9c1b51eSKate Stone         range =
116b9c1b51eSKate Stone             return_address_sc.line_entry.GetSameLineContiguousAddressRange();
117b9c1b51eSKate Stone         if (range.GetByteSize() > 0) {
118b9c1b51eSKate Stone           return_address =
119b9c1b51eSKate Stone               m_thread.GetProcess()->AdvanceAddressToNextBranchInstruction(
120b9c1b51eSKate Stone                   return_address, range);
121fd4cea53SJason Molenda         }
122fd4cea53SJason Molenda       }
123fd4cea53SJason Molenda     }
124b9c1b51eSKate Stone     m_return_addr =
125b9c1b51eSKate Stone         return_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget());
126708709c0SSean Callanan 
127708709c0SSean Callanan     if (m_return_addr == LLDB_INVALID_ADDRESS)
128708709c0SSean Callanan       return;
129708709c0SSean Callanan 
130b9c1b51eSKate Stone     Breakpoint *return_bp = m_thread.CalculateTarget()
131b9c1b51eSKate Stone                                 ->CreateBreakpoint(m_return_addr, true, false)
132b9c1b51eSKate Stone                                 .get();
133e103ae92SJonas Devlieghere 
134b9c1b51eSKate Stone     if (return_bp != nullptr) {
135e103ae92SJonas Devlieghere       if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
136e103ae92SJonas Devlieghere         m_could_not_resolve_hw_bp = true;
13730fdc8d8SChris Lattner       return_bp->SetThreadID(m_thread.GetID());
13830fdc8d8SChris Lattner       m_return_bp_id = return_bp->GetID();
1392995077dSJim Ingham       return_bp->SetBreakpointKind("step-out");
14030fdc8d8SChris Lattner     }
14173ca05a2SJim Ingham 
142b9c1b51eSKate Stone     if (immediate_return_from_sp) {
143b9c1b51eSKate Stone       const SymbolContext &sc =
144b9c1b51eSKate Stone           immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
145b9c1b51eSKate Stone       if (sc.function) {
14673ca05a2SJim Ingham         m_immediate_step_from_function = sc.function;
14773ca05a2SJim Ingham       }
14873ca05a2SJim Ingham     }
14930fdc8d8SChris Lattner   }
150a5ce6c88SJim Ingham }
151a5ce6c88SJim Ingham 
152b9c1b51eSKate Stone void ThreadPlanStepOut::SetupAvoidNoDebug(
153b9c1b51eSKate Stone     LazyBool step_out_avoids_code_without_debug_info) {
1544b4b2478SJim Ingham   bool avoid_nodebug = true;
155b9c1b51eSKate Stone   switch (step_out_avoids_code_without_debug_info) {
1564b4b2478SJim Ingham   case eLazyBoolYes:
1574b4b2478SJim Ingham     avoid_nodebug = true;
1584b4b2478SJim Ingham     break;
1594b4b2478SJim Ingham   case eLazyBoolNo:
1604b4b2478SJim Ingham     avoid_nodebug = false;
1614b4b2478SJim Ingham     break;
1624b4b2478SJim Ingham   case eLazyBoolCalculate:
1634b4b2478SJim Ingham     avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
1644b4b2478SJim Ingham     break;
1654b4b2478SJim Ingham   }
1664b4b2478SJim Ingham   if (avoid_nodebug)
1674b4b2478SJim Ingham     GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1684b4b2478SJim Ingham   else
1694b4b2478SJim Ingham     GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1704b4b2478SJim Ingham }
1714b4b2478SJim Ingham 
172b9c1b51eSKate Stone void ThreadPlanStepOut::DidPush() {
1734b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
1744b4b2478SJim Ingham     m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
175a5ce6c88SJim Ingham   else if (m_step_through_inline_plan_sp)
176a5ce6c88SJim Ingham     m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
17730fdc8d8SChris Lattner }
17830fdc8d8SChris Lattner 
179b9c1b51eSKate Stone ThreadPlanStepOut::~ThreadPlanStepOut() {
18030fdc8d8SChris Lattner   if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
1811ac04c30SGreg Clayton     m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
18230fdc8d8SChris Lattner }
18330fdc8d8SChris Lattner 
184b9c1b51eSKate Stone void ThreadPlanStepOut::GetDescription(Stream *s,
185b9c1b51eSKate Stone                                        lldb::DescriptionLevel level) {
18630fdc8d8SChris Lattner   if (level == lldb::eDescriptionLevelBrief)
18730fdc8d8SChris Lattner     s->Printf("step out");
188b9c1b51eSKate Stone   else {
1894b4b2478SJim Ingham     if (m_step_out_to_inline_plan_sp)
190b5c0d1ccSJim Ingham       s->Printf("Stepping out to inlined frame so we can walk through it.");
191a5ce6c88SJim Ingham     else if (m_step_through_inline_plan_sp)
192a5ce6c88SJim Ingham       s->Printf("Stepping out by stepping through inlined function.");
193b9c1b51eSKate Stone     else {
1942bdbfd50SJim Ingham       s->Printf("Stepping out from ");
1952bdbfd50SJim Ingham       Address tmp_address;
196b9c1b51eSKate Stone       if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) {
197b9c1b51eSKate Stone         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
198b9c1b51eSKate Stone                          Address::DumpStyleLoadAddress);
199b9c1b51eSKate Stone       } else {
2002bdbfd50SJim Ingham         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn);
2012bdbfd50SJim Ingham       }
2022bdbfd50SJim Ingham 
203b9c1b51eSKate Stone       // FIXME: find some useful way to present the m_return_id, since there may
204b9c1b51eSKate Stone       // be multiple copies of the
2052bdbfd50SJim Ingham       // same function on the stack.
2062bdbfd50SJim Ingham 
2072bdbfd50SJim Ingham       s->Printf(" returning to frame at ");
208b9c1b51eSKate Stone       if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) {
209b9c1b51eSKate Stone         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
210b9c1b51eSKate Stone                          Address::DumpStyleLoadAddress);
211b9c1b51eSKate Stone       } else {
2122bdbfd50SJim Ingham         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
2132bdbfd50SJim Ingham       }
2142bdbfd50SJim Ingham 
2152bdbfd50SJim Ingham       if (level == eDescriptionLevelVerbose)
2162bdbfd50SJim Ingham         s->Printf(" using breakpoint site %d", m_return_bp_id);
2172bdbfd50SJim Ingham     }
21830fdc8d8SChris Lattner   }
2194b36f791SVedant Kumar 
2204b36f791SVedant Kumar   s->Printf("\n");
2214b36f791SVedant Kumar   for (StackFrameSP frame_sp : m_stepped_past_frames) {
2224b36f791SVedant Kumar     s->Printf("Stepped out past: ");
2234b36f791SVedant Kumar     frame_sp->DumpUsingSettingsFormat(s);
2244b36f791SVedant Kumar   }
22530fdc8d8SChris Lattner }
22630fdc8d8SChris Lattner 
227b9c1b51eSKate Stone bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
2284b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
2294b4b2478SJim Ingham     return m_step_out_to_inline_plan_sp->ValidatePlan(error);
230e103ae92SJonas Devlieghere 
231e103ae92SJonas Devlieghere   if (m_step_through_inline_plan_sp)
232a5ce6c88SJim Ingham     return m_step_through_inline_plan_sp->ValidatePlan(error);
233e103ae92SJonas Devlieghere 
234e103ae92SJonas Devlieghere   if (m_could_not_resolve_hw_bp) {
235e103ae92SJonas Devlieghere     if (error)
236e103ae92SJonas Devlieghere       error->PutCString(
237e103ae92SJonas Devlieghere           "Could not create hardware breakpoint for thread plan.");
238e103ae92SJonas Devlieghere     return false;
239e103ae92SJonas Devlieghere   }
240e103ae92SJonas Devlieghere 
241e103ae92SJonas Devlieghere   if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
242708709c0SSean Callanan     if (error)
243a5ce6c88SJim Ingham       error->PutCString("Could not create return address breakpoint.");
24430fdc8d8SChris Lattner     return false;
245e103ae92SJonas Devlieghere   }
246e103ae92SJonas Devlieghere 
24730fdc8d8SChris Lattner   return true;
24830fdc8d8SChris Lattner }
24930fdc8d8SChris Lattner 
250b9c1b51eSKate Stone bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
25105097246SAdrian Prantl   // If the step out plan is done, then we just need to step through the
25205097246SAdrian Prantl   // inlined frame.
253b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
254e65b2cf2SEugene Zelenko     return m_step_out_to_inline_plan_sp->MischiefManaged();
255b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
256b9c1b51eSKate Stone     if (m_step_through_inline_plan_sp->MischiefManaged()) {
25773ca05a2SJim Ingham       CalculateReturnValue();
258a5ce6c88SJim Ingham       SetPlanComplete();
259a5ce6c88SJim Ingham       return true;
260b9c1b51eSKate Stone     } else
261a5ce6c88SJim Ingham       return false;
262b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
263e65b2cf2SEugene Zelenko     return m_step_out_further_plan_sp->MischiefManaged();
264a5ce6c88SJim Ingham   }
265a5ce6c88SJim Ingham 
266b9c1b51eSKate Stone   // We don't explain signals or breakpoints (breakpoints that handle stepping
26705097246SAdrian Prantl   // in or out will be handled by a child plan.
268a5ce6c88SJim Ingham 
26960c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
270b9c1b51eSKate Stone   if (stop_info_sp) {
271b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
272b9c1b51eSKate Stone     if (reason == eStopReasonBreakpoint) {
27305097246SAdrian Prantl       // If this is OUR breakpoint, we're fine, otherwise we don't know why
27405097246SAdrian Prantl       // this happened...
275b9c1b51eSKate Stone       BreakpointSiteSP site_sp(
276b9c1b51eSKate Stone           m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
277b9c1b51eSKate Stone               stop_info_sp->GetValue()));
278b9c1b51eSKate Stone       if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
279b5c0d1ccSJim Ingham         bool done;
280b5c0d1ccSJim Ingham 
281b5c0d1ccSJim Ingham         StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
282b5c0d1ccSJim Ingham 
283b5c0d1ccSJim Ingham         if (m_step_out_to_id == frame_zero_id)
284b5c0d1ccSJim Ingham           done = true;
285b9c1b51eSKate Stone         else if (m_step_out_to_id < frame_zero_id) {
286b5c0d1ccSJim Ingham           // Either we stepped past the breakpoint, or the stack ID calculation
287b5c0d1ccSJim Ingham           // was incorrect and we should probably stop.
288b5c0d1ccSJim Ingham           done = true;
289b9c1b51eSKate Stone         } else {
290e65b2cf2SEugene Zelenko           done = (m_immediate_step_from_id < frame_zero_id);
291b5c0d1ccSJim Ingham         }
292b5c0d1ccSJim Ingham 
293b9c1b51eSKate Stone         if (done) {
294e103ae92SJonas Devlieghere           if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
29573ca05a2SJim Ingham             CalculateReturnValue();
296481cef25SGreg Clayton             SetPlanComplete();
29773ca05a2SJim Ingham           }
2984b4b2478SJim Ingham         }
299481cef25SGreg Clayton 
300b9c1b51eSKate Stone         // If there was only one owner, then we're done.  But if we also hit
30105097246SAdrian Prantl         // some user breakpoint on our way out, we should mark ourselves as
30205097246SAdrian Prantl         // done, but also not claim to explain the stop, since it is more
30305097246SAdrian Prantl         // important to report the user breakpoint than the step out
30405097246SAdrian Prantl         // completion.
30530fdc8d8SChris Lattner 
306f4b47e15SGreg Clayton         if (site_sp->GetNumberOfOwners() == 1)
30730fdc8d8SChris Lattner           return true;
30830fdc8d8SChris Lattner       }
30930fdc8d8SChris Lattner       return false;
310b9c1b51eSKate Stone     } else if (IsUsuallyUnexplainedStopReason(reason))
31130fdc8d8SChris Lattner       return false;
3129b03fa0cSJim Ingham     else
31330fdc8d8SChris Lattner       return true;
31430fdc8d8SChris Lattner   }
31530fdc8d8SChris Lattner   return true;
31630fdc8d8SChris Lattner }
31730fdc8d8SChris Lattner 
318b9c1b51eSKate Stone bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
319a5ce6c88SJim Ingham   if (IsPlanComplete())
320a5ce6c88SJim Ingham     return true;
321b5c0d1ccSJim Ingham 
3224b4b2478SJim Ingham   bool done = false;
323b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
324b9c1b51eSKate Stone     if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
3254b4b2478SJim Ingham       // Now step through the inlined stack we are in:
326b9c1b51eSKate Stone       if (QueueInlinedStepPlan(true)) {
3274b4b2478SJim Ingham         // If we can't queue a plan to do this, then just call ourselves done.
3284b4b2478SJim Ingham         m_step_out_to_inline_plan_sp.reset();
3294b4b2478SJim Ingham         SetPlanComplete(false);
3304b4b2478SJim Ingham         return true;
331b9c1b51eSKate Stone       } else
3324b4b2478SJim Ingham         done = true;
333b9c1b51eSKate Stone     } else
3344b4b2478SJim Ingham       return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
335b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
3364b4b2478SJim Ingham     if (m_step_through_inline_plan_sp->MischiefManaged())
3374b4b2478SJim Ingham       done = true;
3384b4b2478SJim Ingham     else
3394b4b2478SJim Ingham       return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
340b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
3414b4b2478SJim Ingham     if (m_step_out_further_plan_sp->MischiefManaged())
3424b4b2478SJim Ingham       m_step_out_further_plan_sp.reset();
3434b4b2478SJim Ingham     else
3444b4b2478SJim Ingham       return m_step_out_further_plan_sp->ShouldStop(event_ptr);
3454b4b2478SJim Ingham   }
346b5c0d1ccSJim Ingham 
347b9c1b51eSKate Stone   if (!done) {
348b5c0d1ccSJim Ingham     StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
349e65b2cf2SEugene Zelenko     done = !(frame_zero_id < m_step_out_to_id);
3504b4b2478SJim Ingham   }
3514b4b2478SJim Ingham 
35205097246SAdrian Prantl   // The normal step out computations think we are done, so all we need to do
35305097246SAdrian Prantl   // is consult the ShouldStopHere, and we are done.
354b5c0d1ccSJim Ingham 
355b9c1b51eSKate Stone   if (done) {
356e103ae92SJonas Devlieghere     if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
35773ca05a2SJim Ingham       CalculateReturnValue();
358a5ce6c88SJim Ingham       SetPlanComplete();
359b9c1b51eSKate Stone     } else {
360b9c1b51eSKate Stone       m_step_out_further_plan_sp =
361e103ae92SJonas Devlieghere           QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder, m_status);
3624b4b2478SJim Ingham       done = false;
363a5ce6c88SJim Ingham     }
364a5ce6c88SJim Ingham   }
3654b4b2478SJim Ingham 
3664b4b2478SJim Ingham   return done;
367a5ce6c88SJim Ingham }
36830fdc8d8SChris Lattner 
369b9c1b51eSKate Stone bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
37030fdc8d8SChris Lattner 
371b9c1b51eSKate Stone StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
37230fdc8d8SChris Lattner 
373b9c1b51eSKate Stone bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
374b9c1b51eSKate Stone                                      bool current_plan) {
3754b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
376a5ce6c88SJim Ingham     return true;
377a5ce6c88SJim Ingham 
37830fdc8d8SChris Lattner   if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
37930fdc8d8SChris Lattner     return false;
38030fdc8d8SChris Lattner 
381b9c1b51eSKate Stone   if (current_plan) {
382b9c1b51eSKate Stone     Breakpoint *return_bp =
383b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
384e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
38530fdc8d8SChris Lattner       return_bp->SetEnabled(true);
38630fdc8d8SChris Lattner   }
38730fdc8d8SChris Lattner   return true;
38830fdc8d8SChris Lattner }
38930fdc8d8SChris Lattner 
390b9c1b51eSKate Stone bool ThreadPlanStepOut::WillStop() {
391b9c1b51eSKate Stone   if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
392b9c1b51eSKate Stone     Breakpoint *return_bp =
393b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
394e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
39530fdc8d8SChris Lattner       return_bp->SetEnabled(false);
396a5ce6c88SJim Ingham   }
397a5ce6c88SJim Ingham 
39830fdc8d8SChris Lattner   return true;
39930fdc8d8SChris Lattner }
40030fdc8d8SChris Lattner 
401b9c1b51eSKate Stone bool ThreadPlanStepOut::MischiefManaged() {
402b9c1b51eSKate Stone   if (IsPlanComplete()) {
40330fdc8d8SChris Lattner     // Did I reach my breakpoint?  If so I'm done.
40430fdc8d8SChris Lattner     //
405b9c1b51eSKate Stone     // I also check the stack depth, since if we've blown past the breakpoint
406b9c1b51eSKate Stone     // for some
407b9c1b51eSKate Stone     // reason and we're now stopping for some other reason altogether, then
40805097246SAdrian Prantl     // we're done with this step out operation.
40930fdc8d8SChris Lattner 
4105160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
41130fdc8d8SChris Lattner     if (log)
41230fdc8d8SChris Lattner       log->Printf("Completed step out plan.");
413b9c1b51eSKate Stone     if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
4141ac04c30SGreg Clayton       m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
41530fdc8d8SChris Lattner       m_return_bp_id = LLDB_INVALID_BREAK_ID;
416a5ce6c88SJim Ingham     }
417a5ce6c88SJim Ingham 
41830fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
41930fdc8d8SChris Lattner     return true;
420b9c1b51eSKate Stone   } else {
42130fdc8d8SChris Lattner     return false;
42230fdc8d8SChris Lattner   }
42330fdc8d8SChris Lattner }
42430fdc8d8SChris Lattner 
425b9c1b51eSKate Stone bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
426b9c1b51eSKate Stone   // Now figure out the range of this inlined block, and set up a "step through
42705097246SAdrian Prantl   // range" plan for that.  If we've been provided with a context, then use the
42805097246SAdrian Prantl   // block in that context.
429b57e4a1bSJason Molenda   StackFrameSP immediate_return_from_sp(m_thread.GetStackFrameAtIndex(0));
430a5ce6c88SJim Ingham   if (!immediate_return_from_sp)
431a5ce6c88SJim Ingham     return false;
432a5ce6c88SJim Ingham 
4335160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
434b9c1b51eSKate Stone   if (log) {
435a5ce6c88SJim Ingham     StreamString s;
436a5ce6c88SJim Ingham     immediate_return_from_sp->Dump(&s, true, false);
437a5ce6c88SJim Ingham     log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
438a5ce6c88SJim Ingham   }
439a5ce6c88SJim Ingham 
440a5ce6c88SJim Ingham   Block *from_block = immediate_return_from_sp->GetFrameBlock();
441b9c1b51eSKate Stone   if (from_block) {
442a5ce6c88SJim Ingham     Block *inlined_block = from_block->GetContainingInlinedBlock();
443b9c1b51eSKate Stone     if (inlined_block) {
444a5ce6c88SJim Ingham       size_t num_ranges = inlined_block->GetNumRanges();
445a5ce6c88SJim Ingham       AddressRange inline_range;
446b9c1b51eSKate Stone       if (inlined_block->GetRangeAtIndex(0, inline_range)) {
447a5ce6c88SJim Ingham         SymbolContext inlined_sc;
448a5ce6c88SJim Ingham         inlined_block->CalculateSymbolContext(&inlined_sc);
4495f1a4e1fSJim Ingham         inlined_sc.target_sp = GetTarget().shared_from_this();
450b9c1b51eSKate Stone         RunMode run_mode =
451b9c1b51eSKate Stone             m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
4524b4b2478SJim Ingham         const LazyBool avoid_no_debug = eLazyBoolNo;
4532bdbfd50SJim Ingham 
454*796ac80bSJonas Devlieghere         m_step_through_inline_plan_sp =
455*796ac80bSJonas Devlieghere             std::make_shared<ThreadPlanStepOverRange>(
456*796ac80bSJonas Devlieghere                 m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug);
457b9c1b51eSKate Stone         ThreadPlanStepOverRange *step_through_inline_plan_ptr =
458b9c1b51eSKate Stone             static_cast<ThreadPlanStepOverRange *>(
459b9c1b51eSKate Stone                 m_step_through_inline_plan_sp.get());
4602bdbfd50SJim Ingham         m_step_through_inline_plan_sp->SetPrivate(true);
4612bdbfd50SJim Ingham 
462a5ce6c88SJim Ingham         step_through_inline_plan_ptr->SetOkayToDiscard(true);
463a5ce6c88SJim Ingham         StreamString errors;
464b9c1b51eSKate Stone         if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
465a5ce6c88SJim Ingham           // FIXME: Log this failure.
466a5ce6c88SJim Ingham           delete step_through_inline_plan_ptr;
467a5ce6c88SJim Ingham           return false;
468a5ce6c88SJim Ingham         }
469a5ce6c88SJim Ingham 
470b9c1b51eSKate Stone         for (size_t i = 1; i < num_ranges; i++) {
471a5ce6c88SJim Ingham           if (inlined_block->GetRangeAtIndex(i, inline_range))
472a5ce6c88SJim Ingham             step_through_inline_plan_ptr->AddRange(inline_range);
473a5ce6c88SJim Ingham         }
4742bdbfd50SJim Ingham 
475a5ce6c88SJim Ingham         if (queue_now)
476a5ce6c88SJim Ingham           m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
477a5ce6c88SJim Ingham         return true;
478a5ce6c88SJim Ingham       }
479a5ce6c88SJim Ingham     }
480a5ce6c88SJim Ingham   }
481a5ce6c88SJim Ingham 
482a5ce6c88SJim Ingham   return false;
483a5ce6c88SJim Ingham }
48473ca05a2SJim Ingham 
485b9c1b51eSKate Stone void ThreadPlanStepOut::CalculateReturnValue() {
48673ca05a2SJim Ingham   if (m_return_valobj_sp)
48773ca05a2SJim Ingham     return;
48873ca05a2SJim Ingham 
489b612ac37SJim Ingham   if (!m_calculate_return_value)
490b612ac37SJim Ingham     return;
491b612ac37SJim Ingham 
492b9c1b51eSKate Stone   if (m_immediate_step_from_function != nullptr) {
493b9c1b51eSKate Stone     CompilerType return_compiler_type =
494b9c1b51eSKate Stone         m_immediate_step_from_function->GetCompilerType()
495b9c1b51eSKate Stone             .GetFunctionReturnType();
496b9c1b51eSKate Stone     if (return_compiler_type) {
4971ac04c30SGreg Clayton       lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
49873ca05a2SJim Ingham       if (abi_sp)
499b9c1b51eSKate Stone         m_return_valobj_sp =
500b9c1b51eSKate Stone             abi_sp->GetReturnValueObject(m_thread, return_compiler_type);
50173ca05a2SJim Ingham     }
50273ca05a2SJim Ingham   }
50373ca05a2SJim Ingham }
50464e7ead1SJim Ingham 
505b9c1b51eSKate Stone bool ThreadPlanStepOut::IsPlanStale() {
50605097246SAdrian Prantl   // If we are still lower on the stack than the frame we are returning to,
50705097246SAdrian Prantl   // then there's something for us to do.  Otherwise, we're stale.
50864e7ead1SJim Ingham 
50964e7ead1SJim Ingham   StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
510e65b2cf2SEugene Zelenko   return !(frame_zero_id < m_step_out_to_id);
51164e7ead1SJim Ingham }
512