struct stat is not posix conform
[glibc.git] / sysdeps / m68k / backtrace.c
blob311425192550c0f86fea6e73639d201a7a12bc54
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 };
114 #ifdef SHARED
115 __libc_once_define (static, once);
117 __libc_once (once, init);
118 if (unwind_backtrace == NULL)
119 return 0;
120 #endif
122 if (size >= 1)
123 unwind_backtrace (backtrace_helper, &arg);
125 if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
126 --arg.cnt;
127 else if (arg.cnt < size)
129 struct layout *fp = (struct layout *) arg.lastfp;
131 while (arg.cnt < size)
133 /* Check for out of range. */
134 if ((void *) fp < arg.lastsp || (void *) fp > __libc_stack_end
135 || ((long) fp & 1))
136 break;
138 array[arg.cnt++] = fp->ret;
139 fp = fp->fp;
142 return arg.cnt != -1 ? arg.cnt : 0;
144 weak_alias (__backtrace, backtrace)
145 libc_hidden_def (__backtrace)
148 #ifdef SHARED
149 /* Free all resources if necessary. */
150 libc_freeres_fn (free_mem)
152 unwind_backtrace = NULL;
153 if (libgcc_handle != NULL)
155 __libc_dlclose (libgcc_handle);
156 libgcc_handle = NULL;
159 #endif