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