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
1630fdc8d8SChris Lattner 
1730fdc8d8SChris Lattner #include "lldb/lldb-private-log.h"
180c61dee1SJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
190c61dee1SJim Ingham #include "lldb/Breakpoint/BreakpointSite.h"
20564d8bc2SJim Ingham #include "lldb/Core/Disassembler.h"
2130fdc8d8SChris Lattner #include "lldb/Core/Log.h"
2230fdc8d8SChris Lattner #include "lldb/Core/Stream.h"
2330fdc8d8SChris Lattner #include "lldb/Symbol/Function.h"
2430fdc8d8SChris Lattner #include "lldb/Symbol/Symbol.h"
25564d8bc2SJim Ingham #include "lldb/Target/ExecutionContext.h"
26f4b47e15SGreg Clayton #include "lldb/Target/Process.h"
27f4b47e15SGreg Clayton #include "lldb/Target/RegisterContext.h"
28f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
29564d8bc2SJim Ingham #include "lldb/Target/Target.h"
30f4b47e15SGreg Clayton #include "lldb/Target/Thread.h"
31564d8bc2SJim Ingham #include "lldb/Target/ThreadPlanRunToAddress.h"
3230fdc8d8SChris Lattner 
3330fdc8d8SChris Lattner using namespace lldb;
3430fdc8d8SChris Lattner using namespace lldb_private;
3530fdc8d8SChris Lattner 
3630fdc8d8SChris Lattner 
3730fdc8d8SChris Lattner //----------------------------------------------------------------------
3830fdc8d8SChris Lattner // ThreadPlanStepRange: Step through a stack range, either stepping over or into
3930fdc8d8SChris Lattner // based on the value of \a type.
4030fdc8d8SChris Lattner //----------------------------------------------------------------------
4130fdc8d8SChris Lattner 
42242e0ad7SJim Ingham ThreadPlanStepRange::ThreadPlanStepRange (ThreadPlanKind kind,
43242e0ad7SJim Ingham                                           const char *name,
44242e0ad7SJim Ingham                                           Thread &thread,
45242e0ad7SJim Ingham                                           const AddressRange &range,
46242e0ad7SJim Ingham                                           const SymbolContext &addr_context,
47*2bdbfd50SJim Ingham                                           lldb::RunMode stop_others,
48*2bdbfd50SJim Ingham                                           bool given_ranges_only) :
4935b21fefSJim Ingham     ThreadPlan (kind, name, thread, eVoteNoOpinion, eVoteNoOpinion),
5030fdc8d8SChris Lattner     m_addr_context (addr_context),
51c4c9fedcSJim Ingham     m_address_ranges (),
5230fdc8d8SChris Lattner     m_stop_others (stop_others),
5330fdc8d8SChris Lattner     m_stack_id (),
54862d1bbdSJim Ingham     m_parent_stack_id(),
55c982c768SGreg Clayton     m_no_more_plans (false),
560c61dee1SJim Ingham     m_first_run_event (true),
57*2bdbfd50SJim Ingham     m_use_fast_step(false),
58*2bdbfd50SJim Ingham     m_given_ranges_only (given_ranges_only)
5930fdc8d8SChris Lattner {
6017d023f6SJim Ingham     m_use_fast_step = GetTarget().GetUseFastStepping();
61c4c9fedcSJim Ingham     AddRange(range);
6230fdc8d8SChris Lattner     m_stack_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
6376447851SJim Ingham     StackFrameSP parent_stack = m_thread.GetStackFrameAtIndex(1);
6476447851SJim Ingham     if (parent_stack)
6576447851SJim Ingham       m_parent_stack_id = parent_stack->GetStackID();
6630fdc8d8SChris Lattner }
6730fdc8d8SChris Lattner 
6830fdc8d8SChris Lattner ThreadPlanStepRange::~ThreadPlanStepRange ()
6930fdc8d8SChris Lattner {
70564d8bc2SJim Ingham     ClearNextBranchBreakpoint();
7156d40428SJim Ingham 
7256d40428SJim Ingham     size_t num_instruction_ranges = m_instruction_ranges.size();
7356d40428SJim Ingham 
7456d40428SJim Ingham     // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
7556d40428SJim Ingham     // I'll fix that but for now, just clear the list and it will go away nicely.
7656d40428SJim Ingham     for (size_t i = 0; i < num_instruction_ranges; i++)
7756d40428SJim Ingham     {
7856d40428SJim Ingham         if (m_instruction_ranges[i])
7956d40428SJim Ingham             m_instruction_ranges[i]->GetInstructionList().Clear();
8056d40428SJim Ingham     }
81564d8bc2SJim Ingham }
82564d8bc2SJim Ingham 
83564d8bc2SJim Ingham void
84564d8bc2SJim Ingham ThreadPlanStepRange::DidPush ()
85564d8bc2SJim Ingham {
86564d8bc2SJim Ingham     // See if we can find a "next range" breakpoint:
87564d8bc2SJim Ingham     SetNextBranchBreakpoint();
8830fdc8d8SChris Lattner }
8930fdc8d8SChris Lattner 
9030fdc8d8SChris Lattner bool
9130fdc8d8SChris Lattner ThreadPlanStepRange::ValidatePlan (Stream *error)
9230fdc8d8SChris Lattner {
9330fdc8d8SChris Lattner     return true;
9430fdc8d8SChris Lattner }
9530fdc8d8SChris Lattner 
9630fdc8d8SChris Lattner Vote
9730fdc8d8SChris Lattner ThreadPlanStepRange::ShouldReportStop (Event *event_ptr)
9830fdc8d8SChris Lattner {
995160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1002cad65a5SGreg Clayton 
1012cad65a5SGreg Clayton     const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo;
1022cad65a5SGreg Clayton     if (log)
103411c0ce8SGreg Clayton         log->Printf ("ThreadPlanStepRange::ShouldReportStop() returning vote %i\n", vote);
1042cad65a5SGreg Clayton     return vote;
10530fdc8d8SChris Lattner }
10630fdc8d8SChris Lattner 
107c4c9fedcSJim Ingham void
108c4c9fedcSJim Ingham ThreadPlanStepRange::AddRange(const AddressRange &new_range)
109c4c9fedcSJim Ingham {
110c4c9fedcSJim Ingham     // For now I'm just adding the ranges.  At some point we may want to
111c4c9fedcSJim Ingham     // condense the ranges if they overlap, though I don't think it is likely
112c4c9fedcSJim Ingham     // to be very important.
113c4c9fedcSJim Ingham     m_address_ranges.push_back (new_range);
11456d40428SJim Ingham 
11556d40428SJim Ingham     // Fill the slot for this address range with an empty DisassemblerSP in the instruction ranges. I want the
11656d40428SJim Ingham     // indices to match, but I don't want to do the work to disassemble this range if I don't step into it.
117564d8bc2SJim Ingham     m_instruction_ranges.push_back (DisassemblerSP());
118c4c9fedcSJim Ingham }
119c4c9fedcSJim Ingham 
120c4c9fedcSJim Ingham void
121c4c9fedcSJim Ingham ThreadPlanStepRange::DumpRanges(Stream *s)
122c4c9fedcSJim Ingham {
123c4c9fedcSJim Ingham     size_t num_ranges = m_address_ranges.size();
124c4c9fedcSJim Ingham     if (num_ranges == 1)
125c4c9fedcSJim Ingham     {
1261ac04c30SGreg Clayton         m_address_ranges[0].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
127c4c9fedcSJim Ingham     }
128c4c9fedcSJim Ingham     else
129c4c9fedcSJim Ingham     {
130c4c9fedcSJim Ingham         for (size_t i = 0; i < num_ranges; i++)
131c4c9fedcSJim Ingham         {
132c4c9fedcSJim Ingham             s->PutCString("%d: ");
1331ac04c30SGreg Clayton             m_address_ranges[i].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
134c4c9fedcSJim Ingham         }
135c4c9fedcSJim Ingham     }
136c4c9fedcSJim Ingham }
137c4c9fedcSJim Ingham 
13830fdc8d8SChris Lattner bool
13930fdc8d8SChris Lattner ThreadPlanStepRange::InRange ()
14030fdc8d8SChris Lattner {
1415160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
14230fdc8d8SChris Lattner     bool ret_value = false;
14330fdc8d8SChris Lattner 
14430fdc8d8SChris Lattner     lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
14530fdc8d8SChris Lattner 
146c4c9fedcSJim Ingham     size_t num_ranges = m_address_ranges.size();
147c4c9fedcSJim Ingham     for (size_t i = 0; i < num_ranges; i++)
148c4c9fedcSJim Ingham     {
1491ac04c30SGreg Clayton         ret_value = m_address_ranges[i].ContainsLoadAddress(pc_load_addr, m_thread.CalculateTarget().get());
150c4c9fedcSJim Ingham         if (ret_value)
151c4c9fedcSJim Ingham             break;
152c4c9fedcSJim Ingham     }
15330fdc8d8SChris Lattner 
154*2bdbfd50SJim Ingham     if (!ret_value && !m_given_ranges_only)
15530fdc8d8SChris Lattner     {
15630fdc8d8SChris Lattner         // See if we've just stepped to another part of the same line number...
157b57e4a1bSJason Molenda         StackFrame *frame = m_thread.GetStackFrameAtIndex(0).get();
15830fdc8d8SChris Lattner 
15930fdc8d8SChris Lattner         SymbolContext new_context(frame->GetSymbolContext(eSymbolContextEverything));
16030fdc8d8SChris Lattner         if (m_addr_context.line_entry.IsValid() && new_context.line_entry.IsValid())
16130fdc8d8SChris Lattner         {
162843bfb2cSJim Ingham             if (m_addr_context.line_entry.file == new_context.line_entry.file)
163843bfb2cSJim Ingham             {
164843bfb2cSJim Ingham                 if (m_addr_context.line_entry.line == new_context.line_entry.line)
16530fdc8d8SChris Lattner                 {
16630fdc8d8SChris Lattner                     m_addr_context = new_context;
167c4c9fedcSJim Ingham                     AddRange(m_addr_context.line_entry.range);
16830fdc8d8SChris Lattner                     ret_value = true;
16930fdc8d8SChris Lattner                     if (log)
17030fdc8d8SChris Lattner                     {
17130fdc8d8SChris Lattner                         StreamString s;
172927bfa3fSJim Ingham                         m_addr_context.line_entry.Dump (&s,
1731ac04c30SGreg Clayton                                                         m_thread.CalculateTarget().get(),
174927bfa3fSJim Ingham                                                         true,
175927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
176927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
177927bfa3fSJim Ingham                                                         true);
17830fdc8d8SChris Lattner 
17930fdc8d8SChris Lattner                         log->Printf ("Step range plan stepped to another range of same line: %s", s.GetData());
18030fdc8d8SChris Lattner                     }
18130fdc8d8SChris Lattner                 }
1822b89a531SJim Ingham                 else if (new_context.line_entry.line == 0)
1832b89a531SJim Ingham                 {
1842b89a531SJim Ingham                     new_context.line_entry.line = m_addr_context.line_entry.line;
1852b89a531SJim Ingham                     m_addr_context = new_context;
1862b89a531SJim Ingham                     AddRange(m_addr_context.line_entry.range);
1872b89a531SJim Ingham                     ret_value = true;
1882b89a531SJim Ingham                     if (log)
1892b89a531SJim Ingham                     {
1902b89a531SJim Ingham                         StreamString s;
1912b89a531SJim Ingham                         m_addr_context.line_entry.Dump (&s,
1922b89a531SJim Ingham                                                         m_thread.CalculateTarget().get(),
1932b89a531SJim Ingham                                                         true,
1942b89a531SJim Ingham                                                         Address::DumpStyleLoadAddress,
1952b89a531SJim Ingham                                                         Address::DumpStyleLoadAddress,
1962b89a531SJim Ingham                                                         true);
1972b89a531SJim Ingham 
1982b89a531SJim Ingham                         log->Printf ("Step range plan stepped to a range at linenumber 0 stepping through that range: %s", s.GetData());
1992b89a531SJim Ingham                     }
2002b89a531SJim Ingham                 }
2011ac04c30SGreg Clayton                 else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(m_thread.CalculateTarget().get())
202843bfb2cSJim Ingham                          != pc_load_addr)
203843bfb2cSJim Ingham                 {
204843bfb2cSJim Ingham                     // Another thing that sometimes happens here is that we step out of one line into the MIDDLE of another
205843bfb2cSJim Ingham                     // line.  So far I mostly see this due to bugs in the debug information.
206843bfb2cSJim Ingham                     // But we probably don't want to be in the middle of a line range, so in that case reset the stepping
207843bfb2cSJim Ingham                     // range to the line we've stepped into the middle of and continue.
208843bfb2cSJim Ingham                     m_addr_context = new_context;
209c4c9fedcSJim Ingham                     m_address_ranges.clear();
210c4c9fedcSJim Ingham                     AddRange(m_addr_context.line_entry.range);
211843bfb2cSJim Ingham                     ret_value = true;
212843bfb2cSJim Ingham                     if (log)
213843bfb2cSJim Ingham                     {
214843bfb2cSJim Ingham                         StreamString s;
215927bfa3fSJim Ingham                         m_addr_context.line_entry.Dump (&s,
2161ac04c30SGreg Clayton                                                         m_thread.CalculateTarget().get(),
217927bfa3fSJim Ingham                                                         true,
218927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
219927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
220927bfa3fSJim Ingham                                                         true);
221843bfb2cSJim Ingham 
222843bfb2cSJim Ingham                         log->Printf ("Step range plan stepped to the middle of new line(%d): %s, continuing to clear this line.",
223843bfb2cSJim Ingham                                      new_context.line_entry.line,
224843bfb2cSJim Ingham                                      s.GetData());
225843bfb2cSJim Ingham                     }
226843bfb2cSJim Ingham 
227843bfb2cSJim Ingham                 }
228843bfb2cSJim Ingham             }
229843bfb2cSJim Ingham 
23030fdc8d8SChris Lattner         }
23130fdc8d8SChris Lattner 
23230fdc8d8SChris Lattner     }
23330fdc8d8SChris Lattner 
23430fdc8d8SChris Lattner     if (!ret_value && log)
235d01b2953SDaniel Malea         log->Printf ("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
23630fdc8d8SChris Lattner 
23730fdc8d8SChris Lattner     return ret_value;
23830fdc8d8SChris Lattner }
23930fdc8d8SChris Lattner 
24030fdc8d8SChris Lattner bool
24130fdc8d8SChris Lattner ThreadPlanStepRange::InSymbol()
24230fdc8d8SChris Lattner {
24330fdc8d8SChris Lattner     lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
24430fdc8d8SChris Lattner     if (m_addr_context.function != NULL)
24530fdc8d8SChris Lattner     {
2461ac04c30SGreg Clayton         return m_addr_context.function->GetAddressRange().ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
24730fdc8d8SChris Lattner     }
248e7612134SGreg Clayton     else if (m_addr_context.symbol)
24930fdc8d8SChris Lattner     {
250e7612134SGreg Clayton         AddressRange range(m_addr_context.symbol->GetAddress(), m_addr_context.symbol->GetByteSize());
251e7612134SGreg Clayton         return range.ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
25230fdc8d8SChris Lattner     }
25330fdc8d8SChris Lattner     return false;
25430fdc8d8SChris Lattner }
25530fdc8d8SChris Lattner 
25630fdc8d8SChris Lattner // FIXME: This should also handle inlining if we aren't going to do inlining in the
25730fdc8d8SChris Lattner // main stack.
25830fdc8d8SChris Lattner //
25930fdc8d8SChris Lattner // Ideally we should remember the whole stack frame list, and then compare that
26030fdc8d8SChris Lattner // to the current list.
26130fdc8d8SChris Lattner 
262b5c0d1ccSJim Ingham lldb::FrameComparison
263b5c0d1ccSJim Ingham ThreadPlanStepRange::CompareCurrentFrameToStartFrame()
26430fdc8d8SChris Lattner {
265b5c0d1ccSJim Ingham     FrameComparison frame_order;
2667ce490c6SJim Ingham 
267b5c0d1ccSJim Ingham     StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
2687ce490c6SJim Ingham 
269b5c0d1ccSJim Ingham     if (cur_frame_id == m_stack_id)
27030fdc8d8SChris Lattner     {
271b5c0d1ccSJim Ingham         frame_order = eFrameCompareEqual;
27230fdc8d8SChris Lattner     }
273b5c0d1ccSJim Ingham     else if (cur_frame_id < m_stack_id)
27430fdc8d8SChris Lattner     {
275b5c0d1ccSJim Ingham         frame_order = eFrameCompareYounger;
27630fdc8d8SChris Lattner     }
27730fdc8d8SChris Lattner     else
27830fdc8d8SChris Lattner     {
27976447851SJim Ingham         StackFrameSP cur_parent_frame = m_thread.GetStackFrameAtIndex(1);
28076447851SJim Ingham         StackID cur_parent_id;
28176447851SJim Ingham         if (cur_parent_frame)
28276447851SJim Ingham           cur_parent_id = cur_parent_frame->GetStackID();
283862d1bbdSJim Ingham         if (m_parent_stack_id.IsValid()
284862d1bbdSJim Ingham             && cur_parent_id.IsValid()
285862d1bbdSJim Ingham             && m_parent_stack_id == cur_parent_id)
286862d1bbdSJim Ingham            frame_order = eFrameCompareSameParent;
287862d1bbdSJim Ingham         else
288b5c0d1ccSJim Ingham             frame_order = eFrameCompareOlder;
28930fdc8d8SChris Lattner     }
290b5c0d1ccSJim Ingham     return frame_order;
29130fdc8d8SChris Lattner }
29230fdc8d8SChris Lattner 
29330fdc8d8SChris Lattner bool
29430fdc8d8SChris Lattner ThreadPlanStepRange::StopOthers ()
29530fdc8d8SChris Lattner {
29630fdc8d8SChris Lattner     if (m_stop_others == lldb::eOnlyThisThread
29730fdc8d8SChris Lattner         || m_stop_others == lldb::eOnlyDuringStepping)
29830fdc8d8SChris Lattner         return true;
29930fdc8d8SChris Lattner     else
30030fdc8d8SChris Lattner         return false;
30130fdc8d8SChris Lattner }
30230fdc8d8SChris Lattner 
303564d8bc2SJim Ingham InstructionList *
304564d8bc2SJim Ingham ThreadPlanStepRange::GetInstructionsForAddress(lldb::addr_t addr, size_t &range_index, size_t &insn_offset)
305564d8bc2SJim Ingham {
306564d8bc2SJim Ingham     size_t num_ranges = m_address_ranges.size();
307564d8bc2SJim Ingham     for (size_t i = 0; i < num_ranges; i++)
308564d8bc2SJim Ingham     {
309564d8bc2SJim Ingham         if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget()))
310564d8bc2SJim Ingham         {
311564d8bc2SJim Ingham             // Some joker added a zero size range to the stepping range...
312564d8bc2SJim Ingham             if (m_address_ranges[i].GetByteSize() == 0)
313564d8bc2SJim Ingham                 return NULL;
314564d8bc2SJim Ingham 
315564d8bc2SJim Ingham             if (!m_instruction_ranges[i])
316564d8bc2SJim Ingham             {
317564d8bc2SJim Ingham                 //Disassemble the address range given:
318564d8bc2SJim Ingham                 ExecutionContext exe_ctx (m_thread.GetProcess());
3190f063ba6SJim Ingham                 const char *plugin_name = NULL;
3200f063ba6SJim Ingham                 const char *flavor = NULL;
3216b3e6d54SJason Molenda                 const bool prefer_file_cache = true;
322564d8bc2SJim Ingham                 m_instruction_ranges[i] = Disassembler::DisassembleRange(GetTarget().GetArchitecture(),
3230f063ba6SJim Ingham                                                                          plugin_name,
3240f063ba6SJim Ingham                                                                          flavor,
325564d8bc2SJim Ingham                                                                          exe_ctx,
3266b3e6d54SJason Molenda                                                                          m_address_ranges[i],
3276b3e6d54SJason Molenda                                                                          prefer_file_cache);
328564d8bc2SJim Ingham 
329564d8bc2SJim Ingham             }
330564d8bc2SJim Ingham             if (!m_instruction_ranges[i])
331564d8bc2SJim Ingham                 return NULL;
332564d8bc2SJim Ingham             else
333564d8bc2SJim Ingham             {
334564d8bc2SJim Ingham                 // Find where we are in the instruction list as well.  If we aren't at an instruction,
335564d8bc2SJim Ingham                 // return NULL.  In this case, we're probably lost, and shouldn't try to do anything fancy.
336564d8bc2SJim Ingham 
337564d8bc2SJim Ingham                 insn_offset = m_instruction_ranges[i]->GetInstructionList().GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
338564d8bc2SJim Ingham                 if (insn_offset == UINT32_MAX)
339564d8bc2SJim Ingham                     return NULL;
340564d8bc2SJim Ingham                 else
341564d8bc2SJim Ingham                 {
342564d8bc2SJim Ingham                     range_index = i;
343564d8bc2SJim Ingham                     return &m_instruction_ranges[i]->GetInstructionList();
344564d8bc2SJim Ingham                 }
345564d8bc2SJim Ingham             }
346564d8bc2SJim Ingham         }
347564d8bc2SJim Ingham     }
348564d8bc2SJim Ingham     return NULL;
349564d8bc2SJim Ingham }
350564d8bc2SJim Ingham 
351564d8bc2SJim Ingham void
352564d8bc2SJim Ingham ThreadPlanStepRange::ClearNextBranchBreakpoint()
353564d8bc2SJim Ingham {
354564d8bc2SJim Ingham     if (m_next_branch_bp_sp)
355564d8bc2SJim Ingham     {
3565160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
3570c61dee1SJim Ingham         if (log)
3580c61dee1SJim Ingham             log->Printf ("Removing next branch breakpoint: %d.", m_next_branch_bp_sp->GetID());
359564d8bc2SJim Ingham         GetTarget().RemoveBreakpointByID (m_next_branch_bp_sp->GetID());
360564d8bc2SJim Ingham         m_next_branch_bp_sp.reset();
361564d8bc2SJim Ingham     }
362564d8bc2SJim Ingham }
363564d8bc2SJim Ingham 
364564d8bc2SJim Ingham bool
365564d8bc2SJim Ingham ThreadPlanStepRange::SetNextBranchBreakpoint ()
366564d8bc2SJim Ingham {
3670c61dee1SJim Ingham     if (m_next_branch_bp_sp)
3680c61dee1SJim Ingham         return true;
3690c61dee1SJim Ingham 
3705160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
371564d8bc2SJim Ingham     // Stepping through ranges using breakpoints doesn't work yet, but with this off we fall back to instruction
372564d8bc2SJim Ingham     // single stepping.
3730c61dee1SJim Ingham     if (!m_use_fast_step)
374564d8bc2SJim Ingham          return false;
3750c61dee1SJim Ingham 
376564d8bc2SJim Ingham     lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC();
377564d8bc2SJim Ingham     // Find the current address in our address ranges, and fetch the disassembly if we haven't already:
378564d8bc2SJim Ingham     size_t pc_index;
379564d8bc2SJim Ingham     size_t range_index;
380564d8bc2SJim Ingham     InstructionList *instructions = GetInstructionsForAddress (cur_addr, range_index, pc_index);
381564d8bc2SJim Ingham     if (instructions == NULL)
382564d8bc2SJim Ingham         return false;
383564d8bc2SJim Ingham     else
384564d8bc2SJim Ingham     {
385564d8bc2SJim Ingham         uint32_t branch_index;
386564d8bc2SJim Ingham         branch_index = instructions->GetIndexOfNextBranchInstruction (pc_index);
387564d8bc2SJim Ingham 
388564d8bc2SJim Ingham         Address run_to_address;
389564d8bc2SJim Ingham 
390564d8bc2SJim Ingham         // If we didn't find a branch, run to the end of the range.
391564d8bc2SJim Ingham         if (branch_index == UINT32_MAX)
392564d8bc2SJim Ingham         {
3930c61dee1SJim Ingham             branch_index = instructions->GetSize() - 1;
394564d8bc2SJim Ingham         }
3950c61dee1SJim Ingham 
396564d8bc2SJim Ingham         if (branch_index - pc_index > 1)
397564d8bc2SJim Ingham         {
398564d8bc2SJim Ingham             const bool is_internal = true;
399564d8bc2SJim Ingham             run_to_address = instructions->GetInstructionAtIndex(branch_index)->GetAddress();
400eb023e75SGreg Clayton             m_next_branch_bp_sp = GetTarget().CreateBreakpoint(run_to_address, is_internal, false);
4012995077dSJim Ingham             if (m_next_branch_bp_sp)
4022995077dSJim Ingham             {
4030c61dee1SJim Ingham                 if (log)
4040c61dee1SJim Ingham                 {
4050c61dee1SJim Ingham                     lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
4060c61dee1SJim Ingham                     BreakpointLocationSP bp_loc = m_next_branch_bp_sp->GetLocationAtIndex(0);
4070c61dee1SJim Ingham                     if (bp_loc)
4080c61dee1SJim Ingham                     {
4090c61dee1SJim Ingham                         BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
4100c61dee1SJim Ingham                         if (bp_site)
4110c61dee1SJim Ingham                         {
4120c61dee1SJim Ingham                             bp_site_id = bp_site->GetID();
4130c61dee1SJim Ingham                         }
4140c61dee1SJim Ingham                     }
4150c61dee1SJim Ingham                     log->Printf ("ThreadPlanStepRange::SetNextBranchBreakpoint - Setting breakpoint %d (site %d) to run to address 0x%" PRIx64,
4160c61dee1SJim Ingham                                  m_next_branch_bp_sp->GetID(),
4170c61dee1SJim Ingham                                  bp_site_id,
4180c61dee1SJim Ingham                                  run_to_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget()));
4190c61dee1SJim Ingham                 }
420564d8bc2SJim Ingham                 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
4212995077dSJim Ingham                 m_next_branch_bp_sp->SetBreakpointKind ("next-branch-location");
4220c61dee1SJim Ingham                 return true;
4232995077dSJim Ingham             }
4242995077dSJim Ingham             else
4252995077dSJim Ingham                 return false;
426564d8bc2SJim Ingham         }
427564d8bc2SJim Ingham     }
428564d8bc2SJim Ingham     return false;
429564d8bc2SJim Ingham }
430564d8bc2SJim Ingham 
431564d8bc2SJim Ingham bool
432564d8bc2SJim Ingham ThreadPlanStepRange::NextRangeBreakpointExplainsStop (lldb::StopInfoSP stop_info_sp)
433564d8bc2SJim Ingham {
4345160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
435564d8bc2SJim Ingham     if (!m_next_branch_bp_sp)
436564d8bc2SJim Ingham         return false;
437564d8bc2SJim Ingham 
438564d8bc2SJim Ingham     break_id_t bp_site_id = stop_info_sp->GetValue();
439564d8bc2SJim Ingham     BreakpointSiteSP bp_site_sp = m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id);
4400c61dee1SJim Ingham     if (!bp_site_sp)
4410c61dee1SJim Ingham         return false;
4420c61dee1SJim Ingham     else if (!bp_site_sp->IsBreakpointAtThisSite (m_next_branch_bp_sp->GetID()))
443564d8bc2SJim Ingham         return false;
444564d8bc2SJim Ingham     else
4450c61dee1SJim Ingham     {
4460c61dee1SJim Ingham         // If we've hit the next branch breakpoint, then clear it.
4470c61dee1SJim Ingham         size_t num_owners = bp_site_sp->GetNumberOfOwners();
4480c61dee1SJim Ingham         bool explains_stop = true;
4490c61dee1SJim Ingham         // If all the owners are internal, then we are probably just stepping over this range from multiple threads,
4500c61dee1SJim Ingham         // or multiple frames, so we want to continue.  If one is not internal, then we should not explain the stop,
4510c61dee1SJim Ingham         // and let the user breakpoint handle the stop.
4520c61dee1SJim Ingham         for (size_t i = 0; i < num_owners; i++)
4530c61dee1SJim Ingham         {
4540c61dee1SJim Ingham             if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
4550c61dee1SJim Ingham             {
4560c61dee1SJim Ingham                 explains_stop = false;
4570c61dee1SJim Ingham                 break;
4580c61dee1SJim Ingham             }
4590c61dee1SJim Ingham         }
4600c61dee1SJim Ingham         if (log)
46199fbc076SDeepak Panickal             log->Printf ("ThreadPlanStepRange::NextRangeBreakpointExplainsStop - Hit next range breakpoint which has %" PRIu64 " owners - explains stop: %u.",
46299fbc076SDeepak Panickal                         (uint64_t)num_owners,
4630c61dee1SJim Ingham                         explains_stop);
4640c61dee1SJim Ingham         ClearNextBranchBreakpoint();
4650c61dee1SJim Ingham         return  explains_stop;
4660c61dee1SJim Ingham     }
467564d8bc2SJim Ingham }
468564d8bc2SJim Ingham 
469564d8bc2SJim Ingham bool
47030fdc8d8SChris Lattner ThreadPlanStepRange::WillStop ()
47130fdc8d8SChris Lattner {
47230fdc8d8SChris Lattner     return true;
47330fdc8d8SChris Lattner }
47430fdc8d8SChris Lattner 
47530fdc8d8SChris Lattner StateType
47606e827ccSJim Ingham ThreadPlanStepRange::GetPlanRunState ()
47730fdc8d8SChris Lattner {
478564d8bc2SJim Ingham     if (m_next_branch_bp_sp)
479564d8bc2SJim Ingham         return eStateRunning;
480564d8bc2SJim Ingham     else
48130fdc8d8SChris Lattner         return eStateStepping;
48230fdc8d8SChris Lattner }
48330fdc8d8SChris Lattner 
48430fdc8d8SChris Lattner bool
48530fdc8d8SChris Lattner ThreadPlanStepRange::MischiefManaged ()
48630fdc8d8SChris Lattner {
487927bfa3fSJim Ingham     // If we have pushed some plans between ShouldStop & MischiefManaged, then we're not done...
488927bfa3fSJim Ingham     // I do this check first because we might have stepped somewhere that will fool InRange into
489927bfa3fSJim Ingham     // thinking it needs to step past the end of that line.  This happens, for instance, when stepping
490927bfa3fSJim Ingham     // over inlined code that is in the middle of the current line.
491927bfa3fSJim Ingham 
492927bfa3fSJim Ingham     if (!m_no_more_plans)
493927bfa3fSJim Ingham         return false;
494927bfa3fSJim Ingham 
49530fdc8d8SChris Lattner     bool done = true;
49630fdc8d8SChris Lattner     if (!IsPlanComplete())
49730fdc8d8SChris Lattner     {
49830fdc8d8SChris Lattner         if (InRange())
49930fdc8d8SChris Lattner         {
50030fdc8d8SChris Lattner             done = false;
50130fdc8d8SChris Lattner         }
502b5c0d1ccSJim Ingham         else
503b5c0d1ccSJim Ingham         {
504b5c0d1ccSJim Ingham             FrameComparison frame_order = CompareCurrentFrameToStartFrame();
505b5c0d1ccSJim Ingham             if (frame_order != eFrameCompareOlder)
50630fdc8d8SChris Lattner             {
50730fdc8d8SChris Lattner                 if (m_no_more_plans)
50830fdc8d8SChris Lattner                     done = true;
50930fdc8d8SChris Lattner                 else
51030fdc8d8SChris Lattner                     done = false;
51130fdc8d8SChris Lattner             }
51230fdc8d8SChris Lattner             else
51330fdc8d8SChris Lattner                 done = true;
51430fdc8d8SChris Lattner         }
515b5c0d1ccSJim Ingham     }
51630fdc8d8SChris Lattner 
51730fdc8d8SChris Lattner     if (done)
51830fdc8d8SChris Lattner     {
5195160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
52030fdc8d8SChris Lattner         if (log)
52130fdc8d8SChris Lattner             log->Printf("Completed step through range plan.");
5220c61dee1SJim Ingham         ClearNextBranchBreakpoint();
52330fdc8d8SChris Lattner         ThreadPlan::MischiefManaged ();
52430fdc8d8SChris Lattner         return true;
52530fdc8d8SChris Lattner     }
52630fdc8d8SChris Lattner     else
52730fdc8d8SChris Lattner     {
52830fdc8d8SChris Lattner         return false;
52930fdc8d8SChris Lattner     }
53030fdc8d8SChris Lattner 
53130fdc8d8SChris Lattner }
53264e7ead1SJim Ingham 
53364e7ead1SJim Ingham bool
53464e7ead1SJim Ingham ThreadPlanStepRange::IsPlanStale ()
53564e7ead1SJim Ingham {
5365160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
53764e7ead1SJim Ingham     FrameComparison frame_order = CompareCurrentFrameToStartFrame();
53864e7ead1SJim Ingham 
53964e7ead1SJim Ingham     if (frame_order == eFrameCompareOlder)
54064e7ead1SJim Ingham     {
54164e7ead1SJim Ingham         if (log)
54264e7ead1SJim Ingham         {
54364e7ead1SJim Ingham             log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've stepped out.");
54464e7ead1SJim Ingham         }
54564e7ead1SJim Ingham         return true;
54664e7ead1SJim Ingham     }
54764e7ead1SJim Ingham     else if (frame_order == eFrameCompareEqual && InSymbol())
54864e7ead1SJim Ingham     {
54964e7ead1SJim Ingham         // If we are not in a place we should step through, we've gotten stale.
55064e7ead1SJim Ingham         // One tricky bit here is that some stubs don't push a frame, so we should.
55164e7ead1SJim Ingham         // check that we are in the same symbol.
55264e7ead1SJim Ingham         if (!InRange())
55364e7ead1SJim Ingham         {
55464e7ead1SJim Ingham             return true;
55564e7ead1SJim Ingham         }
55664e7ead1SJim Ingham     }
55764e7ead1SJim Ingham     return false;
55864e7ead1SJim Ingham }
559