Move i486/strlen.S to strlen.S
[glibc.git] / sysdeps / m68k / backtrace.c
blobca7d25929efee98e507ca2d6b54052d2a361e8d2
1 /* Return backtrace of current program state.
2 Copyright (C) 2013-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <bits/libc-lock.h>
20 #include <dlfcn.h>
21 #include <execinfo.h>
22 #include <stdlib.h>
23 #include <unwind.h>
25 struct trace_arg
27 void **array;
28 int cnt, size;
29 void *lastfp, *lastsp;
32 #ifdef SHARED
33 static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
34 static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
35 static _Unwind_Ptr (*unwind_getcfa) (struct _Unwind_Context *);
36 static _Unwind_Ptr (*unwind_getgr) (struct _Unwind_Context *, int);
37 static void *libgcc_handle;
39 static void
40 init (void)
42 libgcc_handle = __libc_dlopen ("libgcc_s.so.2");
44 if (libgcc_handle == NULL)
45 return;
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)
53 unwind_backtrace = NULL;
54 __libc_dlclose (libgcc_handle);
55 libgcc_handle = NULL;
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
65 static _Unwind_Reason_Code
66 backtrace_helper (struct _Unwind_Context *ctx, void *a)
68 struct trace_arg *arg = a;
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;
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;
84 /* This is a global variable set at program start time. It marks the
85 highest used stack address. */
86 extern void *__libc_stack_end;
89 /* This is the stack layout we see with every stack frame
90 if not compiled without frame pointer.
92 +-----------------+ +-----------------+
93 %fp -> | %fp last frame--------> | %fp last frame--->...
94 | | | |
95 | return address | | return address |
96 +-----------------+ +-----------------+
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. */
103 struct layout
105 struct layout *fp;
106 void *ret;
111 __backtrace (void **array, int size)
113 struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
115 if (size <= 0)
116 return 0;
118 #ifdef SHARED
119 __libc_once_define (static, once);
121 __libc_once (once, init);
122 if (unwind_backtrace == NULL)
123 return 0;
124 #endif
126 unwind_backtrace (backtrace_helper, &arg);
128 if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
129 --arg.cnt;
130 else if (arg.cnt < size)
132 struct layout *fp = (struct layout *) arg.lastfp;
134 while (arg.cnt < size)
136 /* Check for out of range. */
137 if ((void *) fp < arg.lastsp || (void *) fp > __libc_stack_end
138 || ((long) fp & 1))
139 break;
141 array[arg.cnt++] = fp->ret;
142 fp = fp->fp;
145 return arg.cnt != -1 ? arg.cnt : 0;
147 weak_alias (__backtrace, backtrace)
148 libc_hidden_def (__backtrace)
151 #ifdef SHARED
152 /* Free all resources if necessary. */
153 libc_freeres_fn (free_mem)
155 unwind_backtrace = NULL;
156 if (libgcc_handle != NULL)
158 __libc_dlclose (libgcc_handle);
159 libgcc_handle = NULL;
162 #endif