Skip to contentSkip to conversation

perf tools: Add dump_stack function

tip:perf/core tip-bot for Arnaldo Carvalho de Melo 1 participant 1 message Aug 21, 2012 perf events

lore
Share & tools
lore
#0 lore raw
Commit-ID:  dc4552bf7176573ccf79af04ab8648b015738f4a
Gitweb:     http://git.kernel.org/tip/dc4552bf7176573ccf79af04ab8648b015738f4a
Author:     Arnaldo Carvalho de Melo <[email protected]>
AuthorDate: Tue, 7 Aug 2012 23:32:05 -0300
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 7 Aug 2012 23:32:05 -0300

perf tools: Add dump_stack function

To help in debugging the tools, provides functionality roughly similar to the function with the same name in the kernel.

Copied from glibc backtrace function man page.

Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/util/util.c |   19 +++++++++++++++++++
 tools/perf/util/util.h |    2 ++
 2 files changed, 21 insertions(+), 0 deletions(-)
tools/perf/util/util.c +19 -0
index d03599f..1b8775c 100644
@@ -1,6 +1,9 @@
 #include "../perf.h"
 #include "util.h"
 #include <sys/mman.h>
+#include <execinfo.h>
+#include <stdio.h>
+#include <stdlib.h>
 
 /*
  * XXX We need to find a better place for these things...
@@ -158,3 +161,19 @@ size_t hex_width(u64 v)
 
 	return n;
 }
+
+/* Obtain a backtrace and print it to stdout. */
+void dump_stack(void)
+{
+	void *array[16];
+	size_t size = backtrace(array, ARRAY_SIZE(array));
+	char **strings = backtrace_symbols(array, size);
+	size_t i;
+
+	printf("Obtained %zd stack frames.\n", size);
+
+	for (i = 0; i < size; i++)
+		printf("%s\n", strings[i]);
+
+	free(strings);
+}
tools/perf/util/util.h +2 -0
index b13c733..00a93a9 100644
@@ -266,4 +266,6 @@ size_t hex_width(u64 v);
 
 char *rtrim(char *s);
 
+void dump_stack(void);
+
 #endif