blob: caaaffea8e8ad4ba822b45791ec27d07353a830c [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000014#include "lldb/Target/ThreadPlanStepInRange.h"
Jim Ingham4da62062014-01-23 21:52:47 +000015#include "lldb/Core/Module.h"
Jim Ingham7ce490c2010-09-16 00:58:09 +000016#include "lldb/Symbol/Function.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include "lldb/Symbol/Symbol.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Target/Process.h"
19#include "lldb/Target/RegisterContext.h"
Greg Clayton514487e2011-02-15 21:59:32 +000020#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Target/Thread.h"
22#include "lldb/Target/ThreadPlanStepOut.h"
23#include "lldb/Target/ThreadPlanStepThrough.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000024#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000025#include "lldb/Utility/RegularExpression.h"
26#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
30
Kate Stoneb9c1b512016-09-06 20:57:50 +000031uint32_t ThreadPlanStepInRange::s_default_flag_values =
32 ThreadPlanShouldStopHere::eStepInAvoidNoDebug;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
34//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000035// ThreadPlanStepInRange: Step through a stack range, either stepping over or
36// into
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037// based on the value of \a type.
38//----------------------------------------------------------------------
39
Kate Stoneb9c1b512016-09-06 20:57:50 +000040ThreadPlanStepInRange::ThreadPlanStepInRange(
41 Thread &thread, const AddressRange &range,
42 const SymbolContext &addr_context, lldb::RunMode stop_others,
Jim Ingham4b4b2472014-03-13 02:47:14 +000043 LazyBool step_in_avoids_code_without_debug_info,
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 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);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054}
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056ThreadPlanStepInRange::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);
Jim Inghamc6276822012-12-12 19:58:40 +000070}
71
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000072ThreadPlanStepInRange::~ThreadPlanStepInRange() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074void 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);
Jim Ingham4b4b2472014-03-13 02:47:14 +0000110}
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112void ThreadPlanStepInRange::GetDescription(Stream *s,
113 lldb::DescriptionLevel level) {
114 if (level == lldb::eDescriptionLevelBrief) {
115 s->Printf("step in");
116 return;
117 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 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 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 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());
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 if (!printed_line_info || level == eDescriptionLevelVerbose) {
132 s->Printf(" using ranges:");
133 DumpRanges(s);
134 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 s->PutChar('.');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137}
138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139bool ThreadPlanStepInRange::ShouldStop(Event *event_ptr) {
140 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 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 }
Jim Inghamf02a2e92012-09-07 01:11:44 +0000149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 if (IsPlanComplete())
Jim Ingham221d51c2013-05-08 00:35:16 +0000151 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152
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 log->Printf("ShouldStopHere says we should step out of this frame.");
196 } else if (log) {
197 log->Printf(
198 "Thought I stepped out, but in fact arrived at a trampoline.");
199 }
200 } else if (frame_order == eFrameCompareEqual && InSymbol()) {
201 // If we are not in a place we should step through, we're done.
202 // One tricky bit here is that some stubs don't push a frame, so we have
203 // to check
204 // both the case of a frame that is younger, or the same as this frame.
205 // However, if the frame is the same, and we are still in the symbol we
206 // started
207 // in, the we don't need to do this. This first check isn't strictly
208 // necessary,
209 // but it is more efficient.
210
211 // If we're still in the range, keep going, either by running to the next
212 // branch breakpoint, or by
213 // stepping.
214 if (InRange()) {
215 SetNextBranchBreakpoint();
216 return false;
217 }
218
219 SetPlanComplete();
220 m_no_more_plans = true;
221 return true;
222 }
223
224 // If we get to this point, we're not going to use a previously set "next
225 // branch" breakpoint, so delete it:
226 ClearNextBranchBreakpoint();
227
228 // We may have set the plan up above in the FrameIsOlder section:
229
230 if (!m_sub_plan_sp)
231 m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough(m_stack_id, false,
232 stop_others);
233
234 if (log) {
235 if (m_sub_plan_sp)
236 log->Printf("Found a step through plan: %s", m_sub_plan_sp->GetName());
237 else
238 log->Printf("No step through plan found.");
239 }
240
241 // If not, give the "should_stop" callback a chance to push a plan to get us
242 // out of here.
243 // But only do that if we actually have stepped in.
244 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
245 m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order);
246
247 // If we've stepped in and we are going to stop here, check to see if we
248 // were asked to
249 // run past the prologue, and if so do that.
250
251 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger &&
252 m_step_past_prologue) {
253 lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0);
254 if (curr_frame) {
255 size_t bytes_to_skip = 0;
256 lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC();
257 Address func_start_address;
258
259 SymbolContext sc = curr_frame->GetSymbolContext(eSymbolContextFunction |
260 eSymbolContextSymbol);
261
262 if (sc.function) {
263 func_start_address = sc.function->GetAddressRange().GetBaseAddress();
264 if (curr_addr ==
265 func_start_address.GetLoadAddress(
266 m_thread.CalculateTarget().get()))
267 bytes_to_skip = sc.function->GetPrologueByteSize();
268 } else if (sc.symbol) {
269 func_start_address = sc.symbol->GetAddress();
270 if (curr_addr ==
271 func_start_address.GetLoadAddress(
272 m_thread.CalculateTarget().get()))
273 bytes_to_skip = sc.symbol->GetPrologueByteSize();
274 }
275
276 if (bytes_to_skip != 0) {
277 func_start_address.Slide(bytes_to_skip);
278 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP);
279 if (log)
280 log->Printf("Pushing past prologue ");
281
282 m_sub_plan_sp = m_thread.QueueThreadPlanForRunToAddress(
283 false, func_start_address, true);
284 }
285 }
286 }
287 }
288
289 if (!m_sub_plan_sp) {
290 m_no_more_plans = true;
291 SetPlanComplete();
292 return true;
293 } else {
294 m_no_more_plans = false;
295 m_sub_plan_sp->SetPrivate(true);
296 return false;
297 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000298}
Daniel Malea246cb612013-05-14 15:20:12 +0000299
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300void ThreadPlanStepInRange::SetAvoidRegexp(const char *name) {
Zachary Turner95eae422016-09-21 16:01:28 +0000301 auto name_ref = llvm::StringRef::withNullAsEmpty(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 if (!m_avoid_regexp_ap)
Zachary Turner95eae422016-09-21 16:01:28 +0000303 m_avoid_regexp_ap.reset(new RegularExpression(name_ref));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304
Zachary Turner95eae422016-09-21 16:01:28 +0000305 m_avoid_regexp_ap->Compile(name_ref);
Daniel Malea246cb612013-05-14 15:20:12 +0000306}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307
308void ThreadPlanStepInRange::SetDefaultFlagValue(uint32_t new_value) {
309 // TODO: Should we test this for sanity?
310 ThreadPlanStepInRange::s_default_flag_values = new_value;
311}
312
313bool ThreadPlanStepInRange::FrameMatchesAvoidCriteria() {
314 StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
315
316 // Check the library list first, as that's cheapest:
317 bool libraries_say_avoid = false;
318
319 FileSpecList libraries_to_avoid(GetThread().GetLibrariesToAvoid());
320 size_t num_libraries = libraries_to_avoid.GetSize();
321 if (num_libraries > 0) {
322 SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
323 FileSpec frame_library(sc.module_sp->GetFileSpec());
324
325 if (frame_library) {
326 for (size_t i = 0; i < num_libraries; i++) {
327 const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
328 if (FileSpec::Equal(file_spec, frame_library, false)) {
329 libraries_say_avoid = true;
330 break;
331 }
332 }
333 }
334 }
335 if (libraries_say_avoid)
336 return true;
337
338 const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_ap.get();
339 if (avoid_regexp_to_use == nullptr)
340 avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
341
342 if (avoid_regexp_to_use != nullptr) {
343 SymbolContext sc = frame->GetSymbolContext(
344 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
345 if (sc.symbol != nullptr) {
346 const char *frame_function_name =
347 sc.GetFunctionName(Mangled::ePreferDemangledWithoutArguments)
348 .GetCString();
349 if (frame_function_name) {
350 size_t num_matches = 0;
351 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
352 if (log)
353 num_matches = 1;
354
355 RegularExpression::Match regex_match(num_matches);
356
357 bool return_value =
358 avoid_regexp_to_use->Execute(frame_function_name, &regex_match);
359 if (return_value) {
360 if (log) {
361 std::string match;
362 regex_match.GetMatchAtIndex(frame_function_name, 0, match);
363 log->Printf("Stepping out of function \"%s\" because it matches "
364 "the avoid regexp \"%s\" - match substring: \"%s\".",
Zachary Turner95eae422016-09-21 16:01:28 +0000365 frame_function_name,
366 avoid_regexp_to_use->GetText().str().c_str(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 match.c_str());
368 }
369 }
370 return return_value;
371 }
372 }
373 }
374 return false;
375}
376
377bool ThreadPlanStepInRange::DefaultShouldStopHereCallback(
378 ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
379 void *baton) {
380 bool should_stop_here = true;
381 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
382 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
383
384 // First see if the ThreadPlanShouldStopHere default implementation thinks we
385 // should get out of here:
386 should_stop_here = ThreadPlanShouldStopHere::DefaultShouldStopHereCallback(
387 current_plan, flags, operation, baton);
388 if (!should_stop_here)
389 return should_stop_here;
390
391 if (should_stop_here && current_plan->GetKind() == eKindStepInRange &&
392 operation == eFrameCompareYounger) {
393 ThreadPlanStepInRange *step_in_range_plan =
394 static_cast<ThreadPlanStepInRange *>(current_plan);
395 if (step_in_range_plan->m_step_into_target) {
396 SymbolContext sc = frame->GetSymbolContext(
397 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
398 if (sc.symbol != nullptr) {
399 // First try an exact match, since that's cheap with ConstStrings. Then
400 // do a strstr compare.
401 if (step_in_range_plan->m_step_into_target == sc.GetFunctionName()) {
402 should_stop_here = true;
403 } else {
404 const char *target_name =
405 step_in_range_plan->m_step_into_target.AsCString();
406 const char *function_name = sc.GetFunctionName().AsCString();
407
408 if (function_name == nullptr)
409 should_stop_here = false;
410 else if (strstr(function_name, target_name) == nullptr)
411 should_stop_here = false;
412 }
413 if (log && !should_stop_here)
414 log->Printf("Stepping out of frame %s which did not match step into "
415 "target %s.",
416 sc.GetFunctionName().AsCString(),
417 step_in_range_plan->m_step_into_target.AsCString());
418 }
419 }
420
421 if (should_stop_here) {
422 ThreadPlanStepInRange *step_in_range_plan =
423 static_cast<ThreadPlanStepInRange *>(current_plan);
424 // Don't log the should_step_out here, it's easier to do it in
425 // FrameMatchesAvoidCriteria.
426 should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria();
427 }
428 }
429
430 return should_stop_here;
431}
432
433bool ThreadPlanStepInRange::DoPlanExplainsStop(Event *event_ptr) {
434 // We always explain a stop. Either we've just done a single step, in which
435 // case we'll do our ordinary processing, or we stopped for some
436 // reason that isn't handled by our sub-plans, in which case we want to just
437 // stop right
438 // away.
439 // In general, we don't want to mark the plan as complete for unexplained
440 // stops.
441 // For instance, if you step in to some code with no debug info, so you step
442 // out
443 // and in the course of that hit a breakpoint, then you want to stop & show
444 // the user
445 // the breakpoint, but not unship the step in plan, since you still may want
446 // to complete that
447 // plan when you continue. This is particularly true when doing "step in to
448 // target function."
449 // stepping.
450 //
451 // The only variation is that if we are doing "step by running to next branch"
452 // in which case
453 // if we hit our branch breakpoint we don't set the plan to complete.
454
455 bool return_value = false;
456
457 if (m_virtual_step) {
458 return_value = true;
459 } else {
460 StopInfoSP stop_info_sp = GetPrivateStopInfo();
461 if (stop_info_sp) {
462 StopReason reason = stop_info_sp->GetStopReason();
463
464 if (reason == eStopReasonBreakpoint) {
465 if (NextRangeBreakpointExplainsStop(stop_info_sp)) {
466 return_value = true;
467 }
468 } else if (IsUsuallyUnexplainedStopReason(reason)) {
469 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
470 if (log)
471 log->PutCString("ThreadPlanStepInRange got asked if it explains the "
472 "stop for some reason other than step.");
473 return_value = false;
474 } else {
475 return_value = true;
476 }
477 } else
478 return_value = true;
479 }
480
481 return return_value;
482}
483
484bool ThreadPlanStepInRange::DoWillResume(lldb::StateType resume_state,
485 bool current_plan) {
486 m_virtual_step = false;
487 if (resume_state == eStateStepping && current_plan) {
488 // See if we are about to step over a virtual inlined call.
489 bool step_without_resume = m_thread.DecrementCurrentInlinedDepth();
490 if (step_without_resume) {
491 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
492 if (log)
493 log->Printf("ThreadPlanStepInRange::DoWillResume: returning false, "
494 "inline_depth: %d",
495 m_thread.GetCurrentInlinedDepth());
496 SetStopInfo(StopInfo::CreateStopReasonToTrace(m_thread));
497
498 // FIXME: Maybe it would be better to create a InlineStep stop reason, but
499 // then
500 // the whole rest of the world would have to handle that stop reason.
501 m_virtual_step = true;
502 }
503 return !step_without_resume;
504 }
505 return true;
506}
507
508bool ThreadPlanStepInRange::IsVirtualStep() { return m_virtual_step; }