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/API/SBStructuredData.h"
13 #include "lldb/Core/StructuredDataImpl.h"
14 #include "lldb/Utility/Instrumentation.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/Utility/VASPrintf.h"
21 using namespace lldb_private
;
23 SBError::SBError() { LLDB_INSTRUMENT_VA(this); }
25 SBError::SBError(const SBError
&rhs
) {
26 LLDB_INSTRUMENT_VA(this, rhs
);
29 m_opaque_up
= std::make_unique
<Status
>(rhs
.m_opaque_up
->Clone());
32 SBError::SBError(const char *message
) {
33 LLDB_INSTRUMENT_VA(this, message
);
35 SetErrorString(message
);
38 SBError::SBError(lldb_private::Status
&&status
)
39 : m_opaque_up(new Status(std::move(status
))) {
40 LLDB_INSTRUMENT_VA(this, status
);
43 SBError::~SBError() = default;
45 const SBError
&SBError::operator=(const SBError
&rhs
) {
46 LLDB_INSTRUMENT_VA(this, rhs
);
50 m_opaque_up
= std::make_unique
<Status
>(rhs
.m_opaque_up
->Clone());
55 const char *SBError::GetCString() const {
56 LLDB_INSTRUMENT_VA(this);
59 return m_opaque_up
->AsCString();
63 void SBError::Clear() {
64 LLDB_INSTRUMENT_VA(this);
70 bool SBError::Fail() const {
71 LLDB_INSTRUMENT_VA(this);
73 bool ret_value
= false;
75 ret_value
= m_opaque_up
->Fail();
81 bool SBError::Success() const {
82 LLDB_INSTRUMENT_VA(this);
84 bool ret_value
= true;
86 ret_value
= m_opaque_up
->Success();
91 uint32_t SBError::GetError() const {
92 LLDB_INSTRUMENT_VA(this);
96 err
= m_opaque_up
->GetError();
102 SBStructuredData
SBError::GetErrorData() const {
103 LLDB_INSTRUMENT_VA(this);
105 SBStructuredData sb_data
;
109 StructuredData::ObjectSP
data(m_opaque_up
->GetAsStructuredData());
110 sb_data
.m_impl_up
->SetObjectSP(data
);
114 ErrorType
SBError::GetType() const {
115 LLDB_INSTRUMENT_VA(this);
117 ErrorType err_type
= eErrorTypeInvalid
;
119 err_type
= m_opaque_up
->GetType();
124 void SBError::SetError(uint32_t err
, ErrorType type
) {
125 LLDB_INSTRUMENT_VA(this, err
, type
);
128 *m_opaque_up
= Status(err
, type
);
131 void SBError::SetError(Status
&&lldb_error
) {
133 *m_opaque_up
= std::move(lldb_error
);
136 void SBError::SetErrorToErrno() {
137 LLDB_INSTRUMENT_VA(this);
140 *m_opaque_up
= Status::FromErrno();
143 void SBError::SetErrorToGenericError() {
144 LLDB_INSTRUMENT_VA(this);
147 *m_opaque_up
= Status(std::string("generic error"));
150 void SBError::SetErrorString(const char *err_str
) {
151 LLDB_INSTRUMENT_VA(this, err_str
);
154 *m_opaque_up
= Status::FromErrorString(err_str
);
157 int SBError::SetErrorStringWithFormat(const char *format
, ...) {
161 va_start(args
, format
);
162 if (format
!= nullptr && format
[0]) {
163 llvm::SmallString
<1024> buf
;
164 VASprintf(buf
, format
, args
);
165 string
= std::string(buf
.str());
166 *m_opaque_up
= Status(std::move(string
));
169 return string
.size();
172 bool SBError::IsValid() const {
173 LLDB_INSTRUMENT_VA(this);
174 return this->operator bool();
176 SBError::operator bool() const {
177 LLDB_INSTRUMENT_VA(this);
179 return m_opaque_up
!= nullptr;
182 void SBError::CreateIfNeeded() {
183 if (m_opaque_up
== nullptr)
184 m_opaque_up
= std::make_unique
<Status
>();
187 lldb_private::Status
*SBError::operator->() { return m_opaque_up
.get(); }
189 lldb_private::Status
*SBError::get() { return m_opaque_up
.get(); }
191 lldb_private::Status
&SBError::ref() {
196 const lldb_private::Status
&SBError::operator*() const {
197 // Be sure to call "IsValid()" before calling this function or it will crash
201 bool SBError::GetDescription(SBStream
&description
) {
202 LLDB_INSTRUMENT_VA(this, description
);
205 if (m_opaque_up
->Success())
206 description
.Printf("success");
208 const char *err_string
= GetCString();
209 description
.Printf("error: %s",
210 (err_string
!= nullptr ? err_string
: ""));
213 description
.Printf("error: <NULL>");