From: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Markus Armbruster" <armbru@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Gustavo Romero" <gustavo.romero@linaro.org>,
"Pierrick Bouvier" <pierrick.bouvier@linaro.org>,
"Manos Pitsidianakis" <manos.pitsidianakis@linaro.org>
Subject: [PATCH RFC] util/error.c: Print backtrace on error
Date: Tue, 05 Aug 2025 12:19:26 +0300 [thread overview]
Message-ID: <20250805-backtrace-v1-1-d189d09b1e92@linaro.org> (raw)
Add a backtrace_on_error meson feature (enabled with
--enable-backtrace-on-error) that compiles system binaries with
-rdynamic option and prints a function backtrace on error to stderr.
Example output by adding an unconditional error_setg on error_abort in hw/arm/boot.c:
./qemu-system-aarch64(+0x13b4a2c) [0x55d015406a2c]
./qemu-system-aarch64(+0x13b4abd) [0x55d015406abd]
./qemu-system-aarch64(+0x13b4d49) [0x55d015406d49]
./qemu-system-aarch64(error_setg_internal+0xe7) [0x55d015406f62]
./qemu-system-aarch64(arm_load_dtb+0xbf) [0x55d014d7686f]
./qemu-system-aarch64(+0xd2f1d8) [0x55d014d811d8]
./qemu-system-aarch64(notifier_list_notify+0x44) [0x55d01540a282]
./qemu-system-aarch64(qdev_machine_creation_done+0xa0) [0x55d01476ae17]
./qemu-system-aarch64(+0xaa691e) [0x55d014af891e]
./qemu-system-aarch64(qmp_x_exit_preconfig+0x72) [0x55d014af8a5d]
./qemu-system-aarch64(qemu_init+0x2a89) [0x55d014afb657]
./qemu-system-aarch64(main+0x2f) [0x55d01521e836]
/lib/x86_64-linux-gnu/libc.so.6(+0x29ca8) [0x7f3033d67ca8]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x85) [0x7f3033d67d65]
./qemu-system-aarch64(_start+0x21) [0x55d0146814f1]
Unexpected error in arm_load_dtb() at ../hw/arm/boot.c:529:
For qemu-system-aarch64, this adds an additional 2MB to the binary size
so this is not enabled by default. I also only tested this on Linux, it
might not be portable.
Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
---
meson.build | 3 +++
meson_options.txt | 2 ++
scripts/meson-buildoptions.sh | 4 ++++
util/error.c | 17 +++++++++++++++++
4 files changed, 26 insertions(+)
diff --git a/meson.build b/meson.build
index e53cd5b413847f33c972540e1f67a092164a200d..5d6ec5a5d5cb4c176bb32450f2adf85fc4963105 100644
--- a/meson.build
+++ b/meson.build
@@ -2649,6 +2649,7 @@ config_host_data.set('CONFIG_DEBUG_STACK_USAGE', get_option('debug_stack_usage')
config_host_data.set('CONFIG_DEBUG_TCG', get_option('debug_tcg'))
config_host_data.set('CONFIG_DEBUG_REMAP', get_option('debug_remap'))
config_host_data.set('CONFIG_QOM_CAST_DEBUG', get_option('qom_cast_debug'))
+config_host_data.set('CONFIG_BACKTRACE', get_option('backtrace_on_error'))
config_host_data.set('CONFIG_REPLICATION', get_option('replication').allowed())
config_host_data.set('CONFIG_FSFREEZE', qga_fsfreeze)
config_host_data.set('CONFIG_FSTRIM', qga_fstrim)
@@ -4454,6 +4455,7 @@ foreach target : target_dirs
endif
emulator = executable(exe_name, exe['sources'],
+ export_dynamic: get_option('backtrace_on_error'),
install: true,
c_args: c_args,
dependencies: arch_deps + exe['dependencies'],
@@ -4714,6 +4716,7 @@ if 'simple' in get_option('trace_backends')
summary_info += {'Trace output file': get_option('trace_file') + '-<pid>'}
endif
summary_info += {'QOM debugging': get_option('qom_cast_debug')}
+summary_info += {'Backtrace support': get_option('backtrace_on_error')}
summary_info += {'Relocatable install': get_option('relocatable')}
summary_info += {'vhost-kernel support': have_vhost_kernel}
summary_info += {'vhost-net support': have_vhost_net}
diff --git a/meson_options.txt b/meson_options.txt
index dd335307505fb52c5be469aa4fab8883c93ec5c9..450c17349263429e59413b0c483c2d52db13325e 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -368,6 +368,8 @@ option('debug_stack_usage', type: 'boolean', value: false,
description: 'measure coroutine stack usage')
option('qom_cast_debug', type: 'boolean', value: true,
description: 'cast debugging support')
+option('backtrace_on_error', type: 'boolean', value: false,
+ description: 'print backtrace on error')
option('slirp_smbd', type : 'feature', value : 'auto',
description: 'use smbd (at path --smbd=*) in slirp networking')
diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh
index d559e260ed17e50a423aa25b27a86777489565d7..0dd28c93e4d8bd24f7343745c6462aa5d9a603ee 100644
--- a/scripts/meson-buildoptions.sh
+++ b/scripts/meson-buildoptions.sh
@@ -22,6 +22,8 @@ meson_options_help() {
printf "%s\n" ' --docdir=VALUE Base directory for documentation installation'
printf "%s\n" ' (can be empty) [share/doc]'
printf "%s\n" ' --enable-asan enable address sanitizer'
+ printf "%s\n" ' --enable-backtrace-on-error'
+ printf "%s\n" ' print backtrace on error'
printf "%s\n" ' --enable-block-drv-whitelist-in-tools'
printf "%s\n" ' use block whitelist also in tools instead of only'
printf "%s\n" ' QEMU'
@@ -251,6 +253,8 @@ _meson_option_parse() {
--disable-gcov) printf "%s" -Db_coverage=false ;;
--enable-lto) printf "%s" -Db_lto=true ;;
--disable-lto) printf "%s" -Db_lto=false ;;
+ --enable-backtrace-on-error) printf "%s" -Dbacktrace_on_error=true ;;
+ --disable-backtrace-on-error) printf "%s" -Dbacktrace_on_error=false ;;
--bindir=*) quote_sh "-Dbindir=$2" ;;
--enable-blkio) printf "%s" -Dblkio=enabled ;;
--disable-blkio) printf "%s" -Dblkio=disabled ;;
diff --git a/util/error.c b/util/error.c
index daea2142f30121abc46e5342526f5eec40ea246e..d834756abd563f4108b7599bb4da1e5dd85ec46c 100644
--- a/util/error.c
+++ b/util/error.c
@@ -17,13 +17,28 @@
#include "qemu/error-report.h"
#include "qapi/error-internal.h"
+#ifdef CONFIG_BACKTRACE
+#include <execinfo.h>
+#endif /* CONFIG_BACKTRACE */
+
Error *error_abort;
Error *error_fatal;
Error *error_warn;
+static inline void dump_backtrace(void)
+{
+#ifdef CONFIG_BACKTRACE
+ void *buffer[255] = { 0 };
+ const int calls =
+ backtrace(buffer, sizeof(buffer) / sizeof(void *));
+ backtrace_symbols_fd(buffer, calls, 2);
+#endif /* CONFIG_BACKTRACE */
+}
+
static void error_handle(Error **errp, Error *err)
{
if (errp == &error_abort) {
+ dump_backtrace();
if (err->func) {
fprintf(stderr, "Unexpected error in %s() at %.*s:%d:\n",
err->func, err->src_len, err->src, err->line);
@@ -38,10 +53,12 @@ static void error_handle(Error **errp, Error *err)
abort();
}
if (errp == &error_fatal) {
+ dump_backtrace();
error_report_err(err);
exit(1);
}
if (errp == &error_warn) {
+ dump_backtrace();
warn_report_err(err);
} else if (errp && !*errp) {
*errp = err;
---
base-commit: a41280fd5b94c49089f7631c6fa8bb9c308b7962
change-id: 20250805-backtrace-e8e4c2490b46
--
γαῖα πυρί μιχθήτω
next reply other threads:[~2025-08-05 9:20 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-05 9:19 Manos Pitsidianakis [this message]
2025-08-05 15:59 ` [PATCH RFC] util/error.c: Print backtrace on error Daniel P. Berrangé
2025-08-05 16:22 ` Manos Pitsidianakis
2025-08-05 16:48 ` Daniel P. Berrangé
2025-08-05 16:57 ` Manos Pitsidianakis
2025-08-05 18:00 ` Daniel P. Berrangé
2025-08-06 11:11 ` Alex Bennée
2025-08-06 11:34 ` Daniel P. Berrangé
2025-08-06 20:26 ` Pierrick Bouvier
2025-08-07 5:41 ` Markus Armbruster
2025-08-18 16:49 ` Daniel P. Berrangé
2025-08-18 16:52 ` Daniel P. Berrangé
2025-08-07 5:23 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250805-backtrace-v1-1-d189d09b1e92@linaro.org \
--to=manos.pitsidianakis@linaro.org \
--cc=alex.bennee@linaro.org \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=gustavo.romero@linaro.org \
--cc=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=philmd@linaro.org \
--cc=pierrick.bouvier@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).