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 
10e65b2cf2SEugene Zelenko #include "lldb/Target/ThreadPlanStepOut.h"
1130fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h"
1273ca05a2SJim Ingham #include "lldb/Core/Value.h"
1373ca05a2SJim Ingham #include "lldb/Core/ValueObjectConstResult.h"
141f746071SGreg Clayton #include "lldb/Symbol/Block.h"
151f746071SGreg Clayton #include "lldb/Symbol/Function.h"
16fd4cea53SJason Molenda #include "lldb/Symbol/Symbol.h"
171f746071SGreg Clayton #include "lldb/Symbol/Type.h"
1832abc6edSZachary Turner #include "lldb/Target/ABI.h"
1930fdc8d8SChris Lattner #include "lldb/Target/Process.h"
2030fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
21f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
2230fdc8d8SChris Lattner #include "lldb/Target/Target.h"
23a5ce6c88SJim Ingham #include "lldb/Target/ThreadPlanStepOverRange.h"
244b4b2478SJim Ingham #include "lldb/Target/ThreadPlanStepThrough.h"
256f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2630fdc8d8SChris Lattner 
2730fdc8d8SChris Lattner using namespace lldb;
2830fdc8d8SChris Lattner using namespace lldb_private;
2930fdc8d8SChris Lattner 
304b4b2478SJim Ingham uint32_t ThreadPlanStepOut::s_default_flag_values = 0;
314b4b2478SJim Ingham 
3230fdc8d8SChris Lattner //----------------------------------------------------------------------
3330fdc8d8SChris Lattner // ThreadPlanStepOut: Step out of the current frame
3430fdc8d8SChris Lattner //----------------------------------------------------------------------
35b9c1b51eSKate Stone ThreadPlanStepOut::ThreadPlanStepOut(
36b9c1b51eSKate Stone     Thread &thread, SymbolContext *context, bool first_insn, bool stop_others,
37b9c1b51eSKate Stone     Vote stop_vote, Vote run_vote, uint32_t frame_idx,
38fd4cea53SJason Molenda     LazyBool step_out_avoids_code_without_debug_info,
39b9c1b51eSKate Stone     bool continue_to_next_branch, bool gather_return_value)
40b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindStepOut, "Step out", thread, stop_vote,
41b9c1b51eSKate Stone                  run_vote),
42b9c1b51eSKate Stone       ThreadPlanShouldStopHere(this), m_step_from_insn(LLDB_INVALID_ADDRESS),
431ee0d4f7SBenjamin Kramer       m_return_bp_id(LLDB_INVALID_BREAK_ID),
44b9c1b51eSKate Stone       m_return_addr(LLDB_INVALID_ADDRESS), m_stop_others(stop_others),
45b612ac37SJim Ingham       m_immediate_step_from_function(nullptr),
46b9c1b51eSKate Stone       m_calculate_return_value(gather_return_value) {
474b36f791SVedant Kumar   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
484b4b2478SJim Ingham   SetFlagsToDefault();
494b4b2478SJim Ingham   SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
504b4b2478SJim Ingham 
5130fdc8d8SChris Lattner   m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
5230fdc8d8SChris Lattner 
534b36f791SVedant Kumar   uint32_t return_frame_index = frame_idx + 1;
544b36f791SVedant Kumar   StackFrameSP return_frame_sp(
554b36f791SVedant Kumar       m_thread.GetStackFrameAtIndex(return_frame_index));
56b9c1b51eSKate Stone   StackFrameSP immediate_return_from_sp(
57b9c1b51eSKate Stone       m_thread.GetStackFrameAtIndex(frame_idx));
58a5ce6c88SJim Ingham 
59632d2f72SSean Callanan   if (!return_frame_sp || !immediate_return_from_sp)
60632d2f72SSean Callanan     return; // we can't do anything here.  ValidatePlan() will return false.
61632d2f72SSean Callanan 
624b36f791SVedant Kumar   // While stepping out, behave as-if artificial frames are not present.
634b36f791SVedant Kumar   while (return_frame_sp->IsArtificial()) {
644b36f791SVedant Kumar     m_stepped_past_frames.push_back(return_frame_sp);
654b36f791SVedant Kumar 
664b36f791SVedant Kumar     ++return_frame_index;
674b36f791SVedant Kumar     return_frame_sp = m_thread.GetStackFrameAtIndex(return_frame_index);
684b36f791SVedant Kumar 
694b36f791SVedant Kumar     // We never expect to see an artificial frame without a regular ancestor.
704b36f791SVedant Kumar     // If this happens, log the issue and defensively refuse to step out.
714b36f791SVedant Kumar     if (!return_frame_sp) {
724b36f791SVedant Kumar       LLDB_LOG(log, "Can't step out of frame with artificial ancestors");
734b36f791SVedant Kumar       return;
744b36f791SVedant Kumar     }
754b36f791SVedant Kumar   }
764b36f791SVedant Kumar 
77b5c0d1ccSJim Ingham   m_step_out_to_id = return_frame_sp->GetStackID();
78b5c0d1ccSJim Ingham   m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
79a5ce6c88SJim Ingham 
8005097246SAdrian Prantl   // If the frame directly below the one we are returning to is inlined, we
8105097246SAdrian Prantl   // have to be a little more careful.  It is non-trivial to determine the real
8205097246SAdrian Prantl   // "return code address" for an inlined frame, so we have to work our way to
8305097246SAdrian Prantl   // that frame and then step out.
844b36f791SVedant Kumar   if (immediate_return_from_sp->IsInlined()) {
85b9c1b51eSKate Stone     if (frame_idx > 0) {
86b9c1b51eSKate Stone       // First queue a plan that gets us to this inlined frame, and when we get
8705097246SAdrian Prantl       // there we'll queue a second plan that walks us out of this frame.
88b9c1b51eSKate Stone       m_step_out_to_inline_plan_sp.reset(new ThreadPlanStepOut(
89b9c1b51eSKate Stone           m_thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
90b9c1b51eSKate Stone           frame_idx - 1, eLazyBoolNo, continue_to_next_branch));
91b9c1b51eSKate Stone       static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())
92b9c1b51eSKate Stone           ->SetShouldStopHereCallbacks(nullptr, nullptr);
932bdbfd50SJim Ingham       m_step_out_to_inline_plan_sp->SetPrivate(true);
94b9c1b51eSKate Stone     } else {
9505097246SAdrian Prantl       // If we're already at the inlined frame we're stepping through, then
9605097246SAdrian Prantl       // just do that now.
97a5ce6c88SJim Ingham       QueueInlinedStepPlan(false);
98a5ce6c88SJim Ingham     }
994b36f791SVedant Kumar   } else {
10030fdc8d8SChris Lattner     // Find the return address and set a breakpoint there:
10130fdc8d8SChris Lattner     // FIXME - can we do this more securely if we know first_insn?
10230fdc8d8SChris Lattner 
103fd4cea53SJason Molenda     Address return_address(return_frame_sp->GetFrameCodeAddress());
104b9c1b51eSKate Stone     if (continue_to_next_branch) {
105fd4cea53SJason Molenda       SymbolContext return_address_sc;
106fd4cea53SJason Molenda       AddressRange range;
107fd4cea53SJason Molenda       Address return_address_decr_pc = return_address;
108fd4cea53SJason Molenda       if (return_address_decr_pc.GetOffset() > 0)
109fd4cea53SJason Molenda         return_address_decr_pc.Slide(-1);
110fd4cea53SJason Molenda 
111b9c1b51eSKate Stone       return_address_decr_pc.CalculateSymbolContext(
112b9c1b51eSKate Stone           &return_address_sc, lldb::eSymbolContextLineEntry);
113b9c1b51eSKate Stone       if (return_address_sc.line_entry.IsValid()) {
114b9c1b51eSKate Stone         range =
115b9c1b51eSKate Stone             return_address_sc.line_entry.GetSameLineContiguousAddressRange();
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();
132*e103ae92SJonas Devlieghere 
133b9c1b51eSKate Stone     if (return_bp != nullptr) {
134*e103ae92SJonas Devlieghere       if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
135*e103ae92SJonas 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);
229*e103ae92SJonas Devlieghere 
230*e103ae92SJonas Devlieghere   if (m_step_through_inline_plan_sp)
231a5ce6c88SJim Ingham     return m_step_through_inline_plan_sp->ValidatePlan(error);
232*e103ae92SJonas Devlieghere 
233*e103ae92SJonas Devlieghere   if (m_could_not_resolve_hw_bp) {
234*e103ae92SJonas Devlieghere     if (error)
235*e103ae92SJonas Devlieghere       error->PutCString(
236*e103ae92SJonas Devlieghere           "Could not create hardware breakpoint for thread plan.");
237*e103ae92SJonas Devlieghere     return false;
238*e103ae92SJonas Devlieghere   }
239*e103ae92SJonas Devlieghere 
240*e103ae92SJonas 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;
244*e103ae92SJonas Devlieghere   }
245*e103ae92SJonas 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) {
293*e103ae92SJonas 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) {
355*e103ae92SJonas Devlieghere     if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
35673ca05a2SJim Ingham       CalculateReturnValue();
357a5ce6c88SJim Ingham       SetPlanComplete();
358b9c1b51eSKate Stone     } else {
359b9c1b51eSKate Stone       m_step_out_further_plan_sp =
360*e103ae92SJonas 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)
41130fdc8d8SChris Lattner       log->Printf("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);
436a5ce6c88SJim Ingham     log->Printf("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 
453b9c1b51eSKate Stone         m_step_through_inline_plan_sp.reset(new ThreadPlanStepOverRange(
454b9c1b51eSKate Stone             m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug));
455b9c1b51eSKate Stone         ThreadPlanStepOverRange *step_through_inline_plan_ptr =
456b9c1b51eSKate Stone             static_cast<ThreadPlanStepOverRange *>(
457b9c1b51eSKate Stone                 m_step_through_inline_plan_sp.get());
4582bdbfd50SJim Ingham         m_step_through_inline_plan_sp->SetPrivate(true);
4592bdbfd50SJim Ingham 
460a5ce6c88SJim Ingham         step_through_inline_plan_ptr->SetOkayToDiscard(true);
461a5ce6c88SJim Ingham         StreamString errors;
462b9c1b51eSKate Stone         if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
463a5ce6c88SJim Ingham           // FIXME: Log this failure.
464a5ce6c88SJim Ingham           delete step_through_inline_plan_ptr;
465a5ce6c88SJim Ingham           return false;
466a5ce6c88SJim Ingham         }
467a5ce6c88SJim Ingham 
468b9c1b51eSKate Stone         for (size_t i = 1; i < num_ranges; i++) {
469a5ce6c88SJim Ingham           if (inlined_block->GetRangeAtIndex(i, inline_range))
470a5ce6c88SJim Ingham             step_through_inline_plan_ptr->AddRange(inline_range);
471a5ce6c88SJim Ingham         }
4722bdbfd50SJim Ingham 
473a5ce6c88SJim Ingham         if (queue_now)
474a5ce6c88SJim Ingham           m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
475a5ce6c88SJim Ingham         return true;
476a5ce6c88SJim Ingham       }
477a5ce6c88SJim Ingham     }
478a5ce6c88SJim Ingham   }
479a5ce6c88SJim Ingham 
480a5ce6c88SJim Ingham   return false;
481a5ce6c88SJim Ingham }
48273ca05a2SJim Ingham 
483b9c1b51eSKate Stone void ThreadPlanStepOut::CalculateReturnValue() {
48473ca05a2SJim Ingham   if (m_return_valobj_sp)
48573ca05a2SJim Ingham     return;
48673ca05a2SJim Ingham 
487b612ac37SJim Ingham   if (!m_calculate_return_value)
488b612ac37SJim Ingham     return;
489b612ac37SJim Ingham 
490b9c1b51eSKate Stone   if (m_immediate_step_from_function != nullptr) {
491b9c1b51eSKate Stone     CompilerType return_compiler_type =
492b9c1b51eSKate Stone         m_immediate_step_from_function->GetCompilerType()
493b9c1b51eSKate Stone             .GetFunctionReturnType();
494b9c1b51eSKate Stone     if (return_compiler_type) {
4951ac04c30SGreg Clayton       lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
49673ca05a2SJim Ingham       if (abi_sp)
497b9c1b51eSKate Stone         m_return_valobj_sp =
498b9c1b51eSKate Stone             abi_sp->GetReturnValueObject(m_thread, return_compiler_type);
49973ca05a2SJim Ingham     }
50073ca05a2SJim Ingham   }
50173ca05a2SJim Ingham }
50264e7ead1SJim Ingham 
503b9c1b51eSKate Stone bool ThreadPlanStepOut::IsPlanStale() {
50405097246SAdrian Prantl   // If we are still lower on the stack than the frame we are returning to,
50505097246SAdrian Prantl   // then there's something for us to do.  Otherwise, we're stale.
50664e7ead1SJim Ingham 
50764e7ead1SJim Ingham   StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
508e65b2cf2SEugene Zelenko   return !(frame_zero_id < m_step_out_to_id);
50964e7ead1SJim Ingham }
510