[PATCH] PowerPC: use libgcc _Unwind functions to get backtrace
Adhemerval Zanella
azanella@linux.vnet.ibm.com
Sun Jul 21 00:21:00 GMT 2013
This patch changes the way backtraces are obtained on PowerPC by
relaying on libgcc _Unwind function instead of just backchain parse.
It fixes debug/tst-backtrace5 for PPC32 and PPC64.
Tested on PPC32 and PPC64.
---
2013-07-17 Adhemerval Zanella <azanella@linux.vnet.ibm.com>
* sysdeps/powerpc/Makefile: Add -funwind-tables to backtrace.c.
* sysdeps/powerpc/backtrace.c: New file: common PowerPC backtrace
code.
* sysdeps/powerpc/powerpc32/backtrace.c (__backtrace): Rename to
__backchain_backtrace and define it only for shared builds.
* sysdeps/powerpc/powerpc64/backtrace.c (__backtrace): Likewise.
--
diff --git a/sysdeps/powerpc/Makefile b/sysdeps/powerpc/Makefile
index f75e625..53fa8fb 100644
--- a/sysdeps/powerpc/Makefile
+++ b/sysdeps/powerpc/Makefile
@@ -2,6 +2,10 @@ ifeq ($(subdir),string)
CFLAGS-memcmp.c += -Wno-uninitialized
endif
+ifeq ($(subdir),debug)
+CFLAGS-backtrace.c += -funwind-tables
+endif
+
ifeq ($(subdir),elf)
# extra shared linker files to link into dl-allobjs.so and libc
sysdep-dl-routines += dl-machine
diff --git a/sysdeps/powerpc/backtrace.c b/sysdeps/powerpc/backtrace.c
new file mode 100644
index 0000000..06f41e1
--- /dev/null
+++ b/sysdeps/powerpc/backtrace.c
@@ -0,0 +1,129 @@
+/* Return backtrace of current program state.
+ Copyright (C) 2013 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If
+ not, see <http://www.gnu.org/licenses/>. */
+
+#include <execinfo.h>
+#include <dlfcn.h>
+#include <stddef.h>
+#include <unwind.h>
+
+
+struct trace_arg
+{
+ void **array;
+ _Unwind_Word cfa;
+ int cnt;
+ int size;
+};
+
+#ifdef SHARED
+static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
+static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
+static _Unwind_Word (*unwind_getcfa) (struct _Unwind_Context *);
+static void *libgcc_handle;
+
+/* Dummy version in case libgcc_s does not contain the real code. */
+static _Unwind_Word
+dummy_getcfa (struct _Unwind_Context *ctx __attribute__ ((unused)))
+{
+ return 0;
+}
+
+static void
+init (void)
+{
+ libgcc_handle = __libc_dlopen ("libgcc_s.so.1");
+
+ if (libgcc_handle == NULL)
+ return;
+
+ unwind_backtrace = __libc_dlsym (libgcc_handle, "_Unwind_Backtrace");
+ unwind_getip = __libc_dlsym (libgcc_handle, "_Unwind_GetIP");
+ if (unwind_getip == NULL)
+ unwind_backtrace = NULL;
+ unwind_getcfa = (__libc_dlsym (libgcc_handle, "_Unwind_GetCFA")
+ ?: dummy_getcfa);
+}
+
+#else
+# define unwind_backtrace _Unwind_Backtrace
+# define unwind_getip _Unwind_GetIP
+# define unwind_getcfa _Unwind_GetCFA
+#endif
+
+static _Unwind_Reason_Code
+backtrace_helper (struct _Unwind_Context *ctx, void *a)
+{
+ struct trace_arg *arg = a;
+
+ /* We are first called with address in the __backtrace function.
+ Skip it. */
+ if (arg->cnt != -1)
+ {
+ arg->array[arg->cnt] = (void *) unwind_getip (ctx);
+
+ /* Check whether we make any progress. */
+ _Unwind_Word cfa = unwind_getcfa (ctx);
+
+ if (arg->cnt > 0 && arg->array[arg->cnt - 1] == arg->array[arg->cnt]
+ && cfa == arg->cfa)
+ return _URC_END_OF_STACK;
+ arg->cfa = cfa;
+ }
+ if (++arg->cnt == arg->size)
+ return _URC_END_OF_STACK;
+ return _URC_NO_REASON;
+}
+
+
+int
+__backtrace (void **array, int size)
+{
+ struct trace_arg arg = { .array = array, .cfa = 0, .size = size, .cnt = -1 };
+#ifdef SHARED
+ __libc_once_define (static, once);
+ __libc_once (once, init);
+
+ if (unwind_backtrace == NULL)
+ return __backchain_backtrace (array, size);
+#endif
+
+ if (size >= 1)
+ unwind_backtrace (backtrace_helper, &arg);
+
+ /* _Unwind_Backtrace seems to put NULL address above
+ _start. Fix it up here. */
+ if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
+ --arg.cnt;
+ return arg.cnt != -1 ? arg.cnt : 0;
+}
+weak_alias (__backtrace, backtrace)
+libc_hidden_def (__backtrace)
+
+
+#ifdef SHARED
+/* Free all resources if necessary. */
+libc_freeres_fn (free_mem)
+{
+ unwind_backtrace = NULL;
+ if (libgcc_handle != NULL)
+ {
+ __libc_dlclose (libgcc_handle);
+ libgcc_handle = NULL;
+ }
+}
+#endif
diff --git a/sysdeps/powerpc/powerpc32/backtrace.c b/sysdeps/powerpc/powerpc32/backtrace.c
index b4b11dd..ce70d8d 100644
--- a/sysdeps/powerpc/powerpc32/backtrace.c
+++ b/sysdeps/powerpc/powerpc32/backtrace.c
@@ -19,6 +19,8 @@
#include <execinfo.h>
#include <stddef.h>
+#ifdef SHARED
+
/* This is the stack layout we see with every stack frame.
Note that every routine is required by the ABI to lay out the stack
like this.
@@ -35,8 +37,8 @@ struct layout
void *return_address;
};
-int
-__backtrace (void **array, int size)
+static int
+__backchain_backtrace (void **array, int size)
{
struct layout *current;
int count;
@@ -60,5 +62,6 @@ __backtrace (void **array, int size)
return count;
}
-weak_alias (__backtrace, backtrace)
-libc_hidden_def (__backtrace)
+#endif
+
+#include <sysdeps/powerpc/backtrace.c>
diff --git a/sysdeps/powerpc/powerpc64/backtrace.c b/sysdeps/powerpc/powerpc64/backtrace.c
index 2d3e051..4043ab4 100644
--- a/sysdeps/powerpc/powerpc64/backtrace.c
+++ b/sysdeps/powerpc/powerpc64/backtrace.c
@@ -19,6 +19,8 @@
#include <execinfo.h>
#include <stddef.h>
+#ifdef SHARED
+
/* This is the stack layout we see with every stack frame.
Note that every routine is required by the ABI to lay out the stack
like this.
@@ -31,6 +33,7 @@
| (unused) | | return address |
+----------------+ +-----------------+
*/
+
struct layout
{
struct layout *next;
@@ -38,8 +41,8 @@ struct layout
void *return_address;
};
-int
-__backtrace (void **array, int size)
+static int
+__backchain_backtrace (void **array, int size)
{
struct layout *current;
int count;
@@ -63,5 +66,6 @@ __backtrace (void **array, int size)
return count;
}
-weak_alias (__backtrace, backtrace)
-libc_hidden_def (__backtrace)
+#endif
+
+#include <sysdeps/powerpc/backtrace.c>
More information about the Libc-alpha
mailing list