diff --git a/Makefile b/Makefile index 684890283..a41ce764b 100644 --- a/Makefile +++ b/Makefile @@ -85,7 +85,7 @@ LIB_LDFLAGS = $(COMMON_LDFLAGS) $(LDFLAGS_$@) $(LDFLAGS_lib) -Wl,--no-und TEST_LDFLAGS = $(COMMON_LDFLAGS) -L$(objdir)/libtraceevent -ltraceevent ifeq ($(DEBUG), 1) - COMMON_CFLAGS += -O0 -g + COMMON_CFLAGS += -O0 -g -rdynamic else COMMON_CFLAGS += -O2 -g endif diff --git a/utils/utils.c b/utils/utils.c index 528a902c3..e651f43f1 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -881,6 +881,23 @@ char *absolute_dirname(const char *path, char *resolved_path) return resolved_path; } +void dump_stacktrace(void **buffer, int nptrs) +{ + int i; + char **strings; + + pr_yellow("Stack trace:\n"); + strings = backtrace_symbols(buffer, nptrs); + for (i = 0; i < nptrs; i++) { + if (strings) + pr_yellow(" #%d %s\n", i, strings[i]); + else + pr_yellow(" #%d %p\n", i, buffer[i]); + } + pr_yellow("\n"); + free(strings); +} + #ifdef UNIT_TEST TEST_CASE(utils_parse_cmdline) { diff --git a/utils/utils.h b/utils/utils.h index f4b58c85a..7689344c7 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -17,6 +17,7 @@ #include #include #include +#include #include "compiler.h" @@ -366,4 +367,13 @@ struct uftrace_data; char *get_event_name(struct uftrace_data *handle, unsigned evt_id); char *absolute_dirname(const char *path, char *resolved_path); +void dump_stacktrace(void **buffer, int nptrs); + +#define stacktrace() \ + do { \ + void *buffer[64]; \ + int nptrs = backtrace(buffer, 64); \ + dump_stacktrace(buffer, nptrs); \ + } while (0) + #endif /* UFTRACE_UTILS_H */