GCC compilation options

From TracingWiki

Jump to: navigation, search

The GNU Compiler Collection, GCC, implements numerous options for debugging and instrumentation purposes. The instrumentation is typically oriented towards providing information for profiling and code coverage tools.

The compilation option -finstrument-functions automatically inserts tracepoints at every entry and exit of user compiled functions. It is possible to prevent the insertion of tracepoints for some functions with the no_instrument_function attribute.

Two other compilation options, -ftest-coverage and -fprofile-arcs, tell the compiler to instrument the generated code to count the execution frequencies of each basic block and branch and write the results to a file upon exit. This file can later be analyzed with the associated tool, gcov, to print the source code along with the execution frequencies.

The overhead of instrumenting basic blocks and branches with counters is fairly small since it adds a single add instruction for every block or branch execution. As an example, the GNU compression program gzip was compiled with -ftest-coverage and -fprofile-arcs. It was executed to compress a 64MB log file. It turned out that the instrumented gzip required 31.36s of elapsed execution time rather than 28.16s for the normal gzip.

The -pg compilation option is provided for sampling. It instruments function entries by adding a call to a function named mcount. This mechanism is used in the Linux kernel by the function tracer, Ftrace, by defining a special mcount function to trace function entries or measure the latency associated with each function called.

Personal tools