2 * Perform stack unwinding by using the _Unwind_Backtrace.
4 * User application that wants to use backtrace needs to be
5 * compiled with -fexceptions option and -rdynamic to get full
8 * Copyright (C) 2009, 2010 STMicroelectronics Ltd.
10 * Author(s): Giuseppe Cavallaro <peppe.cavallaro@st.com>
11 * - Initial implementation for glibc
13 * Author(s): Carmelo Amoroso <carmelo.amoroso@st.com>
14 * - Reworked for uClibc
15 * - use dlsym/dlopen from libdl
16 * - rewrite initialisation to not use libc_once
17 * - make it available in static link too
19 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
36 static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
37 static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
39 static void backtrace_init (void)
41 void *handle = dlopen ("libgcc_s.so.1", RTLD_LAZY);
44 || ((unwind_backtrace = dlsym (handle, "_Unwind_Backtrace")) == NULL)
45 || ((unwind_getip = dlsym (handle, "_Unwind_GetIP")) == NULL)) {
46 printf("libgcc_s.so.1 must be installed for backtrace to work\n");
51 static _Unwind_Reason_Code
52 backtrace_helper (struct _Unwind_Context *ctx, void *a)
54 struct trace_arg *arg = a;
56 assert (unwind_getip != NULL);
58 /* We are first called with address in the __backtrace function. Skip it. */
60 arg->array[arg->cnt] = (void *) unwind_getip (ctx);
61 if (++arg->cnt == arg->size)
62 return _URC_END_OF_STACK;
63 return _URC_NO_REASON;
67 * Perform stack unwinding by using the _Unwind_Backtrace.
69 * User application that wants to use backtrace needs to be
70 * compiled with -fexceptions option and -rdynamic to get full
73 int backtrace (void **array, int size)
75 struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
77 if (unwind_backtrace == NULL)
81 unwind_backtrace (backtrace_helper, &arg);
83 return arg.cnt != -1 ? arg.cnt : 0;