1 /****************************************************************************
7 * C Implementation File *
9 * Copyright (C) 2000-2009, Free Software Foundation, Inc. *
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. *
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. *
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/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
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:
43 Solaris (sparc and x86) (*)
44 Windows 98/95/NT (x86)
47 (*) on these targets, the compilation must be done with -funwind-tables to
48 be able to build the stack backtrace.
54 #define xstrdup32(S) strcpy ((__char_ptr32) _malloc32 (strlen (S) + 1), S)
56 #define xstrdup32(S) S
61 static FILE *gmemfile
;
63 /* tb_len is the number of call level supported by this module */
65 static void * tracebk
[tb_len
];
66 static int cur_tb_len
, cur_tb_pos
;
72 struct struct_storage_elmt
{
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. */
100 __gnat_convert_addresses (void *addrs
[], int n_addrs
, void *buf
, int *len
)
103 convert_addresses (exename
, addrs
, n_addrs
, buf
, len
);
108 /* reads backtrace information from gmemfile placing them in tracebk
109 array. cur_tb_len is the size of this array
113 gmem_read_backtrace (void)
115 fread (&cur_tb_len
, sizeof (int), 1, gmemfile
);
116 fread (tracebk
, sizeof (void *), cur_tb_len
, gmemfile
);
120 /* initialize gmem feature from the dumpname file. It returns t0 timestamp
121 if the dumpname has been generated by GMEM (instrumented malloc/free)
125 long long __gnat_gmem_initialize (char *dumpname
)
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))
140 fread (&t0
, sizeof (long long), 1, gmemfile
);
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 */
160 __gnat_gmem_read_next (struct struct_storage_elmt
*buf
)
166 j
= fgetc (gmemfile
);
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
);
183 buf
->Elmt
= LOG_DEALL
;
184 fread (&(buf
->Address
), sizeof (void *), 1, gmemfile
);
185 fread (&(buf
->Timestamp
), sizeof (long long), 1, gmemfile
);
188 puts ("GNATMEM dump file corrupt");
192 gmem_read_backtrace ();
196 /* Read the next frame from the current traceback, and move the cursor to the
199 void __gnat_gmem_read_next_frame (void** addr
)
201 if (cur_tb_pos
>= cur_tb_len
) {
204 *addr
= (void*)*(tracebk
+ 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
);