]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/i386/backtrace.c
(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[thirdparty/glibc.git] / sysdeps / i386 / backtrace.c
1 /* Return backtrace of current program state.
2 Copyright (C) 1998, 2000, 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21 #include <bits/libc-lock.h>
22 #include <dlfcn.h>
23 #include <execinfo.h>
24 #include <stdlib.h>
25 #include <unwind.h>
26
27 struct trace_arg
28 {
29 void **array;
30 int cnt, size;
31 void *lastebp, *lastesp;
32 };
33
34 #ifdef SHARED
35 static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
36 static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
37 static _Unwind_Ptr (*unwind_getcfa) (struct _Unwind_Context *);
38 static _Unwind_Ptr (*unwind_getgr) (struct _Unwind_Context *, int);
39
40 static void
41 init (void)
42 {
43 void *handle = __libc_dlopen ("libgcc_s.so.1");
44
45 if (handle == NULL)
46 return;
47
48 unwind_backtrace = __libc_dlsym (handle, "_Unwind_Backtrace");
49 unwind_getip = __libc_dlsym (handle, "_Unwind_GetIP");
50 unwind_getcfa = __libc_dlsym (handle, "_Unwind_GetCFA");
51 unwind_getgr = __libc_dlsym (handle, "_Unwind_GetGR");
52 if (unwind_getip == NULL || unwind_getgr == NULL || unwind_getcfa == NULL)
53 unwind_backtrace = NULL;
54 }
55 #else
56 # define unwind_backtrace _Unwind_Backtrace
57 # define unwind_getip _Unwind_GetIP
58 # define unwind_getcfa _Unwind_GetCFA
59 # define unwind_getgr _Unwind_GetGR
60 #endif
61
62 static _Unwind_Reason_Code
63 backtrace_helper (struct _Unwind_Context *ctx, void *a)
64 {
65 struct trace_arg *arg = a;
66
67 /* We are first called with address in the __backtrace function.
68 Skip it. */
69 if (arg->cnt != -1)
70 arg->array[arg->cnt] = (void *) unwind_getip (ctx);
71 if (++arg->cnt == arg->size)
72 return _URC_END_OF_STACK;
73
74 /* %ebp is DWARF2 register 5 on IA-32. */
75 arg->lastebp = (void *) unwind_getgr (ctx, 5);
76 arg->lastesp = (void *) unwind_getcfa (ctx);
77 return _URC_NO_REASON;
78 }
79
80
81 /* This is a global variable set at program start time. It marks the
82 highest used stack address. */
83 extern void *__libc_stack_end;
84
85
86 /* This is the stack layout we see with every stack frame
87 if not compiled without frame pointer.
88
89 +-----------------+ +-----------------+
90 %ebp -> | %ebp last frame--------> | %ebp last frame--->...
91 | | | |
92 | return address | | return address |
93 +-----------------+ +-----------------+
94
95 First try as far to get as far as possible using
96 _Unwind_Backtrace which handles -fomit-frame-pointer
97 as well, but requires .eh_frame info. Then fall back to
98 walking the stack manually. */
99
100 struct layout
101 {
102 struct layout *ebp;
103 void *ret;
104 };
105
106
107 int
108 __backtrace (array, size)
109 void **array;
110 int size;
111 {
112 struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
113 #ifdef SHARED
114 __libc_once_define (static, once);
115
116 __libc_once (once, init);
117 if (unwind_backtrace == NULL)
118 return 0;
119 #endif
120
121 if (size >= 1)
122 unwind_backtrace (backtrace_helper, &arg);
123
124 if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
125 --arg.cnt;
126 else if (arg.cnt < size)
127 {
128 struct layout *ebp = (struct layout *) arg.lastebp;
129
130 while (arg.cnt < size)
131 {
132 /* Check for out of range. */
133 if ((void *) ebp < arg.lastesp || (void *) ebp > __libc_stack_end
134 || ((long) ebp & 3))
135 break;
136
137 array[arg.cnt++] = ebp->ret;
138 ebp = ebp->ebp;
139 }
140 }
141 return arg.cnt != -1 ? arg.cnt : 0;
142 }
143 weak_alias (__backtrace, backtrace)