blob: 66754d24f89e64d5703c624f051a0db2d8fc9d82 [file] [log] [blame]
Chris Lattner24943d22010-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 Ingham0c8fa2d2012-09-01 01:02:41 +000020#include "lldb/Symbol/Block.h"
Jim Inghamc2a2e132012-11-06 01:14:52 +000021#include "lldb/Symbol/CompileUnit.h"
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000022#include "lldb/Symbol/Function.h"
Jim Inghamc2a2e132012-11-06 01:14:52 +000023#include "lldb/Symbol/LineTable.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Target/Process.h"
25#include "lldb/Target/RegisterContext.h"
Greg Clayton395fc332011-02-15 21:59:32 +000026#include "lldb/Target/Target.h"
Chris Lattner24943d22010-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 Claytone005f2c2010-11-06 01:53:30 +000032using namespace lldb;
Chris Lattner24943d22010-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 Ingham88e3de22012-05-03 21:19:36 +000045 lldb::RunMode stop_others
Chris Lattner24943d22010-06-08 16:52:24 +000046) :
Jim Ingham0c8fa2d2012-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 Lattner24943d22010-06-08 16:52:24 +000049{
Chris Lattner24943d22010-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 Ingham76e55f72011-10-15 00:24:48 +000064 DumpRanges(s);
Chris Lattner24943d22010-06-08 16:52:24 +000065 }
66}
67
68bool
69ThreadPlanStepOverRange::ShouldStop (Event *event_ptr)
70{
Greg Claytone005f2c2010-11-06 01:53:30 +000071 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +000072
73 if (log)
74 {
75 StreamString s;
Greg Clayton395fc332011-02-15 21:59:32 +000076 s.Address (m_thread.GetRegisterContext()->GetPC(),
Greg Claytonf4124de2012-02-21 00:09:25 +000077 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +000078 log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
79 }
80
Chris Lattner24943d22010-06-08 16:52:24 +000081 // If we're out of the range but in the same frame or in our caller's frame
82 // then we should stop.
Jim Ingham23f21942012-10-25 22:30:09 +000083 // When stepping out we only stop others if we are forcing running one thread.
Chris Lattner24943d22010-06-08 16:52:24 +000084 bool stop_others;
85 if (m_stop_others == lldb::eOnlyThisThread)
86 stop_others = true;
87 else
88 stop_others = false;
89
90 ThreadPlan* new_plan = NULL;
Jim Ingham441e3b92012-03-01 00:50:50 +000091
92 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
93
94 if (frame_order == eFrameCompareOlder)
Jim Ingham4f385f12010-11-05 00:18:21 +000095 {
96 // If we're in an older frame then we should stop.
97 //
98 // A caveat to this is if we think the frame is older but we're actually in a trampoline.
99 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are
100 // in a trampoline we think the frame is older because the trampoline confused the backtracer.
101 // As below, we step through first, and then try to figure out how to get back out again.
102
Jim Ingham038fa8e2012-05-10 01:35:39 +0000103 new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham4f385f12010-11-05 00:18:21 +0000104
105 if (new_plan != NULL && log)
106 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
107 }
Jim Ingham441e3b92012-03-01 00:50:50 +0000108 else if (frame_order == eFrameCompareYounger)
Chris Lattner24943d22010-06-08 16:52:24 +0000109 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000110 // Make sure we really are in a new frame. Do that by unwinding and seeing if the
111 // start function really is our start function...
112 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1);
113
114 // But if we can't even unwind one frame we should just get out of here & stop...
115 if (older_frame_sp)
116 {
117 const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
Jim Inghamc3ba2af2012-07-26 18:23:21 +0000118
119 // Match as much as is specified in the m_addr_context:
120 // This is a fairly loose sanity check. Note, sometimes the target doesn't get filled
121 // in so I left out the target check. And sometimes the module comes in as the .o file from the
122 // inlined range, so I left that out too...
123
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000124 bool older_ctx_is_equivalent = true;
Jim Inghamc3ba2af2012-07-26 18:23:21 +0000125 if (m_addr_context.comp_unit)
126 {
127 if (m_addr_context.comp_unit == older_context.comp_unit)
128 {
129 if (m_addr_context.function && m_addr_context.function == older_context.function)
130 {
131 if (m_addr_context.block && m_addr_context.block == older_context.block)
132 {
133 older_ctx_is_equivalent = true;
Jim Inghamc3ba2af2012-07-26 18:23:21 +0000134 }
135 }
136 }
137 }
138 else if (m_addr_context.symbol && m_addr_context.symbol == older_context.symbol)
139 {
140 older_ctx_is_equivalent = true;
141 }
142
143 if (older_ctx_is_equivalent)
Jim Ingham441e3b92012-03-01 00:50:50 +0000144 {
145 new_plan = m_thread.QueueThreadPlanForStepOut (false,
146 NULL,
147 true,
148 stop_others,
149 eVoteNo,
150 eVoteNoOpinion,
151 0);
152 }
153 else
154 {
Jim Ingham038fa8e2012-05-10 01:35:39 +0000155 new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham441e3b92012-03-01 00:50:50 +0000156
157 }
158 }
Chris Lattner24943d22010-06-08 16:52:24 +0000159 }
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000160 else
Chris Lattner24943d22010-06-08 16:52:24 +0000161 {
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000162 // If we're still in the range, keep going.
163 if (InRange())
164 {
165 SetNextBranchBreakpoint();
166 return false;
167 }
168
169
170 if (!InSymbol())
171 {
172 // This one is a little tricky. Sometimes we may be in a stub or something similar,
173 // in which case we need to get out of there. But if we are in a stub then it's
174 // likely going to be hard to get out from here. It is probably easiest to step into the
175 // stub, and then it will be straight-forward to step out.
Jim Ingham038fa8e2012-05-10 01:35:39 +0000176 new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000177 }
Jim Inghamc2a2e132012-11-06 01:14:52 +0000178 else
179 {
180 // The current clang (at least through 424) doesn't always get the address range for the
181 // DW_TAG_inlined_subroutines right, so that when you leave the inlined range the line table says
182 // you are still in the source file of the inlining function. This is bad, because now you are missing
183 // the stack frame for the function containing the inlining, and if you sensibly do "finish" to get
184 // out of this function you will instead exit the containing function.
185 // To work around this, we check whether we are still in the source file we started in, and if not assume
186 // it is an error, and push a plan to get us out of this line and back to the containing file.
187
188 if (m_addr_context.line_entry.IsValid())
189 {
190 SymbolContext sc;
191 StackFrameSP frame_sp = m_thread.GetStackFrameAtIndex(0);
192 sc = frame_sp->GetSymbolContext (eSymbolContextEverything);
193 if (sc.line_entry.IsValid())
194 {
195 if (sc.line_entry.file != m_addr_context.line_entry.file
196 && sc.comp_unit == m_addr_context.comp_unit
197 && sc.function == m_addr_context.function)
198 {
199 // Okay, find the next occurance of this file in the line table:
200 LineTable *line_table = m_addr_context.comp_unit->GetLineTable();
201 if (line_table)
202 {
203 Address cur_address = frame_sp->GetFrameCodeAddress();
204 uint32_t entry_idx;
205 LineEntry line_entry;
206 if (line_table->FindLineEntryByAddress (cur_address, line_entry, &entry_idx))
207 {
208 LineEntry next_line_entry;
209 bool step_past_remaining_inline = false;
210 if (entry_idx > 0)
211 {
212 // We require the the previous line entry and the current line entry come
213 // from the same file.
214 // The other requirement is that the previous line table entry be part of an
215 // inlined block, we don't want to step past cases where people have inlined
216 // some code fragment by using #include <source-fragment.c> directly.
217 LineEntry prev_line_entry;
218 if (line_table->GetLineEntryAtIndex(entry_idx - 1, prev_line_entry)
219 && prev_line_entry.file == line_entry.file)
220 {
221 SymbolContext prev_sc;
222 Address prev_address = prev_line_entry.range.GetBaseAddress();
223 prev_address.CalculateSymbolContext(&prev_sc);
224 if (prev_sc.block)
225 {
226 Block *inlined_block = prev_sc.block->GetContainingInlinedBlock();
227 if (inlined_block)
228 {
229 AddressRange inline_range;
230 inlined_block->GetRangeContainingAddress(prev_address, inline_range);
231 if (!inline_range.ContainsFileAddress(cur_address))
232 {
233
234 step_past_remaining_inline = true;
235 }
236
237 }
238 }
239 }
240 }
241
242 if (step_past_remaining_inline)
243 {
244 uint32_t look_ahead_step = 1;
245 while (line_table->GetLineEntryAtIndex(entry_idx + look_ahead_step, next_line_entry))
246 {
247 // Make sure we haven't wandered out of the function we started from...
248 Address next_line_address = next_line_entry.range.GetBaseAddress();
249 Function *next_line_function = next_line_address.CalculateSymbolContextFunction();
250 if (next_line_function != m_addr_context.function)
251 break;
252
253 if (next_line_entry.file == m_addr_context.line_entry.file)
254 {
255 const bool abort_other_plans = false;
256 const bool stop_other_threads = false;
257 new_plan = m_thread.QueueThreadPlanForRunToAddress(abort_other_plans,
258 next_line_address,
259 stop_other_threads);
260 break;
261 }
262 look_ahead_step++;
263 }
264 }
265 }
266 }
267 }
268 }
269 }
270 }
Chris Lattner24943d22010-06-08 16:52:24 +0000271 }
272
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000273 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
274 ClearNextBranchBreakpoint();
275
Chris Lattner24943d22010-06-08 16:52:24 +0000276 if (new_plan == NULL)
277 m_no_more_plans = true;
278 else
279 m_no_more_plans = false;
280
281 if (new_plan == NULL)
282 {
283 // For efficiencies sake, we know we're done here so we don't have to do this
284 // calculation again in MischiefManaged.
285 SetPlanComplete();
286 return true;
287 }
288 else
289 return false;
290}
Jim Ingham707b7a82012-05-01 18:38:37 +0000291
292bool
293ThreadPlanStepOverRange::PlanExplainsStop ()
294{
295 // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us)
296 // handle the stop. That way the user can see the stop, step around, and then when they
297 // are done, continue and have their step complete. The exception is if we've hit our
298 // "run to next branch" breakpoint.
299 // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an
300 // unexplained breakpoint/crash.
301
302 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
303 StopInfoSP stop_info_sp = GetPrivateStopReason();
304 if (stop_info_sp)
305 {
306 StopReason reason = stop_info_sp->GetStopReason();
307
308 switch (reason)
309 {
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000310 case eStopReasonTrace:
311 return true;
312 break;
Jim Ingham707b7a82012-05-01 18:38:37 +0000313 case eStopReasonBreakpoint:
314 if (NextRangeBreakpointExplainsStop(stop_info_sp))
315 return true;
316 else
317 return false;
318 break;
319 case eStopReasonWatchpoint:
320 case eStopReasonSignal:
321 case eStopReasonException:
Greg Clayton0bce9a22012-12-05 00:16:59 +0000322 case eStopReasonExec:
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000323 default:
Jim Ingham707b7a82012-05-01 18:38:37 +0000324 if (log)
325 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
326 return false;
327 break;
Jim Ingham707b7a82012-05-01 18:38:37 +0000328 }
329 }
330 return true;
331}
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000332
333bool
334ThreadPlanStepOverRange::WillResume (lldb::StateType resume_state, bool current_plan)
335{
336 if (resume_state != eStateSuspended && m_first_resume)
337 {
338 m_first_resume = false;
339 if (resume_state == eStateStepping && current_plan)
340 {
341 // See if we are about to step over an inlined call in the middle of the inlined stack, if so figure
342 // out its extents and reset our range to step over that.
343 bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth();
344 if (in_inlined_stack)
345 {
346 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
347 if (log)
348 log->Printf ("ThreadPlanStepInRange::WillResume: adjusting range to the frame at inlined depth %d.",
349 m_thread.GetCurrentInlinedDepth());
350 StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0);
351 if (stack_sp)
352 {
353 Block *frame_block = stack_sp->GetFrameBlock();
354 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
355 AddressRange my_range;
356 if (frame_block->GetRangeContainingLoadAddress(curr_pc, m_thread.GetProcess()->GetTarget(), my_range))
357 {
358 m_address_ranges.clear();
359 m_address_ranges.push_back(my_range);
360 if (log)
361 {
362 StreamString s;
363 const InlineFunctionInfo *inline_info = frame_block->GetInlinedFunctionInfo();
364 const char *name;
365 if (inline_info)
366 name = inline_info->GetName().AsCString();
367 else
368 name = "<unknown-notinlined>";
369
370 s.Printf ("Stepping over inlined function \"%s\" in inlined stack: ", name);
371 DumpRanges(&s);
372 log->PutCString(s.GetData());
373 }
374 }
375
376 }
377 }
378 }
379 }
380
381 return ThreadPlan::WillResume(resume_state, current_plan);
382}