130fdc8d8SChris Lattner //===-- ThreadPlanStepOut.cpp -----------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner // C Includes
1130fdc8d8SChris Lattner // C++ Includes
1230fdc8d8SChris Lattner // Other libraries and framework includes
1330fdc8d8SChris Lattner // Project includes
14e65b2cf2SEugene Zelenko #include "lldb/Target/ThreadPlanStepOut.h"
1530fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h"
1673ca05a2SJim Ingham #include "lldb/Core/Value.h"
1773ca05a2SJim Ingham #include "lldb/Core/ValueObjectConstResult.h"
181f746071SGreg Clayton #include "lldb/Symbol/Block.h"
191f746071SGreg Clayton #include "lldb/Symbol/Function.h"
20fd4cea53SJason Molenda #include "lldb/Symbol/Symbol.h"
211f746071SGreg Clayton #include "lldb/Symbol/Type.h"
2232abc6edSZachary Turner #include "lldb/Target/ABI.h"
2330fdc8d8SChris Lattner #include "lldb/Target/Process.h"
2430fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
25f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
2630fdc8d8SChris Lattner #include "lldb/Target/Target.h"
27a5ce6c88SJim Ingham #include "lldb/Target/ThreadPlanStepOverRange.h"
284b4b2478SJim Ingham #include "lldb/Target/ThreadPlanStepThrough.h"
296f9e6901SZachary Turner #include "lldb/Utility/Log.h"
3030fdc8d8SChris Lattner 
3130fdc8d8SChris Lattner using namespace lldb;
3230fdc8d8SChris Lattner using namespace lldb_private;
3330fdc8d8SChris Lattner 
344b4b2478SJim Ingham uint32_t ThreadPlanStepOut::s_default_flag_values = 0;
354b4b2478SJim Ingham 
3630fdc8d8SChris Lattner //----------------------------------------------------------------------
3730fdc8d8SChris Lattner // ThreadPlanStepOut: Step out of the current frame
3830fdc8d8SChris Lattner //----------------------------------------------------------------------
39b9c1b51eSKate Stone ThreadPlanStepOut::ThreadPlanStepOut(
40b9c1b51eSKate Stone     Thread &thread, SymbolContext *context, bool first_insn, bool stop_others,
41b9c1b51eSKate Stone     Vote stop_vote, Vote run_vote, uint32_t frame_idx,
42fd4cea53SJason Molenda     LazyBool step_out_avoids_code_without_debug_info,
43b9c1b51eSKate Stone     bool continue_to_next_branch, bool gather_return_value)
44b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindStepOut, "Step out", thread, stop_vote,
45b9c1b51eSKate Stone                  run_vote),
46b9c1b51eSKate Stone       ThreadPlanShouldStopHere(this), m_step_from_insn(LLDB_INVALID_ADDRESS),
471ee0d4f7SBenjamin Kramer       m_return_bp_id(LLDB_INVALID_BREAK_ID),
48b9c1b51eSKate Stone       m_return_addr(LLDB_INVALID_ADDRESS), m_stop_others(stop_others),
49b612ac37SJim Ingham       m_immediate_step_from_function(nullptr),
50b9c1b51eSKate Stone       m_calculate_return_value(gather_return_value) {
51*4b36f791SVedant Kumar   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
524b4b2478SJim Ingham   SetFlagsToDefault();
534b4b2478SJim Ingham   SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
544b4b2478SJim Ingham 
5530fdc8d8SChris Lattner   m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
5630fdc8d8SChris Lattner 
57*4b36f791SVedant Kumar   uint32_t return_frame_index = frame_idx + 1;
58*4b36f791SVedant Kumar   StackFrameSP return_frame_sp(
59*4b36f791SVedant Kumar       m_thread.GetStackFrameAtIndex(return_frame_index));
60b9c1b51eSKate Stone   StackFrameSP immediate_return_from_sp(
61b9c1b51eSKate Stone       m_thread.GetStackFrameAtIndex(frame_idx));
62a5ce6c88SJim Ingham 
63632d2f72SSean Callanan   if (!return_frame_sp || !immediate_return_from_sp)
64632d2f72SSean Callanan     return; // we can't do anything here.  ValidatePlan() will return false.
65632d2f72SSean Callanan 
66*4b36f791SVedant Kumar   // While stepping out, behave as-if artificial frames are not present.
67*4b36f791SVedant Kumar   while (return_frame_sp->IsArtificial()) {
68*4b36f791SVedant Kumar     m_stepped_past_frames.push_back(return_frame_sp);
69*4b36f791SVedant Kumar 
70*4b36f791SVedant Kumar     ++return_frame_index;
71*4b36f791SVedant Kumar     return_frame_sp = m_thread.GetStackFrameAtIndex(return_frame_index);
72*4b36f791SVedant Kumar 
73*4b36f791SVedant Kumar     // We never expect to see an artificial frame without a regular ancestor.
74*4b36f791SVedant Kumar     // If this happens, log the issue and defensively refuse to step out.
75*4b36f791SVedant Kumar     if (!return_frame_sp) {
76*4b36f791SVedant Kumar       LLDB_LOG(log, "Can't step out of frame with artificial ancestors");
77*4b36f791SVedant Kumar       return;
78*4b36f791SVedant Kumar     }
79*4b36f791SVedant Kumar   }
80*4b36f791SVedant Kumar 
81b5c0d1ccSJim Ingham   m_step_out_to_id = return_frame_sp->GetStackID();
82b5c0d1ccSJim Ingham   m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
83a5ce6c88SJim Ingham 
8405097246SAdrian Prantl   // If the frame directly below the one we are returning to is inlined, we
8505097246SAdrian Prantl   // have to be a little more careful.  It is non-trivial to determine the real
8605097246SAdrian Prantl   // "return code address" for an inlined frame, so we have to work our way to
8705097246SAdrian Prantl   // that frame and then step out.
88*4b36f791SVedant Kumar   if (immediate_return_from_sp->IsInlined()) {
89b9c1b51eSKate Stone     if (frame_idx > 0) {
90b9c1b51eSKate Stone       // First queue a plan that gets us to this inlined frame, and when we get
9105097246SAdrian Prantl       // there we'll queue a second plan that walks us out of this frame.
92b9c1b51eSKate Stone       m_step_out_to_inline_plan_sp.reset(new ThreadPlanStepOut(
93b9c1b51eSKate Stone           m_thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
94b9c1b51eSKate Stone           frame_idx - 1, eLazyBoolNo, continue_to_next_branch));
95b9c1b51eSKate Stone       static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())
96b9c1b51eSKate Stone           ->SetShouldStopHereCallbacks(nullptr, nullptr);
972bdbfd50SJim Ingham       m_step_out_to_inline_plan_sp->SetPrivate(true);
98b9c1b51eSKate Stone     } else {
9905097246SAdrian Prantl       // If we're already at the inlined frame we're stepping through, then
10005097246SAdrian Prantl       // just do that now.
101a5ce6c88SJim Ingham       QueueInlinedStepPlan(false);
102a5ce6c88SJim Ingham     }
103*4b36f791SVedant Kumar   } else {
10430fdc8d8SChris Lattner     // Find the return address and set a breakpoint there:
10530fdc8d8SChris Lattner     // FIXME - can we do this more securely if we know first_insn?
10630fdc8d8SChris Lattner 
107fd4cea53SJason Molenda     Address return_address(return_frame_sp->GetFrameCodeAddress());
108b9c1b51eSKate Stone     if (continue_to_next_branch) {
109fd4cea53SJason Molenda       SymbolContext return_address_sc;
110fd4cea53SJason Molenda       AddressRange range;
111fd4cea53SJason Molenda       Address return_address_decr_pc = return_address;
112fd4cea53SJason Molenda       if (return_address_decr_pc.GetOffset() > 0)
113fd4cea53SJason Molenda         return_address_decr_pc.Slide(-1);
114fd4cea53SJason Molenda 
115b9c1b51eSKate Stone       return_address_decr_pc.CalculateSymbolContext(
116b9c1b51eSKate Stone           &return_address_sc, lldb::eSymbolContextLineEntry);
117b9c1b51eSKate Stone       if (return_address_sc.line_entry.IsValid()) {
118b9c1b51eSKate Stone         range =
119b9c1b51eSKate Stone             return_address_sc.line_entry.GetSameLineContiguousAddressRange();
120b9c1b51eSKate Stone         if (range.GetByteSize() > 0) {
121b9c1b51eSKate Stone           return_address =
122b9c1b51eSKate Stone               m_thread.GetProcess()->AdvanceAddressToNextBranchInstruction(
123b9c1b51eSKate Stone                   return_address, range);
124fd4cea53SJason Molenda         }
125fd4cea53SJason Molenda       }
126fd4cea53SJason Molenda     }
127b9c1b51eSKate Stone     m_return_addr =
128b9c1b51eSKate Stone         return_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget());
129708709c0SSean Callanan 
130708709c0SSean Callanan     if (m_return_addr == LLDB_INVALID_ADDRESS)
131708709c0SSean Callanan       return;
132708709c0SSean Callanan 
133b9c1b51eSKate Stone     Breakpoint *return_bp = m_thread.CalculateTarget()
134b9c1b51eSKate Stone                                 ->CreateBreakpoint(m_return_addr, true, false)
135b9c1b51eSKate Stone                                 .get();
136b9c1b51eSKate Stone     if (return_bp != nullptr) {
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   }
219*4b36f791SVedant Kumar 
220*4b36f791SVedant Kumar   s->Printf("\n");
221*4b36f791SVedant Kumar   for (StackFrameSP frame_sp : m_stepped_past_frames) {
222*4b36f791SVedant Kumar     s->Printf("Stepped out past: ");
223*4b36f791SVedant Kumar     frame_sp->DumpUsingSettingsFormat(s);
224*4b36f791SVedant 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);
230a5ce6c88SJim Ingham   else if (m_step_through_inline_plan_sp)
231a5ce6c88SJim Ingham     return m_step_through_inline_plan_sp->ValidatePlan(error);
232b9c1b51eSKate Stone   else if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
233708709c0SSean Callanan     if (error)
234a5ce6c88SJim Ingham       error->PutCString("Could not create return address breakpoint.");
23530fdc8d8SChris Lattner     return false;
236b9c1b51eSKate Stone   } else
23730fdc8d8SChris Lattner     return true;
23830fdc8d8SChris Lattner }
23930fdc8d8SChris Lattner 
240b9c1b51eSKate Stone bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
24105097246SAdrian Prantl   // If the step out plan is done, then we just need to step through the
24205097246SAdrian Prantl   // inlined frame.
243b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
244e65b2cf2SEugene Zelenko     return m_step_out_to_inline_plan_sp->MischiefManaged();
245b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
246b9c1b51eSKate Stone     if (m_step_through_inline_plan_sp->MischiefManaged()) {
24773ca05a2SJim Ingham       CalculateReturnValue();
248a5ce6c88SJim Ingham       SetPlanComplete();
249a5ce6c88SJim Ingham       return true;
250b9c1b51eSKate Stone     } else
251a5ce6c88SJim Ingham       return false;
252b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
253e65b2cf2SEugene Zelenko     return m_step_out_further_plan_sp->MischiefManaged();
254a5ce6c88SJim Ingham   }
255a5ce6c88SJim Ingham 
256b9c1b51eSKate Stone   // We don't explain signals or breakpoints (breakpoints that handle stepping
25705097246SAdrian Prantl   // in or out will be handled by a child plan.
258a5ce6c88SJim Ingham 
25960c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
260b9c1b51eSKate Stone   if (stop_info_sp) {
261b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
262b9c1b51eSKate Stone     if (reason == eStopReasonBreakpoint) {
26305097246SAdrian Prantl       // If this is OUR breakpoint, we're fine, otherwise we don't know why
26405097246SAdrian Prantl       // this happened...
265b9c1b51eSKate Stone       BreakpointSiteSP site_sp(
266b9c1b51eSKate Stone           m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
267b9c1b51eSKate Stone               stop_info_sp->GetValue()));
268b9c1b51eSKate Stone       if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
269b5c0d1ccSJim Ingham         bool done;
270b5c0d1ccSJim Ingham 
271b5c0d1ccSJim Ingham         StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
272b5c0d1ccSJim Ingham 
273b5c0d1ccSJim Ingham         if (m_step_out_to_id == frame_zero_id)
274b5c0d1ccSJim Ingham           done = true;
275b9c1b51eSKate Stone         else if (m_step_out_to_id < frame_zero_id) {
276b5c0d1ccSJim Ingham           // Either we stepped past the breakpoint, or the stack ID calculation
277b5c0d1ccSJim Ingham           // was incorrect and we should probably stop.
278b5c0d1ccSJim Ingham           done = true;
279b9c1b51eSKate Stone         } else {
280e65b2cf2SEugene Zelenko           done = (m_immediate_step_from_id < frame_zero_id);
281b5c0d1ccSJim Ingham         }
282b5c0d1ccSJim Ingham 
283b9c1b51eSKate Stone         if (done) {
284b9c1b51eSKate Stone           if (InvokeShouldStopHereCallback(eFrameCompareOlder)) {
28573ca05a2SJim Ingham             CalculateReturnValue();
286481cef25SGreg Clayton             SetPlanComplete();
28773ca05a2SJim Ingham           }
2884b4b2478SJim Ingham         }
289481cef25SGreg Clayton 
290b9c1b51eSKate Stone         // If there was only one owner, then we're done.  But if we also hit
29105097246SAdrian Prantl         // some user breakpoint on our way out, we should mark ourselves as
29205097246SAdrian Prantl         // done, but also not claim to explain the stop, since it is more
29305097246SAdrian Prantl         // important to report the user breakpoint than the step out
29405097246SAdrian Prantl         // completion.
29530fdc8d8SChris Lattner 
296f4b47e15SGreg Clayton         if (site_sp->GetNumberOfOwners() == 1)
29730fdc8d8SChris Lattner           return true;
29830fdc8d8SChris Lattner       }
29930fdc8d8SChris Lattner       return false;
300b9c1b51eSKate Stone     } else if (IsUsuallyUnexplainedStopReason(reason))
30130fdc8d8SChris Lattner       return false;
3029b03fa0cSJim Ingham     else
30330fdc8d8SChris Lattner       return true;
30430fdc8d8SChris Lattner   }
30530fdc8d8SChris Lattner   return true;
30630fdc8d8SChris Lattner }
30730fdc8d8SChris Lattner 
308b9c1b51eSKate Stone bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
309a5ce6c88SJim Ingham   if (IsPlanComplete())
310a5ce6c88SJim Ingham     return true;
311b5c0d1ccSJim Ingham 
3124b4b2478SJim Ingham   bool done = false;
313b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
314b9c1b51eSKate Stone     if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
3154b4b2478SJim Ingham       // Now step through the inlined stack we are in:
316b9c1b51eSKate Stone       if (QueueInlinedStepPlan(true)) {
3174b4b2478SJim Ingham         // If we can't queue a plan to do this, then just call ourselves done.
3184b4b2478SJim Ingham         m_step_out_to_inline_plan_sp.reset();
3194b4b2478SJim Ingham         SetPlanComplete(false);
3204b4b2478SJim Ingham         return true;
321b9c1b51eSKate Stone       } else
3224b4b2478SJim Ingham         done = true;
323b9c1b51eSKate Stone     } else
3244b4b2478SJim Ingham       return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
325b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
3264b4b2478SJim Ingham     if (m_step_through_inline_plan_sp->MischiefManaged())
3274b4b2478SJim Ingham       done = true;
3284b4b2478SJim Ingham     else
3294b4b2478SJim Ingham       return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
330b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
3314b4b2478SJim Ingham     if (m_step_out_further_plan_sp->MischiefManaged())
3324b4b2478SJim Ingham       m_step_out_further_plan_sp.reset();
3334b4b2478SJim Ingham     else
3344b4b2478SJim Ingham       return m_step_out_further_plan_sp->ShouldStop(event_ptr);
3354b4b2478SJim Ingham   }
336b5c0d1ccSJim Ingham 
337b9c1b51eSKate Stone   if (!done) {
338b5c0d1ccSJim Ingham     StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
339e65b2cf2SEugene Zelenko     done = !(frame_zero_id < m_step_out_to_id);
3404b4b2478SJim Ingham   }
3414b4b2478SJim Ingham 
34205097246SAdrian Prantl   // The normal step out computations think we are done, so all we need to do
34305097246SAdrian Prantl   // is consult the ShouldStopHere, and we are done.
344b5c0d1ccSJim Ingham 
345b9c1b51eSKate Stone   if (done) {
346b9c1b51eSKate Stone     if (InvokeShouldStopHereCallback(eFrameCompareOlder)) {
34773ca05a2SJim Ingham       CalculateReturnValue();
348a5ce6c88SJim Ingham       SetPlanComplete();
349b9c1b51eSKate Stone     } else {
350b9c1b51eSKate Stone       m_step_out_further_plan_sp =
351b9c1b51eSKate Stone           QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder);
3524b4b2478SJim Ingham       done = false;
353a5ce6c88SJim Ingham     }
354a5ce6c88SJim Ingham   }
3554b4b2478SJim Ingham 
3564b4b2478SJim Ingham   return done;
357a5ce6c88SJim Ingham }
35830fdc8d8SChris Lattner 
359b9c1b51eSKate Stone bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
36030fdc8d8SChris Lattner 
361b9c1b51eSKate Stone StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
36230fdc8d8SChris Lattner 
363b9c1b51eSKate Stone bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
364b9c1b51eSKate Stone                                      bool current_plan) {
3654b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
366a5ce6c88SJim Ingham     return true;
367a5ce6c88SJim Ingham 
36830fdc8d8SChris Lattner   if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
36930fdc8d8SChris Lattner     return false;
37030fdc8d8SChris Lattner 
371b9c1b51eSKate Stone   if (current_plan) {
372b9c1b51eSKate Stone     Breakpoint *return_bp =
373b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
374e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
37530fdc8d8SChris Lattner       return_bp->SetEnabled(true);
37630fdc8d8SChris Lattner   }
37730fdc8d8SChris Lattner   return true;
37830fdc8d8SChris Lattner }
37930fdc8d8SChris Lattner 
380b9c1b51eSKate Stone bool ThreadPlanStepOut::WillStop() {
381b9c1b51eSKate Stone   if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
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(false);
386a5ce6c88SJim Ingham   }
387a5ce6c88SJim Ingham 
38830fdc8d8SChris Lattner   return true;
38930fdc8d8SChris Lattner }
39030fdc8d8SChris Lattner 
391b9c1b51eSKate Stone bool ThreadPlanStepOut::MischiefManaged() {
392b9c1b51eSKate Stone   if (IsPlanComplete()) {
39330fdc8d8SChris Lattner     // Did I reach my breakpoint?  If so I'm done.
39430fdc8d8SChris Lattner     //
395b9c1b51eSKate Stone     // I also check the stack depth, since if we've blown past the breakpoint
396b9c1b51eSKate Stone     // for some
397b9c1b51eSKate Stone     // reason and we're now stopping for some other reason altogether, then
39805097246SAdrian Prantl     // we're done with this step out operation.
39930fdc8d8SChris Lattner 
4005160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
40130fdc8d8SChris Lattner     if (log)
40230fdc8d8SChris Lattner       log->Printf("Completed step out plan.");
403b9c1b51eSKate Stone     if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
4041ac04c30SGreg Clayton       m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
40530fdc8d8SChris Lattner       m_return_bp_id = LLDB_INVALID_BREAK_ID;
406a5ce6c88SJim Ingham     }
407a5ce6c88SJim Ingham 
40830fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
40930fdc8d8SChris Lattner     return true;
410b9c1b51eSKate Stone   } else {
41130fdc8d8SChris Lattner     return false;
41230fdc8d8SChris Lattner   }
41330fdc8d8SChris Lattner }
41430fdc8d8SChris Lattner 
415b9c1b51eSKate Stone bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
416b9c1b51eSKate Stone   // Now figure out the range of this inlined block, and set up a "step through
41705097246SAdrian Prantl   // range" plan for that.  If we've been provided with a context, then use the
41805097246SAdrian Prantl   // block in that context.
419b57e4a1bSJason Molenda   StackFrameSP immediate_return_from_sp(m_thread.GetStackFrameAtIndex(0));
420a5ce6c88SJim Ingham   if (!immediate_return_from_sp)
421a5ce6c88SJim Ingham     return false;
422a5ce6c88SJim Ingham 
4235160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
424b9c1b51eSKate Stone   if (log) {
425a5ce6c88SJim Ingham     StreamString s;
426a5ce6c88SJim Ingham     immediate_return_from_sp->Dump(&s, true, false);
427a5ce6c88SJim Ingham     log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
428a5ce6c88SJim Ingham   }
429a5ce6c88SJim Ingham 
430a5ce6c88SJim Ingham   Block *from_block = immediate_return_from_sp->GetFrameBlock();
431b9c1b51eSKate Stone   if (from_block) {
432a5ce6c88SJim Ingham     Block *inlined_block = from_block->GetContainingInlinedBlock();
433b9c1b51eSKate Stone     if (inlined_block) {
434a5ce6c88SJim Ingham       size_t num_ranges = inlined_block->GetNumRanges();
435a5ce6c88SJim Ingham       AddressRange inline_range;
436b9c1b51eSKate Stone       if (inlined_block->GetRangeAtIndex(0, inline_range)) {
437a5ce6c88SJim Ingham         SymbolContext inlined_sc;
438a5ce6c88SJim Ingham         inlined_block->CalculateSymbolContext(&inlined_sc);
4395f1a4e1fSJim Ingham         inlined_sc.target_sp = GetTarget().shared_from_this();
440b9c1b51eSKate Stone         RunMode run_mode =
441b9c1b51eSKate Stone             m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
4424b4b2478SJim Ingham         const LazyBool avoid_no_debug = eLazyBoolNo;
4432bdbfd50SJim Ingham 
444b9c1b51eSKate Stone         m_step_through_inline_plan_sp.reset(new ThreadPlanStepOverRange(
445b9c1b51eSKate Stone             m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug));
446b9c1b51eSKate Stone         ThreadPlanStepOverRange *step_through_inline_plan_ptr =
447b9c1b51eSKate Stone             static_cast<ThreadPlanStepOverRange *>(
448b9c1b51eSKate Stone                 m_step_through_inline_plan_sp.get());
4492bdbfd50SJim Ingham         m_step_through_inline_plan_sp->SetPrivate(true);
4502bdbfd50SJim Ingham 
451a5ce6c88SJim Ingham         step_through_inline_plan_ptr->SetOkayToDiscard(true);
452a5ce6c88SJim Ingham         StreamString errors;
453b9c1b51eSKate Stone         if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
454a5ce6c88SJim Ingham           // FIXME: Log this failure.
455a5ce6c88SJim Ingham           delete step_through_inline_plan_ptr;
456a5ce6c88SJim Ingham           return false;
457a5ce6c88SJim Ingham         }
458a5ce6c88SJim Ingham 
459b9c1b51eSKate Stone         for (size_t i = 1; i < num_ranges; i++) {
460a5ce6c88SJim Ingham           if (inlined_block->GetRangeAtIndex(i, inline_range))
461a5ce6c88SJim Ingham             step_through_inline_plan_ptr->AddRange(inline_range);
462a5ce6c88SJim Ingham         }
4632bdbfd50SJim Ingham 
464a5ce6c88SJim Ingham         if (queue_now)
465a5ce6c88SJim Ingham           m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
466a5ce6c88SJim Ingham         return true;
467a5ce6c88SJim Ingham       }
468a5ce6c88SJim Ingham     }
469a5ce6c88SJim Ingham   }
470a5ce6c88SJim Ingham 
471a5ce6c88SJim Ingham   return false;
472a5ce6c88SJim Ingham }
47373ca05a2SJim Ingham 
474b9c1b51eSKate Stone void ThreadPlanStepOut::CalculateReturnValue() {
47573ca05a2SJim Ingham   if (m_return_valobj_sp)
47673ca05a2SJim Ingham     return;
47773ca05a2SJim Ingham 
478b612ac37SJim Ingham   if (!m_calculate_return_value)
479b612ac37SJim Ingham     return;
480b612ac37SJim Ingham 
481b9c1b51eSKate Stone   if (m_immediate_step_from_function != nullptr) {
482b9c1b51eSKate Stone     CompilerType return_compiler_type =
483b9c1b51eSKate Stone         m_immediate_step_from_function->GetCompilerType()
484b9c1b51eSKate Stone             .GetFunctionReturnType();
485b9c1b51eSKate Stone     if (return_compiler_type) {
4861ac04c30SGreg Clayton       lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
48773ca05a2SJim Ingham       if (abi_sp)
488b9c1b51eSKate Stone         m_return_valobj_sp =
489b9c1b51eSKate Stone             abi_sp->GetReturnValueObject(m_thread, return_compiler_type);
49073ca05a2SJim Ingham     }
49173ca05a2SJim Ingham   }
49273ca05a2SJim Ingham }
49364e7ead1SJim Ingham 
494b9c1b51eSKate Stone bool ThreadPlanStepOut::IsPlanStale() {
49505097246SAdrian Prantl   // If we are still lower on the stack than the frame we are returning to,
49605097246SAdrian Prantl   // then there's something for us to do.  Otherwise, we're stale.
49764e7ead1SJim Ingham 
49864e7ead1SJim Ingham   StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
499e65b2cf2SEugene Zelenko   return !(frame_zero_id < m_step_out_to_id);
50064e7ead1SJim Ingham }
501