]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Target/ThreadPlanStepInRange.cpp
MFV r302218: file 5.28.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Target / ThreadPlanStepInRange.cpp
1 //===-- ThreadPlanStepInRange.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 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project includes
14 #include "lldb/Target/ThreadPlanStepInRange.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Symbol/Symbol.h"
19 #include "lldb/Symbol/Function.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/RegisterContext.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/Thread.h"
24 #include "lldb/Target/ThreadPlanStepOut.h"
25 #include "lldb/Target/ThreadPlanStepThrough.h"
26 #include "lldb/Core/RegularExpression.h"
27
28 using namespace lldb;
29 using namespace lldb_private;
30
31 uint32_t ThreadPlanStepInRange::s_default_flag_values = ThreadPlanShouldStopHere::eStepInAvoidNoDebug;
32
33 //----------------------------------------------------------------------
34 // ThreadPlanStepInRange: Step through a stack range, either stepping over or into
35 // based on the value of \a type.
36 //----------------------------------------------------------------------
37
38 ThreadPlanStepInRange::ThreadPlanStepInRange
39 (
40     Thread &thread,
41     const AddressRange &range,
42     const SymbolContext &addr_context,
43     lldb::RunMode stop_others,
44     LazyBool step_in_avoids_code_without_debug_info,
45     LazyBool step_out_avoids_code_without_debug_info
46 ) :
47     ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
48     ThreadPlanShouldStopHere (this),
49     m_step_past_prologue (true),
50     m_virtual_step (false)
51 {
52     SetCallbacks();
53     SetFlagsToDefault ();
54     SetupAvoidNoDebug(step_in_avoids_code_without_debug_info, step_out_avoids_code_without_debug_info);
55 }
56
57 ThreadPlanStepInRange::ThreadPlanStepInRange
58 (
59     Thread &thread,
60     const AddressRange &range,
61     const SymbolContext &addr_context,
62     const char *step_into_target,
63     lldb::RunMode stop_others,
64     LazyBool step_in_avoids_code_without_debug_info,
65     LazyBool step_out_avoids_code_without_debug_info
66 ) :
67     ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
68     ThreadPlanShouldStopHere (this),
69     m_step_past_prologue (true),
70     m_virtual_step (false),
71     m_step_into_target (step_into_target)
72 {
73     SetCallbacks();
74     SetFlagsToDefault ();
75     SetupAvoidNoDebug(step_in_avoids_code_without_debug_info, step_out_avoids_code_without_debug_info);
76 }
77
78 ThreadPlanStepInRange::~ThreadPlanStepInRange() = default;
79
80 void
81 ThreadPlanStepInRange::SetupAvoidNoDebug(LazyBool step_in_avoids_code_without_debug_info,
82                                          LazyBool step_out_avoids_code_without_debug_info)
83 {
84     bool avoid_nodebug = true;
85     
86     switch (step_in_avoids_code_without_debug_info)
87     {
88         case eLazyBoolYes:
89             avoid_nodebug = true;
90             break;
91         case eLazyBoolNo:
92             avoid_nodebug = false;
93             break;
94         case eLazyBoolCalculate:
95             avoid_nodebug = m_thread.GetStepInAvoidsNoDebug();
96             break;
97     }
98     if (avoid_nodebug)
99         GetFlags().Set (ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
100     else
101         GetFlags().Clear (ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
102     
103     switch (step_out_avoids_code_without_debug_info)
104     {
105         case eLazyBoolYes:
106             avoid_nodebug = true;
107             break;
108         case eLazyBoolNo:
109             avoid_nodebug = false;
110             break;
111         case eLazyBoolCalculate:
112             avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
113             break;
114     }
115     if (avoid_nodebug)
116         GetFlags().Set (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
117     else
118         GetFlags().Clear (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
119 }
120
121 void
122 ThreadPlanStepInRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
123 {
124     if (level == lldb::eDescriptionLevelBrief)
125     {
126         s->Printf("step in");
127         return;
128     }
129
130     s->Printf ("Stepping in");
131     bool printed_line_info = false;
132     if (m_addr_context.line_entry.IsValid())
133     {
134         s->Printf (" through line ");
135         m_addr_context.line_entry.DumpStopContext (s, false);
136         printed_line_info = true;
137     }
138
139     const char *step_into_target = m_step_into_target.AsCString();
140     if (step_into_target && step_into_target[0] != '\0')
141         s->Printf (" targeting %s", m_step_into_target.AsCString());
142
143     if (!printed_line_info || level == eDescriptionLevelVerbose)
144     {
145         s->Printf (" using ranges:");
146         DumpRanges(s);
147     }
148
149     s->PutChar('.');
150 }
151
152 bool
153 ThreadPlanStepInRange::ShouldStop (Event *event_ptr)
154 {
155     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
156     
157     if (log)
158     {
159         StreamString s;
160         s.Address (m_thread.GetRegisterContext()->GetPC(), 
161                    m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
162         log->Printf("ThreadPlanStepInRange reached %s.", s.GetData());
163     }
164
165     if (IsPlanComplete())
166         return true;
167         
168     m_no_more_plans = false;
169     if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete())
170     {
171         if (!m_sub_plan_sp->PlanSucceeded())
172         {
173             SetPlanComplete();
174             m_no_more_plans = true;
175             return true;
176         }
177         else
178             m_sub_plan_sp.reset();
179     }
180     
181     if (m_virtual_step)
182     {
183         // If we've just completed a virtual step, all we need to do is check for a ShouldStopHere plan, and otherwise
184         // we're done.
185         // FIXME - This can be both a step in and a step out.  Probably should record which in the m_virtual_step.
186         m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(eFrameCompareYounger);
187     }
188     else
189     {
190         // Stepping through should be done running other threads in general, since we're setting a breakpoint and
191         // continuing.  So only stop others if we are explicitly told to do so.
192         
193         bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
194             
195         FrameComparison frame_order = CompareCurrentFrameToStartFrame();
196         
197         if (frame_order == eFrameCompareOlder || frame_order == eFrameCompareSameParent)
198         {
199             // If we're in an older frame then we should stop.
200             //
201             // A caveat to this is if we think the frame is older but we're actually in a trampoline.
202             // I'm going to make the assumption that you wouldn't RETURN to a trampoline.  So if we are
203             // in a trampoline we think the frame is older because the trampoline confused the backtracer.
204             m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
205             if (!m_sub_plan_sp)
206             {
207                 // Otherwise check the ShouldStopHere for step out:
208                 m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order);
209                 if (log)
210                     log->Printf ("ShouldStopHere says we should step out of this frame.");
211             }
212             else if (log)
213             {
214                 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
215             }
216         }
217         else if (frame_order == eFrameCompareEqual && InSymbol())
218         {
219             // If we are not in a place we should step through, we're done.
220             // One tricky bit here is that some stubs don't push a frame, so we have to check
221             // both the case of a frame that is younger, or the same as this frame.  
222             // However, if the frame is the same, and we are still in the symbol we started
223             // in, the we don't need to do this.  This first check isn't strictly necessary,
224             // but it is more efficient.
225             
226             // If we're still in the range, keep going, either by running to the next branch breakpoint, or by
227             // stepping.
228             if (InRange())
229             {
230                 SetNextBranchBreakpoint();
231                 return false;
232             }
233         
234             SetPlanComplete();
235             m_no_more_plans = true;
236             return true;
237         }
238         
239         // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
240         ClearNextBranchBreakpoint();
241         
242         // We may have set the plan up above in the FrameIsOlder section:
243         
244         if (!m_sub_plan_sp)
245             m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
246         
247         if (log)
248         {
249             if (m_sub_plan_sp)
250                 log->Printf ("Found a step through plan: %s", m_sub_plan_sp->GetName());
251             else
252                 log->Printf ("No step through plan found.");
253         }
254         
255         // If not, give the "should_stop" callback a chance to push a plan to get us out of here.
256         // But only do that if we actually have stepped in.
257         if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
258             m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order);
259
260         // If we've stepped in and we are going to stop here, check to see if we were asked to
261         // run past the prologue, and if so do that.
262         
263         if (!m_sub_plan_sp && frame_order == eFrameCompareYounger && m_step_past_prologue)
264         {
265             lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0);
266             if (curr_frame)
267             {
268                 size_t bytes_to_skip = 0;
269                 lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC();
270                 Address func_start_address;
271                 
272                 SymbolContext sc = curr_frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextSymbol);
273                 
274                 if (sc.function)
275                 {
276                     func_start_address = sc.function->GetAddressRange().GetBaseAddress();
277                     if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
278                         bytes_to_skip = sc.function->GetPrologueByteSize();
279                 }
280                 else if (sc.symbol)
281                 {
282                     func_start_address = sc.symbol->GetAddress();
283                     if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
284                         bytes_to_skip = sc.symbol->GetPrologueByteSize();
285                 }
286                 
287                 if (bytes_to_skip != 0)
288                 {
289                     func_start_address.Slide (bytes_to_skip);
290                     log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
291                     if (log)
292                         log->Printf ("Pushing past prologue ");
293                         
294                     m_sub_plan_sp = m_thread.QueueThreadPlanForRunToAddress(false, func_start_address,true);
295                 }
296             }
297         }
298      }
299     
300      if (!m_sub_plan_sp)
301      {
302         m_no_more_plans = true;
303         SetPlanComplete();
304         return true;
305     }
306     else
307     {
308         m_no_more_plans = false;
309         m_sub_plan_sp->SetPrivate(true);
310         return false;
311     }
312 }
313
314 void 
315 ThreadPlanStepInRange::SetAvoidRegexp(const char *name)
316 {
317     if (!m_avoid_regexp_ap)
318         m_avoid_regexp_ap.reset (new RegularExpression(name));
319
320     m_avoid_regexp_ap->Compile (name);
321 }
322
323 void
324 ThreadPlanStepInRange::SetDefaultFlagValue (uint32_t new_value)
325 {
326     // TODO: Should we test this for sanity?
327     ThreadPlanStepInRange::s_default_flag_values = new_value;
328 }
329
330 bool
331 ThreadPlanStepInRange::FrameMatchesAvoidCriteria ()
332 {
333     StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
334     
335     // Check the library list first, as that's cheapest:
336     bool libraries_say_avoid = false;
337
338     FileSpecList libraries_to_avoid (GetThread().GetLibrariesToAvoid());
339     size_t num_libraries = libraries_to_avoid.GetSize();
340     if (num_libraries > 0)
341     {
342         SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
343         FileSpec frame_library(sc.module_sp->GetFileSpec());
344         
345         if (frame_library)
346         {
347             for (size_t i = 0; i < num_libraries; i++)
348             {
349                 const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
350                 if (FileSpec::Equal (file_spec, frame_library, false))
351                 {
352                     libraries_say_avoid = true;
353                     break;
354                 }
355             }
356         }
357     }
358     if (libraries_say_avoid)
359         return true;
360     
361     const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_ap.get();
362     if (avoid_regexp_to_use == nullptr)
363         avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
364         
365     if (avoid_regexp_to_use != nullptr)
366     {
367         SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol);
368         if (sc.symbol != nullptr)
369         {
370             const char *frame_function_name = sc.GetFunctionName(Mangled::ePreferDemangledWithoutArguments).GetCString();
371             if (frame_function_name)
372             {
373                 size_t num_matches = 0;
374                 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
375                 if (log)
376                     num_matches = 1;
377                 
378                 RegularExpression::Match regex_match(num_matches);
379
380                 bool return_value = avoid_regexp_to_use->Execute(frame_function_name, &regex_match);
381                 if (return_value)
382                 {
383                     if (log)
384                     {
385                         std::string match;
386                         regex_match.GetMatchAtIndex(frame_function_name,0, match);
387                         log->Printf ("Stepping out of function \"%s\" because it matches the avoid regexp \"%s\" - match substring: \"%s\".",
388                                      frame_function_name,
389                                      avoid_regexp_to_use->GetText(),
390                                      match.c_str());
391                     }
392
393                 }
394                 return return_value;
395             }
396         }
397     }
398     return false;
399 }
400
401 bool
402 ThreadPlanStepInRange::DefaultShouldStopHereCallback (ThreadPlan *current_plan, Flags &flags, FrameComparison operation, void *baton)
403 {
404     bool should_stop_here = true;
405     StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
406     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
407
408     // First see if the ThreadPlanShouldStopHere default implementation thinks we should get out of here:
409     should_stop_here = ThreadPlanShouldStopHere::DefaultShouldStopHereCallback (current_plan, flags, operation, baton);
410     if (!should_stop_here)
411         return should_stop_here;
412     
413     if (should_stop_here && current_plan->GetKind() == eKindStepInRange && operation == eFrameCompareYounger)
414     {
415         ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
416         if (step_in_range_plan->m_step_into_target)
417         {
418             SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol);
419             if (sc.symbol != nullptr)
420             {
421                 // First try an exact match, since that's cheap with ConstStrings.  Then do a strstr compare.
422                 if (step_in_range_plan->m_step_into_target == sc.GetFunctionName())
423                 {
424                     should_stop_here = true;
425                 }
426                 else
427                 {
428                     const char *target_name = step_in_range_plan->m_step_into_target.AsCString();
429                     const char *function_name = sc.GetFunctionName().AsCString();
430                     
431                     if (function_name == nullptr)
432                         should_stop_here = false;
433                     else if (strstr(function_name, target_name) == nullptr)
434                         should_stop_here = false;
435                 }
436                 if (log && !should_stop_here)
437                     log->Printf("Stepping out of frame %s which did not match step into target %s.",
438                                 sc.GetFunctionName().AsCString(),
439                                 step_in_range_plan->m_step_into_target.AsCString());
440             }
441         }
442         
443         if (should_stop_here)
444         {
445             ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
446             // Don't log the should_step_out here, it's easier to do it in FrameMatchesAvoidCriteria.
447             should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria ();
448         }
449     }
450     
451     return should_stop_here;
452 }
453
454 bool
455 ThreadPlanStepInRange::DoPlanExplainsStop (Event *event_ptr)
456 {
457     // We always explain a stop.  Either we've just done a single step, in which
458     // case we'll do our ordinary processing, or we stopped for some
459     // reason that isn't handled by our sub-plans, in which case we want to just stop right
460     // away.
461     // In general, we don't want to mark the plan as complete for unexplained stops.
462     // For instance, if you step in to some code with no debug info, so you step out
463     // and in the course of that hit a breakpoint, then you want to stop & show the user
464     // the breakpoint, but not unship the step in plan, since you still may want to complete that
465     // plan when you continue.  This is particularly true when doing "step in to target function."
466     // stepping.
467     //
468     // The only variation is that if we are doing "step by running to next branch" in which case
469     // if we hit our branch breakpoint we don't set the plan to complete.
470             
471     bool return_value = false;
472     
473     if (m_virtual_step)
474     {
475         return_value = true;
476     }
477     else
478     {
479         StopInfoSP stop_info_sp = GetPrivateStopInfo ();
480         if (stop_info_sp)
481         {
482             StopReason reason = stop_info_sp->GetStopReason();
483             
484             if (reason ==  eStopReasonBreakpoint)
485             {
486                 if (NextRangeBreakpointExplainsStop(stop_info_sp))
487                 {
488                     return_value = true;
489                 }
490             }
491             else if (IsUsuallyUnexplainedStopReason(reason))
492             {
493                 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
494                 if (log)
495                     log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
496                 return_value = false;
497             }
498             else
499             {
500                 return_value = true;
501             }
502         }
503         else
504             return_value = true;
505     }
506     
507     return return_value;
508 }
509
510 bool
511 ThreadPlanStepInRange::DoWillResume (lldb::StateType resume_state, bool current_plan)
512 {
513     m_virtual_step = false;
514     if (resume_state == eStateStepping && current_plan)
515     {
516         // See if we are about to step over a virtual inlined call.
517         bool step_without_resume = m_thread.DecrementCurrentInlinedDepth();
518         if (step_without_resume)
519         {
520             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
521             if (log)
522                 log->Printf ("ThreadPlanStepInRange::DoWillResume: returning false, inline_depth: %d",
523                              m_thread.GetCurrentInlinedDepth());
524             SetStopInfo(StopInfo::CreateStopReasonToTrace(m_thread));
525             
526             // FIXME: Maybe it would be better to create a InlineStep stop reason, but then
527             // the whole rest of the world would have to handle that stop reason.
528             m_virtual_step = true;
529         }
530         return !step_without_resume;
531     }
532     return true;
533 }
534
535 bool
536 ThreadPlanStepInRange::IsVirtualStep()
537 {
538     return m_virtual_step;
539 }