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,
47242e0ad7SJim Ingham                                           lldb::RunMode stop_others) :
4835b21fefSJim Ingham     ThreadPlan (kind, name, thread, eVoteNoOpinion, eVoteNoOpinion),
4930fdc8d8SChris Lattner     m_addr_context (addr_context),
50c4c9fedcSJim Ingham     m_address_ranges (),
5130fdc8d8SChris Lattner     m_stop_others (stop_others),
5230fdc8d8SChris Lattner     m_stack_id (),
53c982c768SGreg Clayton     m_no_more_plans (false),
540c61dee1SJim Ingham     m_first_run_event (true),
550c61dee1SJim Ingham     m_use_fast_step(false)
5630fdc8d8SChris Lattner {
5763c5c2a0SJim Ingham     llvm::Triple::ArchType arch_type = GetTarget().GetArchitecture().GetMachine();
5863c5c2a0SJim Ingham     if (arch_type == llvm::Triple::arm || arch_type == llvm::Triple::thumb)
5963c5c2a0SJim Ingham         m_use_fast_step = false;
6063c5c2a0SJim Ingham     else
6117d023f6SJim Ingham         m_use_fast_step = GetTarget().GetUseFastStepping();
62c4c9fedcSJim Ingham     AddRange(range);
6330fdc8d8SChris Lattner     m_stack_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
6430fdc8d8SChris Lattner }
6530fdc8d8SChris Lattner 
6630fdc8d8SChris Lattner ThreadPlanStepRange::~ThreadPlanStepRange ()
6730fdc8d8SChris Lattner {
68564d8bc2SJim Ingham     ClearNextBranchBreakpoint();
69*56d40428SJim Ingham 
70*56d40428SJim Ingham     size_t num_instruction_ranges = m_instruction_ranges.size();
71*56d40428SJim Ingham 
72*56d40428SJim Ingham     // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
73*56d40428SJim Ingham     // I'll fix that but for now, just clear the list and it will go away nicely.
74*56d40428SJim Ingham     for (size_t i = 0; i < num_instruction_ranges; i++)
75*56d40428SJim Ingham     {
76*56d40428SJim Ingham         if (m_instruction_ranges[i])
77*56d40428SJim Ingham             m_instruction_ranges[i]->GetInstructionList().Clear();
78*56d40428SJim 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);
112*56d40428SJim Ingham 
113*56d40428SJim Ingham     // Fill the slot for this address range with an empty DisassemblerSP in the instruction ranges. I want the
114*56d40428SJim 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 
15230fdc8d8SChris Lattner     if (!ret_value)
15330fdc8d8SChris Lattner     {
15430fdc8d8SChris Lattner         // See if we've just stepped to another part of the same line number...
15530fdc8d8SChris Lattner         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                 }
1801ac04c30SGreg Clayton                 else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(m_thread.CalculateTarget().get())
181843bfb2cSJim Ingham                          != pc_load_addr)
182843bfb2cSJim Ingham                 {
183843bfb2cSJim Ingham                     // Another thing that sometimes happens here is that we step out of one line into the MIDDLE of another
184843bfb2cSJim Ingham                     // line.  So far I mostly see this due to bugs in the debug information.
185843bfb2cSJim Ingham                     // But we probably don't want to be in the middle of a line range, so in that case reset the stepping
186843bfb2cSJim Ingham                     // range to the line we've stepped into the middle of and continue.
187843bfb2cSJim Ingham                     m_addr_context = new_context;
188c4c9fedcSJim Ingham                     m_address_ranges.clear();
189c4c9fedcSJim Ingham                     AddRange(m_addr_context.line_entry.range);
190843bfb2cSJim Ingham                     ret_value = true;
191843bfb2cSJim Ingham                     if (log)
192843bfb2cSJim Ingham                     {
193843bfb2cSJim Ingham                         StreamString s;
194927bfa3fSJim Ingham                         m_addr_context.line_entry.Dump (&s,
1951ac04c30SGreg Clayton                                                         m_thread.CalculateTarget().get(),
196927bfa3fSJim Ingham                                                         true,
197927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
198927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
199927bfa3fSJim Ingham                                                         true);
200843bfb2cSJim Ingham 
201843bfb2cSJim Ingham                         log->Printf ("Step range plan stepped to the middle of new line(%d): %s, continuing to clear this line.",
202843bfb2cSJim Ingham                                      new_context.line_entry.line,
203843bfb2cSJim Ingham                                      s.GetData());
204843bfb2cSJim Ingham                     }
205843bfb2cSJim Ingham 
206843bfb2cSJim Ingham                 }
207843bfb2cSJim Ingham             }
208843bfb2cSJim Ingham 
20930fdc8d8SChris Lattner         }
21030fdc8d8SChris Lattner 
21130fdc8d8SChris Lattner     }
21230fdc8d8SChris Lattner 
21330fdc8d8SChris Lattner     if (!ret_value && log)
214d01b2953SDaniel Malea         log->Printf ("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
21530fdc8d8SChris Lattner 
21630fdc8d8SChris Lattner     return ret_value;
21730fdc8d8SChris Lattner }
21830fdc8d8SChris Lattner 
21930fdc8d8SChris Lattner bool
22030fdc8d8SChris Lattner ThreadPlanStepRange::InSymbol()
22130fdc8d8SChris Lattner {
22230fdc8d8SChris Lattner     lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
22330fdc8d8SChris Lattner     if (m_addr_context.function != NULL)
22430fdc8d8SChris Lattner     {
2251ac04c30SGreg Clayton         return m_addr_context.function->GetAddressRange().ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
22630fdc8d8SChris Lattner     }
227e7612134SGreg Clayton     else if (m_addr_context.symbol)
22830fdc8d8SChris Lattner     {
229e7612134SGreg Clayton         AddressRange range(m_addr_context.symbol->GetAddress(), m_addr_context.symbol->GetByteSize());
230e7612134SGreg Clayton         return range.ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
23130fdc8d8SChris Lattner     }
23230fdc8d8SChris Lattner     return false;
23330fdc8d8SChris Lattner }
23430fdc8d8SChris Lattner 
23530fdc8d8SChris Lattner // FIXME: This should also handle inlining if we aren't going to do inlining in the
23630fdc8d8SChris Lattner // main stack.
23730fdc8d8SChris Lattner //
23830fdc8d8SChris Lattner // Ideally we should remember the whole stack frame list, and then compare that
23930fdc8d8SChris Lattner // to the current list.
24030fdc8d8SChris Lattner 
241b5c0d1ccSJim Ingham lldb::FrameComparison
242b5c0d1ccSJim Ingham ThreadPlanStepRange::CompareCurrentFrameToStartFrame()
24330fdc8d8SChris Lattner {
244b5c0d1ccSJim Ingham     FrameComparison frame_order;
2457ce490c6SJim Ingham 
246b5c0d1ccSJim Ingham     StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
2477ce490c6SJim Ingham 
248b5c0d1ccSJim Ingham     if (cur_frame_id == m_stack_id)
24930fdc8d8SChris Lattner     {
250b5c0d1ccSJim Ingham         frame_order = eFrameCompareEqual;
25130fdc8d8SChris Lattner     }
252b5c0d1ccSJim Ingham     else if (cur_frame_id < m_stack_id)
25330fdc8d8SChris Lattner     {
254b5c0d1ccSJim Ingham         frame_order = eFrameCompareYounger;
25530fdc8d8SChris Lattner     }
25630fdc8d8SChris Lattner     else
25730fdc8d8SChris Lattner     {
258b5c0d1ccSJim Ingham         frame_order = eFrameCompareOlder;
25930fdc8d8SChris Lattner     }
260b5c0d1ccSJim Ingham     return frame_order;
26130fdc8d8SChris Lattner }
26230fdc8d8SChris Lattner 
26330fdc8d8SChris Lattner bool
26430fdc8d8SChris Lattner ThreadPlanStepRange::StopOthers ()
26530fdc8d8SChris Lattner {
26630fdc8d8SChris Lattner     if (m_stop_others == lldb::eOnlyThisThread
26730fdc8d8SChris Lattner         || m_stop_others == lldb::eOnlyDuringStepping)
26830fdc8d8SChris Lattner         return true;
26930fdc8d8SChris Lattner     else
27030fdc8d8SChris Lattner         return false;
27130fdc8d8SChris Lattner }
27230fdc8d8SChris Lattner 
273564d8bc2SJim Ingham InstructionList *
274564d8bc2SJim Ingham ThreadPlanStepRange::GetInstructionsForAddress(lldb::addr_t addr, size_t &range_index, size_t &insn_offset)
275564d8bc2SJim Ingham {
276564d8bc2SJim Ingham     size_t num_ranges = m_address_ranges.size();
277564d8bc2SJim Ingham     for (size_t i = 0; i < num_ranges; i++)
278564d8bc2SJim Ingham     {
279564d8bc2SJim Ingham         if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget()))
280564d8bc2SJim Ingham         {
281564d8bc2SJim Ingham             // Some joker added a zero size range to the stepping range...
282564d8bc2SJim Ingham             if (m_address_ranges[i].GetByteSize() == 0)
283564d8bc2SJim Ingham                 return NULL;
284564d8bc2SJim Ingham 
285564d8bc2SJim Ingham             if (!m_instruction_ranges[i])
286564d8bc2SJim Ingham             {
287564d8bc2SJim Ingham                 //Disassemble the address range given:
288564d8bc2SJim Ingham                 ExecutionContext exe_ctx (m_thread.GetProcess());
2890f063ba6SJim Ingham                 const char *plugin_name = NULL;
2900f063ba6SJim Ingham                 const char *flavor = NULL;
291564d8bc2SJim Ingham                 m_instruction_ranges[i] = Disassembler::DisassembleRange(GetTarget().GetArchitecture(),
2920f063ba6SJim Ingham                                                                          plugin_name,
2930f063ba6SJim Ingham                                                                          flavor,
294564d8bc2SJim Ingham                                                                          exe_ctx,
295564d8bc2SJim Ingham                                                                          m_address_ranges[i]);
296564d8bc2SJim Ingham 
297564d8bc2SJim Ingham             }
298564d8bc2SJim Ingham             if (!m_instruction_ranges[i])
299564d8bc2SJim Ingham                 return NULL;
300564d8bc2SJim Ingham             else
301564d8bc2SJim Ingham             {
302564d8bc2SJim Ingham                 // Find where we are in the instruction list as well.  If we aren't at an instruction,
303564d8bc2SJim Ingham                 // return NULL.  In this case, we're probably lost, and shouldn't try to do anything fancy.
304564d8bc2SJim Ingham 
305564d8bc2SJim Ingham                 insn_offset = m_instruction_ranges[i]->GetInstructionList().GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
306564d8bc2SJim Ingham                 if (insn_offset == UINT32_MAX)
307564d8bc2SJim Ingham                     return NULL;
308564d8bc2SJim Ingham                 else
309564d8bc2SJim Ingham                 {
310564d8bc2SJim Ingham                     range_index = i;
311564d8bc2SJim Ingham                     return &m_instruction_ranges[i]->GetInstructionList();
312564d8bc2SJim Ingham                 }
313564d8bc2SJim Ingham             }
314564d8bc2SJim Ingham         }
315564d8bc2SJim Ingham     }
316564d8bc2SJim Ingham     return NULL;
317564d8bc2SJim Ingham }
318564d8bc2SJim Ingham 
319564d8bc2SJim Ingham void
320564d8bc2SJim Ingham ThreadPlanStepRange::ClearNextBranchBreakpoint()
321564d8bc2SJim Ingham {
322564d8bc2SJim Ingham     if (m_next_branch_bp_sp)
323564d8bc2SJim Ingham     {
3245160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
3250c61dee1SJim Ingham         if (log)
3260c61dee1SJim Ingham             log->Printf ("Removing next branch breakpoint: %d.", m_next_branch_bp_sp->GetID());
327564d8bc2SJim Ingham         GetTarget().RemoveBreakpointByID (m_next_branch_bp_sp->GetID());
328564d8bc2SJim Ingham         m_next_branch_bp_sp.reset();
329564d8bc2SJim Ingham     }
330564d8bc2SJim Ingham }
331564d8bc2SJim Ingham 
332564d8bc2SJim Ingham bool
333564d8bc2SJim Ingham ThreadPlanStepRange::SetNextBranchBreakpoint ()
334564d8bc2SJim Ingham {
3350c61dee1SJim Ingham     if (m_next_branch_bp_sp)
3360c61dee1SJim Ingham         return true;
3370c61dee1SJim Ingham 
3385160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
339564d8bc2SJim Ingham     // Stepping through ranges using breakpoints doesn't work yet, but with this off we fall back to instruction
340564d8bc2SJim Ingham     // single stepping.
3410c61dee1SJim Ingham     if (!m_use_fast_step)
342564d8bc2SJim Ingham          return false;
3430c61dee1SJim Ingham 
344564d8bc2SJim Ingham     lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC();
345564d8bc2SJim Ingham     // Find the current address in our address ranges, and fetch the disassembly if we haven't already:
346564d8bc2SJim Ingham     size_t pc_index;
347564d8bc2SJim Ingham     size_t range_index;
348564d8bc2SJim Ingham     InstructionList *instructions = GetInstructionsForAddress (cur_addr, range_index, pc_index);
349564d8bc2SJim Ingham     if (instructions == NULL)
350564d8bc2SJim Ingham         return false;
351564d8bc2SJim Ingham     else
352564d8bc2SJim Ingham     {
353564d8bc2SJim Ingham         uint32_t branch_index;
354564d8bc2SJim Ingham         branch_index = instructions->GetIndexOfNextBranchInstruction (pc_index);
355564d8bc2SJim Ingham 
356564d8bc2SJim Ingham         Address run_to_address;
357564d8bc2SJim Ingham 
358564d8bc2SJim Ingham         // If we didn't find a branch, run to the end of the range.
359564d8bc2SJim Ingham         if (branch_index == UINT32_MAX)
360564d8bc2SJim Ingham         {
3610c61dee1SJim Ingham             branch_index = instructions->GetSize() - 1;
362564d8bc2SJim Ingham         }
3630c61dee1SJim Ingham 
364564d8bc2SJim Ingham         if (branch_index - pc_index > 1)
365564d8bc2SJim Ingham         {
366564d8bc2SJim Ingham             const bool is_internal = true;
367564d8bc2SJim Ingham             run_to_address = instructions->GetInstructionAtIndex(branch_index)->GetAddress();
368564d8bc2SJim Ingham             m_next_branch_bp_sp = GetTarget().CreateBreakpoint(run_to_address, is_internal);
3692995077dSJim Ingham             if (m_next_branch_bp_sp)
3702995077dSJim Ingham             {
3710c61dee1SJim Ingham                 if (log)
3720c61dee1SJim Ingham                 {
3730c61dee1SJim Ingham                     lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
3740c61dee1SJim Ingham                     BreakpointLocationSP bp_loc = m_next_branch_bp_sp->GetLocationAtIndex(0);
3750c61dee1SJim Ingham                     if (bp_loc)
3760c61dee1SJim Ingham                     {
3770c61dee1SJim Ingham                         BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
3780c61dee1SJim Ingham                         if (bp_site)
3790c61dee1SJim Ingham                         {
3800c61dee1SJim Ingham                             bp_site_id = bp_site->GetID();
3810c61dee1SJim Ingham                         }
3820c61dee1SJim Ingham                     }
3830c61dee1SJim Ingham                     log->Printf ("ThreadPlanStepRange::SetNextBranchBreakpoint - Setting breakpoint %d (site %d) to run to address 0x%" PRIx64,
3840c61dee1SJim Ingham                                  m_next_branch_bp_sp->GetID(),
3850c61dee1SJim Ingham                                  bp_site_id,
3860c61dee1SJim Ingham                                  run_to_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget()));
3870c61dee1SJim Ingham                 }
388564d8bc2SJim Ingham                 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
3892995077dSJim Ingham                 m_next_branch_bp_sp->SetBreakpointKind ("next-branch-location");
3900c61dee1SJim Ingham                 return true;
3912995077dSJim Ingham             }
3922995077dSJim Ingham             else
3932995077dSJim Ingham                 return false;
394564d8bc2SJim Ingham         }
395564d8bc2SJim Ingham     }
396564d8bc2SJim Ingham     return false;
397564d8bc2SJim Ingham }
398564d8bc2SJim Ingham 
399564d8bc2SJim Ingham bool
400564d8bc2SJim Ingham ThreadPlanStepRange::NextRangeBreakpointExplainsStop (lldb::StopInfoSP stop_info_sp)
401564d8bc2SJim Ingham {
4025160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
403564d8bc2SJim Ingham     if (!m_next_branch_bp_sp)
404564d8bc2SJim Ingham         return false;
405564d8bc2SJim Ingham 
406564d8bc2SJim Ingham     break_id_t bp_site_id = stop_info_sp->GetValue();
407564d8bc2SJim Ingham     BreakpointSiteSP bp_site_sp = m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id);
4080c61dee1SJim Ingham     if (!bp_site_sp)
4090c61dee1SJim Ingham         return false;
4100c61dee1SJim Ingham     else if (!bp_site_sp->IsBreakpointAtThisSite (m_next_branch_bp_sp->GetID()))
411564d8bc2SJim Ingham         return false;
412564d8bc2SJim Ingham     else
4130c61dee1SJim Ingham     {
4140c61dee1SJim Ingham         // If we've hit the next branch breakpoint, then clear it.
4150c61dee1SJim Ingham         size_t num_owners = bp_site_sp->GetNumberOfOwners();
4160c61dee1SJim Ingham         bool explains_stop = true;
4170c61dee1SJim Ingham         // If all the owners are internal, then we are probably just stepping over this range from multiple threads,
4180c61dee1SJim Ingham         // or multiple frames, so we want to continue.  If one is not internal, then we should not explain the stop,
4190c61dee1SJim Ingham         // and let the user breakpoint handle the stop.
4200c61dee1SJim Ingham         for (size_t i = 0; i < num_owners; i++)
4210c61dee1SJim Ingham         {
4220c61dee1SJim Ingham             if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
4230c61dee1SJim Ingham             {
4240c61dee1SJim Ingham                 explains_stop = false;
4250c61dee1SJim Ingham                 break;
4260c61dee1SJim Ingham             }
4270c61dee1SJim Ingham         }
4280c61dee1SJim Ingham         if (log)
4290c61dee1SJim Ingham             log->Printf ("ThreadPlanStepRange::NextRangeBreakpointExplainsStop - Hit next range breakpoint which has %zu owners - explains stop: %u.",
4300c61dee1SJim Ingham                         num_owners,
4310c61dee1SJim Ingham                         explains_stop);
4320c61dee1SJim Ingham         ClearNextBranchBreakpoint();
4330c61dee1SJim Ingham         return  explains_stop;
4340c61dee1SJim Ingham     }
435564d8bc2SJim Ingham }
436564d8bc2SJim Ingham 
437564d8bc2SJim Ingham bool
43830fdc8d8SChris Lattner ThreadPlanStepRange::WillStop ()
43930fdc8d8SChris Lattner {
44030fdc8d8SChris Lattner     return true;
44130fdc8d8SChris Lattner }
44230fdc8d8SChris Lattner 
44330fdc8d8SChris Lattner StateType
44406e827ccSJim Ingham ThreadPlanStepRange::GetPlanRunState ()
44530fdc8d8SChris Lattner {
446564d8bc2SJim Ingham     if (m_next_branch_bp_sp)
447564d8bc2SJim Ingham         return eStateRunning;
448564d8bc2SJim Ingham     else
44930fdc8d8SChris Lattner         return eStateStepping;
45030fdc8d8SChris Lattner }
45130fdc8d8SChris Lattner 
45230fdc8d8SChris Lattner bool
45330fdc8d8SChris Lattner ThreadPlanStepRange::MischiefManaged ()
45430fdc8d8SChris Lattner {
455927bfa3fSJim Ingham     // If we have pushed some plans between ShouldStop & MischiefManaged, then we're not done...
456927bfa3fSJim Ingham     // I do this check first because we might have stepped somewhere that will fool InRange into
457927bfa3fSJim Ingham     // thinking it needs to step past the end of that line.  This happens, for instance, when stepping
458927bfa3fSJim Ingham     // over inlined code that is in the middle of the current line.
459927bfa3fSJim Ingham 
460927bfa3fSJim Ingham     if (!m_no_more_plans)
461927bfa3fSJim Ingham         return false;
462927bfa3fSJim Ingham 
46330fdc8d8SChris Lattner     bool done = true;
46430fdc8d8SChris Lattner     if (!IsPlanComplete())
46530fdc8d8SChris Lattner     {
46630fdc8d8SChris Lattner         if (InRange())
46730fdc8d8SChris Lattner         {
46830fdc8d8SChris Lattner             done = false;
46930fdc8d8SChris Lattner         }
470b5c0d1ccSJim Ingham         else
471b5c0d1ccSJim Ingham         {
472b5c0d1ccSJim Ingham             FrameComparison frame_order = CompareCurrentFrameToStartFrame();
473b5c0d1ccSJim Ingham             if (frame_order != eFrameCompareOlder)
47430fdc8d8SChris Lattner             {
47530fdc8d8SChris Lattner                 if (m_no_more_plans)
47630fdc8d8SChris Lattner                     done = true;
47730fdc8d8SChris Lattner                 else
47830fdc8d8SChris Lattner                     done = false;
47930fdc8d8SChris Lattner             }
48030fdc8d8SChris Lattner             else
48130fdc8d8SChris Lattner                 done = true;
48230fdc8d8SChris Lattner         }
483b5c0d1ccSJim Ingham     }
48430fdc8d8SChris Lattner 
48530fdc8d8SChris Lattner     if (done)
48630fdc8d8SChris Lattner     {
4875160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
48830fdc8d8SChris Lattner         if (log)
48930fdc8d8SChris Lattner             log->Printf("Completed step through range plan.");
4900c61dee1SJim Ingham         ClearNextBranchBreakpoint();
49130fdc8d8SChris Lattner         ThreadPlan::MischiefManaged ();
49230fdc8d8SChris Lattner         return true;
49330fdc8d8SChris Lattner     }
49430fdc8d8SChris Lattner     else
49530fdc8d8SChris Lattner     {
49630fdc8d8SChris Lattner         return false;
49730fdc8d8SChris Lattner     }
49830fdc8d8SChris Lattner 
49930fdc8d8SChris Lattner }
50064e7ead1SJim Ingham 
50164e7ead1SJim Ingham bool
50264e7ead1SJim Ingham ThreadPlanStepRange::IsPlanStale ()
50364e7ead1SJim Ingham {
5045160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
50564e7ead1SJim Ingham     FrameComparison frame_order = CompareCurrentFrameToStartFrame();
50664e7ead1SJim Ingham 
50764e7ead1SJim Ingham     if (frame_order == eFrameCompareOlder)
50864e7ead1SJim Ingham     {
50964e7ead1SJim Ingham         if (log)
51064e7ead1SJim Ingham         {
51164e7ead1SJim Ingham             log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've stepped out.");
51264e7ead1SJim Ingham         }
51364e7ead1SJim Ingham         return true;
51464e7ead1SJim Ingham     }
51564e7ead1SJim Ingham     else if (frame_order == eFrameCompareEqual && InSymbol())
51664e7ead1SJim Ingham     {
51764e7ead1SJim Ingham         // If we are not in a place we should step through, we've gotten stale.
51864e7ead1SJim Ingham         // One tricky bit here is that some stubs don't push a frame, so we should.
51964e7ead1SJim Ingham         // check that we are in the same symbol.
52064e7ead1SJim Ingham         if (!InRange())
52164e7ead1SJim Ingham         {
52264e7ead1SJim Ingham             return true;
52364e7ead1SJim Ingham         }
52464e7ead1SJim Ingham     }
52564e7ead1SJim Ingham     return false;
52664e7ead1SJim Ingham }
527