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

Kirill Tkhai ktkhai at virtuozzo.com
Wed Jan 10 17:01:15 MSK 2018


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 at virtuozzo.com>
---
 criu/include/util.h |    5 +++++
 criu/util.c         |   18 ++++++++++++++++++
 2 files changed, 23 insertions(+)

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.



More information about the CRIU mailing list