10b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng//===-- Error.h -------------------------------------------------*- C++ -*-===//
20b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng//
30b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng//                     The LLVM Compiler Infrastructure
40b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng//
50b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng// This file is distributed under the University of Illinois Open Source
60b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng// License. See LICENSE.TXT for details.
70b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng//
80b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng//===----------------------------------------------------------------------===//
90b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng
100b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng#ifndef __DCError_h__
110b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng#define __DCError_h__
120b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng#if defined(__cplusplus)
130b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng
140b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng#if defined (__APPLE__)
150b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng#include <mach/mach.h>
160b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng#endif
170b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng#include <stdint.h>
180b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng#include <stdio.h>
190b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng#include <string>
200b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng
210b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng#include "lldb/lldb-private.h"
220b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng
230b863155f625de76a1e3e76bb424497b1870bff3Ben Chengnamespace lldb_private {
240b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng
250b863155f625de76a1e3e76bb424497b1870bff3Ben Chengclass Log;
260b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng
270b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng//----------------------------------------------------------------------
280b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// @class Error Error.h "lldb/Core/Error.h"
290b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// @brief An error handling class.
300b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng///
310b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// This class is designed to be able to hold any error code that can be
320b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// encountered on a given platform. The errors are stored as a value
330b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// of type Error::ValueType. This value should be large enough to hold
340b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// any and all errors that the class supports. Each error has an
350b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// associated type that is of type lldb::ErrorType. New types
360b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// can be added to support new error types, and architecture specific
370b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// types can be enabled. In the future we may wish to switch to a
380b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// registration mechanism where new error types can be registered at
390b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// runtime instead of a hard coded scheme.
400b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng///
410b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// All errors in this class also know how to generate a string
420b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// representation of themselves for printing results and error codes.
430b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// The string value will be fetched on demand and its string value will
440b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// be cached until the error is cleared of the value of the error
450b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng/// changes.
460b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng//----------------------------------------------------------------------
470b863155f625de76a1e3e76bb424497b1870bff3Ben Chengclass Error
480b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng{
490b863155f625de76a1e3e76bb424497b1870bff3Ben Chengpublic:
500b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng    //------------------------------------------------------------------
510b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng    /// Every error value that this object can contain needs to be able
520b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng    /// to fit into ValueType.
538701663f8720eaae76448f5d61890e142a67eba1Ben Cheng    //------------------------------------------------------------------
540b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng    typedef uint32_t ValueType;
550b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng
560b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng    //------------------------------------------------------------------
570b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng    /// Default constructor.
580b863155f625de76a1e3e76bb424497b1870bff3Ben Cheng    ///
59    /// Initialize the error object with a generic success value.
60    ///
61    /// @param[in] err
62    ///     An error code.
63    ///
64    /// @param[in] type
65    ///     The type for \a err.
66    //------------------------------------------------------------------
67    Error ();
68
69    explicit
70    Error (ValueType err, lldb::ErrorType type = lldb::eErrorTypeGeneric);
71
72    explicit
73    Error (const char* err_str);
74
75    Error (const Error &rhs);
76    //------------------------------------------------------------------
77    /// Assignment operator.
78    ///
79    /// @param[in] err
80    ///     An error code.
81    ///
82    /// @return
83    ///     A const reference to this object.
84    //------------------------------------------------------------------
85    const Error&
86    operator = (const Error& rhs);
87
88
89    //------------------------------------------------------------------
90    /// Assignment operator from a kern_return_t.
91    ///
92    /// Sets the type to \c MachKernel and the error code to \a err.
93    ///
94    /// @param[in] err
95    ///     A mach error code.
96    ///
97    /// @return
98    ///     A const reference to this object.
99    //------------------------------------------------------------------
100    const Error&
101    operator = (uint32_t err);
102
103    ~Error();
104
105    //------------------------------------------------------------------
106    /// Get the error string associated with the current error.
107    //
108    /// Gets the error value as a NULL terminated C string. The error
109    /// string will be fetched and cached on demand. The error string
110    /// will be retrieved from a callback that is appropriate for the
111    /// type of the error and will be cached until the error value is
112    /// changed or cleared.
113    ///
114    /// @return
115    ///     The error as a NULL terminated C string value if the error
116    ///     is valid and is able to be converted to a string value,
117    ///     NULL otherwise.
118    //------------------------------------------------------------------
119    const char *
120    AsCString (const char *default_error_str = "unknown error") const;
121
122    //------------------------------------------------------------------
123    /// Clear the object state.
124    ///
125    /// Reverts the state of this object to contain a generic success
126    /// value and frees any cached error string value.
127    //------------------------------------------------------------------
128    void
129    Clear ();
130
131    //------------------------------------------------------------------
132    /// Test for error condition.
133    ///
134    /// @return
135    ///     \b true if this object contains an error, \b false
136    ///     otherwise.
137    //------------------------------------------------------------------
138    bool
139    Fail () const;
140
141    //------------------------------------------------------------------
142    /// Access the error value.
143    ///
144    /// @return
145    ///     The error value.
146    //------------------------------------------------------------------
147    ValueType
148    GetError () const;
149
150    //------------------------------------------------------------------
151    /// Access the error type.
152    ///
153    /// @return
154    ///     The error type enumeration value.
155    //------------------------------------------------------------------
156    lldb::ErrorType
157    GetType () const;
158
159    //------------------------------------------------------------------
160    /// Log an error to Log().
161    ///
162    /// Log the error given a formatted string \a format. If the this
163    /// object contains an error code, update the error string to
164    /// contain the prefix "error: ", followed by the formatted string,
165    /// followed by the error value and any string that describes the
166    /// error value. This allows more context to be given to an error
167    /// string that remains cached in this object. Logging always occurs
168    /// even when the error code contains a non-error value.
169    ///
170    /// @param[in] format
171    ///     A printf style format string.
172    ///
173    /// @param[in] ...
174    ///     Variable arguments that are needed for the printf style
175    ///     format string \a format.
176    //------------------------------------------------------------------
177    void
178    PutToLog (Log *log, const char *format, ...)  __attribute__ ((format (printf, 3, 4)));
179
180    //------------------------------------------------------------------
181    /// Log an error to Log() if the error value is an error.
182    ///
183    /// Log the error given a formatted string \a format only if the
184    /// error value in this object describes an error condition. If the
185    /// this object contains an error, update the error string to
186    /// contain the prefix "error: " followed by the formatted string,
187    /// followed by the error value and any string that describes the
188    /// error value. This allows more context to be given to an error
189    /// string that remains cached in this object.
190    ///
191    /// @param[in] format
192    ///     A printf style format string.
193    ///
194    /// @param[in] ...
195    ///     Variable arguments that are needed for the printf style
196    ///     format string \a format.
197    //------------------------------------------------------------------
198    void
199    LogIfError (Log *log, const char *format, ...)  __attribute__ ((format (printf, 3, 4)));
200
201    //------------------------------------------------------------------
202    /// Set accessor from a kern_return_t.
203    ///
204    /// Set accesssor for the error value to \a err and the error type
205    /// to \c MachKernel.
206    ///
207    /// @param[in] err
208    ///     A mach error code.
209    //------------------------------------------------------------------
210    void
211    SetMachError (uint32_t err);
212
213    //------------------------------------------------------------------
214    /// Set accesssor with an error value and type.
215    ///
216    /// Set accesssor for the error value to \a err and the error type
217    /// to \a type.
218    ///
219    /// @param[in] err
220    ///     A mach error code.
221    ///
222    /// @param[in] type
223    ///     The type for \a err.
224    //------------------------------------------------------------------
225    void
226    SetError (ValueType err, lldb::ErrorType type);
227
228    //------------------------------------------------------------------
229    /// Set the current error to errno.
230    ///
231    /// Update the error value to be \c errno and update the type to
232    /// be \c Error::POSIX.
233    //------------------------------------------------------------------
234    void
235    SetErrorToErrno ();
236
237    //------------------------------------------------------------------
238    /// Set the current error to a generic error.
239    ///
240    /// Update the error value to be \c LLDB_GENERIC_ERROR and update the
241    /// type to be \c Error::Generic.
242    //------------------------------------------------------------------
243    void
244    SetErrorToGenericError ();
245
246    //------------------------------------------------------------------
247    /// Set the current error string to \a err_str.
248    ///
249    /// Set accessor for the error string value for a generic errors,
250    /// or to supply additional details above and beyond the standard
251    /// error strings that the standard type callbacks typically
252    /// provide. This allows custom strings to be supplied as an
253    /// error explanation. The error string value will remain until the
254    /// error value is cleared or a new error value/type is assigned.
255    ///
256    /// @param err_str
257    ///     The new custom error string to copy and cache.
258    //------------------------------------------------------------------
259    void
260    SetErrorString (const char *err_str);
261
262    //------------------------------------------------------------------
263    /// Set the current error string to a formatted error string.
264    ///
265    /// @param format
266    ///     A printf style format string
267    //------------------------------------------------------------------
268    int
269    SetErrorStringWithFormat (const char *format, ...) __attribute__ ((format (printf, 2, 3)));
270
271    int
272    SetErrorStringWithVarArg (const char *format, va_list args);
273
274    //------------------------------------------------------------------
275    /// Test for success condition.
276    ///
277    /// Returns true if the error code in this object is considered a
278    /// successful return value.
279    ///
280    /// @return
281    ///     \b true if this object contains an value that describes
282    ///     success (non-erro), \b false otherwise.
283    //------------------------------------------------------------------
284    bool
285    Success () const;
286
287    //------------------------------------------------------------------
288    /// Test for a failure due to a generic interrupt.
289    ///
290    /// Returns true if the error code in this object was caused by an interrupt.
291    /// At present only supports Posix EINTR.
292    ///
293    /// @return
294    ///     \b true if this object contains an value that describes
295    ///     failure due to interrupt, \b false otherwise.
296    //------------------------------------------------------------------
297    bool
298    WasInterrupted() const;
299
300protected:
301    //------------------------------------------------------------------
302    /// Member variables
303    //------------------------------------------------------------------
304    ValueType m_code;               ///< Error code as an integer value.
305    lldb::ErrorType m_type;            ///< The type of the above error code.
306    mutable std::string m_string;   ///< A string representation of the error code.
307};
308
309} // namespace lldb_private
310
311#endif  // #if defined(__cplusplus)
312#endif    // #ifndef __DCError_h__
313