16#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/Errno.h"
19#include "llvm/Support/FormatProviders.h"
24#include <system_error>
49class 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"; };
53LLDBGenericCategory &lldb_generic_category() {
54 static LLDBGenericCategory g_generic_category;
55 return g_generic_category;
65 return llvm::make_error<MachKernelError>(
66 std::error_code(err, std::system_category()));
70 return llvm::Error::success();
72 return llvm::make_error<Win32Error>(
73 std::error_code(err, std::system_category()));
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()));
81 return llvm::createStringError(
82 std::move(msg), std::error_code(err, lldb_generic_category()));
93 : m_error(!EC ?
llvm::
Error::success() :
llvm::errorCodeToError(EC)) {}
97 llvm::createStringError(
llvm::inconvertibleErrorCode(), err_str)) {}
101 llvm::consumeError(std::move(
m_error));
102 m_error = std::move(other.m_error);
109 va_start(args, format);
110 if (format !=
nullptr && format[0]) {
111 llvm::SmallString<1024> buf;
113 string = std::string(buf.str());
122 llvm::Error result = llvm::Error::success();
123 auto clone = [](
const llvm::ErrorInfoBase &e) {
126 if (e.isA<llvm::ECError>())
127 return llvm::errorCodeToError(e.convertToErrorCode());
128 return llvm::make_error<llvm::StringError>(e.message(),
129 e.convertToErrorCode(),
true);
131 llvm::visitErrors(
error, [&](
const llvm::ErrorInfoBase &e) {
132 result = joinErrors(std::move(result),
clone(e));
144static std::string RetrieveWin32ErrorString(uint32_t error_code) {
145 char *buffer =
nullptr;
149 if (::FormatMessageA(
150 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
151 FORMAT_MESSAGE_MAX_WIDTH_MASK,
152 NULL, error_code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
153 (LPSTR)&buffer, 0, NULL)) {
154 message.assign(buffer);
158 else if (::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
159 FORMAT_MESSAGE_FROM_SYSTEM |
160 FORMAT_MESSAGE_MAX_WIDTH_MASK,
161 NULL, error_code, 0, (LPSTR)&buffer, 0, NULL)) {
162 message.assign(buffer);
170#if defined(__APPLE__)
171 if (
const char *s = ::mach_error_string(convertToErrorCode().value()))
174 return "MachKernelError";
179 return RetrieveWin32ErrorString(convertToErrorCode().value());
185 return std::make_unique<MachKernelError>(convertToErrorCode());
189 return std::make_unique<Win32Error>(convertToErrorCode());
201 if (
m_error.isA<llvm::ECError>())
206 if (default_error_str)
218 "dropping error {0}");
219 m_error = llvm::Error::success();
224 llvm::visitErrors(
m_error, [&](
const llvm::ErrorInfoBase &
error) {
228 std::error_code ec =
error.convertToErrorCode();
235 if (ec.category() == std::generic_category())
237 if (ec.category() == lldb_generic_category() ||
238 ec == llvm::inconvertibleErrorCode())
256 auto dict_up = std::make_unique<StructuredData::Dictionary>();
257 auto array_up = std::make_unique<StructuredData::Array>();
258 llvm::visitErrors(
m_error, [&](
const llvm::ErrorInfoBase &
error) {
263 array_up->AddStringItem(
error.message());
265 dict_up->AddIntegerItem(
"version", 1u);
266 dict_up->AddIntegerItem(
"type", (
unsigned)
GetType());
267 dict_up->AddItem(
"errors", std::move(array_up));
272 auto dict_up = std::make_unique<StructuredData::Dictionary>();
273 dict_up->AddIntegerItem(
"version", 1u);
274 dict_up->AddIntegerItem(
"error_code",
EC.value());
275 dict_up->AddStringItem(
"message", message());
281 llvm::visitErrors(
m_error, [&](
const llvm::ErrorInfoBase &
error) {
297 return m_error.isA<llvm::ErrorInfoBase>();
306void llvm::format_provider<lldb_private::Status>::format(
309 llvm::format_provider<llvm::StringRef>::format(
error.AsCString(),
OS,
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG_ERRORV(log, error,...)
static llvm::Error CloneError(const llvm::Error &error)
Creates a deep copy of all known errors and converts all other errors to a new llvm::StringError.
static llvm::Error ErrorFromEnums(Status::ValueType err, ErrorType type, std::string msg)
static ErrorType ErrorCodeToErrorType(std::error_code ec)
lldb::ErrorType GetErrorType() const override
virtual StructuredData::ObjectSP GetAsStructuredData() const override
Going a bit against the spirit of llvm::Error, lldb_private::Status need to store errors long-term an...
virtual std::unique_ptr< CloneableError > Clone() const =0
virtual StructuredData::ObjectSP GetAsStructuredData() const =0
virtual lldb::ErrorType GetErrorType() const =0
std::string message() const override
std::unique_ptr< CloneableError > Clone() const override
lldb::ErrorType GetErrorType() const override
A command line option parsing protocol class.
uint32_t ValueType
into ValueType.
void Clear()
Clear the object state.
static Status FromErrno()
Set the current error to errno.
lldb::ErrorType GetType() const
Access the error type.
ValueType GetError() const
Access the error value.
llvm::Error ToError() const
FIXME: Replace all uses with takeError() instead.
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
bool Fail() const
Test for error condition.
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
const Status & operator=(Status &&)
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
bool Success() const
Test for success condition.
std::string m_string
TODO: Replace this with just calling toString(m_error).
StructuredData::ObjectSP GetAsStructuredData() const
Get the error in machine-readable form.
std::shared_ptr< Object > ObjectSP
std::string message() const override
lldb::ErrorType GetErrorType() const override
std::unique_ptr< CloneableError > Clone() const override
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
bool VASprintf(llvm::SmallVectorImpl< char > &buf, const char *fmt, va_list args)
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
@ eErrorTypeGeneric
Generic errors that can be any value.
@ eErrorTypeWin32
Standard Win32 error codes.
@ eErrorTypeMachKernel
Mach kernel error codes.
@ eErrorTypePOSIX
POSIX error codes.