OSDN Git Service

* Make-lang.in (gnat_ug_unx.info): Add dependency on stmp-docobjdir.
[pf3gnuchains/gcc-fork.git] / gcc / ada / memtrack.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                         S Y S T E M . M E M O R Y                        --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --             Copyright (C) 2001 Free Software Foundation, Inc.            --
10 --                                                                          --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the  contents of the part following the private keyword. --
14 --                                                                          --
15 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
16 -- terms of the  GNU General Public License as published  by the Free Soft- --
17 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
18 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
21 -- for  more details.  You should have  received  a copy of the GNU General --
22 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
23 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
24 -- MA 02111-1307, USA.                                                      --
25 --                                                                          --
26 -- As a special exception,  if other files  instantiate  generics from this --
27 -- unit, or you link  this unit with other files  to produce an executable, --
28 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
29 -- covered  by the  GNU  General  Public  License.  This exception does not --
30 -- however invalidate  any other reasons why  the executable file  might be --
31 -- covered by the  GNU Public License.                                      --
32 --                                                                          --
33 -- GNAT was originally developed  by the GNAT team at  New York University. --
34 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
35 --                                                                          --
36 ------------------------------------------------------------------------------
37
38 --  This version contains allocation tracking capability.
39 --  The object file corresponding to this instrumented version is to be found
40 --  in libgmem.
41 --  When enabled, the subsystem logs all the calls to __gnat_malloc and
42 --  __gnat_free. This log can then be processed by gnatmem to detect
43 --  dynamic memory leaks.
44 --
45 --  To use this functionality, you must compile your application with -g
46 --  and then link with this object file:
47 --
48 --     gnatmake -g program -largs -lgmem
49 --
50 --  After compilation, you may use your program as usual except that upon
51 --  completion, it will generate in the current directory the file gmem.out.
52 --
53 --  You can then investigate for possible memory leaks and mismatch by calling
54 --  gnatmem with this file as an input:
55 --
56 --    gnatmem -i gmem.out program
57 --
58 --  See gnatmem section in the GNAT User's Guide for more details.
59 --
60 --  NOTE: This capability is currently supported on the following targets:
61 --
62 --    Windows
63 --    GNU/Linux
64 --    HP-UX
65 --    Irix
66 --    Solaris
67 --    Tru64
68
69 pragma Source_File_Name (System.Memory, Body_File_Name => "memtrack.adb");
70
71 with Ada.Exceptions;
72 with System.Soft_Links;
73 with System.Traceback;
74
75 package body System.Memory is
76
77    use Ada.Exceptions;
78    use System.Soft_Links;
79    use System.Traceback;
80
81    function c_malloc (Size : size_t) return System.Address;
82    pragma Import (C, c_malloc, "malloc");
83
84    procedure c_free (Ptr : System.Address);
85    pragma Import (C, c_free, "free");
86
87    function c_realloc
88      (Ptr : System.Address; Size : size_t) return System.Address;
89    pragma Import (C, c_realloc, "realloc");
90
91    type File_Ptr is new System.Address;
92
93    function fopen (Path : String; Mode : String) return File_Ptr;
94    pragma Import (C, fopen);
95
96    procedure fwrite
97      (Ptr    : System.Address;
98       Size   : size_t;
99       Nmemb  : size_t;
100       Stream : File_Ptr);
101
102    procedure fwrite
103      (Str    : String;
104       Size   : size_t;
105       Nmemb  : size_t;
106       Stream : File_Ptr);
107    pragma Import (C, fwrite);
108
109    procedure fputc (C : Integer; Stream : File_Ptr);
110    pragma Import (C, fputc);
111
112    procedure fclose (Stream : File_Ptr);
113    pragma Import (C, fclose);
114
115    procedure Finalize;
116    --  Replace the default __gnat_finalize to properly close the log file.
117    pragma Export (C, Finalize, "__gnat_finalize");
118
119    Address_Size    : constant := System.Address'Max_Size_In_Storage_Elements;
120    --  Size in bytes of a pointer
121
122    Max_Call_Stack  : constant := 200;
123    --  Maximum number of frames supported
124
125    Skip_Frame : constant := 1;
126    --  Number of frames to remove from the call stack to hide functions from
127    --  this unit.
128
129    Tracebk   : aliased array (0 .. Max_Call_Stack) of System.Address;
130    Num_Calls : aliased Integer := 0;
131    --  Store the current call stack from Alloc and Free
132
133    Gmemfname : constant String := "gmem.out" & ASCII.NUL;
134    --  Allocation log of a program is saved in a file gmem.out
135    --  ??? What about Ada.Command_Line.Command_Name & ".out" instead of static
136    --  gmem.out
137
138    Gmemfile  : File_Ptr;
139    --  Global C file pointer to the allocation log
140
141    procedure Gmem_Initialize;
142    --  Initialization routine; opens the file and writes a header string. This
143    --  header string is used as a magic-tag to know if the .out file is to be
144    --  handled by GDB or by the GMEM (instrumented malloc/free) implementation.
145
146    -----------
147    -- Alloc --
148    -----------
149
150    function Alloc (Size : size_t) return System.Address is
151       Result      : aliased System.Address;
152       Actual_Size : aliased size_t := Size;
153
154    begin
155       if Size = size_t'Last then
156          Raise_Exception (Storage_Error'Identity, "object too large");
157       end if;
158
159       --  Change size from zero to non-zero. We still want a proper pointer
160       --  for the zero case because pointers to zero length objects have to
161       --  be distinct, but we can't just go ahead and allocate zero bytes,
162       --  since some malloc's return zero for a zero argument.
163
164       if Size = 0 then
165          Actual_Size := 1;
166       end if;
167
168       Lock_Task.all;
169
170       Result := c_malloc (Actual_Size);
171
172       --  Logs allocation call
173       --  format is:
174       --   'A' <mem addr> <size chunk> <len backtrace> <addr1> ... <addrn>
175
176       Gmem_Initialize;
177       Call_Chain (Tracebk'Address, Max_Call_Stack, Num_Calls);
178       Num_Calls := Num_Calls - Skip_Frame;
179       fputc (Character'Pos ('A'), Gmemfile);
180       fwrite (Result'Address, Address_Size, 1, Gmemfile);
181       fwrite (Actual_Size'Address, size_t'Max_Size_In_Storage_Elements, 1,
182               Gmemfile);
183       fwrite (Num_Calls'Address, Integer'Max_Size_In_Storage_Elements, 1,
184               Gmemfile);
185       fwrite (Tracebk (Skip_Frame)'Address, Address_Size, size_t (Num_Calls),
186               Gmemfile);
187
188       Unlock_Task.all;
189
190       if Result = System.Null_Address then
191          Raise_Exception (Storage_Error'Identity, "heap exhausted");
192       end if;
193
194       return Result;
195    end Alloc;
196
197    --------------
198    -- Finalize --
199    --------------
200
201    Needs_Init : Boolean := True;
202    --  Reset after first call to Gmem_Initialize
203
204    procedure Finalize is
205    begin
206       if not Needs_Init then
207          fclose (Gmemfile);
208       end if;
209    end Finalize;
210
211    ----------
212    -- Free --
213    ----------
214
215    procedure Free (Ptr : System.Address) is
216       Addr : aliased constant System.Address := Ptr;
217    begin
218       Lock_Task.all;
219
220       --  Logs deallocation call
221       --  format is:
222       --   'D' <mem addr> <len backtrace> <addr1> ... <addrn>
223
224       Gmem_Initialize;
225       Call_Chain (Tracebk'Address, Max_Call_Stack, Num_Calls);
226       Num_Calls := Num_Calls - Skip_Frame;
227       fputc (Character'Pos ('D'), Gmemfile);
228       fwrite (Addr'Address, Address_Size, 1, Gmemfile);
229       fwrite (Num_Calls'Address, Integer'Max_Size_In_Storage_Elements, 1,
230               Gmemfile);
231       fwrite (Tracebk (Skip_Frame)'Address, Address_Size, size_t (Num_Calls),
232               Gmemfile);
233
234       c_free (Ptr);
235
236       Unlock_Task.all;
237    end Free;
238
239    ---------------------
240    -- Gmem_Initialize --
241    ---------------------
242
243    procedure Gmem_Initialize is
244    begin
245       if Needs_Init then
246          Needs_Init := False;
247          Gmemfile := fopen (Gmemfname, "wb" & ASCII.NUL);
248          fwrite ("GMEM DUMP" & ASCII.LF, 10, 1, Gmemfile);
249       end if;
250    end Gmem_Initialize;
251
252    -------------
253    -- Realloc --
254    -------------
255
256    function Realloc
257      (Ptr : System.Address; Size : size_t) return System.Address
258    is
259       Result : System.Address;
260    begin
261       if Size = size_t'Last then
262          Raise_Exception (Storage_Error'Identity, "object too large");
263       end if;
264
265       Abort_Defer.all;
266       Result := c_realloc (Ptr, Size);
267       Abort_Undefer.all;
268
269       if Result = System.Null_Address then
270          Raise_Exception (Storage_Error'Identity, "heap exhausted");
271       end if;
272
273       return Result;
274    end Realloc;
275
276 end System.Memory;