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 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ThreadPlanStepOut.h"
14 // Other libraries and framework includes
16 #include "lldb/Breakpoint/Breakpoint.h"
17 #include "lldb/lldb-private-log.h"
18 #include "lldb/Core/Log.h"
19 #include "lldb/Core/Value.h"
20 #include "lldb/Core/ValueObjectConstResult.h"
21 #include "lldb/Symbol/Block.h"
22 #include "lldb/Symbol/Function.h"
23 #include "lldb/Symbol/Type.h"
24 #include "lldb/Target/Process.h"
25 #include "lldb/Target/RegisterContext.h"
26 #include "lldb/Target/StopInfo.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/ThreadPlanStepOverRange.h"
29 #include "lldb/Target/ThreadPlanStepThrough.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
42 SymbolContext *context,
48 LazyBool step_out_avoids_code_without_debug_info
50 ThreadPlan (ThreadPlan::eKindStepOut, "Step out", thread, stop_vote, run_vote),
51 ThreadPlanShouldStopHere (this),
52 m_step_from_insn (LLDB_INVALID_ADDRESS),
53 m_return_bp_id (LLDB_INVALID_BREAK_ID),
54 m_return_addr (LLDB_INVALID_ADDRESS),
55 m_stop_others (stop_others),
56 m_immediate_step_from_function(NULL)
59 SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
61 m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
63 StackFrameSP return_frame_sp (m_thread.GetStackFrameAtIndex(frame_idx + 1));
64 StackFrameSP immediate_return_from_sp (m_thread.GetStackFrameAtIndex (frame_idx));
66 if (!return_frame_sp || !immediate_return_from_sp)
67 return; // we can't do anything here. ValidatePlan() will return false.
69 m_step_out_to_id = return_frame_sp->GetStackID();
70 m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
72 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
74 // If the frame directly below the one we are returning to is inlined, we have to be
75 // a little more careful. It is non-trivial to determine the real "return code address" for
76 // an inlined frame, so we have to work our way to that frame and then step out.
77 if (immediate_return_from_sp && immediate_return_from_sp->IsInlined())
81 // First queue a plan that gets us to this inlined frame, and when we get there we'll queue a second
82 // plan that walks us out of this frame.
83 m_step_out_to_inline_plan_sp.reset (new ThreadPlanStepOut(m_thread,
91 static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())->SetShouldStopHereCallbacks(nullptr, nullptr);
92 m_step_out_to_inline_plan_sp->SetPrivate(true);
96 // If we're already at the inlined frame we're stepping through, then just do that now.
97 QueueInlinedStepPlan(false);
101 else if (return_frame_sp)
103 // Find the return address and set a breakpoint there:
104 // FIXME - can we do this more securely if we know first_insn?
106 m_return_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess()->GetTarget());
108 if (m_return_addr == LLDB_INVALID_ADDRESS)
111 Breakpoint *return_bp = m_thread.CalculateTarget()->CreateBreakpoint (m_return_addr, true, false).get();
112 if (return_bp != NULL)
114 return_bp->SetThreadID(m_thread.GetID());
115 m_return_bp_id = return_bp->GetID();
116 return_bp->SetBreakpointKind ("step-out");
119 if (immediate_return_from_sp)
121 const SymbolContext &sc = immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
124 m_immediate_step_from_function = sc.function;
132 ThreadPlanStepOut::SetupAvoidNoDebug(LazyBool step_out_avoids_code_without_debug_info)
134 bool avoid_nodebug = true;
135 switch (step_out_avoids_code_without_debug_info)
138 avoid_nodebug = true;
141 avoid_nodebug = false;
143 case eLazyBoolCalculate:
144 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
148 GetFlags().Set (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
150 GetFlags().Clear (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
154 ThreadPlanStepOut::DidPush()
156 if (m_step_out_to_inline_plan_sp)
157 m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
158 else if (m_step_through_inline_plan_sp)
159 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
162 ThreadPlanStepOut::~ThreadPlanStepOut ()
164 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
165 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
169 ThreadPlanStepOut::GetDescription (Stream *s, lldb::DescriptionLevel level)
171 if (level == lldb::eDescriptionLevelBrief)
172 s->Printf ("step out");
175 if (m_step_out_to_inline_plan_sp)
176 s->Printf ("Stepping out to inlined frame so we can walk through it.");
177 else if (m_step_through_inline_plan_sp)
178 s->Printf ("Stepping out by stepping through inlined function.");
181 s->Printf ("Stepping out from ");
183 if (tmp_address.SetLoadAddress (m_step_from_insn, &GetTarget()))
185 tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription, Address::DumpStyleLoadAddress);
189 s->Printf ("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn);
192 // FIXME: find some useful way to present the m_return_id, since there may be multiple copies of the
193 // same function on the stack.
195 s->Printf ("returning to frame at ");
196 if (tmp_address.SetLoadAddress (m_return_addr, &GetTarget()))
198 tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription, Address::DumpStyleLoadAddress);
202 s->Printf ("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
205 if (level == eDescriptionLevelVerbose)
206 s->Printf(" using breakpoint site %d", m_return_bp_id);
212 ThreadPlanStepOut::ValidatePlan (Stream *error)
214 if (m_step_out_to_inline_plan_sp)
215 return m_step_out_to_inline_plan_sp->ValidatePlan (error);
216 else if (m_step_through_inline_plan_sp)
217 return m_step_through_inline_plan_sp->ValidatePlan (error);
218 else if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
221 error->PutCString("Could not create return address breakpoint.");
229 ThreadPlanStepOut::DoPlanExplainsStop (Event *event_ptr)
231 // If the step out plan is done, then we just need to step through the inlined frame.
232 if (m_step_out_to_inline_plan_sp)
234 if (m_step_out_to_inline_plan_sp->MischiefManaged())
239 else if (m_step_through_inline_plan_sp)
241 if (m_step_through_inline_plan_sp->MischiefManaged())
243 CalculateReturnValue();
250 else if (m_step_out_further_plan_sp)
252 if (m_step_out_further_plan_sp->MischiefManaged())
258 // We don't explain signals or breakpoints (breakpoints that handle stepping in or
259 // out will be handled by a child plan.
261 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
264 StopReason reason = stop_info_sp->GetStopReason();
267 case eStopReasonBreakpoint:
269 // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened...
270 BreakpointSiteSP site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue()));
271 if (site_sp && site_sp->IsBreakpointAtThisSite (m_return_bp_id))
275 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
277 if (m_step_out_to_id == frame_zero_id)
279 else if (m_step_out_to_id < frame_zero_id)
281 // Either we stepped past the breakpoint, or the stack ID calculation
282 // was incorrect and we should probably stop.
287 if (m_immediate_step_from_id < frame_zero_id)
295 if (InvokeShouldStopHereCallback (eFrameCompareOlder))
297 CalculateReturnValue();
302 // If there was only one owner, then we're done. But if we also hit some
303 // user breakpoint on our way out, we should mark ourselves as done, but
304 // also not claim to explain the stop, since it is more important to report
305 // the user breakpoint than the step out completion.
307 if (site_sp->GetNumberOfOwners() == 1)
313 case eStopReasonWatchpoint:
314 case eStopReasonSignal:
315 case eStopReasonException:
316 case eStopReasonExec:
317 case eStopReasonThreadExiting:
328 ThreadPlanStepOut::ShouldStop (Event *event_ptr)
330 if (IsPlanComplete())
334 if (m_step_out_to_inline_plan_sp)
336 if (m_step_out_to_inline_plan_sp->MischiefManaged())
338 // Now step through the inlined stack we are in:
339 if (QueueInlinedStepPlan(true))
341 // If we can't queue a plan to do this, then just call ourselves done.
342 m_step_out_to_inline_plan_sp.reset();
343 SetPlanComplete (false);
350 return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
352 else if (m_step_through_inline_plan_sp)
354 if (m_step_through_inline_plan_sp->MischiefManaged())
357 return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
359 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();
364 return m_step_out_further_plan_sp->ShouldStop(event_ptr);
369 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
370 if (frame_zero_id < m_step_out_to_id)
376 // The normal step out computations think we are done, so all we need to do is consult the ShouldStopHere,
381 if (InvokeShouldStopHereCallback(eFrameCompareOlder))
383 CalculateReturnValue();
388 m_step_out_further_plan_sp = QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder);
397 ThreadPlanStepOut::StopOthers ()
399 return m_stop_others;
403 ThreadPlanStepOut::GetPlanRunState ()
405 return eStateRunning;
409 ThreadPlanStepOut::DoWillResume (StateType resume_state, bool current_plan)
411 if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
414 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
419 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
420 if (return_bp != NULL)
421 return_bp->SetEnabled (true);
427 ThreadPlanStepOut::WillStop ()
429 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
431 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
432 if (return_bp != NULL)
433 return_bp->SetEnabled (false);
440 ThreadPlanStepOut::MischiefManaged ()
442 if (IsPlanComplete())
444 // Did I reach my breakpoint? If so I'm done.
446 // I also check the stack depth, since if we've blown past the breakpoint for some
447 // reason and we're now stopping for some other reason altogether, then we're done
448 // with this step out operation.
450 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
452 log->Printf("Completed step out plan.");
453 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
455 m_thread.CalculateTarget()->RemoveBreakpointByID (m_return_bp_id);
456 m_return_bp_id = LLDB_INVALID_BREAK_ID;
459 ThreadPlan::MischiefManaged ();
469 ThreadPlanStepOut::QueueInlinedStepPlan (bool queue_now)
471 // Now figure out the range of this inlined block, and set up a "step through range"
472 // plan for that. If we've been provided with a context, then use the block in that
474 StackFrameSP immediate_return_from_sp (m_thread.GetStackFrameAtIndex (0));
475 if (!immediate_return_from_sp)
478 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
482 immediate_return_from_sp->Dump(&s, true, false);
483 log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
486 Block *from_block = immediate_return_from_sp->GetFrameBlock();
489 Block *inlined_block = from_block->GetContainingInlinedBlock();
492 size_t num_ranges = inlined_block->GetNumRanges();
493 AddressRange inline_range;
494 if (inlined_block->GetRangeAtIndex(0, inline_range))
496 SymbolContext inlined_sc;
497 inlined_block->CalculateSymbolContext(&inlined_sc);
498 inlined_sc.target_sp = GetTarget().shared_from_this();
499 RunMode run_mode = m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
500 const LazyBool avoid_no_debug = eLazyBoolNo;
502 m_step_through_inline_plan_sp.reset (new ThreadPlanStepOverRange(m_thread,
507 ThreadPlanStepOverRange *step_through_inline_plan_ptr
508 = static_cast<ThreadPlanStepOverRange *>(m_step_through_inline_plan_sp.get());
509 m_step_through_inline_plan_sp->SetPrivate(true);
511 step_through_inline_plan_ptr->SetOkayToDiscard(true);
513 if (!step_through_inline_plan_ptr->ValidatePlan(&errors))
515 //FIXME: Log this failure.
516 delete step_through_inline_plan_ptr;
520 for (size_t i = 1; i < num_ranges; i++)
522 if (inlined_block->GetRangeAtIndex (i, inline_range))
523 step_through_inline_plan_ptr->AddRange (inline_range);
527 m_thread.QueueThreadPlan (m_step_through_inline_plan_sp, false);
537 ThreadPlanStepOut::CalculateReturnValue ()
539 if (m_return_valobj_sp)
542 if (m_immediate_step_from_function != NULL)
544 ClangASTType return_clang_type = m_immediate_step_from_function->GetClangType().GetFunctionReturnType();
545 if (return_clang_type)
547 lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
549 m_return_valobj_sp = abi_sp->GetReturnValueObject(m_thread, return_clang_type);
555 ThreadPlanStepOut::IsPlanStale()
557 // If we are still lower on the stack than the frame we are returning to, then
558 // there's something for us to do. Otherwise, we're stale.
560 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
561 if (frame_zero_id < m_step_out_to_id)