summaryrefslogtreecommitdiffstats
path: root/libcorkscrew/test.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2012-05-24 19:03:07 -0700
committerBrian Carlstrom <bdc@google.com>2013-05-14 19:49:22 -0700
commitbfec3a315000e9fcfc8e2d3fcef25e747e3d2053 (patch)
tree8e8c717620381e8f480470cce4517b54d57ed729 /libcorkscrew/test.cpp
parent7382459eca9632168b9d393bc77fb62bc21bb67a (diff)
downloadsystem_core-bfec3a315000e9fcfc8e2d3fcef25e747e3d2053.zip
system_core-bfec3a315000e9fcfc8e2d3fcef25e747e3d2053.tar.gz
system_core-bfec3a315000e9fcfc8e2d3fcef25e747e3d2053.tar.bz2
Add basic Darwin support to libcorkscrew.
This might not seem like much, but it's already better than Mac OS' backtrace(3). (cherry picked from commit 2a46f6293f19b9d6100187fbd53088088e671b88) Change-Id: I197b06c4c19f728ad6f194b2f473bd9dcd99b8a6
Diffstat (limited to 'libcorkscrew/test.cpp')
-rw-r--r--libcorkscrew/test.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/libcorkscrew/test.cpp b/libcorkscrew/test.cpp
new file mode 100644
index 0000000..6f7be3b
--- /dev/null
+++ b/libcorkscrew/test.cpp
@@ -0,0 +1,71 @@
+#include <corkscrew/backtrace.h>
+#include <corkscrew/symbol_table.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int do_backtrace(float /* just to test demangling */) {
+ const size_t MAX_DEPTH = 32;
+ backtrace_frame_t* frames = (backtrace_frame_t*) malloc(sizeof(backtrace_frame_t) * MAX_DEPTH);
+ ssize_t frame_count = unwind_backtrace(frames, 0, MAX_DEPTH);
+ fprintf(stderr, "frame_count=%d\n", (int) frame_count);
+ if (frame_count <= 0) {
+ return frame_count;
+ }
+
+ backtrace_symbol_t* backtrace_symbols = (backtrace_symbol_t*) malloc(sizeof(backtrace_symbol_t) * frame_count);
+ get_backtrace_symbols(frames, frame_count, backtrace_symbols);
+
+ for (size_t i = 0; i < (size_t) frame_count; ++i) {
+ char line[MAX_BACKTRACE_LINE_LENGTH];
+ format_backtrace_line(i, &frames[i], &backtrace_symbols[i],
+ line, MAX_BACKTRACE_LINE_LENGTH);
+ if (backtrace_symbols[i].symbol_name != NULL) {
+ // get_backtrace_symbols found the symbol's name with dladdr(3).
+ fprintf(stderr, " %s\n", line);
+ } else {
+ // We don't have a symbol. Maybe this is a static symbol, and
+ // we can look it up?
+ symbol_table_t* symbols = NULL;
+ if (backtrace_symbols[i].map_name != NULL) {
+ symbols = load_symbol_table(backtrace_symbols[i].map_name);
+ }
+ const symbol_t* symbol = NULL;
+ if (symbols != NULL) {
+ symbol = find_symbol(symbols, frames[i].absolute_pc);
+ }
+ if (symbol != NULL) {
+ int offset = frames[i].absolute_pc - symbol->start;
+ fprintf(stderr, " %s (%s%+d)\n", line, symbol->name, offset);
+ } else {
+ fprintf(stderr, " %s (\?\?\?)\n", line);
+ }
+ free_symbol_table(symbols);
+ }
+ }
+
+ free_backtrace_symbols(backtrace_symbols, frame_count);
+ free(backtrace_symbols);
+ free(frames);
+ return frame_count;
+}
+
+struct C {
+ int g(int i);
+};
+
+__attribute__ ((noinline)) int C::g(int i) {
+ if (i == 0) {
+ return do_backtrace(0.1);
+ }
+ return g(i - 1);
+}
+
+extern "C" __attribute__ ((noinline)) int f() {
+ C c;
+ return c.g(5);
+}
+
+int main() {
+ f();
+ return 0;
+}