1 /****************************************************************************
9 * C Implementation File *
11 * Copyright (C) 2000-2001 Free Software Foundation, Inc. *
13 * GNAT is free software; you can redistribute it and/or modify it under *
14 * terms of the GNU General Public License as published by the Free Soft- *
15 * ware Foundation; either version 2, or (at your option) any later ver- *
16 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
17 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
19 * for more details. You should have received a copy of the GNU General *
20 * Public License distributed with GNAT; see file COPYING. If not, write *
21 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
22 * MA 02111-1307, USA. *
24 * As a special exception, if you link this file with other files to *
25 * produce an executable, this file does not by itself cause the resulting *
26 * executable to be covered by the GNU General Public License. This except- *
27 * ion does not however invalidate any other reasons why the executable *
28 * file might be covered by the GNU Public License. *
30 * GNAT was originally developed by the GNAT team at New York University. *
31 * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
33 ****************************************************************************/
35 /* This unit reads the allocation tracking log produced by augmented
36 __gnat_malloc and __gnat_free procedures (see file a-raise.c) and
37 provides GNATMEM tool with gdb-compliant output. The output is
38 processed by GNATMEM to detect dynamic memory allocation errors.
40 See GNATMEM section in GNAT User's Guide for more information.
42 NOTE: This capability is currently supported on the following targets:
47 Solaris (sparc and x86) (*)
48 Windows 98/95/NT (x86)
50 (*) on these targets, the compilation must be done with -funwind-tables to
51 be able to build the stack backtrace. */
53 #ifdef __alpha_vxworks
67 static FILE *gmemfile;
69 /* tb_len is the number of call level supported by this module */
72 static char *tracebk [TB_LEN];
73 static int cur_tb_len, cur_tb_pos;
75 extern void convert_addresses PARAMS ((char *[], int, void *,
77 static void gmem_read_backtrace PARAMS ((void));
78 static char *spc2nul PARAMS ((char *));
80 extern int __gnat_gmem_initialize PARAMS ((char *));
81 extern void __gnat_gmem_a2l_initialize PARAMS ((char *));
82 extern void __gnat_gmem_read_next PARAMS ((char *));
83 extern void __gnat_gmem_read_bt_frame PARAMS ((char *));
85 /* Reads backtrace information from gmemfile placing them in tracebk
86 array. cur_tb_len is the size of this array. */
89 gmem_read_backtrace ()
91 fread (&cur_tb_len, sizeof (int), 1, gmemfile);
92 fread (tracebk, sizeof (char *), cur_tb_len, gmemfile);
96 /* Initialize gmem feature from the dumpname file. Return 1 if the
97 dumpname has been generated by GMEM (instrumented malloc/free) and 0 if not
98 (i.e. probably a GDB generated file). */
101 __gnat_gmem_initialize (dumpname)
106 gmemfile = fopen (dumpname, "rb");
107 fread (header, 10, 1, gmemfile);
109 /* Check for GMEM magic-tag. */
110 if (memcmp (header, "GMEM DUMP\n", 10))
119 /* Initialize addr2line library */
122 __gnat_gmem_a2l_initialize (exename)
125 extern char **gnat_argv;
129 gnat_argv [0] = exename;
130 convert_addresses (tracebk, 1, s, &l);
133 /* Read next allocation of deallocation information from the GMEM file and
134 write an alloc/free information in buf to be processed by GDB (see gnatmem
138 __gnat_gmem_read_next (buf)
145 if ((c = fgetc (gmemfile)) == EOF)
148 sprintf (buf, "Program exited.");
155 fread (&addr, sizeof (char *), 1, gmemfile);
156 fread (&size, sizeof (int), 1, gmemfile);
157 sprintf (buf, "ALLOC^%d^0x%lx^", size, (long) addr);
160 fread (&addr, sizeof (char *), 1, gmemfile);
161 sprintf (buf, "DEALL^0x%lx^", (long) addr);
164 puts ("GMEM dump file corrupt");
168 gmem_read_backtrace ();
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. */
180 if (*s == ' ' || *s == '\n')
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). */
194 __gnat_gmem_read_bt_frame (buf)
201 if (cur_tb_pos >= cur_tb_len)
208 convert_addresses (tracebk + cur_tb_pos, 1, s, &l);
210 name = spc2nul (s) + 4;
211 file = spc2nul (name) + 4;
215 sprintf (buf, "# %s () at %s", name, file);