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) {
514b4b2478SJim Ingham   SetFlagsToDefault();
524b4b2478SJim Ingham   SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
534b4b2478SJim Ingham 
5430fdc8d8SChris Lattner   m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
5530fdc8d8SChris Lattner 
56b57e4a1bSJason Molenda   StackFrameSP return_frame_sp(m_thread.GetStackFrameAtIndex(frame_idx + 1));
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 
63b5c0d1ccSJim Ingham   m_step_out_to_id = return_frame_sp->GetStackID();
64b5c0d1ccSJim Ingham   m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
65a5ce6c88SJim Ingham 
66*05097246SAdrian Prantl   // If the frame directly below the one we are returning to is inlined, we
67*05097246SAdrian Prantl   // have to be a little more careful.  It is non-trivial to determine the real
68*05097246SAdrian Prantl   // "return code address" for an inlined frame, so we have to work our way to
69*05097246SAdrian Prantl   // that frame and then step out.
70b9c1b51eSKate Stone   if (immediate_return_from_sp && immediate_return_from_sp->IsInlined()) {
71b9c1b51eSKate Stone     if (frame_idx > 0) {
72b9c1b51eSKate Stone       // First queue a plan that gets us to this inlined frame, and when we get
73*05097246SAdrian Prantl       // there we'll queue a second plan that walks us out of this frame.
74b9c1b51eSKate Stone       m_step_out_to_inline_plan_sp.reset(new ThreadPlanStepOut(
75b9c1b51eSKate Stone           m_thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
76b9c1b51eSKate Stone           frame_idx - 1, eLazyBoolNo, continue_to_next_branch));
77b9c1b51eSKate Stone       static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())
78b9c1b51eSKate Stone           ->SetShouldStopHereCallbacks(nullptr, nullptr);
792bdbfd50SJim Ingham       m_step_out_to_inline_plan_sp->SetPrivate(true);
80b9c1b51eSKate Stone     } else {
81*05097246SAdrian Prantl       // If we're already at the inlined frame we're stepping through, then
82*05097246SAdrian Prantl       // just do that now.
83a5ce6c88SJim Ingham       QueueInlinedStepPlan(false);
84a5ce6c88SJim Ingham     }
85b9c1b51eSKate Stone   } else if (return_frame_sp) {
8630fdc8d8SChris Lattner     // Find the return address and set a breakpoint there:
8730fdc8d8SChris Lattner     // FIXME - can we do this more securely if we know first_insn?
8830fdc8d8SChris Lattner 
89fd4cea53SJason Molenda     Address return_address(return_frame_sp->GetFrameCodeAddress());
90b9c1b51eSKate Stone     if (continue_to_next_branch) {
91fd4cea53SJason Molenda       SymbolContext return_address_sc;
92fd4cea53SJason Molenda       AddressRange range;
93fd4cea53SJason Molenda       Address return_address_decr_pc = return_address;
94fd4cea53SJason Molenda       if (return_address_decr_pc.GetOffset() > 0)
95fd4cea53SJason Molenda         return_address_decr_pc.Slide(-1);
96fd4cea53SJason Molenda 
97b9c1b51eSKate Stone       return_address_decr_pc.CalculateSymbolContext(
98b9c1b51eSKate Stone           &return_address_sc, lldb::eSymbolContextLineEntry);
99b9c1b51eSKate Stone       if (return_address_sc.line_entry.IsValid()) {
100b9c1b51eSKate Stone         range =
101b9c1b51eSKate Stone             return_address_sc.line_entry.GetSameLineContiguousAddressRange();
102b9c1b51eSKate Stone         if (range.GetByteSize() > 0) {
103b9c1b51eSKate Stone           return_address =
104b9c1b51eSKate Stone               m_thread.GetProcess()->AdvanceAddressToNextBranchInstruction(
105b9c1b51eSKate Stone                   return_address, range);
106fd4cea53SJason Molenda         }
107fd4cea53SJason Molenda       }
108fd4cea53SJason Molenda     }
109b9c1b51eSKate Stone     m_return_addr =
110b9c1b51eSKate Stone         return_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget());
111708709c0SSean Callanan 
112708709c0SSean Callanan     if (m_return_addr == LLDB_INVALID_ADDRESS)
113708709c0SSean Callanan       return;
114708709c0SSean Callanan 
115b9c1b51eSKate Stone     Breakpoint *return_bp = m_thread.CalculateTarget()
116b9c1b51eSKate Stone                                 ->CreateBreakpoint(m_return_addr, true, false)
117b9c1b51eSKate Stone                                 .get();
118b9c1b51eSKate Stone     if (return_bp != nullptr) {
11930fdc8d8SChris Lattner       return_bp->SetThreadID(m_thread.GetID());
12030fdc8d8SChris Lattner       m_return_bp_id = return_bp->GetID();
1212995077dSJim Ingham       return_bp->SetBreakpointKind("step-out");
12230fdc8d8SChris Lattner     }
12373ca05a2SJim Ingham 
124b9c1b51eSKate Stone     if (immediate_return_from_sp) {
125b9c1b51eSKate Stone       const SymbolContext &sc =
126b9c1b51eSKate Stone           immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
127b9c1b51eSKate Stone       if (sc.function) {
12873ca05a2SJim Ingham         m_immediate_step_from_function = sc.function;
12973ca05a2SJim Ingham       }
13073ca05a2SJim Ingham     }
13130fdc8d8SChris Lattner   }
132a5ce6c88SJim Ingham }
133a5ce6c88SJim Ingham 
134b9c1b51eSKate Stone void ThreadPlanStepOut::SetupAvoidNoDebug(
135b9c1b51eSKate Stone     LazyBool step_out_avoids_code_without_debug_info) {
1364b4b2478SJim Ingham   bool avoid_nodebug = true;
137b9c1b51eSKate Stone   switch (step_out_avoids_code_without_debug_info) {
1384b4b2478SJim Ingham   case eLazyBoolYes:
1394b4b2478SJim Ingham     avoid_nodebug = true;
1404b4b2478SJim Ingham     break;
1414b4b2478SJim Ingham   case eLazyBoolNo:
1424b4b2478SJim Ingham     avoid_nodebug = false;
1434b4b2478SJim Ingham     break;
1444b4b2478SJim Ingham   case eLazyBoolCalculate:
1454b4b2478SJim Ingham     avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
1464b4b2478SJim Ingham     break;
1474b4b2478SJim Ingham   }
1484b4b2478SJim Ingham   if (avoid_nodebug)
1494b4b2478SJim Ingham     GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1504b4b2478SJim Ingham   else
1514b4b2478SJim Ingham     GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1524b4b2478SJim Ingham }
1534b4b2478SJim Ingham 
154b9c1b51eSKate Stone void ThreadPlanStepOut::DidPush() {
1554b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
1564b4b2478SJim Ingham     m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
157a5ce6c88SJim Ingham   else if (m_step_through_inline_plan_sp)
158a5ce6c88SJim Ingham     m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
15930fdc8d8SChris Lattner }
16030fdc8d8SChris Lattner 
161b9c1b51eSKate Stone ThreadPlanStepOut::~ThreadPlanStepOut() {
16230fdc8d8SChris Lattner   if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
1631ac04c30SGreg Clayton     m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
16430fdc8d8SChris Lattner }
16530fdc8d8SChris Lattner 
166b9c1b51eSKate Stone void ThreadPlanStepOut::GetDescription(Stream *s,
167b9c1b51eSKate Stone                                        lldb::DescriptionLevel level) {
16830fdc8d8SChris Lattner   if (level == lldb::eDescriptionLevelBrief)
16930fdc8d8SChris Lattner     s->Printf("step out");
170b9c1b51eSKate Stone   else {
1714b4b2478SJim Ingham     if (m_step_out_to_inline_plan_sp)
172b5c0d1ccSJim Ingham       s->Printf("Stepping out to inlined frame so we can walk through it.");
173a5ce6c88SJim Ingham     else if (m_step_through_inline_plan_sp)
174a5ce6c88SJim Ingham       s->Printf("Stepping out by stepping through inlined function.");
175b9c1b51eSKate Stone     else {
1762bdbfd50SJim Ingham       s->Printf("Stepping out from ");
1772bdbfd50SJim Ingham       Address tmp_address;
178b9c1b51eSKate Stone       if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) {
179b9c1b51eSKate Stone         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
180b9c1b51eSKate Stone                          Address::DumpStyleLoadAddress);
181b9c1b51eSKate Stone       } else {
1822bdbfd50SJim Ingham         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn);
1832bdbfd50SJim Ingham       }
1842bdbfd50SJim Ingham 
185b9c1b51eSKate Stone       // FIXME: find some useful way to present the m_return_id, since there may
186b9c1b51eSKate Stone       // be multiple copies of the
1872bdbfd50SJim Ingham       // same function on the stack.
1882bdbfd50SJim Ingham 
1892bdbfd50SJim Ingham       s->Printf(" returning to frame at ");
190b9c1b51eSKate Stone       if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) {
191b9c1b51eSKate Stone         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
192b9c1b51eSKate Stone                          Address::DumpStyleLoadAddress);
193b9c1b51eSKate Stone       } else {
1942bdbfd50SJim Ingham         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
1952bdbfd50SJim Ingham       }
1962bdbfd50SJim Ingham 
1972bdbfd50SJim Ingham       if (level == eDescriptionLevelVerbose)
1982bdbfd50SJim Ingham         s->Printf(" using breakpoint site %d", m_return_bp_id);
1992bdbfd50SJim Ingham     }
20030fdc8d8SChris Lattner   }
20130fdc8d8SChris Lattner }
20230fdc8d8SChris Lattner 
203b9c1b51eSKate Stone bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
2044b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
2054b4b2478SJim Ingham     return m_step_out_to_inline_plan_sp->ValidatePlan(error);
206a5ce6c88SJim Ingham   else if (m_step_through_inline_plan_sp)
207a5ce6c88SJim Ingham     return m_step_through_inline_plan_sp->ValidatePlan(error);
208b9c1b51eSKate Stone   else if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
209708709c0SSean Callanan     if (error)
210a5ce6c88SJim Ingham       error->PutCString("Could not create return address breakpoint.");
21130fdc8d8SChris Lattner     return false;
212b9c1b51eSKate Stone   } else
21330fdc8d8SChris Lattner     return true;
21430fdc8d8SChris Lattner }
21530fdc8d8SChris Lattner 
216b9c1b51eSKate Stone bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
217*05097246SAdrian Prantl   // If the step out plan is done, then we just need to step through the
218*05097246SAdrian Prantl   // inlined frame.
219b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
220e65b2cf2SEugene Zelenko     return m_step_out_to_inline_plan_sp->MischiefManaged();
221b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
222b9c1b51eSKate Stone     if (m_step_through_inline_plan_sp->MischiefManaged()) {
22373ca05a2SJim Ingham       CalculateReturnValue();
224a5ce6c88SJim Ingham       SetPlanComplete();
225a5ce6c88SJim Ingham       return true;
226b9c1b51eSKate Stone     } else
227a5ce6c88SJim Ingham       return false;
228b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
229e65b2cf2SEugene Zelenko     return m_step_out_further_plan_sp->MischiefManaged();
230a5ce6c88SJim Ingham   }
231a5ce6c88SJim Ingham 
232b9c1b51eSKate Stone   // We don't explain signals or breakpoints (breakpoints that handle stepping
233*05097246SAdrian Prantl   // in or out will be handled by a child plan.
234a5ce6c88SJim Ingham 
23560c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
236b9c1b51eSKate Stone   if (stop_info_sp) {
237b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
238b9c1b51eSKate Stone     if (reason == eStopReasonBreakpoint) {
239*05097246SAdrian Prantl       // If this is OUR breakpoint, we're fine, otherwise we don't know why
240*05097246SAdrian Prantl       // this happened...
241b9c1b51eSKate Stone       BreakpointSiteSP site_sp(
242b9c1b51eSKate Stone           m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
243b9c1b51eSKate Stone               stop_info_sp->GetValue()));
244b9c1b51eSKate Stone       if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
245b5c0d1ccSJim Ingham         bool done;
246b5c0d1ccSJim Ingham 
247b5c0d1ccSJim Ingham         StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
248b5c0d1ccSJim Ingham 
249b5c0d1ccSJim Ingham         if (m_step_out_to_id == frame_zero_id)
250b5c0d1ccSJim Ingham           done = true;
251b9c1b51eSKate Stone         else if (m_step_out_to_id < frame_zero_id) {
252b5c0d1ccSJim Ingham           // Either we stepped past the breakpoint, or the stack ID calculation
253b5c0d1ccSJim Ingham           // was incorrect and we should probably stop.
254b5c0d1ccSJim Ingham           done = true;
255b9c1b51eSKate Stone         } else {
256e65b2cf2SEugene Zelenko           done = (m_immediate_step_from_id < frame_zero_id);
257b5c0d1ccSJim Ingham         }
258b5c0d1ccSJim Ingham 
259b9c1b51eSKate Stone         if (done) {
260b9c1b51eSKate Stone           if (InvokeShouldStopHereCallback(eFrameCompareOlder)) {
26173ca05a2SJim Ingham             CalculateReturnValue();
262481cef25SGreg Clayton             SetPlanComplete();
26373ca05a2SJim Ingham           }
2644b4b2478SJim Ingham         }
265481cef25SGreg Clayton 
266b9c1b51eSKate Stone         // If there was only one owner, then we're done.  But if we also hit
267*05097246SAdrian Prantl         // some user breakpoint on our way out, we should mark ourselves as
268*05097246SAdrian Prantl         // done, but also not claim to explain the stop, since it is more
269*05097246SAdrian Prantl         // important to report the user breakpoint than the step out
270*05097246SAdrian Prantl         // completion.
27130fdc8d8SChris Lattner 
272f4b47e15SGreg Clayton         if (site_sp->GetNumberOfOwners() == 1)
27330fdc8d8SChris Lattner           return true;
27430fdc8d8SChris Lattner       }
27530fdc8d8SChris Lattner       return false;
276b9c1b51eSKate Stone     } else if (IsUsuallyUnexplainedStopReason(reason))
27730fdc8d8SChris Lattner       return false;
2789b03fa0cSJim Ingham     else
27930fdc8d8SChris Lattner       return true;
28030fdc8d8SChris Lattner   }
28130fdc8d8SChris Lattner   return true;
28230fdc8d8SChris Lattner }
28330fdc8d8SChris Lattner 
284b9c1b51eSKate Stone bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
285a5ce6c88SJim Ingham   if (IsPlanComplete())
286a5ce6c88SJim Ingham     return true;
287b5c0d1ccSJim Ingham 
2884b4b2478SJim Ingham   bool done = false;
289b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
290b9c1b51eSKate Stone     if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
2914b4b2478SJim Ingham       // Now step through the inlined stack we are in:
292b9c1b51eSKate Stone       if (QueueInlinedStepPlan(true)) {
2934b4b2478SJim Ingham         // If we can't queue a plan to do this, then just call ourselves done.
2944b4b2478SJim Ingham         m_step_out_to_inline_plan_sp.reset();
2954b4b2478SJim Ingham         SetPlanComplete(false);
2964b4b2478SJim Ingham         return true;
297b9c1b51eSKate Stone       } else
2984b4b2478SJim Ingham         done = true;
299b9c1b51eSKate Stone     } else
3004b4b2478SJim Ingham       return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
301b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
3024b4b2478SJim Ingham     if (m_step_through_inline_plan_sp->MischiefManaged())
3034b4b2478SJim Ingham       done = true;
3044b4b2478SJim Ingham     else
3054b4b2478SJim Ingham       return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
306b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
3074b4b2478SJim Ingham     if (m_step_out_further_plan_sp->MischiefManaged())
3084b4b2478SJim Ingham       m_step_out_further_plan_sp.reset();
3094b4b2478SJim Ingham     else
3104b4b2478SJim Ingham       return m_step_out_further_plan_sp->ShouldStop(event_ptr);
3114b4b2478SJim Ingham   }
312b5c0d1ccSJim Ingham 
313b9c1b51eSKate Stone   if (!done) {
314b5c0d1ccSJim Ingham     StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
315e65b2cf2SEugene Zelenko     done = !(frame_zero_id < m_step_out_to_id);
3164b4b2478SJim Ingham   }
3174b4b2478SJim Ingham 
318*05097246SAdrian Prantl   // The normal step out computations think we are done, so all we need to do
319*05097246SAdrian Prantl   // is consult the ShouldStopHere, and we are done.
320b5c0d1ccSJim Ingham 
321b9c1b51eSKate Stone   if (done) {
322b9c1b51eSKate Stone     if (InvokeShouldStopHereCallback(eFrameCompareOlder)) {
32373ca05a2SJim Ingham       CalculateReturnValue();
324a5ce6c88SJim Ingham       SetPlanComplete();
325b9c1b51eSKate Stone     } else {
326b9c1b51eSKate Stone       m_step_out_further_plan_sp =
327b9c1b51eSKate Stone           QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder);
3284b4b2478SJim Ingham       done = false;
329a5ce6c88SJim Ingham     }
330a5ce6c88SJim Ingham   }
3314b4b2478SJim Ingham 
3324b4b2478SJim Ingham   return done;
333a5ce6c88SJim Ingham }
33430fdc8d8SChris Lattner 
335b9c1b51eSKate Stone bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
33630fdc8d8SChris Lattner 
337b9c1b51eSKate Stone StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
33830fdc8d8SChris Lattner 
339b9c1b51eSKate Stone bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
340b9c1b51eSKate Stone                                      bool current_plan) {
3414b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
342a5ce6c88SJim Ingham     return true;
343a5ce6c88SJim Ingham 
34430fdc8d8SChris Lattner   if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
34530fdc8d8SChris Lattner     return false;
34630fdc8d8SChris Lattner 
347b9c1b51eSKate Stone   if (current_plan) {
348b9c1b51eSKate Stone     Breakpoint *return_bp =
349b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
350e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
35130fdc8d8SChris Lattner       return_bp->SetEnabled(true);
35230fdc8d8SChris Lattner   }
35330fdc8d8SChris Lattner   return true;
35430fdc8d8SChris Lattner }
35530fdc8d8SChris Lattner 
356b9c1b51eSKate Stone bool ThreadPlanStepOut::WillStop() {
357b9c1b51eSKate Stone   if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
358b9c1b51eSKate Stone     Breakpoint *return_bp =
359b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
360e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
36130fdc8d8SChris Lattner       return_bp->SetEnabled(false);
362a5ce6c88SJim Ingham   }
363a5ce6c88SJim Ingham 
36430fdc8d8SChris Lattner   return true;
36530fdc8d8SChris Lattner }
36630fdc8d8SChris Lattner 
367b9c1b51eSKate Stone bool ThreadPlanStepOut::MischiefManaged() {
368b9c1b51eSKate Stone   if (IsPlanComplete()) {
36930fdc8d8SChris Lattner     // Did I reach my breakpoint?  If so I'm done.
37030fdc8d8SChris Lattner     //
371b9c1b51eSKate Stone     // I also check the stack depth, since if we've blown past the breakpoint
372b9c1b51eSKate Stone     // for some
373b9c1b51eSKate Stone     // reason and we're now stopping for some other reason altogether, then
374*05097246SAdrian Prantl     // we're done with this step out operation.
37530fdc8d8SChris Lattner 
3765160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
37730fdc8d8SChris Lattner     if (log)
37830fdc8d8SChris Lattner       log->Printf("Completed step out plan.");
379b9c1b51eSKate Stone     if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
3801ac04c30SGreg Clayton       m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
38130fdc8d8SChris Lattner       m_return_bp_id = LLDB_INVALID_BREAK_ID;
382a5ce6c88SJim Ingham     }
383a5ce6c88SJim Ingham 
38430fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
38530fdc8d8SChris Lattner     return true;
386b9c1b51eSKate Stone   } else {
38730fdc8d8SChris Lattner     return false;
38830fdc8d8SChris Lattner   }
38930fdc8d8SChris Lattner }
39030fdc8d8SChris Lattner 
391b9c1b51eSKate Stone bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
392b9c1b51eSKate Stone   // Now figure out the range of this inlined block, and set up a "step through
393*05097246SAdrian Prantl   // range" plan for that.  If we've been provided with a context, then use the
394*05097246SAdrian Prantl   // block in that context.
395b57e4a1bSJason Molenda   StackFrameSP immediate_return_from_sp(m_thread.GetStackFrameAtIndex(0));
396a5ce6c88SJim Ingham   if (!immediate_return_from_sp)
397a5ce6c88SJim Ingham     return false;
398a5ce6c88SJim Ingham 
3995160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
400b9c1b51eSKate Stone   if (log) {
401a5ce6c88SJim Ingham     StreamString s;
402a5ce6c88SJim Ingham     immediate_return_from_sp->Dump(&s, true, false);
403a5ce6c88SJim Ingham     log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
404a5ce6c88SJim Ingham   }
405a5ce6c88SJim Ingham 
406a5ce6c88SJim Ingham   Block *from_block = immediate_return_from_sp->GetFrameBlock();
407b9c1b51eSKate Stone   if (from_block) {
408a5ce6c88SJim Ingham     Block *inlined_block = from_block->GetContainingInlinedBlock();
409b9c1b51eSKate Stone     if (inlined_block) {
410a5ce6c88SJim Ingham       size_t num_ranges = inlined_block->GetNumRanges();
411a5ce6c88SJim Ingham       AddressRange inline_range;
412b9c1b51eSKate Stone       if (inlined_block->GetRangeAtIndex(0, inline_range)) {
413a5ce6c88SJim Ingham         SymbolContext inlined_sc;
414a5ce6c88SJim Ingham         inlined_block->CalculateSymbolContext(&inlined_sc);
4155f1a4e1fSJim Ingham         inlined_sc.target_sp = GetTarget().shared_from_this();
416b9c1b51eSKate Stone         RunMode run_mode =
417b9c1b51eSKate Stone             m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
4184b4b2478SJim Ingham         const LazyBool avoid_no_debug = eLazyBoolNo;
4192bdbfd50SJim Ingham 
420b9c1b51eSKate Stone         m_step_through_inline_plan_sp.reset(new ThreadPlanStepOverRange(
421b9c1b51eSKate Stone             m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug));
422b9c1b51eSKate Stone         ThreadPlanStepOverRange *step_through_inline_plan_ptr =
423b9c1b51eSKate Stone             static_cast<ThreadPlanStepOverRange *>(
424b9c1b51eSKate Stone                 m_step_through_inline_plan_sp.get());
4252bdbfd50SJim Ingham         m_step_through_inline_plan_sp->SetPrivate(true);
4262bdbfd50SJim Ingham 
427a5ce6c88SJim Ingham         step_through_inline_plan_ptr->SetOkayToDiscard(true);
428a5ce6c88SJim Ingham         StreamString errors;
429b9c1b51eSKate Stone         if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
430a5ce6c88SJim Ingham           // FIXME: Log this failure.
431a5ce6c88SJim Ingham           delete step_through_inline_plan_ptr;
432a5ce6c88SJim Ingham           return false;
433a5ce6c88SJim Ingham         }
434a5ce6c88SJim Ingham 
435b9c1b51eSKate Stone         for (size_t i = 1; i < num_ranges; i++) {
436a5ce6c88SJim Ingham           if (inlined_block->GetRangeAtIndex(i, inline_range))
437a5ce6c88SJim Ingham             step_through_inline_plan_ptr->AddRange(inline_range);
438a5ce6c88SJim Ingham         }
4392bdbfd50SJim Ingham 
440a5ce6c88SJim Ingham         if (queue_now)
441a5ce6c88SJim Ingham           m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
442a5ce6c88SJim Ingham         return true;
443a5ce6c88SJim Ingham       }
444a5ce6c88SJim Ingham     }
445a5ce6c88SJim Ingham   }
446a5ce6c88SJim Ingham 
447a5ce6c88SJim Ingham   return false;
448a5ce6c88SJim Ingham }
44973ca05a2SJim Ingham 
450b9c1b51eSKate Stone void ThreadPlanStepOut::CalculateReturnValue() {
45173ca05a2SJim Ingham   if (m_return_valobj_sp)
45273ca05a2SJim Ingham     return;
45373ca05a2SJim Ingham 
454b612ac37SJim Ingham   if (!m_calculate_return_value)
455b612ac37SJim Ingham     return;
456b612ac37SJim Ingham 
457b9c1b51eSKate Stone   if (m_immediate_step_from_function != nullptr) {
458b9c1b51eSKate Stone     CompilerType return_compiler_type =
459b9c1b51eSKate Stone         m_immediate_step_from_function->GetCompilerType()
460b9c1b51eSKate Stone             .GetFunctionReturnType();
461b9c1b51eSKate Stone     if (return_compiler_type) {
4621ac04c30SGreg Clayton       lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
46373ca05a2SJim Ingham       if (abi_sp)
464b9c1b51eSKate Stone         m_return_valobj_sp =
465b9c1b51eSKate Stone             abi_sp->GetReturnValueObject(m_thread, return_compiler_type);
46673ca05a2SJim Ingham     }
46773ca05a2SJim Ingham   }
46873ca05a2SJim Ingham }
46964e7ead1SJim Ingham 
470b9c1b51eSKate Stone bool ThreadPlanStepOut::IsPlanStale() {
471*05097246SAdrian Prantl   // If we are still lower on the stack than the frame we are returning to,
472*05097246SAdrian Prantl   // then there's something for us to do.  Otherwise, we're stale.
47364e7ead1SJim Ingham 
47464e7ead1SJim Ingham   StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
475e65b2cf2SEugene Zelenko   return !(frame_zero_id < m_step_out_to_id);
47664e7ead1SJim Ingham }
477