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"
24*c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
256f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2630fdc8d8SChris Lattner
27796ac80bSJonas Devlieghere #include <memory>
28796ac80bSJonas Devlieghere
2930fdc8d8SChris Lattner using namespace lldb;
3030fdc8d8SChris Lattner using namespace lldb_private;
3130fdc8d8SChris Lattner
324b4b2478SJim Ingham uint32_t ThreadPlanStepOut::s_default_flag_values = 0;
334b4b2478SJim Ingham
3430fdc8d8SChris Lattner // ThreadPlanStepOut: Step out of the current frame
ThreadPlanStepOut(Thread & thread,SymbolContext * context,bool first_insn,bool stop_others,Vote report_stop_vote,Vote report_run_vote,uint32_t frame_idx,LazyBool step_out_avoids_code_without_debug_info,bool continue_to_next_branch,bool gather_return_value)35b9c1b51eSKate Stone ThreadPlanStepOut::ThreadPlanStepOut(
36b9c1b51eSKate Stone Thread &thread, SymbolContext *context, bool first_insn, bool stop_others,
379d3b9e57SDave Lee Vote report_stop_vote, Vote report_run_vote, uint32_t frame_idx,
38fd4cea53SJason Molenda LazyBool step_out_avoids_code_without_debug_info,
39b9c1b51eSKate Stone bool continue_to_next_branch, bool gather_return_value)
409d3b9e57SDave Lee : ThreadPlan(ThreadPlan::eKindStepOut, "Step out", thread, report_stop_vote,
419d3b9e57SDave Lee report_run_vote),
42b9c1b51eSKate Stone ThreadPlanShouldStopHere(this), m_step_from_insn(LLDB_INVALID_ADDRESS),
431ee0d4f7SBenjamin Kramer m_return_bp_id(LLDB_INVALID_BREAK_ID),
44b9c1b51eSKate Stone m_return_addr(LLDB_INVALID_ADDRESS), m_stop_others(stop_others),
45b612ac37SJim Ingham m_immediate_step_from_function(nullptr),
46b9c1b51eSKate Stone m_calculate_return_value(gather_return_value) {
47a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Step);
484b4b2478SJim Ingham SetFlagsToDefault();
494b4b2478SJim Ingham SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
504b4b2478SJim Ingham
51e4598dc0SJim Ingham m_step_from_insn = thread.GetRegisterContext()->GetPC(0);
5230fdc8d8SChris Lattner
534b36f791SVedant Kumar uint32_t return_frame_index = frame_idx + 1;
54e4598dc0SJim Ingham StackFrameSP return_frame_sp(thread.GetStackFrameAtIndex(return_frame_index));
55e4598dc0SJim Ingham StackFrameSP immediate_return_from_sp(thread.GetStackFrameAtIndex(frame_idx));
56a5ce6c88SJim Ingham
57632d2f72SSean Callanan if (!return_frame_sp || !immediate_return_from_sp)
58632d2f72SSean Callanan return; // we can't do anything here. ValidatePlan() will return false.
59632d2f72SSean Callanan
604b36f791SVedant Kumar // While stepping out, behave as-if artificial frames are not present.
614b36f791SVedant Kumar while (return_frame_sp->IsArtificial()) {
624b36f791SVedant Kumar m_stepped_past_frames.push_back(return_frame_sp);
634b36f791SVedant Kumar
644b36f791SVedant Kumar ++return_frame_index;
65e4598dc0SJim Ingham return_frame_sp = thread.GetStackFrameAtIndex(return_frame_index);
664b36f791SVedant Kumar
674b36f791SVedant Kumar // We never expect to see an artificial frame without a regular ancestor.
684b36f791SVedant Kumar // If this happens, log the issue and defensively refuse to step out.
694b36f791SVedant Kumar if (!return_frame_sp) {
704b36f791SVedant Kumar LLDB_LOG(log, "Can't step out of frame with artificial ancestors");
714b36f791SVedant Kumar return;
724b36f791SVedant Kumar }
734b36f791SVedant Kumar }
744b36f791SVedant Kumar
75b5c0d1ccSJim Ingham m_step_out_to_id = return_frame_sp->GetStackID();
76b5c0d1ccSJim Ingham m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
77a5ce6c88SJim Ingham
7805097246SAdrian Prantl // If the frame directly below the one we are returning to is inlined, we
7905097246SAdrian Prantl // have to be a little more careful. It is non-trivial to determine the real
8005097246SAdrian Prantl // "return code address" for an inlined frame, so we have to work our way to
8105097246SAdrian Prantl // that frame and then step out.
824b36f791SVedant Kumar if (immediate_return_from_sp->IsInlined()) {
83b9c1b51eSKate Stone if (frame_idx > 0) {
84b9c1b51eSKate Stone // First queue a plan that gets us to this inlined frame, and when we get
8505097246SAdrian Prantl // there we'll queue a second plan that walks us out of this frame.
86796ac80bSJonas Devlieghere m_step_out_to_inline_plan_sp = std::make_shared<ThreadPlanStepOut>(
87e4598dc0SJim Ingham thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
88796ac80bSJonas Devlieghere frame_idx - 1, eLazyBoolNo, continue_to_next_branch);
89b9c1b51eSKate Stone static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())
90b9c1b51eSKate Stone ->SetShouldStopHereCallbacks(nullptr, nullptr);
912bdbfd50SJim Ingham m_step_out_to_inline_plan_sp->SetPrivate(true);
92b9c1b51eSKate Stone } else {
9305097246SAdrian Prantl // If we're already at the inlined frame we're stepping through, then
9405097246SAdrian Prantl // just do that now.
95a5ce6c88SJim Ingham QueueInlinedStepPlan(false);
96a5ce6c88SJim Ingham }
974b36f791SVedant Kumar } else {
9830fdc8d8SChris Lattner // Find the return address and set a breakpoint there:
9930fdc8d8SChris Lattner // FIXME - can we do this more securely if we know first_insn?
10030fdc8d8SChris Lattner
101fd4cea53SJason Molenda Address return_address(return_frame_sp->GetFrameCodeAddress());
102b9c1b51eSKate Stone if (continue_to_next_branch) {
103fd4cea53SJason Molenda SymbolContext return_address_sc;
104fd4cea53SJason Molenda AddressRange range;
105fd4cea53SJason Molenda Address return_address_decr_pc = return_address;
106fd4cea53SJason Molenda if (return_address_decr_pc.GetOffset() > 0)
107fd4cea53SJason Molenda return_address_decr_pc.Slide(-1);
108fd4cea53SJason Molenda
109b9c1b51eSKate Stone return_address_decr_pc.CalculateSymbolContext(
110b9c1b51eSKate Stone &return_address_sc, lldb::eSymbolContextLineEntry);
111b9c1b51eSKate Stone if (return_address_sc.line_entry.IsValid()) {
1128a777920SGreg Clayton const bool include_inlined_functions = false;
1138a777920SGreg Clayton range = return_address_sc.line_entry.GetSameLineContiguousAddressRange(
1148a777920SGreg Clayton include_inlined_functions);
115b9c1b51eSKate Stone if (range.GetByteSize() > 0) {
116e4598dc0SJim Ingham return_address = m_process.AdvanceAddressToNextBranchInstruction(
117b9c1b51eSKate Stone return_address, range);
118fd4cea53SJason Molenda }
119fd4cea53SJason Molenda }
120fd4cea53SJason Molenda }
121e4598dc0SJim Ingham m_return_addr = return_address.GetLoadAddress(&m_process.GetTarget());
122708709c0SSean Callanan
123708709c0SSean Callanan if (m_return_addr == LLDB_INVALID_ADDRESS)
124708709c0SSean Callanan return;
125708709c0SSean Callanan
1262a42a5a2SJim Ingham // Perform some additional validation on the return address.
1272a42a5a2SJim Ingham uint32_t permissions = 0;
128e4598dc0SJim Ingham if (!m_process.GetLoadAddressPermissions(m_return_addr, permissions)) {
1296fd818c5STed Woodward LLDB_LOGF(log, "ThreadPlanStepOut(%p): Return address (0x%" PRIx64
1306fd818c5STed Woodward ") permissions not found.", static_cast<void *>(this),
1312a42a5a2SJim Ingham m_return_addr);
1322a42a5a2SJim Ingham } else if (!(permissions & ePermissionsExecutable)) {
1332a42a5a2SJim Ingham m_constructor_errors.Printf("Return address (0x%" PRIx64
1342a42a5a2SJim Ingham ") did not point to executable memory.",
1352a42a5a2SJim Ingham m_return_addr);
1362a42a5a2SJim Ingham LLDB_LOGF(log, "ThreadPlanStepOut(%p): %s", static_cast<void *>(this),
1372a42a5a2SJim Ingham m_constructor_errors.GetData());
1382a42a5a2SJim Ingham return;
1392a42a5a2SJim Ingham }
1402a42a5a2SJim Ingham
141e4598dc0SJim Ingham Breakpoint *return_bp =
142e4598dc0SJim Ingham GetTarget().CreateBreakpoint(m_return_addr, true, false).get();
143e103ae92SJonas Devlieghere
144b9c1b51eSKate Stone if (return_bp != nullptr) {
145e103ae92SJonas Devlieghere if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
146e103ae92SJonas Devlieghere m_could_not_resolve_hw_bp = true;
147e4598dc0SJim Ingham return_bp->SetThreadID(m_tid);
14830fdc8d8SChris Lattner m_return_bp_id = return_bp->GetID();
1492995077dSJim Ingham return_bp->SetBreakpointKind("step-out");
15030fdc8d8SChris Lattner }
15173ca05a2SJim Ingham
152b9c1b51eSKate Stone if (immediate_return_from_sp) {
153b9c1b51eSKate Stone const SymbolContext &sc =
154b9c1b51eSKate Stone immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
155b9c1b51eSKate Stone if (sc.function) {
15673ca05a2SJim Ingham m_immediate_step_from_function = sc.function;
15773ca05a2SJim Ingham }
15873ca05a2SJim Ingham }
15930fdc8d8SChris Lattner }
160a5ce6c88SJim Ingham }
161a5ce6c88SJim Ingham
SetupAvoidNoDebug(LazyBool step_out_avoids_code_without_debug_info)162b9c1b51eSKate Stone void ThreadPlanStepOut::SetupAvoidNoDebug(
163b9c1b51eSKate Stone LazyBool step_out_avoids_code_without_debug_info) {
1644b4b2478SJim Ingham bool avoid_nodebug = true;
165b9c1b51eSKate Stone switch (step_out_avoids_code_without_debug_info) {
1664b4b2478SJim Ingham case eLazyBoolYes:
1674b4b2478SJim Ingham avoid_nodebug = true;
1684b4b2478SJim Ingham break;
1694b4b2478SJim Ingham case eLazyBoolNo:
1704b4b2478SJim Ingham avoid_nodebug = false;
1714b4b2478SJim Ingham break;
1724b4b2478SJim Ingham case eLazyBoolCalculate:
173e4598dc0SJim Ingham avoid_nodebug = GetThread().GetStepOutAvoidsNoDebug();
1744b4b2478SJim Ingham break;
1754b4b2478SJim Ingham }
1764b4b2478SJim Ingham if (avoid_nodebug)
1774b4b2478SJim Ingham GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1784b4b2478SJim Ingham else
1794b4b2478SJim Ingham GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1804b4b2478SJim Ingham }
1814b4b2478SJim Ingham
DidPush()182b9c1b51eSKate Stone void ThreadPlanStepOut::DidPush() {
183e4598dc0SJim Ingham Thread &thread = GetThread();
1844b4b2478SJim Ingham if (m_step_out_to_inline_plan_sp)
185e4598dc0SJim Ingham thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
186a5ce6c88SJim Ingham else if (m_step_through_inline_plan_sp)
187e4598dc0SJim Ingham thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
18830fdc8d8SChris Lattner }
18930fdc8d8SChris Lattner
~ThreadPlanStepOut()190b9c1b51eSKate Stone ThreadPlanStepOut::~ThreadPlanStepOut() {
19130fdc8d8SChris Lattner if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
1921893065dSJim Ingham GetTarget().RemoveBreakpointByID(m_return_bp_id);
19330fdc8d8SChris Lattner }
19430fdc8d8SChris Lattner
GetDescription(Stream * s,lldb::DescriptionLevel level)195b9c1b51eSKate Stone void ThreadPlanStepOut::GetDescription(Stream *s,
196b9c1b51eSKate Stone lldb::DescriptionLevel level) {
19730fdc8d8SChris Lattner if (level == lldb::eDescriptionLevelBrief)
19830fdc8d8SChris Lattner s->Printf("step out");
199b9c1b51eSKate Stone else {
2004b4b2478SJim Ingham if (m_step_out_to_inline_plan_sp)
201b5c0d1ccSJim Ingham s->Printf("Stepping out to inlined frame so we can walk through it.");
202a5ce6c88SJim Ingham else if (m_step_through_inline_plan_sp)
203a5ce6c88SJim Ingham s->Printf("Stepping out by stepping through inlined function.");
204b9c1b51eSKate Stone else {
2052bdbfd50SJim Ingham s->Printf("Stepping out from ");
2062bdbfd50SJim Ingham Address tmp_address;
207b9c1b51eSKate Stone if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) {
2081893065dSJim Ingham tmp_address.Dump(s, &m_process, Address::DumpStyleResolvedDescription,
209b9c1b51eSKate Stone Address::DumpStyleLoadAddress);
210b9c1b51eSKate Stone } else {
2112bdbfd50SJim Ingham s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn);
2122bdbfd50SJim Ingham }
2132bdbfd50SJim Ingham
214b9c1b51eSKate Stone // FIXME: find some useful way to present the m_return_id, since there may
215b9c1b51eSKate Stone // be multiple copies of the
2162bdbfd50SJim Ingham // same function on the stack.
2172bdbfd50SJim Ingham
2182bdbfd50SJim Ingham s->Printf(" returning to frame at ");
219b9c1b51eSKate Stone if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) {
2201893065dSJim Ingham tmp_address.Dump(s, &m_process, Address::DumpStyleResolvedDescription,
221b9c1b51eSKate Stone Address::DumpStyleLoadAddress);
222b9c1b51eSKate Stone } else {
2232bdbfd50SJim Ingham s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
2242bdbfd50SJim Ingham }
2252bdbfd50SJim Ingham
2262bdbfd50SJim Ingham if (level == eDescriptionLevelVerbose)
2272bdbfd50SJim Ingham s->Printf(" using breakpoint site %d", m_return_bp_id);
2282bdbfd50SJim Ingham }
22930fdc8d8SChris Lattner }
2304b36f791SVedant Kumar
2311893065dSJim Ingham if (m_stepped_past_frames.empty())
2321893065dSJim Ingham return;
2331893065dSJim Ingham
2344b36f791SVedant Kumar s->Printf("\n");
2354b36f791SVedant Kumar for (StackFrameSP frame_sp : m_stepped_past_frames) {
2364b36f791SVedant Kumar s->Printf("Stepped out past: ");
2374b36f791SVedant Kumar frame_sp->DumpUsingSettingsFormat(s);
2384b36f791SVedant Kumar }
23930fdc8d8SChris Lattner }
24030fdc8d8SChris Lattner
ValidatePlan(Stream * error)241b9c1b51eSKate Stone bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
2424b4b2478SJim Ingham if (m_step_out_to_inline_plan_sp)
2434b4b2478SJim Ingham return m_step_out_to_inline_plan_sp->ValidatePlan(error);
244e103ae92SJonas Devlieghere
245e103ae92SJonas Devlieghere if (m_step_through_inline_plan_sp)
246a5ce6c88SJim Ingham return m_step_through_inline_plan_sp->ValidatePlan(error);
247e103ae92SJonas Devlieghere
248e103ae92SJonas Devlieghere if (m_could_not_resolve_hw_bp) {
249e103ae92SJonas Devlieghere if (error)
250e103ae92SJonas Devlieghere error->PutCString(
251e103ae92SJonas Devlieghere "Could not create hardware breakpoint for thread plan.");
252e103ae92SJonas Devlieghere return false;
253e103ae92SJonas Devlieghere }
254e103ae92SJonas Devlieghere
255e103ae92SJonas Devlieghere if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
2562a42a5a2SJim Ingham if (error) {
257a5ce6c88SJim Ingham error->PutCString("Could not create return address breakpoint.");
2582a42a5a2SJim Ingham if (m_constructor_errors.GetSize() > 0) {
2592a42a5a2SJim Ingham error->PutCString(" ");
2602a42a5a2SJim Ingham error->PutCString(m_constructor_errors.GetString());
2612a42a5a2SJim Ingham }
2622a42a5a2SJim Ingham }
26330fdc8d8SChris Lattner return false;
264e103ae92SJonas Devlieghere }
265e103ae92SJonas Devlieghere
26630fdc8d8SChris Lattner return true;
26730fdc8d8SChris Lattner }
26830fdc8d8SChris Lattner
DoPlanExplainsStop(Event * event_ptr)269b9c1b51eSKate Stone bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
27005097246SAdrian Prantl // If the step out plan is done, then we just need to step through the
27105097246SAdrian Prantl // inlined frame.
272b9c1b51eSKate Stone if (m_step_out_to_inline_plan_sp) {
273e65b2cf2SEugene Zelenko return m_step_out_to_inline_plan_sp->MischiefManaged();
274b9c1b51eSKate Stone } else if (m_step_through_inline_plan_sp) {
275b9c1b51eSKate Stone if (m_step_through_inline_plan_sp->MischiefManaged()) {
27673ca05a2SJim Ingham CalculateReturnValue();
277a5ce6c88SJim Ingham SetPlanComplete();
278a5ce6c88SJim Ingham return true;
279b9c1b51eSKate Stone } else
280a5ce6c88SJim Ingham return false;
281b9c1b51eSKate Stone } else if (m_step_out_further_plan_sp) {
282e65b2cf2SEugene Zelenko return m_step_out_further_plan_sp->MischiefManaged();
283a5ce6c88SJim Ingham }
284a5ce6c88SJim Ingham
285b9c1b51eSKate Stone // We don't explain signals or breakpoints (breakpoints that handle stepping
28605097246SAdrian Prantl // in or out will be handled by a child plan.
287a5ce6c88SJim Ingham
28860c4118cSJim Ingham StopInfoSP stop_info_sp = GetPrivateStopInfo();
289b9c1b51eSKate Stone if (stop_info_sp) {
290b15bfc75SJim Ingham StopReason reason = stop_info_sp->GetStopReason();
291b9c1b51eSKate Stone if (reason == eStopReasonBreakpoint) {
29205097246SAdrian Prantl // If this is OUR breakpoint, we're fine, otherwise we don't know why
29305097246SAdrian Prantl // this happened...
294b9c1b51eSKate Stone BreakpointSiteSP site_sp(
295e4598dc0SJim Ingham m_process.GetBreakpointSiteList().FindByID(stop_info_sp->GetValue()));
296b9c1b51eSKate Stone if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
297b5c0d1ccSJim Ingham bool done;
298b5c0d1ccSJim Ingham
299e4598dc0SJim Ingham StackID frame_zero_id =
300e4598dc0SJim Ingham GetThread().GetStackFrameAtIndex(0)->GetStackID();
301b5c0d1ccSJim Ingham
302b5c0d1ccSJim Ingham if (m_step_out_to_id == frame_zero_id)
303b5c0d1ccSJim Ingham done = true;
304b9c1b51eSKate Stone else if (m_step_out_to_id < frame_zero_id) {
305b5c0d1ccSJim Ingham // Either we stepped past the breakpoint, or the stack ID calculation
306b5c0d1ccSJim Ingham // was incorrect and we should probably stop.
307b5c0d1ccSJim Ingham done = true;
308b9c1b51eSKate Stone } else {
309e65b2cf2SEugene Zelenko done = (m_immediate_step_from_id < frame_zero_id);
310b5c0d1ccSJim Ingham }
311b5c0d1ccSJim Ingham
312b9c1b51eSKate Stone if (done) {
313e103ae92SJonas Devlieghere if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
31473ca05a2SJim Ingham CalculateReturnValue();
315481cef25SGreg Clayton SetPlanComplete();
31673ca05a2SJim Ingham }
3174b4b2478SJim Ingham }
318481cef25SGreg Clayton
319b9c1b51eSKate Stone // If there was only one owner, then we're done. But if we also hit
32005097246SAdrian Prantl // some user breakpoint on our way out, we should mark ourselves as
32105097246SAdrian Prantl // done, but also not claim to explain the stop, since it is more
32205097246SAdrian Prantl // important to report the user breakpoint than the step out
32305097246SAdrian Prantl // completion.
32430fdc8d8SChris Lattner
325f4b47e15SGreg Clayton if (site_sp->GetNumberOfOwners() == 1)
32630fdc8d8SChris Lattner return true;
32730fdc8d8SChris Lattner }
32830fdc8d8SChris Lattner return false;
329b9c1b51eSKate Stone } else if (IsUsuallyUnexplainedStopReason(reason))
33030fdc8d8SChris Lattner return false;
3319b03fa0cSJim Ingham else
33230fdc8d8SChris Lattner return true;
33330fdc8d8SChris Lattner }
33430fdc8d8SChris Lattner return true;
33530fdc8d8SChris Lattner }
33630fdc8d8SChris Lattner
ShouldStop(Event * event_ptr)337b9c1b51eSKate Stone bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
338a5ce6c88SJim Ingham if (IsPlanComplete())
339a5ce6c88SJim Ingham return true;
340b5c0d1ccSJim Ingham
3414b4b2478SJim Ingham bool done = false;
342b9c1b51eSKate Stone if (m_step_out_to_inline_plan_sp) {
343b9c1b51eSKate Stone if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
3444b4b2478SJim Ingham // Now step through the inlined stack we are in:
345b9c1b51eSKate Stone if (QueueInlinedStepPlan(true)) {
3464b4b2478SJim Ingham // If we can't queue a plan to do this, then just call ourselves done.
3474b4b2478SJim Ingham m_step_out_to_inline_plan_sp.reset();
3484b4b2478SJim Ingham SetPlanComplete(false);
3494b4b2478SJim Ingham return true;
350b9c1b51eSKate Stone } else
3514b4b2478SJim Ingham done = true;
352b9c1b51eSKate Stone } else
3534b4b2478SJim Ingham return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
354b9c1b51eSKate Stone } else if (m_step_through_inline_plan_sp) {
3554b4b2478SJim Ingham if (m_step_through_inline_plan_sp->MischiefManaged())
3564b4b2478SJim Ingham done = true;
3574b4b2478SJim Ingham else
3584b4b2478SJim Ingham return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
359b9c1b51eSKate Stone } else if (m_step_out_further_plan_sp) {
3604b4b2478SJim Ingham if (m_step_out_further_plan_sp->MischiefManaged())
3614b4b2478SJim Ingham m_step_out_further_plan_sp.reset();
3624b4b2478SJim Ingham else
3634b4b2478SJim Ingham return m_step_out_further_plan_sp->ShouldStop(event_ptr);
3644b4b2478SJim Ingham }
365b5c0d1ccSJim Ingham
366b9c1b51eSKate Stone if (!done) {
367e4598dc0SJim Ingham StackID frame_zero_id = GetThread().GetStackFrameAtIndex(0)->GetStackID();
368e65b2cf2SEugene Zelenko done = !(frame_zero_id < m_step_out_to_id);
3694b4b2478SJim Ingham }
3704b4b2478SJim Ingham
37105097246SAdrian Prantl // The normal step out computations think we are done, so all we need to do
37205097246SAdrian Prantl // is consult the ShouldStopHere, and we are done.
373b5c0d1ccSJim Ingham
374b9c1b51eSKate Stone if (done) {
375e103ae92SJonas Devlieghere if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
37673ca05a2SJim Ingham CalculateReturnValue();
377a5ce6c88SJim Ingham SetPlanComplete();
378b9c1b51eSKate Stone } else {
379b9c1b51eSKate Stone m_step_out_further_plan_sp =
380e103ae92SJonas Devlieghere QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder, m_status);
3814b4b2478SJim Ingham done = false;
382a5ce6c88SJim Ingham }
383a5ce6c88SJim Ingham }
3844b4b2478SJim Ingham
3854b4b2478SJim Ingham return done;
386a5ce6c88SJim Ingham }
38730fdc8d8SChris Lattner
StopOthers()388b9c1b51eSKate Stone bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
38930fdc8d8SChris Lattner
GetPlanRunState()390b9c1b51eSKate Stone StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
39130fdc8d8SChris Lattner
DoWillResume(StateType resume_state,bool current_plan)392b9c1b51eSKate Stone bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
393b9c1b51eSKate Stone bool current_plan) {
3944b4b2478SJim Ingham if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
395a5ce6c88SJim Ingham return true;
396a5ce6c88SJim Ingham
39730fdc8d8SChris Lattner if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
39830fdc8d8SChris Lattner return false;
39930fdc8d8SChris Lattner
400b9c1b51eSKate Stone if (current_plan) {
401e4598dc0SJim Ingham Breakpoint *return_bp = GetTarget().GetBreakpointByID(m_return_bp_id).get();
402e65b2cf2SEugene Zelenko if (return_bp != nullptr)
40330fdc8d8SChris Lattner return_bp->SetEnabled(true);
40430fdc8d8SChris Lattner }
40530fdc8d8SChris Lattner return true;
40630fdc8d8SChris Lattner }
40730fdc8d8SChris Lattner
WillStop()408b9c1b51eSKate Stone bool ThreadPlanStepOut::WillStop() {
409b9c1b51eSKate Stone if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
410e4598dc0SJim Ingham Breakpoint *return_bp = GetTarget().GetBreakpointByID(m_return_bp_id).get();
411e65b2cf2SEugene Zelenko if (return_bp != nullptr)
41230fdc8d8SChris Lattner return_bp->SetEnabled(false);
413a5ce6c88SJim Ingham }
414a5ce6c88SJim Ingham
41530fdc8d8SChris Lattner return true;
41630fdc8d8SChris Lattner }
41730fdc8d8SChris Lattner
MischiefManaged()418b9c1b51eSKate Stone bool ThreadPlanStepOut::MischiefManaged() {
419b9c1b51eSKate Stone if (IsPlanComplete()) {
42030fdc8d8SChris Lattner // Did I reach my breakpoint? If so I'm done.
42130fdc8d8SChris Lattner //
422b9c1b51eSKate Stone // I also check the stack depth, since if we've blown past the breakpoint
423b9c1b51eSKate Stone // for some
424b9c1b51eSKate Stone // reason and we're now stopping for some other reason altogether, then
42505097246SAdrian Prantl // we're done with this step out operation.
42630fdc8d8SChris Lattner
427a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Step);
42830fdc8d8SChris Lattner if (log)
42963e5fb76SJonas Devlieghere LLDB_LOGF(log, "Completed step out plan.");
430b9c1b51eSKate Stone if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
431e4598dc0SJim Ingham GetTarget().RemoveBreakpointByID(m_return_bp_id);
43230fdc8d8SChris Lattner m_return_bp_id = LLDB_INVALID_BREAK_ID;
433a5ce6c88SJim Ingham }
434a5ce6c88SJim Ingham
43530fdc8d8SChris Lattner ThreadPlan::MischiefManaged();
43630fdc8d8SChris Lattner return true;
437b9c1b51eSKate Stone } else {
43830fdc8d8SChris Lattner return false;
43930fdc8d8SChris Lattner }
44030fdc8d8SChris Lattner }
44130fdc8d8SChris Lattner
QueueInlinedStepPlan(bool queue_now)442b9c1b51eSKate Stone bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
443b9c1b51eSKate Stone // Now figure out the range of this inlined block, and set up a "step through
44405097246SAdrian Prantl // range" plan for that. If we've been provided with a context, then use the
44505097246SAdrian Prantl // block in that context.
446e4598dc0SJim Ingham Thread &thread = GetThread();
447e4598dc0SJim Ingham StackFrameSP immediate_return_from_sp(thread.GetStackFrameAtIndex(0));
448a5ce6c88SJim Ingham if (!immediate_return_from_sp)
449a5ce6c88SJim Ingham return false;
450a5ce6c88SJim Ingham
451a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Step);
452b9c1b51eSKate Stone if (log) {
453a5ce6c88SJim Ingham StreamString s;
454a5ce6c88SJim Ingham immediate_return_from_sp->Dump(&s, true, false);
45563e5fb76SJonas Devlieghere LLDB_LOGF(log, "Queuing inlined frame to step past: %s.", s.GetData());
456a5ce6c88SJim Ingham }
457a5ce6c88SJim Ingham
458a5ce6c88SJim Ingham Block *from_block = immediate_return_from_sp->GetFrameBlock();
459b9c1b51eSKate Stone if (from_block) {
460a5ce6c88SJim Ingham Block *inlined_block = from_block->GetContainingInlinedBlock();
461b9c1b51eSKate Stone if (inlined_block) {
462a5ce6c88SJim Ingham size_t num_ranges = inlined_block->GetNumRanges();
463a5ce6c88SJim Ingham AddressRange inline_range;
464b9c1b51eSKate Stone if (inlined_block->GetRangeAtIndex(0, inline_range)) {
465a5ce6c88SJim Ingham SymbolContext inlined_sc;
466a5ce6c88SJim Ingham inlined_block->CalculateSymbolContext(&inlined_sc);
4675f1a4e1fSJim Ingham inlined_sc.target_sp = GetTarget().shared_from_this();
468b9c1b51eSKate Stone RunMode run_mode =
469b9c1b51eSKate Stone m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
4704b4b2478SJim Ingham const LazyBool avoid_no_debug = eLazyBoolNo;
4712bdbfd50SJim Ingham
472796ac80bSJonas Devlieghere m_step_through_inline_plan_sp =
473796ac80bSJonas Devlieghere std::make_shared<ThreadPlanStepOverRange>(
474e4598dc0SJim Ingham thread, inline_range, inlined_sc, run_mode, avoid_no_debug);
475b9c1b51eSKate Stone ThreadPlanStepOverRange *step_through_inline_plan_ptr =
476b9c1b51eSKate Stone static_cast<ThreadPlanStepOverRange *>(
477b9c1b51eSKate Stone m_step_through_inline_plan_sp.get());
4782bdbfd50SJim Ingham m_step_through_inline_plan_sp->SetPrivate(true);
4792bdbfd50SJim Ingham
480a5ce6c88SJim Ingham step_through_inline_plan_ptr->SetOkayToDiscard(true);
481a5ce6c88SJim Ingham StreamString errors;
482b9c1b51eSKate Stone if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
483a5ce6c88SJim Ingham // FIXME: Log this failure.
484a5ce6c88SJim Ingham delete step_through_inline_plan_ptr;
485a5ce6c88SJim Ingham return false;
486a5ce6c88SJim Ingham }
487a5ce6c88SJim Ingham
488b9c1b51eSKate Stone for (size_t i = 1; i < num_ranges; i++) {
489a5ce6c88SJim Ingham if (inlined_block->GetRangeAtIndex(i, inline_range))
490a5ce6c88SJim Ingham step_through_inline_plan_ptr->AddRange(inline_range);
491a5ce6c88SJim Ingham }
4922bdbfd50SJim Ingham
493a5ce6c88SJim Ingham if (queue_now)
494e4598dc0SJim Ingham thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
495a5ce6c88SJim Ingham return true;
496a5ce6c88SJim Ingham }
497a5ce6c88SJim Ingham }
498a5ce6c88SJim Ingham }
499a5ce6c88SJim Ingham
500a5ce6c88SJim Ingham return false;
501a5ce6c88SJim Ingham }
50273ca05a2SJim Ingham
CalculateReturnValue()503b9c1b51eSKate Stone void ThreadPlanStepOut::CalculateReturnValue() {
50473ca05a2SJim Ingham if (m_return_valobj_sp)
50573ca05a2SJim Ingham return;
50673ca05a2SJim Ingham
507b612ac37SJim Ingham if (!m_calculate_return_value)
508b612ac37SJim Ingham return;
509b612ac37SJim Ingham
510b9c1b51eSKate Stone if (m_immediate_step_from_function != nullptr) {
511b9c1b51eSKate Stone CompilerType return_compiler_type =
512b9c1b51eSKate Stone m_immediate_step_from_function->GetCompilerType()
513b9c1b51eSKate Stone .GetFunctionReturnType();
514b9c1b51eSKate Stone if (return_compiler_type) {
515e4598dc0SJim Ingham lldb::ABISP abi_sp = m_process.GetABI();
51673ca05a2SJim Ingham if (abi_sp)
517b9c1b51eSKate Stone m_return_valobj_sp =
518e4598dc0SJim Ingham abi_sp->GetReturnValueObject(GetThread(), return_compiler_type);
51973ca05a2SJim Ingham }
52073ca05a2SJim Ingham }
52173ca05a2SJim Ingham }
52264e7ead1SJim Ingham
IsPlanStale()523b9c1b51eSKate Stone bool ThreadPlanStepOut::IsPlanStale() {
52405097246SAdrian Prantl // If we are still lower on the stack than the frame we are returning to,
52505097246SAdrian Prantl // then there's something for us to do. Otherwise, we're stale.
52664e7ead1SJim Ingham
527e4598dc0SJim Ingham StackID frame_zero_id = GetThread().GetStackFrameAtIndex(0)->GetStackID();
528e65b2cf2SEugene Zelenko return !(frame_zero_id < m_step_out_to_id);
52964e7ead1SJim Ingham }
530