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