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>
16 // Other libraries and framework includes
18 #include "lldb/Core/Error.h"
19 #include "lldb/Core/Log.h"
20 #include "llvm/ADT/SmallVector.h"
25 using namespace lldb_private;
29 m_type (eErrorTypeInvalid),
34 //----------------------------------------------------------------------
35 // Default constructor
36 //----------------------------------------------------------------------
37 Error::Error(ValueType err, ErrorType type) :
44 Error::Error (const Error &rhs) :
47 m_string (rhs.m_string)
51 Error::Error (const char* format, ...):
53 m_type (eErrorTypeInvalid),
57 va_start (args, format);
58 SetErrorToGenericError ();
59 SetErrorStringWithVarArg (format, args);
63 //----------------------------------------------------------------------
64 // Assignment operator
65 //----------------------------------------------------------------------
67 Error::operator = (const Error& rhs)
73 m_string = rhs.m_string;
79 //----------------------------------------------------------------------
80 // Assignment operator
81 //----------------------------------------------------------------------
83 Error::operator = (uint32_t err)
86 m_type = eErrorTypeMachKernel;
95 //----------------------------------------------------------------------
96 // Get the error value as a NULL C string. The error string will be
97 // fetched and cached on demand. The cached error string value will
98 // remain until the error value is changed or cleared.
99 //----------------------------------------------------------------------
101 Error::AsCString(const char *default_error_str) const
106 if (m_string.empty())
108 const char *s = NULL;
111 case eErrorTypeMachKernel:
112 #if defined (__APPLE__)
113 s = ::mach_error_string (m_code);
117 case eErrorTypePOSIX:
118 s = ::strerror (m_code);
127 if (m_string.empty())
129 if (default_error_str)
130 m_string.assign(default_error_str);
132 return NULL; // User wanted a NULL string back...
134 return m_string.c_str();
138 //----------------------------------------------------------------------
139 // Clear the error and any cached error string that it might contain.
140 //----------------------------------------------------------------------
145 m_type = eErrorTypeInvalid;
149 //----------------------------------------------------------------------
150 // Access the error value.
151 //----------------------------------------------------------------------
153 Error::GetError () const
158 //----------------------------------------------------------------------
159 // Access the error type.
160 //----------------------------------------------------------------------
162 Error::GetType () const
167 //----------------------------------------------------------------------
168 // Retuns true if this object contains an value that describes an
169 // error or otherwise non-success result.
170 //----------------------------------------------------------------------
177 //----------------------------------------------------------------------
178 // Log the error given a string with format. If the this object
179 // contains an error code, update the error string to contain the
180 // "error: " followed by the formatted string, followed by the error
181 // value and any string that describes the current error. This
182 // allows more context to be given to an error string that remains
183 // cached in this object. Logging always occurs even when the error
184 // code contains a non-error value.
185 //----------------------------------------------------------------------
187 Error::PutToLog (Log *log, const char *format, ...)
189 char *arg_msg = NULL;
191 va_start (args, format);
192 ::vasprintf (&arg_msg, format, args);
199 const char *err_str = AsCString();
203 SetErrorStringWithFormat("error: %s err = %s (0x%8.8x)", arg_msg, err_str, m_code);
205 log->Error("%s", m_string.c_str());
210 log->Printf("%s err = 0x%8.8x", arg_msg, m_code);
216 //----------------------------------------------------------------------
217 // Log the error given a string with format. If the this object
218 // contains an error code, update the error string to contain the
219 // "error: " followed by the formatted string, followed by the error
220 // value and any string that describes the current error. This
221 // allows more context to be given to an error string that remains
222 // cached in this object. Logging only occurs even when the error
223 // code contains a error value.
224 //----------------------------------------------------------------------
226 Error::LogIfError (Log *log, const char *format, ...)
230 char *arg_msg = NULL;
232 va_start (args, format);
233 ::vasprintf (&arg_msg, format, args);
238 const char *err_str = AsCString();
242 SetErrorStringWithFormat("%s err = %s (0x%8.8x)", arg_msg, err_str, m_code);
244 log->Error("%s", m_string.c_str());
251 //----------------------------------------------------------------------
252 // Set accesssor for the error value to "err" and the type to
253 // "eErrorTypeMachKernel"
254 //----------------------------------------------------------------------
256 Error::SetMachError (uint32_t err)
259 m_type = eErrorTypeMachKernel;
264 Error::SetExpressionError (lldb::ExpressionResults result, const char *mssg)
267 m_type = eErrorTypeExpression;
272 Error::SetExpressionErrorWithFormat (lldb::ExpressionResults result, const char *format, ...)
276 if (format && format[0])
279 va_start (args, format);
280 length = SetErrorStringWithVarArg (format, args);
288 m_type = eErrorTypeExpression;
292 //----------------------------------------------------------------------
293 // Set accesssor for the error value and type.
294 //----------------------------------------------------------------------
296 Error::SetError (ValueType err, ErrorType type)
303 //----------------------------------------------------------------------
304 // Update the error value to be "errno" and update the type to
306 //----------------------------------------------------------------------
308 Error::SetErrorToErrno()
311 m_type = eErrorTypePOSIX;
315 //----------------------------------------------------------------------
316 // Update the error value to be LLDB_GENERIC_ERROR and update the type
318 //----------------------------------------------------------------------
320 Error::SetErrorToGenericError ()
322 m_code = LLDB_GENERIC_ERROR;
323 m_type = eErrorTypeGeneric;
327 //----------------------------------------------------------------------
328 // Set accessor for the error string value for a specific error.
329 // This allows any string to be supplied as an error explanation.
330 // The error string value will remain until the error value is
331 // cleared or a new error value/type is assigned.
332 //----------------------------------------------------------------------
334 Error::SetErrorString (const char *err_str)
336 if (err_str && err_str[0])
338 // If we have an error string, we should always at least have
339 // an error set to a generic value.
341 SetErrorToGenericError();
348 //------------------------------------------------------------------
349 /// Set the current error string to a formatted error string.
352 /// A printf style format string
353 //------------------------------------------------------------------
355 Error::SetErrorStringWithFormat (const char *format, ...)
357 if (format && format[0])
360 va_start (args, format);
361 int length = SetErrorStringWithVarArg (format, args);
373 Error::SetErrorStringWithVarArg (const char *format, va_list args)
375 if (format && format[0])
377 // If we have an error string, we should always at least have
378 // an error set to a generic value.
380 SetErrorToGenericError();
382 // Try and fit our error into a 1024 byte buffer first...
383 llvm::SmallVector<char, 1024> buf;
385 // Copy in case our first call to vsnprintf doesn't fit into our
386 // allocated buffer above
388 va_copy (copy_args, args);
389 unsigned length = ::vsnprintf (buf.data(), buf.size(), format, args);
390 if (length >= buf.size())
392 // The error formatted string didn't fit into our buffer, resize it
393 // to the exact needed size, and retry
394 buf.resize(length + 1);
395 length = ::vsnprintf (buf.data(), buf.size(), format, copy_args);
397 assert (length < buf.size());
399 m_string.assign(buf.data(), length);
411 //----------------------------------------------------------------------
412 // Returns true if the error code in this object is considered a
413 // successful return value.
414 //----------------------------------------------------------------------
416 Error::Success() const
422 Error::WasInterrupted() const
424 if (m_type == eErrorTypePOSIX && m_code == EINTR)