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 
50e4598dc0SJim Ingham   m_step_from_insn = thread.GetRegisterContext()->GetPC(0);
5130fdc8d8SChris Lattner 
524b36f791SVedant Kumar   uint32_t return_frame_index = frame_idx + 1;
53e4598dc0SJim Ingham   StackFrameSP return_frame_sp(thread.GetStackFrameAtIndex(return_frame_index));
54e4598dc0SJim 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;
64e4598dc0SJim 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>(
86e4598dc0SJim 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) {
115e4598dc0SJim Ingham           return_address = m_process.AdvanceAddressToNextBranchInstruction(
116b9c1b51eSKate Stone               return_address, range);
117fd4cea53SJason Molenda         }
118fd4cea53SJason Molenda       }
119fd4cea53SJason Molenda     }
120e4598dc0SJim 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;
127e4598dc0SJim 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 
140e4598dc0SJim Ingham     Breakpoint *return_bp =
141e4598dc0SJim 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;
146e4598dc0SJim 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:
172e4598dc0SJim 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() {
182e4598dc0SJim Ingham   Thread &thread = GetThread();
1834b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
184e4598dc0SJim Ingham     thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
185a5ce6c88SJim Ingham   else if (m_step_through_inline_plan_sp)
186e4598dc0SJim 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*1893065dSJim Ingham     GetTarget().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())) {
207*1893065dSJim Ingham         tmp_address.Dump(s, &m_process, 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())) {
219*1893065dSJim Ingham         tmp_address.Dump(s, &m_process, 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 
230*1893065dSJim Ingham   if (m_stepped_past_frames.empty())
231*1893065dSJim Ingham     return;
232*1893065dSJim Ingham 
2334b36f791SVedant Kumar   s->Printf("\n");
2344b36f791SVedant Kumar   for (StackFrameSP frame_sp : m_stepped_past_frames) {
2354b36f791SVedant Kumar     s->Printf("Stepped out past: ");
2364b36f791SVedant Kumar     frame_sp->DumpUsingSettingsFormat(s);
2374b36f791SVedant Kumar   }
23830fdc8d8SChris Lattner }
23930fdc8d8SChris Lattner 
240b9c1b51eSKate Stone bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
2414b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp)
2424b4b2478SJim Ingham     return m_step_out_to_inline_plan_sp->ValidatePlan(error);
243e103ae92SJonas Devlieghere 
244e103ae92SJonas Devlieghere   if (m_step_through_inline_plan_sp)
245a5ce6c88SJim Ingham     return m_step_through_inline_plan_sp->ValidatePlan(error);
246e103ae92SJonas Devlieghere 
247e103ae92SJonas Devlieghere   if (m_could_not_resolve_hw_bp) {
248e103ae92SJonas Devlieghere     if (error)
249e103ae92SJonas Devlieghere       error->PutCString(
250e103ae92SJonas Devlieghere           "Could not create hardware breakpoint for thread plan.");
251e103ae92SJonas Devlieghere     return false;
252e103ae92SJonas Devlieghere   }
253e103ae92SJonas Devlieghere 
254e103ae92SJonas Devlieghere   if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
2552a42a5a2SJim Ingham     if (error) {
256a5ce6c88SJim Ingham       error->PutCString("Could not create return address breakpoint.");
2572a42a5a2SJim Ingham       if (m_constructor_errors.GetSize() > 0) {
2582a42a5a2SJim Ingham         error->PutCString(" ");
2592a42a5a2SJim Ingham         error->PutCString(m_constructor_errors.GetString());
2602a42a5a2SJim Ingham       }
2612a42a5a2SJim Ingham     }
26230fdc8d8SChris Lattner     return false;
263e103ae92SJonas Devlieghere   }
264e103ae92SJonas Devlieghere 
26530fdc8d8SChris Lattner   return true;
26630fdc8d8SChris Lattner }
26730fdc8d8SChris Lattner 
268b9c1b51eSKate Stone bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
26905097246SAdrian Prantl   // If the step out plan is done, then we just need to step through the
27005097246SAdrian Prantl   // inlined frame.
271b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
272e65b2cf2SEugene Zelenko     return m_step_out_to_inline_plan_sp->MischiefManaged();
273b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
274b9c1b51eSKate Stone     if (m_step_through_inline_plan_sp->MischiefManaged()) {
27573ca05a2SJim Ingham       CalculateReturnValue();
276a5ce6c88SJim Ingham       SetPlanComplete();
277a5ce6c88SJim Ingham       return true;
278b9c1b51eSKate Stone     } else
279a5ce6c88SJim Ingham       return false;
280b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
281e65b2cf2SEugene Zelenko     return m_step_out_further_plan_sp->MischiefManaged();
282a5ce6c88SJim Ingham   }
283a5ce6c88SJim Ingham 
284b9c1b51eSKate Stone   // We don't explain signals or breakpoints (breakpoints that handle stepping
28505097246SAdrian Prantl   // in or out will be handled by a child plan.
286a5ce6c88SJim Ingham 
28760c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
288b9c1b51eSKate Stone   if (stop_info_sp) {
289b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
290b9c1b51eSKate Stone     if (reason == eStopReasonBreakpoint) {
29105097246SAdrian Prantl       // If this is OUR breakpoint, we're fine, otherwise we don't know why
29205097246SAdrian Prantl       // this happened...
293b9c1b51eSKate Stone       BreakpointSiteSP site_sp(
294e4598dc0SJim Ingham           m_process.GetBreakpointSiteList().FindByID(stop_info_sp->GetValue()));
295b9c1b51eSKate Stone       if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
296b5c0d1ccSJim Ingham         bool done;
297b5c0d1ccSJim Ingham 
298e4598dc0SJim Ingham         StackID frame_zero_id =
299e4598dc0SJim Ingham             GetThread().GetStackFrameAtIndex(0)->GetStackID();
300b5c0d1ccSJim Ingham 
301b5c0d1ccSJim Ingham         if (m_step_out_to_id == frame_zero_id)
302b5c0d1ccSJim Ingham           done = true;
303b9c1b51eSKate Stone         else if (m_step_out_to_id < frame_zero_id) {
304b5c0d1ccSJim Ingham           // Either we stepped past the breakpoint, or the stack ID calculation
305b5c0d1ccSJim Ingham           // was incorrect and we should probably stop.
306b5c0d1ccSJim Ingham           done = true;
307b9c1b51eSKate Stone         } else {
308e65b2cf2SEugene Zelenko           done = (m_immediate_step_from_id < frame_zero_id);
309b5c0d1ccSJim Ingham         }
310b5c0d1ccSJim Ingham 
311b9c1b51eSKate Stone         if (done) {
312e103ae92SJonas Devlieghere           if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
31373ca05a2SJim Ingham             CalculateReturnValue();
314481cef25SGreg Clayton             SetPlanComplete();
31573ca05a2SJim Ingham           }
3164b4b2478SJim Ingham         }
317481cef25SGreg Clayton 
318b9c1b51eSKate Stone         // If there was only one owner, then we're done.  But if we also hit
31905097246SAdrian Prantl         // some user breakpoint on our way out, we should mark ourselves as
32005097246SAdrian Prantl         // done, but also not claim to explain the stop, since it is more
32105097246SAdrian Prantl         // important to report the user breakpoint than the step out
32205097246SAdrian Prantl         // completion.
32330fdc8d8SChris Lattner 
324f4b47e15SGreg Clayton         if (site_sp->GetNumberOfOwners() == 1)
32530fdc8d8SChris Lattner           return true;
32630fdc8d8SChris Lattner       }
32730fdc8d8SChris Lattner       return false;
328b9c1b51eSKate Stone     } else if (IsUsuallyUnexplainedStopReason(reason))
32930fdc8d8SChris Lattner       return false;
3309b03fa0cSJim Ingham     else
33130fdc8d8SChris Lattner       return true;
33230fdc8d8SChris Lattner   }
33330fdc8d8SChris Lattner   return true;
33430fdc8d8SChris Lattner }
33530fdc8d8SChris Lattner 
336b9c1b51eSKate Stone bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
337a5ce6c88SJim Ingham   if (IsPlanComplete())
338a5ce6c88SJim Ingham     return true;
339b5c0d1ccSJim Ingham 
3404b4b2478SJim Ingham   bool done = false;
341b9c1b51eSKate Stone   if (m_step_out_to_inline_plan_sp) {
342b9c1b51eSKate Stone     if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
3434b4b2478SJim Ingham       // Now step through the inlined stack we are in:
344b9c1b51eSKate Stone       if (QueueInlinedStepPlan(true)) {
3454b4b2478SJim Ingham         // If we can't queue a plan to do this, then just call ourselves done.
3464b4b2478SJim Ingham         m_step_out_to_inline_plan_sp.reset();
3474b4b2478SJim Ingham         SetPlanComplete(false);
3484b4b2478SJim Ingham         return true;
349b9c1b51eSKate Stone       } else
3504b4b2478SJim Ingham         done = true;
351b9c1b51eSKate Stone     } else
3524b4b2478SJim Ingham       return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
353b9c1b51eSKate Stone   } else if (m_step_through_inline_plan_sp) {
3544b4b2478SJim Ingham     if (m_step_through_inline_plan_sp->MischiefManaged())
3554b4b2478SJim Ingham       done = true;
3564b4b2478SJim Ingham     else
3574b4b2478SJim Ingham       return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
358b9c1b51eSKate Stone   } else if (m_step_out_further_plan_sp) {
3594b4b2478SJim Ingham     if (m_step_out_further_plan_sp->MischiefManaged())
3604b4b2478SJim Ingham       m_step_out_further_plan_sp.reset();
3614b4b2478SJim Ingham     else
3624b4b2478SJim Ingham       return m_step_out_further_plan_sp->ShouldStop(event_ptr);
3634b4b2478SJim Ingham   }
364b5c0d1ccSJim Ingham 
365b9c1b51eSKate Stone   if (!done) {
366e4598dc0SJim Ingham     StackID frame_zero_id = GetThread().GetStackFrameAtIndex(0)->GetStackID();
367e65b2cf2SEugene Zelenko     done = !(frame_zero_id < m_step_out_to_id);
3684b4b2478SJim Ingham   }
3694b4b2478SJim Ingham 
37005097246SAdrian Prantl   // The normal step out computations think we are done, so all we need to do
37105097246SAdrian Prantl   // is consult the ShouldStopHere, and we are done.
372b5c0d1ccSJim Ingham 
373b9c1b51eSKate Stone   if (done) {
374e103ae92SJonas Devlieghere     if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
37573ca05a2SJim Ingham       CalculateReturnValue();
376a5ce6c88SJim Ingham       SetPlanComplete();
377b9c1b51eSKate Stone     } else {
378b9c1b51eSKate Stone       m_step_out_further_plan_sp =
379e103ae92SJonas Devlieghere           QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder, m_status);
3804b4b2478SJim Ingham       done = false;
381a5ce6c88SJim Ingham     }
382a5ce6c88SJim Ingham   }
3834b4b2478SJim Ingham 
3844b4b2478SJim Ingham   return done;
385a5ce6c88SJim Ingham }
38630fdc8d8SChris Lattner 
387b9c1b51eSKate Stone bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
38830fdc8d8SChris Lattner 
389b9c1b51eSKate Stone StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
39030fdc8d8SChris Lattner 
391b9c1b51eSKate Stone bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
392b9c1b51eSKate Stone                                      bool current_plan) {
3934b4b2478SJim Ingham   if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
394a5ce6c88SJim Ingham     return true;
395a5ce6c88SJim Ingham 
39630fdc8d8SChris Lattner   if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
39730fdc8d8SChris Lattner     return false;
39830fdc8d8SChris Lattner 
399b9c1b51eSKate Stone   if (current_plan) {
400e4598dc0SJim Ingham     Breakpoint *return_bp = GetTarget().GetBreakpointByID(m_return_bp_id).get();
401e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
40230fdc8d8SChris Lattner       return_bp->SetEnabled(true);
40330fdc8d8SChris Lattner   }
40430fdc8d8SChris Lattner   return true;
40530fdc8d8SChris Lattner }
40630fdc8d8SChris Lattner 
407b9c1b51eSKate Stone bool ThreadPlanStepOut::WillStop() {
408b9c1b51eSKate Stone   if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
409e4598dc0SJim Ingham     Breakpoint *return_bp = GetTarget().GetBreakpointByID(m_return_bp_id).get();
410e65b2cf2SEugene Zelenko     if (return_bp != nullptr)
41130fdc8d8SChris Lattner       return_bp->SetEnabled(false);
412a5ce6c88SJim Ingham   }
413a5ce6c88SJim Ingham 
41430fdc8d8SChris Lattner   return true;
41530fdc8d8SChris Lattner }
41630fdc8d8SChris Lattner 
417b9c1b51eSKate Stone bool ThreadPlanStepOut::MischiefManaged() {
418b9c1b51eSKate Stone   if (IsPlanComplete()) {
41930fdc8d8SChris Lattner     // Did I reach my breakpoint?  If so I'm done.
42030fdc8d8SChris Lattner     //
421b9c1b51eSKate Stone     // I also check the stack depth, since if we've blown past the breakpoint
422b9c1b51eSKate Stone     // for some
423b9c1b51eSKate Stone     // reason and we're now stopping for some other reason altogether, then
42405097246SAdrian Prantl     // we're done with this step out operation.
42530fdc8d8SChris Lattner 
4265160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
42730fdc8d8SChris Lattner     if (log)
42863e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Completed step out plan.");
429b9c1b51eSKate Stone     if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
430e4598dc0SJim Ingham       GetTarget().RemoveBreakpointByID(m_return_bp_id);
43130fdc8d8SChris Lattner       m_return_bp_id = LLDB_INVALID_BREAK_ID;
432a5ce6c88SJim Ingham     }
433a5ce6c88SJim Ingham 
43430fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
43530fdc8d8SChris Lattner     return true;
436b9c1b51eSKate Stone   } else {
43730fdc8d8SChris Lattner     return false;
43830fdc8d8SChris Lattner   }
43930fdc8d8SChris Lattner }
44030fdc8d8SChris Lattner 
441b9c1b51eSKate Stone bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
442b9c1b51eSKate Stone   // Now figure out the range of this inlined block, and set up a "step through
44305097246SAdrian Prantl   // range" plan for that.  If we've been provided with a context, then use the
44405097246SAdrian Prantl   // block in that context.
445e4598dc0SJim Ingham   Thread &thread = GetThread();
446e4598dc0SJim Ingham   StackFrameSP immediate_return_from_sp(thread.GetStackFrameAtIndex(0));
447a5ce6c88SJim Ingham   if (!immediate_return_from_sp)
448a5ce6c88SJim Ingham     return false;
449a5ce6c88SJim Ingham 
4505160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
451b9c1b51eSKate Stone   if (log) {
452a5ce6c88SJim Ingham     StreamString s;
453a5ce6c88SJim Ingham     immediate_return_from_sp->Dump(&s, true, false);
45463e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Queuing inlined frame to step past: %s.", s.GetData());
455a5ce6c88SJim Ingham   }
456a5ce6c88SJim Ingham 
457a5ce6c88SJim Ingham   Block *from_block = immediate_return_from_sp->GetFrameBlock();
458b9c1b51eSKate Stone   if (from_block) {
459a5ce6c88SJim Ingham     Block *inlined_block = from_block->GetContainingInlinedBlock();
460b9c1b51eSKate Stone     if (inlined_block) {
461a5ce6c88SJim Ingham       size_t num_ranges = inlined_block->GetNumRanges();
462a5ce6c88SJim Ingham       AddressRange inline_range;
463b9c1b51eSKate Stone       if (inlined_block->GetRangeAtIndex(0, inline_range)) {
464a5ce6c88SJim Ingham         SymbolContext inlined_sc;
465a5ce6c88SJim Ingham         inlined_block->CalculateSymbolContext(&inlined_sc);
4665f1a4e1fSJim Ingham         inlined_sc.target_sp = GetTarget().shared_from_this();
467b9c1b51eSKate Stone         RunMode run_mode =
468b9c1b51eSKate Stone             m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
4694b4b2478SJim Ingham         const LazyBool avoid_no_debug = eLazyBoolNo;
4702bdbfd50SJim Ingham 
471796ac80bSJonas Devlieghere         m_step_through_inline_plan_sp =
472796ac80bSJonas Devlieghere             std::make_shared<ThreadPlanStepOverRange>(
473e4598dc0SJim Ingham                 thread, inline_range, inlined_sc, run_mode, avoid_no_debug);
474b9c1b51eSKate Stone         ThreadPlanStepOverRange *step_through_inline_plan_ptr =
475b9c1b51eSKate Stone             static_cast<ThreadPlanStepOverRange *>(
476b9c1b51eSKate Stone                 m_step_through_inline_plan_sp.get());
4772bdbfd50SJim Ingham         m_step_through_inline_plan_sp->SetPrivate(true);
4782bdbfd50SJim Ingham 
479a5ce6c88SJim Ingham         step_through_inline_plan_ptr->SetOkayToDiscard(true);
480a5ce6c88SJim Ingham         StreamString errors;
481b9c1b51eSKate Stone         if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
482a5ce6c88SJim Ingham           // FIXME: Log this failure.
483a5ce6c88SJim Ingham           delete step_through_inline_plan_ptr;
484a5ce6c88SJim Ingham           return false;
485a5ce6c88SJim Ingham         }
486a5ce6c88SJim Ingham 
487b9c1b51eSKate Stone         for (size_t i = 1; i < num_ranges; i++) {
488a5ce6c88SJim Ingham           if (inlined_block->GetRangeAtIndex(i, inline_range))
489a5ce6c88SJim Ingham             step_through_inline_plan_ptr->AddRange(inline_range);
490a5ce6c88SJim Ingham         }
4912bdbfd50SJim Ingham 
492a5ce6c88SJim Ingham         if (queue_now)
493e4598dc0SJim Ingham           thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
494a5ce6c88SJim Ingham         return true;
495a5ce6c88SJim Ingham       }
496a5ce6c88SJim Ingham     }
497a5ce6c88SJim Ingham   }
498a5ce6c88SJim Ingham 
499a5ce6c88SJim Ingham   return false;
500a5ce6c88SJim Ingham }
50173ca05a2SJim Ingham 
502b9c1b51eSKate Stone void ThreadPlanStepOut::CalculateReturnValue() {
50373ca05a2SJim Ingham   if (m_return_valobj_sp)
50473ca05a2SJim Ingham     return;
50573ca05a2SJim Ingham 
506b612ac37SJim Ingham   if (!m_calculate_return_value)
507b612ac37SJim Ingham     return;
508b612ac37SJim Ingham 
509b9c1b51eSKate Stone   if (m_immediate_step_from_function != nullptr) {
510b9c1b51eSKate Stone     CompilerType return_compiler_type =
511b9c1b51eSKate Stone         m_immediate_step_from_function->GetCompilerType()
512b9c1b51eSKate Stone             .GetFunctionReturnType();
513b9c1b51eSKate Stone     if (return_compiler_type) {
514e4598dc0SJim Ingham       lldb::ABISP abi_sp = m_process.GetABI();
51573ca05a2SJim Ingham       if (abi_sp)
516b9c1b51eSKate Stone         m_return_valobj_sp =
517e4598dc0SJim Ingham             abi_sp->GetReturnValueObject(GetThread(), return_compiler_type);
51873ca05a2SJim Ingham     }
51973ca05a2SJim Ingham   }
52073ca05a2SJim Ingham }
52164e7ead1SJim Ingham 
522b9c1b51eSKate Stone bool ThreadPlanStepOut::IsPlanStale() {
52305097246SAdrian Prantl   // If we are still lower on the stack than the frame we are returning to,
52405097246SAdrian Prantl   // then there's something for us to do.  Otherwise, we're stale.
52564e7ead1SJim Ingham 
526e4598dc0SJim Ingham   StackID frame_zero_id = GetThread().GetStackFrameAtIndex(0)->GetStackID();
527e65b2cf2SEugene Zelenko   return !(frame_zero_id < m_step_out_to_id);
52864e7ead1SJim Ingham }
529