blob: 8363183a42091d53ab2151a4d70e983412654724 [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 Ingham5beccb22014-08-05 01:59:20 +0000188 // If we found a way through, then we should stop recursing.
189 if (new_plan_sp)
190 break;
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000191 }
192 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 }
Jim Ingham564d8bc22012-03-09 04:10:47 +0000194 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195 {
Jim Ingham564d8bc22012-03-09 04:10:47 +0000196 // If we're still in the range, keep going.
197 if (InRange())
198 {
199 SetNextBranchBreakpoint();
200 return false;
201 }
202
203
204 if (!InSymbol())
205 {
206 // This one is a little tricky. Sometimes we may be in a stub or something similar,
207 // in which case we need to get out of there. But if we are in a stub then it's
208 // likely going to be hard to get out from here. It is probably easiest to step into the
209 // stub, and then it will be straight-forward to step out.
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000210 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham564d8bc22012-03-09 04:10:47 +0000211 }
Jim Ingham3bfa7532012-11-06 01:14:52 +0000212 else
213 {
214 // The current clang (at least through 424) doesn't always get the address range for the
215 // DW_TAG_inlined_subroutines right, so that when you leave the inlined range the line table says
216 // you are still in the source file of the inlining function. This is bad, because now you are missing
217 // the stack frame for the function containing the inlining, and if you sensibly do "finish" to get
218 // out of this function you will instead exit the containing function.
219 // To work around this, we check whether we are still in the source file we started in, and if not assume
220 // it is an error, and push a plan to get us out of this line and back to the containing file.
221
222 if (m_addr_context.line_entry.IsValid())
223 {
224 SymbolContext sc;
Jason Molendab57e4a12013-11-04 09:33:30 +0000225 StackFrameSP frame_sp = m_thread.GetStackFrameAtIndex(0);
Jim Ingham3bfa7532012-11-06 01:14:52 +0000226 sc = frame_sp->GetSymbolContext (eSymbolContextEverything);
227 if (sc.line_entry.IsValid())
228 {
229 if (sc.line_entry.file != m_addr_context.line_entry.file
230 && sc.comp_unit == m_addr_context.comp_unit
231 && sc.function == m_addr_context.function)
232 {
233 // Okay, find the next occurance of this file in the line table:
234 LineTable *line_table = m_addr_context.comp_unit->GetLineTable();
235 if (line_table)
236 {
237 Address cur_address = frame_sp->GetFrameCodeAddress();
238 uint32_t entry_idx;
239 LineEntry line_entry;
240 if (line_table->FindLineEntryByAddress (cur_address, line_entry, &entry_idx))
241 {
242 LineEntry next_line_entry;
243 bool step_past_remaining_inline = false;
244 if (entry_idx > 0)
245 {
246 // We require the the previous line entry and the current line entry come
247 // from the same file.
248 // The other requirement is that the previous line table entry be part of an
249 // inlined block, we don't want to step past cases where people have inlined
250 // some code fragment by using #include <source-fragment.c> directly.
251 LineEntry prev_line_entry;
252 if (line_table->GetLineEntryAtIndex(entry_idx - 1, prev_line_entry)
253 && prev_line_entry.file == line_entry.file)
254 {
255 SymbolContext prev_sc;
256 Address prev_address = prev_line_entry.range.GetBaseAddress();
257 prev_address.CalculateSymbolContext(&prev_sc);
258 if (prev_sc.block)
259 {
260 Block *inlined_block = prev_sc.block->GetContainingInlinedBlock();
261 if (inlined_block)
262 {
263 AddressRange inline_range;
264 inlined_block->GetRangeContainingAddress(prev_address, inline_range);
265 if (!inline_range.ContainsFileAddress(cur_address))
266 {
267
268 step_past_remaining_inline = true;
269 }
270
271 }
272 }
273 }
274 }
275
276 if (step_past_remaining_inline)
277 {
278 uint32_t look_ahead_step = 1;
279 while (line_table->GetLineEntryAtIndex(entry_idx + look_ahead_step, next_line_entry))
280 {
281 // Make sure we haven't wandered out of the function we started from...
282 Address next_line_address = next_line_entry.range.GetBaseAddress();
283 Function *next_line_function = next_line_address.CalculateSymbolContextFunction();
284 if (next_line_function != m_addr_context.function)
285 break;
286
287 if (next_line_entry.file == m_addr_context.line_entry.file)
288 {
289 const bool abort_other_plans = false;
290 const bool stop_other_threads = false;
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000291 new_plan_sp = m_thread.QueueThreadPlanForRunToAddress(abort_other_plans,
Jim Ingham3bfa7532012-11-06 01:14:52 +0000292 next_line_address,
293 stop_other_threads);
294 break;
295 }
296 look_ahead_step++;
297 }
298 }
299 }
300 }
301 }
302 }
303 }
304 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 }
306
Jim Ingham564d8bc22012-03-09 04:10:47 +0000307 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
308 ClearNextBranchBreakpoint();
309
Jim Ingham4b4b2472014-03-13 02:47:14 +0000310
311 // If we haven't figured out something to do yet, then ask the ShouldStopHere callback:
312 if (!new_plan_sp)
313 {
314 new_plan_sp = CheckShouldStopHereAndQueueStepOut (frame_order);
315 }
316
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000317 if (!new_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318 m_no_more_plans = true;
319 else
320 m_no_more_plans = false;
321
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000322 if (!new_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 {
324 // For efficiencies sake, we know we're done here so we don't have to do this
325 // calculation again in MischiefManaged.
326 SetPlanComplete();
327 return true;
328 }
329 else
330 return false;
331}
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000332
333bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000334ThreadPlanStepOverRange::DoPlanExplainsStop (Event *event_ptr)
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000335{
336 // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us)
337 // handle the stop. That way the user can see the stop, step around, and then when they
338 // are done, continue and have their step complete. The exception is if we've hit our
339 // "run to next branch" breakpoint.
340 // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an
341 // unexplained breakpoint/crash.
342
Greg Clayton5160ce52013-03-27 23:08:40 +0000343 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham60c41182013-06-04 01:40:51 +0000344 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
Jim Ingham221d51c2013-05-08 00:35:16 +0000345 bool return_value;
346
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000347 if (stop_info_sp)
348 {
349 StopReason reason = stop_info_sp->GetStopReason();
350
351 switch (reason)
352 {
Jim Ingham513c6bb2012-09-01 01:02:41 +0000353 case eStopReasonTrace:
Jim Ingham221d51c2013-05-08 00:35:16 +0000354 return_value = true;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000355 break;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000356 case eStopReasonBreakpoint:
357 if (NextRangeBreakpointExplainsStop(stop_info_sp))
Jim Ingham221d51c2013-05-08 00:35:16 +0000358 return_value = true;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000359 else
Jim Ingham221d51c2013-05-08 00:35:16 +0000360 return_value = false;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000361 break;
362 case eStopReasonWatchpoint:
363 case eStopReasonSignal:
364 case eStopReasonException:
Greg Clayton90ba8112012-12-05 00:16:59 +0000365 case eStopReasonExec:
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000366 case eStopReasonThreadExiting:
Jim Ingham513c6bb2012-09-01 01:02:41 +0000367 default:
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000368 if (log)
369 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
Jim Ingham221d51c2013-05-08 00:35:16 +0000370 return_value = false;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000371 break;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000372 }
373 }
Jim Ingham221d51c2013-05-08 00:35:16 +0000374 else
375 return_value = true;
376
377 return return_value;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000378}
Jim Ingham513c6bb2012-09-01 01:02:41 +0000379
380bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000381ThreadPlanStepOverRange::DoWillResume (lldb::StateType resume_state, bool current_plan)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000382{
383 if (resume_state != eStateSuspended && m_first_resume)
384 {
385 m_first_resume = false;
386 if (resume_state == eStateStepping && current_plan)
387 {
388 // See if we are about to step over an inlined call in the middle of the inlined stack, if so figure
389 // out its extents and reset our range to step over that.
390 bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth();
391 if (in_inlined_stack)
392 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000393 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham513c6bb2012-09-01 01:02:41 +0000394 if (log)
Jim Ingham221d51c2013-05-08 00:35:16 +0000395 log->Printf ("ThreadPlanStepInRange::DoWillResume: adjusting range to the frame at inlined depth %d.",
Jim Ingham513c6bb2012-09-01 01:02:41 +0000396 m_thread.GetCurrentInlinedDepth());
Jason Molendab57e4a12013-11-04 09:33:30 +0000397 StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0);
Jim Ingham513c6bb2012-09-01 01:02:41 +0000398 if (stack_sp)
399 {
400 Block *frame_block = stack_sp->GetFrameBlock();
401 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
402 AddressRange my_range;
403 if (frame_block->GetRangeContainingLoadAddress(curr_pc, m_thread.GetProcess()->GetTarget(), my_range))
404 {
405 m_address_ranges.clear();
406 m_address_ranges.push_back(my_range);
407 if (log)
408 {
409 StreamString s;
410 const InlineFunctionInfo *inline_info = frame_block->GetInlinedFunctionInfo();
411 const char *name;
412 if (inline_info)
413 name = inline_info->GetName().AsCString();
414 else
415 name = "<unknown-notinlined>";
416
417 s.Printf ("Stepping over inlined function \"%s\" in inlined stack: ", name);
418 DumpRanges(&s);
419 log->PutCString(s.GetData());
420 }
421 }
422
423 }
424 }
425 }
426 }
427
Jim Ingham221d51c2013-05-08 00:35:16 +0000428 return true;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000429}
Jim Ingham4b4b2472014-03-13 02:47:14 +0000430