Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Error.cpp -----------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // C Includes |
Charles Davis | 322fc84 | 2013-08-27 06:13:56 +0000 | [diff] [blame] | 11 | #ifdef __APPLE__ |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 12 | #include <mach/mach.h> |
Charles Davis | 322fc84 | 2013-08-27 06:13:56 +0000 | [diff] [blame] | 13 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 14 | |
| 15 | // C++ Includes |
Eugene Zelenko | a74f37a | 2016-03-10 23:57:12 +0000 | [diff] [blame] | 16 | #include <cerrno> |
| 17 | #include <cstdarg> |
| 18 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | // Other libraries and framework includes |
Eugene Zelenko | a74f37a | 2016-03-10 23:57:12 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | // Project includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Core/Log.h" |
Zachary Turner | f343968f | 2016-08-09 23:06:08 +0000 | [diff] [blame] | 24 | #include "lldb/Host/PosixApi.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 25 | #include "lldb/Utility/Error.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | using namespace lldb; |
| 28 | using namespace lldb_private; |
| 29 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | Error::Error() : m_code(0), m_type(eErrorTypeInvalid), m_string() {} |
Greg Clayton | ca512b3 | 2011-01-14 04:54:56 +0000 | [diff] [blame] | 31 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | Error::Error(ValueType err, ErrorType type) |
| 33 | : m_code(err), m_type(type), m_string() {} |
Greg Clayton | ca512b3 | 2011-01-14 04:54:56 +0000 | [diff] [blame] | 34 | |
Eugene Zelenko | a74f37a | 2016-03-10 23:57:12 +0000 | [diff] [blame] | 35 | Error::Error(const Error &rhs) = default; |
Greg Clayton | ca512b3 | 2011-01-14 04:54:56 +0000 | [diff] [blame] | 36 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | Error::Error(const char *format, ...) |
| 38 | : m_code(0), m_type(eErrorTypeInvalid), m_string() { |
| 39 | va_list args; |
| 40 | va_start(args, format); |
| 41 | SetErrorToGenericError(); |
| 42 | SetErrorStringWithVarArg(format, args); |
| 43 | va_end(args); |
Enrico Granata | f2bbf71 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | //---------------------------------------------------------------------- |
| 47 | // Assignment operator |
| 48 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | const Error &Error::operator=(const Error &rhs) { |
| 50 | if (this != &rhs) { |
| 51 | m_code = rhs.m_code; |
| 52 | m_type = rhs.m_type; |
| 53 | m_string = rhs.m_string; |
| 54 | } |
| 55 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | //---------------------------------------------------------------------- |
| 59 | // Assignment operator |
| 60 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | const Error &Error::operator=(uint32_t err) { |
| 62 | m_code = err; |
| 63 | m_type = eErrorTypeMachKernel; |
| 64 | m_string.clear(); |
| 65 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Eugene Zelenko | a74f37a | 2016-03-10 23:57:12 +0000 | [diff] [blame] | 68 | Error::~Error() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | |
| 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 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | const char *Error::AsCString(const char *default_error_str) const { |
| 76 | if (Success()) |
| 77 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | if (m_string.empty()) { |
| 80 | const char *s = nullptr; |
| 81 | switch (m_type) { |
| 82 | case eErrorTypeMachKernel: |
| 83 | #if defined(__APPLE__) |
| 84 | s = ::mach_error_string(m_code); |
Eli Friedman | 6eb685c | 2010-06-10 23:45:58 +0000 | [diff] [blame] | 85 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | case eErrorTypePOSIX: |
| 89 | s = ::strerror(m_code); |
| 90 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | default: |
| 93 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | if (s != nullptr) |
| 96 | m_string.assign(s); |
| 97 | } |
| 98 | if (m_string.empty()) { |
| 99 | if (default_error_str) |
| 100 | m_string.assign(default_error_str); |
| 101 | else |
| 102 | return nullptr; // User wanted a nullptr string back... |
| 103 | } |
| 104 | return m_string.c_str(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | //---------------------------------------------------------------------- |
| 108 | // Clear the error and any cached error string that it might contain. |
| 109 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | void Error::Clear() { |
| 111 | m_code = 0; |
| 112 | m_type = eErrorTypeInvalid; |
| 113 | m_string.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | //---------------------------------------------------------------------- |
| 117 | // Access the error value. |
| 118 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | Error::ValueType Error::GetError() const { return m_code; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 120 | |
| 121 | //---------------------------------------------------------------------- |
| 122 | // Access the error type. |
| 123 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | ErrorType Error::GetType() const { return m_type; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 125 | |
| 126 | //---------------------------------------------------------------------- |
Ed Maste | 75500e7 | 2016-07-19 15:28:02 +0000 | [diff] [blame] | 127 | // Returns true if this object contains a value that describes an |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 128 | // error or otherwise non-success result. |
| 129 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | bool Error::Fail() const { return m_code != 0; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 131 | |
| 132 | //---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 133 | // Set accesssor for the error value to "err" and the type to |
| 134 | // "eErrorTypeMachKernel" |
| 135 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | void Error::SetMachError(uint32_t err) { |
| 137 | m_code = err; |
| 138 | m_type = eErrorTypeMachKernel; |
| 139 | m_string.clear(); |
| 140 | } |
| 141 | |
| 142 | void Error::SetExpressionError(lldb::ExpressionResults result, |
| 143 | const char *mssg) { |
| 144 | m_code = result; |
| 145 | m_type = eErrorTypeExpression; |
| 146 | m_string = mssg; |
| 147 | } |
| 148 | |
| 149 | int Error::SetExpressionErrorWithFormat(lldb::ExpressionResults result, |
| 150 | const char *format, ...) { |
| 151 | int length = 0; |
| 152 | |
| 153 | if (format != nullptr && format[0]) { |
| 154 | va_list args; |
| 155 | va_start(args, format); |
| 156 | length = SetErrorStringWithVarArg(format, args); |
| 157 | va_end(args); |
| 158 | } else { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | m_string.clear(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | } |
| 161 | m_code = result; |
| 162 | m_type = eErrorTypeExpression; |
| 163 | return length; |
Jim Ingham | 1624a2d | 2014-05-05 02:26:40 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 166 | //---------------------------------------------------------------------- |
| 167 | // Set accesssor for the error value and type. |
| 168 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 169 | void Error::SetError(ValueType err, ErrorType type) { |
| 170 | m_code = err; |
| 171 | m_type = type; |
| 172 | m_string.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | //---------------------------------------------------------------------- |
| 176 | // Update the error value to be "errno" and update the type to |
| 177 | // be "POSIX". |
| 178 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 179 | void Error::SetErrorToErrno() { |
| 180 | m_code = errno; |
| 181 | m_type = eErrorTypePOSIX; |
| 182 | m_string.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | //---------------------------------------------------------------------- |
| 186 | // Update the error value to be LLDB_GENERIC_ERROR and update the type |
| 187 | // to be "Generic". |
| 188 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | void Error::SetErrorToGenericError() { |
| 190 | m_code = LLDB_GENERIC_ERROR; |
| 191 | m_type = eErrorTypeGeneric; |
| 192 | m_string.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | //---------------------------------------------------------------------- |
| 196 | // Set accessor for the error string value for a specific error. |
| 197 | // This allows any string to be supplied as an error explanation. |
| 198 | // The error string value will remain until the error value is |
| 199 | // cleared or a new error value/type is assigned. |
| 200 | //---------------------------------------------------------------------- |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 201 | void Error::SetErrorString(llvm::StringRef err_str) { |
| 202 | if (!err_str.empty()) { |
| 203 | // If we have an error string, we should always at least have an error |
| 204 | // set to a generic value. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | if (Success()) |
| 206 | SetErrorToGenericError(); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 207 | } |
| 208 | m_string = err_str; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | //------------------------------------------------------------------ |
| 212 | /// Set the current error string to a formatted error string. |
| 213 | /// |
| 214 | /// @param format |
| 215 | /// A printf style format string |
| 216 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 217 | int Error::SetErrorStringWithFormat(const char *format, ...) { |
| 218 | if (format != nullptr && format[0]) { |
| 219 | va_list args; |
| 220 | va_start(args, format); |
| 221 | int length = SetErrorStringWithVarArg(format, args); |
| 222 | va_end(args); |
| 223 | return length; |
| 224 | } else { |
| 225 | m_string.clear(); |
| 226 | } |
| 227 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 230 | int Error::SetErrorStringWithVarArg(const char *format, va_list args) { |
| 231 | if (format != nullptr && format[0]) { |
| 232 | // If we have an error string, we should always at least have |
| 233 | // an error set to a generic value. |
| 234 | if (Success()) |
| 235 | SetErrorToGenericError(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 237 | // Try and fit our error into a 1024 byte buffer first... |
| 238 | llvm::SmallVector<char, 1024> buf; |
| 239 | buf.resize(1024); |
| 240 | // Copy in case our first call to vsnprintf doesn't fit into our |
| 241 | // allocated buffer above |
| 242 | va_list copy_args; |
| 243 | va_copy(copy_args, args); |
| 244 | unsigned length = ::vsnprintf(buf.data(), buf.size(), format, args); |
| 245 | if (length >= buf.size()) { |
| 246 | // The error formatted string didn't fit into our buffer, resize it |
| 247 | // to the exact needed size, and retry |
| 248 | buf.resize(length + 1); |
| 249 | length = ::vsnprintf(buf.data(), buf.size(), format, copy_args); |
| 250 | va_end(copy_args); |
| 251 | assert(length < buf.size()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 253 | m_string.assign(buf.data(), length); |
| 254 | va_end(args); |
| 255 | return length; |
| 256 | } else { |
| 257 | m_string.clear(); |
| 258 | } |
| 259 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 262 | //---------------------------------------------------------------------- |
| 263 | // Returns true if the error code in this object is considered a |
| 264 | // successful return value. |
| 265 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 266 | bool Error::Success() const { return m_code == 0; } |
Jim Ingham | 4e5c821 | 2013-06-07 22:09:53 +0000 | [diff] [blame] | 267 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 268 | bool Error::WasInterrupted() const { |
| 269 | return (m_type == eErrorTypePOSIX && m_code == EINTR); |
Jim Ingham | 4e5c821 | 2013-06-07 22:09:53 +0000 | [diff] [blame] | 270 | } |
Zachary Turner | 33aba3c | 2017-02-06 18:31:44 +0000 | [diff] [blame^] | 271 | |
| 272 | void llvm::format_provider<lldb_private::Error>::format( |
| 273 | const lldb_private::Error &error, llvm::raw_ostream &OS, |
| 274 | llvm::StringRef Options) { |
| 275 | llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS, |
| 276 | Options); |
| 277 | } |