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();
65564d8bc2SJim Ingham }
66564d8bc2SJim Ingham 
67564d8bc2SJim Ingham void
68564d8bc2SJim Ingham ThreadPlanStepRange::DidPush ()
69564d8bc2SJim Ingham {
70564d8bc2SJim Ingham     // See if we can find a "next range" breakpoint:
71564d8bc2SJim Ingham     SetNextBranchBreakpoint();
7230fdc8d8SChris Lattner }
7330fdc8d8SChris Lattner 
7430fdc8d8SChris Lattner bool
7530fdc8d8SChris Lattner ThreadPlanStepRange::ValidatePlan (Stream *error)
7630fdc8d8SChris Lattner {
7730fdc8d8SChris Lattner     return true;
7830fdc8d8SChris Lattner }
7930fdc8d8SChris Lattner 
8030fdc8d8SChris Lattner Vote
8130fdc8d8SChris Lattner ThreadPlanStepRange::ShouldReportStop (Event *event_ptr)
8230fdc8d8SChris Lattner {
83*5160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
842cad65a5SGreg Clayton 
852cad65a5SGreg Clayton     const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo;
862cad65a5SGreg Clayton     if (log)
87411c0ce8SGreg Clayton         log->Printf ("ThreadPlanStepRange::ShouldReportStop() returning vote %i\n", vote);
882cad65a5SGreg Clayton     return vote;
8930fdc8d8SChris Lattner }
9030fdc8d8SChris Lattner 
91c4c9fedcSJim Ingham void
92c4c9fedcSJim Ingham ThreadPlanStepRange::AddRange(const AddressRange &new_range)
93c4c9fedcSJim Ingham {
94c4c9fedcSJim Ingham     // For now I'm just adding the ranges.  At some point we may want to
95c4c9fedcSJim Ingham     // condense the ranges if they overlap, though I don't think it is likely
96c4c9fedcSJim Ingham     // to be very important.
97c4c9fedcSJim Ingham     m_address_ranges.push_back (new_range);
98564d8bc2SJim Ingham     m_instruction_ranges.push_back (DisassemblerSP());
99c4c9fedcSJim Ingham }
100c4c9fedcSJim Ingham 
101c4c9fedcSJim Ingham void
102c4c9fedcSJim Ingham ThreadPlanStepRange::DumpRanges(Stream *s)
103c4c9fedcSJim Ingham {
104c4c9fedcSJim Ingham     size_t num_ranges = m_address_ranges.size();
105c4c9fedcSJim Ingham     if (num_ranges == 1)
106c4c9fedcSJim Ingham     {
1071ac04c30SGreg Clayton         m_address_ranges[0].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
108c4c9fedcSJim Ingham     }
109c4c9fedcSJim Ingham     else
110c4c9fedcSJim Ingham     {
111c4c9fedcSJim Ingham         for (size_t i = 0; i < num_ranges; i++)
112c4c9fedcSJim Ingham         {
113c4c9fedcSJim Ingham             s->PutCString("%d: ");
1141ac04c30SGreg Clayton             m_address_ranges[i].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
115c4c9fedcSJim Ingham         }
116c4c9fedcSJim Ingham     }
117c4c9fedcSJim Ingham }
118c4c9fedcSJim Ingham 
11930fdc8d8SChris Lattner bool
12030fdc8d8SChris Lattner ThreadPlanStepRange::InRange ()
12130fdc8d8SChris Lattner {
122*5160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
12330fdc8d8SChris Lattner     bool ret_value = false;
12430fdc8d8SChris Lattner 
12530fdc8d8SChris Lattner     lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
12630fdc8d8SChris Lattner 
127c4c9fedcSJim Ingham     size_t num_ranges = m_address_ranges.size();
128c4c9fedcSJim Ingham     for (size_t i = 0; i < num_ranges; i++)
129c4c9fedcSJim Ingham     {
1301ac04c30SGreg Clayton         ret_value = m_address_ranges[i].ContainsLoadAddress(pc_load_addr, m_thread.CalculateTarget().get());
131c4c9fedcSJim Ingham         if (ret_value)
132c4c9fedcSJim Ingham             break;
133c4c9fedcSJim Ingham     }
13430fdc8d8SChris Lattner 
13530fdc8d8SChris Lattner     if (!ret_value)
13630fdc8d8SChris Lattner     {
13730fdc8d8SChris Lattner         // See if we've just stepped to another part of the same line number...
13830fdc8d8SChris Lattner         StackFrame *frame = m_thread.GetStackFrameAtIndex(0).get();
13930fdc8d8SChris Lattner 
14030fdc8d8SChris Lattner         SymbolContext new_context(frame->GetSymbolContext(eSymbolContextEverything));
14130fdc8d8SChris Lattner         if (m_addr_context.line_entry.IsValid() && new_context.line_entry.IsValid())
14230fdc8d8SChris Lattner         {
143843bfb2cSJim Ingham             if (m_addr_context.line_entry.file == new_context.line_entry.file)
144843bfb2cSJim Ingham             {
145843bfb2cSJim Ingham                 if (m_addr_context.line_entry.line == new_context.line_entry.line)
14630fdc8d8SChris Lattner                 {
14730fdc8d8SChris Lattner                     m_addr_context = new_context;
148c4c9fedcSJim Ingham                     AddRange(m_addr_context.line_entry.range);
14930fdc8d8SChris Lattner                     ret_value = true;
15030fdc8d8SChris Lattner                     if (log)
15130fdc8d8SChris Lattner                     {
15230fdc8d8SChris Lattner                         StreamString s;
153927bfa3fSJim Ingham                         m_addr_context.line_entry.Dump (&s,
1541ac04c30SGreg Clayton                                                         m_thread.CalculateTarget().get(),
155927bfa3fSJim Ingham                                                         true,
156927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
157927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
158927bfa3fSJim Ingham                                                         true);
15930fdc8d8SChris Lattner 
16030fdc8d8SChris Lattner                         log->Printf ("Step range plan stepped to another range of same line: %s", s.GetData());
16130fdc8d8SChris Lattner                     }
16230fdc8d8SChris Lattner                 }
1631ac04c30SGreg Clayton                 else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(m_thread.CalculateTarget().get())
164843bfb2cSJim Ingham                          != pc_load_addr)
165843bfb2cSJim Ingham                 {
166843bfb2cSJim Ingham                     // Another thing that sometimes happens here is that we step out of one line into the MIDDLE of another
167843bfb2cSJim Ingham                     // line.  So far I mostly see this due to bugs in the debug information.
168843bfb2cSJim Ingham                     // But we probably don't want to be in the middle of a line range, so in that case reset the stepping
169843bfb2cSJim Ingham                     // range to the line we've stepped into the middle of and continue.
170843bfb2cSJim Ingham                     m_addr_context = new_context;
171c4c9fedcSJim Ingham                     m_address_ranges.clear();
172c4c9fedcSJim Ingham                     AddRange(m_addr_context.line_entry.range);
173843bfb2cSJim Ingham                     ret_value = true;
174843bfb2cSJim Ingham                     if (log)
175843bfb2cSJim Ingham                     {
176843bfb2cSJim Ingham                         StreamString s;
177927bfa3fSJim Ingham                         m_addr_context.line_entry.Dump (&s,
1781ac04c30SGreg Clayton                                                         m_thread.CalculateTarget().get(),
179927bfa3fSJim Ingham                                                         true,
180927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
181927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
182927bfa3fSJim Ingham                                                         true);
183843bfb2cSJim Ingham 
184843bfb2cSJim Ingham                         log->Printf ("Step range plan stepped to the middle of new line(%d): %s, continuing to clear this line.",
185843bfb2cSJim Ingham                                      new_context.line_entry.line,
186843bfb2cSJim Ingham                                      s.GetData());
187843bfb2cSJim Ingham                     }
188843bfb2cSJim Ingham 
189843bfb2cSJim Ingham                 }
190843bfb2cSJim Ingham             }
191843bfb2cSJim Ingham 
19230fdc8d8SChris Lattner         }
19330fdc8d8SChris Lattner 
19430fdc8d8SChris Lattner     }
19530fdc8d8SChris Lattner 
19630fdc8d8SChris Lattner     if (!ret_value && log)
197d01b2953SDaniel Malea         log->Printf ("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
19830fdc8d8SChris Lattner 
19930fdc8d8SChris Lattner     return ret_value;
20030fdc8d8SChris Lattner }
20130fdc8d8SChris Lattner 
20230fdc8d8SChris Lattner bool
20330fdc8d8SChris Lattner ThreadPlanStepRange::InSymbol()
20430fdc8d8SChris Lattner {
20530fdc8d8SChris Lattner     lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
20630fdc8d8SChris Lattner     if (m_addr_context.function != NULL)
20730fdc8d8SChris Lattner     {
2081ac04c30SGreg Clayton         return m_addr_context.function->GetAddressRange().ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
20930fdc8d8SChris Lattner     }
210e7612134SGreg Clayton     else if (m_addr_context.symbol)
21130fdc8d8SChris Lattner     {
212e7612134SGreg Clayton         AddressRange range(m_addr_context.symbol->GetAddress(), m_addr_context.symbol->GetByteSize());
213e7612134SGreg Clayton         return range.ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
21430fdc8d8SChris Lattner     }
21530fdc8d8SChris Lattner     return false;
21630fdc8d8SChris Lattner }
21730fdc8d8SChris Lattner 
21830fdc8d8SChris Lattner // FIXME: This should also handle inlining if we aren't going to do inlining in the
21930fdc8d8SChris Lattner // main stack.
22030fdc8d8SChris Lattner //
22130fdc8d8SChris Lattner // Ideally we should remember the whole stack frame list, and then compare that
22230fdc8d8SChris Lattner // to the current list.
22330fdc8d8SChris Lattner 
224b5c0d1ccSJim Ingham lldb::FrameComparison
225b5c0d1ccSJim Ingham ThreadPlanStepRange::CompareCurrentFrameToStartFrame()
22630fdc8d8SChris Lattner {
227b5c0d1ccSJim Ingham     FrameComparison frame_order;
2287ce490c6SJim Ingham 
229b5c0d1ccSJim Ingham     StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
2307ce490c6SJim Ingham 
231b5c0d1ccSJim Ingham     if (cur_frame_id == m_stack_id)
23230fdc8d8SChris Lattner     {
233b5c0d1ccSJim Ingham         frame_order = eFrameCompareEqual;
23430fdc8d8SChris Lattner     }
235b5c0d1ccSJim Ingham     else if (cur_frame_id < m_stack_id)
23630fdc8d8SChris Lattner     {
237b5c0d1ccSJim Ingham         frame_order = eFrameCompareYounger;
23830fdc8d8SChris Lattner     }
23930fdc8d8SChris Lattner     else
24030fdc8d8SChris Lattner     {
241b5c0d1ccSJim Ingham         frame_order = eFrameCompareOlder;
24230fdc8d8SChris Lattner     }
243b5c0d1ccSJim Ingham     return frame_order;
24430fdc8d8SChris Lattner }
24530fdc8d8SChris Lattner 
24630fdc8d8SChris Lattner bool
24730fdc8d8SChris Lattner ThreadPlanStepRange::StopOthers ()
24830fdc8d8SChris Lattner {
24930fdc8d8SChris Lattner     if (m_stop_others == lldb::eOnlyThisThread
25030fdc8d8SChris Lattner         || m_stop_others == lldb::eOnlyDuringStepping)
25130fdc8d8SChris Lattner         return true;
25230fdc8d8SChris Lattner     else
25330fdc8d8SChris Lattner         return false;
25430fdc8d8SChris Lattner }
25530fdc8d8SChris Lattner 
256564d8bc2SJim Ingham InstructionList *
257564d8bc2SJim Ingham ThreadPlanStepRange::GetInstructionsForAddress(lldb::addr_t addr, size_t &range_index, size_t &insn_offset)
258564d8bc2SJim Ingham {
259564d8bc2SJim Ingham     size_t num_ranges = m_address_ranges.size();
260564d8bc2SJim Ingham     for (size_t i = 0; i < num_ranges; i++)
261564d8bc2SJim Ingham     {
262564d8bc2SJim Ingham         if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget()))
263564d8bc2SJim Ingham         {
264564d8bc2SJim Ingham             // Some joker added a zero size range to the stepping range...
265564d8bc2SJim Ingham             if (m_address_ranges[i].GetByteSize() == 0)
266564d8bc2SJim Ingham                 return NULL;
267564d8bc2SJim Ingham 
268564d8bc2SJim Ingham             if (!m_instruction_ranges[i])
269564d8bc2SJim Ingham             {
270564d8bc2SJim Ingham                 //Disassemble the address range given:
271564d8bc2SJim Ingham                 ExecutionContext exe_ctx (m_thread.GetProcess());
2720f063ba6SJim Ingham                 const char *plugin_name = NULL;
2730f063ba6SJim Ingham                 const char *flavor = NULL;
274564d8bc2SJim Ingham                 m_instruction_ranges[i] = Disassembler::DisassembleRange(GetTarget().GetArchitecture(),
2750f063ba6SJim Ingham                                                                          plugin_name,
2760f063ba6SJim Ingham                                                                          flavor,
277564d8bc2SJim Ingham                                                                          exe_ctx,
278564d8bc2SJim Ingham                                                                          m_address_ranges[i]);
279564d8bc2SJim Ingham 
280564d8bc2SJim Ingham             }
281564d8bc2SJim Ingham             if (!m_instruction_ranges[i])
282564d8bc2SJim Ingham                 return NULL;
283564d8bc2SJim Ingham             else
284564d8bc2SJim Ingham             {
285564d8bc2SJim Ingham                 // Find where we are in the instruction list as well.  If we aren't at an instruction,
286564d8bc2SJim Ingham                 // return NULL.  In this case, we're probably lost, and shouldn't try to do anything fancy.
287564d8bc2SJim Ingham 
288564d8bc2SJim Ingham                 insn_offset = m_instruction_ranges[i]->GetInstructionList().GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
289564d8bc2SJim Ingham                 if (insn_offset == UINT32_MAX)
290564d8bc2SJim Ingham                     return NULL;
291564d8bc2SJim Ingham                 else
292564d8bc2SJim Ingham                 {
293564d8bc2SJim Ingham                     range_index = i;
294564d8bc2SJim Ingham                     return &m_instruction_ranges[i]->GetInstructionList();
295564d8bc2SJim Ingham                 }
296564d8bc2SJim Ingham             }
297564d8bc2SJim Ingham         }
298564d8bc2SJim Ingham     }
299564d8bc2SJim Ingham     return NULL;
300564d8bc2SJim Ingham }
301564d8bc2SJim Ingham 
302564d8bc2SJim Ingham void
303564d8bc2SJim Ingham ThreadPlanStepRange::ClearNextBranchBreakpoint()
304564d8bc2SJim Ingham {
305564d8bc2SJim Ingham     if (m_next_branch_bp_sp)
306564d8bc2SJim Ingham     {
307*5160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
3080c61dee1SJim Ingham         if (log)
3090c61dee1SJim Ingham             log->Printf ("Removing next branch breakpoint: %d.", m_next_branch_bp_sp->GetID());
310564d8bc2SJim Ingham         GetTarget().RemoveBreakpointByID (m_next_branch_bp_sp->GetID());
311564d8bc2SJim Ingham         m_next_branch_bp_sp.reset();
312564d8bc2SJim Ingham     }
313564d8bc2SJim Ingham }
314564d8bc2SJim Ingham 
315564d8bc2SJim Ingham bool
316564d8bc2SJim Ingham ThreadPlanStepRange::SetNextBranchBreakpoint ()
317564d8bc2SJim Ingham {
3180c61dee1SJim Ingham     if (m_next_branch_bp_sp)
3190c61dee1SJim Ingham         return true;
3200c61dee1SJim Ingham 
321*5160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
322564d8bc2SJim Ingham     // Stepping through ranges using breakpoints doesn't work yet, but with this off we fall back to instruction
323564d8bc2SJim Ingham     // single stepping.
3240c61dee1SJim Ingham     if (!m_use_fast_step)
325564d8bc2SJim Ingham          return false;
3260c61dee1SJim Ingham 
327564d8bc2SJim Ingham     lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC();
328564d8bc2SJim Ingham     // Find the current address in our address ranges, and fetch the disassembly if we haven't already:
329564d8bc2SJim Ingham     size_t pc_index;
330564d8bc2SJim Ingham     size_t range_index;
331564d8bc2SJim Ingham     InstructionList *instructions = GetInstructionsForAddress (cur_addr, range_index, pc_index);
332564d8bc2SJim Ingham     if (instructions == NULL)
333564d8bc2SJim Ingham         return false;
334564d8bc2SJim Ingham     else
335564d8bc2SJim Ingham     {
336564d8bc2SJim Ingham         uint32_t branch_index;
337564d8bc2SJim Ingham         branch_index = instructions->GetIndexOfNextBranchInstruction (pc_index);
338564d8bc2SJim Ingham 
339564d8bc2SJim Ingham         Address run_to_address;
340564d8bc2SJim Ingham 
341564d8bc2SJim Ingham         // If we didn't find a branch, run to the end of the range.
342564d8bc2SJim Ingham         if (branch_index == UINT32_MAX)
343564d8bc2SJim Ingham         {
3440c61dee1SJim Ingham             branch_index = instructions->GetSize() - 1;
345564d8bc2SJim Ingham         }
3460c61dee1SJim Ingham 
347564d8bc2SJim Ingham         if (branch_index - pc_index > 1)
348564d8bc2SJim Ingham         {
349564d8bc2SJim Ingham             const bool is_internal = true;
350564d8bc2SJim Ingham             run_to_address = instructions->GetInstructionAtIndex(branch_index)->GetAddress();
351564d8bc2SJim Ingham             m_next_branch_bp_sp = GetTarget().CreateBreakpoint(run_to_address, is_internal);
3522995077dSJim Ingham             if (m_next_branch_bp_sp)
3532995077dSJim Ingham             {
3540c61dee1SJim Ingham                 if (log)
3550c61dee1SJim Ingham                 {
3560c61dee1SJim Ingham                     lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
3570c61dee1SJim Ingham                     BreakpointLocationSP bp_loc = m_next_branch_bp_sp->GetLocationAtIndex(0);
3580c61dee1SJim Ingham                     if (bp_loc)
3590c61dee1SJim Ingham                     {
3600c61dee1SJim Ingham                         BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
3610c61dee1SJim Ingham                         if (bp_site)
3620c61dee1SJim Ingham                         {
3630c61dee1SJim Ingham                             bp_site_id = bp_site->GetID();
3640c61dee1SJim Ingham                         }
3650c61dee1SJim Ingham                     }
3660c61dee1SJim Ingham                     log->Printf ("ThreadPlanStepRange::SetNextBranchBreakpoint - Setting breakpoint %d (site %d) to run to address 0x%" PRIx64,
3670c61dee1SJim Ingham                                  m_next_branch_bp_sp->GetID(),
3680c61dee1SJim Ingham                                  bp_site_id,
3690c61dee1SJim Ingham                                  run_to_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget()));
3700c61dee1SJim Ingham                 }
371564d8bc2SJim Ingham                 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
3722995077dSJim Ingham                 m_next_branch_bp_sp->SetBreakpointKind ("next-branch-location");
3730c61dee1SJim Ingham                 return true;
3742995077dSJim Ingham             }
3752995077dSJim Ingham             else
3762995077dSJim Ingham                 return false;
377564d8bc2SJim Ingham         }
378564d8bc2SJim Ingham     }
379564d8bc2SJim Ingham     return false;
380564d8bc2SJim Ingham }
381564d8bc2SJim Ingham 
382564d8bc2SJim Ingham bool
383564d8bc2SJim Ingham ThreadPlanStepRange::NextRangeBreakpointExplainsStop (lldb::StopInfoSP stop_info_sp)
384564d8bc2SJim Ingham {
385*5160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
386564d8bc2SJim Ingham     if (!m_next_branch_bp_sp)
387564d8bc2SJim Ingham         return false;
388564d8bc2SJim Ingham 
389564d8bc2SJim Ingham     break_id_t bp_site_id = stop_info_sp->GetValue();
390564d8bc2SJim Ingham     BreakpointSiteSP bp_site_sp = m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id);
3910c61dee1SJim Ingham     if (!bp_site_sp)
3920c61dee1SJim Ingham         return false;
3930c61dee1SJim Ingham     else if (!bp_site_sp->IsBreakpointAtThisSite (m_next_branch_bp_sp->GetID()))
394564d8bc2SJim Ingham         return false;
395564d8bc2SJim Ingham     else
3960c61dee1SJim Ingham     {
3970c61dee1SJim Ingham         // If we've hit the next branch breakpoint, then clear it.
3980c61dee1SJim Ingham         size_t num_owners = bp_site_sp->GetNumberOfOwners();
3990c61dee1SJim Ingham         bool explains_stop = true;
4000c61dee1SJim Ingham         // If all the owners are internal, then we are probably just stepping over this range from multiple threads,
4010c61dee1SJim Ingham         // or multiple frames, so we want to continue.  If one is not internal, then we should not explain the stop,
4020c61dee1SJim Ingham         // and let the user breakpoint handle the stop.
4030c61dee1SJim Ingham         for (size_t i = 0; i < num_owners; i++)
4040c61dee1SJim Ingham         {
4050c61dee1SJim Ingham             if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
4060c61dee1SJim Ingham             {
4070c61dee1SJim Ingham                 explains_stop = false;
4080c61dee1SJim Ingham                 break;
4090c61dee1SJim Ingham             }
4100c61dee1SJim Ingham         }
4110c61dee1SJim Ingham         if (log)
4120c61dee1SJim Ingham             log->Printf ("ThreadPlanStepRange::NextRangeBreakpointExplainsStop - Hit next range breakpoint which has %zu owners - explains stop: %u.",
4130c61dee1SJim Ingham                         num_owners,
4140c61dee1SJim Ingham                         explains_stop);
4150c61dee1SJim Ingham         ClearNextBranchBreakpoint();
4160c61dee1SJim Ingham         return  explains_stop;
4170c61dee1SJim Ingham     }
418564d8bc2SJim Ingham }
419564d8bc2SJim Ingham 
420564d8bc2SJim Ingham bool
42130fdc8d8SChris Lattner ThreadPlanStepRange::WillStop ()
42230fdc8d8SChris Lattner {
42330fdc8d8SChris Lattner     return true;
42430fdc8d8SChris Lattner }
42530fdc8d8SChris Lattner 
42630fdc8d8SChris Lattner StateType
42706e827ccSJim Ingham ThreadPlanStepRange::GetPlanRunState ()
42830fdc8d8SChris Lattner {
429564d8bc2SJim Ingham     if (m_next_branch_bp_sp)
430564d8bc2SJim Ingham         return eStateRunning;
431564d8bc2SJim Ingham     else
43230fdc8d8SChris Lattner         return eStateStepping;
43330fdc8d8SChris Lattner }
43430fdc8d8SChris Lattner 
43530fdc8d8SChris Lattner bool
43630fdc8d8SChris Lattner ThreadPlanStepRange::MischiefManaged ()
43730fdc8d8SChris Lattner {
438927bfa3fSJim Ingham     // If we have pushed some plans between ShouldStop & MischiefManaged, then we're not done...
439927bfa3fSJim Ingham     // I do this check first because we might have stepped somewhere that will fool InRange into
440927bfa3fSJim Ingham     // thinking it needs to step past the end of that line.  This happens, for instance, when stepping
441927bfa3fSJim Ingham     // over inlined code that is in the middle of the current line.
442927bfa3fSJim Ingham 
443927bfa3fSJim Ingham     if (!m_no_more_plans)
444927bfa3fSJim Ingham         return false;
445927bfa3fSJim Ingham 
44630fdc8d8SChris Lattner     bool done = true;
44730fdc8d8SChris Lattner     if (!IsPlanComplete())
44830fdc8d8SChris Lattner     {
44930fdc8d8SChris Lattner         if (InRange())
45030fdc8d8SChris Lattner         {
45130fdc8d8SChris Lattner             done = false;
45230fdc8d8SChris Lattner         }
453b5c0d1ccSJim Ingham         else
454b5c0d1ccSJim Ingham         {
455b5c0d1ccSJim Ingham             FrameComparison frame_order = CompareCurrentFrameToStartFrame();
456b5c0d1ccSJim Ingham             if (frame_order != eFrameCompareOlder)
45730fdc8d8SChris Lattner             {
45830fdc8d8SChris Lattner                 if (m_no_more_plans)
45930fdc8d8SChris Lattner                     done = true;
46030fdc8d8SChris Lattner                 else
46130fdc8d8SChris Lattner                     done = false;
46230fdc8d8SChris Lattner             }
46330fdc8d8SChris Lattner             else
46430fdc8d8SChris Lattner                 done = true;
46530fdc8d8SChris Lattner         }
466b5c0d1ccSJim Ingham     }
46730fdc8d8SChris Lattner 
46830fdc8d8SChris Lattner     if (done)
46930fdc8d8SChris Lattner     {
470*5160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
47130fdc8d8SChris Lattner         if (log)
47230fdc8d8SChris Lattner             log->Printf("Completed step through range plan.");
4730c61dee1SJim Ingham         ClearNextBranchBreakpoint();
47430fdc8d8SChris Lattner         ThreadPlan::MischiefManaged ();
47530fdc8d8SChris Lattner         return true;
47630fdc8d8SChris Lattner     }
47730fdc8d8SChris Lattner     else
47830fdc8d8SChris Lattner     {
47930fdc8d8SChris Lattner         return false;
48030fdc8d8SChris Lattner     }
48130fdc8d8SChris Lattner 
48230fdc8d8SChris Lattner }
48364e7ead1SJim Ingham 
48464e7ead1SJim Ingham bool
48564e7ead1SJim Ingham ThreadPlanStepRange::IsPlanStale ()
48664e7ead1SJim Ingham {
487*5160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
48864e7ead1SJim Ingham     FrameComparison frame_order = CompareCurrentFrameToStartFrame();
48964e7ead1SJim Ingham 
49064e7ead1SJim Ingham     if (frame_order == eFrameCompareOlder)
49164e7ead1SJim Ingham     {
49264e7ead1SJim Ingham         if (log)
49364e7ead1SJim Ingham         {
49464e7ead1SJim Ingham             log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've stepped out.");
49564e7ead1SJim Ingham         }
49664e7ead1SJim Ingham         return true;
49764e7ead1SJim Ingham     }
49864e7ead1SJim Ingham     else if (frame_order == eFrameCompareEqual && InSymbol())
49964e7ead1SJim Ingham     {
50064e7ead1SJim Ingham         // If we are not in a place we should step through, we've gotten stale.
50164e7ead1SJim Ingham         // One tricky bit here is that some stubs don't push a frame, so we should.
50264e7ead1SJim Ingham         // check that we are in the same symbol.
50364e7ead1SJim Ingham         if (!InRange())
50464e7ead1SJim Ingham         {
50564e7ead1SJim Ingham             return true;
50664e7ead1SJim Ingham         }
50764e7ead1SJim Ingham     }
50864e7ead1SJim Ingham     return false;
50964e7ead1SJim Ingham }
510