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