1 //===-- ThreadPlanStepOverRange.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/ThreadPlanStepOverRange.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 
17 #include "lldb/lldb-private-log.h"
18 #include "lldb/Core/Log.h"
19 #include "lldb/Core/Stream.h"
20 #include "lldb/Symbol/Block.h"
21 #include "lldb/Symbol/CompileUnit.h"
22 #include "lldb/Symbol/Function.h"
23 #include "lldb/Symbol/LineTable.h"
24 #include "lldb/Target/Process.h"
25 #include "lldb/Target/RegisterContext.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Target/Thread.h"
28 #include "lldb/Target/ThreadPlanStepOut.h"
29 #include "lldb/Target/ThreadPlanStepThrough.h"
30 
31 using namespace lldb_private;
32 using namespace lldb;
33 
34 uint32_t ThreadPlanStepOverRange::s_default_flag_values = 0;
35 
36 //----------------------------------------------------------------------
37 // ThreadPlanStepOverRange: Step through a stack range, either stepping over or into
38 // based on the value of \a type.
39 //----------------------------------------------------------------------
40 
41 ThreadPlanStepOverRange::ThreadPlanStepOverRange
42 (
43     Thread &thread,
44     const AddressRange &range,
45     const SymbolContext &addr_context,
46     lldb::RunMode stop_others,
47     LazyBool step_out_avoids_code_without_debug_info
48 ) :
49     ThreadPlanStepRange (ThreadPlan::eKindStepOverRange, "Step range stepping over", thread, range, addr_context, stop_others),
50     ThreadPlanShouldStopHere (this),
51     m_first_resume(true)
52 {
53     SetFlagsToDefault();
54     SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
55 }
56 
57 ThreadPlanStepOverRange::~ThreadPlanStepOverRange ()
58 {
59 }
60 
61 void
62 ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
63 {
64     if (level == lldb::eDescriptionLevelBrief)
65         s->Printf("step over");
66     else
67     {
68         s->Printf ("stepping through range (stepping over functions): ");
69         DumpRanges(s);
70     }
71 }
72 
73 void
74 ThreadPlanStepOverRange::SetupAvoidNoDebug(LazyBool step_out_avoids_code_without_debug_info)
75 {
76     bool avoid_nodebug = true;
77     switch (step_out_avoids_code_without_debug_info)
78     {
79         case eLazyBoolYes:
80             avoid_nodebug = true;
81             break;
82         case eLazyBoolNo:
83             avoid_nodebug = false;
84             break;
85         case eLazyBoolCalculate:
86             avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
87             break;
88     }
89     if (avoid_nodebug)
90         GetFlags().Set (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
91     else
92         GetFlags().Clear (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
93 }
94 
95 bool
96 ThreadPlanStepOverRange::IsEquivalentContext(const SymbolContext &context)
97 {
98 
99     // Match as much as is specified in the m_addr_context:
100     // This is a fairly loose sanity check.  Note, sometimes the target doesn't get filled
101     // in so I left out the target check.  And sometimes the module comes in as the .o file from the
102     // inlined range, so I left that out too...
103     if (m_addr_context.comp_unit)
104     {
105         if (m_addr_context.comp_unit == context.comp_unit)
106         {
107             if (m_addr_context.function && m_addr_context.function == context.function)
108             {
109                 if (m_addr_context.block && m_addr_context.block == context.block)
110                     return true;
111             }
112         }
113     }
114     else if (m_addr_context.symbol && m_addr_context.symbol == context.symbol)
115     {
116         return true;
117     }
118     return false;
119 }
120 
121 bool
122 ThreadPlanStepOverRange::ShouldStop (Event *event_ptr)
123 {
124     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
125 
126     if (log)
127     {
128         StreamString s;
129         s.Address (m_thread.GetRegisterContext()->GetPC(),
130                    m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
131         log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
132     }
133 
134     // If we're out of the range but in the same frame or in our caller's frame
135     // then we should stop.
136     // When stepping out we only stop others if we are forcing running one thread.
137     bool stop_others;
138     if (m_stop_others == lldb::eOnlyThisThread)
139         stop_others = true;
140     else
141         stop_others = false;
142 
143     ThreadPlanSP new_plan_sp;
144 
145     FrameComparison frame_order = CompareCurrentFrameToStartFrame();
146 
147     if (frame_order == eFrameCompareOlder)
148     {
149         // If we're in an older frame then we should stop.
150         //
151         // A caveat to this is if we think the frame is older but we're actually in a trampoline.
152         // I'm going to make the assumption that you wouldn't RETURN to a trampoline.  So if we are
153         // in a trampoline we think the frame is older because the trampoline confused the backtracer.
154         // As below, we step through first, and then try to figure out how to get back out again.
155 
156         new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
157 
158         if (new_plan_sp && log)
159             log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
160     }
161     else if (frame_order == eFrameCompareYounger)
162     {
163         // Make sure we really are in a new frame.  Do that by unwinding and seeing if the
164         // start function really is our start function...
165         for(uint32_t i = 1;; ++i)
166         {
167             StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(i);
168             if (!older_frame_sp) {
169                 // We can't unwind the next frame we should just get out of here & stop...
170                 break;
171             }
172 
173             const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
174             if (IsEquivalentContext(older_context))
175             {
176                 new_plan_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop (false,
177                                                                               NULL,
178                                                                               true,
179                                                                               stop_others,
180                                                                               eVoteNo,
181                                                                               eVoteNoOpinion,
182                                                                               0);
183                 break;
184             }
185             else
186             {
187                 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
188             }
189         }
190     }
191     else
192     {
193         // If we're still in the range, keep going.
194         if (InRange())
195         {
196             SetNextBranchBreakpoint();
197             return false;
198         }
199 
200 
201         if (!InSymbol())
202         {
203             // This one is a little tricky.  Sometimes we may be in a stub or something similar,
204             // in which case we need to get out of there.  But if we are in a stub then it's
205             // likely going to be hard to get out from here.  It is probably easiest to step into the
206             // stub, and then it will be straight-forward to step out.
207             new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
208         }
209         else
210         {
211             // The current clang (at least through 424) doesn't always get the address range for the
212             // DW_TAG_inlined_subroutines right, so that when you leave the inlined range the line table says
213             // you are still in the source file of the inlining function.  This is bad, because now you are missing
214             // the stack frame for the function containing the inlining, and if you sensibly do "finish" to get
215             // out of this function you will instead exit the containing function.
216             // To work around this, we check whether we are still in the source file we started in, and if not assume
217             // it is an error, and push a plan to get us out of this line and back to the containing file.
218 
219             if (m_addr_context.line_entry.IsValid())
220             {
221                 SymbolContext sc;
222                 StackFrameSP frame_sp = m_thread.GetStackFrameAtIndex(0);
223                 sc = frame_sp->GetSymbolContext (eSymbolContextEverything);
224                 if (sc.line_entry.IsValid())
225                 {
226                     if (sc.line_entry.file != m_addr_context.line_entry.file
227                          && sc.comp_unit == m_addr_context.comp_unit
228                          && sc.function == m_addr_context.function)
229                     {
230                         // Okay, find the next occurance of this file in the line table:
231                         LineTable *line_table = m_addr_context.comp_unit->GetLineTable();
232                         if (line_table)
233                         {
234                             Address cur_address = frame_sp->GetFrameCodeAddress();
235                             uint32_t entry_idx;
236                             LineEntry line_entry;
237                             if (line_table->FindLineEntryByAddress (cur_address, line_entry, &entry_idx))
238                             {
239                                 LineEntry next_line_entry;
240                                 bool step_past_remaining_inline = false;
241                                 if (entry_idx > 0)
242                                 {
243                                     // We require the the previous line entry and the current line entry come
244                                     // from the same file.
245                                     // The other requirement is that the previous line table entry be part of an
246                                     // inlined block, we don't want to step past cases where people have inlined
247                                     // some code fragment by using #include <source-fragment.c> directly.
248                                     LineEntry prev_line_entry;
249                                     if (line_table->GetLineEntryAtIndex(entry_idx - 1, prev_line_entry)
250                                         && prev_line_entry.file == line_entry.file)
251                                     {
252                                         SymbolContext prev_sc;
253                                         Address prev_address = prev_line_entry.range.GetBaseAddress();
254                                         prev_address.CalculateSymbolContext(&prev_sc);
255                                         if (prev_sc.block)
256                                         {
257                                             Block *inlined_block = prev_sc.block->GetContainingInlinedBlock();
258                                             if (inlined_block)
259                                             {
260                                                 AddressRange inline_range;
261                                                 inlined_block->GetRangeContainingAddress(prev_address, inline_range);
262                                                 if (!inline_range.ContainsFileAddress(cur_address))
263                                                 {
264 
265                                                     step_past_remaining_inline = true;
266                                                 }
267 
268                                             }
269                                         }
270                                     }
271                                 }
272 
273                                 if (step_past_remaining_inline)
274                                 {
275                                     uint32_t look_ahead_step = 1;
276                                     while (line_table->GetLineEntryAtIndex(entry_idx + look_ahead_step, next_line_entry))
277                                     {
278                                         // Make sure we haven't wandered out of the function we started from...
279                                         Address next_line_address = next_line_entry.range.GetBaseAddress();
280                                         Function *next_line_function = next_line_address.CalculateSymbolContextFunction();
281                                         if (next_line_function != m_addr_context.function)
282                                             break;
283 
284                                         if (next_line_entry.file == m_addr_context.line_entry.file)
285                                         {
286                                             const bool abort_other_plans = false;
287                                             const bool stop_other_threads = false;
288                                             new_plan_sp = m_thread.QueueThreadPlanForRunToAddress(abort_other_plans,
289                                                                                                next_line_address,
290                                                                                                stop_other_threads);
291                                             break;
292                                         }
293                                         look_ahead_step++;
294                                     }
295                                 }
296                             }
297                         }
298                     }
299                 }
300             }
301         }
302     }
303 
304     // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
305     ClearNextBranchBreakpoint();
306 
307 
308     // If we haven't figured out something to do yet, then ask the ShouldStopHere callback:
309     if (!new_plan_sp)
310     {
311         new_plan_sp = CheckShouldStopHereAndQueueStepOut (frame_order);
312     }
313 
314     if (!new_plan_sp)
315         m_no_more_plans = true;
316     else
317         m_no_more_plans = false;
318 
319     if (!new_plan_sp)
320     {
321         // For efficiencies sake, we know we're done here so we don't have to do this
322         // calculation again in MischiefManaged.
323         SetPlanComplete();
324         return true;
325     }
326     else
327         return false;
328 }
329 
330 bool
331 ThreadPlanStepOverRange::DoPlanExplainsStop (Event *event_ptr)
332 {
333     // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us)
334     // handle the stop.  That way the user can see the stop, step around, and then when they
335     // are done, continue and have their step complete.  The exception is if we've hit our
336     // "run to next branch" breakpoint.
337     // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an
338     // unexplained breakpoint/crash.
339 
340     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
341     StopInfoSP stop_info_sp = GetPrivateStopInfo ();
342     bool return_value;
343 
344     if (stop_info_sp)
345     {
346         StopReason reason = stop_info_sp->GetStopReason();
347 
348         switch (reason)
349         {
350         case eStopReasonTrace:
351             return_value = true;
352             break;
353         case eStopReasonBreakpoint:
354             if (NextRangeBreakpointExplainsStop(stop_info_sp))
355                 return_value = true;
356             else
357                 return_value = false;
358             break;
359         case eStopReasonWatchpoint:
360         case eStopReasonSignal:
361         case eStopReasonException:
362         case eStopReasonExec:
363         case eStopReasonThreadExiting:
364         default:
365             if (log)
366                 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
367             return_value = false;
368             break;
369         }
370     }
371     else
372         return_value = true;
373 
374     return return_value;
375 }
376 
377 bool
378 ThreadPlanStepOverRange::DoWillResume (lldb::StateType resume_state, bool current_plan)
379 {
380     if (resume_state != eStateSuspended && m_first_resume)
381     {
382         m_first_resume = false;
383         if (resume_state == eStateStepping && current_plan)
384         {
385             // See if we are about to step over an inlined call in the middle of the inlined stack, if so figure
386             // out its extents and reset our range to step over that.
387             bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth();
388             if (in_inlined_stack)
389             {
390                 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
391                 if (log)
392                     log->Printf ("ThreadPlanStepInRange::DoWillResume: adjusting range to the frame at inlined depth %d.",
393                                  m_thread.GetCurrentInlinedDepth());
394                 StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0);
395                 if (stack_sp)
396                 {
397                     Block *frame_block = stack_sp->GetFrameBlock();
398                     lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
399                     AddressRange my_range;
400                     if (frame_block->GetRangeContainingLoadAddress(curr_pc, m_thread.GetProcess()->GetTarget(), my_range))
401                     {
402                         m_address_ranges.clear();
403                         m_address_ranges.push_back(my_range);
404                         if (log)
405                         {
406                             StreamString s;
407                             const InlineFunctionInfo *inline_info = frame_block->GetInlinedFunctionInfo();
408                             const char *name;
409                             if (inline_info)
410                                 name = inline_info->GetName().AsCString();
411                             else
412                                 name = "<unknown-notinlined>";
413 
414                             s.Printf ("Stepping over inlined function \"%s\" in inlined stack: ", name);
415                             DumpRanges(&s);
416                             log->PutCString(s.GetData());
417                         }
418                     }
419 
420                 }
421             }
422         }
423     }
424 
425     return true;
426 }
427 
428