blob: 0e1498dc92353fc0bb1b589421fb98b46df33885 [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
16
17#include "lldb/lldb-private-log.h"
18#include "lldb/Core/Log.h"
19#include "lldb/Core/Stream.h"
Jim Ingham513c6bb2012-09-01 01:02:41 +000020#include "lldb/Symbol/Block.h"
Jim Ingham3bfa7532012-11-06 01:14:52 +000021#include "lldb/Symbol/CompileUnit.h"
Jim Ingham513c6bb2012-09-01 01:02:41 +000022#include "lldb/Symbol/Function.h"
Jim Ingham3bfa7532012-11-06 01:14:52 +000023#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Target/Process.h"
25#include "lldb/Target/RegisterContext.h"
Greg Clayton514487e2011-02-15 21:59:32 +000026#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Target/Thread.h"
28#include "lldb/Target/ThreadPlanStepOut.h"
29#include "lldb/Target/ThreadPlanStepThrough.h"
30
31using namespace lldb_private;
Greg Clayton2d4edfb2010-11-06 01:53:30 +000032using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
Jim Ingham4b4b2472014-03-13 02:47:14 +000034uint32_t ThreadPlanStepOverRange::s_default_flag_values = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
36//----------------------------------------------------------------------
37// ThreadPlanStepOverRange: Step through a stack range, either stepping over or into
38// based on the value of \a type.
39//----------------------------------------------------------------------
40
41ThreadPlanStepOverRange::ThreadPlanStepOverRange
42(
43 Thread &thread,
44 const AddressRange &range,
45 const SymbolContext &addr_context,
Jim Ingham4b4b2472014-03-13 02:47:14 +000046 lldb::RunMode stop_others,
47 LazyBool step_out_avoids_code_without_debug_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048) :
Jim Ingham513c6bb2012-09-01 01:02:41 +000049 ThreadPlanStepRange (ThreadPlan::eKindStepOverRange, "Step range stepping over", thread, range, addr_context, stop_others),
Jim Ingham4b4b2472014-03-13 02:47:14 +000050 ThreadPlanShouldStopHere (this),
Jim Ingham513c6bb2012-09-01 01:02:41 +000051 m_first_resume(true)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052{
Jim Ingham4b4b2472014-03-13 02:47:14 +000053 SetFlagsToDefault();
54 SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055}
56
57ThreadPlanStepOverRange::~ThreadPlanStepOverRange ()
58{
59}
60
61void
62ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
63{
64 if (level == lldb::eDescriptionLevelBrief)
65 s->Printf("step over");
66 else
67 {
68 s->Printf ("stepping through range (stepping over functions): ");
Jim Inghamc4c9fed2011-10-15 00:24:48 +000069 DumpRanges(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070 }
71}
72
Jim Ingham4b4b2472014-03-13 02:47:14 +000073void
74ThreadPlanStepOverRange::SetupAvoidNoDebug(LazyBool step_out_avoids_code_without_debug_info)
75{
76 bool avoid_nodebug = true;
77 switch (step_out_avoids_code_without_debug_info)
78 {
79 case eLazyBoolYes:
80 avoid_nodebug = true;
81 break;
82 case eLazyBoolNo:
83 avoid_nodebug = false;
84 break;
85 case eLazyBoolCalculate:
86 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
87 break;
88 }
89 if (avoid_nodebug)
90 GetFlags().Set (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
91 else
92 GetFlags().Clear (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
93}
94
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095bool
Daniel Malea4b867282013-09-17 16:35:45 +000096ThreadPlanStepOverRange::IsEquivalentContext(const SymbolContext &context)
97{
98
99 // Match as much as is specified in the m_addr_context:
100 // This is a fairly loose sanity check. Note, sometimes the target doesn't get filled
101 // in so I left out the target check. And sometimes the module comes in as the .o file from the
102 // inlined range, so I left that out too...
103 if (m_addr_context.comp_unit)
104 {
105 if (m_addr_context.comp_unit == context.comp_unit)
106 {
107 if (m_addr_context.function && m_addr_context.function == context.function)
108 {
109 if (m_addr_context.block && m_addr_context.block == context.block)
110 return true;
111 }
112 }
113 }
114 else if (m_addr_context.symbol && m_addr_context.symbol == context.symbol)
115 {
116 return true;
117 }
118 return false;
119}
120
121bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122ThreadPlanStepOverRange::ShouldStop (Event *event_ptr)
123{
Greg Clayton5160ce52013-03-27 23:08:40 +0000124 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125
126 if (log)
127 {
128 StreamString s;
Greg Clayton514487e2011-02-15 21:59:32 +0000129 s.Address (m_thread.GetRegisterContext()->GetPC(),
Greg Clayton1ac04c32012-02-21 00:09:25 +0000130 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
132 }
133
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134 // If we're out of the range but in the same frame or in our caller's frame
135 // then we should stop.
Jim Ingham4a58e962012-10-25 22:30:09 +0000136 // When stepping out we only stop others if we are forcing running one thread.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137 bool stop_others;
138 if (m_stop_others == lldb::eOnlyThisThread)
139 stop_others = true;
140 else
141 stop_others = false;
142
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000143 ThreadPlanSP new_plan_sp;
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000144
145 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
146
147 if (frame_order == eFrameCompareOlder)
Jim Ingham58221732010-11-05 00:18:21 +0000148 {
149 // If we're in an older frame then we should stop.
150 //
151 // A caveat to this is if we think the frame is older but we're actually in a trampoline.
152 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are
153 // in a trampoline we think the frame is older because the trampoline confused the backtracer.
154 // As below, we step through first, and then try to figure out how to get back out again.
155
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000156 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham58221732010-11-05 00:18:21 +0000157
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000158 if (new_plan_sp && log)
Jim Ingham58221732010-11-05 00:18:21 +0000159 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
160 }
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000161 else if (frame_order == eFrameCompareYounger)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 {
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000163 // Make sure we really are in a new frame. Do that by unwinding and seeing if the
164 // start function really is our start function...
Daniel Malea4b867282013-09-17 16:35:45 +0000165 for(uint32_t i = 1;; ++i)
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000166 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000167 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(i);
Daniel Malea4b867282013-09-17 16:35:45 +0000168 if (!older_frame_sp) {
169 // We can't unwind the next frame we should just get out of here & stop...
170 break;
171 }
172
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000173 const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
Daniel Malea4b867282013-09-17 16:35:45 +0000174 if (IsEquivalentContext(older_context))
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000175 {
Jim Ingham4b4b2472014-03-13 02:47:14 +0000176 new_plan_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop (false,
177 NULL,
178 true,
179 stop_others,
180 eVoteNo,
181 eVoteNoOpinion,
182 0);
Daniel Malea4b867282013-09-17 16:35:45 +0000183 break;
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000184 }
Daniel Malea4b867282013-09-17 16:35:45 +0000185 else
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000186 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000187 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000188 }
189 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190 }
Jim Ingham564d8bc22012-03-09 04:10:47 +0000191 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 {
Jim Ingham564d8bc22012-03-09 04:10:47 +0000193 // If we're still in the range, keep going.
194 if (InRange())
195 {
196 SetNextBranchBreakpoint();
197 return false;
198 }
199
200
201 if (!InSymbol())
202 {
203 // This one is a little tricky. Sometimes we may be in a stub or something similar,
204 // in which case we need to get out of there. But if we are in a stub then it's
205 // likely going to be hard to get out from here. It is probably easiest to step into the
206 // stub, and then it will be straight-forward to step out.
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000207 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham564d8bc22012-03-09 04:10:47 +0000208 }
Jim Ingham3bfa7532012-11-06 01:14:52 +0000209 else
210 {
211 // The current clang (at least through 424) doesn't always get the address range for the
212 // DW_TAG_inlined_subroutines right, so that when you leave the inlined range the line table says
213 // you are still in the source file of the inlining function. This is bad, because now you are missing
214 // the stack frame for the function containing the inlining, and if you sensibly do "finish" to get
215 // out of this function you will instead exit the containing function.
216 // To work around this, we check whether we are still in the source file we started in, and if not assume
217 // it is an error, and push a plan to get us out of this line and back to the containing file.
218
219 if (m_addr_context.line_entry.IsValid())
220 {
221 SymbolContext sc;
Jason Molendab57e4a12013-11-04 09:33:30 +0000222 StackFrameSP frame_sp = m_thread.GetStackFrameAtIndex(0);
Jim Ingham3bfa7532012-11-06 01:14:52 +0000223 sc = frame_sp->GetSymbolContext (eSymbolContextEverything);
224 if (sc.line_entry.IsValid())
225 {
226 if (sc.line_entry.file != m_addr_context.line_entry.file
227 && sc.comp_unit == m_addr_context.comp_unit
228 && sc.function == m_addr_context.function)
229 {
230 // Okay, find the next occurance of this file in the line table:
231 LineTable *line_table = m_addr_context.comp_unit->GetLineTable();
232 if (line_table)
233 {
234 Address cur_address = frame_sp->GetFrameCodeAddress();
235 uint32_t entry_idx;
236 LineEntry line_entry;
237 if (line_table->FindLineEntryByAddress (cur_address, line_entry, &entry_idx))
238 {
239 LineEntry next_line_entry;
240 bool step_past_remaining_inline = false;
241 if (entry_idx > 0)
242 {
243 // We require the the previous line entry and the current line entry come
244 // from the same file.
245 // The other requirement is that the previous line table entry be part of an
246 // inlined block, we don't want to step past cases where people have inlined
247 // some code fragment by using #include <source-fragment.c> directly.
248 LineEntry prev_line_entry;
249 if (line_table->GetLineEntryAtIndex(entry_idx - 1, prev_line_entry)
250 && prev_line_entry.file == line_entry.file)
251 {
252 SymbolContext prev_sc;
253 Address prev_address = prev_line_entry.range.GetBaseAddress();
254 prev_address.CalculateSymbolContext(&prev_sc);
255 if (prev_sc.block)
256 {
257 Block *inlined_block = prev_sc.block->GetContainingInlinedBlock();
258 if (inlined_block)
259 {
260 AddressRange inline_range;
261 inlined_block->GetRangeContainingAddress(prev_address, inline_range);
262 if (!inline_range.ContainsFileAddress(cur_address))
263 {
264
265 step_past_remaining_inline = true;
266 }
267
268 }
269 }
270 }
271 }
272
273 if (step_past_remaining_inline)
274 {
275 uint32_t look_ahead_step = 1;
276 while (line_table->GetLineEntryAtIndex(entry_idx + look_ahead_step, next_line_entry))
277 {
278 // Make sure we haven't wandered out of the function we started from...
279 Address next_line_address = next_line_entry.range.GetBaseAddress();
280 Function *next_line_function = next_line_address.CalculateSymbolContextFunction();
281 if (next_line_function != m_addr_context.function)
282 break;
283
284 if (next_line_entry.file == m_addr_context.line_entry.file)
285 {
286 const bool abort_other_plans = false;
287 const bool stop_other_threads = false;
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000288 new_plan_sp = m_thread.QueueThreadPlanForRunToAddress(abort_other_plans,
Jim Ingham3bfa7532012-11-06 01:14:52 +0000289 next_line_address,
290 stop_other_threads);
291 break;
292 }
293 look_ahead_step++;
294 }
295 }
296 }
297 }
298 }
299 }
300 }
301 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302 }
303
Jim Ingham564d8bc22012-03-09 04:10:47 +0000304 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
305 ClearNextBranchBreakpoint();
306
Jim Ingham4b4b2472014-03-13 02:47:14 +0000307
308 // If we haven't figured out something to do yet, then ask the ShouldStopHere callback:
309 if (!new_plan_sp)
310 {
311 new_plan_sp = CheckShouldStopHereAndQueueStepOut (frame_order);
312 }
313
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000314 if (!new_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315 m_no_more_plans = true;
316 else
317 m_no_more_plans = false;
318
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000319 if (!new_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 {
321 // For efficiencies sake, we know we're done here so we don't have to do this
322 // calculation again in MischiefManaged.
323 SetPlanComplete();
324 return true;
325 }
326 else
327 return false;
328}
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000329
330bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000331ThreadPlanStepOverRange::DoPlanExplainsStop (Event *event_ptr)
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000332{
333 // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us)
334 // handle the stop. That way the user can see the stop, step around, and then when they
335 // are done, continue and have their step complete. The exception is if we've hit our
336 // "run to next branch" breakpoint.
337 // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an
338 // unexplained breakpoint/crash.
339
Greg Clayton5160ce52013-03-27 23:08:40 +0000340 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham60c41182013-06-04 01:40:51 +0000341 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
Jim Ingham221d51c2013-05-08 00:35:16 +0000342 bool return_value;
343
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000344 if (stop_info_sp)
345 {
346 StopReason reason = stop_info_sp->GetStopReason();
347
348 switch (reason)
349 {
Jim Ingham513c6bb2012-09-01 01:02:41 +0000350 case eStopReasonTrace:
Jim Ingham221d51c2013-05-08 00:35:16 +0000351 return_value = true;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000352 break;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000353 case eStopReasonBreakpoint:
354 if (NextRangeBreakpointExplainsStop(stop_info_sp))
Jim Ingham221d51c2013-05-08 00:35:16 +0000355 return_value = true;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000356 else
Jim Ingham221d51c2013-05-08 00:35:16 +0000357 return_value = false;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000358 break;
359 case eStopReasonWatchpoint:
360 case eStopReasonSignal:
361 case eStopReasonException:
Greg Clayton90ba8112012-12-05 00:16:59 +0000362 case eStopReasonExec:
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000363 case eStopReasonThreadExiting:
Jim Ingham513c6bb2012-09-01 01:02:41 +0000364 default:
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000365 if (log)
366 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
Jim Ingham221d51c2013-05-08 00:35:16 +0000367 return_value = false;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000368 break;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000369 }
370 }
Jim Ingham221d51c2013-05-08 00:35:16 +0000371 else
372 return_value = true;
373
374 return return_value;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000375}
Jim Ingham513c6bb2012-09-01 01:02:41 +0000376
377bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000378ThreadPlanStepOverRange::DoWillResume (lldb::StateType resume_state, bool current_plan)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000379{
380 if (resume_state != eStateSuspended && m_first_resume)
381 {
382 m_first_resume = false;
383 if (resume_state == eStateStepping && current_plan)
384 {
385 // See if we are about to step over an inlined call in the middle of the inlined stack, if so figure
386 // out its extents and reset our range to step over that.
387 bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth();
388 if (in_inlined_stack)
389 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000390 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham513c6bb2012-09-01 01:02:41 +0000391 if (log)
Jim Ingham221d51c2013-05-08 00:35:16 +0000392 log->Printf ("ThreadPlanStepInRange::DoWillResume: adjusting range to the frame at inlined depth %d.",
Jim Ingham513c6bb2012-09-01 01:02:41 +0000393 m_thread.GetCurrentInlinedDepth());
Jason Molendab57e4a12013-11-04 09:33:30 +0000394 StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0);
Jim Ingham513c6bb2012-09-01 01:02:41 +0000395 if (stack_sp)
396 {
397 Block *frame_block = stack_sp->GetFrameBlock();
398 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
399 AddressRange my_range;
400 if (frame_block->GetRangeContainingLoadAddress(curr_pc, m_thread.GetProcess()->GetTarget(), my_range))
401 {
402 m_address_ranges.clear();
403 m_address_ranges.push_back(my_range);
404 if (log)
405 {
406 StreamString s;
407 const InlineFunctionInfo *inline_info = frame_block->GetInlinedFunctionInfo();
408 const char *name;
409 if (inline_info)
410 name = inline_info->GetName().AsCString();
411 else
412 name = "<unknown-notinlined>";
413
414 s.Printf ("Stepping over inlined function \"%s\" in inlined stack: ", name);
415 DumpRanges(&s);
416 log->PutCString(s.GetData());
417 }
418 }
419
420 }
421 }
422 }
423 }
424
Jim Ingham221d51c2013-05-08 00:35:16 +0000425 return true;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000426}
Jim Ingham4b4b2472014-03-13 02:47:14 +0000427