[v4,06/19] utils: Add print_stack_trace()

Submitted by Kirill Tkhai on Jan. 10, 2018, 2:01 p.m.

Details

Message ID 151559287533.31108.13426385401741952313.stgit@localhost.localdomain
State Accepted
Series "Introduce custom per-task service fds placement"
Commit b5bea099f661c6a8ffaa0241ee08c713f5cfcec1
Headers show

Commit Message

Kirill Tkhai Jan. 10, 2018, 2:01 p.m.
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: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 criu/include/util.h |    5 +++++
 criu/util.c         |   18 ++++++++++++++++++
 2 files changed, 23 insertions(+)

Patch hide | download patch | download mbox

diff --git a/criu/include/util.h b/criu/include/util.h
index c40b0c6f9..0ebdb4b80 100644
--- a/criu/include/util.h
+++ b/criu/include/util.h
@@ -351,6 +351,11 @@  extern int epoll_prepare(int nr_events, struct epoll_event **evs);
 extern int open_fd_of_vpid(pid_t pid, int fd, int flags);
 
 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;						\
diff --git a/criu/util.c b/criu/util.c
index 88f4914a0..d90bab09c 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -1487,6 +1487,24 @@  int call_in_child_process(int (*fn)(void *), void *arg)
 	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
+
 /*
  * In glibc 2.24, getpid() returns a parent PID, if a child was
  * created with the CLONE_VM flag.