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"
13#include "lldb/Utility/Status.h"
15
16#include <cstdarg>
17
18using namespace lldb;
19using namespace lldb_private;
20
22
24 LLDB_INSTRUMENT_VA(this, rhs);
25
26 if (rhs.m_opaque_up)
27 m_opaque_up = std::make_unique<Status>(rhs.m_opaque_up->Clone());
28}
29
30SBError::SBError(const char *message) {
31 LLDB_INSTRUMENT_VA(this, message);
32
33 SetErrorString(message);
34}
35
37 : m_opaque_up(new Status(std::move(status))) {
38 LLDB_INSTRUMENT_VA(this, status);
39}
40
41SBError::~SBError() = default;
42
44 LLDB_INSTRUMENT_VA(this, rhs);
45
46 if (this != &rhs)
47 if (rhs.m_opaque_up)
48 m_opaque_up = std::make_unique<Status>(rhs.m_opaque_up->Clone());
49
50 return *this;
51}
52
53const char *SBError::GetCString() const {
55
56 if (m_opaque_up)
57 return m_opaque_up->AsCString();
58 return nullptr;
59}
60
63
64 if (m_opaque_up)
65 m_opaque_up->Clear();
66}
67
68bool SBError::Fail() const {
70
71 bool ret_value = false;
72 if (m_opaque_up)
73 ret_value = m_opaque_up->Fail();
74
75
76 return ret_value;
77}
78
79bool SBError::Success() const {
81
82 bool ret_value = true;
83 if (m_opaque_up)
84 ret_value = m_opaque_up->Success();
85
86 return ret_value;
87}
88
89uint32_t SBError::GetError() const {
91
92 uint32_t err = 0;
93 if (m_opaque_up)
94 err = m_opaque_up->GetError();
95
96
97 return err;
98}
99
101 LLDB_INSTRUMENT_VA(this);
102
103 ErrorType err_type = eErrorTypeInvalid;
104 if (m_opaque_up)
105 err_type = m_opaque_up->GetType();
106
107 return err_type;
108}
109
110void SBError::SetError(uint32_t err, ErrorType type) {
111 LLDB_INSTRUMENT_VA(this, err, type);
112
114 *m_opaque_up = Status(err, type);
115}
116
117void SBError::SetError(Status &&lldb_error) {
119 *m_opaque_up = std::move(lldb_error);
120}
121
123 LLDB_INSTRUMENT_VA(this);
124
127}
128
130 LLDB_INSTRUMENT_VA(this);
131
133 *m_opaque_up = Status(std::string("generic error"));
134}
135
136void SBError::SetErrorString(const char *err_str) {
137 LLDB_INSTRUMENT_VA(this, err_str);
138
141}
142
143int SBError::SetErrorStringWithFormat(const char *format, ...) {
145 std::string string;
146 va_list args;
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));
153 }
154 va_end(args);
155 return string.size();
156}
157
158bool SBError::IsValid() const {
159 LLDB_INSTRUMENT_VA(this);
160 return this->operator bool();
161}
162SBError::operator bool() const {
163 LLDB_INSTRUMENT_VA(this);
164
165 return m_opaque_up != nullptr;
166}
167
169 if (m_opaque_up == nullptr)
170 m_opaque_up = std::make_unique<Status>();
171}
172
174
176
179 return *m_opaque_up;
180}
181
183 // Be sure to call "IsValid()" before calling this function or it will crash
184 return *m_opaque_up;
185}
186
188 LLDB_INSTRUMENT_VA(this, description);
189
190 if (m_opaque_up) {
191 if (m_opaque_up->Success())
192 description.Printf("success");
193 else {
194 const char *err_string = GetCString();
195 description.Printf("error: %s",
196 (err_string != nullptr ? err_string : ""));
197 }
198 } else
199 description.Printf("error: <NULL>");
200
201 return true;
202}
#define LLDB_INSTRUMENT_VA(...)
uint32_t GetError() const
Definition: SBError.cpp:89
bool GetDescription(lldb::SBStream &description)
Definition: SBError.cpp:187
bool Success() const
Definition: SBError.cpp:79
std::unique_ptr< lldb_private::Status > m_opaque_up
Definition: SBError.h:113
void SetErrorString(const char *err_str)
Definition: SBError.cpp:136
void SetError(uint32_t err, lldb::ErrorType type)
Definition: SBError.cpp:110
bool Fail() const
Definition: SBError.cpp:68
const SBError & operator=(const lldb::SBError &rhs)
Definition: SBError.cpp:43
lldb_private::Status & ref()
Definition: SBError.cpp:177
lldb::ErrorType GetType() const
Definition: SBError.cpp:100
const lldb_private::Status & operator*() const
Definition: SBError.cpp:182
void SetErrorToGenericError()
Definition: SBError.cpp:129
const char * GetCString() const
Get the error string as a NULL terminated UTF8 c-string.
Definition: SBError.cpp:53
lldb_private::Status * operator->()
Definition: SBError.cpp:173
bool IsValid() const
Definition: SBError.cpp:158
void SetErrorToErrno()
Definition: SBError.cpp:122
void CreateIfNeeded()
Definition: SBError.cpp:168
lldb_private::Status * get()
Definition: SBError.cpp:175
void Clear()
Definition: SBError.cpp:61
An error handling class.
Definition: Status.h:115
static Status FromErrno()
Set the current error to errno.
Definition: Status.cpp:276
static Status FromErrorString(const char *str)
Definition: Status.h:138
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