Tcov

Last updated
tcov
Developer(s) Oracle Corporation
Operating system Solaris
Platform SPARC
Type Code coverage
License Free for download and use as described in the Sun Studio product license.
Website developers.sun.com

Tcov is a source code coverage analysis and statement-by-statement profiling tool for software written in Fortran, C and C++. Tcov generates exact counts of the number of times each statement in a program is executed and annotates source code to add instrumentation. It is a standard utility, provided free of cost with Sun Studio software.

Contents

The tcov utility gives information on how often a program executes segments of code. It produces a copy of the source file, annotated with execution frequencies. The code can be annotated at the basic block level or the source line level. As the statements in a basic block are executed the same number of times, a count of basic block executions equals number of times each statement in the block is executed. The tcov utility does not produce any time-based data.

Description

tcov produces a test coverage analysis of a compiled program. tcov takes source files as arguments and produces an annotated source listing. Each basic block of code (or each line if the particular option to tcov is specified) is prefixed with the number of times it has been executed; lines that have not been executed are prefixed with "#####".

The tcov utility also places a summary at the end of the annotated program listing. The statistics for the most frequently executed basic blocks are listed in order of execution frequency. The line number is the number of the first line in the block.

There are two implementations of tcov:

Enhanced coverage analysis overcomes some of the shortcomings of the original analysis tool, such as: [3]

Implementation

To generate annotated source code, following three steps are required: [4]

Each subsequent run accumulates more coverage data into the profile data file. Data for each object file is zeroed out the first time the program is executed after recompilation. Data for the entire program is zeroed by removing the tcovd file. [5]

The above steps are explained for both original and enhanced tcov below:

Old Style coverage analysis

Source code is compiled with -xa option for C program and -a option for Fortran and C++ programs. The compiler creates a coverage data file with the suffix .d for each object file. The coverage data file is created in the directory specified by the environment variable TCOVDIR. If TCOVDIR is not set, the coverage data file is created in the current directory. The above instrumented build is run and at program completion, the .d files are updated. Finally, tcov command is run to generate the annotated source files. The syntax of the tcov command is as follows:

tcov options source-file-list 

Here, source-file-list is a list of the source code filenames. For a list of options, The default output of tcov is a set of files, each with the suffix .tcov, which can be changed with the -o filename option.

A program compiled for code coverage analysis can be run multiple times (with potentially varying input); tcov can be used on the program after each run to compare behavior.

New Style coverage analysis

Source code is compiled with -xprofile=tcov option. Unlike original mode, enhanced tcov doesn't generate any files at compile time. [6] The above instrumented build is run and at program completion, a directory is created to store the profile data, and a single coverage data file called tcovd is created in that directory. tcovd holds the information about the line numbers, and the execution count. It is a plain text file. By default, the directory is created in the location where program is run, and it is named after executable and suffixed by .profile. The directory is also known as the profile bucket. The location of profile bucket can be overridden by setting SUN_PROFDATA_DIR or SUN_PROFDATA environment variables. Finally, tcov command is run to generate the annotated source files. The syntax of the tcov command is same as for original command, except for the mandatory -x option.

tcov options -x profilebucket source-file-list 

The only difference in command from original tcov is the mandatory addition is of -x dir option to denote enhanced tcov.

Example

The following program, written in C programming language, loops overs the integers 1 to 9 and tests their divisibility with the modulus (%) operator.

#include<stdio.h>intmain(void){inti;for(i=1;i<10;i++){if(i%3==0)printf("%d is divisible by 3\n",i);if(i%11==0)printf("%d is divisible by 11\n",i);}return0;}

To enable coverage testing the program must be compiled with the following options:

for old style code coverage,

cc -xa cov.c

and for new style code coverage,

cc -xprofile=tcov -o cov cov.c

where cov.c is the name of the program file. This creates an instrumented executable which contains additional instructions that record the number of times each line of the program is executed. -o option is used to set the name of the executable. The executable must then be run to create the coverage data. The creation and location of this file is different for old- and new- style code analysis. In old style analysis, this file with extension .d, created after compilation, either in TCOVDIR directory or current one, is updated with coverage data. In new style analysis, coverage data file, with name tcovd, is created in <executable name>.profile directory. This data can be analyzed using the tcov command and the name of a source file:

for old style code coverage,

tcov cov.c

and for new style code coverage,

tcov -x cov.profile cov.c

the addition argument in new style analysis is profile bucket. The tcov command produces an annotated version of the original source file, with the file extension ‘.tcov’, containing counts of the number of times each line was executed:

#include<stdio.h>intmain(void){1inti;10for(i=1;i<10;i++){9if(i%3==0)3printf("%d is divisible by 3\n",i);9if(i%11==0)######          printf ("%d is divisible by 11\n", i);9}1return0;1}

The tcov utility also places a summary at the end of the annotated program listing. The statistics for the most frequently executed basic blocks are listed in order of execution frequency. The line number is the number of the first line in the block.

Command line options

Tcov command line utility supports following options while generating annotated files from profile data: [7]

See also

Related Research Articles

C (programming language) General-purpose programming language

C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. By design, C provides constructs that map efficiently to typical machine instructions. It has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computer architectures that range from supercomputers to PLCs and embedded systems.

Shell script Script written for the shell, or command line interpreter, of an operating system

A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script which sets up the environment, runs the program, and does any necessary cleanup or logging, is called a wrapper.

In computing, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. Common requirements are to minimize a program's execution time, memory footprint, storage size, and power consumption.

Interpreter (computing) Program that executes source code without a separate compilation step

In computer science, an interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program. An interpreter generally uses one of the following strategies for program execution:

  1. Parse the source code and perform its behavior directly;
  2. Translate source code into some efficient intermediate representation or object code and immediately execute that;
  3. Explicitly execute stored precompiled bytecode made by a compiler and matched with the interpreter Virtual Machine.
C shell Unix shell

The C shell is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the Berkeley Software Distribution (BSD) which Joy first distributed in 1978. Other early contributors to the ideas or the code were Michael Ubell, Eric Allman, Mike O'Brien and Jim Kulp.

In computer science, self-modifying code (SMC) is code that alters its own instructions while it is executing – usually to reduce the instruction path length and improve performance or simply to reduce otherwise repetitively similar code, thus simplifying maintenance. Self-modification is an alternative to the method of "flag setting" and conditional program branching, used primarily to reduce the number of times a condition needs to be tested. The term is usually only applied to code where the self-modification is intentional, not in situations where code accidentally modifies itself due to an error such as a buffer overflow.

C syntax Set of rules governing writing of software in the language

The syntax of the C programming language is the set of rules governing writing of software in the C language. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

In compiler theory, dead code elimination is a compiler optimization to remove code which does not affect the program results. Removing such code has several benefits: it shrinks program size, an important consideration in some contexts, and it allows the running program to avoid executing irrelevant operations, which reduces its running time. It can also enable further optimizations by simplifying program structure. Dead code includes code that can never be executed, and code that only affects dead variables, that is, irrelevant to the program.

The MCP is the operating system of the Burroughs small, medium and large systems, including the Unisys Clearpath/MCP systems.

In computer programming, an entry point is a point in a program where the execution of a program begins, and where the program has access to command line arguments.

IP Pascal is an implementation of the Pascal programming language using the IP portability platform, a multiple machine, operating system and language implementation system. It implements the language "Pascaline", and has passed the Pascal Validation Suite.

In software engineering, profiling is a form of dynamic program analysis that measures, for example, the space (memory) or time complexity of a program, the usage of particular instructions, or the frequency and duration of function calls. Most commonly, profiling information serves to aid program optimization, and more specifically, performance engineering.

Automatic parallelization, also auto parallelization, or autoparallelization refers to converting sequential code into multi-threaded and/or vectorized code in order to use multiple processors simultaneously in a shared-memory multiprocessor (SMP) machine. Fully automatic parallelization of sequential programs is a challenge because it requires complex program analysis and the best approach may depend upon parameter values that are not known at compilation time.

Dynamic-link library (DLL) is Microsoft's implementation of the shared library concept in the Microsoft Windows and OS/2 operating systems. These libraries usually have the file extension DLL, OCX, or DRV . The file formats for DLLs are the same as for Windows EXE files – that is, Portable Executable (PE) for 32-bit and 64-bit Windows, and New Executable (NE) for 16-bit Windows. As with EXEs, DLLs can contain code, data, and resources, in any combination.

Exception handling syntax is the set of keywords and/or structures provided by a computer programming language to allow exception handling, which separates the handling of errors that arise during a program's operation from its ordinary processes. Syntax for exception handling varies between programming languages, partly to cover semantic differences but largely to fit into each language's overall syntactic structure. Some languages do not call the relevant concept "exception handling"; others may not have direct facilities for it, but can still provide means to implement it.

Dynamic program analysis is the analysis of computer software that is performed by executing programs on a real or virtual processor. For dynamic program analysis to be effective, the target program must be executed with sufficient test inputs to cover almost all possible outputs. Use of software testing measures such as code coverage helps ensure that an adequate slice of the program's set of possible behaviors has been observed. Also, care must be taken to minimize the effect that instrumentation has on the execution of the target program. Dynamic analysis is in contrast to static program analysis. Unit tests, integration tests, system tests and acceptance tests use dynamic testing.

SystemTap Scripting language and tool

In computing, SystemTap (stap) is a scripting language and tool for dynamically instrumenting running production Linux-based operating systems. System administrators can use SystemTap to extract, filter and summarize data in order to enable diagnosis of complex performance or functional problems.

Oracle Developer Studio, formerly named Oracle Solaris Studio, Sun Studio, Sun WorkShop, Forte Developer, and SunPro Compilers, is Oracle Corporation's flagship software development product for the Solaris and Linux operating systems. It includes optimizing C, C++, and Fortran compilers, libraries, and performance analysis and debugging tools, for Solaris on SPARC and x86 platforms, and Linux on x86/x64 platforms, including multi-core systems.

Gcov is a source code coverage analysis and statement-by-statement profiling tool. Gcov generates exact counts of the number of times each statement in a program is executed and annotates source code to add instrumentation. Gcov comes as a standard utility with the GNU Compiler Collection (GCC) suite.

Parasoft C/C++test Integrated set of tools

Parasoft C/C++test is an integrated set of tools for testing C and C++ source code that software developers use to analyze, test, find defects, and measure the quality and security of their applications. It supports software development practices that are part of development testing, including static code analysis, dynamic code analysis, unit test case generation and execution, code coverage analysis, regression testing, runtime error detection, requirements traceability, and code review. It's a commercial tool that supports operation on Linux, Windows, and Solaris platforms as well as support for on-target embedded testing and cross compilers.

References

  1. "Original Tcov statement-by-statement analysis" . Retrieved 6 Feb 2012.
  2. "Enhanced Tcov statement-by-statement analysis" . Retrieved 6 Feb 2012.
  3. "Improved features of tcov enhanced over tcov original" . Retrieved 6 Feb 2012.
  4. oracle.com. "steps required to generate annotated source code".
  5. www.sics.se. "SunOS manual page".
  6. docs.oracle.com. "enhanced tcov".
  7. developers.sun.com. "Tcov documentation" . Retrieved Feb 7, 2012.