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