1 //===-- SBError.cpp -------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBError.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Utility/Instrumentation.h"
13 #include "lldb/Utility/Status.h"
14 #include "lldb/Utility/VASPrintf.h"
19 using namespace lldb_private
;
21 SBError::SBError() { LLDB_INSTRUMENT_VA(this); }
23 SBError::SBError(const SBError
&rhs
) {
24 LLDB_INSTRUMENT_VA(this, rhs
);
27 m_opaque_up
= std::make_unique
<Status
>(rhs
.m_opaque_up
->Clone());
30 SBError::SBError(const char *message
) {
31 LLDB_INSTRUMENT_VA(this, message
);
33 SetErrorString(message
);
36 SBError::SBError(lldb_private::Status
&&status
)
37 : m_opaque_up(new Status(std::move(status
))) {
38 LLDB_INSTRUMENT_VA(this, status
);
41 SBError::~SBError() = default;
43 const SBError
&SBError::operator=(const SBError
&rhs
) {
44 LLDB_INSTRUMENT_VA(this, rhs
);
48 m_opaque_up
= std::make_unique
<Status
>(rhs
.m_opaque_up
->Clone());
53 const char *SBError::GetCString() const {
54 LLDB_INSTRUMENT_VA(this);
57 return m_opaque_up
->AsCString();
61 void SBError::Clear() {
62 LLDB_INSTRUMENT_VA(this);
68 bool SBError::Fail() const {
69 LLDB_INSTRUMENT_VA(this);
71 bool ret_value
= false;
73 ret_value
= m_opaque_up
->Fail();
79 bool SBError::Success() const {
80 LLDB_INSTRUMENT_VA(this);
82 bool ret_value
= true;
84 ret_value
= m_opaque_up
->Success();
89 uint32_t SBError::GetError() const {
90 LLDB_INSTRUMENT_VA(this);
94 err
= m_opaque_up
->GetError();
100 ErrorType
SBError::GetType() const {
101 LLDB_INSTRUMENT_VA(this);
103 ErrorType err_type
= eErrorTypeInvalid
;
105 err_type
= m_opaque_up
->GetType();
110 void SBError::SetError(uint32_t err
, ErrorType type
) {
111 LLDB_INSTRUMENT_VA(this, err
, type
);
114 *m_opaque_up
= Status(err
, type
);
117 void SBError::SetError(Status
&&lldb_error
) {
119 *m_opaque_up
= std::move(lldb_error
);
122 void SBError::SetErrorToErrno() {
123 LLDB_INSTRUMENT_VA(this);
126 *m_opaque_up
= Status::FromErrno();
129 void SBError::SetErrorToGenericError() {
130 LLDB_INSTRUMENT_VA(this);
133 *m_opaque_up
= Status(std::string("generic error"));
136 void SBError::SetErrorString(const char *err_str
) {
137 LLDB_INSTRUMENT_VA(this, err_str
);
140 *m_opaque_up
= Status::FromErrorString(err_str
);
143 int SBError::SetErrorStringWithFormat(const char *format
, ...) {
147 va_start(args
, format
);
148 if (format
!= nullptr && format
[0]) {
149 llvm::SmallString
<1024> buf
;
150 VASprintf(buf
, format
, args
);
151 string
= std::string(buf
.str());
152 *m_opaque_up
= Status(std::move(string
));
155 return string
.size();
158 bool SBError::IsValid() const {
159 LLDB_INSTRUMENT_VA(this);
160 return this->operator bool();
162 SBError::operator bool() const {
163 LLDB_INSTRUMENT_VA(this);
165 return m_opaque_up
!= nullptr;
168 void SBError::CreateIfNeeded() {
169 if (m_opaque_up
== nullptr)
170 m_opaque_up
= std::make_unique
<Status
>();
173 lldb_private::Status
*SBError::operator->() { return m_opaque_up
.get(); }
175 lldb_private::Status
*SBError::get() { return m_opaque_up
.get(); }
177 lldb_private::Status
&SBError::ref() {
182 const lldb_private::Status
&SBError::operator*() const {
183 // Be sure to call "IsValid()" before calling this function or it will crash
187 bool SBError::GetDescription(SBStream
&description
) {
188 LLDB_INSTRUMENT_VA(this, description
);
191 if (m_opaque_up
->Success())
192 description
.Printf("success");
194 const char *err_string
= GetCString();
195 description
.Printf("error: %s",
196 (err_string
!= nullptr ? err_string
: ""));
199 description
.Printf("error: <NULL>");