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