Coverage Measurement and Profiling
In order to use gcov, you must use GCC as your compiler. In addition, you must pass the options -fprofile-arcs -ftest-coverage during compilation. This causes GCC to insert additional code into the created object files to record the information needed by gcov and save it to disk.
The gcov data is written in the same directory as your source files, using a .da extension instead of .c. Nothing is written until you run your program. You can run your program multiple times to accumulate measurements, for example, trying different command-line options or input data.
Per the GCC documentation, the object file contains the absolute pathname of the .da file. So, it is not possible to move your source files after building and continue to collect coverage measurements. Doing so produces the error message arc profiling: Can't open output file filename when attempting to save the coverage data. gcov also shows results for only a single file at a time, and it does not provide a summarization method.
Creating gcov reports is straightforward. I use glib (the base library for Gtk+ and GNOME, see www.gtk.org) as an example. First, download glib from the Gtk+ site or a mirror. I used glib-2.2.1, which was the latest at the time of this writing.
To turn on coverage measurement, set the environment variable CFLAGS to -fprofile-arcs -ftest-coverage and run the standard ./configure; make; make check. This should work for any software that follows the GNU guidelines.
After make check has completed, it is possible to use gcov to create coverage reports. The most basic report we can access is a statement coverage report:
[zfrey@warthog glib]$ gcov garray.c 68.37% of 215 source lines executed in file garray.c Creating garray.c.gcov.
Besides printing out summary statistics, gcov creates an annotated source listing. For each executable line, this listing shows either the number of times it has been executed or a flag indicating it never has been executed (Listing 1).
Listing 1. Excerpt of a gcov Annotated Source Listing
GArray* g_array_sized_new
(gboolean zero_terminated,
gboolean clear,
guint elt_size,
guint reserved_size)
40 {
40 GRealArray *array;
40 G_LOCK (array_mem_chunk);
40 if (!array_mem_chunk)
14 array_mem_chunk = g_mem_chunk_new
("array mem chunk",
sizeof (GRealArray),
1024, G_ALLOC_AND_FREE);
40 array = g_chunk_new (GRealArray,
array_mem_chunk);
To see the branch coverage, use the -b option:
[zfrey@warthog glib]$ gcov -b garray.c 68.37% of 215 source lines executed in file garray.c 59.02% of 122 branches executed in file garray.c 46.72% of 122 branches taken at least once in file garray.c 45.35% of 86 calls executed in file garray.c Creating garray.c.gcov.
The annotated source file now contains the branch data (Listing 2).
Listing 2. Excerpt from a Source File Annotated with Branch Data
GArray* g_array_sized_new (
gboolean zero_terminated,
gboolean clear,
guint elt_size,
guint reserved_size)
40 {
40 GRealArray *array;
40 G_LOCK (array_mem_chunk);
branch 0 taken = 40%
branch 1 taken = 58%
call 2 returns = 100%
branch 3 taken = 100%
call 4 returns = 100%
40 if (!array_mem_chunk)
branch 0 taken = 65%
14 array_mem_chunk = g_mem_chunk_new (
"array mem chunk",
call 0 returns = 100%
sizeof (GRealArray),
1024, G_ALLOC_AND_FREE);
1024, G_ALLOC_AND_FREE);
40 array = g_chunk_new (GRealArray,
array_mem_chunk);
call 0 returns = 100%
To see coverage broken down by function within the source file, use the -f option. This can be combined with -b to get the branch coverage data on a per-function basis as well. Unfortunately, gcov does not provide a way to summarize results over multiple source files.
GGCov is a Gtk+-based GUI front end for gcov written by Greg Banks and available at www.alphalink.com.au/~gnb/ggcov. I was able to download the latest version (0.1.1 at the time), compile and install with it no problems. This was done using ./configure; make; make install and allowing /usr/local as the prefix.
To use GGCov, cd to the directory with your source files and launch GGCov. GGCov then looks for all the files in that directory with coverage measurements and creates a summary (Figure 1). This is something you cannot do with GNU gcov by itself.

Figure 1. A GGCov Summary Report
Selecting the Filename button activates a drop-down list of all files in the directory that have coverage information. Selecting a filename returns the statistics for that file.

Figure 2. A GGCov Report by File
Selecting the Function button activates a drop-down list of all the functions from all the files in the directory that have coverage information. Selecting a function name returns the statistics for that function (Figure 3).

Figure 3. A GGCov Report by Function
As if that weren't enough power, the Range button allows the selection of a file and a line number range and creates a summary for only the functions within that range. All views except the Overall summary have a View button, which brings up a new window containing the annotated source display for the selected file.
Overall, I found the basic functionality of GGCov easy to use and helpful. GGCov also contains features for displaying call graphs; unfortunately, on Red Hat 8.0 the glib example caused GGCov to segfault. I contacted Greg Banks, who said the call graph features were the most immature and that he hopes to address these problems in the next few releases.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Developer Poll
- Dart: a New Web Programming Experience
- Readers' Choice Awards
- What's the tweeting protocol?
- Linux is good
1 hour 6 min ago - Reply to comment | Linux Journal
1 hour 23 min ago - Web Hosting IQ
1 hour 53 min ago - Web Hosting IQ
1 hour 53 min ago - Web Hosting IQ
1 hour 54 min ago - Reply to comment | Linux Journal
4 hours 55 min ago - play with linux? i think you mean work-around linux
13 hours 21 min ago - Where is Epistle?
13 hours 27 min ago - You forgot OwnCloud
13 hours 56 min ago - aplikasi free
17 hours 11 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.



Comments
Is there any coverage tool
Is there any coverage tool available that only works on source files and not on executables. I mean I dont want to compile the source files even once still I want to find out the coverage in the code.
Is it possible?
Please help. I'll really appreciate your effort if you get me any information regarding the matter.
Thanks!
help needed in installing ggcov
We have tried installing ggcov
we had followed following steps.
./configure (it satisfies our pc configuration)
make
make install
It is not getting installed properly...
How can we check it is properly installed or not....?
Is there any other steps to covered...
please kindly help
Regards
savita
hai, i understood your
hai,
i understood your problem.....
me also struck with that problem but what i did was,
i just installed ggcov-06 instead of ggcov-08.....
in ggcov ,it is asking someother file which we could not get fro internet...
so better to go to install ggcov-06
thanks and regards,
Rajasekhar...
ggcov
when i try to run ggcov in SUSE LINUX 10.1 using the command line ggcov ,i do not get any error message.but the results are not displayed.
the installation had no problems.
If anybody who is familiar with ggcov can give me a detailed idea about how to use this tool,please reply.
ggcov
anybody kindly post a reply regarding how to implement ggcov
I could run ggcov successfully on the command prompt but I can't view any GUI.So please tell me the syntax/procedures of using ggcov.My linux version is SUSE 9.0
error on running kprof
when i try to make the kprof i am getting this errors..
Kindly help me to correct and run the application
parseprofile_pose.h:30: error: expected `)' before â&â token
parseprofile_pose.cpp:29: error: prototype for âCParseProfile_pose::CParseProfile_pose(QTextStream&, QPtrVector&)â does not match any in class âCParseProfile_poseâ
parseprofile_pose.h:28: error: candidates are: CParseProfile_pose::CParseProfile_pose(const CParseProfile_pose&)
parseprofile_pose.h:36: error: CParseProfile_pose::CParseProfile_pose()
need help for GCOV
I have a problem:
I'm not able to gcc -fprofile-arcs -ftest-coverage file.c
Could you let me know how I can set environemt CFLAGS variable?
I have problem when I gcc on glib files i.e garray.c
please hel--
valgrind coverage
I thought I'd add a comment to say that some people are using Valgrind to do coverage testing using the 'cachegrind' tool.
Great article, a nice clear introduction to the topic.
Thanks
Thanks very much for this informative example. I was surprised that 50% is about average for code coverage!
Re: Coverage Measurement and Profiling
Very nice article, I enjoyed it and learned new (for me) methods
thanks a lot
b.
Don't forget lcov!
lcov is a nice addition to gcof; like ggcov, it shows summaries
for multiple files. It goes beyond ggcov, I think, in that it
handles entire directory hierarchies. It can handle the Linux
kernel and Wine, for instance. It outputs HTML rather than
presenting a GUI, but that's kind of a plus in my book.
It's at ltp.sf.net/coverage/lcov.php
Re: Don't forget lcov!
Lcov does look very nice. My intent wasn't to do a comprehensive survey of tools, but to introduce the basics of gcov and gprof and give an example of a representative "add-on" for each.