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/Errno.h" 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 26 #ifdef __APPLE__ 27 #include <mach/mach.h> 28 #endif 29 30 #include <stdint.h> // for uint32_t 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 switch (m_type) { 125 case eErrorTypeMachKernel: 126 #if defined(__APPLE__) 127 if (const char *s = ::mach_error_string(m_code)) 128 m_string.assign(s); 129 #endif 130 break; 131 132 case eErrorTypePOSIX: 133 m_string = llvm::sys::StrError(m_code); 134 break; 135 136 default: 137 break; 138 } 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(); 147 } 148 149 //---------------------------------------------------------------------- 150 // Clear the error and any cached error string that it might contain. 151 //---------------------------------------------------------------------- 152 void Status::Clear() { 153 m_code = 0; 154 m_type = eErrorTypeInvalid; 155 m_string.clear(); 156 } 157 158 //---------------------------------------------------------------------- 159 // Access the error value. 160 //---------------------------------------------------------------------- 161 Status::ValueType Status::GetError() const { return m_code; } 162 163 //---------------------------------------------------------------------- 164 // Access the error type. 165 //---------------------------------------------------------------------- 166 ErrorType Status::GetType() const { return m_type; } 167 168 //---------------------------------------------------------------------- 169 // Returns true if this object contains a value that describes an 170 // error or otherwise non-success result. 171 //---------------------------------------------------------------------- 172 bool Status::Fail() const { return m_code != 0; } 173 174 //---------------------------------------------------------------------- 175 // Set accesssor for the error value to "err" and the type to 176 // "eErrorTypeMachKernel" 177 //---------------------------------------------------------------------- 178 void Status::SetMachError(uint32_t err) { 179 m_code = err; 180 m_type = eErrorTypeMachKernel; 181 m_string.clear(); 182 } 183 184 void Status::SetExpressionError(lldb::ExpressionResults result, 185 const char *mssg) { 186 m_code = result; 187 m_type = eErrorTypeExpression; 188 m_string = mssg; 189 } 190 191 int Status::SetExpressionErrorWithFormat(lldb::ExpressionResults result, 192 const char *format, ...) { 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 { 201 m_string.clear(); 202 } 203 m_code = result; 204 m_type = eErrorTypeExpression; 205 return length; 206 } 207 208 //---------------------------------------------------------------------- 209 // Set accesssor for the error value and type. 210 //---------------------------------------------------------------------- 211 void Status::SetError(ValueType err, ErrorType type) { 212 m_code = err; 213 m_type = type; 214 m_string.clear(); 215 } 216 217 //---------------------------------------------------------------------- 218 // Update the error value to be "errno" and update the type to 219 // be "POSIX". 220 //---------------------------------------------------------------------- 221 void Status::SetErrorToErrno() { 222 m_code = errno; 223 m_type = eErrorTypePOSIX; 224 m_string.clear(); 225 } 226 227 //---------------------------------------------------------------------- 228 // Update the error value to be LLDB_GENERIC_ERROR and update the type 229 // to be "Generic". 230 //---------------------------------------------------------------------- 231 void Status::SetErrorToGenericError() { 232 m_code = LLDB_GENERIC_ERROR; 233 m_type = eErrorTypeGeneric; 234 m_string.clear(); 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 //---------------------------------------------------------------------- 243 void Status::SetErrorString(llvm::StringRef err_str) { 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. 247 if (Success()) 248 SetErrorToGenericError(); 249 } 250 m_string = err_str; 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 //------------------------------------------------------------------ 259 int Status::SetErrorStringWithFormat(const char *format, ...) { 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; 270 } 271 272 int Status::SetErrorStringWithVarArg(const char *format, va_list args) { 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(); 278 279 llvm::SmallString<1024> buf; 280 VASprintf(buf, format, args); 281 m_string = buf.str(); 282 return buf.size(); 283 } else { 284 m_string.clear(); 285 } 286 return 0; 287 } 288 289 //---------------------------------------------------------------------- 290 // Returns true if the error code in this object is considered a 291 // successful return value. 292 //---------------------------------------------------------------------- 293 bool Status::Success() const { return m_code == 0; } 294 295 bool Status::WasInterrupted() const { 296 return (m_type == eErrorTypePOSIX && m_code == EINTR); 297 } 298 299 void llvm::format_provider<lldb_private::Status>::format( 300 const lldb_private::Status &error, llvm::raw_ostream &OS, 301 llvm::StringRef Options) { 302 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS, 303 Options); 304 } 305