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"
15
16using namespace lldb;
17using namespace lldb_private;
18
21
22public:
24 : m_hidden(hidden) {
25 m_arguments = std::move(args);
26 }
27 bool ShouldHide() override { return m_hidden; }
28};
29
31 ScriptInterpreter *interpreter, const char *pclass)
32 : m_interpreter(interpreter), m_python_class(pclass) {
35}
36
41
44 auto args_synthesized = ValueObjectListSP(new ValueObjectList());
45 if (args) {
46 for (const auto &o : args->GetObjects())
47 args_synthesized->Append(ValueObjectRecognizerSynthesizedValue::Create(
49 }
50
51 bool hidden = m_interpreter->ShouldHide(m_python_object_sp, frame);
52
54 new ScriptedRecognizedStackFrame(args_synthesized, hidden));
55}
56
58 uint32_t n = m_generation;
59 n = (n + 1) & ((1 << 16) - 1);
60 m_generation = n;
61}
62
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});
70}
71
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});
79}
80
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
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);
116 return true;
117}
118
121 m_recognizers.clear();
122}
123
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
167 auto recognizer = GetRecognizerForFrame(frame);
168 if (!recognizer)
169 return RecognizedStackFrameSP();
170 return recognizer->RecognizeFrame(frame);
171}
ScriptedRecognizedStackFrame(ValueObjectListSP args, bool hidden)
bool ShouldHide() override
Controls whether this frame should be filtered out when displaying backtraces, for example.
A section + offset based address class.
Definition: Address.h:62
A uniqued constant string class.
Definition: ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:197
This class provides extra information about a stack frame that was provided by a specific stack frame...
virtual lldb::ValueObjectListSP GetRecognizedArguments(const StructuredData::ObjectSP &implementor, lldb::StackFrameSP frame_sp)
virtual StructuredData::GenericSP CreateFrameRecognizer(const char *class_name)
virtual bool ShouldHide(const StructuredData::ObjectSP &implementor, lldb::StackFrameSP frame_sp)
ScriptedStackFrameRecognizer(lldb_private::ScriptInterpreter *interpreter, const char *pclass)
lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame) override
lldb_private::StructuredData::ObjectSP m_python_object_sp
lldb_private::ScriptInterpreter * m_interpreter
void BumpGeneration()
Increase the generation counter.
lldb::StackFrameRecognizerSP GetRecognizerForFrame(lldb::StackFrameSP frame)
lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame)
bool RemoveRecognizerWithID(uint32_t recognizer_id)
void ForEach(std::function< void(uint32_t recognizer_id, std::string recognizer_name, std::string module, llvm::ArrayRef< ConstString > symbols, bool regexp)> const &callback)
std::deque< RegisteredEntry > m_recognizers
void AddRecognizer(lldb::StackFrameRecognizerSP recognizer, ConstString module, llvm::ArrayRef< ConstString > symbols, bool first_instruction_only=true)
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:34
ConstString GetFunctionName(Mangled::NamePreference preference=Mangled::ePreferDemangled) const
Find a name of the innermost function for the symbol context.
lldb::ModuleSP module_sp
The Module for a given query.
Symbol * symbol
The Symbol for a given query.
Address GetAddress() const
Definition: Symbol.h:88
A collection of ValueObject values that.
static lldb::ValueObjectSP Create(ValueObject &parent, lldb::ValueType type)
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:420
std::shared_ptr< lldb_private::RecognizedStackFrame > RecognizedStackFrameSP
Definition: lldb-forward.h:401
std::shared_ptr< lldb_private::RegularExpression > RegularExpressionSP
Definition: lldb-forward.h:395
std::shared_ptr< lldb_private::ValueObjectList > ValueObjectListSP
Definition: lldb-forward.h:484
std::shared_ptr< lldb_private::StackFrameRecognizer > StackFrameRecognizerSP
Definition: lldb-forward.h:424
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:371
@ eValueTypeVariableArgument
function argument variables