Raphael Isemann | 8081428 | 2020-01-24 07:23:27 | [diff] [blame] | 1 | //===-- ThreadPlanStepOut.cpp ---------------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 | [diff] [blame] | 9 | #include "lldb/Target/ThreadPlanStepOut.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 10 | #include "lldb/Breakpoint/Breakpoint.h" |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 | [diff] [blame] | 11 | #include "lldb/Core/Value.h" |
| 12 | #include "lldb/Core/ValueObjectConstResult.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 | [diff] [blame] | 13 | #include "lldb/Symbol/Block.h" |
| 14 | #include "lldb/Symbol/Function.h" |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 | [diff] [blame] | 15 | #include "lldb/Symbol/Symbol.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 | [diff] [blame] | 16 | #include "lldb/Symbol/Type.h" |
Zachary Turner | 32abc6e | 2015-03-03 19:23:09 | [diff] [blame] | 17 | #include "lldb/Target/ABI.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 18 | #include "lldb/Target/Process.h" |
| 19 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 | [diff] [blame] | 20 | #include "lldb/Target/StopInfo.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 21 | #include "lldb/Target/Target.h" |
Jim Ingham | a5ce6c8 | 2011-10-15 00:57:28 | [diff] [blame] | 22 | #include "lldb/Target/ThreadPlanStepOverRange.h" |
Jim Ingham | 4b4b247 | 2014-03-13 02:47:14 | [diff] [blame] | 23 | #include "lldb/Target/ThreadPlanStepThrough.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 | [diff] [blame] | 24 | #include "lldb/Utility/Log.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 25 | |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 | [diff] [blame] | 26 | #include <memory> |
| 27 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 28 | using namespace lldb; |
| 29 | using namespace lldb_private; |
| 30 | |
Jim Ingham | 4b4b247 | 2014-03-13 02:47:14 | [diff] [blame] | 31 | uint32_t ThreadPlanStepOut::s_default_flag_values = 0; |
| 32 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 33 | // ThreadPlanStepOut: Step out of the current frame |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 34 | ThreadPlanStepOut::ThreadPlanStepOut( |
| 35 | Thread &thread, SymbolContext *context, bool first_insn, bool stop_others, |
Dave Lee | 9d3b9e5 | 2021-02-17 23:09:50 | [diff] [blame] | 36 | Vote report_stop_vote, Vote report_run_vote, uint32_t frame_idx, |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 | [diff] [blame] | 37 | LazyBool step_out_avoids_code_without_debug_info, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 38 | bool continue_to_next_branch, bool gather_return_value) |
Dave Lee | 9d3b9e5 | 2021-02-17 23:09:50 | [diff] [blame] | 39 | : ThreadPlan(ThreadPlan::eKindStepOut, "Step out", thread, report_stop_vote, |
| 40 | report_run_vote), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 41 | ThreadPlanShouldStopHere(this), m_step_from_insn(LLDB_INVALID_ADDRESS), |
| 42 | m_return_bp_id(LLDB_INVALID_BREAK_ID), |
| 43 | m_return_addr(LLDB_INVALID_ADDRESS), m_stop_others(stop_others), |
| 44 | m_immediate_step_from_function(nullptr), |
| 45 | m_calculate_return_value(gather_return_value) { |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 46 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 47 | SetFlagsToDefault(); |
| 48 | SetupAvoidNoDebug(step_out_avoids_code_without_debug_info); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 49 | |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 50 | m_step_from_insn = thread.GetRegisterContext()->GetPC(0); |
Jim Ingham | a5ce6c8 | 2011-10-15 00:57:28 | [diff] [blame] | 51 | |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 52 | uint32_t return_frame_index = frame_idx + 1; |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 53 | StackFrameSP return_frame_sp(thread.GetStackFrameAtIndex(return_frame_index)); |
| 54 | StackFrameSP immediate_return_from_sp(thread.GetStackFrameAtIndex(frame_idx)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 55 | |
| 56 | if (!return_frame_sp || !immediate_return_from_sp) |
| 57 | return; // we can't do anything here. ValidatePlan() will return false. |
| 58 | |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 59 | // While stepping out, behave as-if artificial frames are not present. |
| 60 | while (return_frame_sp->IsArtificial()) { |
| 61 | m_stepped_past_frames.push_back(return_frame_sp); |
| 62 | |
| 63 | ++return_frame_index; |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 64 | return_frame_sp = thread.GetStackFrameAtIndex(return_frame_index); |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 65 | |
| 66 | // We never expect to see an artificial frame without a regular ancestor. |
| 67 | // If this happens, log the issue and defensively refuse to step out. |
| 68 | if (!return_frame_sp) { |
| 69 | LLDB_LOG(log, "Can't step out of frame with artificial ancestors"); |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 74 | m_step_out_to_id = return_frame_sp->GetStackID(); |
| 75 | m_immediate_step_from_id = immediate_return_from_sp->GetStackID(); |
| 76 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 77 | // If the frame directly below the one we are returning to is inlined, we |
| 78 | // have to be a little more careful. It is non-trivial to determine the real |
| 79 | // "return code address" for an inlined frame, so we have to work our way to |
| 80 | // that frame and then step out. |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 81 | if (immediate_return_from_sp->IsInlined()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 82 | if (frame_idx > 0) { |
| 83 | // First queue a plan that gets us to this inlined frame, and when we get |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 84 | // there we'll queue a second plan that walks us out of this frame. |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 | [diff] [blame] | 85 | m_step_out_to_inline_plan_sp = std::make_shared<ThreadPlanStepOut>( |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 86 | thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion, |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 | [diff] [blame] | 87 | frame_idx - 1, eLazyBoolNo, continue_to_next_branch); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 88 | static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get()) |
| 89 | ->SetShouldStopHereCallbacks(nullptr, nullptr); |
| 90 | m_step_out_to_inline_plan_sp->SetPrivate(true); |
| 91 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 92 | // If we're already at the inlined frame we're stepping through, then |
| 93 | // just do that now. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 94 | QueueInlinedStepPlan(false); |
Jim Ingham | a5ce6c8 | 2011-10-15 00:57:28 | [diff] [blame] | 95 | } |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 96 | } else { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 97 | // Find the return address and set a breakpoint there: |
| 98 | // FIXME - can we do this more securely if we know first_insn? |
Jim Ingham | a5ce6c8 | 2011-10-15 00:57:28 | [diff] [blame] | 99 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 100 | Address return_address(return_frame_sp->GetFrameCodeAddress()); |
| 101 | if (continue_to_next_branch) { |
| 102 | SymbolContext return_address_sc; |
| 103 | AddressRange range; |
| 104 | Address return_address_decr_pc = return_address; |
| 105 | if (return_address_decr_pc.GetOffset() > 0) |
| 106 | return_address_decr_pc.Slide(-1); |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 | [diff] [blame] | 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 108 | return_address_decr_pc.CalculateSymbolContext( |
| 109 | &return_address_sc, lldb::eSymbolContextLineEntry); |
| 110 | if (return_address_sc.line_entry.IsValid()) { |
Greg Clayton | 8a77792 | 2019-05-06 20:01:21 | [diff] [blame] | 111 | const bool include_inlined_functions = false; |
| 112 | range = return_address_sc.line_entry.GetSameLineContiguousAddressRange( |
| 113 | include_inlined_functions); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 114 | if (range.GetByteSize() > 0) { |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 115 | return_address = m_process.AdvanceAddressToNextBranchInstruction( |
| 116 | return_address, range); |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 | [diff] [blame] | 117 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 118 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 119 | } |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 120 | m_return_addr = return_address.GetLoadAddress(&m_process.GetTarget()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 121 | |
| 122 | if (m_return_addr == LLDB_INVALID_ADDRESS) |
| 123 | return; |
| 124 | |
Jim Ingham | 2a42a5a | 2019-12-20 19:00:11 | [diff] [blame] | 125 | // Perform some additional validation on the return address. |
| 126 | uint32_t permissions = 0; |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 127 | if (!m_process.GetLoadAddressPermissions(m_return_addr, permissions)) { |
Ted Woodward | 6fd818c | 2020-02-10 19:40:17 | [diff] [blame] | 128 | LLDB_LOGF(log, "ThreadPlanStepOut(%p): Return address (0x%" PRIx64 |
| 129 | ") permissions not found.", static_cast<void *>(this), |
| 130 | m_return_addr); |
Jim Ingham | 2a42a5a | 2019-12-20 19:00:11 | [diff] [blame] | 131 | } else if (!(permissions & ePermissionsExecutable)) { |
| 132 | m_constructor_errors.Printf("Return address (0x%" PRIx64 |
| 133 | ") did not point to executable memory.", |
| 134 | m_return_addr); |
| 135 | LLDB_LOGF(log, "ThreadPlanStepOut(%p): %s", static_cast<void *>(this), |
| 136 | m_constructor_errors.GetData()); |
| 137 | return; |
| 138 | } |
| 139 | |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 140 | Breakpoint *return_bp = |
| 141 | GetTarget().CreateBreakpoint(m_return_addr, true, false).get(); |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 | [diff] [blame] | 142 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 143 | if (return_bp != nullptr) { |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 | [diff] [blame] | 144 | if (return_bp->IsHardware() && !return_bp->HasResolvedLocations()) |
| 145 | m_could_not_resolve_hw_bp = true; |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 146 | return_bp->SetThreadID(m_tid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 147 | m_return_bp_id = return_bp->GetID(); |
| 148 | return_bp->SetBreakpointKind("step-out"); |
| 149 | } |
| 150 | |
| 151 | if (immediate_return_from_sp) { |
| 152 | const SymbolContext &sc = |
| 153 | immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction); |
| 154 | if (sc.function) { |
| 155 | m_immediate_step_from_function = sc.function; |
| 156 | } |
| 157 | } |
| 158 | } |
Jim Ingham | a5ce6c8 | 2011-10-15 00:57:28 | [diff] [blame] | 159 | } |
| 160 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 161 | void ThreadPlanStepOut::SetupAvoidNoDebug( |
| 162 | LazyBool step_out_avoids_code_without_debug_info) { |
| 163 | bool avoid_nodebug = true; |
| 164 | switch (step_out_avoids_code_without_debug_info) { |
| 165 | case eLazyBoolYes: |
| 166 | avoid_nodebug = true; |
| 167 | break; |
| 168 | case eLazyBoolNo: |
| 169 | avoid_nodebug = false; |
| 170 | break; |
| 171 | case eLazyBoolCalculate: |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 172 | avoid_nodebug = GetThread().GetStepOutAvoidsNoDebug(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | if (avoid_nodebug) |
| 176 | GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug); |
| 177 | else |
| 178 | GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug); |
Jim Ingham | 4b4b247 | 2014-03-13 02:47:14 | [diff] [blame] | 179 | } |
| 180 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 181 | void ThreadPlanStepOut::DidPush() { |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 182 | Thread &thread = GetThread(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 183 | if (m_step_out_to_inline_plan_sp) |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 184 | thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 185 | else if (m_step_through_inline_plan_sp) |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 186 | thread.QueueThreadPlan(m_step_through_inline_plan_sp, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | ThreadPlanStepOut::~ThreadPlanStepOut() { |
| 190 | if (m_return_bp_id != LLDB_INVALID_BREAK_ID) |
Jim Ingham | 1893065 | 2020-03-18 19:05:08 | [diff] [blame] | 191 | GetTarget().RemoveBreakpointByID(m_return_bp_id); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void ThreadPlanStepOut::GetDescription(Stream *s, |
| 195 | lldb::DescriptionLevel level) { |
| 196 | if (level == lldb::eDescriptionLevelBrief) |
| 197 | s->Printf("step out"); |
| 198 | else { |
Jim Ingham | 4b4b247 | 2014-03-13 02:47:14 | [diff] [blame] | 199 | if (m_step_out_to_inline_plan_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 200 | s->Printf("Stepping out to inlined frame so we can walk through it."); |
Jim Ingham | a5ce6c8 | 2011-10-15 00:57:28 | [diff] [blame] | 201 | else if (m_step_through_inline_plan_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 202 | s->Printf("Stepping out by stepping through inlined function."); |
| 203 | else { |
| 204 | s->Printf("Stepping out from "); |
| 205 | Address tmp_address; |
| 206 | if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) { |
Jim Ingham | 1893065 | 2020-03-18 19:05:08 | [diff] [blame] | 207 | tmp_address.Dump(s, &m_process, Address::DumpStyleResolvedDescription, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 208 | Address::DumpStyleLoadAddress); |
| 209 | } else { |
| 210 | s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn); |
| 211 | } |
| 212 | |
| 213 | // FIXME: find some useful way to present the m_return_id, since there may |
| 214 | // be multiple copies of the |
| 215 | // same function on the stack. |
| 216 | |
| 217 | s->Printf(" returning to frame at "); |
| 218 | if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) { |
Jim Ingham | 1893065 | 2020-03-18 19:05:08 | [diff] [blame] | 219 | tmp_address.Dump(s, &m_process, Address::DumpStyleResolvedDescription, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 220 | Address::DumpStyleLoadAddress); |
| 221 | } else { |
| 222 | s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr); |
| 223 | } |
| 224 | |
| 225 | if (level == eDescriptionLevelVerbose) |
| 226 | s->Printf(" using breakpoint site %d", m_return_bp_id); |
| 227 | } |
| 228 | } |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 229 | |
Jim Ingham | 1893065 | 2020-03-18 19:05:08 | [diff] [blame] | 230 | if (m_stepped_past_frames.empty()) |
| 231 | return; |
| 232 | |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 233 | s->Printf("\n"); |
| 234 | for (StackFrameSP frame_sp : m_stepped_past_frames) { |
| 235 | s->Printf("Stepped out past: "); |
| 236 | frame_sp->DumpUsingSettingsFormat(s); |
| 237 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 238 | } |
| 239 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 240 | bool ThreadPlanStepOut::ValidatePlan(Stream *error) { |
| 241 | if (m_step_out_to_inline_plan_sp) |
| 242 | return m_step_out_to_inline_plan_sp->ValidatePlan(error); |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 | [diff] [blame] | 243 | |
| 244 | if (m_step_through_inline_plan_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 245 | return m_step_through_inline_plan_sp->ValidatePlan(error); |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 | [diff] [blame] | 246 | |
| 247 | if (m_could_not_resolve_hw_bp) { |
| 248 | if (error) |
| 249 | error->PutCString( |
| 250 | "Could not create hardware breakpoint for thread plan."); |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | if (m_return_bp_id == LLDB_INVALID_BREAK_ID) { |
Jim Ingham | 2a42a5a | 2019-12-20 19:00:11 | [diff] [blame] | 255 | if (error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 256 | error->PutCString("Could not create return address breakpoint."); |
Jim Ingham | 2a42a5a | 2019-12-20 19:00:11 | [diff] [blame] | 257 | if (m_constructor_errors.GetSize() > 0) { |
| 258 | error->PutCString(" "); |
| 259 | error->PutCString(m_constructor_errors.GetString()); |
| 260 | } |
| 261 | } |
Jim Ingham | a5ce6c8 | 2011-10-15 00:57:28 | [diff] [blame] | 262 | return false; |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | return true; |
Jim Ingham | a5ce6c8 | 2011-10-15 00:57:28 | [diff] [blame] | 266 | } |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 | [diff] [blame] | 267 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 268 | bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 269 | // If the step out plan is done, then we just need to step through the |
| 270 | // inlined frame. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 271 | if (m_step_out_to_inline_plan_sp) { |
| 272 | return m_step_out_to_inline_plan_sp->MischiefManaged(); |
| 273 | } else if (m_step_through_inline_plan_sp) { |
| 274 | if (m_step_through_inline_plan_sp->MischiefManaged()) { |
| 275 | CalculateReturnValue(); |
| 276 | SetPlanComplete(); |
| 277 | return true; |
| 278 | } else |
| 279 | return false; |
| 280 | } else if (m_step_out_further_plan_sp) { |
| 281 | return m_step_out_further_plan_sp->MischiefManaged(); |
| 282 | } |
| 283 | |
| 284 | // We don't explain signals or breakpoints (breakpoints that handle stepping |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 285 | // in or out will be handled by a child plan. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 286 | |
| 287 | StopInfoSP stop_info_sp = GetPrivateStopInfo(); |
| 288 | if (stop_info_sp) { |
| 289 | StopReason reason = stop_info_sp->GetStopReason(); |
| 290 | if (reason == eStopReasonBreakpoint) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 291 | // If this is OUR breakpoint, we're fine, otherwise we don't know why |
| 292 | // this happened... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 293 | BreakpointSiteSP site_sp( |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 294 | m_process.GetBreakpointSiteList().FindByID(stop_info_sp->GetValue())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 295 | if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) { |
| 296 | bool done; |
| 297 | |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 298 | StackID frame_zero_id = |
| 299 | GetThread().GetStackFrameAtIndex(0)->GetStackID(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 300 | |
| 301 | if (m_step_out_to_id == frame_zero_id) |
| 302 | done = true; |
| 303 | else if (m_step_out_to_id < frame_zero_id) { |
| 304 | // Either we stepped past the breakpoint, or the stack ID calculation |
| 305 | // was incorrect and we should probably stop. |
| 306 | done = true; |
| 307 | } else { |
| 308 | done = (m_immediate_step_from_id < frame_zero_id); |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 | [diff] [blame] | 309 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 310 | |
| 311 | if (done) { |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 | [diff] [blame] | 312 | if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 313 | CalculateReturnValue(); |
| 314 | SetPlanComplete(); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // If there was only one owner, then we're done. But if we also hit |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 319 | // some user breakpoint on our way out, we should mark ourselves as |
| 320 | // done, but also not claim to explain the stop, since it is more |
| 321 | // important to report the user breakpoint than the step out |
| 322 | // completion. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 323 | |
| 324 | if (site_sp->GetNumberOfOwners() == 1) |
| 325 | return true; |
| 326 | } |
| 327 | return false; |
| 328 | } else if (IsUsuallyUnexplainedStopReason(reason)) |
| 329 | return false; |
| 330 | else |
| 331 | return true; |
| 332 | } |
| 333 | return true; |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 | [diff] [blame] | 334 | } |
Jim Ingham | 64e7ead | 2012-05-03 21:19:36 | [diff] [blame] | 335 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 336 | bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) { |
| 337 | if (IsPlanComplete()) |
| 338 | return true; |
| 339 | |
| 340 | bool done = false; |
| 341 | if (m_step_out_to_inline_plan_sp) { |
| 342 | if (m_step_out_to_inline_plan_sp->MischiefManaged()) { |
| 343 | // Now step through the inlined stack we are in: |
| 344 | if (QueueInlinedStepPlan(true)) { |
| 345 | // If we can't queue a plan to do this, then just call ourselves done. |
| 346 | m_step_out_to_inline_plan_sp.reset(); |
| 347 | SetPlanComplete(false); |
| 348 | return true; |
| 349 | } else |
| 350 | done = true; |
| 351 | } else |
| 352 | return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr); |
| 353 | } else if (m_step_through_inline_plan_sp) { |
| 354 | if (m_step_through_inline_plan_sp->MischiefManaged()) |
| 355 | done = true; |
| 356 | else |
| 357 | return m_step_through_inline_plan_sp->ShouldStop(event_ptr); |
| 358 | } else if (m_step_out_further_plan_sp) { |
| 359 | if (m_step_out_further_plan_sp->MischiefManaged()) |
| 360 | m_step_out_further_plan_sp.reset(); |
| 361 | else |
| 362 | return m_step_out_further_plan_sp->ShouldStop(event_ptr); |
| 363 | } |
| 364 | |
| 365 | if (!done) { |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 366 | StackID frame_zero_id = GetThread().GetStackFrameAtIndex(0)->GetStackID(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 367 | done = !(frame_zero_id < m_step_out_to_id); |
| 368 | } |
| 369 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 370 | // The normal step out computations think we are done, so all we need to do |
| 371 | // is consult the ShouldStopHere, and we are done. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 372 | |
| 373 | if (done) { |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 | [diff] [blame] | 374 | if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 375 | CalculateReturnValue(); |
| 376 | SetPlanComplete(); |
| 377 | } else { |
| 378 | m_step_out_further_plan_sp = |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 | [diff] [blame] | 379 | QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder, m_status); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 380 | done = false; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | return done; |
| 385 | } |
| 386 | |
| 387 | bool ThreadPlanStepOut::StopOthers() { return m_stop_others; } |
| 388 | |
| 389 | StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; } |
| 390 | |
| 391 | bool ThreadPlanStepOut::DoWillResume(StateType resume_state, |
| 392 | bool current_plan) { |
| 393 | if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp) |
| 394 | return true; |
| 395 | |
| 396 | if (m_return_bp_id == LLDB_INVALID_BREAK_ID) |
| 397 | return false; |
| 398 | |
| 399 | if (current_plan) { |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 400 | Breakpoint *return_bp = GetTarget().GetBreakpointByID(m_return_bp_id).get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 401 | if (return_bp != nullptr) |
| 402 | return_bp->SetEnabled(true); |
| 403 | } |
| 404 | return true; |
| 405 | } |
| 406 | |
| 407 | bool ThreadPlanStepOut::WillStop() { |
| 408 | if (m_return_bp_id != LLDB_INVALID_BREAK_ID) { |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 409 | Breakpoint *return_bp = GetTarget().GetBreakpointByID(m_return_bp_id).get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 410 | if (return_bp != nullptr) |
| 411 | return_bp->SetEnabled(false); |
| 412 | } |
| 413 | |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | bool ThreadPlanStepOut::MischiefManaged() { |
| 418 | if (IsPlanComplete()) { |
| 419 | // Did I reach my breakpoint? If so I'm done. |
| 420 | // |
| 421 | // I also check the stack depth, since if we've blown past the breakpoint |
| 422 | // for some |
| 423 | // reason and we're now stopping for some other reason altogether, then |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 424 | // we're done with this step out operation. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 425 | |
| 426 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 427 | if (log) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 | [diff] [blame] | 428 | LLDB_LOGF(log, "Completed step out plan."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 429 | if (m_return_bp_id != LLDB_INVALID_BREAK_ID) { |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 430 | GetTarget().RemoveBreakpointByID(m_return_bp_id); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 431 | m_return_bp_id = LLDB_INVALID_BREAK_ID; |
| 432 | } |
| 433 | |
| 434 | ThreadPlan::MischiefManaged(); |
| 435 | return true; |
| 436 | } else { |
| 437 | return false; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) { |
| 442 | // Now figure out the range of this inlined block, and set up a "step through |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 443 | // range" plan for that. If we've been provided with a context, then use the |
| 444 | // block in that context. |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 445 | Thread &thread = GetThread(); |
| 446 | StackFrameSP immediate_return_from_sp(thread.GetStackFrameAtIndex(0)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 447 | if (!immediate_return_from_sp) |
| 448 | return false; |
| 449 | |
| 450 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 451 | if (log) { |
| 452 | StreamString s; |
| 453 | immediate_return_from_sp->Dump(&s, true, false); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 | [diff] [blame] | 454 | LLDB_LOGF(log, "Queuing inlined frame to step past: %s.", s.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | Block *from_block = immediate_return_from_sp->GetFrameBlock(); |
| 458 | if (from_block) { |
| 459 | Block *inlined_block = from_block->GetContainingInlinedBlock(); |
| 460 | if (inlined_block) { |
| 461 | size_t num_ranges = inlined_block->GetNumRanges(); |
| 462 | AddressRange inline_range; |
| 463 | if (inlined_block->GetRangeAtIndex(0, inline_range)) { |
| 464 | SymbolContext inlined_sc; |
| 465 | inlined_block->CalculateSymbolContext(&inlined_sc); |
| 466 | inlined_sc.target_sp = GetTarget().shared_from_this(); |
| 467 | RunMode run_mode = |
| 468 | m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads; |
| 469 | const LazyBool avoid_no_debug = eLazyBoolNo; |
| 470 | |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 | [diff] [blame] | 471 | m_step_through_inline_plan_sp = |
| 472 | std::make_shared<ThreadPlanStepOverRange>( |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 473 | thread, inline_range, inlined_sc, run_mode, avoid_no_debug); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 474 | ThreadPlanStepOverRange *step_through_inline_plan_ptr = |
| 475 | static_cast<ThreadPlanStepOverRange *>( |
| 476 | m_step_through_inline_plan_sp.get()); |
| 477 | m_step_through_inline_plan_sp->SetPrivate(true); |
| 478 | |
| 479 | step_through_inline_plan_ptr->SetOkayToDiscard(true); |
| 480 | StreamString errors; |
| 481 | if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) { |
| 482 | // FIXME: Log this failure. |
| 483 | delete step_through_inline_plan_ptr; |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | for (size_t i = 1; i < num_ranges; i++) { |
| 488 | if (inlined_block->GetRangeAtIndex(i, inline_range)) |
| 489 | step_through_inline_plan_ptr->AddRange(inline_range); |
| 490 | } |
| 491 | |
| 492 | if (queue_now) |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 493 | thread.QueueThreadPlan(m_step_through_inline_plan_sp, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 494 | return true; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | void ThreadPlanStepOut::CalculateReturnValue() { |
| 503 | if (m_return_valobj_sp) |
| 504 | return; |
| 505 | |
| 506 | if (!m_calculate_return_value) |
| 507 | return; |
| 508 | |
| 509 | if (m_immediate_step_from_function != nullptr) { |
| 510 | CompilerType return_compiler_type = |
| 511 | m_immediate_step_from_function->GetCompilerType() |
| 512 | .GetFunctionReturnType(); |
| 513 | if (return_compiler_type) { |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 514 | lldb::ABISP abi_sp = m_process.GetABI(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 515 | if (abi_sp) |
| 516 | m_return_valobj_sp = |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 517 | abi_sp->GetReturnValueObject(GetThread(), return_compiler_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | bool ThreadPlanStepOut::IsPlanStale() { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 523 | // If we are still lower on the stack than the frame we are returning to, |
| 524 | // then there's something for us to do. Otherwise, we're stale. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 525 | |
Jim Ingham | e4598dc | 2020-03-10 21:03:53 | [diff] [blame] | 526 | StackID frame_zero_id = GetThread().GetStackFrameAtIndex(0)->GetStackID(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 527 | return !(frame_zero_id < m_step_out_to_id); |
Jim Ingham | 64e7ead | 2012-05-03 21:19:36 | [diff] [blame] | 528 | } |