2 * Perform stack unwinding by using the _Unwind_Backtrace.
4 * User application that wants to use backtrace needs to be
5 * compiled with -fasynchronous-unwid-tables option and -rdynamic i
6 * to get full symbols printed.
8 * Author(s): Khem Raj <raj.khem@gmail.com>
9 * - ARM specific implementation of backtrace
11 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
28 static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
29 static _Unwind_VRS_Result (*unwind_vrs_get) (_Unwind_Context *,
32 _Unwind_VRS_DataRepresentation,
35 static void backtrace_init (void)
37 void *handle = dlopen ("libgcc_s.so.1", RTLD_LAZY);
39 || ((unwind_backtrace = dlsym (handle, "_Unwind_Backtrace")) == NULL)
40 || ((unwind_vrs_get = dlsym (handle, "_Unwind_VRS_Get")) == NULL)) {
41 printf("libgcc_s.so.1 must be installed for backtrace to work\n");
45 /* This function is identical to "_Unwind_GetGR", except that it uses
46 "unwind_vrs_get" instead of "_Unwind_VRS_Get". */
47 static inline _Unwind_Word
48 unwind_getgr (_Unwind_Context *context, int regno)
51 unwind_vrs_get (context, _UVRSC_CORE, regno, _UVRSD_UINT32, &val);
55 /* This macro is identical to the _Unwind_GetIP macro, except that it
56 uses "unwind_getgr" instead of "_Unwind_GetGR". */
57 #define unwind_getip(context) \
58 (unwind_getgr (context, 15) & ~(_Unwind_Word)1)
60 static _Unwind_Reason_Code
61 backtrace_helper (struct _Unwind_Context *ctx, void *a)
63 struct trace_arg *arg = a;
65 assert (unwind_getip != NULL);
67 /* We are first called with address in the __backtrace function. Skip it. */
69 arg->array[arg->cnt] = (void *) unwind_getip (ctx);
70 if (++arg->cnt == arg->size)
71 return _URC_END_OF_STACK;
72 return _URC_NO_REASON;
76 * Perform stack unwinding by using the _Unwind_Backtrace.
79 int backtrace (void **array, int size)
81 struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
83 if (unwind_backtrace == NULL)
87 unwind_backtrace (backtrace_helper, &arg);
89 return arg.cnt != -1 ? arg.cnt : 0;