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