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"
13#include "lldb/Symbol/Symbol.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
25
26public:
28 lldb::StackFrameSP most_relevant_frame,
29 lldb::ValueObjectSP exception,
30 std::string stop_desc)
31 : m_hidden(hidden), m_most_relevant_frame(std::move(most_relevant_frame)),
32 m_exception(std::move(exception)) {
33 m_arguments = std::move(args);
34 m_stop_desc = std::move(stop_desc);
35 }
36 bool ShouldHide() override { return m_hidden; }
41};
42
44 ScriptInterpreter *interpreter, const char *pclass)
45 : m_python_class(pclass) {
46 if (!interpreter)
47 return;
48
50 if (!m_interface_sp)
51 return;
52
53 ScriptedMetadata scripted_metadata(m_python_class, nullptr);
54 auto obj_or_err = m_interface_sp->CreatePluginObject(scripted_metadata);
55 if (!obj_or_err) {
56 llvm::consumeError(obj_or_err.takeError());
57 m_interface_sp.reset();
58 }
59}
60
63 if (!m_interface_sp)
65
66 ValueObjectListSP args = m_interface_sp->GetRecognizedArguments(frame);
67 auto args_synthesized = std::make_shared<ValueObjectList>();
68 if (args) {
69 for (const auto &o : args->GetObjects())
70 args_synthesized->Append(ValueObjectRecognizerSynthesizedValue::Create(
72 }
73
74 bool hidden = m_interface_sp->ShouldHide(frame);
75 lldb::StackFrameSP most_relevant =
76 m_interface_sp->SelectMostRelevantFrame(frame);
77 lldb::ValueObjectSP exception = m_interface_sp->GetException(frame);
78 std::string stop_desc = m_interface_sp->GetStopDescription(frame);
79
81 args_synthesized, hidden, std::move(most_relevant), std::move(exception),
82 std::move(stop_desc)));
83}
84
86 uint32_t n = m_generation;
87 n = (n + 1) & ((1 << 16) - 1);
88 m_generation = n;
89}
90
92 StackFrameRecognizerSP recognizer, ConstString module,
93 llvm::ArrayRef<ConstString> symbols,
94 Mangled::NamePreference symbol_mangling, bool first_instruction_only) {
95 m_recognizers.push_front({(uint32_t)m_recognizers.size(), recognizer, false,
96 module, RegularExpressionSP(), symbols,
97 RegularExpressionSP(), symbol_mangling,
98 first_instruction_only, true});
100}
101
104 RegularExpressionSP symbol, Mangled::NamePreference symbol_mangling,
105 bool first_instruction_only) {
106 m_recognizers.push_front({(uint32_t)m_recognizers.size(), recognizer, true,
107 ConstString(), module, std::vector<ConstString>(),
108 symbol, symbol_mangling, first_instruction_only,
109 true});
111}
112
114 const std::function<void(
115 uint32_t, bool, std::string, std::string, llvm::ArrayRef<ConstString>,
116 Mangled::NamePreference name_preference, bool)> &callback) {
117 for (auto entry : m_recognizers) {
118 if (entry.is_regexp) {
119 std::string module_name;
120 std::string symbol_name;
121
122 if (entry.module_regexp)
123 module_name = entry.module_regexp->GetText().str();
124 if (entry.symbol_regexp)
125 symbol_name = entry.symbol_regexp->GetText().str();
126
127 callback(entry.recognizer_id, entry.enabled, entry.recognizer->GetName(),
128 module_name, llvm::ArrayRef(ConstString(symbol_name)),
129 entry.symbol_mangling, true);
130 } else {
131 callback(entry.recognizer_id, entry.enabled, entry.recognizer->GetName(),
132 entry.module.GetCString(), entry.symbols, entry.symbol_mangling,
133 false);
134 }
135 }
136}
137
139 bool enabled) {
140 auto found =
141 llvm::find_if(m_recognizers, [recognizer_id](const RegisteredEntry &e) {
142 return e.recognizer_id == recognizer_id;
143 });
144 if (found == m_recognizers.end())
145 return false;
146 found->enabled = enabled;
148 return true;
149}
150
152 uint32_t recognizer_id) {
153 auto found =
154 llvm::find_if(m_recognizers, [recognizer_id](const RegisteredEntry &e) {
155 return e.recognizer_id == recognizer_id;
156 });
157 if (found == m_recognizers.end())
158 return false;
159 m_recognizers.erase(found);
161 return true;
162}
163
168
171 const SymbolContext &symctx = frame->GetSymbolContext(
172 eSymbolContextModule | eSymbolContextFunction | eSymbolContextSymbol);
173 ModuleSP module_sp = symctx.module_sp;
174 if (!module_sp)
175 return StackFrameRecognizerSP();
176 llvm::StringRef module_name = module_sp->GetFileSpec().GetFilename();
177 const Symbol *symbol = symctx.symbol;
178 if (!symbol)
179 return StackFrameRecognizerSP();
180 Address start_addr = symbol->GetAddress();
181 Address current_addr = frame->GetFrameCodeAddress();
182
183 for (const auto &entry : m_recognizers) {
184 if (!entry.enabled)
185 continue;
186
187 if (entry.module)
188 if (entry.module != module_name)
189 continue;
190
191 if (entry.module_regexp)
192 if (!entry.module_regexp->Execute(module_name))
193 continue;
194
195 ConstString function_name = symctx.GetFunctionName(entry.symbol_mangling);
196
197 if (!entry.symbols.empty())
198 if (!llvm::is_contained(entry.symbols, function_name))
199 continue;
200
201 if (entry.symbol_regexp)
202 if (!entry.symbol_regexp->Execute(function_name.GetStringRef()))
203 continue;
204
205 if (entry.first_instruction_only)
206 if (start_addr != current_addr)
207 continue;
208
209 return entry.recognizer;
210 }
211 return StackFrameRecognizerSP();
212}
213
216 auto recognizer = GetRecognizerForFrame(frame);
217 if (!recognizer)
218 return RecognizedStackFrameSP();
219 return recognizer->RecognizeFrame(frame);
220}
lldb::StackFrameSP GetMostRelevantFrame() override
lldb::ValueObjectSP GetExceptionObject() override
ScriptedRecognizedStackFrame(ValueObjectListSP args, bool hidden, lldb::StackFrameSP most_relevant_frame, lldb::ValueObjectSP exception, std::string stop_desc)
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.
This class provides extra information about a stack frame that was provided by a specific stack frame...
virtual lldb::ScriptedStackFrameRecognizerInterfaceSP CreateScriptedStackFrameRecognizerInterface()
ScriptedStackFrameRecognizer(lldb_private::ScriptInterpreter *interpreter, const char *pclass)
lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame) override
lldb::ScriptedStackFrameRecognizerInterfaceSP m_interface_sp
void BumpGeneration()
Increase the generation counter.
void AddRecognizer(lldb::StackFrameRecognizerSP recognizer, ConstString module, llvm::ArrayRef< ConstString > symbols, Mangled::NamePreference symbol_mangling, bool first_instruction_only=true)
Add a new recognizer that triggers on a given symbol name.
void ForEach(std::function< void(uint32_t recognizer_id, bool enabled, std::string recognizer_name, std::string module, llvm::ArrayRef< ConstString > symbols, Mangled::NamePreference name_preference, bool regexp)> const &callback)
lldb::StackFrameRecognizerSP GetRecognizerForFrame(lldb::StackFrameSP frame)
lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame)
bool RemoveRecognizerWithID(uint32_t recognizer_id)
bool SetEnabledForID(uint32_t recognizer_id, bool enabled)
Defines a symbol context baton that can be handed other debug core functions.
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:89
static lldb::ValueObjectSP Create(ValueObject &parent, lldb::ValueType type)
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::RecognizedStackFrame > RecognizedStackFrameSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::RegularExpression > RegularExpressionSP
std::shared_ptr< lldb_private::ValueObjectList > ValueObjectListSP
@ eValueTypeVariableArgument
function argument variables
std::shared_ptr< lldb_private::StackFrameRecognizer > StackFrameRecognizerSP
std::shared_ptr< lldb_private::Module > ModuleSP