1 //===-- Error.cpp -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include <mach/mach.h>
19 // Other libraries and framework includes
20 #include "llvm/ADT/SmallVector.h"
23 #include "lldb/Core/Error.h"
24 #include "lldb/Core/Log.h"
25 #include "lldb/Host/PosixApi.h"
28 using namespace lldb_private;
30 Error::Error() : m_code(0), m_type(eErrorTypeInvalid), m_string() {}
32 Error::Error(ValueType err, ErrorType type)
33 : m_code(err), m_type(type), m_string() {}
35 Error::Error(const Error &rhs) = default;
37 Error::Error(const char *format, ...)
38 : m_code(0), m_type(eErrorTypeInvalid), m_string() {
40 va_start(args, format);
41 SetErrorToGenericError();
42 SetErrorStringWithVarArg(format, args);
46 //----------------------------------------------------------------------
47 // Assignment operator
48 //----------------------------------------------------------------------
49 const Error &Error::operator=(const Error &rhs) {
53 m_string = rhs.m_string;
58 //----------------------------------------------------------------------
59 // Assignment operator
60 //----------------------------------------------------------------------
61 const Error &Error::operator=(uint32_t err) {
63 m_type = eErrorTypeMachKernel;
68 Error::~Error() = default;
70 //----------------------------------------------------------------------
71 // Get the error value as a NULL C string. The error string will be
72 // fetched and cached on demand. The cached error string value will
73 // remain until the error value is changed or cleared.
74 //----------------------------------------------------------------------
75 const char *Error::AsCString(const char *default_error_str) const {
79 if (m_string.empty()) {
80 const char *s = nullptr;
82 case eErrorTypeMachKernel:
83 #if defined(__APPLE__)
84 s = ::mach_error_string(m_code);
89 s = ::strerror(m_code);
98 if (m_string.empty()) {
99 if (default_error_str)
100 m_string.assign(default_error_str);
102 return nullptr; // User wanted a nullptr string back...
104 return m_string.c_str();
107 //----------------------------------------------------------------------
108 // Clear the error and any cached error string that it might contain.
109 //----------------------------------------------------------------------
110 void Error::Clear() {
112 m_type = eErrorTypeInvalid;
116 //----------------------------------------------------------------------
117 // Access the error value.
118 //----------------------------------------------------------------------
119 Error::ValueType Error::GetError() const { return m_code; }
121 //----------------------------------------------------------------------
122 // Access the error type.
123 //----------------------------------------------------------------------
124 ErrorType Error::GetType() const { return m_type; }
126 //----------------------------------------------------------------------
127 // Returns true if this object contains a value that describes an
128 // error or otherwise non-success result.
129 //----------------------------------------------------------------------
130 bool Error::Fail() const { return m_code != 0; }
132 //----------------------------------------------------------------------
133 // Log the error given a string with format. If the this object
134 // contains an error code, update the error string to contain the
135 // "error: " followed by the formatted string, followed by the error
136 // value and any string that describes the current error. This
137 // allows more context to be given to an error string that remains
138 // cached in this object. Logging always occurs even when the error
139 // code contains a non-error value.
140 //----------------------------------------------------------------------
141 void Error::PutToLog(Log *log, const char *format, ...) {
142 char *arg_msg = nullptr;
144 va_start(args, format);
145 ::vasprintf(&arg_msg, format, args);
148 if (arg_msg != nullptr) {
150 const char *err_str = AsCString();
151 if (err_str == nullptr)
154 SetErrorStringWithFormat("error: %s err = %s (0x%8.8x)", arg_msg, err_str,
157 log->Error("%s", m_string.c_str());
160 log->Printf("%s err = 0x%8.8x", arg_msg, m_code);
166 //----------------------------------------------------------------------
167 // Log the error given a string with format. If the this object
168 // contains an error code, update the error string to contain the
169 // "error: " followed by the formatted string, followed by the error
170 // value and any string that describes the current error. This
171 // allows more context to be given to an error string that remains
172 // cached in this object. Logging only occurs even when the error
173 // code contains a error value.
174 //----------------------------------------------------------------------
175 void Error::LogIfError(Log *log, const char *format, ...) {
177 char *arg_msg = nullptr;
179 va_start(args, format);
180 ::vasprintf(&arg_msg, format, args);
183 if (arg_msg != nullptr) {
184 const char *err_str = AsCString();
185 if (err_str == nullptr)
188 SetErrorStringWithFormat("%s err = %s (0x%8.8x)", arg_msg, err_str,
191 log->Error("%s", m_string.c_str());
198 //----------------------------------------------------------------------
199 // Set accesssor for the error value to "err" and the type to
200 // "eErrorTypeMachKernel"
201 //----------------------------------------------------------------------
202 void Error::SetMachError(uint32_t err) {
204 m_type = eErrorTypeMachKernel;
208 void Error::SetExpressionError(lldb::ExpressionResults result,
211 m_type = eErrorTypeExpression;
215 int Error::SetExpressionErrorWithFormat(lldb::ExpressionResults result,
216 const char *format, ...) {
219 if (format != nullptr && format[0]) {
221 va_start(args, format);
222 length = SetErrorStringWithVarArg(format, args);
228 m_type = eErrorTypeExpression;
232 //----------------------------------------------------------------------
233 // Set accesssor for the error value and type.
234 //----------------------------------------------------------------------
235 void Error::SetError(ValueType err, ErrorType type) {
241 //----------------------------------------------------------------------
242 // Update the error value to be "errno" and update the type to
244 //----------------------------------------------------------------------
245 void Error::SetErrorToErrno() {
247 m_type = eErrorTypePOSIX;
251 //----------------------------------------------------------------------
252 // Update the error value to be LLDB_GENERIC_ERROR and update the type
254 //----------------------------------------------------------------------
255 void Error::SetErrorToGenericError() {
256 m_code = LLDB_GENERIC_ERROR;
257 m_type = eErrorTypeGeneric;
261 //----------------------------------------------------------------------
262 // Set accessor for the error string value for a specific error.
263 // This allows any string to be supplied as an error explanation.
264 // The error string value will remain until the error value is
265 // cleared or a new error value/type is assigned.
266 //----------------------------------------------------------------------
267 void Error::SetErrorString(llvm::StringRef err_str) {
268 if (!err_str.empty()) {
269 // If we have an error string, we should always at least have an error
270 // set to a generic value.
272 SetErrorToGenericError();
277 //------------------------------------------------------------------
278 /// Set the current error string to a formatted error string.
281 /// A printf style format string
282 //------------------------------------------------------------------
283 int Error::SetErrorStringWithFormat(const char *format, ...) {
284 if (format != nullptr && format[0]) {
286 va_start(args, format);
287 int length = SetErrorStringWithVarArg(format, args);
296 int Error::SetErrorStringWithVarArg(const char *format, va_list args) {
297 if (format != nullptr && format[0]) {
298 // If we have an error string, we should always at least have
299 // an error set to a generic value.
301 SetErrorToGenericError();
303 // Try and fit our error into a 1024 byte buffer first...
304 llvm::SmallVector<char, 1024> buf;
306 // Copy in case our first call to vsnprintf doesn't fit into our
307 // allocated buffer above
309 va_copy(copy_args, args);
310 unsigned length = ::vsnprintf(buf.data(), buf.size(), format, args);
311 if (length >= buf.size()) {
312 // The error formatted string didn't fit into our buffer, resize it
313 // to the exact needed size, and retry
314 buf.resize(length + 1);
315 length = ::vsnprintf(buf.data(), buf.size(), format, copy_args);
317 assert(length < buf.size());
319 m_string.assign(buf.data(), length);
328 //----------------------------------------------------------------------
329 // Returns true if the error code in this object is considered a
330 // successful return value.
331 //----------------------------------------------------------------------
332 bool Error::Success() const { return m_code == 0; }
334 bool Error::WasInterrupted() const {
335 return (m_type == eErrorTypePOSIX && m_code == EINTR);