blob: 113e45963eac8540f777ea21b872cb0e954f10d6 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ThreadPlanStepRange.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/ThreadPlanStepRange.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"
Jim Inghamb2cf58a2012-03-09 04:10:47 +000018#include "lldb/Core/Disassembler.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/Log.h"
20#include "lldb/Core/Stream.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Symbol/Function.h"
22#include "lldb/Symbol/Symbol.h"
Jim Inghamb2cf58a2012-03-09 04:10:47 +000023#include "lldb/Target/ExecutionContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000024#include "lldb/Target/Process.h"
25#include "lldb/Target/RegisterContext.h"
26#include "lldb/Target/StopInfo.h"
Jim Inghamb2cf58a2012-03-09 04:10:47 +000027#include "lldb/Target/Target.h"
Greg Clayton643ee732010-08-04 01:40:35 +000028#include "lldb/Target/Thread.h"
Jim Inghamb2cf58a2012-03-09 04:10:47 +000029#include "lldb/Target/ThreadPlanRunToAddress.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030
31using namespace lldb;
32using namespace lldb_private;
33
34
35//----------------------------------------------------------------------
36// ThreadPlanStepRange: Step through a stack range, either stepping over or into
37// based on the value of \a type.
38//----------------------------------------------------------------------
39
Jim Ingham8af1bb72011-02-08 04:27:50 +000040ThreadPlanStepRange::ThreadPlanStepRange (ThreadPlanKind kind,
41 const char *name,
42 Thread &thread,
43 const AddressRange &range,
44 const SymbolContext &addr_context,
45 lldb::RunMode stop_others) :
Jim Ingham5b668b52010-07-10 02:23:31 +000046 ThreadPlan (kind, name, thread, eVoteNoOpinion, eVoteNoOpinion),
Chris Lattner24943d22010-06-08 16:52:24 +000047 m_addr_context (addr_context),
Jim Ingham76e55f72011-10-15 00:24:48 +000048 m_address_ranges (),
Chris Lattner24943d22010-06-08 16:52:24 +000049 m_stop_others (stop_others),
Chris Lattner24943d22010-06-08 16:52:24 +000050 m_stack_id (),
Greg Clayton54e7afa2010-07-09 20:39:50 +000051 m_no_more_plans (false),
Chris Lattner24943d22010-06-08 16:52:24 +000052 m_first_run_event (true)
53{
Jim Ingham76e55f72011-10-15 00:24:48 +000054 AddRange(range);
Chris Lattner24943d22010-06-08 16:52:24 +000055 m_stack_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
56}
57
58ThreadPlanStepRange::~ThreadPlanStepRange ()
59{
Jim Inghamb2cf58a2012-03-09 04:10:47 +000060 ClearNextBranchBreakpoint();
61}
62
63void
64ThreadPlanStepRange::DidPush ()
65{
66 // See if we can find a "next range" breakpoint:
67 SetNextBranchBreakpoint();
Chris Lattner24943d22010-06-08 16:52:24 +000068}
69
70bool
71ThreadPlanStepRange::ValidatePlan (Stream *error)
72{
73 return true;
74}
75
Chris Lattner24943d22010-06-08 16:52:24 +000076Vote
77ThreadPlanStepRange::ShouldReportStop (Event *event_ptr)
78{
Greg Claytone005f2c2010-11-06 01:53:30 +000079 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +000080
81 const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo;
82 if (log)
Greg Clayton590cce32011-01-18 21:44:45 +000083 log->Printf ("ThreadPlanStepRange::ShouldReportStop() returning vote %i\n", vote);
Greg Clayton5205f0b2010-09-03 17:10:42 +000084 return vote;
Chris Lattner24943d22010-06-08 16:52:24 +000085}
86
Jim Ingham76e55f72011-10-15 00:24:48 +000087void
88ThreadPlanStepRange::AddRange(const AddressRange &new_range)
89{
90 // For now I'm just adding the ranges. At some point we may want to
91 // condense the ranges if they overlap, though I don't think it is likely
92 // to be very important.
93 m_address_ranges.push_back (new_range);
Jim Inghamb2cf58a2012-03-09 04:10:47 +000094 m_instruction_ranges.push_back (DisassemblerSP());
Jim Ingham76e55f72011-10-15 00:24:48 +000095}
96
97void
98ThreadPlanStepRange::DumpRanges(Stream *s)
99{
100 size_t num_ranges = m_address_ranges.size();
101 if (num_ranges == 1)
102 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000103 m_address_ranges[0].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
Jim Ingham76e55f72011-10-15 00:24:48 +0000104 }
105 else
106 {
107 for (size_t i = 0; i < num_ranges; i++)
108 {
109 s->PutCString("%d: ");
Greg Claytonf4124de2012-02-21 00:09:25 +0000110 m_address_ranges[i].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
Jim Ingham76e55f72011-10-15 00:24:48 +0000111 }
112 }
113}
114
Chris Lattner24943d22010-06-08 16:52:24 +0000115bool
116ThreadPlanStepRange::InRange ()
117{
Greg Claytone005f2c2010-11-06 01:53:30 +0000118 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000119 bool ret_value = false;
120
121 lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
122
Jim Ingham76e55f72011-10-15 00:24:48 +0000123 size_t num_ranges = m_address_ranges.size();
124 for (size_t i = 0; i < num_ranges; i++)
125 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000126 ret_value = m_address_ranges[i].ContainsLoadAddress(pc_load_addr, m_thread.CalculateTarget().get());
Jim Ingham76e55f72011-10-15 00:24:48 +0000127 if (ret_value)
128 break;
129 }
Chris Lattner24943d22010-06-08 16:52:24 +0000130
131 if (!ret_value)
132 {
133 // See if we've just stepped to another part of the same line number...
134 StackFrame *frame = m_thread.GetStackFrameAtIndex(0).get();
135
136 SymbolContext new_context(frame->GetSymbolContext(eSymbolContextEverything));
137 if (m_addr_context.line_entry.IsValid() && new_context.line_entry.IsValid())
138 {
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000139 if (m_addr_context.line_entry.file == new_context.line_entry.file)
Chris Lattner24943d22010-06-08 16:52:24 +0000140 {
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000141 if (m_addr_context.line_entry.line == new_context.line_entry.line)
Chris Lattner24943d22010-06-08 16:52:24 +0000142 {
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000143 m_addr_context = new_context;
Jim Ingham76e55f72011-10-15 00:24:48 +0000144 AddRange(m_addr_context.line_entry.range);
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000145 ret_value = true;
146 if (log)
147 {
148 StreamString s;
Jim Ingham76e55f72011-10-15 00:24:48 +0000149 m_addr_context.line_entry.range.Dump (&s,
Greg Claytonf4124de2012-02-21 00:09:25 +0000150 m_thread.CalculateTarget().get(),
Jim Ingham76e55f72011-10-15 00:24:48 +0000151 Address::DumpStyleLoadAddress);
Chris Lattner24943d22010-06-08 16:52:24 +0000152
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000153 log->Printf ("Step range plan stepped to another range of same line: %s", s.GetData());
154 }
155 }
Greg Claytonf4124de2012-02-21 00:09:25 +0000156 else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(m_thread.CalculateTarget().get())
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000157 != pc_load_addr)
158 {
159 // Another thing that sometimes happens here is that we step out of one line into the MIDDLE of another
160 // line. So far I mostly see this due to bugs in the debug information.
161 // But we probably don't want to be in the middle of a line range, so in that case reset the stepping
162 // range to the line we've stepped into the middle of and continue.
163 m_addr_context = new_context;
Jim Ingham76e55f72011-10-15 00:24:48 +0000164 m_address_ranges.clear();
165 AddRange(m_addr_context.line_entry.range);
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000166 ret_value = true;
167 if (log)
168 {
169 StreamString s;
Jim Ingham76e55f72011-10-15 00:24:48 +0000170 m_addr_context.line_entry.range.Dump (&s,
Greg Claytonf4124de2012-02-21 00:09:25 +0000171 m_thread.CalculateTarget().get(),
Jim Ingham76e55f72011-10-15 00:24:48 +0000172 Address::DumpStyleLoadAddress);
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000173
174 log->Printf ("Step range plan stepped to the middle of new line(%d): %s, continuing to clear this line.",
175 new_context.line_entry.line,
176 s.GetData());
177 }
178
Chris Lattner24943d22010-06-08 16:52:24 +0000179 }
180 }
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000181
Chris Lattner24943d22010-06-08 16:52:24 +0000182 }
183
184 }
185
186 if (!ret_value && log)
187 log->Printf ("Step range plan out of range to 0x%llx", pc_load_addr);
188
189 return ret_value;
190}
191
192bool
193ThreadPlanStepRange::InSymbol()
194{
195 lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
Chris Lattner24943d22010-06-08 16:52:24 +0000196 if (m_addr_context.function != NULL)
197 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000198 return m_addr_context.function->GetAddressRange().ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
Chris Lattner24943d22010-06-08 16:52:24 +0000199 }
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000200 else if (m_addr_context.symbol)
Chris Lattner24943d22010-06-08 16:52:24 +0000201 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000202 AddressRange range(m_addr_context.symbol->GetAddress(), m_addr_context.symbol->GetByteSize());
203 return range.ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
Chris Lattner24943d22010-06-08 16:52:24 +0000204 }
205 return false;
206}
207
208// FIXME: This should also handle inlining if we aren't going to do inlining in the
209// main stack.
210//
211// Ideally we should remember the whole stack frame list, and then compare that
212// to the current list.
213
Jim Ingham441e3b92012-03-01 00:50:50 +0000214lldb::FrameComparison
215ThreadPlanStepRange::CompareCurrentFrameToStartFrame()
Chris Lattner24943d22010-06-08 16:52:24 +0000216{
Jim Ingham441e3b92012-03-01 00:50:50 +0000217 FrameComparison frame_order;
Jim Ingham0e81b642010-09-16 00:58:09 +0000218
Jim Ingham441e3b92012-03-01 00:50:50 +0000219 StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
Jim Ingham0e81b642010-09-16 00:58:09 +0000220
Jim Ingham441e3b92012-03-01 00:50:50 +0000221 if (cur_frame_id == m_stack_id)
Chris Lattner24943d22010-06-08 16:52:24 +0000222 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000223 frame_order = eFrameCompareEqual;
Chris Lattner24943d22010-06-08 16:52:24 +0000224 }
Jim Ingham441e3b92012-03-01 00:50:50 +0000225 else if (cur_frame_id < m_stack_id)
Chris Lattner24943d22010-06-08 16:52:24 +0000226 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000227 frame_order = eFrameCompareYounger;
Chris Lattner24943d22010-06-08 16:52:24 +0000228 }
229 else
230 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000231 frame_order = eFrameCompareOlder;
Chris Lattner24943d22010-06-08 16:52:24 +0000232 }
Jim Ingham441e3b92012-03-01 00:50:50 +0000233 return frame_order;
Chris Lattner24943d22010-06-08 16:52:24 +0000234}
235
236bool
237ThreadPlanStepRange::StopOthers ()
238{
239 if (m_stop_others == lldb::eOnlyThisThread
240 || m_stop_others == lldb::eOnlyDuringStepping)
241 return true;
242 else
243 return false;
244}
245
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000246InstructionList *
247ThreadPlanStepRange::GetInstructionsForAddress(lldb::addr_t addr, size_t &range_index, size_t &insn_offset)
248{
249 size_t num_ranges = m_address_ranges.size();
250 for (size_t i = 0; i < num_ranges; i++)
251 {
252 if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget()))
253 {
254 // Some joker added a zero size range to the stepping range...
255 if (m_address_ranges[i].GetByteSize() == 0)
256 return NULL;
257
258 if (!m_instruction_ranges[i])
259 {
260 //Disassemble the address range given:
261 ExecutionContext exe_ctx (m_thread.GetProcess());
262 m_instruction_ranges[i] = Disassembler::DisassembleRange(GetTarget().GetArchitecture(),
263 NULL,
264 exe_ctx,
265 m_address_ranges[i]);
266
267 }
268 if (!m_instruction_ranges[i])
269 return NULL;
270 else
271 {
272 // Find where we are in the instruction list as well. If we aren't at an instruction,
273 // return NULL. In this case, we're probably lost, and shouldn't try to do anything fancy.
274
275 insn_offset = m_instruction_ranges[i]->GetInstructionList().GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
276 if (insn_offset == UINT32_MAX)
277 return NULL;
278 else
279 {
280 range_index = i;
281 return &m_instruction_ranges[i]->GetInstructionList();
282 }
283 }
284 }
285 }
286 return NULL;
287}
288
289void
290ThreadPlanStepRange::ClearNextBranchBreakpoint()
291{
292 if (m_next_branch_bp_sp)
293 {
294 GetTarget().RemoveBreakpointByID (m_next_branch_bp_sp->GetID());
295 m_next_branch_bp_sp.reset();
296 }
297}
298
299bool
300ThreadPlanStepRange::SetNextBranchBreakpoint ()
301{
302 // Stepping through ranges using breakpoints doesn't work yet, but with this off we fall back to instruction
303 // single stepping.
304 return false;
305 // Always clear the next branch breakpoint, we don't want to leave one of these stranded.
306 ClearNextBranchBreakpoint();
307 lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC();
308 // Find the current address in our address ranges, and fetch the disassembly if we haven't already:
309 size_t pc_index;
310 size_t range_index;
311 InstructionList *instructions = GetInstructionsForAddress (cur_addr, range_index, pc_index);
312 if (instructions == NULL)
313 return false;
314 else
315 {
316 uint32_t branch_index;
317 branch_index = instructions->GetIndexOfNextBranchInstruction (pc_index);
318
319 Address run_to_address;
320
321 // If we didn't find a branch, run to the end of the range.
322 if (branch_index == UINT32_MAX)
323 {
324 branch_index = instructions->GetSize() - 2;
325 }
326 if (branch_index - pc_index > 1)
327 {
328 const bool is_internal = true;
329 run_to_address = instructions->GetInstructionAtIndex(branch_index)->GetAddress();
330 m_next_branch_bp_sp = GetTarget().CreateBreakpoint(run_to_address, is_internal);
331 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
332 return true;
333 }
334 }
335 return false;
336}
337
338bool
339ThreadPlanStepRange::NextRangeBreakpointExplainsStop (lldb::StopInfoSP stop_info_sp)
340{
341 if (!m_next_branch_bp_sp)
342 return false;
343
344 break_id_t bp_site_id = stop_info_sp->GetValue();
345 BreakpointSiteSP bp_site_sp = m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id);
346 if (!bp_site_sp->IsBreakpointAtThisSite (m_next_branch_bp_sp->GetID()))
347 return false;
348 else
349 return bp_site_sp->GetNumberOfOwners() == 1;
350}
351
352bool
353ThreadPlanStepRange::PlanExplainsStop ()
354{
355 // We always explain a stop. Either we've just done a single step, in which
356 // case we'll do our ordinary processing, or we stopped for some
357 // reason that isn't handled by our sub-plans, in which case we want to just stop right
358 // away.
359
360 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
361 StopInfoSP stop_info_sp = GetPrivateStopReason();
362 if (stop_info_sp)
363 {
364 StopReason reason = stop_info_sp->GetStopReason();
365
366 switch (reason)
367 {
368 case eStopReasonBreakpoint:
369 if (NextRangeBreakpointExplainsStop(stop_info_sp))
370 return true;
Jim Inghame787c7e2012-04-20 21:16:56 +0000371 else
372 return false;
373 break;
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000374 case eStopReasonWatchpoint:
375 case eStopReasonSignal:
376 case eStopReasonException:
377 if (log)
378 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
379 SetPlanComplete();
380 break;
381 default:
382 break;
383 }
384 }
385 return true;
386}
387
Chris Lattner24943d22010-06-08 16:52:24 +0000388bool
389ThreadPlanStepRange::WillStop ()
390{
391 return true;
392}
393
394StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000395ThreadPlanStepRange::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000396{
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000397 if (m_next_branch_bp_sp)
398 return eStateRunning;
399 else
400 return eStateStepping;
Chris Lattner24943d22010-06-08 16:52:24 +0000401}
402
403bool
404ThreadPlanStepRange::MischiefManaged ()
405{
406 bool done = true;
407 if (!IsPlanComplete())
408 {
409 if (InRange())
410 {
411 done = false;
412 }
Jim Ingham441e3b92012-03-01 00:50:50 +0000413 else
Chris Lattner24943d22010-06-08 16:52:24 +0000414 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000415 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
416 if (frame_order != eFrameCompareOlder)
417 {
418 if (m_no_more_plans)
419 done = true;
420 else
421 done = false;
422 }
Chris Lattner24943d22010-06-08 16:52:24 +0000423 else
Jim Ingham441e3b92012-03-01 00:50:50 +0000424 done = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000425 }
Chris Lattner24943d22010-06-08 16:52:24 +0000426 }
427
428 if (done)
429 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000430 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000431 if (log)
432 log->Printf("Completed step through range plan.");
433 ThreadPlan::MischiefManaged ();
434 return true;
435 }
436 else
437 {
438 return false;
439 }
440
441}