1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M - S T A C K _ U S A G E --
9 -- Copyright (C) 2004-2011, Free Software Foundation, Inc. --
11 -- GNARL 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. --
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. --
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/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 with System
.Parameters
;
36 package body System
.Stack_Usage
is
37 use System
.Storage_Elements
;
46 -- Stackl_Slots is an internal data type to represent a sequence of real
47 -- stack slots initialized with a provided pattern, with operations to
48 -- abstract away the target call stack growth direction.
50 type Stack_Slots
is array (Integer range <>) of Pattern_Type
;
51 for Stack_Slots
'Component_Size use Pattern_Type
'Object_Size;
53 -- We will carefully handle the initializations ourselves and might want
54 -- to remap an initialized overlay later on with an address clause.
56 pragma Suppress_Initialization
(Stack_Slots
);
58 -- The abstract Stack_Slots operations all operate over the simple array
61 -- memory addresses increasing ---->
63 -- Slots('First) Slots('Last)
66 -- +------------------------------------------------------------------+
68 -- +------------------------------------------------------------------+
70 -- What we call Top or Bottom always denotes call chain leaves or entry
71 -- points respectively, and their relative positions in the stack array
72 -- depends on the target stack growth direction:
76 -- <----- calls push frames towards decreasing addresses
78 -- Top(most) Slot Bottom(most) Slot
81 -- +------------------------------------------------------------------+
82 -- |####| | leaf frame | ... | entry frame |
83 -- +------------------------------------------------------------------+
87 -- calls push frames towards increasing addresses ----->
89 -- Bottom(most) Slot Top(most) Slot
92 -- +------------------------------------------------------------------+
93 -- | entry frame | ... | leaf frame | |####|
94 -- +------------------------------------------------------------------+
100 -- Now the implementation of the services offered by this unit, on top of
101 -- the Stack_Slots abstraction above.
103 Index_Str
: constant String := "Index";
104 Task_Name_Str
: constant String := "Task Name";
105 Stack_Size_Str
: constant String := "Stack Size";
106 Actual_Size_Str
: constant String := "Stack usage";
108 procedure Output_Result
109 (Result_Id
: Natural;
110 Result
: Task_Result
;
111 Max_Stack_Size_Len
: Natural;
112 Max_Actual_Use_Len
: Natural);
113 -- Prints the result on the standard output. Result Id is the number of
114 -- the result in the array, and Result the contents of the actual result.
115 -- Max_Stack_Size_Len and Max_Actual_Use_Len are used for displaying the
116 -- proper layout. They hold the maximum length of the string representing
117 -- the Stack_Size and Actual_Use values.
123 procedure Initialize
(Buffer_Size
: Natural) is
124 Stack_Size_Chars
: System
.Address
;
127 -- Initialize the buffered result array
129 Result_Array
:= new Result_Array_Type
(1 .. Buffer_Size
);
132 (Task_Name
=> (others => ASCII
.NUL
),
136 -- Set the Is_Enabled flag to true, so that the task wrapper knows that
137 -- it has to handle dynamic stack analysis
141 Stack_Size_Chars
:= System
.CRTL
.getenv
("GNAT_STACK_LIMIT" & ASCII
.NUL
);
143 -- If variable GNAT_STACK_LIMIT is set, then we will take care of the
144 -- environment task, using GNAT_STASK_LIMIT as the size of the stack.
145 -- It doesn't make sens to process the stack when no bound is set (e.g.
146 -- limit is typically up to 4 GB).
148 if Stack_Size_Chars
/= Null_Address
then
150 My_Stack_Size
: Integer;
153 My_Stack_Size
:= System
.CRTL
.atoi
(Stack_Size_Chars
) * 1024;
156 (Environment_Task_Analyzer
,
162 Fill_Stack
(Environment_Task_Analyzer
);
164 Compute_Environment_Task
:= True;
167 -- GNAT_STACK_LIMIT not set
170 Compute_Environment_Task
:= False;
178 procedure Fill_Stack
(Analyzer
: in out Stack_Analyzer
) is
180 -- Change the local variables and parameters of this function with
181 -- super-extra care. The more the stack frame size of this function is
182 -- big, the more an "instrumentation threshold at writing" error is
185 Current_Stack_Level
: aliased Integer;
187 Guard
: constant := 256;
188 -- Guard space between the Current_Stack_Level'Address and the last
189 -- allocated byte on the stack.
191 if Parameters
.Stack_Grows_Down
then
192 if Analyzer
.Stack_Base
- Stack_Address
(Analyzer
.Pattern_Size
) >
193 To_Stack_Address
(Current_Stack_Level
'Address) - Guard
195 -- No room for a pattern
197 Analyzer
.Pattern_Size
:= 0;
201 Analyzer
.Pattern_Limit
:=
202 Analyzer
.Stack_Base
- Stack_Address
(Analyzer
.Pattern_Size
);
204 if Analyzer
.Stack_Base
>
205 To_Stack_Address
(Current_Stack_Level
'Address) - Guard
207 -- Reduce pattern size to prevent local frame overwrite
209 Analyzer
.Pattern_Size
:=
210 Integer (To_Stack_Address
(Current_Stack_Level
'Address) - Guard
211 - Analyzer
.Pattern_Limit
);
214 Analyzer
.Pattern_Overlay_Address
:=
215 To_Address
(Analyzer
.Pattern_Limit
);
217 if Analyzer
.Stack_Base
+ Stack_Address
(Analyzer
.Pattern_Size
) <
218 To_Stack_Address
(Current_Stack_Level
'Address) + Guard
220 -- No room for a pattern
222 Analyzer
.Pattern_Size
:= 0;
226 Analyzer
.Pattern_Limit
:=
227 Analyzer
.Stack_Base
+ Stack_Address
(Analyzer
.Pattern_Size
);
229 if Analyzer
.Stack_Base
<
230 To_Stack_Address
(Current_Stack_Level
'Address) + Guard
232 -- Reduce pattern size to prevent local frame overwrite
234 Analyzer
.Pattern_Size
:=
236 (Analyzer
.Pattern_Limit
-
237 (To_Stack_Address
(Current_Stack_Level
'Address) + Guard
));
240 Analyzer
.Pattern_Overlay_Address
:=
241 To_Address
(Analyzer
.Pattern_Limit
-
242 Stack_Address
(Analyzer
.Pattern_Size
));
245 -- Declare and fill the pattern buffer
248 Pattern
: aliased Stack_Slots
249 (1 .. Analyzer
.Pattern_Size
/ Bytes_Per_Pattern
);
250 for Pattern
'Address use Analyzer
.Pattern_Overlay_Address
;
253 if System
.Parameters
.Stack_Grows_Down
then
254 for J
in reverse Pattern
'Range loop
255 Pattern
(J
) := Analyzer
.Pattern
;
259 for J
in Pattern
'Range loop
260 Pattern
(J
) := Analyzer
.Pattern
;
266 -------------------------
267 -- Initialize_Analyzer --
268 -------------------------
270 procedure Initialize_Analyzer
271 (Analyzer
: in out Stack_Analyzer
;
273 Stack_Size
: Natural;
274 Stack_Base
: Stack_Address
;
275 Pattern_Size
: Natural;
276 Pattern
: Interfaces
.Unsigned_32
:= 16#DEAD_BEEF#
)
279 -- Initialize the analyzer fields
281 Analyzer
.Stack_Base
:= Stack_Base
;
282 Analyzer
.Stack_Size
:= Stack_Size
;
283 Analyzer
.Pattern_Size
:= Pattern_Size
;
284 Analyzer
.Pattern
:= Pattern
;
285 Analyzer
.Result_Id
:= Next_Id
;
286 Analyzer
.Task_Name
:= (others => ' ');
288 -- Compute the task name, and truncate if bigger than Task_Name_Length
290 if Task_Name
'Length <= Task_Name_Length
then
291 Analyzer
.Task_Name
(1 .. Task_Name
'Length) := Task_Name
;
293 Analyzer
.Task_Name
:=
294 Task_Name
(Task_Name
'First ..
295 Task_Name
'First + Task_Name_Length
- 1);
298 Next_Id
:= Next_Id
+ 1;
299 end Initialize_Analyzer
;
306 (SP_Low
: Stack_Address
;
307 SP_High
: Stack_Address
) return Natural
310 if SP_Low
> SP_High
then
311 return Natural (SP_Low
- SP_High
);
313 return Natural (SP_High
- SP_Low
);
321 procedure Compute_Result
(Analyzer
: in out Stack_Analyzer
) is
323 -- Change the local variables and parameters of this function with
324 -- super-extra care. The larger the stack frame size of this function
325 -- is, the more an "instrumentation threshold at reading" error is
328 Stack
: Stack_Slots
(1 .. Analyzer
.Pattern_Size
/ Bytes_Per_Pattern
);
329 for Stack
'Address use Analyzer
.Pattern_Overlay_Address
;
332 -- Value if the pattern was not modified
334 if Parameters
.Stack_Grows_Down
then
335 Analyzer
.Topmost_Touched_Mark
:=
336 Analyzer
.Pattern_Limit
+ Stack_Address
(Analyzer
.Pattern_Size
);
338 Analyzer
.Topmost_Touched_Mark
:=
339 Analyzer
.Pattern_Limit
- Stack_Address
(Analyzer
.Pattern_Size
);
342 if Analyzer
.Pattern_Size
= 0 then
346 -- Look backward from the topmost possible end of the marked stack to
347 -- the bottom of it. The first index not equals to the patterns marks
348 -- the beginning of the used stack.
350 if System
.Parameters
.Stack_Grows_Down
then
351 for J
in Stack
'Range loop
352 if Stack
(J
) /= Analyzer
.Pattern
then
353 Analyzer
.Topmost_Touched_Mark
:=
354 To_Stack_Address
(Stack
(J
)'Address);
360 for J
in reverse Stack
'Range loop
361 if Stack
(J
) /= Analyzer
.Pattern
then
362 Analyzer
.Topmost_Touched_Mark
:=
363 To_Stack_Address
(Stack
(J
)'Address);
371 ---------------------
373 ---------------------
375 procedure Output_Result
376 (Result_Id
: Natural;
377 Result
: Task_Result
;
378 Max_Stack_Size_Len
: Natural;
379 Max_Actual_Use_Len
: Natural)
381 Result_Id_Str
: constant String := Natural'Image (Result_Id
);
382 Stack_Size_Str
: constant String := Natural'Image (Result
.Stack_Size
);
383 Actual_Use_Str
: constant String := Natural'Image (Result
.Value
);
385 Result_Id_Blanks
: constant
386 String (1 .. Index_Str
'Length - Result_Id_Str
'Length) :=
389 Stack_Size_Blanks
: constant
390 String (1 .. Max_Stack_Size_Len
- Stack_Size_Str
'Length) :=
393 Actual_Use_Blanks
: constant
394 String (1 .. Max_Actual_Use_Len
- Actual_Use_Str
'Length) :=
398 Set_Output
(Standard_Error
);
399 Put
(Result_Id_Blanks
& Natural'Image (Result_Id
));
401 Put
(Result
.Task_Name
);
403 Put
(Stack_Size_Blanks
& Stack_Size_Str
);
405 Put
(Actual_Use_Blanks
& Actual_Use_Str
);
409 ---------------------
411 ---------------------
413 procedure Output_Results
is
414 Max_Stack_Size
: Natural := 0;
415 Max_Stack_Usage
: Natural := 0;
416 Max_Stack_Size_Len
, Max_Actual_Use_Len
: Natural := 0;
418 Task_Name_Blanks
: constant
420 (1 .. Task_Name_Length
- Task_Name_Str
'Length) :=
424 Set_Output
(Standard_Error
);
426 if Compute_Environment_Task
then
427 Compute_Result
(Environment_Task_Analyzer
);
428 Report_Result
(Environment_Task_Analyzer
);
431 if Result_Array
'Length > 0 then
433 -- Computes the size of the largest strings that will get displayed,
434 -- in order to do correct column alignment.
436 for J
in Result_Array
'Range loop
437 exit when J
>= Next_Id
;
439 if Result_Array
(J
).Value
> Max_Stack_Usage
then
440 Max_Stack_Usage
:= Result_Array
(J
).Value
;
443 if Result_Array
(J
).Stack_Size
> Max_Stack_Size
then
444 Max_Stack_Size
:= Result_Array
(J
).Stack_Size
;
448 Max_Stack_Size_Len
:= Natural'Image (Max_Stack_Size
)'Length;
450 Max_Actual_Use_Len
:= Natural'Image (Max_Stack_Usage
)'Length;
452 -- Display the output header. Blanks will be added in front of the
456 Stack_Size_Blanks
: constant
457 String (1 .. Max_Stack_Size_Len
-
458 Stack_Size_Str
'Length) :=
461 Stack_Usage_Blanks
: constant
462 String (1 .. Max_Actual_Use_Len
-
463 Actual_Size_Str
'Length) :=
467 if Stack_Size_Str
'Length > Max_Stack_Size_Len
then
468 Max_Stack_Size_Len
:= Stack_Size_Str
'Length;
471 if Actual_Size_Str
'Length > Max_Actual_Use_Len
then
472 Max_Actual_Use_Len
:= Actual_Size_Str
'Length;
476 (Index_Str
& " | " & Task_Name_Str
& Task_Name_Blanks
& " | "
477 & Stack_Size_Str
& Stack_Size_Blanks
& " | "
478 & Stack_Usage_Blanks
& Actual_Size_Str
);
483 -- Now display the individual results
485 for J
in Result_Array
'Range loop
486 exit when J
>= Next_Id
;
488 (J
, Result_Array
(J
), Max_Stack_Size_Len
, Max_Actual_Use_Len
);
491 -- Case of no result stored, still display the labels
495 (Index_Str
& " | " & Task_Name_Str
& Task_Name_Blanks
& " | "
496 & Stack_Size_Str
& " | " & Actual_Size_Str
);
505 procedure Report_Result
(Analyzer
: Stack_Analyzer
) is
506 Result
: Task_Result
:= (Task_Name
=> Analyzer
.Task_Name
,
507 Stack_Size
=> Analyzer
.Stack_Size
,
510 if Analyzer
.Pattern_Size
= 0 then
512 -- If we have that result, it means that we didn't do any computation
513 -- at all (i.e. we used at least everything (and possibly more).
515 Result
.Value
:= Analyzer
.Stack_Size
;
518 Result
.Value
:= Stack_Size
(Analyzer
.Topmost_Touched_Mark
,
519 Analyzer
.Stack_Base
);
522 if Analyzer
.Result_Id
in Result_Array
'Range then
524 -- If the result can be stored, then store it in Result_Array
526 Result_Array
(Analyzer
.Result_Id
) := Result
;
529 -- If the result cannot be stored, then we display it right away
532 Result_Str_Len
: constant Natural :=
533 Natural'Image (Result
.Value
)'Length;
534 Size_Str_Len
: constant Natural :=
535 Natural'Image (Analyzer
.Stack_Size
)'Length;
537 Max_Stack_Size_Len
: Natural;
538 Max_Actual_Use_Len
: Natural;
541 -- Take either the label size or the number image size for the
542 -- size of the column "Stack Size".
544 Max_Stack_Size_Len
:=
545 (if Size_Str_Len
> Stack_Size_Str
'Length
547 else Stack_Size_Str
'Length);
549 -- Take either the label size or the number image size for the
550 -- size of the column "Stack Usage".
552 Max_Actual_Use_Len
:=
553 (if Result_Str_Len
> Actual_Size_Str
'Length
555 else Actual_Size_Str
'Length);
566 end System
.Stack_Usage
;