C + + code coverage tool Gcov, Lcov

Source: Internet
Author: User
Tags gcov

Gcov is a code coverage tool that can be used in C/s + +, and is a built-in tool for GCC. Here's how to use Gcov to collect code coverage information.

To collect code overwrite information with Gcov, you need to add these 2 options "-fprofile-arcs-ftest-coverage" when GCC compiles the code, and compile this simple program

Gcc-fprofile-arcs-ftest-coverage Hello.c-o Hello

After compiling, you get an executable hello and hello.gcno file, and when you compile the file with GCC, the. gcno file is generated with the "-ftest-coverage" parameter, which contains information such as the program block and line number.
Next, you can run this Hello program

./hello 5
./hello 12

A HELLO.GCDA file is generated after the run finishes and is generated if an executable file is compiled with the "-fprofile-arcs" parameter and runs at least once. This file contains information about the basic block jumps of the program. Next you can generate code coverage information with Gcov:

Gcov hello.c

2 files Hello.c.gcov and Myfunc.c.gcov are generated after the run finishes. Open the message to look inside:

-: 0:SOURCE:MYFUNC.C
-: 0:graph:hello.gcno
-: 0:DATA:HELLO.GCDA
-: 0:runs:1
-: 0:programs:1
-: 1: #include
-: 2:
-: 3:void Test (int count)
1:4:{
-: 5:int i;
10:6: for (i = 1; i < count; i++)
-: 7: {
9:8: if (i% 3 = = 0)
3:9: printf ("%d is divisible by 3 \ n", i);
9:10:if (i% 11 = = 0)
#####: 11:printf ("%d is divisible by one \ n", i);
9:12:if (i% 13 = = 0)
#####: 13:printf ("%d is divisible by", I);
-: 14:}
1:15:}

is marked as # # # #的代码行就是没有被执行过的, the code covers the correct information, but it is really a cup of tea to read these words. Don't worry, there is another tool called Lcov, you can use the program to parse these obscure characters, the final output to the HTML format of the report, very good!

Lcov-d. -T ' Hello Test '-o ' hello_test.info '-B. -C

Specify Lcov in the current directory "." To find the code to cover the information, output as ' hello_test.info ', this hello_test.info is an intermediate result, need to use it genhtml to deal with, genhtml is a tool inside the Lcov.

Genhtml-o result Hello_test.info

Specifies that the output directory is result. A full HTML report is generated, a connection is made, and the directory is attached to a directory of any Web server, so you can see the report.

My project is using Lcov, and using Makefile, the main usage is as follows:

To define a macro file in makefile:

Profile

    1. Ifeq ($ (profile), 1)
    2. CFLAGS + =-fprofile-arcs
    3. CFLAGS + =-ftest-coverage
    4. Cxxflags + =-fprofile-arcs
    5. Cxxflags + =-ftest-coverage
    6. Ldflags + =-fprofile-arcs
    7. Ldflags + =-ftest-coverage
    8. Ldflags + =-lgcov
    9. Libldflags + =-fprofile-arcs
    10. Libldflags + =-ftest-coverage
    11. Libldflags + =-lgcov
    12. endif

Open this macro at compile time: profile=1 make RTM debug=0

Then the *.GCDA file is generated after running.

Use the following command to generate the report:

  1. Lcov-d. -B. -c-o $rtmcovfile >/dev/null
  2. #sed-i-e ' s#/home/mac_ci/hudson/home/jobs/trunk_rtm/workspace/trunk#\. #g ' $rtmcovfile
  3. #-Keep Sstddps folders in the
  4. Lcov-r $rtmcovfile "*/ss_macdata/*"-O $rtmcovfile
  5. Lcov-r $rtmcovfile "*/ss_macpswmp/*"-O $rtmcovfile
  6. Lcov-r $rtmcovfile "*/sscommon/*"-O $rtmcovfile
  7. Lcov-r $rtmcovfile "*/ssdata/*"-O $rtmcovfile
  8. Lcov-r $rtmcovfile "*/ssdcmps/*"-O $rtmcovfile
  9. Lcov-r $rtmcovfile "*/sspscommon/*"-O $rtmcovfile
  10. Lcov-r $rtmcovfile "*/sstestmodel/*"-O $rtmcovfile
  11. #-Remove unneeded paths from coverage
  12. Lcov-r $rtmcovfile "/build/ltesdkroot/*"-O $rtmcovfile
  13. Lcov-r $rtmcovfile "*/sc_dsp_common/*"-O $rtmcovfile
  14. Lcov-r $rtmcovfile "*/c_test/*"-O $rtmcovfile
  15. Lcov-r $rtmcovfile "*/t_tools/*"-O $rtmcovfile
  16. Lcov-r $rtmcovfile "*/env/*"-O $rtmcovfile
  17. Lcov-r $rtmcovfile "*/i_interface/*"-O $rtmcovfile
  18. Lcov-r $rtmcovfile "/usr/*"-O $rtmcovfile
  19. Lcov-r $rtmcovfile "/opt/*"-O $rtmcovfile
  20. MV $rtmcovfile $PROJECT _root/c_test/sc_mac/maclinuxrtm/logs
  21. CD $PROJECT _root/c_test/sc_mac/maclinuxrtm/logs
  22. Genhtml-o tdd_ut_rtm_tests_coverage $rtmcovfile >/dev/null

Test Results Overview

Coverage for a specific file

C + + code coverage tool Gcov, Lcov

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.