]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/tracebak.c
[Ada] Bump copyright year
[thirdparty/gcc.git] / gcc / ada / tracebak.c
1 /****************************************************************************
2 * *
3 * GNAT RUN-TIME COMPONENTS *
4 * *
5 * T R A C E B A C K *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2000-2020, Free Software Foundation, Inc. *
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 3, 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. *
17 * *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
21 * *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
26 * *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
29 * *
30 ****************************************************************************/
31
32 /* This file contains low level support for stack unwinding using GCC intrinsic
33 functions.
34 It has been tested on the following configurations:
35 PowerPC/AiX
36 PowerPC/Darwin
37 PowerPC/VxWorks
38 PowerPC/LynxOS-178
39 SPARC/Solaris
40 i386/GNU/Linux
41 i386/Solaris
42 i386/NT
43 i386/OS2
44 i386/LynxOS
45 Alpha/VxWorks
46 Alpha/VMS
47 */
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 #ifdef IN_RTS
54 #define POSIX
55 #include "runtime.h"
56 #include <stddef.h>
57 #else
58 #include "config.h"
59 #include "system.h"
60 /* We don't want fancy_abort here. */
61 #undef abort
62 #endif
63
64 extern int __gnat_backtrace (void **, int, void *, void *, int);
65
66 /* The point is to provide an implementation of the __gnat_backtrace function
67 above, called by the default implementation of the System.Traceback package.
68
69 We first have a series of target specific implementations, each included
70 from a separate C file for readability purposes.
71
72 Then come two flavors of a generic implementation: one relying on static
73 assumptions about the frame layout, and the other one using the GCC EH
74 infrastructure. The former uses a whole set of macros and structures which
75 may be tailored on a per target basis, and is activated as soon as
76 USE_GENERIC_UNWINDER is defined. The latter uses a small subset of the
77 macro definitions and is activated when USE_GCC_UNWINDER is defined. It is
78 only available post GCC 3.3.
79
80 Finally, there is a default dummy implementation, necessary to make the
81 linker happy on platforms where the feature is not supported, but where the
82 function is still referenced by the default System.Traceback. */
83
84 #define Lock_Task system__soft_links__lock_task
85 extern void (*Lock_Task) (void);
86
87 #define Unlock_Task system__soft_links__unlock_task
88 extern void (*Unlock_Task) (void);
89
90 /*-------------------------------------*
91 *-- Target specific implementations --*
92 *-------------------------------------*/
93
94 #if defined (_WIN64) && defined (__SEH__)
95
96 #include <windows.h>
97
98 #define IS_BAD_PTR(ptr) (IsBadCodePtr((FARPROC)ptr))
99
100 int
101 __gnat_backtrace (void **array,
102 int size,
103 void *exclude_min,
104 void *exclude_max,
105 int skip_frames)
106 {
107 CONTEXT context;
108 UNWIND_HISTORY_TABLE history;
109 int i;
110
111 /* Get the context. */
112 RtlCaptureContext (&context);
113
114 /* Setup unwind history table (a cached to speed-up unwinding). */
115 memset (&history, 0, sizeof (history));
116
117 i = 0;
118 while (1)
119 {
120 PRUNTIME_FUNCTION RuntimeFunction;
121 KNONVOLATILE_CONTEXT_POINTERS NvContext;
122 ULONG64 ImageBase;
123 VOID *HandlerData;
124 ULONG64 EstablisherFrame;
125
126 /* Get function metadata. */
127 RuntimeFunction = RtlLookupFunctionEntry
128 (context.Rip, &ImageBase, &history);
129
130 if (!RuntimeFunction)
131 {
132 /* In case of failure, assume this is a leaf function. */
133 context.Rip = *(ULONG64 *) context.Rsp;
134 context.Rsp += 8;
135 }
136 else
137 {
138 /* If the last unwinding step failed somehow, stop here. */
139 if (IS_BAD_PTR(context.Rip))
140 break;
141
142 /* Unwind. */
143 memset (&NvContext, 0, sizeof (KNONVOLATILE_CONTEXT_POINTERS));
144 RtlVirtualUnwind (0, ImageBase, context.Rip, RuntimeFunction,
145 &context, &HandlerData, &EstablisherFrame,
146 &NvContext);
147 }
148
149 /* 0 means bottom of the stack. */
150 if (context.Rip == 0)
151 break;
152
153 /* Skip frames. */
154 if (skip_frames > 1)
155 {
156 skip_frames--;
157 continue;
158 }
159 /* Excluded frames. */
160 if ((void *)context.Rip >= exclude_min
161 && (void *)context.Rip <= exclude_max)
162 continue;
163
164 array[i++] = (void *)(context.Rip - 2);
165 if (i >= size)
166 break;
167 }
168 return i;
169 }
170 #else
171
172 /* No target specific implementation. */
173
174 /*----------------------------------------------------------------*
175 *-- Target specific definitions for the generic implementation --*
176 *----------------------------------------------------------------*/
177
178 /* The stack layout is specified by the target ABI. The "generic" scheme is
179 based on the following assumption:
180
181 The stack layout from some frame pointer is such that the information
182 required to compute the backtrace is available at static offsets.
183
184 For a given frame, the information we are interested in is the saved return
185 address (somewhere after the call instruction in the caller) and a pointer
186 to the caller's frame. The former is the base of the call chain information
187 we store in the tracebacks array. The latter allows us to loop over the
188 successive frames in the chain.
189
190 To initiate the process, we retrieve an initial frame address using the
191 appropriate GCC builtin (__builtin_frame_address).
192
193 This scheme is unfortunately not applicable on every target because the
194 stack layout is not necessarily regular (static) enough. On targets where
195 this scheme applies, the implementation relies on the following items:
196
197 o struct layout, describing the expected stack data layout relevant to the
198 information we are interested in,
199
200 o FRAME_OFFSET, the offset, from a given frame address or frame pointer
201 value, at which this layout will be found,
202
203 o FRAME_LEVEL, controls how many frames up we get at to start with,
204 from the initial frame pointer we compute by way of the GCC builtin,
205
206 0 is most often the appropriate value. 1 may be necessary on targets
207 where return addresses are saved by a function in it's caller's frame
208 (e.g. PPC).
209
210 o PC_ADJUST, to account for the difference between a call point (address
211 of a call instruction), which is what we want in the output array, and
212 the associated return address, which is what we retrieve from the stack.
213
214 o STOP_FRAME, to decide whether we reached the top of the call chain, and
215 thus if the process shall stop.
216
217 :
218 : stack
219 | +----------------+
220 | +-------->| : |
221 | | | (FRAME_OFFSET) |
222 | | | : | (PC_ADJUST)
223 | | layout:| return_address ----------------+
224 | | | .... | |
225 +--------------- next_frame | |
226 | | .... | |
227 | | | |
228 | +----------------+ | +-----+
229 | | : |<- Base fp | | : |
230 | | (FRAME_OFFSET) | (FRAME_LEVEL) | | : |
231 | | : | +---> | [1]
232 | layout:| return_address --------------------> | [0]
233 | | ... | (PC_ADJUST) +-----+
234 +---------- next_frame | traceback[]
235 | ... |
236 | |
237 +----------------+
238
239 o BASE_SKIP,
240
241 Since we inherently deal with return addresses, there is an implicit shift
242 by at least one for the initial point we are able to observe in the chain.
243
244 On some targets (e.g. sparc-solaris), the first return address we can
245 easily get without special code is even our caller's return address, so
246 there is a initial shift of two.
247
248 BASE_SKIP represents this initial shift, which is the minimal "skip_frames"
249 value we support. We could add special code for the skip_frames < BASE_SKIP
250 cases. This is not done currently because there is virtually no situation
251 in which this would be useful.
252
253 Finally, to account for some ABI specificities, a target may (but does
254 not have to) define:
255
256 o FORCE_CALL, to force a call to a dummy function at the very beginning
257 of the computation. See the PPC AIX target for an example where this
258 is useful.
259
260 o FETCH_UP_FRAME, to force an invocation of __builtin_frame_address with a
261 positive argument right after a possibly forced call even if FRAME_LEVEL
262 is 0. See the SPARC Solaris case for an example where this is useful.
263
264 */
265
266 /*------------------- Darwin 8 (OSX 10.4) or newer ----------------------*/
267 #if defined (__APPLE__) \
268 && defined (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) \
269 && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1040
270
271 #define USE_GCC_UNWINDER
272
273 #if defined (__i386__) || defined (__x86_64__)
274 #define PC_ADJUST -2
275 #elif defined (__ppc__) || defined (__ppc64__)
276 #define PC_ADJUST -4
277 #elif defined (__arm__)
278 #define PC_ADJUST -2
279 #elif defined (__arm64__)
280 #define PC_ADJUST -4
281 #else
282 #error Unhandled darwin architecture.
283 #endif
284
285 /*---------------------------- x86 *BSD --------------------------------*/
286
287 #elif defined (__i386__) && \
288 ( defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) )
289
290 #define USE_GCC_UNWINDER
291 /* The generic unwinder is not used for this target because the default
292 implementation doesn't unwind on the BSD platforms. AMD64 targets use the
293 gcc unwinder for all platforms, so let's keep i386 consistent with that.
294 */
295
296 #define PC_ADJUST -2
297 /* The minimum size of call instructions on this architecture is 2 bytes */
298
299 /*---------------------- ARM VxWorks ------------------------------------*/
300 #elif (defined (ARMEL) && defined (__vxworks))
301
302 #include "vxWorks.h"
303 #include "version.h"
304
305 #define USE_GCC_UNWINDER
306 #define PC_ADJUST -2
307
308 #if ((_WRS_VXWORKS_MAJOR >= 7) && (_VX_CPU != ARMARCH8A))
309 #define USING_ARM_UNWINDING 1
310 #endif
311
312 /*---------------------- PPC AIX/PPC Lynx 178/Older Darwin --------------*/
313 #elif ((defined (_POWER) && defined (_AIX)) || \
314 (defined (__powerpc__) && defined (__Lynx__) && !defined(__ELF__)) || \
315 (defined (__ppc__) && defined (__APPLE__)))
316
317 #define USE_GENERIC_UNWINDER
318
319 struct layout
320 {
321 struct layout *next;
322 void *pad;
323 void *return_address;
324 };
325
326 #define FRAME_OFFSET(FP) 0
327 #define PC_ADJUST -4
328
329 /* Eventhough the base PPC ABI states that a toplevel frame entry
330 should to feature a null backchain, AIX might expose a null return
331 address instead. */
332
333 /* Then LynxOS-178 features yet another variation, with return_address
334 == &<entrypoint>, with two possible entry points (one for the main
335 process and one for threads). Beware that &bla returns the address
336 of a descriptor when "bla" is a function. Getting the code address
337 requires an extra dereference. */
338
339 #if defined (__Lynx__)
340 extern void __start(); /* process entry point. */
341 extern void __runnit(); /* thread entry point. */
342 #define EXTRA_STOP_CONDITION(CURRENT) \
343 ((CURRENT)->return_address == *(void**)&__start \
344 || (CURRENT)->return_address == *(void**)&__runnit)
345 #else
346 #define EXTRA_STOP_CONDITION(CURRENT) (0)
347 #endif
348
349 #define STOP_FRAME(CURRENT, TOP_STACK) \
350 (((void *) (CURRENT) < (TOP_STACK)) \
351 || (CURRENT)->return_address == NULL \
352 || EXTRA_STOP_CONDITION(CURRENT))
353
354 /* The PPC ABI has an interesting specificity: the return address saved by a
355 function is located in it's caller's frame, and the save operation only
356 takes place if the function performs a call.
357
358 To have __gnat_backtrace retrieve its own return address, we then
359 define ... */
360
361 #define FORCE_CALL 1
362 #define FRAME_LEVEL 1
363
364 #define BASE_SKIP 1
365
366 /*----------- PPC ELF (GNU/Linux & VxWorks & Lynx178e) -------------------*/
367
368 #elif (defined (_ARCH_PPC) && defined (__vxworks)) || \
369 (defined (__powerpc__) && defined (__Lynx__) && defined(__ELF__)) || \
370 (defined (__linux__) && defined (__powerpc__))
371
372 #if defined (_ARCH_PPC64) && !defined (__USING_SJLJ_EXCEPTIONS__)
373 #define USE_GCC_UNWINDER
374 #else
375 #define USE_GENERIC_UNWINDER
376 #endif
377
378 struct layout
379 {
380 struct layout *next;
381 void *return_address;
382 };
383
384 #define FORCE_CALL 1
385 #define FRAME_LEVEL 1
386 /* See the PPC AIX case for an explanation of these values. */
387
388 #define FRAME_OFFSET(FP) 0
389 #define PC_ADJUST -4
390
391 /* According to the base PPC ABI, a toplevel frame entry should feature
392 a null backchain. What happens at signal handler frontiers isn't so
393 well specified, so we add a safety guard on top. */
394
395 #define STOP_FRAME(CURRENT, TOP_STACK) \
396 ((CURRENT)->next == 0 || ((long)(CURRENT)->next % __alignof__(void*)) != 0)
397
398 #define BASE_SKIP 1
399
400 /*-------------------------- SPARC Solaris -----------------------------*/
401
402 #elif defined (__sun__) && defined (__sparc__)
403
404 #define USE_GENERIC_UNWINDER
405
406 /* These definitions are inspired from the Appendix D (Software
407 Considerations) of the SPARC V8 architecture manual. */
408
409 struct layout
410 {
411 struct layout *next;
412 void *return_address;
413 };
414
415 #ifdef __arch64__
416 #define STACK_BIAS 2047 /* V9 ABI */
417 #else
418 #define STACK_BIAS 0 /* V8 ABI */
419 #endif
420
421 #define FRAME_LEVEL 0
422 #define FRAME_OFFSET(FP) (14 * sizeof (void*) + (FP ? STACK_BIAS : 0))
423 #define PC_ADJUST 0
424 #define STOP_FRAME(CURRENT, TOP_STACK) \
425 ((CURRENT)->return_address == 0|| (CURRENT)->next == 0 \
426 || (void *) (CURRENT) < (TOP_STACK))
427
428 /* The SPARC register windows need to be flushed before we may access them
429 from the stack. This is achieved by way of builtin_frame_address only
430 when the "count" argument is positive, so force at least one such call. */
431 #define FETCH_UP_FRAME_ADDRESS
432
433 #define BASE_SKIP 2
434 /* From the frame pointer of frame N, we are accessing the flushed register
435 window of frame N-1 (positive offset from fp), in which we retrieve the
436 saved return address. We then end up with our caller's return address. */
437
438 /*---------------------------- x86 & x86_64 ---------------------------------*/
439
440 #elif defined (__i386__) || defined (__x86_64__)
441
442 #if defined (__WIN32)
443 #include <windows.h>
444 #define IS_BAD_PTR(ptr) (IsBadCodePtr((FARPROC)ptr))
445 #elif defined (__sun__)
446 #define IS_BAD_PTR(ptr) ((unsigned long)ptr == -1UL)
447 #else
448 #define IS_BAD_PTR(ptr) 0
449 #endif
450
451 /* Use the dwarf2 unwinder when we expect to have dwarf2 tables at
452 hand. Backtraces will reliably stop on frames missing such tables,
453 but our only alternative is the generic unwinder which requires
454 compilation forcing a frame pointer to be reliable. */
455
456 #if (defined (__x86_64__) || defined (__linux__)) && !defined (__USING_SJLJ_EXCEPTIONS__)
457 #define USE_GCC_UNWINDER
458 #else
459 #define USE_GENERIC_UNWINDER
460 #endif
461
462 struct layout
463 {
464 struct layout *next;
465 void *return_address;
466 };
467
468 #define FRAME_LEVEL 1
469 /* builtin_frame_address (1) is expected to work on this family of targets,
470 and (0) might return the soft stack pointer, which does not designate a
471 location where a backchain and a return address might be found. */
472
473 #define FRAME_OFFSET(FP) 0
474 #define PC_ADJUST -2
475 #define STOP_FRAME(CURRENT, TOP_STACK) \
476 (IS_BAD_PTR((long)(CURRENT)) \
477 || (void *) (CURRENT) < (TOP_STACK) \
478 || IS_BAD_PTR((long)(CURRENT)->return_address) \
479 || (CURRENT)->return_address == 0 \
480 || (void *) ((CURRENT)->next) < (TOP_STACK) \
481 || EXTRA_STOP_CONDITION(CURRENT))
482
483 #define BASE_SKIP (1+FRAME_LEVEL)
484
485 /* On i386 architecture we check that at the call point we really have a call
486 insn. Possible call instructions are:
487
488 call addr16 E8 xx xx xx xx
489 call reg FF Dx
490 call off(reg) FF xx xx
491 lcall addr seg 9A xx xx xx xx xx xx
492
493 This check will not catch all cases but it will increase the backtrace
494 reliability on this architecture.
495 */
496
497 #define VALID_STACK_FRAME(ptr) \
498 (!IS_BAD_PTR(ptr) \
499 && (((*((ptr) - 3) & 0xff) == 0xe8) \
500 || ((*((ptr) - 5) & 0xff) == 0x9a) \
501 || ((*((ptr) - 1) & 0xff) == 0xff) \
502 || (((*(ptr) & 0xd0ff) == 0xd0ff))))
503
504 #if defined (__vxworks) && defined (__RTP__)
505
506 /* For VxWorks following backchains past the "main" frame gets us into the
507 kernel space, where it can't be dereferenced. So lets stop at the main
508 symbol. */
509 extern void main();
510
511 static int
512 is_return_from(void *symbol_addr, void *ret_addr)
513 {
514 int ret = 0;
515 char *ptr = (char *)ret_addr;
516
517 if ((*(ptr - 5) & 0xff) == 0xe8)
518 {
519 /* call addr16 E8 xx xx xx xx */
520 int32_t offset = *(int32_t *)(ptr - 4);
521 ret = (ptr + offset) == symbol_addr;
522 }
523
524 /* Others not implemented yet... But it is very likely that call addr16
525 is used here. */
526 return ret;
527 }
528
529 #define EXTRA_STOP_CONDITION(CURRENT) \
530 (is_return_from(&main, (CURRENT)->return_address))
531 #else /* not (defined (__vxworks) && defined (__RTP__)) */
532 #define EXTRA_STOP_CONDITION(CURRENT) (0)
533 #endif /* not (defined (__vxworks) && defined (__RTP__)) */
534
535 /*----------------------------- qnx ----------------------------------*/
536
537 #elif defined (__QNX__)
538
539 #define USE_GCC_UNWINDER
540
541 #if defined (__aarch64__)
542 #define PC_ADJUST -4
543 #else
544 #error Unhandled QNX architecture.
545 #endif
546
547 /*------------------- aarch64-linux ----------------------------------*/
548
549 #elif (defined (__aarch64__) && defined (__linux__))
550
551 #define USE_GCC_UNWINDER
552 #define PC_ADJUST -4
553
554 /*----------------------------- ia64 ---------------------------------*/
555
556 #elif defined (__ia64__) && (defined (__linux__) || defined (__hpux__))
557
558 #define USE_GCC_UNWINDER
559 /* Use _Unwind_Backtrace driven exceptions on ia64 HP-UX and ia64
560 GNU/Linux, where _Unwind_Backtrace is provided by the system unwind
561 library. On HP-UX 11.23 this requires patch PHSS_33352, which adds
562 _Unwind_Backtrace to the system unwind library. */
563
564 #define PC_ADJUST -4
565
566
567 #endif
568
569 /*---------------------------------------------------------------------*
570 *-- The post GCC 3.3 infrastructure based implementation --*
571 *---------------------------------------------------------------------*/
572
573 #if defined (USE_GCC_UNWINDER) && (__GNUC__ * 10 + __GNUC_MINOR__ > 33)
574
575 /* Conditioning the inclusion on the GCC version is useful to avoid bootstrap
576 path problems, since the included file refers to post 3.3 functions in
577 libgcc, and the stage1 compiler is unlikely to be linked against a post 3.3
578 library. It actually disables the support for backtraces in this compiler
579 for targets defining USE_GCC_UNWINDER, which is OK since we don't use the
580 traceback capability in the compiler anyway.
581
582 The condition is expressed the way above because we cannot reliably rely on
583 any other macro from the base compiler when compiling stage1. */
584
585 #ifdef USING_ARM_UNWINDING
586 /* This value is not part of the enumerated reason codes defined in unwind.h
587 for ARM style unwinding, but is used in the included "C" code, so we
588 define it to a reasonable value to avoid a compilation error. */
589 #define _URC_NORMAL_STOP 0
590 #endif
591
592 /* This is an implementation of the __gnat_backtrace routine using the
593 underlying GCC unwinding support associated with the exception handling
594 infrastructure. This will only work for ZCX based applications. */
595
596 #include <unwind.h>
597
598 /* The implementation boils down to a call to _Unwind_Backtrace with a
599 tailored callback and carried-on data structure to keep track of the
600 input parameters we got as well as of the basic processing state. */
601
602 /******************
603 * trace_callback *
604 ******************/
605
606 #if !defined (__USING_SJLJ_EXCEPTIONS__)
607
608 typedef struct {
609 void ** traceback;
610 int max_len;
611 void * exclude_min;
612 void * exclude_max;
613 int n_frames_to_skip;
614 int n_frames_skipped;
615 int n_entries_filled;
616 } uw_data_t;
617
618 #if defined (__ia64__) && defined (__hpux__)
619 #include <uwx.h>
620 #endif
621
622 static _Unwind_Reason_Code
623 trace_callback (struct _Unwind_Context * uw_context, uw_data_t * uw_data)
624 {
625 char * pc;
626
627 #if defined (__ia64__) && defined (__hpux__) && defined (USE_LIBUNWIND_EXCEPTIONS)
628 /* Work around problem with _Unwind_GetIP on ia64 HP-UX. */
629 uwx_get_reg ((struct uwx_env *) uw_context, UWX_REG_IP, (uint64_t *) &pc);
630 #else
631 pc = (char *) _Unwind_GetIP (uw_context);
632 #endif
633
634 if (uw_data->n_frames_skipped < uw_data->n_frames_to_skip)
635 {
636 uw_data->n_frames_skipped ++;
637 return _URC_NO_REASON;
638 }
639
640 if (uw_data->n_entries_filled >= uw_data->max_len)
641 return _URC_NORMAL_STOP;
642
643 if (pc < (char *)uw_data->exclude_min || pc > (char *)uw_data->exclude_max)
644 uw_data->traceback [uw_data->n_entries_filled ++] = pc + PC_ADJUST;
645
646 return _URC_NO_REASON;
647 }
648
649 #endif
650
651 /********************
652 * __gnat_backtrace *
653 ********************/
654
655 int
656 __gnat_backtrace (void ** traceback __attribute__((unused)),
657 int max_len __attribute__((unused)),
658 void * exclude_min __attribute__((unused)),
659 void * exclude_max __attribute__((unused)),
660 int skip_frames __attribute__((unused)))
661 {
662 #if defined (__USING_SJLJ_EXCEPTIONS__)
663 /* We have no unwind material (tables) at hand with sjlj eh, and no
664 way to retrieve complete and accurate call chain information from
665 the context stack we maintain. */
666 return 0;
667 #else
668 uw_data_t uw_data;
669 /* State carried over during the whole unwinding process. */
670
671 uw_data.traceback = traceback;
672 uw_data.max_len = max_len;
673 uw_data.exclude_min = exclude_min;
674 uw_data.exclude_max = exclude_max;
675
676 uw_data.n_frames_to_skip = skip_frames;
677
678 uw_data.n_frames_skipped = 0;
679 uw_data.n_entries_filled = 0;
680
681 _Unwind_Backtrace ((_Unwind_Trace_Fn)trace_callback, &uw_data);
682
683 return uw_data.n_entries_filled;
684 #endif
685 }
686
687 /*------------------------------------------------------------------*
688 *-- The generic implementation based on frame layout assumptions --*
689 *------------------------------------------------------------------*/
690
691 #elif defined (USE_GENERIC_UNWINDER)
692
693 #ifndef CURRENT_STACK_FRAME
694 # define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
695 #endif
696
697 #ifndef VALID_STACK_FRAME
698 #define VALID_STACK_FRAME(ptr) 1
699 #endif
700
701 #ifndef MAX
702 #define MAX(x,y) ((x) > (y) ? (x) : (y))
703 #endif
704
705 #ifndef FORCE_CALL
706 #define FORCE_CALL 0
707 #endif
708
709 /* Make sure the function is not inlined. */
710 static void forced_callee (void) __attribute__ ((noinline));
711
712 static void forced_callee (void)
713 {
714 /* Make sure the function is not pure. */
715 volatile int i __attribute__ ((unused)) = 0;
716 }
717
718 int
719 __gnat_backtrace (void **array,
720 int size,
721 void *exclude_min,
722 void *exclude_max,
723 int skip_frames)
724 {
725 struct layout *current;
726 void *top_frame;
727 void *top_stack ATTRIBUTE_UNUSED;
728 int cnt = 0;
729
730 if (FORCE_CALL)
731 forced_callee ();
732
733 /* Force a call to builtin_frame_address with a positive argument
734 if required. This is necessary e.g. on SPARC to have the register
735 windows flushed before we attempt to access them on the stack. */
736 #if defined (FETCH_UP_FRAME_ADDRESS) && (FRAME_LEVEL == 0)
737 __builtin_frame_address (1);
738 #endif
739
740 top_frame = __builtin_frame_address (FRAME_LEVEL);
741 top_stack = CURRENT_STACK_FRAME;
742 current = (struct layout *) ((size_t) top_frame + FRAME_OFFSET (0));
743
744 /* Skip the number of calls we have been requested to skip, accounting for
745 the BASE_SKIP parameter.
746
747 FRAME_LEVEL is meaningless for the count adjustment. It impacts where we
748 start retrieving data from, but how many frames "up" we start at is in
749 BASE_SKIP by definition. */
750
751 skip_frames = MAX (0, skip_frames - BASE_SKIP);
752
753 while (cnt < skip_frames)
754 {
755 current = (struct layout *) ((size_t) current->next + FRAME_OFFSET (1));
756 cnt++;
757 }
758
759 cnt = 0;
760 while (cnt < size)
761 {
762 if (STOP_FRAME (current, top_stack) ||
763 !VALID_STACK_FRAME(((char *) current->return_address) + PC_ADJUST))
764 break;
765
766 if (current->return_address < exclude_min
767 || current->return_address > exclude_max)
768 array[cnt++] = ((char *) current->return_address) + PC_ADJUST;
769
770 current = (struct layout *) ((size_t) current->next + FRAME_OFFSET (1));
771 }
772
773 return cnt;
774 }
775
776 #else
777
778 /* No target specific implementation and neither USE_GCC_UNWINDER nor
779 USE_GENERIC_UNWINDER defined. */
780
781 /*------------------------------*
782 *-- The dummy implementation --*
783 *------------------------------*/
784
785 int
786 __gnat_backtrace (void **array ATTRIBUTE_UNUSED,
787 int size ATTRIBUTE_UNUSED,
788 void *exclude_min ATTRIBUTE_UNUSED,
789 void *exclude_max ATTRIBUTE_UNUSED,
790 int skip_frames ATTRIBUTE_UNUSED)
791 {
792 return 0;
793 }
794
795 #endif
796
797 #endif
798
799 #ifdef __cplusplus
800 }
801 #endif