1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . M E M O R Y --
9 -- Copyright (C) 2001-2007, 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 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. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- This version contains allocation tracking capability
36 -- The object file corresponding to this instrumented version is to be found
39 -- When enabled, the subsystem logs all the calls to __gnat_malloc and
40 -- __gnat_free. This log can then be processed by gnatmem to detect
41 -- dynamic memory leaks.
43 -- To use this functionality, you must compile your application with -g
44 -- and then link with this object file:
46 -- gnatmake -g program -largs -lgmem
48 -- After compilation, you may use your program as usual except that upon
49 -- completion, it will generate in the current directory the file gmem.out.
51 -- You can then investigate for possible memory leaks and mismatch by calling
52 -- gnatmem with this file as an input:
54 -- gnatmem -i gmem.out program
56 -- See gnatmem section in the GNAT User's Guide for more details
58 -- NOTE: This capability is currently supported on the following targets:
69 -- NOTE FOR FUTURE PLATFORMS SUPPORT: It is assumed that type Duration is
70 -- 64 bit. If the need arises to support architectures where this assumption
71 -- is incorrect, it will require changing the way timestamps of allocation
72 -- events are recorded.
74 pragma Source_File_Name (System.Memory, Body_File_Name => "memtrack.adb");
77 with System.Soft_Links;
78 with System.Traceback;
79 with System.Traceback_Entries;
81 with System.OS_Primitives;
83 package body System.Memory is
86 use System.Soft_Links;
88 use System.Traceback_Entries;
91 function c_malloc (Size : size_t) return System.Address;
92 pragma Import (C, c_malloc, "malloc");
94 procedure c_free (Ptr : System.Address);
95 pragma Import (C, c_free, "free");
98 (Ptr : System.Address; Size : size_t) return System.Address;
99 pragma Import (C, c_realloc, "realloc");
101 subtype File_Ptr is System.Address;
103 function fopen (Path : String; Mode : String) return File_Ptr;
104 pragma Import (C, fopen);
106 procedure OS_Exit (Status : Integer);
107 pragma Import (C, OS_Exit, "__gnat_os_exit");
108 pragma No_Return (OS_Exit);
111 (Ptr : System.Address;
121 pragma Import (C, fwrite);
123 procedure fputc (C : Integer; Stream : File_Ptr);
124 pragma Import (C, fputc);
126 procedure fclose (Stream : File_Ptr);
127 pragma Import (C, fclose);
130 pragma Export (C, Finalize, "__gnat_finalize");
131 -- Replace the default __gnat_finalize to properly close the log file
133 Address_Size : constant := System.Address'Max_Size_In_Storage_Elements;
134 -- Size in bytes of a pointer
136 Max_Call_Stack : constant := 200;
137 -- Maximum number of frames supported
139 Tracebk : aliased array (0 .. Max_Call_Stack) of Traceback_Entry;
140 Num_Calls : aliased Integer := 0;
142 Gmemfname : constant String := "gmem.out" & ASCII.NUL;
143 -- Allocation log of a program is saved in a file gmem.out
144 -- ??? What about Ada.Command_Line.Command_Name & ".out" instead of static
148 -- Global C file pointer to the allocation log
150 Needs_Init : Boolean := True;
151 -- Reset after first call to Gmem_Initialize
153 procedure Gmem_Initialize;
154 -- Initialization routine; opens the file and writes a header string. This
155 -- header string is used as a magic-tag to know if the .out file is to be
156 -- handled by GDB or by the GMEM (instrumented malloc/free) implementation.
158 First_Call : Boolean := True;
159 -- Depending on implementation, some of the traceback routines may
160 -- themselves do dynamic allocation. We use First_Call flag to avoid
161 -- infinite recursion
167 function Alloc (Size : size_t) return System.Address is
168 Result : aliased System.Address;
169 Actual_Size : aliased size_t := Size;
170 Timestamp : aliased Duration;
173 if Size = size_t'Last then
174 Raise_Exception (Storage_Error'Identity, "object too large");
177 -- Change size from zero to non-zero. We still want a proper pointer
178 -- for the zero case because pointers to zero length objects have to
179 -- be distinct, but we can't just go ahead and allocate zero bytes,
180 -- since some malloc's return zero for a zero argument.
188 Result := c_malloc (Actual_Size);
192 -- Logs allocation call
194 -- 'A' <mem addr> <size chunk> <len backtrace> <addr1> ... <addrn>
202 Timestamp := System.OS_Primitives.Clock;
203 Call_Chain (Tracebk'Address, Max_Call_Stack, Num_Calls,
205 fputc (Character'Pos ('A'), Gmemfile);
206 fwrite (Result'Address, Address_Size, 1, Gmemfile);
207 fwrite (Actual_Size'Address, size_t'Max_Size_In_Storage_Elements, 1,
209 fwrite (Timestamp'Address, Duration'Max_Size_In_Storage_Elements, 1,
211 fwrite (Num_Calls'Address, Integer'Max_Size_In_Storage_Elements, 1,
214 for J in Tracebk'First .. Tracebk'First + Num_Calls - 1 loop
216 Ptr : System.Address := PC_For (Tracebk (J));
218 fwrite (Ptr'Address, Address_Size, 1, Gmemfile);
228 if Result = System.Null_Address then
229 Raise_Exception (Storage_Error'Identity, "heap exhausted");
239 procedure Finalize is
241 if not Needs_Init then
250 procedure Free (Ptr : System.Address) is
251 Addr : aliased constant System.Address := Ptr;
252 Timestamp : aliased Duration;
259 -- Logs deallocation call
261 -- 'D' <mem addr> <len backtrace> <addr1> ... <addrn>
269 Call_Chain (Tracebk'Address, Max_Call_Stack, Num_Calls,
271 Timestamp := System.OS_Primitives.Clock;
272 fputc (Character'Pos ('D'), Gmemfile);
273 fwrite (Addr'Address, Address_Size, 1, Gmemfile);
274 fwrite (Timestamp'Address, Duration'Max_Size_In_Storage_Elements, 1,
276 fwrite (Num_Calls'Address, Integer'Max_Size_In_Storage_Elements, 1,
279 for J in Tracebk'First .. Tracebk'First + Num_Calls - 1 loop
281 Ptr : System.Address := PC_For (Tracebk (J));
283 fwrite (Ptr'Address, Address_Size, 1, Gmemfile);
295 ---------------------
296 -- Gmem_Initialize --
297 ---------------------
299 procedure Gmem_Initialize is
300 Timestamp : aliased Duration;
305 System.OS_Primitives.Initialize;
306 Timestamp := System.OS_Primitives.Clock;
307 Gmemfile := fopen (Gmemfname, "wb" & ASCII.NUL);
309 if Gmemfile = System.Null_Address then
310 Put_Line ("Couldn't open gnatmem log file for writing");
314 fwrite ("GMEM DUMP" & ASCII.LF, 10, 1, Gmemfile);
315 fwrite (Timestamp'Address, Duration'Max_Size_In_Storage_Elements, 1,
325 (Ptr : System.Address;
326 Size : size_t) return System.Address
328 Addr : aliased constant System.Address := Ptr;
329 Result : aliased System.Address;
330 Timestamp : aliased Duration;
333 -- For the purposes of allocations logging, we treat realloc as a free
334 -- followed by malloc. This is not exactly accurate, but is a good way
335 -- to fit it into malloc/free-centered reports.
337 if Size = size_t'Last then
338 Raise_Exception (Storage_Error'Identity, "object too large");
347 -- We first log deallocation call
352 Call_Chain (Tracebk'Address, Max_Call_Stack, Num_Calls,
354 Timestamp := System.OS_Primitives.Clock;
355 fputc (Character'Pos ('D'), Gmemfile);
356 fwrite (Addr'Address, Address_Size, 1, Gmemfile);
357 fwrite (Timestamp'Address, Duration'Max_Size_In_Storage_Elements, 1,
359 fwrite (Num_Calls'Address, Integer'Max_Size_In_Storage_Elements, 1,
362 for J in Tracebk'First .. Tracebk'First + Num_Calls - 1 loop
364 Ptr : System.Address := PC_For (Tracebk (J));
366 fwrite (Ptr'Address, Address_Size, 1, Gmemfile);
370 -- Now perform actual realloc
372 Result := c_realloc (Ptr, Size);
374 -- Log allocation call using the same backtrace
376 fputc (Character'Pos ('A'), Gmemfile);
377 fwrite (Result'Address, Address_Size, 1, Gmemfile);
378 fwrite (Size'Address, size_t'Max_Size_In_Storage_Elements, 1,
380 fwrite (Timestamp'Address, Duration'Max_Size_In_Storage_Elements, 1,
382 fwrite (Num_Calls'Address, Integer'Max_Size_In_Storage_Elements, 1,
385 for J in Tracebk'First .. Tracebk'First + Num_Calls - 1 loop
387 Ptr : System.Address := PC_For (Tracebk (J));
389 fwrite (Ptr'Address, Address_Size, 1, Gmemfile);
399 if Result = System.Null_Address then
400 Raise_Exception (Storage_Error'Identity, "heap exhausted");