1 //===-- ThreadPlanShouldStopHere.cpp ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ThreadPlanShouldStopHere.h"
11 #include "lldb/Symbol/Symbol.h"
12 #include "lldb/Target/RegisterContext.h"
13 #include "lldb/Target/Thread.h"
14 #include "lldb/Utility/Log.h"
17 using namespace lldb_private;
19 //----------------------------------------------------------------------
20 // ThreadPlanShouldStopHere constructor
21 //----------------------------------------------------------------------
22 ThreadPlanShouldStopHere::ThreadPlanShouldStopHere(ThreadPlan *owner)
23 : m_callbacks(), m_baton(nullptr), m_owner(owner),
24 m_flags(ThreadPlanShouldStopHere::eNone) {
25 m_callbacks.should_stop_here_callback =
26 ThreadPlanShouldStopHere::DefaultShouldStopHereCallback;
27 m_callbacks.step_from_here_callback =
28 ThreadPlanShouldStopHere::DefaultStepFromHereCallback;
31 ThreadPlanShouldStopHere::ThreadPlanShouldStopHere(
32 ThreadPlan *owner, const ThreadPlanShouldStopHereCallbacks *callbacks,
34 : m_callbacks(), m_baton(), m_owner(owner),
35 m_flags(ThreadPlanShouldStopHere::eNone) {
36 SetShouldStopHereCallbacks(callbacks, baton);
39 ThreadPlanShouldStopHere::~ThreadPlanShouldStopHere() = default;
41 bool ThreadPlanShouldStopHere::InvokeShouldStopHereCallback(
42 FrameComparison operation, Status &status) {
43 bool should_stop_here = true;
44 if (m_callbacks.should_stop_here_callback) {
45 should_stop_here = m_callbacks.should_stop_here_callback(
46 m_owner, m_flags, operation, status, m_baton);
47 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
49 lldb::addr_t current_addr =
50 m_owner->GetThread().GetRegisterContext()->GetPC(0);
52 log->Printf("ShouldStopHere callback returned %u from 0x%" PRIx64 ".",
53 should_stop_here, current_addr);
57 return should_stop_here;
60 bool ThreadPlanShouldStopHere::DefaultShouldStopHereCallback(
61 ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
62 Status &status, void *baton) {
63 bool should_stop_here = true;
64 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
68 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
70 if ((operation == eFrameCompareOlder && flags.Test(eStepOutAvoidNoDebug)) ||
71 (operation == eFrameCompareYounger && flags.Test(eStepInAvoidNoDebug)) ||
72 (operation == eFrameCompareSameParent &&
73 flags.Test(eStepInAvoidNoDebug))) {
74 if (!frame->HasDebugInformation()) {
76 log->Printf("Stepping out of frame with no debug info");
78 should_stop_here = false;
82 // Always avoid code with line number 0.
83 // FIXME: At present the ShouldStop and the StepFromHere calculate this
84 // independently. If this ever
85 // becomes expensive (this one isn't) we can try to have this set a state
86 // that the StepFromHere can use.
89 sc = frame->GetSymbolContext(eSymbolContextLineEntry);
90 if (sc.line_entry.line == 0)
91 should_stop_here = false;
94 return should_stop_here;
97 ThreadPlanSP ThreadPlanShouldStopHere::DefaultStepFromHereCallback(
98 ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
99 Status &status, void *baton) {
100 const bool stop_others = false;
101 const size_t frame_index = 0;
102 ThreadPlanSP return_plan_sp;
103 // If we are stepping through code at line number 0, then we need to step
104 // over this range. Otherwise we will step out.
105 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
107 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
109 return return_plan_sp;
111 sc = frame->GetSymbolContext(eSymbolContextLineEntry | eSymbolContextSymbol);
113 if (sc.line_entry.line == 0) {
114 AddressRange range = sc.line_entry.range;
116 // If the whole function is marked line 0 just step out, that's easier &
117 // faster than continuing to step through it.
118 bool just_step_out = false;
119 if (sc.symbol && sc.symbol->ValueIsAddress()) {
120 Address symbol_end = sc.symbol->GetAddress();
121 symbol_end.Slide(sc.symbol->GetByteSize() - 1);
122 if (range.ContainsFileAddress(sc.symbol->GetAddress()) &&
123 range.ContainsFileAddress(symbol_end)) {
125 log->Printf("Stopped in a function with only line 0 lines, just "
127 just_step_out = true;
130 if (!just_step_out) {
132 log->Printf("ThreadPlanShouldStopHere::DefaultStepFromHereCallback "
133 "Queueing StepInRange plan to step through line 0 code.");
135 return_plan_sp = current_plan->GetThread().QueueThreadPlanForStepInRange(
136 false, range, sc, NULL, eOnlyDuringStepping, status,
137 eLazyBoolCalculate, eLazyBoolNo);
143 current_plan->GetThread().QueueThreadPlanForStepOutNoShouldStop(
144 false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion,
145 frame_index, status, true);
146 return return_plan_sp;
149 ThreadPlanSP ThreadPlanShouldStopHere::QueueStepOutFromHerePlan(
150 lldb_private::Flags &flags, lldb::FrameComparison operation,
152 ThreadPlanSP return_plan_sp;
153 if (m_callbacks.step_from_here_callback) {
154 return_plan_sp = m_callbacks.step_from_here_callback(
155 m_owner, flags, operation, status, m_baton);
157 return return_plan_sp;
160 lldb::ThreadPlanSP ThreadPlanShouldStopHere::CheckShouldStopHereAndQueueStepOut(
161 lldb::FrameComparison operation, Status &status) {
162 if (!InvokeShouldStopHereCallback(operation, status))
163 return QueueStepOutFromHerePlan(m_flags, operation, status);
165 return ThreadPlanSP();