8sa1-gcc/gcc/ada/gnatmem.adb
Robert Dewar 9de61fcb9b a-secain.adb, [...]: Minor reformatting
2005-11-14  Robert Dewar  <dewar@adacore.com>

	* a-secain.adb, a-slcain.adb, a-shcain.adb, a-chtgke.ads, a-chtgke.adb,
	a-stwiha.adb, a-strhas.adb, a-chzla1.ads, a-chzla9.ads, a-stzbou.adb,
	a-stzbou.ads, a-stzfix.adb, a-stzhas.adb, a-stzmap.adb, a-stzmap.ads,
	a-stzsea.adb, a-stzsea.ads, a-stzsup.adb, a-stzsup.ads, a-stzunb.adb,
	a-stzunb.ads, a-szuzha.adb, a-szuzha.ads, a-szuzti.adb, a-szuzti.ads,
	a-ztcoau.adb, a-ztcoau.ads, a-ztcoio.adb, a-ztcstr.adb, a-ztcstr.ads,
	a-ztdeau.adb, a-ztdeau.ads, a-ztdeio.adb, a-ztdeio.ads, a-ztedit.adb,
	a-ztedit.ads, a-ztenau.ads, a-ztenio.adb, a-ztenio.ads, a-ztexio.adb,
	a-ztexio.ads, a-ztfiio.adb, a-ztfiio.ads, a-ztflau.adb, a-ztflau.ads,
	a-ztflio.adb, a-ztflio.ads, a-ztgeau.adb, a-ztgeau.ads, a-ztinau.adb,
	a-ztinau.ads, a-ztinio.adb, a-ztmoau.ads, a-ztmoio.adb, a-ztmoio.ads,
	a-zttest.adb, g-enblsp-vms-alpha.adb, g-enblsp-vms-alpha.adb,
	g-enblsp-vms-ia64.adb, g-enblsp-vms-ia64.adb, system-linux-hppa.ads,
	a-chacon.adb, a-chacon.ads, a-wichun.adb, a-wichun.ads, a-zchuni.adb,
	a-zchuni.ads, g-trasym-vms-alpha.adb, g-trasym-vms-ia64.adb,
	system-hpux-ia64.ads, g-soccon-unixware.ads, g-soliop-unixware.ads,
	g-soccon-interix.ads, g-soliop-solaris.ads, g-eacodu-vms.adb,
	g-expect-vms.adb, g-socthi-vms.adb, g-soliop-mingw.ads,
	a-intnam-unixware.ads, a-intnam-lynxos.ads, a-intnam-tru64.ads,
	a-intnam-aix.ads, a-intnam-linux.ads, a-intnam-linux.ads,
	a-intnam-dummy.ads, a-numaux-libc-x86.ads, a-intnam-interix.ads,
	a-intnam-solaris.ads, a-calend-vms.adb, a-calend-vms.ads,
	a-intnam-vms.ads, a-calend-mingw.adb, a-intnam-mingw.ads,
	a-intnam-vxworks.ads, a-numaux-vxworks.ads, system-unixware.ads,
	system-linux-ia64.ads, a-intnam-freebsd.ads, system-freebsd-x86.ads,
	system-lynxos-ppc.ads, system-linux-x86_64.ads, a-stunha.adb,
	a-cgaaso.ads, a-cgaaso.adb, a-chtgop.adb, a-cgcaso.adb, a-cgarso.adb,
	a-cohata.ads, a-crbtgk.adb, a-crbltr.ads, a-coprnu.adb, a-rbtgso.adb,
	a-intnam-darwin.ads, system-darwin-ppc.ads, gprmake.adb, makegpr.ads,
	system-tru64.ads, system-aix.ads, system-solaris-x86.ads,
	system-irix-o32.ads, s-interr-sigaction.adb, system-irix-n32.ads,
	s-parame-mingw.adb, system-hpux.ads, s-traceb-hpux.adb,
	system-linux-x86.ads, s-inmaop-dummy.adb, system-os2.ads,
	system-interix.ads, system-solaris-sparc.ads,
	system-solaris-sparcv9.ads, s-inmaop-vms.adb,
	s-mastop-vms.adb, expander.adb, expander.ads, s-gloloc-mingw.adb,
	system-mingw.ads, system-vms-zcx.ads, s-osinte-fsu.adb,
	s-traceb-mastop.adb, a-exextr.adb, a-exstat.adb, a-filico.ads,
	a-finali.ads, a-interr.ads, a-intsig.adb, a-intsig.ads,
	a-except.ads, a-numaux-x86.ads, a-astaco.adb, a-calend.adb,
	a-calend.ads, a-chahan.adb, a-chahan.ads, a-chlat9.ads,
	a-colien.adb, a-colien.ads, a-colire.adb, a-colire.ads,
	a-comlin.adb, a-comlin.ads, a-cwila1.ads, a-cwila9.ads,
	a-elchha.adb, a-decima.adb, a-decima.ads, a-diocst.ads,
	a-direio.adb, a-direio.ads, a-excach.adb, a-excach.adb,
	a-exctra.ads, ali-util.adb, a-ngcefu.adb, a-ngcoty.adb,
	a-ngcoty.ads, a-nudira.adb, a-nudira.ads, a-nuflra.adb,
	a-numaux.ads, a-reatim.ads, a-sequio.adb, a-sequio.ads,
	a-siocst.ads, a-ssicst.ads, a-stmaco.ads, a-storio.adb,
	a-strbou.adb, a-strbou.ads, a-stream.ads, a-strfix.adb,
	a-strmap.adb, a-strmap.ads, a-strsea.adb, a-strsea.ads,
	a-strsup.adb, a-strsup.ads, a-strunb.adb, a-strunb.ads,
	a-stwibo.adb, a-stwibo.ads, a-stwifi.adb, a-stwima.adb,
	a-stwima.ads, a-stwise.adb, a-stwise.ads, a-stwisu.adb,
	a-stwisu.ads, a-stwiun.adb, a-stwiun.ads, a-suteio.adb,
	a-suteio.ads, a-swmwco.ads, a-swuwti.adb, a-swuwti.ads,
	a-sytaco.adb, a-sytaco.ads, a-tasatt.adb, a-taside.adb,
	a-taside.ads, a-teioed.adb, a-textio.adb, a-textio.ads,
	a-ticoau.adb, a-ticoau.ads, a-ticoio.adb, a-tideau.adb,
	a-tideio.adb, a-tienau.adb, a-tienio.adb, a-tifiio.adb,
	a-tiflio.adb, a-tigeau.adb, a-tigeau.ads, a-tiinau.adb,
	a-tiinio.adb, a-timoau.adb, a-timoio.adb, a-timoio.ads,
	a-tiocst.ads, a-titest.adb, atree.adb, a-witeio.adb,
	a-witeio.ads, a-wtcoau.adb, a-wtcoau.ads, a-wtcoio.adb,
	a-wtcstr.ads, a-wtdeau.adb, a-wtdeio.adb, a-wtedit.adb,
	a-wtedit.ads, a-wtenau.adb, a-wtenio.adb, a-wtfiio.adb,
	a-wtflio.adb, a-wtgeau.adb, a-wtinau.adb, a-wtinio.adb,
	a-wtmoau.adb, a-wtmoio.adb, a-wttest.adb, back_end.adb,
	bindgen.adb, butil.adb, butil.ads, checks.ads, cio.c, comperr.adb,
	csets.ads, cstand.adb, debug.ads, elists.ads, errno.c, errout.adb,
	errout.ads, erroutc.adb, erroutc.ads, errutil.adb, errutil.ads,
	errutil.ads, err_vars.ads, eval_fat.adb, exp_ch11.adb, exp_ch11.ads,
	exp_ch2.adb, exp_ch7.ads, exp_imgv.ads, exp_pakd.adb, exp_prag.adb,
	exp_prag.ads, exp_tss.adb, exp_tss.ads, exp_vfpt.ads, fe.h, fmap.adb,
	freeze.ads, frontend.adb, frontend.ads, g-arrspl.adb, g-arrspl.ads,
	g-awk.adb, g-awk.ads, g-boumai.ads, g-calend.adb, g-calend.ads,
	g-catiio.adb, g-comlin.adb, g-comlin.ads, g-comlin.ads, g-comlin.ads,
	g-comver.adb, g-crc32.adb, g-crc32.ads, g-ctrl_c.ads, g-curexc.ads,
	g-debpoo.ads, g-debuti.adb, g-diopit.adb, g-diopit.ads, g-dirope.adb,
	g-dirope.ads, g-dyntab.adb, g-dyntab.ads, g-excact.adb, g-excact.ads,
	g-except.ads, g-exctra.adb, g-exctra.ads, g-expect.ads, g-flocon.ads,
	g-hesorg.ads, g-io.adb, g-locfil.ads, g-md5.adb, g-md5.ads, g-md5.ads,
	g-moreex.adb, g-signal.ads, g-signal.adb, gnatbind.ads, gnatchop.adb,
	gnatcmd.adb, gnatfind.adb, gnatlbr.adb, gnatmake.ads, gnatmem.adb,
	gnatprep.adb, gnatprep.ads, gnatsym.adb, gnatxref.adb, g-os_lib.adb,
	g-os_lib.ads, g-pehage.adb, g-pehage.ads, gprep.ads, g-regexp.adb,
	g-regexp.ads, g-regist.adb, g-regist.ads, g-regpat.ads, g-semaph.adb,
	g-socthi.adb, g-soliop.ads, g-spipat.adb, g-spipat.ads, g-sptabo.ads,
	g-sptain.ads, g-sptavs.ads, g-string.ads, g-tasloc.adb, g-tasloc.ads,
	g-trasym.adb, g-trasym.ads, i-fortra.adb, i-fortra.ads, inline.adb,
	layout.adb, live.adb, make.ads, makeutl.ads, makeutl.adb, mdll-fil.adb,
	mdll-fil.ads, mdll-utl.ads, memroot.ads, memtrack.adb, mlib.ads,
	mlib-fil.adb, mlib-fil.ads, mlib-prj.ads, mlib-utl.adb, mlib-utl.ads,
	nlists.adb, nlists.ads, osint.adb, osint.ads, osint-c.adb, osint-l.adb,
	osint-l.ads, osint-m.ads, output.adb, par.adb, par.adb, par.ads,
	par-ch11.adb, par-ch12.adb, par-ch2.adb, par-ch4.adb, par-ch5.adb,
	par-ch6.adb, par-ch9.adb, par-endh.adb, par-labl.adb, par-load.adb,
	par-tchk.adb, prep.adb, prepcomp.adb, prepcomp.ads, prj-attr.ads,
	prj-com.ads, prj-dect.adb, prj-dect.ads, prj-env.ads, prj-err.ads,
	prj-ext.ads, prj-makr.adb, prj-makr.ads, prj-nmsc.ads, prj-pars.adb,
	prj-pars.ads, prj-part.ads, prj-pp.ads, prj-proc.ads, prj-strt.adb,
	prj-strt.ads, prj-tree.adb, prj-util.adb, prj-util.ads, rtsfind.adb,
	sem.adb, sem.ads, sem_case.adb, sem_case.ads, sem_ch11.adb,
	sem_ch4.adb, sem_ch6.ads, sem_ch7.ads, sem_dist.ads, sem_elab.ads,
	sem_elim.ads, sem_eval.ads, sem_intr.ads, sem_maps.adb, sem_maps.ads,
	sem_maps.ads, sem_res.ads, sem_type.ads, sfn_scan.adb, sfn_scan.ads,
	s-imgwch.ads, s-imgwiu.adb, s-imgwiu.ads, s-inmaop.ads, sinput.adb,
	sinput.ads, s-pack03.adb, s-pack03.ads, s-pack05.adb, s-pack05.ads,
	s-pack06.adb, s-pack06.ads, s-pack07.adb, s-pack07.ads, s-pack09.adb,
	s-pack09.ads, s-pack10.adb, s-pack10.ads, s-pack11.adb, s-pack11.ads,
	s-pack12.adb, s-pack12.ads, s-pack13.adb, s-pack13.ads, s-pack14.adb,
	s-pack14.ads, s-pack15.adb, s-pack15.ads, s-pack17.adb, s-pack17.ads,
	s-pack18.adb, s-pack18.ads, s-pack19.adb, s-pack19.ads, s-pack20.adb,
	s-pack20.ads, s-pack21.adb, s-pack21.ads, s-pack22.adb, s-pack22.ads,
	s-pack23.adb, s-pack23.ads, s-pack24.adb, s-pack24.ads, s-pack25.adb,
	s-pack25.ads, s-pack26.adb, s-pack26.ads, s-pack27.adb, s-pack27.ads,
	s-pack28.adb, s-pack28.ads, s-pack29.adb, s-pack29.ads, s-pack30.adb,
	s-pack30.ads, s-pack31.adb, s-pack31.ads, s-pack33.adb, s-pack33.ads,
	s-pack34.adb, s-pack34.ads, s-pack35.adb, s-pack35.ads, s-pack36.adb,
	s-pack36.ads, s-pack37.adb, s-pack37.ads, s-pack38.adb, s-pack38.ads,
	s-pack39.adb, s-pack39.ads, s-pack40.adb, s-pack40.ads, s-pack41.adb,
	s-pack41.ads, s-pack42.adb, s-pack42.ads, s-pack43.adb, s-pack43.ads,
	s-pack44.adb, s-pack44.ads, s-pack45.adb, s-pack45.ads, s-pack46.adb,
	s-pack46.ads, s-pack47.adb, s-pack47.ads, s-pack48.adb, s-pack48.ads,
	s-pack49.adb, s-pack49.ads, s-pack50.adb, s-pack50.ads, s-pack51.adb,
	s-pack51.ads, s-pack52.adb, s-pack52.ads, s-pack53.adb, s-pack53.ads,
	s-pack54.adb, s-pack54.ads, s-pack55.adb, s-pack55.ads, s-pack56.adb,
	s-pack56.ads, s-pack57.adb, s-pack57.ads, s-pack58.adb, s-pack58.ads,
	s-pack59.adb, s-pack59.ads, s-pack60.adb, s-pack60.adb, s-pack60.ads,
	s-pack61.adb, s-pack61.ads, s-pack62.adb, s-pack62.ads, s-pack63.adb,
	s-pack63.ads, s-parint.adb, s-parint.adb, s-parint.ads, sprint.ads,
	s-purexc.ads, s-restri.ads, s-restri.adb, s-scaval.adb, s-scaval.ads,
	s-secsta.adb, s-secsta.ads, s-sequio.adb, s-sequio.ads, stand.ads,
	s-tasuti.adb, s-traceb.adb, s-traceb.ads, stringt.adb, stringt.ads,
	styleg.ads, s-valboo.adb, s-valboo.ads, s-valcha.adb, s-valcha.ads,
	s-valdec.adb, s-valdec.ads, s-valint.adb, s-valint.ads, s-valint.ads,
	s-vallld.adb, s-vallld.ads, s-vallli.adb, s-vallli.ads, s-valllu.adb,
	s-valllu.ads, s-valrea.adb, s-valrea.ads, s-valuns.adb, s-valuns.ads,
	s-valuti.adb, s-valuti.ads, s-valwch.ads, s-veboop.adb, s-veboop.ads,
	s-vercon.adb, s-vercon.ads, s-wchcnv.adb, s-wchcnv.ads, s-wchcon.ads,
	s-wchjis.adb, s-wchjis.ads, s-wchstw.adb, s-wchstw.adb, s-wchstw.ads,
	s-wchwts.adb, s-wchwts.ads, s-widboo.adb, s-widboo.ads, s-widcha.adb,
	s-widcha.ads, s-widenu.adb, s-widenu.ads, s-widlli.adb, s-widlli.ads,
	s-widllu.adb, s-widllu.ads, s-widwch.adb, s-widwch.ads, s-wwdcha.adb,
	s-wwdcha.ads, s-wwdenu.adb, s-wwdenu.ads, symbols.adb, symbols.ads,
	table.ads, targparm.adb, targparm.ads, tb-alvms.c, tb-alvxw.c,
	tbuild.adb, tree_io.ads, treepr.adb, treeprs.adt, ttypef.ads,
	ttypes.ads, types.adb, uintp.adb, uintp.ads, uname.ads, urealp.ads,
	usage.ads, validsw.ads, vxaddr2line.adb, widechar.adb, widechar.ads,
	xeinfo.adb, xnmake.adb, xref_lib.ads, xr_tabls.adb, xr_tabls.ads,
	xsinfo.adb, xtreeprs.adb, xsnames.adb, vms_conv.ads, vms_conv.adb,
	a-dirval.ads, a-dirval.adb, a-dirval-mingw.adb, a-direct.ads,
	a-direct.adb, indepsw.ads, prj-attr-pm.ads, system-linux-ppc.ads,
	a-numaux-darwin.ads, a-numaux-darwin.adb,
	a-swuwha.ads, a-stunha.ads: Minor reformatting

From-SVN: r107016
2005-11-15 15:06:45 +01:00

708 lines
22 KiB
Ada

------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- G N A T M E M --
-- --
-- B o d y --
-- --
-- Copyright (C) 1997-2005, AdaCore --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 2, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
-- for more details. You should have received a copy of the GNU General --
-- Public License distributed with GNAT; see file COPYING. If not, write --
-- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
-- Boston, MA 02110-1301, USA. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc. --
-- --
------------------------------------------------------------------------------
-- GNATMEM is a utility that tracks memory leaks. It is based on a simple
-- idea:
-- - Read the allocation log generated by the application linked using
-- instrumented memory allocation and dealocation (see memtrack.adb for
-- this circuitry). To get access to this functionality, the application
-- must be relinked with library libgmem.a:
-- $ gnatmake my_prog -largs -lgmem
-- The running my_prog will produce a file named gmem.out that will be
-- parsed by gnatmem.
-- - Record a reference to the allocated memory on each allocation call
-- - Suppress this reference on deallocation
-- - At the end of the program, remaining references are potential leaks.
-- sort them out the best possible way in order to locate the root of
-- the leak.
-- This capability is not supported on all platforms, please refer to
-- memtrack.adb for further information.
-- In order to help finding out the real leaks, the notion of "allocation
-- root" is defined. An allocation root is a specific point in the program
-- execution generating memory allocation where data is collected (such as
-- number of allocations, amount of memory allocated, high water mark, etc.)
with Gnatvsn; use Gnatvsn;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO;
with Ada.Integer_Text_IO;
with GNAT.Command_Line; use GNAT.Command_Line;
with GNAT.Heap_Sort_G;
with GNAT.OS_Lib; use GNAT.OS_Lib;
with GNAT.HTable; use GNAT.HTable;
with System; use System;
with System.Storage_Elements; use System.Storage_Elements;
with Memroot; use Memroot;
procedure Gnatmem is
------------------------
-- Other Declarations --
------------------------
type Storage_Elmt is record
Elmt : Character;
-- * = End of log file
-- A = found a ALLOC mark in the log
-- D = found a DEALL mark in the log
Address : Integer_Address;
Size : Storage_Count;
end record;
-- This needs a comment ???
Log_Name, Program_Name : String_Access;
-- These need comments, and should be on separate lines ???
function Read_Next return Storage_Elmt;
-- Reads next dynamic storage operation from the log file
function Mem_Image (X : Storage_Count) return String;
-- X is a size in storage_element. Returns a value
-- in Megabytes, Kilobytes or Bytes as appropriate.
procedure Process_Arguments;
-- Read command line arguments
procedure Usage;
-- Prints out the option help
function Gmem_Initialize (Dumpname : String) return Boolean;
-- Opens the file represented by Dumpname and prepares it for
-- work. Returns False if the file does not have the correct format, True
-- otherwise.
procedure Gmem_A2l_Initialize (Exename : String);
-- Initialises the convert_addresses interface by supplying it with
-- the name of the executable file Exename
-----------------------------------
-- HTable address --> Allocation --
-----------------------------------
type Allocation is record
Root : Root_Id;
Size : Storage_Count;
end record;
type Address_Range is range 0 .. 4097;
function H (A : Integer_Address) return Address_Range;
No_Alloc : constant Allocation := (No_Root_Id, 0);
package Address_HTable is new GNAT.HTable.Simple_HTable (
Header_Num => Address_Range,
Element => Allocation,
No_Element => No_Alloc,
Key => Integer_Address,
Hash => H,
Equal => "=");
BT_Depth : Integer := 1;
-- The following need comments ???
Global_Alloc_Size : Storage_Count := 0;
Global_High_Water_Mark : Storage_Count := 0;
Global_Nb_Alloc : Integer := 0;
Global_Nb_Dealloc : Integer := 0;
Nb_Root : Integer := 0;
Nb_Wrong_Deall : Integer := 0;
Minimum_NB_Leaks : Integer := 1;
Tmp_Alloc : Allocation;
Quiet_Mode : Boolean := False;
------------------------------
-- Allocation Roots Sorting --
------------------------------
Sort_Order : String (1 .. 3) := "nwh";
-- This is the default order in which sorting criteria will be applied
-- n - Total number of unfreed allocations
-- w - Final watermark
-- h - High watermark
--------------------------------
-- GMEM functionality binding --
--------------------------------
function Gmem_Initialize (Dumpname : String) return Boolean is
function Initialize (Dumpname : System.Address) return Boolean;
pragma Import (C, Initialize, "__gnat_gmem_initialize");
S : aliased String := Dumpname & ASCII.NUL;
begin
return Initialize (S'Address);
end Gmem_Initialize;
procedure Gmem_A2l_Initialize (Exename : String) is
procedure A2l_Initialize (Exename : System.Address);
pragma Import (C, A2l_Initialize, "__gnat_gmem_a2l_initialize");
S : aliased String := Exename & ASCII.NUL;
begin
A2l_Initialize (S'Address);
end Gmem_A2l_Initialize;
function Read_Next return Storage_Elmt is
procedure Read_Next (buf : System.Address);
pragma Import (C, Read_Next, "__gnat_gmem_read_next");
S : Storage_Elmt;
begin
Read_Next (S'Address);
return S;
end Read_Next;
-------
-- H --
-------
function H (A : Integer_Address) return Address_Range is
begin
return Address_Range (A mod Integer_Address (Address_Range'Last));
end H;
---------------
-- Mem_Image --
---------------
function Mem_Image (X : Storage_Count) return String is
Ks : constant Storage_Count := X / 1024;
Megs : constant Storage_Count := Ks / 1024;
Buff : String (1 .. 7);
begin
if Megs /= 0 then
Ada.Float_Text_IO.Put (Buff, Float (X) / 1024.0 / 1024.0, 2, 0);
return Buff & " Megabytes";
elsif Ks /= 0 then
Ada.Float_Text_IO.Put (Buff, Float (X) / 1024.0, 2, 0);
return Buff & " Kilobytes";
else
Ada.Integer_Text_IO.Put (Buff (1 .. 4), Integer (X));
return Buff (1 .. 4) & " Bytes";
end if;
end Mem_Image;
-----------
-- Usage --
-----------
procedure Usage is
begin
New_Line;
Put ("GNATMEM ");
Put_Line (Gnat_Version_String);
Put_Line ("Copyright 1997-2005, Free Software Foundation, Inc.");
New_Line;
Put_Line ("Usage: gnatmem switches [depth] exename");
New_Line;
Put_Line (" depth backtrace depth to take into account, default is"
& Integer'Image (BT_Depth));
Put_Line (" exename the name of the executable to be analyzed");
New_Line;
Put_Line ("Switches:");
Put_Line (" -b n same as depth parameter");
Put_Line (" -i file read the allocation log from specific file");
Put_Line (" default is gmem.out in the current directory");
Put_Line (" -m n masks roots with less than n leaks, default is 1");
Put_Line (" specify 0 to see even released allocation roots");
Put_Line (" -q quiet, minimum output");
Put_Line (" -s order sort allocation roots according to an order of");
Put_Line (" sort criteria");
GNAT.OS_Lib.OS_Exit (1);
end Usage;
-----------------------
-- Process_Arguments --
-----------------------
procedure Process_Arguments is
begin
-- Parse the options first
loop
case Getopt ("b: m: i: q s:") is
when ASCII.Nul => exit;
when 'b' =>
begin
BT_Depth := Natural'Value (Parameter);
exception
when Constraint_Error =>
Usage;
end;
when 'm' =>
begin
Minimum_NB_Leaks := Natural'Value (Parameter);
exception
when Constraint_Error =>
Usage;
end;
when 'i' =>
Log_Name := new String'(Parameter);
when 'q' =>
Quiet_Mode := True;
when 's' =>
declare
S : constant String (Sort_Order'Range) := Parameter;
begin
for J in Sort_Order'Range loop
if S (J) = 'n' or else
S (J) = 'w' or else
S (J) = 'h'
then
Sort_Order (J) := S (J);
else
Put_Line ("Invalid sort criteria string.");
GNAT.OS_Lib.OS_Exit (1);
end if;
end loop;
end;
when others =>
null;
end case;
end loop;
-- Set default log file if -i hasn't been specified
if Log_Name = null then
Log_Name := new String'("gmem.out");
end if;
-- Get the optional backtrace length and program name
declare
Str1 : constant String := GNAT.Command_Line.Get_Argument;
Str2 : constant String := GNAT.Command_Line.Get_Argument;
begin
if Str1 = "" then
Usage;
end if;
if Str2 = "" then
Program_Name := new String'(Str1);
else
BT_Depth := Natural'Value (Str1);
Program_Name := new String'(Str2);
end if;
exception
when Constraint_Error =>
Usage;
end;
-- Ensure presence of executable suffix in Program_Name
declare
Suffix : String_Access := Get_Executable_Suffix;
Tmp : String_Access;
begin
if Suffix.all /= ""
and then
Program_Name.all
(Program_Name.all'Last - Suffix.all'Length + 1 ..
Program_Name.all'Last) /= Suffix.all
then
Tmp := new String'(Program_Name.all & Suffix.all);
Free (Program_Name);
Program_Name := Tmp;
end if;
Free (Suffix);
-- Search the executable on the path. If not found in the PATH, we
-- default to the current directory. Otherwise, libaddr2line will
-- fail with an error:
-- (null): Bad address
Tmp := Locate_Exec_On_Path (Program_Name.all);
if Tmp = null then
Tmp := new String'('.' & Directory_Separator & Program_Name.all);
end if;
Free (Program_Name);
Program_Name := Tmp;
end;
if not Is_Regular_File (Log_Name.all) then
Put_Line ("Couldn't find " & Log_Name.all);
GNAT.OS_Lib.OS_Exit (1);
end if;
if not Gmem_Initialize (Log_Name.all) then
Put_Line ("File " & Log_Name.all & " is not a gnatmem log file");
GNAT.OS_Lib.OS_Exit (1);
end if;
if not Is_Regular_File (Program_Name.all) then
Put_Line ("Couldn't find " & Program_Name.all);
end if;
Gmem_A2l_Initialize (Program_Name.all);
exception
when GNAT.Command_Line.Invalid_Switch =>
Ada.Text_IO.Put_Line ("Invalid switch : "
& GNAT.Command_Line.Full_Switch);
Usage;
end Process_Arguments;
Cur_Elmt : Storage_Elmt;
-- Start of processing for Gnatmem
begin
Process_Arguments;
-- Main loop analysing the data generated by the instrumented routines.
-- For each allocation, the backtrace is kept and stored in a htable
-- whose entry is the address. For each deallocation, we look for the
-- corresponding allocation and cancel it.
Main : loop
Cur_Elmt := Read_Next;
case Cur_Elmt.Elmt is
when '*' =>
exit Main;
when 'A' =>
-- Update global counters if the allocated size is meaningful
if Quiet_Mode then
Tmp_Alloc.Root := Read_BT (BT_Depth);
if Nb_Alloc (Tmp_Alloc.Root) = 0 then
Nb_Root := Nb_Root + 1;
end if;
Set_Nb_Alloc (Tmp_Alloc.Root, Nb_Alloc (Tmp_Alloc.Root) + 1);
Address_HTable.Set (Cur_Elmt.Address, Tmp_Alloc);
elsif Cur_Elmt.Size > 0 then
Global_Alloc_Size := Global_Alloc_Size + Cur_Elmt.Size;
Global_Nb_Alloc := Global_Nb_Alloc + 1;
if Global_High_Water_Mark < Global_Alloc_Size then
Global_High_Water_Mark := Global_Alloc_Size;
end if;
-- Read the corresponding back trace
Tmp_Alloc.Root := Read_BT (BT_Depth);
-- Update the number of allocation root if this is a new one
if Nb_Alloc (Tmp_Alloc.Root) = 0 then
Nb_Root := Nb_Root + 1;
end if;
-- Update allocation root specific counters
Set_Alloc_Size (Tmp_Alloc.Root,
Alloc_Size (Tmp_Alloc.Root) + Cur_Elmt.Size);
Set_Nb_Alloc (Tmp_Alloc.Root, Nb_Alloc (Tmp_Alloc.Root) + 1);
if High_Water_Mark (Tmp_Alloc.Root) <
Alloc_Size (Tmp_Alloc.Root)
then
Set_High_Water_Mark (Tmp_Alloc.Root,
Alloc_Size (Tmp_Alloc.Root));
end if;
-- Associate this allocation root to the allocated address
Tmp_Alloc.Size := Cur_Elmt.Size;
Address_HTable.Set (Cur_Elmt.Address, Tmp_Alloc);
-- non meaningful output, just consumes the backtrace
else
Tmp_Alloc.Root := Read_BT (BT_Depth);
end if;
when 'D' =>
-- Get the corresponding Dealloc_Size and Root
Tmp_Alloc := Address_HTable.Get (Cur_Elmt.Address);
if Tmp_Alloc.Root = No_Root_Id then
-- There was no prior allocation at this address, something is
-- very wrong. Mark this allocation root as problematic
Tmp_Alloc.Root := Read_BT (BT_Depth);
if Nb_Alloc (Tmp_Alloc.Root) = 0 then
Set_Nb_Alloc (Tmp_Alloc.Root, Nb_Alloc (Tmp_Alloc.Root) - 1);
Nb_Wrong_Deall := Nb_Wrong_Deall + 1;
end if;
else
-- Update global counters
if not Quiet_Mode then
Global_Alloc_Size := Global_Alloc_Size - Tmp_Alloc.Size;
end if;
Global_Nb_Dealloc := Global_Nb_Dealloc + 1;
-- Update allocation root specific counters
if not Quiet_Mode then
Set_Alloc_Size (Tmp_Alloc.Root,
Alloc_Size (Tmp_Alloc.Root) - Tmp_Alloc.Size);
end if;
Set_Nb_Alloc (Tmp_Alloc.Root, Nb_Alloc (Tmp_Alloc.Root) - 1);
-- update the number of allocation root if this one disappear
if Nb_Alloc (Tmp_Alloc.Root) = 0
and then Minimum_NB_Leaks > 0 then
Nb_Root := Nb_Root - 1;
end if;
-- De-associate the deallocated address
Address_HTable.Remove (Cur_Elmt.Address);
end if;
when others =>
raise Program_Error;
end case;
end loop Main;
-- Print out general information about overall allocation
if not Quiet_Mode then
Put_Line ("Global information");
Put_Line ("------------------");
Put (" Total number of allocations :");
Ada.Integer_Text_IO.Put (Global_Nb_Alloc, 4);
New_Line;
Put (" Total number of deallocations :");
Ada.Integer_Text_IO.Put (Global_Nb_Dealloc, 4);
New_Line;
Put_Line (" Final Water Mark (non freed mem) :"
& Mem_Image (Global_Alloc_Size));
Put_Line (" High Water Mark :"
& Mem_Image (Global_High_Water_Mark));
New_Line;
end if;
-- Print out the back traces corresponding to potential leaks in order
-- greatest number of non-deallocated allocations
Print_Back_Traces : declare
type Root_Array is array (Natural range <>) of Root_Id;
Leaks : Root_Array (0 .. Nb_Root);
Leak_Index : Natural := 0;
Bogus_Dealls : Root_Array (1 .. Nb_Wrong_Deall);
Deall_Index : Natural := 0;
Nb_Alloc_J : Natural := 0;
procedure Move (From : Natural; To : Natural);
function Lt (Op1, Op2 : Natural) return Boolean;
package Root_Sort is new GNAT.Heap_Sort_G (Move, Lt);
procedure Move (From : Natural; To : Natural) is
begin
Leaks (To) := Leaks (From);
end Move;
function Lt (Op1, Op2 : Natural) return Boolean is
function Apply_Sort_Criterion (S : Character) return Integer;
-- Applies a specific sort criterion; returns -1, 0 or 1 if Op1 is
-- smaller than, equal, or greater than Op2 according to criterion
function Apply_Sort_Criterion (S : Character) return Integer is
LOp1, LOp2 : Integer;
begin
case S is
when 'n' =>
LOp1 := Nb_Alloc (Leaks (Op1));
LOp2 := Nb_Alloc (Leaks (Op2));
when 'w' =>
LOp1 := Integer (Alloc_Size (Leaks (Op1)));
LOp2 := Integer (Alloc_Size (Leaks (Op2)));
when 'h' =>
LOp1 := Integer (High_Water_Mark (Leaks (Op1)));
LOp2 := Integer (High_Water_Mark (Leaks (Op2)));
when others =>
return 0; -- Can't actually happen
end case;
if LOp1 < LOp2 then
return -1;
elsif LOp1 > LOp2 then
return 1;
else
return 0;
end if;
exception
when Constraint_Error =>
return 0;
end Apply_Sort_Criterion;
Result : Integer;
-- Start of processing for Lt
begin
for S in Sort_Order'Range loop
Result := Apply_Sort_Criterion (Sort_Order (S));
if Result = -1 then
return False;
elsif Result = 1 then
return True;
end if;
end loop;
return False;
end Lt;
-- Start of processing for Print_Back_Traces
begin
-- Transfer all the relevant Roots in the Leaks and a
-- Bogus_Deall arrays
Tmp_Alloc.Root := Get_First;
while Tmp_Alloc.Root /= No_Root_Id loop
if Nb_Alloc (Tmp_Alloc.Root) = 0 and then Minimum_NB_Leaks > 0 then
null;
elsif Nb_Alloc (Tmp_Alloc.Root) < 0 then
Deall_Index := Deall_Index + 1;
Bogus_Dealls (Deall_Index) := Tmp_Alloc.Root;
else
Leak_Index := Leak_Index + 1;
Leaks (Leak_Index) := Tmp_Alloc.Root;
end if;
Tmp_Alloc.Root := Get_Next;
end loop;
-- Print out wrong deallocations
if Nb_Wrong_Deall > 0 then
Put_Line ("Releasing deallocated memory at :");
if not Quiet_Mode then
Put_Line ("--------------------------------");
end if;
for J in 1 .. Bogus_Dealls'Last loop
Print_BT (Bogus_Dealls (J), Short => Quiet_Mode);
New_Line;
end loop;
end if;
-- Print out all allocation Leaks
if Nb_Root > 0 then
-- Sort the Leaks so that potentially important leaks appear first
Root_Sort.Sort (Nb_Root);
for J in 1 .. Leaks'Last loop
Nb_Alloc_J := Nb_Alloc (Leaks (J));
if Nb_Alloc_J >= Minimum_NB_Leaks then
if Quiet_Mode then
if Nb_Alloc_J = 1 then
Put_Line (" 1 leak at :");
else
Put_Line (Integer'Image (Nb_Alloc_J) & " leaks at :");
end if;
else
Put_Line ("Allocation Root #" & Integer'Image (J));
Put_Line ("-------------------");
Put (" Number of non freed allocations :");
Ada.Integer_Text_IO.Put (Nb_Alloc_J, 4);
New_Line;
Put_Line
(" Final Water Mark (non freed mem) :"
& Mem_Image (Alloc_Size (Leaks (J))));
Put_Line
(" High Water Mark :"
& Mem_Image (High_Water_Mark (Leaks (J))));
Put_Line (" Backtrace :");
end if;
Print_BT (Leaks (J), Short => Quiet_Mode);
New_Line;
end if;
end loop;
end if;
end Print_Back_Traces;
end Gnatmem;