1 //===-- ThreadPlanShouldStopHere.cpp ----------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
15
16 using namespace lldb;
17 using namespace lldb_private;
18
19 //----------------------------------------------------------------------
20 // ThreadPlanShouldStopHere constructor
21 //----------------------------------------------------------------------
ThreadPlanShouldStopHere(ThreadPlan * owner)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;
29 }
30
ThreadPlanShouldStopHere(ThreadPlan * owner,const ThreadPlanShouldStopHereCallbacks * callbacks,void * baton)31 ThreadPlanShouldStopHere::ThreadPlanShouldStopHere(
32 ThreadPlan *owner, const ThreadPlanShouldStopHereCallbacks *callbacks,
33 void *baton)
34 : m_callbacks(), m_baton(), m_owner(owner),
35 m_flags(ThreadPlanShouldStopHere::eNone) {
36 SetShouldStopHereCallbacks(callbacks, baton);
37 }
38
39 ThreadPlanShouldStopHere::~ThreadPlanShouldStopHere() = default;
40
InvokeShouldStopHereCallback(FrameComparison operation,Status & status)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));
48 if (log) {
49 lldb::addr_t current_addr =
50 m_owner->GetThread().GetRegisterContext()->GetPC(0);
51
52 log->Printf("ShouldStopHere callback returned %u from 0x%" PRIx64 ".",
53 should_stop_here, current_addr);
54 }
55 }
56
57 return should_stop_here;
58 }
59
DefaultShouldStopHereCallback(ThreadPlan * current_plan,Flags & flags,FrameComparison operation,Status & status,void * baton)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();
65 if (!frame)
66 return true;
67
68 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
69
70 if ((operation == eFrameCompareOlder && flags.Test(eStepOutAvoidNoDebug)) ||
71 (operation == eFrameCompareYounger && flags.Test(eStepInAvoidNoDebug)) ||
72 (operation == eFrameCompareSameParent &&
73 flags.Test(eStepInAvoidNoDebug))) {
74 if (!frame->HasDebugInformation()) {
75 if (log)
76 log->Printf("Stepping out of frame with no debug info");
77
78 should_stop_here = false;
79 }
80 }
81
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.
87 if (frame) {
88 SymbolContext sc;
89 sc = frame->GetSymbolContext(eSymbolContextLineEntry);
90 if (sc.line_entry.line == 0)
91 should_stop_here = false;
92 }
93
94 return should_stop_here;
95 }
96
DefaultStepFromHereCallback(ThreadPlan * current_plan,Flags & flags,FrameComparison operation,Status & status,void * baton)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));
106
107 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
108 if (!frame)
109 return return_plan_sp;
110 SymbolContext sc;
111 sc = frame->GetSymbolContext(eSymbolContextLineEntry | eSymbolContextSymbol);
112
113 if (sc.line_entry.line == 0) {
114 AddressRange range = sc.line_entry.range;
115
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)) {
124 if (log)
125 log->Printf("Stopped in a function with only line 0 lines, just "
126 "stepping out.");
127 just_step_out = true;
128 }
129 }
130 if (!just_step_out) {
131 if (log)
132 log->Printf("ThreadPlanShouldStopHere::DefaultStepFromHereCallback "
133 "Queueing StepInRange plan to step through line 0 code.");
134
135 return_plan_sp = current_plan->GetThread().QueueThreadPlanForStepInRange(
136 false, range, sc, NULL, eOnlyDuringStepping, status,
137 eLazyBoolCalculate, eLazyBoolNo);
138 }
139 }
140
141 if (!return_plan_sp)
142 return_plan_sp =
143 current_plan->GetThread().QueueThreadPlanForStepOutNoShouldStop(
144 false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion,
145 frame_index, status, true);
146 return return_plan_sp;
147 }
148
QueueStepOutFromHerePlan(lldb_private::Flags & flags,lldb::FrameComparison operation,Status & status)149 ThreadPlanSP ThreadPlanShouldStopHere::QueueStepOutFromHerePlan(
150 lldb_private::Flags &flags, lldb::FrameComparison operation,
151 Status &status) {
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);
156 }
157 return return_plan_sp;
158 }
159
CheckShouldStopHereAndQueueStepOut(lldb::FrameComparison operation,Status & status)160 lldb::ThreadPlanSP ThreadPlanShouldStopHere::CheckShouldStopHereAndQueueStepOut(
161 lldb::FrameComparison operation, Status &status) {
162 if (!InvokeShouldStopHereCallback(operation, status))
163 return QueueStepOutFromHerePlan(m_flags, operation, status);
164 else
165 return ThreadPlanSP();
166 }
167