]> git.ipfire.org Git - thirdparty/glibc.git/blame - ports/sysdeps/arm/backtrace.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / ports / sysdeps / arm / backtrace.c
CommitLineData
b6dec188 1/* Return backtrace of current program state.
d4697bc9 2 Copyright (C) 2008-2014 Free Software Foundation, Inc.
b6dec188
MK
3 This file is part of the GNU C Library.
4 Contributed by Kazu Hirata <kazu@codesourcery.com>, 2008.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
ab84e3ff
PE
17 License along with the GNU C Library. If not, see
18 <http://www.gnu.org/licenses/>. */
b6dec188
MK
19
20#include <bits/libc-lock.h>
21#include <dlfcn.h>
22#include <execinfo.h>
23#include <stdlib.h>
24#include <unwind.h>
25
26struct trace_arg
27{
28 void **array;
29 int cnt, size;
30};
31
32#ifdef SHARED
33static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
34static _Unwind_VRS_Result (*unwind_vrs_get) (_Unwind_Context *,
35 _Unwind_VRS_RegClass,
36 _uw,
37 _Unwind_VRS_DataRepresentation,
38 void *);
39
40static void *libgcc_handle;
41
42static void
43init (void)
44{
45 libgcc_handle = __libc_dlopen ("libgcc_s.so.1");
46
47 if (libgcc_handle == NULL)
48 return;
49
50 unwind_backtrace = __libc_dlsym (libgcc_handle, "_Unwind_Backtrace");
51 unwind_vrs_get = __libc_dlsym (libgcc_handle, "_Unwind_VRS_Get");
52 if (unwind_vrs_get == NULL)
53 unwind_backtrace = NULL;
54}
55
56/* This function is identical to "_Unwind_GetGR", except that it uses
57 "unwind_vrs_get" instead of "_Unwind_VRS_Get". */
58static inline _Unwind_Word
59unwind_getgr (_Unwind_Context *context, int regno)
60{
61 _uw val;
62 unwind_vrs_get (context, _UVRSC_CORE, regno, _UVRSD_UINT32, &val);
63 return val;
64}
65
66/* This macro is identical to the _Unwind_GetIP macro, except that it
67 uses "unwind_getgr" instead of "_Unwind_GetGR". */
68# define unwind_getip(context) \
69 (unwind_getgr (context, 15) & ~(_Unwind_Word)1)
70#else
71# define unwind_backtrace _Unwind_Backtrace
72# define unwind_getip _Unwind_GetIP
73#endif
74
75static _Unwind_Reason_Code
76backtrace_helper (struct _Unwind_Context *ctx, void *a)
77{
78 struct trace_arg *arg = a;
79
80 /* We are first called with address in the __backtrace function.
81 Skip it. */
82 if (arg->cnt != -1)
83 arg->array[arg->cnt] = (void *) unwind_getip (ctx);
84 if (++arg->cnt == arg->size)
85 return _URC_END_OF_STACK;
86 return _URC_NO_REASON;
87}
88
89int
90__backtrace (array, size)
91 void **array;
92 int size;
93{
94 struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
95#ifdef SHARED
96 __libc_once_define (static, once);
97
98 __libc_once (once, init);
99 if (unwind_backtrace == NULL)
100 return 0;
101#endif
102
103 if (size >= 1)
104 unwind_backtrace (backtrace_helper, &arg);
105
106 if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
107 --arg.cnt;
108 return arg.cnt != -1 ? arg.cnt : 0;
109}
110weak_alias (__backtrace, backtrace)
111libc_hidden_def (__backtrace)
112
113
114#ifdef SHARED
115/* Free all resources if necessary. */
116libc_freeres_fn (free_mem)
117{
118 unwind_backtrace = NULL;
119 if (libgcc_handle != NULL)
120 {
121 __libc_dlclose (libgcc_handle);
122 libgcc_handle = NULL;
123 }
124}
125#endif