1 //===-- Status.cpp -----------------------------------------------*- C++
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/Status.h"
12 #include "lldb/Utility/VASPrintf.h"
13 #include "lldb/lldb-defines.h"
14 #include "lldb/lldb-enumerations.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/Errno.h"
18 #include "llvm/Support/FormatProviders.h"
23 #include <system_error>
26 #include <mach/mach.h>
39 using namespace lldb_private;
41 Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
43 Status::Status(ValueType err, ErrorType type)
44 : m_code(err), m_type(type), m_string() {}
46 Status::Status(std::error_code EC)
47 : m_code(EC.value()), m_type(ErrorType::eErrorTypeGeneric),
48 m_string(EC.message()) {}
50 Status::Status(const char *format, ...)
51 : m_code(0), m_type(eErrorTypeInvalid), m_string() {
53 va_start(args, format);
54 SetErrorToGenericError();
55 SetErrorStringWithVarArg(format, args);
59 const Status &Status::operator=(llvm::Error error) {
65 // if the error happens to be a errno error, preserve the error code
66 error = llvm::handleErrors(
67 std::move(error), [&](std::unique_ptr<llvm::ECError> e) -> llvm::Error {
68 std::error_code ec = e->convertToErrorCode();
69 if (ec.category() == std::generic_category()) {
71 m_type = ErrorType::eErrorTypePOSIX;
72 return llvm::Error::success();
74 return llvm::Error(std::move(e));
77 // Otherwise, just preserve the message
79 SetErrorToGenericError();
80 SetErrorString(llvm::toString(std::move(error)));
86 llvm::Error Status::ToError() const {
88 return llvm::Error::success();
89 if (m_type == ErrorType::eErrorTypePOSIX)
90 return llvm::errorCodeToError(
91 std::error_code(m_code, std::generic_category()));
92 return llvm::make_error<llvm::StringError>(AsCString(),
93 llvm::inconvertibleErrorCode());
96 // Assignment operator
97 const Status &Status::operator=(const Status &rhs) {
101 m_string = rhs.m_string;
106 Status::~Status() = default;
109 static std::string RetrieveWin32ErrorString(uint32_t error_code) {
110 char *buffer = nullptr;
112 // Retrieve win32 system error.
113 if (::FormatMessageA(
114 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
115 FORMAT_MESSAGE_MAX_WIDTH_MASK,
116 NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
117 (LPSTR)&buffer, 0, NULL)) {
118 message.assign(buffer);
125 // Get the error value as a NULL C string. The error string will be fetched and
126 // cached on demand. The cached error string value will remain until the error
127 // value is changed or cleared.
128 const char *Status::AsCString(const char *default_error_str) const {
132 if (m_string.empty()) {
134 case eErrorTypeMachKernel:
135 #if defined(__APPLE__)
136 if (const char *s = ::mach_error_string(m_code))
141 case eErrorTypePOSIX:
142 m_string = llvm::sys::StrError(m_code);
145 case eErrorTypeWin32:
147 m_string = RetrieveWin32ErrorString(m_code);
155 if (m_string.empty()) {
156 if (default_error_str)
157 m_string.assign(default_error_str);
159 return nullptr; // User wanted a nullptr string back...
161 return m_string.c_str();
164 // Clear the error and any cached error string that it might contain.
165 void Status::Clear() {
167 m_type = eErrorTypeInvalid;
171 // Access the error value.
172 Status::ValueType Status::GetError() const { return m_code; }
174 // Access the error type.
175 ErrorType Status::GetType() const { return m_type; }
177 // Returns true if this object contains a value that describes an error or
178 // otherwise non-success result.
179 bool Status::Fail() const { return m_code != 0; }
181 // Set accessor for the error value to "err" and the type to
182 // "eErrorTypeMachKernel"
183 void Status::SetMachError(uint32_t err) {
185 m_type = eErrorTypeMachKernel;
189 void Status::SetExpressionError(lldb::ExpressionResults result,
192 m_type = eErrorTypeExpression;
196 int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
197 const char *format, ...) {
200 if (format != nullptr && format[0]) {
202 va_start(args, format);
203 length = SetErrorStringWithVarArg(format, args);
209 m_type = eErrorTypeExpression;
213 // Set accessor for the error value and type.
214 void Status::SetError(ValueType err, ErrorType type) {
220 // Update the error value to be "errno" and update the type to be "POSIX".
221 void Status::SetErrorToErrno() {
223 m_type = eErrorTypePOSIX;
227 // Update the error value to be LLDB_GENERIC_ERROR and update the type to be
229 void Status::SetErrorToGenericError() {
230 m_code = LLDB_GENERIC_ERROR;
231 m_type = eErrorTypeGeneric;
235 // Set accessor for the error string value for a specific error. This allows
236 // any string to be supplied as an error explanation. The error string value
237 // will remain until the error value is cleared or a new error value/type is
239 void Status::SetErrorString(llvm::StringRef err_str) {
240 if (!err_str.empty()) {
241 // If we have an error string, we should always at least have an error set
242 // to a generic value.
244 SetErrorToGenericError();
249 /// Set the current error string to a formatted error string.
252 /// A printf style format string
253 int Status::SetErrorStringWithFormat(const char *format, ...) {
254 if (format != nullptr && format[0]) {
256 va_start(args, format);
257 int length = SetErrorStringWithVarArg(format, args);
266 int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
267 if (format != nullptr && format[0]) {
268 // If we have an error string, we should always at least have an error set
269 // to a generic value.
271 SetErrorToGenericError();
273 llvm::SmallString<1024> buf;
274 VASprintf(buf, format, args);
275 m_string = buf.str();
283 // Returns true if the error code in this object is considered a successful
285 bool Status::Success() const { return m_code == 0; }
287 bool Status::WasInterrupted() const {
288 return (m_type == eErrorTypePOSIX && m_code == EINTR);
291 void llvm::format_provider<lldb_private::Status>::format(
292 const lldb_private::Status &error, llvm::raw_ostream &OS,
293 llvm::StringRef Options) {
294 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,