Coverage Report

Created: 2021-09-21 08:58

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