1 //===-- ThreadPlanStepOut.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Target/ThreadPlanStepOut.h"
15 #include "lldb/Breakpoint/Breakpoint.h"
16 #include "lldb/Core/Value.h"
17 #include "lldb/Core/ValueObjectConstResult.h"
18 #include "lldb/Symbol/Block.h"
19 #include "lldb/Symbol/Function.h"
20 #include "lldb/Symbol/Symbol.h"
21 #include "lldb/Symbol/Type.h"
22 #include "lldb/Target/ABI.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/RegisterContext.h"
25 #include "lldb/Target/StopInfo.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Target/ThreadPlanStepOverRange.h"
28 #include "lldb/Target/ThreadPlanStepThrough.h"
29 #include "lldb/Utility/Log.h"
32 using namespace lldb_private;
34 uint32_t ThreadPlanStepOut::s_default_flag_values = 0;
36 //----------------------------------------------------------------------
37 // ThreadPlanStepOut: Step out of the current frame
38 //----------------------------------------------------------------------
39 ThreadPlanStepOut::ThreadPlanStepOut(
40 Thread &thread, SymbolContext *context, bool first_insn, bool stop_others,
41 Vote stop_vote, Vote run_vote, uint32_t frame_idx,
42 LazyBool step_out_avoids_code_without_debug_info,
43 bool continue_to_next_branch, bool gather_return_value)
44 : ThreadPlan(ThreadPlan::eKindStepOut, "Step out", thread, stop_vote,
46 ThreadPlanShouldStopHere(this), m_step_from_insn(LLDB_INVALID_ADDRESS),
47 m_return_bp_id(LLDB_INVALID_BREAK_ID),
48 m_return_addr(LLDB_INVALID_ADDRESS), m_stop_others(stop_others),
49 m_immediate_step_from_function(nullptr),
50 m_calculate_return_value(gather_return_value) {
52 SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
54 m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
56 StackFrameSP return_frame_sp(m_thread.GetStackFrameAtIndex(frame_idx + 1));
57 StackFrameSP immediate_return_from_sp(
58 m_thread.GetStackFrameAtIndex(frame_idx));
60 if (!return_frame_sp || !immediate_return_from_sp)
61 return; // we can't do anything here. ValidatePlan() will return false.
63 m_step_out_to_id = return_frame_sp->GetStackID();
64 m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
66 // If the frame directly below the one we are returning to is inlined, we have
68 // a little more careful. It is non-trivial to determine the real "return
70 // an inlined frame, so we have to work our way to that frame and then step
72 if (immediate_return_from_sp && immediate_return_from_sp->IsInlined()) {
74 // First queue a plan that gets us to this inlined frame, and when we get
75 // there we'll queue a second
76 // plan that walks us out of this frame.
77 m_step_out_to_inline_plan_sp.reset(new ThreadPlanStepOut(
78 m_thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
79 frame_idx - 1, eLazyBoolNo, continue_to_next_branch));
80 static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())
81 ->SetShouldStopHereCallbacks(nullptr, nullptr);
82 m_step_out_to_inline_plan_sp->SetPrivate(true);
84 // If we're already at the inlined frame we're stepping through, then just
86 QueueInlinedStepPlan(false);
88 } else if (return_frame_sp) {
89 // Find the return address and set a breakpoint there:
90 // FIXME - can we do this more securely if we know first_insn?
92 Address return_address(return_frame_sp->GetFrameCodeAddress());
93 if (continue_to_next_branch) {
94 SymbolContext return_address_sc;
96 Address return_address_decr_pc = return_address;
97 if (return_address_decr_pc.GetOffset() > 0)
98 return_address_decr_pc.Slide(-1);
100 return_address_decr_pc.CalculateSymbolContext(
101 &return_address_sc, lldb::eSymbolContextLineEntry);
102 if (return_address_sc.line_entry.IsValid()) {
104 return_address_sc.line_entry.GetSameLineContiguousAddressRange();
105 if (range.GetByteSize() > 0) {
107 m_thread.GetProcess()->AdvanceAddressToNextBranchInstruction(
108 return_address, range);
113 return_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget());
115 if (m_return_addr == LLDB_INVALID_ADDRESS)
118 Breakpoint *return_bp = m_thread.CalculateTarget()
119 ->CreateBreakpoint(m_return_addr, true, false)
121 if (return_bp != nullptr) {
122 return_bp->SetThreadID(m_thread.GetID());
123 m_return_bp_id = return_bp->GetID();
124 return_bp->SetBreakpointKind("step-out");
127 if (immediate_return_from_sp) {
128 const SymbolContext &sc =
129 immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
131 m_immediate_step_from_function = sc.function;
137 void ThreadPlanStepOut::SetupAvoidNoDebug(
138 LazyBool step_out_avoids_code_without_debug_info) {
139 bool avoid_nodebug = true;
140 switch (step_out_avoids_code_without_debug_info) {
142 avoid_nodebug = true;
145 avoid_nodebug = false;
147 case eLazyBoolCalculate:
148 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
152 GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
154 GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
157 void ThreadPlanStepOut::DidPush() {
158 if (m_step_out_to_inline_plan_sp)
159 m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
160 else if (m_step_through_inline_plan_sp)
161 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
164 ThreadPlanStepOut::~ThreadPlanStepOut() {
165 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
166 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
169 void ThreadPlanStepOut::GetDescription(Stream *s,
170 lldb::DescriptionLevel level) {
171 if (level == lldb::eDescriptionLevelBrief)
172 s->Printf("step out");
174 if (m_step_out_to_inline_plan_sp)
175 s->Printf("Stepping out to inlined frame so we can walk through it.");
176 else if (m_step_through_inline_plan_sp)
177 s->Printf("Stepping out by stepping through inlined function.");
179 s->Printf("Stepping out from ");
181 if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) {
182 tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
183 Address::DumpStyleLoadAddress);
185 s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn);
188 // FIXME: find some useful way to present the m_return_id, since there may
189 // be multiple copies of the
190 // same function on the stack.
192 s->Printf(" returning to frame at ");
193 if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) {
194 tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
195 Address::DumpStyleLoadAddress);
197 s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
200 if (level == eDescriptionLevelVerbose)
201 s->Printf(" using breakpoint site %d", m_return_bp_id);
206 bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
207 if (m_step_out_to_inline_plan_sp)
208 return m_step_out_to_inline_plan_sp->ValidatePlan(error);
209 else if (m_step_through_inline_plan_sp)
210 return m_step_through_inline_plan_sp->ValidatePlan(error);
211 else if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
213 error->PutCString("Could not create return address breakpoint.");
219 bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
220 // If the step out plan is done, then we just need to step through the inlined
222 if (m_step_out_to_inline_plan_sp) {
223 return m_step_out_to_inline_plan_sp->MischiefManaged();
224 } else if (m_step_through_inline_plan_sp) {
225 if (m_step_through_inline_plan_sp->MischiefManaged()) {
226 CalculateReturnValue();
231 } else if (m_step_out_further_plan_sp) {
232 return m_step_out_further_plan_sp->MischiefManaged();
235 // We don't explain signals or breakpoints (breakpoints that handle stepping
237 // out will be handled by a child plan.
239 StopInfoSP stop_info_sp = GetPrivateStopInfo();
241 StopReason reason = stop_info_sp->GetStopReason();
242 if (reason == eStopReasonBreakpoint) {
243 // If this is OUR breakpoint, we're fine, otherwise we don't know why this
245 BreakpointSiteSP site_sp(
246 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
247 stop_info_sp->GetValue()));
248 if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
251 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
253 if (m_step_out_to_id == frame_zero_id)
255 else if (m_step_out_to_id < frame_zero_id) {
256 // Either we stepped past the breakpoint, or the stack ID calculation
257 // was incorrect and we should probably stop.
260 done = (m_immediate_step_from_id < frame_zero_id);
264 if (InvokeShouldStopHereCallback(eFrameCompareOlder)) {
265 CalculateReturnValue();
270 // If there was only one owner, then we're done. But if we also hit
272 // user breakpoint on our way out, we should mark ourselves as done, but
273 // also not claim to explain the stop, since it is more important to
275 // the user breakpoint than the step out completion.
277 if (site_sp->GetNumberOfOwners() == 1)
281 } else if (IsUsuallyUnexplainedStopReason(reason))
289 bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
290 if (IsPlanComplete())
294 if (m_step_out_to_inline_plan_sp) {
295 if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
296 // Now step through the inlined stack we are in:
297 if (QueueInlinedStepPlan(true)) {
298 // If we can't queue a plan to do this, then just call ourselves done.
299 m_step_out_to_inline_plan_sp.reset();
300 SetPlanComplete(false);
305 return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
306 } else if (m_step_through_inline_plan_sp) {
307 if (m_step_through_inline_plan_sp->MischiefManaged())
310 return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
311 } else if (m_step_out_further_plan_sp) {
312 if (m_step_out_further_plan_sp->MischiefManaged())
313 m_step_out_further_plan_sp.reset();
315 return m_step_out_further_plan_sp->ShouldStop(event_ptr);
319 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
320 done = !(frame_zero_id < m_step_out_to_id);
323 // The normal step out computations think we are done, so all we need to do is
324 // consult the ShouldStopHere,
328 if (InvokeShouldStopHereCallback(eFrameCompareOlder)) {
329 CalculateReturnValue();
332 m_step_out_further_plan_sp =
333 QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder);
341 bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
343 StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
345 bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
347 if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
350 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
354 Breakpoint *return_bp =
355 m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
356 if (return_bp != nullptr)
357 return_bp->SetEnabled(true);
362 bool ThreadPlanStepOut::WillStop() {
363 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
364 Breakpoint *return_bp =
365 m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
366 if (return_bp != nullptr)
367 return_bp->SetEnabled(false);
373 bool ThreadPlanStepOut::MischiefManaged() {
374 if (IsPlanComplete()) {
375 // Did I reach my breakpoint? If so I'm done.
377 // I also check the stack depth, since if we've blown past the breakpoint
379 // reason and we're now stopping for some other reason altogether, then
381 // with this step out operation.
383 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
385 log->Printf("Completed step out plan.");
386 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
387 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
388 m_return_bp_id = LLDB_INVALID_BREAK_ID;
391 ThreadPlan::MischiefManaged();
398 bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
399 // Now figure out the range of this inlined block, and set up a "step through
401 // plan for that. If we've been provided with a context, then use the block
404 StackFrameSP immediate_return_from_sp(m_thread.GetStackFrameAtIndex(0));
405 if (!immediate_return_from_sp)
408 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
411 immediate_return_from_sp->Dump(&s, true, false);
412 log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
415 Block *from_block = immediate_return_from_sp->GetFrameBlock();
417 Block *inlined_block = from_block->GetContainingInlinedBlock();
419 size_t num_ranges = inlined_block->GetNumRanges();
420 AddressRange inline_range;
421 if (inlined_block->GetRangeAtIndex(0, inline_range)) {
422 SymbolContext inlined_sc;
423 inlined_block->CalculateSymbolContext(&inlined_sc);
424 inlined_sc.target_sp = GetTarget().shared_from_this();
426 m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
427 const LazyBool avoid_no_debug = eLazyBoolNo;
429 m_step_through_inline_plan_sp.reset(new ThreadPlanStepOverRange(
430 m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug));
431 ThreadPlanStepOverRange *step_through_inline_plan_ptr =
432 static_cast<ThreadPlanStepOverRange *>(
433 m_step_through_inline_plan_sp.get());
434 m_step_through_inline_plan_sp->SetPrivate(true);
436 step_through_inline_plan_ptr->SetOkayToDiscard(true);
438 if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
439 // FIXME: Log this failure.
440 delete step_through_inline_plan_ptr;
444 for (size_t i = 1; i < num_ranges; i++) {
445 if (inlined_block->GetRangeAtIndex(i, inline_range))
446 step_through_inline_plan_ptr->AddRange(inline_range);
450 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
459 void ThreadPlanStepOut::CalculateReturnValue() {
460 if (m_return_valobj_sp)
463 if (!m_calculate_return_value)
466 if (m_immediate_step_from_function != nullptr) {
467 CompilerType return_compiler_type =
468 m_immediate_step_from_function->GetCompilerType()
469 .GetFunctionReturnType();
470 if (return_compiler_type) {
471 lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
474 abi_sp->GetReturnValueObject(m_thread, return_compiler_type);
479 bool ThreadPlanStepOut::IsPlanStale() {
480 // If we are still lower on the stack than the frame we are returning to, then
481 // there's something for us to do. Otherwise, we're stale.
483 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
484 return !(frame_zero_id < m_step_out_to_id);