Text of lldb.llvm.org › cpp_reference › Status_8cpp_source.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

LLDB: Status.cpp Source File

LLDB mainline llvm-project lldb source Utility Status.cpp Go to the documentation of this file. 1 //===-- Status.cpp --------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include " lldb/Utility/Status.h " 10 11 #include " lldb/Utility/LLDBLog.h " 12 #include " lldb/Utility/Log.h " 13 #include " lldb/Utility/VASPrintf.h " 14 #include " lldb/lldb-defines.h " 15 #include " lldb/lldb-enumerations.h " 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/Support/Errno.h" 19 #include "llvm/Support/FormatProviders.h" 20 21 #include <cerrno> 22 #include <cstdarg> 23 #include <string> 24 #include <system_error> 25 26 #ifdef __APPLE__ 27 #include <mach/mach.h> 28 #endif 29 30 #ifdef _WIN32 31 #include < windows.h > 32 #endif 33 #include <cstdint> 34 35 namespace llvm { 36 class raw_ostream; 37 } 38 39 using namespace lldb ; 40 using namespace lldb_private ; 41 42 char CloneableError::ID ; 43 char CloneableECError::ID ; 44 char MachKernelError::ID ; 45 char Win32Error::ID ; 46 47 namespace { 48 /// A std::error_code category for eErrorTypeGeneric.

49 class LLDBGenericCategory : public std::error_category { 50 const char *name() const noexcept override { return "LLDBGenericCategory" ; } 51 std::string message( int __ev) const override { return "generic LLDB error" ; }; 52 }; 53 LLDBGenericCategory &lldb_generic_category() { 54 static LLDBGenericCategory g_generic_category; 55 return g_generic_category; 56 } 57 } // namespace 58 59 Status::Status () : m_error ( llvm ::Error::success()) {} 60 61 static llvm::Error ErrorFromEnums ( Status::ValueType err, ErrorType type, 62 std::string msg) { 63 switch (type) { 64 case eErrorTypeMachKernel : 65 return llvm::make_error<MachKernelError>( 66 std::error_code(err, std::system_category())); 67 case eErrorTypeWin32 : 68 #ifdef _WIN32 69 if (err == NO_ERROR) 70 return llvm::Error::success(); 71 #endif 72 return llvm::make_error<Win32Error>( 73 std::error_code(err, std::system_category())); 74 case eErrorTypePOSIX : 75 if (msg.empty()) 76 return llvm::errorCodeToError( 77 std::error_code(err, std::generic_category())); 78 return llvm::createStringError( 79 std::move(msg), std::error_code(err, std::generic_category())); 80 default : 81 return llvm::createStringError( 82 std::move(msg), std::error_code(err, lldb_generic_category())); 83 } 84 } 85 86 Status::Status ( ValueType err, ErrorType type, std::string msg) 87 : m_error ( ErrorFromEnums (err, type, msg)) {} 88 89 // This logic is confusing because C++ calls the traditional (posix) errno codes 90 // "generic errors", while we use the term "generic" to mean completely 91 // arbitrary (text-based) errors.

92 Status::Status (std::error_code EC) 93 : m_error (!EC ? llvm ::Error::success() : llvm ::errorCodeToError(EC)) {} 94 95 Status::Status (std::string err_str) 96 : m_error ( 97 llvm ::createStringError( llvm ::inconvertibleErrorCode(), err_str)) {} 98 99 const Status & Status::operator= ( Status &&other) { 100 Clear (); 101 llvm::consumeError(std::move( m_error )); 102 m_error = std::move(other.m_error); 103 return * this ; 104 } 105 106 Status Status::FromErrorStringWithFormat ( const char *format, ...) { 107 std::string string; 108 va_list args; 109 va_start(args, format); 110 if (format != nullptr && format[0]) { 111 llvm::SmallString<1024> buf; 112 VASprintf (buf, format, args); 113 string = std::string(buf.str()); 114 } 115 va_end(args); 116 return Status ( string ); 117 } 118 119 /// Creates a deep copy of all known errors and converts all other 120 /// errors to a new llvm::StringError.

121 static llvm::Error CloneError ( const llvm::Error & error ) { 122 llvm::Error result = llvm::Error::success(); 123 auto clone = []( const llvm::ErrorInfoBase &e) { 124 if (e.isA< CloneableError >()) 125 return llvm::Error( static_cast< const CloneableError & > (e).Clone()); 126 if (e.isA<llvm::ECError>()) 127 return llvm::errorCodeToError(e.convertToErrorCode()); 128 return llvm::createStringError(e.message(), e.convertToErrorCode()); 129 }; 130 llvm::visitErrors( error , [&]( const llvm::ErrorInfoBase &e) { 131 result = joinErrors(std::move(result), clone (e)); 132 }); 133 return result; 134 } 135 136 Status Status::FromError (llvm::Error error ) { return Status (std::move( error )); } 137 138 llvm::Error Status::ToError () const { return CloneError ( m_error ); } 139 140 Status::~Status () { llvm::consumeError(std::move( m_error )); } 141 142 #ifdef _WIN32 143 static std::string RetrieveWin32ErrorString(uint32_t error_code) { 144 char *buffer = nullptr ; 145 std::string message; 146 // Retrieve win32 system error.

147 // First, attempt to load a en-US message 148 if (::FormatMessageA( 149 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | 150 FORMAT_MESSAGE_MAX_WIDTH_MASK, 151 NULL, error_code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), 152 (LPSTR)&buffer, 0, NULL)) { 153 message.assign(buffer); 154 ::LocalFree(buffer); 155 } 156 // If the previous didn't work, use the default OS language 157 else if (::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | 158 FORMAT_MESSAGE_FROM_SYSTEM | 159 FORMAT_MESSAGE_MAX_WIDTH_MASK, 160 NULL, error_code, 0, (LPSTR)&buffer, 0, NULL)) { 161 message.assign(buffer); 162 ::LocalFree(buffer); 163 } 164 return message; 165 } 166 #endif 167 168 std::string MachKernelError::message () const { 169 #if defined(__APPLE__) 170 if ( const char *s = ::mach_error_string(convertToErrorCode().value())) 171 return s; 172 #endif 173 return "MachKernelError" ; 174 } 175 176 std::string Win32Error::message () const { 177 #if defined(_WIN32) 178 return RetrieveWin32ErrorString(convertToErrorCode().value()); 179 #endif 180 return "Win32Error" ; 181 } 182 183 std::unique_ptr<CloneableError> MachKernelError::Clone () const { 184 return std::make_unique<MachKernelError>(convertToErrorCode()); 185 } 186 187 std::unique_ptr<CloneableError> Win32Error::Clone () const { 188 return std::make_unique<Win32Error>(convertToErrorCode()); 189 } 190 191 // Get the error value as a NULL C string.

The error string will be fetched and 192 // cached on demand. The cached error string value will remain until the error 193 // value is changed or cleared. 194 const char * Status::AsCString ( const char *default_error_str) const { 195 if ( Success ()) 196 return nullptr ; 197 198 m_string = llvm::toStringWithoutConsuming( m_error ); 199 // Backwards compatibility with older implementations of Status. 200 if ( m_error .isA<llvm::ECError>()) 201 if (! m_string .empty() && m_string [ m_string .size() - 1] == '\n' ) 202 m_string .pop_back(); 203 204 if ( m_string .empty()) { 205 if (default_error_str) 206 m_string .assign(default_error_str); 207 else 208 return nullptr ; // User wanted a nullptr string back...

209 } 210 return m_string .c_str(); 211 } 212 213 // Clear the error and any cached error string that it might contain. 214 void Status::Clear () { 215 if ( m_error ) 216 LLDB_LOG_ERRORV ( GetLog ( LLDBLog::API ), std::move( m_error ), 217 "dropping error {0}" ); 218 m_error = llvm::Error::success(); 219 } 220 221 Status::ValueType Status::GetError () const { 222 Status::ValueType result = 0; 223 llvm::visitErrors( m_error , [&]( const llvm::ErrorInfoBase & error ) { 224 // Return the first only.

225 if (result) 226 return ; 227 std::error_code ec = error .convertToErrorCode(); 228 result = ec.value(); 229 }); 230 return result; 231 } 232 233 static ErrorType ErrorCodeToErrorType (std::error_code ec) { 234 if (ec.category() == std::generic_category()) 235 return eErrorTypePOSIX ; 236 if (ec.category() == lldb_generic_category() || 237 ec == llvm::inconvertibleErrorCode()) 238 return eErrorTypeGeneric ; 239 return eErrorTypeInval

The rest of this page is on the live site.