]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/m68k/backtrace.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / m68k / backtrace.c
CommitLineData
32ebef01 1/* Return backtrace of current program state.
2b778ceb 2 Copyright (C) 2013-2021 Free Software Foundation, Inc.
32ebef01
AS
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
32ebef01 18
ec999b8e 19#include <libc-lock.h>
32ebef01
AS
20#include <dlfcn.h>
21#include <execinfo.h>
22#include <stdlib.h>
23#include <unwind.h>
24
25struct trace_arg
26{
27 void **array;
28 int cnt, size;
29 void *lastfp, *lastsp;
30};
31
32#ifdef SHARED
33static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
34static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
35static _Unwind_Ptr (*unwind_getcfa) (struct _Unwind_Context *);
36static _Unwind_Ptr (*unwind_getgr) (struct _Unwind_Context *, int);
37static void *libgcc_handle;
38
39static void
40init (void)
41{
42 libgcc_handle = __libc_dlopen ("libgcc_s.so.2");
43
44 if (libgcc_handle == NULL)
45 return;
46
47 unwind_backtrace = __libc_dlsym (libgcc_handle, "_Unwind_Backtrace");
48 unwind_getip = __libc_dlsym (libgcc_handle, "_Unwind_GetIP");
49 unwind_getcfa = __libc_dlsym (libgcc_handle, "_Unwind_GetCFA");
50 unwind_getgr = __libc_dlsym (libgcc_handle, "_Unwind_GetGR");
51 if (unwind_getip == NULL || unwind_getgr == NULL || unwind_getcfa == NULL)
52 {
53 unwind_backtrace = NULL;
54 __libc_dlclose (libgcc_handle);
55 libgcc_handle = NULL;
56 }
57}
58#else
59# define unwind_backtrace _Unwind_Backtrace
60# define unwind_getip _Unwind_GetIP
61# define unwind_getcfa _Unwind_GetCFA
62# define unwind_getgr _Unwind_GetGR
63#endif
64
65static _Unwind_Reason_Code
66backtrace_helper (struct _Unwind_Context *ctx, void *a)
67{
68 struct trace_arg *arg = a;
69
70 /* We are first called with address in the __backtrace function.
71 Skip it. */
72 if (arg->cnt != -1)
73 arg->array[arg->cnt] = (void *) unwind_getip (ctx);
74 if (++arg->cnt == arg->size)
75 return _URC_END_OF_STACK;
76
77 /* %fp is DWARF2 register 14 on M68K. */
78 arg->lastfp = (void *) unwind_getgr (ctx, 14);
79 arg->lastsp = (void *) unwind_getcfa (ctx);
80 return _URC_NO_REASON;
81}
82
83
84/* This is a global variable set at program start time. It marks the
85 highest used stack address. */
86extern void *__libc_stack_end;
87
88
89/* This is the stack layout we see with every stack frame
90 if not compiled without frame pointer.
91
92 +-----------------+ +-----------------+
93 %fp -> | %fp last frame--------> | %fp last frame--->...
94 | | | |
95 | return address | | return address |
96 +-----------------+ +-----------------+
97
98 First try as far to get as far as possible using
99 _Unwind_Backtrace which handles -fomit-frame-pointer
100 as well, but requires .eh_frame info. Then fall back to
101 walking the stack manually. */
102
103struct layout
104{
105 struct layout *fp;
106 void *ret;
107};
108
109
110int
111__backtrace (void **array, int size)
112{
113 struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
d5dff793
PP
114
115 if (size <= 0)
116 return 0;
117
32ebef01
AS
118#ifdef SHARED
119 __libc_once_define (static, once);
120
121 __libc_once (once, init);
122 if (unwind_backtrace == NULL)
123 return 0;
124#endif
125
d5dff793 126 unwind_backtrace (backtrace_helper, &arg);
32ebef01
AS
127
128 if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
129 --arg.cnt;
130 else if (arg.cnt < size)
131 {
132 struct layout *fp = (struct layout *) arg.lastfp;
133
134 while (arg.cnt < size)
135 {
136 /* Check for out of range. */
137 if ((void *) fp < arg.lastsp || (void *) fp > __libc_stack_end
138 || ((long) fp & 1))
139 break;
140
141 array[arg.cnt++] = fp->ret;
142 fp = fp->fp;
143 }
144 }
145 return arg.cnt != -1 ? arg.cnt : 0;
146}
147weak_alias (__backtrace, backtrace)
148libc_hidden_def (__backtrace)
149
150
151#ifdef SHARED
152/* Free all resources if necessary. */
153libc_freeres_fn (free_mem)
154{
155 unwind_backtrace = NULL;
156 if (libgcc_handle != NULL)
157 {
158 __libc_dlclose (libgcc_handle);
159 libgcc_handle = NULL;
160 }
161}
162#endif