1 //===-- ThreadPlanStepOut.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Target/ThreadPlanStepOut.h"
10 #include "lldb/Breakpoint/Breakpoint.h"
11 #include "lldb/Core/Value.h"
12 #include "lldb/Core/ValueObjectConstResult.h"
13 #include "lldb/Symbol/Block.h"
14 #include "lldb/Symbol/Function.h"
15 #include "lldb/Symbol/Symbol.h"
16 #include "lldb/Symbol/Type.h"
17 #include "lldb/Target/ABI.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Target/RegisterContext.h"
20 #include "lldb/Target/StopInfo.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Target/ThreadPlanStepOverRange.h"
23 #include "lldb/Target/ThreadPlanStepThrough.h"
24 #include "lldb/Utility/Log.h"
25 
26 #include <memory>
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 uint32_t ThreadPlanStepOut::s_default_flag_values = 0;
32 
33 // ThreadPlanStepOut: Step out of the current frame
34 ThreadPlanStepOut::ThreadPlanStepOut(
35     Thread &thread, SymbolContext *context, bool first_insn, bool stop_others,
36     Vote stop_vote, Vote run_vote, uint32_t frame_idx,
37     LazyBool step_out_avoids_code_without_debug_info,
38     bool continue_to_next_branch, bool gather_return_value)
39     : ThreadPlan(ThreadPlan::eKindStepOut, "Step out", thread, stop_vote,
40                  run_vote),
41       ThreadPlanShouldStopHere(this), m_step_from_insn(LLDB_INVALID_ADDRESS),
42       m_return_bp_id(LLDB_INVALID_BREAK_ID),
43       m_return_addr(LLDB_INVALID_ADDRESS), m_stop_others(stop_others),
44       m_immediate_step_from_function(nullptr),
45       m_calculate_return_value(gather_return_value) {
46   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
47   SetFlagsToDefault();
48   SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
49 
50   m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
51 
52   uint32_t return_frame_index = frame_idx + 1;
53   StackFrameSP return_frame_sp(
54       m_thread.GetStackFrameAtIndex(return_frame_index));
55   StackFrameSP immediate_return_from_sp(
56       m_thread.GetStackFrameAtIndex(frame_idx));
57 
58   if (!return_frame_sp || !immediate_return_from_sp)
59     return; // we can't do anything here.  ValidatePlan() will return false.
60 
61   // While stepping out, behave as-if artificial frames are not present.
62   while (return_frame_sp->IsArtificial()) {
63     m_stepped_past_frames.push_back(return_frame_sp);
64 
65     ++return_frame_index;
66     return_frame_sp = m_thread.GetStackFrameAtIndex(return_frame_index);
67 
68     // We never expect to see an artificial frame without a regular ancestor.
69     // If this happens, log the issue and defensively refuse to step out.
70     if (!return_frame_sp) {
71       LLDB_LOG(log, "Can't step out of frame with artificial ancestors");
72       return;
73     }
74   }
75 
76   m_step_out_to_id = return_frame_sp->GetStackID();
77   m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
78 
79   // If the frame directly below the one we are returning to is inlined, we
80   // have to be a little more careful.  It is non-trivial to determine the real
81   // "return code address" for an inlined frame, so we have to work our way to
82   // that frame and then step out.
83   if (immediate_return_from_sp->IsInlined()) {
84     if (frame_idx > 0) {
85       // First queue a plan that gets us to this inlined frame, and when we get
86       // there we'll queue a second plan that walks us out of this frame.
87       m_step_out_to_inline_plan_sp = std::make_shared<ThreadPlanStepOut>(
88           m_thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
89           frame_idx - 1, eLazyBoolNo, continue_to_next_branch);
90       static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())
91           ->SetShouldStopHereCallbacks(nullptr, nullptr);
92       m_step_out_to_inline_plan_sp->SetPrivate(true);
93     } else {
94       // If we're already at the inlined frame we're stepping through, then
95       // just do that now.
96       QueueInlinedStepPlan(false);
97     }
98   } else {
99     // Find the return address and set a breakpoint there:
100     // FIXME - can we do this more securely if we know first_insn?
101 
102     Address return_address(return_frame_sp->GetFrameCodeAddress());
103     if (continue_to_next_branch) {
104       SymbolContext return_address_sc;
105       AddressRange range;
106       Address return_address_decr_pc = return_address;
107       if (return_address_decr_pc.GetOffset() > 0)
108         return_address_decr_pc.Slide(-1);
109 
110       return_address_decr_pc.CalculateSymbolContext(
111           &return_address_sc, lldb::eSymbolContextLineEntry);
112       if (return_address_sc.line_entry.IsValid()) {
113         const bool include_inlined_functions = false;
114         range = return_address_sc.line_entry.GetSameLineContiguousAddressRange(
115             include_inlined_functions);
116         if (range.GetByteSize() > 0) {
117           return_address =
118               m_thread.GetProcess()->AdvanceAddressToNextBranchInstruction(
119                   return_address, range);
120         }
121       }
122     }
123     m_return_addr =
124         return_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget());
125 
126     if (m_return_addr == LLDB_INVALID_ADDRESS)
127       return;
128 
129     // Perform some additional validation on the return address.
130     uint32_t permissions = 0;
131     if (!m_thread.GetProcess()->GetLoadAddressPermissions(m_return_addr,
132                                                           permissions)) {
133       LLDB_LOGF(log, "ThreadPlanStepOut(%p): Return address (0x%" PRIx64
134                 ") permissions not found.", static_cast<void *>(this),
135                 m_return_addr);
136     } else if (!(permissions & ePermissionsExecutable)) {
137       m_constructor_errors.Printf("Return address (0x%" PRIx64
138                                   ") did not point to executable memory.",
139                                   m_return_addr);
140       LLDB_LOGF(log, "ThreadPlanStepOut(%p): %s", static_cast<void *>(this),
141                 m_constructor_errors.GetData());
142       return;
143     }
144 
145     Breakpoint *return_bp = m_thread.CalculateTarget()
146                                 ->CreateBreakpoint(m_return_addr, true, false)
147                                 .get();
148 
149     if (return_bp != nullptr) {
150       if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
151         m_could_not_resolve_hw_bp = true;
152       return_bp->SetThreadID(m_thread.GetID());
153       m_return_bp_id = return_bp->GetID();
154       return_bp->SetBreakpointKind("step-out");
155     }
156 
157     if (immediate_return_from_sp) {
158       const SymbolContext &sc =
159           immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
160       if (sc.function) {
161         m_immediate_step_from_function = sc.function;
162       }
163     }
164   }
165 }
166 
167 void ThreadPlanStepOut::SetupAvoidNoDebug(
168     LazyBool step_out_avoids_code_without_debug_info) {
169   bool avoid_nodebug = true;
170   switch (step_out_avoids_code_without_debug_info) {
171   case eLazyBoolYes:
172     avoid_nodebug = true;
173     break;
174   case eLazyBoolNo:
175     avoid_nodebug = false;
176     break;
177   case eLazyBoolCalculate:
178     avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
179     break;
180   }
181   if (avoid_nodebug)
182     GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
183   else
184     GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
185 }
186 
187 void ThreadPlanStepOut::DidPush() {
188   if (m_step_out_to_inline_plan_sp)
189     m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
190   else if (m_step_through_inline_plan_sp)
191     m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
192 }
193 
194 ThreadPlanStepOut::~ThreadPlanStepOut() {
195   if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
196     m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
197 }
198 
199 void ThreadPlanStepOut::GetDescription(Stream *s,
200                                        lldb::DescriptionLevel level) {
201   if (level == lldb::eDescriptionLevelBrief)
202     s->Printf("step out");
203   else {
204     if (m_step_out_to_inline_plan_sp)
205       s->Printf("Stepping out to inlined frame so we can walk through it.");
206     else if (m_step_through_inline_plan_sp)
207       s->Printf("Stepping out by stepping through inlined function.");
208     else {
209       s->Printf("Stepping out from ");
210       Address tmp_address;
211       if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) {
212         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
213                          Address::DumpStyleLoadAddress);
214       } else {
215         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn);
216       }
217 
218       // FIXME: find some useful way to present the m_return_id, since there may
219       // be multiple copies of the
220       // same function on the stack.
221 
222       s->Printf(" returning to frame at ");
223       if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) {
224         tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
225                          Address::DumpStyleLoadAddress);
226       } else {
227         s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
228       }
229 
230       if (level == eDescriptionLevelVerbose)
231         s->Printf(" using breakpoint site %d", m_return_bp_id);
232     }
233   }
234 
235   s->Printf("\n");
236   for (StackFrameSP frame_sp : m_stepped_past_frames) {
237     s->Printf("Stepped out past: ");
238     frame_sp->DumpUsingSettingsFormat(s);
239   }
240 }
241 
242 bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
243   if (m_step_out_to_inline_plan_sp)
244     return m_step_out_to_inline_plan_sp->ValidatePlan(error);
245 
246   if (m_step_through_inline_plan_sp)
247     return m_step_through_inline_plan_sp->ValidatePlan(error);
248 
249   if (m_could_not_resolve_hw_bp) {
250     if (error)
251       error->PutCString(
252           "Could not create hardware breakpoint for thread plan.");
253     return false;
254   }
255 
256   if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
257     if (error) {
258       error->PutCString("Could not create return address breakpoint.");
259       if (m_constructor_errors.GetSize() > 0) {
260         error->PutCString(" ");
261         error->PutCString(m_constructor_errors.GetString());
262       }
263     }
264     return false;
265   }
266 
267   return true;
268 }
269 
270 bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
271   // If the step out plan is done, then we just need to step through the
272   // inlined frame.
273   if (m_step_out_to_inline_plan_sp) {
274     return m_step_out_to_inline_plan_sp->MischiefManaged();
275   } else if (m_step_through_inline_plan_sp) {
276     if (m_step_through_inline_plan_sp->MischiefManaged()) {
277       CalculateReturnValue();
278       SetPlanComplete();
279       return true;
280     } else
281       return false;
282   } else if (m_step_out_further_plan_sp) {
283     return m_step_out_further_plan_sp->MischiefManaged();
284   }
285 
286   // We don't explain signals or breakpoints (breakpoints that handle stepping
287   // in or out will be handled by a child plan.
288 
289   StopInfoSP stop_info_sp = GetPrivateStopInfo();
290   if (stop_info_sp) {
291     StopReason reason = stop_info_sp->GetStopReason();
292     if (reason == eStopReasonBreakpoint) {
293       // If this is OUR breakpoint, we're fine, otherwise we don't know why
294       // this happened...
295       BreakpointSiteSP site_sp(
296           m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
297               stop_info_sp->GetValue()));
298       if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
299         bool done;
300 
301         StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
302 
303         if (m_step_out_to_id == frame_zero_id)
304           done = true;
305         else if (m_step_out_to_id < frame_zero_id) {
306           // Either we stepped past the breakpoint, or the stack ID calculation
307           // was incorrect and we should probably stop.
308           done = true;
309         } else {
310           done = (m_immediate_step_from_id < frame_zero_id);
311         }
312 
313         if (done) {
314           if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
315             CalculateReturnValue();
316             SetPlanComplete();
317           }
318         }
319 
320         // If there was only one owner, then we're done.  But if we also hit
321         // some user breakpoint on our way out, we should mark ourselves as
322         // done, but also not claim to explain the stop, since it is more
323         // important to report the user breakpoint than the step out
324         // completion.
325 
326         if (site_sp->GetNumberOfOwners() == 1)
327           return true;
328       }
329       return false;
330     } else if (IsUsuallyUnexplainedStopReason(reason))
331       return false;
332     else
333       return true;
334   }
335   return true;
336 }
337 
338 bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
339   if (IsPlanComplete())
340     return true;
341 
342   bool done = false;
343   if (m_step_out_to_inline_plan_sp) {
344     if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
345       // Now step through the inlined stack we are in:
346       if (QueueInlinedStepPlan(true)) {
347         // If we can't queue a plan to do this, then just call ourselves done.
348         m_step_out_to_inline_plan_sp.reset();
349         SetPlanComplete(false);
350         return true;
351       } else
352         done = true;
353     } else
354       return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
355   } else if (m_step_through_inline_plan_sp) {
356     if (m_step_through_inline_plan_sp->MischiefManaged())
357       done = true;
358     else
359       return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
360   } 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();
363     else
364       return m_step_out_further_plan_sp->ShouldStop(event_ptr);
365   }
366 
367   if (!done) {
368     StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
369     done = !(frame_zero_id < m_step_out_to_id);
370   }
371 
372   // The normal step out computations think we are done, so all we need to do
373   // is consult the ShouldStopHere, and we are done.
374 
375   if (done) {
376     if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
377       CalculateReturnValue();
378       SetPlanComplete();
379     } else {
380       m_step_out_further_plan_sp =
381           QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder, m_status);
382       done = false;
383     }
384   }
385 
386   return done;
387 }
388 
389 bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
390 
391 StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
392 
393 bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
394                                      bool current_plan) {
395   if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
396     return true;
397 
398   if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
399     return false;
400 
401   if (current_plan) {
402     Breakpoint *return_bp =
403         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
404     if (return_bp != nullptr)
405       return_bp->SetEnabled(true);
406   }
407   return true;
408 }
409 
410 bool ThreadPlanStepOut::WillStop() {
411   if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
412     Breakpoint *return_bp =
413         m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
414     if (return_bp != nullptr)
415       return_bp->SetEnabled(false);
416   }
417 
418   return true;
419 }
420 
421 bool ThreadPlanStepOut::MischiefManaged() {
422   if (IsPlanComplete()) {
423     // Did I reach my breakpoint?  If so I'm done.
424     //
425     // I also check the stack depth, since if we've blown past the breakpoint
426     // for some
427     // reason and we're now stopping for some other reason altogether, then
428     // we're done with this step out operation.
429 
430     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
431     if (log)
432       LLDB_LOGF(log, "Completed step out plan.");
433     if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
434       m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
435       m_return_bp_id = LLDB_INVALID_BREAK_ID;
436     }
437 
438     ThreadPlan::MischiefManaged();
439     return true;
440   } else {
441     return false;
442   }
443 }
444 
445 bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
446   // Now figure out the range of this inlined block, and set up a "step through
447   // range" plan for that.  If we've been provided with a context, then use the
448   // block in that context.
449   StackFrameSP immediate_return_from_sp(m_thread.GetStackFrameAtIndex(0));
450   if (!immediate_return_from_sp)
451     return false;
452 
453   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
454   if (log) {
455     StreamString s;
456     immediate_return_from_sp->Dump(&s, true, false);
457     LLDB_LOGF(log, "Queuing inlined frame to step past: %s.", s.GetData());
458   }
459 
460   Block *from_block = immediate_return_from_sp->GetFrameBlock();
461   if (from_block) {
462     Block *inlined_block = from_block->GetContainingInlinedBlock();
463     if (inlined_block) {
464       size_t num_ranges = inlined_block->GetNumRanges();
465       AddressRange inline_range;
466       if (inlined_block->GetRangeAtIndex(0, inline_range)) {
467         SymbolContext inlined_sc;
468         inlined_block->CalculateSymbolContext(&inlined_sc);
469         inlined_sc.target_sp = GetTarget().shared_from_this();
470         RunMode run_mode =
471             m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
472         const LazyBool avoid_no_debug = eLazyBoolNo;
473 
474         m_step_through_inline_plan_sp =
475             std::make_shared<ThreadPlanStepOverRange>(
476                 m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug);
477         ThreadPlanStepOverRange *step_through_inline_plan_ptr =
478             static_cast<ThreadPlanStepOverRange *>(
479                 m_step_through_inline_plan_sp.get());
480         m_step_through_inline_plan_sp->SetPrivate(true);
481 
482         step_through_inline_plan_ptr->SetOkayToDiscard(true);
483         StreamString errors;
484         if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
485           // FIXME: Log this failure.
486           delete step_through_inline_plan_ptr;
487           return false;
488         }
489 
490         for (size_t i = 1; i < num_ranges; i++) {
491           if (inlined_block->GetRangeAtIndex(i, inline_range))
492             step_through_inline_plan_ptr->AddRange(inline_range);
493         }
494 
495         if (queue_now)
496           m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
497         return true;
498       }
499     }
500   }
501 
502   return false;
503 }
504 
505 void ThreadPlanStepOut::CalculateReturnValue() {
506   if (m_return_valobj_sp)
507     return;
508 
509   if (!m_calculate_return_value)
510     return;
511 
512   if (m_immediate_step_from_function != nullptr) {
513     CompilerType return_compiler_type =
514         m_immediate_step_from_function->GetCompilerType()
515             .GetFunctionReturnType();
516     if (return_compiler_type) {
517       lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
518       if (abi_sp)
519         m_return_valobj_sp =
520             abi_sp->GetReturnValueObject(m_thread, return_compiler_type);
521     }
522   }
523 }
524 
525 bool ThreadPlanStepOut::IsPlanStale() {
526   // If we are still lower on the stack than the frame we are returning to,
527   // then there's something for us to do.  Otherwise, we're stale.
528 
529   StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
530   return !(frame_zero_id < m_step_out_to_id);
531 }
532