1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1997-2001, Ada Core Technologies, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 -- GNATMEM is a utility that tracks memory leaks. It is based on a simple
31 -- - run the application under gdb
32 -- - set a breakpoint on __gnat_malloc and __gnat_free
33 -- - record a reference to the allocated memory on each allocation call
34 -- - suppress this reference on deallocation
35 -- - at the end of the program, remaining references are potential leaks.
36 -- sort them out the best possible way in order to locate the root of
39 -- GNATMEM can also be used with instrumented allocation/deallocation
40 -- routine (see a-raise.c with symbol GMEM defined). This is not supported
41 -- in all platforms, again refer to a-raise.c for further information.
42 -- In this case the application must be relinked with library libgmem.a:
44 -- $ gnatmake my_prog -largs -lgmem
46 -- The running my_prog will produce a file named gmem.out that will be
49 -- In order to help finding out the real leaks, the notion of "allocation
50 -- root" is defined. An allocation root is a specific point in the program
51 -- execution generating memory allocation where data is collected (such as
52 -- number of allocations, quantify of memory allocated, high water mark,
55 with Ada
.Command_Line
; use Ada
.Command_Line
;
56 with Ada
.Text_IO
; use Ada
.Text_IO
;
57 with Ada
.Text_IO
.C_Streams
;
58 with Ada
.Float_Text_IO
;
59 with Ada
.Integer_Text_IO
;
60 with Gnatvsn
; use Gnatvsn
;
61 with GNAT
.Heap_Sort_G
;
63 with GNAT
.HTable
; use GNAT
.HTable
;
64 with Interfaces
.C_Streams
; use Interfaces
.C_Streams
;
65 with System
; use System
;
66 with System
.Storage_Elements
; use System
.Storage_Elements
;
68 with Memroot
; use Memroot
;
72 ------------------------------------------------
73 -- Potentially Target Dependent Subprograms. --
74 ------------------------------------------------
76 function Get_Current_TTY
return String;
77 -- Give the current tty on which the program is run. This is needed to
78 -- separate the output of the debugger from the output of the program.
79 -- The output of this function will be used to call the gdb command "tty"
80 -- in the gdb script in order to get the program output on the current tty
81 -- while the gdb output is redirected and processed by gnatmem.
83 function popen
(File
, Mode
: System
.Address
) return FILEs
;
84 pragma Import
(C
, popen
, "popen");
85 -- Execute the program 'File'. If the mode is "r" the standard output
86 -- of the program is redirected and the FILEs handler of the
87 -- redirection is returned.
89 procedure System_Cmd
(X
: System
.Address
);
90 pragma Import
(C
, System_Cmd
, "system");
91 -- Execute the program "X".
93 subtype Cstring
is String (1 .. Integer'Last);
94 type Cstring_Ptr
is access all Cstring
;
96 function ttyname
(Dec
: Integer) return Cstring_Ptr
;
97 pragma Import
(C
, ttyname
, "__gnat_ttyname");
98 -- Return a null-terminated string containing the current tty
100 Dir_Sep
: constant Character := '/';
102 ------------------------
103 -- Other Declarations --
104 ------------------------
106 type Gdb_Output_Elmt
is (Eof
, Alloc
, Deall
);
107 -- Eof = End of gdb output file
108 -- Alloc = found a ALLOC mark in the gdb output
109 -- Deall = found a DEALL mark in the gdb output
110 Gdb_Output_Format_Error
: exception;
112 function Read_Next
return Gdb_Output_Elmt
;
113 -- Read the output of the debugger till it finds either the end of the
114 -- output, or the 'ALLOC' mark or the 'DEALL' mark. In the second case,
115 -- it sets the Tmp_Size and Tmp_Address global variables, in the
116 -- third case it sets the Tmp_Address variable.
118 procedure Create_Gdb_Script
;
119 -- Create the GDB script and save it in a temporary file
121 function Mem_Image
(X
: Storage_Count
) return String;
122 -- X is a size in storage_element. Returns a value
123 -- in Megabytes, Kiloytes or Bytes as appropriate.
125 procedure Process_Arguments
;
126 -- Read command line arguments;
129 -- Prints out the option help
131 function Gmem_Initialize
(Dumpname
: String) return Boolean;
132 -- Opens the file represented by Dumpname and prepares it for
133 -- work. Returns False if the file does not have the correct format, True
136 procedure Gmem_A2l_Initialize
(Exename
: String);
137 -- Initialises the convert_addresses interface by supplying it with
138 -- the name of the executable file Exename
140 procedure Gmem_Read_Next
(Buf
: out String; Last
: out Natural);
141 -- Reads the next allocation/deallocation entry and its backtrace
142 -- and prepares in the string Buf (up to the position of Last) the
143 -- expression compatible with gnatmem parser:
144 -- Allocation entry produces the expression "ALLOC^[size]^0x[address]^"
145 -- Deallocation entry produces the expression "DEALLOC^0x[address]^"
147 Argc
: constant Integer := Argument_Count
;
148 Gnatmem_Tmp
: aliased constant String := "gnatmem.tmp";
150 Mode_R
: aliased constant String (1 .. 2) := 'r' & ASCII
.NUL
;
151 Mode_W
: aliased constant String (1 .. 3) := "w+" & ASCII
.NUL
;
153 -----------------------------------
154 -- HTable address --> Allocation --
155 -----------------------------------
157 type Allocation
is record
159 Size
: Storage_Count
;
162 type Address_Range
is range 0 .. 4097;
163 function H
(A
: Integer_Address
) return Address_Range
;
164 No_Alloc
: constant Allocation
:= (No_Root_Id
, 0);
166 package Address_HTable
is new GNAT
.HTable
.Simple_HTable
(
167 Header_Num
=> Address_Range
,
168 Element
=> Allocation
,
169 No_Element
=> No_Alloc
,
170 Key
=> Integer_Address
,
174 BT_Depth
: Integer := 1;
177 File_Pos
: Integer := 0;
178 Exec_Pos
: Integer := 0;
179 Target_Pos
: Integer := 0;
180 Run_Gdb
: Boolean := True;
182 Global_Alloc_Size
: Storage_Count
:= 0;
183 Global_High_Water_Mark
: Storage_Count
:= 0;
184 Global_Nb_Alloc
: Integer := 0;
185 Global_Nb_Dealloc
: Integer := 0;
186 Nb_Root
: Integer := 0;
187 Nb_Wrong_Deall
: Integer := 0;
188 Target_Name
: String (1 .. 80);
189 Target_Protocol
: String (1 .. 80);
190 Target_Name_Len
: Integer;
191 Target_Protocol_Len
: Integer;
192 Cross_Case
: Boolean := False;
194 Tmp_Size
: Storage_Count
:= 0;
195 Tmp_Address
: Integer_Address
;
196 Tmp_Alloc
: Allocation
;
197 Quiet_Mode
: Boolean := False;
199 --------------------------------
200 -- GMEM functionality binding --
201 --------------------------------
203 function Gmem_Initialize
(Dumpname
: String) return Boolean is
204 function Initialize
(Dumpname
: System
.Address
) return Boolean;
205 pragma Import
(C
, Initialize
, "__gnat_gmem_initialize");
206 S
: aliased String := Dumpname
& ASCII
.NUL
;
208 return Initialize
(S
'Address);
211 procedure Gmem_A2l_Initialize
(Exename
: String) is
212 procedure A2l_Initialize
(Exename
: System
.Address
);
213 pragma Import
(C
, A2l_Initialize
, "__gnat_gmem_a2l_initialize");
214 S
: aliased String := Exename
& ASCII
.NUL
;
216 A2l_Initialize
(S
'Address);
217 end Gmem_A2l_Initialize
;
219 procedure Gmem_Read_Next
(Buf
: out String; Last
: out Natural) is
220 procedure Read_Next
(buf
: System
.Address
);
221 pragma Import
(C
, Read_Next
, "__gnat_gmem_read_next");
222 function Strlen
(str
: System
.Address
) return Natural;
223 pragma Import
(C
, Strlen
, "strlen");
225 S
: String (1 .. 1000);
227 Read_Next
(S
'Address);
228 Last
:= Strlen
(S
'Address);
229 Buf
(1 .. Last
) := S
(1 .. Last
);
232 ---------------------
233 -- Get_Current_TTY --
234 ---------------------
236 function Get_Current_TTY
return String is
238 stdout
: constant Integer := 1;
239 Max_TTY_Name
: constant Integer := 500;
242 if isatty
(stdout
) /= 1 then
248 for J
in Cstring
'First .. Max_TTY_Name
loop
249 if Res
(J
) = ASCII
.NUL
then
250 return Res
(Cstring
'First .. J
- 1);
255 -- if we fall thru the ttyname result was dubious. Just forget it.
264 function H
(A
: Integer_Address
) return Address_Range
is
266 return Address_Range
(A
mod Integer_Address
(Address_Range
'Last));
269 -----------------------
270 -- Create_Gdb_Script --
271 -----------------------
273 procedure Create_Gdb_Script
is
278 Create
(FD
, Out_File
, Gnatmem_Tmp
);
281 Put_Line
("Cannot create temporary file : " & Gnatmem_Tmp
);
282 GNAT
.OS_Lib
.OS_Exit
(1);
286 TTY
: constant String := Get_Current_TTY
;
288 if TTY
'Length > 0 then
289 Put_Line
(FD
, "tty " & TTY
);
295 Put
(FD
, Target_Protocol
(1 .. Target_Protocol_Len
));
297 Put
(FD
, Argument
(Target_Pos
));
300 Put_Line
(FD
, Argument
(Exec_Pos
));
303 -- In the native case, run the program before setting the
304 -- breakpoints so that gnatmem will also work with shared
307 Put_Line
(FD
, "set lang c");
308 Put_Line
(FD
, "break main");
309 Put_Line
(FD
, "set lang auto");
311 for J
in Exec_Pos
+ 1 .. Argc
loop
313 Put
(FD
, Argument
(J
));
317 -- At this point, gdb knows about __gnat_malloc and __gnat_free
320 -- Make sure that outputing long backtraces do not pause
322 Put_Line
(FD
, "set height 0");
323 Put_Line
(FD
, "set width 0");
326 Put_Line
(FD
, "break __gnat_malloc");
327 Put_Line
(FD
, "command");
328 Put_Line
(FD
, " silent");
329 Put_Line
(FD
, " set lang c");
330 Put_Line
(FD
, " set print address on");
331 Put_Line
(FD
, " finish");
332 Put_Line
(FD
, " set $gm_addr = $");
333 Put_Line
(FD
, " printf ""\n\n""");
334 Put_Line
(FD
, " printf ""ALLOC^0x%x^\n"", $gm_addr");
335 Put_Line
(FD
, " set print address off");
336 Put_Line
(FD
, " set lang auto");
338 Put_Line
(FD
, "break __gnat_malloc");
339 Put_Line
(FD
, "command");
340 Put_Line
(FD
, " silent");
341 Put_Line
(FD
, " set lang c");
342 Put_Line
(FD
, " set $gm_size = size");
343 Put_Line
(FD
, " set print address on");
344 Put_Line
(FD
, " finish");
345 Put_Line
(FD
, " set $gm_addr = $");
346 Put_Line
(FD
, " printf ""\n\n""");
347 Put_Line
(FD
, " printf ""ALLOC^%d^0x%x^\n"", $gm_size, $gm_addr");
348 Put_Line
(FD
, " set print address off");
349 Put_Line
(FD
, " set lang auto");
352 Put
(FD
, " backtrace");
354 if BT_Depth
/= 0 then
355 Put
(FD
, Integer'Image (BT_Depth
));
360 Put_Line
(FD
, " printf ""\n\n""");
361 Put_Line
(FD
, " continue");
362 Put_Line
(FD
, "end");
365 Put_Line
(FD
, "break __gnat_free");
366 Put_Line
(FD
, "command");
367 Put_Line
(FD
, " silent");
368 Put_Line
(FD
, " set print address on");
369 Put_Line
(FD
, " printf ""\n\n""");
370 Put_Line
(FD
, " printf ""DEALL^0x%x^\n"", ptr");
371 Put_Line
(FD
, " set print address off");
372 Put_Line
(FD
, " finish");
374 Put
(FD
, " backtrace");
376 if BT_Depth
/= 0 then
377 Put
(FD
, Integer'Image (BT_Depth
));
382 Put_Line
(FD
, " printf ""\n\n""");
383 Put_Line
(FD
, " continue");
384 Put_Line
(FD
, "end");
391 Put_Line
(FD
, Argument
(Exec_Pos
));
393 if Target_Protocol
(1 .. Target_Protocol_Len
) = "wtx" then
395 Put_Line
(FD
, Argument
(Exec_Pos
));
398 Put_Line
(FD
, "continue");
402 end Create_Gdb_Script
;
408 function Mem_Image
(X
: Storage_Count
) return String is
409 Ks
: constant Storage_Count
:= X
/ 1024;
410 Megs
: constant Storage_Count
:= Ks
/ 1024;
411 Buff
: String (1 .. 7);
415 Ada
.Float_Text_IO
.Put
(Buff
, Float (X
) / 1024.0 / 1024.0, 2, 0);
416 return Buff
& " Megabytes";
419 Ada
.Float_Text_IO
.Put
(Buff
, Float (X
) / 1024.0, 2, 0);
420 return Buff
& " Kilobytes";
423 Ada
.Integer_Text_IO
.Put
(Buff
(1 .. 4), Integer (X
));
424 return Buff
(1 .. 4) & " Bytes";
436 Put
(Gnat_Version_String
);
437 Put_Line
(" Copyright 1997-2000 Free Software Foundation, Inc.");
441 Put_Line
(Command_Name
442 & " [-q] [n] [-o file] target entry_point ...");
443 Put_Line
(Command_Name
& " [-q] [n] [-i file]");
446 Put_Line
("GDB mode");
447 Put_Line
(" " & Command_Name
448 & " [-q] [n] [-o file] program arg1 arg2 ...");
449 Put_Line
(" " & Command_Name
450 & " [-q] [n] [-i file]");
452 Put_Line
("GMEM mode");
453 Put_Line
(" " & Command_Name
454 & " [-q] [n] -i gmem.out program arg1 arg2 ...");
458 Put_Line
(" -q quiet, minimum output");
459 Put_Line
(" n number of frames for allocation root backtraces");
460 Put_Line
(" default is 1.");
461 Put_Line
(" -o file save gdb output in 'file' and process data");
462 Put_Line
(" post mortem. also keep the gdb script around");
463 Put_Line
(" -i file don't run gdb output. Do only post mortem");
464 Put_Line
(" processing from file");
465 GNAT
.OS_Lib
.OS_Exit
(1);
468 -----------------------
469 -- Process_Arguments --
470 -----------------------
472 procedure Process_Arguments
is
475 procedure Check_File
(Arg_Pos
: Integer; For_Creat
: Boolean := False);
476 -- Check that Argument (Arg_Pos) is an existing file if For_Creat is
477 -- false or if it is possible to create it if For_Creat is true
479 procedure Check_File
(Arg_Pos
: Integer; For_Creat
: Boolean := False) is
480 Name
: aliased constant String := Argument
(Arg_Pos
) & ASCII
.NUL
;
485 FD
:= fopen
(Name
'Address, Mode_W
'Address);
487 FD
:= fopen
(Name
'Address, Mode_R
'Address);
490 if FD
= NULL_Stream
then
493 Put_Line
("Cannot create file : " & Argument
(Arg_Pos
));
495 Put_Line
("Cannot locate file : " & Argument
(Arg_Pos
));
504 -- Start of processing for Process_Arguments
508 -- Is it a cross version?
511 Std_Name
: constant String := "gnatmem";
512 Name
: constant String := Command_Name
;
513 End_Pref
: constant Integer := Name
'Last - Std_Name
'Length;
516 if Name
'Length > Std_Name
'Length + 9
518 Name
(End_Pref
+ 1 .. Name
'Last) = Std_Name
520 Name
(End_Pref
- 8 .. End_Pref
) = "-vxworks-"
524 Target_Name_Len
:= End_Pref
- 1;
525 for J
in reverse Name
'First .. End_Pref
- 1 loop
526 if Name
(J
) = Dir_Sep
then
527 Target_Name_Len
:= Target_Name_Len
- J
;
532 Target_Name
(1 .. Target_Name_Len
)
533 := Name
(End_Pref
- Target_Name_Len
.. End_Pref
- 1);
535 if Target_Name
(1 .. 5) = "alpha" then
536 Target_Protocol
(1 .. 7) := "vxworks";
537 Target_Protocol_Len
:= 7;
539 Target_Protocol
(1 .. 3) := "wtx";
540 Target_Protocol_Len
:= 3;
553 if Argument
(Arg
) = "-q" then
563 -- Deal with back trace depth
565 if Argument
(Arg
) (1) in '0' .. '9' then
567 BT_Depth
:= Integer'Value (Argument
(Arg
));
580 -- Deal with "-o file" or "-i file"
582 while Arg
<= Argc
and then Argument
(Arg
) (1) = '-' loop
589 case Argument
(Arg
- 1) (2) is
591 Check_File
(Arg
, For_Creat
=> True);
598 if Gmem_Initialize
(Argument
(Arg
)) then
603 Put_Line
("Unknown option : " & Argument
(Arg
));
609 if Argc
< Arg
and then Run_Gdb
then
614 -- In the cross case, we first get the target
620 if Argc
< Arg
and then Run_Gdb
then
625 -- Now all the following arguments are to be passed to gdb
629 Check_File
(Exec_Pos
);
636 Check_File
(Exec_Pos
);
637 Gmem_A2l_Initialize
(Argument
(Exec_Pos
));
640 -- ... in other cases further arguments are disallowed
642 elsif Arg
<= Argc
then
645 end Process_Arguments
;
651 function Read_Next
return Gdb_Output_Elmt
is
652 Max_Line
: constant Integer := 100;
653 Line
: String (1 .. Max_Line
);
656 Curs1
, Curs2
: Integer;
657 Separator
: constant Character := '^';
659 function Next_Separator
return Integer;
660 -- Return the index of the next separator after Curs1 in Line
662 function Next_Separator
return Integer is
663 Curs
: Integer := Curs1
;
668 raise Gdb_Output_Format_Error
;
670 elsif Line
(Curs
) = Separator
then
678 -- Start of processing for Read_Next
685 Gmem_Read_Next
(Line
, Last
);
687 Get_Line
(FT
, Line
, Last
);
690 if Line
(1 .. 14) = "Program exited" then
693 elsif Line
(1 .. 5) = "ALLOC" then
694 -- ALLOC ^ <size> ^0x <addr> ^
699 Curs2
:= Next_Separator
- 1;
701 if not Quiet_Mode
then
702 Tmp_Size
:= Storage_Count
'Value (Line
(Curs1
.. Curs2
));
705 -- Read the address, skip "^0x"
708 Curs2
:= Next_Separator
- 1;
709 Tmp_Address
:= Integer_Address
'Value (
710 "16#" & Line
(Curs1
.. Curs2
) & "#");
713 elsif Line
(1 .. 5) = "DEALL" then
714 -- DEALL ^ 0x <addr> ^
716 -- Read the address, skip "^0x"
719 Curs2
:= Next_Separator
- 1;
720 Tmp_Address
:= Integer_Address
'Value (
721 "16#" & Line
(Curs1
.. Curs2
) & "#");
728 Put_Line
("### incorrect user program termination detected.");
729 Put_Line
(" following data may not be meaningful");
734 -- Start of processing for Gnatmem
743 -- Now we start the gdb session using the following syntax
745 -- gdb --nx --nw -batch -x gnatmem.tmp
747 -- If there is a -o option we redirect the gdb output in the specified
748 -- file, otherwise we just read directly from a pipe.
750 if File_Pos
/= 0 then
752 Name
: aliased String := Argument
(File_Pos
) & ASCII
.NUL
;
758 Cmd
: aliased String := Target_Name
(1 .. Target_Name_Len
)
759 & "-gdb --nx --nw -batch -x " & Gnatmem_Tmp
& " > "
762 System_Cmd
(Cmd
'Address);
768 := "gdb --nx --nw " & Argument
(Exec_Pos
)
769 & " -batch -x " & Gnatmem_Tmp
& " > "
772 System_Cmd
(Cmd
'Address);
777 if not Gmem_Mode
then
778 FD
:= fopen
(Name
'Address, Mode_R
'Address);
785 Cmd
: aliased String := Target_Name
(1 .. Target_Name_Len
)
786 & "-gdb --nx --nw -batch -x " & Gnatmem_Tmp
& ASCII
.NUL
;
788 FD
:= popen
(Cmd
'Address, Mode_R
'Address);
792 Cmd
: aliased String := "gdb --nx --nw " & Argument
(Exec_Pos
)
793 & " -batch -x " & Gnatmem_Tmp
& ASCII
.NUL
;
796 FD
:= popen
(Cmd
'Address, Mode_R
'Address);
801 -- Open the FD file as a regular Text_IO file
803 if not Gmem_Mode
then
804 Ada
.Text_IO
.C_Streams
.Open
(FT
, In_File
, FD
);
807 -- Main loop analysing the data generated by the debugger
808 -- for each allocation, the backtrace is kept and stored in a htable
809 -- whose entry is the address. Fore ach deallocation, we look for the
810 -- corresponding allocation and cancel it.
819 -- Update global counters if the allocated size is meaningful
822 Tmp_Alloc
.Root
:= Read_BT
(BT_Depth
, FT
);
823 if Nb_Alloc
(Tmp_Alloc
.Root
) = 0 then
824 Nb_Root
:= Nb_Root
+ 1;
826 Set_Nb_Alloc
(Tmp_Alloc
.Root
, Nb_Alloc
(Tmp_Alloc
.Root
) + 1);
827 Address_HTable
.Set
(Tmp_Address
, Tmp_Alloc
);
829 elsif Tmp_Size
> 0 then
831 Global_Alloc_Size
:= Global_Alloc_Size
+ Tmp_Size
;
832 Global_Nb_Alloc
:= Global_Nb_Alloc
+ 1;
834 if Global_High_Water_Mark
< Global_Alloc_Size
then
835 Global_High_Water_Mark
:= Global_Alloc_Size
;
838 -- Read the corresponding back trace
840 Tmp_Alloc
.Root
:= Read_BT
(BT_Depth
, FT
);
842 -- Update the number of allocation root if this is a new one
844 if Nb_Alloc
(Tmp_Alloc
.Root
) = 0 then
845 Nb_Root
:= Nb_Root
+ 1;
848 -- Update allocation root specific counters
850 Set_Alloc_Size
(Tmp_Alloc
.Root
,
851 Alloc_Size
(Tmp_Alloc
.Root
) + Tmp_Size
);
853 Set_Nb_Alloc
(Tmp_Alloc
.Root
, Nb_Alloc
(Tmp_Alloc
.Root
) + 1);
855 if High_Water_Mark
(Tmp_Alloc
.Root
)
856 < Alloc_Size
(Tmp_Alloc
.Root
)
858 Set_High_Water_Mark
(Tmp_Alloc
.Root
,
859 Alloc_Size
(Tmp_Alloc
.Root
));
862 -- Associate this allocation root to the allocated address
864 Tmp_Alloc
.Size
:= Tmp_Size
;
865 Address_HTable
.Set
(Tmp_Address
, Tmp_Alloc
);
867 -- non meaninful output, just consumes the backtrace
870 Tmp_Alloc
.Root
:= Read_BT
(BT_Depth
, FT
);
875 -- Get the corresponding Dealloc_Size and Root
877 Tmp_Alloc
:= Address_HTable
.Get
(Tmp_Address
);
879 if Tmp_Alloc
.Root
= No_Root_Id
then
881 -- There was no prior allocation at this address, something is
882 -- very wrong. Mark this allocation root as problematic a
884 Tmp_Alloc
.Root
:= Read_BT
(BT_Depth
, FT
);
886 if Nb_Alloc
(Tmp_Alloc
.Root
) = 0 then
887 Set_Nb_Alloc
(Tmp_Alloc
.Root
, Nb_Alloc
(Tmp_Alloc
.Root
) - 1);
888 Nb_Wrong_Deall
:= Nb_Wrong_Deall
+ 1;
892 -- Update global counters
894 if not Quiet_Mode
then
895 Global_Alloc_Size
:= Global_Alloc_Size
- Tmp_Alloc
.Size
;
897 Global_Nb_Dealloc
:= Global_Nb_Dealloc
+ 1;
899 -- Update allocation root specific counters
901 if not Quiet_Mode
then
902 Set_Alloc_Size
(Tmp_Alloc
.Root
,
903 Alloc_Size
(Tmp_Alloc
.Root
) - Tmp_Alloc
.Size
);
905 Set_Nb_Alloc
(Tmp_Alloc
.Root
, Nb_Alloc
(Tmp_Alloc
.Root
) - 1);
907 -- update the number of allocation root if this one disappear
909 if Nb_Alloc
(Tmp_Alloc
.Root
) = 0 then
910 Nb_Root
:= Nb_Root
- 1;
913 -- De-associate the deallocated address
915 Address_HTable
.Remove
(Tmp_Address
);
920 -- We can get rid of the temp file now
922 if Run_Gdb
and then File_Pos
= 0 then
926 X
:= unlink
(Gnatmem_Tmp
'Address);
930 -- Print out general information about overall allocation
932 if not Quiet_Mode
then
933 Put_Line
("Global information");
934 Put_Line
("------------------");
936 Put
(" Total number of allocations :");
937 Ada
.Integer_Text_IO
.Put
(Global_Nb_Alloc
, 4);
940 Put
(" Total number of deallocations :");
941 Ada
.Integer_Text_IO
.Put
(Global_Nb_Dealloc
, 4);
944 Put_Line
(" Final Water Mark (non freed mem) :"
945 & Mem_Image
(Global_Alloc_Size
));
946 Put_Line
(" High Water Mark :"
947 & Mem_Image
(Global_High_Water_Mark
));
951 -- Print out the back traces corresponding to potential leaks in order
952 -- greatest number of non-deallocated allocations
954 Print_Back_Traces
: declare
955 type Root_Array
is array (Natural range <>) of Root_Id
;
956 Leaks
: Root_Array
(0 .. Nb_Root
);
957 Leak_Index
: Natural := 0;
959 Bogus_Dealls
: Root_Array
(1 .. Nb_Wrong_Deall
);
960 Deall_Index
: Natural := 0;
962 procedure Move
(From
: Natural; To
: Natural);
963 function Lt
(Op1
, Op2
: Natural) return Boolean;
964 package Root_Sort
is new GNAT
.Heap_Sort_G
(Move
, Lt
);
966 procedure Move
(From
: Natural; To
: Natural) is
968 Leaks
(To
) := Leaks
(From
);
971 function Lt
(Op1
, Op2
: Natural) return Boolean is
973 if Nb_Alloc
(Leaks
(Op1
)) > Nb_Alloc
(Leaks
(Op2
)) then
975 elsif Nb_Alloc
(Leaks
(Op1
)) = Nb_Alloc
(Leaks
(Op2
)) then
976 return Alloc_Size
(Leaks
(Op1
)) > Alloc_Size
(Leaks
(Op2
));
982 -- Start of processing for Print_Back_Traces
985 -- Transfer all the relevant Roots in the Leaks and a
986 -- Bogus_Deall arrays
988 Tmp_Alloc
.Root
:= Get_First
;
989 while Tmp_Alloc
.Root
/= No_Root_Id
loop
990 if Nb_Alloc
(Tmp_Alloc
.Root
) = 0 then
993 elsif Nb_Alloc
(Tmp_Alloc
.Root
) < 0 then
994 Deall_Index
:= Deall_Index
+ 1;
995 Bogus_Dealls
(Deall_Index
) := Tmp_Alloc
.Root
;
998 Leak_Index
:= Leak_Index
+ 1;
999 Leaks
(Leak_Index
) := Tmp_Alloc
.Root
;
1002 Tmp_Alloc
.Root
:= Get_Next
;
1005 -- Print out wrong deallocations
1007 if Nb_Wrong_Deall
> 0 then
1008 Put_Line
("Releasing deallocated memory at :");
1009 if not Quiet_Mode
then
1010 Put_Line
("--------------------------------");
1013 for J
in 1 .. Bogus_Dealls
'Last loop
1014 Print_BT
(Bogus_Dealls
(J
));
1019 -- Print out all allocation Leaks
1023 -- Sort the Leaks so that potentially important leaks appear first
1025 Root_Sort
.Sort
(Nb_Root
);
1027 for J
in 1 .. Leaks
'Last loop
1029 if Nb_Alloc
(Leaks
(J
)) = 1 then
1030 Put_Line
(Integer'Image (Nb_Alloc
(Leaks
(J
)))
1033 Put_Line
(Integer'Image (Nb_Alloc
(Leaks
(J
)))
1037 Put_Line
("Allocation Root #" & Integer'Image (J
));
1038 Put_Line
("-------------------");
1040 Put
(" Number of non freed allocations :");
1041 Ada
.Integer_Text_IO
.Put
(Nb_Alloc
(Leaks
(J
)), 4);
1044 Put_Line
(" Final Water Mark (non freed mem) :"
1045 & Mem_Image
(Alloc_Size
(Leaks
(J
))));
1047 Put_Line
(" High Water Mark :"
1048 & Mem_Image
(High_Water_Mark
(Leaks
(J
))));
1050 Put_Line
(" Backtrace :");
1052 Print_BT
(Leaks
(J
));
1056 end Print_Back_Traces
;