1 //===-- Status.cpp -----------------------------------------------*- C++
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #include "lldb/Utility/Status.h"
13 #include "lldb/Utility/VASPrintf.h"
14 #include "lldb/lldb-defines.h" // for LLDB_GENERIC_ERROR
15 #include "lldb/lldb-enumerations.h" // for ErrorType, ErrorType::eErr...
16 #include "llvm/ADT/SmallString.h" // for SmallString
17 #include "llvm/ADT/StringRef.h" // for StringRef
18 #include "llvm/Support/FormatProviders.h" // for format_provider
22 #include <string> // for string
23 #include <system_error>
26 #include <mach/mach.h>
29 #include <stdint.h> // for uint32_t
30 #include <string.h> // for strerror
37 using namespace lldb_private;
39 Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
41 Status::Status(ValueType err, ErrorType type)
42 : m_code(err), m_type(type), m_string() {}
44 Status::Status(std::error_code EC)
45 : m_code(EC.value()), m_type(ErrorType::eErrorTypeGeneric),
46 m_string(EC.message()) {}
48 Status::Status(const Status &rhs) = default;
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 Status::Status(llvm::Error error)
60 : m_code(0), m_type(ErrorType::eErrorTypeGeneric) {
64 // if the error happens to be a errno error, preserve the error code
65 error = llvm::handleErrors(
66 std::move(error), [&](std::unique_ptr<llvm::ECError> e) -> llvm::Error {
67 std::error_code ec = e->convertToErrorCode();
68 if (ec.category() == std::generic_category()) {
70 m_type = ErrorType::eErrorTypePOSIX;
71 return llvm::Error::success();
73 return llvm::Error(std::move(e));
76 // Otherwise, just preserve the message
78 SetErrorString(llvm::toString(std::move(error)));
81 llvm::Error Status::ToError() const {
83 return llvm::Error::success();
84 if (m_type == ErrorType::eErrorTypePOSIX)
85 return llvm::errorCodeToError(std::error_code(m_code, std::generic_category()));
86 return llvm::make_error<llvm::StringError>(AsCString(),
87 llvm::inconvertibleErrorCode());
90 //----------------------------------------------------------------------
91 // Assignment operator
92 //----------------------------------------------------------------------
93 const Status &Status::operator=(const Status &rhs) {
97 m_string = rhs.m_string;
102 //----------------------------------------------------------------------
103 // Assignment operator
104 //----------------------------------------------------------------------
105 const Status &Status::operator=(uint32_t err) {
107 m_type = eErrorTypeMachKernel;
112 Status::~Status() = default;
114 //----------------------------------------------------------------------
115 // Get the error value as a NULL C string. The error string will be
116 // fetched and cached on demand. The cached error string value will
117 // remain until the error value is changed or cleared.
118 //----------------------------------------------------------------------
119 const char *Status::AsCString(const char *default_error_str) const {
123 if (m_string.empty()) {
124 const char *s = nullptr;
126 case eErrorTypeMachKernel:
127 #if defined(__APPLE__)
128 s = ::mach_error_string(m_code);
132 case eErrorTypePOSIX:
133 s = ::strerror(m_code);
142 if (m_string.empty()) {
143 if (default_error_str)
144 m_string.assign(default_error_str);
146 return nullptr; // User wanted a nullptr string back...
148 return m_string.c_str();
151 //----------------------------------------------------------------------
152 // Clear the error and any cached error string that it might contain.
153 //----------------------------------------------------------------------
154 void Status::Clear() {
156 m_type = eErrorTypeInvalid;
160 //----------------------------------------------------------------------
161 // Access the error value.
162 //----------------------------------------------------------------------
163 Status::ValueType Status::GetError() const { return m_code; }
165 //----------------------------------------------------------------------
166 // Access the error type.
167 //----------------------------------------------------------------------
168 ErrorType Status::GetType() const { return m_type; }
170 //----------------------------------------------------------------------
171 // Returns true if this object contains a value that describes an
172 // error or otherwise non-success result.
173 //----------------------------------------------------------------------
174 bool Status::Fail() const { return m_code != 0; }
176 //----------------------------------------------------------------------
177 // Set accesssor for the error value to "err" and the type to
178 // "eErrorTypeMachKernel"
179 //----------------------------------------------------------------------
180 void Status::SetMachError(uint32_t err) {
182 m_type = eErrorTypeMachKernel;
186 void Status::SetExpressionError(lldb::ExpressionResults result,
189 m_type = eErrorTypeExpression;
193 int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
194 const char *format, ...) {
197 if (format != nullptr && format[0]) {
199 va_start(args, format);
200 length = SetErrorStringWithVarArg(format, args);
206 m_type = eErrorTypeExpression;
210 //----------------------------------------------------------------------
211 // Set accesssor for the error value and type.
212 //----------------------------------------------------------------------
213 void Status::SetError(ValueType err, ErrorType type) {
219 //----------------------------------------------------------------------
220 // Update the error value to be "errno" and update the type to
222 //----------------------------------------------------------------------
223 void Status::SetErrorToErrno() {
225 m_type = eErrorTypePOSIX;
229 //----------------------------------------------------------------------
230 // Update the error value to be LLDB_GENERIC_ERROR and update the type
232 //----------------------------------------------------------------------
233 void Status::SetErrorToGenericError() {
234 m_code = LLDB_GENERIC_ERROR;
235 m_type = eErrorTypeGeneric;
239 //----------------------------------------------------------------------
240 // Set accessor for the error string value for a specific error.
241 // This allows any string to be supplied as an error explanation.
242 // The error string value will remain until the error value is
243 // cleared or a new error value/type is assigned.
244 //----------------------------------------------------------------------
245 void Status::SetErrorString(llvm::StringRef err_str) {
246 if (!err_str.empty()) {
247 // If we have an error string, we should always at least have an error
248 // set to a generic value.
250 SetErrorToGenericError();
255 //------------------------------------------------------------------
256 /// Set the current error string to a formatted error string.
259 /// A printf style format string
260 //------------------------------------------------------------------
261 int Status::SetErrorStringWithFormat(const char *format, ...) {
262 if (format != nullptr && format[0]) {
264 va_start(args, format);
265 int length = SetErrorStringWithVarArg(format, args);
274 int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
275 if (format != nullptr && format[0]) {
276 // If we have an error string, we should always at least have
277 // an error set to a generic value.
279 SetErrorToGenericError();
281 llvm::SmallString<1024> buf;
282 VASprintf(buf, format, args);
283 m_string = buf.str();
291 //----------------------------------------------------------------------
292 // Returns true if the error code in this object is considered a
293 // successful return value.
294 //----------------------------------------------------------------------
295 bool Status::Success() const { return m_code == 0; }
297 bool Status::WasInterrupted() const {
298 return (m_type == eErrorTypePOSIX && m_code == EINTR);
301 void llvm::format_provider<lldb_private::Status>::format(
302 const lldb_private::Status &error, llvm::raw_ostream &OS,
303 llvm::StringRef Options) {
304 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,