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 // C Includes
1130fdc8d8SChris Lattner // C++ Includes
1230fdc8d8SChris Lattner // Other libraries and framework includes
1330fdc8d8SChris Lattner // Project includes
14*e65b2cf2SEugene Zelenko #include "lldb/Target/ThreadPlanStepRange.h"
150c61dee1SJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
160c61dee1SJim Ingham #include "lldb/Breakpoint/BreakpointSite.h"
17564d8bc2SJim Ingham #include "lldb/Core/Disassembler.h"
1830fdc8d8SChris Lattner #include "lldb/Core/Log.h"
1930fdc8d8SChris Lattner #include "lldb/Core/Stream.h"
2030fdc8d8SChris Lattner #include "lldb/Symbol/Function.h"
2130fdc8d8SChris Lattner #include "lldb/Symbol/Symbol.h"
22564d8bc2SJim Ingham #include "lldb/Target/ExecutionContext.h"
23f4b47e15SGreg Clayton #include "lldb/Target/Process.h"
24f4b47e15SGreg Clayton #include "lldb/Target/RegisterContext.h"
25f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
26564d8bc2SJim Ingham #include "lldb/Target/Target.h"
27f4b47e15SGreg Clayton #include "lldb/Target/Thread.h"
28564d8bc2SJim Ingham #include "lldb/Target/ThreadPlanRunToAddress.h"
2930fdc8d8SChris Lattner 
3030fdc8d8SChris Lattner using namespace lldb;
3130fdc8d8SChris Lattner using namespace lldb_private;
3230fdc8d8SChris Lattner 
3330fdc8d8SChris Lattner //----------------------------------------------------------------------
3430fdc8d8SChris Lattner // ThreadPlanStepRange: Step through a stack range, either stepping over or into
3530fdc8d8SChris Lattner // based on the value of \a type.
3630fdc8d8SChris Lattner //----------------------------------------------------------------------
3730fdc8d8SChris Lattner 
38242e0ad7SJim Ingham ThreadPlanStepRange::ThreadPlanStepRange (ThreadPlanKind kind,
39242e0ad7SJim Ingham                                           const char *name,
40242e0ad7SJim Ingham                                           Thread &thread,
41242e0ad7SJim Ingham                                           const AddressRange &range,
42242e0ad7SJim Ingham                                           const SymbolContext &addr_context,
432bdbfd50SJim Ingham                                           lldb::RunMode stop_others,
442bdbfd50SJim Ingham                                           bool given_ranges_only) :
4535b21fefSJim Ingham     ThreadPlan (kind, name, thread, eVoteNoOpinion, eVoteNoOpinion),
4630fdc8d8SChris Lattner     m_addr_context (addr_context),
47c4c9fedcSJim Ingham     m_address_ranges (),
4830fdc8d8SChris Lattner     m_stop_others (stop_others),
4930fdc8d8SChris Lattner     m_stack_id (),
50862d1bbdSJim Ingham     m_parent_stack_id(),
51c982c768SGreg Clayton     m_no_more_plans (false),
520c61dee1SJim Ingham     m_first_run_event (true),
532bdbfd50SJim Ingham     m_use_fast_step(false),
542bdbfd50SJim Ingham     m_given_ranges_only (given_ranges_only)
5530fdc8d8SChris Lattner {
5617d023f6SJim Ingham     m_use_fast_step = GetTarget().GetUseFastStepping();
57c4c9fedcSJim Ingham     AddRange(range);
5830fdc8d8SChris Lattner     m_stack_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
5976447851SJim Ingham     StackFrameSP parent_stack = m_thread.GetStackFrameAtIndex(1);
6076447851SJim Ingham     if (parent_stack)
6176447851SJim Ingham       m_parent_stack_id = parent_stack->GetStackID();
6230fdc8d8SChris Lattner }
6330fdc8d8SChris Lattner 
6430fdc8d8SChris Lattner ThreadPlanStepRange::~ThreadPlanStepRange ()
6530fdc8d8SChris Lattner {
66564d8bc2SJim Ingham     ClearNextBranchBreakpoint();
6756d40428SJim Ingham 
6856d40428SJim Ingham     size_t num_instruction_ranges = m_instruction_ranges.size();
6956d40428SJim Ingham 
7056d40428SJim Ingham     // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
7156d40428SJim Ingham     // I'll fix that but for now, just clear the list and it will go away nicely.
7256d40428SJim Ingham     for (size_t i = 0; i < num_instruction_ranges; i++)
7356d40428SJim Ingham     {
7456d40428SJim Ingham         if (m_instruction_ranges[i])
7556d40428SJim Ingham             m_instruction_ranges[i]->GetInstructionList().Clear();
7656d40428SJim Ingham     }
77564d8bc2SJim Ingham }
78564d8bc2SJim Ingham 
79564d8bc2SJim Ingham void
80564d8bc2SJim Ingham ThreadPlanStepRange::DidPush ()
81564d8bc2SJim Ingham {
82564d8bc2SJim Ingham     // See if we can find a "next range" breakpoint:
83564d8bc2SJim Ingham     SetNextBranchBreakpoint();
8430fdc8d8SChris Lattner }
8530fdc8d8SChris Lattner 
8630fdc8d8SChris Lattner bool
8730fdc8d8SChris Lattner ThreadPlanStepRange::ValidatePlan (Stream *error)
8830fdc8d8SChris Lattner {
8930fdc8d8SChris Lattner     return true;
9030fdc8d8SChris Lattner }
9130fdc8d8SChris Lattner 
9230fdc8d8SChris Lattner Vote
9330fdc8d8SChris Lattner ThreadPlanStepRange::ShouldReportStop (Event *event_ptr)
9430fdc8d8SChris Lattner {
955160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
962cad65a5SGreg Clayton 
972cad65a5SGreg Clayton     const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo;
982cad65a5SGreg Clayton     if (log)
99411c0ce8SGreg Clayton         log->Printf ("ThreadPlanStepRange::ShouldReportStop() returning vote %i\n", vote);
1002cad65a5SGreg Clayton     return vote;
10130fdc8d8SChris Lattner }
10230fdc8d8SChris Lattner 
103c4c9fedcSJim Ingham void
104c4c9fedcSJim Ingham ThreadPlanStepRange::AddRange(const AddressRange &new_range)
105c4c9fedcSJim Ingham {
106c4c9fedcSJim Ingham     // For now I'm just adding the ranges.  At some point we may want to
107c4c9fedcSJim Ingham     // condense the ranges if they overlap, though I don't think it is likely
108c4c9fedcSJim Ingham     // to be very important.
109c4c9fedcSJim Ingham     m_address_ranges.push_back (new_range);
11056d40428SJim Ingham 
11156d40428SJim Ingham     // Fill the slot for this address range with an empty DisassemblerSP in the instruction ranges. I want the
11256d40428SJim Ingham     // indices to match, but I don't want to do the work to disassemble this range if I don't step into it.
113564d8bc2SJim Ingham     m_instruction_ranges.push_back (DisassemblerSP());
114c4c9fedcSJim Ingham }
115c4c9fedcSJim Ingham 
116c4c9fedcSJim Ingham void
117c4c9fedcSJim Ingham ThreadPlanStepRange::DumpRanges(Stream *s)
118c4c9fedcSJim Ingham {
119c4c9fedcSJim Ingham     size_t num_ranges = m_address_ranges.size();
120c4c9fedcSJim Ingham     if (num_ranges == 1)
121c4c9fedcSJim Ingham     {
1221ac04c30SGreg Clayton         m_address_ranges[0].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
123c4c9fedcSJim Ingham     }
124c4c9fedcSJim Ingham     else
125c4c9fedcSJim Ingham     {
126c4c9fedcSJim Ingham         for (size_t i = 0; i < num_ranges; i++)
127c4c9fedcSJim Ingham         {
1288c0970feSPavel Labath             s->Printf(" %" PRIu64 ": ", uint64_t(i));
1291ac04c30SGreg Clayton             m_address_ranges[i].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
130c4c9fedcSJim Ingham         }
131c4c9fedcSJim Ingham     }
132c4c9fedcSJim Ingham }
133c4c9fedcSJim Ingham 
13430fdc8d8SChris Lattner bool
13530fdc8d8SChris Lattner ThreadPlanStepRange::InRange ()
13630fdc8d8SChris Lattner {
1375160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
13830fdc8d8SChris Lattner     bool ret_value = false;
13930fdc8d8SChris Lattner 
14030fdc8d8SChris Lattner     lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
14130fdc8d8SChris Lattner 
142c4c9fedcSJim Ingham     size_t num_ranges = m_address_ranges.size();
143c4c9fedcSJim Ingham     for (size_t i = 0; i < num_ranges; i++)
144c4c9fedcSJim Ingham     {
1451ac04c30SGreg Clayton         ret_value = m_address_ranges[i].ContainsLoadAddress(pc_load_addr, m_thread.CalculateTarget().get());
146c4c9fedcSJim Ingham         if (ret_value)
147c4c9fedcSJim Ingham             break;
148c4c9fedcSJim Ingham     }
14930fdc8d8SChris Lattner 
1502bdbfd50SJim Ingham     if (!ret_value && !m_given_ranges_only)
15130fdc8d8SChris Lattner     {
15230fdc8d8SChris Lattner         // See if we've just stepped to another part of the same line number...
153b57e4a1bSJason Molenda         StackFrame *frame = m_thread.GetStackFrameAtIndex(0).get();
15430fdc8d8SChris Lattner 
15530fdc8d8SChris Lattner         SymbolContext new_context(frame->GetSymbolContext(eSymbolContextEverything));
15630fdc8d8SChris Lattner         if (m_addr_context.line_entry.IsValid() && new_context.line_entry.IsValid())
15730fdc8d8SChris Lattner         {
158843bfb2cSJim Ingham             if (m_addr_context.line_entry.file == new_context.line_entry.file)
159843bfb2cSJim Ingham             {
160843bfb2cSJim Ingham                 if (m_addr_context.line_entry.line == new_context.line_entry.line)
16130fdc8d8SChris Lattner                 {
16230fdc8d8SChris Lattner                     m_addr_context = new_context;
16325d5b10bSJason Molenda                     AddRange(m_addr_context.line_entry.GetSameLineContiguousAddressRange());
16430fdc8d8SChris Lattner                     ret_value = true;
16530fdc8d8SChris Lattner                     if (log)
16630fdc8d8SChris Lattner                     {
16730fdc8d8SChris Lattner                         StreamString s;
168927bfa3fSJim Ingham                         m_addr_context.line_entry.Dump (&s,
1691ac04c30SGreg Clayton                                                         m_thread.CalculateTarget().get(),
170927bfa3fSJim Ingham                                                         true,
171927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
172927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
173927bfa3fSJim Ingham                                                         true);
17430fdc8d8SChris Lattner 
17530fdc8d8SChris Lattner                         log->Printf ("Step range plan stepped to another range of same line: %s", s.GetData());
17630fdc8d8SChris Lattner                     }
17730fdc8d8SChris Lattner                 }
1782b89a531SJim Ingham                 else if (new_context.line_entry.line == 0)
1792b89a531SJim Ingham                 {
1802b89a531SJim Ingham                     new_context.line_entry.line = m_addr_context.line_entry.line;
1812b89a531SJim Ingham                     m_addr_context = new_context;
18225d5b10bSJason Molenda                     AddRange(m_addr_context.line_entry.GetSameLineContiguousAddressRange());
1832b89a531SJim Ingham                     ret_value = true;
1842b89a531SJim Ingham                     if (log)
1852b89a531SJim Ingham                     {
1862b89a531SJim Ingham                         StreamString s;
1872b89a531SJim Ingham                         m_addr_context.line_entry.Dump (&s,
1882b89a531SJim Ingham                                                         m_thread.CalculateTarget().get(),
1892b89a531SJim Ingham                                                         true,
1902b89a531SJim Ingham                                                         Address::DumpStyleLoadAddress,
1912b89a531SJim Ingham                                                         Address::DumpStyleLoadAddress,
1922b89a531SJim Ingham                                                         true);
1932b89a531SJim Ingham 
1942b89a531SJim Ingham                         log->Printf ("Step range plan stepped to a range at linenumber 0 stepping through that range: %s", s.GetData());
1952b89a531SJim Ingham                     }
1962b89a531SJim Ingham                 }
1971ac04c30SGreg Clayton                 else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(m_thread.CalculateTarget().get())
198843bfb2cSJim Ingham                          != pc_load_addr)
199843bfb2cSJim Ingham                 {
200843bfb2cSJim Ingham                     // Another thing that sometimes happens here is that we step out of one line into the MIDDLE of another
201843bfb2cSJim Ingham                     // line.  So far I mostly see this due to bugs in the debug information.
202843bfb2cSJim Ingham                     // But we probably don't want to be in the middle of a line range, so in that case reset the stepping
203843bfb2cSJim Ingham                     // range to the line we've stepped into the middle of and continue.
204843bfb2cSJim Ingham                     m_addr_context = new_context;
205c4c9fedcSJim Ingham                     m_address_ranges.clear();
206c4c9fedcSJim Ingham                     AddRange(m_addr_context.line_entry.range);
207843bfb2cSJim Ingham                     ret_value = true;
208843bfb2cSJim Ingham                     if (log)
209843bfb2cSJim Ingham                     {
210843bfb2cSJim Ingham                         StreamString s;
211927bfa3fSJim Ingham                         m_addr_context.line_entry.Dump (&s,
2121ac04c30SGreg Clayton                                                         m_thread.CalculateTarget().get(),
213927bfa3fSJim Ingham                                                         true,
214927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
215927bfa3fSJim Ingham                                                         Address::DumpStyleLoadAddress,
216927bfa3fSJim Ingham                                                         true);
217843bfb2cSJim Ingham 
218843bfb2cSJim Ingham                         log->Printf ("Step range plan stepped to the middle of new line(%d): %s, continuing to clear this line.",
219843bfb2cSJim Ingham                                      new_context.line_entry.line,
220843bfb2cSJim Ingham                                      s.GetData());
221843bfb2cSJim Ingham                     }
222843bfb2cSJim Ingham                 }
223843bfb2cSJim Ingham             }
22430fdc8d8SChris Lattner         }
22530fdc8d8SChris Lattner     }
22630fdc8d8SChris Lattner 
22730fdc8d8SChris Lattner     if (!ret_value && log)
228d01b2953SDaniel Malea         log->Printf ("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
22930fdc8d8SChris Lattner 
23030fdc8d8SChris Lattner     return ret_value;
23130fdc8d8SChris Lattner }
23230fdc8d8SChris Lattner 
23330fdc8d8SChris Lattner bool
23430fdc8d8SChris Lattner ThreadPlanStepRange::InSymbol()
23530fdc8d8SChris Lattner {
23630fdc8d8SChris Lattner     lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
237*e65b2cf2SEugene Zelenko     if (m_addr_context.function != nullptr)
23830fdc8d8SChris Lattner     {
2391ac04c30SGreg Clayton         return m_addr_context.function->GetAddressRange().ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
24030fdc8d8SChris Lattner     }
241358cf1eaSGreg Clayton     else if (m_addr_context.symbol && m_addr_context.symbol->ValueIsAddress())
24230fdc8d8SChris Lattner     {
243358cf1eaSGreg Clayton         AddressRange range(m_addr_context.symbol->GetAddressRef(), m_addr_context.symbol->GetByteSize());
244e7612134SGreg Clayton         return range.ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
24530fdc8d8SChris Lattner     }
24630fdc8d8SChris Lattner     return false;
24730fdc8d8SChris Lattner }
24830fdc8d8SChris Lattner 
24930fdc8d8SChris Lattner // FIXME: This should also handle inlining if we aren't going to do inlining in the
25030fdc8d8SChris Lattner // main stack.
25130fdc8d8SChris Lattner //
25230fdc8d8SChris Lattner // Ideally we should remember the whole stack frame list, and then compare that
25330fdc8d8SChris Lattner // to the current list.
25430fdc8d8SChris Lattner 
255b5c0d1ccSJim Ingham lldb::FrameComparison
256b5c0d1ccSJim Ingham ThreadPlanStepRange::CompareCurrentFrameToStartFrame()
25730fdc8d8SChris Lattner {
258b5c0d1ccSJim Ingham     FrameComparison frame_order;
2597ce490c6SJim Ingham 
260b5c0d1ccSJim Ingham     StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
2617ce490c6SJim Ingham 
262b5c0d1ccSJim Ingham     if (cur_frame_id == m_stack_id)
26330fdc8d8SChris Lattner     {
264b5c0d1ccSJim Ingham         frame_order = eFrameCompareEqual;
26530fdc8d8SChris Lattner     }
266b5c0d1ccSJim Ingham     else if (cur_frame_id < m_stack_id)
26730fdc8d8SChris Lattner     {
268b5c0d1ccSJim Ingham         frame_order = eFrameCompareYounger;
26930fdc8d8SChris Lattner     }
27030fdc8d8SChris Lattner     else
27130fdc8d8SChris Lattner     {
27276447851SJim Ingham         StackFrameSP cur_parent_frame = m_thread.GetStackFrameAtIndex(1);
27376447851SJim Ingham         StackID cur_parent_id;
27476447851SJim Ingham         if (cur_parent_frame)
27576447851SJim Ingham           cur_parent_id = cur_parent_frame->GetStackID();
276862d1bbdSJim Ingham         if (m_parent_stack_id.IsValid()
277862d1bbdSJim Ingham             && cur_parent_id.IsValid()
278862d1bbdSJim Ingham             && m_parent_stack_id == cur_parent_id)
279862d1bbdSJim Ingham            frame_order = eFrameCompareSameParent;
280862d1bbdSJim Ingham         else
281b5c0d1ccSJim Ingham             frame_order = eFrameCompareOlder;
28230fdc8d8SChris Lattner     }
283b5c0d1ccSJim Ingham     return frame_order;
28430fdc8d8SChris Lattner }
28530fdc8d8SChris Lattner 
28630fdc8d8SChris Lattner bool
28730fdc8d8SChris Lattner ThreadPlanStepRange::StopOthers ()
28830fdc8d8SChris Lattner {
289*e65b2cf2SEugene Zelenko     return (m_stop_others == lldb::eOnlyThisThread || m_stop_others == lldb::eOnlyDuringStepping);
29030fdc8d8SChris Lattner }
29130fdc8d8SChris Lattner 
292564d8bc2SJim Ingham InstructionList *
293564d8bc2SJim Ingham ThreadPlanStepRange::GetInstructionsForAddress(lldb::addr_t addr, size_t &range_index, size_t &insn_offset)
294564d8bc2SJim Ingham {
295564d8bc2SJim Ingham     size_t num_ranges = m_address_ranges.size();
296564d8bc2SJim Ingham     for (size_t i = 0; i < num_ranges; i++)
297564d8bc2SJim Ingham     {
298564d8bc2SJim Ingham         if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget()))
299564d8bc2SJim Ingham         {
300564d8bc2SJim Ingham             // Some joker added a zero size range to the stepping range...
301564d8bc2SJim Ingham             if (m_address_ranges[i].GetByteSize() == 0)
302*e65b2cf2SEugene Zelenko                 return nullptr;
303564d8bc2SJim Ingham 
304564d8bc2SJim Ingham             if (!m_instruction_ranges[i])
305564d8bc2SJim Ingham             {
306564d8bc2SJim Ingham                 //Disassemble the address range given:
307564d8bc2SJim Ingham                 ExecutionContext exe_ctx (m_thread.GetProcess());
308*e65b2cf2SEugene Zelenko                 const char *plugin_name = nullptr;
309*e65b2cf2SEugene Zelenko                 const char *flavor = nullptr;
3106b3e6d54SJason Molenda                 const bool prefer_file_cache = true;
311564d8bc2SJim Ingham                 m_instruction_ranges[i] = Disassembler::DisassembleRange(GetTarget().GetArchitecture(),
3120f063ba6SJim Ingham                                                                          plugin_name,
3130f063ba6SJim Ingham                                                                          flavor,
314564d8bc2SJim Ingham                                                                          exe_ctx,
3156b3e6d54SJason Molenda                                                                          m_address_ranges[i],
3166b3e6d54SJason Molenda                                                                          prefer_file_cache);
317564d8bc2SJim Ingham             }
318564d8bc2SJim Ingham             if (!m_instruction_ranges[i])
319*e65b2cf2SEugene Zelenko                 return nullptr;
320564d8bc2SJim Ingham             else
321564d8bc2SJim Ingham             {
322564d8bc2SJim Ingham                 // Find where we are in the instruction list as well.  If we aren't at an instruction,
323*e65b2cf2SEugene Zelenko                 // return nullptr. In this case, we're probably lost, and shouldn't try to do anything fancy.
324564d8bc2SJim Ingham 
325564d8bc2SJim Ingham                 insn_offset = m_instruction_ranges[i]->GetInstructionList().GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
326564d8bc2SJim Ingham                 if (insn_offset == UINT32_MAX)
327*e65b2cf2SEugene Zelenko                     return nullptr;
328564d8bc2SJim Ingham                 else
329564d8bc2SJim Ingham                 {
330564d8bc2SJim Ingham                     range_index = i;
331564d8bc2SJim Ingham                     return &m_instruction_ranges[i]->GetInstructionList();
332564d8bc2SJim Ingham                 }
333564d8bc2SJim Ingham             }
334564d8bc2SJim Ingham         }
335564d8bc2SJim Ingham     }
336*e65b2cf2SEugene Zelenko     return nullptr;
337564d8bc2SJim Ingham }
338564d8bc2SJim Ingham 
339564d8bc2SJim Ingham void
340564d8bc2SJim Ingham ThreadPlanStepRange::ClearNextBranchBreakpoint()
341564d8bc2SJim Ingham {
342564d8bc2SJim Ingham     if (m_next_branch_bp_sp)
343564d8bc2SJim Ingham     {
3445160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
3450c61dee1SJim Ingham         if (log)
3460c61dee1SJim Ingham             log->Printf ("Removing next branch breakpoint: %d.", m_next_branch_bp_sp->GetID());
347564d8bc2SJim Ingham         GetTarget().RemoveBreakpointByID (m_next_branch_bp_sp->GetID());
348564d8bc2SJim Ingham         m_next_branch_bp_sp.reset();
349564d8bc2SJim Ingham     }
350564d8bc2SJim Ingham }
351564d8bc2SJim Ingham 
352564d8bc2SJim Ingham bool
353564d8bc2SJim Ingham ThreadPlanStepRange::SetNextBranchBreakpoint ()
354564d8bc2SJim Ingham {
3550c61dee1SJim Ingham     if (m_next_branch_bp_sp)
3560c61dee1SJim Ingham         return true;
3570c61dee1SJim Ingham 
3585160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
359564d8bc2SJim Ingham     // Stepping through ranges using breakpoints doesn't work yet, but with this off we fall back to instruction
360564d8bc2SJim Ingham     // single stepping.
3610c61dee1SJim Ingham     if (!m_use_fast_step)
362564d8bc2SJim Ingham          return false;
3630c61dee1SJim Ingham 
364564d8bc2SJim Ingham     lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC();
365564d8bc2SJim Ingham     // Find the current address in our address ranges, and fetch the disassembly if we haven't already:
366564d8bc2SJim Ingham     size_t pc_index;
367564d8bc2SJim Ingham     size_t range_index;
368564d8bc2SJim Ingham     InstructionList *instructions = GetInstructionsForAddress (cur_addr, range_index, pc_index);
369*e65b2cf2SEugene Zelenko     if (instructions == nullptr)
370564d8bc2SJim Ingham         return false;
371564d8bc2SJim Ingham     else
372564d8bc2SJim Ingham     {
373e76e7e93STed Woodward         Target &target = GetThread().GetProcess()->GetTarget();
374564d8bc2SJim Ingham         uint32_t branch_index;
375e76e7e93STed Woodward         branch_index = instructions->GetIndexOfNextBranchInstruction (pc_index, target);
376564d8bc2SJim Ingham 
377564d8bc2SJim Ingham         Address run_to_address;
378564d8bc2SJim Ingham 
379564d8bc2SJim Ingham         // If we didn't find a branch, run to the end of the range.
380564d8bc2SJim Ingham         if (branch_index == UINT32_MAX)
381564d8bc2SJim Ingham         {
382a3f466b9SJim Ingham             uint32_t last_index = instructions->GetSize() - 1;
383a3f466b9SJim Ingham             if (last_index - pc_index > 1)
384a3f466b9SJim Ingham             {
385a3f466b9SJim Ingham                 InstructionSP last_inst = instructions->GetInstructionAtIndex(last_index);
386a3f466b9SJim Ingham                 size_t last_inst_size = last_inst->GetOpcode().GetByteSize();
387a3f466b9SJim Ingham                 run_to_address = last_inst->GetAddress();
388a3f466b9SJim Ingham                 run_to_address.Slide(last_inst_size);
389a3f466b9SJim Ingham             }
390a3f466b9SJim Ingham         }
391a3f466b9SJim Ingham         else if (branch_index - pc_index > 1)
392a3f466b9SJim Ingham         {
393a3f466b9SJim Ingham             run_to_address = instructions->GetInstructionAtIndex(branch_index)->GetAddress();
394564d8bc2SJim Ingham         }
3950c61dee1SJim Ingham 
396a3f466b9SJim Ingham         if (run_to_address.IsValid())
397564d8bc2SJim Ingham         {
398564d8bc2SJim Ingham             const bool is_internal = true;
399eb023e75SGreg Clayton             m_next_branch_bp_sp = GetTarget().CreateBreakpoint(run_to_address, is_internal, false);
4002995077dSJim Ingham             if (m_next_branch_bp_sp)
4012995077dSJim Ingham             {
4020c61dee1SJim Ingham                 if (log)
4030c61dee1SJim Ingham                 {
4040c61dee1SJim Ingham                     lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
4050c61dee1SJim Ingham                     BreakpointLocationSP bp_loc = m_next_branch_bp_sp->GetLocationAtIndex(0);
4060c61dee1SJim Ingham                     if (bp_loc)
4070c61dee1SJim Ingham                     {
4080c61dee1SJim Ingham                         BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
4090c61dee1SJim Ingham                         if (bp_site)
4100c61dee1SJim Ingham                         {
4110c61dee1SJim Ingham                             bp_site_id = bp_site->GetID();
4120c61dee1SJim Ingham                         }
4130c61dee1SJim Ingham                     }
4140c61dee1SJim Ingham                     log->Printf ("ThreadPlanStepRange::SetNextBranchBreakpoint - Setting breakpoint %d (site %d) to run to address 0x%" PRIx64,
4150c61dee1SJim Ingham                                  m_next_branch_bp_sp->GetID(),
4160c61dee1SJim Ingham                                  bp_site_id,
4170c61dee1SJim Ingham                                  run_to_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget()));
4180c61dee1SJim Ingham                 }
419564d8bc2SJim Ingham                 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
4202995077dSJim Ingham                 m_next_branch_bp_sp->SetBreakpointKind ("next-branch-location");
4210c61dee1SJim Ingham                 return true;
4222995077dSJim Ingham             }
4232995077dSJim Ingham             else
4242995077dSJim Ingham                 return false;
425564d8bc2SJim Ingham         }
426564d8bc2SJim Ingham     }
427564d8bc2SJim Ingham     return false;
428564d8bc2SJim Ingham }
429564d8bc2SJim Ingham 
430564d8bc2SJim Ingham bool
431564d8bc2SJim Ingham ThreadPlanStepRange::NextRangeBreakpointExplainsStop (lldb::StopInfoSP stop_info_sp)
432564d8bc2SJim Ingham {
4335160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
434564d8bc2SJim Ingham     if (!m_next_branch_bp_sp)
435564d8bc2SJim Ingham         return false;
436564d8bc2SJim Ingham 
437564d8bc2SJim Ingham     break_id_t bp_site_id = stop_info_sp->GetValue();
438564d8bc2SJim Ingham     BreakpointSiteSP bp_site_sp = m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id);
4390c61dee1SJim Ingham     if (!bp_site_sp)
4400c61dee1SJim Ingham         return false;
4410c61dee1SJim Ingham     else if (!bp_site_sp->IsBreakpointAtThisSite (m_next_branch_bp_sp->GetID()))
442564d8bc2SJim Ingham         return false;
443564d8bc2SJim Ingham     else
4440c61dee1SJim Ingham     {
4450c61dee1SJim Ingham         // If we've hit the next branch breakpoint, then clear it.
4460c61dee1SJim Ingham         size_t num_owners = bp_site_sp->GetNumberOfOwners();
4470c61dee1SJim Ingham         bool explains_stop = true;
4480c61dee1SJim Ingham         // If all the owners are internal, then we are probably just stepping over this range from multiple threads,
4490c61dee1SJim Ingham         // or multiple frames, so we want to continue.  If one is not internal, then we should not explain the stop,
4500c61dee1SJim Ingham         // and let the user breakpoint handle the stop.
4510c61dee1SJim Ingham         for (size_t i = 0; i < num_owners; i++)
4520c61dee1SJim Ingham         {
4530c61dee1SJim Ingham             if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
4540c61dee1SJim Ingham             {
4550c61dee1SJim Ingham                 explains_stop = false;
4560c61dee1SJim Ingham                 break;
4570c61dee1SJim Ingham             }
4580c61dee1SJim Ingham         }
4590c61dee1SJim Ingham         if (log)
46099fbc076SDeepak Panickal             log->Printf ("ThreadPlanStepRange::NextRangeBreakpointExplainsStop - Hit next range breakpoint which has %" PRIu64 " owners - explains stop: %u.",
46199fbc076SDeepak Panickal                         (uint64_t)num_owners,
4620c61dee1SJim Ingham                         explains_stop);
4630c61dee1SJim Ingham         ClearNextBranchBreakpoint();
4640c61dee1SJim Ingham         return  explains_stop;
4650c61dee1SJim Ingham     }
466564d8bc2SJim Ingham }
467564d8bc2SJim Ingham 
468564d8bc2SJim Ingham bool
46930fdc8d8SChris Lattner ThreadPlanStepRange::WillStop ()
47030fdc8d8SChris Lattner {
47130fdc8d8SChris Lattner     return true;
47230fdc8d8SChris Lattner }
47330fdc8d8SChris Lattner 
47430fdc8d8SChris Lattner StateType
47506e827ccSJim Ingham ThreadPlanStepRange::GetPlanRunState ()
47630fdc8d8SChris Lattner {
477564d8bc2SJim Ingham     if (m_next_branch_bp_sp)
478564d8bc2SJim Ingham         return eStateRunning;
479564d8bc2SJim Ingham     else
48030fdc8d8SChris Lattner         return eStateStepping;
48130fdc8d8SChris Lattner }
48230fdc8d8SChris Lattner 
48330fdc8d8SChris Lattner bool
48430fdc8d8SChris Lattner ThreadPlanStepRange::MischiefManaged ()
48530fdc8d8SChris Lattner {
486927bfa3fSJim Ingham     // If we have pushed some plans between ShouldStop & MischiefManaged, then we're not done...
487927bfa3fSJim Ingham     // I do this check first because we might have stepped somewhere that will fool InRange into
488927bfa3fSJim Ingham     // thinking it needs to step past the end of that line.  This happens, for instance, when stepping
489927bfa3fSJim Ingham     // over inlined code that is in the middle of the current line.
490927bfa3fSJim Ingham 
491927bfa3fSJim Ingham     if (!m_no_more_plans)
492927bfa3fSJim Ingham         return false;
493927bfa3fSJim Ingham 
49430fdc8d8SChris Lattner     bool done = true;
49530fdc8d8SChris Lattner     if (!IsPlanComplete())
49630fdc8d8SChris Lattner     {
49730fdc8d8SChris Lattner         if (InRange())
49830fdc8d8SChris Lattner         {
49930fdc8d8SChris Lattner             done = false;
50030fdc8d8SChris Lattner         }
501b5c0d1ccSJim Ingham         else
502b5c0d1ccSJim Ingham         {
503b5c0d1ccSJim Ingham             FrameComparison frame_order = CompareCurrentFrameToStartFrame();
504*e65b2cf2SEugene Zelenko             done = (frame_order != eFrameCompareOlder) ? m_no_more_plans : true;
50530fdc8d8SChris Lattner         }
506b5c0d1ccSJim Ingham     }
50730fdc8d8SChris Lattner 
50830fdc8d8SChris Lattner     if (done)
50930fdc8d8SChris Lattner     {
5105160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
51130fdc8d8SChris Lattner         if (log)
51230fdc8d8SChris Lattner             log->Printf("Completed step through range plan.");
5130c61dee1SJim Ingham         ClearNextBranchBreakpoint();
51430fdc8d8SChris Lattner         ThreadPlan::MischiefManaged ();
51530fdc8d8SChris Lattner         return true;
51630fdc8d8SChris Lattner     }
51730fdc8d8SChris Lattner     else
51830fdc8d8SChris Lattner     {
51930fdc8d8SChris Lattner         return false;
52030fdc8d8SChris Lattner     }
52130fdc8d8SChris Lattner }
52264e7ead1SJim Ingham 
52364e7ead1SJim Ingham bool
52464e7ead1SJim Ingham ThreadPlanStepRange::IsPlanStale ()
52564e7ead1SJim Ingham {
5265160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
52764e7ead1SJim Ingham     FrameComparison frame_order = CompareCurrentFrameToStartFrame();
52864e7ead1SJim Ingham 
52964e7ead1SJim Ingham     if (frame_order == eFrameCompareOlder)
53064e7ead1SJim Ingham     {
53164e7ead1SJim Ingham         if (log)
53264e7ead1SJim Ingham         {
53364e7ead1SJim Ingham             log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've stepped out.");
53464e7ead1SJim Ingham         }
53564e7ead1SJim Ingham         return true;
53664e7ead1SJim Ingham     }
53764e7ead1SJim Ingham     else if (frame_order == eFrameCompareEqual && InSymbol())
53864e7ead1SJim Ingham     {
53964e7ead1SJim Ingham         // If we are not in a place we should step through, we've gotten stale.
54064e7ead1SJim Ingham         // One tricky bit here is that some stubs don't push a frame, so we should.
54164e7ead1SJim Ingham         // check that we are in the same symbol.
54264e7ead1SJim Ingham         if (!InRange())
54364e7ead1SJim Ingham         {
54464e7ead1SJim Ingham             return true;
54564e7ead1SJim Ingham         }
54664e7ead1SJim Ingham     }
54764e7ead1SJim Ingham     return false;
54864e7ead1SJim Ingham }
549