From 25deaa1b85dee71a6c5d40f8d86426eff006901d Mon Sep 17 00:00:00 2001 From: Pierrick Bouvier Date: Fri, 11 Oct 2024 15:00:46 -0700 Subject: [PATCH] unwind trace --- include/qemu/osdep.h | 41 +++++++++++++++++++++++++++++++++++++++++ meson.build | 4 ++++ 2 files changed, 45 insertions(+) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index fe7c3c5f67314..d078ee6dae62f 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -133,6 +133,9 @@ QEMU_EXTERN_C int daemon(int, int); #include #include +#define UNW_LOCAL_ONLY +#include + #ifdef CONFIG_IOVEC #include #endif @@ -812,6 +815,44 @@ static inline void qemu_thread_jit_write(void) {} static inline void qemu_thread_jit_execute(void) {} #endif +/* returns caller, return 0 if it worked */ +static inline __attribute__((always_inline)) +int unwind_caller(GString *res) +{ + unw_cursor_t cursor; + unw_context_t context; + + unw_getcontext(&context); + unw_init_local(&cursor, &context); + + bool unwind_success = unw_step(&cursor); + if (!unwind_success) { + g_string_printf(res, ""); + return -1; + } + + unw_word_t offset; + char sym[256]; + if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) != 0) { + g_string_printf(res, ""); + return -2; + } + + g_string_printf(res, "%s+0x%"PRIx64, sym, offset); + return 0; +} + +static inline __attribute__((always_inline)) +const char* qemu_get_caller(void) +{ + static __thread GString *info; + if (!info) { + info = g_string_new(0); + } + unwind_caller(info); + return info->str; +} + /** * Platforms which do not support system() return ENOSYS */ diff --git a/meson.build b/meson.build index d26690ce20401..8a1bfe9e3113d 100644 --- a/meson.build +++ b/meson.build @@ -954,6 +954,9 @@ have_xen_pci_passthrough = get_option('xen_pci_passthrough') \ # Dependencies # ################ +libunwind = dependency('libunwind', required: true, method: 'pkg-config') +add_project_dependencies(libunwind, language: all_languages) + # When bumping glib minimum version, please check also whether to increase # the _WIN32_WINNT setting in osdep.h according to the value from glib. # You should also check if any of the glib.version() checks @@ -3577,6 +3580,7 @@ event_loop_base = declare_dependency(objects: event_loop_base.extract_all_object stub_ss = stub_ss.apply({}) util_ss.add_all(trace_ss) +util_ss.add(libunwind) util_ss = util_ss.apply({}) libqemuutil = static_library('qemuutil', build_by_default: false,