Skip to content

Instantly share code, notes, and snippets.

@storypku
Last active August 29, 2015 13:59
Show Gist options
  • Save storypku/10899279 to your computer and use it in GitHub Desktop.
Save storypku/10899279 to your computer and use it in GitHub Desktop.
Coverage test and visualisation for C/C++ programs using gcov and lcov
今天晚上尝试用gcov和lcov做一个C++项目的代码覆盖率测试。这个项目是多进程的,并且其中若干进程都是长时运行
的服务器端进程。试验结果说明gcov和lcov是可以胜任这一任务的。因此,为了备忘,取一简单的试验程序,说明使
用的步骤是怎样的。整个过程的session log如下:
[root@dev2:coverage]#ls
hello.c Makefile myapp.c myapp.h
[root@dev2:coverage]#cat hello.c
#include <stdio.h>
#include "myapp.h"
#include <signal.h>
#include <stdlib.h>
void sig_handler(int sig) {
exit(0);
}
int main() {
signal(SIGINT, sig_handler);
int a = 10;
myfunc();
if (a > 10)
printf(" a > 10\n");
else
printf(" a <= 10\n");
printf("hello world from %s\n", __func__);
while(1)
;
return 0;
}
[root@dev2:coverage]#cat myapp.c
#include "myapp.h"
#include <stdio.h>
void myfunc(void) {
printf("My func called\n");
}
int useless(void) {
int c = 5;
c = c * 2;
return c;
}
[root@dev2:coverage]#cat myapp.h
#ifndef MY_APP_H_
#define MY_APP_H_
void myfunc(void);
int useless(void);
#endif
[root@dev2:coverage]#cat Makefile
CC = gcc
TARGET = hello
OBJS = myapp.o hello.o
$(TARGET):$(OBJS)
$(CC) -o $@ $^ -fprofile-arcs
%.o:%.c
$(CC) -c $< -o $@ -ftest-coverage -fprofile-arcs
.PHONY:clean
clean:
@rm -f $(OBJS) $(TARGET) *.gcno *.gcda
[root@dev2:coverage]#ls
hello.c Makefile myapp.c myapp.h
[root@dev2:coverage]#make
gcc -c myapp.c -o myapp.o -ftest-coverage -fprofile-arcs
gcc -c hello.c -o hello.o -ftest-coverage -fprofile-arcs
gcc -o hello myapp.o hello.o -fprofile-arcs
[root@dev2:coverage]#ls
hello hello.c hello.gcno hello.o Makefile myapp.c myapp.gcno myapp.h myapp.o
[root@dev2:coverage]#./hello
My func called
a <= 10
hello world from main
^C[root@dev2:coverage]#ls
hello hello.gcda hello.o myapp.c myapp.gcno myapp.o
hello.c hello.gcno Makefile myapp.gcda myapp.h
[root@dev2:coverage]#lcov -c -o myapp.prof -d .
Capturing coverage data from .
Found gcov version: 4.4.7
Scanning . for .gcda files ...
Found 2 data files in .
Processing ./hello.gcda
Processing ./myapp.gcda
Finished .info-file creation
[root@dev2:coverage]#ls
hello hello.gcda hello.o myapp.c myapp.gcno myapp.o
hello.c hello.gcno Makefile myapp.gcda myapp.h myapp.prof
[root@dev2:coverage]#genhtml myapp.prof -o lcov_report
Reading data file myapp.prof
Found 2 entries.
Found common filename prefix "/home/story"
Writing .css and .png files.
Generating output.
Processing file test/coverage/hello.c
Processing file test/coverage/myapp.c
Writing directory view page.
Overall coverage rate:
lines......: 72.2% (13 of 18 lines)
functions..: 75.0% (3 of 4 functions)
[root@dev2:coverage]#ls
hello hello.gcda hello.o Makefile myapp.gcda myapp.h myapp.prof
hello.c hello.gcno lcov_report myapp.c myapp.gcno myapp.o typescript
[root@dev2:coverage]#cd lcov_report/
[root@dev2:lcov_report]#ll
total 52
-rw-r--r-- 1 root root 141 Apr 16 23:39 amber.png
-rw-r--r-- 1 root root 141 Apr 16 23:39 emerald.png
-rw-r--r-- 1 root root 9097 Apr 16 23:39 gcov.css
-rw-r--r-- 1 root root 167 Apr 16 23:39 glass.png
-rw-r--r-- 1 root root 3707 Apr 16 23:39 index.html
-rw-r--r-- 1 root root 3700 Apr 16 23:39 index-sort-f.html
-rw-r--r-- 1 root root 3700 Apr 16 23:39 index-sort-l.html
-rw-r--r-- 1 root root 141 Apr 16 23:39 ruby.png
-rw-r--r-- 1 root root 141 Apr 16 23:39 snow.png
drwxr-xr-x 3 root root 4096 Apr 16 23:39 test
-rw-r--r-- 1 root root 117 Apr 16 23:39 updown.png
[root@dev2:lcov_report]#which genhtml
/usr/bin/genhtml
[root@dev2:lcov_report]#rpm -qf /usr/bin/genhtml
lcov-1.7-1.el6.noarch
[root@dev2:lcov_report]#which lcov
/usr/bin/lcov
[root@dev2:lcov_report]#rpm -qf /usr/bin/lcov
lcov-1.7-1.el6.noarch
[root@dev2:lcov_report]#which gcov
/usr/bin/gcov
[root@dev2:lcov_report]#rpm -qf /usr/bin/gcov
gcc-4.4.7-4.el6.x86_64
参考:
整个试验是参考http://www.cnblogs.com/turtle-fly/archive/2013/01/09/2851474.html完成的。特此致谢。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment