Update concepts branch to revision 131834
[official-gcc.git] / gcc / ada / gmem.c
blobf19f77fca0b69b28897b8b10c37ff8ab548a27e8
1 /****************************************************************************
2 * *
3 * GNATMEM COMPONENTS *
4 * *
5 * G M E M *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2000-2008, Free Software Foundation, 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 ****************************************************************************/
33 /* This unit reads the allocation tracking log produced by augmented
34 __gnat_malloc and __gnat_free procedures (see file memtrack.adb) and
35 provides GNATMEM tool with gdb-compliant output. The output is
36 processed by GNATMEM to detect dynamic memory allocation errors.
38 See GNATMEM section in GNAT User's Guide for more information.
40 NOTE: This capability is currently supported on the following targets:
42 DEC Unix
43 GNU/Linux x86
44 Solaris (sparc and x86) (*)
45 Windows 98/95/NT (x86)
46 Alpha OpenVMS
48 (*) on these targets, the compilation must be done with -funwind-tables to
49 be able to build the stack backtrace.
53 #ifdef VMS
54 #include <string.h>
55 #define xstrdup32(S) strcpy ((__char_ptr32) _malloc32 (strlen (S) + 1), S)
56 #else
57 #define xstrdup32(S) S
58 #endif
60 #include <stdio.h>
62 static FILE *gmemfile;
64 /* tb_len is the number of call level supported by this module */
65 #define tb_len 200
66 static void * tracebk [tb_len];
67 static int cur_tb_len, cur_tb_pos;
69 #define LOG_EOF '*'
70 #define LOG_ALLOC 'A'
71 #define LOG_DEALL 'D'
73 struct struct_storage_elmt {
74 char Elmt;
75 void * Address;
76 size_t Size;
77 long long Timestamp;
80 static void
81 __gnat_convert_addresses (void *addrs[], int n_addrs, void *buf, int *len);
82 /* Place in BUF a string representing the symbolic translation of N_ADDRS raw
83 addresses provided in ADDRS. LEN is filled with the result length.
85 This is a GNAT specific interface to the libaddr2line convert_addresses
86 routine. The latter examines debug info from a provided executable file
87 name to perform the translation into symbolic form of an input sequence of
88 raw binary addresses. It attempts to open the file from the provided name
89 "as is", so an absolute path must be provided to ensure the file is
90 always found. We compute this name once, at initialization time. */
92 static const char * exename = 0;
94 extern void convert_addresses (const char * , void *[], int, void *, int *);
95 extern char *__gnat_locate_exec_on_path (char *);
96 /* ??? Both of these extern functions are prototyped in adaint.h, which
97 also refers to "time_t" hence needs complex extra header inclusions to
98 be satisfied on every target. */
100 static void
101 __gnat_convert_addresses (void *addrs[], int n_addrs, void *buf, int *len)
103 if (exename != 0)
104 convert_addresses (exename, addrs, n_addrs, buf, len);
105 else
106 *len = 0;
109 /* reads backtrace information from gmemfile placing them in tracebk
110 array. cur_tb_len is the size of this array
113 static void
114 gmem_read_backtrace (void)
116 fread (&cur_tb_len, sizeof (int), 1, gmemfile);
117 fread (tracebk, sizeof (void *), cur_tb_len, gmemfile);
118 cur_tb_pos = 0;
121 /* initialize gmem feature from the dumpname file. It returns t0 timestamp
122 if the dumpname has been generated by GMEM (instrumented malloc/free)
123 and 0 if not.
126 long long __gnat_gmem_initialize (char *dumpname)
128 char header [10];
129 long long t0;
131 gmemfile = fopen (dumpname, "rb");
132 fread (header, 10, 1, gmemfile);
134 /* check for GMEM magic-tag */
135 if (memcmp (header, "GMEM DUMP\n", 10))
137 fclose (gmemfile);
138 return 0;
141 fread (&t0, sizeof (long long), 1, gmemfile);
143 return t0;
146 /* initialize addr2line library */
148 void __gnat_gmem_a2l_initialize (char *exearg)
150 /* Resolve the executable filename to use in later invocations of
151 the libaddr2line symbolization service. Ensure that on VMS
152 exename is allocated in 32 bit memory for compatibility
153 with libaddr2line. */
154 exename = xstrdup32 (__gnat_locate_exec_on_path (exearg));
157 /* Read next allocation of deallocation information from the GMEM file and
158 write an alloc/free information in buf to be processed by gnatmem */
160 void
161 __gnat_gmem_read_next (struct struct_storage_elmt *buf)
163 void *addr;
164 size_t size;
165 int j;
167 j = fgetc (gmemfile);
168 if (j == EOF)
170 fclose (gmemfile);
171 buf->Elmt = LOG_EOF;
173 else
175 switch (j)
177 case 'A' :
178 buf->Elmt = LOG_ALLOC;
179 fread (&(buf->Address), sizeof (void *), 1, gmemfile);
180 fread (&(buf->Size), sizeof (size_t), 1, gmemfile);
181 fread (&(buf->Timestamp), sizeof (long long), 1, gmemfile);
182 break;
183 case 'D' :
184 buf->Elmt = LOG_DEALL;
185 fread (&(buf->Address), sizeof (void *), 1, gmemfile);
186 fread (&(buf->Timestamp), sizeof (long long), 1, gmemfile);
187 break;
188 default:
189 puts ("GNATMEM dump file corrupt");
190 __gnat_os_exit (1);
193 gmem_read_backtrace ();
197 /* Read the next frame from the current traceback, and move the cursor to the
198 next frame */
200 void __gnat_gmem_read_next_frame (void** addr)
202 if (cur_tb_pos >= cur_tb_len) {
203 *addr = NULL;
204 } else {
205 *addr = (void*)*(tracebk + cur_tb_pos);
206 ++cur_tb_pos;
210 /* Converts addr into a symbolic traceback, and stores the result in buf
211 with a format suitable for gnatmem */
213 void __gnat_gmem_symbolic (void * addr, char* buf, int* length)
215 void * addresses [] = { addr };
217 __gnat_convert_addresses (addresses, 1, buf, length);