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