LLDB mainline
SBError.cpp
Go to the documentation of this file.
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"
15#include "lldb/Utility/Status.h"
17
18#include <cstdarg>
19
20using namespace lldb;
21using namespace lldb_private;
22
24
26 LLDB_INSTRUMENT_VA(this, rhs);
27
28 if (rhs.m_opaque_up)
29 m_opaque_up = std::make_unique<Status>(rhs.m_opaque_up->Clone());
30}
31
32SBError::SBError(const char *message) {
33 LLDB_INSTRUMENT_VA(this, message);
34
35 SetErrorString(message);
36}
37
39 : m_opaque_up(new Status(std::move(status))) {
40 LLDB_INSTRUMENT_VA(this, status);
41}
42
43SBError::~SBError() = default;
44
46 LLDB_INSTRUMENT_VA(this, rhs);
47
48 if (this != &rhs)
49 if (rhs.m_opaque_up)
50 m_opaque_up = std::make_unique<Status>(rhs.m_opaque_up->Clone());
51
52 return *this;
53}
54
55const char *SBError::GetCString() const {
57
58 if (m_opaque_up)
59 return m_opaque_up->AsCString();
60 return nullptr;
61}
62
65
66 if (m_opaque_up)
67 m_opaque_up->Clear();
68}
69
70bool SBError::Fail() const {
72
73 bool ret_value = false;
74 if (m_opaque_up)
75 ret_value = m_opaque_up->Fail();
76
77
78 return ret_value;
79}
80
81bool SBError::Success() const {
83
84 bool ret_value = true;
85 if (m_opaque_up)
86 ret_value = m_opaque_up->Success();
87
88 return ret_value;
89}
90
91uint32_t SBError::GetError() const {
93
94 uint32_t err = 0;
95 if (m_opaque_up)
96 err = m_opaque_up->GetError();
97
98
99 return err;
100}
101
103 LLDB_INSTRUMENT_VA(this);
104
105 SBStructuredData sb_data;
106 if (!m_opaque_up)
107 return sb_data;
108
109 StructuredData::ObjectSP data(m_opaque_up->GetAsStructuredData());
110 sb_data.m_impl_up->SetObjectSP(data);
111 return sb_data;
112}
113
115 LLDB_INSTRUMENT_VA(this);
116
117 ErrorType err_type = eErrorTypeInvalid;
118 if (m_opaque_up)
119 err_type = m_opaque_up->GetType();
120
121 return err_type;
122}
123
124void SBError::SetError(uint32_t err, ErrorType type) {
125 LLDB_INSTRUMENT_VA(this, err, type);
126
128 *m_opaque_up = Status(err, type);
129}
130
131void SBError::SetError(Status &&lldb_error) {
133 *m_opaque_up = std::move(lldb_error);
134}
135
137 LLDB_INSTRUMENT_VA(this);
138
141}
142
144 LLDB_INSTRUMENT_VA(this);
145
147 *m_opaque_up = Status(std::string("generic error"));
148}
149
150void SBError::SetErrorString(const char *err_str) {
151 LLDB_INSTRUMENT_VA(this, err_str);
152
155}
156
157int SBError::SetErrorStringWithFormat(const char *format, ...) {
159 std::string string;
160 va_list args;
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));
167 }
168 va_end(args);
169 return string.size();
170}
171
172bool SBError::IsValid() const {
173 LLDB_INSTRUMENT_VA(this);
174 return this->operator bool();
175}
176SBError::operator bool() const {
177 LLDB_INSTRUMENT_VA(this);
178
179 return m_opaque_up != nullptr;
180}
181
183 if (m_opaque_up == nullptr)
184 m_opaque_up = std::make_unique<Status>();
185}
186
188
190
193 return *m_opaque_up;
194}
195
197 // Be sure to call "IsValid()" before calling this function or it will crash
198 return *m_opaque_up;
199}
200
202 LLDB_INSTRUMENT_VA(this, description);
203
204 if (m_opaque_up) {
205 if (m_opaque_up->Success())
206 description.Printf("success");
207 else {
208 const char *err_string = GetCString();
209 description.Printf("error: %s",
210 (err_string != nullptr ? err_string : ""));
211 }
212 } else
213 description.Printf("error: <NULL>");
214
215 return true;
216}
#define LLDB_INSTRUMENT_VA(...)
uint32_t GetError() const
Get the error code.
Definition: SBError.cpp:91
bool GetDescription(lldb::SBStream &description)
Definition: SBError.cpp:201
bool Success() const
Definition: SBError.cpp:81
std::unique_ptr< lldb_private::Status > m_opaque_up
Definition: SBError.h:118
void SetErrorString(const char *err_str)
Definition: SBError.cpp:150
void SetError(uint32_t err, lldb::ErrorType type)
Definition: SBError.cpp:124
bool Fail() const
Definition: SBError.cpp:70
const SBError & operator=(const lldb::SBError &rhs)
Definition: SBError.cpp:45
lldb_private::Status & ref()
Definition: SBError.cpp:191
lldb::ErrorType GetType() const
Definition: SBError.cpp:114
const lldb_private::Status & operator*() const
Definition: SBError.cpp:196
void SetErrorToGenericError()
Definition: SBError.cpp:143
const char * GetCString() const
Get the error string as a NULL terminated UTF8 c-string.
Definition: SBError.cpp:55
lldb_private::Status * operator->()
Definition: SBError.cpp:187
bool IsValid() const
Definition: SBError.cpp:172
SBStructuredData GetErrorData() const
Get the error in machine-readable form.
Definition: SBError.cpp:102
void SetErrorToErrno()
Definition: SBError.cpp:136
void CreateIfNeeded()
Definition: SBError.cpp:182
lldb_private::Status * get()
Definition: SBError.cpp:189
void Clear()
Definition: SBError.cpp:63
StructuredDataImplUP m_impl_up
An error handling class.
Definition: Status.h:118
static Status FromErrno()
Set the current error to errno.
Definition: Status.cpp:300
static Status FromErrorString(const char *str)
Definition: Status.h:141
std::shared_ptr< Object > ObjectSP
A class that represents a running process on the host machine.
bool VASprintf(llvm::SmallVectorImpl< char > &buf, const char *fmt, va_list args)
Definition: VASprintf.cpp:19
Definition: SBAddress.h:15
@ eErrorTypeInvalid