2 * Perform stack unwinding by using the _Unwind_Backtrace.
4 * User application that wants to use backtrace needs to be
5 * compiled with -fasynchronous-unwind-tables 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.
38 static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
39 static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
41 static void backtrace_init (void)
43 void *handle = dlopen (LIBGCC_S_SO, RTLD_LAZY);
46 || ((unwind_backtrace = dlsym (handle, "_Unwind_Backtrace")) == NULL)
47 || ((unwind_getip = dlsym (handle, "_Unwind_GetIP")) == NULL)) {
48 printf(LIBGCC_S_SO " must be installed for backtrace to work\n");
53 # define unwind_backtrace _Unwind_Backtrace
54 # define unwind_getip _Unwind_GetIP
57 static _Unwind_Reason_Code
58 backtrace_helper (struct _Unwind_Context *ctx, void *a)
60 struct trace_arg *arg = a;
62 assert (unwind_getip != NULL);
64 /* We are first called with address in the __backtrace function. Skip it. */
66 arg->array[arg->cnt] = (void *) unwind_getip (ctx);
67 if (++arg->cnt == arg->size)
68 return _URC_END_OF_STACK;
69 return _URC_NO_REASON;
73 * Perform stack unwinding by using the _Unwind_Backtrace.
76 int backtrace (void **array, int size)
78 struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
81 if (unwind_backtrace == NULL)
86 unwind_backtrace (backtrace_helper, &arg);
88 return arg.cnt != -1 ? arg.cnt : 0;