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