Commit 2f301786 authored by Kirill Tkhai's avatar Kirill Tkhai Committed by Andrei Vagin

utils: Add print_stack_trace()

Function to print call trace of a process.

Borrowed from this fm:
https://www.gnu.org/software/libc/manual/html_node/Backtraces.html

backtrace() and backtrace_symbols() are not implemented in alpine,
so we use __GLIBC__ ifdef to do not compile this function there.

v4: New
Signed-off-by: 's avatarKirill Tkhai <ktkhai@virtuozzo.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent c8449dbb
......@@ -348,6 +348,11 @@ extern int epoll_run_rfds(int epfd, struct epoll_event *evs, int nr_fds, int tmo
extern int epoll_prepare(int nr_events, struct epoll_event **evs);
extern int call_in_child_process(int (*fn)(void *), void *arg);
#ifdef __GLIBC__
extern void print_stack_trace(pid_t pid);
#else
static inline void print_stack_trace(pid_t pid) {}
#endif
#define block_sigmask(saved_mask, sig_mask) ({ \
sigset_t ___blocked_mask; \
......
......@@ -1407,3 +1407,21 @@ out:
close_pid_proc();
return ret;
}
#ifdef __GLIBC__
#include <execinfo.h>
void print_stack_trace(pid_t pid)
{
void *array[10];
char **strings;
size_t size, i;
size = backtrace(array, 10);
strings = backtrace_symbols(array, size);
for (i = 0; i < size; i++)
pr_err("stack %d#%zu: %s\n", pid, i, strings[i]);
free(strings);
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment