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