blob: 955b32fcbbc91b70842e5cdf492bbb38cd78d2c6 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ThreadPlanStepOverRange.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/ThreadPlanStepOverRange.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Log.h"
17#include "lldb/Core/Stream.h"
Jim Ingham513c6bb2012-09-01 01:02:41 +000018#include "lldb/Symbol/Block.h"
Jim Ingham3bfa7532012-11-06 01:14:52 +000019#include "lldb/Symbol/CompileUnit.h"
Jim Ingham513c6bb2012-09-01 01:02:41 +000020#include "lldb/Symbol/Function.h"
Jim Ingham3bfa7532012-11-06 01:14:52 +000021#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Target/Process.h"
23#include "lldb/Target/RegisterContext.h"
Greg Clayton514487e2011-02-15 21:59:32 +000024#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Target/Thread.h"
26#include "lldb/Target/ThreadPlanStepOut.h"
27#include "lldb/Target/ThreadPlanStepThrough.h"
28
29using namespace lldb_private;
Greg Clayton2d4edfb2010-11-06 01:53:30 +000030using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Jim Ingham4b4b2472014-03-13 02:47:14 +000032uint32_t ThreadPlanStepOverRange::s_default_flag_values = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
34//----------------------------------------------------------------------
35// ThreadPlanStepOverRange: Step through a stack range, either stepping over or into
36// based on the value of \a type.
37//----------------------------------------------------------------------
38
39ThreadPlanStepOverRange::ThreadPlanStepOverRange
40(
41 Thread &thread,
42 const AddressRange &range,
43 const SymbolContext &addr_context,
Jim Ingham4b4b2472014-03-13 02:47:14 +000044 lldb::RunMode stop_others,
45 LazyBool step_out_avoids_code_without_debug_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046) :
Jim Ingham513c6bb2012-09-01 01:02:41 +000047 ThreadPlanStepRange (ThreadPlan::eKindStepOverRange, "Step range stepping over", thread, range, addr_context, stop_others),
Jim Ingham4b4b2472014-03-13 02:47:14 +000048 ThreadPlanShouldStopHere (this),
Jim Ingham513c6bb2012-09-01 01:02:41 +000049 m_first_resume(true)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050{
Jim Ingham4b4b2472014-03-13 02:47:14 +000051 SetFlagsToDefault();
52 SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053}
54
55ThreadPlanStepOverRange::~ThreadPlanStepOverRange ()
56{
57}
58
59void
60ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
61{
62 if (level == lldb::eDescriptionLevelBrief)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +000064 s->Printf("step over");
65 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +000067 s->Printf ("Stepping over");
68 bool printed_line_info = false;
69 if (m_addr_context.line_entry.IsValid())
70 {
71 s->Printf (" line ");
72 m_addr_context.line_entry.DumpStopContext (s, false);
73 printed_line_info = true;
74 }
75
76 if (!printed_line_info || level == eDescriptionLevelVerbose)
77 {
78 s->Printf (" using ranges: ");
79 DumpRanges(s);
80 }
81
82 s->PutChar('.');
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083}
84
Jim Ingham4b4b2472014-03-13 02:47:14 +000085void
86ThreadPlanStepOverRange::SetupAvoidNoDebug(LazyBool step_out_avoids_code_without_debug_info)
87{
88 bool avoid_nodebug = true;
89 switch (step_out_avoids_code_without_debug_info)
90 {
91 case eLazyBoolYes:
92 avoid_nodebug = true;
93 break;
94 case eLazyBoolNo:
95 avoid_nodebug = false;
96 break;
97 case eLazyBoolCalculate:
98 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
99 break;
100 }
101 if (avoid_nodebug)
102 GetFlags().Set (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
103 else
104 GetFlags().Clear (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
Jim Ingham862d1bb2014-08-06 01:49:59 +0000105 // Step Over plans should always avoid no-debug on step in. Seems like you shouldn't
106 // have to say this, but a tail call looks more like a step in that a step out, so
107 // we want to catch this case.
108 GetFlags().Set (ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
Jim Ingham4b4b2472014-03-13 02:47:14 +0000109}
110
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111bool
Daniel Malea4b867282013-09-17 16:35:45 +0000112ThreadPlanStepOverRange::IsEquivalentContext(const SymbolContext &context)
113{
114
115 // Match as much as is specified in the m_addr_context:
116 // This is a fairly loose sanity check. Note, sometimes the target doesn't get filled
117 // in so I left out the target check. And sometimes the module comes in as the .o file from the
118 // inlined range, so I left that out too...
119 if (m_addr_context.comp_unit)
120 {
121 if (m_addr_context.comp_unit == context.comp_unit)
122 {
123 if (m_addr_context.function && m_addr_context.function == context.function)
124 {
125 if (m_addr_context.block && m_addr_context.block == context.block)
126 return true;
127 }
128 }
129 }
130 else if (m_addr_context.symbol && m_addr_context.symbol == context.symbol)
131 {
132 return true;
133 }
134 return false;
135}
136
137bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138ThreadPlanStepOverRange::ShouldStop (Event *event_ptr)
139{
Greg Clayton5160ce52013-03-27 23:08:40 +0000140 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141
142 if (log)
143 {
144 StreamString s;
Greg Clayton514487e2011-02-15 21:59:32 +0000145 s.Address (m_thread.GetRegisterContext()->GetPC(),
Greg Clayton1ac04c32012-02-21 00:09:25 +0000146 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147 log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
148 }
149
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150 // If we're out of the range but in the same frame or in our caller's frame
151 // then we should stop.
Jim Ingham4a58e962012-10-25 22:30:09 +0000152 // When stepping out we only stop others if we are forcing running one thread.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 bool stop_others;
154 if (m_stop_others == lldb::eOnlyThisThread)
155 stop_others = true;
156 else
157 stop_others = false;
158
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000159 ThreadPlanSP new_plan_sp;
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000160
161 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
162
163 if (frame_order == eFrameCompareOlder)
Jim Ingham58221732010-11-05 00:18:21 +0000164 {
165 // If we're in an older frame then we should stop.
166 //
167 // A caveat to this is if we think the frame is older but we're actually in a trampoline.
168 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are
169 // in a trampoline we think the frame is older because the trampoline confused the backtracer.
170 // As below, we step through first, and then try to figure out how to get back out again.
171
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000172 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham58221732010-11-05 00:18:21 +0000173
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000174 if (new_plan_sp && log)
Jim Ingham58221732010-11-05 00:18:21 +0000175 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
176 }
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000177 else if (frame_order == eFrameCompareYounger)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 {
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000179 // Make sure we really are in a new frame. Do that by unwinding and seeing if the
180 // start function really is our start function...
Daniel Malea4b867282013-09-17 16:35:45 +0000181 for(uint32_t i = 1;; ++i)
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000182 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000183 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(i);
Daniel Malea4b867282013-09-17 16:35:45 +0000184 if (!older_frame_sp) {
185 // We can't unwind the next frame we should just get out of here & stop...
186 break;
187 }
188
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000189 const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
Daniel Malea4b867282013-09-17 16:35:45 +0000190 if (IsEquivalentContext(older_context))
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000191 {
Jim Ingham4b4b2472014-03-13 02:47:14 +0000192 new_plan_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop (false,
193 NULL,
194 true,
195 stop_others,
196 eVoteNo,
197 eVoteNoOpinion,
198 0);
Daniel Malea4b867282013-09-17 16:35:45 +0000199 break;
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000200 }
Daniel Malea4b867282013-09-17 16:35:45 +0000201 else
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000202 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000203 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham5beccb22014-08-05 01:59:20 +0000204 // If we found a way through, then we should stop recursing.
205 if (new_plan_sp)
206 break;
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000207 }
208 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209 }
Jim Ingham564d8bc22012-03-09 04:10:47 +0000210 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 {
Jim Ingham564d8bc22012-03-09 04:10:47 +0000212 // If we're still in the range, keep going.
213 if (InRange())
214 {
215 SetNextBranchBreakpoint();
216 return false;
217 }
218
219
220 if (!InSymbol())
221 {
222 // This one is a little tricky. Sometimes we may be in a stub or something similar,
223 // in which case we need to get out of there. But if we are in a stub then it's
224 // likely going to be hard to get out from here. It is probably easiest to step into the
225 // stub, and then it will be straight-forward to step out.
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000226 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham564d8bc22012-03-09 04:10:47 +0000227 }
Jim Ingham3bfa7532012-11-06 01:14:52 +0000228 else
229 {
230 // The current clang (at least through 424) doesn't always get the address range for the
231 // DW_TAG_inlined_subroutines right, so that when you leave the inlined range the line table says
232 // you are still in the source file of the inlining function. This is bad, because now you are missing
233 // the stack frame for the function containing the inlining, and if you sensibly do "finish" to get
234 // out of this function you will instead exit the containing function.
235 // To work around this, we check whether we are still in the source file we started in, and if not assume
236 // it is an error, and push a plan to get us out of this line and back to the containing file.
237
238 if (m_addr_context.line_entry.IsValid())
239 {
240 SymbolContext sc;
Jason Molendab57e4a12013-11-04 09:33:30 +0000241 StackFrameSP frame_sp = m_thread.GetStackFrameAtIndex(0);
Jim Ingham3bfa7532012-11-06 01:14:52 +0000242 sc = frame_sp->GetSymbolContext (eSymbolContextEverything);
243 if (sc.line_entry.IsValid())
244 {
245 if (sc.line_entry.file != m_addr_context.line_entry.file
246 && sc.comp_unit == m_addr_context.comp_unit
247 && sc.function == m_addr_context.function)
248 {
Bruce Mitchener58ef3912015-06-18 05:27:05 +0000249 // Okay, find the next occurrence of this file in the line table:
Jim Ingham3bfa7532012-11-06 01:14:52 +0000250 LineTable *line_table = m_addr_context.comp_unit->GetLineTable();
251 if (line_table)
252 {
253 Address cur_address = frame_sp->GetFrameCodeAddress();
254 uint32_t entry_idx;
255 LineEntry line_entry;
256 if (line_table->FindLineEntryByAddress (cur_address, line_entry, &entry_idx))
257 {
258 LineEntry next_line_entry;
259 bool step_past_remaining_inline = false;
260 if (entry_idx > 0)
261 {
Bruce Mitchener58ef3912015-06-18 05:27:05 +0000262 // We require the previous line entry and the current line entry come
Jim Ingham3bfa7532012-11-06 01:14:52 +0000263 // from the same file.
264 // The other requirement is that the previous line table entry be part of an
265 // inlined block, we don't want to step past cases where people have inlined
266 // some code fragment by using #include <source-fragment.c> directly.
267 LineEntry prev_line_entry;
268 if (line_table->GetLineEntryAtIndex(entry_idx - 1, prev_line_entry)
269 && prev_line_entry.file == line_entry.file)
270 {
271 SymbolContext prev_sc;
272 Address prev_address = prev_line_entry.range.GetBaseAddress();
273 prev_address.CalculateSymbolContext(&prev_sc);
274 if (prev_sc.block)
275 {
276 Block *inlined_block = prev_sc.block->GetContainingInlinedBlock();
277 if (inlined_block)
278 {
279 AddressRange inline_range;
280 inlined_block->GetRangeContainingAddress(prev_address, inline_range);
281 if (!inline_range.ContainsFileAddress(cur_address))
282 {
283
284 step_past_remaining_inline = true;
285 }
286
287 }
288 }
289 }
290 }
291
292 if (step_past_remaining_inline)
293 {
294 uint32_t look_ahead_step = 1;
295 while (line_table->GetLineEntryAtIndex(entry_idx + look_ahead_step, next_line_entry))
296 {
297 // Make sure we haven't wandered out of the function we started from...
298 Address next_line_address = next_line_entry.range.GetBaseAddress();
299 Function *next_line_function = next_line_address.CalculateSymbolContextFunction();
300 if (next_line_function != m_addr_context.function)
301 break;
302
303 if (next_line_entry.file == m_addr_context.line_entry.file)
304 {
305 const bool abort_other_plans = false;
306 const bool stop_other_threads = false;
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000307 new_plan_sp = m_thread.QueueThreadPlanForRunToAddress(abort_other_plans,
Jim Ingham3bfa7532012-11-06 01:14:52 +0000308 next_line_address,
309 stop_other_threads);
310 break;
311 }
312 look_ahead_step++;
313 }
314 }
315 }
316 }
317 }
318 }
319 }
320 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 }
322
Jim Ingham564d8bc22012-03-09 04:10:47 +0000323 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
324 ClearNextBranchBreakpoint();
325
Jim Ingham4b4b2472014-03-13 02:47:14 +0000326
327 // If we haven't figured out something to do yet, then ask the ShouldStopHere callback:
328 if (!new_plan_sp)
329 {
330 new_plan_sp = CheckShouldStopHereAndQueueStepOut (frame_order);
331 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000332
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000333 if (!new_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 m_no_more_plans = true;
335 else
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000336 {
337 // Any new plan will be an implementation plan, so mark it private:
338 new_plan_sp->SetPrivate(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339 m_no_more_plans = false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000340 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000342 if (!new_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 {
344 // For efficiencies sake, we know we're done here so we don't have to do this
345 // calculation again in MischiefManaged.
346 SetPlanComplete();
347 return true;
348 }
349 else
350 return false;
351}
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000352
353bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000354ThreadPlanStepOverRange::DoPlanExplainsStop (Event *event_ptr)
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000355{
356 // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us)
357 // handle the stop. That way the user can see the stop, step around, and then when they
358 // are done, continue and have their step complete. The exception is if we've hit our
359 // "run to next branch" breakpoint.
360 // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an
361 // unexplained breakpoint/crash.
362
Greg Clayton5160ce52013-03-27 23:08:40 +0000363 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham60c41182013-06-04 01:40:51 +0000364 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
Jim Ingham221d51c2013-05-08 00:35:16 +0000365 bool return_value;
366
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000367 if (stop_info_sp)
368 {
369 StopReason reason = stop_info_sp->GetStopReason();
370
Jim Ingham9b03fa02015-07-23 19:55:02 +0000371 if (reason == eStopReasonTrace)
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000372 {
Jim Ingham221d51c2013-05-08 00:35:16 +0000373 return_value = true;
Jim Ingham9b03fa02015-07-23 19:55:02 +0000374 }
375 else if (reason == eStopReasonBreakpoint)
376 {
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000377 if (NextRangeBreakpointExplainsStop(stop_info_sp))
Jim Ingham221d51c2013-05-08 00:35:16 +0000378 return_value = true;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000379 else
Jim Ingham221d51c2013-05-08 00:35:16 +0000380 return_value = false;
Jim Ingham9b03fa02015-07-23 19:55:02 +0000381 }
382 else
383 {
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000384 if (log)
385 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
Jim Ingham221d51c2013-05-08 00:35:16 +0000386 return_value = false;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000387 }
388 }
Jim Ingham221d51c2013-05-08 00:35:16 +0000389 else
390 return_value = true;
391
392 return return_value;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000393}
Jim Ingham513c6bb2012-09-01 01:02:41 +0000394
395bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000396ThreadPlanStepOverRange::DoWillResume (lldb::StateType resume_state, bool current_plan)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000397{
398 if (resume_state != eStateSuspended && m_first_resume)
399 {
400 m_first_resume = false;
401 if (resume_state == eStateStepping && current_plan)
402 {
403 // See if we are about to step over an inlined call in the middle of the inlined stack, if so figure
404 // out its extents and reset our range to step over that.
405 bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth();
406 if (in_inlined_stack)
407 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000408 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham513c6bb2012-09-01 01:02:41 +0000409 if (log)
Jim Ingham221d51c2013-05-08 00:35:16 +0000410 log->Printf ("ThreadPlanStepInRange::DoWillResume: adjusting range to the frame at inlined depth %d.",
Jim Ingham513c6bb2012-09-01 01:02:41 +0000411 m_thread.GetCurrentInlinedDepth());
Jason Molendab57e4a12013-11-04 09:33:30 +0000412 StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0);
Jim Ingham513c6bb2012-09-01 01:02:41 +0000413 if (stack_sp)
414 {
415 Block *frame_block = stack_sp->GetFrameBlock();
416 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
417 AddressRange my_range;
418 if (frame_block->GetRangeContainingLoadAddress(curr_pc, m_thread.GetProcess()->GetTarget(), my_range))
419 {
420 m_address_ranges.clear();
421 m_address_ranges.push_back(my_range);
422 if (log)
423 {
424 StreamString s;
425 const InlineFunctionInfo *inline_info = frame_block->GetInlinedFunctionInfo();
426 const char *name;
427 if (inline_info)
Greg Claytonddaf6a72015-07-08 22:32:23 +0000428 name = inline_info->GetName(frame_block->CalculateSymbolContextFunction()->GetLanguage()).AsCString();
Jim Ingham513c6bb2012-09-01 01:02:41 +0000429 else
430 name = "<unknown-notinlined>";
431
432 s.Printf ("Stepping over inlined function \"%s\" in inlined stack: ", name);
433 DumpRanges(&s);
434 log->PutCString(s.GetData());
435 }
436 }
437
438 }
439 }
440 }
441 }
442
Jim Ingham221d51c2013-05-08 00:35:16 +0000443 return true;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000444}
Jim Ingham4b4b2472014-03-13 02:47:14 +0000445