Skip to content

Commit

Permalink
unwind trace
Browse files Browse the repository at this point in the history
  • Loading branch information
pbo-linaro committed Oct 15, 2024
1 parent 3b69fff commit 25deaa1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
41 changes: 41 additions & 0 deletions include/qemu/osdep.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ QEMU_EXTERN_C int daemon(int, int);
#include <setjmp.h>
#include <signal.h>

#define UNW_LOCAL_ONLY
#include <libunwind.h>

#ifdef CONFIG_IOVEC
#include <sys/uio.h>
#endif
Expand Down Expand Up @@ -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, "<unknown - can't unwind>");
return -1;
}

unw_word_t offset;
char sym[256];
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) != 0) {
g_string_printf(res, "<unknown - can't get proc info>");
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
*/
Expand Down
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 25deaa1

Please sign in to comment.