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