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, bool first_instruction_only) { 66 m_recognizers.push_front({(uint32_t)m_recognizers.size(), recognizer, false, 67 module, RegularExpressionSP(), symbols, 68 RegularExpressionSP(), first_instruction_only}); 69 BumpGeneration(); 70 } 71 72 void StackFrameRecognizerManager::AddRecognizer( 73 StackFrameRecognizerSP recognizer, RegularExpressionSP module, 74 RegularExpressionSP symbol, bool first_instruction_only) { 75 m_recognizers.push_front({(uint32_t)m_recognizers.size(), recognizer, true, 76 ConstString(), module, std::vector<ConstString>(), 77 symbol, first_instruction_only}); 78 BumpGeneration(); 79 } 80 81 void StackFrameRecognizerManager::ForEach( 82 const std::function<void(uint32_t, std::string, std::string, 83 llvm::ArrayRef<ConstString>, bool)> &callback) { 84 for (auto entry : m_recognizers) { 85 if (entry.is_regexp) { 86 std::string module_name; 87 std::string symbol_name; 88 89 if (entry.module_regexp) 90 module_name = entry.module_regexp->GetText().str(); 91 if (entry.symbol_regexp) 92 symbol_name = entry.symbol_regexp->GetText().str(); 93 94 callback(entry.recognizer_id, entry.recognizer->GetName(), module_name, 95 llvm::ArrayRef(ConstString(symbol_name)), true); 96 97 } else { 98 callback(entry.recognizer_id, entry.recognizer->GetName(), 99 entry.module.GetCString(), entry.symbols, false); 100 } 101 } 102 } 103 104 bool StackFrameRecognizerManager::RemoveRecognizerWithID( 105 uint32_t recognizer_id) { 106 if (recognizer_id >= m_recognizers.size()) 107 return false; 108 auto found = 109 llvm::find_if(m_recognizers, [recognizer_id](const RegisteredEntry &e) { 110 return e.recognizer_id == recognizer_id; 111 }); 112 if (found == m_recognizers.end()) 113 return false; 114 m_recognizers.erase(found); 115 BumpGeneration(); 116 return true; 117 } 118 119 void StackFrameRecognizerManager::RemoveAllRecognizers() { 120 BumpGeneration(); 121 m_recognizers.clear(); 122 } 123 124 StackFrameRecognizerSP 125 StackFrameRecognizerManager::GetRecognizerForFrame(StackFrameSP frame) { 126 const SymbolContext &symctx = frame->GetSymbolContext( 127 eSymbolContextModule | eSymbolContextFunction | eSymbolContextSymbol); 128 ConstString function_name = symctx.GetFunctionName(); 129 ModuleSP module_sp = symctx.module_sp; 130 if (!module_sp) 131 return StackFrameRecognizerSP(); 132 ConstString module_name = module_sp->GetFileSpec().GetFilename(); 133 Symbol *symbol = symctx.symbol; 134 if (!symbol) 135 return StackFrameRecognizerSP(); 136 Address start_addr = symbol->GetAddress(); 137 Address current_addr = frame->GetFrameCodeAddress(); 138 139 for (auto entry : m_recognizers) { 140 if (entry.module) 141 if (entry.module != module_name) 142 continue; 143 144 if (entry.module_regexp) 145 if (!entry.module_regexp->Execute(module_name.GetStringRef())) 146 continue; 147 148 if (!entry.symbols.empty()) 149 if (!llvm::is_contained(entry.symbols, function_name)) 150 continue; 151 152 if (entry.symbol_regexp) 153 if (!entry.symbol_regexp->Execute(function_name.GetStringRef())) 154 continue; 155 156 if (entry.first_instruction_only) 157 if (start_addr != current_addr) 158 continue; 159 160 return entry.recognizer; 161 } 162 return StackFrameRecognizerSP(); 163 } 164 165 RecognizedStackFrameSP 166 StackFrameRecognizerManager::RecognizeFrame(StackFrameSP frame) { 167 auto recognizer = GetRecognizerForFrame(frame); 168 if (!recognizer) 169 return RecognizedStackFrameSP(); 170 return recognizer->RecognizeFrame(frame); 171 } 172