blob: 5298762124adbc5d5398b319b133581cf3210ca4 [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"
21#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Target/Process.h"
23#include "lldb/Target/RegisterContext.h"
Greg Clayton395fc332011-02-15 21:59:32 +000024#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Target/Thread.h"
26#include "lldb/Target/ThreadPlanStepOut.h"
27#include "lldb/Target/ThreadPlanStepThrough.h"
28
29using namespace lldb_private;
Greg Claytone005f2c2010-11-06 01:53:30 +000030using namespace lldb;
Chris Lattner24943d22010-06-08 16:52:24 +000031
32
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 Ingham88e3de22012-05-03 21:19:36 +000043 lldb::RunMode stop_others
Chris Lattner24943d22010-06-08 16:52:24 +000044) :
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000045 ThreadPlanStepRange (ThreadPlan::eKindStepOverRange, "Step range stepping over", thread, range, addr_context, stop_others),
46 m_first_resume(true)
Chris Lattner24943d22010-06-08 16:52:24 +000047{
Chris Lattner24943d22010-06-08 16:52:24 +000048}
49
50ThreadPlanStepOverRange::~ThreadPlanStepOverRange ()
51{
52}
53
54void
55ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
56{
57 if (level == lldb::eDescriptionLevelBrief)
58 s->Printf("step over");
59 else
60 {
61 s->Printf ("stepping through range (stepping over functions): ");
Jim Ingham76e55f72011-10-15 00:24:48 +000062 DumpRanges(s);
Chris Lattner24943d22010-06-08 16:52:24 +000063 }
64}
65
66bool
67ThreadPlanStepOverRange::ShouldStop (Event *event_ptr)
68{
Greg Claytone005f2c2010-11-06 01:53:30 +000069 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +000070
71 if (log)
72 {
73 StreamString s;
Greg Clayton395fc332011-02-15 21:59:32 +000074 s.Address (m_thread.GetRegisterContext()->GetPC(),
Greg Claytonf4124de2012-02-21 00:09:25 +000075 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +000076 log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
77 }
78
Chris Lattner24943d22010-06-08 16:52:24 +000079 // If we're out of the range but in the same frame or in our caller's frame
80 // then we should stop.
81 // When stepping out we only step if we are forcing running one thread.
82 bool stop_others;
83 if (m_stop_others == lldb::eOnlyThisThread)
84 stop_others = true;
85 else
86 stop_others = false;
87
88 ThreadPlan* new_plan = NULL;
Jim Ingham441e3b92012-03-01 00:50:50 +000089
90 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
91
92 if (frame_order == eFrameCompareOlder)
Jim Ingham4f385f12010-11-05 00:18:21 +000093 {
94 // If we're in an older frame then we should stop.
95 //
96 // A caveat to this is if we think the frame is older but we're actually in a trampoline.
97 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are
98 // in a trampoline we think the frame is older because the trampoline confused the backtracer.
99 // As below, we step through first, and then try to figure out how to get back out again.
100
Jim Ingham038fa8e2012-05-10 01:35:39 +0000101 new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham4f385f12010-11-05 00:18:21 +0000102
103 if (new_plan != NULL && log)
104 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
105 }
Jim Ingham441e3b92012-03-01 00:50:50 +0000106 else if (frame_order == eFrameCompareYounger)
Chris Lattner24943d22010-06-08 16:52:24 +0000107 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000108 // Make sure we really are in a new frame. Do that by unwinding and seeing if the
109 // start function really is our start function...
110 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1);
111
112 // But if we can't even unwind one frame we should just get out of here & stop...
113 if (older_frame_sp)
114 {
115 const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
Jim Inghamc3ba2af2012-07-26 18:23:21 +0000116
117 // Match as much as is specified in the m_addr_context:
118 // This is a fairly loose sanity check. Note, sometimes the target doesn't get filled
119 // in so I left out the target check. And sometimes the module comes in as the .o file from the
120 // inlined range, so I left that out too...
121
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000122 bool older_ctx_is_equivalent = true;
Jim Inghamc3ba2af2012-07-26 18:23:21 +0000123 if (m_addr_context.comp_unit)
124 {
125 if (m_addr_context.comp_unit == older_context.comp_unit)
126 {
127 if (m_addr_context.function && m_addr_context.function == older_context.function)
128 {
129 if (m_addr_context.block && m_addr_context.block == older_context.block)
130 {
131 older_ctx_is_equivalent = true;
Jim Inghamc3ba2af2012-07-26 18:23:21 +0000132 }
133 }
134 }
135 }
136 else if (m_addr_context.symbol && m_addr_context.symbol == older_context.symbol)
137 {
138 older_ctx_is_equivalent = true;
139 }
140
141 if (older_ctx_is_equivalent)
Jim Ingham441e3b92012-03-01 00:50:50 +0000142 {
143 new_plan = m_thread.QueueThreadPlanForStepOut (false,
144 NULL,
145 true,
146 stop_others,
147 eVoteNo,
148 eVoteNoOpinion,
149 0);
150 }
151 else
152 {
Jim Ingham038fa8e2012-05-10 01:35:39 +0000153 new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Ingham441e3b92012-03-01 00:50:50 +0000154
155 }
156 }
Chris Lattner24943d22010-06-08 16:52:24 +0000157 }
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000158 else
Chris Lattner24943d22010-06-08 16:52:24 +0000159 {
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000160 // If we're still in the range, keep going.
161 if (InRange())
162 {
163 SetNextBranchBreakpoint();
164 return false;
165 }
166
167
168 if (!InSymbol())
169 {
170 // This one is a little tricky. Sometimes we may be in a stub or something similar,
171 // in which case we need to get out of there. But if we are in a stub then it's
172 // likely going to be hard to get out from here. It is probably easiest to step into the
173 // stub, and then it will be straight-forward to step out.
Jim Ingham038fa8e2012-05-10 01:35:39 +0000174 new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000175 }
Chris Lattner24943d22010-06-08 16:52:24 +0000176 }
177
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000178 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
179 ClearNextBranchBreakpoint();
180
Chris Lattner24943d22010-06-08 16:52:24 +0000181 if (new_plan == NULL)
182 m_no_more_plans = true;
183 else
184 m_no_more_plans = false;
185
186 if (new_plan == NULL)
187 {
188 // For efficiencies sake, we know we're done here so we don't have to do this
189 // calculation again in MischiefManaged.
190 SetPlanComplete();
191 return true;
192 }
193 else
194 return false;
195}
Jim Ingham707b7a82012-05-01 18:38:37 +0000196
197bool
198ThreadPlanStepOverRange::PlanExplainsStop ()
199{
200 // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us)
201 // handle the stop. That way the user can see the stop, step around, and then when they
202 // are done, continue and have their step complete. The exception is if we've hit our
203 // "run to next branch" breakpoint.
204 // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an
205 // unexplained breakpoint/crash.
206
207 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
208 StopInfoSP stop_info_sp = GetPrivateStopReason();
209 if (stop_info_sp)
210 {
211 StopReason reason = stop_info_sp->GetStopReason();
212
213 switch (reason)
214 {
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000215 case eStopReasonTrace:
216 return true;
217 break;
Jim Ingham707b7a82012-05-01 18:38:37 +0000218 case eStopReasonBreakpoint:
219 if (NextRangeBreakpointExplainsStop(stop_info_sp))
220 return true;
221 else
222 return false;
223 break;
224 case eStopReasonWatchpoint:
225 case eStopReasonSignal:
226 case eStopReasonException:
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000227 default:
Jim Ingham707b7a82012-05-01 18:38:37 +0000228 if (log)
229 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
230 return false;
231 break;
Jim Ingham707b7a82012-05-01 18:38:37 +0000232 }
233 }
234 return true;
235}
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000236
237bool
238ThreadPlanStepOverRange::WillResume (lldb::StateType resume_state, bool current_plan)
239{
240 if (resume_state != eStateSuspended && m_first_resume)
241 {
242 m_first_resume = false;
243 if (resume_state == eStateStepping && current_plan)
244 {
245 // See if we are about to step over an inlined call in the middle of the inlined stack, if so figure
246 // out its extents and reset our range to step over that.
247 bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth();
248 if (in_inlined_stack)
249 {
250 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
251 if (log)
252 log->Printf ("ThreadPlanStepInRange::WillResume: adjusting range to the frame at inlined depth %d.",
253 m_thread.GetCurrentInlinedDepth());
254 StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0);
255 if (stack_sp)
256 {
257 Block *frame_block = stack_sp->GetFrameBlock();
258 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
259 AddressRange my_range;
260 if (frame_block->GetRangeContainingLoadAddress(curr_pc, m_thread.GetProcess()->GetTarget(), my_range))
261 {
262 m_address_ranges.clear();
263 m_address_ranges.push_back(my_range);
264 if (log)
265 {
266 StreamString s;
267 const InlineFunctionInfo *inline_info = frame_block->GetInlinedFunctionInfo();
268 const char *name;
269 if (inline_info)
270 name = inline_info->GetName().AsCString();
271 else
272 name = "<unknown-notinlined>";
273
274 s.Printf ("Stepping over inlined function \"%s\" in inlined stack: ", name);
275 DumpRanges(&s);
276 log->PutCString(s.GetData());
277 }
278 }
279
280 }
281 }
282 }
283 }
284
285 return ThreadPlan::WillResume(resume_state, current_plan);
286}