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