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