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