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