2011-06-01 19:14:49 +02:00
|
|
|
/*
|
|
|
|
* QEMU Error Objects
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
2015-06-19 18:29:24 +02:00
|
|
|
* Copyright (C) 2011-2015 Red Hat, Inc.
|
2011-06-01 19:14:49 +02:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
2015-06-19 18:29:24 +02:00
|
|
|
* Markus Armbruster <armbru@redhat.com>
|
2011-06-01 19:14:49 +02:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2. See
|
|
|
|
* the COPYING.LIB file in the top-level directory.
|
|
|
|
*/
|
2015-06-19 18:29:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Error reporting system loosely patterned after Glib's GError.
|
|
|
|
*
|
|
|
|
* Create an error:
|
|
|
|
* error_setg(&err, "situation normal, all fouled up");
|
|
|
|
*
|
|
|
|
* Report an error to stderr:
|
|
|
|
* error_report_err(err);
|
|
|
|
* This frees the error object.
|
|
|
|
*
|
|
|
|
* Report an error somewhere else:
|
|
|
|
* const char *msg = error_get_pretty(err);
|
|
|
|
* do with msg what needs to be done...
|
|
|
|
* error_free(err);
|
|
|
|
*
|
|
|
|
* Handle an error without reporting it (just for completeness):
|
|
|
|
* error_free(err);
|
|
|
|
*
|
2015-11-06 07:35:31 +01:00
|
|
|
* Assert that an expected error occurred, but clean it up without
|
|
|
|
* reporting it (primarily useful in testsuites):
|
|
|
|
* error_free_or_abort(&err);
|
|
|
|
*
|
2015-06-19 18:29:24 +02:00
|
|
|
* Pass an existing error to the caller:
|
|
|
|
* error_propagate(errp, err);
|
|
|
|
* where Error **errp is a parameter, by convention the last one.
|
|
|
|
*
|
|
|
|
* Create a new error and pass it to the caller:
|
|
|
|
* error_setg(errp, "situation normal, all fouled up");
|
|
|
|
*
|
|
|
|
* Call a function and receive an error from it:
|
|
|
|
* Error *err = NULL;
|
|
|
|
* foo(arg, &err);
|
|
|
|
* if (err) {
|
|
|
|
* handle the error...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Call a function ignoring errors:
|
|
|
|
* foo(arg, NULL);
|
|
|
|
*
|
|
|
|
* Call a function aborting on errors:
|
|
|
|
* foo(arg, &error_abort);
|
|
|
|
*
|
2015-09-11 16:51:42 +02:00
|
|
|
* Call a function treating errors as fatal:
|
|
|
|
* foo(arg, &error_fatal);
|
|
|
|
*
|
2015-06-19 18:29:24 +02:00
|
|
|
* Receive an error and pass it on to the caller:
|
|
|
|
* Error *err = NULL;
|
|
|
|
* foo(arg, &err);
|
|
|
|
* if (err) {
|
|
|
|
* handle the error...
|
|
|
|
* error_propagate(errp, err);
|
|
|
|
* }
|
|
|
|
* where Error **errp is a parameter, by convention the last one.
|
|
|
|
*
|
|
|
|
* Do *not* "optimize" this to
|
|
|
|
* foo(arg, errp);
|
|
|
|
* if (*errp) { // WRONG!
|
|
|
|
* handle the error...
|
|
|
|
* }
|
|
|
|
* because errp may be NULL!
|
|
|
|
*
|
|
|
|
* But when all you do with the error is pass it on, please use
|
|
|
|
* foo(arg, errp);
|
|
|
|
* for readability.
|
|
|
|
*/
|
|
|
|
|
2011-06-01 19:14:49 +02:00
|
|
|
#ifndef ERROR_H
|
|
|
|
#define ERROR_H
|
|
|
|
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/compiler.h"
|
2012-07-27 19:09:29 +02:00
|
|
|
#include "qapi-types.h"
|
2011-06-01 19:14:49 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Opaque error object.
|
2011-06-01 19:14:49 +02:00
|
|
|
*/
|
|
|
|
typedef struct Error Error;
|
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Get @err's human-readable error message.
|
2011-06-01 19:14:49 +02:00
|
|
|
*/
|
2015-06-19 18:29:24 +02:00
|
|
|
const char *error_get_pretty(Error *err);
|
2011-06-01 19:14:49 +02:00
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Get @err's error class.
|
|
|
|
* Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
|
|
|
|
* strongly discouraged.
|
|
|
|
*/
|
|
|
|
ErrorClass error_get_class(const Error *err);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new error object and assign it to *@errp.
|
|
|
|
* If @errp is NULL, the error is ignored. Don't bother creating one
|
|
|
|
* then.
|
|
|
|
* If @errp is &error_abort, print a suitable message and abort().
|
2015-09-11 16:51:42 +02:00
|
|
|
* If @errp is &error_fatal, print a suitable message and exit(1).
|
2015-06-19 18:29:24 +02:00
|
|
|
* If @errp is anything else, *@errp must be NULL.
|
|
|
|
* The new error's class is ERROR_CLASS_GENERIC_ERROR, and its
|
|
|
|
* human-readable error message is made from printf-style @fmt, ...
|
|
|
|
*/
|
error: On abort, report where the error was created
This is particularly useful when we abort in error_propagate(),
because there the stack backtrace doesn't lead to where the error was
created. Looks like this:
Unexpected error in parse_block_error_action() at .../qemu/blockdev.c:322:
qemu-system-x86_64: -drive if=none,werror=foo: 'foo' invalid write error action
Aborted (core dumped)
Note: to get this example output, I monkey-patched drive_new() to pass
&error_abort to blockdev_init().
To keep the error handling boiler plate from growing even more, all
error_setFOO() become macros expanding into error_setFOO_internal()
with additional __FILE__, __LINE__, __func__ arguments. Not exactly
pretty, but it works.
The macro trickery breaks down when you take the address of an
error_setFOO(). Fortunately, we do that in just one place: qemu-ga's
Windows VSS provider and requester DLL wants to call
error_setg_win32() through a function pointer "to avoid linking glib
to the DLL". Use error_setg_win32_internal() there. The use of the
function pointer is already wrapped in a macro, so the churn isn't
bad.
Code size increases by some 35KiB for me (0.7%). Tolerable. Could be
less if we passed relative rather than absolute source file names to
the compiler, or forwent reporting __func__.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
2015-06-19 19:21:59 +02:00
|
|
|
#define error_setg(errp, fmt, ...) \
|
|
|
|
error_setg_internal((errp), __FILE__, __LINE__, __func__, \
|
|
|
|
(fmt), ## __VA_ARGS__)
|
|
|
|
void error_setg_internal(Error **errp,
|
|
|
|
const char *src, int line, const char *func,
|
|
|
|
const char *fmt, ...)
|
|
|
|
GCC_FMT_ATTR(5, 6);
|
2015-06-19 18:29:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Just like error_setg(), with @os_error info added to the message.
|
|
|
|
* If @os_error is non-zero, ": " + strerror(os_error) is appended to
|
|
|
|
* the human-readable error message.
|
2012-10-02 09:00:45 +02:00
|
|
|
*/
|
error: On abort, report where the error was created
This is particularly useful when we abort in error_propagate(),
because there the stack backtrace doesn't lead to where the error was
created. Looks like this:
Unexpected error in parse_block_error_action() at .../qemu/blockdev.c:322:
qemu-system-x86_64: -drive if=none,werror=foo: 'foo' invalid write error action
Aborted (core dumped)
Note: to get this example output, I monkey-patched drive_new() to pass
&error_abort to blockdev_init().
To keep the error handling boiler plate from growing even more, all
error_setFOO() become macros expanding into error_setFOO_internal()
with additional __FILE__, __LINE__, __func__ arguments. Not exactly
pretty, but it works.
The macro trickery breaks down when you take the address of an
error_setFOO(). Fortunately, we do that in just one place: qemu-ga's
Windows VSS provider and requester DLL wants to call
error_setg_win32() through a function pointer "to avoid linking glib
to the DLL". Use error_setg_win32_internal() there. The use of the
function pointer is already wrapped in a macro, so the churn isn't
bad.
Code size increases by some 35KiB for me (0.7%). Tolerable. Could be
less if we passed relative rather than absolute source file names to
the compiler, or forwent reporting __func__.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
2015-06-19 19:21:59 +02:00
|
|
|
#define error_setg_errno(errp, os_error, fmt, ...) \
|
|
|
|
error_setg_errno_internal((errp), __FILE__, __LINE__, __func__, \
|
|
|
|
(os_error), (fmt), ## __VA_ARGS__)
|
|
|
|
void error_setg_errno_internal(Error **errp,
|
|
|
|
const char *fname, int line, const char *func,
|
|
|
|
int os_error, const char *fmt, ...)
|
|
|
|
GCC_FMT_ATTR(6, 7);
|
2012-10-02 09:00:45 +02:00
|
|
|
|
2013-08-07 17:40:11 +02:00
|
|
|
#ifdef _WIN32
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Just like error_setg(), with @win32_error info added to the message.
|
|
|
|
* If @win32_error is non-zero, ": " + g_win32_error_message(win32_err)
|
|
|
|
* is appended to the human-readable error message.
|
2013-08-07 17:40:11 +02:00
|
|
|
*/
|
error: On abort, report where the error was created
This is particularly useful when we abort in error_propagate(),
because there the stack backtrace doesn't lead to where the error was
created. Looks like this:
Unexpected error in parse_block_error_action() at .../qemu/blockdev.c:322:
qemu-system-x86_64: -drive if=none,werror=foo: 'foo' invalid write error action
Aborted (core dumped)
Note: to get this example output, I monkey-patched drive_new() to pass
&error_abort to blockdev_init().
To keep the error handling boiler plate from growing even more, all
error_setFOO() become macros expanding into error_setFOO_internal()
with additional __FILE__, __LINE__, __func__ arguments. Not exactly
pretty, but it works.
The macro trickery breaks down when you take the address of an
error_setFOO(). Fortunately, we do that in just one place: qemu-ga's
Windows VSS provider and requester DLL wants to call
error_setg_win32() through a function pointer "to avoid linking glib
to the DLL". Use error_setg_win32_internal() there. The use of the
function pointer is already wrapped in a macro, so the churn isn't
bad.
Code size increases by some 35KiB for me (0.7%). Tolerable. Could be
less if we passed relative rather than absolute source file names to
the compiler, or forwent reporting __func__.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
2015-06-19 19:21:59 +02:00
|
|
|
#define error_setg_win32(errp, win32_err, fmt, ...) \
|
|
|
|
error_setg_win32_internal((errp), __FILE__, __LINE__, __func__, \
|
|
|
|
(win32_err), (fmt), ## __VA_ARGS__)
|
|
|
|
void error_setg_win32_internal(Error **errp,
|
|
|
|
const char *src, int line, const char *func,
|
|
|
|
int win32_err, const char *fmt, ...)
|
|
|
|
GCC_FMT_ATTR(6, 7);
|
2013-08-07 17:40:11 +02:00
|
|
|
#endif
|
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Propagate error object (if any) from @local_err to @dst_errp.
|
|
|
|
* If @local_err is NULL, do nothing (because there's nothing to
|
|
|
|
* propagate).
|
|
|
|
* Else, if @dst_errp is NULL, errors are being ignored. Free the
|
|
|
|
* error object.
|
|
|
|
* Else, if @dst_errp is &error_abort, print a suitable message and
|
|
|
|
* abort().
|
2015-09-11 16:51:42 +02:00
|
|
|
* Else, if @dst_errp is &error_fatal, print a suitable message and
|
|
|
|
* exit(1).
|
2015-06-19 18:29:24 +02:00
|
|
|
* Else, if @dst_errp already contains an error, ignore this one: free
|
|
|
|
* the error object.
|
|
|
|
* Else, move the error object from @local_err to *@dst_errp.
|
|
|
|
* On return, @local_err is invalid.
|
2012-08-29 16:20:57 +02:00
|
|
|
*/
|
2015-06-19 18:29:24 +02:00
|
|
|
void error_propagate(Error **dst_errp, Error *local_err);
|
2012-08-29 16:20:57 +02:00
|
|
|
|
hmp: Allow for error message hints on HMP
Commits 7216ae3d and d2828429 disabled some error message hints,
all because a change to use modern error reporting meant that the
hint would be output prior to the actual error. Fix this by making
hints a first-class member of Error.
For example, we are now back to the pleasant:
$ qemu-system-x86_64 --nodefaults -S --vnc :0 --chardev null,id=,
qemu-system-x86_64: --chardev null,id=,: Parameter 'id' expects an identifier
Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <1441901956-21991-1-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2015-09-10 18:19:16 +02:00
|
|
|
/**
|
|
|
|
* Append a printf-style human-readable explanation to an existing error.
|
|
|
|
* May be called multiple times, and safe if @errp is NULL.
|
|
|
|
*/
|
|
|
|
void error_append_hint(Error **errp, const char *fmt, ...)
|
|
|
|
GCC_FMT_ATTR(2, 3);
|
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Convenience function to report open() failure.
|
2013-06-07 20:24:49 +02:00
|
|
|
*/
|
error: On abort, report where the error was created
This is particularly useful when we abort in error_propagate(),
because there the stack backtrace doesn't lead to where the error was
created. Looks like this:
Unexpected error in parse_block_error_action() at .../qemu/blockdev.c:322:
qemu-system-x86_64: -drive if=none,werror=foo: 'foo' invalid write error action
Aborted (core dumped)
Note: to get this example output, I monkey-patched drive_new() to pass
&error_abort to blockdev_init().
To keep the error handling boiler plate from growing even more, all
error_setFOO() become macros expanding into error_setFOO_internal()
with additional __FILE__, __LINE__, __func__ arguments. Not exactly
pretty, but it works.
The macro trickery breaks down when you take the address of an
error_setFOO(). Fortunately, we do that in just one place: qemu-ga's
Windows VSS provider and requester DLL wants to call
error_setg_win32() through a function pointer "to avoid linking glib
to the DLL". Use error_setg_win32_internal() there. The use of the
function pointer is already wrapped in a macro, so the churn isn't
bad.
Code size increases by some 35KiB for me (0.7%). Tolerable. Could be
less if we passed relative rather than absolute source file names to
the compiler, or forwent reporting __func__.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
2015-06-19 19:21:59 +02:00
|
|
|
#define error_setg_file_open(errp, os_errno, filename) \
|
|
|
|
error_setg_file_open_internal((errp), __FILE__, __LINE__, __func__, \
|
|
|
|
(os_errno), (filename))
|
|
|
|
void error_setg_file_open_internal(Error **errp,
|
|
|
|
const char *src, int line, const char *func,
|
|
|
|
int os_errno, const char *filename);
|
2013-06-07 20:24:49 +02:00
|
|
|
|
2012-08-01 21:29:38 +02:00
|
|
|
/*
|
2015-06-19 18:29:24 +02:00
|
|
|
* Return an exact copy of @err.
|
2011-12-05 19:04:05 +01:00
|
|
|
*/
|
|
|
|
Error *error_copy(const Error *err);
|
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Free @err.
|
|
|
|
* @err may be NULL.
|
2011-06-01 19:14:49 +02:00
|
|
|
*/
|
2015-06-19 18:29:24 +02:00
|
|
|
void error_free(Error *err);
|
2011-06-01 19:14:49 +02:00
|
|
|
|
2015-11-06 07:35:31 +01:00
|
|
|
/*
|
|
|
|
* Convenience function to assert that *@errp is set, then silently free it.
|
|
|
|
*/
|
|
|
|
void error_free_or_abort(Error **errp);
|
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Convenience function to error_report() and free @err.
|
2015-02-06 15:27:19 +01:00
|
|
|
*/
|
|
|
|
void error_report_err(Error *);
|
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Just like error_setg(), except you get to specify the error class.
|
|
|
|
* Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
|
|
|
|
* strongly discouraged.
|
2011-06-01 19:14:49 +02:00
|
|
|
*/
|
error: On abort, report where the error was created
This is particularly useful when we abort in error_propagate(),
because there the stack backtrace doesn't lead to where the error was
created. Looks like this:
Unexpected error in parse_block_error_action() at .../qemu/blockdev.c:322:
qemu-system-x86_64: -drive if=none,werror=foo: 'foo' invalid write error action
Aborted (core dumped)
Note: to get this example output, I monkey-patched drive_new() to pass
&error_abort to blockdev_init().
To keep the error handling boiler plate from growing even more, all
error_setFOO() become macros expanding into error_setFOO_internal()
with additional __FILE__, __LINE__, __func__ arguments. Not exactly
pretty, but it works.
The macro trickery breaks down when you take the address of an
error_setFOO(). Fortunately, we do that in just one place: qemu-ga's
Windows VSS provider and requester DLL wants to call
error_setg_win32() through a function pointer "to avoid linking glib
to the DLL". Use error_setg_win32_internal() there. The use of the
function pointer is already wrapped in a macro, so the churn isn't
bad.
Code size increases by some 35KiB for me (0.7%). Tolerable. Could be
less if we passed relative rather than absolute source file names to
the compiler, or forwent reporting __func__.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
2015-06-19 19:21:59 +02:00
|
|
|
#define error_set(errp, err_class, fmt, ...) \
|
|
|
|
error_set_internal((errp), __FILE__, __LINE__, __func__, \
|
|
|
|
(err_class), (fmt), ## __VA_ARGS__)
|
|
|
|
void error_set_internal(Error **errp,
|
|
|
|
const char *src, int line, const char *func,
|
|
|
|
ErrorClass err_class, const char *fmt, ...)
|
|
|
|
GCC_FMT_ATTR(6, 7);
|
2011-06-01 19:14:49 +02:00
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Pass to error_setg() & friends to abort() on error.
|
2014-01-02 03:46:59 +01:00
|
|
|
*/
|
|
|
|
extern Error *error_abort;
|
|
|
|
|
2015-09-11 16:51:42 +02:00
|
|
|
/*
|
|
|
|
* Pass to error_setg() & friends to exit(1) on error.
|
|
|
|
*/
|
|
|
|
extern Error *error_fatal;
|
|
|
|
|
2011-06-01 19:14:49 +02:00
|
|
|
#endif
|