1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1997-2002, Ada Core Technologies, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 ------------------------------------------------------------------------------
28 -- GNATMEM is a utility that tracks memory leaks. It is based on a simple
30 -- - run the application under gdb
31 -- - set a breakpoint on __gnat_malloc and __gnat_free
32 -- - record a reference to the allocated memory on each allocation call
33 -- - suppress this reference on deallocation
34 -- - at the end of the program, remaining references are potential leaks.
35 -- sort them out the best possible way in order to locate the root of
38 -- GNATMEM can also be used with instrumented allocation/deallocation
39 -- routine (see a-raise.c with symbol GMEM defined). This is not supported
40 -- in all platforms, again refer to a-raise.c for further information.
41 -- In this case the application must be relinked with library libgmem.a:
43 -- $ gnatmake my_prog -largs -lgmem
45 -- The running my_prog will produce a file named gmem.out that will be
48 -- In order to help finding out the real leaks, the notion of "allocation
49 -- root" is defined. An allocation root is a specific point in the program
50 -- execution generating memory allocation where data is collected (such as
51 -- number of allocations, quantify of memory allocated, high water mark,
54 with Ada
.Command_Line
; use Ada
.Command_Line
;
55 with Ada
.Text_IO
; use Ada
.Text_IO
;
56 with Ada
.Text_IO
.C_Streams
;
57 with Ada
.Float_Text_IO
;
58 with Ada
.Integer_Text_IO
;
59 with Gnatvsn
; use Gnatvsn
;
60 with GNAT
.Heap_Sort_G
;
62 with GNAT
.HTable
; use GNAT
.HTable
;
63 with Interfaces
.C_Streams
; use Interfaces
.C_Streams
;
64 with System
; use System
;
65 with System
.Storage_Elements
; use System
.Storage_Elements
;
67 with Memroot
; use Memroot
;
71 ------------------------------------------------
72 -- Potentially Target Dependent Subprograms. --
73 ------------------------------------------------
75 function Get_Current_TTY
return String;
76 -- Give the current tty on which the program is run. This is needed to
77 -- separate the output of the debugger from the output of the program.
78 -- The output of this function will be used to call the gdb command "tty"
79 -- in the gdb script in order to get the program output on the current tty
80 -- while the gdb output is redirected and processed by gnatmem.
82 function popen
(File
, Mode
: System
.Address
) return FILEs
;
83 pragma Import
(C
, popen
, "popen");
84 -- Execute the program 'File'. If the mode is "r" the standard output
85 -- of the program is redirected and the FILEs handler of the
86 -- redirection is returned.
88 procedure System_Cmd
(X
: System
.Address
);
89 pragma Import
(C
, System_Cmd
, "system");
90 -- Execute the program "X".
92 subtype Cstring
is String (1 .. Integer'Last);
93 type Cstring_Ptr
is access all Cstring
;
95 function ttyname
(Dec
: Integer) return Cstring_Ptr
;
96 pragma Import
(C
, ttyname
, "__gnat_ttyname");
97 -- Return a null-terminated string containing the current tty
99 Dir_Sep
: constant Character := '/';
101 ------------------------
102 -- Other Declarations --
103 ------------------------
105 type Gdb_Output_Elmt
is (Eof
, Alloc
, Deall
);
106 -- Eof = End of gdb output file
107 -- Alloc = found a ALLOC mark in the gdb output
108 -- Deall = found a DEALL mark in the gdb output
109 Gdb_Output_Format_Error
: exception;
111 function Read_Next
return Gdb_Output_Elmt
;
112 -- Read the output of the debugger till it finds either the end of the
113 -- output, or the 'ALLOC' mark or the 'DEALL' mark. In the second case,
114 -- it sets the Tmp_Size and Tmp_Address global variables, in the
115 -- third case it sets the Tmp_Address variable.
117 procedure Create_Gdb_Script
;
118 -- Create the GDB script and save it in a temporary file
120 function Mem_Image
(X
: Storage_Count
) return String;
121 -- X is a size in storage_element. Returns a value
122 -- in Megabytes, Kiloytes or Bytes as appropriate.
124 procedure Process_Arguments
;
125 -- Read command line arguments;
128 -- Prints out the option help
130 function Gmem_Initialize
(Dumpname
: String) return Boolean;
131 -- Opens the file represented by Dumpname and prepares it for
132 -- work. Returns False if the file does not have the correct format, True
135 procedure Gmem_A2l_Initialize
(Exename
: String);
136 -- Initialises the convert_addresses interface by supplying it with
137 -- the name of the executable file Exename
139 procedure Gmem_Read_Next
(Buf
: out String; Last
: out Natural);
140 -- Reads the next allocation/deallocation entry and its backtrace
141 -- and prepares in the string Buf (up to the position of Last) the
142 -- expression compatible with gnatmem parser:
143 -- Allocation entry produces the expression "ALLOC^[size]^0x[address]^"
144 -- Deallocation entry produces the expression "DEALLOC^0x[address]^"
146 Argc
: constant Integer := Argument_Count
;
147 Gnatmem_Tmp
: aliased constant String := "gnatmem.tmp";
149 Mode_R
: aliased constant String (1 .. 2) := 'r' & ASCII
.NUL
;
150 Mode_W
: aliased constant String (1 .. 3) := "w+" & ASCII
.NUL
;
152 -----------------------------------
153 -- HTable address --> Allocation --
154 -----------------------------------
156 type Allocation
is record
158 Size
: Storage_Count
;
161 type Address_Range
is range 0 .. 4097;
162 function H
(A
: Integer_Address
) return Address_Range
;
163 No_Alloc
: constant Allocation
:= (No_Root_Id
, 0);
165 package Address_HTable
is new GNAT
.HTable
.Simple_HTable
(
166 Header_Num
=> Address_Range
,
167 Element
=> Allocation
,
168 No_Element
=> No_Alloc
,
169 Key
=> Integer_Address
,
173 BT_Depth
: Integer := 1;
176 File_Pos
: Integer := 0;
177 Exec_Pos
: Integer := 0;
178 Target_Pos
: Integer := 0;
179 Run_Gdb
: Boolean := True;
181 Global_Alloc_Size
: Storage_Count
:= 0;
182 Global_High_Water_Mark
: Storage_Count
:= 0;
183 Global_Nb_Alloc
: Integer := 0;
184 Global_Nb_Dealloc
: Integer := 0;
185 Nb_Root
: Integer := 0;
186 Nb_Wrong_Deall
: Integer := 0;
187 Target_Name
: String (1 .. 80);
188 Target_Protocol
: String (1 .. 80);
189 Target_Name_Len
: Integer;
190 Target_Protocol_Len
: Integer;
191 Cross_Case
: Boolean := False;
193 Tmp_Size
: Storage_Count
:= 0;
194 Tmp_Address
: Integer_Address
;
195 Tmp_Alloc
: Allocation
;
196 Quiet_Mode
: Boolean := False;
198 --------------------------------
199 -- GMEM functionality binding --
200 --------------------------------
202 function Gmem_Initialize
(Dumpname
: String) return Boolean is
203 function Initialize
(Dumpname
: System
.Address
) return Boolean;
204 pragma Import
(C
, Initialize
, "__gnat_gmem_initialize");
205 S
: aliased String := Dumpname
& ASCII
.NUL
;
207 return Initialize
(S
'Address);
210 procedure Gmem_A2l_Initialize
(Exename
: String) is
211 procedure A2l_Initialize
(Exename
: System
.Address
);
212 pragma Import
(C
, A2l_Initialize
, "__gnat_gmem_a2l_initialize");
213 S
: aliased String := Exename
& ASCII
.NUL
;
215 A2l_Initialize
(S
'Address);
216 end Gmem_A2l_Initialize
;
218 procedure Gmem_Read_Next
(Buf
: out String; Last
: out Natural) is
219 procedure Read_Next
(buf
: System
.Address
);
220 pragma Import
(C
, Read_Next
, "__gnat_gmem_read_next");
221 function Strlen
(str
: System
.Address
) return Natural;
222 pragma Import
(C
, Strlen
, "strlen");
224 S
: String (1 .. 1000);
226 Read_Next
(S
'Address);
227 Last
:= Strlen
(S
'Address);
228 Buf
(1 .. Last
) := S
(1 .. Last
);
231 ---------------------
232 -- Get_Current_TTY --
233 ---------------------
235 function Get_Current_TTY
return String is
237 stdout
: constant Integer := 1;
238 Max_TTY_Name
: constant Integer := 500;
241 if isatty
(stdout
) /= 1 then
247 for J
in Cstring
'First .. Max_TTY_Name
loop
248 if Res
(J
) = ASCII
.NUL
then
249 return Res
(Cstring
'First .. J
- 1);
254 -- if we fall thru the ttyname result was dubious. Just forget it.
263 function H
(A
: Integer_Address
) return Address_Range
is
265 return Address_Range
(A
mod Integer_Address
(Address_Range
'Last));
268 -----------------------
269 -- Create_Gdb_Script --
270 -----------------------
272 procedure Create_Gdb_Script
is
277 Create
(FD
, Out_File
, Gnatmem_Tmp
);
280 Put_Line
("Cannot create temporary file : " & Gnatmem_Tmp
);
281 GNAT
.OS_Lib
.OS_Exit
(1);
285 TTY
: constant String := Get_Current_TTY
;
287 if TTY
'Length > 0 then
288 Put_Line
(FD
, "tty " & TTY
);
294 Put
(FD
, Target_Protocol
(1 .. Target_Protocol_Len
));
296 Put
(FD
, Argument
(Target_Pos
));
299 Put_Line
(FD
, Argument
(Exec_Pos
));
302 -- In the native case, run the program before setting the
303 -- breakpoints so that gnatmem will also work with shared
306 Put_Line
(FD
, "set lang c");
307 Put_Line
(FD
, "break main");
308 Put_Line
(FD
, "set lang auto");
310 for J
in Exec_Pos
+ 1 .. Argc
loop
312 Put
(FD
, Argument
(J
));
316 -- At this point, gdb knows about __gnat_malloc and __gnat_free
319 -- Make sure that outputing long backtraces do not pause
321 Put_Line
(FD
, "set height 0");
322 Put_Line
(FD
, "set width 0");
325 Put_Line
(FD
, "break __gnat_malloc");
326 Put_Line
(FD
, "command");
327 Put_Line
(FD
, " silent");
328 Put_Line
(FD
, " set lang c");
329 Put_Line
(FD
, " set print address on");
330 Put_Line
(FD
, " up");
331 Put_Line
(FD
, " set $gm_addr = $pc");
332 Put_Line
(FD
, " printf ""\n\n""");
333 Put_Line
(FD
, " printf ""ALLOC^0x%x^\n"", $gm_addr");
334 Put_Line
(FD
, " set print address off");
335 Put_Line
(FD
, " set lang auto");
337 Put_Line
(FD
, "break __gnat_malloc");
338 Put_Line
(FD
, "command");
339 Put_Line
(FD
, " silent");
340 Put_Line
(FD
, " set lang c");
341 Put_Line
(FD
, " set $gm_size = size");
342 Put_Line
(FD
, " set print address on");
343 Put_Line
(FD
, " up");
344 Put_Line
(FD
, " set $gm_addr = $pc");
345 Put_Line
(FD
, " printf ""\n\n""");
346 Put_Line
(FD
, " printf ""ALLOC^%d^0x%x^\n"", $gm_size, $gm_addr");
347 Put_Line
(FD
, " set print address off");
348 Put_Line
(FD
, " set lang auto");
351 Put
(FD
, " backtrace");
353 if BT_Depth
/= 0 then
354 Put
(FD
, Integer'Image (BT_Depth
+ 1));
359 Put_Line
(FD
, " printf ""\n\n""");
360 Put_Line
(FD
, " continue");
361 Put_Line
(FD
, "end");
364 Put_Line
(FD
, "break __gnat_free");
365 Put_Line
(FD
, "command");
366 Put_Line
(FD
, " silent");
367 Put_Line
(FD
, " set print address on");
368 Put_Line
(FD
, " printf ""\n\n""");
369 Put_Line
(FD
, " printf ""DEALL^0x%x^\n"", ptr");
370 Put_Line
(FD
, " set print address off");
371 Put_Line
(FD
, " up");
373 Put
(FD
, " backtrace");
375 if BT_Depth
/= 0 then
376 Put
(FD
, Integer'Image (BT_Depth
+ 1));
381 Put_Line
(FD
, " printf ""\n\n""");
382 Put_Line
(FD
, " continue");
383 Put_Line
(FD
, "end");
390 Put_Line
(FD
, Argument
(Exec_Pos
));
392 if Target_Protocol
(1 .. Target_Protocol_Len
) = "wtx" then
394 Put_Line
(FD
, Argument
(Exec_Pos
));
397 Put_Line
(FD
, "continue");
401 end Create_Gdb_Script
;
407 function Mem_Image
(X
: Storage_Count
) return String is
408 Ks
: constant Storage_Count
:= X
/ 1024;
409 Megs
: constant Storage_Count
:= Ks
/ 1024;
410 Buff
: String (1 .. 7);
414 Ada
.Float_Text_IO
.Put
(Buff
, Float (X
) / 1024.0 / 1024.0, 2, 0);
415 return Buff
& " Megabytes";
418 Ada
.Float_Text_IO
.Put
(Buff
, Float (X
) / 1024.0, 2, 0);
419 return Buff
& " Kilobytes";
422 Ada
.Integer_Text_IO
.Put
(Buff
(1 .. 4), Integer (X
));
423 return Buff
(1 .. 4) & " Bytes";
435 Put
(Gnat_Version_String
);
436 Put_Line
(" Copyright 1997-2002 Free Software Foundation, Inc.");
440 Put_Line
(Command_Name
441 & " [-q] [n] [-o file] target entry_point ...");
442 Put_Line
(Command_Name
& " [-q] [n] [-i file]");
445 Put_Line
("GDB mode");
446 Put_Line
(" " & Command_Name
447 & " [-q] [n] [-o file] program arg1 arg2 ...");
448 Put_Line
(" " & Command_Name
449 & " [-q] [n] [-i file]");
451 Put_Line
("GMEM mode");
452 Put_Line
(" " & Command_Name
453 & " [-q] [n] -i gmem.out program arg1 arg2 ...");
457 Put_Line
(" -q quiet, minimum output");
458 Put_Line
(" n number of frames for allocation root backtraces");
459 Put_Line
(" default is 1.");
460 Put_Line
(" -o file save gdb output in 'file' and process data");
461 Put_Line
(" post mortem. also keep the gdb script around");
462 Put_Line
(" -i file don't run gdb output. Do only post mortem");
463 Put_Line
(" processing from file");
464 GNAT
.OS_Lib
.OS_Exit
(1);
467 -----------------------
468 -- Process_Arguments --
469 -----------------------
471 procedure Process_Arguments
is
474 procedure Check_File
(Arg_Pos
: Integer; For_Creat
: Boolean := False);
475 -- Check that Argument (Arg_Pos) is an existing file if For_Creat is
476 -- false or if it is possible to create it if For_Creat is true
478 procedure Check_File
(Arg_Pos
: Integer; For_Creat
: Boolean := False) is
479 Name
: aliased constant String := Argument
(Arg_Pos
) & ASCII
.NUL
;
484 FD
:= fopen
(Name
'Address, Mode_W
'Address);
486 FD
:= fopen
(Name
'Address, Mode_R
'Address);
489 if FD
= NULL_Stream
then
492 Put_Line
("Cannot create file : " & Argument
(Arg_Pos
));
494 Put_Line
("Cannot locate file : " & Argument
(Arg_Pos
));
503 -- Start of processing for Process_Arguments
507 -- Is it a cross version?
510 Std_Name
: constant String := "gnatmem";
511 Name
: constant String := Command_Name
;
512 End_Pref
: constant Integer := Name
'Last - Std_Name
'Length;
515 if Name
'Length > Std_Name
'Length + 9
517 Name
(End_Pref
+ 1 .. Name
'Last) = Std_Name
519 Name
(End_Pref
- 8 .. End_Pref
) = "-vxworks-"
523 Target_Name_Len
:= End_Pref
- 1;
524 for J
in reverse Name
'First .. End_Pref
- 1 loop
525 if Name
(J
) = Dir_Sep
then
526 Target_Name_Len
:= Target_Name_Len
- J
;
531 Target_Name
(1 .. Target_Name_Len
)
532 := Name
(End_Pref
- Target_Name_Len
.. End_Pref
- 1);
534 if Target_Name
(1 .. 5) = "alpha" then
535 Target_Protocol
(1 .. 7) := "vxworks";
536 Target_Protocol_Len
:= 7;
538 Target_Protocol
(1 .. 3) := "wtx";
539 Target_Protocol_Len
:= 3;
552 if Argument
(Arg
) = "-q" then
562 -- Deal with back trace depth
564 if Argument
(Arg
) (1) in '0' .. '9' then
566 BT_Depth
:= Integer'Value (Argument
(Arg
));
579 -- Deal with "-o file" or "-i file"
581 while Arg
<= Argc
and then Argument
(Arg
) (1) = '-' loop
588 case Argument
(Arg
- 1) (2) is
590 Check_File
(Arg
, For_Creat
=> True);
597 if Gmem_Initialize
(Argument
(Arg
)) then
602 Put_Line
("Unknown option : " & Argument
(Arg
));
608 if Argc
< Arg
and then Run_Gdb
then
613 -- In the cross case, we first get the target
619 if Argc
< Arg
and then Run_Gdb
then
624 -- Now all the following arguments are to be passed to gdb
628 Check_File
(Exec_Pos
);
635 Check_File
(Exec_Pos
);
636 Gmem_A2l_Initialize
(Argument
(Exec_Pos
));
639 -- ... in other cases further arguments are disallowed
641 elsif Arg
<= Argc
then
644 end Process_Arguments
;
650 function Read_Next
return Gdb_Output_Elmt
is
651 Max_Line
: constant Integer := 100;
652 Line
: String (1 .. Max_Line
);
655 Curs1
, Curs2
: Integer;
656 Separator
: constant Character := '^';
658 function Next_Separator
return Integer;
659 -- Return the index of the next separator after Curs1 in Line
661 function Next_Separator
return Integer is
662 Curs
: Integer := Curs1
;
667 raise Gdb_Output_Format_Error
;
669 elsif Line
(Curs
) = Separator
then
677 -- Start of processing for Read_Next
684 Gmem_Read_Next
(Line
, Last
);
686 Get_Line
(FT
, Line
, Last
);
689 if Line
(1 .. 14) = "Program exited" then
692 elsif Line
(1 .. 5) = "ALLOC" then
693 -- ALLOC ^ <size> ^0x <addr> ^
698 Curs2
:= Next_Separator
- 1;
700 if not Quiet_Mode
then
701 Tmp_Size
:= Storage_Count
'Value (Line
(Curs1
.. Curs2
));
704 -- Read the address, skip "^0x"
707 Curs2
:= Next_Separator
- 1;
708 Tmp_Address
:= Integer_Address
'Value (
709 "16#" & Line
(Curs1
.. Curs2
) & "#");
712 elsif Line
(1 .. 5) = "DEALL" then
713 -- DEALL ^ 0x <addr> ^
715 -- Read the address, skip "^0x"
718 Curs2
:= Next_Separator
- 1;
719 Tmp_Address
:= Integer_Address
'Value (
720 "16#" & Line
(Curs1
.. Curs2
) & "#");
727 Put_Line
("### incorrect user program termination detected.");
728 Put_Line
(" following data may not be meaningful");
733 -- Start of processing for Gnatmem
742 -- Now we start the gdb session using the following syntax
744 -- gdb --nx --nw -batch -x gnatmem.tmp
746 -- If there is a -o option we redirect the gdb output in the specified
747 -- file, otherwise we just read directly from a pipe.
749 if File_Pos
/= 0 then
751 Name
: aliased String := Argument
(File_Pos
) & ASCII
.NUL
;
757 Cmd
: aliased String := Target_Name
(1 .. Target_Name_Len
)
758 & "-gdb --nx --nw -batch -x " & Gnatmem_Tmp
& " > "
761 System_Cmd
(Cmd
'Address);
767 := "gdb --nx --nw " & Argument
(Exec_Pos
)
768 & " -batch -x " & Gnatmem_Tmp
& " > "
771 System_Cmd
(Cmd
'Address);
776 if not Gmem_Mode
then
777 FD
:= fopen
(Name
'Address, Mode_R
'Address);
784 Cmd
: aliased String := Target_Name
(1 .. Target_Name_Len
)
785 & "-gdb --nx --nw -batch -x " & Gnatmem_Tmp
& ASCII
.NUL
;
787 FD
:= popen
(Cmd
'Address, Mode_R
'Address);
791 Cmd
: aliased String := "gdb --nx --nw " & Argument
(Exec_Pos
)
792 & " -batch -x " & Gnatmem_Tmp
& ASCII
.NUL
;
795 FD
:= popen
(Cmd
'Address, Mode_R
'Address);
800 -- Open the FD file as a regular Text_IO file
802 if not Gmem_Mode
then
803 Ada
.Text_IO
.C_Streams
.Open
(FT
, In_File
, FD
);
806 -- Main loop analysing the data generated by the debugger
807 -- for each allocation, the backtrace is kept and stored in a htable
808 -- whose entry is the address. Fore ach deallocation, we look for the
809 -- corresponding allocation and cancel it.
818 -- Update global counters if the allocated size is meaningful
821 Tmp_Alloc
.Root
:= Read_BT
(BT_Depth
, FT
);
822 if Nb_Alloc
(Tmp_Alloc
.Root
) = 0 then
823 Nb_Root
:= Nb_Root
+ 1;
825 Set_Nb_Alloc
(Tmp_Alloc
.Root
, Nb_Alloc
(Tmp_Alloc
.Root
) + 1);
826 Address_HTable
.Set
(Tmp_Address
, Tmp_Alloc
);
828 elsif Tmp_Size
> 0 then
830 Global_Alloc_Size
:= Global_Alloc_Size
+ Tmp_Size
;
831 Global_Nb_Alloc
:= Global_Nb_Alloc
+ 1;
833 if Global_High_Water_Mark
< Global_Alloc_Size
then
834 Global_High_Water_Mark
:= Global_Alloc_Size
;
837 -- Read the corresponding back trace
839 Tmp_Alloc
.Root
:= Read_BT
(BT_Depth
, FT
);
841 -- Update the number of allocation root if this is a new one
843 if Nb_Alloc
(Tmp_Alloc
.Root
) = 0 then
844 Nb_Root
:= Nb_Root
+ 1;
847 -- Update allocation root specific counters
849 Set_Alloc_Size
(Tmp_Alloc
.Root
,
850 Alloc_Size
(Tmp_Alloc
.Root
) + Tmp_Size
);
852 Set_Nb_Alloc
(Tmp_Alloc
.Root
, Nb_Alloc
(Tmp_Alloc
.Root
) + 1);
854 if High_Water_Mark
(Tmp_Alloc
.Root
)
855 < Alloc_Size
(Tmp_Alloc
.Root
)
857 Set_High_Water_Mark
(Tmp_Alloc
.Root
,
858 Alloc_Size
(Tmp_Alloc
.Root
));
861 -- Associate this allocation root to the allocated address
863 Tmp_Alloc
.Size
:= Tmp_Size
;
864 Address_HTable
.Set
(Tmp_Address
, Tmp_Alloc
);
866 -- non meaninful output, just consumes the backtrace
869 Tmp_Alloc
.Root
:= Read_BT
(BT_Depth
, FT
);
874 -- Get the corresponding Dealloc_Size and Root
876 Tmp_Alloc
:= Address_HTable
.Get
(Tmp_Address
);
878 if Tmp_Alloc
.Root
= No_Root_Id
then
880 -- There was no prior allocation at this address, something is
881 -- very wrong. Mark this allocation root as problematic a
883 Tmp_Alloc
.Root
:= Read_BT
(BT_Depth
, FT
);
885 if Nb_Alloc
(Tmp_Alloc
.Root
) = 0 then
886 Set_Nb_Alloc
(Tmp_Alloc
.Root
, Nb_Alloc
(Tmp_Alloc
.Root
) - 1);
887 Nb_Wrong_Deall
:= Nb_Wrong_Deall
+ 1;
891 -- Update global counters
893 if not Quiet_Mode
then
894 Global_Alloc_Size
:= Global_Alloc_Size
- Tmp_Alloc
.Size
;
896 Global_Nb_Dealloc
:= Global_Nb_Dealloc
+ 1;
898 -- Update allocation root specific counters
900 if not Quiet_Mode
then
901 Set_Alloc_Size
(Tmp_Alloc
.Root
,
902 Alloc_Size
(Tmp_Alloc
.Root
) - Tmp_Alloc
.Size
);
904 Set_Nb_Alloc
(Tmp_Alloc
.Root
, Nb_Alloc
(Tmp_Alloc
.Root
) - 1);
906 -- update the number of allocation root if this one disappear
908 if Nb_Alloc
(Tmp_Alloc
.Root
) = 0 then
909 Nb_Root
:= Nb_Root
- 1;
912 -- De-associate the deallocated address
914 Address_HTable
.Remove
(Tmp_Address
);
919 -- We can get rid of the temp file now
921 if Run_Gdb
and then File_Pos
= 0 then
925 X
:= unlink
(Gnatmem_Tmp
'Address);
929 -- Print out general information about overall allocation
931 if not Quiet_Mode
then
932 Put_Line
("Global information");
933 Put_Line
("------------------");
935 Put
(" Total number of allocations :");
936 Ada
.Integer_Text_IO
.Put
(Global_Nb_Alloc
, 4);
939 Put
(" Total number of deallocations :");
940 Ada
.Integer_Text_IO
.Put
(Global_Nb_Dealloc
, 4);
943 Put_Line
(" Final Water Mark (non freed mem) :"
944 & Mem_Image
(Global_Alloc_Size
));
945 Put_Line
(" High Water Mark :"
946 & Mem_Image
(Global_High_Water_Mark
));
950 -- Print out the back traces corresponding to potential leaks in order
951 -- greatest number of non-deallocated allocations
953 Print_Back_Traces
: declare
954 type Root_Array
is array (Natural range <>) of Root_Id
;
955 Leaks
: Root_Array
(0 .. Nb_Root
);
956 Leak_Index
: Natural := 0;
958 Bogus_Dealls
: Root_Array
(1 .. Nb_Wrong_Deall
);
959 Deall_Index
: Natural := 0;
961 procedure Move
(From
: Natural; To
: Natural);
962 function Lt
(Op1
, Op2
: Natural) return Boolean;
963 package Root_Sort
is new GNAT
.Heap_Sort_G
(Move
, Lt
);
965 procedure Move
(From
: Natural; To
: Natural) is
967 Leaks
(To
) := Leaks
(From
);
970 function Lt
(Op1
, Op2
: Natural) return Boolean is
972 if Nb_Alloc
(Leaks
(Op1
)) > Nb_Alloc
(Leaks
(Op2
)) then
974 elsif Nb_Alloc
(Leaks
(Op1
)) = Nb_Alloc
(Leaks
(Op2
)) then
975 return Alloc_Size
(Leaks
(Op1
)) > Alloc_Size
(Leaks
(Op2
));
981 -- Start of processing for Print_Back_Traces
984 -- Transfer all the relevant Roots in the Leaks and a
985 -- Bogus_Deall arrays
987 Tmp_Alloc
.Root
:= Get_First
;
988 while Tmp_Alloc
.Root
/= No_Root_Id
loop
989 if Nb_Alloc
(Tmp_Alloc
.Root
) = 0 then
992 elsif Nb_Alloc
(Tmp_Alloc
.Root
) < 0 then
993 Deall_Index
:= Deall_Index
+ 1;
994 Bogus_Dealls
(Deall_Index
) := Tmp_Alloc
.Root
;
997 Leak_Index
:= Leak_Index
+ 1;
998 Leaks
(Leak_Index
) := Tmp_Alloc
.Root
;
1001 Tmp_Alloc
.Root
:= Get_Next
;
1004 -- Print out wrong deallocations
1006 if Nb_Wrong_Deall
> 0 then
1007 Put_Line
("Releasing deallocated memory at :");
1008 if not Quiet_Mode
then
1009 Put_Line
("--------------------------------");
1012 for J
in 1 .. Bogus_Dealls
'Last loop
1013 Print_BT
(Bogus_Dealls
(J
));
1018 -- Print out all allocation Leaks
1022 -- Sort the Leaks so that potentially important leaks appear first
1024 Root_Sort
.Sort
(Nb_Root
);
1026 for J
in 1 .. Leaks
'Last loop
1028 if Nb_Alloc
(Leaks
(J
)) = 1 then
1029 Put_Line
(Integer'Image (Nb_Alloc
(Leaks
(J
)))
1032 Put_Line
(Integer'Image (Nb_Alloc
(Leaks
(J
)))
1036 Put_Line
("Allocation Root #" & Integer'Image (J
));
1037 Put_Line
("-------------------");
1039 Put
(" Number of non freed allocations :");
1040 Ada
.Integer_Text_IO
.Put
(Nb_Alloc
(Leaks
(J
)), 4);
1043 Put_Line
(" Final Water Mark (non freed mem) :"
1044 & Mem_Image
(Alloc_Size
(Leaks
(J
))));
1046 Put_Line
(" High Water Mark :"
1047 & Mem_Image
(High_Water_Mark
(Leaks
(J
))));
1049 Put_Line
(" Backtrace :");
1051 Print_BT
(Leaks
(J
));
1055 end Print_Back_Traces
;