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, 54 ConstString module, ConstString symbol, 55 bool first_instruction_only) { 56 m_recognizers.push_front({(uint32_t)m_recognizers.size(), false, recognizer, false, module, RegularExpressionSP(), 57 symbol, RegularExpressionSP(), 58 first_instruction_only}); 59 } 60 61 void AddRecognizer(StackFrameRecognizerSP recognizer, 62 RegularExpressionSP module, RegularExpressionSP symbol, 63 bool first_instruction_only) { 64 m_recognizers.push_front({(uint32_t)m_recognizers.size(), false, recognizer, true, ConstString(), module, 65 ConstString(), symbol, first_instruction_only}); 66 } 67 68 void ForEach( 69 std::function<void(uint32_t recognized_id, std::string recognizer_name, std::string module, 70 std::string symbol, bool regexp)> const &callback) { 71 for (auto entry : m_recognizers) { 72 if (entry.is_regexp) { 73 std::string module_name; 74 std::string symbol_name; 75 76 if (entry.module_regexp) 77 module_name = entry.module_regexp->GetText().str(); 78 if (entry.symbol_regexp) 79 symbol_name = entry.symbol_regexp->GetText().str(); 80 81 callback(entry.recognizer_id, entry.recognizer->GetName(), module_name, 82 symbol_name, true); 83 84 } else { 85 callback(entry.recognizer_id, entry.recognizer->GetName(), entry.module.GetCString(), 86 entry.symbol.GetCString(), false); 87 } 88 } 89 } 90 91 bool RemoveRecognizerWithID(uint32_t recognizer_id) { 92 if (recognizer_id >= m_recognizers.size()) return false; 93 if (m_recognizers[recognizer_id].deleted) return false; 94 m_recognizers[recognizer_id].deleted = true; 95 return true; 96 } 97 98 void RemoveAllRecognizers() { 99 m_recognizers.clear(); 100 } 101 102 StackFrameRecognizerSP GetRecognizerForFrame(StackFrameSP frame) { 103 const SymbolContext &symctx = frame->GetSymbolContext( 104 eSymbolContextModule | eSymbolContextFunction | eSymbolContextSymbol); 105 ConstString function_name = symctx.GetFunctionName(); 106 ModuleSP module_sp = symctx.module_sp; 107 if (!module_sp) return StackFrameRecognizerSP(); 108 ConstString module_name = module_sp->GetFileSpec().GetFilename(); 109 Symbol *symbol = symctx.symbol; 110 if (!symbol) return StackFrameRecognizerSP(); 111 Address start_addr = symbol->GetAddress(); 112 Address current_addr = frame->GetFrameCodeAddress(); 113 114 for (auto entry : m_recognizers) { 115 if (entry.deleted) continue; 116 if (entry.module) 117 if (entry.module != module_name) continue; 118 119 if (entry.module_regexp) 120 if (!entry.module_regexp->Execute(module_name.GetStringRef())) continue; 121 122 if (entry.symbol) 123 if (entry.symbol != function_name) continue; 124 125 if (entry.symbol_regexp) 126 if (!entry.symbol_regexp->Execute(function_name.GetStringRef())) 127 continue; 128 129 if (entry.first_instruction_only) 130 if (start_addr != current_addr) continue; 131 132 return entry.recognizer; 133 } 134 return StackFrameRecognizerSP(); 135 } 136 137 RecognizedStackFrameSP RecognizeFrame(StackFrameSP frame) { 138 auto recognizer = GetRecognizerForFrame(frame); 139 if (!recognizer) return RecognizedStackFrameSP(); 140 return recognizer->RecognizeFrame(frame); 141 } 142 143 private: 144 struct RegisteredEntry { 145 uint32_t recognizer_id; 146 bool deleted; 147 StackFrameRecognizerSP recognizer; 148 bool is_regexp; 149 ConstString module; 150 RegularExpressionSP module_regexp; 151 ConstString symbol; 152 RegularExpressionSP symbol_regexp; 153 bool first_instruction_only; 154 }; 155 156 std::deque<RegisteredEntry> m_recognizers; 157 }; 158 159 StackFrameRecognizerManagerImpl &GetStackFrameRecognizerManagerImpl() { 160 static StackFrameRecognizerManagerImpl instance = 161 StackFrameRecognizerManagerImpl(); 162 return instance; 163 } 164 165 void StackFrameRecognizerManager::AddRecognizer( 166 StackFrameRecognizerSP recognizer, ConstString module, 167 ConstString symbol, bool first_instruction_only) { 168 GetStackFrameRecognizerManagerImpl().AddRecognizer(recognizer, module, symbol, 169 first_instruction_only); 170 } 171 172 void StackFrameRecognizerManager::AddRecognizer( 173 StackFrameRecognizerSP recognizer, RegularExpressionSP module, 174 RegularExpressionSP symbol, bool first_instruction_only) { 175 GetStackFrameRecognizerManagerImpl().AddRecognizer(recognizer, module, symbol, 176 first_instruction_only); 177 } 178 179 void StackFrameRecognizerManager::ForEach( 180 std::function<void(uint32_t recognized_id, std::string recognizer_name, std::string module, 181 std::string symbol, bool regexp)> const &callback) { 182 GetStackFrameRecognizerManagerImpl().ForEach(callback); 183 } 184 185 void StackFrameRecognizerManager::RemoveAllRecognizers() { 186 GetStackFrameRecognizerManagerImpl().RemoveAllRecognizers(); 187 } 188 189 bool StackFrameRecognizerManager::RemoveRecognizerWithID(uint32_t recognizer_id) { 190 return GetStackFrameRecognizerManagerImpl().RemoveRecognizerWithID(recognizer_id); 191 } 192 193 RecognizedStackFrameSP StackFrameRecognizerManager::RecognizeFrame( 194 StackFrameSP frame) { 195 return GetStackFrameRecognizerManagerImpl().RecognizeFrame(frame); 196 } 197 198 StackFrameRecognizerSP StackFrameRecognizerManager::GetRecognizerForFrame( 199 lldb::StackFrameSP frame) { 200 return GetStackFrameRecognizerManagerImpl().GetRecognizerForFrame(frame); 201 } 202