OSDN Git Service

Delete all lines containing "$Revision:".
[pf3gnuchains/gcc-fork.git] / gcc / ada / gmem.c
1 /****************************************************************************
2  *                                                                          *
3  *                            GNATMEM COMPONENTS                            *
4  *                                                                          *
5  *                                 G M E M                                  *
6  *                                                                          *
7  *                                                                          *
8  *                          C Implementation File                           *
9  *                                                                          *
10  *         Copyright (C) 2000-2001 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,  59 Temple Place - Suite 330,  Boston, *
21  * MA 02111-1307, 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  * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
31  *                                                                          *
32  ****************************************************************************/
33
34 /*  This unit reads the allocation tracking log produced by augmented
35     __gnat_malloc and __gnat_free procedures (see file a-raise.c) and
36     provides GNATMEM tool with gdb-compliant output. The output is
37     processed by GNATMEM to detect dynamic memory allocation errors.
38
39     See GNATMEM section in GNAT User's Guide for more information.
40
41     NOTE: This capability is currently supported on the following targets:
42
43       DEC Unix
44       SGI Irix
45       GNU/Linux x86
46       Solaris (sparc and x86) (*)
47       Windows 98/95/NT (x86)
48
49     (*) on these targets, the compilation must be done with -funwind-tables to
50     be able to build the stack backtrace.   */
51
52 #ifdef __alpha_vxworks
53 #include "vxWorks.h"
54 #endif
55
56 #ifdef IN_RTS
57 #include "tconfig.h"
58 #include "tsystem.h"
59 #else
60 #include "config.h"
61 #include "system.h"
62 #endif
63
64 #include "adaint.h"
65
66 static FILE *gmemfile;
67
68 /* tb_len is the number of call level supported by this module */
69 #define TB_LEN 200
70
71 static char *tracebk[TB_LEN];
72 static int cur_tb_len, cur_tb_pos;
73
74 extern void convert_addresses           PARAMS ((char *[], int, void *,
75                                                  int *));
76 static void gmem_read_backtrace         PARAMS ((void));
77 static char *spc2nul                    PARAMS ((char *));
78
79 extern int __gnat_gmem_initialize       PARAMS ((char *));
80 extern void __gnat_gmem_a2l_initialize  PARAMS ((char *));
81 extern void __gnat_gmem_read_next       PARAMS ((char *));
82 extern void __gnat_gmem_read_bt_frame   PARAMS ((char *));
83 \f
84 /* Reads backtrace information from gmemfile placing them in tracebk
85    array. cur_tb_len is the size of this array.   */
86
87 static void
88 gmem_read_backtrace ()
89 {
90   fread (&cur_tb_len, sizeof (int), 1, gmemfile);
91   fread (tracebk, sizeof (char *), cur_tb_len, gmemfile);
92   cur_tb_pos = 0;
93 }
94
95 /* Initialize gmem feature from the dumpname file. Return 1 if the
96    dumpname has been generated by GMEM (instrumented malloc/free) and 0 if not
97    (i.e. probably a GDB generated file). */
98
99 int
100 __gnat_gmem_initialize (dumpname)
101      char *dumpname;
102 {
103   char header[10];
104
105   gmemfile = fopen (dumpname, "rb");
106   fread (header, 10, 1, gmemfile);
107
108   /* Check for GMEM magic-tag.  */
109   if (memcmp (header, "GMEM DUMP\n", 10))
110     {
111       fclose (gmemfile);
112       return 0;
113     }
114
115   return 1;
116 }
117
118 /* Initialize addr2line library */
119
120 void
121 __gnat_gmem_a2l_initialize (exename)
122      char *exename;
123 {
124   extern char **gnat_argv;
125   char s[100];
126   int l;
127
128   gnat_argv[0] = exename;
129   convert_addresses (tracebk, 1, s, &l);
130 }
131
132 /* Read next allocation of deallocation information from the GMEM file and
133    write an alloc/free information in buf to be processed by GDB (see gnatmem
134    implementation). */
135
136 void
137 __gnat_gmem_read_next (buf)
138      char *buf;
139 {
140   void *addr;
141   int size;
142   int j;
143
144   j = fgetc (gmemfile);
145   if (j == EOF)
146     {
147       fclose (gmemfile);
148       sprintf (buf, "Program exited.");
149     }
150   else
151     {
152       switch (j)
153         {
154           case 'A' :
155             fread (&addr, sizeof (char *), 1, gmemfile);
156             fread (&size, sizeof (int), 1, gmemfile);
157             sprintf (buf, "ALLOC^%d^0x%lx^", size, (long) addr);
158             break;
159           case 'D' :
160             fread (&addr, sizeof (char *), 1, gmemfile);
161             sprintf (buf, "DEALL^0x%lx^", (long) addr);
162             break;
163           default:
164             puts ("GMEM dump file corrupt");
165             __gnat_os_exit (1);
166         }
167
168       gmem_read_backtrace ();
169     }
170 }
171
172 /* Scans the line until the space or new-line character is encountered;
173    this character is replaced by nul and its position is returned.  */
174
175 static char *
176 spc2nul (s)
177      char *s;
178 {
179   while (*++s)
180     if (*s == ' ' || *s == '\n')
181       {
182         *s = 0;
183         return s;
184       }
185
186   abort ();
187 }
188
189 /* Convert backtrace address in tracebk at position cur_tb_pos to a symbolic
190    traceback information returned in buf and to be processed by GDB (see
191    gnatmem implementation).  */
192
193 void
194 __gnat_gmem_read_bt_frame (buf)
195      char *buf;
196 {
197   int l = 0;
198   char s[1000];
199   char *name, *file;
200
201   if (cur_tb_pos >= cur_tb_len)
202     {
203       buf[0] = ' ';
204       buf[1] = '\0';
205       return;
206     }
207
208   convert_addresses (tracebk + cur_tb_pos, 1, s, &l);
209   s[l] = '\0';
210   name = spc2nul (s) + 4;
211   file = spc2nul (name) + 4;
212   spc2nul (file);
213   ++cur_tb_pos;
214
215   sprintf (buf, "#  %s () at %s", name, file);
216 }