[CRIU] [PATCH 1/3] utils: Add print_stack_trace()

Kirill Tkhai ktkhai at virtuozzo.com
Mon May 29 07:01:39 PDT 2017


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.

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 criu/include/util.h |    1 +
 criu/util.c         |   18 ++++++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/criu/include/util.h b/criu/include/util.h
index 91587ef1c..c1dd66676 100644
--- a/criu/include/util.h
+++ b/criu/include/util.h
@@ -310,6 +310,7 @@ extern int epoll_prepare(int nr_events, struct epoll_event **evs);
 extern int open_fd_of_real_pid(pid_t pid, int fd, int flags);
 
 extern int call_in_child_process(int (*fn)(void *), void *arg);
+extern void print_stack_trace(pid_t pid);
 
 #define block_sigmask(saved_mask, sig_mask)	({					\
 		sigset_t ___blocked_mask;						\
diff --git a/criu/util.c b/criu/util.c
index 3363eef99..fef3ce7c8 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -1397,6 +1397,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