ArticleslgStudy

computer science

Tcov

Tcov is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Tcov rather than just read about it. In short: 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.

Key takeaways

  • Tcov belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Tcov to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Tcov from memory before moving on to harder problems.

Reference excerpt

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. 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:

Old Style coverage analysis: In this implementation, also known as tcov original, the compiler creates a coverage data file with the suffix .d for each object file. When program completes, the coverage data files are updated. New Style coverage analysis: In this implementation, also known as tcov enhanced, no additional files are created at compile time. Instead, directory is created to store the profile data, and a single coverage data file called tcovd is created in that directory. Enhanced coverage analysis overcomes some of the shortcomings of the original analysis tool, such as:

Provides more complete support for C++. Supports code found in #include header files and corrects a flaw that obscured coverage numbers for template classes and functions. More efficient runtime than the original tcov runtime. Supported for all the platforms that the compilers support.

Implementation To generate annotated source code, following three steps are required:

Code compilation with appropriate compiler option Program execution to accumulate profile data tcov command execution to generate annotated files 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. 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. 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.

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:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Tcov

Start with the simplest possible case. Write down what Tcov claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Tcov before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Tcov ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Tcov

In research
Tcov appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Tcov in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Tcov is common in secondary-school and first-year university syllabi. It links to neighbouring topics Software metrics, Software testing tools, so understanding it makes those chapters shorter.
In everyday life
Look for Tcov outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Tcov in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Tcov means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Tcov out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Tcov in simple terms?

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.

Why does Tcov matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Tcov?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Tcov.

Tags

  • Software metrics
  • Software testing tools

Keep exploring