]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/tracebak.c
tracebak.c (PPC AIX/Darwin): Define FORCE_CALL to 1.
[gcc.git] / gcc / ada / tracebak.c
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * T R A C E B A C K *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2000-2006, AdaCore *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 2, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, USA. *
21 * *
22 * As a special exception, if you link this file with other files to *
23 * produce an executable, this file does not by itself cause the resulting *
24 * executable to be covered by the GNU General Public License. This except- *
25 * ion does not however invalidate any other reasons why the executable *
26 * file might be covered by the GNU Public License. *
27 * *
28 * GNAT was originally developed by the GNAT team at New York University. *
29 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 * *
31 ****************************************************************************/
32
33 /* This file contains low level support for stack unwinding using GCC intrinsic
34 functions.
35 It has been tested on the following configurations:
36 PowerPC/AiX
37 PowerPC/VxWorks
38 Sparc/Solaris
39 i386/GNU/Linux
40 i386/Solaris
41 i386/NT
42 i386/OS2
43 i386/LynxOS
44 Alpha/VxWorks
45 Alpha/VMS
46 */
47
48 #ifdef __alpha_vxworks
49 #include "vxWorks.h"
50 #endif
51
52 #ifdef IN_RTS
53 #define POSIX
54 #include "tconfig.h"
55 #include "tsystem.h"
56 #else
57 #include "config.h"
58 #include "system.h"
59 #endif
60
61 extern int __gnat_backtrace (void **, int, void *, void *, int);
62
63 /* The point is to provide an implementation of the __gnat_backtrace function
64 above, called by the default implementation of the System.Traceback package.
65
66 We first have a series of target specific implementations, each included
67 from a separate C file for readability purposes.
68
69 Then come two flavors of a generic implementation: one relying on static
70 assumptions about the frame layout, and the other one using the GCC EH
71 infrastructure. The former uses a whole set of macros and structures which
72 may be tailored on a per target basis, and is activated as soon as
73 USE_GENERIC_UNWINDER is defined. The latter uses a small subset of the
74 macro definitions and is activated when USE_GCC_UNWINDER is defined. It is
75 only available post GCC 3.3.
76
77 Finally, there is a default dummy implementation, necessary to make the
78 linker happy on platforms where the feature is not supported, but where the
79 function is still referenced by the default System.Traceback. */
80
81 #define Lock_Task system__soft_links__lock_task
82 extern void (*Lock_Task) (void);
83
84 #define Unlock_Task system__soft_links__unlock_task
85 extern void (*Unlock_Task) (void);
86
87 /*-------------------------------------*
88 *-- Target specific implementations --*
89 *-------------------------------------*/
90
91 #if defined (__alpha_vxworks)
92
93 #include "tb-alvxw.c"
94
95 #elif defined (__ALPHA) && defined (__VMS__)
96
97 #include "tb-alvms.c"
98
99 #else
100 /* No target specific implementation. */
101
102 /*----------------------------------------------------------------*
103 *-- Target specific definitions for the generic implementation --*
104 *----------------------------------------------------------------*/
105
106 /* The stack layout is specified by the target ABI. The "generic" scheme is
107 based on the following assumption:
108
109 The stack layout from some frame pointer is such that the information
110 required to compute the backtrace is available at static offsets.
111
112 For a given frame, the information we are interested in is the saved return
113 address (somewhere after the call instruction in the caller) and a pointer
114 to the caller's frame. The former is the base of the call chain information
115 we store in the tracebacks array. The latter allows us to loop over the
116 successive frames in the chain.
117
118 To initiate the process, we retrieve an initial frame pointer using the
119 appropriate GCC builtin (__builtin_frame_address).
120
121 This scheme is unfortunately not applicable on every target because the
122 stack layout is not necessarily regular (static) enough. On targets where
123 this scheme applies, the implementation relies on the following items:
124
125 o struct layout, describing the expected stack data layout relevant to the
126 information we are interested in,
127
128 o FRAME_OFFSET, the offset, from a given frame pointer, at which this
129 layout will be found,
130
131 o FRAME_LEVEL, controls how many frames up we get at to start with,
132 from the initial frame pointer we compute by way of the GCC builtin,
133
134 0 is most often the appropriate value. 1 may be necessary on targets
135 where return addresses are saved by a function in it's caller's frame
136 (e.g. PPC).
137
138 o PC_ADJUST, to account for the difference between a call point (address
139 of a call instruction), which is what we want in the output array, and
140 the associated return address, which is what we retrieve from the stack.
141
142 o STOP_FRAME, to decide wether we reached the top of the call chain, and
143 thus if the process shall stop.
144
145 :
146 : stack
147 | +----------------+
148 | +-------->| : |
149 | | | (FRAME_OFFSET) |
150 | | | : | (PC_ADJUST)
151 | | layout:| return_address ----------------+
152 | | | .... | |
153 +--------------- next_frame | |
154 | | .... | |
155 | | | |
156 | +----------------+ | +-----+
157 | | : |<- Base fp | | : |
158 | | (FRAME_OFFSET) | (FRAME_LEVEL) | | : |
159 | | : | +---> | [1]
160 | layout:| return_address --------------------> | [0]
161 | | ... | (PC_ADJUST) +-----+
162 +---------- next_frame | traceback[]
163 | ... |
164 | |
165 +----------------+
166
167 o BASE_SKIP,
168
169 Since we inherently deal with return addresses, there is an implicit shift
170 by at least one for the initial point we are able to observe in the chain.
171
172 On some targets (e.g. sparc-solaris), the first return address we can
173 easily get without special code is even our caller's return address, so
174 there is a initial shift of two.
175
176 BASE_SKIP represents this initial shift, which is the minimal "skip_frames"
177 value we support. We could add special code for the skip_frames < BASE_SKIP
178 cases. This is not done currently because there is virtually no situation
179 in which this would be useful.
180
181 Finally, to account for some ABI specificities, a target may (but does
182 not have to) define:
183
184 o FORCE_CALL, to force a call to a dummy function at the very beginning
185 of the computation. See the PPC AIX target for an example where this
186 is useful.
187
188 o FETCH_UP_FRAME, to force an invocation of __builtin_frame_address with a
189 positive argument right after a possibly forced call even if FRAME_LEVEL
190 is 0. See the Sparc Solaris case for an example where this is useful.
191
192 */
193
194 /*--------------------------- PPC AIX/Darwin ----------------------------*/
195
196 #if ((defined (_POWER) && defined (_AIX)) || \
197 (defined (__ppc__) && defined (__APPLE__)))
198
199 #define USE_GENERIC_UNWINDER
200
201 struct layout
202 {
203 struct layout *next;
204 void *pad;
205 void *return_address;
206 };
207
208 #define FRAME_OFFSET 0
209 #define PC_ADJUST -4
210 #define STOP_FRAME(CURRENT, TOP_STACK) ((void *) (CURRENT) < (TOP_STACK))
211
212 /* The PPC ABI has an interesting specificity: the return address saved by a
213 function is located in it's caller's frame, and the save operation only
214 takes place if the function performs a call.
215
216 To have __gnat_backtrace retrieve its own return address, we then
217 define ... */
218
219 #define FORCE_CALL 1
220 #define FRAME_LEVEL 1
221
222 #define BASE_SKIP 1
223
224 /*---------------------------- PPC VxWorks------------------------------*/
225
226 #elif defined (_ARCH_PPC) && defined (__vxworks)
227
228 #define USE_GENERIC_UNWINDER
229
230 struct layout
231 {
232 struct layout *next;
233 void *return_address;
234 };
235
236 #define FORCE_CALL 1
237 #define FRAME_LEVEL 1
238 /* See the PPC AIX case for an explanation of these values. */
239
240 #define FRAME_OFFSET 0
241 #define PC_ADJUST -4
242 #define STOP_FRAME(CURRENT, TOP_STACK) ((CURRENT)->next == 0)
243
244 #define BASE_SKIP 1
245
246 /*-------------------------- SPARC Solaris -----------------------------*/
247
248 #elif defined (sun) && defined (sparc)
249
250 #define USE_GENERIC_UNWINDER
251
252 /* These definitions are inspired from the Appendix D (Software
253 Considerations) of the SPARC V8 architecture manual. */
254
255 struct layout
256 {
257 struct layout *next;
258 void *return_address;
259 };
260
261 #ifdef __arch64__
262 #define STACK_BIAS 2047 /* V9 ABI */
263 #else
264 #define STACK_BIAS 0 /* V8 ABI */
265 #endif
266
267 #define FRAME_LEVEL 0
268 #define FRAME_OFFSET (14 * sizeof (void*) + STACK_BIAS)
269 #define PC_ADJUST 0
270 #define STOP_FRAME(CURRENT, TOP_STACK) \
271 ((CURRENT)->return_address == 0|| (CURRENT)->next == 0 \
272 || (void *) (CURRENT) < (TOP_STACK))
273
274 /* The sparc register windows need to be flushed before we may access them
275 from the stack. This is achieved by way of builtin_frame_address only
276 when the "count" argument is positive, so force at least one such call. */
277 #define FETCH_UP_FRAME_ADDRESS
278
279 #define BASE_SKIP 2
280 /* From the frame pointer of frame N, we are accessing the flushed register
281 window of frame N-1 (positive offset from fp), in which we retrieve the
282 saved return address. We then end up with our caller's return address. */
283
284 /*------------------------------- x86 ----------------------------------*/
285
286 #elif defined (i386)
287
288 #ifdef __WIN32
289 #include <windows.h>
290 #define IS_BAD_PTR(ptr) (IsBadCodePtr((void *)ptr))
291 #else
292 #define IS_BAD_PTR(ptr) 0
293 #endif
294
295 #define USE_GENERIC_UNWINDER
296
297 struct layout
298 {
299 struct layout *next;
300 void *return_address;
301 };
302
303 #define LOWEST_ADDR 0
304 #define FRAME_LEVEL 1
305 /* builtin_frame_address (1) is expected to work on this target, and (0) might
306 return the soft stack pointer, which does not designate a location where a
307 backchain and a return address might be found. */
308
309 #define FRAME_OFFSET 0
310 #define PC_ADJUST -2
311 #define STOP_FRAME(CURRENT, TOP_STACK) \
312 (IS_BAD_PTR((long)(CURRENT)->return_address) \
313 || (unsigned int)(CURRENT)->return_address < LOWEST_ADDR \
314 || (CURRENT)->return_address == 0|| (CURRENT)->next == 0 \
315 || (void *) (CURRENT) < (TOP_STACK))
316
317 #define BASE_SKIP (1+FRAME_LEVEL)
318
319 /* On i386 architecture we check that at the call point we really have a call
320 insn. Possible call instructions are:
321
322 call addr16 E8 xx xx xx xx
323 call reg FF Dx
324 call off(reg) FF xx xx
325 lcall addr seg 9A xx xx xx xx xx xx
326
327 This check will not catch all cases but it will increase the backtrace
328 reliability on this architecture.
329 */
330
331 #define VALID_STACK_FRAME(ptr) \
332 (!IS_BAD_PTR(ptr) \
333 && (((*((ptr) - 3) & 0xff) == 0xe8) \
334 || ((*((ptr) - 5) & 0xff) == 0x9a) \
335 || ((*((ptr) - 1) & 0xff) == 0xff) \
336 || (((*(ptr) & 0xd0ff) == 0xd0ff))))
337
338 /*----------------------------- x86_64 ---------------------------------*/
339
340 #elif defined (__x86_64__)
341
342 #define USE_GCC_UNWINDER
343 /* The generic unwinder is not used for this target because it is based
344 on frame layout assumptions that are not reliable on this target (the
345 rbp register is very likely used for something else than storing the
346 frame pointer in optimized code). Hence, we use the GCC unwinder
347 based on DWARF 2 call frame information, although it has the drawback
348 of not being able to unwind through frames compiled without DWARF 2
349 information.
350 */
351
352 #define PC_ADJUST -2
353 /* The minimum size of call instructions on this architecture is 2 bytes */
354
355 /*----------------------------- ia64 ---------------------------------*/
356
357 #elif defined (__ia64__) && (defined (linux) || defined (__hpux__))
358
359 #define USE_GCC_UNWINDER
360 /* Use _Unwind_Backtrace driven exceptions on ia64 HP-UX and ia64
361 GNU/Linux, where _Unwind_Backtrace is provided by the system unwind
362 library. On HP-UX 11.23 this requires patch PHSS_33352, which adds
363 _Unwind_Backtrace to the system unwind library. */
364
365 #define PC_ADJUST -16
366 /* Every call on ia64 is part of a 128 bit bundle, so an adjustment of
367 minus 16 bytes from the point of return finds the address of the
368 previous bundle. */
369
370 #endif
371
372 /*---------------------------------------------------------------------*
373 *-- The post GCC 3.3 infrastructure based implementation --*
374 *---------------------------------------------------------------------*/
375
376 #if defined (USE_GCC_UNWINDER) && (__GNUC__ * 10 + __GNUC_MINOR__ > 33)
377
378 /* Conditioning the inclusion on the GCC version is useful to avoid bootstrap
379 path problems, since the included file refers to post 3.3 functions in
380 libgcc, and the stage1 compiler is unlikely to be linked against a post 3.3
381 library. It actually disables the support for backtraces in this compiler
382 for targets defining USE_GCC_UNWINDER, which is OK since we don't use the
383 traceback capability in the compiler anyway.
384
385 The condition is expressed the way above because we cannot reliably rely on
386 any other macro from the base compiler when compiling stage1. */
387
388 #include "tb-gcc.c"
389
390 /*------------------------------------------------------------------*
391 *-- The generic implementation based on frame layout assumptions --*
392 *------------------------------------------------------------------*/
393
394 #elif defined (USE_GENERIC_UNWINDER)
395
396 #ifndef CURRENT_STACK_FRAME
397 # define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
398 #endif
399
400 #ifndef VALID_STACK_FRAME
401 #define VALID_STACK_FRAME(ptr) 1
402 #endif
403
404 #ifndef MAX
405 #define MAX(x,y) ((x) > (y) ? (x) : (y))
406 #endif
407
408 #ifndef FORCE_CALL
409 #define FORCE_CALL 0
410 #endif
411
412 /* Make sure the function is not inlined. */
413 static void forced_callee (void) __attribute__ ((noinline));
414
415 static void forced_callee (void)
416 {
417 /* Make sure the function is not pure. */
418 volatile int i __attribute__ ((unused)) = 0;
419 }
420
421 int
422 __gnat_backtrace (void **array,
423 int size,
424 void *exclude_min,
425 void *exclude_max,
426 int skip_frames)
427 {
428 struct layout *current;
429 void *top_frame;
430 void *top_stack;
431 int cnt = 0;
432
433 if (FORCE_CALL)
434 forced_callee ();
435
436 /* Force a call to builtin_frame_address with a positive argument
437 if required. This is necessary e.g. on sparc to have the register
438 windows flushed before we attempt to access them on the stack. */
439 #if defined (FETCH_UP_FRAME_ADDRESS) && (FRAME_LEVEL == 0)
440 __builtin_frame_address (1);
441 #endif
442
443 top_frame = __builtin_frame_address (FRAME_LEVEL);
444 top_stack = CURRENT_STACK_FRAME;
445 current = (struct layout *) ((size_t) top_frame + FRAME_OFFSET);
446
447 /* Skip the number of calls we have been requested to skip, accounting for
448 the BASE_SKIP parameter.
449
450 FRAME_LEVEL is meaningless for the count adjustment. It impacts where we
451 start retrieving data from, but how many frames "up" we start at is in
452 BASE_SKIP by definition. */
453
454 skip_frames = MAX (0, skip_frames - BASE_SKIP);
455
456 while (cnt < skip_frames)
457 {
458 current = (struct layout *) ((size_t) current->next + FRAME_OFFSET);
459 cnt++;
460 }
461
462 cnt = 0;
463 while (cnt < size)
464 {
465 if (STOP_FRAME (current, top_stack) ||
466 !VALID_STACK_FRAME((char *)(current->return_address + PC_ADJUST)))
467 break;
468
469 if (current->return_address < exclude_min
470 || current->return_address > exclude_max)
471 array[cnt++] = current->return_address + PC_ADJUST;
472
473 current = (struct layout *) ((size_t) current->next + FRAME_OFFSET);
474 }
475
476 return cnt;
477 }
478
479 #else
480
481 /* No target specific implementation and neither USE_GCC_UNWINDER not
482 USE_GCC_UNWINDER defined. */
483
484 /*------------------------------*
485 *-- The dummy implementation --*
486 *------------------------------*/
487
488 int
489 __gnat_backtrace (array, size, exclude_min, exclude_max, skip_frames)
490 void **array ATTRIBUTE_UNUSED;
491 int size ATTRIBUTE_UNUSED;
492 void *exclude_min ATTRIBUTE_UNUSED;
493 void *exclude_max ATTRIBUTE_UNUSED;
494 int skip_frames ATTRIBUTE_UNUSED;
495 {
496 return 0;
497 }
498
499 #endif
500
501 #endif
This page took 0.069255 seconds and 6 git commands to generate.