1 //===-- Status.cpp -----------------------------------------------*- C++ 2 //-*-===// 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 11 #include "lldb/Utility/Status.h" 12 13 #include "lldb/Utility/VASPrintf.h" 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/FormatProviders.h" // for format_provider 19 20 #include <cerrno> 21 #include <cstdarg> 22 #include <string> // for string 23 #include <system_error> 24 25 #ifdef __APPLE__ 26 #include <mach/mach.h> 27 #endif 28 29 #include <stdint.h> // for uint32_t 30 #include <string.h> // for strerror 31 32 namespace llvm { 33 class raw_ostream; 34 } 35 36 using namespace lldb; 37 using namespace lldb_private; 38 39 Status::Status() : m_code(0), m_type(eErrorTypeInvalid), m_string() {} 40 41 Status::Status(ValueType err, ErrorType type) 42 : m_code(err), m_type(type), m_string() {} 43 44 Status::Status(std::error_code EC) 45 : m_code(EC.value()), m_type(ErrorType::eErrorTypeGeneric), 46 m_string(EC.message()) {} 47 48 Status::Status(const Status &rhs) = default; 49 50 Status::Status(const char *format, ...) 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); 57 } 58 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 90 //---------------------------------------------------------------------- 91 // Assignment operator 92 //---------------------------------------------------------------------- 93 const Status &Status::operator=(const Status &rhs) { 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; 100 } 101 102 //---------------------------------------------------------------------- 103 // Assignment operator 104 //---------------------------------------------------------------------- 105 const Status &Status::operator=(uint32_t err) { 106 m_code = err; 107 m_type = eErrorTypeMachKernel; 108 m_string.clear(); 109 return *this; 110 } 111 112 Status::~Status() = default; 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 //---------------------------------------------------------------------- 119 const char *Status::AsCString(const char *default_error_str) const { 120 if (Success()) 121 return nullptr; 122 123 if (m_string.empty()) { 124 const char *s = nullptr; 125 switch (m_type) { 126 case eErrorTypeMachKernel: 127 #if defined(__APPLE__) 128 s = ::mach_error_string(m_code); 129 #endif 130 break; 131 132 case eErrorTypePOSIX: 133 s = ::strerror(m_code); 134 break; 135 136 default: 137 break; 138 } 139 if (s != nullptr) 140 m_string.assign(s); 141 } 142 if (m_string.empty()) { 143 if (default_error_str) 144 m_string.assign(default_error_str); 145 else 146 return nullptr; // User wanted a nullptr string back... 147 } 148 return m_string.c_str(); 149 } 150 151 //---------------------------------------------------------------------- 152 // Clear the error and any cached error string that it might contain. 153 //---------------------------------------------------------------------- 154 void Status::Clear() { 155 m_code = 0; 156 m_type = eErrorTypeInvalid; 157 m_string.clear(); 158 } 159 160 //---------------------------------------------------------------------- 161 // Access the error value. 162 //---------------------------------------------------------------------- 163 Status::ValueType Status::GetError() const { return m_code; } 164 165 //---------------------------------------------------------------------- 166 // Access the error type. 167 //---------------------------------------------------------------------- 168 ErrorType Status::GetType() const { return m_type; } 169 170 //---------------------------------------------------------------------- 171 // Returns true if this object contains a value that describes an 172 // error or otherwise non-success result. 173 //---------------------------------------------------------------------- 174 bool Status::Fail() const { return m_code != 0; } 175 176 //---------------------------------------------------------------------- 177 // Set accesssor for the error value to "err" and the type to 178 // "eErrorTypeMachKernel" 179 //---------------------------------------------------------------------- 180 void Status::SetMachError(uint32_t err) { 181 m_code = err; 182 m_type = eErrorTypeMachKernel; 183 m_string.clear(); 184 } 185 186 void Status::SetExpressionError(lldb::ExpressionResults result, 187 const char *mssg) { 188 m_code = result; 189 m_type = eErrorTypeExpression; 190 m_string = mssg; 191 } 192 193 int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result, 194 const char *format, ...) { 195 int length = 0; 196 197 if (format != nullptr && format[0]) { 198 va_list args; 199 va_start(args, format); 200 length = SetErrorStringWithVarArg(format, args); 201 va_end(args); 202 } else { 203 m_string.clear(); 204 } 205 m_code = result; 206 m_type = eErrorTypeExpression; 207 return length; 208 } 209 210 //---------------------------------------------------------------------- 211 // Set accesssor for the error value and type. 212 //---------------------------------------------------------------------- 213 void Status::SetError(ValueType err, ErrorType type) { 214 m_code = err; 215 m_type = type; 216 m_string.clear(); 217 } 218 219 //---------------------------------------------------------------------- 220 // Update the error value to be "errno" and update the type to 221 // be "POSIX". 222 //---------------------------------------------------------------------- 223 void Status::SetErrorToErrno() { 224 m_code = errno; 225 m_type = eErrorTypePOSIX; 226 m_string.clear(); 227 } 228 229 //---------------------------------------------------------------------- 230 // Update the error value to be LLDB_GENERIC_ERROR and update the type 231 // to be "Generic". 232 //---------------------------------------------------------------------- 233 void Status::SetErrorToGenericError() { 234 m_code = LLDB_GENERIC_ERROR; 235 m_type = eErrorTypeGeneric; 236 m_string.clear(); 237 } 238 239 //---------------------------------------------------------------------- 240 // Set accessor for the error string value for a specific error. 241 // This allows any string to be supplied as an error explanation. 242 // The error string value will remain until the error value is 243 // cleared or a new error value/type is assigned. 244 //---------------------------------------------------------------------- 245 void Status::SetErrorString(llvm::StringRef err_str) { 246 if (!err_str.empty()) { 247 // If we have an error string, we should always at least have an error 248 // set to a generic value. 249 if (Success()) 250 SetErrorToGenericError(); 251 } 252 m_string = err_str; 253 } 254 255 //------------------------------------------------------------------ 256 /// Set the current error string to a formatted error string. 257 /// 258 /// @param format 259 /// A printf style format string 260 //------------------------------------------------------------------ 261 int Status::SetErrorStringWithFormat(const char *format, ...) { 262 if (format != nullptr && format[0]) { 263 va_list args; 264 va_start(args, format); 265 int length = SetErrorStringWithVarArg(format, args); 266 va_end(args); 267 return length; 268 } else { 269 m_string.clear(); 270 } 271 return 0; 272 } 273 274 int Status::SetErrorStringWithVarArg(const char *format, va_list args) { 275 if (format != nullptr && format[0]) { 276 // If we have an error string, we should always at least have 277 // an error set to a generic value. 278 if (Success()) 279 SetErrorToGenericError(); 280 281 llvm::SmallString<1024> buf; 282 VASprintf(buf, format, args); 283 m_string = buf.str(); 284 return buf.size(); 285 } else { 286 m_string.clear(); 287 } 288 return 0; 289 } 290 291 //---------------------------------------------------------------------- 292 // Returns true if the error code in this object is considered a 293 // successful return value. 294 //---------------------------------------------------------------------- 295 bool Status::Success() const { return m_code == 0; } 296 297 bool Status::WasInterrupted() const { 298 return (m_type == eErrorTypePOSIX && m_code == EINTR); 299 } 300 301 void llvm::format_provider<lldb_private::Status>::format( 302 const lldb_private::Status &error, llvm::raw_ostream &OS, 303 llvm::StringRef Options) { 304 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS, 305 Options); 306 } 307