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>
This commit is contained in:
Markus Armbruster 2015-06-19 19:21:59 +02:00
parent edf6f3b335
commit 1e9b65bb1b
5 changed files with 81 additions and 30 deletions

View file

@ -104,16 +104,26 @@ ErrorClass error_get_class(const Error *err);
* The new error's class is ERROR_CLASS_GENERIC_ERROR, and its
* human-readable error message is made from printf-style @fmt, ...
*/
void error_setg(Error **errp, const char *fmt, ...)
GCC_FMT_ATTR(2, 3);
#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);
/*
* 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.
*/
void error_setg_errno(Error **errp, int os_error, const char *fmt, ...)
GCC_FMT_ATTR(3, 4);
#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);
#ifdef _WIN32
/*
@ -121,8 +131,13 @@ void error_setg_errno(Error **errp, int os_error, const char *fmt, ...)
* If @win32_error is non-zero, ": " + g_win32_error_message(win32_err)
* is appended to the human-readable error message.
*/
void error_setg_win32(Error **errp, int win32_err, const char *fmt, ...)
GCC_FMT_ATTR(3, 4);
#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);
#endif
/*
@ -143,7 +158,12 @@ void error_propagate(Error **dst_errp, Error *local_err);
/*
* Convenience function to report open() failure.
*/
void error_setg_file_open(Error **errp, int os_errno, const char *filename);
#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);
/*
* Return an exact copy of @err.
@ -166,8 +186,13 @@ void error_report_err(Error *);
* Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
* strongly discouraged.
*/
void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
GCC_FMT_ATTR(3, 4);
#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);
/*
* Pass to error_setg() & friends to abort() on error.