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 <vector> 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/Target/StackFrameRecognizer.h" 15 #include "lldb/Utility/RegularExpression.h" 16 17 using namespace lldb; 18 using namespace lldb_private; 19 20 class ScriptedRecognizedStackFrame : public RecognizedStackFrame { 21 public: 22 ScriptedRecognizedStackFrame(ValueObjectListSP args) { 23 m_arguments = args; 24 } 25 }; 26 27 ScriptedStackFrameRecognizer::ScriptedStackFrameRecognizer( 28 ScriptInterpreter *interpreter, const char *pclass) 29 : m_interpreter(interpreter), m_python_class(pclass) { 30 m_python_object_sp = 31 m_interpreter->CreateFrameRecognizer(m_python_class.c_str()); 32 } 33 34 RecognizedStackFrameSP 35 ScriptedStackFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame) { 36 if (!m_python_object_sp || !m_interpreter) 37 return RecognizedStackFrameSP(); 38 39 ValueObjectListSP args = 40 m_interpreter->GetRecognizedArguments(m_python_object_sp, frame); 41 auto args_synthesized = ValueObjectListSP(new ValueObjectList()); 42 for (const auto &o : args->GetObjects()) { 43 args_synthesized->Append(ValueObjectRecognizerSynthesizedValue::Create( 44 *o, eValueTypeVariableArgument)); 45 } 46 47 return RecognizedStackFrameSP( 48 new ScriptedRecognizedStackFrame(args_synthesized)); 49 } 50 51 class StackFrameRecognizerManagerImpl { 52 public: 53 void AddRecognizer(StackFrameRecognizerSP recognizer, ConstString module, 54 ConstString symbol, ConstString alternate_symbol, 55 bool first_instruction_only) { 56 m_recognizers.push_front({(uint32_t)m_recognizers.size(), false, recognizer, 57 false, module, RegularExpressionSP(), symbol, 58 alternate_symbol, RegularExpressionSP(), 59 first_instruction_only}); 60 } 61 62 void AddRecognizer(StackFrameRecognizerSP recognizer, 63 RegularExpressionSP module, RegularExpressionSP symbol, 64 bool first_instruction_only) { 65 m_recognizers.push_front({(uint32_t)m_recognizers.size(), false, recognizer, 66 true, ConstString(), module, ConstString(), 67 ConstString(), symbol, first_instruction_only}); 68 } 69 70 void ForEach( 71 std::function<void(uint32_t recognized_id, std::string recognizer_name, 72 std::string module, std::string symbol, 73 std::string alternate_symbol, bool regexp)> const 74 &callback) { 75 for (auto entry : m_recognizers) { 76 if (entry.is_regexp) { 77 std::string module_name; 78 std::string symbol_name; 79 80 if (entry.module_regexp) 81 module_name = entry.module_regexp->GetText().str(); 82 if (entry.symbol_regexp) 83 symbol_name = entry.symbol_regexp->GetText().str(); 84 85 callback(entry.recognizer_id, entry.recognizer->GetName(), module_name, 86 symbol_name, {}, true); 87 88 } else { 89 std::string alternate_symbol; 90 if (!entry.alternate_symbol.IsEmpty()) 91 alternate_symbol.append(entry.alternate_symbol.GetCString()); 92 93 callback(entry.recognizer_id, entry.recognizer->GetName(), 94 entry.module.GetCString(), entry.symbol.GetCString(), 95 alternate_symbol, false); 96 } 97 } 98 } 99 100 bool RemoveRecognizerWithID(uint32_t recognizer_id) { 101 if (recognizer_id >= m_recognizers.size()) return false; 102 if (m_recognizers[recognizer_id].deleted) return false; 103 m_recognizers[recognizer_id].deleted = true; 104 return true; 105 } 106 107 void RemoveAllRecognizers() { 108 m_recognizers.clear(); 109 } 110 111 StackFrameRecognizerSP GetRecognizerForFrame(StackFrameSP frame) { 112 const SymbolContext &symctx = frame->GetSymbolContext( 113 eSymbolContextModule | eSymbolContextFunction | eSymbolContextSymbol); 114 ConstString function_name = symctx.GetFunctionName(); 115 ModuleSP module_sp = symctx.module_sp; 116 if (!module_sp) return StackFrameRecognizerSP(); 117 ConstString module_name = module_sp->GetFileSpec().GetFilename(); 118 Symbol *symbol = symctx.symbol; 119 if (!symbol) return StackFrameRecognizerSP(); 120 Address start_addr = symbol->GetAddress(); 121 Address current_addr = frame->GetFrameCodeAddress(); 122 123 for (auto entry : m_recognizers) { 124 if (entry.deleted) continue; 125 if (entry.module) 126 if (entry.module != module_name) continue; 127 128 if (entry.module_regexp) 129 if (!entry.module_regexp->Execute(module_name.GetStringRef())) continue; 130 131 if (entry.symbol) 132 if (entry.symbol != function_name && 133 (!entry.alternate_symbol || 134 entry.alternate_symbol != function_name)) 135 continue; 136 137 if (entry.symbol_regexp) 138 if (!entry.symbol_regexp->Execute(function_name.GetStringRef())) 139 continue; 140 141 if (entry.first_instruction_only) 142 if (start_addr != current_addr) continue; 143 144 return entry.recognizer; 145 } 146 return StackFrameRecognizerSP(); 147 } 148 149 RecognizedStackFrameSP RecognizeFrame(StackFrameSP frame) { 150 auto recognizer = GetRecognizerForFrame(frame); 151 if (!recognizer) return RecognizedStackFrameSP(); 152 return recognizer->RecognizeFrame(frame); 153 } 154 155 private: 156 struct RegisteredEntry { 157 uint32_t recognizer_id; 158 bool deleted; 159 StackFrameRecognizerSP recognizer; 160 bool is_regexp; 161 ConstString module; 162 RegularExpressionSP module_regexp; 163 ConstString symbol; 164 ConstString alternate_symbol; 165 RegularExpressionSP symbol_regexp; 166 bool first_instruction_only; 167 }; 168 169 std::deque<RegisteredEntry> m_recognizers; 170 }; 171 172 StackFrameRecognizerManagerImpl &GetStackFrameRecognizerManagerImpl() { 173 static StackFrameRecognizerManagerImpl instance = 174 StackFrameRecognizerManagerImpl(); 175 return instance; 176 } 177 178 void StackFrameRecognizerManager::AddRecognizer( 179 StackFrameRecognizerSP recognizer, ConstString module, ConstString symbol, 180 ConstString alternate_symbol, bool first_instruction_only) { 181 GetStackFrameRecognizerManagerImpl().AddRecognizer( 182 recognizer, module, symbol, alternate_symbol, first_instruction_only); 183 } 184 185 void StackFrameRecognizerManager::AddRecognizer( 186 StackFrameRecognizerSP recognizer, RegularExpressionSP module, 187 RegularExpressionSP symbol, bool first_instruction_only) { 188 GetStackFrameRecognizerManagerImpl().AddRecognizer(recognizer, module, symbol, 189 first_instruction_only); 190 } 191 192 void StackFrameRecognizerManager::ForEach( 193 std::function<void(uint32_t recognized_id, std::string recognizer_name, 194 std::string module, std::string symbol, 195 std::string alternate_symbol, bool regexp)> const 196 &callback) { 197 GetStackFrameRecognizerManagerImpl().ForEach(callback); 198 } 199 200 void StackFrameRecognizerManager::RemoveAllRecognizers() { 201 GetStackFrameRecognizerManagerImpl().RemoveAllRecognizers(); 202 } 203 204 bool StackFrameRecognizerManager::RemoveRecognizerWithID(uint32_t recognizer_id) { 205 return GetStackFrameRecognizerManagerImpl().RemoveRecognizerWithID(recognizer_id); 206 } 207 208 RecognizedStackFrameSP StackFrameRecognizerManager::RecognizeFrame( 209 StackFrameSP frame) { 210 return GetStackFrameRecognizerManagerImpl().RecognizeFrame(frame); 211 } 212 213 StackFrameRecognizerSP StackFrameRecognizerManager::GetRecognizerForFrame( 214 lldb::StackFrameSP frame) { 215 return GetStackFrameRecognizerManagerImpl().GetRecognizerForFrame(frame); 216 } 217