180814287SRaphael Isemann //===-- ThreadPlanStepOut.cpp ---------------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
9e65b2cf2SEugene Zelenko #include "lldb/Target/ThreadPlanStepOut.h"
1030fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h"
1173ca05a2SJim Ingham #include "lldb/Core/Value.h"
1273ca05a2SJim Ingham #include "lldb/Core/ValueObjectConstResult.h"
131f746071SGreg Clayton #include "lldb/Symbol/Block.h"
141f746071SGreg Clayton #include "lldb/Symbol/Function.h"
15fd4cea53SJason Molenda #include "lldb/Symbol/Symbol.h"
161f746071SGreg Clayton #include "lldb/Symbol/Type.h"
1732abc6edSZachary Turner #include "lldb/Target/ABI.h"
1830fdc8d8SChris Lattner #include "lldb/Target/Process.h"
1930fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
20f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
2130fdc8d8SChris Lattner #include "lldb/Target/Target.h"
22a5ce6c88SJim Ingham #include "lldb/Target/ThreadPlanStepOverRange.h"
234b4b2478SJim Ingham #include "lldb/Target/ThreadPlanStepThrough.h"
246f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2530fdc8d8SChris Lattner 
26796ac80bSJonas Devlieghere #include <memory>
27796ac80bSJonas Devlieghere 
2830fdc8d8SChris Lattner using namespace lldb;
2930fdc8d8SChris Lattner using namespace lldb_private;
3030fdc8d8SChris Lattner 
314b4b2478SJim Ingham uint32_t ThreadPlanStepOut::s_default_flag_values = 0;
324b4b2478SJim Ingham 
3330fdc8d8SChris Lattner // ThreadPlanStepOut: Step out of the current frame
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 
50*e4598dc0SJim Ingham   m_step_from_insn = thread.GetRegisterContext()->GetPC(0);
5130fdc8d8SChris Lattner 
524b36f791SVedant Kumar   uint32_t return_frame_index = frame_idx + 1;
53*e4598dc0SJim Ingham   StackFrameSP return_frame_sp(thread.GetStackFrameAtIndex(return_frame_index));
54*e4598dc0SJim Ingham   StackFrameSP immediate_return_from_sp(thread.GetStackFrameAtIndex(frame_idx));
55a5ce6c88SJim Ingham 
56632d2f72SSean Callanan   if (!return_frame_sp || !immediate_return_from_sp)
57632d2f72SSean Callanan     return; // we can't do anything here.  ValidatePlan() will return false.
58632d2f72SSean Callanan 
594b36f791SVedant Kumar   // While stepping out, behave as-if artificial frames are not present.
604b36f791SVedant Kumar   while (return_frame_sp->IsArtificial()) {
614b36f791SVedant Kumar     m_stepped_past_frames.push_back(return_frame_sp);
624b36f791SVedant Kumar 
634b36f791SVedant Kumar     ++return_frame_index;
64*e4598dc0SJim Ingham     return_frame_sp = thread.GetStackFrameAtIndex(return_frame_index);
654b36f791SVedant Kumar 
664b36f791SVedant Kumar     // We never expect to see an artificial frame without a regular ancestor.
674b36f791SVedant Kumar     // If this happens, log the issue and defensively refuse to step out.
684b36f791SVedant Kumar     if (!return_frame_sp) {
694b36f791SVedant Kumar       LLDB_LOG(log, "Can't step out of frame with artificial ancestors");
704b36f791SVedant Kumar       return;
714b36f791SVedant Kumar     }
724b36f791SVedant Kumar   }
734b36f791SVedant Kumar 
74b5c0d1ccSJim Ingham   m_step_out_to_id = return_frame_sp->GetStackID();
75b5c0d1ccSJim Ingham   m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
76a5ce6c88SJim Ingham 
7705097246SAdrian Prantl   // If the frame directly below the one we are returning to is inlined, we
7805097246SAdrian Prantl   // have to be a little more careful.  It is non-trivial to determine the real
7905097246SAdrian Prantl   // "return code address" for an inlined frame, so we have to work our way to
8005097246SAdrian Prantl   // that frame and then step out.
814b36f791SVedant Kumar   if (immediate_return_from_sp->IsInlined()) {
82b9c1b51eSKate Stone     if (frame_idx > 0) {
83b9c1b51eSKate Stone       // First queue a plan that gets us to this inlined frame, and when we get
8405097246SAdrian Prantl       // there we'll queue a second plan that walks us out of this frame.
85796ac80bSJonas Devlieghere       m_step_out_to_inline_plan_sp = std::make_shared<ThreadPlanStepOut>(
86*e4598dc0SJim Ingham           thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
87796ac80bSJonas Devlieghere           frame_idx - 1, eLazyBoolNo, continue_to_next_branch);
88b9c1b51eSKate Stone       static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())
89b9c1b51eSKate Stone           ->SetShouldStopHereCallbacks(nullptr, nullptr);
902bdbfd50SJim Ingham       m_step_out_to_inline_plan_sp->SetPrivate(true);
91b9c1b51eSKate Stone     } else {
9205097246SAdrian Prantl       // If we're already at the inlined frame we're stepping through, then
9305097246SAdrian Prantl       // just do that now.
94a5ce6c88SJim Ingham       QueueInlinedStepPlan(false);
95a5ce6c88SJim Ingham     }
964b36f791SVedant Kumar   } else {
9730fdc8d8SChris Lattner     // Find the return address and set a breakpoint there:
9830fdc8d8SChris Lattner     // FIXME - can we do this more securely if we know first_insn?
9930fdc8d8SChris Lattner 
100fd4cea53SJason Molenda     Address return_address(return_frame_sp->GetFrameCodeAddress());
101b9c1b51eSKate Stone     if (continue_to_next_branch) {
102fd4cea53SJason Molenda       SymbolContext return_address_sc;
103fd4cea53SJason Molenda       AddressRange range;
104fd4cea53SJason Molenda       Address return_address_decr_pc = return_address;
105fd4cea53SJason Molenda       if (return_address_decr_pc.GetOffset() > 0)
106fd4cea53SJason Molenda         return_address_decr_pc.Slide(-1);
107fd4cea53SJason Molenda 
108b9c1b51eSKate Stone       return_address_decr_pc.CalculateSymbolContext(
109b9c1b51eSKate Stone           &return_address_sc, lldb::eSymbolContextLineEntry);
110b9c1b51eSKate Stone       if (return_address_sc.line_entry.IsValid()) {
1118a777920SGreg Clayton         const bool include_inlined_functions = false;
1128a777920SGreg Clayton         range = return_address_sc.line_entry.GetSameLineContiguousAddressRange(
1138a777920SGreg Clayton             include_inlined_functions);
114b9c1b51eSKate Stone         if (range.GetByteSize() > 0) {
115*e4598dc0SJim Ingham           return_address = m_process.AdvanceAddressToNextBranchInstruction(
116b9c1b51eSKate Stone               return_address, range);
117fd4cea53SJason Molenda         }
118fd4cea53SJason Molenda       }
119fd4cea53SJason Molenda     }
120*e4598dc0SJim Ingham     m_return_addr = return_address.GetLoadAddress(&m_process.GetTarget());
121708709c0SSean Callanan 
122708709c0SSean Callanan     if (m_return_addr == LLDB_INVALID_ADDRESS)
123708709c0SSean Callanan       return;
124708709c0SSean Callanan 
1252a42a5a2SJim Ingham     // Perform some additional validation on the return address.
1262a42a5a2SJim Ingham     uint32_t permissions = 0;
127*e4598dc0SJim Ingham     if (!m_process.GetLoadAddressPermissions(m_return_addr, permissions)) {
1286fd818c5STed Woodward       LLDB_LOGF(log, "ThreadPlanStepOut(%p): Return address (0x%" PRIx64
1296fd818c5STed Woodward                 ") permissions not found.", static_cast<void *>(this),
1302a42a5a2SJim Ingham                 m_return_addr);
1312a42a5a2SJim Ingham     } else if (!(permissions & ePermissionsExecutable)) {
1322a42a5a2SJim Ingham       m_constructor_errors.Printf("Return address (0x%" PRIx64
1332a42a5a2SJim Ingham                                   ") did not point to executable memory.",
1342a42a5a2SJim Ingham                                   m_return_addr);
1352a42a5a2SJim Ingham       LLDB_LOGF(log, "ThreadPlanStepOut(%p): %s", static_cast<void *>(this),
1362a42a5a2SJim Ingham                 m_constructor_errors.GetData());
1372a42a5a2SJim Ingham       return;
1382a42a5a2SJim Ingham     }
1392a42a5a2SJim Ingham 
140*e4598dc0SJim Ingham     Breakpoint *return_bp =
141*e4598dc0SJim Ingham         GetTarget().CreateBreakpoint(m_return_addr, true, false).get();
142e103ae92SJonas Devlieghere 
143b9c1b51eSKate Stone     if (return_bp != nullptr) {
144e103ae92SJonas Devlieghere       if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
145e103ae92SJonas Devlieghere         m_could_not_resolve_hw_bp = true;
146*e4598dc0SJim Ingham       return_bp->SetThreadID(m_tid);
14730fdc8d8SChris Lattner       m_return_bp_id = return_bp->GetID();
1482995077dSJim Ingham       return_bp->SetBreakpointKind("step-out");
14930fdc8d8SChris Lattner     }
15073ca05a2SJim Ingham 
151b9c1b51eSKate Stone     if (immediate_return_from_sp) {
152b9c1b51eSKate Stone       const SymbolContext &sc =
153b9c1b51eSKate Stone           immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
154b9c1b51eSKate Stone       if (sc.function) {
15573ca05a2SJim Ingham         m_immediate_step_from_function = sc.function;
15673ca05a2SJim Ingham       }
15773ca05a2SJim Ingham     }
15830fdc8d8SChris Lattner   }
159a5ce6c88SJim Ingham }
160a5ce6c88SJim Ingham 
161b9c1b51eSKate Stone void ThreadPlanStepOut::SetupAvoidNoDebug(
162b9c1b51eSKate Stone     LazyBool step_out_avoids_code_without_debug_info) {
1634b4b2478SJim Ingham   bool avoid_nodebug = true;
164b9c1b51eSKate Stone   switch (step_out_avoids_code_without_debug_info) {
1654b4b2478SJim Ingham   case eLazyBoolYes:
1664b4b2478SJim Ingham     avoid_nodebug = true;
1674b4b2478SJim Ingham     break;
1684b4b2478SJim Ingham   case eLazyBoolNo:
1694b4b2478SJim Ingham     avoid_nodebug = false;
1704b4b2478SJim Ingham     break;
1714b4b2478SJim Ingham   case eLazyBoolCalculate:
172*e4598dc0SJim Ingham     avoid_nodebug = GetThread().GetStepOutAvoidsNoDebug();
1734b4b2478SJim Ingham     break;
1744b4b2478SJim Ingham   }
1754b4b2478SJim Ingham   if (avoid_nodebug)
1764b4b2478SJim Ingham     GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1774b4b2478SJim Ingham   else
1784b4b2478SJim Ingham     GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1794b4b2478SJim Ingham }
1804b4b2478SJim Ingham 
181b9c1b51eSKate Stone void ThreadPlanStepOut::DidPush() {
182*e4598dc0SJim Ingham   Thread &thread = GetThread();
1834b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
184*e4598dc0SJim Ingham     thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
185a5ce6c88SJim Ingham   else if (m_step_through_inline_plan_sp)
186*e4598dc0SJim Ingham     thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
18730fdc8d8SChris Lattner }
18830fdc8d8SChris Lattner 
189b9c1b51eSKate Stone ThreadPlanStepOut::~ThreadPlanStepOut() {
19030fdc8d8SChris Lattner   if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
191*e4598dc0SJim Ingham     GetThread().CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
19230fdc8d8SChris Lattner }
19330fdc8d8SChris Lattner 
194b9c1b51eSKate Stone void ThreadPlanStepOut::GetDescription(Stream *s,
195b9c1b51eSKate Stone                                        lldb::DescriptionLevel level) {
19630fdc8d8SChris Lattner   if (level == lldb::eDescriptionLevelBrief)
19730fdc8d8SChris Lattner     s->Printf("step out");
198b9c1b51eSKate Stone   else {
1994b4b2478SJim Ingham     if (m_step_out_to_inline_plan_sp)
200b5c0d1ccSJim Ingham       s->Printf("Stepping out to inlined frame so we can walk through it.");
201a5ce6c88SJim Ingham     else if (m_step_through_inline_plan_sp)
202a5ce6c88SJim Ingham       s->Printf("Stepping out by stepping through inlined function.");
203b9c1b51eSKate Stone     else {
2042bdbfd50SJim Ingham       s->Printf("Stepping out from ");
2052bdbfd50SJim Ingham       Address tmp_address;
206b9c1b51eSKate Stone       if (tmp_address.SetLoadAddress(m_step_from_insn, &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_step_from_insn);
2112bdbfd50SJim Ingham       }
2122bdbfd50SJim Ingham 
213b9c1b51eSKate Stone       // FIXME: find some useful way to present the m_return_id, since there may
214b9c1b51eSKate Stone       // be multiple copies of the
2152bdbfd50SJim Ingham       // same function on the stack.
2162bdbfd50SJim Ingham 
2172bdbfd50SJim Ingham       s->Printf(" returning to frame at ");
218b9c1b51eSKate Stone       if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) {
219b9c1b51eSKate Stone         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
220b9c1b51eSKate Stone                          Address::DumpStyleLoadAddress);
221b9c1b51eSKate Stone       } else {
2222bdbfd50SJim Ingham         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
2232bdbfd50SJim Ingham       }
2242bdbfd50SJim Ingham 
2252bdbfd50SJim Ingham       if (level == eDescriptionLevelVerbose)
2262bdbfd50SJim Ingham         s->Printf(" using breakpoint site %d", m_return_bp_id);
2272bdbfd50SJim Ingham     }
22830fdc8d8SChris Lattner   }
2294b36f791SVedant Kumar 
2304b36f791SVedant Kumar   s->Printf("\n");
2314b36f791SVedant Kumar   for (StackFrameSP frame_sp : m_stepped_past_frames) {
2324b36f791SVedant Kumar     s->Printf("Stepped out past: ");
2334b36f791SVedant Kumar     frame_sp->DumpUsingSettingsFormat(s);
2344b36f791SVedant Kumar   }
23530fdc8d8SChris Lattner }
23630fdc8d8SChris Lattner 
237b9c1b51eSKate Stone bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
2384b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
2394b4b2478SJim Ingham     return m_step_out_to_inline_plan_sp->ValidatePlan(error);
240e103ae92SJonas Devlieghere 
241e103ae92SJonas Devlieghere   if (m_step_through_inline_plan_sp)
242a5ce6c88SJim Ingham     return m_step_through_inline_plan_sp->ValidatePlan(error);
243e103ae92SJonas Devlieghere 
244e103ae92SJonas Devlieghere   if (m_could_not_resolve_hw_bp) {
245e103ae92SJonas Devlieghere     if (error)
246e103ae92SJonas Devlieghere       error->PutCString(
247e103ae92SJonas Devlieghere           "Could not create hardware breakpoint for thread plan.");
248e103ae92SJonas Devlieghere     return false;
249e103ae92SJonas Devlieghere   }
250e103ae92SJonas Devlieghere 
251e103ae92SJonas Devlieghere   if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
2522a42a5a2SJim Ingham     if (error) {
253a5ce6c88SJim Ingham       error->PutCString("Could not create return address breakpoint.");
2542a42a5a2SJim Ingham       if (m_constructor_errors.GetSize() > 0) {
2552a42a5a2SJim Ingham         error->PutCString(" ");
2562a42a5a2SJim Ingham         error->PutCString(m_constructor_errors.GetString());
2572a42a5a2SJim Ingham       }
2582a42a5a2SJim Ingham     }
25930fdc8d8SChris Lattner     return false;
260e103ae92SJonas Devlieghere   }
261e103ae92SJonas Devlieghere 
26230fdc8d8SChris Lattner   return true;
26330fdc8d8SChris Lattner }
26430fdc8d8SChris Lattner 
265b9c1b51eSKate Stone bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
26605097246SAdrian Prantl   // If the step out plan is done, then we just need to step through the
26705097246SAdrian Prantl   // inlined frame.
268b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
269e65b2cf2SEugene Zelenko     return m_step_out_to_inline_plan_sp->MischiefManaged();
270b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
271b9c1b51eSKate Stone     if (m_step_through_inline_plan_sp->MischiefManaged()) {
27273ca05a2SJim Ingham       CalculateReturnValue();
273a5ce6c88SJim Ingham       SetPlanComplete();
274a5ce6c88SJim Ingham       return true;
275b9c1b51eSKate Stone     } else
276a5ce6c88SJim Ingham       return false;
277b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
278e65b2cf2SEugene Zelenko     return m_step_out_further_plan_sp->MischiefManaged();
279a5ce6c88SJim Ingham   }
280a5ce6c88SJim Ingham 
281b9c1b51eSKate Stone   // We don't explain signals or breakpoints (breakpoints that handle stepping
28205097246SAdrian Prantl   // in or out will be handled by a child plan.
283a5ce6c88SJim Ingham 
28460c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
285b9c1b51eSKate Stone   if (stop_info_sp) {
286b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
287b9c1b51eSKate Stone     if (reason == eStopReasonBreakpoint) {
28805097246SAdrian Prantl       // If this is OUR breakpoint, we're fine, otherwise we don't know why
28905097246SAdrian Prantl       // this happened...
290b9c1b51eSKate Stone       BreakpointSiteSP site_sp(
291*e4598dc0SJim Ingham           m_process.GetBreakpointSiteList().FindByID(stop_info_sp->GetValue()));
292b9c1b51eSKate Stone       if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
293b5c0d1ccSJim Ingham         bool done;
294b5c0d1ccSJim Ingham 
295*e4598dc0SJim Ingham         StackID frame_zero_id =
296*e4598dc0SJim Ingham             GetThread().GetStackFrameAtIndex(0)->GetStackID();
297b5c0d1ccSJim Ingham 
298b5c0d1ccSJim Ingham         if (m_step_out_to_id == frame_zero_id)
299b5c0d1ccSJim Ingham           done = true;
300b9c1b51eSKate Stone         else if (m_step_out_to_id < frame_zero_id) {
301b5c0d1ccSJim Ingham           // Either we stepped past the breakpoint, or the stack ID calculation
302b5c0d1ccSJim Ingham           // was incorrect and we should probably stop.
303b5c0d1ccSJim Ingham           done = true;
304b9c1b51eSKate Stone         } else {
305e65b2cf2SEugene Zelenko           done = (m_immediate_step_from_id < frame_zero_id);
306b5c0d1ccSJim Ingham         }
307b5c0d1ccSJim Ingham 
308b9c1b51eSKate Stone         if (done) {
309e103ae92SJonas Devlieghere           if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
31073ca05a2SJim Ingham             CalculateReturnValue();
311481cef25SGreg Clayton             SetPlanComplete();
31273ca05a2SJim Ingham           }
3134b4b2478SJim Ingham         }
314481cef25SGreg Clayton 
315b9c1b51eSKate Stone         // If there was only one owner, then we're done.  But if we also hit
31605097246SAdrian Prantl         // some user breakpoint on our way out, we should mark ourselves as
31705097246SAdrian Prantl         // done, but also not claim to explain the stop, since it is more
31805097246SAdrian Prantl         // important to report the user breakpoint than the step out
31905097246SAdrian Prantl         // completion.
32030fdc8d8SChris Lattner 
321f4b47e15SGreg Clayton         if (site_sp->GetNumberOfOwners() == 1)
32230fdc8d8SChris Lattner           return true;
32330fdc8d8SChris Lattner       }
32430fdc8d8SChris Lattner       return false;
325b9c1b51eSKate Stone     } else if (IsUsuallyUnexplainedStopReason(reason))
32630fdc8d8SChris Lattner       return false;
3279b03fa0cSJim Ingham     else
32830fdc8d8SChris Lattner       return true;
32930fdc8d8SChris Lattner   }
33030fdc8d8SChris Lattner   return true;
33130fdc8d8SChris Lattner }
33230fdc8d8SChris Lattner 
333b9c1b51eSKate Stone bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
334a5ce6c88SJim Ingham   if (IsPlanComplete())
335a5ce6c88SJim Ingham     return true;
336b5c0d1ccSJim Ingham 
3374b4b2478SJim Ingham   bool done = false;
338b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
339b9c1b51eSKate Stone     if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
3404b4b2478SJim Ingham       // Now step through the inlined stack we are in:
341b9c1b51eSKate Stone       if (QueueInlinedStepPlan(true)) {
3424b4b2478SJim Ingham         // If we can't queue a plan to do this, then just call ourselves done.
3434b4b2478SJim Ingham         m_step_out_to_inline_plan_sp.reset();
3444b4b2478SJim Ingham         SetPlanComplete(false);
3454b4b2478SJim Ingham         return true;
346b9c1b51eSKate Stone       } else
3474b4b2478SJim Ingham         done = true;
348b9c1b51eSKate Stone     } else
3494b4b2478SJim Ingham       return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
350b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
3514b4b2478SJim Ingham     if (m_step_through_inline_plan_sp->MischiefManaged())
3524b4b2478SJim Ingham       done = true;
3534b4b2478SJim Ingham     else
3544b4b2478SJim Ingham       return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
355b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
3564b4b2478SJim Ingham     if (m_step_out_further_plan_sp->MischiefManaged())
3574b4b2478SJim Ingham       m_step_out_further_plan_sp.reset();
3584b4b2478SJim Ingham     else
3594b4b2478SJim Ingham       return m_step_out_further_plan_sp->ShouldStop(event_ptr);
3604b4b2478SJim Ingham   }
361b5c0d1ccSJim Ingham 
362b9c1b51eSKate Stone   if (!done) {
363*e4598dc0SJim Ingham     StackID frame_zero_id = GetThread().GetStackFrameAtIndex(0)->GetStackID();
364e65b2cf2SEugene Zelenko     done = !(frame_zero_id < m_step_out_to_id);
3654b4b2478SJim Ingham   }
3664b4b2478SJim Ingham 
36705097246SAdrian Prantl   // The normal step out computations think we are done, so all we need to do
36805097246SAdrian Prantl   // is consult the ShouldStopHere, and we are done.
369b5c0d1ccSJim Ingham 
370b9c1b51eSKate Stone   if (done) {
371e103ae92SJonas Devlieghere     if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
37273ca05a2SJim Ingham       CalculateReturnValue();
373a5ce6c88SJim Ingham       SetPlanComplete();
374b9c1b51eSKate Stone     } else {
375b9c1b51eSKate Stone       m_step_out_further_plan_sp =
376e103ae92SJonas Devlieghere           QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder, m_status);
3774b4b2478SJim Ingham       done = false;
378a5ce6c88SJim Ingham     }
379a5ce6c88SJim Ingham   }
3804b4b2478SJim Ingham 
3814b4b2478SJim Ingham   return done;
382a5ce6c88SJim Ingham }
38330fdc8d8SChris Lattner 
384b9c1b51eSKate Stone bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
38530fdc8d8SChris Lattner 
386b9c1b51eSKate Stone StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
38730fdc8d8SChris Lattner 
388b9c1b51eSKate Stone bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
389b9c1b51eSKate Stone                                      bool current_plan) {
3904b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
391a5ce6c88SJim Ingham     return true;
392a5ce6c88SJim Ingham 
39330fdc8d8SChris Lattner   if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
39430fdc8d8SChris Lattner     return false;
39530fdc8d8SChris Lattner 
396b9c1b51eSKate Stone   if (current_plan) {
397*e4598dc0SJim Ingham     Breakpoint *return_bp = GetTarget().GetBreakpointByID(m_return_bp_id).get();
398e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
39930fdc8d8SChris Lattner       return_bp->SetEnabled(true);
40030fdc8d8SChris Lattner   }
40130fdc8d8SChris Lattner   return true;
40230fdc8d8SChris Lattner }
40330fdc8d8SChris Lattner 
404b9c1b51eSKate Stone bool ThreadPlanStepOut::WillStop() {
405b9c1b51eSKate Stone   if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
406*e4598dc0SJim Ingham     Breakpoint *return_bp = GetTarget().GetBreakpointByID(m_return_bp_id).get();
407e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
40830fdc8d8SChris Lattner       return_bp->SetEnabled(false);
409a5ce6c88SJim Ingham   }
410a5ce6c88SJim Ingham 
41130fdc8d8SChris Lattner   return true;
41230fdc8d8SChris Lattner }
41330fdc8d8SChris Lattner 
414b9c1b51eSKate Stone bool ThreadPlanStepOut::MischiefManaged() {
415b9c1b51eSKate Stone   if (IsPlanComplete()) {
41630fdc8d8SChris Lattner     // Did I reach my breakpoint?  If so I'm done.
41730fdc8d8SChris Lattner     //
418b9c1b51eSKate Stone     // I also check the stack depth, since if we've blown past the breakpoint
419b9c1b51eSKate Stone     // for some
420b9c1b51eSKate Stone     // reason and we're now stopping for some other reason altogether, then
42105097246SAdrian Prantl     // we're done with this step out operation.
42230fdc8d8SChris Lattner 
4235160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
42430fdc8d8SChris Lattner     if (log)
42563e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Completed step out plan.");
426b9c1b51eSKate Stone     if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
427*e4598dc0SJim Ingham       GetTarget().RemoveBreakpointByID(m_return_bp_id);
42830fdc8d8SChris Lattner       m_return_bp_id = LLDB_INVALID_BREAK_ID;
429a5ce6c88SJim Ingham     }
430a5ce6c88SJim Ingham 
43130fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
43230fdc8d8SChris Lattner     return true;
433b9c1b51eSKate Stone   } else {
43430fdc8d8SChris Lattner     return false;
43530fdc8d8SChris Lattner   }
43630fdc8d8SChris Lattner }
43730fdc8d8SChris Lattner 
438b9c1b51eSKate Stone bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
439b9c1b51eSKate Stone   // Now figure out the range of this inlined block, and set up a "step through
44005097246SAdrian Prantl   // range" plan for that.  If we've been provided with a context, then use the
44105097246SAdrian Prantl   // block in that context.
442*e4598dc0SJim Ingham   Thread &thread = GetThread();
443*e4598dc0SJim Ingham   StackFrameSP immediate_return_from_sp(thread.GetStackFrameAtIndex(0));
444a5ce6c88SJim Ingham   if (!immediate_return_from_sp)
445a5ce6c88SJim Ingham     return false;
446a5ce6c88SJim Ingham 
4475160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
448b9c1b51eSKate Stone   if (log) {
449a5ce6c88SJim Ingham     StreamString s;
450a5ce6c88SJim Ingham     immediate_return_from_sp->Dump(&s, true, false);
45163e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Queuing inlined frame to step past: %s.", s.GetData());
452a5ce6c88SJim Ingham   }
453a5ce6c88SJim Ingham 
454a5ce6c88SJim Ingham   Block *from_block = immediate_return_from_sp->GetFrameBlock();
455b9c1b51eSKate Stone   if (from_block) {
456a5ce6c88SJim Ingham     Block *inlined_block = from_block->GetContainingInlinedBlock();
457b9c1b51eSKate Stone     if (inlined_block) {
458a5ce6c88SJim Ingham       size_t num_ranges = inlined_block->GetNumRanges();
459a5ce6c88SJim Ingham       AddressRange inline_range;
460b9c1b51eSKate Stone       if (inlined_block->GetRangeAtIndex(0, inline_range)) {
461a5ce6c88SJim Ingham         SymbolContext inlined_sc;
462a5ce6c88SJim Ingham         inlined_block->CalculateSymbolContext(&inlined_sc);
4635f1a4e1fSJim Ingham         inlined_sc.target_sp = GetTarget().shared_from_this();
464b9c1b51eSKate Stone         RunMode run_mode =
465b9c1b51eSKate Stone             m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
4664b4b2478SJim Ingham         const LazyBool avoid_no_debug = eLazyBoolNo;
4672bdbfd50SJim Ingham 
468796ac80bSJonas Devlieghere         m_step_through_inline_plan_sp =
469796ac80bSJonas Devlieghere             std::make_shared<ThreadPlanStepOverRange>(
470*e4598dc0SJim Ingham                 thread, inline_range, inlined_sc, run_mode, avoid_no_debug);
471b9c1b51eSKate Stone         ThreadPlanStepOverRange *step_through_inline_plan_ptr =
472b9c1b51eSKate Stone             static_cast<ThreadPlanStepOverRange *>(
473b9c1b51eSKate Stone                 m_step_through_inline_plan_sp.get());
4742bdbfd50SJim Ingham         m_step_through_inline_plan_sp->SetPrivate(true);
4752bdbfd50SJim Ingham 
476a5ce6c88SJim Ingham         step_through_inline_plan_ptr->SetOkayToDiscard(true);
477a5ce6c88SJim Ingham         StreamString errors;
478b9c1b51eSKate Stone         if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
479a5ce6c88SJim Ingham           // FIXME: Log this failure.
480a5ce6c88SJim Ingham           delete step_through_inline_plan_ptr;
481a5ce6c88SJim Ingham           return false;
482a5ce6c88SJim Ingham         }
483a5ce6c88SJim Ingham 
484b9c1b51eSKate Stone         for (size_t i = 1; i < num_ranges; i++) {
485a5ce6c88SJim Ingham           if (inlined_block->GetRangeAtIndex(i, inline_range))
486a5ce6c88SJim Ingham             step_through_inline_plan_ptr->AddRange(inline_range);
487a5ce6c88SJim Ingham         }
4882bdbfd50SJim Ingham 
489a5ce6c88SJim Ingham         if (queue_now)
490*e4598dc0SJim Ingham           thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
491a5ce6c88SJim Ingham         return true;
492a5ce6c88SJim Ingham       }
493a5ce6c88SJim Ingham     }
494a5ce6c88SJim Ingham   }
495a5ce6c88SJim Ingham 
496a5ce6c88SJim Ingham   return false;
497a5ce6c88SJim Ingham }
49873ca05a2SJim Ingham 
499b9c1b51eSKate Stone void ThreadPlanStepOut::CalculateReturnValue() {
50073ca05a2SJim Ingham   if (m_return_valobj_sp)
50173ca05a2SJim Ingham     return;
50273ca05a2SJim Ingham 
503b612ac37SJim Ingham   if (!m_calculate_return_value)
504b612ac37SJim Ingham     return;
505b612ac37SJim Ingham 
506b9c1b51eSKate Stone   if (m_immediate_step_from_function != nullptr) {
507b9c1b51eSKate Stone     CompilerType return_compiler_type =
508b9c1b51eSKate Stone         m_immediate_step_from_function->GetCompilerType()
509b9c1b51eSKate Stone             .GetFunctionReturnType();
510b9c1b51eSKate Stone     if (return_compiler_type) {
511*e4598dc0SJim Ingham       lldb::ABISP abi_sp = m_process.GetABI();
51273ca05a2SJim Ingham       if (abi_sp)
513b9c1b51eSKate Stone         m_return_valobj_sp =
514*e4598dc0SJim Ingham             abi_sp->GetReturnValueObject(GetThread(), return_compiler_type);
51573ca05a2SJim Ingham     }
51673ca05a2SJim Ingham   }
51773ca05a2SJim Ingham }
51864e7ead1SJim Ingham 
519b9c1b51eSKate Stone bool ThreadPlanStepOut::IsPlanStale() {
52005097246SAdrian Prantl   // If we are still lower on the stack than the frame we are returning to,
52105097246SAdrian Prantl   // then there's something for us to do.  Otherwise, we're stale.
52264e7ead1SJim Ingham 
523*e4598dc0SJim Ingham   StackID frame_zero_id = GetThread().GetStackFrameAtIndex(0)->GetStackID();
524e65b2cf2SEugene Zelenko   return !(frame_zero_id < m_step_out_to_id);
52564e7ead1SJim Ingham }
526