blob: e6da7a037c0d1cbee15be4492020ad7b68200545 [file] [log] [blame]
Chris Lattner24943d22010-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
10#include "lldb/Target/ThreadPlanStepInRange.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"
Jim Ingham809ab9b2010-07-10 02:27:39 +000020#include "lldb/Symbol/Symbol.h"
Jim Ingham0e81b642010-09-16 00:58:09 +000021#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Target/Process.h"
23#include "lldb/Target/RegisterContext.h"
Greg Clayton395fc332011-02-15 21:59:32 +000024#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Target/Thread.h"
26#include "lldb/Target/ThreadPlanStepOut.h"
27#include "lldb/Target/ThreadPlanStepThrough.h"
Jim Ingham809ab9b2010-07-10 02:27:39 +000028#include "lldb/Core/RegularExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
33uint32_t ThreadPlanStepInRange::s_default_flag_values = ThreadPlanShouldStopHere::eAvoidNoDebug;
34
35//----------------------------------------------------------------------
36// ThreadPlanStepInRange: Step through a stack range, either stepping over or into
37// based on the value of \a type.
38//----------------------------------------------------------------------
39
40ThreadPlanStepInRange::ThreadPlanStepInRange
41(
42 Thread &thread,
43 const AddressRange &range,
44 const SymbolContext &addr_context,
45 lldb::RunMode stop_others
46) :
Jim Ingham5a47e8b2010-06-19 04:45:32 +000047 ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
Jim Ingham0e81b642010-09-16 00:58:09 +000048 ThreadPlanShouldStopHere (this, ThreadPlanStepInRange::DefaultShouldStopHereCallback, NULL),
Jim Ingham45bc4a22012-09-07 01:11:44 +000049 m_step_past_prologue (true),
50 m_virtual_step (false)
Chris Lattner24943d22010-06-08 16:52:24 +000051{
52 SetFlagsToDefault ();
53}
54
Jim Inghamf2ca5732012-12-12 19:58:40 +000055ThreadPlanStepInRange::ThreadPlanStepInRange
56(
57 Thread &thread,
58 const AddressRange &range,
59 const SymbolContext &addr_context,
60 const char *step_into_target,
61 lldb::RunMode stop_others
62) :
63 ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
64 ThreadPlanShouldStopHere (this, ThreadPlanStepInRange::DefaultShouldStopHereCallback, NULL),
65 m_step_past_prologue (true),
66 m_virtual_step (false),
67 m_step_into_target (step_into_target)
68{
69 SetFlagsToDefault ();
70}
71
Chris Lattner24943d22010-06-08 16:52:24 +000072ThreadPlanStepInRange::~ThreadPlanStepInRange ()
73{
74}
75
76void
77ThreadPlanStepInRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
78{
79 if (level == lldb::eDescriptionLevelBrief)
80 s->Printf("step in");
81 else
82 {
83 s->Printf ("Stepping through range (stepping into functions): ");
Jim Ingham76e55f72011-10-15 00:24:48 +000084 DumpRanges(s);
Jim Inghamf2ca5732012-12-12 19:58:40 +000085 s->Printf ("targeting %s.", m_step_into_target.AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +000086 }
87}
88
89bool
90ThreadPlanStepInRange::ShouldStop (Event *event_ptr)
91{
Greg Clayton952e9dc2013-03-27 23:08:40 +000092 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +000093 m_no_more_plans = false;
94
95 if (log)
96 {
97 StreamString s;
Greg Clayton395fc332011-02-15 21:59:32 +000098 s.Address (m_thread.GetRegisterContext()->GetPC(),
Greg Claytonf4124de2012-02-21 00:09:25 +000099 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +0000100 log->Printf("ThreadPlanStepInRange reached %s.", s.GetData());
101 }
102
Jim Inghamad382c52011-12-03 01:52:59 +0000103 if (IsPlanComplete())
104 return true;
105
Chris Lattner24943d22010-06-08 16:52:24 +0000106 ThreadPlan* new_plan = NULL;
107
Jim Ingham45bc4a22012-09-07 01:11:44 +0000108 if (m_virtual_step)
Jim Ingham4f385f12010-11-05 00:18:21 +0000109 {
Jim Ingham45bc4a22012-09-07 01:11:44 +0000110 // If we've just completed a virtual step, all we need to do is check for a ShouldStopHere plan, and otherwise
111 // we're done.
Chris Lattner24943d22010-06-08 16:52:24 +0000112 new_plan = InvokeShouldStopHereCallback();
Jim Ingham45bc4a22012-09-07 01:11:44 +0000113 }
114 else
Chris Lattner24943d22010-06-08 16:52:24 +0000115 {
Jim Ingham23f21942012-10-25 22:30:09 +0000116 // Stepping through should be done running other threads in general, since we're setting a breakpoint and
117 // continuing. So only stop others if we are explicitly told to do so.
Jim Ingham45bc4a22012-09-07 01:11:44 +0000118
119 bool stop_others;
Jim Ingham23f21942012-10-25 22:30:09 +0000120 if (m_stop_others == lldb::eOnlyThisThread)
Jim Ingham45bc4a22012-09-07 01:11:44 +0000121 stop_others = false;
Jim Ingham23f21942012-10-25 22:30:09 +0000122 else
123 stop_others = true;
Jim Ingham45bc4a22012-09-07 01:11:44 +0000124
125 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
126
127 if (frame_order == eFrameCompareOlder)
Jim Ingham0e81b642010-09-16 00:58:09 +0000128 {
Jim Ingham45bc4a22012-09-07 01:11:44 +0000129 // If we're in an older frame then we should stop.
130 //
131 // A caveat to this is if we think the frame is older but we're actually in a trampoline.
132 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are
133 // in a trampoline we think the frame is older because the trampoline confused the backtracer.
134 new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
135 if (new_plan == NULL)
136 return true;
137 else if (log)
Jim Ingham0e81b642010-09-16 00:58:09 +0000138 {
Jim Ingham45bc4a22012-09-07 01:11:44 +0000139 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
Jim Ingham0e81b642010-09-16 00:58:09 +0000140 }
Jim Ingham45bc4a22012-09-07 01:11:44 +0000141
142 }
143 else if (frame_order == eFrameCompareEqual && InSymbol())
144 {
145 // If we are not in a place we should step through, we're done.
146 // One tricky bit here is that some stubs don't push a frame, so we have to check
147 // both the case of a frame that is younger, or the same as this frame.
148 // However, if the frame is the same, and we are still in the symbol we started
149 // in, the we don't need to do this. This first check isn't strictly necessary,
150 // but it is more efficient.
Jim Ingham0e81b642010-09-16 00:58:09 +0000151
Jim Ingham45bc4a22012-09-07 01:11:44 +0000152 // If we're still in the range, keep going, either by running to the next branch breakpoint, or by
153 // stepping.
154 if (InRange())
Jim Ingham0e81b642010-09-16 00:58:09 +0000155 {
Jim Ingham45bc4a22012-09-07 01:11:44 +0000156 SetNextBranchBreakpoint();
157 return false;
158 }
159
160 SetPlanComplete();
Jim Inghamf2ca5732012-12-12 19:58:40 +0000161 m_no_more_plans = true;
Jim Ingham45bc4a22012-09-07 01:11:44 +0000162 return true;
163 }
164
165 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
166 ClearNextBranchBreakpoint();
167
168 // We may have set the plan up above in the FrameIsOlder section:
169
170 if (new_plan == NULL)
171 new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
172
173 if (log)
174 {
175 if (new_plan != NULL)
176 log->Printf ("Found a step through plan: %s", new_plan->GetName());
177 else
178 log->Printf ("No step through plan found.");
179 }
180
181 // If not, give the "should_stop" callback a chance to push a plan to get us out of here.
182 // But only do that if we actually have stepped in.
183 if (!new_plan && frame_order == eFrameCompareYounger)
184 new_plan = InvokeShouldStopHereCallback();
185
186 // If we've stepped in and we are going to stop here, check to see if we were asked to
187 // run past the prologue, and if so do that.
188
189 if (new_plan == NULL && frame_order == eFrameCompareYounger && m_step_past_prologue)
190 {
191 lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0);
192 if (curr_frame)
193 {
194 size_t bytes_to_skip = 0;
195 lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC();
196 Address func_start_address;
197
198 SymbolContext sc = curr_frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextSymbol);
199
200 if (sc.function)
201 {
202 func_start_address = sc.function->GetAddressRange().GetBaseAddress();
203 if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
204 bytes_to_skip = sc.function->GetPrologueByteSize();
205 }
206 else if (sc.symbol)
207 {
208 func_start_address = sc.symbol->GetAddress();
209 if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
210 bytes_to_skip = sc.symbol->GetPrologueByteSize();
211 }
212
213 if (bytes_to_skip != 0)
214 {
215 func_start_address.Slide (bytes_to_skip);
216 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
217 if (log)
218 log->Printf ("Pushing past prologue ");
219
220 new_plan = m_thread.QueueThreadPlanForRunToAddress(false, func_start_address,true);
221 }
Jim Ingham0e81b642010-09-16 00:58:09 +0000222 }
223 }
Jim Ingham45bc4a22012-09-07 01:11:44 +0000224 }
Jim Ingham0e81b642010-09-16 00:58:09 +0000225
226 if (new_plan == NULL)
227 {
Chris Lattner24943d22010-06-08 16:52:24 +0000228 m_no_more_plans = true;
229 SetPlanComplete();
230 return true;
231 }
232 else
233 {
234 m_no_more_plans = false;
235 return false;
236 }
237}
238
239void
240ThreadPlanStepInRange::SetFlagsToDefault ()
241{
242 GetFlags().Set(ThreadPlanStepInRange::s_default_flag_values);
243}
244
Jim Ingham809ab9b2010-07-10 02:27:39 +0000245void
246ThreadPlanStepInRange::SetAvoidRegexp(const char *name)
247{
248 if (m_avoid_regexp_ap.get() == NULL)
249 m_avoid_regexp_ap.reset (new RegularExpression(name));
250
251 m_avoid_regexp_ap->Compile (name);
252}
253
Chris Lattner24943d22010-06-08 16:52:24 +0000254void
255ThreadPlanStepInRange::SetDefaultFlagValue (uint32_t new_value)
256{
257 // TODO: Should we test this for sanity?
258 ThreadPlanStepInRange::s_default_flag_values = new_value;
259}
260
Jim Ingham809ab9b2010-07-10 02:27:39 +0000261bool
262ThreadPlanStepInRange::FrameMatchesAvoidRegexp ()
263{
264 StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
265
Greg Clayton73844aa2012-08-22 17:17:09 +0000266 const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_ap.get();
Jim Ingham20594b12010-09-08 03:14:33 +0000267 if (avoid_regexp_to_use == NULL)
268 avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
269
270 if (avoid_regexp_to_use != NULL)
Jim Ingham809ab9b2010-07-10 02:27:39 +0000271 {
Greg Clayton002b3cf2012-07-13 00:19:40 +0000272 SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol);
Jim Ingham809ab9b2010-07-10 02:27:39 +0000273 if (sc.symbol != NULL)
274 {
Greg Clayton002b3cf2012-07-13 00:19:40 +0000275 const char *frame_function_name = sc.GetFunctionName().GetCString();
276 if (frame_function_name)
Jim Ingham5c38c312013-03-14 21:44:36 +0000277 {
Jim Ingham34a96b82013-03-14 22:00:18 +0000278 size_t num_matches = 0;
Greg Clayton952e9dc2013-03-27 23:08:40 +0000279 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham34a96b82013-03-14 22:00:18 +0000280 if (log)
281 num_matches = 1;
Greg Clayton00af72e2013-04-03 21:37:16 +0000282
283 RegularExpression::Match regex_match(num_matches);
284
285 bool return_value = avoid_regexp_to_use->Execute(frame_function_name, &regex_match);
Jim Ingham5c38c312013-03-14 21:44:36 +0000286 if (return_value)
287 {
Jim Ingham5c38c312013-03-14 21:44:36 +0000288 if (log)
Jim Ingham34a96b82013-03-14 22:00:18 +0000289 {
290 std::string match;
Greg Clayton00af72e2013-04-03 21:37:16 +0000291 regex_match.GetMatchAtIndex(frame_function_name,0, match);
Jim Ingham34a96b82013-03-14 22:00:18 +0000292 log->Printf ("Stepping out of function \"%s\" because it matches the avoid regexp \"%s\" - match substring: \"%s\".",
Jim Ingham5c38c312013-03-14 21:44:36 +0000293 frame_function_name,
Jim Ingham34a96b82013-03-14 22:00:18 +0000294 avoid_regexp_to_use->GetText(),
295 match.c_str());
296 }
Jim Ingham5c38c312013-03-14 21:44:36 +0000297
298 }
299 return return_value;
300 }
Jim Ingham809ab9b2010-07-10 02:27:39 +0000301 }
302 }
303 return false;
304}
305
Chris Lattner24943d22010-06-08 16:52:24 +0000306ThreadPlan *
307ThreadPlanStepInRange::DefaultShouldStopHereCallback (ThreadPlan *current_plan, Flags &flags, void *baton)
308{
Jim Ingham809ab9b2010-07-10 02:27:39 +0000309 bool should_step_out = false;
310 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
Greg Clayton952e9dc2013-03-27 23:08:40 +0000311 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham809ab9b2010-07-10 02:27:39 +0000312
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000313 if (flags.Test(eAvoidNoDebug))
Chris Lattner24943d22010-06-08 16:52:24 +0000314 {
Chris Lattner24943d22010-06-08 16:52:24 +0000315 if (!frame->HasDebugInformation())
Jim Inghame4b8aeb2010-09-15 00:06:51 +0000316 {
317 if (log)
318 log->Printf ("Stepping out of frame with no debug info");
319
Jim Ingham809ab9b2010-07-10 02:27:39 +0000320 should_step_out = true;
Jim Inghame4b8aeb2010-09-15 00:06:51 +0000321 }
Jim Ingham809ab9b2010-07-10 02:27:39 +0000322 }
323
Jim Inghamf2ca5732012-12-12 19:58:40 +0000324 if (current_plan->GetKind() == eKindStepInRange)
Jim Ingham809ab9b2010-07-10 02:27:39 +0000325 {
Jim Inghamf2ca5732012-12-12 19:58:40 +0000326 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
327 if (step_in_range_plan->m_step_into_target)
Chris Lattner24943d22010-06-08 16:52:24 +0000328 {
Jim Inghamf2ca5732012-12-12 19:58:40 +0000329 SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol);
330 if (sc.symbol != NULL)
331 {
332 // First try an exact match, since that's cheap with ConstStrings. Then do a strstr compare.
333 if (step_in_range_plan->m_step_into_target == sc.GetFunctionName())
334 {
335 should_step_out = false;
336 }
337 else
338 {
339 const char *target_name = step_in_range_plan->m_step_into_target.AsCString();
340 const char *function_name = sc.GetFunctionName().AsCString();
341
342 if (function_name == NULL)
343 should_step_out = true;
344 else if (strstr (function_name, target_name) == NULL)
345 should_step_out = true;
346 }
Jim Ingham5c38c312013-03-14 21:44:36 +0000347 if (log && should_step_out)
348 log->Printf("Stepping out of frame %s which did not match step into target %s.",
349 sc.GetFunctionName().AsCString(),
350 step_in_range_plan->m_step_into_target.AsCString());
Jim Inghamf2ca5732012-12-12 19:58:40 +0000351 }
352 }
353
354 if (!should_step_out)
355 {
Jim Ingham5c38c312013-03-14 21:44:36 +0000356 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
357 // Don't log the should_step_out here, it's easier to do it in FrameMatchesAvoidRegexp.
358 should_step_out = step_in_range_plan->FrameMatchesAvoidRegexp ();
Chris Lattner24943d22010-06-08 16:52:24 +0000359 }
360 }
Jim Ingham809ab9b2010-07-10 02:27:39 +0000361
Jim Inghamf2ca5732012-12-12 19:58:40 +0000362
Jim Ingham809ab9b2010-07-10 02:27:39 +0000363 if (should_step_out)
364 {
365 // FIXME: Make sure the ThreadPlanForStepOut does the right thing with inlined functions.
Jim Ingham23f21942012-10-25 22:30:09 +0000366 // We really should have all plans take the tri-state for "stop others" so we can do the right
367 // thing. For now let's be safe and always run others when we are likely to run arbitrary code.
368 const bool stop_others = false;
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000369 return current_plan->GetThread().QueueThreadPlanForStepOut (false,
370 NULL,
371 true,
Jim Ingham23f21942012-10-25 22:30:09 +0000372 stop_others,
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000373 eVoteNo,
374 eVoteNoOpinion,
375 0); // Frame index
Jim Ingham809ab9b2010-07-10 02:27:39 +0000376 }
Chris Lattner24943d22010-06-08 16:52:24 +0000377
378 return NULL;
379}
Jim Ingham707b7a82012-05-01 18:38:37 +0000380
381bool
Jim Ingham89e248f2013-02-09 01:29:05 +0000382ThreadPlanStepInRange::PlanExplainsStop (Event *event_ptr)
Jim Ingham707b7a82012-05-01 18:38:37 +0000383{
384 // We always explain a stop. Either we've just done a single step, in which
385 // case we'll do our ordinary processing, or we stopped for some
386 // reason that isn't handled by our sub-plans, in which case we want to just stop right
387 // away.
Jim Inghamf2ca5732012-12-12 19:58:40 +0000388 // In general, we don't want to mark the plan as complete for unexplained stops.
389 // For instance, if you step in to some code with no debug info, so you step out
390 // and in the course of that hit a breakpoint, then you want to stop & show the user
391 // the breakpoint, but not unship the step in plan, since you still may want to complete that
392 // plan when you continue. This is particularly true when doing "step in to target function."
393 // stepping.
Jim Ingham707b7a82012-05-01 18:38:37 +0000394 //
395 // The only variation is that if we are doing "step by running to next branch" in which case
396 // if we hit our branch breakpoint we don't set the plan to complete.
397
Jim Ingham45bc4a22012-09-07 01:11:44 +0000398 if (m_virtual_step)
399 return true;
400
Jim Ingham707b7a82012-05-01 18:38:37 +0000401 StopInfoSP stop_info_sp = GetPrivateStopReason();
402 if (stop_info_sp)
403 {
404 StopReason reason = stop_info_sp->GetStopReason();
405
406 switch (reason)
407 {
408 case eStopReasonBreakpoint:
409 if (NextRangeBreakpointExplainsStop(stop_info_sp))
410 return true;
411 case eStopReasonWatchpoint:
412 case eStopReasonSignal:
413 case eStopReasonException:
Greg Clayton0bce9a22012-12-05 00:16:59 +0000414 case eStopReasonExec:
Andrew Kaylor278f16e2012-12-20 23:08:03 +0000415 case eStopReasonThreadExiting:
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000416 {
Greg Clayton952e9dc2013-03-27 23:08:40 +0000417 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000418 if (log)
419 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000420 }
Jim Inghamf2ca5732012-12-12 19:58:40 +0000421 return false;
Jim Ingham707b7a82012-05-01 18:38:37 +0000422 break;
423 default:
424 break;
425 }
426 }
427 return true;
428}
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000429
430bool
431ThreadPlanStepInRange::WillResume (lldb::StateType resume_state, bool current_plan)
432{
433 if (resume_state == eStateStepping && current_plan)
434 {
435 // See if we are about to step over a virtual inlined call.
436 bool step_without_resume = m_thread.DecrementCurrentInlinedDepth();
437 if (step_without_resume)
438 {
Greg Clayton952e9dc2013-03-27 23:08:40 +0000439 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000440 if (log)
441 log->Printf ("ThreadPlanStepInRange::WillResume: returning false, inline_depth: %d",
442 m_thread.GetCurrentInlinedDepth());
443 SetStopInfo(StopInfo::CreateStopReasonToTrace(m_thread));
Jim Ingham45bc4a22012-09-07 01:11:44 +0000444
445 // FIXME: Maybe it would be better to create a InlineStep stop reason, but then
446 // the whole rest of the world would have to handle that stop reason.
447 m_virtual_step = true;
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000448 }
449 return !step_without_resume;
450 }
451 else
452 return ThreadPlan::WillResume(resume_state, current_plan);
453
454}