31564c5b9f2a998916704a45a593539f609f2270
[gcc.git] / gcc / ada / g-memdum.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- G N A T . M E M O R Y _ D U M P --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2003-2014, AdaCore --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with System; use System;
33 with System.Img_BIU; use System.Img_BIU;
34 with System.Storage_Elements; use System.Storage_Elements;
35
36 with GNAT.IO; use GNAT.IO;
37 with GNAT.Debug_Utilities; use GNAT.Debug_Utilities;
38
39 with Ada.Unchecked_Conversion;
40
41 package body GNAT.Memory_Dump is
42
43 ----------
44 -- Dump --
45 ----------
46
47 procedure Dump
48 (Addr : Address;
49 Count : Natural;
50 Prefix : Prefix_Type := Absolute_Address)
51 is
52 Ctr : Natural := Count;
53 -- Count of bytes left to output
54
55 Offset_Buf : String (1 .. Standard'Address_Size / 4 + 4);
56 Offset_Last : Natural;
57 -- Buffer for prefix in Offset mode
58
59 Adr : Address := Addr;
60 -- Current address
61
62 N : Natural := 0;
63 -- Number of bytes output on current line
64
65 C : Character;
66 -- Character at current storage address
67
68 AIL : Natural;
69 -- Number of chars in prefix (including colon and space)
70
71 Line_Len : Natural;
72 -- Line length for entire line
73
74 Hex : constant array (0 .. 15) of Character := "0123456789ABCDEF";
75
76 type Char_Ptr is access all Character;
77
78 function To_Char_Ptr is new Ada.Unchecked_Conversion (Address, Char_Ptr);
79
80 begin
81 case Prefix is
82 when Absolute_Address =>
83 AIL := Address_Image_Length - 4 + 2;
84 when Offset =>
85 Offset_Last := Offset_Buf'First - 1;
86 Set_Image_Based_Integer (Ctr, 16, 0, Offset_Buf, Offset_Last);
87 AIL := Offset_Last - 4 + 2;
88 when None =>
89 AIL := 0;
90 end case;
91 Line_Len := AIL + 3 * 16 + 2 + 16;
92
93 declare
94 Line_Buf : String (1 .. Line_Len);
95 begin
96 while Ctr /= 0 loop
97
98 -- Start of line processing
99
100 if N = 0 then
101 case Prefix is
102 when Absolute_Address =>
103 declare
104 S : constant String := Image (Adr);
105 begin
106 Line_Buf (1 .. AIL) := S (4 .. S'Length - 1) & ": ";
107 end;
108
109 when Offset =>
110 declare
111 Last : Natural := 0;
112 Len : Natural;
113 begin
114 Set_Image_Based_Integer
115 (Count - Ctr, 16, 0, Offset_Buf, Last);
116 Len := Last - 4;
117
118 Line_Buf (1 .. AIL - Len - 2) := (others => '0');
119 Line_Buf (AIL - Len - 1 .. AIL - 2) :=
120 Offset_Buf (4 .. Last - 1);
121 Line_Buf (AIL - 1 .. AIL) := ": ";
122 end;
123 when None =>
124 null;
125 end case;
126
127 Line_Buf (AIL + 1 .. Line_Buf'Last) := (others => ' ');
128 Line_Buf (AIL + 3 * 16 + 1) := '"';
129 end if;
130
131 -- Add one character to current line
132
133 C := To_Char_Ptr (Adr).all;
134 Adr := Adr + 1;
135 Ctr := Ctr - 1;
136
137 Line_Buf (AIL + 3 * N + 1) := Hex (Character'Pos (C) / 16);
138 Line_Buf (AIL + 3 * N + 2) := Hex (Character'Pos (C) mod 16);
139
140 if C < ' ' or else C = Character'Val (16#7F#) then
141 C := '?';
142 end if;
143
144 Line_Buf (AIL + 3 * 16 + 2 + N) := C;
145 N := N + 1;
146
147 -- End of line processing
148
149 if N = 16 then
150 Line_Buf (Line_Buf'Last) := '"';
151 GNAT.IO.Put_Line (Line_Buf);
152 N := 0;
153 end if;
154 end loop;
155
156 -- Deal with possible last partial line
157
158 if N /= 0 then
159 Line_Buf (AIL + 3 * 16 + 2 + N) := '"';
160 GNAT.IO.Put_Line (Line_Buf (1 .. AIL + 3 * 16 + 2 + N));
161 end if;
162 end;
163
164 end Dump;
165
166 end GNAT.Memory_Dump;