|
| 1 | +.. _integrated_instr: |
| 2 | + |
| 3 | +###################################################################### |
| 4 | +Producing source traces with integrated instrumentation (experimental) |
| 5 | +###################################################################### |
| 6 | + |
| 7 | +|gcv| provides an alternate way of instrumenting sources, available for C/C++ |
| 8 | +under linux and when using gcc or g++ as a compiler. Theoretically, any build |
| 9 | +system should be supported, but it has only been tested for the Makefile / CMake |
| 10 | +build systems so far. |
| 11 | + |
| 12 | +As with the workflow involving a separate instrumentation, a :term:`coverage |
| 13 | +runtime <Coverage Runtime>` to be used by the instrumented code needs to be |
| 14 | +setup as a required prerequisite. Refer to the :ref:`instr-rts` section of this |
| 15 | +manual for a description of this step. The installed instrumentation runtime |
| 16 | +must be visible on the ``GPR_PROJECT_PATH``. |
| 17 | + |
| 18 | +Then the process essentially goes like: |
| 19 | + |
| 20 | +#. Setup the integrated instrumentation process, specifying the instrumentation |
| 21 | + parameters (units of interest, coverage level etc.) and the compilers of use. |
| 22 | +#. Build the instrumented code using the generated compiler wrapper(s); |
| 23 | +#. Execute the program to produce a trace. |
| 24 | + |
| 25 | + |
| 26 | +Specifying instrumentation switches |
| 27 | +=================================== |
| 28 | + |
| 29 | +The user specifies the switches through the ``gnatcov setup-integration`` |
| 30 | +command, which accepts any option accepted by ``gnatcov instrument``, except the |
| 31 | +ones using project mechanisms to filter out units of interest: ``--units`` and |
| 32 | +``--projects``. Refer to :ref:`src_traces` for more information regarding the |
| 33 | +source instrumentation specific switches. |
| 34 | + |
| 35 | +As there is no project acting as a units of interest provider, the user must |
| 36 | +explicitly pass files of interest through the ``--files`` switch, which expects |
| 37 | +a list of full filenames that can be passed through the command line, or using a |
| 38 | +response file. |
| 39 | + |
| 40 | +To generate a compiler wrapper, use the ``--compilers`` switch. The list of |
| 41 | +supported compilers is ``gcc`` and ``g++``. |
| 42 | + |
| 43 | +This will generate in the current directory, or in the directory specified by |
| 44 | +the ``--output-dir`` switch an executable compiler wrapper and a |
| 45 | +``gnatcov_config.json`` file along. Note that this configuration file is used to |
| 46 | +configure various instrumentation options: it is thus very important that it |
| 47 | +resides in the same directory as the compiler wrapper. |
| 48 | + |
| 49 | + |
| 50 | +Building an instrumented executable |
| 51 | +=================================== |
| 52 | + |
| 53 | +The build process can be run unchanged after it has been configured to use the |
| 54 | +generated compiler wrapper instead of the original compiler. |
| 55 | + |
| 56 | + |
| 57 | +A simple Makefile example |
| 58 | +========================= |
| 59 | + |
| 60 | +The following considers that the instrumentation runtime was previously |
| 61 | +installed with ``gnatcov setup``, and that the ``GPR_PROJECT_PATH`` variable |
| 62 | +contains its installed location. See :ref:`instr-rts`. |
| 63 | + |
| 64 | +Let's consider a simple ``main.cpp`` file: |
| 65 | + |
| 66 | +.. code-block:: c++ |
| 67 | + |
| 68 | + #include <iostream> |
| 69 | + |
| 70 | + int main(int argc, char **argv){ |
| 71 | + std::cout << "Hello World" << std::endl; |
| 72 | + return 0; |
| 73 | + } |
| 74 | +
|
| 75 | +and the following Makefile: |
| 76 | + |
| 77 | +.. code-block:: makefile |
| 78 | +
|
| 79 | + CC=g++ |
| 80 | + OBJ = main.o |
| 81 | +
|
| 82 | + %.o: %.c |
| 83 | + $(CC) -c -o $@ $< |
| 84 | +
|
| 85 | + test: $(OBJ) |
| 86 | + $(CC) -o $@ $^ |
| 87 | +
|
| 88 | +We start by configuring the instrumentation process: |
| 89 | + |
| 90 | +.. code-block:: sh |
| 91 | +
|
| 92 | + cd <my-project> |
| 93 | + gnatcov setup-integration --files=<my_project>/main.cpp --compilers=g++ |
| 94 | +
|
| 95 | +Then, we launch the build processed unchanged, with the compiler wrapper first |
| 96 | +on the path: |
| 97 | + |
| 98 | +.. code-block:: sh |
| 99 | +
|
| 100 | + export PATH=<my-project>:$PATH |
| 101 | + make |
| 102 | +
|
| 103 | +This will produce an instrumented executable, that will produce a source trace |
| 104 | +when run, that can be analyzed with ``gnatcov coverage``. |
| 105 | + |
| 106 | +A simple CMake example |
| 107 | +====================== |
| 108 | + |
| 109 | +The following considers that the instrumentation runtime was installed through |
| 110 | +the use of ``gnatcov setup``. |
| 111 | + |
| 112 | +Let's consider a simple ``main.cpp`` file |
| 113 | + |
| 114 | +.. code-block:: c++ |
| 115 | + |
| 116 | + #include <iostream> |
| 117 | + |
| 118 | + int main(int argc, char **argv){ |
| 119 | + std::cout << "Hello World" << std::endl; |
| 120 | + return 0; |
| 121 | + } |
| 122 | +
|
| 123 | +The CMakeLists.txt file to be used to compile the main.cpp file is : |
| 124 | + |
| 125 | +.. code-block:: cmake |
| 126 | +
|
| 127 | + project(HelloWorld) |
| 128 | + cmake_minimum_required(VERSION 3.0) |
| 129 | +
|
| 130 | + add_executable(hello_world main.cpp) |
| 131 | +
|
| 132 | +We start by creating the build directory, and configuring the instrumentation |
| 133 | +process there: |
| 134 | + |
| 135 | +.. code-block:: sh |
| 136 | +
|
| 137 | + cd <my-project> |
| 138 | + mkdir build |
| 139 | + cd build |
| 140 | + gnatcov setup-integration --files=<my_project>/main.cpp --compilers=g++ |
| 141 | +
|
| 142 | +This creates a ``g++`` compiler wrapper in the build directory, along with a |
| 143 | +``gnatcov_config.json`` file that we intend to use as a proxy for compilation. |
| 144 | +To do that, we have to configure the CMake build process accordingly, using the |
| 145 | +``CMAKE_CXX_COMPILER`` variable. We run the configuration command in the build |
| 146 | +directory: |
| 147 | + |
| 148 | +.. code-block:: sh |
| 149 | +
|
| 150 | + cmake .. -CMAKE_CXX_COMPILER=<my_project>/build/g++ |
| 151 | +
|
| 152 | +The default generator for CMake is "Unix Makefiles", so we can then run the |
| 153 | +build process with ``make``, and our executable which will produce a source trace |
| 154 | +that can be analyzed by ``gnatcov coverage``. |
0 commit comments