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 {
5717d023f6SJim Ingham     m_use_fast_step = GetTarget().GetUseFastStepping();
58c4c9fedcSJim Ingham     AddRange(range);
5930fdc8d8SChris Lattner     m_stack_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
6030fdc8d8SChris Lattner }
6130fdc8d8SChris Lattner 
6230fdc8d8SChris Lattner ThreadPlanStepRange::~ThreadPlanStepRange ()
6330fdc8d8SChris Lattner {
64564d8bc2SJim Ingham     ClearNextBranchBreakpoint();
6556d40428SJim Ingham 
6656d40428SJim Ingham     size_t num_instruction_ranges = m_instruction_ranges.size();
6756d40428SJim Ingham 
6856d40428SJim Ingham     // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
6956d40428SJim Ingham     // I'll fix that but for now, just clear the list and it will go away nicely.
7056d40428SJim Ingham     for (size_t i = 0; i < num_instruction_ranges; i++)
7156d40428SJim Ingham     {
7256d40428SJim Ingham         if (m_instruction_ranges[i])
7356d40428SJim Ingham             m_instruction_ranges[i]->GetInstructionList().Clear();
7456d40428SJim Ingham     }
75564d8bc2SJim Ingham }
76564d8bc2SJim Ingham 
77564d8bc2SJim Ingham void
78564d8bc2SJim Ingham ThreadPlanStepRange::DidPush ()
79564d8bc2SJim Ingham {
80564d8bc2SJim Ingham     // See if we can find a "next range" breakpoint:
81564d8bc2SJim Ingham     SetNextBranchBreakpoint();
8230fdc8d8SChris Lattner }
8330fdc8d8SChris Lattner 
8430fdc8d8SChris Lattner bool
8530fdc8d8SChris Lattner ThreadPlanStepRange::ValidatePlan (Stream *error)
8630fdc8d8SChris Lattner {
8730fdc8d8SChris Lattner     return true;
8830fdc8d8SChris Lattner }
8930fdc8d8SChris Lattner 
9030fdc8d8SChris Lattner Vote
9130fdc8d8SChris Lattner ThreadPlanStepRange::ShouldReportStop (Event *event_ptr)
9230fdc8d8SChris Lattner {
935160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
942cad65a5SGreg Clayton 
952cad65a5SGreg Clayton     const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo;
962cad65a5SGreg Clayton     if (log)
97411c0ce8SGreg Clayton         log->Printf ("ThreadPlanStepRange::ShouldReportStop() returning vote %i\n", vote);
982cad65a5SGreg Clayton     return vote;
9930fdc8d8SChris Lattner }
10030fdc8d8SChris Lattner 
101c4c9fedcSJim Ingham void
102c4c9fedcSJim Ingham ThreadPlanStepRange::AddRange(const AddressRange &new_range)
103c4c9fedcSJim Ingham {
104c4c9fedcSJim Ingham     // For now I'm just adding the ranges.  At some point we may want to
105c4c9fedcSJim Ingham     // condense the ranges if they overlap, though I don't think it is likely
106c4c9fedcSJim Ingham     // to be very important.
107c4c9fedcSJim Ingham     m_address_ranges.push_back (new_range);
10856d40428SJim Ingham 
10956d40428SJim Ingham     // Fill the slot for this address range with an empty DisassemblerSP in the instruction ranges. I want the
11056d40428SJim Ingham     // indices to match, but I don't want to do the work to disassemble this range if I don't step into it.
111564d8bc2SJim Ingham     m_instruction_ranges.push_back (DisassemblerSP());
112c4c9fedcSJim Ingham }
113c4c9fedcSJim Ingham 
114c4c9fedcSJim Ingham void
115c4c9fedcSJim Ingham ThreadPlanStepRange::DumpRanges(Stream *s)
116c4c9fedcSJim Ingham {
117c4c9fedcSJim Ingham     size_t num_ranges = m_address_ranges.size();
118c4c9fedcSJim Ingham     if (num_ranges == 1)
119c4c9fedcSJim Ingham     {
1201ac04c30SGreg Clayton         m_address_ranges[0].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
121c4c9fedcSJim Ingham     }
122c4c9fedcSJim Ingham     else
123c4c9fedcSJim Ingham     {
124c4c9fedcSJim Ingham         for (size_t i = 0; i < num_ranges; i++)
125c4c9fedcSJim Ingham         {
126c4c9fedcSJim Ingham             s->PutCString("%d: ");
1271ac04c30SGreg Clayton             m_address_ranges[i].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
128c4c9fedcSJim Ingham         }
129c4c9fedcSJim Ingham     }
130c4c9fedcSJim Ingham }
131c4c9fedcSJim Ingham 
13230fdc8d8SChris Lattner bool
13330fdc8d8SChris Lattner ThreadPlanStepRange::InRange ()
13430fdc8d8SChris Lattner {
1355160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
13630fdc8d8SChris Lattner     bool ret_value = false;
13730fdc8d8SChris Lattner 
13830fdc8d8SChris Lattner     lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
13930fdc8d8SChris Lattner 
140c4c9fedcSJim Ingham     size_t num_ranges = m_address_ranges.size();
141c4c9fedcSJim Ingham     for (size_t i = 0; i < num_ranges; i++)
142c4c9fedcSJim Ingham     {
1431ac04c30SGreg Clayton         ret_value = m_address_ranges[i].ContainsLoadAddress(pc_load_addr, m_thread.CalculateTarget().get());
144c4c9fedcSJim Ingham         if (ret_value)
145c4c9fedcSJim Ingham             break;
146c4c9fedcSJim Ingham     }
14730fdc8d8SChris Lattner 
14830fdc8d8SChris Lattner     if (!ret_value)
14930fdc8d8SChris Lattner     {
15030fdc8d8SChris Lattner         // See if we've just stepped to another part of the same line number...
15130fdc8d8SChris Lattner         StackFrame *frame = m_thread.GetStackFrameAtIndex(0).get();
15230fdc8d8SChris Lattner 
15330fdc8d8SChris Lattner         SymbolContext new_context(frame->GetSymbolContext(eSymbolContextEverything));
15430fdc8d8SChris Lattner         if (m_addr_context.line_entry.IsValid() && new_context.line_entry.IsValid())
15530fdc8d8SChris Lattner         {
156843bfb2cSJim Ingham             if (m_addr_context.line_entry.file == new_context.line_entry.file)
157843bfb2cSJim Ingham             {
158843bfb2cSJim Ingham                 if (m_addr_context.line_entry.line == new_context.line_entry.line)
15930fdc8d8SChris Lattner                 {
16030fdc8d8SChris Lattner                     m_addr_context = new_context;
161c4c9fedcSJim Ingham                     AddRange(m_addr_context.line_entry.range);
16230fdc8d8SChris Lattner                     ret_value = true;
16330fdc8d8SChris Lattner                     if (log)
16430fdc8d8SChris Lattner                     {
16530fdc8d8SChris Lattner                         StreamString s;
166927bfa3fSJim Ingham                         m_addr_context.line_entry.Dump (&s,
1671ac04c30SGreg Clayton                                                         m_thread.CalculateTarget().get(),
168927bfa3fSJim Ingham                                                         true,
169927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
170927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
171927bfa3fSJim Ingham                                                         true);
17230fdc8d8SChris Lattner 
17330fdc8d8SChris Lattner                         log->Printf ("Step range plan stepped to another range of same line: %s", s.GetData());
17430fdc8d8SChris Lattner                     }
17530fdc8d8SChris Lattner                 }
1761ac04c30SGreg Clayton                 else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(m_thread.CalculateTarget().get())
177843bfb2cSJim Ingham                          != pc_load_addr)
178843bfb2cSJim Ingham                 {
179843bfb2cSJim Ingham                     // Another thing that sometimes happens here is that we step out of one line into the MIDDLE of another
180843bfb2cSJim Ingham                     // line.  So far I mostly see this due to bugs in the debug information.
181843bfb2cSJim Ingham                     // But we probably don't want to be in the middle of a line range, so in that case reset the stepping
182843bfb2cSJim Ingham                     // range to the line we've stepped into the middle of and continue.
183843bfb2cSJim Ingham                     m_addr_context = new_context;
184c4c9fedcSJim Ingham                     m_address_ranges.clear();
185c4c9fedcSJim Ingham                     AddRange(m_addr_context.line_entry.range);
186843bfb2cSJim Ingham                     ret_value = true;
187843bfb2cSJim Ingham                     if (log)
188843bfb2cSJim Ingham                     {
189843bfb2cSJim Ingham                         StreamString s;
190927bfa3fSJim Ingham                         m_addr_context.line_entry.Dump (&s,
1911ac04c30SGreg Clayton                                                         m_thread.CalculateTarget().get(),
192927bfa3fSJim Ingham                                                         true,
193927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
194927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
195927bfa3fSJim Ingham                                                         true);
196843bfb2cSJim Ingham 
197843bfb2cSJim Ingham                         log->Printf ("Step range plan stepped to the middle of new line(%d): %s, continuing to clear this line.",
198843bfb2cSJim Ingham                                      new_context.line_entry.line,
199843bfb2cSJim Ingham                                      s.GetData());
200843bfb2cSJim Ingham                     }
201843bfb2cSJim Ingham 
202843bfb2cSJim Ingham                 }
203843bfb2cSJim Ingham             }
204843bfb2cSJim Ingham 
20530fdc8d8SChris Lattner         }
20630fdc8d8SChris Lattner 
20730fdc8d8SChris Lattner     }
20830fdc8d8SChris Lattner 
20930fdc8d8SChris Lattner     if (!ret_value && log)
210d01b2953SDaniel Malea         log->Printf ("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
21130fdc8d8SChris Lattner 
21230fdc8d8SChris Lattner     return ret_value;
21330fdc8d8SChris Lattner }
21430fdc8d8SChris Lattner 
21530fdc8d8SChris Lattner bool
21630fdc8d8SChris Lattner ThreadPlanStepRange::InSymbol()
21730fdc8d8SChris Lattner {
21830fdc8d8SChris Lattner     lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
21930fdc8d8SChris Lattner     if (m_addr_context.function != NULL)
22030fdc8d8SChris Lattner     {
2211ac04c30SGreg Clayton         return m_addr_context.function->GetAddressRange().ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
22230fdc8d8SChris Lattner     }
223e7612134SGreg Clayton     else if (m_addr_context.symbol)
22430fdc8d8SChris Lattner     {
225e7612134SGreg Clayton         AddressRange range(m_addr_context.symbol->GetAddress(), m_addr_context.symbol->GetByteSize());
226e7612134SGreg Clayton         return range.ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
22730fdc8d8SChris Lattner     }
22830fdc8d8SChris Lattner     return false;
22930fdc8d8SChris Lattner }
23030fdc8d8SChris Lattner 
23130fdc8d8SChris Lattner // FIXME: This should also handle inlining if we aren't going to do inlining in the
23230fdc8d8SChris Lattner // main stack.
23330fdc8d8SChris Lattner //
23430fdc8d8SChris Lattner // Ideally we should remember the whole stack frame list, and then compare that
23530fdc8d8SChris Lattner // to the current list.
23630fdc8d8SChris Lattner 
237b5c0d1ccSJim Ingham lldb::FrameComparison
238b5c0d1ccSJim Ingham ThreadPlanStepRange::CompareCurrentFrameToStartFrame()
23930fdc8d8SChris Lattner {
240b5c0d1ccSJim Ingham     FrameComparison frame_order;
2417ce490c6SJim Ingham 
242b5c0d1ccSJim Ingham     StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
2437ce490c6SJim Ingham 
244b5c0d1ccSJim Ingham     if (cur_frame_id == m_stack_id)
24530fdc8d8SChris Lattner     {
246b5c0d1ccSJim Ingham         frame_order = eFrameCompareEqual;
24730fdc8d8SChris Lattner     }
248b5c0d1ccSJim Ingham     else if (cur_frame_id < m_stack_id)
24930fdc8d8SChris Lattner     {
250b5c0d1ccSJim Ingham         frame_order = eFrameCompareYounger;
25130fdc8d8SChris Lattner     }
25230fdc8d8SChris Lattner     else
25330fdc8d8SChris Lattner     {
254b5c0d1ccSJim Ingham         frame_order = eFrameCompareOlder;
25530fdc8d8SChris Lattner     }
256b5c0d1ccSJim Ingham     return frame_order;
25730fdc8d8SChris Lattner }
25830fdc8d8SChris Lattner 
25930fdc8d8SChris Lattner bool
26030fdc8d8SChris Lattner ThreadPlanStepRange::StopOthers ()
26130fdc8d8SChris Lattner {
26230fdc8d8SChris Lattner     if (m_stop_others == lldb::eOnlyThisThread
26330fdc8d8SChris Lattner         || m_stop_others == lldb::eOnlyDuringStepping)
26430fdc8d8SChris Lattner         return true;
26530fdc8d8SChris Lattner     else
26630fdc8d8SChris Lattner         return false;
26730fdc8d8SChris Lattner }
26830fdc8d8SChris Lattner 
269564d8bc2SJim Ingham InstructionList *
270564d8bc2SJim Ingham ThreadPlanStepRange::GetInstructionsForAddress(lldb::addr_t addr, size_t &range_index, size_t &insn_offset)
271564d8bc2SJim Ingham {
272564d8bc2SJim Ingham     size_t num_ranges = m_address_ranges.size();
273564d8bc2SJim Ingham     for (size_t i = 0; i < num_ranges; i++)
274564d8bc2SJim Ingham     {
275564d8bc2SJim Ingham         if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget()))
276564d8bc2SJim Ingham         {
277564d8bc2SJim Ingham             // Some joker added a zero size range to the stepping range...
278564d8bc2SJim Ingham             if (m_address_ranges[i].GetByteSize() == 0)
279564d8bc2SJim Ingham                 return NULL;
280564d8bc2SJim Ingham 
281564d8bc2SJim Ingham             if (!m_instruction_ranges[i])
282564d8bc2SJim Ingham             {
283564d8bc2SJim Ingham                 //Disassemble the address range given:
284564d8bc2SJim Ingham                 ExecutionContext exe_ctx (m_thread.GetProcess());
2850f063ba6SJim Ingham                 const char *plugin_name = NULL;
2860f063ba6SJim Ingham                 const char *flavor = NULL;
287*6b3e6d54SJason Molenda                 const bool prefer_file_cache = true;
288564d8bc2SJim Ingham                 m_instruction_ranges[i] = Disassembler::DisassembleRange(GetTarget().GetArchitecture(),
2890f063ba6SJim Ingham                                                                          plugin_name,
2900f063ba6SJim Ingham                                                                          flavor,
291564d8bc2SJim Ingham                                                                          exe_ctx,
292*6b3e6d54SJason Molenda                                                                          m_address_ranges[i],
293*6b3e6d54SJason Molenda                                                                          prefer_file_cache);
294564d8bc2SJim Ingham 
295564d8bc2SJim Ingham             }
296564d8bc2SJim Ingham             if (!m_instruction_ranges[i])
297564d8bc2SJim Ingham                 return NULL;
298564d8bc2SJim Ingham             else
299564d8bc2SJim Ingham             {
300564d8bc2SJim Ingham                 // Find where we are in the instruction list as well.  If we aren't at an instruction,
301564d8bc2SJim Ingham                 // return NULL.  In this case, we're probably lost, and shouldn't try to do anything fancy.
302564d8bc2SJim Ingham 
303564d8bc2SJim Ingham                 insn_offset = m_instruction_ranges[i]->GetInstructionList().GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
304564d8bc2SJim Ingham                 if (insn_offset == UINT32_MAX)
305564d8bc2SJim Ingham                     return NULL;
306564d8bc2SJim Ingham                 else
307564d8bc2SJim Ingham                 {
308564d8bc2SJim Ingham                     range_index = i;
309564d8bc2SJim Ingham                     return &m_instruction_ranges[i]->GetInstructionList();
310564d8bc2SJim Ingham                 }
311564d8bc2SJim Ingham             }
312564d8bc2SJim Ingham         }
313564d8bc2SJim Ingham     }
314564d8bc2SJim Ingham     return NULL;
315564d8bc2SJim Ingham }
316564d8bc2SJim Ingham 
317564d8bc2SJim Ingham void
318564d8bc2SJim Ingham ThreadPlanStepRange::ClearNextBranchBreakpoint()
319564d8bc2SJim Ingham {
320564d8bc2SJim Ingham     if (m_next_branch_bp_sp)
321564d8bc2SJim Ingham     {
3225160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
3230c61dee1SJim Ingham         if (log)
3240c61dee1SJim Ingham             log->Printf ("Removing next branch breakpoint: %d.", m_next_branch_bp_sp->GetID());
325564d8bc2SJim Ingham         GetTarget().RemoveBreakpointByID (m_next_branch_bp_sp->GetID());
326564d8bc2SJim Ingham         m_next_branch_bp_sp.reset();
327564d8bc2SJim Ingham     }
328564d8bc2SJim Ingham }
329564d8bc2SJim Ingham 
330564d8bc2SJim Ingham bool
331564d8bc2SJim Ingham ThreadPlanStepRange::SetNextBranchBreakpoint ()
332564d8bc2SJim Ingham {
3330c61dee1SJim Ingham     if (m_next_branch_bp_sp)
3340c61dee1SJim Ingham         return true;
3350c61dee1SJim Ingham 
3365160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
337564d8bc2SJim Ingham     // Stepping through ranges using breakpoints doesn't work yet, but with this off we fall back to instruction
338564d8bc2SJim Ingham     // single stepping.
3390c61dee1SJim Ingham     if (!m_use_fast_step)
340564d8bc2SJim Ingham          return false;
3410c61dee1SJim Ingham 
342564d8bc2SJim Ingham     lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC();
343564d8bc2SJim Ingham     // Find the current address in our address ranges, and fetch the disassembly if we haven't already:
344564d8bc2SJim Ingham     size_t pc_index;
345564d8bc2SJim Ingham     size_t range_index;
346564d8bc2SJim Ingham     InstructionList *instructions = GetInstructionsForAddress (cur_addr, range_index, pc_index);
347564d8bc2SJim Ingham     if (instructions == NULL)
348564d8bc2SJim Ingham         return false;
349564d8bc2SJim Ingham     else
350564d8bc2SJim Ingham     {
351564d8bc2SJim Ingham         uint32_t branch_index;
352564d8bc2SJim Ingham         branch_index = instructions->GetIndexOfNextBranchInstruction (pc_index);
353564d8bc2SJim Ingham 
354564d8bc2SJim Ingham         Address run_to_address;
355564d8bc2SJim Ingham 
356564d8bc2SJim Ingham         // If we didn't find a branch, run to the end of the range.
357564d8bc2SJim Ingham         if (branch_index == UINT32_MAX)
358564d8bc2SJim Ingham         {
3590c61dee1SJim Ingham             branch_index = instructions->GetSize() - 1;
360564d8bc2SJim Ingham         }
3610c61dee1SJim Ingham 
362564d8bc2SJim Ingham         if (branch_index - pc_index > 1)
363564d8bc2SJim Ingham         {
364564d8bc2SJim Ingham             const bool is_internal = true;
365564d8bc2SJim Ingham             run_to_address = instructions->GetInstructionAtIndex(branch_index)->GetAddress();
366564d8bc2SJim Ingham             m_next_branch_bp_sp = GetTarget().CreateBreakpoint(run_to_address, is_internal);
3672995077dSJim Ingham             if (m_next_branch_bp_sp)
3682995077dSJim Ingham             {
3690c61dee1SJim Ingham                 if (log)
3700c61dee1SJim Ingham                 {
3710c61dee1SJim Ingham                     lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
3720c61dee1SJim Ingham                     BreakpointLocationSP bp_loc = m_next_branch_bp_sp->GetLocationAtIndex(0);
3730c61dee1SJim Ingham                     if (bp_loc)
3740c61dee1SJim Ingham                     {
3750c61dee1SJim Ingham                         BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
3760c61dee1SJim Ingham                         if (bp_site)
3770c61dee1SJim Ingham                         {
3780c61dee1SJim Ingham                             bp_site_id = bp_site->GetID();
3790c61dee1SJim Ingham                         }
3800c61dee1SJim Ingham                     }
3810c61dee1SJim Ingham                     log->Printf ("ThreadPlanStepRange::SetNextBranchBreakpoint - Setting breakpoint %d (site %d) to run to address 0x%" PRIx64,
3820c61dee1SJim Ingham                                  m_next_branch_bp_sp->GetID(),
3830c61dee1SJim Ingham                                  bp_site_id,
3840c61dee1SJim Ingham                                  run_to_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget()));
3850c61dee1SJim Ingham                 }
386564d8bc2SJim Ingham                 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
3872995077dSJim Ingham                 m_next_branch_bp_sp->SetBreakpointKind ("next-branch-location");
3880c61dee1SJim Ingham                 return true;
3892995077dSJim Ingham             }
3902995077dSJim Ingham             else
3912995077dSJim Ingham                 return false;
392564d8bc2SJim Ingham         }
393564d8bc2SJim Ingham     }
394564d8bc2SJim Ingham     return false;
395564d8bc2SJim Ingham }
396564d8bc2SJim Ingham 
397564d8bc2SJim Ingham bool
398564d8bc2SJim Ingham ThreadPlanStepRange::NextRangeBreakpointExplainsStop (lldb::StopInfoSP stop_info_sp)
399564d8bc2SJim Ingham {
4005160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
401564d8bc2SJim Ingham     if (!m_next_branch_bp_sp)
402564d8bc2SJim Ingham         return false;
403564d8bc2SJim Ingham 
404564d8bc2SJim Ingham     break_id_t bp_site_id = stop_info_sp->GetValue();
405564d8bc2SJim Ingham     BreakpointSiteSP bp_site_sp = m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id);
4060c61dee1SJim Ingham     if (!bp_site_sp)
4070c61dee1SJim Ingham         return false;
4080c61dee1SJim Ingham     else if (!bp_site_sp->IsBreakpointAtThisSite (m_next_branch_bp_sp->GetID()))
409564d8bc2SJim Ingham         return false;
410564d8bc2SJim Ingham     else
4110c61dee1SJim Ingham     {
4120c61dee1SJim Ingham         // If we've hit the next branch breakpoint, then clear it.
4130c61dee1SJim Ingham         size_t num_owners = bp_site_sp->GetNumberOfOwners();
4140c61dee1SJim Ingham         bool explains_stop = true;
4150c61dee1SJim Ingham         // If all the owners are internal, then we are probably just stepping over this range from multiple threads,
4160c61dee1SJim Ingham         // or multiple frames, so we want to continue.  If one is not internal, then we should not explain the stop,
4170c61dee1SJim Ingham         // and let the user breakpoint handle the stop.
4180c61dee1SJim Ingham         for (size_t i = 0; i < num_owners; i++)
4190c61dee1SJim Ingham         {
4200c61dee1SJim Ingham             if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
4210c61dee1SJim Ingham             {
4220c61dee1SJim Ingham                 explains_stop = false;
4230c61dee1SJim Ingham                 break;
4240c61dee1SJim Ingham             }
4250c61dee1SJim Ingham         }
4260c61dee1SJim Ingham         if (log)
4270c61dee1SJim Ingham             log->Printf ("ThreadPlanStepRange::NextRangeBreakpointExplainsStop - Hit next range breakpoint which has %zu owners - explains stop: %u.",
4280c61dee1SJim Ingham                         num_owners,
4290c61dee1SJim Ingham                         explains_stop);
4300c61dee1SJim Ingham         ClearNextBranchBreakpoint();
4310c61dee1SJim Ingham         return  explains_stop;
4320c61dee1SJim Ingham     }
433564d8bc2SJim Ingham }
434564d8bc2SJim Ingham 
435564d8bc2SJim Ingham bool
43630fdc8d8SChris Lattner ThreadPlanStepRange::WillStop ()
43730fdc8d8SChris Lattner {
43830fdc8d8SChris Lattner     return true;
43930fdc8d8SChris Lattner }
44030fdc8d8SChris Lattner 
44130fdc8d8SChris Lattner StateType
44206e827ccSJim Ingham ThreadPlanStepRange::GetPlanRunState ()
44330fdc8d8SChris Lattner {
444564d8bc2SJim Ingham     if (m_next_branch_bp_sp)
445564d8bc2SJim Ingham         return eStateRunning;
446564d8bc2SJim Ingham     else
44730fdc8d8SChris Lattner         return eStateStepping;
44830fdc8d8SChris Lattner }
44930fdc8d8SChris Lattner 
45030fdc8d8SChris Lattner bool
45130fdc8d8SChris Lattner ThreadPlanStepRange::MischiefManaged ()
45230fdc8d8SChris Lattner {
453927bfa3fSJim Ingham     // If we have pushed some plans between ShouldStop & MischiefManaged, then we're not done...
454927bfa3fSJim Ingham     // I do this check first because we might have stepped somewhere that will fool InRange into
455927bfa3fSJim Ingham     // thinking it needs to step past the end of that line.  This happens, for instance, when stepping
456927bfa3fSJim Ingham     // over inlined code that is in the middle of the current line.
457927bfa3fSJim Ingham 
458927bfa3fSJim Ingham     if (!m_no_more_plans)
459927bfa3fSJim Ingham         return false;
460927bfa3fSJim Ingham 
46130fdc8d8SChris Lattner     bool done = true;
46230fdc8d8SChris Lattner     if (!IsPlanComplete())
46330fdc8d8SChris Lattner     {
46430fdc8d8SChris Lattner         if (InRange())
46530fdc8d8SChris Lattner         {
46630fdc8d8SChris Lattner             done = false;
46730fdc8d8SChris Lattner         }
468b5c0d1ccSJim Ingham         else
469b5c0d1ccSJim Ingham         {
470b5c0d1ccSJim Ingham             FrameComparison frame_order = CompareCurrentFrameToStartFrame();
471b5c0d1ccSJim Ingham             if (frame_order != eFrameCompareOlder)
47230fdc8d8SChris Lattner             {
47330fdc8d8SChris Lattner                 if (m_no_more_plans)
47430fdc8d8SChris Lattner                     done = true;
47530fdc8d8SChris Lattner                 else
47630fdc8d8SChris Lattner                     done = false;
47730fdc8d8SChris Lattner             }
47830fdc8d8SChris Lattner             else
47930fdc8d8SChris Lattner                 done = true;
48030fdc8d8SChris Lattner         }
481b5c0d1ccSJim Ingham     }
48230fdc8d8SChris Lattner 
48330fdc8d8SChris Lattner     if (done)
48430fdc8d8SChris Lattner     {
4855160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
48630fdc8d8SChris Lattner         if (log)
48730fdc8d8SChris Lattner             log->Printf("Completed step through range plan.");
4880c61dee1SJim Ingham         ClearNextBranchBreakpoint();
48930fdc8d8SChris Lattner         ThreadPlan::MischiefManaged ();
49030fdc8d8SChris Lattner         return true;
49130fdc8d8SChris Lattner     }
49230fdc8d8SChris Lattner     else
49330fdc8d8SChris Lattner     {
49430fdc8d8SChris Lattner         return false;
49530fdc8d8SChris Lattner     }
49630fdc8d8SChris Lattner 
49730fdc8d8SChris Lattner }
49864e7ead1SJim Ingham 
49964e7ead1SJim Ingham bool
50064e7ead1SJim Ingham ThreadPlanStepRange::IsPlanStale ()
50164e7ead1SJim Ingham {
5025160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
50364e7ead1SJim Ingham     FrameComparison frame_order = CompareCurrentFrameToStartFrame();
50464e7ead1SJim Ingham 
50564e7ead1SJim Ingham     if (frame_order == eFrameCompareOlder)
50664e7ead1SJim Ingham     {
50764e7ead1SJim Ingham         if (log)
50864e7ead1SJim Ingham         {
50964e7ead1SJim Ingham             log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've stepped out.");
51064e7ead1SJim Ingham         }
51164e7ead1SJim Ingham         return true;
51264e7ead1SJim Ingham     }
51364e7ead1SJim Ingham     else if (frame_order == eFrameCompareEqual && InSymbol())
51464e7ead1SJim Ingham     {
51564e7ead1SJim Ingham         // If we are not in a place we should step through, we've gotten stale.
51664e7ead1SJim Ingham         // One tricky bit here is that some stubs don't push a frame, so we should.
51764e7ead1SJim Ingham         // check that we are in the same symbol.
51864e7ead1SJim Ingham         if (!InRange())
51964e7ead1SJim Ingham         {
52064e7ead1SJim Ingham             return true;
52164e7ead1SJim Ingham         }
52264e7ead1SJim Ingham     }
52364e7ead1SJim Ingham     return false;
52464e7ead1SJim Ingham }
525