130fdc8d8SChris Lattner //===-- ThreadPlanStepOut.cpp -----------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler 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 
2630fdc8d8SChris Lattner using namespace lldb;
2730fdc8d8SChris Lattner using namespace lldb_private;
2830fdc8d8SChris Lattner 
294b4b2478SJim Ingham uint32_t ThreadPlanStepOut::s_default_flag_values = 0;
304b4b2478SJim Ingham 
3130fdc8d8SChris Lattner //----------------------------------------------------------------------
3230fdc8d8SChris Lattner // ThreadPlanStepOut: Step out of the current frame
3330fdc8d8SChris Lattner //----------------------------------------------------------------------
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.
87b9c1b51eSKate Stone       m_step_out_to_inline_plan_sp.reset(new ThreadPlanStepOut(
88b9c1b51eSKate Stone           m_thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
89b9c1b51eSKate Stone           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()) {
113b9c1b51eSKate Stone         range =
114b9c1b51eSKate Stone             return_address_sc.line_entry.GetSameLineContiguousAddressRange();
115b9c1b51eSKate Stone         if (range.GetByteSize() > 0) {
116b9c1b51eSKate Stone           return_address =
117b9c1b51eSKate Stone               m_thread.GetProcess()->AdvanceAddressToNextBranchInstruction(
118b9c1b51eSKate Stone                   return_address, range);
119fd4cea53SJason Molenda         }
120fd4cea53SJason Molenda       }
121fd4cea53SJason Molenda     }
122b9c1b51eSKate Stone     m_return_addr =
123b9c1b51eSKate Stone         return_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget());
124708709c0SSean Callanan 
125708709c0SSean Callanan     if (m_return_addr == LLDB_INVALID_ADDRESS)
126708709c0SSean Callanan       return;
127708709c0SSean Callanan 
128b9c1b51eSKate Stone     Breakpoint *return_bp = m_thread.CalculateTarget()
129b9c1b51eSKate Stone                                 ->CreateBreakpoint(m_return_addr, true, false)
130b9c1b51eSKate Stone                                 .get();
131e103ae92SJonas Devlieghere 
132b9c1b51eSKate Stone     if (return_bp != nullptr) {
133e103ae92SJonas Devlieghere       if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
134e103ae92SJonas Devlieghere         m_could_not_resolve_hw_bp = true;
13530fdc8d8SChris Lattner       return_bp->SetThreadID(m_thread.GetID());
13630fdc8d8SChris Lattner       m_return_bp_id = return_bp->GetID();
1372995077dSJim Ingham       return_bp->SetBreakpointKind("step-out");
13830fdc8d8SChris Lattner     }
13973ca05a2SJim Ingham 
140b9c1b51eSKate Stone     if (immediate_return_from_sp) {
141b9c1b51eSKate Stone       const SymbolContext &sc =
142b9c1b51eSKate Stone           immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
143b9c1b51eSKate Stone       if (sc.function) {
14473ca05a2SJim Ingham         m_immediate_step_from_function = sc.function;
14573ca05a2SJim Ingham       }
14673ca05a2SJim Ingham     }
14730fdc8d8SChris Lattner   }
148a5ce6c88SJim Ingham }
149a5ce6c88SJim Ingham 
150b9c1b51eSKate Stone void ThreadPlanStepOut::SetupAvoidNoDebug(
151b9c1b51eSKate Stone     LazyBool step_out_avoids_code_without_debug_info) {
1524b4b2478SJim Ingham   bool avoid_nodebug = true;
153b9c1b51eSKate Stone   switch (step_out_avoids_code_without_debug_info) {
1544b4b2478SJim Ingham   case eLazyBoolYes:
1554b4b2478SJim Ingham     avoid_nodebug = true;
1564b4b2478SJim Ingham     break;
1574b4b2478SJim Ingham   case eLazyBoolNo:
1584b4b2478SJim Ingham     avoid_nodebug = false;
1594b4b2478SJim Ingham     break;
1604b4b2478SJim Ingham   case eLazyBoolCalculate:
1614b4b2478SJim Ingham     avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
1624b4b2478SJim Ingham     break;
1634b4b2478SJim Ingham   }
1644b4b2478SJim Ingham   if (avoid_nodebug)
1654b4b2478SJim Ingham     GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1664b4b2478SJim Ingham   else
1674b4b2478SJim Ingham     GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1684b4b2478SJim Ingham }
1694b4b2478SJim Ingham 
170b9c1b51eSKate Stone void ThreadPlanStepOut::DidPush() {
1714b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
1724b4b2478SJim Ingham     m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
173a5ce6c88SJim Ingham   else if (m_step_through_inline_plan_sp)
174a5ce6c88SJim Ingham     m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
17530fdc8d8SChris Lattner }
17630fdc8d8SChris Lattner 
177b9c1b51eSKate Stone ThreadPlanStepOut::~ThreadPlanStepOut() {
17830fdc8d8SChris Lattner   if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
1791ac04c30SGreg Clayton     m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
18030fdc8d8SChris Lattner }
18130fdc8d8SChris Lattner 
182b9c1b51eSKate Stone void ThreadPlanStepOut::GetDescription(Stream *s,
183b9c1b51eSKate Stone                                        lldb::DescriptionLevel level) {
18430fdc8d8SChris Lattner   if (level == lldb::eDescriptionLevelBrief)
18530fdc8d8SChris Lattner     s->Printf("step out");
186b9c1b51eSKate Stone   else {
1874b4b2478SJim Ingham     if (m_step_out_to_inline_plan_sp)
188b5c0d1ccSJim Ingham       s->Printf("Stepping out to inlined frame so we can walk through it.");
189a5ce6c88SJim Ingham     else if (m_step_through_inline_plan_sp)
190a5ce6c88SJim Ingham       s->Printf("Stepping out by stepping through inlined function.");
191b9c1b51eSKate Stone     else {
1922bdbfd50SJim Ingham       s->Printf("Stepping out from ");
1932bdbfd50SJim Ingham       Address tmp_address;
194b9c1b51eSKate Stone       if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) {
195b9c1b51eSKate Stone         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
196b9c1b51eSKate Stone                          Address::DumpStyleLoadAddress);
197b9c1b51eSKate Stone       } else {
1982bdbfd50SJim Ingham         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn);
1992bdbfd50SJim Ingham       }
2002bdbfd50SJim Ingham 
201b9c1b51eSKate Stone       // FIXME: find some useful way to present the m_return_id, since there may
202b9c1b51eSKate Stone       // be multiple copies of the
2032bdbfd50SJim Ingham       // same function on the stack.
2042bdbfd50SJim Ingham 
2052bdbfd50SJim Ingham       s->Printf(" returning to frame at ");
206b9c1b51eSKate Stone       if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) {
207b9c1b51eSKate Stone         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
208b9c1b51eSKate Stone                          Address::DumpStyleLoadAddress);
209b9c1b51eSKate Stone       } else {
2102bdbfd50SJim Ingham         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
2112bdbfd50SJim Ingham       }
2122bdbfd50SJim Ingham 
2132bdbfd50SJim Ingham       if (level == eDescriptionLevelVerbose)
2142bdbfd50SJim Ingham         s->Printf(" using breakpoint site %d", m_return_bp_id);
2152bdbfd50SJim Ingham     }
21630fdc8d8SChris Lattner   }
2174b36f791SVedant Kumar 
2184b36f791SVedant Kumar   s->Printf("\n");
2194b36f791SVedant Kumar   for (StackFrameSP frame_sp : m_stepped_past_frames) {
2204b36f791SVedant Kumar     s->Printf("Stepped out past: ");
2214b36f791SVedant Kumar     frame_sp->DumpUsingSettingsFormat(s);
2224b36f791SVedant Kumar   }
22330fdc8d8SChris Lattner }
22430fdc8d8SChris Lattner 
225b9c1b51eSKate Stone bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
2264b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
2274b4b2478SJim Ingham     return m_step_out_to_inline_plan_sp->ValidatePlan(error);
228e103ae92SJonas Devlieghere 
229e103ae92SJonas Devlieghere   if (m_step_through_inline_plan_sp)
230a5ce6c88SJim Ingham     return m_step_through_inline_plan_sp->ValidatePlan(error);
231e103ae92SJonas Devlieghere 
232e103ae92SJonas Devlieghere   if (m_could_not_resolve_hw_bp) {
233e103ae92SJonas Devlieghere     if (error)
234e103ae92SJonas Devlieghere       error->PutCString(
235e103ae92SJonas Devlieghere           "Could not create hardware breakpoint for thread plan.");
236e103ae92SJonas Devlieghere     return false;
237e103ae92SJonas Devlieghere   }
238e103ae92SJonas Devlieghere 
239e103ae92SJonas Devlieghere   if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
240708709c0SSean Callanan     if (error)
241a5ce6c88SJim Ingham       error->PutCString("Could not create return address breakpoint.");
24230fdc8d8SChris Lattner     return false;
243e103ae92SJonas Devlieghere   }
244e103ae92SJonas Devlieghere 
24530fdc8d8SChris Lattner   return true;
24630fdc8d8SChris Lattner }
24730fdc8d8SChris Lattner 
248b9c1b51eSKate Stone bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
24905097246SAdrian Prantl   // If the step out plan is done, then we just need to step through the
25005097246SAdrian Prantl   // inlined frame.
251b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
252e65b2cf2SEugene Zelenko     return m_step_out_to_inline_plan_sp->MischiefManaged();
253b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
254b9c1b51eSKate Stone     if (m_step_through_inline_plan_sp->MischiefManaged()) {
25573ca05a2SJim Ingham       CalculateReturnValue();
256a5ce6c88SJim Ingham       SetPlanComplete();
257a5ce6c88SJim Ingham       return true;
258b9c1b51eSKate Stone     } else
259a5ce6c88SJim Ingham       return false;
260b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
261e65b2cf2SEugene Zelenko     return m_step_out_further_plan_sp->MischiefManaged();
262a5ce6c88SJim Ingham   }
263a5ce6c88SJim Ingham 
264b9c1b51eSKate Stone   // We don't explain signals or breakpoints (breakpoints that handle stepping
26505097246SAdrian Prantl   // in or out will be handled by a child plan.
266a5ce6c88SJim Ingham 
26760c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
268b9c1b51eSKate Stone   if (stop_info_sp) {
269b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
270b9c1b51eSKate Stone     if (reason == eStopReasonBreakpoint) {
27105097246SAdrian Prantl       // If this is OUR breakpoint, we're fine, otherwise we don't know why
27205097246SAdrian Prantl       // this happened...
273b9c1b51eSKate Stone       BreakpointSiteSP site_sp(
274b9c1b51eSKate Stone           m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
275b9c1b51eSKate Stone               stop_info_sp->GetValue()));
276b9c1b51eSKate Stone       if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
277b5c0d1ccSJim Ingham         bool done;
278b5c0d1ccSJim Ingham 
279b5c0d1ccSJim Ingham         StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
280b5c0d1ccSJim Ingham 
281b5c0d1ccSJim Ingham         if (m_step_out_to_id == frame_zero_id)
282b5c0d1ccSJim Ingham           done = true;
283b9c1b51eSKate Stone         else if (m_step_out_to_id < frame_zero_id) {
284b5c0d1ccSJim Ingham           // Either we stepped past the breakpoint, or the stack ID calculation
285b5c0d1ccSJim Ingham           // was incorrect and we should probably stop.
286b5c0d1ccSJim Ingham           done = true;
287b9c1b51eSKate Stone         } else {
288e65b2cf2SEugene Zelenko           done = (m_immediate_step_from_id < frame_zero_id);
289b5c0d1ccSJim Ingham         }
290b5c0d1ccSJim Ingham 
291b9c1b51eSKate Stone         if (done) {
292e103ae92SJonas Devlieghere           if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
29373ca05a2SJim Ingham             CalculateReturnValue();
294481cef25SGreg Clayton             SetPlanComplete();
29573ca05a2SJim Ingham           }
2964b4b2478SJim Ingham         }
297481cef25SGreg Clayton 
298b9c1b51eSKate Stone         // If there was only one owner, then we're done.  But if we also hit
29905097246SAdrian Prantl         // some user breakpoint on our way out, we should mark ourselves as
30005097246SAdrian Prantl         // done, but also not claim to explain the stop, since it is more
30105097246SAdrian Prantl         // important to report the user breakpoint than the step out
30205097246SAdrian Prantl         // completion.
30330fdc8d8SChris Lattner 
304f4b47e15SGreg Clayton         if (site_sp->GetNumberOfOwners() == 1)
30530fdc8d8SChris Lattner           return true;
30630fdc8d8SChris Lattner       }
30730fdc8d8SChris Lattner       return false;
308b9c1b51eSKate Stone     } else if (IsUsuallyUnexplainedStopReason(reason))
30930fdc8d8SChris Lattner       return false;
3109b03fa0cSJim Ingham     else
31130fdc8d8SChris Lattner       return true;
31230fdc8d8SChris Lattner   }
31330fdc8d8SChris Lattner   return true;
31430fdc8d8SChris Lattner }
31530fdc8d8SChris Lattner 
316b9c1b51eSKate Stone bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
317a5ce6c88SJim Ingham   if (IsPlanComplete())
318a5ce6c88SJim Ingham     return true;
319b5c0d1ccSJim Ingham 
3204b4b2478SJim Ingham   bool done = false;
321b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
322b9c1b51eSKate Stone     if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
3234b4b2478SJim Ingham       // Now step through the inlined stack we are in:
324b9c1b51eSKate Stone       if (QueueInlinedStepPlan(true)) {
3254b4b2478SJim Ingham         // If we can't queue a plan to do this, then just call ourselves done.
3264b4b2478SJim Ingham         m_step_out_to_inline_plan_sp.reset();
3274b4b2478SJim Ingham         SetPlanComplete(false);
3284b4b2478SJim Ingham         return true;
329b9c1b51eSKate Stone       } else
3304b4b2478SJim Ingham         done = true;
331b9c1b51eSKate Stone     } else
3324b4b2478SJim Ingham       return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
333b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
3344b4b2478SJim Ingham     if (m_step_through_inline_plan_sp->MischiefManaged())
3354b4b2478SJim Ingham       done = true;
3364b4b2478SJim Ingham     else
3374b4b2478SJim Ingham       return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
338b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
3394b4b2478SJim Ingham     if (m_step_out_further_plan_sp->MischiefManaged())
3404b4b2478SJim Ingham       m_step_out_further_plan_sp.reset();
3414b4b2478SJim Ingham     else
3424b4b2478SJim Ingham       return m_step_out_further_plan_sp->ShouldStop(event_ptr);
3434b4b2478SJim Ingham   }
344b5c0d1ccSJim Ingham 
345b9c1b51eSKate Stone   if (!done) {
346b5c0d1ccSJim Ingham     StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
347e65b2cf2SEugene Zelenko     done = !(frame_zero_id < m_step_out_to_id);
3484b4b2478SJim Ingham   }
3494b4b2478SJim Ingham 
35005097246SAdrian Prantl   // The normal step out computations think we are done, so all we need to do
35105097246SAdrian Prantl   // is consult the ShouldStopHere, and we are done.
352b5c0d1ccSJim Ingham 
353b9c1b51eSKate Stone   if (done) {
354e103ae92SJonas Devlieghere     if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
35573ca05a2SJim Ingham       CalculateReturnValue();
356a5ce6c88SJim Ingham       SetPlanComplete();
357b9c1b51eSKate Stone     } else {
358b9c1b51eSKate Stone       m_step_out_further_plan_sp =
359e103ae92SJonas Devlieghere           QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder, m_status);
3604b4b2478SJim Ingham       done = false;
361a5ce6c88SJim Ingham     }
362a5ce6c88SJim Ingham   }
3634b4b2478SJim Ingham 
3644b4b2478SJim Ingham   return done;
365a5ce6c88SJim Ingham }
36630fdc8d8SChris Lattner 
367b9c1b51eSKate Stone bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
36830fdc8d8SChris Lattner 
369b9c1b51eSKate Stone StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
37030fdc8d8SChris Lattner 
371b9c1b51eSKate Stone bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
372b9c1b51eSKate Stone                                      bool current_plan) {
3734b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
374a5ce6c88SJim Ingham     return true;
375a5ce6c88SJim Ingham 
37630fdc8d8SChris Lattner   if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
37730fdc8d8SChris Lattner     return false;
37830fdc8d8SChris Lattner 
379b9c1b51eSKate Stone   if (current_plan) {
380b9c1b51eSKate Stone     Breakpoint *return_bp =
381b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
382e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
38330fdc8d8SChris Lattner       return_bp->SetEnabled(true);
38430fdc8d8SChris Lattner   }
38530fdc8d8SChris Lattner   return true;
38630fdc8d8SChris Lattner }
38730fdc8d8SChris Lattner 
388b9c1b51eSKate Stone bool ThreadPlanStepOut::WillStop() {
389b9c1b51eSKate Stone   if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
390b9c1b51eSKate Stone     Breakpoint *return_bp =
391b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
392e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
39330fdc8d8SChris Lattner       return_bp->SetEnabled(false);
394a5ce6c88SJim Ingham   }
395a5ce6c88SJim Ingham 
39630fdc8d8SChris Lattner   return true;
39730fdc8d8SChris Lattner }
39830fdc8d8SChris Lattner 
399b9c1b51eSKate Stone bool ThreadPlanStepOut::MischiefManaged() {
400b9c1b51eSKate Stone   if (IsPlanComplete()) {
40130fdc8d8SChris Lattner     // Did I reach my breakpoint?  If so I'm done.
40230fdc8d8SChris Lattner     //
403b9c1b51eSKate Stone     // I also check the stack depth, since if we've blown past the breakpoint
404b9c1b51eSKate Stone     // for some
405b9c1b51eSKate Stone     // reason and we're now stopping for some other reason altogether, then
40605097246SAdrian Prantl     // we're done with this step out operation.
40730fdc8d8SChris Lattner 
4085160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
40930fdc8d8SChris Lattner     if (log)
41030fdc8d8SChris Lattner       log->Printf("Completed step out plan.");
411b9c1b51eSKate Stone     if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
4121ac04c30SGreg Clayton       m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
41330fdc8d8SChris Lattner       m_return_bp_id = LLDB_INVALID_BREAK_ID;
414a5ce6c88SJim Ingham     }
415a5ce6c88SJim Ingham 
41630fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
41730fdc8d8SChris Lattner     return true;
418b9c1b51eSKate Stone   } else {
41930fdc8d8SChris Lattner     return false;
42030fdc8d8SChris Lattner   }
42130fdc8d8SChris Lattner }
42230fdc8d8SChris Lattner 
423b9c1b51eSKate Stone bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
424b9c1b51eSKate Stone   // Now figure out the range of this inlined block, and set up a "step through
42505097246SAdrian Prantl   // range" plan for that.  If we've been provided with a context, then use the
42605097246SAdrian Prantl   // block in that context.
427b57e4a1bSJason Molenda   StackFrameSP immediate_return_from_sp(m_thread.GetStackFrameAtIndex(0));
428a5ce6c88SJim Ingham   if (!immediate_return_from_sp)
429a5ce6c88SJim Ingham     return false;
430a5ce6c88SJim Ingham 
4315160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
432b9c1b51eSKate Stone   if (log) {
433a5ce6c88SJim Ingham     StreamString s;
434a5ce6c88SJim Ingham     immediate_return_from_sp->Dump(&s, true, false);
435a5ce6c88SJim Ingham     log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
436a5ce6c88SJim Ingham   }
437a5ce6c88SJim Ingham 
438a5ce6c88SJim Ingham   Block *from_block = immediate_return_from_sp->GetFrameBlock();
439b9c1b51eSKate Stone   if (from_block) {
440a5ce6c88SJim Ingham     Block *inlined_block = from_block->GetContainingInlinedBlock();
441b9c1b51eSKate Stone     if (inlined_block) {
442a5ce6c88SJim Ingham       size_t num_ranges = inlined_block->GetNumRanges();
443a5ce6c88SJim Ingham       AddressRange inline_range;
444b9c1b51eSKate Stone       if (inlined_block->GetRangeAtIndex(0, inline_range)) {
445a5ce6c88SJim Ingham         SymbolContext inlined_sc;
446a5ce6c88SJim Ingham         inlined_block->CalculateSymbolContext(&inlined_sc);
4475f1a4e1fSJim Ingham         inlined_sc.target_sp = GetTarget().shared_from_this();
448b9c1b51eSKate Stone         RunMode run_mode =
449b9c1b51eSKate Stone             m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
4504b4b2478SJim Ingham         const LazyBool avoid_no_debug = eLazyBoolNo;
4512bdbfd50SJim Ingham 
452b9c1b51eSKate Stone         m_step_through_inline_plan_sp.reset(new ThreadPlanStepOverRange(
453b9c1b51eSKate Stone             m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug));
454b9c1b51eSKate Stone         ThreadPlanStepOverRange *step_through_inline_plan_ptr =
455b9c1b51eSKate Stone             static_cast<ThreadPlanStepOverRange *>(
456b9c1b51eSKate Stone                 m_step_through_inline_plan_sp.get());
4572bdbfd50SJim Ingham         m_step_through_inline_plan_sp->SetPrivate(true);
4582bdbfd50SJim Ingham 
459a5ce6c88SJim Ingham         step_through_inline_plan_ptr->SetOkayToDiscard(true);
460a5ce6c88SJim Ingham         StreamString errors;
461b9c1b51eSKate Stone         if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
462a5ce6c88SJim Ingham           // FIXME: Log this failure.
463a5ce6c88SJim Ingham           delete step_through_inline_plan_ptr;
464a5ce6c88SJim Ingham           return false;
465a5ce6c88SJim Ingham         }
466a5ce6c88SJim Ingham 
467b9c1b51eSKate Stone         for (size_t i = 1; i < num_ranges; i++) {
468a5ce6c88SJim Ingham           if (inlined_block->GetRangeAtIndex(i, inline_range))
469a5ce6c88SJim Ingham             step_through_inline_plan_ptr->AddRange(inline_range);
470a5ce6c88SJim Ingham         }
4712bdbfd50SJim Ingham 
472a5ce6c88SJim Ingham         if (queue_now)
473a5ce6c88SJim Ingham           m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
474a5ce6c88SJim Ingham         return true;
475a5ce6c88SJim Ingham       }
476a5ce6c88SJim Ingham     }
477a5ce6c88SJim Ingham   }
478a5ce6c88SJim Ingham 
479a5ce6c88SJim Ingham   return false;
480a5ce6c88SJim Ingham }
48173ca05a2SJim Ingham 
482b9c1b51eSKate Stone void ThreadPlanStepOut::CalculateReturnValue() {
48373ca05a2SJim Ingham   if (m_return_valobj_sp)
48473ca05a2SJim Ingham     return;
48573ca05a2SJim Ingham 
486b612ac37SJim Ingham   if (!m_calculate_return_value)
487b612ac37SJim Ingham     return;
488b612ac37SJim Ingham 
489b9c1b51eSKate Stone   if (m_immediate_step_from_function != nullptr) {
490b9c1b51eSKate Stone     CompilerType return_compiler_type =
491b9c1b51eSKate Stone         m_immediate_step_from_function->GetCompilerType()
492b9c1b51eSKate Stone             .GetFunctionReturnType();
493b9c1b51eSKate Stone     if (return_compiler_type) {
4941ac04c30SGreg Clayton       lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
49573ca05a2SJim Ingham       if (abi_sp)
496b9c1b51eSKate Stone         m_return_valobj_sp =
497b9c1b51eSKate Stone             abi_sp->GetReturnValueObject(m_thread, return_compiler_type);
49873ca05a2SJim Ingham     }
49973ca05a2SJim Ingham   }
50073ca05a2SJim Ingham }
50164e7ead1SJim Ingham 
502b9c1b51eSKate Stone bool ThreadPlanStepOut::IsPlanStale() {
50305097246SAdrian Prantl   // If we are still lower on the stack than the frame we are returning to,
50405097246SAdrian Prantl   // then there's something for us to do.  Otherwise, we're stale.
50564e7ead1SJim Ingham 
50664e7ead1SJim Ingham   StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
507e65b2cf2SEugene Zelenko   return !(frame_zero_id < m_step_out_to_id);
50864e7ead1SJim Ingham }
509