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 
5030fdc8d8SChris Lattner   m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
5130fdc8d8SChris Lattner 
524b36f791SVedant Kumar   uint32_t return_frame_index = frame_idx + 1;
534b36f791SVedant Kumar   StackFrameSP return_frame_sp(
544b36f791SVedant Kumar       m_thread.GetStackFrameAtIndex(return_frame_index));
55b9c1b51eSKate Stone   StackFrameSP immediate_return_from_sp(
56b9c1b51eSKate Stone       m_thread.GetStackFrameAtIndex(frame_idx));
57a5ce6c88SJim Ingham 
58632d2f72SSean Callanan   if (!return_frame_sp || !immediate_return_from_sp)
59632d2f72SSean Callanan     return; // we can't do anything here.  ValidatePlan() will return false.
60632d2f72SSean Callanan 
614b36f791SVedant Kumar   // While stepping out, behave as-if artificial frames are not present.
624b36f791SVedant Kumar   while (return_frame_sp->IsArtificial()) {
634b36f791SVedant Kumar     m_stepped_past_frames.push_back(return_frame_sp);
644b36f791SVedant Kumar 
654b36f791SVedant Kumar     ++return_frame_index;
664b36f791SVedant Kumar     return_frame_sp = m_thread.GetStackFrameAtIndex(return_frame_index);
674b36f791SVedant Kumar 
684b36f791SVedant Kumar     // We never expect to see an artificial frame without a regular ancestor.
694b36f791SVedant Kumar     // If this happens, log the issue and defensively refuse to step out.
704b36f791SVedant Kumar     if (!return_frame_sp) {
714b36f791SVedant Kumar       LLDB_LOG(log, "Can't step out of frame with artificial ancestors");
724b36f791SVedant Kumar       return;
734b36f791SVedant Kumar     }
744b36f791SVedant Kumar   }
754b36f791SVedant Kumar 
76b5c0d1ccSJim Ingham   m_step_out_to_id = return_frame_sp->GetStackID();
77b5c0d1ccSJim Ingham   m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
78a5ce6c88SJim Ingham 
7905097246SAdrian Prantl   // If the frame directly below the one we are returning to is inlined, we
8005097246SAdrian Prantl   // have to be a little more careful.  It is non-trivial to determine the real
8105097246SAdrian Prantl   // "return code address" for an inlined frame, so we have to work our way to
8205097246SAdrian Prantl   // that frame and then step out.
834b36f791SVedant Kumar   if (immediate_return_from_sp->IsInlined()) {
84b9c1b51eSKate Stone     if (frame_idx > 0) {
85b9c1b51eSKate Stone       // First queue a plan that gets us to this inlined frame, and when we get
8605097246SAdrian Prantl       // there we'll queue a second plan that walks us out of this frame.
87796ac80bSJonas Devlieghere       m_step_out_to_inline_plan_sp = std::make_shared<ThreadPlanStepOut>(
88b9c1b51eSKate Stone           m_thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
89796ac80bSJonas Devlieghere           frame_idx - 1, eLazyBoolNo, continue_to_next_branch);
90b9c1b51eSKate Stone       static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())
91b9c1b51eSKate Stone           ->SetShouldStopHereCallbacks(nullptr, nullptr);
922bdbfd50SJim Ingham       m_step_out_to_inline_plan_sp->SetPrivate(true);
93b9c1b51eSKate Stone     } else {
9405097246SAdrian Prantl       // If we're already at the inlined frame we're stepping through, then
9505097246SAdrian Prantl       // just do that now.
96a5ce6c88SJim Ingham       QueueInlinedStepPlan(false);
97a5ce6c88SJim Ingham     }
984b36f791SVedant Kumar   } else {
9930fdc8d8SChris Lattner     // Find the return address and set a breakpoint there:
10030fdc8d8SChris Lattner     // FIXME - can we do this more securely if we know first_insn?
10130fdc8d8SChris Lattner 
102fd4cea53SJason Molenda     Address return_address(return_frame_sp->GetFrameCodeAddress());
103b9c1b51eSKate Stone     if (continue_to_next_branch) {
104fd4cea53SJason Molenda       SymbolContext return_address_sc;
105fd4cea53SJason Molenda       AddressRange range;
106fd4cea53SJason Molenda       Address return_address_decr_pc = return_address;
107fd4cea53SJason Molenda       if (return_address_decr_pc.GetOffset() > 0)
108fd4cea53SJason Molenda         return_address_decr_pc.Slide(-1);
109fd4cea53SJason Molenda 
110b9c1b51eSKate Stone       return_address_decr_pc.CalculateSymbolContext(
111b9c1b51eSKate Stone           &return_address_sc, lldb::eSymbolContextLineEntry);
112b9c1b51eSKate Stone       if (return_address_sc.line_entry.IsValid()) {
1138a777920SGreg Clayton         const bool include_inlined_functions = false;
1148a777920SGreg Clayton         range = return_address_sc.line_entry.GetSameLineContiguousAddressRange(
1158a777920SGreg Clayton             include_inlined_functions);
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 
1292a42a5a2SJim Ingham     // Perform some additional validation on the return address.
1302a42a5a2SJim Ingham     uint32_t permissions = 0;
1312a42a5a2SJim Ingham     if (!m_thread.GetProcess()->GetLoadAddressPermissions(m_return_addr,
1322a42a5a2SJim Ingham                                                           permissions)) {
133*6fd818c5STed Woodward       LLDB_LOGF(log, "ThreadPlanStepOut(%p): Return address (0x%" PRIx64
134*6fd818c5STed Woodward                 ") permissions not found.", static_cast<void *>(this),
1352a42a5a2SJim Ingham                 m_return_addr);
1362a42a5a2SJim Ingham     } else if (!(permissions & ePermissionsExecutable)) {
1372a42a5a2SJim Ingham       m_constructor_errors.Printf("Return address (0x%" PRIx64
1382a42a5a2SJim Ingham                                   ") did not point to executable memory.",
1392a42a5a2SJim Ingham                                   m_return_addr);
1402a42a5a2SJim Ingham       LLDB_LOGF(log, "ThreadPlanStepOut(%p): %s", static_cast<void *>(this),
1412a42a5a2SJim Ingham                 m_constructor_errors.GetData());
1422a42a5a2SJim Ingham       return;
1432a42a5a2SJim Ingham     }
1442a42a5a2SJim Ingham 
145b9c1b51eSKate Stone     Breakpoint *return_bp = m_thread.CalculateTarget()
146b9c1b51eSKate Stone                                 ->CreateBreakpoint(m_return_addr, true, false)
147b9c1b51eSKate Stone                                 .get();
148e103ae92SJonas Devlieghere 
149b9c1b51eSKate Stone     if (return_bp != nullptr) {
150e103ae92SJonas Devlieghere       if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
151e103ae92SJonas Devlieghere         m_could_not_resolve_hw_bp = true;
15230fdc8d8SChris Lattner       return_bp->SetThreadID(m_thread.GetID());
15330fdc8d8SChris Lattner       m_return_bp_id = return_bp->GetID();
1542995077dSJim Ingham       return_bp->SetBreakpointKind("step-out");
15530fdc8d8SChris Lattner     }
15673ca05a2SJim Ingham 
157b9c1b51eSKate Stone     if (immediate_return_from_sp) {
158b9c1b51eSKate Stone       const SymbolContext &sc =
159b9c1b51eSKate Stone           immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
160b9c1b51eSKate Stone       if (sc.function) {
16173ca05a2SJim Ingham         m_immediate_step_from_function = sc.function;
16273ca05a2SJim Ingham       }
16373ca05a2SJim Ingham     }
16430fdc8d8SChris Lattner   }
165a5ce6c88SJim Ingham }
166a5ce6c88SJim Ingham 
167b9c1b51eSKate Stone void ThreadPlanStepOut::SetupAvoidNoDebug(
168b9c1b51eSKate Stone     LazyBool step_out_avoids_code_without_debug_info) {
1694b4b2478SJim Ingham   bool avoid_nodebug = true;
170b9c1b51eSKate Stone   switch (step_out_avoids_code_without_debug_info) {
1714b4b2478SJim Ingham   case eLazyBoolYes:
1724b4b2478SJim Ingham     avoid_nodebug = true;
1734b4b2478SJim Ingham     break;
1744b4b2478SJim Ingham   case eLazyBoolNo:
1754b4b2478SJim Ingham     avoid_nodebug = false;
1764b4b2478SJim Ingham     break;
1774b4b2478SJim Ingham   case eLazyBoolCalculate:
1784b4b2478SJim Ingham     avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
1794b4b2478SJim Ingham     break;
1804b4b2478SJim Ingham   }
1814b4b2478SJim Ingham   if (avoid_nodebug)
1824b4b2478SJim Ingham     GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1834b4b2478SJim Ingham   else
1844b4b2478SJim Ingham     GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1854b4b2478SJim Ingham }
1864b4b2478SJim Ingham 
187b9c1b51eSKate Stone void ThreadPlanStepOut::DidPush() {
1884b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
1894b4b2478SJim Ingham     m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
190a5ce6c88SJim Ingham   else if (m_step_through_inline_plan_sp)
191a5ce6c88SJim Ingham     m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
19230fdc8d8SChris Lattner }
19330fdc8d8SChris Lattner 
194b9c1b51eSKate Stone ThreadPlanStepOut::~ThreadPlanStepOut() {
19530fdc8d8SChris Lattner   if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
1961ac04c30SGreg Clayton     m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
19730fdc8d8SChris Lattner }
19830fdc8d8SChris Lattner 
199b9c1b51eSKate Stone void ThreadPlanStepOut::GetDescription(Stream *s,
200b9c1b51eSKate Stone                                        lldb::DescriptionLevel level) {
20130fdc8d8SChris Lattner   if (level == lldb::eDescriptionLevelBrief)
20230fdc8d8SChris Lattner     s->Printf("step out");
203b9c1b51eSKate Stone   else {
2044b4b2478SJim Ingham     if (m_step_out_to_inline_plan_sp)
205b5c0d1ccSJim Ingham       s->Printf("Stepping out to inlined frame so we can walk through it.");
206a5ce6c88SJim Ingham     else if (m_step_through_inline_plan_sp)
207a5ce6c88SJim Ingham       s->Printf("Stepping out by stepping through inlined function.");
208b9c1b51eSKate Stone     else {
2092bdbfd50SJim Ingham       s->Printf("Stepping out from ");
2102bdbfd50SJim Ingham       Address tmp_address;
211b9c1b51eSKate Stone       if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) {
212b9c1b51eSKate Stone         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
213b9c1b51eSKate Stone                          Address::DumpStyleLoadAddress);
214b9c1b51eSKate Stone       } else {
2152bdbfd50SJim Ingham         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn);
2162bdbfd50SJim Ingham       }
2172bdbfd50SJim Ingham 
218b9c1b51eSKate Stone       // FIXME: find some useful way to present the m_return_id, since there may
219b9c1b51eSKate Stone       // be multiple copies of the
2202bdbfd50SJim Ingham       // same function on the stack.
2212bdbfd50SJim Ingham 
2222bdbfd50SJim Ingham       s->Printf(" returning to frame at ");
223b9c1b51eSKate Stone       if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) {
224b9c1b51eSKate Stone         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
225b9c1b51eSKate Stone                          Address::DumpStyleLoadAddress);
226b9c1b51eSKate Stone       } else {
2272bdbfd50SJim Ingham         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
2282bdbfd50SJim Ingham       }
2292bdbfd50SJim Ingham 
2302bdbfd50SJim Ingham       if (level == eDescriptionLevelVerbose)
2312bdbfd50SJim Ingham         s->Printf(" using breakpoint site %d", m_return_bp_id);
2322bdbfd50SJim Ingham     }
23330fdc8d8SChris Lattner   }
2344b36f791SVedant Kumar 
2354b36f791SVedant Kumar   s->Printf("\n");
2364b36f791SVedant Kumar   for (StackFrameSP frame_sp : m_stepped_past_frames) {
2374b36f791SVedant Kumar     s->Printf("Stepped out past: ");
2384b36f791SVedant Kumar     frame_sp->DumpUsingSettingsFormat(s);
2394b36f791SVedant Kumar   }
24030fdc8d8SChris Lattner }
24130fdc8d8SChris Lattner 
242b9c1b51eSKate Stone bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
2434b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
2444b4b2478SJim Ingham     return m_step_out_to_inline_plan_sp->ValidatePlan(error);
245e103ae92SJonas Devlieghere 
246e103ae92SJonas Devlieghere   if (m_step_through_inline_plan_sp)
247a5ce6c88SJim Ingham     return m_step_through_inline_plan_sp->ValidatePlan(error);
248e103ae92SJonas Devlieghere 
249e103ae92SJonas Devlieghere   if (m_could_not_resolve_hw_bp) {
250e103ae92SJonas Devlieghere     if (error)
251e103ae92SJonas Devlieghere       error->PutCString(
252e103ae92SJonas Devlieghere           "Could not create hardware breakpoint for thread plan.");
253e103ae92SJonas Devlieghere     return false;
254e103ae92SJonas Devlieghere   }
255e103ae92SJonas Devlieghere 
256e103ae92SJonas Devlieghere   if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
2572a42a5a2SJim Ingham     if (error) {
258a5ce6c88SJim Ingham       error->PutCString("Could not create return address breakpoint.");
2592a42a5a2SJim Ingham       if (m_constructor_errors.GetSize() > 0) {
2602a42a5a2SJim Ingham         error->PutCString(" ");
2612a42a5a2SJim Ingham         error->PutCString(m_constructor_errors.GetString());
2622a42a5a2SJim Ingham       }
2632a42a5a2SJim Ingham     }
26430fdc8d8SChris Lattner     return false;
265e103ae92SJonas Devlieghere   }
266e103ae92SJonas Devlieghere 
26730fdc8d8SChris Lattner   return true;
26830fdc8d8SChris Lattner }
26930fdc8d8SChris Lattner 
270b9c1b51eSKate Stone bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
27105097246SAdrian Prantl   // If the step out plan is done, then we just need to step through the
27205097246SAdrian Prantl   // inlined frame.
273b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
274e65b2cf2SEugene Zelenko     return m_step_out_to_inline_plan_sp->MischiefManaged();
275b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
276b9c1b51eSKate Stone     if (m_step_through_inline_plan_sp->MischiefManaged()) {
27773ca05a2SJim Ingham       CalculateReturnValue();
278a5ce6c88SJim Ingham       SetPlanComplete();
279a5ce6c88SJim Ingham       return true;
280b9c1b51eSKate Stone     } else
281a5ce6c88SJim Ingham       return false;
282b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
283e65b2cf2SEugene Zelenko     return m_step_out_further_plan_sp->MischiefManaged();
284a5ce6c88SJim Ingham   }
285a5ce6c88SJim Ingham 
286b9c1b51eSKate Stone   // We don't explain signals or breakpoints (breakpoints that handle stepping
28705097246SAdrian Prantl   // in or out will be handled by a child plan.
288a5ce6c88SJim Ingham 
28960c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
290b9c1b51eSKate Stone   if (stop_info_sp) {
291b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
292b9c1b51eSKate Stone     if (reason == eStopReasonBreakpoint) {
29305097246SAdrian Prantl       // If this is OUR breakpoint, we're fine, otherwise we don't know why
29405097246SAdrian Prantl       // this happened...
295b9c1b51eSKate Stone       BreakpointSiteSP site_sp(
296b9c1b51eSKate Stone           m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
297b9c1b51eSKate Stone               stop_info_sp->GetValue()));
298b9c1b51eSKate Stone       if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
299b5c0d1ccSJim Ingham         bool done;
300b5c0d1ccSJim Ingham 
301b5c0d1ccSJim Ingham         StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
302b5c0d1ccSJim Ingham 
303b5c0d1ccSJim Ingham         if (m_step_out_to_id == frame_zero_id)
304b5c0d1ccSJim Ingham           done = true;
305b9c1b51eSKate Stone         else if (m_step_out_to_id < frame_zero_id) {
306b5c0d1ccSJim Ingham           // Either we stepped past the breakpoint, or the stack ID calculation
307b5c0d1ccSJim Ingham           // was incorrect and we should probably stop.
308b5c0d1ccSJim Ingham           done = true;
309b9c1b51eSKate Stone         } else {
310e65b2cf2SEugene Zelenko           done = (m_immediate_step_from_id < frame_zero_id);
311b5c0d1ccSJim Ingham         }
312b5c0d1ccSJim Ingham 
313b9c1b51eSKate Stone         if (done) {
314e103ae92SJonas Devlieghere           if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
31573ca05a2SJim Ingham             CalculateReturnValue();
316481cef25SGreg Clayton             SetPlanComplete();
31773ca05a2SJim Ingham           }
3184b4b2478SJim Ingham         }
319481cef25SGreg Clayton 
320b9c1b51eSKate Stone         // If there was only one owner, then we're done.  But if we also hit
32105097246SAdrian Prantl         // some user breakpoint on our way out, we should mark ourselves as
32205097246SAdrian Prantl         // done, but also not claim to explain the stop, since it is more
32305097246SAdrian Prantl         // important to report the user breakpoint than the step out
32405097246SAdrian Prantl         // completion.
32530fdc8d8SChris Lattner 
326f4b47e15SGreg Clayton         if (site_sp->GetNumberOfOwners() == 1)
32730fdc8d8SChris Lattner           return true;
32830fdc8d8SChris Lattner       }
32930fdc8d8SChris Lattner       return false;
330b9c1b51eSKate Stone     } else if (IsUsuallyUnexplainedStopReason(reason))
33130fdc8d8SChris Lattner       return false;
3329b03fa0cSJim Ingham     else
33330fdc8d8SChris Lattner       return true;
33430fdc8d8SChris Lattner   }
33530fdc8d8SChris Lattner   return true;
33630fdc8d8SChris Lattner }
33730fdc8d8SChris Lattner 
338b9c1b51eSKate Stone bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
339a5ce6c88SJim Ingham   if (IsPlanComplete())
340a5ce6c88SJim Ingham     return true;
341b5c0d1ccSJim Ingham 
3424b4b2478SJim Ingham   bool done = false;
343b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
344b9c1b51eSKate Stone     if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
3454b4b2478SJim Ingham       // Now step through the inlined stack we are in:
346b9c1b51eSKate Stone       if (QueueInlinedStepPlan(true)) {
3474b4b2478SJim Ingham         // If we can't queue a plan to do this, then just call ourselves done.
3484b4b2478SJim Ingham         m_step_out_to_inline_plan_sp.reset();
3494b4b2478SJim Ingham         SetPlanComplete(false);
3504b4b2478SJim Ingham         return true;
351b9c1b51eSKate Stone       } else
3524b4b2478SJim Ingham         done = true;
353b9c1b51eSKate Stone     } else
3544b4b2478SJim Ingham       return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
355b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
3564b4b2478SJim Ingham     if (m_step_through_inline_plan_sp->MischiefManaged())
3574b4b2478SJim Ingham       done = true;
3584b4b2478SJim Ingham     else
3594b4b2478SJim Ingham       return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
360b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
3614b4b2478SJim Ingham     if (m_step_out_further_plan_sp->MischiefManaged())
3624b4b2478SJim Ingham       m_step_out_further_plan_sp.reset();
3634b4b2478SJim Ingham     else
3644b4b2478SJim Ingham       return m_step_out_further_plan_sp->ShouldStop(event_ptr);
3654b4b2478SJim Ingham   }
366b5c0d1ccSJim Ingham 
367b9c1b51eSKate Stone   if (!done) {
368b5c0d1ccSJim Ingham     StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
369e65b2cf2SEugene Zelenko     done = !(frame_zero_id < m_step_out_to_id);
3704b4b2478SJim Ingham   }
3714b4b2478SJim Ingham 
37205097246SAdrian Prantl   // The normal step out computations think we are done, so all we need to do
37305097246SAdrian Prantl   // is consult the ShouldStopHere, and we are done.
374b5c0d1ccSJim Ingham 
375b9c1b51eSKate Stone   if (done) {
376e103ae92SJonas Devlieghere     if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
37773ca05a2SJim Ingham       CalculateReturnValue();
378a5ce6c88SJim Ingham       SetPlanComplete();
379b9c1b51eSKate Stone     } else {
380b9c1b51eSKate Stone       m_step_out_further_plan_sp =
381e103ae92SJonas Devlieghere           QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder, m_status);
3824b4b2478SJim Ingham       done = false;
383a5ce6c88SJim Ingham     }
384a5ce6c88SJim Ingham   }
3854b4b2478SJim Ingham 
3864b4b2478SJim Ingham   return done;
387a5ce6c88SJim Ingham }
38830fdc8d8SChris Lattner 
389b9c1b51eSKate Stone bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
39030fdc8d8SChris Lattner 
391b9c1b51eSKate Stone StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
39230fdc8d8SChris Lattner 
393b9c1b51eSKate Stone bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
394b9c1b51eSKate Stone                                      bool current_plan) {
3954b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
396a5ce6c88SJim Ingham     return true;
397a5ce6c88SJim Ingham 
39830fdc8d8SChris Lattner   if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
39930fdc8d8SChris Lattner     return false;
40030fdc8d8SChris Lattner 
401b9c1b51eSKate Stone   if (current_plan) {
402b9c1b51eSKate Stone     Breakpoint *return_bp =
403b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
404e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
40530fdc8d8SChris Lattner       return_bp->SetEnabled(true);
40630fdc8d8SChris Lattner   }
40730fdc8d8SChris Lattner   return true;
40830fdc8d8SChris Lattner }
40930fdc8d8SChris Lattner 
410b9c1b51eSKate Stone bool ThreadPlanStepOut::WillStop() {
411b9c1b51eSKate Stone   if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
412b9c1b51eSKate Stone     Breakpoint *return_bp =
413b9c1b51eSKate Stone         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
414e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
41530fdc8d8SChris Lattner       return_bp->SetEnabled(false);
416a5ce6c88SJim Ingham   }
417a5ce6c88SJim Ingham 
41830fdc8d8SChris Lattner   return true;
41930fdc8d8SChris Lattner }
42030fdc8d8SChris Lattner 
421b9c1b51eSKate Stone bool ThreadPlanStepOut::MischiefManaged() {
422b9c1b51eSKate Stone   if (IsPlanComplete()) {
42330fdc8d8SChris Lattner     // Did I reach my breakpoint?  If so I'm done.
42430fdc8d8SChris Lattner     //
425b9c1b51eSKate Stone     // I also check the stack depth, since if we've blown past the breakpoint
426b9c1b51eSKate Stone     // for some
427b9c1b51eSKate Stone     // reason and we're now stopping for some other reason altogether, then
42805097246SAdrian Prantl     // we're done with this step out operation.
42930fdc8d8SChris Lattner 
4305160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
43130fdc8d8SChris Lattner     if (log)
43263e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Completed step out plan.");
433b9c1b51eSKate Stone     if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
4341ac04c30SGreg Clayton       m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
43530fdc8d8SChris Lattner       m_return_bp_id = LLDB_INVALID_BREAK_ID;
436a5ce6c88SJim Ingham     }
437a5ce6c88SJim Ingham 
43830fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
43930fdc8d8SChris Lattner     return true;
440b9c1b51eSKate Stone   } else {
44130fdc8d8SChris Lattner     return false;
44230fdc8d8SChris Lattner   }
44330fdc8d8SChris Lattner }
44430fdc8d8SChris Lattner 
445b9c1b51eSKate Stone bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
446b9c1b51eSKate Stone   // Now figure out the range of this inlined block, and set up a "step through
44705097246SAdrian Prantl   // range" plan for that.  If we've been provided with a context, then use the
44805097246SAdrian Prantl   // block in that context.
449b57e4a1bSJason Molenda   StackFrameSP immediate_return_from_sp(m_thread.GetStackFrameAtIndex(0));
450a5ce6c88SJim Ingham   if (!immediate_return_from_sp)
451a5ce6c88SJim Ingham     return false;
452a5ce6c88SJim Ingham 
4535160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
454b9c1b51eSKate Stone   if (log) {
455a5ce6c88SJim Ingham     StreamString s;
456a5ce6c88SJim Ingham     immediate_return_from_sp->Dump(&s, true, false);
45763e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Queuing inlined frame to step past: %s.", s.GetData());
458a5ce6c88SJim Ingham   }
459a5ce6c88SJim Ingham 
460a5ce6c88SJim Ingham   Block *from_block = immediate_return_from_sp->GetFrameBlock();
461b9c1b51eSKate Stone   if (from_block) {
462a5ce6c88SJim Ingham     Block *inlined_block = from_block->GetContainingInlinedBlock();
463b9c1b51eSKate Stone     if (inlined_block) {
464a5ce6c88SJim Ingham       size_t num_ranges = inlined_block->GetNumRanges();
465a5ce6c88SJim Ingham       AddressRange inline_range;
466b9c1b51eSKate Stone       if (inlined_block->GetRangeAtIndex(0, inline_range)) {
467a5ce6c88SJim Ingham         SymbolContext inlined_sc;
468a5ce6c88SJim Ingham         inlined_block->CalculateSymbolContext(&inlined_sc);
4695f1a4e1fSJim Ingham         inlined_sc.target_sp = GetTarget().shared_from_this();
470b9c1b51eSKate Stone         RunMode run_mode =
471b9c1b51eSKate Stone             m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
4724b4b2478SJim Ingham         const LazyBool avoid_no_debug = eLazyBoolNo;
4732bdbfd50SJim Ingham 
474796ac80bSJonas Devlieghere         m_step_through_inline_plan_sp =
475796ac80bSJonas Devlieghere             std::make_shared<ThreadPlanStepOverRange>(
476796ac80bSJonas Devlieghere                 m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug);
477b9c1b51eSKate Stone         ThreadPlanStepOverRange *step_through_inline_plan_ptr =
478b9c1b51eSKate Stone             static_cast<ThreadPlanStepOverRange *>(
479b9c1b51eSKate Stone                 m_step_through_inline_plan_sp.get());
4802bdbfd50SJim Ingham         m_step_through_inline_plan_sp->SetPrivate(true);
4812bdbfd50SJim Ingham 
482a5ce6c88SJim Ingham         step_through_inline_plan_ptr->SetOkayToDiscard(true);
483a5ce6c88SJim Ingham         StreamString errors;
484b9c1b51eSKate Stone         if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
485a5ce6c88SJim Ingham           // FIXME: Log this failure.
486a5ce6c88SJim Ingham           delete step_through_inline_plan_ptr;
487a5ce6c88SJim Ingham           return false;
488a5ce6c88SJim Ingham         }
489a5ce6c88SJim Ingham 
490b9c1b51eSKate Stone         for (size_t i = 1; i < num_ranges; i++) {
491a5ce6c88SJim Ingham           if (inlined_block->GetRangeAtIndex(i, inline_range))
492a5ce6c88SJim Ingham             step_through_inline_plan_ptr->AddRange(inline_range);
493a5ce6c88SJim Ingham         }
4942bdbfd50SJim Ingham 
495a5ce6c88SJim Ingham         if (queue_now)
496a5ce6c88SJim Ingham           m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
497a5ce6c88SJim Ingham         return true;
498a5ce6c88SJim Ingham       }
499a5ce6c88SJim Ingham     }
500a5ce6c88SJim Ingham   }
501a5ce6c88SJim Ingham 
502a5ce6c88SJim Ingham   return false;
503a5ce6c88SJim Ingham }
50473ca05a2SJim Ingham 
505b9c1b51eSKate Stone void ThreadPlanStepOut::CalculateReturnValue() {
50673ca05a2SJim Ingham   if (m_return_valobj_sp)
50773ca05a2SJim Ingham     return;
50873ca05a2SJim Ingham 
509b612ac37SJim Ingham   if (!m_calculate_return_value)
510b612ac37SJim Ingham     return;
511b612ac37SJim Ingham 
512b9c1b51eSKate Stone   if (m_immediate_step_from_function != nullptr) {
513b9c1b51eSKate Stone     CompilerType return_compiler_type =
514b9c1b51eSKate Stone         m_immediate_step_from_function->GetCompilerType()
515b9c1b51eSKate Stone             .GetFunctionReturnType();
516b9c1b51eSKate Stone     if (return_compiler_type) {
5171ac04c30SGreg Clayton       lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
51873ca05a2SJim Ingham       if (abi_sp)
519b9c1b51eSKate Stone         m_return_valobj_sp =
520b9c1b51eSKate Stone             abi_sp->GetReturnValueObject(m_thread, return_compiler_type);
52173ca05a2SJim Ingham     }
52273ca05a2SJim Ingham   }
52373ca05a2SJim Ingham }
52464e7ead1SJim Ingham 
525b9c1b51eSKate Stone bool ThreadPlanStepOut::IsPlanStale() {
52605097246SAdrian Prantl   // If we are still lower on the stack than the frame we are returning to,
52705097246SAdrian Prantl   // then there's something for us to do.  Otherwise, we're stale.
52864e7ead1SJim Ingham 
52964e7ead1SJim Ingham   StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
530e65b2cf2SEugene Zelenko   return !(frame_zero_id < m_step_out_to_id);
53164e7ead1SJim Ingham }
532