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
67 // have to be a little more careful. It is non-trivial to determine the real
68 // "return code address" for an inlined frame, so we have to work our way to
69 // that frame and then step out.
70 if (immediate_return_from_sp && immediate_return_from_sp->IsInlined()) {
72 // First queue a plan that gets us to this inlined frame, and when we get
73 // there we'll queue a second plan that walks us out of this frame.
74 m_step_out_to_inline_plan_sp.reset(new ThreadPlanStepOut(
75 m_thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
76 frame_idx - 1, eLazyBoolNo, continue_to_next_branch));
77 static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())
78 ->SetShouldStopHereCallbacks(nullptr, nullptr);
79 m_step_out_to_inline_plan_sp->SetPrivate(true);
81 // If we're already at the inlined frame we're stepping through, then
83 QueueInlinedStepPlan(false);
85 } else if (return_frame_sp) {
86 // Find the return address and set a breakpoint there:
87 // FIXME - can we do this more securely if we know first_insn?
89 Address return_address(return_frame_sp->GetFrameCodeAddress());
90 if (continue_to_next_branch) {
91 SymbolContext return_address_sc;
93 Address return_address_decr_pc = return_address;
94 if (return_address_decr_pc.GetOffset() > 0)
95 return_address_decr_pc.Slide(-1);
97 return_address_decr_pc.CalculateSymbolContext(
98 &return_address_sc, lldb::eSymbolContextLineEntry);
99 if (return_address_sc.line_entry.IsValid()) {
101 return_address_sc.line_entry.GetSameLineContiguousAddressRange();
102 if (range.GetByteSize() > 0) {
104 m_thread.GetProcess()->AdvanceAddressToNextBranchInstruction(
105 return_address, range);
110 return_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget());
112 if (m_return_addr == LLDB_INVALID_ADDRESS)
115 Breakpoint *return_bp = m_thread.CalculateTarget()
116 ->CreateBreakpoint(m_return_addr, true, false)
118 if (return_bp != nullptr) {
119 return_bp->SetThreadID(m_thread.GetID());
120 m_return_bp_id = return_bp->GetID();
121 return_bp->SetBreakpointKind("step-out");
124 if (immediate_return_from_sp) {
125 const SymbolContext &sc =
126 immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
128 m_immediate_step_from_function = sc.function;
134 void ThreadPlanStepOut::SetupAvoidNoDebug(
135 LazyBool step_out_avoids_code_without_debug_info) {
136 bool avoid_nodebug = true;
137 switch (step_out_avoids_code_without_debug_info) {
139 avoid_nodebug = true;
142 avoid_nodebug = false;
144 case eLazyBoolCalculate:
145 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
149 GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
151 GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
154 void ThreadPlanStepOut::DidPush() {
155 if (m_step_out_to_inline_plan_sp)
156 m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
157 else if (m_step_through_inline_plan_sp)
158 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
161 ThreadPlanStepOut::~ThreadPlanStepOut() {
162 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
163 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
166 void ThreadPlanStepOut::GetDescription(Stream *s,
167 lldb::DescriptionLevel level) {
168 if (level == lldb::eDescriptionLevelBrief)
169 s->Printf("step out");
171 if (m_step_out_to_inline_plan_sp)
172 s->Printf("Stepping out to inlined frame so we can walk through it.");
173 else if (m_step_through_inline_plan_sp)
174 s->Printf("Stepping out by stepping through inlined function.");
176 s->Printf("Stepping out from ");
178 if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) {
179 tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
180 Address::DumpStyleLoadAddress);
182 s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn);
185 // FIXME: find some useful way to present the m_return_id, since there may
186 // be multiple copies of the
187 // same function on the stack.
189 s->Printf(" returning to frame at ");
190 if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) {
191 tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
192 Address::DumpStyleLoadAddress);
194 s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
197 if (level == eDescriptionLevelVerbose)
198 s->Printf(" using breakpoint site %d", m_return_bp_id);
203 bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
204 if (m_step_out_to_inline_plan_sp)
205 return m_step_out_to_inline_plan_sp->ValidatePlan(error);
206 else if (m_step_through_inline_plan_sp)
207 return m_step_through_inline_plan_sp->ValidatePlan(error);
208 else if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
210 error->PutCString("Could not create return address breakpoint.");
216 bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
217 // If the step out plan is done, then we just need to step through the
219 if (m_step_out_to_inline_plan_sp) {
220 return m_step_out_to_inline_plan_sp->MischiefManaged();
221 } else if (m_step_through_inline_plan_sp) {
222 if (m_step_through_inline_plan_sp->MischiefManaged()) {
223 CalculateReturnValue();
228 } else if (m_step_out_further_plan_sp) {
229 return m_step_out_further_plan_sp->MischiefManaged();
232 // We don't explain signals or breakpoints (breakpoints that handle stepping
233 // in or out will be handled by a child plan.
235 StopInfoSP stop_info_sp = GetPrivateStopInfo();
237 StopReason reason = stop_info_sp->GetStopReason();
238 if (reason == eStopReasonBreakpoint) {
239 // If this is OUR breakpoint, we're fine, otherwise we don't know why
241 BreakpointSiteSP site_sp(
242 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
243 stop_info_sp->GetValue()));
244 if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
247 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
249 if (m_step_out_to_id == frame_zero_id)
251 else if (m_step_out_to_id < frame_zero_id) {
252 // Either we stepped past the breakpoint, or the stack ID calculation
253 // was incorrect and we should probably stop.
256 done = (m_immediate_step_from_id < frame_zero_id);
260 if (InvokeShouldStopHereCallback(eFrameCompareOlder)) {
261 CalculateReturnValue();
266 // If there was only one owner, then we're done. But if we also hit
267 // some user breakpoint on our way out, we should mark ourselves as
268 // done, but also not claim to explain the stop, since it is more
269 // important to report the user breakpoint than the step out
272 if (site_sp->GetNumberOfOwners() == 1)
276 } else if (IsUsuallyUnexplainedStopReason(reason))
284 bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
285 if (IsPlanComplete())
289 if (m_step_out_to_inline_plan_sp) {
290 if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
291 // Now step through the inlined stack we are in:
292 if (QueueInlinedStepPlan(true)) {
293 // If we can't queue a plan to do this, then just call ourselves done.
294 m_step_out_to_inline_plan_sp.reset();
295 SetPlanComplete(false);
300 return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
301 } else if (m_step_through_inline_plan_sp) {
302 if (m_step_through_inline_plan_sp->MischiefManaged())
305 return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
306 } else if (m_step_out_further_plan_sp) {
307 if (m_step_out_further_plan_sp->MischiefManaged())
308 m_step_out_further_plan_sp.reset();
310 return m_step_out_further_plan_sp->ShouldStop(event_ptr);
314 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
315 done = !(frame_zero_id < m_step_out_to_id);
318 // The normal step out computations think we are done, so all we need to do
319 // is consult the ShouldStopHere, and we are done.
322 if (InvokeShouldStopHereCallback(eFrameCompareOlder)) {
323 CalculateReturnValue();
326 m_step_out_further_plan_sp =
327 QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder);
335 bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
337 StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
339 bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
341 if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
344 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
348 Breakpoint *return_bp =
349 m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
350 if (return_bp != nullptr)
351 return_bp->SetEnabled(true);
356 bool ThreadPlanStepOut::WillStop() {
357 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
358 Breakpoint *return_bp =
359 m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
360 if (return_bp != nullptr)
361 return_bp->SetEnabled(false);
367 bool ThreadPlanStepOut::MischiefManaged() {
368 if (IsPlanComplete()) {
369 // Did I reach my breakpoint? If so I'm done.
371 // I also check the stack depth, since if we've blown past the breakpoint
373 // reason and we're now stopping for some other reason altogether, then
374 // we're done with this step out operation.
376 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
378 log->Printf("Completed step out plan.");
379 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
380 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
381 m_return_bp_id = LLDB_INVALID_BREAK_ID;
384 ThreadPlan::MischiefManaged();
391 bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
392 // Now figure out the range of this inlined block, and set up a "step through
393 // range" plan for that. If we've been provided with a context, then use the
394 // block in that context.
395 StackFrameSP immediate_return_from_sp(m_thread.GetStackFrameAtIndex(0));
396 if (!immediate_return_from_sp)
399 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
402 immediate_return_from_sp->Dump(&s, true, false);
403 log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
406 Block *from_block = immediate_return_from_sp->GetFrameBlock();
408 Block *inlined_block = from_block->GetContainingInlinedBlock();
410 size_t num_ranges = inlined_block->GetNumRanges();
411 AddressRange inline_range;
412 if (inlined_block->GetRangeAtIndex(0, inline_range)) {
413 SymbolContext inlined_sc;
414 inlined_block->CalculateSymbolContext(&inlined_sc);
415 inlined_sc.target_sp = GetTarget().shared_from_this();
417 m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
418 const LazyBool avoid_no_debug = eLazyBoolNo;
420 m_step_through_inline_plan_sp.reset(new ThreadPlanStepOverRange(
421 m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug));
422 ThreadPlanStepOverRange *step_through_inline_plan_ptr =
423 static_cast<ThreadPlanStepOverRange *>(
424 m_step_through_inline_plan_sp.get());
425 m_step_through_inline_plan_sp->SetPrivate(true);
427 step_through_inline_plan_ptr->SetOkayToDiscard(true);
429 if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
430 // FIXME: Log this failure.
431 delete step_through_inline_plan_ptr;
435 for (size_t i = 1; i < num_ranges; i++) {
436 if (inlined_block->GetRangeAtIndex(i, inline_range))
437 step_through_inline_plan_ptr->AddRange(inline_range);
441 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
450 void ThreadPlanStepOut::CalculateReturnValue() {
451 if (m_return_valobj_sp)
454 if (!m_calculate_return_value)
457 if (m_immediate_step_from_function != nullptr) {
458 CompilerType return_compiler_type =
459 m_immediate_step_from_function->GetCompilerType()
460 .GetFunctionReturnType();
461 if (return_compiler_type) {
462 lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
465 abi_sp->GetReturnValueObject(m_thread, return_compiler_type);
470 bool ThreadPlanStepOut::IsPlanStale() {
471 // If we are still lower on the stack than the frame we are returning to,
472 // then there's something for us to do. Otherwise, we're stale.
474 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
475 return !(frame_zero_id < m_step_out_to_id);