Fix thinko
[official-gcc.git] / gcc / ada / gmem.c
blobfe9928c6918a784ea861e7b517c91659e9500b75
1 /****************************************************************************
2 * *
3 * GNATMEM COMPONENTS *
4 * *
5 * G M E M *
6 * *
7 * *
8 * C Implementation File *
9 * *
10 * Copyright (C) 2000-2003 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 * Extensive contributions were provided by Ada Core Technologies Inc. *
31 * *
32 ****************************************************************************/
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.
39 See GNATMEM section in GNAT User's Guide for more information.
41 NOTE: This capability is currently supported on the following targets:
43 DEC Unix
44 SGI Irix
45 GNU/Linux x86
46 Solaris (sparc and x86) (*)
47 Windows 98/95/NT (x86)
49 (*) on these targets, the compilation must be done with -funwind-tables to
50 be able to build the stack backtrace. */
52 #ifdef __alpha_vxworks
53 #include "vxWorks.h"
54 #endif
56 #ifdef IN_RTS
57 #include "tconfig.h"
58 #include "tsystem.h"
59 #else
60 #include "config.h"
61 #include "system.h"
62 #endif
64 #include "adaint.h"
66 static FILE *gmemfile;
68 /* tb_len is the number of call level supported by this module */
69 #define TB_LEN 200
71 static char *tracebk[TB_LEN];
72 static int cur_tb_len, cur_tb_pos;
74 static void gmem_read_backtrace PARAMS ((void));
75 static char *spc2nul PARAMS ((char *));
77 extern int __gnat_gmem_initialize PARAMS ((char *));
78 extern void __gnat_gmem_a2l_initialize PARAMS ((char *));
79 extern void __gnat_gmem_read_next PARAMS ((char *));
80 extern void __gnat_gmem_read_bt_frame PARAMS ((char *));
82 /* Reads backtrace information from gmemfile placing them in tracebk
83 array. cur_tb_len is the size of this array. */
85 static void
86 gmem_read_backtrace ()
88 fread (&cur_tb_len, sizeof (int), 1, gmemfile);
89 fread (tracebk, sizeof (char *), cur_tb_len, gmemfile);
90 cur_tb_pos = 0;
93 /* Initialize gmem feature from the dumpname file. Return 1 if the
94 dumpname has been generated by GMEM (instrumented malloc/free) and 0 if not
95 (i.e. probably a GDB generated file). */
97 int
98 __gnat_gmem_initialize (dumpname)
99 char *dumpname;
101 char header[10];
103 gmemfile = fopen (dumpname, "rb");
104 fread (header, 10, 1, gmemfile);
106 /* Check for GMEM magic-tag. */
107 if (memcmp (header, "GMEM DUMP\n", 10))
109 fclose (gmemfile);
110 return 0;
113 return 1;
116 /* Initialize addr2line library */
118 void
119 __gnat_gmem_a2l_initialize (exename)
120 char *exename;
122 extern char **gnat_argv;
123 char s[100];
124 int l;
126 gnat_argv[0] = exename;
127 convert_addresses (tracebk, 1, s, &l);
130 /* Read next allocation of deallocation information from the GMEM file and
131 write an alloc/free information in buf to be processed by GDB (see gnatmem
132 implementation). */
134 void
135 __gnat_gmem_read_next (buf)
136 char *buf;
138 void *addr;
139 int size;
140 int j;
142 j = fgetc (gmemfile);
143 if (j == EOF)
145 fclose (gmemfile);
146 sprintf (buf, "Program exited.");
148 else
150 switch (j)
152 case 'A' :
153 fread (&addr, sizeof (char *), 1, gmemfile);
154 fread (&size, sizeof (int), 1, gmemfile);
155 sprintf (buf, "ALLOC^%d^0x%lx^", size, (long) addr);
156 break;
157 case 'D' :
158 fread (&addr, sizeof (char *), 1, gmemfile);
159 sprintf (buf, "DEALL^0x%lx^", (long) addr);
160 break;
161 default:
162 puts ("GMEM dump file corrupt");
163 __gnat_os_exit (1);
166 gmem_read_backtrace ();
170 /* Scans the line until the space or new-line character is encountered;
171 this character is replaced by nul and its position is returned. */
173 static char *
174 spc2nul (s)
175 char *s;
177 while (*++s)
178 if (*s == ' ' || *s == '\n')
180 *s = 0;
181 return s;
184 abort ();
187 /* Convert backtrace address in tracebk at position cur_tb_pos to a symbolic
188 traceback information returned in buf and to be processed by GDB (see
189 gnatmem implementation). */
191 void
192 __gnat_gmem_read_bt_frame (buf)
193 char *buf;
195 int l = 0;
196 char s[1000];
197 char *name, *file;
199 if (cur_tb_pos >= cur_tb_len)
201 buf[0] = ' ';
202 buf[1] = '\0';
203 return;
206 convert_addresses (tracebk + cur_tb_pos, 1, s, &l);
207 s[l] = '\0';
208 name = spc2nul (s) + 4;
209 file = spc2nul (name) + 4;
210 spc2nul (file);
211 ++cur_tb_pos;
213 sprintf (buf, "# %s () at %s", name, file);