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.
30 static _Unwind_Reason_Code (*unwind_backtrace
) (_Unwind_Trace_Fn
, void *);
31 static _Unwind_VRS_Result (*unwind_vrs_get
) (_Unwind_Context
*,
34 _Unwind_VRS_DataRepresentation
,
37 static void backtrace_init (void)
39 void *handle
= dlopen (LIBGCC_S_SO
, RTLD_LAZY
);
41 || ((unwind_backtrace
= dlsym (handle
, "_Unwind_Backtrace")) == NULL
)
42 || ((unwind_vrs_get
= dlsym (handle
, "_Unwind_VRS_Get")) == NULL
)) {
43 printf(LIBGCC_S_SO
" must be installed for backtrace to work\n");
48 # define unwind_backtrace _Unwind_Backtrace
49 # define unwind_vrs_get _Unwind_VRS_Get
51 /* This function is identical to "_Unwind_GetGR", except that it uses
52 "unwind_vrs_get" instead of "_Unwind_VRS_Get". */
53 static inline _Unwind_Word
54 unwind_getgr (_Unwind_Context
*context
, int regno
)
57 unwind_vrs_get (context
, _UVRSC_CORE
, regno
, _UVRSD_UINT32
, &val
);
61 /* This macro is identical to the _Unwind_GetIP macro, except that it
62 uses "unwind_getgr" instead of "_Unwind_GetGR". */
63 #define unwind_getip(context) \
64 (unwind_getgr (context, 15) & ~(_Unwind_Word)1)
66 static _Unwind_Reason_Code
67 backtrace_helper (struct _Unwind_Context
*ctx
, void *a
)
69 struct trace_arg
*arg
= a
;
71 assert (unwind_getip(ctx
) != NULL
);
73 /* We are first called with address in the __backtrace function. Skip it. */
75 arg
->array
[arg
->cnt
] = (void *) unwind_getip (ctx
);
76 if (++arg
->cnt
== arg
->size
)
77 return _URC_END_OF_STACK
;
78 return _URC_NO_REASON
;
82 * Perform stack unwinding by using the _Unwind_Backtrace.
85 int backtrace (void **array
, int size
)
87 struct trace_arg arg
= { .array
= array
, .size
= size
, .cnt
= -1 };
90 if (unwind_backtrace
== NULL
)
95 unwind_backtrace (backtrace_helper
, &arg
);
97 return arg
.cnt
!= -1 ? arg
.cnt
: 0;