OSDN Git Service

2008-08-22 Sergey Rybin <rybin@adacore.com>
[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-2007, AdaCore                     *
10  *             Copyright (C) 2008, Free Software Foundation, Inc.           *
11  *                                                                          *
12  * GNAT is free software;  you can  redistribute it  and/or modify it under *
13  * terms of the  GNU General Public License as published  by the Free Soft- *
14  * ware  Foundation;  either version 2,  or (at your option) any later ver- *
15  * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
16  * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
17  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License *
18  * for  more details.  You should have  received  a copy of the GNU General *
19  * Public License  distributed with GNAT;  see file COPYING.  If not, write *
20  * to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, *
21  * Boston, MA 02110-1301, USA.                                              *
22  *                                                                          *
23  * As a  special  exception,  if you  link  this file  with other  files to *
24  * produce an executable,  this file does not by itself cause the resulting *
25  * executable to be covered by the GNU General Public License. This except- *
26  * ion does not  however invalidate  any other reasons  why the  executable *
27  * file might be covered by the  GNU Public License.                        *
28  *                                                                          *
29  * GNAT was originally developed  by the GNAT team at  New York University. *
30  * Extensive contributions were provided by Ada Core Technologies Inc.      *
31  *                                                                          *
32  ****************************************************************************/
33
34 /* This is an implementation of the __gnat_backtrace routine using the
35    underlying GCC unwinding support associated with the exception handling
36    infrastructure.  This will only work for ZCX based applications.  */
37
38 #include <unwind.h>
39
40 /* The implementation boils down to a call to _Unwind_Backtrace with a
41    tailored callback and carried-on data structure to keep track of the
42    input parameters we got as well as of the basic processing state.  */
43
44 /******************
45  * trace_callback *
46  ******************/
47
48 #if !defined (__USING_SJLJ_EXCEPTIONS__)
49
50 typedef struct {
51   void ** traceback;
52   int max_len;
53   void * exclude_min;
54   void * exclude_max;
55   int  n_frames_to_skip;
56   int  n_frames_skipped;
57   int  n_entries_filled;
58 } uw_data_t;
59
60 #if defined (__ia64__) && defined (__hpux__)
61 #include <uwx.h>
62 #endif
63
64 static _Unwind_Reason_Code
65 trace_callback (struct _Unwind_Context * uw_context, uw_data_t * uw_data)
66 {
67   void * pc;
68
69 #if defined (__ia64__) && defined (__hpux__)
70   /* Work around problem with _Unwind_GetIP on ia64 HP-UX. */
71   uwx_get_reg ((struct uwx_env *) uw_context, UWX_REG_IP, (uint64_t *) &pc);
72 #else
73   pc = (void *) _Unwind_GetIP (uw_context);
74 #endif
75
76   if (uw_data->n_frames_skipped < uw_data->n_frames_to_skip)
77     {
78       uw_data->n_frames_skipped ++;
79       return _URC_NO_REASON;
80     }
81
82   if (uw_data->n_entries_filled >= uw_data->max_len)
83     return _URC_NORMAL_STOP;
84
85   if (pc < uw_data->exclude_min || pc > uw_data->exclude_max)
86     uw_data->traceback [uw_data->n_entries_filled ++] = pc + PC_ADJUST;
87
88   return _URC_NO_REASON;
89 }
90
91 #endif
92
93 /********************
94  * __gnat_backtrace *
95  ********************/
96
97 int
98 __gnat_backtrace (void ** traceback, int max_len,
99                   void * exclude_min, void * exclude_max,
100                   int  skip_frames)
101 {
102 #if defined (__USING_SJLJ_EXCEPTIONS__)
103   /* We have no unwind material (tables) at hand with sjlj eh, and no
104      way to retrieve complete and accurate call chain information from
105      the context stack we maintain.  */
106   return 0;
107 #else
108   uw_data_t uw_data;
109   /* State carried over during the whole unwinding process.  */
110
111   uw_data.traceback   = traceback;
112   uw_data.max_len     = max_len;
113   uw_data.exclude_min = exclude_min;
114   uw_data.exclude_max = exclude_max;
115
116   uw_data.n_frames_to_skip = skip_frames;
117
118   uw_data.n_frames_skipped = 0;
119   uw_data.n_entries_filled = 0;
120
121   _Unwind_Backtrace ((_Unwind_Trace_Fn)trace_callback, &uw_data);
122
123   return uw_data.n_entries_filled;
124 #endif
125 }