1 //===-- ThreadPlanShouldStopHere.cpp ----------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Target/ThreadPlanShouldStopHere.h"
10 #include "lldb/Symbol/Symbol.h"
11 #include "lldb/Target/RegisterContext.h"
12 #include "lldb/Target/Thread.h"
13 #include "lldb/Utility/Log.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 //----------------------------------------------------------------------
19 // ThreadPlanShouldStopHere constructor
20 //----------------------------------------------------------------------
21 ThreadPlanShouldStopHere::ThreadPlanShouldStopHere(ThreadPlan *owner)
22     : m_callbacks(), m_baton(nullptr), m_owner(owner),
23       m_flags(ThreadPlanShouldStopHere::eNone) {
24   m_callbacks.should_stop_here_callback =
25       ThreadPlanShouldStopHere::DefaultShouldStopHereCallback;
26   m_callbacks.step_from_here_callback =
27       ThreadPlanShouldStopHere::DefaultStepFromHereCallback;
28 }
29 
30 ThreadPlanShouldStopHere::ThreadPlanShouldStopHere(
31     ThreadPlan *owner, const ThreadPlanShouldStopHereCallbacks *callbacks,
32     void *baton)
33     : m_callbacks(), m_baton(), m_owner(owner),
34       m_flags(ThreadPlanShouldStopHere::eNone) {
35   SetShouldStopHereCallbacks(callbacks, baton);
36 }
37 
38 ThreadPlanShouldStopHere::~ThreadPlanShouldStopHere() = default;
39 
40 bool ThreadPlanShouldStopHere::InvokeShouldStopHereCallback(
41     FrameComparison operation, Status &status) {
42   bool should_stop_here = true;
43   if (m_callbacks.should_stop_here_callback) {
44     should_stop_here = m_callbacks.should_stop_here_callback(
45         m_owner, m_flags, operation, status, m_baton);
46     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
47     if (log) {
48       lldb::addr_t current_addr =
49           m_owner->GetThread().GetRegisterContext()->GetPC(0);
50 
51       log->Printf("ShouldStopHere callback returned %u from 0x%" PRIx64 ".",
52                   should_stop_here, current_addr);
53     }
54   }
55 
56   return should_stop_here;
57 }
58 
59 bool ThreadPlanShouldStopHere::DefaultShouldStopHereCallback(
60     ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
61     Status &status, void *baton) {
62   bool should_stop_here = true;
63   StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
64   if (!frame)
65     return true;
66 
67   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
68 
69   if ((operation == eFrameCompareOlder && flags.Test(eStepOutAvoidNoDebug)) ||
70       (operation == eFrameCompareYounger && flags.Test(eStepInAvoidNoDebug)) ||
71       (operation == eFrameCompareSameParent &&
72        flags.Test(eStepInAvoidNoDebug))) {
73     if (!frame->HasDebugInformation()) {
74       if (log)
75         log->Printf("Stepping out of frame with no debug info");
76 
77       should_stop_here = false;
78     }
79   }
80 
81   // Always avoid code with line number 0.
82   // FIXME: At present the ShouldStop and the StepFromHere calculate this
83   // independently.  If this ever
84   // becomes expensive (this one isn't) we can try to have this set a state
85   // that the StepFromHere can use.
86   if (frame) {
87     SymbolContext sc;
88     sc = frame->GetSymbolContext(eSymbolContextLineEntry);
89     if (sc.line_entry.line == 0)
90       should_stop_here = false;
91   }
92 
93   return should_stop_here;
94 }
95 
96 ThreadPlanSP ThreadPlanShouldStopHere::DefaultStepFromHereCallback(
97     ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
98     Status &status, void *baton) {
99   const bool stop_others = false;
100   const size_t frame_index = 0;
101   ThreadPlanSP return_plan_sp;
102   // If we are stepping through code at line number 0, then we need to step
103   // over this range.  Otherwise we will step out.
104   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
105 
106   StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
107   if (!frame)
108     return return_plan_sp;
109   SymbolContext sc;
110   sc = frame->GetSymbolContext(eSymbolContextLineEntry | eSymbolContextSymbol);
111 
112   if (sc.line_entry.line == 0) {
113     AddressRange range = sc.line_entry.range;
114 
115     // If the whole function is marked line 0 just step out, that's easier &
116     // faster than continuing to step through it.
117     bool just_step_out = false;
118     if (sc.symbol && sc.symbol->ValueIsAddress()) {
119       Address symbol_end = sc.symbol->GetAddress();
120       symbol_end.Slide(sc.symbol->GetByteSize() - 1);
121       if (range.ContainsFileAddress(sc.symbol->GetAddress()) &&
122           range.ContainsFileAddress(symbol_end)) {
123         if (log)
124           log->Printf("Stopped in a function with only line 0 lines, just "
125                       "stepping out.");
126         just_step_out = true;
127       }
128     }
129     if (!just_step_out) {
130       if (log)
131         log->Printf("ThreadPlanShouldStopHere::DefaultStepFromHereCallback "
132                     "Queueing StepInRange plan to step through line 0 code.");
133 
134       return_plan_sp = current_plan->GetThread().QueueThreadPlanForStepInRange(
135           false, range, sc, NULL, eOnlyDuringStepping, status,
136           eLazyBoolCalculate, eLazyBoolNo);
137     }
138   }
139 
140   if (!return_plan_sp)
141     return_plan_sp =
142         current_plan->GetThread().QueueThreadPlanForStepOutNoShouldStop(
143             false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion,
144             frame_index, status, true);
145   return return_plan_sp;
146 }
147 
148 ThreadPlanSP ThreadPlanShouldStopHere::QueueStepOutFromHerePlan(
149     lldb_private::Flags &flags, lldb::FrameComparison operation,
150     Status &status) {
151   ThreadPlanSP return_plan_sp;
152   if (m_callbacks.step_from_here_callback) {
153     return_plan_sp = m_callbacks.step_from_here_callback(
154         m_owner, flags, operation, status, m_baton);
155   }
156   return return_plan_sp;
157 }
158 
159 lldb::ThreadPlanSP ThreadPlanShouldStopHere::CheckShouldStopHereAndQueueStepOut(
160     lldb::FrameComparison operation, Status &status) {
161   if (!InvokeShouldStopHereCallback(operation, status))
162     return QueueStepOutFromHerePlan(m_flags, operation, status);
163   else
164     return ThreadPlanSP();
165 }
166