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