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 public:
21   ScriptedRecognizedStackFrame(ValueObjectListSP args) {
22     m_arguments = args;
23   }
24 };
25 
26 ScriptedStackFrameRecognizer::ScriptedStackFrameRecognizer(
27     ScriptInterpreter *interpreter, const char *pclass)
28     : m_interpreter(interpreter), m_python_class(pclass) {
29   m_python_object_sp =
30       m_interpreter->CreateFrameRecognizer(m_python_class.c_str());
31 }
32 
33 RecognizedStackFrameSP
34 ScriptedStackFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame) {
35   if (!m_python_object_sp || !m_interpreter)
36     return RecognizedStackFrameSP();
37 
38   ValueObjectListSP args =
39       m_interpreter->GetRecognizedArguments(m_python_object_sp, frame);
40   auto args_synthesized = ValueObjectListSP(new ValueObjectList());
41   for (const auto &o : args->GetObjects()) {
42     args_synthesized->Append(ValueObjectRecognizerSynthesizedValue::Create(
43         *o, eValueTypeVariableArgument));
44   }
45 
46   return RecognizedStackFrameSP(
47       new ScriptedRecognizedStackFrame(args_synthesized));
48 }
49 
50 void StackFrameRecognizerManager::AddRecognizer(
51     StackFrameRecognizerSP recognizer, ConstString module,
52     llvm::ArrayRef<ConstString> symbols, bool first_instruction_only) {
53   m_recognizers.push_front({(uint32_t)m_recognizers.size(), false, recognizer,
54                             false, module, RegularExpressionSP(), symbols,
55                             RegularExpressionSP(), first_instruction_only});
56 }
57 
58 void StackFrameRecognizerManager::AddRecognizer(
59     StackFrameRecognizerSP recognizer, RegularExpressionSP module,
60     RegularExpressionSP symbol, bool first_instruction_only) {
61   m_recognizers.push_front(
62       {(uint32_t)m_recognizers.size(), false, recognizer, true, ConstString(),
63        module, std::vector<ConstString>(), symbol, first_instruction_only});
64 }
65 
66 void StackFrameRecognizerManager::ForEach(
67     const std::function<void(uint32_t, std::string, std::string,
68                              llvm::ArrayRef<ConstString>, bool)> &callback) {
69   for (auto entry : m_recognizers) {
70     if (entry.is_regexp) {
71       std::string module_name;
72       std::string symbol_name;
73 
74       if (entry.module_regexp)
75         module_name = entry.module_regexp->GetText().str();
76       if (entry.symbol_regexp)
77         symbol_name = entry.symbol_regexp->GetText().str();
78 
79       callback(entry.recognizer_id, entry.recognizer->GetName(), module_name,
80                llvm::makeArrayRef(ConstString(symbol_name)), true);
81 
82     } else {
83       callback(entry.recognizer_id, entry.recognizer->GetName(),
84                entry.module.GetCString(), entry.symbols, false);
85     }
86   }
87 }
88 
89 bool StackFrameRecognizerManager::RemoveRecognizerWithID(
90     uint32_t recognizer_id) {
91   if (recognizer_id >= m_recognizers.size())
92     return false;
93   if (m_recognizers[recognizer_id].deleted)
94     return false;
95   m_recognizers[recognizer_id].deleted = true;
96   return true;
97 }
98 
99 void StackFrameRecognizerManager::RemoveAllRecognizers() {
100   m_recognizers.clear();
101 }
102 
103 StackFrameRecognizerSP
104 StackFrameRecognizerManager::GetRecognizerForFrame(StackFrameSP frame) {
105   const SymbolContext &symctx = frame->GetSymbolContext(
106       eSymbolContextModule | eSymbolContextFunction | eSymbolContextSymbol);
107   ConstString function_name = symctx.GetFunctionName();
108   ModuleSP module_sp = symctx.module_sp;
109   if (!module_sp)
110     return StackFrameRecognizerSP();
111   ConstString module_name = module_sp->GetFileSpec().GetFilename();
112   Symbol *symbol = symctx.symbol;
113   if (!symbol)
114     return StackFrameRecognizerSP();
115   Address start_addr = symbol->GetAddress();
116   Address current_addr = frame->GetFrameCodeAddress();
117 
118   for (auto entry : m_recognizers) {
119     if (entry.deleted)
120       continue;
121     if (entry.module)
122       if (entry.module != module_name)
123         continue;
124 
125     if (entry.module_regexp)
126       if (!entry.module_regexp->Execute(module_name.GetStringRef()))
127         continue;
128 
129     if (!entry.symbols.empty())
130       if (!llvm::is_contained(entry.symbols, function_name))
131         continue;
132 
133     if (entry.symbol_regexp)
134       if (!entry.symbol_regexp->Execute(function_name.GetStringRef()))
135         continue;
136 
137     if (entry.first_instruction_only)
138       if (start_addr != current_addr)
139         continue;
140 
141     return entry.recognizer;
142   }
143   return StackFrameRecognizerSP();
144 }
145 
146 RecognizedStackFrameSP
147 StackFrameRecognizerManager::RecognizeFrame(StackFrameSP frame) {
148   auto recognizer = GetRecognizerForFrame(frame);
149   if (!recognizer)
150     return RecognizedStackFrameSP();
151   return recognizer->RecognizeFrame(frame);
152 }
153