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"
11 #include "lldb/Breakpoint/Breakpoint.h"
12 #include "lldb/Core/Value.h"
13 #include "lldb/Core/ValueObjectConstResult.h"
14 #include "lldb/Symbol/Block.h"
15 #include "lldb/Symbol/Function.h"
16 #include "lldb/Symbol/Symbol.h"
17 #include "lldb/Symbol/Type.h"
18 #include "lldb/Target/ABI.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/RegisterContext.h"
21 #include "lldb/Target/StopInfo.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/ThreadPlanStepOverRange.h"
24 #include "lldb/Target/ThreadPlanStepThrough.h"
25 #include "lldb/Utility/Log.h"
28 using namespace lldb_private;
30 uint32_t ThreadPlanStepOut::s_default_flag_values = 0;
32 //----------------------------------------------------------------------
33 // ThreadPlanStepOut: Step out of the current frame
34 //----------------------------------------------------------------------
35 ThreadPlanStepOut::ThreadPlanStepOut(
36 Thread &thread, SymbolContext *context, bool first_insn, bool stop_others,
37 Vote stop_vote, Vote run_vote, uint32_t frame_idx,
38 LazyBool step_out_avoids_code_without_debug_info,
39 bool continue_to_next_branch, bool gather_return_value)
40 : ThreadPlan(ThreadPlan::eKindStepOut, "Step out", thread, stop_vote,
42 ThreadPlanShouldStopHere(this), m_step_from_insn(LLDB_INVALID_ADDRESS),
43 m_return_bp_id(LLDB_INVALID_BREAK_ID),
44 m_return_addr(LLDB_INVALID_ADDRESS), m_stop_others(stop_others),
45 m_immediate_step_from_function(nullptr),
46 m_calculate_return_value(gather_return_value) {
47 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
49 SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
51 m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
53 uint32_t return_frame_index = frame_idx + 1;
54 StackFrameSP return_frame_sp(
55 m_thread.GetStackFrameAtIndex(return_frame_index));
56 StackFrameSP immediate_return_from_sp(
57 m_thread.GetStackFrameAtIndex(frame_idx));
59 if (!return_frame_sp || !immediate_return_from_sp)
60 return; // we can't do anything here. ValidatePlan() will return false.
62 // While stepping out, behave as-if artificial frames are not present.
63 while (return_frame_sp->IsArtificial()) {
64 m_stepped_past_frames.push_back(return_frame_sp);
67 return_frame_sp = m_thread.GetStackFrameAtIndex(return_frame_index);
69 // We never expect to see an artificial frame without a regular ancestor.
70 // If this happens, log the issue and defensively refuse to step out.
71 if (!return_frame_sp) {
72 LLDB_LOG(log, "Can't step out of frame with artificial ancestors");
77 m_step_out_to_id = return_frame_sp->GetStackID();
78 m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
80 // If the frame directly below the one we are returning to is inlined, we
81 // have to be a little more careful. It is non-trivial to determine the real
82 // "return code address" for an inlined frame, so we have to work our way to
83 // that frame and then step out.
84 if (immediate_return_from_sp->IsInlined()) {
86 // First queue a plan that gets us to this inlined frame, and when we get
87 // there we'll queue a second plan that walks us out of this frame.
88 m_step_out_to_inline_plan_sp.reset(new ThreadPlanStepOut(
89 m_thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
90 frame_idx - 1, eLazyBoolNo, continue_to_next_branch));
91 static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())
92 ->SetShouldStopHereCallbacks(nullptr, nullptr);
93 m_step_out_to_inline_plan_sp->SetPrivate(true);
95 // If we're already at the inlined frame we're stepping through, then
97 QueueInlinedStepPlan(false);
100 // Find the return address and set a breakpoint there:
101 // FIXME - can we do this more securely if we know first_insn?
103 Address return_address(return_frame_sp->GetFrameCodeAddress());
104 if (continue_to_next_branch) {
105 SymbolContext return_address_sc;
107 Address return_address_decr_pc = return_address;
108 if (return_address_decr_pc.GetOffset() > 0)
109 return_address_decr_pc.Slide(-1);
111 return_address_decr_pc.CalculateSymbolContext(
112 &return_address_sc, lldb::eSymbolContextLineEntry);
113 if (return_address_sc.line_entry.IsValid()) {
115 return_address_sc.line_entry.GetSameLineContiguousAddressRange();
116 if (range.GetByteSize() > 0) {
118 m_thread.GetProcess()->AdvanceAddressToNextBranchInstruction(
119 return_address, range);
124 return_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget());
126 if (m_return_addr == LLDB_INVALID_ADDRESS)
129 Breakpoint *return_bp = m_thread.CalculateTarget()
130 ->CreateBreakpoint(m_return_addr, true, false)
133 if (return_bp != nullptr) {
134 if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
135 m_could_not_resolve_hw_bp = true;
136 return_bp->SetThreadID(m_thread.GetID());
137 m_return_bp_id = return_bp->GetID();
138 return_bp->SetBreakpointKind("step-out");
141 if (immediate_return_from_sp) {
142 const SymbolContext &sc =
143 immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
145 m_immediate_step_from_function = sc.function;
151 void ThreadPlanStepOut::SetupAvoidNoDebug(
152 LazyBool step_out_avoids_code_without_debug_info) {
153 bool avoid_nodebug = true;
154 switch (step_out_avoids_code_without_debug_info) {
156 avoid_nodebug = true;
159 avoid_nodebug = false;
161 case eLazyBoolCalculate:
162 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
166 GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
168 GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
171 void ThreadPlanStepOut::DidPush() {
172 if (m_step_out_to_inline_plan_sp)
173 m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
174 else if (m_step_through_inline_plan_sp)
175 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
178 ThreadPlanStepOut::~ThreadPlanStepOut() {
179 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
180 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
183 void ThreadPlanStepOut::GetDescription(Stream *s,
184 lldb::DescriptionLevel level) {
185 if (level == lldb::eDescriptionLevelBrief)
186 s->Printf("step out");
188 if (m_step_out_to_inline_plan_sp)
189 s->Printf("Stepping out to inlined frame so we can walk through it.");
190 else if (m_step_through_inline_plan_sp)
191 s->Printf("Stepping out by stepping through inlined function.");
193 s->Printf("Stepping out from ");
195 if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) {
196 tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
197 Address::DumpStyleLoadAddress);
199 s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn);
202 // FIXME: find some useful way to present the m_return_id, since there may
203 // be multiple copies of the
204 // same function on the stack.
206 s->Printf(" returning to frame at ");
207 if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) {
208 tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
209 Address::DumpStyleLoadAddress);
211 s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
214 if (level == eDescriptionLevelVerbose)
215 s->Printf(" using breakpoint site %d", m_return_bp_id);
220 for (StackFrameSP frame_sp : m_stepped_past_frames) {
221 s->Printf("Stepped out past: ");
222 frame_sp->DumpUsingSettingsFormat(s);
226 bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
227 if (m_step_out_to_inline_plan_sp)
228 return m_step_out_to_inline_plan_sp->ValidatePlan(error);
230 if (m_step_through_inline_plan_sp)
231 return m_step_through_inline_plan_sp->ValidatePlan(error);
233 if (m_could_not_resolve_hw_bp) {
236 "Could not create hardware breakpoint for thread plan.");
240 if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
242 error->PutCString("Could not create return address breakpoint.");
249 bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
250 // If the step out plan is done, then we just need to step through the
252 if (m_step_out_to_inline_plan_sp) {
253 return m_step_out_to_inline_plan_sp->MischiefManaged();
254 } else if (m_step_through_inline_plan_sp) {
255 if (m_step_through_inline_plan_sp->MischiefManaged()) {
256 CalculateReturnValue();
261 } else if (m_step_out_further_plan_sp) {
262 return m_step_out_further_plan_sp->MischiefManaged();
265 // We don't explain signals or breakpoints (breakpoints that handle stepping
266 // in or out will be handled by a child plan.
268 StopInfoSP stop_info_sp = GetPrivateStopInfo();
270 StopReason reason = stop_info_sp->GetStopReason();
271 if (reason == eStopReasonBreakpoint) {
272 // If this is OUR breakpoint, we're fine, otherwise we don't know why
274 BreakpointSiteSP site_sp(
275 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
276 stop_info_sp->GetValue()));
277 if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
280 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
282 if (m_step_out_to_id == frame_zero_id)
284 else if (m_step_out_to_id < frame_zero_id) {
285 // Either we stepped past the breakpoint, or the stack ID calculation
286 // was incorrect and we should probably stop.
289 done = (m_immediate_step_from_id < frame_zero_id);
293 if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
294 CalculateReturnValue();
299 // If there was only one owner, then we're done. But if we also hit
300 // some user breakpoint on our way out, we should mark ourselves as
301 // done, but also not claim to explain the stop, since it is more
302 // important to report the user breakpoint than the step out
305 if (site_sp->GetNumberOfOwners() == 1)
309 } else if (IsUsuallyUnexplainedStopReason(reason))
317 bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
318 if (IsPlanComplete())
322 if (m_step_out_to_inline_plan_sp) {
323 if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
324 // Now step through the inlined stack we are in:
325 if (QueueInlinedStepPlan(true)) {
326 // If we can't queue a plan to do this, then just call ourselves done.
327 m_step_out_to_inline_plan_sp.reset();
328 SetPlanComplete(false);
333 return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
334 } else if (m_step_through_inline_plan_sp) {
335 if (m_step_through_inline_plan_sp->MischiefManaged())
338 return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
339 } else if (m_step_out_further_plan_sp) {
340 if (m_step_out_further_plan_sp->MischiefManaged())
341 m_step_out_further_plan_sp.reset();
343 return m_step_out_further_plan_sp->ShouldStop(event_ptr);
347 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
348 done = !(frame_zero_id < m_step_out_to_id);
351 // The normal step out computations think we are done, so all we need to do
352 // is consult the ShouldStopHere, and we are done.
355 if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
356 CalculateReturnValue();
359 m_step_out_further_plan_sp =
360 QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder, m_status);
368 bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
370 StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
372 bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
374 if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
377 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
381 Breakpoint *return_bp =
382 m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
383 if (return_bp != nullptr)
384 return_bp->SetEnabled(true);
389 bool ThreadPlanStepOut::WillStop() {
390 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
391 Breakpoint *return_bp =
392 m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
393 if (return_bp != nullptr)
394 return_bp->SetEnabled(false);
400 bool ThreadPlanStepOut::MischiefManaged() {
401 if (IsPlanComplete()) {
402 // Did I reach my breakpoint? If so I'm done.
404 // I also check the stack depth, since if we've blown past the breakpoint
406 // reason and we're now stopping for some other reason altogether, then
407 // we're done with this step out operation.
409 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
411 log->Printf("Completed step out plan.");
412 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
413 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
414 m_return_bp_id = LLDB_INVALID_BREAK_ID;
417 ThreadPlan::MischiefManaged();
424 bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
425 // Now figure out the range of this inlined block, and set up a "step through
426 // range" plan for that. If we've been provided with a context, then use the
427 // block in that context.
428 StackFrameSP immediate_return_from_sp(m_thread.GetStackFrameAtIndex(0));
429 if (!immediate_return_from_sp)
432 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
435 immediate_return_from_sp->Dump(&s, true, false);
436 log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
439 Block *from_block = immediate_return_from_sp->GetFrameBlock();
441 Block *inlined_block = from_block->GetContainingInlinedBlock();
443 size_t num_ranges = inlined_block->GetNumRanges();
444 AddressRange inline_range;
445 if (inlined_block->GetRangeAtIndex(0, inline_range)) {
446 SymbolContext inlined_sc;
447 inlined_block->CalculateSymbolContext(&inlined_sc);
448 inlined_sc.target_sp = GetTarget().shared_from_this();
450 m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
451 const LazyBool avoid_no_debug = eLazyBoolNo;
453 m_step_through_inline_plan_sp.reset(new ThreadPlanStepOverRange(
454 m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug));
455 ThreadPlanStepOverRange *step_through_inline_plan_ptr =
456 static_cast<ThreadPlanStepOverRange *>(
457 m_step_through_inline_plan_sp.get());
458 m_step_through_inline_plan_sp->SetPrivate(true);
460 step_through_inline_plan_ptr->SetOkayToDiscard(true);
462 if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
463 // FIXME: Log this failure.
464 delete step_through_inline_plan_ptr;
468 for (size_t i = 1; i < num_ranges; i++) {
469 if (inlined_block->GetRangeAtIndex(i, inline_range))
470 step_through_inline_plan_ptr->AddRange(inline_range);
474 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
483 void ThreadPlanStepOut::CalculateReturnValue() {
484 if (m_return_valobj_sp)
487 if (!m_calculate_return_value)
490 if (m_immediate_step_from_function != nullptr) {
491 CompilerType return_compiler_type =
492 m_immediate_step_from_function->GetCompilerType()
493 .GetFunctionReturnType();
494 if (return_compiler_type) {
495 lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
498 abi_sp->GetReturnValueObject(m_thread, return_compiler_type);
503 bool ThreadPlanStepOut::IsPlanStale() {
504 // If we are still lower on the stack than the frame we are returning to,
505 // then there's something for us to do. Otherwise, we're stale.
507 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
508 return !(frame_zero_id < m_step_out_to_id);