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"
15 #include "lldb/lldb-enumerations.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Errno.h"
19 #include "llvm/Support/FormatProviders.h"
24 #include <system_error>
27 #include <mach/mach.h>
40 using namespace lldb_private;
42 Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
44 Status::Status(ValueType err, ErrorType type)
45 : m_code(err), m_type(type), m_string() {}
47 Status::Status(std::error_code EC)
48 : m_code(EC.value()), m_type(ErrorType::eErrorTypeGeneric),
49 m_string(EC.message()) {}
51 Status::Status(const Status &rhs) = default;
53 Status::Status(const char *format, ...)
54 : m_code(0), m_type(eErrorTypeInvalid), m_string() {
56 va_start(args, format);
57 SetErrorToGenericError();
58 SetErrorStringWithVarArg(format, args);
62 const Status &Status::operator=(llvm::Error error) {
68 // if the error happens to be a errno error, preserve the error code
69 error = llvm::handleErrors(
70 std::move(error), [&](std::unique_ptr<llvm::ECError> e) -> llvm::Error {
71 std::error_code ec = e->convertToErrorCode();
72 if (ec.category() == std::generic_category()) {
74 m_type = ErrorType::eErrorTypePOSIX;
75 return llvm::Error::success();
77 return llvm::Error(std::move(e));
80 // Otherwise, just preserve the message
82 SetErrorToGenericError();
83 SetErrorString(llvm::toString(std::move(error)));
89 llvm::Error Status::ToError() const {
91 return llvm::Error::success();
92 if (m_type == ErrorType::eErrorTypePOSIX)
93 return llvm::errorCodeToError(
94 std::error_code(m_code, std::generic_category()));
95 return llvm::make_error<llvm::StringError>(AsCString(),
96 llvm::inconvertibleErrorCode());
99 //----------------------------------------------------------------------
100 // Assignment operator
101 //----------------------------------------------------------------------
102 const Status &Status::operator=(const Status &rhs) {
106 m_string = rhs.m_string;
111 Status::~Status() = default;
114 static std::string RetrieveWin32ErrorString(uint32_t error_code) {
115 char *buffer = nullptr;
117 // Retrieve win32 system error.
118 if (::FormatMessageA(
119 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
120 FORMAT_MESSAGE_MAX_WIDTH_MASK,
121 NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
122 (LPSTR)&buffer, 0, NULL)) {
123 message.assign(buffer);
130 //----------------------------------------------------------------------
131 // Get the error value as a NULL C string. The error string will be fetched and
132 // cached on demand. The cached error string value will remain until the error
133 // value is changed or cleared.
134 //----------------------------------------------------------------------
135 const char *Status::AsCString(const char *default_error_str) const {
139 if (m_string.empty()) {
141 case eErrorTypeMachKernel:
142 #if defined(__APPLE__)
143 if (const char *s = ::mach_error_string(m_code))
148 case eErrorTypePOSIX:
149 m_string = llvm::sys::StrError(m_code);
152 case eErrorTypeWin32:
154 m_string = RetrieveWin32ErrorString(m_code);
162 if (m_string.empty()) {
163 if (default_error_str)
164 m_string.assign(default_error_str);
166 return nullptr; // User wanted a nullptr string back...
168 return m_string.c_str();
171 //----------------------------------------------------------------------
172 // Clear the error and any cached error string that it might contain.
173 //----------------------------------------------------------------------
174 void Status::Clear() {
176 m_type = eErrorTypeInvalid;
180 //----------------------------------------------------------------------
181 // Access the error value.
182 //----------------------------------------------------------------------
183 Status::ValueType Status::GetError() const { return m_code; }
185 //----------------------------------------------------------------------
186 // Access the error type.
187 //----------------------------------------------------------------------
188 ErrorType Status::GetType() const { return m_type; }
190 //----------------------------------------------------------------------
191 // Returns true if this object contains a value that describes an error or
192 // otherwise non-success result.
193 //----------------------------------------------------------------------
194 bool Status::Fail() const { return m_code != 0; }
196 //----------------------------------------------------------------------
197 // Set accessor for the error value to "err" and the type to
198 // "eErrorTypeMachKernel"
199 //----------------------------------------------------------------------
200 void Status::SetMachError(uint32_t err) {
202 m_type = eErrorTypeMachKernel;
206 void Status::SetExpressionError(lldb::ExpressionResults result,
209 m_type = eErrorTypeExpression;
213 int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
214 const char *format, ...) {
217 if (format != nullptr && format[0]) {
219 va_start(args, format);
220 length = SetErrorStringWithVarArg(format, args);
226 m_type = eErrorTypeExpression;
230 //----------------------------------------------------------------------
231 // Set accessor for the error value and type.
232 //----------------------------------------------------------------------
233 void Status::SetError(ValueType err, ErrorType type) {
239 //----------------------------------------------------------------------
240 // Update the error value to be "errno" and update the type to be "POSIX".
241 //----------------------------------------------------------------------
242 void Status::SetErrorToErrno() {
244 m_type = eErrorTypePOSIX;
248 //----------------------------------------------------------------------
249 // Update the error value to be LLDB_GENERIC_ERROR and update the type to be
251 //----------------------------------------------------------------------
252 void Status::SetErrorToGenericError() {
253 m_code = LLDB_GENERIC_ERROR;
254 m_type = eErrorTypeGeneric;
258 //----------------------------------------------------------------------
259 // Set accessor for the error string value for a specific error. This allows
260 // any string to be supplied as an error explanation. The error string value
261 // will remain until the error value is cleared or a new error value/type is
263 //----------------------------------------------------------------------
264 void Status::SetErrorString(llvm::StringRef err_str) {
265 if (!err_str.empty()) {
266 // If we have an error string, we should always at least have an error set
267 // to a generic value.
269 SetErrorToGenericError();
274 //------------------------------------------------------------------
275 /// Set the current error string to a formatted error string.
278 /// A printf style format string
279 //------------------------------------------------------------------
280 int Status::SetErrorStringWithFormat(const char *format, ...) {
281 if (format != nullptr && format[0]) {
283 va_start(args, format);
284 int length = SetErrorStringWithVarArg(format, args);
293 int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
294 if (format != nullptr && format[0]) {
295 // If we have an error string, we should always at least have an error set
296 // to a generic value.
298 SetErrorToGenericError();
300 llvm::SmallString<1024> buf;
301 VASprintf(buf, format, args);
302 m_string = buf.str();
310 //----------------------------------------------------------------------
311 // Returns true if the error code in this object is considered a successful
313 //----------------------------------------------------------------------
314 bool Status::Success() const { return m_code == 0; }
316 bool Status::WasInterrupted() const {
317 return (m_type == eErrorTypePOSIX && m_code == EINTR);
320 void llvm::format_provider<lldb_private::Status>::format(
321 const lldb_private::Status &error, llvm::raw_ostream &OS,
322 llvm::StringRef Options) {
323 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,