blob: baffc890bb065bf2a45f7a3905aba5bb3897738e [file] [log] [blame]
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001//===-- StackFrameRecognizer.h ----------------------------------*- C++ -*-===//
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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02009#ifndef LLDB_TARGET_STACKFRAMERECOGNIZER_H
10#define LLDB_TARGET_STACKFRAMERECOGNIZER_H
Andrew Walbran3d2c1972020-04-07 12:24:26 +010011
12#include "lldb/Core/ValueObject.h"
13#include "lldb/Core/ValueObjectList.h"
14#include "lldb/Symbol/VariableList.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020015#include "lldb/Target/StopInfo.h"
Andrew Walbran3d2c1972020-04-07 12:24:26 +010016#include "lldb/Utility/StructuredData.h"
17#include "lldb/lldb-private-forward.h"
18#include "lldb/lldb-public.h"
19
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020020#include <vector>
21
Andrew Walbran3d2c1972020-04-07 12:24:26 +010022namespace lldb_private {
23
24/// \class RecognizedStackFrame
25///
26/// This class provides extra information about a stack frame that was
27/// provided by a specific stack frame recognizer. Right now, this class only
28/// holds recognized arguments (via GetRecognizedArguments).
29
30class RecognizedStackFrame
31 : public std::enable_shared_from_this<RecognizedStackFrame> {
32public:
33 virtual lldb::ValueObjectListSP GetRecognizedArguments() {
34 return m_arguments;
35 }
36 virtual lldb::ValueObjectSP GetExceptionObject() {
37 return lldb::ValueObjectSP();
38 }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020039 virtual lldb::StackFrameSP GetMostRelevantFrame() { return nullptr; };
Andrew Walbran3d2c1972020-04-07 12:24:26 +010040 virtual ~RecognizedStackFrame(){};
41
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020042 std::string GetStopDescription() { return m_stop_desc; }
43
Andrew Walbran3d2c1972020-04-07 12:24:26 +010044protected:
45 lldb::ValueObjectListSP m_arguments;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020046 std::string m_stop_desc;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010047};
48
49/// \class StackFrameRecognizer
50///
51/// A base class for frame recognizers. Subclasses (actual frame recognizers)
52/// should implement RecognizeFrame to provide a RecognizedStackFrame for a
53/// given stack frame.
54
55class StackFrameRecognizer
56 : public std::enable_shared_from_this<StackFrameRecognizer> {
57public:
58 virtual lldb::RecognizedStackFrameSP RecognizeFrame(
59 lldb::StackFrameSP frame) {
60 return lldb::RecognizedStackFrameSP();
61 };
62 virtual std::string GetName() {
63 return "";
64 }
65
66 virtual ~StackFrameRecognizer(){};
67};
68
69/// \class ScriptedStackFrameRecognizer
70///
71/// Python implementation for frame recognizers. An instance of this class
72/// tracks a particular Python classobject, which will be asked to recognize
73/// stack frames.
74
75class ScriptedStackFrameRecognizer : public StackFrameRecognizer {
76 lldb_private::ScriptInterpreter *m_interpreter;
77 lldb_private::StructuredData::ObjectSP m_python_object_sp;
78 std::string m_python_class;
79
80public:
81 ScriptedStackFrameRecognizer(lldb_private::ScriptInterpreter *interpreter,
82 const char *pclass);
83 ~ScriptedStackFrameRecognizer() override {}
84
85 std::string GetName() override {
86 return GetPythonClassName();
87 }
88
89 const char *GetPythonClassName() { return m_python_class.c_str(); }
90
91 lldb::RecognizedStackFrameSP RecognizeFrame(
92 lldb::StackFrameSP frame) override;
93
94private:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020095 ScriptedStackFrameRecognizer(const ScriptedStackFrameRecognizer &) = delete;
96 const ScriptedStackFrameRecognizer &
97 operator=(const ScriptedStackFrameRecognizer &) = delete;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010098};
99
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200100/// Class that provides a registry of known stack frame recognizers.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100101class StackFrameRecognizerManager {
102public:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200103 void AddRecognizer(lldb::StackFrameRecognizerSP recognizer,
104 ConstString module, llvm::ArrayRef<ConstString> symbols,
105 bool first_instruction_only = true);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100106
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200107 void AddRecognizer(lldb::StackFrameRecognizerSP recognizer,
108 lldb::RegularExpressionSP module,
109 lldb::RegularExpressionSP symbol,
110 bool first_instruction_only = true);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100111
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200112 void ForEach(std::function<
113 void(uint32_t recognizer_id, std::string recognizer_name,
114 std::string module, llvm::ArrayRef<ConstString> symbols,
115 bool regexp)> const &callback);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100116
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200117 bool RemoveRecognizerWithID(uint32_t recognizer_id);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100118
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200119 void RemoveAllRecognizers();
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100120
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200121 lldb::StackFrameRecognizerSP GetRecognizerForFrame(lldb::StackFrameSP frame);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100122
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200123 lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame);
124
125private:
126 struct RegisteredEntry {
127 uint32_t recognizer_id;
128 lldb::StackFrameRecognizerSP recognizer;
129 bool is_regexp;
130 ConstString module;
131 lldb::RegularExpressionSP module_regexp;
132 std::vector<ConstString> symbols;
133 lldb::RegularExpressionSP symbol_regexp;
134 bool first_instruction_only;
135 };
136
137 std::deque<RegisteredEntry> m_recognizers;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100138};
139
140/// \class ValueObjectRecognizerSynthesizedValue
141///
142/// ValueObject subclass that presents the passed ValueObject as a recognized
143/// value with the specified ValueType. Frame recognizers should return
144/// instances of this class as the returned objects in GetRecognizedArguments().
145
146class ValueObjectRecognizerSynthesizedValue : public ValueObject {
147 public:
148 static lldb::ValueObjectSP Create(ValueObject &parent, lldb::ValueType type) {
149 return (new ValueObjectRecognizerSynthesizedValue(parent, type))->GetSP();
150 }
151 ValueObjectRecognizerSynthesizedValue(ValueObject &parent,
152 lldb::ValueType type)
153 : ValueObject(parent), m_type(type) {
154 SetName(parent.GetName());
155 }
156
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200157 llvm::Optional<uint64_t> GetByteSize() override {
158 return m_parent->GetByteSize();
159 }
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100160 lldb::ValueType GetValueType() const override { return m_type; }
161 bool UpdateValue() override {
162 if (!m_parent->UpdateValueIfNeeded()) return false;
163 m_value = m_parent->GetValue();
164 return true;
165 }
166 size_t CalculateNumChildren(uint32_t max = UINT32_MAX) override {
167 return m_parent->GetNumChildren(max);
168 }
169 CompilerType GetCompilerTypeImpl() override {
170 return m_parent->GetCompilerType();
171 }
172 bool IsSynthetic() override { return true; }
173
174 private:
175 lldb::ValueType m_type;
176};
177
178} // namespace lldb_private
179
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200180#endif // LLDB_TARGET_STACKFRAMERECOGNIZER_H