xref: /llvm-project/lldb/source/Target/StackFrameRecognizer.cpp (revision dd060bdede8edec18ad5ca122e15cc24a821e3fe)
1 //===-- StackFrameRecognizer.cpp ------------------------------------------===//
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/StackFrameRecognizer.h"
10 #include "lldb/Core/Module.h"
11 #include "lldb/Interpreter/ScriptInterpreter.h"
12 #include "lldb/Symbol/Symbol.h"
13 #include "lldb/Target/StackFrame.h"
14 #include "lldb/Utility/RegularExpression.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 class ScriptedRecognizedStackFrame : public RecognizedStackFrame {
20   bool m_hidden;
21 
22 public:
23   ScriptedRecognizedStackFrame(ValueObjectListSP args, bool hidden)
24       : m_hidden(hidden) {
25     m_arguments = std::move(args);
26   }
27   bool ShouldHide() override { return m_hidden; }
28 };
29 
30 ScriptedStackFrameRecognizer::ScriptedStackFrameRecognizer(
31     ScriptInterpreter *interpreter, const char *pclass)
32     : m_interpreter(interpreter), m_python_class(pclass) {
33   m_python_object_sp =
34       m_interpreter->CreateFrameRecognizer(m_python_class.c_str());
35 }
36 
37 RecognizedStackFrameSP
38 ScriptedStackFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame) {
39   if (!m_python_object_sp || !m_interpreter)
40     return RecognizedStackFrameSP();
41 
42   ValueObjectListSP args =
43       m_interpreter->GetRecognizedArguments(m_python_object_sp, frame);
44   auto args_synthesized = ValueObjectListSP(new ValueObjectList());
45   if (args) {
46     for (const auto &o : args->GetObjects())
47       args_synthesized->Append(ValueObjectRecognizerSynthesizedValue::Create(
48           *o, eValueTypeVariableArgument));
49   }
50 
51   bool hidden = m_interpreter->ShouldHide(m_python_object_sp, frame);
52 
53   return RecognizedStackFrameSP(
54       new ScriptedRecognizedStackFrame(args_synthesized, hidden));
55 }
56 
57 void StackFrameRecognizerManager::BumpGeneration() {
58   uint32_t n = m_generation;
59   n = (n + 1) & ((1 << 16) - 1);
60   m_generation = n;
61 }
62 
63 void StackFrameRecognizerManager::AddRecognizer(
64     StackFrameRecognizerSP recognizer, ConstString module,
65     llvm::ArrayRef<ConstString> symbols,
66     Mangled::NamePreference symbol_mangling, bool first_instruction_only) {
67   m_recognizers.push_front({(uint32_t)m_recognizers.size(), recognizer, false,
68                             module, RegularExpressionSP(), symbols,
69                             RegularExpressionSP(), symbol_mangling,
70                             first_instruction_only});
71   BumpGeneration();
72 }
73 
74 void StackFrameRecognizerManager::AddRecognizer(
75     StackFrameRecognizerSP recognizer, RegularExpressionSP module,
76     RegularExpressionSP symbol, Mangled::NamePreference symbol_mangling,
77     bool first_instruction_only) {
78   m_recognizers.push_front({(uint32_t)m_recognizers.size(), recognizer, true,
79                             ConstString(), module, std::vector<ConstString>(),
80                             symbol, symbol_mangling, first_instruction_only});
81   BumpGeneration();
82 }
83 
84 void StackFrameRecognizerManager::ForEach(
85     const std::function<
86         void(uint32_t, std::string, std::string, llvm::ArrayRef<ConstString>,
87              Mangled::NamePreference name_reference, bool)> &callback) {
88   for (auto entry : m_recognizers) {
89     if (entry.is_regexp) {
90       std::string module_name;
91       std::string symbol_name;
92 
93       if (entry.module_regexp)
94         module_name = entry.module_regexp->GetText().str();
95       if (entry.symbol_regexp)
96         symbol_name = entry.symbol_regexp->GetText().str();
97 
98       callback(entry.recognizer_id, entry.recognizer->GetName(), module_name,
99                llvm::ArrayRef(ConstString(symbol_name)), entry.symbol_mangling,
100                true);
101 
102     } else {
103       callback(entry.recognizer_id, entry.recognizer->GetName(),
104                entry.module.GetCString(), entry.symbols, entry.symbol_mangling,
105                false);
106     }
107   }
108 }
109 
110 bool StackFrameRecognizerManager::RemoveRecognizerWithID(
111     uint32_t recognizer_id) {
112   if (recognizer_id >= m_recognizers.size())
113     return false;
114   auto found =
115       llvm::find_if(m_recognizers, [recognizer_id](const RegisteredEntry &e) {
116         return e.recognizer_id == recognizer_id;
117       });
118   if (found == m_recognizers.end())
119     return false;
120   m_recognizers.erase(found);
121   BumpGeneration();
122   return true;
123 }
124 
125 void StackFrameRecognizerManager::RemoveAllRecognizers() {
126   BumpGeneration();
127   m_recognizers.clear();
128 }
129 
130 StackFrameRecognizerSP
131 StackFrameRecognizerManager::GetRecognizerForFrame(StackFrameSP frame) {
132   const SymbolContext &symctx = frame->GetSymbolContext(
133       eSymbolContextModule | eSymbolContextFunction | eSymbolContextSymbol);
134   ModuleSP module_sp = symctx.module_sp;
135   if (!module_sp)
136     return StackFrameRecognizerSP();
137   ConstString module_name = module_sp->GetFileSpec().GetFilename();
138   Symbol *symbol = symctx.symbol;
139   if (!symbol)
140     return StackFrameRecognizerSP();
141   Address start_addr = symbol->GetAddress();
142   Address current_addr = frame->GetFrameCodeAddress();
143 
144   for (auto entry : m_recognizers) {
145     if (entry.module)
146       if (entry.module != module_name)
147         continue;
148 
149     if (entry.module_regexp)
150       if (!entry.module_regexp->Execute(module_name.GetStringRef()))
151         continue;
152 
153     ConstString function_name = symctx.GetFunctionName(entry.symbol_mangling);
154 
155     if (!entry.symbols.empty())
156       if (!llvm::is_contained(entry.symbols, function_name))
157         continue;
158 
159     if (entry.symbol_regexp)
160       if (!entry.symbol_regexp->Execute(function_name.GetStringRef()))
161         continue;
162 
163     if (entry.first_instruction_only)
164       if (start_addr != current_addr)
165         continue;
166 
167     return entry.recognizer;
168   }
169   return StackFrameRecognizerSP();
170 }
171 
172 RecognizedStackFrameSP
173 StackFrameRecognizerManager::RecognizeFrame(StackFrameSP frame) {
174   auto recognizer = GetRecognizerForFrame(frame);
175   if (!recognizer)
176     return RecognizedStackFrameSP();
177   return recognizer->RecognizeFrame(frame);
178 }
179