clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name StackFrameRecognizer.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/liblldbTarget/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/liblldbTarget/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/liblldbTarget/../include -I /usr/src/gnu/usr.bin/clang/liblldbTarget/obj -I /usr/src/gnu/usr.bin/clang/liblldbTarget/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -I /usr/src/gnu/usr.bin/clang/liblldbTarget/../../../llvm/lldb/include -I /usr/src/gnu/usr.bin/clang/liblldbTarget/../../../llvm/lldb/source -I /usr/src/gnu/usr.bin/clang/liblldbTarget/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/liblldbTarget/obj/../include/lldb/Target -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/liblldbTarget/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/liblldbTarget/../../../llvm/lldb/source/Target/StackFrameRecognizer.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | #include "lldb/Target/StackFrameRecognizer.h" |
10 | #include "lldb/Core/Module.h" |
11 | #include "lldb/Interpreter/ScriptInterpreter.h" |
12 | #include "lldb/Symbol/Symbol.h" |
13 | #include "lldb/Target/StackFrame.h" |
14 | #include "lldb/Utility/RegularExpression.h" |
15 | |
16 | using namespace lldb; |
17 | using namespace lldb_private; |
18 | |
19 | class ScriptedRecognizedStackFrame : public RecognizedStackFrame { |
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) { |
29 | m_python_object_sp = |
30 | m_interpreter->CreateFrameRecognizer(m_python_class.c_str()); |
31 | } |
32 | |
33 | RecognizedStackFrameSP |
34 | ScriptedStackFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame) { |
35 | if (!m_python_object_sp || !m_interpreter) |
| 1 | Assuming field 'm_interpreter' is non-null | |
|
| |
36 | return RecognizedStackFrameSP(); |
37 | |
38 | ValueObjectListSP args = |
39 | m_interpreter->GetRecognizedArguments(m_python_object_sp, frame); |
40 | auto args_synthesized = ValueObjectListSP(new ValueObjectList()); |
41 | for (const auto &o : args->GetObjects()) { |
42 | args_synthesized->Append(ValueObjectRecognizerSynthesizedValue::Create( |
| 3 | | Calling 'ValueObjectRecognizerSynthesizedValue::Create' | |
|
43 | *o, eValueTypeVariableArgument)); |
44 | } |
45 | |
46 | return RecognizedStackFrameSP( |
47 | new ScriptedRecognizedStackFrame(args_synthesized)); |
48 | } |
49 | |
50 | void StackFrameRecognizerManager::AddRecognizer( |
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 | |
58 | void StackFrameRecognizerManager::AddRecognizer( |
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 | |
66 | void StackFrameRecognizerManager::ForEach( |
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 | |
89 | bool StackFrameRecognizerManager::RemoveRecognizerWithID( |
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 | |
103 | void StackFrameRecognizerManager::RemoveAllRecognizers() { |
104 | m_recognizers.clear(); |
105 | } |
106 | |
107 | StackFrameRecognizerSP |
108 | StackFrameRecognizerManager::GetRecognizerForFrame(StackFrameSP frame) { |
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 |
149 | StackFrameRecognizerManager::RecognizeFrame(StackFrameSP frame) { |
150 | auto recognizer = GetRecognizerForFrame(frame); |
151 | if (!recognizer) |
152 | return RecognizedStackFrameSP(); |
153 | return recognizer->RecognizeFrame(frame); |
154 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | #ifndef LLDB_TARGET_STACKFRAMERECOGNIZER_H |
10 | #define LLDB_TARGET_STACKFRAMERECOGNIZER_H |
11 | |
12 | #include "lldb/Core/ValueObject.h" |
13 | #include "lldb/Core/ValueObjectList.h" |
14 | #include "lldb/Symbol/VariableList.h" |
15 | #include "lldb/Target/StopInfo.h" |
16 | #include "lldb/Utility/StructuredData.h" |
17 | #include "lldb/lldb-private-forward.h" |
18 | #include "lldb/lldb-public.h" |
19 | |
20 | #include <vector> |
21 | |
22 | namespace lldb_private { |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | class RecognizedStackFrame |
31 | : public std::enable_shared_from_this<RecognizedStackFrame> { |
32 | public: |
33 | virtual lldb::ValueObjectListSP GetRecognizedArguments() { |
34 | return m_arguments; |
35 | } |
36 | virtual lldb::ValueObjectSP GetExceptionObject() { |
37 | return lldb::ValueObjectSP(); |
38 | } |
39 | virtual lldb::StackFrameSP GetMostRelevantFrame() { return nullptr; }; |
40 | virtual ~RecognizedStackFrame() = default; |
41 | |
42 | std::string GetStopDescription() { return m_stop_desc; } |
43 | |
44 | protected: |
45 | lldb::ValueObjectListSP m_arguments; |
46 | std::string m_stop_desc; |
47 | }; |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | class StackFrameRecognizer |
56 | : public std::enable_shared_from_this<StackFrameRecognizer> { |
57 | public: |
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() = default; |
67 | }; |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | class 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 | |
80 | public: |
81 | ScriptedStackFrameRecognizer(lldb_private::ScriptInterpreter *interpreter, |
82 | const char *pclass); |
83 | ~ScriptedStackFrameRecognizer() override = default; |
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 | |
94 | private: |
95 | ScriptedStackFrameRecognizer(const ScriptedStackFrameRecognizer &) = delete; |
96 | const ScriptedStackFrameRecognizer & |
97 | operator=(const ScriptedStackFrameRecognizer &) = delete; |
98 | }; |
99 | |
100 | |
101 | class StackFrameRecognizerManager { |
102 | public: |
103 | void AddRecognizer(lldb::StackFrameRecognizerSP recognizer, |
104 | ConstString module, llvm::ArrayRef<ConstString> symbols, |
105 | bool first_instruction_only = true); |
106 | |
107 | void AddRecognizer(lldb::StackFrameRecognizerSP recognizer, |
108 | lldb::RegularExpressionSP module, |
109 | lldb::RegularExpressionSP symbol, |
110 | bool first_instruction_only = true); |
111 | |
112 | 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); |
116 | |
117 | bool RemoveRecognizerWithID(uint32_t recognizer_id); |
118 | |
119 | void RemoveAllRecognizers(); |
120 | |
121 | lldb::StackFrameRecognizerSP GetRecognizerForFrame(lldb::StackFrameSP frame); |
122 | |
123 | lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame); |
124 | |
125 | private: |
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; |
138 | }; |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | class 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 | |
157 | llvm::Optional<uint64_t> GetByteSize() override { |
158 | return m_parent->GetByteSize(); |
159 | } |
160 | 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 | } |
179 | |
180 | #endif // LLDB_TARGET_STACKFRAMERECOGNIZER_H |