OSDN Git Service

* gcc.dg/attr-weakref-1.c: Add exit (0) to avoid spurious
[pf3gnuchains/gcc-fork.git] / gcc / ada / tb-gcc.c
1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                   T R A C E B A C K - G C C t a b l e s                  *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *              Copyright (C) 2004 Ada Core Technologies, 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 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 is an implementation of the __gnat_backtrace routine using the
34    underlying GCC unwinding support associated with the exception handling
35    infrastructure.  This will only work for ZCX based applications.  */
36
37 #include <unwind.h>
38
39 /* The implementation boils down to a call to _Unwind_Backtrace with a
40    tailored callback and carried-on datastructure to keep track of the
41    input parameters we got as well as of the basic processing state.  */
42
43 typedef struct {
44   void ** traceback;
45   int max_len;
46   void * exclude_min;
47   void * exclude_max;
48   int  n_frames_to_skip;
49   int  n_frames_skipped;
50   int  n_entries_filled;
51 } uw_data_t;
52
53 /******************
54  * trace_callback *
55  ******************/
56
57 static _Unwind_Reason_Code
58 trace_callback (struct _Unwind_Context * uw_context, uw_data_t * uw_data)
59 {
60   void * pc = (void *) _Unwind_GetIP (uw_context);
61
62   if (uw_data->n_frames_skipped < uw_data->n_frames_to_skip)
63     {
64       uw_data->n_frames_skipped ++;
65       return _URC_NO_REASON;
66     }
67
68   if (uw_data->n_entries_filled >= uw_data->max_len)
69     return _URC_NORMAL_STOP;
70
71   if (pc < uw_data->exclude_min || pc > uw_data->exclude_max)
72     uw_data->traceback [uw_data->n_entries_filled ++] = pc + PC_ADJUST;
73
74   return _URC_NO_REASON;
75 }
76
77 /********************
78  * __gnat_backtrace *
79  ********************/
80
81 int
82 __gnat_backtrace (void ** traceback, int max_len,
83                   void * exclude_min, void * exclude_max,
84                   int  skip_frames)
85 {
86   uw_data_t uw_data;
87   /* State carried over during the whole unwinding process.  */
88
89   uw_data.traceback   = traceback;
90   uw_data.max_len     = max_len;
91   uw_data.exclude_min = exclude_min;
92   uw_data.exclude_max = exclude_max;
93
94   uw_data.n_frames_to_skip = skip_frames;
95
96   uw_data.n_frames_skipped = 0;
97   uw_data.n_entries_filled = 0;
98
99   _Unwind_Backtrace ((_Unwind_Trace_Fn)trace_callback, &uw_data);
100
101   return uw_data.n_entries_filled;
102 }