blob: 2d8108bf9b77812b1253fd287f95fc694134ded7 [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
34
35//----------------------------------------------------------------------
36// ThreadPlanStepOverRange: Step through a stack range, either stepping over or into
37// based on the value of \a type.
38//----------------------------------------------------------------------
39
40ThreadPlanStepOverRange::ThreadPlanStepOverRange
41(
42 Thread &thread,
43 const AddressRange &range,
44 const SymbolContext &addr_context,
Jim Ingham64e7ead2012-05-03 21:19:36 +000045 lldb::RunMode stop_others
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),
48 m_first_resume(true)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049{
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050}
51
52ThreadPlanStepOverRange::~ThreadPlanStepOverRange ()
53{
54}
55
56void
57ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
58{
59 if (level == lldb::eDescriptionLevelBrief)
60 s->Printf("step over");
61 else
62 {
63 s->Printf ("stepping through range (stepping over functions): ");
Jim Inghamc4c9fed2011-10-15 00:24:48 +000064 DumpRanges(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065 }
66}
67
68bool
Daniel Malea4b867282013-09-17 16:35:45 +000069ThreadPlanStepOverRange::IsEquivalentContext(const SymbolContext &context)
70{
71
72 // Match as much as is specified in the m_addr_context:
73 // This is a fairly loose sanity check. Note, sometimes the target doesn't get filled
74 // in so I left out the target check. And sometimes the module comes in as the .o file from the
75 // inlined range, so I left that out too...
76 if (m_addr_context.comp_unit)
77 {
78 if (m_addr_context.comp_unit == context.comp_unit)
79 {
80 if (m_addr_context.function && m_addr_context.function == context.function)
81 {
82 if (m_addr_context.block && m_addr_context.block == context.block)
83 return true;
84 }
85 }
86 }
87 else if (m_addr_context.symbol && m_addr_context.symbol == context.symbol)
88 {
89 return true;
90 }
91 return false;
92}
93
94bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095ThreadPlanStepOverRange::ShouldStop (Event *event_ptr)
96{
Greg Clayton5160ce52013-03-27 23:08:40 +000097 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098
99 if (log)
100 {
101 StreamString s;
Greg Clayton514487e2011-02-15 21:59:32 +0000102 s.Address (m_thread.GetRegisterContext()->GetPC(),
Greg Clayton1ac04c32012-02-21 00:09:25 +0000103 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
105 }
106
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 // If we're out of the range but in the same frame or in our caller's frame
108 // then we should stop.
Jim Ingham4a58e962012-10-25 22:30:09 +0000109 // When stepping out we only stop others if we are forcing running one thread.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 bool stop_others;
111 if (m_stop_others == lldb::eOnlyThisThread)
112 stop_others = true;
113 else
114 stop_others = false;
115
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000116 ThreadPlanSP new_plan_sp;
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000117
118 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
119
120 if (frame_order == eFrameCompareOlder)
Jim Ingham58221732010-11-05 00:18:21 +0000121 {
122 // If we're in an older frame then we should stop.
123 //
124 // A caveat to this is if we think the frame is older but we're actually in a trampoline.
125 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are
126 // in a trampoline we think the frame is older because the trampoline confused the backtracer.
127 // As below, we step through first, and then try to figure out how to get back out again.
128
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000129 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham58221732010-11-05 00:18:21 +0000130
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000131 if (new_plan_sp && log)
Jim Ingham58221732010-11-05 00:18:21 +0000132 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
133 }
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000134 else if (frame_order == eFrameCompareYounger)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 {
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000136 // Make sure we really are in a new frame. Do that by unwinding and seeing if the
137 // start function really is our start function...
Daniel Malea4b867282013-09-17 16:35:45 +0000138 for(uint32_t i = 1;; ++i)
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000139 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000140 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(i);
Daniel Malea4b867282013-09-17 16:35:45 +0000141 if (!older_frame_sp) {
142 // We can't unwind the next frame we should just get out of here & stop...
143 break;
144 }
145
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000146 const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
Daniel Malea4b867282013-09-17 16:35:45 +0000147 if (IsEquivalentContext(older_context))
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000148 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000149 new_plan_sp = m_thread.QueueThreadPlanForStepOut (false,
Daniel Malea4b867282013-09-17 16:35:45 +0000150 NULL,
151 true,
152 stop_others,
153 eVoteNo,
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000154 eVoteNoOpinion,
155 0);
Daniel Malea4b867282013-09-17 16:35:45 +0000156 break;
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000157 }
Daniel Malea4b867282013-09-17 16:35:45 +0000158 else
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000159 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000160 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000161 }
162 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163 }
Jim Ingham564d8bc22012-03-09 04:10:47 +0000164 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 {
Jim Ingham564d8bc22012-03-09 04:10:47 +0000166 // If we're still in the range, keep going.
167 if (InRange())
168 {
169 SetNextBranchBreakpoint();
170 return false;
171 }
172
173
174 if (!InSymbol())
175 {
176 // This one is a little tricky. Sometimes we may be in a stub or something similar,
177 // in which case we need to get out of there. But if we are in a stub then it's
178 // likely going to be hard to get out from here. It is probably easiest to step into the
179 // stub, and then it will be straight-forward to step out.
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000180 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham564d8bc22012-03-09 04:10:47 +0000181 }
Jim Ingham3bfa7532012-11-06 01:14:52 +0000182 else
183 {
184 // The current clang (at least through 424) doesn't always get the address range for the
185 // DW_TAG_inlined_subroutines right, so that when you leave the inlined range the line table says
186 // you are still in the source file of the inlining function. This is bad, because now you are missing
187 // the stack frame for the function containing the inlining, and if you sensibly do "finish" to get
188 // out of this function you will instead exit the containing function.
189 // To work around this, we check whether we are still in the source file we started in, and if not assume
190 // it is an error, and push a plan to get us out of this line and back to the containing file.
191
192 if (m_addr_context.line_entry.IsValid())
193 {
194 SymbolContext sc;
Jason Molendab57e4a12013-11-04 09:33:30 +0000195 StackFrameSP frame_sp = m_thread.GetStackFrameAtIndex(0);
Jim Ingham3bfa7532012-11-06 01:14:52 +0000196 sc = frame_sp->GetSymbolContext (eSymbolContextEverything);
197 if (sc.line_entry.IsValid())
198 {
199 if (sc.line_entry.file != m_addr_context.line_entry.file
200 && sc.comp_unit == m_addr_context.comp_unit
201 && sc.function == m_addr_context.function)
202 {
203 // Okay, find the next occurance of this file in the line table:
204 LineTable *line_table = m_addr_context.comp_unit->GetLineTable();
205 if (line_table)
206 {
207 Address cur_address = frame_sp->GetFrameCodeAddress();
208 uint32_t entry_idx;
209 LineEntry line_entry;
210 if (line_table->FindLineEntryByAddress (cur_address, line_entry, &entry_idx))
211 {
212 LineEntry next_line_entry;
213 bool step_past_remaining_inline = false;
214 if (entry_idx > 0)
215 {
216 // We require the the previous line entry and the current line entry come
217 // from the same file.
218 // The other requirement is that the previous line table entry be part of an
219 // inlined block, we don't want to step past cases where people have inlined
220 // some code fragment by using #include <source-fragment.c> directly.
221 LineEntry prev_line_entry;
222 if (line_table->GetLineEntryAtIndex(entry_idx - 1, prev_line_entry)
223 && prev_line_entry.file == line_entry.file)
224 {
225 SymbolContext prev_sc;
226 Address prev_address = prev_line_entry.range.GetBaseAddress();
227 prev_address.CalculateSymbolContext(&prev_sc);
228 if (prev_sc.block)
229 {
230 Block *inlined_block = prev_sc.block->GetContainingInlinedBlock();
231 if (inlined_block)
232 {
233 AddressRange inline_range;
234 inlined_block->GetRangeContainingAddress(prev_address, inline_range);
235 if (!inline_range.ContainsFileAddress(cur_address))
236 {
237
238 step_past_remaining_inline = true;
239 }
240
241 }
242 }
243 }
244 }
245
246 if (step_past_remaining_inline)
247 {
248 uint32_t look_ahead_step = 1;
249 while (line_table->GetLineEntryAtIndex(entry_idx + look_ahead_step, next_line_entry))
250 {
251 // Make sure we haven't wandered out of the function we started from...
252 Address next_line_address = next_line_entry.range.GetBaseAddress();
253 Function *next_line_function = next_line_address.CalculateSymbolContextFunction();
254 if (next_line_function != m_addr_context.function)
255 break;
256
257 if (next_line_entry.file == m_addr_context.line_entry.file)
258 {
259 const bool abort_other_plans = false;
260 const bool stop_other_threads = false;
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000261 new_plan_sp = m_thread.QueueThreadPlanForRunToAddress(abort_other_plans,
Jim Ingham3bfa7532012-11-06 01:14:52 +0000262 next_line_address,
263 stop_other_threads);
264 break;
265 }
266 look_ahead_step++;
267 }
268 }
269 }
270 }
271 }
272 }
273 }
274 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 }
276
Jim Ingham564d8bc22012-03-09 04:10:47 +0000277 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
278 ClearNextBranchBreakpoint();
279
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000280 if (!new_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 m_no_more_plans = true;
282 else
283 m_no_more_plans = false;
284
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000285 if (!new_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 {
287 // For efficiencies sake, we know we're done here so we don't have to do this
288 // calculation again in MischiefManaged.
289 SetPlanComplete();
290 return true;
291 }
292 else
293 return false;
294}
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000295
296bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000297ThreadPlanStepOverRange::DoPlanExplainsStop (Event *event_ptr)
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000298{
299 // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us)
300 // handle the stop. That way the user can see the stop, step around, and then when they
301 // are done, continue and have their step complete. The exception is if we've hit our
302 // "run to next branch" breakpoint.
303 // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an
304 // unexplained breakpoint/crash.
305
Greg Clayton5160ce52013-03-27 23:08:40 +0000306 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham60c41182013-06-04 01:40:51 +0000307 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
Jim Ingham221d51c2013-05-08 00:35:16 +0000308 bool return_value;
309
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000310 if (stop_info_sp)
311 {
312 StopReason reason = stop_info_sp->GetStopReason();
313
314 switch (reason)
315 {
Jim Ingham513c6bb2012-09-01 01:02:41 +0000316 case eStopReasonTrace:
Jim Ingham221d51c2013-05-08 00:35:16 +0000317 return_value = true;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000318 break;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000319 case eStopReasonBreakpoint:
320 if (NextRangeBreakpointExplainsStop(stop_info_sp))
Jim Ingham221d51c2013-05-08 00:35:16 +0000321 return_value = true;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000322 else
Jim Ingham221d51c2013-05-08 00:35:16 +0000323 return_value = false;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000324 break;
325 case eStopReasonWatchpoint:
326 case eStopReasonSignal:
327 case eStopReasonException:
Greg Clayton90ba8112012-12-05 00:16:59 +0000328 case eStopReasonExec:
Andrew Kaylorf85defa2012-12-20 23:08:03 +0000329 case eStopReasonThreadExiting:
Jim Ingham513c6bb2012-09-01 01:02:41 +0000330 default:
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000331 if (log)
332 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
Jim Ingham221d51c2013-05-08 00:35:16 +0000333 return_value = false;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000334 break;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000335 }
336 }
Jim Ingham221d51c2013-05-08 00:35:16 +0000337 else
338 return_value = true;
339
340 return return_value;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000341}
Jim Ingham513c6bb2012-09-01 01:02:41 +0000342
343bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000344ThreadPlanStepOverRange::DoWillResume (lldb::StateType resume_state, bool current_plan)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000345{
346 if (resume_state != eStateSuspended && m_first_resume)
347 {
348 m_first_resume = false;
349 if (resume_state == eStateStepping && current_plan)
350 {
351 // See if we are about to step over an inlined call in the middle of the inlined stack, if so figure
352 // out its extents and reset our range to step over that.
353 bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth();
354 if (in_inlined_stack)
355 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000356 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham513c6bb2012-09-01 01:02:41 +0000357 if (log)
Jim Ingham221d51c2013-05-08 00:35:16 +0000358 log->Printf ("ThreadPlanStepInRange::DoWillResume: adjusting range to the frame at inlined depth %d.",
Jim Ingham513c6bb2012-09-01 01:02:41 +0000359 m_thread.GetCurrentInlinedDepth());
Jason Molendab57e4a12013-11-04 09:33:30 +0000360 StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0);
Jim Ingham513c6bb2012-09-01 01:02:41 +0000361 if (stack_sp)
362 {
363 Block *frame_block = stack_sp->GetFrameBlock();
364 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
365 AddressRange my_range;
366 if (frame_block->GetRangeContainingLoadAddress(curr_pc, m_thread.GetProcess()->GetTarget(), my_range))
367 {
368 m_address_ranges.clear();
369 m_address_ranges.push_back(my_range);
370 if (log)
371 {
372 StreamString s;
373 const InlineFunctionInfo *inline_info = frame_block->GetInlinedFunctionInfo();
374 const char *name;
375 if (inline_info)
376 name = inline_info->GetName().AsCString();
377 else
378 name = "<unknown-notinlined>";
379
380 s.Printf ("Stepping over inlined function \"%s\" in inlined stack: ", name);
381 DumpRanges(&s);
382 log->PutCString(s.GetData());
383 }
384 }
385
386 }
387 }
388 }
389 }
390
Jim Ingham221d51c2013-05-08 00:35:16 +0000391 return true;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000392}