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 //----------------------------------------------------------------------
60 // Assignment operator
61 //----------------------------------------------------------------------
62 const Status &Status::operator=(const Status &rhs) {
66 m_string = rhs.m_string;
71 //----------------------------------------------------------------------
72 // Assignment operator
73 //----------------------------------------------------------------------
74 const Status &Status::operator=(uint32_t err) {
76 m_type = eErrorTypeMachKernel;
81 Status::~Status() = default;
83 //----------------------------------------------------------------------
84 // Get the error value as a NULL C string. The error string will be
85 // fetched and cached on demand. The cached error string value will
86 // remain until the error value is changed or cleared.
87 //----------------------------------------------------------------------
88 const char *Status::AsCString(const char *default_error_str) const {
92 if (m_string.empty()) {
93 const char *s = nullptr;
95 case eErrorTypeMachKernel:
96 #if defined(__APPLE__)
97 s = ::mach_error_string(m_code);
101 case eErrorTypePOSIX:
102 s = ::strerror(m_code);
111 if (m_string.empty()) {
112 if (default_error_str)
113 m_string.assign(default_error_str);
115 return nullptr; // User wanted a nullptr string back...
117 return m_string.c_str();
120 //----------------------------------------------------------------------
121 // Clear the error and any cached error string that it might contain.
122 //----------------------------------------------------------------------
123 void Status::Clear() {
125 m_type = eErrorTypeInvalid;
129 //----------------------------------------------------------------------
130 // Access the error value.
131 //----------------------------------------------------------------------
132 Status::ValueType Status::GetError() const { return m_code; }
134 //----------------------------------------------------------------------
135 // Access the error type.
136 //----------------------------------------------------------------------
137 ErrorType Status::GetType() const { return m_type; }
139 //----------------------------------------------------------------------
140 // Returns true if this object contains a value that describes an
141 // error or otherwise non-success result.
142 //----------------------------------------------------------------------
143 bool Status::Fail() const { return m_code != 0; }
145 //----------------------------------------------------------------------
146 // Set accesssor for the error value to "err" and the type to
147 // "eErrorTypeMachKernel"
148 //----------------------------------------------------------------------
149 void Status::SetMachError(uint32_t err) {
151 m_type = eErrorTypeMachKernel;
155 void Status::SetExpressionError(lldb::ExpressionResults result,
158 m_type = eErrorTypeExpression;
162 int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
163 const char *format, ...) {
166 if (format != nullptr && format[0]) {
168 va_start(args, format);
169 length = SetErrorStringWithVarArg(format, args);
175 m_type = eErrorTypeExpression;
179 //----------------------------------------------------------------------
180 // Set accesssor for the error value and type.
181 //----------------------------------------------------------------------
182 void Status::SetError(ValueType err, ErrorType type) {
188 //----------------------------------------------------------------------
189 // Update the error value to be "errno" and update the type to
191 //----------------------------------------------------------------------
192 void Status::SetErrorToErrno() {
194 m_type = eErrorTypePOSIX;
198 //----------------------------------------------------------------------
199 // Update the error value to be LLDB_GENERIC_ERROR and update the type
201 //----------------------------------------------------------------------
202 void Status::SetErrorToGenericError() {
203 m_code = LLDB_GENERIC_ERROR;
204 m_type = eErrorTypeGeneric;
208 //----------------------------------------------------------------------
209 // Set accessor for the error string value for a specific error.
210 // This allows any string to be supplied as an error explanation.
211 // The error string value will remain until the error value is
212 // cleared or a new error value/type is assigned.
213 //----------------------------------------------------------------------
214 void Status::SetErrorString(llvm::StringRef err_str) {
215 if (!err_str.empty()) {
216 // If we have an error string, we should always at least have an error
217 // set to a generic value.
219 SetErrorToGenericError();
224 //------------------------------------------------------------------
225 /// Set the current error string to a formatted error string.
228 /// A printf style format string
229 //------------------------------------------------------------------
230 int Status::SetErrorStringWithFormat(const char *format, ...) {
231 if (format != nullptr && format[0]) {
233 va_start(args, format);
234 int length = SetErrorStringWithVarArg(format, args);
243 int Status::SetErrorStringWithVarArg(const char *format, va_list args) {
244 if (format != nullptr && format[0]) {
245 // If we have an error string, we should always at least have
246 // an error set to a generic value.
248 SetErrorToGenericError();
250 llvm::SmallString<1024> buf;
251 VASprintf(buf, format, args);
252 m_string = buf.str();
260 //----------------------------------------------------------------------
261 // Returns true if the error code in this object is considered a
262 // successful return value.
263 //----------------------------------------------------------------------
264 bool Status::Success() const { return m_code == 0; }
266 bool Status::WasInterrupted() const {
267 return (m_type == eErrorTypePOSIX && m_code == EINTR);
270 void llvm::format_provider<lldb_private::Status>::format(
271 const lldb_private::Status &error, llvm::raw_ostream &OS,
272 llvm::StringRef Options) {
273 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,