LLDB  mainline
StackFrameRecognizer.cpp
Go to the documentation of this file.
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 
10 #include "lldb/Core/Module.h"
12 #include "lldb/Symbol/Symbol.h"
13 #include "lldb/Target/StackFrame.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
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) {
31 }
32 
33 RecognizedStackFrameSP
36  return RecognizedStackFrameSP();
37 
38  ValueObjectListSP args =
40  auto args_synthesized = ValueObjectListSP(new ValueObjectList());
41  for (const auto &o : args->GetObjects()) {
42  args_synthesized->Append(ValueObjectRecognizerSynthesizedValue::Create(
44  }
45 
46  return RecognizedStackFrameSP(
47  new ScriptedRecognizedStackFrame(args_synthesized));
48 }
49 
51  StackFrameRecognizerSP recognizer, ConstString module,
52  llvm::ArrayRef<ConstString> symbols, bool first_instruction_only) {
53  m_recognizers.push_front({(uint32_t)m_recognizers.size(), recognizer, false,
54  module, RegularExpressionSP(), symbols,
55  RegularExpressionSP(), first_instruction_only});
56 }
57 
59  StackFrameRecognizerSP recognizer, RegularExpressionSP module,
60  RegularExpressionSP symbol, bool first_instruction_only) {
61  m_recognizers.push_front({(uint32_t)m_recognizers.size(), recognizer, true,
62  ConstString(), module, std::vector<ConstString>(),
63  symbol, first_instruction_only});
64 }
65 
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 
90  uint32_t recognizer_id) {
91  if (recognizer_id >= m_recognizers.size())
92  return false;
93  auto found =
94  llvm::find_if(m_recognizers, [recognizer_id](const RegisteredEntry &e) {
95  return e.recognizer_id == recognizer_id;
96  });
97  if (found == m_recognizers.end())
98  return false;
99  m_recognizers.erase(found);
100  return true;
101 }
102 
104  m_recognizers.clear();
105 }
106 
107 StackFrameRecognizerSP
109  const SymbolContext &symctx = frame->GetSymbolContext(
110  eSymbolContextModule | eSymbolContextFunction | eSymbolContextSymbol);
111  ConstString function_name = symctx.GetFunctionName();
112  ModuleSP module_sp = symctx.module_sp;
113  if (!module_sp)
114  return StackFrameRecognizerSP();
115  ConstString module_name = module_sp->GetFileSpec().GetFilename();
116  Symbol *symbol = symctx.symbol;
117  if (!symbol)
118  return StackFrameRecognizerSP();
119  Address start_addr = symbol->GetAddress();
120  Address current_addr = frame->GetFrameCodeAddress();
121 
122  for (auto entry : m_recognizers) {
123  if (entry.module)
124  if (entry.module != module_name)
125  continue;
126 
127  if (entry.module_regexp)
128  if (!entry.module_regexp->Execute(module_name.GetStringRef()))
129  continue;
130 
131  if (!entry.symbols.empty())
132  if (!llvm::is_contained(entry.symbols, function_name))
133  continue;
134 
135  if (entry.symbol_regexp)
136  if (!entry.symbol_regexp->Execute(function_name.GetStringRef()))
137  continue;
138 
139  if (entry.first_instruction_only)
140  if (start_addr != current_addr)
141  continue;
142 
143  return entry.recognizer;
144  }
145  return StackFrameRecognizerSP();
146 }
147 
148 RecognizedStackFrameSP
150  auto recognizer = GetRecognizerForFrame(frame);
151  if (!recognizer)
152  return RecognizedStackFrameSP();
153  return recognizer->RecognizeFrame(frame);
154 }
RegularExpression.h
lldb_private::ValueObjectList
A collection of ValueObject values that.
Definition: ValueObjectList.h:23
lldb_private::StackFrameRecognizerManager::RecognizeFrame
lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame)
Definition: StackFrameRecognizer.cpp:149
lldb_private::Symbol
Definition: Symbol.h:20
lldb_private::ScriptInterpreter::GetRecognizedArguments
virtual lldb::ValueObjectListSP GetRecognizedArguments(const StructuredData::ObjectSP &implementor, lldb::StackFrameSP frame_sp)
Definition: ScriptInterpreter.h:216
ScriptedRecognizedStackFrame
Definition: StackFrameRecognizer.cpp:19
lldb_private::ScriptedStackFrameRecognizer::m_python_class
std::string m_python_class
Definition: StackFrameRecognizer.h:78
lldb_private::ValueObjectRecognizerSynthesizedValue::Create
static lldb::ValueObjectSP Create(ValueObject &parent, lldb::ValueType type)
Definition: StackFrameRecognizer.h:148
Module.h
lldb_private::ScriptedStackFrameRecognizer::m_python_object_sp
lldb_private::StructuredData::ObjectSP m_python_object_sp
Definition: StackFrameRecognizer.h:77
StackFrame.h
lldb_private::StackFrameRecognizerManager::GetRecognizerForFrame
lldb::StackFrameRecognizerSP GetRecognizerForFrame(lldb::StackFrameSP frame)
Definition: StackFrameRecognizer.cpp:108
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::SymbolContext::symbol
Symbol * symbol
The Symbol for a given query.
Definition: SymbolContext.h:323
lldb_private::ScriptedStackFrameRecognizer::RecognizeFrame
lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame) override
Definition: StackFrameRecognizer.cpp:34
lldb_private::StackFrameRecognizerManager::m_recognizers
std::deque< RegisteredEntry > m_recognizers
Definition: StackFrameRecognizer.h:137
lldb_private::ConstString::GetStringRef
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:202
lldb_private::StackFrameRecognizerManager::ForEach
void ForEach(std::function< void(uint32_t recognizer_id, std::string recognizer_name, std::string module, llvm::ArrayRef< ConstString > symbols, bool regexp)> const &callback)
Definition: StackFrameRecognizer.cpp:66
lldb_private::StackFrameRecognizerManager::RemoveAllRecognizers
void RemoveAllRecognizers()
Definition: StackFrameRecognizer.cpp:103
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::SymbolContext::GetFunctionName
ConstString GetFunctionName(Mangled::NamePreference preference=Mangled::ePreferDemangled) const
Find a name of the innermost function for the symbol context.
Definition: SymbolContext.cpp:656
lldb::eValueTypeVariableArgument
@ eValueTypeVariableArgument
function argument variables
Definition: lldb-enumerations.h:319
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb_private::StackFrameRecognizerManager::RegisteredEntry::recognizer_id
uint32_t recognizer_id
Definition: StackFrameRecognizer.h:127
lldb_private::ScriptInterpreter::CreateFrameRecognizer
virtual StructuredData::GenericSP CreateFrameRecognizer(const char *class_name)
Definition: ScriptInterpreter.h:212
lldb_private::ScriptedStackFrameRecognizer::m_interpreter
lldb_private::ScriptInterpreter * m_interpreter
Definition: StackFrameRecognizer.h:76
lldb_private::StackFrameRecognizerManager::AddRecognizer
void AddRecognizer(lldb::StackFrameRecognizerSP recognizer, ConstString module, llvm::ArrayRef< ConstString > symbols, bool first_instruction_only=true)
Symbol.h
uint32_t
lldb_private::Address
Definition: Address.h:59
lldb_private::RecognizedStackFrame
Definition: StackFrameRecognizer.h:30
lldb_private::SymbolContext::module_sp
lldb::ModuleSP module_sp
The Module for a given query.
Definition: SymbolContext.h:318
lldb_private::StackFrameRecognizerManager::RemoveRecognizerWithID
bool RemoveRecognizerWithID(uint32_t recognizer_id)
Definition: StackFrameRecognizer.cpp:89
lldb_private::ScriptInterpreter
Definition: ScriptInterpreter.h:69
ScriptedRecognizedStackFrame::ScriptedRecognizedStackFrame
ScriptedRecognizedStackFrame(ValueObjectListSP args)
Definition: StackFrameRecognizer.cpp:21
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
ScriptInterpreter.h
lldb_private::StackFrameRecognizerManager::RegisteredEntry
Definition: StackFrameRecognizer.h:126
lldb
Definition: SBAddress.h:15
StackFrameRecognizer.h
lldb_private::Symbol::GetAddress
Address GetAddress() const
Definition: Symbol.h:73