1 //===-- SBError.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
9 #include "lldb/API/SBError.h"
10 #include "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Utility/Status.h"
14
15 #include <stdarg.h>
16
17 using namespace lldb;
18 using namespace lldb_private;
19
SBError()20 SBError::SBError() : m_opaque_up() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBError); }
21
SBError(const SBError & rhs)22 SBError::SBError(const SBError &rhs) : m_opaque_up() {
23 LLDB_RECORD_CONSTRUCTOR(SBError, (const lldb::SBError &), rhs);
24
25 m_opaque_up = clone(rhs.m_opaque_up);
26 }
27
28 SBError::~SBError() = default;
29
operator =(const SBError & rhs)30 const SBError &SBError::operator=(const SBError &rhs) {
31 LLDB_RECORD_METHOD(const lldb::SBError &,
32 SBError, operator=,(const lldb::SBError &), rhs);
33
34 if (this != &rhs)
35 m_opaque_up = clone(rhs.m_opaque_up);
36 return LLDB_RECORD_RESULT(*this);
37 }
38
GetCString() const39 const char *SBError::GetCString() const {
40 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBError, GetCString);
41
42 if (m_opaque_up)
43 return m_opaque_up->AsCString();
44 return nullptr;
45 }
46
Clear()47 void SBError::Clear() {
48 LLDB_RECORD_METHOD_NO_ARGS(void, SBError, Clear);
49
50 if (m_opaque_up)
51 m_opaque_up->Clear();
52 }
53
Fail() const54 bool SBError::Fail() const {
55 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBError, Fail);
56
57 bool ret_value = false;
58 if (m_opaque_up)
59 ret_value = m_opaque_up->Fail();
60
61
62 return ret_value;
63 }
64
Success() const65 bool SBError::Success() const {
66 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBError, Success);
67
68 bool ret_value = true;
69 if (m_opaque_up)
70 ret_value = m_opaque_up->Success();
71
72 return ret_value;
73 }
74
GetError() const75 uint32_t SBError::GetError() const {
76 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBError, GetError);
77
78
79 uint32_t err = 0;
80 if (m_opaque_up)
81 err = m_opaque_up->GetError();
82
83
84 return err;
85 }
86
GetType() const87 ErrorType SBError::GetType() const {
88 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::ErrorType, SBError, GetType);
89
90 ErrorType err_type = eErrorTypeInvalid;
91 if (m_opaque_up)
92 err_type = m_opaque_up->GetType();
93
94 return err_type;
95 }
96
SetError(uint32_t err,ErrorType type)97 void SBError::SetError(uint32_t err, ErrorType type) {
98 LLDB_RECORD_METHOD(void, SBError, SetError, (uint32_t, lldb::ErrorType), err,
99 type);
100
101 CreateIfNeeded();
102 m_opaque_up->SetError(err, type);
103 }
104
SetError(const Status & lldb_error)105 void SBError::SetError(const Status &lldb_error) {
106 CreateIfNeeded();
107 *m_opaque_up = lldb_error;
108 }
109
SetErrorToErrno()110 void SBError::SetErrorToErrno() {
111 LLDB_RECORD_METHOD_NO_ARGS(void, SBError, SetErrorToErrno);
112
113 CreateIfNeeded();
114 m_opaque_up->SetErrorToErrno();
115 }
116
SetErrorToGenericError()117 void SBError::SetErrorToGenericError() {
118 LLDB_RECORD_METHOD_NO_ARGS(void, SBError, SetErrorToGenericError);
119
120 CreateIfNeeded();
121 m_opaque_up->SetErrorToGenericError();
122 }
123
SetErrorString(const char * err_str)124 void SBError::SetErrorString(const char *err_str) {
125 LLDB_RECORD_METHOD(void, SBError, SetErrorString, (const char *), err_str);
126
127 CreateIfNeeded();
128 m_opaque_up->SetErrorString(err_str);
129 }
130
SetErrorStringWithFormat(const char * format,...)131 int SBError::SetErrorStringWithFormat(const char *format, ...) {
132 CreateIfNeeded();
133 va_list args;
134 va_start(args, format);
135 int num_chars = m_opaque_up->SetErrorStringWithVarArg(format, args);
136 va_end(args);
137 return num_chars;
138 }
139
IsValid() const140 bool SBError::IsValid() const {
141 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBError, IsValid);
142 return this->operator bool();
143 }
operator bool() const144 SBError::operator bool() const {
145 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBError, operator bool);
146
147 return m_opaque_up != nullptr;
148 }
149
CreateIfNeeded()150 void SBError::CreateIfNeeded() {
151 if (m_opaque_up == nullptr)
152 m_opaque_up = std::make_unique<Status>();
153 }
154
operator ->()155 lldb_private::Status *SBError::operator->() { return m_opaque_up.get(); }
156
get()157 lldb_private::Status *SBError::get() { return m_opaque_up.get(); }
158
ref()159 lldb_private::Status &SBError::ref() {
160 CreateIfNeeded();
161 return *m_opaque_up;
162 }
163
operator *() const164 const lldb_private::Status &SBError::operator*() const {
165 // Be sure to call "IsValid()" before calling this function or it will crash
166 return *m_opaque_up;
167 }
168
GetDescription(SBStream & description)169 bool SBError::GetDescription(SBStream &description) {
170 LLDB_RECORD_METHOD(bool, SBError, GetDescription, (lldb::SBStream &),
171 description);
172
173 if (m_opaque_up) {
174 if (m_opaque_up->Success())
175 description.Printf("success");
176 else {
177 const char *err_string = GetCString();
178 description.Printf("error: %s",
179 (err_string != nullptr ? err_string : ""));
180 }
181 } else
182 description.Printf("error: <NULL>");
183
184 return true;
185 }
186
187 namespace lldb_private {
188 namespace repro {
189
190 template <>
RegisterMethods(Registry & R)191 void RegisterMethods<SBError>(Registry &R) {
192 LLDB_REGISTER_CONSTRUCTOR(SBError, ());
193 LLDB_REGISTER_CONSTRUCTOR(SBError, (const lldb::SBError &));
194 LLDB_REGISTER_METHOD(const lldb::SBError &,
195 SBError, operator=,(const lldb::SBError &));
196 LLDB_REGISTER_METHOD_CONST(const char *, SBError, GetCString, ());
197 LLDB_REGISTER_METHOD(void, SBError, Clear, ());
198 LLDB_REGISTER_METHOD_CONST(bool, SBError, Fail, ());
199 LLDB_REGISTER_METHOD_CONST(bool, SBError, Success, ());
200 LLDB_REGISTER_METHOD_CONST(uint32_t, SBError, GetError, ());
201 LLDB_REGISTER_METHOD_CONST(lldb::ErrorType, SBError, GetType, ());
202 LLDB_REGISTER_METHOD(void, SBError, SetError, (uint32_t, lldb::ErrorType));
203 LLDB_REGISTER_METHOD(void, SBError, SetErrorToErrno, ());
204 LLDB_REGISTER_METHOD(void, SBError, SetErrorToGenericError, ());
205 LLDB_REGISTER_METHOD(void, SBError, SetErrorString, (const char *));
206 LLDB_REGISTER_METHOD_CONST(bool, SBError, IsValid, ());
207 LLDB_REGISTER_METHOD_CONST(bool, SBError, operator bool, ());
208 LLDB_REGISTER_METHOD(bool, SBError, GetDescription, (lldb::SBStream &));
209 }
210
211 }
212 }
213