1 |
281 |
jeremybenn |
/****************************************************************************
|
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 |
|
|
****************************************************************************/
|
31 |
|
|
|
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.
|
36 |
|
|
|
37 |
|
|
See GNATMEM section in GNAT User's Guide for more information.
|
38 |
|
|
|
39 |
|
|
NOTE: This capability is currently supported on the following targets:
|
40 |
|
|
|
41 |
|
|
DEC Unix
|
42 |
|
|
GNU/Linux x86
|
43 |
|
|
Solaris (sparc and x86) (*)
|
44 |
|
|
Windows 98/95/NT (x86)
|
45 |
|
|
Alpha OpenVMS
|
46 |
|
|
|
47 |
|
|
(*) on these targets, the compilation must be done with -funwind-tables to
|
48 |
|
|
be able to build the stack backtrace.
|
49 |
|
|
|
50 |
|
|
*/
|
51 |
|
|
|
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
|
58 |
|
|
|
59 |
|
|
#include <stdio.h>
|
60 |
|
|
|
61 |
|
|
static FILE *gmemfile;
|
62 |
|
|
|
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;
|
67 |
|
|
|
68 |
|
|
#define LOG_EOF '*'
|
69 |
|
|
#define LOG_ALLOC 'A'
|
70 |
|
|
#define LOG_DEALL 'D'
|
71 |
|
|
|
72 |
|
|
struct struct_storage_elmt {
|
73 |
|
|
char Elmt;
|
74 |
|
|
void * Address;
|
75 |
|
|
size_t Size;
|
76 |
|
|
long long Timestamp;
|
77 |
|
|
};
|
78 |
|
|
|
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.
|
83 |
|
|
|
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. */
|
90 |
|
|
|
91 |
|
|
static const char * exename = 0;
|
92 |
|
|
|
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. */
|
98 |
|
|
|
99 |
|
|
static void
|
100 |
|
|
__gnat_convert_addresses (void *addrs[], int n_addrs, void *buf, int *len)
|
101 |
|
|
{
|
102 |
|
|
if (exename != 0)
|
103 |
|
|
convert_addresses (exename, addrs, n_addrs, buf, len);
|
104 |
|
|
else
|
105 |
|
|
*len = 0;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
/* reads backtrace information from gmemfile placing them in tracebk
|
109 |
|
|
array. cur_tb_len is the size of this array
|
110 |
|
|
*/
|
111 |
|
|
|
112 |
|
|
static void
|
113 |
|
|
gmem_read_backtrace (void)
|
114 |
|
|
{
|
115 |
|
|
fread (&cur_tb_len, sizeof (int), 1, gmemfile);
|
116 |
|
|
fread (tracebk, sizeof (void *), cur_tb_len, gmemfile);
|
117 |
|
|
cur_tb_pos = 0;
|
118 |
|
|
}
|
119 |
|
|
|
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.
|
123 |
|
|
*/
|
124 |
|
|
|
125 |
|
|
long long __gnat_gmem_initialize (char *dumpname)
|
126 |
|
|
{
|
127 |
|
|
char header [10];
|
128 |
|
|
long long t0;
|
129 |
|
|
|
130 |
|
|
gmemfile = fopen (dumpname, "rb");
|
131 |
|
|
fread (header, 10, 1, gmemfile);
|
132 |
|
|
|
133 |
|
|
/* check for GMEM magic-tag */
|
134 |
|
|
if (memcmp (header, "GMEM DUMP\n", 10))
|
135 |
|
|
{
|
136 |
|
|
fclose (gmemfile);
|
137 |
|
|
return 0;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
fread (&t0, sizeof (long long), 1, gmemfile);
|
141 |
|
|
|
142 |
|
|
return t0;
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
/* initialize addr2line library */
|
146 |
|
|
|
147 |
|
|
void __gnat_gmem_a2l_initialize (char *exearg)
|
148 |
|
|
{
|
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));
|
154 |
|
|
}
|
155 |
|
|
|
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 */
|
158 |
|
|
|
159 |
|
|
void
|
160 |
|
|
__gnat_gmem_read_next (struct struct_storage_elmt *buf)
|
161 |
|
|
{
|
162 |
|
|
void *addr;
|
163 |
|
|
size_t size;
|
164 |
|
|
int j;
|
165 |
|
|
|
166 |
|
|
j = fgetc (gmemfile);
|
167 |
|
|
if (j == EOF)
|
168 |
|
|
{
|
169 |
|
|
fclose (gmemfile);
|
170 |
|
|
buf->Elmt = LOG_EOF;
|
171 |
|
|
}
|
172 |
|
|
else
|
173 |
|
|
{
|
174 |
|
|
switch (j)
|
175 |
|
|
{
|
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);
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
gmem_read_backtrace ();
|
193 |
|
|
}
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
/* Read the next frame from the current traceback, and move the cursor to the
|
197 |
|
|
next frame */
|
198 |
|
|
|
199 |
|
|
void __gnat_gmem_read_next_frame (void** addr)
|
200 |
|
|
{
|
201 |
|
|
if (cur_tb_pos >= cur_tb_len) {
|
202 |
|
|
*addr = NULL;
|
203 |
|
|
} else {
|
204 |
|
|
*addr = (void*)*(tracebk + cur_tb_pos);
|
205 |
|
|
++cur_tb_pos;
|
206 |
|
|
}
|
207 |
|
|
}
|
208 |
|
|
|
209 |
|
|
/* Converts addr into a symbolic traceback, and stores the result in buf
|
210 |
|
|
with a format suitable for gnatmem */
|
211 |
|
|
|
212 |
|
|
void __gnat_gmem_symbolic (void * addr, char* buf, int* length)
|
213 |
|
|
{
|
214 |
|
|
void * addresses [] = { addr };
|
215 |
|
|
|
216 |
|
|
__gnat_convert_addresses (addresses, 1, buf, length);
|
217 |
|
|
}
|