2015-08-21 07:04:50 +00:00
|
|
|
/*
|
|
|
|
* QEMU Error Objects
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
2018-02-15 16:36:23 +00:00
|
|
|
* Copyright (C) 2011-2015 Red Hat, Inc.
|
2015-08-21 07:04:50 +00:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
2018-02-15 16:36:23 +00:00
|
|
|
* Markus Armbruster <armbru@redhat.com>
|
2015-08-21 07:04:50 +00:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2. See
|
|
|
|
* the COPYING.LIB file in the top-level directory.
|
|
|
|
*/
|
2018-02-15 16:36:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Error reporting system loosely patterned after Glib's GError.
|
|
|
|
*
|
|
|
|
* Create an error:
|
|
|
|
* error_setg(&err, "situation normal, all fouled up");
|
|
|
|
*
|
2018-02-18 01:51:28 +00:00
|
|
|
* Create an error and add additional explanation:
|
|
|
|
* error_setg(&err, "invalid quark");
|
|
|
|
* error_append_hint(&err, "Valid quarks are up, down, strange, "
|
|
|
|
* "charm, top, bottom.\n");
|
|
|
|
*
|
|
|
|
* Do *not* contract this to
|
|
|
|
* error_setg(&err, "invalid quark\n"
|
|
|
|
* "Valid quarks are up, down, strange, charm, top, bottom.");
|
|
|
|
*
|
|
|
|
*
|
2018-02-20 13:13:30 +00:00
|
|
|
* Report an error to the current monitor if we have one, else stderr:
|
2018-02-15 16:36:23 +00:00
|
|
|
* error_report_err(err);
|
|
|
|
* This frees the error object.
|
|
|
|
*
|
2018-02-20 13:13:30 +00:00
|
|
|
* Likewise, but with additional text prepended:
|
|
|
|
* error_reportf_err(err, "Could not frobnicate '%s': ", name);
|
|
|
|
*
|
2018-02-15 16:36:23 +00:00
|
|
|
* Report an error somewhere else:
|
|
|
|
* const char *msg = error_get_pretty(err);
|
|
|
|
* do with msg what needs to be done...
|
|
|
|
* error_free(err);
|
2018-02-18 01:51:28 +00:00
|
|
|
* Note that this loses hints added with error_append_hint().
|
2018-02-15 16:36:23 +00:00
|
|
|
*
|
|
|
|
* Handle an error without reporting it (just for completeness):
|
|
|
|
* error_free(err);
|
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
*
|
2018-02-20 13:22:25 +00:00
|
|
|
* Call a function treating errors as fatal:
|
|
|
|
* foo(arg, &error_fatal);
|
|
|
|
*
|
2018-02-15 16:36:23 +00: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.
|
2018-02-18 01:49:03 +00:00
|
|
|
*
|
|
|
|
* Receive and accumulate multiple errors (first one wins):
|
|
|
|
* Error *err = NULL, *local_err = NULL;
|
|
|
|
* foo(arg, &err);
|
|
|
|
* bar(arg, &local_err);
|
|
|
|
* error_propagate(&err, local_err);
|
|
|
|
* if (err) {
|
|
|
|
* handle the error...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Do *not* "optimize" this to
|
|
|
|
* foo(arg, &err);
|
|
|
|
* bar(arg, &err); // WRONG!
|
|
|
|
* if (err) {
|
|
|
|
* handle the error...
|
|
|
|
* }
|
|
|
|
* because this may pass a non-null err to bar().
|
2018-02-15 16:36:23 +00:00
|
|
|
*/
|
|
|
|
|
2015-08-21 07:04:50 +00:00
|
|
|
#ifndef ERROR_H
|
|
|
|
#define ERROR_H
|
|
|
|
|
|
|
|
#include "qemu/compiler.h"
|
|
|
|
#include "qapi-types.h"
|
2017-01-20 13:13:21 +00:00
|
|
|
#include "unicorn/platform.h"
|
2015-08-21 07:04:50 +00:00
|
|
|
|
qapi: Add alias for ErrorClass
The qapi enum ErrorClass is unusual that it uses 'CamelCase' names,
contrary to our documented convention of preferring 'lower-case'.
However, this enum is entrenched in the API; we cannot change
what strings QMP outputs. Meanwhile, we want to simplify how
c_enum_const() is used to generate enum constants, by moving away
from the heuristics of camel_to_upper() to a more straightforward
c_name(N).upper() - but doing so will rename all of the ErrorClass
constants and cause churn to all client files, where the new names
are aesthetically less pleasing (ERROR_CLASS_DEVICENOTFOUND looks
like we can't make up our minds on whether to break between words).
So as always in computer science, solve the problem by some more
indirection: rename the qapi type to QapiErrorClass, and add a
new enum ErrorClass in error.h whose members are aliases of the
qapi type, but with the spelling expected elsewhere in the tree.
Then, when c_enum_const() changes the munging, we only have to
adjust the one alias spot.
Backports commit f22a28b898322c01b0463a8b7ec551d72bc61a5b from qemu
2018-02-20 01:38:49 +00:00
|
|
|
/*
|
|
|
|
* Overall category of an error.
|
|
|
|
* Based on the qapi type QapiErrorClass, but reproduced here for nicer
|
|
|
|
* enum names.
|
|
|
|
*/
|
|
|
|
typedef enum ErrorClass {
|
qapi: Change munging of CamelCase enum values
When munging enum values, the fact that we were passing the entire
prefix + value through camel_to_upper() meant that enum values
spelled with CamelCase could be turned into CAMEL_CASE. However,
this provides a potential collision (both OneTwo and One-Two would
munge into ONE_TWO) for enum types, when the same two names are
valid side-by-side as QAPI member names. By changing the generation
of enum constants to always be prefix + '_' + c_name(value,
False).upper(), and ensuring that there are no case collisions (in
the next patches), we no longer have to worry about names that
would be distinct as QAPI members but collide as variant tag names,
without having to think about what munging the heuristics in
camel_to_upper() will actually perform on an enum value.
Making the change will affect enums that did not follow coding
conventions, using 'CamelCase' rather than desired 'lower-case'.
Thankfully, there are only two culprits: InputButton and ErrorClass.
We already tweaked ErrorClass to make it an alias of QapiErrorClass,
where only the alias needs changing rather than the whole tree. So
the bulk of this change is modifying INPUT_BUTTON_WHEEL_UP to the
new INPUT_BUTTON_WHEELUP (and likewise for WHEELDOWN). That part
of this commit may later need reverting if we rename the enum
constants from 'WheelUp' to 'wheel-up' as part of moving
x-input-send-event to a stable interface; but at least we have
documentation bread crumbs in place to remind us (commit 513e7cd),
and it matches the fact that SDL constants are also spelled
SDL_BUTTON_WHEELUP.
Backports commit d20a580bc0eac9d489884f6d2ed28105880532b6 from qemu
2018-02-20 01:40:12 +00:00
|
|
|
ERROR_CLASS_GENERIC_ERROR = QAPI_ERROR_CLASS_GENERICERROR,
|
|
|
|
ERROR_CLASS_COMMAND_NOT_FOUND = QAPI_ERROR_CLASS_COMMANDNOTFOUND,
|
|
|
|
ERROR_CLASS_DEVICE_ENCRYPTED = QAPI_ERROR_CLASS_DEVICEENCRYPTED,
|
|
|
|
ERROR_CLASS_DEVICE_NOT_ACTIVE = QAPI_ERROR_CLASS_DEVICENOTACTIVE,
|
|
|
|
ERROR_CLASS_DEVICE_NOT_FOUND = QAPI_ERROR_CLASS_DEVICENOTFOUND,
|
|
|
|
ERROR_CLASS_KVM_MISSING_CAP = QAPI_ERROR_CLASS_KVMMISSINGCAP,
|
qapi: Add alias for ErrorClass
The qapi enum ErrorClass is unusual that it uses 'CamelCase' names,
contrary to our documented convention of preferring 'lower-case'.
However, this enum is entrenched in the API; we cannot change
what strings QMP outputs. Meanwhile, we want to simplify how
c_enum_const() is used to generate enum constants, by moving away
from the heuristics of camel_to_upper() to a more straightforward
c_name(N).upper() - but doing so will rename all of the ErrorClass
constants and cause churn to all client files, where the new names
are aesthetically less pleasing (ERROR_CLASS_DEVICENOTFOUND looks
like we can't make up our minds on whether to break between words).
So as always in computer science, solve the problem by some more
indirection: rename the qapi type to QapiErrorClass, and add a
new enum ErrorClass in error.h whose members are aliases of the
qapi type, but with the spelling expected elsewhere in the tree.
Then, when c_enum_const() changes the munging, we only have to
adjust the one alias spot.
Backports commit f22a28b898322c01b0463a8b7ec551d72bc61a5b from qemu
2018-02-20 01:38:49 +00:00
|
|
|
} ErrorClass;
|
|
|
|
|
2015-08-21 07:04:50 +00:00
|
|
|
/**
|
2018-02-15 16:36:23 +00:00
|
|
|
* Get @err's human-readable error message.
|
2015-08-21 07:04:50 +00:00
|
|
|
*/
|
2018-02-15 16:36:23 +00:00
|
|
|
const char *error_get_pretty(Error *err);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2015-08-21 07:04:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an indirect pointer to an error given a ErrorClass value and a
|
|
|
|
* printf-style human message, followed by a strerror() string if
|
|
|
|
* @os_error is not zero.
|
|
|
|
*/
|
|
|
|
void error_set_errno(Error **errp, int os_error, ErrorClass err_class,
|
|
|
|
const char *fmt, ...) GCC_FMT_ATTR(4, 5);
|
|
|
|
|
2018-02-15 16:36:23 +00:00
|
|
|
/*
|
|
|
|
* 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().
|
|
|
|
* 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, ...
|
2018-02-18 01:51:28 +00:00
|
|
|
* The resulting message should be a single phrase, with no newline or
|
|
|
|
* trailing punctuation.
|
2018-02-20 13:13:30 +00:00
|
|
|
* Please don't error_setg(&error_fatal, ...), use error_report() and
|
|
|
|
* exit(), because that's more obvious.
|
|
|
|
* Likewise, don't error_setg(&error_abort, ...), use assert().
|
2015-08-21 07:04:50 +00: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__.
Backports commit 1e9b65bb1bad51735cab6c861c29b592dccabf0e from qemu
2018-02-15 16:40:27 +00: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-08-21 07:04:50 +00:00
|
|
|
|
|
|
|
/*
|
2018-02-15 16:36:23 +00: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.
|
2018-03-02 04:38:18 +00:00
|
|
|
*
|
|
|
|
* The value of errno (which usually can get clobbered by almost any
|
|
|
|
* function call) will be preserved.
|
2015-08-21 07:04:50 +00: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__.
Backports commit 1e9b65bb1bad51735cab6c861c29b592dccabf0e from qemu
2018-02-15 16:40:27 +00: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);
|
|
|
|
|
2015-08-21 07:04:50 +00:00
|
|
|
|
2018-02-15 16:36:23 +00: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().
|
|
|
|
* 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.
|
2018-02-20 13:13:30 +00:00
|
|
|
* Please don't error_propagate(&error_fatal, ...), use
|
|
|
|
* error_report_err() and exit(), because that's more obvious.
|
2015-08-21 07:04:50 +00:00
|
|
|
*/
|
2018-02-15 16:36:23 +00:00
|
|
|
void error_propagate(Error **dst_errp, Error *local_err);
|
2015-08-21 07:04:50 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-15 16:36:23 +00:00
|
|
|
* Convenience function to report open() failure.
|
2015-08-21 07:04:50 +00: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__.
Backports commit 1e9b65bb1bad51735cab6c861c29b592dccabf0e from qemu
2018-02-15 16:40:27 +00: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);
|
2015-08-21 07:04:50 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-15 16:36:23 +00:00
|
|
|
* Return an exact copy of @err.
|
2015-08-21 07:04:50 +00:00
|
|
|
*/
|
2018-02-15 16:36:23 +00:00
|
|
|
Error *error_copy(const Error *err);
|
2015-08-21 07:04:50 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-15 16:36:23 +00:00
|
|
|
* Free @err.
|
|
|
|
* @err may be NULL.
|
2015-08-21 07:04:50 +00:00
|
|
|
*/
|
|
|
|
void error_free(Error *err);
|
|
|
|
|
2018-02-15 16:36:23 +00: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.
|
2015-08-21 07:04:50 +00: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__.
Backports commit 1e9b65bb1bad51735cab6c861c29b592dccabf0e from qemu
2018-02-15 16:40:27 +00: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);
|
2015-08-21 07:04:50 +00:00
|
|
|
|
2018-02-15 16:36:23 +00:00
|
|
|
/*
|
2018-02-20 13:13:30 +00:00
|
|
|
* Special error destination to abort on error.
|
|
|
|
* See error_setg() and error_propagate() for details.
|
2018-02-15 16:36:23 +00:00
|
|
|
*/
|
2015-08-21 07:04:50 +00:00
|
|
|
extern Error *error_abort;
|
|
|
|
|
2018-02-20 13:22:25 +00:00
|
|
|
/*
|
|
|
|
* Special error destination to exit(1) on error.
|
|
|
|
* See error_setg() and error_propagate() for details.
|
|
|
|
*/
|
|
|
|
extern Error *error_fatal;
|
|
|
|
|
2015-08-21 07:04:50 +00:00
|
|
|
#endif
|