blob: 08655be24395b46422ffd494853e5938996d6dca [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
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/ThreadPlanStepOverRange.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Log.h"
16#include "lldb/Core/Stream.h"
Jim Ingham513c6bb2012-09-01 01:02:41 +000017#include "lldb/Symbol/Block.h"
Jim Ingham3bfa7532012-11-06 01:14:52 +000018#include "lldb/Symbol/CompileUnit.h"
Jim Ingham513c6bb2012-09-01 01:02:41 +000019#include "lldb/Symbol/Function.h"
Jim Ingham3bfa7532012-11-06 01:14:52 +000020#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Target/Process.h"
22#include "lldb/Target/RegisterContext.h"
Greg Clayton514487e2011-02-15 21:59:32 +000023#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Target/Thread.h"
25#include "lldb/Target/ThreadPlanStepOut.h"
26#include "lldb/Target/ThreadPlanStepThrough.h"
27
28using namespace lldb_private;
Greg Clayton2d4edfb2010-11-06 01:53:30 +000029using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Jim Ingham4b4b2472014-03-13 02:47:14 +000031uint32_t ThreadPlanStepOverRange::s_default_flag_values = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
33//----------------------------------------------------------------------
34// ThreadPlanStepOverRange: Step through a stack range, either stepping over or into
35// based on the value of \a type.
36//----------------------------------------------------------------------
37
38ThreadPlanStepOverRange::ThreadPlanStepOverRange
39(
40 Thread &thread,
41 const AddressRange &range,
42 const SymbolContext &addr_context,
Jim Ingham4b4b2472014-03-13 02:47:14 +000043 lldb::RunMode stop_others,
44 LazyBool step_out_avoids_code_without_debug_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045) :
Jim Ingham513c6bb2012-09-01 01:02:41 +000046 ThreadPlanStepRange (ThreadPlan::eKindStepOverRange, "Step range stepping over", thread, range, addr_context, stop_others),
Jim Ingham4b4b2472014-03-13 02:47:14 +000047 ThreadPlanShouldStopHere (this),
Jim Ingham513c6bb2012-09-01 01:02:41 +000048 m_first_resume(true)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049{
Jim Ingham4b4b2472014-03-13 02:47:14 +000050 SetFlagsToDefault();
51 SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052}
53
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000054ThreadPlanStepOverRange::~ThreadPlanStepOverRange() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
56void
57ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
58{
59 if (level == lldb::eDescriptionLevelBrief)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +000061 s->Printf("step over");
62 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +000064 s->Printf ("Stepping over");
65 bool printed_line_info = false;
66 if (m_addr_context.line_entry.IsValid())
67 {
68 s->Printf (" line ");
69 m_addr_context.line_entry.DumpStopContext (s, false);
70 printed_line_info = true;
71 }
72
73 if (!printed_line_info || level == eDescriptionLevelVerbose)
74 {
75 s->Printf (" using ranges: ");
76 DumpRanges(s);
77 }
78
79 s->PutChar('.');
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080}
81
Jim Ingham4b4b2472014-03-13 02:47:14 +000082void
83ThreadPlanStepOverRange::SetupAvoidNoDebug(LazyBool step_out_avoids_code_without_debug_info)
84{
85 bool avoid_nodebug = true;
86 switch (step_out_avoids_code_without_debug_info)
87 {
88 case eLazyBoolYes:
89 avoid_nodebug = true;
90 break;
91 case eLazyBoolNo:
92 avoid_nodebug = false;
93 break;
94 case eLazyBoolCalculate:
95 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
96 break;
97 }
98 if (avoid_nodebug)
99 GetFlags().Set (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
100 else
101 GetFlags().Clear (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
Jim Ingham862d1bb2014-08-06 01:49:59 +0000102 // Step Over plans should always avoid no-debug on step in. Seems like you shouldn't
103 // have to say this, but a tail call looks more like a step in that a step out, so
104 // we want to catch this case.
105 GetFlags().Set (ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
Jim Ingham4b4b2472014-03-13 02:47:14 +0000106}
107
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108bool
Daniel Malea4b867282013-09-17 16:35:45 +0000109ThreadPlanStepOverRange::IsEquivalentContext(const SymbolContext &context)
110{
Daniel Malea4b867282013-09-17 16:35:45 +0000111 // Match as much as is specified in the m_addr_context:
112 // This is a fairly loose sanity check. Note, sometimes the target doesn't get filled
113 // in so I left out the target check. And sometimes the module comes in as the .o file from the
114 // inlined range, so I left that out too...
115 if (m_addr_context.comp_unit)
116 {
117 if (m_addr_context.comp_unit == context.comp_unit)
118 {
119 if (m_addr_context.function && m_addr_context.function == context.function)
120 {
121 if (m_addr_context.block && m_addr_context.block == context.block)
122 return true;
123 }
124 }
125 }
126 else if (m_addr_context.symbol && m_addr_context.symbol == context.symbol)
127 {
128 return true;
129 }
130 return false;
131}
132
133bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134ThreadPlanStepOverRange::ShouldStop (Event *event_ptr)
135{
Greg Clayton5160ce52013-03-27 23:08:40 +0000136 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137
138 if (log)
139 {
140 StreamString s;
Greg Clayton514487e2011-02-15 21:59:32 +0000141 s.Address (m_thread.GetRegisterContext()->GetPC(),
Greg Clayton1ac04c32012-02-21 00:09:25 +0000142 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143 log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
144 }
145
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146 // If we're out of the range but in the same frame or in our caller's frame
147 // then we should stop.
Jim Ingham4a58e962012-10-25 22:30:09 +0000148 // When stepping out we only stop others if we are forcing running one thread.
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000149 bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000150 ThreadPlanSP new_plan_sp;
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000151 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
152
153 if (frame_order == eFrameCompareOlder)
Jim Ingham58221732010-11-05 00:18:21 +0000154 {
155 // If we're in an older frame then we should stop.
156 //
157 // A caveat to this is if we think the frame is older but we're actually in a trampoline.
158 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are
159 // in a trampoline we think the frame is older because the trampoline confused the backtracer.
160 // As below, we step through first, and then try to figure out how to get back out again.
161
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000162 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham58221732010-11-05 00:18:21 +0000163
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000164 if (new_plan_sp && log)
Jim Ingham58221732010-11-05 00:18:21 +0000165 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
166 }
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000167 else if (frame_order == eFrameCompareYounger)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168 {
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000169 // Make sure we really are in a new frame. Do that by unwinding and seeing if the
170 // start function really is our start function...
Daniel Malea4b867282013-09-17 16:35:45 +0000171 for(uint32_t i = 1;; ++i)
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000172 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000173 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(i);
Daniel Malea4b867282013-09-17 16:35:45 +0000174 if (!older_frame_sp) {
175 // We can't unwind the next frame we should just get out of here & stop...
176 break;
177 }
178
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000179 const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
Daniel Malea4b867282013-09-17 16:35:45 +0000180 if (IsEquivalentContext(older_context))
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000181 {
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000182 new_plan_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop(false,
183 nullptr,
184 true,
185 stop_others,
186 eVoteNo,
187 eVoteNoOpinion,
Jason Molenda7cb9d982016-01-08 02:26:03 +0000188 0);
Daniel Malea4b867282013-09-17 16:35:45 +0000189 break;
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000190 }
Daniel Malea4b867282013-09-17 16:35:45 +0000191 else
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000192 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000193 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham5beccb22014-08-05 01:59:20 +0000194 // If we found a way through, then we should stop recursing.
195 if (new_plan_sp)
196 break;
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000197 }
198 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199 }
Jim Ingham564d8bc22012-03-09 04:10:47 +0000200 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 {
Jim Ingham564d8bc22012-03-09 04:10:47 +0000202 // If we're still in the range, keep going.
203 if (InRange())
204 {
205 SetNextBranchBreakpoint();
206 return false;
207 }
208
Jim Ingham564d8bc22012-03-09 04:10:47 +0000209 if (!InSymbol())
210 {
211 // This one is a little tricky. Sometimes we may be in a stub or something similar,
212 // in which case we need to get out of there. But if we are in a stub then it's
213 // likely going to be hard to get out from here. It is probably easiest to step into the
214 // stub, and then it will be straight-forward to step out.
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000215 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham564d8bc22012-03-09 04:10:47 +0000216 }
Jim Ingham3bfa7532012-11-06 01:14:52 +0000217 else
218 {
219 // The current clang (at least through 424) doesn't always get the address range for the
220 // DW_TAG_inlined_subroutines right, so that when you leave the inlined range the line table says
221 // you are still in the source file of the inlining function. This is bad, because now you are missing
222 // the stack frame for the function containing the inlining, and if you sensibly do "finish" to get
223 // out of this function you will instead exit the containing function.
224 // To work around this, we check whether we are still in the source file we started in, and if not assume
225 // it is an error, and push a plan to get us out of this line and back to the containing file.
226
227 if (m_addr_context.line_entry.IsValid())
228 {
229 SymbolContext sc;
Jason Molendab57e4a12013-11-04 09:33:30 +0000230 StackFrameSP frame_sp = m_thread.GetStackFrameAtIndex(0);
Jim Ingham3bfa7532012-11-06 01:14:52 +0000231 sc = frame_sp->GetSymbolContext (eSymbolContextEverything);
232 if (sc.line_entry.IsValid())
233 {
234 if (sc.line_entry.file != m_addr_context.line_entry.file
235 && sc.comp_unit == m_addr_context.comp_unit
236 && sc.function == m_addr_context.function)
237 {
Bruce Mitchener58ef3912015-06-18 05:27:05 +0000238 // Okay, find the next occurrence of this file in the line table:
Jim Ingham3bfa7532012-11-06 01:14:52 +0000239 LineTable *line_table = m_addr_context.comp_unit->GetLineTable();
240 if (line_table)
241 {
242 Address cur_address = frame_sp->GetFrameCodeAddress();
243 uint32_t entry_idx;
244 LineEntry line_entry;
245 if (line_table->FindLineEntryByAddress (cur_address, line_entry, &entry_idx))
246 {
247 LineEntry next_line_entry;
248 bool step_past_remaining_inline = false;
249 if (entry_idx > 0)
250 {
Bruce Mitchener58ef3912015-06-18 05:27:05 +0000251 // We require the previous line entry and the current line entry come
Jim Ingham3bfa7532012-11-06 01:14:52 +0000252 // from the same file.
253 // The other requirement is that the previous line table entry be part of an
254 // inlined block, we don't want to step past cases where people have inlined
255 // some code fragment by using #include <source-fragment.c> directly.
256 LineEntry prev_line_entry;
257 if (line_table->GetLineEntryAtIndex(entry_idx - 1, prev_line_entry)
258 && prev_line_entry.file == line_entry.file)
259 {
260 SymbolContext prev_sc;
261 Address prev_address = prev_line_entry.range.GetBaseAddress();
262 prev_address.CalculateSymbolContext(&prev_sc);
263 if (prev_sc.block)
264 {
265 Block *inlined_block = prev_sc.block->GetContainingInlinedBlock();
266 if (inlined_block)
267 {
268 AddressRange inline_range;
269 inlined_block->GetRangeContainingAddress(prev_address, inline_range);
270 if (!inline_range.ContainsFileAddress(cur_address))
271 {
272
273 step_past_remaining_inline = true;
274 }
Jim Ingham3bfa7532012-11-06 01:14:52 +0000275 }
276 }
277 }
278 }
279
280 if (step_past_remaining_inline)
281 {
282 uint32_t look_ahead_step = 1;
283 while (line_table->GetLineEntryAtIndex(entry_idx + look_ahead_step, next_line_entry))
284 {
285 // Make sure we haven't wandered out of the function we started from...
286 Address next_line_address = next_line_entry.range.GetBaseAddress();
287 Function *next_line_function = next_line_address.CalculateSymbolContextFunction();
288 if (next_line_function != m_addr_context.function)
289 break;
290
291 if (next_line_entry.file == m_addr_context.line_entry.file)
292 {
293 const bool abort_other_plans = false;
Jim Ingham55828082015-08-14 01:38:21 +0000294 const RunMode stop_other_threads = RunMode::eAllThreads;
295 lldb::addr_t cur_pc = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
296 AddressRange step_range(cur_pc, next_line_address.GetLoadAddress(&GetTarget()) - cur_pc);
297
298 new_plan_sp = m_thread.QueueThreadPlanForStepOverRange (abort_other_plans,
299 step_range,
300 sc,
301 stop_other_threads);
Jim Ingham3bfa7532012-11-06 01:14:52 +0000302 break;
303 }
304 look_ahead_step++;
305 }
306 }
307 }
308 }
309 }
310 }
311 }
312 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313 }
314
Jim Ingham564d8bc22012-03-09 04:10:47 +0000315 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
316 ClearNextBranchBreakpoint();
317
Jim Ingham4b4b2472014-03-13 02:47:14 +0000318
319 // If we haven't figured out something to do yet, then ask the ShouldStopHere callback:
320 if (!new_plan_sp)
321 {
322 new_plan_sp = CheckShouldStopHereAndQueueStepOut (frame_order);
323 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000324
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000325 if (!new_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326 m_no_more_plans = true;
327 else
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000328 {
329 // Any new plan will be an implementation plan, so mark it private:
330 new_plan_sp->SetPrivate(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331 m_no_more_plans = false;
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000332 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000334 if (!new_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 {
336 // For efficiencies sake, we know we're done here so we don't have to do this
337 // calculation again in MischiefManaged.
338 SetPlanComplete();
339 return true;
340 }
341 else
342 return false;
343}
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000344
345bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000346ThreadPlanStepOverRange::DoPlanExplainsStop (Event *event_ptr)
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000347{
348 // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us)
349 // handle the stop. That way the user can see the stop, step around, and then when they
350 // are done, continue and have their step complete. The exception is if we've hit our
351 // "run to next branch" breakpoint.
352 // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an
353 // unexplained breakpoint/crash.
354
Greg Clayton5160ce52013-03-27 23:08:40 +0000355 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham60c41182013-06-04 01:40:51 +0000356 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
Jim Ingham221d51c2013-05-08 00:35:16 +0000357 bool return_value;
358
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000359 if (stop_info_sp)
360 {
361 StopReason reason = stop_info_sp->GetStopReason();
362
Jim Ingham9b03fa02015-07-23 19:55:02 +0000363 if (reason == eStopReasonTrace)
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000364 {
Jim Ingham221d51c2013-05-08 00:35:16 +0000365 return_value = true;
Jim Ingham9b03fa02015-07-23 19:55:02 +0000366 }
367 else if (reason == eStopReasonBreakpoint)
368 {
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000369 return_value = NextRangeBreakpointExplainsStop(stop_info_sp);
Jim Ingham9b03fa02015-07-23 19:55:02 +0000370 }
371 else
372 {
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000373 if (log)
374 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
Jim Ingham221d51c2013-05-08 00:35:16 +0000375 return_value = false;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000376 }
377 }
Jim Ingham221d51c2013-05-08 00:35:16 +0000378 else
379 return_value = true;
380
381 return return_value;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000382}
Jim Ingham513c6bb2012-09-01 01:02:41 +0000383
384bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000385ThreadPlanStepOverRange::DoWillResume (lldb::StateType resume_state, bool current_plan)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000386{
387 if (resume_state != eStateSuspended && m_first_resume)
388 {
389 m_first_resume = false;
390 if (resume_state == eStateStepping && current_plan)
391 {
392 // See if we are about to step over an inlined call in the middle of the inlined stack, if so figure
393 // out its extents and reset our range to step over that.
394 bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth();
395 if (in_inlined_stack)
396 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000397 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham513c6bb2012-09-01 01:02:41 +0000398 if (log)
Jim Ingham221d51c2013-05-08 00:35:16 +0000399 log->Printf ("ThreadPlanStepInRange::DoWillResume: adjusting range to the frame at inlined depth %d.",
Jim Ingham513c6bb2012-09-01 01:02:41 +0000400 m_thread.GetCurrentInlinedDepth());
Jason Molendab57e4a12013-11-04 09:33:30 +0000401 StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0);
Jim Ingham513c6bb2012-09-01 01:02:41 +0000402 if (stack_sp)
403 {
404 Block *frame_block = stack_sp->GetFrameBlock();
405 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
406 AddressRange my_range;
407 if (frame_block->GetRangeContainingLoadAddress(curr_pc, m_thread.GetProcess()->GetTarget(), my_range))
408 {
409 m_address_ranges.clear();
410 m_address_ranges.push_back(my_range);
411 if (log)
412 {
413 StreamString s;
414 const InlineFunctionInfo *inline_info = frame_block->GetInlinedFunctionInfo();
415 const char *name;
416 if (inline_info)
Greg Claytonddaf6a72015-07-08 22:32:23 +0000417 name = inline_info->GetName(frame_block->CalculateSymbolContextFunction()->GetLanguage()).AsCString();
Jim Ingham513c6bb2012-09-01 01:02:41 +0000418 else
419 name = "<unknown-notinlined>";
420
421 s.Printf ("Stepping over inlined function \"%s\" in inlined stack: ", name);
422 DumpRanges(&s);
423 log->PutCString(s.GetData());
424 }
425 }
426
427 }
428 }
429 }
430 }
431
Jim Ingham221d51c2013-05-08 00:35:16 +0000432 return true;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000433}