180814287SRaphael Isemann //===-- ThreadPlanStepRange.cpp -------------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
9e65b2cf2SEugene Zelenko #include "lldb/Target/ThreadPlanStepRange.h"
100c61dee1SJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
110c61dee1SJim Ingham #include "lldb/Breakpoint/BreakpointSite.h"
12564d8bc2SJim Ingham #include "lldb/Core/Disassembler.h"
1330fdc8d8SChris Lattner #include "lldb/Symbol/Function.h"
1430fdc8d8SChris Lattner #include "lldb/Symbol/Symbol.h"
15564d8bc2SJim Ingham #include "lldb/Target/ExecutionContext.h"
16f4b47e15SGreg Clayton #include "lldb/Target/Process.h"
17f4b47e15SGreg Clayton #include "lldb/Target/RegisterContext.h"
18f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
19564d8bc2SJim Ingham #include "lldb/Target/Target.h"
20f4b47e15SGreg Clayton #include "lldb/Target/Thread.h"
21564d8bc2SJim Ingham #include "lldb/Target/ThreadPlanRunToAddress.h"
226f9e6901SZachary Turner #include "lldb/Utility/Log.h"
23bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
2430fdc8d8SChris Lattner 
2530fdc8d8SChris Lattner using namespace lldb;
2630fdc8d8SChris Lattner using namespace lldb_private;
2730fdc8d8SChris Lattner 
2805097246SAdrian Prantl // ThreadPlanStepRange: Step through a stack range, either stepping over or
2905097246SAdrian Prantl // into based on the value of \a type.
3030fdc8d8SChris Lattner 
31b9c1b51eSKate Stone ThreadPlanStepRange::ThreadPlanStepRange(ThreadPlanKind kind, const char *name,
32242e0ad7SJim Ingham                                          Thread &thread,
33242e0ad7SJim Ingham                                          const AddressRange &range,
34242e0ad7SJim Ingham                                          const SymbolContext &addr_context,
352bdbfd50SJim Ingham                                          lldb::RunMode stop_others,
36b9c1b51eSKate Stone                                          bool given_ranges_only)
37b9c1b51eSKate Stone     : ThreadPlan(kind, name, thread, eVoteNoOpinion, eVoteNoOpinion),
38b9c1b51eSKate Stone       m_addr_context(addr_context), m_address_ranges(),
39b9c1b51eSKate Stone       m_stop_others(stop_others), m_stack_id(), m_parent_stack_id(),
40b9c1b51eSKate Stone       m_no_more_plans(false), m_first_run_event(true), m_use_fast_step(false),
41b9c1b51eSKate Stone       m_given_ranges_only(given_ranges_only) {
4217d023f6SJim Ingham   m_use_fast_step = GetTarget().GetUseFastStepping();
43c4c9fedcSJim Ingham   AddRange(range);
4430fdc8d8SChris Lattner   m_stack_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
4576447851SJim Ingham   StackFrameSP parent_stack = m_thread.GetStackFrameAtIndex(1);
4676447851SJim Ingham   if (parent_stack)
4776447851SJim Ingham     m_parent_stack_id = parent_stack->GetStackID();
4830fdc8d8SChris Lattner }
4930fdc8d8SChris Lattner 
50b9c1b51eSKate Stone ThreadPlanStepRange::~ThreadPlanStepRange() { ClearNextBranchBreakpoint(); }
51564d8bc2SJim Ingham 
52b9c1b51eSKate Stone void ThreadPlanStepRange::DidPush() {
53564d8bc2SJim Ingham   // See if we can find a "next range" breakpoint:
54564d8bc2SJim Ingham   SetNextBranchBreakpoint();
5530fdc8d8SChris Lattner }
5630fdc8d8SChris Lattner 
57e103ae92SJonas Devlieghere bool ThreadPlanStepRange::ValidatePlan(Stream *error) {
58e103ae92SJonas Devlieghere   if (m_could_not_resolve_hw_bp) {
59e103ae92SJonas Devlieghere     if (error)
60e103ae92SJonas Devlieghere       error->PutCString(
61e103ae92SJonas Devlieghere           "Could not create hardware breakpoint for thread plan.");
62e103ae92SJonas Devlieghere     return false;
63e103ae92SJonas Devlieghere   }
64e103ae92SJonas Devlieghere   return true;
65e103ae92SJonas Devlieghere }
6630fdc8d8SChris Lattner 
67b9c1b51eSKate Stone Vote ThreadPlanStepRange::ShouldReportStop(Event *event_ptr) {
685160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
692cad65a5SGreg Clayton 
702cad65a5SGreg Clayton   const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo;
7163e5fb76SJonas Devlieghere   LLDB_LOGF(log, "ThreadPlanStepRange::ShouldReportStop() returning vote %i\n",
72b9c1b51eSKate Stone             vote);
732cad65a5SGreg Clayton   return vote;
7430fdc8d8SChris Lattner }
7530fdc8d8SChris Lattner 
76b9c1b51eSKate Stone void ThreadPlanStepRange::AddRange(const AddressRange &new_range) {
7705097246SAdrian Prantl   // For now I'm just adding the ranges.  At some point we may want to condense
7805097246SAdrian Prantl   // the ranges if they overlap, though I don't think it is likely to be very
7905097246SAdrian Prantl   // important.
80c4c9fedcSJim Ingham   m_address_ranges.push_back(new_range);
8156d40428SJim Ingham 
82b9c1b51eSKate Stone   // Fill the slot for this address range with an empty DisassemblerSP in the
8305097246SAdrian Prantl   // instruction ranges. I want the indices to match, but I don't want to do
8405097246SAdrian Prantl   // the work to disassemble this range if I don't step into it.
85564d8bc2SJim Ingham   m_instruction_ranges.push_back(DisassemblerSP());
86c4c9fedcSJim Ingham }
87c4c9fedcSJim Ingham 
88b9c1b51eSKate Stone void ThreadPlanStepRange::DumpRanges(Stream *s) {
89c4c9fedcSJim Ingham   size_t num_ranges = m_address_ranges.size();
90b9c1b51eSKate Stone   if (num_ranges == 1) {
91b9c1b51eSKate Stone     m_address_ranges[0].Dump(s, m_thread.CalculateTarget().get(),
92b9c1b51eSKate Stone                              Address::DumpStyleLoadAddress);
93b9c1b51eSKate Stone   } else {
94b9c1b51eSKate Stone     for (size_t i = 0; i < num_ranges; i++) {
958c0970feSPavel Labath       s->Printf(" %" PRIu64 ": ", uint64_t(i));
96b9c1b51eSKate Stone       m_address_ranges[i].Dump(s, m_thread.CalculateTarget().get(),
97b9c1b51eSKate Stone                                Address::DumpStyleLoadAddress);
98c4c9fedcSJim Ingham     }
99c4c9fedcSJim Ingham   }
100c4c9fedcSJim Ingham }
101c4c9fedcSJim Ingham 
102b9c1b51eSKate Stone bool ThreadPlanStepRange::InRange() {
1035160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
10430fdc8d8SChris Lattner   bool ret_value = false;
10530fdc8d8SChris Lattner 
10630fdc8d8SChris Lattner   lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
10730fdc8d8SChris Lattner 
108c4c9fedcSJim Ingham   size_t num_ranges = m_address_ranges.size();
109b9c1b51eSKate Stone   for (size_t i = 0; i < num_ranges; i++) {
110b9c1b51eSKate Stone     ret_value = m_address_ranges[i].ContainsLoadAddress(
111b9c1b51eSKate Stone         pc_load_addr, m_thread.CalculateTarget().get());
112c4c9fedcSJim Ingham     if (ret_value)
113c4c9fedcSJim Ingham       break;
114c4c9fedcSJim Ingham   }
11530fdc8d8SChris Lattner 
116b9c1b51eSKate Stone   if (!ret_value && !m_given_ranges_only) {
11730fdc8d8SChris Lattner     // See if we've just stepped to another part of the same line number...
118b57e4a1bSJason Molenda     StackFrame *frame = m_thread.GetStackFrameAtIndex(0).get();
11930fdc8d8SChris Lattner 
120b9c1b51eSKate Stone     SymbolContext new_context(
121b9c1b51eSKate Stone         frame->GetSymbolContext(eSymbolContextEverything));
122b9c1b51eSKate Stone     if (m_addr_context.line_entry.IsValid() &&
123b9c1b51eSKate Stone         new_context.line_entry.IsValid()) {
124b9c1b51eSKate Stone       if (m_addr_context.line_entry.original_file ==
125b9c1b51eSKate Stone           new_context.line_entry.original_file) {
126b9c1b51eSKate Stone         if (m_addr_context.line_entry.line == new_context.line_entry.line) {
12730fdc8d8SChris Lattner           m_addr_context = new_context;
1288a777920SGreg Clayton           const bool include_inlined_functions =
1298a777920SGreg Clayton               GetKind() == eKindStepOverRange;
1308a777920SGreg Clayton           AddRange(m_addr_context.line_entry.GetSameLineContiguousAddressRange(
1318a777920SGreg Clayton               include_inlined_functions));
13230fdc8d8SChris Lattner           ret_value = true;
133b9c1b51eSKate Stone           if (log) {
13430fdc8d8SChris Lattner             StreamString s;
135b9c1b51eSKate Stone             m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
136b9c1b51eSKate Stone                                            true, Address::DumpStyleLoadAddress,
137b9c1b51eSKate Stone                                            Address::DumpStyleLoadAddress, true);
13830fdc8d8SChris Lattner 
13963e5fb76SJonas Devlieghere             LLDB_LOGF(
14063e5fb76SJonas Devlieghere                 log,
141b9c1b51eSKate Stone                 "Step range plan stepped to another range of same line: %s",
142b9c1b51eSKate Stone                 s.GetData());
14330fdc8d8SChris Lattner           }
144b9c1b51eSKate Stone         } else if (new_context.line_entry.line == 0) {
1452b89a531SJim Ingham           new_context.line_entry.line = m_addr_context.line_entry.line;
1462b89a531SJim Ingham           m_addr_context = new_context;
1478a777920SGreg Clayton           const bool include_inlined_functions =
1488a777920SGreg Clayton               GetKind() == eKindStepOverRange;
1498a777920SGreg Clayton           AddRange(m_addr_context.line_entry.GetSameLineContiguousAddressRange(
1508a777920SGreg Clayton               include_inlined_functions));
1512b89a531SJim Ingham           ret_value = true;
152b9c1b51eSKate Stone           if (log) {
1532b89a531SJim Ingham             StreamString s;
154b9c1b51eSKate Stone             m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
155b9c1b51eSKate Stone                                            true, Address::DumpStyleLoadAddress,
156b9c1b51eSKate Stone                                            Address::DumpStyleLoadAddress, true);
1572b89a531SJim Ingham 
15863e5fb76SJonas Devlieghere             LLDB_LOGF(log,
15963e5fb76SJonas Devlieghere                       "Step range plan stepped to a range at linenumber 0 "
160b9c1b51eSKate Stone                       "stepping through that range: %s",
161b9c1b51eSKate Stone                       s.GetData());
1622b89a531SJim Ingham           }
163b9c1b51eSKate Stone         } else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(
164b9c1b51eSKate Stone                        m_thread.CalculateTarget().get()) != pc_load_addr) {
165b9c1b51eSKate Stone           // Another thing that sometimes happens here is that we step out of
16605097246SAdrian Prantl           // one line into the MIDDLE of another line.  So far I mostly see
16705097246SAdrian Prantl           // this due to bugs in the debug information. But we probably don't
16805097246SAdrian Prantl           // want to be in the middle of a line range, so in that case reset
16905097246SAdrian Prantl           // the stepping range to the line we've stepped into the middle of
17005097246SAdrian Prantl           // and continue.
171843bfb2cSJim Ingham           m_addr_context = new_context;
172c4c9fedcSJim Ingham           m_address_ranges.clear();
173c4c9fedcSJim Ingham           AddRange(m_addr_context.line_entry.range);
174843bfb2cSJim Ingham           ret_value = true;
175b9c1b51eSKate Stone           if (log) {
176843bfb2cSJim Ingham             StreamString s;
177b9c1b51eSKate Stone             m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
178b9c1b51eSKate Stone                                            true, Address::DumpStyleLoadAddress,
179b9c1b51eSKate Stone                                            Address::DumpStyleLoadAddress, true);
180843bfb2cSJim Ingham 
18163e5fb76SJonas Devlieghere             LLDB_LOGF(log,
18263e5fb76SJonas Devlieghere                       "Step range plan stepped to the middle of new "
183b9c1b51eSKate Stone                       "line(%d): %s, continuing to clear this line.",
184b9c1b51eSKate Stone                       new_context.line_entry.line, s.GetData());
185843bfb2cSJim Ingham           }
186843bfb2cSJim Ingham         }
187843bfb2cSJim Ingham       }
18830fdc8d8SChris Lattner     }
18930fdc8d8SChris Lattner   }
19030fdc8d8SChris Lattner 
19130fdc8d8SChris Lattner   if (!ret_value && log)
19263e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Step range plan out of range to 0x%" PRIx64, pc_load_addr);
19330fdc8d8SChris Lattner 
19430fdc8d8SChris Lattner   return ret_value;
19530fdc8d8SChris Lattner }
19630fdc8d8SChris Lattner 
197b9c1b51eSKate Stone bool ThreadPlanStepRange::InSymbol() {
19830fdc8d8SChris Lattner   lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
199b9c1b51eSKate Stone   if (m_addr_context.function != nullptr) {
200b9c1b51eSKate Stone     return m_addr_context.function->GetAddressRange().ContainsLoadAddress(
201b9c1b51eSKate Stone         cur_pc, m_thread.CalculateTarget().get());
202b9c1b51eSKate Stone   } else if (m_addr_context.symbol && m_addr_context.symbol->ValueIsAddress()) {
203b9c1b51eSKate Stone     AddressRange range(m_addr_context.symbol->GetAddressRef(),
204b9c1b51eSKate Stone                        m_addr_context.symbol->GetByteSize());
205e7612134SGreg Clayton     return range.ContainsLoadAddress(cur_pc, m_thread.CalculateTarget().get());
20630fdc8d8SChris Lattner   }
20730fdc8d8SChris Lattner   return false;
20830fdc8d8SChris Lattner }
20930fdc8d8SChris Lattner 
210b9c1b51eSKate Stone // FIXME: This should also handle inlining if we aren't going to do inlining in
211b9c1b51eSKate Stone // the
21230fdc8d8SChris Lattner // main stack.
21330fdc8d8SChris Lattner //
21430fdc8d8SChris Lattner // Ideally we should remember the whole stack frame list, and then compare that
21530fdc8d8SChris Lattner // to the current list.
21630fdc8d8SChris Lattner 
217b9c1b51eSKate Stone lldb::FrameComparison ThreadPlanStepRange::CompareCurrentFrameToStartFrame() {
218b5c0d1ccSJim Ingham   FrameComparison frame_order;
2197ce490c6SJim Ingham 
220b5c0d1ccSJim Ingham   StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
2217ce490c6SJim Ingham 
222b9c1b51eSKate Stone   if (cur_frame_id == m_stack_id) {
223b5c0d1ccSJim Ingham     frame_order = eFrameCompareEqual;
224b9c1b51eSKate Stone   } else if (cur_frame_id < m_stack_id) {
225b5c0d1ccSJim Ingham     frame_order = eFrameCompareYounger;
226b9c1b51eSKate Stone   } else {
22776447851SJim Ingham     StackFrameSP cur_parent_frame = m_thread.GetStackFrameAtIndex(1);
22876447851SJim Ingham     StackID cur_parent_id;
22976447851SJim Ingham     if (cur_parent_frame)
23076447851SJim Ingham       cur_parent_id = cur_parent_frame->GetStackID();
231b9c1b51eSKate Stone     if (m_parent_stack_id.IsValid() && cur_parent_id.IsValid() &&
232b9c1b51eSKate Stone         m_parent_stack_id == cur_parent_id)
233862d1bbdSJim Ingham       frame_order = eFrameCompareSameParent;
234862d1bbdSJim Ingham     else
235b5c0d1ccSJim Ingham       frame_order = eFrameCompareOlder;
23630fdc8d8SChris Lattner   }
237b5c0d1ccSJim Ingham   return frame_order;
23830fdc8d8SChris Lattner }
23930fdc8d8SChris Lattner 
240b9c1b51eSKate Stone bool ThreadPlanStepRange::StopOthers() {
241434905b9SJim Ingham   switch (m_stop_others) {
242434905b9SJim Ingham   case lldb::eOnlyThisThread:
243434905b9SJim Ingham     return true;
244434905b9SJim Ingham   case lldb::eOnlyDuringStepping:
245434905b9SJim Ingham     // If there is a call in the range of the next branch breakpoint,
246434905b9SJim Ingham     // then we should always run all threads, since a call can execute
247434905b9SJim Ingham     // arbitrary code which might for instance take a lock that's held
248434905b9SJim Ingham     // by another thread.
249434905b9SJim Ingham     return !m_found_calls;
250434905b9SJim Ingham   case lldb::eAllThreads:
251434905b9SJim Ingham     return false;
252434905b9SJim Ingham   }
25305c3b36bSPavel Labath   llvm_unreachable("Unhandled run mode!");
25430fdc8d8SChris Lattner }
25530fdc8d8SChris Lattner 
256b9c1b51eSKate Stone InstructionList *ThreadPlanStepRange::GetInstructionsForAddress(
257b9c1b51eSKate Stone     lldb::addr_t addr, size_t &range_index, size_t &insn_offset) {
258564d8bc2SJim Ingham   size_t num_ranges = m_address_ranges.size();
259b9c1b51eSKate Stone   for (size_t i = 0; i < num_ranges; i++) {
260b9c1b51eSKate Stone     if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget())) {
261564d8bc2SJim Ingham       // Some joker added a zero size range to the stepping range...
262564d8bc2SJim Ingham       if (m_address_ranges[i].GetByteSize() == 0)
263e65b2cf2SEugene Zelenko         return nullptr;
264564d8bc2SJim Ingham 
265b9c1b51eSKate Stone       if (!m_instruction_ranges[i]) {
266564d8bc2SJim Ingham         // Disassemble the address range given:
267e65b2cf2SEugene Zelenko         const char *plugin_name = nullptr;
268e65b2cf2SEugene Zelenko         const char *flavor = nullptr;
2696b3e6d54SJason Molenda         const bool prefer_file_cache = true;
270b9c1b51eSKate Stone         m_instruction_ranges[i] = Disassembler::DisassembleRange(
271*04592d5bSPavel Labath             GetTarget().GetArchitecture(), plugin_name, flavor, GetTarget(),
272b9c1b51eSKate Stone             m_address_ranges[i], prefer_file_cache);
273564d8bc2SJim Ingham       }
274564d8bc2SJim Ingham       if (!m_instruction_ranges[i])
275e65b2cf2SEugene Zelenko         return nullptr;
276b9c1b51eSKate Stone       else {
277b9c1b51eSKate Stone         // Find where we are in the instruction list as well.  If we aren't at
27805097246SAdrian Prantl         // an instruction, return nullptr. In this case, we're probably lost,
27905097246SAdrian Prantl         // and shouldn't try to do anything fancy.
280564d8bc2SJim Ingham 
281b9c1b51eSKate Stone         insn_offset =
282b9c1b51eSKate Stone             m_instruction_ranges[i]
283b9c1b51eSKate Stone                 ->GetInstructionList()
284b9c1b51eSKate Stone                 .GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
285564d8bc2SJim Ingham         if (insn_offset == UINT32_MAX)
286e65b2cf2SEugene Zelenko           return nullptr;
287b9c1b51eSKate Stone         else {
288564d8bc2SJim Ingham           range_index = i;
289564d8bc2SJim Ingham           return &m_instruction_ranges[i]->GetInstructionList();
290564d8bc2SJim Ingham         }
291564d8bc2SJim Ingham       }
292564d8bc2SJim Ingham     }
293564d8bc2SJim Ingham   }
294e65b2cf2SEugene Zelenko   return nullptr;
295564d8bc2SJim Ingham }
296564d8bc2SJim Ingham 
297b9c1b51eSKate Stone void ThreadPlanStepRange::ClearNextBranchBreakpoint() {
298b9c1b51eSKate Stone   if (m_next_branch_bp_sp) {
2995160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
30063e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Removing next branch breakpoint: %d.",
301b9c1b51eSKate Stone               m_next_branch_bp_sp->GetID());
302564d8bc2SJim Ingham     GetTarget().RemoveBreakpointByID(m_next_branch_bp_sp->GetID());
303564d8bc2SJim Ingham     m_next_branch_bp_sp.reset();
304e103ae92SJonas Devlieghere     m_could_not_resolve_hw_bp = false;
305434905b9SJim Ingham     m_found_calls = false;
306564d8bc2SJim Ingham   }
307564d8bc2SJim Ingham }
308564d8bc2SJim Ingham 
309b9c1b51eSKate Stone bool ThreadPlanStepRange::SetNextBranchBreakpoint() {
3100c61dee1SJim Ingham   if (m_next_branch_bp_sp)
3110c61dee1SJim Ingham     return true;
3120c61dee1SJim Ingham 
3135160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
314b9c1b51eSKate Stone   // Stepping through ranges using breakpoints doesn't work yet, but with this
31505097246SAdrian Prantl   // off we fall back to instruction single stepping.
3160c61dee1SJim Ingham   if (!m_use_fast_step)
317564d8bc2SJim Ingham     return false;
3180c61dee1SJim Ingham 
319434905b9SJim Ingham   // clear the m_found_calls, we'll rediscover it for this range.
320434905b9SJim Ingham   m_found_calls = false;
321434905b9SJim Ingham 
322564d8bc2SJim Ingham   lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC();
323b9c1b51eSKate Stone   // Find the current address in our address ranges, and fetch the disassembly
324b9c1b51eSKate Stone   // if we haven't already:
325564d8bc2SJim Ingham   size_t pc_index;
326564d8bc2SJim Ingham   size_t range_index;
327b9c1b51eSKate Stone   InstructionList *instructions =
328b9c1b51eSKate Stone       GetInstructionsForAddress(cur_addr, range_index, pc_index);
329e65b2cf2SEugene Zelenko   if (instructions == nullptr)
330564d8bc2SJim Ingham     return false;
331b9c1b51eSKate Stone   else {
332e76e7e93STed Woodward     Target &target = GetThread().GetProcess()->GetTarget();
333df225764SGreg Clayton     const bool ignore_calls = GetKind() == eKindStepOverRange;
334df225764SGreg Clayton     uint32_t branch_index =
335df225764SGreg Clayton         instructions->GetIndexOfNextBranchInstruction(pc_index, target,
336434905b9SJim Ingham                                                       ignore_calls,
337434905b9SJim Ingham                                                       &m_found_calls);
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.
342b9c1b51eSKate Stone     if (branch_index == UINT32_MAX) {
343a3f466b9SJim Ingham       uint32_t last_index = instructions->GetSize() - 1;
344b9c1b51eSKate Stone       if (last_index - pc_index > 1) {
345b9c1b51eSKate Stone         InstructionSP last_inst =
346b9c1b51eSKate Stone             instructions->GetInstructionAtIndex(last_index);
347a3f466b9SJim Ingham         size_t last_inst_size = last_inst->GetOpcode().GetByteSize();
348a3f466b9SJim Ingham         run_to_address = last_inst->GetAddress();
349a3f466b9SJim Ingham         run_to_address.Slide(last_inst_size);
350a3f466b9SJim Ingham       }
351b9c1b51eSKate Stone     } else if (branch_index - pc_index > 1) {
352b9c1b51eSKate Stone       run_to_address =
353b9c1b51eSKate Stone           instructions->GetInstructionAtIndex(branch_index)->GetAddress();
354564d8bc2SJim Ingham     }
3550c61dee1SJim Ingham 
356b9c1b51eSKate Stone     if (run_to_address.IsValid()) {
357564d8bc2SJim Ingham       const bool is_internal = true;
358b9c1b51eSKate Stone       m_next_branch_bp_sp =
359b9c1b51eSKate Stone           GetTarget().CreateBreakpoint(run_to_address, is_internal, false);
360b9c1b51eSKate Stone       if (m_next_branch_bp_sp) {
361e103ae92SJonas Devlieghere 
362e103ae92SJonas Devlieghere         if (m_next_branch_bp_sp->IsHardware() &&
363e103ae92SJonas Devlieghere             !m_next_branch_bp_sp->HasResolvedLocations())
364e103ae92SJonas Devlieghere           m_could_not_resolve_hw_bp = true;
365e103ae92SJonas Devlieghere 
366b9c1b51eSKate Stone         if (log) {
3670c61dee1SJim Ingham           lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
368b9c1b51eSKate Stone           BreakpointLocationSP bp_loc =
369b9c1b51eSKate Stone               m_next_branch_bp_sp->GetLocationAtIndex(0);
370b9c1b51eSKate Stone           if (bp_loc) {
3710c61dee1SJim Ingham             BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
372b9c1b51eSKate Stone             if (bp_site) {
3730c61dee1SJim Ingham               bp_site_id = bp_site->GetID();
3740c61dee1SJim Ingham             }
3750c61dee1SJim Ingham           }
37663e5fb76SJonas Devlieghere           LLDB_LOGF(log,
37763e5fb76SJonas Devlieghere                     "ThreadPlanStepRange::SetNextBranchBreakpoint - Setting "
378b9c1b51eSKate Stone                     "breakpoint %d (site %d) to run to address 0x%" PRIx64,
379b9c1b51eSKate Stone                     m_next_branch_bp_sp->GetID(), bp_site_id,
380b9c1b51eSKate Stone                     run_to_address.GetLoadAddress(
381b9c1b51eSKate Stone                         &m_thread.GetProcess()->GetTarget()));
3820c61dee1SJim Ingham         }
383e103ae92SJonas Devlieghere 
384564d8bc2SJim Ingham         m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
3852995077dSJim Ingham         m_next_branch_bp_sp->SetBreakpointKind("next-branch-location");
386e103ae92SJonas Devlieghere 
3870c61dee1SJim Ingham         return true;
388b9c1b51eSKate Stone       } else
3892995077dSJim Ingham         return false;
390564d8bc2SJim Ingham     }
391564d8bc2SJim Ingham   }
392564d8bc2SJim Ingham   return false;
393564d8bc2SJim Ingham }
394564d8bc2SJim Ingham 
395b9c1b51eSKate Stone bool ThreadPlanStepRange::NextRangeBreakpointExplainsStop(
396b9c1b51eSKate Stone     lldb::StopInfoSP stop_info_sp) {
3975160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
398564d8bc2SJim Ingham   if (!m_next_branch_bp_sp)
399564d8bc2SJim Ingham     return false;
400564d8bc2SJim Ingham 
401564d8bc2SJim Ingham   break_id_t bp_site_id = stop_info_sp->GetValue();
402b9c1b51eSKate Stone   BreakpointSiteSP bp_site_sp =
403b9c1b51eSKate Stone       m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id);
4040c61dee1SJim Ingham   if (!bp_site_sp)
4050c61dee1SJim Ingham     return false;
4060c61dee1SJim Ingham   else if (!bp_site_sp->IsBreakpointAtThisSite(m_next_branch_bp_sp->GetID()))
407564d8bc2SJim Ingham     return false;
408b9c1b51eSKate Stone   else {
4090c61dee1SJim Ingham     // If we've hit the next branch breakpoint, then clear it.
4100c61dee1SJim Ingham     size_t num_owners = bp_site_sp->GetNumberOfOwners();
4110c61dee1SJim Ingham     bool explains_stop = true;
412b9c1b51eSKate Stone     // If all the owners are internal, then we are probably just stepping over
41305097246SAdrian Prantl     // this range from multiple threads, or multiple frames, so we want to
41405097246SAdrian Prantl     // continue.  If one is not internal, then we should not explain the stop,
4150c61dee1SJim Ingham     // and let the user breakpoint handle the stop.
416b9c1b51eSKate Stone     for (size_t i = 0; i < num_owners; i++) {
417b9c1b51eSKate Stone       if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal()) {
4180c61dee1SJim Ingham         explains_stop = false;
4190c61dee1SJim Ingham         break;
4200c61dee1SJim Ingham       }
4210c61dee1SJim Ingham     }
42263e5fb76SJonas Devlieghere     LLDB_LOGF(log,
42363e5fb76SJonas Devlieghere               "ThreadPlanStepRange::NextRangeBreakpointExplainsStop - Hit "
424b9c1b51eSKate Stone               "next range breakpoint which has %" PRIu64
425b9c1b51eSKate Stone               " owners - explains stop: %u.",
426b9c1b51eSKate Stone               (uint64_t)num_owners, explains_stop);
4270c61dee1SJim Ingham     ClearNextBranchBreakpoint();
4280c61dee1SJim Ingham     return explains_stop;
4290c61dee1SJim Ingham   }
430564d8bc2SJim Ingham }
431564d8bc2SJim Ingham 
432b9c1b51eSKate Stone bool ThreadPlanStepRange::WillStop() { return true; }
43330fdc8d8SChris Lattner 
434b9c1b51eSKate Stone StateType ThreadPlanStepRange::GetPlanRunState() {
435564d8bc2SJim Ingham   if (m_next_branch_bp_sp)
436564d8bc2SJim Ingham     return eStateRunning;
437564d8bc2SJim Ingham   else
43830fdc8d8SChris Lattner     return eStateStepping;
43930fdc8d8SChris Lattner }
44030fdc8d8SChris Lattner 
441b9c1b51eSKate Stone bool ThreadPlanStepRange::MischiefManaged() {
442b9c1b51eSKate Stone   // If we have pushed some plans between ShouldStop & MischiefManaged, then
443b9c1b51eSKate Stone   // we're not done...
444b9c1b51eSKate Stone   // I do this check first because we might have stepped somewhere that will
445b9c1b51eSKate Stone   // fool InRange into
446b9c1b51eSKate Stone   // thinking it needs to step past the end of that line.  This happens, for
44705097246SAdrian Prantl   // instance, when stepping over inlined code that is in the middle of the
44805097246SAdrian Prantl   // current line.
449927bfa3fSJim Ingham 
450927bfa3fSJim Ingham   if (!m_no_more_plans)
451927bfa3fSJim Ingham     return false;
452927bfa3fSJim Ingham 
45330fdc8d8SChris Lattner   bool done = true;
454b9c1b51eSKate Stone   if (!IsPlanComplete()) {
455b9c1b51eSKate Stone     if (InRange()) {
45630fdc8d8SChris Lattner       done = false;
457b9c1b51eSKate Stone     } else {
458b5c0d1ccSJim Ingham       FrameComparison frame_order = CompareCurrentFrameToStartFrame();
459e65b2cf2SEugene Zelenko       done = (frame_order != eFrameCompareOlder) ? m_no_more_plans : true;
46030fdc8d8SChris Lattner     }
461b5c0d1ccSJim Ingham   }
46230fdc8d8SChris Lattner 
463b9c1b51eSKate Stone   if (done) {
4645160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
46563e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Completed step through range plan.");
4660c61dee1SJim Ingham     ClearNextBranchBreakpoint();
46730fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
46830fdc8d8SChris Lattner     return true;
469b9c1b51eSKate Stone   } else {
47030fdc8d8SChris Lattner     return false;
47130fdc8d8SChris Lattner   }
47230fdc8d8SChris Lattner }
47364e7ead1SJim Ingham 
474b9c1b51eSKate Stone bool ThreadPlanStepRange::IsPlanStale() {
4755160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
47664e7ead1SJim Ingham   FrameComparison frame_order = CompareCurrentFrameToStartFrame();
47764e7ead1SJim Ingham 
478b9c1b51eSKate Stone   if (frame_order == eFrameCompareOlder) {
479b9c1b51eSKate Stone     if (log) {
48063e5fb76SJonas Devlieghere       LLDB_LOGF(log, "ThreadPlanStepRange::IsPlanStale returning true, we've "
481b9c1b51eSKate Stone                      "stepped out.");
48264e7ead1SJim Ingham     }
48364e7ead1SJim Ingham     return true;
484b9c1b51eSKate Stone   } else if (frame_order == eFrameCompareEqual && InSymbol()) {
48505097246SAdrian Prantl     // If we are not in a place we should step through, we've gotten stale. One
48605097246SAdrian Prantl     // tricky bit here is that some stubs don't push a frame, so we should.
48764e7ead1SJim Ingham     // check that we are in the same symbol.
488b9c1b51eSKate Stone     if (!InRange()) {
48986aaa8a2SBoris Ulasevich       // Set plan Complete when we reach next instruction just after the range
49086aaa8a2SBoris Ulasevich       lldb::addr_t addr = m_thread.GetRegisterContext()->GetPC() - 1;
49186aaa8a2SBoris Ulasevich       size_t num_ranges = m_address_ranges.size();
49286aaa8a2SBoris Ulasevich       for (size_t i = 0; i < num_ranges; i++) {
49386aaa8a2SBoris Ulasevich         bool in_range = m_address_ranges[i].ContainsLoadAddress(
49486aaa8a2SBoris Ulasevich             addr, m_thread.CalculateTarget().get());
49586aaa8a2SBoris Ulasevich         if (in_range) {
49686aaa8a2SBoris Ulasevich           SetPlanComplete();
49786aaa8a2SBoris Ulasevich         }
49886aaa8a2SBoris Ulasevich       }
49964e7ead1SJim Ingham       return true;
50064e7ead1SJim Ingham     }
50164e7ead1SJim Ingham   }
50264e7ead1SJim Ingham   return false;
50364e7ead1SJim Ingham }
504