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

source code of lldb/source/API/SBError.cpp