comparison gcc/ada/doc/gnat_ugn/gnat_and_program_execution.rst @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
15 ************************** 15 **************************
16 16
17 This chapter covers several topics: 17 This chapter covers several topics:
18 18
19 * `Running and Debugging Ada Programs`_ 19 * `Running and Debugging Ada Programs`_
20 * `Code Coverage and Profiling`_ 20 * `Profiling`_
21 * `Improving Performance`_ 21 * `Improving Performance`_
22 * `Overflow Check Handling in GNAT`_ 22 * `Overflow Check Handling in GNAT`_
23 * `Performing Dimensionality Analysis in GNAT`_ 23 * `Performing Dimensionality Analysis in GNAT`_
24 * `Stack Related Facilities`_ 24 * `Stack Related Facilities`_
25 * `Memory Management Issues`_ 25 * `Memory Management Issues`_
1204 documentation 1204 documentation
1205 <http://docs.adacore.com/gdb-docs/html/gdb.html#Pretty_002dPrinter-Commands>`_ 1205 <http://docs.adacore.com/gdb-docs/html/gdb.html#Pretty_002dPrinter-Commands>`_
1206 for more information. 1206 for more information.
1207 1207
1208 1208
1209 .. index:: Code Coverage
1210 .. index:: Profiling 1209 .. index:: Profiling
1211 1210
1212 1211
1213 .. _Code_Coverage_and_Profiling: 1212 .. _Profiling:
1214 1213
1215 Code Coverage and Profiling 1214 Profiling
1216 =========================== 1215 =========
1217 1216
1218 This section describes how to use the ``gcov`` coverage testing tool and 1217 This section describes how to use the the ``gprof`` profiler tool on Ada
1219 the ``gprof`` profiler tool on Ada programs. 1218 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 1219
1307 .. index:: ! gprof 1220 .. index:: ! gprof
1308 .. index:: Profiling 1221 .. index:: Profiling
1309 1222
1310 .. _Profiling_an_Ada_Program_with_gprof: 1223 .. _Profiling_an_Ada_Program_with_gprof:
1322 ``gprof`` is the standard GNU profiling tool; it has been enhanced to 1235 ``gprof`` is the standard GNU profiling tool; it has been enhanced to
1323 better handle Ada programs and multitasking. 1236 better handle Ada programs and multitasking.
1324 It is currently supported on the following platforms 1237 It is currently supported on the following platforms
1325 1238
1326 * linux x86/x86_64 1239 * linux x86/x86_64
1327 * solaris sparc/sparc64/x86
1328 * windows x86 1240 * windows x86
1329 1241
1330 In order to profile a program using ``gprof``, several steps are needed: 1242 In order to profile a program using ``gprof``, several steps are needed:
1331 1243
1332 #. Instrument the code, which requires a full recompilation of the project with the 1244 #. Instrument the code, which requires a full recompilation of the project with the
2698 program. 2610 program.
2699 2611
2700 ``gnatelim`` is a project-aware tool. 2612 ``gnatelim`` is a project-aware tool.
2701 (See :ref:`Using_Project_Files_with_GNAT_Tools` for a description of 2613 (See :ref:`Using_Project_Files_with_GNAT_Tools` for a description of
2702 the project-related switches but note that ``gnatelim`` does not support 2614 the project-related switches but note that ``gnatelim`` does not support
2703 the :samp:`-U`, :samp:`-U {main_unit}`, :samp:`--subdirs={dir}`, or 2615 the :samp:`-U {main_unit}`, :samp:`--subdirs={dir}`, or
2704 :samp:`--no_objects_dir` switches.) 2616 :samp:`--no_objects_dir` switches.)
2705 The project file package that can specify 2617 The project file package that can specify
2706 ``gnatelim`` switches is named ``Eliminate``. 2618 ``gnatelim`` switches is named ``Eliminate``.
2707 2619
2708 .. _About_gnatelim: 2620 .. _About_gnatelim:
2728 2640
2729 If a set of source files is specified as ``gnatelim`` arguments, it 2641 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 2642 treats these files as a complete set of sources making up a program to
2731 analyse, and analyses only these sources. 2643 analyse, and analyses only these sources.
2732 2644
2733 After a full successful build of the main subprogram ``gnatelim`` can be 2645 If ``gnatelim`` is called with a project file and :samp:`-U` option is
2734 called without specifying sources to analyse, in this case it computes 2646 used, then in process all the files from the argument project but
2735 the source closure of the main unit from the :file:`ALI` files. 2647 not just the closure of the main subprogram.
2648
2649 In all the other cases (that are typical cases of ``gnatelim`` usage, when
2650 the only ``gnatelim`` parameter is the name of the source file containing
2651 the main subprogram) gnatelim needs the full closure of the main subprogram.
2652 When called with a project file, gnatelim computes this closure itself.
2653 Otherwise it assumes that it can reuse the results of the previous
2654 build of the main subprogram.
2736 2655
2737 If the set of sources to be processed by ``gnatelim`` contains sources with 2656 If the set of sources to be processed by ``gnatelim`` contains sources with
2738 preprocessing directives 2657 preprocessing directives
2739 then the needed options should be provided to run preprocessor as a part of 2658 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`` 2659 the ``gnatelim`` call, and the generated set of pragmas ``Eliminate``
2741 will correspond to preprocessed sources. 2660 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 2661
2752 2662
2753 .. _Running_gnatelim: 2663 .. _Running_gnatelim:
2754 2664
2755 Running ``gnatelim`` 2665 Running ``gnatelim``
2812 :samp:`--RTS={rts-path}` 2722 :samp:`--RTS={rts-path}`
2813 Specifies the default location of the runtime library. Same meaning as the 2723 Specifies the default location of the runtime library. Same meaning as the
2814 equivalent ``gnatmake`` flag (:ref:`Switches_for_gnatmake`). 2724 equivalent ``gnatmake`` flag (:ref:`Switches_for_gnatmake`).
2815 2725
2816 2726
2727 .. index:: -U (gnatelim)
2728
2729 :samp:`-U`
2730 Process all the sources from the argument project. If no project file
2731 is specified, this option has no effect. If this option is used with the
2732 project file, ``gnatelim`` does not require the preliminary build of the
2733 argument main subprogram.
2734
2735
2817 .. index:: -files (gnatelim) 2736 .. index:: -files (gnatelim)
2818 2737
2819 :samp:`-files={filename}` 2738 :samp:`-files={filename}`
2820 Take the argument source files from the specified file. This file should be an 2739 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 2740 ordinary text file containing file names separated by spaces or
2856 :samp:`-j{n}` 2775 :samp:`-j{n}`
2857 Use ``n`` processes to carry out the tree creations (internal representations 2776 Use ``n`` processes to carry out the tree creations (internal representations
2858 of the argument sources). On a multiprocessor machine this speeds up processing 2777 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 2778 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. 2779 parallel tree creations is the number of core processors on the platform.
2780 This possibility is disabled if ``gnatelim`` has to compute the closure
2781 of the main unit.
2861 2782
2862 2783
2863 .. index:: -q (gnatelim) 2784 .. index:: -q (gnatelim)
2864 2785
2865 :samp:`-q` 2786 :samp:`-q`
2922 You will need to manually remove the wrong ``Eliminate`` pragmas from 2843 You will need to manually remove the wrong ``Eliminate`` pragmas from
2923 the configuration file indicated in the error message. You should recompile 2844 the configuration file indicated in the error message. You should recompile
2924 your program from scratch after that, because you need a consistent 2845 your program from scratch after that, because you need a consistent
2925 configuration file(s) during the entire compilation. 2846 configuration file(s) during the entire compilation.
2926 2847
2927 2848 If ``gnatelim`` is called with a project file and with ``-U`` option
2928 .. _Making_Your_Executables_Smaller: 2849 the generated set of pragmas may contain pragmas for subprograms that
2929 2850 does not belong to the closure of the argument main subprogram. These
2930 Making Your Executables Smaller 2851 pragmas has no effect when the set of pragmas is used to reduce the size
2931 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2852 of executable.
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 2853
2983 2854
2984 .. index:: Overflow checks 2855 .. index:: Overflow checks
2985 .. index:: Checks (overflow) 2856 .. index:: Checks (overflow)
2986 2857
3407 3278
3408 .. index:: System.Dim.Mks package (GNAT library) 3279 .. index:: System.Dim.Mks package (GNAT library)
3409 .. index:: MKS_Type type 3280 .. index:: MKS_Type type
3410 3281
3411 The simplest way to impose dimensionality checking on a computation is to make 3282 The simplest way to impose dimensionality checking on a computation is to make
3412 use of the package ``System.Dim.Mks``, 3283 use of one of the instantiations of the package ``System.Dim.Generic_Mks``, which
3413 which is part of the GNAT library. This 3284 are part of the GNAT library. This generic package defines a floating-point
3414 package defines a floating-point type ``MKS_Type``, 3285 type ``MKS_Type``, for which a sequence of dimension names are specified,
3415 for which a sequence of 3286 together with their conventional abbreviations. The following should be read
3416 dimension names are specified, together with their conventional abbreviations. 3287 together with the full specification of the package, in file
3417 The following should be read together with the full specification of the 3288 :file:`s-digemk.ads`.
3418 package, in file :file:`s-dimmks.ads`. 3289
3419 3290 .. index:: s-digemk.ads file
3420 .. index:: s-dimmks.ads file 3291
3421 3292 .. code-block:: ada
3422 .. code-block:: ada 3293
3423 3294 type Mks_Type is new Float_Type
3424 type Mks_Type is new Long_Long_Float
3425 with 3295 with
3426 Dimension_System => ( 3296 Dimension_System => (
3427 (Unit_Name => Meter, Unit_Symbol => 'm', Dim_Symbol => 'L'), 3297 (Unit_Name => Meter, Unit_Symbol => 'm', Dim_Symbol => 'L'),
3428 (Unit_Name => Kilogram, Unit_Symbol => "kg", Dim_Symbol => 'M'), 3298 (Unit_Name => Kilogram, Unit_Symbol => "kg", Dim_Symbol => 'M'),
3429 (Unit_Name => Second, Unit_Symbol => 's', Dim_Symbol => 'T'), 3299 (Unit_Name => Second, Unit_Symbol => 's', Dim_Symbol => 'T'),
3463 g : constant Mass := 1.0E-03; 3333 g : constant Mass := 1.0E-03;
3464 min : constant Time := 60.0; 3334 min : constant Time := 60.0;
3465 day : constant Time := 60.0 * 24.0 * min; 3335 day : constant Time := 60.0 * 24.0 * min;
3466 ... 3336 ...
3467 3337
3468 Using this package, you can then define a derived unit by 3338 There are three instantiations of ``System.Dim.Generic_Mks`` defined in the
3469 providing the aspect that 3339 GNAT library:
3470 specifies its dimensions within the MKS system, as well as the string to 3340
3471 be used for output of a value of that unit: 3341 * ``System.Dim.Float_Mks`` based on ``Float`` defined in :file:`s-diflmk.ads`.
3342 * ``System.Dim.Long_Mks`` based on ``Long_Float`` defined in :file:`s-dilomk.ads`.
3343 * ``System.Dim.Mks`` based on ``Long_Long_Float`` defined in :file:`s-dimmks.ads`.
3344
3345 Using one of these packages, you can then define a derived unit by providing
3346 the aspect that specifies its dimensions within the MKS system, as well as the
3347 string to be used for output of a value of that unit:
3472 3348
3473 .. code-block:: ada 3349 .. code-block:: ada
3474 3350
3475 subtype Acceleration is Mks_Type 3351 subtype Acceleration is Mks_Type
3476 with Dimension => ("m/sec^2", 3352 with Dimension => ("m/sec^2",
3720 Units compiled with this option will generate extra instructions to check 3596 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 3597 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. 3598 variables in declare blocks) does not exceed the available stack space.
3723 If the space is exceeded, then a ``Storage_Error`` exception is raised. 3599 If the space is exceeded, then a ``Storage_Error`` exception is raised.
3724 3600
3725 For declared tasks, the stack size is controlled by the size 3601 For declared tasks, the default stack size is defined by the GNAT runtime,
3726 given in an applicable ``Storage_Size`` pragma or by the value specified 3602 whose size may be modified at bind time through the ``-d`` bind switch
3727 at bind time with ``-d`` (:ref:`Switches_for_gnatbind`) or is set to 3603 (:ref:`Switches_for_gnatbind`). Task specific stack sizes may be set using the
3728 the default size as defined in the GNAT runtime otherwise. 3604 ``Storage_Size`` pragma.
3729 3605
3730 .. index:: GNAT_STACK_LIMIT 3606 For the environment task, the stack size is determined by the operating system.
3731 3607 Consequently, to modify the size of the environment task please refer to your
3732 For the environment task, the stack size depends on 3608 operating system documentation.
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 3609
3753 3610
3754 .. _Static_Stack_Usage_Analysis: 3611 .. _Static_Stack_Usage_Analysis:
3755 3612
3756 Static Stack Usage Analysis 3613 Static Stack Usage Analysis
3834 * *Stack Usage* is the measure done by the stack analyzer. 3691 * *Stack Usage* is the measure done by the stack analyzer.
3835 In order to prevent overflow, the stack 3692 In order to prevent overflow, the stack
3836 is not entirely analyzed, and it's not possible to know exactly how 3693 is not entirely analyzed, and it's not possible to know exactly how
3837 much has actually been used. 3694 much has actually been used.
3838 3695
3839 The environment task stack, e.g., the stack that contains the main unit, is 3696 By default the environment task stack, the stack that contains the main unit,
3840 only processed when the environment variable GNAT_STACK_LIMIT is set. 3697 is not processed. To enable processing of the environment task stack, the
3698 environment variable GNAT_STACK_LIMIT needs to be set to the maximum size of
3699 the environment task stack. This amount is given in kilobytes. For example:
3700
3701 ::
3702
3703 $ set GNAT_STACK_LIMIT 1600
3704
3705 would specify to the analyzer that the environment task stack has a limit
3706 of 1.6 megabytes. Any stack usage beyond this will be ignored by the analysis.
3841 3707
3842 The package ``GNAT.Task_Stack_Usage`` provides facilities to get 3708 The package ``GNAT.Task_Stack_Usage`` provides facilities to get
3843 stack usage reports at run-time. See its body for the details. 3709 stack-usage reports at run time. See its body for the details.
3844 3710
3845 3711
3846 3712
3847 .. _Memory_Management_Issues: 3713 .. _Memory_Management_Issues:
3848 3714