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
This commit is contained in:
Markus Armbruster 2018-02-15 11:40:27 -05:00 committed by Lioncash
parent 46f398569f
commit 97ad660361
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
16 changed files with 78 additions and 33 deletions

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_aarch64
#define error_get_class error_get_class_aarch64
#define error_get_pretty error_get_pretty_aarch64
#define error_setg_file_open error_setg_file_open_aarch64
#define error_setg_file_open_internal error_setg_file_open_internal_aarch64
#define estimateDiv128To64 estimateDiv128To64_aarch64
#define estimateSqrt32 estimateSqrt32_aarch64
#define excnames excnames_aarch64

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_aarch64eb
#define error_get_class error_get_class_aarch64eb
#define error_get_pretty error_get_pretty_aarch64eb
#define error_setg_file_open error_setg_file_open_aarch64eb
#define error_setg_file_open_internal error_setg_file_open_internal_aarch64eb
#define estimateDiv128To64 estimateDiv128To64_aarch64eb
#define estimateSqrt32 estimateSqrt32_aarch64eb
#define excnames excnames_aarch64eb

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_arm
#define error_get_class error_get_class_arm
#define error_get_pretty error_get_pretty_arm
#define error_setg_file_open error_setg_file_open_arm
#define error_setg_file_open_internal error_setg_file_open_internal_arm
#define estimateDiv128To64 estimateDiv128To64_arm
#define estimateSqrt32 estimateSqrt32_arm
#define excnames excnames_arm

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_armeb
#define error_get_class error_get_class_armeb
#define error_get_pretty error_get_pretty_armeb
#define error_setg_file_open error_setg_file_open_armeb
#define error_setg_file_open_internal error_setg_file_open_internal_armeb
#define estimateDiv128To64 estimateDiv128To64_armeb
#define estimateSqrt32 estimateSqrt32_armeb
#define excnames excnames_armeb

View file

@ -397,7 +397,7 @@ symbols = (
'error_exit',
'error_get_class',
'error_get_pretty',
'error_setg_file_open',
'error_setg_file_open_internal',
'estimateDiv128To64',
'estimateSqrt32',
'excnames',

View file

@ -112,16 +112,27 @@ void error_set_errno(Error **errp, int os_error, ErrorClass err_class,
* 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);
/*
* Propagate error object (if any) from @local_err to @dst_errp.
@ -141,7 +152,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.
@ -159,8 +175,13 @@ void error_free(Error *err);
* 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.

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_m68k
#define error_get_class error_get_class_m68k
#define error_get_pretty error_get_pretty_m68k
#define error_setg_file_open error_setg_file_open_m68k
#define error_setg_file_open_internal error_setg_file_open_internal_m68k
#define estimateDiv128To64 estimateDiv128To64_m68k
#define estimateSqrt32 estimateSqrt32_m68k
#define excnames excnames_m68k

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_mips
#define error_get_class error_get_class_mips
#define error_get_pretty error_get_pretty_mips
#define error_setg_file_open error_setg_file_open_mips
#define error_setg_file_open_internal error_setg_file_open_internal_mips
#define estimateDiv128To64 estimateDiv128To64_mips
#define estimateSqrt32 estimateSqrt32_mips
#define excnames excnames_mips

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_mips64
#define error_get_class error_get_class_mips64
#define error_get_pretty error_get_pretty_mips64
#define error_setg_file_open error_setg_file_open_mips64
#define error_setg_file_open_internal error_setg_file_open_internal_mips64
#define estimateDiv128To64 estimateDiv128To64_mips64
#define estimateSqrt32 estimateSqrt32_mips64
#define excnames excnames_mips64

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_mips64el
#define error_get_class error_get_class_mips64el
#define error_get_pretty error_get_pretty_mips64el
#define error_setg_file_open error_setg_file_open_mips64el
#define error_setg_file_open_internal error_setg_file_open_internal_mips64el
#define estimateDiv128To64 estimateDiv128To64_mips64el
#define estimateSqrt32 estimateSqrt32_mips64el
#define excnames excnames_mips64el

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_mipsel
#define error_get_class error_get_class_mipsel
#define error_get_pretty error_get_pretty_mipsel
#define error_setg_file_open error_setg_file_open_mipsel
#define error_setg_file_open_internal error_setg_file_open_internal_mipsel
#define estimateDiv128To64 estimateDiv128To64_mipsel
#define estimateSqrt32 estimateSqrt32_mipsel
#define excnames excnames_mipsel

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_powerpc
#define error_get_class error_get_class_powerpc
#define error_get_pretty error_get_pretty_powerpc
#define error_setg_file_open error_setg_file_open_powerpc
#define error_setg_file_open_internal error_setg_file_open_internal_powerpc
#define estimateDiv128To64 estimateDiv128To64_powerpc
#define estimateSqrt32 estimateSqrt32_powerpc
#define excnames excnames_powerpc

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_sparc
#define error_get_class error_get_class_sparc
#define error_get_pretty error_get_pretty_sparc
#define error_setg_file_open error_setg_file_open_sparc
#define error_setg_file_open_internal error_setg_file_open_internal_sparc
#define estimateDiv128To64 estimateDiv128To64_sparc
#define estimateSqrt32 estimateSqrt32_sparc
#define excnames excnames_sparc

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_sparc64
#define error_get_class error_get_class_sparc64
#define error_get_pretty error_get_pretty_sparc64
#define error_setg_file_open error_setg_file_open_sparc64
#define error_setg_file_open_internal error_setg_file_open_internal_sparc64
#define estimateDiv128To64 estimateDiv128To64_sparc64
#define estimateSqrt32 estimateSqrt32_sparc64
#define excnames excnames_sparc64

View file

@ -17,12 +17,24 @@ struct Error
{
char *msg;
ErrorClass err_class;
const char *src, *func;
int line;
};
Error *error_abort;
static void error_setv(Error **errp, ErrorClass err_class,
const char *fmt, va_list ap)
static void error_do_abort(Error *err)
{
fprintf(stderr, "Unexpected error in %s() at %s:%d:\n",
err->func, err->src, err->line);
// UNICORN: Commented out
// error_report_err(err);
// abort();
}
static void error_setv(Error **errp,
const char *src, int line, const char *func,
ErrorClass err_class, const char *fmt, va_list ap)
{
Error *err;
int saved_errno = errno;
@ -36,9 +48,12 @@ static void error_setv(Error **errp, ErrorClass err_class,
err->msg = g_strdup_vprintf(fmt, ap);
err->err_class = err_class;
err->src = src;
err->line = line;
err->func = func;
if (errp == &error_abort) {
// abort();
error_do_abort(err);
}
*errp = err;
@ -46,25 +61,31 @@ static void error_setv(Error **errp, ErrorClass err_class,
errno = saved_errno;
}
void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
void error_set_internal(Error **errp,
const char *src, int line, const char *func,
ErrorClass err_class, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error_setv(errp, err_class, fmt, ap);
error_setv(errp, src, line, func, err_class, fmt, ap);
va_end(ap);
}
void error_setg(Error **errp, const char *fmt, ...)
void error_setg_internal(Error **errp,
const char *src, int line, const char *func,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error_setv(errp, ERROR_CLASS_GENERIC_ERROR, fmt, ap);
error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR, fmt, ap);
va_end(ap);
}
void error_setg_errno(Error **errp, int os_errno, const char *fmt, ...)
void error_setg_errno_internal(Error **errp,
const char *src, int line, const char *func,
int os_errno, const char *fmt, ...)
{
va_list ap;
char *msg;
@ -75,7 +96,7 @@ void error_setg_errno(Error **errp, int os_errno, const char *fmt, ...)
}
va_start(ap, fmt);
error_setv(errp, ERROR_CLASS_GENERIC_ERROR, fmt, ap);
error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR, fmt, ap);
va_end(ap);
if (os_errno != 0) {
@ -87,9 +108,12 @@ void error_setg_errno(Error **errp, int os_errno, const char *fmt, ...)
errno = saved_errno;
}
void error_setg_file_open(Error **errp, int os_errno, const char *filename)
void error_setg_file_open_internal(Error **errp,
const char *src, int line, const char *func,
int os_errno, const char *filename)
{
error_setg_errno(errp, os_errno, "Could not open '%s'", filename);
error_setg_errno_internal(errp, src, line, func, os_errno,
"Could not open '%s'", filename);
}
Error *error_copy(const Error *err)
@ -124,7 +148,7 @@ void error_free(Error *err)
void error_propagate(Error **dst_errp, Error *local_err)
{
if (local_err && dst_errp == &error_abort) {
// abort();
error_do_abort(local_err);
} else if (dst_errp && !*dst_errp) {
*dst_errp = local_err;
} else if (local_err) {

View file

@ -391,7 +391,7 @@
#define error_exit error_exit_x86_64
#define error_get_class error_get_class_x86_64
#define error_get_pretty error_get_pretty_x86_64
#define error_setg_file_open error_setg_file_open_x86_64
#define error_setg_file_open_internal error_setg_file_open_internal_x86_64
#define estimateDiv128To64 estimateDiv128To64_x86_64
#define estimateSqrt32 estimateSqrt32_x86_64
#define excnames excnames_x86_64