LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2005, 03:57 AM   #1
rajsun
Member
 
Registered: Mar 2005
Posts: 61

Rep: Reputation: 15
Code coverage


Hi all,
I m useing 'gcov' for first time.
The code is :
========================================================================
#include<stdio.h>

void Disply1(int sum)
{
printf("The sum %d less than 100\n",sum);
}

void Disply2(int sum)
{
printf("The sum %d more than 100\n",sum);
}


int main()
{
int i=0;
int sum = 0;

for(; i<10; )
{
sum+=5;
i++;
}

if (sum <= 100)
{
Disply1(sum);
}
else
{
Disply2(sum);
}
return 0;
}
==============================================================================
I compiled the code :
gcc -fprofile-arcs -ftest-coverage cfile.c

then I executed the code :
./a.out

then i typed the following commend :
gcov -b cfile.c

And I got the following code :
==============================================================================
#include<stdio.h>

void Disply1(int sum)
1 {
1 printf("The sum %d less than 100\n",sum);
call 0 returns = 100%
}

void Disply2(int sum)
###### {
###### printf("The sum %d more than 100\n",sum);
call 0 never executed
}


int main()
1 {
1 int i=0;
1 int sum = 0;

21 for(; i<10; )
branch 0 taken = 91%
branch 1 taken = 100%
branch 2 taken = 100%
{
10 sum+=5;
10 i++;
}

1 if (sum <= 100)
branch 0 taken = 0%
{
1 Disply1(sum);
call 0 returns = 100%
branch 1 taken = 100%
}
else
{
###### Disply2(sum);
call 0 never executed
}
1 return 0;
}
==============================================================================


Problem :
I cant understand

Why it is giving :
-----------------------------
21 for(; i<10; )
branch 0 taken = 91%
branch 1 taken = 100%
branch 2 taken = 100%
{
10 sum+=5;
10 i++;
}
-------------------------------------

1-> what is the meaning of branch 0, branch 1 and branch 2.
2-> why for statement has been executed 21 time?


Plz hepl me I could not understand the cause.


Tahnking U all,
With Regard
Rajsun
 
Old 06-29-2005, 03:14 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
gcov manual says
Code:
       For each basic block, a line is printed after the last line of the
       basic block describing the branch or call that ends the basic block.
       There can be multiple branches and calls listed for a single source
       line if there are multiple basic blocks that end on that line.  In this
       case, the branches and calls are each given a number.  There is no sim-
       ple way to map these branches and calls back to source constructs.  In
       general, though, the lowest numbered branch or call will correspond to
       the leftmost construct on the source line.
My guess (and it's a good guess) is that in line
for(; i<10; )
the three branches are (empty) then i < 10; and then (empty).

21 runs - don't know why (if you sum up execution of the three branches above, the result is 21, but it's probably not the case).
 
Old 06-29-2005, 05:28 PM   #3
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
Look at the asembly code

You can use:

gcc -g cfile.c
objdump -S a.out > cfile.asm

And look for the line with the for loop. My disassembly looks like:

for(; i < 10 {
804837c: 83 7d fc 09 cmpl $0x9,0xfffffffc(%ebp)
8048380: 7e 02 jle 8048384 <main+0x26>
8048382: eb 0d jmp 8048391 <main+0x33>
sum += 5;
8048384: 8d 45 f8 lea 0xfffffff8(%ebp),%eax
8048387: 83 00 05 addl $0x5,(%eax)
i++;
804838a: 8d 45 fc lea 0xfffffffc(%ebp),%eax
804838d: ff 00 incl (%eax)
804838f: eb eb jmp 804837c <main+0x1e>
}
if(sum <= 100) {
8048391: 83 7d f8 64 cmpl $0x64,0xfffffff8(%ebp)

So the first branch is the jle (jump less equal) which executes only 10 time from 11 executions (0.909% aprox 0.91%)
I assume the the second branch is the fall branch (jle not taken so flow goes to the next instruction).
The third branch must be the jmp.

The execution count could be computed on basic blocks and in the for instruction there are 2 basic blocks (cmp and jle one, and jmp another) So 11 for the first and 10 for the second.

Hope it helps.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Editing buttons (quote, code etc.) add the code to the end vharishankar LQ Suggestions & Feedback 2 09-13-2004 09:32 AM
Open Firmware code for booting OS from SATA : sample code available somewhere ? drsparikh Linux - Hardware 0 03-12-2004 11:16 AM
Code Coverage tools icwong Programming 3 10-27-2003 07:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration