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"
20 #include "lldb/lldb-private.h"
22 namespace lldb_private {
26 //----------------------------------------------------------------------
27 /// @class Error Error.h "lldb/Core/Error.h"
28 /// @brief An error handling class.
30 /// This class is designed to be able to hold any error code that can be
31 /// encountered on a given platform. The errors are stored as a value
32 /// of type Error::ValueType. This value should be large enough to hold
33 /// any and all errors that the class supports. Each error has an
34 /// associated type that is of type lldb::ErrorType. New types
35 /// can be added to support new error types, and architecture specific
36 /// types can be enabled. In the future we may wish to switch to a
37 /// registration mechanism where new error types can be registered at
38 /// runtime instead of a hard coded scheme.
40 /// All errors in this class also know how to generate a string
41 /// representation of themselves for printing results and error codes.
42 /// The string value will be fetched on demand and its string value will
43 /// be cached until the error is cleared of the value of the error
45 //----------------------------------------------------------------------
49 //------------------------------------------------------------------
50 /// Every error value that this object can contain needs to be able
51 /// to fit into ValueType.
52 //------------------------------------------------------------------
53 typedef uint32_t ValueType;
55 //------------------------------------------------------------------
56 /// Default constructor.
58 /// Initialize the error object with a generic success value.
64 /// The type for \a err.
65 //------------------------------------------------------------------
69 Error (ValueType err, lldb::ErrorType type = lldb::eErrorTypeGeneric);
72 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 //------------------------------------------------------------------
85 operator = (const Error& rhs);
88 //------------------------------------------------------------------
89 /// Assignment operator from a kern_return_t.
91 /// Sets the type to \c MachKernel and the error code to \a err.
94 /// A mach error code.
97 /// A const reference to this object.
98 //------------------------------------------------------------------
100 operator = (uint32_t err);
104 //------------------------------------------------------------------
105 /// Get the error string associated with the current error.
107 /// Gets the error value as a NULL terminated C string. The error
108 /// string will be fetched and cached on demand. The error string
109 /// will be retrieved from a callback that is appropriate for the
110 /// type of the error and will be cached until the error value is
111 /// changed or cleared.
114 /// The error as a NULL terminated C string value if the error
115 /// is valid and is able to be converted to a string value,
117 //------------------------------------------------------------------
119 AsCString (const char *default_error_str = "unknown error") const;
121 //------------------------------------------------------------------
122 /// Clear the object state.
124 /// Reverts the state of this object to contain a generic success
125 /// value and frees any cached error string value.
126 //------------------------------------------------------------------
130 //------------------------------------------------------------------
131 /// Test for error condition.
134 /// \b true if this object contains an error, \b false
136 //------------------------------------------------------------------
140 //------------------------------------------------------------------
141 /// Access the error value.
145 //------------------------------------------------------------------
149 //------------------------------------------------------------------
150 /// Access the error type.
153 /// The error type enumeration value.
154 //------------------------------------------------------------------
158 //------------------------------------------------------------------
159 /// Log an error to Log().
161 /// Log the error given a formatted string \a format. If the this
162 /// object contains an error code, update the error string to
163 /// contain the prefix "error: ", followed by the formatted string,
164 /// followed by the error value and any string that describes the
165 /// error value. This allows more context to be given to an error
166 /// string that remains cached in this object. Logging always occurs
167 /// even when the error code contains a non-error value.
169 /// @param[in] format
170 /// A printf style format string.
173 /// Variable arguments that are needed for the printf style
174 /// format string \a format.
175 //------------------------------------------------------------------
177 PutToLog (Log *log, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
179 //------------------------------------------------------------------
180 /// Log an error to Log() if the error value is an error.
182 /// Log the error given a formatted string \a format only if the
183 /// error value in this object describes an error condition. If the
184 /// this object contains an error, update the error string to
185 /// contain the prefix "error: " followed by the formatted string,
186 /// followed by the error value and any string that describes the
187 /// error value. This allows more context to be given to an error
188 /// string that remains cached in this object.
190 /// @param[in] format
191 /// A printf style format string.
194 /// Variable arguments that are needed for the printf style
195 /// format string \a format.
196 //------------------------------------------------------------------
198 LogIfError (Log *log, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
200 //------------------------------------------------------------------
201 /// Set accessor from a kern_return_t.
203 /// Set accesssor for the error value to \a err and the error type
204 /// to \c MachKernel.
207 /// A mach error code.
208 //------------------------------------------------------------------
210 SetMachError (uint32_t err);
212 //------------------------------------------------------------------
213 /// Set accesssor with an error value and type.
215 /// Set accesssor for the error value to \a err and the error type
219 /// A mach error code.
222 /// The type for \a err.
223 //------------------------------------------------------------------
225 SetError (ValueType err, lldb::ErrorType type);
227 //------------------------------------------------------------------
228 /// Set the current error to errno.
230 /// Update the error value to be \c errno and update the type to
231 /// be \c Error::POSIX.
232 //------------------------------------------------------------------
236 //------------------------------------------------------------------
237 /// Set the current error to a generic error.
239 /// Update the error value to be \c LLDB_GENERIC_ERROR and update the
240 /// type to be \c Error::Generic.
241 //------------------------------------------------------------------
243 SetErrorToGenericError ();
245 //------------------------------------------------------------------
246 /// Set the current error string to \a err_str.
248 /// Set accessor for the error string value for a generic errors,
249 /// or to supply additional details above and beyond the standard
250 /// error strings that the standard type callbacks typically
251 /// provide. This allows custom strings to be supplied as an
252 /// error explanation. The error string value will remain until the
253 /// error value is cleared or a new error value/type is assigned.
256 /// The new custom error string to copy and cache.
257 //------------------------------------------------------------------
259 SetErrorString (const char *err_str);
261 //------------------------------------------------------------------
262 /// Set the current error string to a formatted error string.
265 /// A printf style format string
266 //------------------------------------------------------------------
268 SetErrorStringWithFormat (const char *format, ...) __attribute__ ((format (printf, 2, 3)));
271 SetErrorStringWithVarArg (const char *format, va_list args);
273 //------------------------------------------------------------------
274 /// Test for success condition.
276 /// Returns true if the error code in this object is considered a
277 /// successful return value.
280 /// \b true if this object contains an value that describes
281 /// success (non-erro), \b false otherwise.
282 //------------------------------------------------------------------
286 //------------------------------------------------------------------
287 /// Test for a failure due to a generic interrupt.
289 /// Returns true if the error code in this object was caused by an interrupt.
290 /// At present only supports Posix EINTR.
293 /// \b true if this object contains an value that describes
294 /// failure due to interrupt, \b false otherwise.
295 //------------------------------------------------------------------
297 WasInterrupted() const;
300 //------------------------------------------------------------------
302 //------------------------------------------------------------------
303 ValueType m_code; ///< Error code as an integer value.
304 lldb::ErrorType m_type; ///< The type of the above error code.
305 mutable std::string m_string; ///< A string representation of the error code.
308 } // namespace lldb_private
310 #endif // #if defined(__cplusplus)
311 #endif // #ifndef __DCError_h__