blob: 0137e8b865d5bdcdd816c5fdeac587637267e97f [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 Ingham69f834f2012-09-10 23:42:44 +0000149 m_addr_context.line_entry.Dump (&s,
150 m_thread.CalculateTarget().get(),
151 true,
152 Address::DumpStyleLoadAddress,
153 Address::DumpStyleLoadAddress,
154 true);
Chris Lattner24943d22010-06-08 16:52:24 +0000155
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000156 log->Printf ("Step range plan stepped to another range of same line: %s", s.GetData());
157 }
158 }
Greg Claytonf4124de2012-02-21 00:09:25 +0000159 else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(m_thread.CalculateTarget().get())
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000160 != pc_load_addr)
161 {
162 // Another thing that sometimes happens here is that we step out of one line into the MIDDLE of another
163 // line. So far I mostly see this due to bugs in the debug information.
164 // But we probably don't want to be in the middle of a line range, so in that case reset the stepping
165 // range to the line we've stepped into the middle of and continue.
166 m_addr_context = new_context;
Jim Ingham76e55f72011-10-15 00:24:48 +0000167 m_address_ranges.clear();
168 AddRange(m_addr_context.line_entry.range);
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000169 ret_value = true;
170 if (log)
171 {
172 StreamString s;
Jim Ingham69f834f2012-09-10 23:42:44 +0000173 m_addr_context.line_entry.Dump (&s,
174 m_thread.CalculateTarget().get(),
175 true,
176 Address::DumpStyleLoadAddress,
177 Address::DumpStyleLoadAddress,
178 true);
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000179
180 log->Printf ("Step range plan stepped to the middle of new line(%d): %s, continuing to clear this line.",
181 new_context.line_entry.line,
182 s.GetData());
183 }
184
Chris Lattner24943d22010-06-08 16:52:24 +0000185 }
186 }
Jim Ingham0f4d0f32011-09-23 21:08:11 +0000187
Chris Lattner24943d22010-06-08 16:52:24 +0000188 }
189
190 }
191
192 if (!ret_value && log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000193 log->Printf ("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000194
195 return ret_value;
196}
197
198bool
199ThreadPlanStepRange::InSymbol()
200{
201 lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
Chris Lattner24943d22010-06-08 16:52:24 +0000202 if (m_addr_context.function != NULL)
203 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000204 return m_addr_context.function->GetAddressRange().ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
Chris Lattner24943d22010-06-08 16:52:24 +0000205 }
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000206 else if (m_addr_context.symbol)
Chris Lattner24943d22010-06-08 16:52:24 +0000207 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000208 AddressRange range(m_addr_context.symbol->GetAddress(), m_addr_context.symbol->GetByteSize());
209 return range.ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
Chris Lattner24943d22010-06-08 16:52:24 +0000210 }
211 return false;
212}
213
214// FIXME: This should also handle inlining if we aren't going to do inlining in the
215// main stack.
216//
217// Ideally we should remember the whole stack frame list, and then compare that
218// to the current list.
219
Jim Ingham441e3b92012-03-01 00:50:50 +0000220lldb::FrameComparison
221ThreadPlanStepRange::CompareCurrentFrameToStartFrame()
Chris Lattner24943d22010-06-08 16:52:24 +0000222{
Jim Ingham441e3b92012-03-01 00:50:50 +0000223 FrameComparison frame_order;
Jim Ingham0e81b642010-09-16 00:58:09 +0000224
Jim Ingham441e3b92012-03-01 00:50:50 +0000225 StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
Jim Ingham0e81b642010-09-16 00:58:09 +0000226
Jim Ingham441e3b92012-03-01 00:50:50 +0000227 if (cur_frame_id == m_stack_id)
Chris Lattner24943d22010-06-08 16:52:24 +0000228 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000229 frame_order = eFrameCompareEqual;
Chris Lattner24943d22010-06-08 16:52:24 +0000230 }
Jim Ingham441e3b92012-03-01 00:50:50 +0000231 else if (cur_frame_id < m_stack_id)
Chris Lattner24943d22010-06-08 16:52:24 +0000232 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000233 frame_order = eFrameCompareYounger;
Chris Lattner24943d22010-06-08 16:52:24 +0000234 }
235 else
236 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000237 frame_order = eFrameCompareOlder;
Chris Lattner24943d22010-06-08 16:52:24 +0000238 }
Jim Ingham441e3b92012-03-01 00:50:50 +0000239 return frame_order;
Chris Lattner24943d22010-06-08 16:52:24 +0000240}
241
242bool
243ThreadPlanStepRange::StopOthers ()
244{
245 if (m_stop_others == lldb::eOnlyThisThread
246 || m_stop_others == lldb::eOnlyDuringStepping)
247 return true;
248 else
249 return false;
250}
251
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000252InstructionList *
253ThreadPlanStepRange::GetInstructionsForAddress(lldb::addr_t addr, size_t &range_index, size_t &insn_offset)
254{
255 size_t num_ranges = m_address_ranges.size();
256 for (size_t i = 0; i < num_ranges; i++)
257 {
258 if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget()))
259 {
260 // Some joker added a zero size range to the stepping range...
261 if (m_address_ranges[i].GetByteSize() == 0)
262 return NULL;
263
264 if (!m_instruction_ranges[i])
265 {
266 //Disassemble the address range given:
267 ExecutionContext exe_ctx (m_thread.GetProcess());
268 m_instruction_ranges[i] = Disassembler::DisassembleRange(GetTarget().GetArchitecture(),
269 NULL,
270 exe_ctx,
271 m_address_ranges[i]);
272
273 }
274 if (!m_instruction_ranges[i])
275 return NULL;
276 else
277 {
278 // Find where we are in the instruction list as well. If we aren't at an instruction,
279 // return NULL. In this case, we're probably lost, and shouldn't try to do anything fancy.
280
281 insn_offset = m_instruction_ranges[i]->GetInstructionList().GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
282 if (insn_offset == UINT32_MAX)
283 return NULL;
284 else
285 {
286 range_index = i;
287 return &m_instruction_ranges[i]->GetInstructionList();
288 }
289 }
290 }
291 }
292 return NULL;
293}
294
295void
296ThreadPlanStepRange::ClearNextBranchBreakpoint()
297{
298 if (m_next_branch_bp_sp)
299 {
300 GetTarget().RemoveBreakpointByID (m_next_branch_bp_sp->GetID());
301 m_next_branch_bp_sp.reset();
302 }
303}
304
305bool
306ThreadPlanStepRange::SetNextBranchBreakpoint ()
307{
308 // Stepping through ranges using breakpoints doesn't work yet, but with this off we fall back to instruction
309 // single stepping.
310 return false;
311 // Always clear the next branch breakpoint, we don't want to leave one of these stranded.
312 ClearNextBranchBreakpoint();
313 lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC();
314 // Find the current address in our address ranges, and fetch the disassembly if we haven't already:
315 size_t pc_index;
316 size_t range_index;
317 InstructionList *instructions = GetInstructionsForAddress (cur_addr, range_index, pc_index);
318 if (instructions == NULL)
319 return false;
320 else
321 {
322 uint32_t branch_index;
323 branch_index = instructions->GetIndexOfNextBranchInstruction (pc_index);
324
325 Address run_to_address;
326
327 // If we didn't find a branch, run to the end of the range.
328 if (branch_index == UINT32_MAX)
329 {
330 branch_index = instructions->GetSize() - 2;
331 }
332 if (branch_index - pc_index > 1)
333 {
334 const bool is_internal = true;
335 run_to_address = instructions->GetInstructionAtIndex(branch_index)->GetAddress();
336 m_next_branch_bp_sp = GetTarget().CreateBreakpoint(run_to_address, is_internal);
Jim Ingham090f8312013-01-26 02:19:28 +0000337 if (m_next_branch_bp_sp)
338 {
339 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
340 m_next_branch_bp_sp->SetBreakpointKind ("next-branch-location");
341 }
342 else
343 return false;
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000344 }
345 }
346 return false;
347}
348
349bool
350ThreadPlanStepRange::NextRangeBreakpointExplainsStop (lldb::StopInfoSP stop_info_sp)
351{
352 if (!m_next_branch_bp_sp)
353 return false;
354
355 break_id_t bp_site_id = stop_info_sp->GetValue();
356 BreakpointSiteSP bp_site_sp = m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id);
357 if (!bp_site_sp->IsBreakpointAtThisSite (m_next_branch_bp_sp->GetID()))
358 return false;
359 else
360 return bp_site_sp->GetNumberOfOwners() == 1;
361}
362
363bool
Chris Lattner24943d22010-06-08 16:52:24 +0000364ThreadPlanStepRange::WillStop ()
365{
366 return true;
367}
368
369StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000370ThreadPlanStepRange::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000371{
Jim Inghamb2cf58a2012-03-09 04:10:47 +0000372 if (m_next_branch_bp_sp)
373 return eStateRunning;
374 else
375 return eStateStepping;
Chris Lattner24943d22010-06-08 16:52:24 +0000376}
377
378bool
379ThreadPlanStepRange::MischiefManaged ()
380{
Jim Ingham69f834f2012-09-10 23:42:44 +0000381 // If we have pushed some plans between ShouldStop & MischiefManaged, then we're not done...
382 // I do this check first because we might have stepped somewhere that will fool InRange into
383 // thinking it needs to step past the end of that line. This happens, for instance, when stepping
384 // over inlined code that is in the middle of the current line.
385
386 if (!m_no_more_plans)
387 return false;
388
Chris Lattner24943d22010-06-08 16:52:24 +0000389 bool done = true;
390 if (!IsPlanComplete())
391 {
392 if (InRange())
393 {
394 done = false;
395 }
Jim Ingham441e3b92012-03-01 00:50:50 +0000396 else
Chris Lattner24943d22010-06-08 16:52:24 +0000397 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000398 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
399 if (frame_order != eFrameCompareOlder)
400 {
401 if (m_no_more_plans)
402 done = true;
403 else
404 done = false;
405 }
Chris Lattner24943d22010-06-08 16:52:24 +0000406 else
Jim Ingham441e3b92012-03-01 00:50:50 +0000407 done = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000408 }
Chris Lattner24943d22010-06-08 16:52:24 +0000409 }
410
411 if (done)
412 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000413 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000414 if (log)
415 log->Printf("Completed step through range plan.");
416 ThreadPlan::MischiefManaged ();
417 return true;
418 }
419 else
420 {
421 return false;
422 }
423
424}
Jim Ingham88e3de22012-05-03 21:19:36 +0000425
426bool
427ThreadPlanStepRange::IsPlanStale ()
428{
429 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
430 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
431
432 if (frame_order == eFrameCompareOlder)
433 {
434 if (log)
435 {
436 log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've stepped out.");
437 }
438 return true;
439 }
440 else if (frame_order == eFrameCompareEqual && InSymbol())
441 {
442 // If we are not in a place we should step through, we've gotten stale.
443 // One tricky bit here is that some stubs don't push a frame, so we should.
444 // check that we are in the same symbol.
445 if (!InRange())
446 {
447 return true;
448 }
449 }
450 return false;
451}