130fdc8d8SChris Lattner //===-- ThreadPlanStepRange.cpp ---------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanStepRange.h"
1130fdc8d8SChris Lattner 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner // Other libraries and framework includes
1530fdc8d8SChris Lattner // Project includes
160c61dee1SJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
170c61dee1SJim Ingham #include "lldb/Breakpoint/BreakpointSite.h"
18564d8bc2SJim Ingham #include "lldb/Core/Disassembler.h"
1930fdc8d8SChris Lattner #include "lldb/Core/Log.h"
2030fdc8d8SChris Lattner #include "lldb/Core/Stream.h"
2130fdc8d8SChris Lattner #include "lldb/Symbol/Function.h"
2230fdc8d8SChris Lattner #include "lldb/Symbol/Symbol.h"
23564d8bc2SJim Ingham #include "lldb/Target/ExecutionContext.h"
24f4b47e15SGreg Clayton #include "lldb/Target/Process.h"
25f4b47e15SGreg Clayton #include "lldb/Target/RegisterContext.h"
26f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
27564d8bc2SJim Ingham #include "lldb/Target/Target.h"
28f4b47e15SGreg Clayton #include "lldb/Target/Thread.h"
29564d8bc2SJim Ingham #include "lldb/Target/ThreadPlanRunToAddress.h"
3030fdc8d8SChris Lattner 
3130fdc8d8SChris Lattner using namespace lldb;
3230fdc8d8SChris Lattner using namespace lldb_private;
3330fdc8d8SChris Lattner 
3430fdc8d8SChris Lattner 
3530fdc8d8SChris Lattner //----------------------------------------------------------------------
3630fdc8d8SChris Lattner // ThreadPlanStepRange: Step through a stack range, either stepping over or into
3730fdc8d8SChris Lattner // based on the value of \a type.
3830fdc8d8SChris Lattner //----------------------------------------------------------------------
3930fdc8d8SChris Lattner 
40242e0ad7SJim Ingham ThreadPlanStepRange::ThreadPlanStepRange (ThreadPlanKind kind,
41242e0ad7SJim Ingham                                           const char *name,
42242e0ad7SJim Ingham                                           Thread &thread,
43242e0ad7SJim Ingham                                           const AddressRange &range,
44242e0ad7SJim Ingham                                           const SymbolContext &addr_context,
452bdbfd50SJim Ingham                                           lldb::RunMode stop_others,
462bdbfd50SJim Ingham                                           bool given_ranges_only) :
4735b21fefSJim Ingham     ThreadPlan (kind, name, thread, eVoteNoOpinion, eVoteNoOpinion),
4830fdc8d8SChris Lattner     m_addr_context (addr_context),
49c4c9fedcSJim Ingham     m_address_ranges (),
5030fdc8d8SChris Lattner     m_stop_others (stop_others),
5130fdc8d8SChris Lattner     m_stack_id (),
52862d1bbdSJim Ingham     m_parent_stack_id(),
53c982c768SGreg Clayton     m_no_more_plans (false),
540c61dee1SJim Ingham     m_first_run_event (true),
552bdbfd50SJim Ingham     m_use_fast_step(false),
562bdbfd50SJim Ingham     m_given_ranges_only (given_ranges_only)
5730fdc8d8SChris Lattner {
5817d023f6SJim Ingham     m_use_fast_step = GetTarget().GetUseFastStepping();
59c4c9fedcSJim Ingham     AddRange(range);
6030fdc8d8SChris Lattner     m_stack_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
6176447851SJim Ingham     StackFrameSP parent_stack = m_thread.GetStackFrameAtIndex(1);
6276447851SJim Ingham     if (parent_stack)
6376447851SJim Ingham       m_parent_stack_id = parent_stack->GetStackID();
6430fdc8d8SChris Lattner }
6530fdc8d8SChris Lattner 
6630fdc8d8SChris Lattner ThreadPlanStepRange::~ThreadPlanStepRange ()
6730fdc8d8SChris Lattner {
68564d8bc2SJim Ingham     ClearNextBranchBreakpoint();
6956d40428SJim Ingham 
7056d40428SJim Ingham     size_t num_instruction_ranges = m_instruction_ranges.size();
7156d40428SJim Ingham 
7256d40428SJim Ingham     // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
7356d40428SJim Ingham     // I'll fix that but for now, just clear the list and it will go away nicely.
7456d40428SJim Ingham     for (size_t i = 0; i < num_instruction_ranges; i++)
7556d40428SJim Ingham     {
7656d40428SJim Ingham         if (m_instruction_ranges[i])
7756d40428SJim Ingham             m_instruction_ranges[i]->GetInstructionList().Clear();
7856d40428SJim Ingham     }
79564d8bc2SJim Ingham }
80564d8bc2SJim Ingham 
81564d8bc2SJim Ingham void
82564d8bc2SJim Ingham ThreadPlanStepRange::DidPush ()
83564d8bc2SJim Ingham {
84564d8bc2SJim Ingham     // See if we can find a "next range" breakpoint:
85564d8bc2SJim Ingham     SetNextBranchBreakpoint();
8630fdc8d8SChris Lattner }
8730fdc8d8SChris Lattner 
8830fdc8d8SChris Lattner bool
8930fdc8d8SChris Lattner ThreadPlanStepRange::ValidatePlan (Stream *error)
9030fdc8d8SChris Lattner {
9130fdc8d8SChris Lattner     return true;
9230fdc8d8SChris Lattner }
9330fdc8d8SChris Lattner 
9430fdc8d8SChris Lattner Vote
9530fdc8d8SChris Lattner ThreadPlanStepRange::ShouldReportStop (Event *event_ptr)
9630fdc8d8SChris Lattner {
975160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
982cad65a5SGreg Clayton 
992cad65a5SGreg Clayton     const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo;
1002cad65a5SGreg Clayton     if (log)
101411c0ce8SGreg Clayton         log->Printf ("ThreadPlanStepRange::ShouldReportStop() returning vote %i\n", vote);
1022cad65a5SGreg Clayton     return vote;
10330fdc8d8SChris Lattner }
10430fdc8d8SChris Lattner 
105c4c9fedcSJim Ingham void
106c4c9fedcSJim Ingham ThreadPlanStepRange::AddRange(const AddressRange &new_range)
107c4c9fedcSJim Ingham {
108c4c9fedcSJim Ingham     // For now I'm just adding the ranges.  At some point we may want to
109c4c9fedcSJim Ingham     // condense the ranges if they overlap, though I don't think it is likely
110c4c9fedcSJim Ingham     // to be very important.
111c4c9fedcSJim Ingham     m_address_ranges.push_back (new_range);
11256d40428SJim Ingham 
11356d40428SJim Ingham     // Fill the slot for this address range with an empty DisassemblerSP in the instruction ranges. I want the
11456d40428SJim Ingham     // indices to match, but I don't want to do the work to disassemble this range if I don't step into it.
115564d8bc2SJim Ingham     m_instruction_ranges.push_back (DisassemblerSP());
116c4c9fedcSJim Ingham }
117c4c9fedcSJim Ingham 
118c4c9fedcSJim Ingham void
119c4c9fedcSJim Ingham ThreadPlanStepRange::DumpRanges(Stream *s)
120c4c9fedcSJim Ingham {
121c4c9fedcSJim Ingham     size_t num_ranges = m_address_ranges.size();
122c4c9fedcSJim Ingham     if (num_ranges == 1)
123c4c9fedcSJim Ingham     {
1241ac04c30SGreg Clayton         m_address_ranges[0].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
125c4c9fedcSJim Ingham     }
126c4c9fedcSJim Ingham     else
127c4c9fedcSJim Ingham     {
128c4c9fedcSJim Ingham         for (size_t i = 0; i < num_ranges; i++)
129c4c9fedcSJim Ingham         {
130c4c9fedcSJim Ingham             s->PutCString("%d: ");
1311ac04c30SGreg Clayton             m_address_ranges[i].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
132c4c9fedcSJim Ingham         }
133c4c9fedcSJim Ingham     }
134c4c9fedcSJim Ingham }
135c4c9fedcSJim Ingham 
13630fdc8d8SChris Lattner bool
13730fdc8d8SChris Lattner ThreadPlanStepRange::InRange ()
13830fdc8d8SChris Lattner {
1395160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
14030fdc8d8SChris Lattner     bool ret_value = false;
14130fdc8d8SChris Lattner 
14230fdc8d8SChris Lattner     lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
14330fdc8d8SChris Lattner 
144c4c9fedcSJim Ingham     size_t num_ranges = m_address_ranges.size();
145c4c9fedcSJim Ingham     for (size_t i = 0; i < num_ranges; i++)
146c4c9fedcSJim Ingham     {
1471ac04c30SGreg Clayton         ret_value = m_address_ranges[i].ContainsLoadAddress(pc_load_addr, m_thread.CalculateTarget().get());
148c4c9fedcSJim Ingham         if (ret_value)
149c4c9fedcSJim Ingham             break;
150c4c9fedcSJim Ingham     }
15130fdc8d8SChris Lattner 
1522bdbfd50SJim Ingham     if (!ret_value && !m_given_ranges_only)
15330fdc8d8SChris Lattner     {
15430fdc8d8SChris Lattner         // See if we've just stepped to another part of the same line number...
155b57e4a1bSJason Molenda         StackFrame *frame = m_thread.GetStackFrameAtIndex(0).get();
15630fdc8d8SChris Lattner 
15730fdc8d8SChris Lattner         SymbolContext new_context(frame->GetSymbolContext(eSymbolContextEverything));
15830fdc8d8SChris Lattner         if (m_addr_context.line_entry.IsValid() && new_context.line_entry.IsValid())
15930fdc8d8SChris Lattner         {
160843bfb2cSJim Ingham             if (m_addr_context.line_entry.file == new_context.line_entry.file)
161843bfb2cSJim Ingham             {
162843bfb2cSJim Ingham                 if (m_addr_context.line_entry.line == new_context.line_entry.line)
16330fdc8d8SChris Lattner                 {
16430fdc8d8SChris Lattner                     m_addr_context = new_context;
165c4c9fedcSJim Ingham                     AddRange(m_addr_context.line_entry.range);
16630fdc8d8SChris Lattner                     ret_value = true;
16730fdc8d8SChris Lattner                     if (log)
16830fdc8d8SChris Lattner                     {
16930fdc8d8SChris Lattner                         StreamString s;
170927bfa3fSJim Ingham                         m_addr_context.line_entry.Dump (&s,
1711ac04c30SGreg Clayton                                                         m_thread.CalculateTarget().get(),
172927bfa3fSJim Ingham                                                         true,
173927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
174927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
175927bfa3fSJim Ingham                                                         true);
17630fdc8d8SChris Lattner 
17730fdc8d8SChris Lattner                         log->Printf ("Step range plan stepped to another range of same line: %s", s.GetData());
17830fdc8d8SChris Lattner                     }
17930fdc8d8SChris Lattner                 }
1802b89a531SJim Ingham                 else if (new_context.line_entry.line == 0)
1812b89a531SJim Ingham                 {
1822b89a531SJim Ingham                     new_context.line_entry.line = m_addr_context.line_entry.line;
1832b89a531SJim Ingham                     m_addr_context = new_context;
1842b89a531SJim Ingham                     AddRange(m_addr_context.line_entry.range);
1852b89a531SJim Ingham                     ret_value = true;
1862b89a531SJim Ingham                     if (log)
1872b89a531SJim Ingham                     {
1882b89a531SJim Ingham                         StreamString s;
1892b89a531SJim Ingham                         m_addr_context.line_entry.Dump (&s,
1902b89a531SJim Ingham                                                         m_thread.CalculateTarget().get(),
1912b89a531SJim Ingham                                                         true,
1922b89a531SJim Ingham                                                         Address::DumpStyleLoadAddress,
1932b89a531SJim Ingham                                                         Address::DumpStyleLoadAddress,
1942b89a531SJim Ingham                                                         true);
1952b89a531SJim Ingham 
1962b89a531SJim Ingham                         log->Printf ("Step range plan stepped to a range at linenumber 0 stepping through that range: %s", s.GetData());
1972b89a531SJim Ingham                     }
1982b89a531SJim Ingham                 }
1991ac04c30SGreg Clayton                 else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(m_thread.CalculateTarget().get())
200843bfb2cSJim Ingham                          != pc_load_addr)
201843bfb2cSJim Ingham                 {
202843bfb2cSJim Ingham                     // Another thing that sometimes happens here is that we step out of one line into the MIDDLE of another
203843bfb2cSJim Ingham                     // line.  So far I mostly see this due to bugs in the debug information.
204843bfb2cSJim Ingham                     // But we probably don't want to be in the middle of a line range, so in that case reset the stepping
205843bfb2cSJim Ingham                     // range to the line we've stepped into the middle of and continue.
206843bfb2cSJim Ingham                     m_addr_context = new_context;
207c4c9fedcSJim Ingham                     m_address_ranges.clear();
208c4c9fedcSJim Ingham                     AddRange(m_addr_context.line_entry.range);
209843bfb2cSJim Ingham                     ret_value = true;
210843bfb2cSJim Ingham                     if (log)
211843bfb2cSJim Ingham                     {
212843bfb2cSJim Ingham                         StreamString s;
213927bfa3fSJim Ingham                         m_addr_context.line_entry.Dump (&s,
2141ac04c30SGreg Clayton                                                         m_thread.CalculateTarget().get(),
215927bfa3fSJim Ingham                                                         true,
216927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
217927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
218927bfa3fSJim Ingham                                                         true);
219843bfb2cSJim Ingham 
220843bfb2cSJim Ingham                         log->Printf ("Step range plan stepped to the middle of new line(%d): %s, continuing to clear this line.",
221843bfb2cSJim Ingham                                      new_context.line_entry.line,
222843bfb2cSJim Ingham                                      s.GetData());
223843bfb2cSJim Ingham                     }
224843bfb2cSJim Ingham 
225843bfb2cSJim Ingham                 }
226843bfb2cSJim Ingham             }
227843bfb2cSJim Ingham 
22830fdc8d8SChris Lattner         }
22930fdc8d8SChris Lattner 
23030fdc8d8SChris Lattner     }
23130fdc8d8SChris Lattner 
23230fdc8d8SChris Lattner     if (!ret_value && log)
233d01b2953SDaniel Malea         log->Printf ("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
23430fdc8d8SChris Lattner 
23530fdc8d8SChris Lattner     return ret_value;
23630fdc8d8SChris Lattner }
23730fdc8d8SChris Lattner 
23830fdc8d8SChris Lattner bool
23930fdc8d8SChris Lattner ThreadPlanStepRange::InSymbol()
24030fdc8d8SChris Lattner {
24130fdc8d8SChris Lattner     lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
24230fdc8d8SChris Lattner     if (m_addr_context.function != NULL)
24330fdc8d8SChris Lattner     {
2441ac04c30SGreg Clayton         return m_addr_context.function->GetAddressRange().ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
24530fdc8d8SChris Lattner     }
246*358cf1eaSGreg Clayton     else if (m_addr_context.symbol && m_addr_context.symbol->ValueIsAddress())
24730fdc8d8SChris Lattner     {
248*358cf1eaSGreg Clayton         AddressRange range(m_addr_context.symbol->GetAddressRef(), m_addr_context.symbol->GetByteSize());
249e7612134SGreg Clayton         return range.ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
25030fdc8d8SChris Lattner     }
25130fdc8d8SChris Lattner     return false;
25230fdc8d8SChris Lattner }
25330fdc8d8SChris Lattner 
25430fdc8d8SChris Lattner // FIXME: This should also handle inlining if we aren't going to do inlining in the
25530fdc8d8SChris Lattner // main stack.
25630fdc8d8SChris Lattner //
25730fdc8d8SChris Lattner // Ideally we should remember the whole stack frame list, and then compare that
25830fdc8d8SChris Lattner // to the current list.
25930fdc8d8SChris Lattner 
260b5c0d1ccSJim Ingham lldb::FrameComparison
261b5c0d1ccSJim Ingham ThreadPlanStepRange::CompareCurrentFrameToStartFrame()
26230fdc8d8SChris Lattner {
263b5c0d1ccSJim Ingham     FrameComparison frame_order;
2647ce490c6SJim Ingham 
265b5c0d1ccSJim Ingham     StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
2667ce490c6SJim Ingham 
267b5c0d1ccSJim Ingham     if (cur_frame_id == m_stack_id)
26830fdc8d8SChris Lattner     {
269b5c0d1ccSJim Ingham         frame_order = eFrameCompareEqual;
27030fdc8d8SChris Lattner     }
271b5c0d1ccSJim Ingham     else if (cur_frame_id < m_stack_id)
27230fdc8d8SChris Lattner     {
273b5c0d1ccSJim Ingham         frame_order = eFrameCompareYounger;
27430fdc8d8SChris Lattner     }
27530fdc8d8SChris Lattner     else
27630fdc8d8SChris Lattner     {
27776447851SJim Ingham         StackFrameSP cur_parent_frame = m_thread.GetStackFrameAtIndex(1);
27876447851SJim Ingham         StackID cur_parent_id;
27976447851SJim Ingham         if (cur_parent_frame)
28076447851SJim Ingham           cur_parent_id = cur_parent_frame->GetStackID();
281862d1bbdSJim Ingham         if (m_parent_stack_id.IsValid()
282862d1bbdSJim Ingham             && cur_parent_id.IsValid()
283862d1bbdSJim Ingham             && m_parent_stack_id == cur_parent_id)
284862d1bbdSJim Ingham            frame_order = eFrameCompareSameParent;
285862d1bbdSJim Ingham         else
286b5c0d1ccSJim Ingham             frame_order = eFrameCompareOlder;
28730fdc8d8SChris Lattner     }
288b5c0d1ccSJim Ingham     return frame_order;
28930fdc8d8SChris Lattner }
29030fdc8d8SChris Lattner 
29130fdc8d8SChris Lattner bool
29230fdc8d8SChris Lattner ThreadPlanStepRange::StopOthers ()
29330fdc8d8SChris Lattner {
29430fdc8d8SChris Lattner     if (m_stop_others == lldb::eOnlyThisThread
29530fdc8d8SChris Lattner         || m_stop_others == lldb::eOnlyDuringStepping)
29630fdc8d8SChris Lattner         return true;
29730fdc8d8SChris Lattner     else
29830fdc8d8SChris Lattner         return false;
29930fdc8d8SChris Lattner }
30030fdc8d8SChris Lattner 
301564d8bc2SJim Ingham InstructionList *
302564d8bc2SJim Ingham ThreadPlanStepRange::GetInstructionsForAddress(lldb::addr_t addr, size_t &range_index, size_t &insn_offset)
303564d8bc2SJim Ingham {
304564d8bc2SJim Ingham     size_t num_ranges = m_address_ranges.size();
305564d8bc2SJim Ingham     for (size_t i = 0; i < num_ranges; i++)
306564d8bc2SJim Ingham     {
307564d8bc2SJim Ingham         if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget()))
308564d8bc2SJim Ingham         {
309564d8bc2SJim Ingham             // Some joker added a zero size range to the stepping range...
310564d8bc2SJim Ingham             if (m_address_ranges[i].GetByteSize() == 0)
311564d8bc2SJim Ingham                 return NULL;
312564d8bc2SJim Ingham 
313564d8bc2SJim Ingham             if (!m_instruction_ranges[i])
314564d8bc2SJim Ingham             {
315564d8bc2SJim Ingham                 //Disassemble the address range given:
316564d8bc2SJim Ingham                 ExecutionContext exe_ctx (m_thread.GetProcess());
3170f063ba6SJim Ingham                 const char *plugin_name = NULL;
3180f063ba6SJim Ingham                 const char *flavor = NULL;
3196b3e6d54SJason Molenda                 const bool prefer_file_cache = true;
320564d8bc2SJim Ingham                 m_instruction_ranges[i] = Disassembler::DisassembleRange(GetTarget().GetArchitecture(),
3210f063ba6SJim Ingham                                                                          plugin_name,
3220f063ba6SJim Ingham                                                                          flavor,
323564d8bc2SJim Ingham                                                                          exe_ctx,
3246b3e6d54SJason Molenda                                                                          m_address_ranges[i],
3256b3e6d54SJason Molenda                                                                          prefer_file_cache);
326564d8bc2SJim Ingham 
327564d8bc2SJim Ingham             }
328564d8bc2SJim Ingham             if (!m_instruction_ranges[i])
329564d8bc2SJim Ingham                 return NULL;
330564d8bc2SJim Ingham             else
331564d8bc2SJim Ingham             {
332564d8bc2SJim Ingham                 // Find where we are in the instruction list as well.  If we aren't at an instruction,
333564d8bc2SJim Ingham                 // return NULL.  In this case, we're probably lost, and shouldn't try to do anything fancy.
334564d8bc2SJim Ingham 
335564d8bc2SJim Ingham                 insn_offset = m_instruction_ranges[i]->GetInstructionList().GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
336564d8bc2SJim Ingham                 if (insn_offset == UINT32_MAX)
337564d8bc2SJim Ingham                     return NULL;
338564d8bc2SJim Ingham                 else
339564d8bc2SJim Ingham                 {
340564d8bc2SJim Ingham                     range_index = i;
341564d8bc2SJim Ingham                     return &m_instruction_ranges[i]->GetInstructionList();
342564d8bc2SJim Ingham                 }
343564d8bc2SJim Ingham             }
344564d8bc2SJim Ingham         }
345564d8bc2SJim Ingham     }
346564d8bc2SJim Ingham     return NULL;
347564d8bc2SJim Ingham }
348564d8bc2SJim Ingham 
349564d8bc2SJim Ingham void
350564d8bc2SJim Ingham ThreadPlanStepRange::ClearNextBranchBreakpoint()
351564d8bc2SJim Ingham {
352564d8bc2SJim Ingham     if (m_next_branch_bp_sp)
353564d8bc2SJim Ingham     {
3545160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
3550c61dee1SJim Ingham         if (log)
3560c61dee1SJim Ingham             log->Printf ("Removing next branch breakpoint: %d.", m_next_branch_bp_sp->GetID());
357564d8bc2SJim Ingham         GetTarget().RemoveBreakpointByID (m_next_branch_bp_sp->GetID());
358564d8bc2SJim Ingham         m_next_branch_bp_sp.reset();
359564d8bc2SJim Ingham     }
360564d8bc2SJim Ingham }
361564d8bc2SJim Ingham 
362564d8bc2SJim Ingham bool
363564d8bc2SJim Ingham ThreadPlanStepRange::SetNextBranchBreakpoint ()
364564d8bc2SJim Ingham {
3650c61dee1SJim Ingham     if (m_next_branch_bp_sp)
3660c61dee1SJim Ingham         return true;
3670c61dee1SJim Ingham 
3685160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
369564d8bc2SJim Ingham     // Stepping through ranges using breakpoints doesn't work yet, but with this off we fall back to instruction
370564d8bc2SJim Ingham     // single stepping.
3710c61dee1SJim Ingham     if (!m_use_fast_step)
372564d8bc2SJim Ingham          return false;
3730c61dee1SJim Ingham 
374564d8bc2SJim Ingham     lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC();
375564d8bc2SJim Ingham     // Find the current address in our address ranges, and fetch the disassembly if we haven't already:
376564d8bc2SJim Ingham     size_t pc_index;
377564d8bc2SJim Ingham     size_t range_index;
378564d8bc2SJim Ingham     InstructionList *instructions = GetInstructionsForAddress (cur_addr, range_index, pc_index);
379564d8bc2SJim Ingham     if (instructions == NULL)
380564d8bc2SJim Ingham         return false;
381564d8bc2SJim Ingham     else
382564d8bc2SJim Ingham     {
383e76e7e93STed Woodward         Target &target = GetThread().GetProcess()->GetTarget();
384564d8bc2SJim Ingham         uint32_t branch_index;
385e76e7e93STed Woodward         branch_index = instructions->GetIndexOfNextBranchInstruction (pc_index, target);
386564d8bc2SJim Ingham 
387564d8bc2SJim Ingham         Address run_to_address;
388564d8bc2SJim Ingham 
389564d8bc2SJim Ingham         // If we didn't find a branch, run to the end of the range.
390564d8bc2SJim Ingham         if (branch_index == UINT32_MAX)
391564d8bc2SJim Ingham         {
3920c61dee1SJim Ingham             branch_index = instructions->GetSize() - 1;
393564d8bc2SJim Ingham         }
3940c61dee1SJim Ingham 
395564d8bc2SJim Ingham         if (branch_index - pc_index > 1)
396564d8bc2SJim Ingham         {
397564d8bc2SJim Ingham             const bool is_internal = true;
398564d8bc2SJim Ingham             run_to_address = instructions->GetInstructionAtIndex(branch_index)->GetAddress();
399eb023e75SGreg Clayton             m_next_branch_bp_sp = GetTarget().CreateBreakpoint(run_to_address, is_internal, false);
4002995077dSJim Ingham             if (m_next_branch_bp_sp)
4012995077dSJim Ingham             {
4020c61dee1SJim Ingham                 if (log)
4030c61dee1SJim Ingham                 {
4040c61dee1SJim Ingham                     lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
4050c61dee1SJim Ingham                     BreakpointLocationSP bp_loc = m_next_branch_bp_sp->GetLocationAtIndex(0);
4060c61dee1SJim Ingham                     if (bp_loc)
4070c61dee1SJim Ingham                     {
4080c61dee1SJim Ingham                         BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
4090c61dee1SJim Ingham                         if (bp_site)
4100c61dee1SJim Ingham                         {
4110c61dee1SJim Ingham                             bp_site_id = bp_site->GetID();
4120c61dee1SJim Ingham                         }
4130c61dee1SJim Ingham                     }
4140c61dee1SJim Ingham                     log->Printf ("ThreadPlanStepRange::SetNextBranchBreakpoint - Setting breakpoint %d (site %d) to run to address 0x%" PRIx64,
4150c61dee1SJim Ingham                                  m_next_branch_bp_sp->GetID(),
4160c61dee1SJim Ingham                                  bp_site_id,
4170c61dee1SJim Ingham                                  run_to_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget()));
4180c61dee1SJim Ingham                 }
419564d8bc2SJim Ingham                 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
4202995077dSJim Ingham                 m_next_branch_bp_sp->SetBreakpointKind ("next-branch-location");
4210c61dee1SJim Ingham                 return true;
4222995077dSJim Ingham             }
4232995077dSJim Ingham             else
4242995077dSJim Ingham                 return false;
425564d8bc2SJim Ingham         }
426564d8bc2SJim Ingham     }
427564d8bc2SJim Ingham     return false;
428564d8bc2SJim Ingham }
429564d8bc2SJim Ingham 
430564d8bc2SJim Ingham bool
431564d8bc2SJim Ingham ThreadPlanStepRange::NextRangeBreakpointExplainsStop (lldb::StopInfoSP stop_info_sp)
432564d8bc2SJim Ingham {
4335160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
434564d8bc2SJim Ingham     if (!m_next_branch_bp_sp)
435564d8bc2SJim Ingham         return false;
436564d8bc2SJim Ingham 
437564d8bc2SJim Ingham     break_id_t bp_site_id = stop_info_sp->GetValue();
438564d8bc2SJim Ingham     BreakpointSiteSP bp_site_sp = m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id);
4390c61dee1SJim Ingham     if (!bp_site_sp)
4400c61dee1SJim Ingham         return false;
4410c61dee1SJim Ingham     else if (!bp_site_sp->IsBreakpointAtThisSite (m_next_branch_bp_sp->GetID()))
442564d8bc2SJim Ingham         return false;
443564d8bc2SJim Ingham     else
4440c61dee1SJim Ingham     {
4450c61dee1SJim Ingham         // If we've hit the next branch breakpoint, then clear it.
4460c61dee1SJim Ingham         size_t num_owners = bp_site_sp->GetNumberOfOwners();
4470c61dee1SJim Ingham         bool explains_stop = true;
4480c61dee1SJim Ingham         // If all the owners are internal, then we are probably just stepping over this range from multiple threads,
4490c61dee1SJim Ingham         // or multiple frames, so we want to continue.  If one is not internal, then we should not explain the stop,
4500c61dee1SJim Ingham         // and let the user breakpoint handle the stop.
4510c61dee1SJim Ingham         for (size_t i = 0; i < num_owners; i++)
4520c61dee1SJim Ingham         {
4530c61dee1SJim Ingham             if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
4540c61dee1SJim Ingham             {
4550c61dee1SJim Ingham                 explains_stop = false;
4560c61dee1SJim Ingham                 break;
4570c61dee1SJim Ingham             }
4580c61dee1SJim Ingham         }
4590c61dee1SJim Ingham         if (log)
46099fbc076SDeepak Panickal             log->Printf ("ThreadPlanStepRange::NextRangeBreakpointExplainsStop - Hit next range breakpoint which has %" PRIu64 " owners - explains stop: %u.",
46199fbc076SDeepak Panickal                         (uint64_t)num_owners,
4620c61dee1SJim Ingham                         explains_stop);
4630c61dee1SJim Ingham         ClearNextBranchBreakpoint();
4640c61dee1SJim Ingham         return  explains_stop;
4650c61dee1SJim Ingham     }
466564d8bc2SJim Ingham }
467564d8bc2SJim Ingham 
468564d8bc2SJim Ingham bool
46930fdc8d8SChris Lattner ThreadPlanStepRange::WillStop ()
47030fdc8d8SChris Lattner {
47130fdc8d8SChris Lattner     return true;
47230fdc8d8SChris Lattner }
47330fdc8d8SChris Lattner 
47430fdc8d8SChris Lattner StateType
47506e827ccSJim Ingham ThreadPlanStepRange::GetPlanRunState ()
47630fdc8d8SChris Lattner {
477564d8bc2SJim Ingham     if (m_next_branch_bp_sp)
478564d8bc2SJim Ingham         return eStateRunning;
479564d8bc2SJim Ingham     else
48030fdc8d8SChris Lattner         return eStateStepping;
48130fdc8d8SChris Lattner }
48230fdc8d8SChris Lattner 
48330fdc8d8SChris Lattner bool
48430fdc8d8SChris Lattner ThreadPlanStepRange::MischiefManaged ()
48530fdc8d8SChris Lattner {
486927bfa3fSJim Ingham     // If we have pushed some plans between ShouldStop & MischiefManaged, then we're not done...
487927bfa3fSJim Ingham     // I do this check first because we might have stepped somewhere that will fool InRange into
488927bfa3fSJim Ingham     // thinking it needs to step past the end of that line.  This happens, for instance, when stepping
489927bfa3fSJim Ingham     // over inlined code that is in the middle of the current line.
490927bfa3fSJim Ingham 
491927bfa3fSJim Ingham     if (!m_no_more_plans)
492927bfa3fSJim Ingham         return false;
493927bfa3fSJim Ingham 
49430fdc8d8SChris Lattner     bool done = true;
49530fdc8d8SChris Lattner     if (!IsPlanComplete())
49630fdc8d8SChris Lattner     {
49730fdc8d8SChris Lattner         if (InRange())
49830fdc8d8SChris Lattner         {
49930fdc8d8SChris Lattner             done = false;
50030fdc8d8SChris Lattner         }
501b5c0d1ccSJim Ingham         else
502b5c0d1ccSJim Ingham         {
503b5c0d1ccSJim Ingham             FrameComparison frame_order = CompareCurrentFrameToStartFrame();
504b5c0d1ccSJim Ingham             if (frame_order != eFrameCompareOlder)
50530fdc8d8SChris Lattner             {
50630fdc8d8SChris Lattner                 if (m_no_more_plans)
50730fdc8d8SChris Lattner                     done = true;
50830fdc8d8SChris Lattner                 else
50930fdc8d8SChris Lattner                     done = false;
51030fdc8d8SChris Lattner             }
51130fdc8d8SChris Lattner             else
51230fdc8d8SChris Lattner                 done = true;
51330fdc8d8SChris Lattner         }
514b5c0d1ccSJim Ingham     }
51530fdc8d8SChris Lattner 
51630fdc8d8SChris Lattner     if (done)
51730fdc8d8SChris Lattner     {
5185160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
51930fdc8d8SChris Lattner         if (log)
52030fdc8d8SChris Lattner             log->Printf("Completed step through range plan.");
5210c61dee1SJim Ingham         ClearNextBranchBreakpoint();
52230fdc8d8SChris Lattner         ThreadPlan::MischiefManaged ();
52330fdc8d8SChris Lattner         return true;
52430fdc8d8SChris Lattner     }
52530fdc8d8SChris Lattner     else
52630fdc8d8SChris Lattner     {
52730fdc8d8SChris Lattner         return false;
52830fdc8d8SChris Lattner     }
52930fdc8d8SChris Lattner 
53030fdc8d8SChris Lattner }
53164e7ead1SJim Ingham 
53264e7ead1SJim Ingham bool
53364e7ead1SJim Ingham ThreadPlanStepRange::IsPlanStale ()
53464e7ead1SJim Ingham {
5355160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
53664e7ead1SJim Ingham     FrameComparison frame_order = CompareCurrentFrameToStartFrame();
53764e7ead1SJim Ingham 
53864e7ead1SJim Ingham     if (frame_order == eFrameCompareOlder)
53964e7ead1SJim Ingham     {
54064e7ead1SJim Ingham         if (log)
54164e7ead1SJim Ingham         {
54264e7ead1SJim Ingham             log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've stepped out.");
54364e7ead1SJim Ingham         }
54464e7ead1SJim Ingham         return true;
54564e7ead1SJim Ingham     }
54664e7ead1SJim Ingham     else if (frame_order == eFrameCompareEqual && InSymbol())
54764e7ead1SJim Ingham     {
54864e7ead1SJim Ingham         // If we are not in a place we should step through, we've gotten stale.
54964e7ead1SJim Ingham         // One tricky bit here is that some stubs don't push a frame, so we should.
55064e7ead1SJim Ingham         // check that we are in the same symbol.
55164e7ead1SJim Ingham         if (!InRange())
55264e7ead1SJim Ingham         {
55364e7ead1SJim Ingham             return true;
55464e7ead1SJim Ingham         }
55564e7ead1SJim Ingham     }
55664e7ead1SJim Ingham     return false;
55764e7ead1SJim Ingham }
558