diff options
author | Salvatore Cro <salvatore.cro@st.com> | 2010-09-09 15:45:44 +0200 |
---|---|---|
committer | Carmelo Amoroso <carmelo.amoroso@st.com> | 2010-09-15 12:31:22 +0200 |
commit | 37eb913ed8c4798b736e678f4dbd9f4a91a68f74 (patch) | |
tree | 63fc2c1dbb887a043a7291c9c6215d2db7b5ba5a /libubacktrace/sysdeps/sh/backtrace.c | |
parent | 7ac7be14eb4c8927fddffbe01fed74c605bf8597 (diff) |
libubacktrace: Provide uClibc with backtrace functions
A new shared object, libubacktrace.so.0 is added to uClibc
to provide backtrace functions to support application self-debugging.
This set of functions requires to dynamically load libgcc_s.so so they
need to call dlopen/dlsym that are provided by libdl. For this reason
they cannot be included into libc.so.0 but are provided by a new library.
User application that wants to use backtrace needs to be compiled with
-fexceptions option and -rdynamic to get full symbols printed and must be
linked against libubacktrace.so
Signed-off-by: Carmelo Amoroso <carmelo.amoroso@st.com>
Diffstat (limited to 'libubacktrace/sysdeps/sh/backtrace.c')
-rw-r--r-- | libubacktrace/sysdeps/sh/backtrace.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/libubacktrace/sysdeps/sh/backtrace.c b/libubacktrace/sysdeps/sh/backtrace.c new file mode 100644 index 000000000..18b91b1bb --- /dev/null +++ b/libubacktrace/sysdeps/sh/backtrace.c @@ -0,0 +1,84 @@ +/* + * Perform stack unwinding by using the _Unwind_Backtrace. + * + * User application that wants to use backtrace needs to be + * compiled with -fexceptions option and -rdynamic to get full + * symbols printed. + * + * Copyright (C) 2009, 2010 STMicroelectronics Ltd. + * + * Author(s): Giuseppe Cavallaro <peppe.cavallaro@st.com> + * - Initial implementation for glibc + * + * Author(s): Carmelo Amoroso <carmelo.amoroso@st.com> + * - Reworked for uClibc + * - use dlsym/dlopen from libdl + * - rewrite initialisation to not use libc_once + * - make it available in static link too + * + * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. + * + */ + +#include <execinfo.h> +#include <dlfcn.h> +#include <stdlib.h> +#include <unwind.h> +#include <assert.h> +#include <stdio.h> + +struct trace_arg +{ + void **array; + int cnt, size; +}; + +static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *); +static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *); + +static void backtrace_init (void) +{ + void *handle = dlopen ("libgcc_s.so.1", RTLD_LAZY); + + if (handle == NULL + || ((unwind_backtrace = dlsym (handle, "_Unwind_Backtrace")) == NULL) + || ((unwind_getip = dlsym (handle, "_Unwind_GetIP")) == NULL)) { + printf("libgcc_s.so.1 must be installed for backtrace to work\n"); + abort(); + } +} + +static _Unwind_Reason_Code +backtrace_helper (struct _Unwind_Context *ctx, void *a) +{ + struct trace_arg *arg = a; + + assert (unwind_getip != NULL); + + /* We are first called with address in the __backtrace function. Skip it. */ + if (arg->cnt != -1) + arg->array[arg->cnt] = (void *) unwind_getip (ctx); + if (++arg->cnt == arg->size) + return _URC_END_OF_STACK; + return _URC_NO_REASON; +} + +/* + * Perform stack unwinding by using the _Unwind_Backtrace. + * + * User application that wants to use backtrace needs to be + * compiled with -fexceptions option and -rdynamic to get full + * symbols printed. + */ +int backtrace (void **array, int size) +{ + struct trace_arg arg = { .array = array, .size = size, .cnt = -1 }; + + if (unwind_backtrace == NULL) + backtrace_init(); + + if (size >= 1) + unwind_backtrace (backtrace_helper, &arg); + + return arg.cnt != -1 ? arg.cnt : 0; +} |