1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . M E M O R Y --
9 -- Copyright (C) 2001-2012, 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 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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- This version contains allocation tracking capability
34 -- The object file corresponding to this instrumented version is to be found
37 -- When enabled, the subsystem logs all the calls to __gnat_malloc and
38 -- __gnat_free. This log can then be processed by gnatmem to detect
39 -- dynamic memory leaks.
41 -- To use this functionality, you must compile your application with -g
42 -- and then link with this object file:
44 -- gnatmake -g program -largs -lgmem
46 -- After compilation, you may use your program as usual except that upon
47 -- completion, it will generate in the current directory the file gmem.out.
49 -- You can then investigate for possible memory leaks and mismatch by calling
50 -- gnatmem with this file as an input:
52 -- gnatmem -i gmem.out program
54 -- See gnatmem section in the GNAT User's Guide for more details
56 -- NOTE: This capability is currently supported on the following targets:
65 -- NOTE FOR FUTURE PLATFORMS SUPPORT: It is assumed that type Duration is
66 -- 64 bit. If the need arises to support architectures where this assumption
67 -- is incorrect, it will require changing the way timestamps of allocation
68 -- events are recorded.
70 pragma Source_File_Name
(System
.Memory
, Body_File_Name
=> "memtrack.adb");
73 with System
.Soft_Links
;
74 with System
.Traceback
;
75 with System
.Traceback_Entries
;
77 with System
.OS_Primitives
;
79 package body System
.Memory
is
82 use System
.Soft_Links
;
84 use System
.Traceback_Entries
;
87 function c_malloc
(Size
: size_t
) return System
.Address
;
88 pragma Import
(C
, c_malloc
, "malloc");
90 procedure c_free
(Ptr
: System
.Address
);
91 pragma Import
(C
, c_free
, "free");
94 (Ptr
: System
.Address
; Size
: size_t
) return System
.Address
;
95 pragma Import
(C
, c_realloc
, "realloc");
97 subtype File_Ptr
is System
.Address
;
99 function fopen
(Path
: String; Mode
: String) return File_Ptr
;
100 pragma Import
(C
, fopen
);
102 procedure OS_Exit
(Status
: Integer);
103 pragma Import
(C
, OS_Exit
, "__gnat_os_exit");
104 pragma No_Return
(OS_Exit
);
107 (Ptr
: System
.Address
;
117 pragma Import
(C
, fwrite
);
119 procedure fputc
(C
: Integer; Stream
: File_Ptr
);
120 pragma Import
(C
, fputc
);
122 procedure fclose
(Stream
: File_Ptr
);
123 pragma Import
(C
, fclose
);
126 pragma Export
(C
, Finalize
, "__gnat_finalize");
127 -- Replace the default __gnat_finalize to properly close the log file
129 Address_Size
: constant := System
.Address
'Max_Size_In_Storage_Elements;
130 -- Size in bytes of a pointer
132 Max_Call_Stack
: constant := 200;
133 -- Maximum number of frames supported
135 Tracebk
: aliased array (0 .. Max_Call_Stack
) of Traceback_Entry
;
136 Num_Calls
: aliased Integer := 0;
138 Gmemfname
: constant String := "gmem.out" & ASCII
.NUL
;
139 -- Allocation log of a program is saved in a file gmem.out
140 -- ??? What about Ada.Command_Line.Command_Name & ".out" instead of static
144 -- Global C file pointer to the allocation log
146 Needs_Init
: Boolean := True;
147 -- Reset after first call to Gmem_Initialize
149 procedure Gmem_Initialize
;
150 -- Initialization routine; opens the file and writes a header string. This
151 -- header string is used as a magic-tag to know if the .out file is to be
152 -- handled by GDB or by the GMEM (instrumented malloc/free) implementation.
154 First_Call
: Boolean := True;
155 -- Depending on implementation, some of the traceback routines may
156 -- themselves do dynamic allocation. We use First_Call flag to avoid
157 -- infinite recursion
163 function Alloc
(Size
: size_t
) return System
.Address
is
164 Result
: aliased System
.Address
;
165 Actual_Size
: aliased size_t
:= Size
;
166 Timestamp
: aliased Duration;
169 if Size
= size_t
'Last then
170 Raise_Exception
(Storage_Error
'Identity, "object too large");
173 -- Change size from zero to non-zero. We still want a proper pointer
174 -- for the zero case because pointers to zero length objects have to
175 -- be distinct, but we can't just go ahead and allocate zero bytes,
176 -- since some malloc's return zero for a zero argument.
184 Result
:= c_malloc
(Actual_Size
);
188 -- Logs allocation call
190 -- 'A' <mem addr> <size chunk> <len backtrace> <addr1> ... <addrn>
198 Timestamp
:= System
.OS_Primitives
.Clock
;
199 Call_Chain
(Tracebk
'Address, Max_Call_Stack
, Num_Calls
,
201 fputc
(Character'Pos ('A'), Gmemfile
);
202 fwrite
(Result
'Address, Address_Size
, 1, Gmemfile
);
203 fwrite
(Actual_Size
'Address, size_t
'Max_Size_In_Storage_Elements, 1,
205 fwrite
(Timestamp
'Address, Duration'Max_Size_In_Storage_Elements, 1,
207 fwrite
(Num_Calls
'Address, Integer'Max_Size_In_Storage_Elements, 1,
210 for J
in Tracebk
'First .. Tracebk
'First + Num_Calls
- 1 loop
212 Ptr
: System
.Address
:= PC_For
(Tracebk
(J
));
214 fwrite
(Ptr
'Address, Address_Size
, 1, Gmemfile
);
224 if Result
= System
.Null_Address
then
225 Raise_Exception
(Storage_Error
'Identity, "heap exhausted");
235 procedure Finalize
is
237 if not Needs_Init
then
246 procedure Free
(Ptr
: System
.Address
) is
247 Addr
: aliased constant System
.Address
:= Ptr
;
248 Timestamp
: aliased Duration;
255 -- Logs deallocation call
257 -- 'D' <mem addr> <len backtrace> <addr1> ... <addrn>
265 Call_Chain
(Tracebk
'Address, Max_Call_Stack
, Num_Calls
,
267 Timestamp
:= System
.OS_Primitives
.Clock
;
268 fputc
(Character'Pos ('D'), Gmemfile
);
269 fwrite
(Addr
'Address, Address_Size
, 1, Gmemfile
);
270 fwrite
(Timestamp
'Address, Duration'Max_Size_In_Storage_Elements, 1,
272 fwrite
(Num_Calls
'Address, Integer'Max_Size_In_Storage_Elements, 1,
275 for J
in Tracebk
'First .. Tracebk
'First + Num_Calls
- 1 loop
277 Ptr
: System
.Address
:= PC_For
(Tracebk
(J
));
279 fwrite
(Ptr
'Address, Address_Size
, 1, Gmemfile
);
291 ---------------------
292 -- Gmem_Initialize --
293 ---------------------
295 procedure Gmem_Initialize
is
296 Timestamp
: aliased Duration;
301 System
.OS_Primitives
.Initialize
;
302 Timestamp
:= System
.OS_Primitives
.Clock
;
303 Gmemfile
:= fopen
(Gmemfname
, "wb" & ASCII
.NUL
);
305 if Gmemfile
= System
.Null_Address
then
306 Put_Line
("Couldn't open gnatmem log file for writing");
310 fwrite
("GMEM DUMP" & ASCII
.LF
, 10, 1, Gmemfile
);
311 fwrite
(Timestamp
'Address, Duration'Max_Size_In_Storage_Elements, 1,
321 (Ptr
: System
.Address
;
322 Size
: size_t
) return System
.Address
324 Addr
: aliased constant System
.Address
:= Ptr
;
325 Result
: aliased System
.Address
;
326 Timestamp
: aliased Duration;
329 -- For the purposes of allocations logging, we treat realloc as a free
330 -- followed by malloc. This is not exactly accurate, but is a good way
331 -- to fit it into malloc/free-centered reports.
333 if Size
= size_t
'Last then
334 Raise_Exception
(Storage_Error
'Identity, "object too large");
343 -- We first log deallocation call
348 Call_Chain
(Tracebk
'Address, Max_Call_Stack
, Num_Calls
,
350 Timestamp
:= System
.OS_Primitives
.Clock
;
351 fputc
(Character'Pos ('D'), Gmemfile
);
352 fwrite
(Addr
'Address, Address_Size
, 1, Gmemfile
);
353 fwrite
(Timestamp
'Address, Duration'Max_Size_In_Storage_Elements, 1,
355 fwrite
(Num_Calls
'Address, Integer'Max_Size_In_Storage_Elements, 1,
358 for J
in Tracebk
'First .. Tracebk
'First + Num_Calls
- 1 loop
360 Ptr
: System
.Address
:= PC_For
(Tracebk
(J
));
362 fwrite
(Ptr
'Address, Address_Size
, 1, Gmemfile
);
366 -- Now perform actual realloc
368 Result
:= c_realloc
(Ptr
, Size
);
370 -- Log allocation call using the same backtrace
372 fputc
(Character'Pos ('A'), Gmemfile
);
373 fwrite
(Result
'Address, Address_Size
, 1, Gmemfile
);
374 fwrite
(Size
'Address, size_t
'Max_Size_In_Storage_Elements, 1,
376 fwrite
(Timestamp
'Address, Duration'Max_Size_In_Storage_Elements, 1,
378 fwrite
(Num_Calls
'Address, Integer'Max_Size_In_Storage_Elements, 1,
381 for J
in Tracebk
'First .. Tracebk
'First + Num_Calls
- 1 loop
383 Ptr
: System
.Address
:= PC_For
(Tracebk
(J
));
385 fwrite
(Ptr
'Address, Address_Size
, 1, Gmemfile
);
395 if Result
= System
.Null_Address
then
396 Raise_Exception
(Storage_Error
'Identity, "heap exhausted");