Skip to content

Commit 6a4074d

Browse files
committed
unwind trace
1 parent a1e24d0 commit 6a4074d

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

include/qemu/osdep.h

+41
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ QEMU_EXTERN_C int daemon(int, int);
133133
#include <setjmp.h>
134134
#include <signal.h>
135135

136+
#define UNW_LOCAL_ONLY
137+
#include <libunwind.h>
138+
136139
#ifdef CONFIG_IOVEC
137140
#include <sys/uio.h>
138141
#endif
@@ -812,6 +815,44 @@ static inline void qemu_thread_jit_write(void) {}
812815
static inline void qemu_thread_jit_execute(void) {}
813816
#endif
814817

818+
/* returns caller, return 0 if it worked */
819+
static inline __attribute__((always_inline))
820+
int unwind_caller(GString *res)
821+
{
822+
unw_cursor_t cursor;
823+
unw_context_t context;
824+
825+
unw_getcontext(&context);
826+
unw_init_local(&cursor, &context);
827+
828+
bool unwind_success = unw_step(&cursor);
829+
if (!unwind_success) {
830+
g_string_printf(res, "<unknown - can't unwind>");
831+
return -1;
832+
}
833+
834+
unw_word_t offset;
835+
char sym[256];
836+
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) != 0) {
837+
g_string_printf(res, "<unknown - can't get proc info>");
838+
return -2;
839+
}
840+
841+
g_string_printf(res, "%s+0x%"PRIx64, sym, offset);
842+
return 0;
843+
}
844+
845+
static inline __attribute__((always_inline))
846+
const char* qemu_get_caller(void)
847+
{
848+
static __thread GString *info;
849+
if (!info) {
850+
info = g_string_new(0);
851+
}
852+
unwind_caller(info);
853+
return info->str;
854+
}
855+
815856
/**
816857
* Platforms which do not support system() return ENOSYS
817858
*/

meson.build

+4
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,9 @@ have_xen_pci_passthrough = get_option('xen_pci_passthrough') \
954954
# Dependencies #
955955
################
956956

957+
libunwind = dependency('libunwind', required: true, method: 'pkg-config')
958+
add_project_dependencies(libunwind, language: all_languages)
959+
957960
# When bumping glib minimum version, please check also whether to increase
958961
# the _WIN32_WINNT setting in osdep.h according to the value from glib.
959962
# 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
35773580
stub_ss = stub_ss.apply({})
35783581

35793582
util_ss.add_all(trace_ss)
3583+
util_ss.add(libunwind)
35803584
util_ss = util_ss.apply({})
35813585
libqemuutil = static_library('qemuutil',
35823586
build_by_default: false,

0 commit comments

Comments
 (0)