OSDN Git Service

2007-08-31 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / gmem.c
1 /****************************************************************************
2  *                                                                          *
3  *                            GNATMEM COMPONENTS                            *
4  *                                                                          *
5  *                                 G M E M                                  *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *         Copyright (C) 2000-2007, 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  ****************************************************************************/
32
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.
37
38     See GNATMEM section in GNAT User's Guide for more information.
39
40     NOTE: This capability is currently supported on the following targets:
41
42       DEC Unix
43       GNU/Linux x86
44       Solaris (sparc and x86) (*)
45       Windows 98/95/NT (x86)
46       Alpha OpenVMS
47
48     (*) on these targets, the compilation must be done with -funwind-tables to
49     be able to build the stack backtrace.
50
51 */
52
53 #include <stdio.h>
54
55 static FILE *gmemfile;
56
57 /* tb_len is the number of call level supported by this module */
58 #define tb_len 200
59 static void * tracebk [tb_len];
60 static int cur_tb_len, cur_tb_pos;
61
62 #define LOG_EOF   '*'
63 #define LOG_ALLOC 'A'
64 #define LOG_DEALL 'D'
65
66 struct struct_storage_elmt {
67   char   Elmt;
68   void * Address;
69   size_t Size;
70   long long Timestamp;
71 };
72
73 static void
74 __gnat_convert_addresses (void *addrs[], int n_addrs, void *buf, int *len);
75 /* Place in BUF a string representing the symbolic translation of N_ADDRS raw
76    addresses provided in ADDRS.  LEN is filled with the result length.
77
78    This is a GNAT specific interface to the libaddr2line convert_addresses
79    routine.  The latter examines debug info from a provided executable file
80    name to perform the translation into symbolic form of an input sequence of
81    raw binary addresses.  It attempts to open the file from the provided name
82    "as is", so an an absolute path must be provided to ensure the file is
83    always found.  We compute this name once, at initialization time.  */
84
85 static const char * exename = 0;
86
87 extern void convert_addresses (const char * , void *[], int, void *, int *);
88 extern char  *__gnat_locate_exec_on_path (char *);
89 /* ??? Both of these extern functions are prototyped in adaint.h, which
90    also refers to "time_t" hence needs complex extra header inclusions to
91    be satisfied on every target.  */
92
93 static void
94 __gnat_convert_addresses (void *addrs[], int n_addrs, void *buf, int *len)
95 {
96   if (exename != 0)
97     convert_addresses (exename, addrs, n_addrs, buf, len);
98   else
99     *len = 0;
100 }
101
102 /* reads backtrace information from gmemfile placing them in tracebk
103    array. cur_tb_len is the size of this array
104 */
105
106 static void
107 gmem_read_backtrace (void)
108 {
109   fread (&cur_tb_len, sizeof (int), 1, gmemfile);
110   fread (tracebk, sizeof (void *), cur_tb_len, gmemfile);
111   cur_tb_pos = 0;
112 }
113
114 /* initialize gmem feature from the dumpname file. It returns t0 timestamp
115    if the dumpname has been generated by GMEM (instrumented malloc/free)
116    and 0 if not.
117 */
118
119 long long __gnat_gmem_initialize (char *dumpname)
120 {
121   char header [10];
122   long long t0;
123
124   gmemfile = fopen (dumpname, "rb");
125   fread (header, 10, 1, gmemfile);
126
127   /* check for GMEM magic-tag */
128   if (memcmp (header, "GMEM DUMP\n", 10))
129     {
130       fclose (gmemfile);
131       return 0;
132     }
133
134   fread (&t0, sizeof (long long), 1, gmemfile);
135
136   return t0;
137 }
138
139 /* initialize addr2line library */
140
141 void __gnat_gmem_a2l_initialize (char *exearg)
142 {
143   /* Resolve the executable filename to use in later invocations of
144      the libaddr2line symbolization service.  */
145   exename = __gnat_locate_exec_on_path (exearg);
146 }
147
148 /* Read next allocation of deallocation information from the GMEM file and
149    write an alloc/free information in buf to be processed by gnatmem */
150
151 void
152 __gnat_gmem_read_next (struct struct_storage_elmt *buf)
153 {
154   void *addr;
155   size_t size;
156   int j;
157
158   j = fgetc (gmemfile);
159   if (j == EOF)
160     {
161       fclose (gmemfile);
162       buf->Elmt = LOG_EOF;
163     }
164   else
165     {
166       switch (j)
167         {
168           case 'A' :
169             buf->Elmt = LOG_ALLOC;
170             fread (&(buf->Address), sizeof (void *), 1, gmemfile);
171             fread (&(buf->Size), sizeof (size_t), 1, gmemfile);
172             fread (&(buf->Timestamp), sizeof (long long), 1, gmemfile);
173             break;
174           case 'D' :
175             buf->Elmt = LOG_DEALL;
176             fread (&(buf->Address), sizeof (void *), 1, gmemfile);
177             fread (&(buf->Timestamp), sizeof (long long), 1, gmemfile);
178             break;
179           default:
180             puts ("GNATMEM dump file corrupt");
181             __gnat_os_exit (1);
182         }
183
184       gmem_read_backtrace ();
185     }
186 }
187
188 /* Read the next frame from the current traceback, and move the cursor to the
189    next frame */
190
191 void __gnat_gmem_read_next_frame (void** addr)
192 {
193   if (cur_tb_pos >= cur_tb_len) {
194     *addr = NULL;
195   } else {
196     *addr = (void*)*(tracebk + cur_tb_pos);
197     ++cur_tb_pos;
198   }
199 }
200
201 /* Converts addr into a symbolic traceback, and stores the result in buf
202    with a format suitable for gnatmem */
203
204 void __gnat_gmem_symbolic (void * addr, char* buf, int* length)
205 {
206   void * addresses [] = { addr };
207
208   __gnat_convert_addresses (addresses, 1, buf, length);
209 }