Writing custom report scripts

To write your own scripts to produce custom reports, you can use the PureCoverage export format.

The PureCoverage export format is in ASCII and is used to transfer coverage data to other programs. The export format contains nested summary information about directories, files, functions, lines, and blocks.

To create a custom report:

  1. Convert the appropriate PureCoverage data files to export format. To obtain export data, use the command purecov -export.

  2. Read the export data and process it into the desired form.

A sample custom report script

This example shows how to write a simple report script onefunc that prints the line coverage percentage for a specific function within a program. The script syntax is:

onefunc <program> <function>

where <program> is the name of the program to search, and <function> is the name of the function to display. The script automatically appends the .pcv extension to the program name, and it ignores the possibility of invalid parameters.

The custom report script onefunc is shown here. You can use it as a template for writing scripts of your own.

#!/usr/local/bin/perl

#   Usage: onefunc program function
#   Reports coverage for "function" which is used by "program".
#   Example: onefunc myprog main

open(EXPORT, "purecov -export $ARGV[0].pcv |");
while (<EXPORT>) {
   if (/^di/) { ($key, $curdir) = split(/\t/); }
   if (/^fi/) { ($key, $curfile) = split(/\t/); }
   if (/^fu/) {
      ($key, $curfunc, $ign, $ign, $unused, $lines) =
         split(/\t/);
      if ($curfunc eq $ARGV[1]) {
         $count = $lines - $unused;
         $percent = ($count * 100) / $lines;
         push(@matches,
            sprintf("%3d%% %s%s:%s\n", $percent, $curdir,
                $curfile, $curfunc));
      }
   }
}
close(<EXPORT>);
print sort @matches;

The basic script structure uses purecov to produce the export data, and then extracts the function information and records it in the array @matches. It uses sort to organize the data, in case the function name is used more than once in the program.

The command line to purecov uses the -export option with $ARGV[0], the program name, to export data from the program you specify when you run the script.

The while loop has three major pattern-matching sections. This report handles three different types of lines from the export data:

The caret (^) in front of the keywords makes the patterns match only at the beginnings of lines. For the file and directory lines, you only need to record the name. For the function line, you also want to record the line coverage data from $unused and $lines, calculate the coverage percentage, and print function information.

When you run the script, you get coverage information for the function you specify.

% chmod +x onefunc
% onefunc myprog myfunc
    70% /home/pat/dir1/file1.c:myfunc