comparison gcc/ada/doc/gnat_ugn/gnat_and_program_execution.rst @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 .. role:: switch(samp)
2
3 .. |with| replace:: *with*
4 .. |withs| replace:: *with*\ s
5 .. |withed| replace:: *with*\ ed
6 .. |withing| replace:: *with*\ ing
7
8 .. -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
9
10
11 .. _GNAT_and_Program_Execution:
12
13 **************************
14 GNAT and Program Execution
15 **************************
16
17 This chapter covers several topics:
18
19 * `Running and Debugging Ada Programs`_
20 * `Code Coverage and Profiling`_
21 * `Improving Performance`_
22 * `Overflow Check Handling in GNAT`_
23 * `Performing Dimensionality Analysis in GNAT`_
24 * `Stack Related Facilities`_
25 * `Memory Management Issues`_
26
27 .. _Running_and_Debugging_Ada_Programs:
28
29 Running and Debugging Ada Programs
30 ==================================
31
32 .. index:: Debugging
33
34 This section discusses how to debug Ada programs.
35
36 An incorrect Ada program may be handled in three ways by the GNAT compiler:
37
38 * The illegality may be a violation of the static semantics of Ada. In
39 that case GNAT diagnoses the constructs in the program that are illegal.
40 It is then a straightforward matter for the user to modify those parts of
41 the program.
42
43 * The illegality may be a violation of the dynamic semantics of Ada. In
44 that case the program compiles and executes, but may generate incorrect
45 results, or may terminate abnormally with some exception.
46
47 * When presented with a program that contains convoluted errors, GNAT
48 itself may terminate abnormally without providing full diagnostics on
49 the incorrect user program.
50
51 .. index:: Debugger
52
53 .. index:: ! gdb
54
55 .. _The_GNAT_Debugger_GDB:
56
57 The GNAT Debugger GDB
58 ---------------------
59
60 ``GDB`` is a general purpose, platform-independent debugger that
61 can be used to debug mixed-language programs compiled with ``gcc``,
62 and in particular is capable of debugging Ada programs compiled with
63 GNAT. The latest versions of ``GDB`` are Ada-aware and can handle
64 complex Ada data structures.
65
66 See :title:`Debugging with GDB`,
67 for full details on the usage of ``GDB``, including a section on
68 its usage on programs. This manual should be consulted for full
69 details. The section that follows is a brief introduction to the
70 philosophy and use of ``GDB``.
71
72 When GNAT programs are compiled, the compiler optionally writes debugging
73 information into the generated object file, including information on
74 line numbers, and on declared types and variables. This information is
75 separate from the generated code. It makes the object files considerably
76 larger, but it does not add to the size of the actual executable that
77 will be loaded into memory, and has no impact on run-time performance. The
78 generation of debug information is triggered by the use of the
79 :switch:`-g` switch in the ``gcc`` or ``gnatmake`` command
80 used to carry out the compilations. It is important to emphasize that
81 the use of these options does not change the generated code.
82
83 The debugging information is written in standard system formats that
84 are used by many tools, including debuggers and profilers. The format
85 of the information is typically designed to describe C types and
86 semantics, but GNAT implements a translation scheme which allows full
87 details about Ada types and variables to be encoded into these
88 standard C formats. Details of this encoding scheme may be found in
89 the file exp_dbug.ads in the GNAT source distribution. However, the
90 details of this encoding are, in general, of no interest to a user,
91 since ``GDB`` automatically performs the necessary decoding.
92
93 When a program is bound and linked, the debugging information is
94 collected from the object files, and stored in the executable image of
95 the program. Again, this process significantly increases the size of
96 the generated executable file, but it does not increase the size of
97 the executable program itself. Furthermore, if this program is run in
98 the normal manner, it runs exactly as if the debug information were
99 not present, and takes no more actual memory.
100
101 However, if the program is run under control of ``GDB``, the
102 debugger is activated. The image of the program is loaded, at which
103 point it is ready to run. If a run command is given, then the program
104 will run exactly as it would have if ``GDB`` were not present. This
105 is a crucial part of the ``GDB`` design philosophy. ``GDB`` is
106 entirely non-intrusive until a breakpoint is encountered. If no
107 breakpoint is ever hit, the program will run exactly as it would if no
108 debugger were present. When a breakpoint is hit, ``GDB`` accesses
109 the debugging information and can respond to user commands to inspect
110 variables, and more generally to report on the state of execution.
111
112 .. _Running_GDB:
113
114 Running GDB
115 -----------
116
117 This section describes how to initiate the debugger.
118
119 The debugger can be launched from a ``GPS`` menu or
120 directly from the command line. The description below covers the latter use.
121 All the commands shown can be used in the ``GPS`` debug console window,
122 but there are usually more GUI-based ways to achieve the same effect.
123
124 The command to run ``GDB`` is
125
126 ::
127
128 $ gdb program
129
130 where ``program`` is the name of the executable file. This
131 activates the debugger and results in a prompt for debugger commands.
132 The simplest command is simply ``run``, which causes the program to run
133 exactly as if the debugger were not present. The following section
134 describes some of the additional commands that can be given to ``GDB``.
135
136
137 .. _Introduction_to_GDB_Commands:
138
139 Introduction to GDB Commands
140 ----------------------------
141
142 ``GDB`` contains a large repertoire of commands.
143 See :title:`Debugging with GDB` for extensive documentation on the use
144 of these commands, together with examples of their use. Furthermore,
145 the command *help* invoked from within GDB activates a simple help
146 facility which summarizes the available commands and their options.
147 In this section we summarize a few of the most commonly
148 used commands to give an idea of what ``GDB`` is about. You should create
149 a simple program with debugging information and experiment with the use of
150 these ``GDB`` commands on the program as you read through the
151 following section.
152
153 * :samp:`set args {arguments}`
154 The *arguments* list above is a list of arguments to be passed to
155 the program on a subsequent run command, just as though the arguments
156 had been entered on a normal invocation of the program. The ``set args``
157 command is not needed if the program does not require arguments.
158
159
160 * :samp:`run`
161 The ``run`` command causes execution of the program to start from
162 the beginning. If the program is already running, that is to say if
163 you are currently positioned at a breakpoint, then a prompt will ask
164 for confirmation that you want to abandon the current execution and
165 restart.
166
167
168 * :samp:`breakpoint {location}`
169 The breakpoint command sets a breakpoint, that is to say a point at which
170 execution will halt and ``GDB`` will await further
171 commands. *location* is
172 either a line number within a file, given in the format ``file:linenumber``,
173 or it is the name of a subprogram. If you request that a breakpoint be set on
174 a subprogram that is overloaded, a prompt will ask you to specify on which of
175 those subprograms you want to breakpoint. You can also
176 specify that all of them should be breakpointed. If the program is run
177 and execution encounters the breakpoint, then the program
178 stops and ``GDB`` signals that the breakpoint was encountered by
179 printing the line of code before which the program is halted.
180
181
182 * :samp:`catch exception {name}`
183 This command causes the program execution to stop whenever exception
184 ``name`` is raised. If ``name`` is omitted, then the execution is
185 suspended when any exception is raised.
186
187
188 * :samp:`print {expression}`
189 This will print the value of the given expression. Most simple
190 Ada expression formats are properly handled by ``GDB``, so the expression
191 can contain function calls, variables, operators, and attribute references.
192
193
194 * :samp:`continue`
195 Continues execution following a breakpoint, until the next breakpoint or the
196 termination of the program.
197
198
199 * :samp:`step`
200 Executes a single line after a breakpoint. If the next statement
201 is a subprogram call, execution continues into (the first statement of)
202 the called subprogram.
203
204
205 * :samp:`next`
206 Executes a single line. If this line is a subprogram call, executes and
207 returns from the call.
208
209
210 * :samp:`list`
211 Lists a few lines around the current source location. In practice, it
212 is usually more convenient to have a separate edit window open with the
213 relevant source file displayed. Successive applications of this command
214 print subsequent lines. The command can be given an argument which is a
215 line number, in which case it displays a few lines around the specified one.
216
217
218 * :samp:`backtrace`
219 Displays a backtrace of the call chain. This command is typically
220 used after a breakpoint has occurred, to examine the sequence of calls that
221 leads to the current breakpoint. The display includes one line for each
222 activation record (frame) corresponding to an active subprogram.
223
224
225 * :samp:`up`
226 At a breakpoint, ``GDB`` can display the values of variables local
227 to the current frame. The command ``up`` can be used to
228 examine the contents of other active frames, by moving the focus up
229 the stack, that is to say from callee to caller, one frame at a time.
230
231
232 * :samp:`down`
233 Moves the focus of ``GDB`` down from the frame currently being
234 examined to the frame of its callee (the reverse of the previous command),
235
236
237 * :samp:`frame {n}`
238 Inspect the frame with the given number. The value 0 denotes the frame
239 of the current breakpoint, that is to say the top of the call stack.
240
241
242 * :samp:`kill`
243 Kills the child process in which the program is running under GDB.
244 This may be useful for several purposes:
245
246 * It allows you to recompile and relink your program, since on many systems
247 you cannot regenerate an executable file while it is running in a process.
248
249 * You can run your program outside the debugger, on systems that do not
250 permit executing a program outside GDB while breakpoints are set
251 within GDB.
252
253 * It allows you to debug a core dump rather than a running process.
254
255 The above list is a very short introduction to the commands that
256 ``GDB`` provides. Important additional capabilities, including conditional
257 breakpoints, the ability to execute command sequences on a breakpoint,
258 the ability to debug at the machine instruction level and many other
259 features are described in detail in :title:`Debugging with GDB`.
260 Note that most commands can be abbreviated
261 (for example, c for continue, bt for backtrace).
262
263
264 .. _Using_Ada_Expressions:
265
266 Using Ada Expressions
267 ---------------------
268
269 .. index:: Ada expressions (in gdb)
270
271 ``GDB`` supports a fairly large subset of Ada expression syntax, with some
272 extensions. The philosophy behind the design of this subset is
273
274 * That ``GDB`` should provide basic literals and access to operations for
275 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
276 leaving more sophisticated computations to subprograms written into the
277 program (which therefore may be called from ``GDB``).
278
279 * That type safety and strict adherence to Ada language restrictions
280 are not particularly relevant in a debugging context.
281
282 * That brevity is important to the ``GDB`` user.
283
284 Thus, for brevity, the debugger acts as if there were
285 implicit ``with`` and ``use`` clauses in effect for all user-written
286 packages, thus making it unnecessary to fully qualify most names with
287 their packages, regardless of context. Where this causes ambiguity,
288 ``GDB`` asks the user's intent.
289
290 For details on the supported Ada syntax, see :title:`Debugging with GDB`.
291
292
293 .. _Calling_User-Defined_Subprograms:
294
295 Calling User-Defined Subprograms
296 --------------------------------
297
298 An important capability of ``GDB`` is the ability to call user-defined
299 subprograms while debugging. This is achieved simply by entering
300 a subprogram call statement in the form:
301
302 ::
303
304 call subprogram-name (parameters)
305
306 The keyword ``call`` can be omitted in the normal case where the
307 ``subprogram-name`` does not coincide with any of the predefined
308 ``GDB`` commands.
309
310 The effect is to invoke the given subprogram, passing it the
311 list of parameters that is supplied. The parameters can be expressions and
312 can include variables from the program being debugged. The
313 subprogram must be defined
314 at the library level within your program, and ``GDB`` will call the
315 subprogram within the environment of your program execution (which
316 means that the subprogram is free to access or even modify variables
317 within your program).
318
319 The most important use of this facility is in allowing the inclusion of
320 debugging routines that are tailored to particular data structures
321 in your program. Such debugging routines can be written to provide a suitably
322 high-level description of an abstract type, rather than a low-level dump
323 of its physical layout. After all, the standard
324 ``GDB print`` command only knows the physical layout of your
325 types, not their abstract meaning. Debugging routines can provide information
326 at the desired semantic level and are thus enormously useful.
327
328 For example, when debugging GNAT itself, it is crucial to have access to
329 the contents of the tree nodes used to represent the program internally.
330 But tree nodes are represented simply by an integer value (which in turn
331 is an index into a table of nodes).
332 Using the ``print`` command on a tree node would simply print this integer
333 value, which is not very useful. But the PN routine (defined in file
334 treepr.adb in the GNAT sources) takes a tree node as input, and displays
335 a useful high level representation of the tree node, which includes the
336 syntactic category of the node, its position in the source, the integers
337 that denote descendant nodes and parent node, as well as varied
338 semantic information. To study this example in more detail, you might want to
339 look at the body of the PN procedure in the stated file.
340
341 Another useful application of this capability is to deal with situations of
342 complex data which are not handled suitably by GDB. For example, if you specify
343 Convention Fortran for a multi-dimensional array, GDB does not know that
344 the ordering of array elements has been switched and will not properly
345 address the array elements. In such a case, instead of trying to print the
346 elements directly from GDB, you can write a callable procedure that prints
347 the elements in the desired format.
348
349
350 .. _Using_the_Next_Command_in_a_Function:
351
352 Using the *next* Command in a Function
353 --------------------------------------
354
355 When you use the ``next`` command in a function, the current source
356 location will advance to the next statement as usual. A special case
357 arises in the case of a ``return`` statement.
358
359 Part of the code for a return statement is the 'epilogue' of the function.
360 This is the code that returns to the caller. There is only one copy of
361 this epilogue code, and it is typically associated with the last return
362 statement in the function if there is more than one return. In some
363 implementations, this epilogue is associated with the first statement
364 of the function.
365
366 The result is that if you use the ``next`` command from a return
367 statement that is not the last return statement of the function you
368 may see a strange apparent jump to the last return statement or to
369 the start of the function. You should simply ignore this odd jump.
370 The value returned is always that from the first return statement
371 that was stepped through.
372
373
374 .. _Stopping_When_Ada_Exceptions_Are_Raised:
375
376 Stopping When Ada Exceptions Are Raised
377 ---------------------------------------
378
379 .. index:: Exceptions (in gdb)
380
381 You can set catchpoints that stop the program execution when your program
382 raises selected exceptions.
383
384
385 * :samp:`catch exception`
386 Set a catchpoint that stops execution whenever (any task in the) program
387 raises any exception.
388
389
390 * :samp:`catch exception {name}`
391 Set a catchpoint that stops execution whenever (any task in the) program
392 raises the exception *name*.
393
394
395 * :samp:`catch exception unhandled`
396 Set a catchpoint that stops executing whenever (any task in the) program
397 raises an exception for which there is no handler.
398
399
400 * :samp:`info exceptions`, :samp:`info exceptions {regexp}`
401 The ``info exceptions`` command permits the user to examine all defined
402 exceptions within Ada programs. With a regular expression, *regexp*, as
403 argument, prints out only those exceptions whose name matches *regexp*.
404
405
406 .. index:: Tasks (in gdb)
407
408 .. _Ada_Tasks:
409
410 Ada Tasks
411 ---------
412
413 ``GDB`` allows the following task-related commands:
414
415
416 * :samp:`info tasks`
417 This command shows a list of current Ada tasks, as in the following example:
418
419 ::
420
421 (gdb) info tasks
422 ID TID P-ID Thread Pri State Name
423 1 8088000 0 807e000 15 Child Activation Wait main_task
424 2 80a4000 1 80ae000 15 Accept/Select Wait b
425 3 809a800 1 80a4800 15 Child Activation Wait a
426 * 4 80ae800 3 80b8000 15 Running c
427
428
429 In this listing, the asterisk before the first task indicates it to be the
430 currently running task. The first column lists the task ID that is used
431 to refer to tasks in the following commands.
432
433
434 .. index:: Breakpoints and tasks
435
436 * ``break``*linespec* ``task`` *taskid*, ``break`` *linespec* ``task`` *taskid* ``if`` ...
437
438 These commands are like the ``break ... thread ...``.
439 *linespec* specifies source lines.
440
441 Use the qualifier :samp:`task {taskid}` with a breakpoint command
442 to specify that you only want ``GDB`` to stop the program when a
443 particular Ada task reaches this breakpoint. *taskid* is one of the
444 numeric task identifiers assigned by ``GDB``, shown in the first
445 column of the ``info tasks`` display.
446
447 If you do not specify :samp:`task {taskid}` when you set a
448 breakpoint, the breakpoint applies to *all* tasks of your
449 program.
450
451 You can use the ``task`` qualifier on conditional breakpoints as
452 well; in this case, place :samp:`task {taskid}` before the
453 breakpoint condition (before the ``if``).
454
455 .. index:: Task switching (in gdb)
456
457 * :samp:`task {taskno}`
458
459 This command allows switching to the task referred by *taskno*. In
460 particular, this allows browsing of the backtrace of the specified
461 task. It is advisable to switch back to the original task before
462 continuing execution otherwise the scheduling of the program may be
463 perturbed.
464
465 For more detailed information on the tasking support,
466 see :title:`Debugging with GDB`.
467
468
469 .. index:: Debugging Generic Units
470 .. index:: Generics
471
472 .. _Debugging_Generic_Units:
473
474 Debugging Generic Units
475 -----------------------
476
477 GNAT always uses code expansion for generic instantiation. This means that
478 each time an instantiation occurs, a complete copy of the original code is
479 made, with appropriate substitutions of formals by actuals.
480
481 It is not possible to refer to the original generic entities in
482 ``GDB``, but it is always possible to debug a particular instance of
483 a generic, by using the appropriate expanded names. For example, if we have
484
485 .. code-block:: ada
486
487 procedure g is
488
489 generic package k is
490 procedure kp (v1 : in out integer);
491 end k;
492
493 package body k is
494 procedure kp (v1 : in out integer) is
495 begin
496 v1 := v1 + 1;
497 end kp;
498 end k;
499
500 package k1 is new k;
501 package k2 is new k;
502
503 var : integer := 1;
504
505 begin
506 k1.kp (var);
507 k2.kp (var);
508 k1.kp (var);
509 k2.kp (var);
510 end;
511
512 Then to break on a call to procedure kp in the k2 instance, simply
513 use the command:
514
515 ::
516
517 (gdb) break g.k2.kp
518
519 When the breakpoint occurs, you can step through the code of the
520 instance in the normal manner and examine the values of local variables, as for
521 other units.
522
523
524 .. index:: Remote Debugging with gdbserver
525
526 .. _Remote_Debugging_with_gdbserver:
527
528 Remote Debugging with gdbserver
529 -------------------------------
530
531 On platforms where gdbserver is supported, it is possible to use this tool
532 to debug your application remotely. This can be useful in situations
533 where the program needs to be run on a target host that is different
534 from the host used for development, particularly when the target has
535 a limited amount of resources (either CPU and/or memory).
536
537 To do so, start your program using gdbserver on the target machine.
538 gdbserver then automatically suspends the execution of your program
539 at its entry point, waiting for a debugger to connect to it. The
540 following commands starts an application and tells gdbserver to
541 wait for a connection with the debugger on localhost port 4444.
542
543
544 ::
545
546 $ gdbserver localhost:4444 program
547 Process program created; pid = 5685
548 Listening on port 4444
549
550 Once gdbserver has started listening, we can tell the debugger to establish
551 a connection with this gdbserver, and then start the same debugging session
552 as if the program was being debugged on the same host, directly under
553 the control of GDB.
554
555 ::
556
557 $ gdb program
558 (gdb) target remote targethost:4444
559 Remote debugging using targethost:4444
560 0x00007f29936d0af0 in ?? () from /lib64/ld-linux-x86-64.so.
561 (gdb) b foo.adb:3
562 Breakpoint 1 at 0x401f0c: file foo.adb, line 3.
563 (gdb) continue
564 Continuing.
565
566 Breakpoint 1, foo () at foo.adb:4
567 4 end foo;
568
569 It is also possible to use gdbserver to attach to an already running
570 program, in which case the execution of that program is simply suspended
571 until the connection between the debugger and gdbserver is established.
572
573 For more information on how to use gdbserver, see the *Using the gdbserver Program*
574 section in :title:`Debugging with GDB`.
575 GNAT provides support for gdbserver on x86-linux, x86-windows and x86_64-linux.
576
577
578 .. index:: Abnormal Termination or Failure to Terminate
579
580 .. _GNAT_Abnormal_Termination_or_Failure_to_Terminate:
581
582 GNAT Abnormal Termination or Failure to Terminate
583 -------------------------------------------------
584
585 When presented with programs that contain serious errors in syntax
586 or semantics,
587 GNAT may on rare occasions experience problems in operation, such
588 as aborting with a
589 segmentation fault or illegal memory access, raising an internal
590 exception, terminating abnormally, or failing to terminate at all.
591 In such cases, you can activate
592 various features of GNAT that can help you pinpoint the construct in your
593 program that is the likely source of the problem.
594
595 The following strategies are presented in increasing order of
596 difficulty, corresponding to your experience in using GNAT and your
597 familiarity with compiler internals.
598
599 * Run ``gcc`` with the :switch:`-gnatf`. This first
600 switch causes all errors on a given line to be reported. In its absence,
601 only the first error on a line is displayed.
602
603 The :switch:`-gnatdO` switch causes errors to be displayed as soon as they
604 are encountered, rather than after compilation is terminated. If GNAT
605 terminates prematurely or goes into an infinite loop, the last error
606 message displayed may help to pinpoint the culprit.
607
608 * Run ``gcc`` with the :switch:`-v` (verbose) switch. In this
609 mode, ``gcc`` produces ongoing information about the progress of the
610 compilation and provides the name of each procedure as code is
611 generated. This switch allows you to find which Ada procedure was being
612 compiled when it encountered a code generation problem.
613
614 .. index:: -gnatdc switch
615
616 * Run ``gcc`` with the :switch:`-gnatdc` switch. This is a GNAT specific
617 switch that does for the front-end what :switch:`-v` does
618 for the back end. The system prints the name of each unit,
619 either a compilation unit or nested unit, as it is being analyzed.
620
621 * Finally, you can start
622 ``gdb`` directly on the ``gnat1`` executable. ``gnat1`` is the
623 front-end of GNAT, and can be run independently (normally it is just
624 called from ``gcc``). You can use ``gdb`` on ``gnat1`` as you
625 would on a C program (but :ref:`The_GNAT_Debugger_GDB` for caveats). The
626 ``where`` command is the first line of attack; the variable
627 ``lineno`` (seen by ``print lineno``), used by the second phase of
628 ``gnat1`` and by the ``gcc`` backend, indicates the source line at
629 which the execution stopped, and ``input_file name`` indicates the name of
630 the source file.
631
632
633 .. _Naming_Conventions_for_GNAT_Source_Files:
634
635 Naming Conventions for GNAT Source Files
636 ----------------------------------------
637
638 In order to examine the workings of the GNAT system, the following
639 brief description of its organization may be helpful:
640
641 * Files with prefix :file:`sc` contain the lexical scanner.
642
643 * All files prefixed with :file:`par` are components of the parser. The
644 numbers correspond to chapters of the Ada Reference Manual. For example,
645 parsing of select statements can be found in :file:`par-ch9.adb`.
646
647 * All files prefixed with :file:`sem` perform semantic analysis. The
648 numbers correspond to chapters of the Ada standard. For example, all
649 issues involving context clauses can be found in :file:`sem_ch10.adb`. In
650 addition, some features of the language require sufficient special processing
651 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
652 dynamic dispatching, etc.
653
654 * All files prefixed with :file:`exp` perform normalization and
655 expansion of the intermediate representation (abstract syntax tree, or AST).
656 these files use the same numbering scheme as the parser and semantics files.
657 For example, the construction of record initialization procedures is done in
658 :file:`exp_ch3.adb`.
659
660 * The files prefixed with :file:`bind` implement the binder, which
661 verifies the consistency of the compilation, determines an order of
662 elaboration, and generates the bind file.
663
664 * The files :file:`atree.ads` and :file:`atree.adb` detail the low-level
665 data structures used by the front-end.
666
667 * The files :file:`sinfo.ads` and :file:`sinfo.adb` detail the structure of
668 the abstract syntax tree as produced by the parser.
669
670 * The files :file:`einfo.ads` and :file:`einfo.adb` detail the attributes of
671 all entities, computed during semantic analysis.
672
673 * Library management issues are dealt with in files with prefix
674 :file:`lib`.
675
676 .. index:: Annex A (in Ada Reference Manual)
677
678 * Ada files with the prefix :file:`a-` are children of ``Ada``, as
679 defined in Annex A.
680
681 .. index:: Annex B (in Ada reference Manual)
682
683 * Files with prefix :file:`i-` are children of ``Interfaces``, as
684 defined in Annex B.
685
686 .. index:: System (package in Ada Reference Manual)
687
688 * Files with prefix :file:`s-` are children of ``System``. This includes
689 both language-defined children and GNAT run-time routines.
690
691 .. index:: GNAT (package)
692
693 * Files with prefix :file:`g-` are children of ``GNAT``. These are useful
694 general-purpose packages, fully documented in their specs. All
695 the other :file:`.c` files are modifications of common ``gcc`` files.
696
697
698 .. _Getting_Internal_Debugging_Information:
699
700 Getting Internal Debugging Information
701 --------------------------------------
702
703 Most compilers have internal debugging switches and modes. GNAT
704 does also, except GNAT internal debugging switches and modes are not
705 secret. A summary and full description of all the compiler and binder
706 debug flags are in the file :file:`debug.adb`. You must obtain the
707 sources of the compiler to see the full detailed effects of these flags.
708
709 The switches that print the source of the program (reconstructed from
710 the internal tree) are of general interest for user programs, as are the
711 options to print
712 the full internal tree, and the entity table (the symbol table
713 information). The reconstructed source provides a readable version of the
714 program after the front-end has completed analysis and expansion,
715 and is useful when studying the performance of specific constructs.
716 For example, constraint checks are indicated, complex aggregates
717 are replaced with loops and assignments, and tasking primitives
718 are replaced with run-time calls.
719
720
721 .. index:: traceback
722 .. index:: stack traceback
723 .. index:: stack unwinding
724
725 .. _Stack_Traceback:
726
727 Stack Traceback
728 ---------------
729
730 Traceback is a mechanism to display the sequence of subprogram calls that
731 leads to a specified execution point in a program. Often (but not always)
732 the execution point is an instruction at which an exception has been raised.
733 This mechanism is also known as *stack unwinding* because it obtains
734 its information by scanning the run-time stack and recovering the activation
735 records of all active subprograms. Stack unwinding is one of the most
736 important tools for program debugging.
737
738 The first entry stored in traceback corresponds to the deepest calling level,
739 that is to say the subprogram currently executing the instruction
740 from which we want to obtain the traceback.
741
742 Note that there is no runtime performance penalty when stack traceback
743 is enabled, and no exception is raised during program execution.
744
745 .. index:: traceback, non-symbolic
746
747 .. _Non-Symbolic_Traceback:
748
749 Non-Symbolic Traceback
750 ^^^^^^^^^^^^^^^^^^^^^^
751
752 Note: this feature is not supported on all platforms. See
753 :samp:`GNAT.Traceback` spec in :file:`g-traceb.ads`
754 for a complete list of supported platforms.
755
756 .. rubric:: Tracebacks From an Unhandled Exception
757
758 A runtime non-symbolic traceback is a list of addresses of call instructions.
759 To enable this feature you must use the :switch:`-E`
760 ``gnatbind`` option. With this option a stack traceback is stored as part
761 of exception information. You can retrieve this information using the
762 ``addr2line`` tool.
763
764 Here is a simple example:
765
766 .. code-block:: ada
767
768 procedure STB is
769
770 procedure P1 is
771 begin
772 raise Constraint_Error;
773 end P1;
774
775 procedure P2 is
776 begin
777 P1;
778 end P2;
779
780 begin
781 P2;
782 end STB;
783
784 ::
785
786 $ gnatmake stb -bargs -E
787 $ stb
788
789 Execution terminated by unhandled exception
790 Exception name: CONSTRAINT_ERROR
791 Message: stb.adb:5
792 Call stack traceback locations:
793 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
794
795 As we see the traceback lists a sequence of addresses for the unhandled
796 exception ``CONSTRAINT_ERROR`` raised in procedure P1. It is easy to
797 guess that this exception come from procedure P1. To translate these
798 addresses into the source lines where the calls appear, the
799 ``addr2line`` tool, described below, is invaluable. The use of this tool
800 requires the program to be compiled with debug information.
801
802 ::
803
804 $ gnatmake -g stb -bargs -E
805 $ stb
806
807 Execution terminated by unhandled exception
808 Exception name: CONSTRAINT_ERROR
809 Message: stb.adb:5
810 Call stack traceback locations:
811 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
812
813 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
814 0x4011f1 0x77e892a4
815
816 00401373 at d:/stb/stb.adb:5
817 0040138B at d:/stb/stb.adb:10
818 0040139C at d:/stb/stb.adb:14
819 00401335 at d:/stb/b~stb.adb:104
820 004011C4 at /build/.../crt1.c:200
821 004011F1 at /build/.../crt1.c:222
822 77E892A4 in ?? at ??:0
823
824 The ``addr2line`` tool has several other useful options:
825
826 ======================== ========================================================
827 :samp:`--functions` to get the function name corresponding to any location
828 :samp:`--demangle=gnat` to use the gnat decoding mode for the function names.
829 Note that for binutils version 2.9.x the option is
830 simply :samp:`--demangle`.
831 ======================== ========================================================
832
833 ::
834
835 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
836 0x40139c 0x401335 0x4011c4 0x4011f1
837
838 00401373 in stb.p1 at d:/stb/stb.adb:5
839 0040138B in stb.p2 at d:/stb/stb.adb:10
840 0040139C in stb at d:/stb/stb.adb:14
841 00401335 in main at d:/stb/b~stb.adb:104
842 004011C4 in <__mingw_CRTStartup> at /build/.../crt1.c:200
843 004011F1 in <mainCRTStartup> at /build/.../crt1.c:222
844
845 From this traceback we can see that the exception was raised in
846 :file:`stb.adb` at line 5, which was reached from a procedure call in
847 :file:`stb.adb` at line 10, and so on. The :file:`b~std.adb` is the binder file,
848 which contains the call to the main program.
849 :ref:`Running_gnatbind`. The remaining entries are assorted runtime routines,
850 and the output will vary from platform to platform.
851
852 It is also possible to use ``GDB`` with these traceback addresses to debug
853 the program. For example, we can break at a given code location, as reported
854 in the stack traceback:
855
856 ::
857
858 $ gdb -nw stb
859
860 Furthermore, this feature is not implemented inside Windows DLL. Only
861 the non-symbolic traceback is reported in this case.
862
863 ::
864
865 (gdb) break *0x401373
866 Breakpoint 1 at 0x401373: file stb.adb, line 5.
867
868 It is important to note that the stack traceback addresses
869 do not change when debug information is included. This is particularly useful
870 because it makes it possible to release software without debug information (to
871 minimize object size), get a field report that includes a stack traceback
872 whenever an internal bug occurs, and then be able to retrieve the sequence
873 of calls with the same program compiled with debug information.
874
875
876 .. rubric:: Tracebacks From Exception Occurrences
877
878 Non-symbolic tracebacks are obtained by using the :switch:`-E` binder argument.
879 The stack traceback is attached to the exception information string, and can
880 be retrieved in an exception handler within the Ada program, by means of the
881 Ada facilities defined in ``Ada.Exceptions``. Here is a simple example:
882
883 .. code-block:: ada
884
885 with Ada.Text_IO;
886 with Ada.Exceptions;
887
888 procedure STB is
889
890 use Ada;
891 use Ada.Exceptions;
892
893 procedure P1 is
894 K : Positive := 1;
895 begin
896 K := K - 1;
897 exception
898 when E : others =>
899 Text_IO.Put_Line (Exception_Information (E));
900 end P1;
901
902 procedure P2 is
903 begin
904 P1;
905 end P2;
906
907 begin
908 P2;
909 end STB;
910
911 This program will output:
912
913 ::
914
915 $ stb
916
917 Exception name: CONSTRAINT_ERROR
918 Message: stb.adb:12
919 Call stack traceback locations:
920 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
921
922
923 .. rubric:: Tracebacks From Anywhere in a Program
924
925 It is also possible to retrieve a stack traceback from anywhere in a
926 program. For this you need to
927 use the ``GNAT.Traceback`` API. This package includes a procedure called
928 ``Call_Chain`` that computes a complete stack traceback, as well as useful
929 display procedures described below. It is not necessary to use the
930 :switch:`-E` ``gnatbind`` option in this case, because the stack traceback mechanism
931 is invoked explicitly.
932
933 In the following example we compute a traceback at a specific location in
934 the program, and we display it using ``GNAT.Debug_Utilities.Image`` to
935 convert addresses to strings:
936
937
938 .. code-block:: ada
939
940 with Ada.Text_IO;
941 with GNAT.Traceback;
942 with GNAT.Debug_Utilities;
943
944 procedure STB is
945
946 use Ada;
947 use GNAT;
948 use GNAT.Traceback;
949
950 procedure P1 is
951 TB : Tracebacks_Array (1 .. 10);
952 -- We are asking for a maximum of 10 stack frames.
953 Len : Natural;
954 -- Len will receive the actual number of stack frames returned.
955 begin
956 Call_Chain (TB, Len);
957
958 Text_IO.Put ("In STB.P1 : ");
959
960 for K in 1 .. Len loop
961 Text_IO.Put (Debug_Utilities.Image (TB (K)));
962 Text_IO.Put (' ');
963 end loop;
964
965 Text_IO.New_Line;
966 end P1;
967
968 procedure P2 is
969 begin
970 P1;
971 end P2;
972
973 begin
974 P2;
975 end STB;
976
977 ::
978
979 $ gnatmake -g stb
980 $ stb
981
982 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
983 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
984
985
986 You can then get further information by invoking the ``addr2line``
987 tool as described earlier (note that the hexadecimal addresses
988 need to be specified in C format, with a leading '0x').
989
990 .. index:: traceback, symbolic
991
992 .. _Symbolic_Traceback:
993
994 Symbolic Traceback
995 ^^^^^^^^^^^^^^^^^^
996
997 A symbolic traceback is a stack traceback in which procedure names are
998 associated with each code location.
999
1000 Note that this feature is not supported on all platforms. See
1001 :samp:`GNAT.Traceback.Symbolic` spec in :file:`g-trasym.ads` for a complete
1002 list of currently supported platforms.
1003
1004 Note that the symbolic traceback requires that the program be compiled
1005 with debug information. If it is not compiled with debug information
1006 only the non-symbolic information will be valid.
1007
1008
1009 .. rubric:: Tracebacks From Exception Occurrences
1010
1011 Here is an example:
1012
1013 .. code-block:: ada
1014
1015 with Ada.Text_IO;
1016 with GNAT.Traceback.Symbolic;
1017
1018 procedure STB is
1019
1020 procedure P1 is
1021 begin
1022 raise Constraint_Error;
1023 end P1;
1024
1025 procedure P2 is
1026 begin
1027 P1;
1028 end P2;
1029
1030 procedure P3 is
1031 begin
1032 P2;
1033 end P3;
1034
1035 begin
1036 P3;
1037 exception
1038 when E : others =>
1039 Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
1040 end STB;
1041
1042 ::
1043
1044 $ gnatmake -g .\stb -bargs -E
1045 $ stb
1046
1047 0040149F in stb.p1 at stb.adb:8
1048 004014B7 in stb.p2 at stb.adb:13
1049 004014CF in stb.p3 at stb.adb:18
1050 004015DD in ada.stb at stb.adb:22
1051 00401461 in main at b~stb.adb:168
1052 004011C4 in __mingw_CRTStartup at crt1.c:200
1053 004011F1 in mainCRTStartup at crt1.c:222
1054 77E892A4 in ?? at ??:0
1055
1056 In the above example the ``.\`` syntax in the ``gnatmake`` command
1057 is currently required by ``addr2line`` for files that are in
1058 the current working directory.
1059 Moreover, the exact sequence of linker options may vary from platform
1060 to platform.
1061 The above :switch:`-largs` section is for Windows platforms. By contrast,
1062 under Unix there is no need for the :switch:`-largs` section.
1063 Differences across platforms are due to details of linker implementation.
1064
1065
1066 .. rubric:: Tracebacks From Anywhere in a Program
1067
1068 It is possible to get a symbolic stack traceback
1069 from anywhere in a program, just as for non-symbolic tracebacks.
1070 The first step is to obtain a non-symbolic
1071 traceback, and then call ``Symbolic_Traceback`` to compute the symbolic
1072 information. Here is an example:
1073
1074 .. code-block:: ada
1075
1076 with Ada.Text_IO;
1077 with GNAT.Traceback;
1078 with GNAT.Traceback.Symbolic;
1079
1080 procedure STB is
1081
1082 use Ada;
1083 use GNAT.Traceback;
1084 use GNAT.Traceback.Symbolic;
1085
1086 procedure P1 is
1087 TB : Tracebacks_Array (1 .. 10);
1088 -- We are asking for a maximum of 10 stack frames.
1089 Len : Natural;
1090 -- Len will receive the actual number of stack frames returned.
1091 begin
1092 Call_Chain (TB, Len);
1093 Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
1094 end P1;
1095
1096 procedure P2 is
1097 begin
1098 P1;
1099 end P2;
1100
1101 begin
1102 P2;
1103 end STB;
1104
1105
1106 .. rubric:: Automatic Symbolic Tracebacks
1107
1108 Symbolic tracebacks may also be enabled by using the -Es switch to gnatbind (as
1109 in ``gprbuild -g ... -bargs -Es``).
1110 This will cause the Exception_Information to contain a symbolic traceback,
1111 which will also be printed if an unhandled exception terminates the
1112 program.
1113
1114
1115 .. _Pretty-Printers_For_The_GNAT_Runtime:
1116
1117 Pretty-Printers for the GNAT runtime
1118 ------------------------------------
1119
1120 As discussed in :title:`Calling User-Defined Subprograms`, GDB's
1121 ``print`` command only knows about the physical layout of program data
1122 structures and therefore normally displays only low-level dumps, which
1123 are often hard to understand.
1124
1125 An example of this is when trying to display the contents of an Ada
1126 standard container, such as ``Ada.Containers.Ordered_Maps.Map``:
1127
1128 .. code-block:: ada
1129
1130 with Ada.Containers.Ordered_Maps;
1131
1132 procedure PP is
1133 package Int_To_Nat is
1134 new Ada.Containers.Ordered_Maps (Integer, Natural);
1135
1136 Map : Int_To_Nat.Map;
1137 begin
1138 Map.Insert (1, 10);
1139 Map.Insert (2, 20);
1140 Map.Insert (3, 30);
1141
1142 Map.Clear; -- BREAK HERE
1143 end PP;
1144
1145 When this program is built with debugging information and run under
1146 GDB up to the ``Map.Clear`` statement, trying to print ``Map`` will
1147 yield information that is only relevant to the developers of our standard
1148 containers:
1149
1150 ::
1151
1152 (gdb) print map
1153 $1 = (
1154 tree => (
1155 first => 0x64e010,
1156 last => 0x64e070,
1157 root => 0x64e040,
1158 length => 3,
1159 tc => (
1160 busy => 0,
1161 lock => 0
1162 )
1163 )
1164 )
1165
1166 Fortunately, GDB has a feature called `pretty-printers
1167 <http://docs.adacore.com/gdb-docs/html/gdb.html#Pretty_002dPrinter-Introduction>`_,
1168 which allows customizing how GDB displays data structures. The GDB
1169 shipped with GNAT embeds such pretty-printers for the most common
1170 containers in the standard library. To enable them, either run the
1171 following command manually under GDB or add it to your ``.gdbinit`` file:
1172
1173 ::
1174
1175 python import gnatdbg; gnatdbg.setup()
1176
1177 Once this is done, GDB's ``print`` command will automatically use
1178 these pretty-printers when appropriate. Using the previous example:
1179
1180 ::
1181
1182 (gdb) print map
1183 $1 = pp.int_to_nat.map of length 3 = {
1184 [1] = 10,
1185 [2] = 20,
1186 [3] = 30
1187 }
1188
1189 Pretty-printers are invoked each time GDB tries to display a value,
1190 including when displaying the arguments of a called subprogram (in
1191 GDB's ``backtrace`` command) or when printing the value returned by a
1192 function (in GDB's ``finish`` command).
1193
1194 To display a value without involving pretty-printers, ``print`` can be
1195 invoked with its ``/r`` option:
1196
1197 ::
1198
1199 (gdb) print/r map
1200 $1 = (
1201 tree => (...
1202
1203 Finer control of pretty-printers is also possible: see `GDB's online
1204 documentation
1205 <http://docs.adacore.com/gdb-docs/html/gdb.html#Pretty_002dPrinter-Commands>`_
1206 for more information.
1207
1208
1209 .. index:: Code Coverage
1210 .. index:: Profiling
1211
1212
1213 .. _Code_Coverage_and_Profiling:
1214
1215 Code Coverage and Profiling
1216 ===========================
1217
1218 This section describes how to use the ``gcov`` coverage testing tool and
1219 the ``gprof`` profiler tool on Ada programs.
1220
1221 .. index:: ! gcov
1222
1223 .. _Code_Coverage_of_Ada_Programs_with_gcov:
1224
1225 Code Coverage of Ada Programs with gcov
1226 ---------------------------------------
1227
1228 ``gcov`` is a test coverage program: it analyzes the execution of a given
1229 program on selected tests, to help you determine the portions of the program
1230 that are still untested.
1231
1232 ``gcov`` is part of the GCC suite, and is described in detail in the GCC
1233 User's Guide. You can refer to this documentation for a more complete
1234 description.
1235
1236 This chapter provides a quick startup guide, and
1237 details some GNAT-specific features.
1238
1239 .. _Quick_startup_guide:
1240
1241 Quick startup guide
1242 ^^^^^^^^^^^^^^^^^^^
1243
1244 In order to perform coverage analysis of a program using ``gcov``, several
1245 steps are needed:
1246
1247 #. Instrument the code during the compilation process,
1248 #. Execute the instrumented program, and
1249 #. Invoke the ``gcov`` tool to generate the coverage results.
1250
1251 .. index:: -fprofile-arcs (gcc)
1252 .. index:: -ftest-coverage (gcc
1253 .. index:: -fprofile-arcs (gnatbind)
1254
1255 The code instrumentation needed by gcov is created at the object level.
1256 The source code is not modified in any way, because the instrumentation code is
1257 inserted by gcc during the compilation process. To compile your code with code
1258 coverage activated, you need to recompile your whole project using the
1259 switches
1260 :switch:`-fprofile-arcs` and :switch:`-ftest-coverage`, and link it using
1261 :switch:`-fprofile-arcs`.
1262
1263 ::
1264
1265 $ gnatmake -P my_project.gpr -f -cargs -fprofile-arcs -ftest-coverage \\
1266 -largs -fprofile-arcs
1267
1268 This compilation process will create :file:`.gcno` files together with
1269 the usual object files.
1270
1271 Once the program is compiled with coverage instrumentation, you can
1272 run it as many times as needed -- on portions of a test suite for
1273 example. The first execution will produce :file:`.gcda` files at the
1274 same location as the :file:`.gcno` files. Subsequent executions
1275 will update those files, so that a cumulative result of the covered
1276 portions of the program is generated.
1277
1278 Finally, you need to call the ``gcov`` tool. The different options of
1279 ``gcov`` are described in the GCC User's Guide, section *Invoking gcov*.
1280
1281 This will create annotated source files with a :file:`.gcov` extension:
1282 :file:`my_main.adb` file will be analyzed in :file:`my_main.adb.gcov`.
1283
1284
1285 .. _GNAT_specifics:
1286
1287 GNAT specifics
1288 ^^^^^^^^^^^^^^
1289
1290 Because of Ada semantics, portions of the source code may be shared among
1291 several object files. This is the case for example when generics are
1292 involved, when inlining is active or when declarations generate initialisation
1293 calls. In order to take
1294 into account this shared code, you need to call ``gcov`` on all
1295 source files of the tested program at once.
1296
1297 The list of source files might exceed the system's maximum command line
1298 length. In order to bypass this limitation, a new mechanism has been
1299 implemented in ``gcov``: you can now list all your project's files into a
1300 text file, and provide this file to gcov as a parameter, preceded by a ``@``
1301 (e.g. :samp:`gcov @mysrclist.txt`).
1302
1303 Note that on AIX compiling a static library with :switch:`-fprofile-arcs` is
1304 not supported as there can be unresolved symbols during the final link.
1305
1306
1307 .. index:: ! gprof
1308 .. index:: Profiling
1309
1310 .. _Profiling_an_Ada_Program_with_gprof:
1311
1312 Profiling an Ada Program with gprof
1313 -----------------------------------
1314
1315 This section is not meant to be an exhaustive documentation of ``gprof``.
1316 Full documentation for it can be found in the :title:`GNU Profiler User's Guide`
1317 documentation that is part of this GNAT distribution.
1318
1319 Profiling a program helps determine the parts of a program that are executed
1320 most often, and are therefore the most time-consuming.
1321
1322 ``gprof`` is the standard GNU profiling tool; it has been enhanced to
1323 better handle Ada programs and multitasking.
1324 It is currently supported on the following platforms
1325
1326 * linux x86/x86_64
1327 * solaris sparc/sparc64/x86
1328 * windows x86
1329
1330 In order to profile a program using ``gprof``, several steps are needed:
1331
1332 #. Instrument the code, which requires a full recompilation of the project with the
1333 proper switches.
1334
1335 #. Execute the program under the analysis conditions, i.e. with the desired
1336 input.
1337
1338 #. Analyze the results using the ``gprof`` tool.
1339
1340 The following sections detail the different steps, and indicate how
1341 to interpret the results.
1342
1343
1344 .. _Compilation_for_profiling:
1345
1346 Compilation for profiling
1347 ^^^^^^^^^^^^^^^^^^^^^^^^^
1348
1349 .. index:: -pg (gcc), for profiling
1350 .. index:: -pg (gnatlink), for profiling
1351
1352 In order to profile a program the first step is to tell the compiler
1353 to generate the necessary profiling information. The compiler switch to be used
1354 is ``-pg``, which must be added to other compilation switches. This
1355 switch needs to be specified both during compilation and link stages, and can
1356 be specified once when using gnatmake:
1357
1358 ::
1359
1360 $ gnatmake -f -pg -P my_project
1361
1362 Note that only the objects that were compiled with the ``-pg`` switch will
1363 be profiled; if you need to profile your whole project, use the ``-f``
1364 gnatmake switch to force full recompilation.
1365
1366 .. _Program_execution:
1367
1368
1369 Program execution
1370 ^^^^^^^^^^^^^^^^^
1371
1372 Once the program has been compiled for profiling, you can run it as usual.
1373
1374 The only constraint imposed by profiling is that the program must terminate
1375 normally. An interrupted program (via a Ctrl-C, kill, etc.) will not be
1376 properly analyzed.
1377
1378 Once the program completes execution, a data file called :file:`gmon.out` is
1379 generated in the directory where the program was launched from. If this file
1380 already exists, it will be overwritten.
1381
1382
1383 .. _Running_gprof:
1384
1385 Running gprof
1386 ^^^^^^^^^^^^^
1387
1388 The ``gprof`` tool is called as follow:
1389
1390 ::
1391
1392 $ gprof my_prog gmon.out
1393
1394 or simply:
1395
1396 ::
1397
1398 $ gprof my_prog
1399
1400 The complete form of the gprof command line is the following:
1401
1402 ::
1403
1404 $ gprof [switches] [executable [data-file]]
1405
1406 ``gprof`` supports numerous switches. The order of these
1407 switch does not matter. The full list of options can be found in
1408 the GNU Profiler User's Guide documentation that comes with this documentation.
1409
1410 The following is the subset of those switches that is most relevant:
1411
1412 .. index:: --demangle (gprof)
1413
1414 :samp:`--demangle[={style}]`, :samp:`--no-demangle`
1415 These options control whether symbol names should be demangled when
1416 printing output. The default is to demangle C++ symbols. The
1417 ``--no-demangle`` option may be used to turn off demangling. Different
1418 compilers have different mangling styles. The optional demangling style
1419 argument can be used to choose an appropriate demangling style for your
1420 compiler, in particular Ada symbols generated by GNAT can be demangled using
1421 ``--demangle=gnat``.
1422
1423
1424 .. index:: -e (gprof)
1425
1426 :samp:`-e {function_name}`
1427 The :samp:`-e {function}` option tells ``gprof`` not to print
1428 information about the function ``function_name`` (and its
1429 children...) in the call graph. The function will still be listed
1430 as a child of any functions that call it, but its index number will be
1431 shown as ``[not printed]``. More than one ``-e`` option may be
1432 given; only one ``function_name`` may be indicated with each ``-e``
1433 option.
1434
1435
1436 .. index:: -E (gprof)
1437
1438 :samp:`-E {function_name}`
1439 The :samp:`-E {function}` option works like the ``-e`` option, but
1440 execution time spent in the function (and children who were not called from
1441 anywhere else), will not be used to compute the percentages-of-time for
1442 the call graph. More than one :switch:`-E` option may be given; only one
1443 ``function_name`` may be indicated with each :switch:`-E`` option.
1444
1445
1446 .. index:: -f (gprof)
1447
1448 :samp:`-f {function_name}`
1449 The :samp:`-f {function}` option causes ``gprof`` to limit the
1450 call graph to the function ``function_name`` and its children (and
1451 their children...). More than one ``-f`` option may be given;
1452 only one ``function_name`` may be indicated with each ``-f``
1453 option.
1454
1455
1456 .. index:: -F (gprof)
1457
1458 :samp:`-F {function_name}`
1459 The :samp:`-F {function}` option works like the ``-f`` option, but
1460 only time spent in the function and its children (and their
1461 children...) will be used to determine total-time and
1462 percentages-of-time for the call graph. More than one ``-F`` option
1463 may be given; only one ``function_name`` may be indicated with each
1464 ``-F`` option. The ``-F`` option overrides the ``-E`` option.
1465
1466
1467 .. _Interpretation_of_profiling_results:
1468
1469 Interpretation of profiling results
1470 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1471
1472 The results of the profiling analysis are represented by two arrays: the
1473 'flat profile' and the 'call graph'. Full documentation of those outputs
1474 can be found in the GNU Profiler User's Guide.
1475
1476 The flat profile shows the time spent in each function of the program, and how
1477 many time it has been called. This allows you to locate easily the most
1478 time-consuming functions.
1479
1480 The call graph shows, for each subprogram, the subprograms that call it,
1481 and the subprograms that it calls. It also provides an estimate of the time
1482 spent in each of those callers/called subprograms.
1483
1484
1485
1486 .. _Improving_Performance:
1487
1488 Improving Performance
1489 =====================
1490
1491 .. index:: Improving performance
1492
1493 This section presents several topics related to program performance.
1494 It first describes some of the tradeoffs that need to be considered
1495 and some of the techniques for making your program run faster.
1496
1497 .. only:: PRO or GPL
1498
1499 It then documents the unused subprogram/data elimination feature
1500 and the ``gnatelim`` tool,
1501 which can reduce the size of program executables.
1502
1503
1504 .. only:: FSF
1505
1506 It then documents the unused subprogram/data elimination feature,
1507 which can reduce the size of program executables.
1508
1509
1510 .. _Performance_Considerations:
1511
1512 Performance Considerations
1513 --------------------------
1514
1515 The GNAT system provides a number of options that allow a trade-off
1516 between
1517
1518 * performance of the generated code
1519
1520 * speed of compilation
1521
1522 * minimization of dependences and recompilation
1523
1524 * the degree of run-time checking.
1525
1526 The defaults (if no options are selected) aim at improving the speed
1527 of compilation and minimizing dependences, at the expense of performance
1528 of the generated code:
1529
1530 * no optimization
1531
1532 * no inlining of subprogram calls
1533
1534 * all run-time checks enabled except overflow and elaboration checks
1535
1536 These options are suitable for most program development purposes. This
1537 section describes how you can modify these choices, and also provides
1538 some guidelines on debugging optimized code.
1539
1540
1541 .. _Controlling_Run-Time_Checks:
1542
1543 Controlling Run-Time Checks
1544 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1545
1546 By default, GNAT generates all run-time checks, except stack overflow
1547 checks, and checks for access before elaboration on subprogram
1548 calls. The latter are not required in default mode, because all
1549 necessary checking is done at compile time.
1550
1551 .. index:: -gnatp (gcc)
1552 .. index:: -gnato (gcc)
1553
1554 The gnat switch, :switch:`-gnatp` allows this default to be modified. See
1555 :ref:`Run-Time_Checks`.
1556
1557 Our experience is that the default is suitable for most development
1558 purposes.
1559
1560 Elaboration checks are off by default, and also not needed by default, since
1561 GNAT uses a static elaboration analysis approach that avoids the need for
1562 run-time checking. This manual contains a full chapter discussing the issue
1563 of elaboration checks, and if the default is not satisfactory for your use,
1564 you should read this chapter.
1565
1566 For validity checks, the minimal checks required by the Ada Reference
1567 Manual (for case statements and assignments to array elements) are on
1568 by default. These can be suppressed by use of the :switch:`-gnatVn` switch.
1569 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
1570 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
1571 it may be reasonable to routinely use :switch:`-gnatVn`. Validity checks
1572 are also suppressed entirely if :switch:`-gnatp` is used.
1573
1574 .. index:: Overflow checks
1575 .. index:: Checks, overflow
1576
1577 .. index:: Suppress
1578 .. index:: Unsuppress
1579 .. index:: pragma Suppress
1580 .. index:: pragma Unsuppress
1581
1582 Note that the setting of the switches controls the default setting of
1583 the checks. They may be modified using either ``pragma Suppress`` (to
1584 remove checks) or ``pragma Unsuppress`` (to add back suppressed
1585 checks) in the program source.
1586
1587
1588 .. _Use_of_Restrictions:
1589
1590 Use of Restrictions
1591 ^^^^^^^^^^^^^^^^^^^
1592
1593 The use of pragma Restrictions allows you to control which features are
1594 permitted in your program. Apart from the obvious point that if you avoid
1595 relatively expensive features like finalization (enforceable by the use
1596 of pragma Restrictions (No_Finalization), the use of this pragma does not
1597 affect the generated code in most cases.
1598
1599 One notable exception to this rule is that the possibility of task abort
1600 results in some distributed overhead, particularly if finalization or
1601 exception handlers are used. The reason is that certain sections of code
1602 have to be marked as non-abortable.
1603
1604 If you use neither the ``abort`` statement, nor asynchronous transfer
1605 of control (``select ... then abort``), then this distributed overhead
1606 is removed, which may have a general positive effect in improving
1607 overall performance. Especially code involving frequent use of tasking
1608 constructs and controlled types will show much improved performance.
1609 The relevant restrictions pragmas are
1610
1611 .. code-block:: ada
1612
1613 pragma Restrictions (No_Abort_Statements);
1614 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
1615
1616 It is recommended that these restriction pragmas be used if possible. Note
1617 that this also means that you can write code without worrying about the
1618 possibility of an immediate abort at any point.
1619
1620
1621 .. _Optimization_Levels:
1622
1623 Optimization Levels
1624 ^^^^^^^^^^^^^^^^^^^
1625
1626 .. index:: -O (gcc)
1627
1628 Without any optimization option,
1629 the compiler's goal is to reduce the cost of
1630 compilation and to make debugging produce the expected results.
1631 Statements are independent: if you stop the program with a breakpoint between
1632 statements, you can then assign a new value to any variable or change
1633 the program counter to any other statement in the subprogram and get exactly
1634 the results you would expect from the source code.
1635
1636 Turning on optimization makes the compiler attempt to improve the
1637 performance and/or code size at the expense of compilation time and
1638 possibly the ability to debug the program.
1639
1640 If you use multiple
1641 -O options, with or without level numbers,
1642 the last such option is the one that is effective.
1643
1644 The default is optimization off. This results in the fastest compile
1645 times, but GNAT makes absolutely no attempt to optimize, and the
1646 generated programs are considerably larger and slower than when
1647 optimization is enabled. You can use the
1648 :switch:`-O` switch (the permitted forms are :switch:`-O0`, :switch:`-O1`
1649 :switch:`-O2`, :switch:`-O3`, and :switch:`-Os`)
1650 to ``gcc`` to control the optimization level:
1651
1652
1653 * :switch:`-O0`
1654 No optimization (the default);
1655 generates unoptimized code but has
1656 the fastest compilation time.
1657
1658 Note that many other compilers do substantial optimization even
1659 if 'no optimization' is specified. With gcc, it is very unusual
1660 to use :switch:`-O0` for production if execution time is of any concern,
1661 since :switch:`-O0` means (almost) no optimization. This difference
1662 between gcc and other compilers should be kept in mind when
1663 doing performance comparisons.
1664
1665 * :switch:`-O1`
1666 Moderate optimization;
1667 optimizes reasonably well but does not
1668 degrade compilation time significantly.
1669
1670 * :switch:`-O2`
1671 Full optimization;
1672 generates highly optimized code and has
1673 the slowest compilation time.
1674
1675 * :switch:`-O3`
1676 Full optimization as in :switch:`-O2`;
1677 also uses more aggressive automatic inlining of subprograms within a unit
1678 (:ref:`Inlining_of_Subprograms`) and attempts to vectorize loops.
1679
1680
1681 * :switch:`-Os`
1682 Optimize space usage (code and data) of resulting program.
1683
1684 Higher optimization levels perform more global transformations on the
1685 program and apply more expensive analysis algorithms in order to generate
1686 faster and more compact code. The price in compilation time, and the
1687 resulting improvement in execution time,
1688 both depend on the particular application and the hardware environment.
1689 You should experiment to find the best level for your application.
1690
1691 Since the precise set of optimizations done at each level will vary from
1692 release to release (and sometime from target to target), it is best to think
1693 of the optimization settings in general terms.
1694 See the *Options That Control Optimization* section in
1695 :title:`Using the GNU Compiler Collection (GCC)`
1696 for details about
1697 the :switch:`-O` settings and a number of :switch:`-f` options that
1698 individually enable or disable specific optimizations.
1699
1700 Unlike some other compilation systems, ``gcc`` has
1701 been tested extensively at all optimization levels. There are some bugs
1702 which appear only with optimization turned on, but there have also been
1703 bugs which show up only in *unoptimized* code. Selecting a lower
1704 level of optimization does not improve the reliability of the code
1705 generator, which in practice is highly reliable at all optimization
1706 levels.
1707
1708 Note regarding the use of :switch:`-O3`: The use of this optimization level
1709 ought not to be automatically preferred over that of level :switch:`-O2`,
1710 since it often results in larger executables which may run more slowly.
1711 See further discussion of this point in :ref:`Inlining_of_Subprograms`.
1712
1713
1714 .. _Debugging_Optimized_Code:
1715
1716 Debugging Optimized Code
1717 ^^^^^^^^^^^^^^^^^^^^^^^^
1718
1719 .. index:: Debugging optimized code
1720 .. index:: Optimization and debugging
1721
1722 Although it is possible to do a reasonable amount of debugging at
1723 nonzero optimization levels,
1724 the higher the level the more likely that
1725 source-level constructs will have been eliminated by optimization.
1726 For example, if a loop is strength-reduced, the loop
1727 control variable may be completely eliminated and thus cannot be
1728 displayed in the debugger.
1729 This can only happen at :switch:`-O2` or :switch:`-O3`.
1730 Explicit temporary variables that you code might be eliminated at
1731 level :switch:`-O1` or higher.
1732
1733 .. index:: -g (gcc)
1734
1735 The use of the :switch:`-g` switch,
1736 which is needed for source-level debugging,
1737 affects the size of the program executable on disk,
1738 and indeed the debugging information can be quite large.
1739 However, it has no effect on the generated code (and thus does not
1740 degrade performance)
1741
1742 Since the compiler generates debugging tables for a compilation unit before
1743 it performs optimizations, the optimizing transformations may invalidate some
1744 of the debugging data. You therefore need to anticipate certain
1745 anomalous situations that may arise while debugging optimized code.
1746 These are the most common cases:
1747
1748 * *The 'hopping Program Counter':* Repeated ``step`` or ``next``
1749 commands show
1750 the PC bouncing back and forth in the code. This may result from any of
1751 the following optimizations:
1752
1753 - *Common subexpression elimination:* using a single instance of code for a
1754 quantity that the source computes several times. As a result you
1755 may not be able to stop on what looks like a statement.
1756
1757 - *Invariant code motion:* moving an expression that does not change within a
1758 loop, to the beginning of the loop.
1759
1760 - *Instruction scheduling:* moving instructions so as to
1761 overlap loads and stores (typically) with other code, or in
1762 general to move computations of values closer to their uses. Often
1763 this causes you to pass an assignment statement without the assignment
1764 happening and then later bounce back to the statement when the
1765 value is actually needed. Placing a breakpoint on a line of code
1766 and then stepping over it may, therefore, not always cause all the
1767 expected side-effects.
1768
1769 * *The 'big leap':* More commonly known as *cross-jumping*, in which
1770 two identical pieces of code are merged and the program counter suddenly
1771 jumps to a statement that is not supposed to be executed, simply because
1772 it (and the code following) translates to the same thing as the code
1773 that *was* supposed to be executed. This effect is typically seen in
1774 sequences that end in a jump, such as a ``goto``, a ``return``, or
1775 a ``break`` in a C ``switch`` statement.
1776
1777 * *The 'roving variable':* The symptom is an unexpected value in a variable.
1778 There are various reasons for this effect:
1779
1780 - In a subprogram prologue, a parameter may not yet have been moved to its
1781 'home'.
1782
1783 - A variable may be dead, and its register re-used. This is
1784 probably the most common cause.
1785
1786 - As mentioned above, the assignment of a value to a variable may
1787 have been moved.
1788
1789 - A variable may be eliminated entirely by value propagation or
1790 other means. In this case, GCC may incorrectly generate debugging
1791 information for the variable
1792
1793 In general, when an unexpected value appears for a local variable or parameter
1794 you should first ascertain if that value was actually computed by
1795 your program, as opposed to being incorrectly reported by the debugger.
1796 Record fields or
1797 array elements in an object designated by an access value
1798 are generally less of a problem, once you have ascertained that the access
1799 value is sensible.
1800 Typically, this means checking variables in the preceding code and in the
1801 calling subprogram to verify that the value observed is explainable from other
1802 values (one must apply the procedure recursively to those
1803 other values); or re-running the code and stopping a little earlier
1804 (perhaps before the call) and stepping to better see how the variable obtained
1805 the value in question; or continuing to step *from* the point of the
1806 strange value to see if code motion had simply moved the variable's
1807 assignments later.
1808
1809 In light of such anomalies, a recommended technique is to use :switch:`-O0`
1810 early in the software development cycle, when extensive debugging capabilities
1811 are most needed, and then move to :switch:`-O1` and later :switch:`-O2` as
1812 the debugger becomes less critical.
1813 Whether to use the :switch:`-g` switch in the release version is
1814 a release management issue.
1815 Note that if you use :switch:`-g` you can then use the ``strip`` program
1816 on the resulting executable,
1817 which removes both debugging information and global symbols.
1818
1819
1820 .. _Inlining_of_Subprograms:
1821
1822 Inlining of Subprograms
1823 ^^^^^^^^^^^^^^^^^^^^^^^
1824
1825 A call to a subprogram in the current unit is inlined if all the
1826 following conditions are met:
1827
1828 * The optimization level is at least :switch:`-O1`.
1829
1830 * The called subprogram is suitable for inlining: It must be small enough
1831 and not contain something that ``gcc`` cannot support in inlined
1832 subprograms.
1833
1834 .. index:: pragma Inline
1835 .. index:: Inline
1836
1837 * Any one of the following applies: ``pragma Inline`` is applied to the
1838 subprogram; the subprogram is local to the unit and called once from
1839 within it; the subprogram is small and optimization level :switch:`-O2` is
1840 specified; optimization level :switch:`-O3` is specified.
1841
1842 Calls to subprograms in |withed| units are normally not inlined.
1843 To achieve actual inlining (that is, replacement of the call by the code
1844 in the body of the subprogram), the following conditions must all be true:
1845
1846 * The optimization level is at least :switch:`-O1`.
1847
1848 * The called subprogram is suitable for inlining: It must be small enough
1849 and not contain something that ``gcc`` cannot support in inlined
1850 subprograms.
1851
1852 * There is a ``pragma Inline`` for the subprogram.
1853
1854 * The :switch:`-gnatn` switch is used on the command line.
1855
1856 Even if all these conditions are met, it may not be possible for
1857 the compiler to inline the call, due to the length of the body,
1858 or features in the body that make it impossible for the compiler
1859 to do the inlining.
1860
1861 Note that specifying the :switch:`-gnatn` switch causes additional
1862 compilation dependencies. Consider the following:
1863
1864 .. code-block:: ada
1865
1866 package R is
1867 procedure Q;
1868 pragma Inline (Q);
1869 end R;
1870 package body R is
1871 ...
1872 end R;
1873
1874 with R;
1875 procedure Main is
1876 begin
1877 ...
1878 R.Q;
1879 end Main;
1880
1881 With the default behavior (no :switch:`-gnatn` switch specified), the
1882 compilation of the ``Main`` procedure depends only on its own source,
1883 :file:`main.adb`, and the spec of the package in file :file:`r.ads`. This
1884 means that editing the body of ``R`` does not require recompiling
1885 ``Main``.
1886
1887 On the other hand, the call ``R.Q`` is not inlined under these
1888 circumstances. If the :switch:`-gnatn` switch is present when ``Main``
1889 is compiled, the call will be inlined if the body of ``Q`` is small
1890 enough, but now ``Main`` depends on the body of ``R`` in
1891 :file:`r.adb` as well as on the spec. This means that if this body is edited,
1892 the main program must be recompiled. Note that this extra dependency
1893 occurs whether or not the call is in fact inlined by ``gcc``.
1894
1895 The use of front end inlining with :switch:`-gnatN` generates similar
1896 additional dependencies.
1897
1898 .. index:: -fno-inline (gcc)
1899
1900 Note: The :switch:`-fno-inline` switch overrides all other conditions and ensures that
1901 no inlining occurs, unless requested with pragma Inline_Always for ``gcc``
1902 back-ends. The extra dependences resulting from :switch:`-gnatn` will still be active,
1903 even if this switch is used to suppress the resulting inlining actions.
1904
1905 .. index:: -fno-inline-functions (gcc)
1906
1907 Note: The :switch:`-fno-inline-functions` switch can be used to prevent
1908 automatic inlining of subprograms if :switch:`-O3` is used.
1909
1910 .. index:: -fno-inline-small-functions (gcc)
1911
1912 Note: The :switch:`-fno-inline-small-functions` switch can be used to prevent
1913 automatic inlining of small subprograms if :switch:`-O2` is used.
1914
1915 .. index:: -fno-inline-functions-called-once (gcc)
1916
1917 Note: The :switch:`-fno-inline-functions-called-once` switch
1918 can be used to prevent inlining of subprograms local to the unit
1919 and called once from within it if :switch:`-O1` is used.
1920
1921 Note regarding the use of :switch:`-O3`: :switch:`-gnatn` is made up of two
1922 sub-switches :switch:`-gnatn1` and :switch:`-gnatn2` that can be directly
1923 specified in lieu of it, :switch:`-gnatn` being translated into one of them
1924 based on the optimization level. With :switch:`-O2` or below, :switch:`-gnatn`
1925 is equivalent to :switch:`-gnatn1` which activates pragma ``Inline`` with
1926 moderate inlining across modules. With :switch:`-O3`, :switch:`-gnatn` is
1927 equivalent to :switch:`-gnatn2` which activates pragma ``Inline`` with
1928 full inlining across modules. If you have used pragma ``Inline`` in
1929 appropriate cases, then it is usually much better to use :switch:`-O2`
1930 and :switch:`-gnatn` and avoid the use of :switch:`-O3` which has the additional
1931 effect of inlining subprograms you did not think should be inlined. We have
1932 found that the use of :switch:`-O3` may slow down the compilation and increase
1933 the code size by performing excessive inlining, leading to increased
1934 instruction cache pressure from the increased code size and thus minor
1935 performance improvements. So the bottom line here is that you should not
1936 automatically assume that :switch:`-O3` is better than :switch:`-O2`, and
1937 indeed you should use :switch:`-O3` only if tests show that it actually
1938 improves performance for your program.
1939
1940 .. _Floating_Point_Operations:
1941
1942 Floating_Point_Operations
1943 ^^^^^^^^^^^^^^^^^^^^^^^^^
1944
1945 .. index:: Floating-Point Operations
1946
1947 On almost all targets, GNAT maps Float and Long_Float to the 32-bit and
1948 64-bit standard IEEE floating-point representations, and operations will
1949 use standard IEEE arithmetic as provided by the processor. On most, but
1950 not all, architectures, the attribute Machine_Overflows is False for these
1951 types, meaning that the semantics of overflow is implementation-defined.
1952 In the case of GNAT, these semantics correspond to the normal IEEE
1953 treatment of infinities and NaN (not a number) values. For example,
1954 1.0 / 0.0 yields plus infinitiy and 0.0 / 0.0 yields a NaN. By
1955 avoiding explicit overflow checks, the performance is greatly improved
1956 on many targets. However, if required, floating-point overflow can be
1957 enabled by the use of the pragma Check_Float_Overflow.
1958
1959 Another consideration that applies specifically to x86 32-bit
1960 architectures is which form of floating-point arithmetic is used.
1961 By default the operations use the old style x86 floating-point,
1962 which implements an 80-bit extended precision form (on these
1963 architectures the type Long_Long_Float corresponds to that form).
1964 In addition, generation of efficient code in this mode means that
1965 the extended precision form will be used for intermediate results.
1966 This may be helpful in improving the final precision of a complex
1967 expression. However it means that the results obtained on the x86
1968 will be different from those on other architectures, and for some
1969 algorithms, the extra intermediate precision can be detrimental.
1970
1971 In addition to this old-style floating-point, all modern x86 chips
1972 implement an alternative floating-point operation model referred
1973 to as SSE2. In this model there is no extended form, and furthermore
1974 execution performance is significantly enhanced. To force GNAT to use
1975 this more modern form, use both of the switches:
1976
1977 -msse2 -mfpmath=sse
1978
1979 A unit compiled with these switches will automatically use the more
1980 efficient SSE2 instruction set for Float and Long_Float operations.
1981 Note that the ABI has the same form for both floating-point models,
1982 so it is permissible to mix units compiled with and without these
1983 switches.
1984
1985
1986
1987
1988
1989 .. _Vectorization_of_loops:
1990
1991 Vectorization of loops
1992 ^^^^^^^^^^^^^^^^^^^^^^
1993
1994 .. index:: Optimization Switches
1995
1996 You can take advantage of the auto-vectorizer present in the ``gcc``
1997 back end to vectorize loops with GNAT. The corresponding command line switch
1998 is :switch:`-ftree-vectorize` but, as it is enabled by default at :switch:`-O3`
1999 and other aggressive optimizations helpful for vectorization also are enabled
2000 by default at this level, using :switch:`-O3` directly is recommended.
2001
2002 You also need to make sure that the target architecture features a supported
2003 SIMD instruction set. For example, for the x86 architecture, you should at
2004 least specify :switch:`-msse2` to get significant vectorization (but you don't
2005 need to specify it for x86-64 as it is part of the base 64-bit architecture).
2006 Similarly, for the PowerPC architecture, you should specify :switch:`-maltivec`.
2007
2008 The preferred loop form for vectorization is the ``for`` iteration scheme.
2009 Loops with a ``while`` iteration scheme can also be vectorized if they are
2010 very simple, but the vectorizer will quickly give up otherwise. With either
2011 iteration scheme, the flow of control must be straight, in particular no
2012 ``exit`` statement may appear in the loop body. The loop may however
2013 contain a single nested loop, if it can be vectorized when considered alone:
2014
2015 .. code-block:: ada
2016
2017 A : array (1..4, 1..4) of Long_Float;
2018 S : array (1..4) of Long_Float;
2019
2020 procedure Sum is
2021 begin
2022 for I in A'Range(1) loop
2023 for J in A'Range(2) loop
2024 S (I) := S (I) + A (I, J);
2025 end loop;
2026 end loop;
2027 end Sum;
2028
2029 The vectorizable operations depend on the targeted SIMD instruction set, but
2030 the adding and some of the multiplying operators are generally supported, as
2031 well as the logical operators for modular types. Note that compiling
2032 with :switch:`-gnatp` might well reveal cases where some checks do thwart
2033 vectorization.
2034
2035 Type conversions may also prevent vectorization if they involve semantics that
2036 are not directly supported by the code generator or the SIMD instruction set.
2037 A typical example is direct conversion from floating-point to integer types.
2038 The solution in this case is to use the following idiom:
2039
2040 .. code-block:: ada
2041
2042 Integer (S'Truncation (F))
2043
2044 if ``S`` is the subtype of floating-point object ``F``.
2045
2046 In most cases, the vectorizable loops are loops that iterate over arrays.
2047 All kinds of array types are supported, i.e. constrained array types with
2048 static bounds:
2049
2050 .. code-block:: ada
2051
2052 type Array_Type is array (1 .. 4) of Long_Float;
2053
2054 constrained array types with dynamic bounds:
2055
2056
2057 .. code-block:: ada
2058
2059 type Array_Type is array (1 .. Q.N) of Long_Float;
2060
2061 type Array_Type is array (Q.K .. 4) of Long_Float;
2062
2063 type Array_Type is array (Q.K .. Q.N) of Long_Float;
2064
2065 or unconstrained array types:
2066
2067 .. code-block:: ada
2068
2069 type Array_Type is array (Positive range <>) of Long_Float;
2070
2071 The quality of the generated code decreases when the dynamic aspect of the
2072 array type increases, the worst code being generated for unconstrained array
2073 types. This is so because, the less information the compiler has about the
2074 bounds of the array, the more fallback code it needs to generate in order to
2075 fix things up at run time.
2076
2077 It is possible to specify that a given loop should be subject to vectorization
2078 preferably to other optimizations by means of pragma ``Loop_Optimize``:
2079
2080 .. code-block:: ada
2081
2082 pragma Loop_Optimize (Vector);
2083
2084 placed immediately within the loop will convey the appropriate hint to the
2085 compiler for this loop.
2086
2087 It is also possible to help the compiler generate better vectorized code
2088 for a given loop by asserting that there are no loop-carried dependencies
2089 in the loop. Consider for example the procedure:
2090
2091 .. code-block:: ada
2092
2093 type Arr is array (1 .. 4) of Long_Float;
2094
2095 procedure Add (X, Y : not null access Arr; R : not null access Arr) is
2096 begin
2097 for I in Arr'Range loop
2098 R(I) := X(I) + Y(I);
2099 end loop;
2100 end;
2101
2102 By default, the compiler cannot unconditionally vectorize the loop because
2103 assigning to a component of the array designated by R in one iteration could
2104 change the value read from the components of the array designated by X or Y
2105 in a later iteration. As a result, the compiler will generate two versions
2106 of the loop in the object code, one vectorized and the other not vectorized,
2107 as well as a test to select the appropriate version at run time. This can
2108 be overcome by another hint:
2109
2110 .. code-block:: ada
2111
2112 pragma Loop_Optimize (Ivdep);
2113
2114 placed immediately within the loop will tell the compiler that it can safely
2115 omit the non-vectorized version of the loop as well as the run-time test.
2116
2117
2118 .. _Other_Optimization_Switches:
2119
2120 Other Optimization Switches
2121 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2122
2123 .. index:: Optimization Switches
2124
2125 Since GNAT uses the ``gcc`` back end, all the specialized
2126 ``gcc`` optimization switches are potentially usable. These switches
2127 have not been extensively tested with GNAT but can generally be expected
2128 to work. Examples of switches in this category are :switch:`-funroll-loops`
2129 and the various target-specific :switch:`-m` options (in particular, it has
2130 been observed that :switch:`-march=xxx` can significantly improve performance
2131 on appropriate machines). For full details of these switches, see
2132 the *Submodel Options* section in the *Hardware Models and Configurations*
2133 chapter of :title:`Using the GNU Compiler Collection (GCC)`.
2134
2135
2136 .. _Optimization_and_Strict_Aliasing:
2137
2138 Optimization and Strict Aliasing
2139 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2140
2141 .. index:: Aliasing
2142 .. index:: Strict Aliasing
2143 .. index:: No_Strict_Aliasing
2144
2145 The strong typing capabilities of Ada allow an optimizer to generate
2146 efficient code in situations where other languages would be forced to
2147 make worst case assumptions preventing such optimizations. Consider
2148 the following example:
2149
2150 .. code-block:: ada
2151
2152 procedure R is
2153 type Int1 is new Integer;
2154 type Int2 is new Integer;
2155 type Int1A is access Int1;
2156 type Int2A is access Int2;
2157 Int1V : Int1A;
2158 Int2V : Int2A;
2159 ...
2160
2161 begin
2162 ...
2163 for J in Data'Range loop
2164 if Data (J) = Int1V.all then
2165 Int2V.all := Int2V.all + 1;
2166 end if;
2167 end loop;
2168 ...
2169 end R;
2170
2171 In this example, since the variable ``Int1V`` can only access objects
2172 of type ``Int1``, and ``Int2V`` can only access objects of type
2173 ``Int2``, there is no possibility that the assignment to
2174 ``Int2V.all`` affects the value of ``Int1V.all``. This means that
2175 the compiler optimizer can "know" that the value ``Int1V.all`` is constant
2176 for all iterations of the loop and avoid the extra memory reference
2177 required to dereference it each time through the loop.
2178
2179 This kind of optimization, called strict aliasing analysis, is
2180 triggered by specifying an optimization level of :switch:`-O2` or
2181 higher or :switch:`-Os` and allows GNAT to generate more efficient code
2182 when access values are involved.
2183
2184 However, although this optimization is always correct in terms of
2185 the formal semantics of the Ada Reference Manual, difficulties can
2186 arise if features like ``Unchecked_Conversion`` are used to break
2187 the typing system. Consider the following complete program example:
2188
2189 .. code-block:: ada
2190
2191 package p1 is
2192 type int1 is new integer;
2193 type int2 is new integer;
2194 type a1 is access int1;
2195 type a2 is access int2;
2196 end p1;
2197
2198 with p1; use p1;
2199 package p2 is
2200 function to_a2 (Input : a1) return a2;
2201 end p2;
2202
2203 with Unchecked_Conversion;
2204 package body p2 is
2205 function to_a2 (Input : a1) return a2 is
2206 function to_a2u is
2207 new Unchecked_Conversion (a1, a2);
2208 begin
2209 return to_a2u (Input);
2210 end to_a2;
2211 end p2;
2212
2213 with p2; use p2;
2214 with p1; use p1;
2215 with Text_IO; use Text_IO;
2216 procedure m is
2217 v1 : a1 := new int1;
2218 v2 : a2 := to_a2 (v1);
2219 begin
2220 v1.all := 1;
2221 v2.all := 0;
2222 put_line (int1'image (v1.all));
2223 end;
2224
2225 This program prints out 0 in :switch:`-O0` or :switch:`-O1`
2226 mode, but it prints out 1 in :switch:`-O2` mode. That's
2227 because in strict aliasing mode, the compiler can and
2228 does assume that the assignment to ``v2.all`` could not
2229 affect the value of ``v1.all``, since different types
2230 are involved.
2231
2232 This behavior is not a case of non-conformance with the standard, since
2233 the Ada RM specifies that an unchecked conversion where the resulting
2234 bit pattern is not a correct value of the target type can result in an
2235 abnormal value and attempting to reference an abnormal value makes the
2236 execution of a program erroneous. That's the case here since the result
2237 does not point to an object of type ``int2``. This means that the
2238 effect is entirely unpredictable.
2239
2240 However, although that explanation may satisfy a language
2241 lawyer, in practice an applications programmer expects an
2242 unchecked conversion involving pointers to create true
2243 aliases and the behavior of printing 1 seems plain wrong.
2244 In this case, the strict aliasing optimization is unwelcome.
2245
2246 Indeed the compiler recognizes this possibility, and the
2247 unchecked conversion generates a warning:
2248
2249 ::
2250
2251 p2.adb:5:07: warning: possible aliasing problem with type "a2"
2252 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
2253 p2.adb:5:07: warning: or use "pragma No_Strict_Aliasing (a2);"
2254
2255 Unfortunately the problem is recognized when compiling the body of
2256 package ``p2``, but the actual "bad" code is generated while
2257 compiling the body of ``m`` and this latter compilation does not see
2258 the suspicious ``Unchecked_Conversion``.
2259
2260 As implied by the warning message, there are approaches you can use to
2261 avoid the unwanted strict aliasing optimization in a case like this.
2262
2263 One possibility is to simply avoid the use of :switch:`-O2`, but
2264 that is a bit drastic, since it throws away a number of useful
2265 optimizations that do not involve strict aliasing assumptions.
2266
2267 A less drastic approach is to compile the program using the
2268 option :switch:`-fno-strict-aliasing`. Actually it is only the
2269 unit containing the dereferencing of the suspicious pointer
2270 that needs to be compiled. So in this case, if we compile
2271 unit ``m`` with this switch, then we get the expected
2272 value of zero printed. Analyzing which units might need
2273 the switch can be painful, so a more reasonable approach
2274 is to compile the entire program with options :switch:`-O2`
2275 and :switch:`-fno-strict-aliasing`. If the performance is
2276 satisfactory with this combination of options, then the
2277 advantage is that the entire issue of possible "wrong"
2278 optimization due to strict aliasing is avoided.
2279
2280 To avoid the use of compiler switches, the configuration
2281 pragma ``No_Strict_Aliasing`` with no parameters may be
2282 used to specify that for all access types, the strict
2283 aliasing optimization should be suppressed.
2284
2285 However, these approaches are still overkill, in that they causes
2286 all manipulations of all access values to be deoptimized. A more
2287 refined approach is to concentrate attention on the specific
2288 access type identified as problematic.
2289
2290 First, if a careful analysis of uses of the pointer shows
2291 that there are no possible problematic references, then
2292 the warning can be suppressed by bracketing the
2293 instantiation of ``Unchecked_Conversion`` to turn
2294 the warning off:
2295
2296 .. code-block:: ada
2297
2298 pragma Warnings (Off);
2299 function to_a2u is
2300 new Unchecked_Conversion (a1, a2);
2301 pragma Warnings (On);
2302
2303 Of course that approach is not appropriate for this particular
2304 example, since indeed there is a problematic reference. In this
2305 case we can take one of two other approaches.
2306
2307 The first possibility is to move the instantiation of unchecked
2308 conversion to the unit in which the type is declared. In
2309 this example, we would move the instantiation of
2310 ``Unchecked_Conversion`` from the body of package
2311 ``p2`` to the spec of package ``p1``. Now the
2312 warning disappears. That's because any use of the
2313 access type knows there is a suspicious unchecked
2314 conversion, and the strict aliasing optimization
2315 is automatically suppressed for the type.
2316
2317 If it is not practical to move the unchecked conversion to the same unit
2318 in which the destination access type is declared (perhaps because the
2319 source type is not visible in that unit), you may use pragma
2320 ``No_Strict_Aliasing`` for the type. This pragma must occur in the
2321 same declarative sequence as the declaration of the access type:
2322
2323 .. code-block:: ada
2324
2325 type a2 is access int2;
2326 pragma No_Strict_Aliasing (a2);
2327
2328 Here again, the compiler now knows that the strict aliasing optimization
2329 should be suppressed for any reference to type ``a2`` and the
2330 expected behavior is obtained.
2331
2332 Finally, note that although the compiler can generate warnings for
2333 simple cases of unchecked conversions, there are tricker and more
2334 indirect ways of creating type incorrect aliases which the compiler
2335 cannot detect. Examples are the use of address overlays and unchecked
2336 conversions involving composite types containing access types as
2337 components. In such cases, no warnings are generated, but there can
2338 still be aliasing problems. One safe coding practice is to forbid the
2339 use of address clauses for type overlaying, and to allow unchecked
2340 conversion only for primitive types. This is not really a significant
2341 restriction since any possible desired effect can be achieved by
2342 unchecked conversion of access values.
2343
2344 The aliasing analysis done in strict aliasing mode can certainly
2345 have significant benefits. We have seen cases of large scale
2346 application code where the time is increased by up to 5% by turning
2347 this optimization off. If you have code that includes significant
2348 usage of unchecked conversion, you might want to just stick with
2349 :switch:`-O1` and avoid the entire issue. If you get adequate
2350 performance at this level of optimization level, that's probably
2351 the safest approach. If tests show that you really need higher
2352 levels of optimization, then you can experiment with :switch:`-O2`
2353 and :switch:`-O2 -fno-strict-aliasing` to see how much effect this
2354 has on size and speed of the code. If you really need to use
2355 :switch:`-O2` with strict aliasing in effect, then you should
2356 review any uses of unchecked conversion of access types,
2357 particularly if you are getting the warnings described above.
2358
2359
2360 .. _Aliased_Variables_and_Optimization:
2361
2362 Aliased Variables and Optimization
2363 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2364
2365 .. index:: Aliasing
2366
2367 There are scenarios in which programs may
2368 use low level techniques to modify variables
2369 that otherwise might be considered to be unassigned. For example,
2370 a variable can be passed to a procedure by reference, which takes
2371 the address of the parameter and uses the address to modify the
2372 variable's value, even though it is passed as an IN parameter.
2373 Consider the following example:
2374
2375 .. code-block:: ada
2376
2377 procedure P is
2378 Max_Length : constant Natural := 16;
2379 type Char_Ptr is access all Character;
2380
2381 procedure Get_String(Buffer: Char_Ptr; Size : Integer);
2382 pragma Import (C, Get_String, "get_string");
2383
2384 Name : aliased String (1 .. Max_Length) := (others => ' ');
2385 Temp : Char_Ptr;
2386
2387 function Addr (S : String) return Char_Ptr is
2388 function To_Char_Ptr is
2389 new Ada.Unchecked_Conversion (System.Address, Char_Ptr);
2390 begin
2391 return To_Char_Ptr (S (S'First)'Address);
2392 end;
2393
2394 begin
2395 Temp := Addr (Name);
2396 Get_String (Temp, Max_Length);
2397 end;
2398
2399 where Get_String is a C function that uses the address in Temp to
2400 modify the variable ``Name``. This code is dubious, and arguably
2401 erroneous, and the compiler would be entitled to assume that
2402 ``Name`` is never modified, and generate code accordingly.
2403
2404 However, in practice, this would cause some existing code that
2405 seems to work with no optimization to start failing at high
2406 levels of optimzization.
2407
2408 What the compiler does for such cases is to assume that marking
2409 a variable as aliased indicates that some "funny business" may
2410 be going on. The optimizer recognizes the aliased keyword and
2411 inhibits optimizations that assume the value cannot be assigned.
2412 This means that the above example will in fact "work" reliably,
2413 that is, it will produce the expected results.
2414
2415
2416 .. _Atomic_Variables_and_Optimization:
2417
2418 Atomic Variables and Optimization
2419 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2420
2421 .. index:: Atomic
2422
2423 There are two considerations with regard to performance when
2424 atomic variables are used.
2425
2426 First, the RM only guarantees that access to atomic variables
2427 be atomic, it has nothing to say about how this is achieved,
2428 though there is a strong implication that this should not be
2429 achieved by explicit locking code. Indeed GNAT will never
2430 generate any locking code for atomic variable access (it will
2431 simply reject any attempt to make a variable or type atomic
2432 if the atomic access cannot be achieved without such locking code).
2433
2434 That being said, it is important to understand that you cannot
2435 assume that the entire variable will always be accessed. Consider
2436 this example:
2437
2438 .. code-block:: ada
2439
2440 type R is record
2441 A,B,C,D : Character;
2442 end record;
2443 for R'Size use 32;
2444 for R'Alignment use 4;
2445
2446 RV : R;
2447 pragma Atomic (RV);
2448 X : Character;
2449 ...
2450 X := RV.B;
2451
2452 You cannot assume that the reference to ``RV.B``
2453 will read the entire 32-bit
2454 variable with a single load instruction. It is perfectly legitimate if
2455 the hardware allows it to do a byte read of just the B field. This read
2456 is still atomic, which is all the RM requires. GNAT can and does take
2457 advantage of this, depending on the architecture and optimization level.
2458 Any assumption to the contrary is non-portable and risky. Even if you
2459 examine the assembly language and see a full 32-bit load, this might
2460 change in a future version of the compiler.
2461
2462 If your application requires that all accesses to ``RV`` in this
2463 example be full 32-bit loads, you need to make a copy for the access
2464 as in:
2465
2466 .. code-block:: ada
2467
2468 declare
2469 RV_Copy : constant R := RV;
2470 begin
2471 X := RV_Copy.B;
2472 end;
2473
2474 Now the reference to RV must read the whole variable.
2475 Actually one can imagine some compiler which figures
2476 out that the whole copy is not required (because only
2477 the B field is actually accessed), but GNAT
2478 certainly won't do that, and we don't know of any
2479 compiler that would not handle this right, and the
2480 above code will in practice work portably across
2481 all architectures (that permit the Atomic declaration).
2482
2483 The second issue with atomic variables has to do with
2484 the possible requirement of generating synchronization
2485 code. For more details on this, consult the sections on
2486 the pragmas Enable/Disable_Atomic_Synchronization in the
2487 GNAT Reference Manual. If performance is critical, and
2488 such synchronization code is not required, it may be
2489 useful to disable it.
2490
2491
2492 .. _Passive_Task_Optimization:
2493
2494 Passive Task Optimization
2495 ^^^^^^^^^^^^^^^^^^^^^^^^^
2496
2497 .. index:: Passive Task
2498
2499 A passive task is one which is sufficiently simple that
2500 in theory a compiler could recognize it an implement it
2501 efficiently without creating a new thread. The original design
2502 of Ada 83 had in mind this kind of passive task optimization, but
2503 only a few Ada 83 compilers attempted it. The problem was that
2504 it was difficult to determine the exact conditions under which
2505 the optimization was possible. The result is a very fragile
2506 optimization where a very minor change in the program can
2507 suddenly silently make a task non-optimizable.
2508
2509 With the revisiting of this issue in Ada 95, there was general
2510 agreement that this approach was fundamentally flawed, and the
2511 notion of protected types was introduced. When using protected
2512 types, the restrictions are well defined, and you KNOW that the
2513 operations will be optimized, and furthermore this optimized
2514 performance is fully portable.
2515
2516 Although it would theoretically be possible for GNAT to attempt to
2517 do this optimization, but it really doesn't make sense in the
2518 context of Ada 95, and none of the Ada 95 compilers implement
2519 this optimization as far as we know. In particular GNAT never
2520 attempts to perform this optimization.
2521
2522 In any new Ada 95 code that is written, you should always
2523 use protected types in place of tasks that might be able to
2524 be optimized in this manner.
2525 Of course this does not help if you have legacy Ada 83 code
2526 that depends on this optimization, but it is unusual to encounter
2527 a case where the performance gains from this optimization
2528 are significant.
2529
2530 Your program should work correctly without this optimization. If
2531 you have performance problems, then the most practical
2532 approach is to figure out exactly where these performance problems
2533 arise, and update those particular tasks to be protected types. Note
2534 that typically clients of the tasks who call entries, will not have
2535 to be modified, only the task definition itself.
2536
2537
2538 .. _Text_IO_Suggestions:
2539
2540 ``Text_IO`` Suggestions
2541 -----------------------
2542
2543 .. index:: Text_IO and performance
2544
2545 The ``Ada.Text_IO`` package has fairly high overheads due in part to
2546 the requirement of maintaining page and line counts. If performance
2547 is critical, a recommendation is to use ``Stream_IO`` instead of
2548 ``Text_IO`` for volume output, since this package has less overhead.
2549
2550 If ``Text_IO`` must be used, note that by default output to the standard
2551 output and standard error files is unbuffered (this provides better
2552 behavior when output statements are used for debugging, or if the
2553 progress of a program is observed by tracking the output, e.g. by
2554 using the Unix *tail -f* command to watch redirected output.
2555
2556 If you are generating large volumes of output with ``Text_IO`` and
2557 performance is an important factor, use a designated file instead
2558 of the standard output file, or change the standard output file to
2559 be buffered using ``Interfaces.C_Streams.setvbuf``.
2560
2561
2562 .. _Reducing_Size_of_Executables_with_Unused_Subprogram/Data_Elimination:
2563
2564 Reducing Size of Executables with Unused Subprogram/Data Elimination
2565 --------------------------------------------------------------------
2566
2567 .. index:: Uunused subprogram/data elimination
2568
2569 This section describes how you can eliminate unused subprograms and data from
2570 your executable just by setting options at compilation time.
2571
2572 .. _About_unused_subprogram/data_elimination:
2573
2574 About unused subprogram/data elimination
2575 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2576
2577 By default, an executable contains all code and data of its composing objects
2578 (directly linked or coming from statically linked libraries), even data or code
2579 never used by this executable.
2580
2581 This feature will allow you to eliminate such unused code from your
2582 executable, making it smaller (in disk and in memory).
2583
2584 This functionality is available on all Linux platforms except for the IA-64
2585 architecture and on all cross platforms using the ELF binary file format.
2586 In both cases GNU binutils version 2.16 or later are required to enable it.
2587
2588 .. _Compilation_options:
2589
2590 Compilation options
2591 ^^^^^^^^^^^^^^^^^^^
2592
2593 The operation of eliminating the unused code and data from the final executable
2594 is directly performed by the linker.
2595
2596 .. index:: -ffunction-sections (gcc)
2597 .. index:: -fdata-sections (gcc)
2598
2599 In order to do this, it has to work with objects compiled with the
2600 following options:
2601 :switch:`-ffunction-sections` :switch:`-fdata-sections`.
2602
2603 These options are usable with C and Ada files.
2604 They will place respectively each
2605 function or data in a separate section in the resulting object file.
2606
2607 Once the objects and static libraries are created with these options, the
2608 linker can perform the dead code elimination. You can do this by setting
2609 the :switch:`-Wl,--gc-sections` option to gcc command or in the
2610 :switch:`-largs` section of ``gnatmake``. This will perform a
2611 garbage collection of code and data never referenced.
2612
2613 If the linker performs a partial link (:switch:`-r` linker option), then you
2614 will need to provide the entry point using the :switch:`-e` / :switch:`--entry`
2615 linker option.
2616
2617 Note that objects compiled without the :switch:`-ffunction-sections` and
2618 :switch:`-fdata-sections` options can still be linked with the executable.
2619 However, no dead code elimination will be performed on those objects (they will
2620 be linked as is).
2621
2622 The GNAT static library is now compiled with -ffunction-sections and
2623 -fdata-sections on some platforms. This allows you to eliminate the unused code
2624 and data of the GNAT library from your executable.
2625
2626
2627 .. _Example_of_unused_subprogram/data_elimination:
2628
2629 Example of unused subprogram/data elimination
2630 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2631
2632 Here is a simple example:
2633
2634 .. code-block:: ada
2635
2636 with Aux;
2637
2638 procedure Test is
2639 begin
2640 Aux.Used (10);
2641 end Test;
2642
2643 package Aux is
2644 Used_Data : Integer;
2645 Unused_Data : Integer;
2646
2647 procedure Used (Data : Integer);
2648 procedure Unused (Data : Integer);
2649 end Aux;
2650
2651 package body Aux is
2652 procedure Used (Data : Integer) is
2653 begin
2654 Used_Data := Data;
2655 end Used;
2656
2657 procedure Unused (Data : Integer) is
2658 begin
2659 Unused_Data := Data;
2660 end Unused;
2661 end Aux;
2662
2663 ``Unused`` and ``Unused_Data`` are never referenced in this code
2664 excerpt, and hence they may be safely removed from the final executable.
2665
2666 ::
2667
2668 $ gnatmake test
2669
2670 $ nm test | grep used
2671 020015f0 T aux__unused
2672 02005d88 B aux__unused_data
2673 020015cc T aux__used
2674 02005d84 B aux__used_data
2675
2676 $ gnatmake test -cargs -fdata-sections -ffunction-sections \\
2677 -largs -Wl,--gc-sections
2678
2679 $ nm test | grep used
2680 02005350 T aux__used
2681 0201ffe0 B aux__used_data
2682
2683 It can be observed that the procedure ``Unused`` and the object
2684 ``Unused_Data`` are removed by the linker when using the
2685 appropriate options.
2686
2687 .. only:: PRO or GPL
2688
2689 .. _Reducing_Size_of_Ada_Executables_with_gnatelim:
2690
2691 Reducing Size of Ada Executables with ``gnatelim``
2692 --------------------------------------------------
2693
2694 .. index:: gnatelim
2695
2696 This section describes ``gnatelim``, a tool which detects unused
2697 subprograms and helps the compiler to create a smaller executable for your
2698 program.
2699
2700 ``gnatelim`` is a project-aware tool.
2701 (See :ref:`Using_Project_Files_with_GNAT_Tools` for a description of
2702 the project-related switches but note that ``gnatelim`` does not support
2703 the :samp:`-U`, :samp:`-U {main_unit}`, :samp:`--subdirs={dir}`, or
2704 :samp:`--no_objects_dir` switches.)
2705 The project file package that can specify
2706 ``gnatelim`` switches is named ``Eliminate``.
2707
2708 .. _About_gnatelim:
2709
2710 About ``gnatelim``
2711 ^^^^^^^^^^^^^^^^^^
2712
2713 When a program shares a set of Ada
2714 packages with other programs, it may happen that this program uses
2715 only a fraction of the subprograms defined in these packages. The code
2716 created for these unused subprograms increases the size of the executable.
2717
2718 ``gnatelim`` tracks unused subprograms in an Ada program and
2719 outputs a list of GNAT-specific pragmas ``Eliminate`` marking all the
2720 subprograms that are declared but never called. By placing the list of
2721 ``Eliminate`` pragmas in the GNAT configuration file :file:`gnat.adc` and
2722 recompiling your program, you may decrease the size of its executable,
2723 because the compiler will not generate the code for 'eliminated' subprograms.
2724 See ``Pragma_Eliminate`` in the :title:`GNAT_Reference_Manual` for more
2725 information about this pragma.
2726
2727 ``gnatelim`` needs as its input data the name of the main subprogram.
2728
2729 If a set of source files is specified as ``gnatelim`` arguments, it
2730 treats these files as a complete set of sources making up a program to
2731 analyse, and analyses only these sources.
2732
2733 After a full successful build of the main subprogram ``gnatelim`` can be
2734 called without specifying sources to analyse, in this case it computes
2735 the source closure of the main unit from the :file:`ALI` files.
2736
2737 If the set of sources to be processed by ``gnatelim`` contains sources with
2738 preprocessing directives
2739 then the needed options should be provided to run preprocessor as a part of
2740 the ``gnatelim`` call, and the generated set of pragmas ``Eliminate``
2741 will correspond to preprocessed sources.
2742
2743 The following command will create the set of :file:`ALI` files needed for
2744 ``gnatelim``:
2745
2746 ::
2747
2748 $ gnatmake -c Main_Prog
2749
2750 Note that ``gnatelim`` does not need object files.
2751
2752
2753 .. _Running_gnatelim:
2754
2755 Running ``gnatelim``
2756 ^^^^^^^^^^^^^^^^^^^^
2757
2758 ``gnatelim`` has the following command-line interface:
2759
2760
2761 ::
2762
2763 $ gnatelim [switches] -main=`main_unit_name {filename} [-cargs gcc_switches]
2764
2765 ``main_unit_name`` should be a name of a source file that contains the main
2766 subprogram of a program (partition).
2767
2768 Each ``filename`` is the name (including the extension) of a source
2769 file to process. 'Wildcards' are allowed, and
2770 the file name may contain path information.
2771
2772 ``gcc_switches`` is a list of switches for
2773 ``gcc``. They will be passed on to all compiler invocations made by
2774 ``gnatelim`` to generate the ASIS trees. Here you can provide
2775 :switch:`-I` switches to form the source search path,
2776 use the :switch:`-gnatec` switch to set the configuration file,
2777 use the :switch:`-gnat05` switch if sources should be compiled in
2778 Ada 2005 mode etc.
2779
2780 ``gnatelim`` has the following switches:
2781
2782
2783 .. index:: --version (gnatelim)
2784
2785 :samp:`--version`
2786 Display Copyright and version, then exit disregarding all other options.
2787
2788
2789 .. index:: --help (gnatelim)
2790
2791 :samp:`--help`
2792 Display usage, then exit disregarding all other options.
2793
2794
2795 .. index:: -P (gnatelim)
2796
2797 :samp:`-P {file}`
2798 Indicates the name of the project file that describes the set of sources
2799 to be processed.
2800
2801
2802 .. index:: -X (gnatelim)
2803
2804 :samp:`-X{name}={value}`
2805 Indicates that external variable ``name`` in the argument project
2806 has the value ``value``. Has no effect if no project is specified as
2807 tool argument.
2808
2809
2810 .. index:: --RTS (gnatelim)
2811
2812 :samp:`--RTS={rts-path}`
2813 Specifies the default location of the runtime library. Same meaning as the
2814 equivalent ``gnatmake`` flag (:ref:`Switches_for_gnatmake`).
2815
2816
2817 .. index:: -files (gnatelim)
2818
2819 :samp:`-files={filename}`
2820 Take the argument source files from the specified file. This file should be an
2821 ordinary text file containing file names separated by spaces or
2822 line breaks. You can use this switch more than once in the same call to
2823 ``gnatelim``. You also can combine this switch with
2824 an explicit list of files.
2825
2826
2827 .. index:: -log (gnatelim)
2828
2829 :samp:`-log`
2830 Duplicate all the output sent to :file:`stderr` into a log file. The log file
2831 is named :file:`gnatelim.log` and is located in the current directory.
2832
2833 .. index:: --no-elim-dispatch (gnatelim)
2834
2835 :samp:`--no-elim-dispatch`
2836 Do not generate pragmas for dispatching operations.
2837
2838
2839 .. index:: --ignore (gnatelim)
2840
2841 :samp:`--ignore={filename}`
2842 Do not generate pragmas for subprograms declared in the sources
2843 listed in a specified file
2844
2845 .. index:: -o (gnatelim)
2846
2847
2848 :samp:`-o={report_file}`
2849 Put ``gnatelim`` output into a specified file. If this file already exists,
2850 it is overridden. If this switch is not used, ``gnatelim`` outputs its results
2851 into :file:`stderr`
2852
2853
2854 .. index:: -j (gnatelim)
2855
2856 :samp:`-j{n}`
2857 Use ``n`` processes to carry out the tree creations (internal representations
2858 of the argument sources). On a multiprocessor machine this speeds up processing
2859 of big sets of argument sources. If ``n`` is 0, then the maximum number of
2860 parallel tree creations is the number of core processors on the platform.
2861
2862
2863 .. index:: -q (gnatelim)
2864
2865 :samp:`-q`
2866 Quiet mode: by default ``gnatelim`` outputs to the standard error
2867 stream the number of program units left to be processed. This option turns
2868 this trace off.
2869
2870 .. index:: -t (gnatelim)
2871
2872
2873 :samp:`-t`
2874 Print out execution time.
2875
2876
2877 .. index:: -v (gnatelim)
2878
2879 :samp:`-v`
2880 Verbose mode: ``gnatelim`` version information is printed as Ada
2881 comments to the standard output stream. Also, in addition to the number of
2882 program units left ``gnatelim`` will output the name of the current unit
2883 being processed.
2884
2885
2886 .. index:: -wq (gnatelim)
2887
2888 :samp:`-wq`
2889 Quiet warning mode - some warnings are suppressed. In particular warnings that
2890 indicate that the analysed set of sources is incomplete to make up a
2891 partition and that some subprogram bodies are missing are not generated.
2892
2893
2894
2895 .. _Processing_Precompiled_Libraries:
2896
2897 Processing Precompiled Libraries
2898 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2899
2900 If some program uses a precompiled Ada library, it can be processed by
2901 ``gnatelim`` in a usual way. ``gnatelim`` will newer generate an
2902 Eliminate pragma for a subprogram if the body of this subprogram has not
2903 been analysed, this is a typical case for subprograms from precompiled
2904 libraries. Switch :switch:`-wq` may be used to suppress
2905 warnings about missing source files and non-analyzed subprogram bodies
2906 that can be generated when processing precompiled Ada libraries.
2907
2908
2909 .. _Correcting_the_List_of_Eliminate_Pragmas:
2910
2911 Correcting the List of Eliminate Pragmas
2912 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2913
2914 In some rare cases ``gnatelim`` may try to eliminate
2915 subprograms that are actually called in the program. In this case, the
2916 compiler will generate an error message of the form:
2917
2918 ::
2919
2920 main.adb:4:08: cannot reference subprogram "P" eliminated at elim.out:5
2921
2922 You will need to manually remove the wrong ``Eliminate`` pragmas from
2923 the configuration file indicated in the error message. You should recompile
2924 your program from scratch after that, because you need a consistent
2925 configuration file(s) during the entire compilation.
2926
2927
2928 .. _Making_Your_Executables_Smaller:
2929
2930 Making Your Executables Smaller
2931 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2932
2933 In order to get a smaller executable for your program you now have to
2934 recompile the program completely with the configuration file containing
2935 pragmas Eliminate generated by gnatelim. If these pragmas are placed in
2936 :file:`gnat.adc` file located in your current directory, just do:
2937
2938 ::
2939
2940 $ gnatmake -f main_prog
2941
2942 (Use the :switch:`-f` option for ``gnatmake`` to
2943 recompile everything
2944 with the set of pragmas ``Eliminate`` that you have obtained with
2945 ``gnatelim``).
2946
2947 Be aware that the set of ``Eliminate`` pragmas is specific to each
2948 program. It is not recommended to merge sets of ``Eliminate``
2949 pragmas created for different programs in one configuration file.
2950
2951
2952 .. _Summary_of_the_gnatelim_Usage_Cycle:
2953
2954 Summary of the ``gnatelim`` Usage Cycle
2955 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2956
2957 Here is a quick summary of the steps to be taken in order to reduce
2958 the size of your executables with ``gnatelim``. You may use
2959 other GNAT options to control the optimization level,
2960 to produce the debugging information, to set search path, etc.
2961
2962 * Create a complete set of :file:`ALI` files (if the program has not been
2963 built already)
2964
2965 ::
2966
2967 $ gnatmake -c main_prog
2968
2969 * Generate a list of ``Eliminate`` pragmas in default configuration file
2970 :file:`gnat.adc` in the current directory
2971
2972 ::
2973
2974 $ gnatelim main_prog >[>] gnat.adc
2975
2976 * Recompile the application
2977
2978 ::
2979
2980 $ gnatmake -f main_prog
2981
2982
2983
2984 .. index:: Overflow checks
2985 .. index:: Checks (overflow)
2986
2987 .. _Overflow_Check_Handling_in_GNAT:
2988
2989 Overflow Check Handling in GNAT
2990 ===============================
2991
2992 This section explains how to control the handling of overflow checks.
2993
2994 .. _Background:
2995
2996 Background
2997 ----------
2998
2999 Overflow checks are checks that the compiler may make to ensure
3000 that intermediate results are not out of range. For example:
3001
3002 .. code-block:: ada
3003
3004 A : Integer;
3005 ...
3006 A := A + 1;
3007
3008 If ``A`` has the value ``Integer'Last``, then the addition may cause
3009 overflow since the result is out of range of the type ``Integer``.
3010 In this case ``Constraint_Error`` will be raised if checks are
3011 enabled.
3012
3013 A trickier situation arises in examples like the following:
3014
3015 .. code-block:: ada
3016
3017 A, C : Integer;
3018 ...
3019 A := (A + 1) + C;
3020
3021 where ``A`` is ``Integer'Last`` and ``C`` is ``-1``.
3022 Now the final result of the expression on the right hand side is
3023 ``Integer'Last`` which is in range, but the question arises whether the
3024 intermediate addition of ``(A + 1)`` raises an overflow error.
3025
3026 The (perhaps surprising) answer is that the Ada language
3027 definition does not answer this question. Instead it leaves
3028 it up to the implementation to do one of two things if overflow
3029 checks are enabled.
3030
3031 * raise an exception (``Constraint_Error``), or
3032
3033 * yield the correct mathematical result which is then used in
3034 subsequent operations.
3035
3036 If the compiler chooses the first approach, then the assignment of this
3037 example will indeed raise ``Constraint_Error`` if overflow checking is
3038 enabled, or result in erroneous execution if overflow checks are suppressed.
3039
3040 But if the compiler
3041 chooses the second approach, then it can perform both additions yielding
3042 the correct mathematical result, which is in range, so no exception
3043 will be raised, and the right result is obtained, regardless of whether
3044 overflow checks are suppressed.
3045
3046 Note that in the first example an
3047 exception will be raised in either case, since if the compiler
3048 gives the correct mathematical result for the addition, it will
3049 be out of range of the target type of the assignment, and thus
3050 fails the range check.
3051
3052 This lack of specified behavior in the handling of overflow for
3053 intermediate results is a source of non-portability, and can thus
3054 be problematic when programs are ported. Most typically this arises
3055 in a situation where the original compiler did not raise an exception,
3056 and then the application is moved to a compiler where the check is
3057 performed on the intermediate result and an unexpected exception is
3058 raised.
3059
3060 Furthermore, when using Ada 2012's preconditions and other
3061 assertion forms, another issue arises. Consider:
3062
3063 .. code-block:: ada
3064
3065 procedure P (A, B : Integer) with
3066 Pre => A + B <= Integer'Last;
3067
3068 One often wants to regard arithmetic in a context like this from
3069 a mathematical point of view. So for example, if the two actual parameters
3070 for a call to ``P`` are both ``Integer'Last``, then
3071 the precondition should be regarded as False. If we are executing
3072 in a mode with run-time checks enabled for preconditions, then we would
3073 like this precondition to fail, rather than raising an exception
3074 because of the intermediate overflow.
3075
3076 However, the language definition leaves the specification of
3077 whether the above condition fails (raising ``Assert_Error``) or
3078 causes an intermediate overflow (raising ``Constraint_Error``)
3079 up to the implementation.
3080
3081 The situation is worse in a case such as the following:
3082
3083 .. code-block:: ada
3084
3085 procedure Q (A, B, C : Integer) with
3086 Pre => A + B + C <= Integer'Last;
3087
3088 Consider the call
3089
3090 .. code-block:: ada
3091
3092 Q (A => Integer'Last, B => 1, C => -1);
3093
3094 From a mathematical point of view the precondition
3095 is True, but at run time we may (but are not guaranteed to) get an
3096 exception raised because of the intermediate overflow (and we really
3097 would prefer this precondition to be considered True at run time).
3098
3099
3100 .. _Management_of_Overflows_in_GNAT:
3101
3102 Management of Overflows in GNAT
3103 -------------------------------
3104
3105 To deal with the portability issue, and with the problem of
3106 mathematical versus run-time interpretation of the expressions in
3107 assertions, GNAT provides comprehensive control over the handling
3108 of intermediate overflow. GNAT can operate in three modes, and
3109 furthemore, permits separate selection of operating modes for
3110 the expressions within assertions (here the term 'assertions'
3111 is used in the technical sense, which includes preconditions and so forth)
3112 and for expressions appearing outside assertions.
3113
3114 The three modes are:
3115
3116 * *Use base type for intermediate operations* (``STRICT``)
3117
3118 In this mode, all intermediate results for predefined arithmetic
3119 operators are computed using the base type, and the result must
3120 be in range of the base type. If this is not the
3121 case then either an exception is raised (if overflow checks are
3122 enabled) or the execution is erroneous (if overflow checks are suppressed).
3123 This is the normal default mode.
3124
3125 * *Most intermediate overflows avoided* (``MINIMIZED``)
3126
3127 In this mode, the compiler attempts to avoid intermediate overflows by
3128 using a larger integer type, typically ``Long_Long_Integer``,
3129 as the type in which arithmetic is
3130 performed for predefined arithmetic operators. This may be slightly more
3131 expensive at
3132 run time (compared to suppressing intermediate overflow checks), though
3133 the cost is negligible on modern 64-bit machines. For the examples given
3134 earlier, no intermediate overflows would have resulted in exceptions,
3135 since the intermediate results are all in the range of
3136 ``Long_Long_Integer`` (typically 64-bits on nearly all implementations
3137 of GNAT). In addition, if checks are enabled, this reduces the number of
3138 checks that must be made, so this choice may actually result in an
3139 improvement in space and time behavior.
3140
3141 However, there are cases where ``Long_Long_Integer`` is not large
3142 enough, consider the following example:
3143
3144 .. code-block:: ada
3145
3146 procedure R (A, B, C, D : Integer) with
3147 Pre => (A**2 * B**2) / (C**2 * D**2) <= 10;
3148
3149 where ``A`` = ``B`` = ``C`` = ``D`` = ``Integer'Last``.
3150 Now the intermediate results are
3151 out of the range of ``Long_Long_Integer`` even though the final result
3152 is in range and the precondition is True (from a mathematical point
3153 of view). In such a case, operating in this mode, an overflow occurs
3154 for the intermediate computation (which is why this mode
3155 says *most* intermediate overflows are avoided). In this case,
3156 an exception is raised if overflow checks are enabled, and the
3157 execution is erroneous if overflow checks are suppressed.
3158
3159 * *All intermediate overflows avoided* (``ELIMINATED``)
3160
3161 In this mode, the compiler avoids all intermediate overflows
3162 by using arbitrary precision arithmetic as required. In this
3163 mode, the above example with ``A**2 * B**2`` would
3164 not cause intermediate overflow, because the intermediate result
3165 would be evaluated using sufficient precision, and the result
3166 of evaluating the precondition would be True.
3167
3168 This mode has the advantage of avoiding any intermediate
3169 overflows, but at the expense of significant run-time overhead,
3170 including the use of a library (included automatically in this
3171 mode) for multiple-precision arithmetic.
3172
3173 This mode provides cleaner semantics for assertions, since now
3174 the run-time behavior emulates true arithmetic behavior for the
3175 predefined arithmetic operators, meaning that there is never a
3176 conflict between the mathematical view of the assertion, and its
3177 run-time behavior.
3178
3179 Note that in this mode, the behavior is unaffected by whether or
3180 not overflow checks are suppressed, since overflow does not occur.
3181 It is possible for gigantic intermediate expressions to raise
3182 ``Storage_Error`` as a result of attempting to compute the
3183 results of such expressions (e.g. ``Integer'Last ** Integer'Last``)
3184 but overflow is impossible.
3185
3186
3187 Note that these modes apply only to the evaluation of predefined
3188 arithmetic, membership, and comparison operators for signed integer
3189 arithmetic.
3190
3191 For fixed-point arithmetic, checks can be suppressed. But if checks
3192 are enabled
3193 then fixed-point values are always checked for overflow against the
3194 base type for intermediate expressions (that is such checks always
3195 operate in the equivalent of ``STRICT`` mode).
3196
3197 For floating-point, on nearly all architectures, ``Machine_Overflows``
3198 is False, and IEEE infinities are generated, so overflow exceptions
3199 are never raised. If you want to avoid infinities, and check that
3200 final results of expressions are in range, then you can declare a
3201 constrained floating-point type, and range checks will be carried
3202 out in the normal manner (with infinite values always failing all
3203 range checks).
3204
3205
3206 .. _Specifying_the_Desired_Mode:
3207
3208 Specifying the Desired Mode
3209 ---------------------------
3210
3211 .. index:: pragma Overflow_Mode
3212
3213 The desired mode of for handling intermediate overflow can be specified using
3214 either the ``Overflow_Mode`` pragma or an equivalent compiler switch.
3215 The pragma has the form
3216
3217 .. code-block:: ada
3218
3219 pragma Overflow_Mode ([General =>] MODE [, [Assertions =>] MODE]);
3220
3221 where ``MODE`` is one of
3222
3223 * ``STRICT``: intermediate overflows checked (using base type)
3224 * ``MINIMIZED``: minimize intermediate overflows
3225 * ``ELIMINATED``: eliminate intermediate overflows
3226
3227 The case is ignored, so ``MINIMIZED``, ``Minimized`` and
3228 ``minimized`` all have the same effect.
3229
3230 If only the ``General`` parameter is present, then the given ``MODE`` applies
3231 to expressions both within and outside assertions. If both arguments
3232 are present, then ``General`` applies to expressions outside assertions,
3233 and ``Assertions`` applies to expressions within assertions. For example:
3234
3235 .. code-block:: ada
3236
3237 pragma Overflow_Mode
3238 (General => Minimized, Assertions => Eliminated);
3239
3240 specifies that general expressions outside assertions be evaluated
3241 in 'minimize intermediate overflows' mode, and expressions within
3242 assertions be evaluated in 'eliminate intermediate overflows' mode.
3243 This is often a reasonable choice, avoiding excessive overhead
3244 outside assertions, but assuring a high degree of portability
3245 when importing code from another compiler, while incurring
3246 the extra overhead for assertion expressions to ensure that
3247 the behavior at run time matches the expected mathematical
3248 behavior.
3249
3250 The ``Overflow_Mode`` pragma has the same scoping and placement
3251 rules as pragma ``Suppress``, so it can occur either as a
3252 configuration pragma, specifying a default for the whole
3253 program, or in a declarative scope, where it applies to the
3254 remaining declarations and statements in that scope.
3255
3256 Note that pragma ``Overflow_Mode`` does not affect whether
3257 overflow checks are enabled or suppressed. It only controls the
3258 method used to compute intermediate values. To control whether
3259 overflow checking is enabled or suppressed, use pragma ``Suppress``
3260 or ``Unsuppress`` in the usual manner.
3261
3262
3263 .. index:: -gnato? (gcc)
3264 .. index:: -gnato?? (gcc)
3265
3266 Additionally, a compiler switch :switch:`-gnato?` or :switch:`-gnato??`
3267 can be used to control the checking mode default (which can be subsequently
3268 overridden using pragmas).
3269
3270 Here ``?`` is one of the digits ``1`` through ``3``:
3271
3272 ====== ======================================================
3273 ``1`` use base type for intermediate operations (``STRICT``)
3274 ``2`` minimize intermediate overflows (``MINIMIZED``)
3275 ``3`` eliminate intermediate overflows (``ELIMINATED``)
3276 ====== ======================================================
3277
3278 As with the pragma, if only one digit appears then it applies to all
3279 cases; if two digits are given, then the first applies outside
3280 assertions, and the second within assertions. Thus the equivalent
3281 of the example pragma above would be
3282 :switch:`-gnato23`.
3283
3284 If no digits follow the :switch:`-gnato`, then it is equivalent to
3285 :switch:`-gnato11`,
3286 causing all intermediate operations to be computed using the base
3287 type (``STRICT`` mode).
3288
3289
3290 .. _Default_Settings:
3291
3292 Default Settings
3293 ----------------
3294
3295 The default mode for overflow checks is
3296
3297 ::
3298
3299 General => Strict
3300
3301 which causes all computations both inside and outside assertions to use
3302 the base type.
3303
3304 This retains compatibility with previous versions of
3305 GNAT which suppressed overflow checks by default and always
3306 used the base type for computation of intermediate results.
3307
3308 .. Sphinx allows no emphasis within :index: role. As a workaround we
3309 point the index to "switch" and use emphasis for "-gnato".
3310
3311 The :index:`switch <-gnato (gcc)>` :switch:`-gnato` (with no digits following)
3312 is equivalent to
3313
3314 ::
3315
3316 General => Strict
3317
3318 which causes overflow checking of all intermediate overflows
3319 both inside and outside assertions against the base type.
3320
3321 The pragma ``Suppress (Overflow_Check)`` disables overflow
3322 checking, but it has no effect on the method used for computing
3323 intermediate results.
3324
3325 The pragma ``Unsuppress (Overflow_Check)`` enables overflow
3326 checking, but it has no effect on the method used for computing
3327 intermediate results.
3328
3329
3330 .. _Implementation_Notes:
3331
3332 Implementation Notes
3333 --------------------
3334
3335 In practice on typical 64-bit machines, the ``MINIMIZED`` mode is
3336 reasonably efficient, and can be generally used. It also helps
3337 to ensure compatibility with code imported from some other
3338 compiler to GNAT.
3339
3340 Setting all intermediate overflows checking (``CHECKED`` mode)
3341 makes sense if you want to
3342 make sure that your code is compatible with any other possible
3343 Ada implementation. This may be useful in ensuring portability
3344 for code that is to be exported to some other compiler than GNAT.
3345
3346 The Ada standard allows the reassociation of expressions at
3347 the same precedence level if no parentheses are present. For
3348 example, ``A+B+C`` parses as though it were ``(A+B)+C``, but
3349 the compiler can reintepret this as ``A+(B+C)``, possibly
3350 introducing or eliminating an overflow exception. The GNAT
3351 compiler never takes advantage of this freedom, and the
3352 expression ``A+B+C`` will be evaluated as ``(A+B)+C``.
3353 If you need the other order, you can write the parentheses
3354 explicitly ``A+(B+C)`` and GNAT will respect this order.
3355
3356 The use of ``ELIMINATED`` mode will cause the compiler to
3357 automatically include an appropriate arbitrary precision
3358 integer arithmetic package. The compiler will make calls
3359 to this package, though only in cases where it cannot be
3360 sure that ``Long_Long_Integer`` is sufficient to guard against
3361 intermediate overflows. This package does not use dynamic
3362 alllocation, but it does use the secondary stack, so an
3363 appropriate secondary stack package must be present (this
3364 is always true for standard full Ada, but may require
3365 specific steps for restricted run times such as ZFP).
3366
3367 Although ``ELIMINATED`` mode causes expressions to use arbitrary
3368 precision arithmetic, avoiding overflow, the final result
3369 must be in an appropriate range. This is true even if the
3370 final result is of type ``[Long_[Long_]]Integer'Base``, which
3371 still has the same bounds as its associated constrained
3372 type at run-time.
3373
3374 Currently, the ``ELIMINATED`` mode is only available on target
3375 platforms for which ``Long_Long_Integer`` is 64-bits (nearly all GNAT
3376 platforms).
3377
3378
3379
3380 .. _Performing_Dimensionality_Analysis_in_GNAT:
3381
3382 Performing Dimensionality Analysis in GNAT
3383 ==========================================
3384
3385 .. index:: Dimensionality analysis
3386
3387 The GNAT compiler supports dimensionality checking. The user can
3388 specify physical units for objects, and the compiler will verify that uses
3389 of these objects are compatible with their dimensions, in a fashion that is
3390 familiar to engineering practice. The dimensions of algebraic expressions
3391 (including powers with static exponents) are computed from their constituents.
3392
3393 .. index:: Dimension_System aspect
3394 .. index:: Dimension aspect
3395
3396 This feature depends on Ada 2012 aspect specifications, and is available from
3397 version 7.0.1 of GNAT onwards.
3398 The GNAT-specific aspect ``Dimension_System``
3399 allows you to define a system of units; the aspect ``Dimension``
3400 then allows the user to declare dimensioned quantities within a given system.
3401 (These aspects are described in the *Implementation Defined Aspects*
3402 chapter of the *GNAT Reference Manual*).
3403
3404 The major advantage of this model is that it does not require the declaration of
3405 multiple operators for all possible combinations of types: it is only necessary
3406 to use the proper subtypes in object declarations.
3407
3408 .. index:: System.Dim.Mks package (GNAT library)
3409 .. index:: MKS_Type type
3410
3411 The simplest way to impose dimensionality checking on a computation is to make
3412 use of the package ``System.Dim.Mks``,
3413 which is part of the GNAT library. This
3414 package defines a floating-point type ``MKS_Type``,
3415 for which a sequence of
3416 dimension names are specified, together with their conventional abbreviations.
3417 The following should be read together with the full specification of the
3418 package, in file :file:`s-dimmks.ads`.
3419
3420 .. index:: s-dimmks.ads file
3421
3422 .. code-block:: ada
3423
3424 type Mks_Type is new Long_Long_Float
3425 with
3426 Dimension_System => (
3427 (Unit_Name => Meter, Unit_Symbol => 'm', Dim_Symbol => 'L'),
3428 (Unit_Name => Kilogram, Unit_Symbol => "kg", Dim_Symbol => 'M'),
3429 (Unit_Name => Second, Unit_Symbol => 's', Dim_Symbol => 'T'),
3430 (Unit_Name => Ampere, Unit_Symbol => 'A', Dim_Symbol => 'I'),
3431 (Unit_Name => Kelvin, Unit_Symbol => 'K', Dim_Symbol => "Theta"),
3432 (Unit_Name => Mole, Unit_Symbol => "mol", Dim_Symbol => 'N'),
3433 (Unit_Name => Candela, Unit_Symbol => "cd", Dim_Symbol => 'J'));
3434
3435 The package then defines a series of subtypes that correspond to these
3436 conventional units. For example:
3437
3438 .. code-block:: ada
3439
3440 subtype Length is Mks_Type
3441 with
3442 Dimension => (Symbol => 'm', Meter => 1, others => 0);
3443
3444 and similarly for ``Mass``, ``Time``, ``Electric_Current``,
3445 ``Thermodynamic_Temperature``, ``Amount_Of_Substance``, and
3446 ``Luminous_Intensity`` (the standard set of units of the SI system).
3447
3448 The package also defines conventional names for values of each unit, for
3449 example:
3450
3451 .. code-block:: ada
3452
3453 m : constant Length := 1.0;
3454 kg : constant Mass := 1.0;
3455 s : constant Time := 1.0;
3456 A : constant Electric_Current := 1.0;
3457
3458 as well as useful multiples of these units:
3459
3460 .. code-block:: ada
3461
3462 cm : constant Length := 1.0E-02;
3463 g : constant Mass := 1.0E-03;
3464 min : constant Time := 60.0;
3465 day : constant Time := 60.0 * 24.0 * min;
3466 ...
3467
3468 Using this package, you can then define a derived unit by
3469 providing the aspect that
3470 specifies its dimensions within the MKS system, as well as the string to
3471 be used for output of a value of that unit:
3472
3473 .. code-block:: ada
3474
3475 subtype Acceleration is Mks_Type
3476 with Dimension => ("m/sec^2",
3477 Meter => 1,
3478 Second => -2,
3479 others => 0);
3480
3481 Here is a complete example of use:
3482
3483 .. code-block:: ada
3484
3485 with System.Dim.MKS; use System.Dim.Mks;
3486 with System.Dim.Mks_IO; use System.Dim.Mks_IO;
3487 with Text_IO; use Text_IO;
3488 procedure Free_Fall is
3489 subtype Acceleration is Mks_Type
3490 with Dimension => ("m/sec^2", 1, 0, -2, others => 0);
3491 G : constant acceleration := 9.81 * m / (s ** 2);
3492 T : Time := 10.0*s;
3493 Distance : Length;
3494
3495 begin
3496 Put ("Gravitational constant: ");
3497 Put (G, Aft => 2, Exp => 0); Put_Line ("");
3498 Distance := 0.5 * G * T ** 2;
3499 Put ("distance travelled in 10 seconds of free fall ");
3500 Put (Distance, Aft => 2, Exp => 0);
3501 Put_Line ("");
3502 end Free_Fall;
3503
3504 Execution of this program yields:
3505
3506 ::
3507
3508 Gravitational constant: 9.81 m/sec^2
3509 distance travelled in 10 seconds of free fall 490.50 m
3510
3511 However, incorrect assignments such as:
3512
3513 .. code-block:: ada
3514
3515 Distance := 5.0;
3516 Distance := 5.0 * kg;
3517
3518 are rejected with the following diagnoses:
3519
3520 ::
3521
3522 Distance := 5.0;
3523 >>> dimensions mismatch in assignment
3524 >>> left-hand side has dimension [L]
3525 >>> right-hand side is dimensionless
3526
3527 Distance := 5.0 * kg:
3528 >>> dimensions mismatch in assignment
3529 >>> left-hand side has dimension [L]
3530 >>> right-hand side has dimension [M]
3531
3532 The dimensions of an expression are properly displayed, even if there is
3533 no explicit subtype for it. If we add to the program:
3534
3535 .. code-block:: ada
3536
3537 Put ("Final velocity: ");
3538 Put (G * T, Aft =>2, Exp =>0);
3539 Put_Line ("");
3540
3541 then the output includes:
3542
3543 ::
3544
3545 Final velocity: 98.10 m.s**(-1)
3546
3547
3548 .. index:: Dimensionable type
3549 .. index:: Dimensioned subtype
3550
3551 The type ``Mks_Type`` is said to be a *dimensionable type* since it has a
3552 ``Dimension_System`` aspect, and the subtypes ``Length``, ``Mass``, etc.,
3553 are said to be *dimensioned subtypes* since each one has a ``Dimension``
3554 aspect.
3555
3556 .. index:: Dimension Vector (for a dimensioned subtype)
3557 .. index:: Dimension aspect
3558 .. index:: Dimension_System aspect
3559
3560 The ``Dimension`` aspect of a dimensioned subtype ``S`` defines a mapping
3561 from the base type's Unit_Names to integer (or, more generally, rational)
3562 values. This mapping is the *dimension vector* (also referred to as the
3563 *dimensionality*) for that subtype, denoted by ``DV(S)``, and thus for each
3564 object of that subtype. Intuitively, the value specified for each
3565 ``Unit_Name`` is the exponent associated with that unit; a zero value
3566 means that the unit is not used. For example:
3567
3568 .. code-block:: ada
3569
3570 declare
3571 Acc : Acceleration;
3572 ...
3573 begin
3574 ...
3575 end;
3576
3577 Here ``DV(Acc)`` = ``DV(Acceleration)`` =
3578 ``(Meter=>1, Kilogram=>0, Second=>-2, Ampere=>0, Kelvin=>0, Mole=>0, Candela=>0)``.
3579 Symbolically, we can express this as ``Meter / Second**2``.
3580
3581 The dimension vector of an arithmetic expression is synthesized from the
3582 dimension vectors of its components, with compile-time dimensionality checks
3583 that help prevent mismatches such as using an ``Acceleration`` where a
3584 ``Length`` is required.
3585
3586 The dimension vector of the result of an arithmetic expression *expr*, or
3587 :samp:`DV({expr})`, is defined as follows, assuming conventional
3588 mathematical definitions for the vector operations that are used:
3589
3590 * If *expr* is of the type *universal_real*, or is not of a dimensioned subtype,
3591 then *expr* is dimensionless; :samp:`DV({expr})` is the empty vector.
3592
3593 * :samp:`DV({op expr})`, where *op* is a unary operator, is :samp:`DV({expr})`
3594
3595 * :samp:`DV({expr1 op expr2})` where *op* is "+" or "-" is :samp:`DV({expr1})`
3596 provided that :samp:`DV({expr1})` = :samp:`DV({expr2})`.
3597 If this condition is not met then the construct is illegal.
3598
3599 * :samp:`DV({expr1} * {expr2})` is :samp:`DV({expr1})` + :samp:`DV({expr2})`,
3600 and :samp:`DV({expr1} / {expr2})` = :samp:`DV({expr1})` - :samp:`DV({expr2})`.
3601 In this context if one of the *expr*\ s is dimensionless then its empty
3602 dimension vector is treated as ``(others => 0)``.
3603
3604 * :samp:`DV({expr} ** {power})` is *power* * :samp:`DV({expr})`,
3605 provided that *power* is a static rational value. If this condition is not
3606 met then the construct is illegal.
3607
3608 Note that, by the above rules, it is illegal to use binary "+" or "-" to
3609 combine a dimensioned and dimensionless value. Thus an expression such as
3610 ``acc-10.0`` is illegal, where ``acc`` is an object of subtype
3611 ``Acceleration``.
3612
3613 The dimensionality checks for relationals use the same rules as
3614 for "+" and "-", except when comparing to a literal; thus
3615
3616 .. code-block:: ada
3617
3618 acc > len
3619
3620 is equivalent to
3621
3622 .. code-block:: ada
3623
3624 acc-len > 0.0
3625
3626 and is thus illegal, but
3627
3628 .. code-block:: ada
3629
3630 acc > 10.0
3631
3632 is accepted with a warning. Analogously a conditional expression requires the
3633 same dimension vector for each branch (with no exception for literals).
3634
3635 The dimension vector of a type conversion :samp:`T({expr})` is defined
3636 as follows, based on the nature of ``T``:
3637
3638 * If ``T`` is a dimensioned subtype then :samp:`DV(T({expr}))` is ``DV(T)``
3639 provided that either *expr* is dimensionless or
3640 :samp:`DV(T)` = :samp:`DV({expr})`. The conversion is illegal
3641 if *expr* is dimensioned and :samp:`DV({expr})` /= ``DV(T)``.
3642 Note that vector equality does not require that the corresponding
3643 Unit_Names be the same.
3644
3645 As a consequence of the above rule, it is possible to convert between
3646 different dimension systems that follow the same international system
3647 of units, with the seven physical components given in the standard order
3648 (length, mass, time, etc.). Thus a length in meters can be converted to
3649 a length in inches (with a suitable conversion factor) but cannot be
3650 converted, for example, to a mass in pounds.
3651
3652 * If ``T`` is the base type for *expr* (and the dimensionless root type of
3653 the dimension system), then :samp:`DV(T({expr}))` is ``DV(expr)``.
3654 Thus, if *expr* is of a dimensioned subtype of ``T``, the conversion may
3655 be regarded as a "view conversion" that preserves dimensionality.
3656
3657 This rule makes it possible to write generic code that can be instantiated
3658 with compatible dimensioned subtypes. The generic unit will contain
3659 conversions that will consequently be present in instantiations, but
3660 conversions to the base type will preserve dimensionality and make it
3661 possible to write generic code that is correct with respect to
3662 dimensionality.
3663
3664 * Otherwise (i.e., ``T`` is neither a dimensioned subtype nor a dimensionable
3665 base type), :samp:`DV(T({expr}))` is the empty vector. Thus a dimensioned
3666 value can be explicitly converted to a non-dimensioned subtype, which
3667 of course then escapes dimensionality analysis.
3668
3669 The dimension vector for a type qualification :samp:`T'({expr})` is the same
3670 as for the type conversion :samp:`T({expr})`.
3671
3672 An assignment statement
3673
3674 .. code-block:: ada
3675
3676 Source := Target;
3677
3678 requires ``DV(Source)`` = ``DV(Target)``, and analogously for parameter
3679 passing (the dimension vector for the actual parameter must be equal to the
3680 dimension vector for the formal parameter).
3681
3682
3683 .. _Stack_Related_Facilities:
3684
3685 Stack Related Facilities
3686 ========================
3687
3688 This section describes some useful tools associated with stack
3689 checking and analysis. In
3690 particular, it deals with dynamic and static stack usage measurements.
3691
3692 .. _Stack_Overflow_Checking:
3693
3694 Stack Overflow Checking
3695 -----------------------
3696
3697 .. index:: Stack Overflow Checking
3698
3699 .. index:: -fstack-check (gcc)
3700
3701 For most operating systems, ``gcc`` does not perform stack overflow
3702 checking by default. This means that if the main environment task or
3703 some other task exceeds the available stack space, then unpredictable
3704 behavior will occur. Most native systems offer some level of protection by
3705 adding a guard page at the end of each task stack. This mechanism is usually
3706 not enough for dealing properly with stack overflow situations because
3707 a large local variable could "jump" above the guard page.
3708 Furthermore, when the
3709 guard page is hit, there may not be any space left on the stack for executing
3710 the exception propagation code. Enabling stack checking avoids
3711 such situations.
3712
3713 To activate stack checking, compile all units with the ``gcc`` option
3714 :switch:`-fstack-check`. For example:
3715
3716 ::
3717
3718 $ gcc -c -fstack-check package1.adb
3719
3720 Units compiled with this option will generate extra instructions to check
3721 that any use of the stack (for procedure calls or for declaring local
3722 variables in declare blocks) does not exceed the available stack space.
3723 If the space is exceeded, then a ``Storage_Error`` exception is raised.
3724
3725 For declared tasks, the stack size is controlled by the size
3726 given in an applicable ``Storage_Size`` pragma or by the value specified
3727 at bind time with ``-d`` (:ref:`Switches_for_gnatbind`) or is set to
3728 the default size as defined in the GNAT runtime otherwise.
3729
3730 .. index:: GNAT_STACK_LIMIT
3731
3732 For the environment task, the stack size depends on
3733 system defaults and is unknown to the compiler. Stack checking
3734 may still work correctly if a fixed
3735 size stack is allocated, but this cannot be guaranteed.
3736 To ensure that a clean exception is signalled for stack
3737 overflow, set the environment variable
3738 :envvar:`GNAT_STACK_LIMIT` to indicate the maximum
3739 stack area that can be used, as in:
3740
3741 ::
3742
3743 $ SET GNAT_STACK_LIMIT 1600
3744
3745 The limit is given in kilobytes, so the above declaration would
3746 set the stack limit of the environment task to 1.6 megabytes.
3747 Note that the only purpose of this usage is to limit the amount
3748 of stack used by the environment task. If it is necessary to
3749 increase the amount of stack for the environment task, then this
3750 is an operating systems issue, and must be addressed with the
3751 appropriate operating systems commands.
3752
3753
3754 .. _Static_Stack_Usage_Analysis:
3755
3756 Static Stack Usage Analysis
3757 ---------------------------
3758
3759 .. index:: Static Stack Usage Analysis
3760
3761 .. index:: -fstack-usage
3762
3763 A unit compiled with ``-fstack-usage`` will generate an extra file
3764 that specifies
3765 the maximum amount of stack used, on a per-function basis.
3766 The file has the same
3767 basename as the target object file with a :file:`.su` extension.
3768 Each line of this file is made up of three fields:
3769
3770 * The name of the function.
3771 * A number of bytes.
3772 * One or more qualifiers: ``static``, ``dynamic``, ``bounded``.
3773
3774 The second field corresponds to the size of the known part of the function
3775 frame.
3776
3777 The qualifier ``static`` means that the function frame size
3778 is purely static.
3779 It usually means that all local variables have a static size.
3780 In this case, the second field is a reliable measure of the function stack
3781 utilization.
3782
3783 The qualifier ``dynamic`` means that the function frame size is not static.
3784 It happens mainly when some local variables have a dynamic size. When this
3785 qualifier appears alone, the second field is not a reliable measure
3786 of the function stack analysis. When it is qualified with ``bounded``, it
3787 means that the second field is a reliable maximum of the function stack
3788 utilization.
3789
3790 A unit compiled with ``-Wstack-usage`` will issue a warning for each
3791 subprogram whose stack usage might be larger than the specified amount of
3792 bytes. The wording is in keeping with the qualifier documented above.
3793
3794
3795 .. _Dynamic_Stack_Usage_Analysis:
3796
3797 Dynamic Stack Usage Analysis
3798 ----------------------------
3799
3800 It is possible to measure the maximum amount of stack used by a task, by
3801 adding a switch to ``gnatbind``, as:
3802
3803 ::
3804
3805 $ gnatbind -u0 file
3806
3807 With this option, at each task termination, its stack usage is output on
3808 :file:`stderr`.
3809 It is not always convenient to output the stack usage when the program
3810 is still running. Hence, it is possible to delay this output until program
3811 termination. for a given number of tasks specified as the argument of the
3812 ``-u`` option. For instance:
3813
3814 ::
3815
3816 $ gnatbind -u100 file
3817
3818 will buffer the stack usage information of the first 100 tasks to terminate and
3819 output this info at program termination. Results are displayed in four
3820 columns:
3821
3822 ::
3823
3824 Index | Task Name | Stack Size | Stack Usage
3825
3826 where:
3827
3828 * *Index* is a number associated with each task.
3829
3830 * *Task Name* is the name of the task analyzed.
3831
3832 * *Stack Size* is the maximum size for the stack.
3833
3834 * *Stack Usage* is the measure done by the stack analyzer.
3835 In order to prevent overflow, the stack
3836 is not entirely analyzed, and it's not possible to know exactly how
3837 much has actually been used.
3838
3839 The environment task stack, e.g., the stack that contains the main unit, is
3840 only processed when the environment variable GNAT_STACK_LIMIT is set.
3841
3842 The package ``GNAT.Task_Stack_Usage`` provides facilities to get
3843 stack usage reports at run-time. See its body for the details.
3844
3845
3846
3847 .. _Memory_Management_Issues:
3848
3849 Memory Management Issues
3850 ========================
3851
3852 This section describes some useful memory pools provided in the GNAT library
3853 and in particular the GNAT Debug Pool facility, which can be used to detect
3854 incorrect uses of access values (including 'dangling references').
3855
3856 .. only:: PRO or GPL
3857
3858 It also describes the ``gnatmem`` tool, which can be used to track down
3859 "memory leaks".
3860
3861 .. _Some_Useful_Memory_Pools:
3862
3863 Some Useful Memory Pools
3864 ------------------------
3865
3866 .. index:: Memory Pool
3867 .. index:: storage, pool
3868
3869 The ``System.Pool_Global`` package offers the Unbounded_No_Reclaim_Pool
3870 storage pool. Allocations use the standard system call ``malloc`` while
3871 deallocations use the standard system call ``free``. No reclamation is
3872 performed when the pool goes out of scope. For performance reasons, the
3873 standard default Ada allocators/deallocators do not use any explicit storage
3874 pools but if they did, they could use this storage pool without any change in
3875 behavior. That is why this storage pool is used when the user
3876 manages to make the default implicit allocator explicit as in this example:
3877
3878 .. code-block:: ada
3879
3880 type T1 is access Something;
3881 -- no Storage pool is defined for T2
3882
3883 type T2 is access Something_Else;
3884 for T2'Storage_Pool use T1'Storage_Pool;
3885 -- the above is equivalent to
3886 for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
3887
3888 The ``System.Pool_Local`` package offers the ``Unbounded_Reclaim_Pool`` storage
3889 pool. The allocation strategy is similar to ``Pool_Local``
3890 except that the all
3891 storage allocated with this pool is reclaimed when the pool object goes out of
3892 scope. This pool provides a explicit mechanism similar to the implicit one
3893 provided by several Ada 83 compilers for allocations performed through a local
3894 access type and whose purpose was to reclaim memory when exiting the
3895 scope of a given local access. As an example, the following program does not
3896 leak memory even though it does not perform explicit deallocation:
3897
3898 .. code-block:: ada
3899
3900 with System.Pool_Local;
3901 procedure Pooloc1 is
3902 procedure Internal is
3903 type A is access Integer;
3904 X : System.Pool_Local.Unbounded_Reclaim_Pool;
3905 for A'Storage_Pool use X;
3906 v : A;
3907 begin
3908 for I in 1 .. 50 loop
3909 v := new Integer;
3910 end loop;
3911 end Internal;
3912 begin
3913 for I in 1 .. 100 loop
3914 Internal;
3915 end loop;
3916 end Pooloc1;
3917
3918 The ``System.Pool_Size`` package implements the ``Stack_Bounded_Pool`` used when
3919 ``Storage_Size`` is specified for an access type.
3920 The whole storage for the pool is
3921 allocated at once, usually on the stack at the point where the access type is
3922 elaborated. It is automatically reclaimed when exiting the scope where the
3923 access type is defined. This package is not intended to be used directly by the
3924 user and it is implicitly used for each such declaration:
3925
3926 .. code-block:: ada
3927
3928 type T1 is access Something;
3929 for T1'Storage_Size use 10_000;
3930
3931
3932 .. _The_GNAT_Debug_Pool_Facility:
3933
3934 The GNAT Debug Pool Facility
3935 ----------------------------
3936
3937 .. index:: Debug Pool
3938 .. index:: storage, pool, memory corruption
3939
3940 The use of unchecked deallocation and unchecked conversion can easily
3941 lead to incorrect memory references. The problems generated by such
3942 references are usually difficult to tackle because the symptoms can be
3943 very remote from the origin of the problem. In such cases, it is
3944 very helpful to detect the problem as early as possible. This is the
3945 purpose of the Storage Pool provided by ``GNAT.Debug_Pools``.
3946
3947 In order to use the GNAT specific debugging pool, the user must
3948 associate a debug pool object with each of the access types that may be
3949 related to suspected memory problems. See Ada Reference Manual 13.11.
3950
3951 .. code-block:: ada
3952
3953 type Ptr is access Some_Type;
3954 Pool : GNAT.Debug_Pools.Debug_Pool;
3955 for Ptr'Storage_Pool use Pool;
3956
3957 ``GNAT.Debug_Pools`` is derived from a GNAT-specific kind of
3958 pool: the ``Checked_Pool``. Such pools, like standard Ada storage pools,
3959 allow the user to redefine allocation and deallocation strategies. They
3960 also provide a checkpoint for each dereference, through the use of
3961 the primitive operation ``Dereference`` which is implicitly called at
3962 each dereference of an access value.
3963
3964 Once an access type has been associated with a debug pool, operations on
3965 values of the type may raise four distinct exceptions,
3966 which correspond to four potential kinds of memory corruption:
3967
3968 * ``GNAT.Debug_Pools.Accessing_Not_Allocated_Storage``
3969 * ``GNAT.Debug_Pools.Accessing_Deallocated_Storage``
3970 * ``GNAT.Debug_Pools.Freeing_Not_Allocated_Storage``
3971 * ``GNAT.Debug_Pools.Freeing_Deallocated_Storage``
3972
3973 For types associated with a Debug_Pool, dynamic allocation is performed using
3974 the standard GNAT allocation routine. References to all allocated chunks of
3975 memory are kept in an internal dictionary. Several deallocation strategies are
3976 provided, whereupon the user can choose to release the memory to the system,
3977 keep it allocated for further invalid access checks, or fill it with an easily
3978 recognizable pattern for debug sessions. The memory pattern is the old IBM
3979 hexadecimal convention: ``16#DEADBEEF#``.
3980
3981 See the documentation in the file g-debpoo.ads for more information on the
3982 various strategies.
3983
3984 Upon each dereference, a check is made that the access value denotes a
3985 properly allocated memory location. Here is a complete example of use of
3986 ``Debug_Pools``, that includes typical instances of memory corruption:
3987
3988 .. code-block:: ada
3989
3990 with Gnat.Io; use Gnat.Io;
3991 with Unchecked_Deallocation;
3992 with Unchecked_Conversion;
3993 with GNAT.Debug_Pools;
3994 with System.Storage_Elements;
3995 with Ada.Exceptions; use Ada.Exceptions;
3996 procedure Debug_Pool_Test is
3997
3998 type T is access Integer;
3999 type U is access all T;
4000
4001 P : GNAT.Debug_Pools.Debug_Pool;
4002 for T'Storage_Pool use P;
4003
4004 procedure Free is new Unchecked_Deallocation (Integer, T);
4005 function UC is new Unchecked_Conversion (U, T);
4006 A, B : aliased T;
4007
4008 procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
4009
4010 begin
4011 Info (P);
4012 A := new Integer;
4013 B := new Integer;
4014 B := A;
4015 Info (P);
4016 Free (A);
4017 begin
4018 Put_Line (Integer'Image(B.all));
4019 exception
4020 when E : others => Put_Line ("raised: " & Exception_Name (E));
4021 end;
4022 begin
4023 Free (B);
4024 exception
4025 when E : others => Put_Line ("raised: " & Exception_Name (E));
4026 end;
4027 B := UC(A'Access);
4028 begin
4029 Put_Line (Integer'Image(B.all));
4030 exception
4031 when E : others => Put_Line ("raised: " & Exception_Name (E));
4032 end;
4033 begin
4034 Free (B);
4035 exception
4036 when E : others => Put_Line ("raised: " & Exception_Name (E));
4037 end;
4038 Info (P);
4039 end Debug_Pool_Test;
4040
4041 The debug pool mechanism provides the following precise diagnostics on the
4042 execution of this erroneous program:
4043
4044 ::
4045
4046 Debug Pool info:
4047 Total allocated bytes : 0
4048 Total deallocated bytes : 0
4049 Current Water Mark: 0
4050 High Water Mark: 0
4051
4052 Debug Pool info:
4053 Total allocated bytes : 8
4054 Total deallocated bytes : 0
4055 Current Water Mark: 8
4056 High Water Mark: 8
4057
4058 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
4059 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
4060 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
4061 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
4062 Debug Pool info:
4063 Total allocated bytes : 8
4064 Total deallocated bytes : 4
4065 Current Water Mark: 4
4066 High Water Mark: 8
4067
4068 .. only:: PRO or GPL
4069
4070 .. _The_gnatmem_Tool:
4071
4072 The ``gnatmem`` Tool
4073 --------------------
4074
4075 .. index:: ! gnatmem
4076
4077 The ``gnatmem`` utility monitors dynamic allocation and
4078 deallocation activity in a program, and displays information about
4079 incorrect deallocations and possible sources of memory leaks.
4080 It is designed to work in association with a static runtime library
4081 only and in this context provides three types of information:
4082
4083 * General information concerning memory management, such as the total
4084 number of allocations and deallocations, the amount of allocated
4085 memory and the high water mark, i.e., the largest amount of allocated
4086 memory in the course of program execution.
4087
4088 * Backtraces for all incorrect deallocations, that is to say deallocations
4089 which do not correspond to a valid allocation.
4090
4091 * Information on each allocation that is potentially the origin of a memory
4092 leak.
4093
4094 .. _Running_gnatmem:
4095
4096 Running ``gnatmem``
4097 ^^^^^^^^^^^^^^^^^^^
4098
4099 ``gnatmem`` makes use of the output created by the special version of
4100 allocation and deallocation routines that record call information. This allows
4101 it to obtain accurate dynamic memory usage history at a minimal cost to the
4102 execution speed. Note however, that ``gnatmem`` is only supported on
4103 GNU/Linux and Windows.
4104
4105 The ``gnatmem`` command has the form
4106
4107 ::
4108
4109 $ gnatmem [ switches ] user_program
4110
4111 The program must have been linked with the instrumented version of the
4112 allocation and deallocation routines. This is done by linking with the
4113 :file:`libgmem.a` library. For correct symbolic backtrace information,
4114 the user program should be compiled with debugging options
4115 (see :ref:`Switches_for_gcc`). For example to build :file:`my_program`:
4116
4117 ::
4118
4119 $ gnatmake -g my_program -largs -lgmem
4120
4121 As library :file:`libgmem.a` contains an alternate body for package
4122 ``System.Memory``, :file:`s-memory.adb` should not be compiled and linked
4123 when an executable is linked with library :file:`libgmem.a`. It is then not
4124 recommended to use ``gnatmake`` with switch :switch:`-a`.
4125
4126 When :file:`my_program` is executed, the file :file:`gmem.out` is produced.
4127 This file contains information about all allocations and deallocations
4128 performed by the program. It is produced by the instrumented allocations and
4129 deallocations routines and will be used by ``gnatmem``.
4130
4131 In order to produce symbolic backtrace information for allocations and
4132 deallocations performed by the GNAT run-time library, you need to use a
4133 version of that library that has been compiled with the :switch:`-g` switch
4134 (see :ref:`Rebuilding_the_GNAT_Run-Time_Library`).
4135
4136 ``gnatmem`` must be supplied with the :file:`gmem.out` file and the executable to
4137 examine. If the location of :file:`gmem.out` file was not explicitly supplied by
4138 :switch:`-i` switch, gnatmem will assume that this file can be found in the
4139 current directory. For example, after you have executed :file:`my_program`,
4140 :file:`gmem.out` can be analyzed by ``gnatmem`` using the command:
4141
4142 ::
4143
4144 $ gnatmem my_program
4145
4146 This will produce the output with the following format:
4147
4148 ::
4149
4150 $ gnatmem my_program
4151
4152 Global information
4153 ------------------
4154 Total number of allocations : 45
4155 Total number of deallocations : 6
4156 Final Water Mark (non freed mem) : 11.29 Kilobytes
4157 High Water Mark : 11.40 Kilobytes
4158
4159 .
4160 .
4161 .
4162 Allocation Root # 2
4163 -------------------
4164 Number of non freed allocations : 11
4165 Final Water Mark (non freed mem) : 1.16 Kilobytes
4166 High Water Mark : 1.27 Kilobytes
4167 Backtrace :
4168 my_program.adb:23 my_program.alloc
4169 .
4170 .
4171 .
4172
4173 The first block of output gives general information. In this case, the
4174 Ada construct ``new`` was executed 45 times, and only 6 calls to an
4175 Unchecked_Deallocation routine occurred.
4176
4177 Subsequent paragraphs display information on all allocation roots.
4178 An allocation root is a specific point in the execution of the program
4179 that generates some dynamic allocation, such as a ``new``
4180 construct. This root is represented by an execution backtrace (or subprogram
4181 call stack). By default the backtrace depth for allocations roots is 1, so
4182 that a root corresponds exactly to a source location. The backtrace can
4183 be made deeper, to make the root more specific.
4184
4185 .. _Switches_for_gnatmem:
4186
4187 Switches for ``gnatmem``
4188 ^^^^^^^^^^^^^^^^^^^^^^^^
4189
4190 ``gnatmem`` recognizes the following switches:
4191
4192 .. index:: -q (gnatmem)
4193
4194 :samp:`-q`
4195 Quiet. Gives the minimum output needed to identify the origin of the
4196 memory leaks. Omits statistical information.
4197
4198
4199 .. index:: N switch (gnatmem)
4200
4201 :samp:`{N}`
4202 ``N`` is an integer literal (usually between 1 and 10) which controls the
4203 depth of the backtraces defining allocation root. The default value for
4204 N is 1. The deeper the backtrace, the more precise the localization of
4205 the root. Note that the total number of roots can depend on this
4206 parameter. This parameter must be specified *before* the name of the
4207 executable to be analyzed, to avoid ambiguity.
4208
4209
4210 .. index:: -b (gnatmem)
4211
4212 :samp:`-b {N}`
4213 This switch has the same effect as just a depth parameter ``N``.
4214
4215
4216 .. index:: -i (gnatmem)
4217
4218 :samp:`-i {file}`
4219 Do the ``gnatmem`` processing starting from :file:`file`, rather than
4220 :file:`gmem.out` in the current directory.
4221
4222
4223 .. index:: -m (gnatmem)
4224
4225 :samp:`-m {n}`
4226 This switch causes ``gnatmem`` to mask the allocation roots that have less
4227 than n leaks. The default value is 1. Specifying the value of 0 will allow
4228 examination of even the roots that did not result in leaks.
4229
4230
4231 .. index:: -s (gnatmem)
4232
4233 :samp:`-s {order}`
4234 This switch causes ``gnatmem`` to sort the allocation roots according to the
4235 specified order of sort criteria, each identified by a single letter. The
4236 currently supported criteria are ``n``, ``h``, and ``w`` standing respectively for
4237 number of unfreed allocations, high watermark, and final watermark
4238 corresponding to a specific root. The default order is ``nwh``.
4239
4240
4241 .. index:: -t (gnatmem)
4242
4243 :samp:`-t`
4244 This switch causes memory allocated size to be always output in bytes.
4245 Default ``gnatmem`` behavior is to show memory sizes less then 1 kilobyte
4246 in bytes, from 1 kilobyte till 1 megabyte in kilobytes and the rest in
4247 megabytes.
4248
4249
4250 .. _Example_of_gnatmem_Usage:
4251
4252 Example of ``gnatmem`` Usage
4253 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4254
4255 The following example shows the use of ``gnatmem``
4256 on a simple memory-leaking program.
4257 Suppose that we have the following Ada program:
4258
4259 .. code-block:: ada
4260
4261 with Unchecked_Deallocation;
4262 procedure Test_Gm is
4263
4264 type T is array (1..1000) of Integer;
4265 type Ptr is access T;
4266 procedure Free is new Unchecked_Deallocation (T, Ptr);
4267 A : Ptr;
4268
4269 procedure My_Alloc is
4270 begin
4271 A := new T;
4272 end My_Alloc;
4273
4274 procedure My_DeAlloc is
4275 B : Ptr := A;
4276 begin
4277 Free (B);
4278 end My_DeAlloc;
4279
4280 begin
4281 My_Alloc;
4282 for I in 1 .. 5 loop
4283 for J in I .. 5 loop
4284 My_Alloc;
4285 end loop;
4286 My_Dealloc;
4287 end loop;
4288 end;
4289
4290 The program needs to be compiled with the debugging option and linked with
4291 the ``gmem`` library:
4292
4293 ::
4294
4295 $ gnatmake -g test_gm -largs -lgmem
4296
4297 Then we execute the program as usual:
4298
4299 ::
4300
4301 $ test_gm
4302
4303 Then ``gnatmem`` is invoked simply with
4304
4305 ::
4306
4307 $ gnatmem test_gm
4308
4309 which produces the following output (result may vary on different platforms):
4310
4311 ::
4312
4313 Global information
4314 ------------------
4315 Total number of allocations : 18
4316 Total number of deallocations : 5
4317 Final Water Mark (non freed mem) : 53.00 Kilobytes
4318 High Water Mark : 56.90 Kilobytes
4319
4320 Allocation Root # 1
4321 -------------------
4322 Number of non freed allocations : 11
4323 Final Water Mark (non freed mem) : 42.97 Kilobytes
4324 High Water Mark : 46.88 Kilobytes
4325 Backtrace :
4326 test_gm.adb:11 test_gm.my_alloc
4327
4328 Allocation Root # 2
4329 -------------------
4330 Number of non freed allocations : 1
4331 Final Water Mark (non freed mem) : 10.02 Kilobytes
4332 High Water Mark : 10.02 Kilobytes
4333 Backtrace :
4334 s-secsta.adb:81 system.secondary_stack.ss_init
4335
4336 Allocation Root # 3
4337 -------------------
4338 Number of non freed allocations : 1
4339 Final Water Mark (non freed mem) : 12 Bytes
4340 High Water Mark : 12 Bytes
4341 Backtrace :
4342 s-secsta.adb:181 system.secondary_stack.ss_init
4343
4344
4345 Note that the GNAT runtime contains itself a certain number of
4346 allocations that have no corresponding deallocation,
4347 as shown here for root #2 and root #3.
4348 This is a normal behavior when the number of non-freed allocations
4349 is one, it allocates dynamic data structures that the run time needs for
4350 the complete lifetime of the program. Note also that there is only one
4351 allocation root in the user program with a single line back trace:
4352 test_gm.adb:11 test_gm.my_alloc, whereas a careful analysis of the
4353 program shows that 'My_Alloc' is called at 2 different points in the
4354 source (line 21 and line 24). If those two allocation roots need to be
4355 distinguished, the backtrace depth parameter can be used:
4356
4357 ::
4358
4359 $ gnatmem 3 test_gm
4360
4361 which will give the following output:
4362
4363
4364 ::
4365
4366 Global information
4367 ------------------
4368 Total number of allocations : 18
4369 Total number of deallocations : 5
4370 Final Water Mark (non freed mem) : 53.00 Kilobytes
4371 High Water Mark : 56.90 Kilobytes
4372
4373 Allocation Root # 1
4374 -------------------
4375 Number of non freed allocations : 10
4376 Final Water Mark (non freed mem) : 39.06 Kilobytes
4377 High Water Mark : 42.97 Kilobytes
4378 Backtrace :
4379 test_gm.adb:11 test_gm.my_alloc
4380 test_gm.adb:24 test_gm
4381 b_test_gm.c:52 main
4382
4383 Allocation Root # 2
4384 -------------------
4385 Number of non freed allocations : 1
4386 Final Water Mark (non freed mem) : 10.02 Kilobytes
4387 High Water Mark : 10.02 Kilobytes
4388 Backtrace :
4389 s-secsta.adb:81 system.secondary_stack.ss_init
4390 s-secsta.adb:283 <system__secondary_stack___elabb>
4391 b_test_gm.c:33 adainit
4392
4393 Allocation Root # 3
4394 -------------------
4395 Number of non freed allocations : 1
4396 Final Water Mark (non freed mem) : 3.91 Kilobytes
4397 High Water Mark : 3.91 Kilobytes
4398 Backtrace :
4399 test_gm.adb:11 test_gm.my_alloc
4400 test_gm.adb:21 test_gm
4401 b_test_gm.c:52 main
4402
4403 Allocation Root # 4
4404 -------------------
4405 Number of non freed allocations : 1
4406 Final Water Mark (non freed mem) : 12 Bytes
4407 High Water Mark : 12 Bytes
4408 Backtrace :
4409 s-secsta.adb:181 system.secondary_stack.ss_init
4410 s-secsta.adb:283 <system__secondary_stack___elabb>
4411 b_test_gm.c:33 adainit
4412
4413 The allocation root #1 of the first example has been split in 2 roots #1
4414 and #3, thanks to the more precise associated backtrace.