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"
31 using namespace lldb_private;
33 //----------------------------------------------------------------------
34 // ThreadPlanStepOut: Step out of the current frame
35 //----------------------------------------------------------------------
36 ThreadPlanStepOut::ThreadPlanStepOut
39 SymbolContext *context,
46 ThreadPlan (ThreadPlan::eKindStepOut, "Step out", thread, stop_vote, run_vote),
47 m_step_from_context (context),
48 m_step_from_insn (LLDB_INVALID_ADDRESS),
49 m_return_bp_id (LLDB_INVALID_BREAK_ID),
50 m_return_addr (LLDB_INVALID_ADDRESS),
51 m_first_insn (first_insn),
52 m_stop_others (stop_others),
53 m_step_through_inline_plan_sp(),
54 m_step_out_plan_sp (),
55 m_immediate_step_from_function(NULL)
58 m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
60 StackFrameSP return_frame_sp (m_thread.GetStackFrameAtIndex(frame_idx + 1));
61 StackFrameSP immediate_return_from_sp (m_thread.GetStackFrameAtIndex (frame_idx));
63 if (!return_frame_sp || !immediate_return_from_sp)
64 return; // we can't do anything here. ValidatePlan() will return false.
66 m_step_out_to_id = return_frame_sp->GetStackID();
67 m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
69 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
71 // If the frame directly below the one we are returning to is inlined, we have to be
72 // a little more careful. It is non-trivial to determine the real "return code address" for
73 // an inlined frame, so we have to work our way to that frame and then step out.
74 if (immediate_return_from_sp && immediate_return_from_sp->IsInlined())
78 // First queue a plan that gets us to this inlined frame, and when we get there we'll queue a second
79 // plan that walks us out of this frame.
80 m_step_out_plan_sp.reset (new ThreadPlanStepOut(m_thread,
90 // If we're already at the inlined frame we're stepping through, then just do that now.
91 QueueInlinedStepPlan(false);
95 else if (return_frame_sp)
97 // Find the return address and set a breakpoint there:
98 // FIXME - can we do this more securely if we know first_insn?
100 m_return_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess()->GetTarget());
102 if (m_return_addr == LLDB_INVALID_ADDRESS)
105 Breakpoint *return_bp = m_thread.CalculateTarget()->CreateBreakpoint (m_return_addr, true, false).get();
106 if (return_bp != NULL)
108 return_bp->SetThreadID(m_thread.GetID());
109 m_return_bp_id = return_bp->GetID();
110 return_bp->SetBreakpointKind ("step-out");
113 if (immediate_return_from_sp)
115 const SymbolContext &sc = immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
118 m_immediate_step_from_function = sc.function;
126 ThreadPlanStepOut::DidPush()
128 if (m_step_out_plan_sp)
129 m_thread.QueueThreadPlan(m_step_out_plan_sp, false);
130 else if (m_step_through_inline_plan_sp)
131 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
134 ThreadPlanStepOut::~ThreadPlanStepOut ()
136 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
137 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
141 ThreadPlanStepOut::GetDescription (Stream *s, lldb::DescriptionLevel level)
143 if (level == lldb::eDescriptionLevelBrief)
144 s->Printf ("step out");
147 if (m_step_out_plan_sp)
148 s->Printf ("Stepping out to inlined frame so we can walk through it.");
149 else if (m_step_through_inline_plan_sp)
150 s->Printf ("Stepping out by stepping through inlined function.");
152 s->Printf ("Stepping out from address 0x%" PRIx64 " to return address 0x%" PRIx64 " using breakpoint site %d",
153 (uint64_t)m_step_from_insn,
154 (uint64_t)m_return_addr,
160 ThreadPlanStepOut::ValidatePlan (Stream *error)
162 if (m_step_out_plan_sp)
163 return m_step_out_plan_sp->ValidatePlan (error);
164 else if (m_step_through_inline_plan_sp)
165 return m_step_through_inline_plan_sp->ValidatePlan (error);
166 else if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
169 error->PutCString("Could not create return address breakpoint.");
177 ThreadPlanStepOut::DoPlanExplainsStop (Event *event_ptr)
179 // If one of our child plans just finished, then we do explain the stop.
180 if (m_step_out_plan_sp)
182 if (m_step_out_plan_sp->MischiefManaged())
184 // If this one is done, then we are all done.
185 CalculateReturnValue();
192 else if (m_step_through_inline_plan_sp)
194 if (m_step_through_inline_plan_sp->MischiefManaged())
200 // We don't explain signals or breakpoints (breakpoints that handle stepping in or
201 // out will be handled by a child plan.
203 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
206 StopReason reason = stop_info_sp->GetStopReason();
209 case eStopReasonBreakpoint:
211 // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened...
212 BreakpointSiteSP site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue()));
213 if (site_sp && site_sp->IsBreakpointAtThisSite (m_return_bp_id))
217 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
219 if (m_step_out_to_id == frame_zero_id)
221 else if (m_step_out_to_id < frame_zero_id)
223 // Either we stepped past the breakpoint, or the stack ID calculation
224 // was incorrect and we should probably stop.
229 if (m_immediate_step_from_id < frame_zero_id)
237 CalculateReturnValue();
241 // If there was only one owner, then we're done. But if we also hit some
242 // user breakpoint on our way out, we should mark ourselves as done, but
243 // also not claim to explain the stop, since it is more important to report
244 // the user breakpoint than the step out completion.
246 if (site_sp->GetNumberOfOwners() == 1)
252 case eStopReasonWatchpoint:
253 case eStopReasonSignal:
254 case eStopReasonException:
255 case eStopReasonExec:
256 case eStopReasonThreadExiting:
267 ThreadPlanStepOut::ShouldStop (Event *event_ptr)
269 if (IsPlanComplete())
274 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
275 if (frame_zero_id < m_step_out_to_id)
282 CalculateReturnValue();
288 if (m_step_out_plan_sp)
290 if (m_step_out_plan_sp->MischiefManaged())
292 // Now step through the inlined stack we are in:
293 if (QueueInlinedStepPlan(true))
299 CalculateReturnValue();
305 return m_step_out_plan_sp->ShouldStop(event_ptr);
307 else if (m_step_through_inline_plan_sp)
309 if (m_step_through_inline_plan_sp->MischiefManaged())
311 // We don't calculate the return value here because we don't know how to.
312 // But in case we had a return value sitting around from our process in
313 // getting here, let's clear it out.
314 m_return_valobj_sp.reset();
319 return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
327 ThreadPlanStepOut::StopOthers ()
329 return m_stop_others;
333 ThreadPlanStepOut::GetPlanRunState ()
335 return eStateRunning;
339 ThreadPlanStepOut::DoWillResume (StateType resume_state, bool current_plan)
341 if (m_step_out_plan_sp || m_step_through_inline_plan_sp)
344 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
349 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
350 if (return_bp != NULL)
351 return_bp->SetEnabled (true);
357 ThreadPlanStepOut::WillStop ()
359 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
361 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
362 if (return_bp != NULL)
363 return_bp->SetEnabled (false);
370 ThreadPlanStepOut::MischiefManaged ()
372 if (IsPlanComplete())
374 // Did I reach my breakpoint? If so I'm done.
376 // I also check the stack depth, since if we've blown past the breakpoint for some
377 // reason and we're now stopping for some other reason altogether, then we're done
378 // with this step out operation.
380 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
382 log->Printf("Completed step out plan.");
383 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
385 m_thread.CalculateTarget()->RemoveBreakpointByID (m_return_bp_id);
386 m_return_bp_id = LLDB_INVALID_BREAK_ID;
389 ThreadPlan::MischiefManaged ();
399 ThreadPlanStepOut::QueueInlinedStepPlan (bool queue_now)
401 // Now figure out the range of this inlined block, and set up a "step through range"
402 // plan for that. If we've been provided with a context, then use the block in that
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));
412 immediate_return_from_sp->Dump(&s, true, false);
413 log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
416 Block *from_block = immediate_return_from_sp->GetFrameBlock();
419 Block *inlined_block = from_block->GetContainingInlinedBlock();
422 size_t num_ranges = inlined_block->GetNumRanges();
423 AddressRange inline_range;
424 if (inlined_block->GetRangeAtIndex(0, inline_range))
426 SymbolContext inlined_sc;
427 inlined_block->CalculateSymbolContext(&inlined_sc);
428 inlined_sc.target_sp = GetTarget().shared_from_this();
429 RunMode run_mode = m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
430 ThreadPlanStepOverRange *step_through_inline_plan_ptr = new ThreadPlanStepOverRange(m_thread,
434 step_through_inline_plan_ptr->SetOkayToDiscard(true);
436 if (!step_through_inline_plan_ptr->ValidatePlan(&errors))
438 //FIXME: Log this failure.
439 delete step_through_inline_plan_ptr;
443 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);
448 m_step_through_inline_plan_sp.reset (step_through_inline_plan_ptr);
450 m_thread.QueueThreadPlan (m_step_through_inline_plan_sp, false);
460 ThreadPlanStepOut::CalculateReturnValue ()
462 if (m_return_valobj_sp)
465 if (m_immediate_step_from_function != NULL)
467 ClangASTType return_clang_type = m_immediate_step_from_function->GetClangType().GetFunctionReturnType();
468 if (return_clang_type)
470 lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
472 m_return_valobj_sp = abi_sp->GetReturnValueObject(m_thread, return_clang_type);
478 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 if (frame_zero_id < m_step_out_to_id)