2015-08-21 07:04:50 +00:00
|
|
|
/*
|
|
|
|
* QEMU Error Objects
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
2018-02-20 13:22:25 +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-20 13:22:25 +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-19 06:27:52 +00:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Backports commit da34e65cb4025728566d6504a99916f6e7e1dd6a from qemu
2018-02-22 04:05:15 +00:00
|
|
|
#include "qapi/error.h"
|
2015-08-21 07:04:50 +00:00
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "qapi/error.h"
|
|
|
|
|
|
|
|
struct Error
|
|
|
|
{
|
|
|
|
char *msg;
|
|
|
|
ErrorClass err_class;
|
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
|
|
|
const char *src, *func;
|
|
|
|
int line;
|
2015-08-21 07:04:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Error *error_abort;
|
2018-02-20 13:22:25 +00:00
|
|
|
Error *error_fatal;
|
2015-08-21 07:04:50 +00:00
|
|
|
|
2018-02-20 13:22:25 +00:00
|
|
|
static void error_handle_fatal(Error **errp, Error *err)
|
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
|
|
|
{
|
2018-02-20 13:22:25 +00:00
|
|
|
if (errp == &error_abort) {
|
|
|
|
fprintf(stderr, "Unexpected error in %s() at %s:%d:\n",
|
|
|
|
err->func, err->src, err->line);
|
|
|
|
|
|
|
|
// Unicorn: commented out
|
|
|
|
//error_report_err(err);
|
|
|
|
//abort();
|
|
|
|
}
|
|
|
|
if (errp == &error_fatal) {
|
|
|
|
// Unicorn: commented out
|
|
|
|
//error_report_err(err);
|
|
|
|
//exit(1);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void error_setv(Error **errp,
|
|
|
|
const char *src, int line, const char *func,
|
2018-02-22 02:39:59 +00:00
|
|
|
ErrorClass err_class, const char *fmt, va_list ap,
|
|
|
|
const char *suffix)
|
2015-08-21 07:04:50 +00:00
|
|
|
{
|
|
|
|
Error *err;
|
|
|
|
int saved_errno = errno;
|
|
|
|
|
|
|
|
if (errp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(*errp == NULL);
|
|
|
|
|
|
|
|
err = g_malloc0(sizeof(*err));
|
|
|
|
|
|
|
|
err->msg = g_strdup_vprintf(fmt, ap);
|
2018-02-22 02:39:59 +00:00
|
|
|
if (suffix) {
|
|
|
|
char *msg = err->msg;
|
|
|
|
err->msg = g_strdup_printf("%s: %s", msg, suffix);
|
|
|
|
g_free(msg);
|
|
|
|
}
|
2015-08-21 07:04:50 +00:00
|
|
|
err->err_class = err_class;
|
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
|
|
|
err->src = src;
|
|
|
|
err->line = line;
|
|
|
|
err->func = func;
|
2015-08-21 07:04:50 +00:00
|
|
|
|
2018-02-20 13:22:25 +00:00
|
|
|
error_handle_fatal(errp, err);
|
2015-08-21 07:04:50 +00:00
|
|
|
*errp = err;
|
|
|
|
|
|
|
|
errno = saved_errno;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
void error_set_internal(Error **errp,
|
|
|
|
const char *src, int line, const char *func,
|
|
|
|
ErrorClass err_class, const char *fmt, ...)
|
2018-02-15 16:23:02 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-02-22 02:39:59 +00:00
|
|
|
error_setv(errp, src, line, func, err_class, fmt, ap, NULL);
|
2018-02-15 16:23:02 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
void error_setg_internal(Error **errp,
|
|
|
|
const char *src, int line, const char *func,
|
|
|
|
const char *fmt, ...)
|
2018-02-15 16:27:00 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-02-22 02:39:59 +00:00
|
|
|
error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR, fmt, ap, NULL);
|
2018-02-15 16:27:00 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
void error_setg_errno_internal(Error **errp,
|
|
|
|
const char *src, int line, const char *func,
|
|
|
|
int os_errno, const char *fmt, ...)
|
2015-08-21 07:04:50 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int saved_errno = errno;
|
|
|
|
|
|
|
|
if (errp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-02-22 02:39:59 +00:00
|
|
|
error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR, fmt, ap,
|
|
|
|
os_errno != 0 ? strerror(os_errno) : NULL);
|
2015-08-21 07:04:50 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
errno = saved_errno;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
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
|
|
|
{
|
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
|
|
|
error_setg_errno_internal(errp, src, line, func, os_errno,
|
|
|
|
"Could not open '%s'", filename);
|
2015-08-21 07:04:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Error *error_copy(const Error *err)
|
|
|
|
{
|
|
|
|
Error *err_new;
|
|
|
|
|
|
|
|
err_new = g_malloc0(sizeof(*err));
|
|
|
|
err_new->msg = g_strdup(err->msg);
|
|
|
|
err_new->err_class = err->err_class;
|
2018-02-16 05:03:18 +00:00
|
|
|
err_new->src = err->src;
|
|
|
|
err_new->line = err->line;
|
|
|
|
err_new->func = err->func;
|
2015-08-21 07:04:50 +00:00
|
|
|
|
|
|
|
return err_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorClass error_get_class(const Error *err)
|
|
|
|
{
|
|
|
|
return err->err_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *error_get_pretty(Error *err)
|
|
|
|
{
|
|
|
|
return err->msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void error_free(Error *err)
|
|
|
|
{
|
|
|
|
if (err) {
|
2016-12-21 14:28:36 +00:00
|
|
|
g_free(err->msg);
|
|
|
|
g_free(err);
|
2015-08-21 07:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void error_propagate(Error **dst_errp, Error *local_err)
|
|
|
|
{
|
2018-02-20 13:22:25 +00:00
|
|
|
if (!local_err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
error_handle_fatal(dst_errp, local_err);
|
|
|
|
if (dst_errp && !*dst_errp) {
|
2015-08-21 07:04:50 +00:00
|
|
|
*dst_errp = local_err;
|
2018-02-20 13:22:25 +00:00
|
|
|
} else {
|
2015-08-21 07:04:50 +00:00
|
|
|
error_free(local_err);
|
|
|
|
}
|
|
|
|
}
|