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