perf tools: Add dump_stack function
authorArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 8 Aug 2012 02:32:05 +0000 (23:32 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 8 Aug 2012 02:32:05 +0000 (23:32 -0300)
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 <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Namhyung Kim <namhyung@gmail.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-6nw2sak21bqy8h1m2syyo816@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/util.c
tools/perf/util/util.h

index d03599fbe78bab080e59a096c3629b650bc12e84..1b8775c3707d483676c170bbf67d220d8ef1c14a 100644 (file)
@@ -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);
+}
index b13c7331eaf8cbf89a8a1b6626238e44ab84d657..00a93a91a23542d04c29b906f7be866295bf7ab6 100644 (file)
@@ -266,4 +266,6 @@ size_t hex_width(u64 v);
 
 char *rtrim(char *s);
 
+void dump_stack(void);
+
 #endif