File indexing completed on 2026-05-10 08:42:58
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLDB_UTILITY_STATUS_H
0010 #define LLDB_UTILITY_STATUS_H
0011
0012 #include "lldb/Utility/FileSpec.h"
0013 #include "lldb/Utility/StructuredData.h"
0014 #include "lldb/lldb-defines.h"
0015 #include "lldb/lldb-enumerations.h"
0016 #include "llvm/ADT/StringRef.h"
0017 #include "llvm/Support/Error.h"
0018 #include "llvm/Support/FormatVariadic.h"
0019 #include <cstdarg>
0020 #include <cstdint>
0021 #include <string>
0022 #include <system_error>
0023 #include <type_traits>
0024
0025 namespace llvm {
0026 class raw_ostream;
0027 }
0028
0029 namespace lldb_private {
0030
0031
0032
0033
0034
0035 class CloneableError
0036 : public llvm::ErrorInfo<CloneableError, llvm::ErrorInfoBase> {
0037 public:
0038 using llvm::ErrorInfo<CloneableError, llvm::ErrorInfoBase>::ErrorInfo;
0039 CloneableError() : ErrorInfo() {}
0040 virtual std::unique_ptr<CloneableError> Clone() const = 0;
0041 virtual lldb::ErrorType GetErrorType() const = 0;
0042 virtual StructuredData::ObjectSP GetAsStructuredData() const = 0;
0043 static char ID;
0044 };
0045
0046
0047 class CloneableECError
0048 : public llvm::ErrorInfo<CloneableECError, CloneableError> {
0049 public:
0050 using llvm::ErrorInfo<CloneableECError, CloneableError>::ErrorInfo;
0051 std::error_code convertToErrorCode() const override { return EC; }
0052 void log(llvm::raw_ostream &OS) const override { OS << EC.message(); }
0053 lldb::ErrorType GetErrorType() const override;
0054 virtual StructuredData::ObjectSP GetAsStructuredData() const override;
0055 static char ID;
0056
0057 protected:
0058 CloneableECError() = delete;
0059 CloneableECError(std::error_code ec) : ErrorInfo(), EC(ec) {}
0060 std::error_code EC;
0061 };
0062
0063 class MachKernelError
0064 : public llvm::ErrorInfo<MachKernelError, CloneableECError> {
0065 public:
0066 using llvm::ErrorInfo<MachKernelError, CloneableECError>::ErrorInfo;
0067 MachKernelError(std::error_code ec) : ErrorInfo(ec) {}
0068 std::string message() const override;
0069 std::unique_ptr<CloneableError> Clone() const override;
0070 lldb::ErrorType GetErrorType() const override;
0071 static char ID;
0072 };
0073
0074 class Win32Error : public llvm::ErrorInfo<Win32Error, CloneableECError> {
0075 public:
0076 using llvm::ErrorInfo<Win32Error, CloneableECError>::ErrorInfo;
0077 Win32Error(std::error_code ec, const llvm::Twine &msg = {}) : ErrorInfo(ec) {}
0078 std::string message() const override;
0079 std::unique_ptr<CloneableError> Clone() const override;
0080 lldb::ErrorType GetErrorType() const override;
0081 static char ID;
0082 };
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 class Status {
0119 public:
0120
0121 typedef uint32_t ValueType;
0122
0123 Status();
0124 Status(Status &&other) = default;
0125
0126
0127
0128
0129
0130
0131
0132
0133 explicit Status(ValueType err, lldb::ErrorType type = lldb::eErrorTypeGeneric,
0134 std::string msg = {});
0135
0136 Status(std::error_code EC);
0137
0138
0139 explicit Status(std::string err_str);
0140
0141 static Status FromErrorString(const char *str) {
0142 if (str)
0143 return Status(std::string(str));
0144 return Status(std::string("null error"));
0145 }
0146
0147 static Status FromErrorStringWithFormat(const char *format, ...)
0148 __attribute__((format(printf, 1, 2)));
0149
0150 template <typename... Args>
0151 static Status FromErrorStringWithFormatv(const char *format, Args &&...args) {
0152 return Status(llvm::formatv(format, std::forward<Args>(args)...));
0153 }
0154
0155
0156
0157
0158
0159 static Status FromErrno();
0160
0161 ~Status();
0162
0163 const Status &operator=(Status &&);
0164
0165 static Status FromError(llvm::Error error);
0166
0167
0168 llvm::Error ToError() const;
0169
0170 llvm::Error takeError() { return std::move(m_error); }
0171
0172
0173
0174 Status Clone() const { return Status(ToError()); }
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187 const char *AsCString(const char *default_error_str = "unknown error") const;
0188
0189
0190 StructuredData::ObjectSP GetAsStructuredData() const;
0191
0192
0193
0194
0195
0196 void Clear();
0197
0198
0199
0200
0201
0202
0203 bool Fail() const;
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213 ValueType GetError() const;
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223 lldb::ErrorType GetType() const;
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233 bool Success() const;
0234
0235 protected:
0236 Status(llvm::Error error) : m_error(std::move(error)) {}
0237 llvm::Error m_error;
0238
0239 mutable std::string m_string;
0240 };
0241
0242 }
0243
0244 namespace llvm {
0245 template <> struct format_provider<lldb_private::Status> {
0246 static void format(const lldb_private::Status &error, llvm::raw_ostream &OS,
0247 llvm::StringRef Options);
0248 };
0249 }
0250
0251 #define LLDB_ERRORF(status, fmt, ...) \
0252 do { \
0253 if (status) { \
0254 (status)->SetErrorStringWithFormat((fmt), __VA_ARGS__); \
0255 } \
0256 } while (0);
0257
0258 #endif