1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1997-2005, AdaCore --
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 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
28 with GNAT
.HTable
; use GNAT
.HTable
;
29 with Ada
.Text_IO
; use Ada
.Text_IO
;
30 with System
.Storage_Elements
; use System
.Storage_Elements
;
32 package body Memroot
is
34 Main_Name_Id
: Name_Id
;
35 -- The constant "main" where we should stop the backtraces
41 package Chars
is new GNAT
.Table
(
42 Table_Component_Type
=> Character,
43 Table_Index_Type
=> Integer,
45 Table_Initial
=> 10_000
,
46 Table_Increment
=> 100);
47 -- The actual character container for names
50 First
, Last
: Integer;
53 package Names
is new GNAT
.Table
(
54 Table_Component_Type
=> Name
,
55 Table_Index_Type
=> Name_Id
,
58 Table_Increment
=> 100);
60 type Name_Range
is range 1 .. 1023;
62 function Name_Eq
(N1
, N2
: Name
) return Boolean;
65 function H
(N
: Name
) return Name_Range
;
67 package Name_HTable
is new GNAT
.HTable
.Simple_HTable
(
68 Header_Num
=> Name_Range
,
70 No_Element
=> No_Name_Id
,
80 Name
, File
, Line
: Name_Id
;
87 Short
: Boolean := False) return String;
88 -- Returns an image for F containing the file name, the Line number,
89 -- and if 'Short' is not true, the subprogram name. When possible, spaces
90 -- are inserted between the line number and the subprogram name in order
91 -- to align images of the same frame. Alignement is cimputed with Max_Fil
92 -- & Max_Lin representing the max number of character in a filename or
93 -- length in a given frame.
95 package Frames
is new GNAT
.Table
(
96 Table_Component_Type
=> Frame
,
97 Table_Index_Type
=> Frame_Id
,
100 Table_Increment
=> 100);
102 type Frame_Range
is range 1 .. 10000;
103 function H
(N
: Integer_Address
) return Frame_Range
;
105 package Frame_HTable
is new GNAT
.HTable
.Simple_HTable
(
106 Header_Num
=> Frame_Range
,
108 No_Element
=> No_Frame_Id
,
109 Key
=> Integer_Address
,
118 First
, Last
: Integer;
120 Alloc_Size
: Storage_Count
;
121 High_Water_Mark
: Storage_Count
;
124 package Frames_In_Root
is new GNAT
.Table
(
125 Table_Component_Type
=> Frame_Id
,
126 Table_Index_Type
=> Integer,
127 Table_Low_Bound
=> 1,
128 Table_Initial
=> 400,
129 Table_Increment
=> 100);
131 package Roots
is new GNAT
.Table
(
132 Table_Component_Type
=> Root
,
133 Table_Index_Type
=> Root_Id
,
134 Table_Low_Bound
=> 1,
135 Table_Initial
=> 200,
136 Table_Increment
=> 100);
137 type Root_Range
is range 1 .. 513;
139 function Root_Eq
(N1
, N2
: Root
) return Boolean;
140 function H
(B
: Root
) return Root_Range
;
142 package Root_HTable
is new GNAT
.HTable
.Simple_HTable
(
143 Header_Num
=> Root_Range
,
145 No_Element
=> No_Root_Id
,
154 function Alloc_Size
(B
: Root_Id
) return Storage_Count
is
156 return Roots
.Table
(B
).Alloc_Size
;
164 (Addr
: System
.Address
;
171 Frames
.Increment_Last
;
172 Frames
.Table
(Frames
.Last
) := Frame
'(Name, File, Line);
174 Frame_HTable.Set (To_Integer (Addr), Frames.Last);
182 function Enter_Name (S : String) return Name_Id is
183 Old_L : constant Integer := Chars.Last;
184 Len : constant Integer := S'Length;
185 F : constant Integer := Chars.Allocate (Len);
189 Chars.Table (F .. F + Len - 1) := Chars.Table_Type (S);
190 Names.Increment_Last;
191 Names.Table (Names.Last) := Name'(F
, F
+ Len
- 1);
192 Res
:= Name_HTable
.Get
(Names
.Table
(Names
.Last
));
194 if Res
/= No_Name_Id
then
195 Names
.Decrement_Last
;
196 Chars
.Set_Last
(Old_L
);
200 Name_HTable
.Set
(Names
.Table
(Names
.Last
), Names
.Last
);
209 function Enter_Root
(Fr
: Frame_Array
) return Root_Id
is
210 Old_L
: constant Integer := Frames_In_Root
.Last
;
211 Len
: constant Integer := Fr
'Length;
212 F
: constant Integer := Frames_In_Root
.Allocate
(Len
);
216 Frames_In_Root
.Table
(F
.. F
+ Len
- 1) :=
217 Frames_In_Root
.Table_Type
(Fr
);
218 Roots
.Increment_Last
;
219 Roots
.Table
(Roots
.Last
) := Root
'(F, F + Len - 1, 0, 0, 0);
220 Res := Root_HTable.Get (Roots.Table (Roots.Last));
222 if Res /= No_Root_Id then
223 Frames_In_Root.Set_Last (Old_L);
224 Roots.Decrement_Last;
228 Root_HTable.Set (Roots.Table (Roots.Last), Roots.Last);
237 function Frames_Of (B : Root_Id) return Frame_Array is
240 Frames_In_Root.Table (Roots.Table (B).First .. Roots.Table (B).Last));
247 function Get_First return Root_Id is
249 return Root_HTable.Get_First;
256 function Get_Next return Root_Id is
258 return Root_HTable.Get_Next;
265 function H (B : Root) return Root_Range is
267 type Uns is mod 2 ** 32;
269 function Rotate_Left (Value : Uns; Amount : Natural) return Uns;
270 pragma Import (Intrinsic, Rotate_Left);
275 for J in B.First .. B.Last loop
276 Tmp := Rotate_Left (Tmp, 1) + Uns (Frames_In_Root.Table (J));
279 return Root_Range'First
280 + Root_Range'Base (Tmp mod Root_Range'Range_Length);
283 function H (N : Name) return Name_Range is
284 function H is new Hash (Name_Range);
287 return H (String (Chars.Table (N.First .. N.Last)));
290 function H (N : Integer_Address) return Frame_Range is
292 return Frame_Range (1 + N mod Frame_Range'Range_Length);
295 ---------------------
296 -- High_Water_Mark --
297 ---------------------
299 function High_Water_Mark (B : Root_Id) return Storage_Count is
301 return Roots.Table (B).High_Water_Mark;
308 function Image (N : Name_Id) return String is
309 Nam : Name renames Names.Table (N);
312 return String (Chars.Table (Nam.First .. Nam.Last));
319 Short : Boolean := False) return String
321 Fram : Frame renames Frames.Table (F);
322 Fil : Name renames Names.Table (Fram.File);
323 Lin : Name renames Names.Table (Fram.Line);
324 Nam : Name renames Names.Table (Fram.Name);
326 Fil_Len : constant Integer := Fil.Last - Fil.First + 1;
327 Lin_Len : constant Integer := Lin.Last - Lin.First + 1;
329 use type Chars.Table_Type;
331 Spaces : constant String (1 .. 80) := (1 .. 80 => ' ');
333 Result : constant String :=
334 String (Chars.Table (Fil.First .. Fil.Last))
336 & String (Chars.Table (Lin.First .. Lin.Last));
342 & Spaces (1 .. 1 + Max_Fil - Fil_Len + Max_Lin - Lin_Len)
343 & String (Chars.Table (Nam.First .. Nam.Last));
351 function Name_Eq (N1, N2 : Name) return Boolean is
352 use type Chars.Table_Type;
355 Chars.Table (N1.First .. N1.Last) = Chars.Table (N2.First .. N2.Last);
362 function Nb_Alloc (B : Root_Id) return Integer is
364 return Roots.Table (B).Nb_Alloc;
371 procedure Print_BT (B : Root_Id; Short : Boolean := False) is
372 Max_Col_Width : constant := 35;
373 -- Largest filename length for which backtraces will be
374 -- properly aligned. Frames containing longer names won't be
375 -- truncated but they won't be properly aligned either.
377 F : constant Frame_Array := Frames_Of (B);
386 for J in F'Range loop
388 Fram : Frame renames Frames.Table (F (J));
389 Fil : Name renames Names.Table (Fram.File);
390 Lin : Name renames Names.Table (Fram.Line);
393 Max_Fil := Integer'Max (Max_Fil, Fil.Last - Fil.First + 1);
394 Max_Lin := Integer'Max (Max_Lin, Lin.Last - Lin.First + 1);
398 Max_Fil := Integer'Min (Max_Fil, Max_Col_Width);
400 for J in F'Range loop
402 Put_Line (Image (F (J), Max_Fil, Max_Lin, Short));
410 function Read_BT (BT_Depth : Integer) return Root_Id is
411 Max_Line : constant Integer := 500;
414 Line : String (1 .. Max_Line);
416 Frames : Frame_Array (1 .. BT_Depth);
417 F : Integer := Frames'First;
421 Add : System.Address;
422 Int_Add : Integer_Address;
424 Main_Found : Boolean := False;
425 pragma Warnings (Off, Line);
428 pragma Inline (Find_File);
429 -- Position Curs1 and Curs2 so that Line (Curs1 .. Curs2) contains
430 -- the file name. The file name may not be on the current line since
431 -- a frame may be printed on more than one line when there is a lot
432 -- of parameters or names are long, so this subprogram can read new
436 pragma Inline (Find_Line);
437 -- Position Curs1 and Curs2 so that Line (Curs1 .. Curs2) contains
441 pragma Inline (Find_Name);
442 -- Position Curs1 and Curs2 so that Line (Curs1 .. Curs2) contains
443 -- the subprogram name.
445 function Skip_To_Space (Pos : Integer) return Integer;
446 pragma Inline (Skip_To_Space);
447 -- Scans Line starting with position Pos, returning the position
448 -- immediately before the first space, or the value of Last if no
455 procedure Find_File is
462 -- Scan backwards from end of line until ':' is encountered
464 for J in reverse Curs1 .. Last loop
465 if Line (J) = ':' then
475 procedure Find_Line is
480 -- Check for Curs1 too large. Should never happen with non-corrupt
481 -- output. If it does happen, just reset it to the highest value.
492 procedure Find_Name is
494 -- Skip the address value and " in "
496 Curs1 := Skip_To_Space (1) + 5;
497 Curs2 := Skip_To_Space (Curs1);
504 function Skip_To_Space (Pos : Integer) return Integer is
506 for Cur in Pos .. Last loop
507 if Line (Cur) = ' ' then
515 procedure Gmem_Read_Next_Frame (Addr : out System.Address);
516 pragma Import (C, Gmem_Read_Next_Frame, "__gnat_gmem_read_next_frame");
517 -- Read the next frame in the current traceback. Addr is set to 0 if
518 -- there are no more addresses in this traceback. The pointer is moved
519 -- to the next frame.
521 procedure Gmem_Symbolic
522 (Addr : System.Address; Buf : String; Last : out Natural);
523 pragma Import (C, Gmem_Symbolic, "__gnat_gmem_symbolic");
524 -- Get the symbolic traceback for Addr. Note: we cannot use
525 -- GNAT.Tracebacks.Symbolic, since the latter will only work with the
526 -- current executable.
528 -- "__gnat_gmem_symbolic" will work with the executable whose name is
529 -- given in gnat_argv[0], as initialized by Gnatmem.Gmem_A21_Initialize.
531 -- Start of processing for Read_BT
534 while F <= BT_Depth and then not Main_Found loop
535 Gmem_Read_Next_Frame (Add);
536 Int_Add := To_Integer (Add);
537 exit when Int_Add = 0;
539 Fr := Frame_HTable.Get (Int_Add);
541 if Fr = No_Frame_Id then
542 Gmem_Symbolic (Add, Line, Last);
543 Last := Last - 1; -- get rid of the trailing line-feed
546 -- Skip the __gnat_malloc frame itself
548 if Line (Curs1 .. Curs2) /= "<__gnat_malloc>" then
549 Nam := Enter_Name (Line (Curs1 .. Curs2));
550 Main_Found := (Nam = Main_Name_Id);
553 Fil := Enter_Name (Line (Curs1 .. Curs2));
555 Lin := Enter_Name (Line (Curs1 .. Curs2));
557 Frames (F) := Enter_Frame (Add, Nam, Fil, Lin);
563 Main_Found := (Memroot.Frames.Table (Fr).Name = Main_Name_Id);
568 return Enter_Root (Frames (1 .. F - 1));
575 function Root_Eq (N1, N2 : Root) return Boolean is
576 use type Frames_In_Root.Table_Type;
580 Frames_In_Root.Table (N1.First .. N1.Last)
581 = Frames_In_Root.Table (N2.First .. N2.Last);
588 procedure Set_Alloc_Size (B : Root_Id; V : Storage_Count) is
590 Roots.Table (B).Alloc_Size := V;
593 -------------------------
594 -- Set_High_Water_Mark --
595 -------------------------
597 procedure Set_High_Water_Mark (B : Root_Id; V : Storage_Count) is
599 Roots.Table (B).High_Water_Mark := V;
600 end Set_High_Water_Mark;
606 procedure Set_Nb_Alloc (B : Root_Id; V : Integer) is
608 Roots.Table (B).Nb_Alloc := V;
612 -- Initialize name for No_Name_ID
614 Names.Increment_Last;
615 Names.Table (Names.Last) := Name'(1, 0);
616 Main_Name_Id
:= Enter_Name
("main");