Fix unused warnings.
[official-gcc/graphite-test-results.git] / gcc / ada / gmem.c
blob12c3c0a82ea5f5f2c80ecf0c193d7c046216a51b
1 /****************************************************************************
2 * *
3 * GNATMEM COMPONENTS *
4 * *
5 * G M E M *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2000-2009, 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 3, 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. *
17 * *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
21 * *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
26 * *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
29 * *
30 ****************************************************************************/
32 /* This unit reads the allocation tracking log produced by augmented
33 __gnat_malloc and __gnat_free procedures (see file memtrack.adb) and
34 provides GNATMEM tool with gdb-compliant output. The output is
35 processed by GNATMEM to detect dynamic memory allocation errors.
37 See GNATMEM section in GNAT User's Guide for more information.
39 NOTE: This capability is currently supported on the following targets:
41 DEC Unix
42 GNU/Linux x86
43 Solaris (sparc and x86) (*)
44 Windows 98/95/NT (x86)
45 Alpha OpenVMS
47 (*) on these targets, the compilation must be done with -funwind-tables to
48 be able to build the stack backtrace.
52 #ifdef VMS
53 #include <string.h>
54 #define xstrdup32(S) strcpy ((__char_ptr32) _malloc32 (strlen (S) + 1), S)
55 #else
56 #define xstrdup32(S) S
57 #endif
59 #include <stdio.h>
61 static FILE *gmemfile;
63 /* tb_len is the number of call level supported by this module */
64 #define tb_len 200
65 static void * tracebk [tb_len];
66 static int cur_tb_len, cur_tb_pos;
68 #define LOG_EOF '*'
69 #define LOG_ALLOC 'A'
70 #define LOG_DEALL 'D'
72 struct struct_storage_elmt {
73 char Elmt;
74 void * Address;
75 size_t Size;
76 long long Timestamp;
79 static void
80 __gnat_convert_addresses (void *addrs[], int n_addrs, void *buf, int *len);
81 /* Place in BUF a string representing the symbolic translation of N_ADDRS raw
82 addresses provided in ADDRS. LEN is filled with the result length.
84 This is a GNAT specific interface to the libaddr2line convert_addresses
85 routine. The latter examines debug info from a provided executable file
86 name to perform the translation into symbolic form of an input sequence of
87 raw binary addresses. It attempts to open the file from the provided name
88 "as is", so an absolute path must be provided to ensure the file is
89 always found. We compute this name once, at initialization time. */
91 static const char * exename = 0;
93 extern void convert_addresses (const char * , void *[], int, void *, int *);
94 extern char *__gnat_locate_exec_on_path (char *);
95 /* ??? Both of these extern functions are prototyped in adaint.h, which
96 also refers to "time_t" hence needs complex extra header inclusions to
97 be satisfied on every target. */
99 static void
100 __gnat_convert_addresses (void *addrs[], int n_addrs, void *buf, int *len)
102 if (exename != 0)
103 convert_addresses (exename, addrs, n_addrs, buf, len);
104 else
105 *len = 0;
108 /* reads backtrace information from gmemfile placing them in tracebk
109 array. cur_tb_len is the size of this array
112 static void
113 gmem_read_backtrace (void)
115 fread (&cur_tb_len, sizeof (int), 1, gmemfile);
116 fread (tracebk, sizeof (void *), cur_tb_len, gmemfile);
117 cur_tb_pos = 0;
120 /* initialize gmem feature from the dumpname file. It returns t0 timestamp
121 if the dumpname has been generated by GMEM (instrumented malloc/free)
122 and 0 if not.
125 long long __gnat_gmem_initialize (char *dumpname)
127 char header [10];
128 long long t0;
130 gmemfile = fopen (dumpname, "rb");
131 fread (header, 10, 1, gmemfile);
133 /* check for GMEM magic-tag */
134 if (memcmp (header, "GMEM DUMP\n", 10))
136 fclose (gmemfile);
137 return 0;
140 fread (&t0, sizeof (long long), 1, gmemfile);
142 return t0;
145 /* initialize addr2line library */
147 void __gnat_gmem_a2l_initialize (char *exearg)
149 /* Resolve the executable filename to use in later invocations of
150 the libaddr2line symbolization service. Ensure that on VMS
151 exename is allocated in 32 bit memory for compatibility
152 with libaddr2line. */
153 exename = xstrdup32 (__gnat_locate_exec_on_path (exearg));
156 /* Read next allocation of deallocation information from the GMEM file and
157 write an alloc/free information in buf to be processed by gnatmem */
159 void
160 __gnat_gmem_read_next (struct struct_storage_elmt *buf)
162 void *addr;
163 size_t size;
164 int j;
166 j = fgetc (gmemfile);
167 if (j == EOF)
169 fclose (gmemfile);
170 buf->Elmt = LOG_EOF;
172 else
174 switch (j)
176 case 'A' :
177 buf->Elmt = LOG_ALLOC;
178 fread (&(buf->Address), sizeof (void *), 1, gmemfile);
179 fread (&(buf->Size), sizeof (size_t), 1, gmemfile);
180 fread (&(buf->Timestamp), sizeof (long long), 1, gmemfile);
181 break;
182 case 'D' :
183 buf->Elmt = LOG_DEALL;
184 fread (&(buf->Address), sizeof (void *), 1, gmemfile);
185 fread (&(buf->Timestamp), sizeof (long long), 1, gmemfile);
186 break;
187 default:
188 puts ("GNATMEM dump file corrupt");
189 __gnat_os_exit (1);
192 gmem_read_backtrace ();
196 /* Read the next frame from the current traceback, and move the cursor to the
197 next frame */
199 void __gnat_gmem_read_next_frame (void** addr)
201 if (cur_tb_pos >= cur_tb_len) {
202 *addr = NULL;
203 } else {
204 *addr = (void*)*(tracebk + cur_tb_pos);
205 ++cur_tb_pos;
209 /* Converts addr into a symbolic traceback, and stores the result in buf
210 with a format suitable for gnatmem */
212 void __gnat_gmem_symbolic (void * addr, char* buf, int* length)
214 void * addresses [] = { addr };
216 __gnat_convert_addresses (addresses, 1, buf, length);