blob: 9fbca6b03f71c1d0af7a6e3c2a33498b4e9f4d45 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ThreadPlanStepInRange.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/ThreadPlanStepInRange.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 Ingham809ab9b2010-07-10 02:27:39 +000020#include "lldb/Symbol/Symbol.h"
Jim Ingham0e81b642010-09-16 00:58:09 +000021#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"
Jim Ingham809ab9b2010-07-10 02:27:39 +000028#include "lldb/Core/RegularExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
33uint32_t ThreadPlanStepInRange::s_default_flag_values = ThreadPlanShouldStopHere::eAvoidNoDebug;
34
35//----------------------------------------------------------------------
36// ThreadPlanStepInRange: Step through a stack range, either stepping over or into
37// based on the value of \a type.
38//----------------------------------------------------------------------
39
40ThreadPlanStepInRange::ThreadPlanStepInRange
41(
42 Thread &thread,
43 const AddressRange &range,
44 const SymbolContext &addr_context,
45 lldb::RunMode stop_others
46) :
Jim Ingham5a47e8b2010-06-19 04:45:32 +000047 ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
Jim Ingham0e81b642010-09-16 00:58:09 +000048 ThreadPlanShouldStopHere (this, ThreadPlanStepInRange::DefaultShouldStopHereCallback, NULL),
49 m_step_past_prologue (true)
Chris Lattner24943d22010-06-08 16:52:24 +000050{
51 SetFlagsToDefault ();
52}
53
54ThreadPlanStepInRange::~ThreadPlanStepInRange ()
55{
56}
57
58void
59ThreadPlanStepInRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
60{
61 if (level == lldb::eDescriptionLevelBrief)
62 s->Printf("step in");
63 else
64 {
65 s->Printf ("Stepping through range (stepping into functions): ");
Jim Ingham76e55f72011-10-15 00:24:48 +000066 DumpRanges(s);
Chris Lattner24943d22010-06-08 16:52:24 +000067 }
68}
69
70bool
71ThreadPlanStepInRange::ShouldStop (Event *event_ptr)
72{
Greg Claytone005f2c2010-11-06 01:53:30 +000073 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +000074 m_no_more_plans = false;
75
76 if (log)
77 {
78 StreamString s;
Greg Clayton395fc332011-02-15 21:59:32 +000079 s.Address (m_thread.GetRegisterContext()->GetPC(),
Greg Claytonf4124de2012-02-21 00:09:25 +000080 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +000081 log->Printf("ThreadPlanStepInRange reached %s.", s.GetData());
82 }
83
Jim Inghamad382c52011-12-03 01:52:59 +000084 if (IsPlanComplete())
85 return true;
86
Chris Lattner24943d22010-06-08 16:52:24 +000087 ThreadPlan* new_plan = NULL;
88
Jim Ingham81cfc992010-07-14 02:25:06 +000089 // Stepping through should be done stopping other threads in general, since we're setting a breakpoint and
90 // continuing...
91
Chris Lattner24943d22010-06-08 16:52:24 +000092 bool stop_others;
Jim Ingham81cfc992010-07-14 02:25:06 +000093 if (m_stop_others != lldb::eAllThreads)
Chris Lattner24943d22010-06-08 16:52:24 +000094 stop_others = true;
95 else
96 stop_others = false;
97
Jim Ingham441e3b92012-03-01 00:50:50 +000098 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
99
100 if (frame_order == eFrameCompareOlder)
Jim Ingham4f385f12010-11-05 00:18:21 +0000101 {
102 // If we're in an older frame then we should stop.
103 //
104 // A caveat to this is if we think the frame is older but we're actually in a trampoline.
105 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are
106 // in a trampoline we think the frame is older because the trampoline confused the backtracer.
107 new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others);
108 if (new_plan == NULL)
109 return true;
110 else if (log)
111 {
112 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
113 }
114
115 }
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000116 else if (frame_order == eFrameCompareEqual && InSymbol())
Jim Ingham4f385f12010-11-05 00:18:21 +0000117 {
118 // If we are not in a place we should step through, we're done.
119 // One tricky bit here is that some stubs don't push a frame, so we have to check
120 // both the case of a frame that is younger, or the same as this frame.
121 // However, if the frame is the same, and we are still in the symbol we started
122 // in, the we don't need to do this. This first check isn't strictly necessary,
123 // but it is more efficient.
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000124
125 // If we're still in the range, keep going, either by running to the next branch breakpoint, or by
126 // stepping.
127 if (InRange())
128 {
129 SetNextBranchBreakpoint();
130 return false;
131 }
Jim Ingham4f385f12010-11-05 00:18:21 +0000132
133 SetPlanComplete();
134 return true;
135 }
136
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000137 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
138 ClearNextBranchBreakpoint();
139
Jim Ingham4f385f12010-11-05 00:18:21 +0000140 // We may have set the plan up above in the FrameIsOlder section:
141
142 if (new_plan == NULL)
143 new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others);
Jim Ingham17454cf2010-09-14 22:03:00 +0000144
145 if (log)
146 {
147 if (new_plan != NULL)
148 log->Printf ("Found a step through plan: %s", new_plan->GetName());
149 else
150 log->Printf ("No step through plan found.");
151 }
152
Chris Lattner24943d22010-06-08 16:52:24 +0000153 // If not, give the "should_stop" callback a chance to push a plan to get us out of here.
154 // But only do that if we actually have stepped in.
Jim Ingham441e3b92012-03-01 00:50:50 +0000155 if (!new_plan && frame_order == eFrameCompareYounger)
Chris Lattner24943d22010-06-08 16:52:24 +0000156 new_plan = InvokeShouldStopHereCallback();
157
Jim Ingham0e81b642010-09-16 00:58:09 +0000158 // If we've stepped in and we are going to stop here, check to see if we were asked to
159 // run past the prologue, and if so do that.
160
Jim Ingham441e3b92012-03-01 00:50:50 +0000161 if (new_plan == NULL && frame_order == eFrameCompareYounger && m_step_past_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000162 {
Jim Ingham0e81b642010-09-16 00:58:09 +0000163 lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0);
164 if (curr_frame)
165 {
166 size_t bytes_to_skip = 0;
167 lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC();
168 Address func_start_address;
169
170 SymbolContext sc = curr_frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextSymbol);
171
172 if (sc.function)
173 {
174 func_start_address = sc.function->GetAddressRange().GetBaseAddress();
Greg Clayton289afcb2012-02-18 05:35:26 +0000175 if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
Jim Ingham0e81b642010-09-16 00:58:09 +0000176 bytes_to_skip = sc.function->GetPrologueByteSize();
177 }
178 else if (sc.symbol)
179 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000180 func_start_address = sc.symbol->GetAddress();
Greg Clayton289afcb2012-02-18 05:35:26 +0000181 if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
Jim Ingham0e81b642010-09-16 00:58:09 +0000182 bytes_to_skip = sc.symbol->GetPrologueByteSize();
183 }
184
185 if (bytes_to_skip != 0)
186 {
187 func_start_address.Slide (bytes_to_skip);
Caroline Tice926060e2010-10-29 21:48:37 +0000188 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
Jim Ingham0e81b642010-09-16 00:58:09 +0000189 if (log)
190 log->Printf ("Pushing past prologue ");
191
192 new_plan = m_thread.QueueThreadPlanForRunToAddress(false, func_start_address,true);
193 }
194 }
195 }
196
197 if (new_plan == NULL)
198 {
Chris Lattner24943d22010-06-08 16:52:24 +0000199 m_no_more_plans = true;
200 SetPlanComplete();
201 return true;
202 }
203 else
204 {
205 m_no_more_plans = false;
206 return false;
207 }
208}
209
210void
211ThreadPlanStepInRange::SetFlagsToDefault ()
212{
213 GetFlags().Set(ThreadPlanStepInRange::s_default_flag_values);
214}
215
Jim Ingham809ab9b2010-07-10 02:27:39 +0000216void
217ThreadPlanStepInRange::SetAvoidRegexp(const char *name)
218{
219 if (m_avoid_regexp_ap.get() == NULL)
220 m_avoid_regexp_ap.reset (new RegularExpression(name));
221
222 m_avoid_regexp_ap->Compile (name);
223}
224
Chris Lattner24943d22010-06-08 16:52:24 +0000225void
226ThreadPlanStepInRange::SetDefaultFlagValue (uint32_t new_value)
227{
228 // TODO: Should we test this for sanity?
229 ThreadPlanStepInRange::s_default_flag_values = new_value;
230}
231
Jim Ingham809ab9b2010-07-10 02:27:39 +0000232bool
233ThreadPlanStepInRange::FrameMatchesAvoidRegexp ()
234{
235 StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
236
Jim Ingham20594b12010-09-08 03:14:33 +0000237 RegularExpression *avoid_regexp_to_use;
238
239 avoid_regexp_to_use = m_avoid_regexp_ap.get();
240 if (avoid_regexp_to_use == NULL)
241 avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
242
243 if (avoid_regexp_to_use != NULL)
Jim Ingham809ab9b2010-07-10 02:27:39 +0000244 {
245 SymbolContext sc = frame->GetSymbolContext(eSymbolContextSymbol);
246 if (sc.symbol != NULL)
247 {
248 const char *unnamed_symbol = "<UNKNOWN>";
249 const char *sym_name = sc.symbol->GetMangled().GetName().AsCString(unnamed_symbol);
250 if (strcmp (sym_name, unnamed_symbol) != 0)
Jim Ingham20594b12010-09-08 03:14:33 +0000251 return avoid_regexp_to_use->Execute(sym_name);
Jim Ingham809ab9b2010-07-10 02:27:39 +0000252 }
253 }
254 return false;
255}
256
Chris Lattner24943d22010-06-08 16:52:24 +0000257ThreadPlan *
258ThreadPlanStepInRange::DefaultShouldStopHereCallback (ThreadPlan *current_plan, Flags &flags, void *baton)
259{
Jim Ingham809ab9b2010-07-10 02:27:39 +0000260 bool should_step_out = false;
261 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
Greg Claytone005f2c2010-11-06 01:53:30 +0000262 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham809ab9b2010-07-10 02:27:39 +0000263
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000264 if (flags.Test(eAvoidNoDebug))
Chris Lattner24943d22010-06-08 16:52:24 +0000265 {
Chris Lattner24943d22010-06-08 16:52:24 +0000266 if (!frame->HasDebugInformation())
Jim Inghame4b8aeb2010-09-15 00:06:51 +0000267 {
268 if (log)
269 log->Printf ("Stepping out of frame with no debug info");
270
Jim Ingham809ab9b2010-07-10 02:27:39 +0000271 should_step_out = true;
Jim Inghame4b8aeb2010-09-15 00:06:51 +0000272 }
Jim Ingham809ab9b2010-07-10 02:27:39 +0000273 }
274
275 if (!should_step_out)
276 {
277 if (current_plan->GetKind() == eKindStepInRange)
Chris Lattner24943d22010-06-08 16:52:24 +0000278 {
Jim Ingham809ab9b2010-07-10 02:27:39 +0000279 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
280 should_step_out = step_in_range_plan->FrameMatchesAvoidRegexp ();
Chris Lattner24943d22010-06-08 16:52:24 +0000281 }
282 }
Jim Ingham809ab9b2010-07-10 02:27:39 +0000283
284 if (should_step_out)
285 {
286 // FIXME: Make sure the ThreadPlanForStepOut does the right thing with inlined functions.
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000287 return current_plan->GetThread().QueueThreadPlanForStepOut (false,
288 NULL,
289 true,
Jim Ingham809ab9b2010-07-10 02:27:39 +0000290 current_plan->StopOthers(),
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000291 eVoteNo,
292 eVoteNoOpinion,
293 0); // Frame index
Jim Ingham809ab9b2010-07-10 02:27:39 +0000294 }
Chris Lattner24943d22010-06-08 16:52:24 +0000295
296 return NULL;
297}