1 //===-- Error.h -------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #if defined(__cplusplus)
14 #include "llvm/Support/DataTypes.h"
15 #include "llvm/Support/FormatVariadic.h"
21 #include "lldb/lldb-private.h"
23 #include "llvm/Support/FormatVariadic.h"
25 namespace lldb_private {
29 //----------------------------------------------------------------------
30 /// @class Error Error.h "lldb/Core/Error.h"
31 /// @brief An error handling class.
33 /// This class is designed to be able to hold any error code that can be
34 /// encountered on a given platform. The errors are stored as a value
35 /// of type Error::ValueType. This value should be large enough to hold
36 /// any and all errors that the class supports. Each error has an
37 /// associated type that is of type lldb::ErrorType. New types
38 /// can be added to support new error types, and architecture specific
39 /// types can be enabled. In the future we may wish to switch to a
40 /// registration mechanism where new error types can be registered at
41 /// runtime instead of a hard coded scheme.
43 /// All errors in this class also know how to generate a string
44 /// representation of themselves for printing results and error codes.
45 /// The string value will be fetched on demand and its string value will
46 /// be cached until the error is cleared of the value of the error
48 //----------------------------------------------------------------------
51 //------------------------------------------------------------------
52 /// Every error value that this object can contain needs to be able
53 /// to fit into ValueType.
54 //------------------------------------------------------------------
55 typedef uint32_t ValueType;
57 //------------------------------------------------------------------
58 /// Default constructor.
60 /// Initialize the error object with a generic success value.
66 /// The type for \a err.
67 //------------------------------------------------------------------
70 explicit Error(ValueType err, lldb::ErrorType type = lldb::eErrorTypeGeneric);
72 explicit Error(const char *format, ...) __attribute__((format(printf, 2, 3)));
74 Error(const Error &rhs);
75 //------------------------------------------------------------------
76 /// Assignment operator.
82 /// A const reference to this object.
83 //------------------------------------------------------------------
84 const Error &operator=(const Error &rhs);
86 //------------------------------------------------------------------
87 /// Assignment operator from a kern_return_t.
89 /// Sets the type to \c MachKernel and the error code to \a err.
92 /// A mach error code.
95 /// A const reference to this object.
96 //------------------------------------------------------------------
97 const Error &operator=(uint32_t err);
101 //------------------------------------------------------------------
102 /// Get the error string associated with the current error.
104 /// Gets the error value as a NULL terminated C string. The error
105 /// string will be fetched and cached on demand. The error string
106 /// will be retrieved from a callback that is appropriate for the
107 /// type of the error and will be cached until the error value is
108 /// changed or cleared.
111 /// The error as a NULL terminated C string value if the error
112 /// is valid and is able to be converted to a string value,
114 //------------------------------------------------------------------
115 const char *AsCString(const char *default_error_str = "unknown error") const;
117 //------------------------------------------------------------------
118 /// Clear the object state.
120 /// Reverts the state of this object to contain a generic success
121 /// value and frees any cached error string value.
122 //------------------------------------------------------------------
125 //------------------------------------------------------------------
126 /// Test for error condition.
129 /// \b true if this object contains an error, \b false
131 //------------------------------------------------------------------
134 //------------------------------------------------------------------
135 /// Access the error value.
139 //------------------------------------------------------------------
140 ValueType GetError() const;
142 //------------------------------------------------------------------
143 /// Access the error type.
146 /// The error type enumeration value.
147 //------------------------------------------------------------------
148 lldb::ErrorType GetType() const;
150 //------------------------------------------------------------------
151 /// Log an error to Log().
153 /// Log the error given a formatted string \a format. If the this
154 /// object contains an error code, update the error string to
155 /// contain the prefix "error: ", followed by the formatted string,
156 /// followed by the error value and any string that describes the
157 /// error value. This allows more context to be given to an error
158 /// string that remains cached in this object. Logging always occurs
159 /// even when the error code contains a non-error value.
161 /// @param[in] format
162 /// A printf style format string.
165 /// Variable arguments that are needed for the printf style
166 /// format string \a format.
167 //------------------------------------------------------------------
168 void PutToLog(Log *log, const char *format, ...)
169 __attribute__((format(printf, 3, 4)));
171 //------------------------------------------------------------------
172 /// Log an error to Log() if the error value is an error.
174 /// Log the error given a formatted string \a format only if the
175 /// error value in this object describes an error condition. If the
176 /// this object contains an error, update the error string to
177 /// contain the prefix "error: " followed by the formatted string,
178 /// followed by the error value and any string that describes the
179 /// error value. This allows more context to be given to an error
180 /// string that remains cached in this object.
182 /// @param[in] format
183 /// A printf style format string.
186 /// Variable arguments that are needed for the printf style
187 /// format string \a format.
188 //------------------------------------------------------------------
189 void LogIfError(Log *log, const char *format, ...)
190 __attribute__((format(printf, 3, 4)));
192 //------------------------------------------------------------------
193 /// Set accessor from a kern_return_t.
195 /// Set accesssor for the error value to \a err and the error type
196 /// to \c MachKernel.
199 /// A mach error code.
200 //------------------------------------------------------------------
201 void SetMachError(uint32_t err);
203 void SetExpressionError(lldb::ExpressionResults, const char *mssg);
205 int SetExpressionErrorWithFormat(lldb::ExpressionResults, const char *format,
206 ...) __attribute__((format(printf, 3, 4)));
208 //------------------------------------------------------------------
209 /// Set accesssor with an error value and type.
211 /// Set accesssor for the error value to \a err and the error type
215 /// A mach error code.
218 /// The type for \a err.
219 //------------------------------------------------------------------
220 void SetError(ValueType err, lldb::ErrorType type);
222 //------------------------------------------------------------------
223 /// Set the current error to errno.
225 /// Update the error value to be \c errno and update the type to
226 /// be \c Error::POSIX.
227 //------------------------------------------------------------------
228 void SetErrorToErrno();
230 //------------------------------------------------------------------
231 /// Set the current error to a generic error.
233 /// Update the error value to be \c LLDB_GENERIC_ERROR and update the
234 /// type to be \c Error::Generic.
235 //------------------------------------------------------------------
236 void SetErrorToGenericError();
238 //------------------------------------------------------------------
239 /// Set the current error string to \a err_str.
241 /// Set accessor for the error string value for a generic errors,
242 /// or to supply additional details above and beyond the standard
243 /// error strings that the standard type callbacks typically
244 /// provide. This allows custom strings to be supplied as an
245 /// error explanation. The error string value will remain until the
246 /// error value is cleared or a new error value/type is assigned.
249 /// The new custom error string to copy and cache.
250 //------------------------------------------------------------------
251 void SetErrorString(llvm::StringRef err_str);
253 //------------------------------------------------------------------
254 /// Set the current error string to a formatted error string.
257 /// A printf style format string
258 //------------------------------------------------------------------
259 int SetErrorStringWithFormat(const char *format, ...)
260 __attribute__((format(printf, 2, 3)));
262 int SetErrorStringWithVarArg(const char *format, va_list args);
264 template <typename... Args>
265 void SetErrorStringWithFormatv(const char *format, Args &&... args) {
266 SetErrorString(llvm::formatv(format, std::forward<Args>(args)...).str());
269 //------------------------------------------------------------------
270 /// Test for success condition.
272 /// Returns true if the error code in this object is considered a
273 /// successful return value.
276 /// \b true if this object contains an value that describes
277 /// success (non-erro), \b false otherwise.
278 //------------------------------------------------------------------
279 bool Success() const;
281 //------------------------------------------------------------------
282 /// Test for a failure due to a generic interrupt.
284 /// Returns true if the error code in this object was caused by an interrupt.
285 /// At present only supports Posix EINTR.
288 /// \b true if this object contains an value that describes
289 /// failure due to interrupt, \b false otherwise.
290 //------------------------------------------------------------------
291 bool WasInterrupted() const;
294 //------------------------------------------------------------------
296 //------------------------------------------------------------------
297 ValueType m_code; ///< Error code as an integer value.
298 lldb::ErrorType m_type; ///< The type of the above error code.
299 mutable std::string m_string; ///< A string representation of the error code.
302 } // namespace lldb_private
305 template <> struct format_provider<lldb_private::Error> {
306 static void format(const lldb_private::Error &error, llvm::raw_ostream &OS,
307 llvm::StringRef Options) {
308 llvm::format_provider<llvm::StringRef>::format(error.AsCString(), OS,
314 #endif // #if defined(__cplusplus)
315 #endif // #ifndef __DCError_h__