What happens under the hood when you execute your C Programs?

I was a little curious to find out how my program gets executed. In order to find that out, I decided to write a simple c program which is here:



 #include <stdio.h>
int main()
{
    sleep(30);
    printf("Just testing an application!");
}



I have kept the sleep(30) in the beginning of the program so as to give me time to execute the DTrace script, about which I will be talking a little later.


Now i complied it with Sun Compiler and GCC



kumar@myosbox:~/Desktop/Demos$ suncc -o sunCCOutput simplec.c


kumar@myosbox:~/Desktop/Demos$ gcc -o gccOutput simplec.c



Now I wrote a simple DTrace script using the pid provider in order to figure out how printf is implemented (execution detail - functions which get invoked when printf function is called) in these compilers. The dtrace script is below:



pid$1::$2:entry
{
        self->trace = 1;
}

pid$1::$2:return
/self->trace/
{
        self->trace = 0;
}

pid$1:::entry,
pid$1:::return
/self->trace/
{
}



Now it was the time to execute this script and compare the outputs. I am going to redirect the output of this script into two text files (sunCCOutput.txt and gccOutput.txt).



kumar@myosbox:~/Desktop/Demos$ ./sunCCOutput 
kumar@myosbox:~/Desktop/Demos$ pfexec dtrace -F -s pid.d `pgrep sunCCOutput` printf > SunCCOutput.txt
dtrace: script 'pid.d' matched 6712 probes
kumar@myosbox:~/Desktop/Demos$ ./gccOutput
kumar@myosbox:~/Desktop/Demos$ pfexec dtrace -F -s pid.d `pgrep gccOutput` printf > gccOutput.txt
dtrace: script 'pid.d' matched 6714 probes



Now, the content of the file SunCCOutput.txt is as follows:



CPU FUNCTION                                 
1 -> printf
1 -> _setorientation
1 <- _setorientation
1 -> _ndoprnt
1 -> _findbuf
1 -> isatty
1 -> ___errno
1 <- ___errno
1 -> ioctl
1 <- ioctl
1 <- isatty
1 -> _setbufend
1 -> getxfdat
1 <- getxfdat
1 -> isseekable
1 -> ___errno
1 <- ___errno
1 -> fstat64
1 <- fstat64
1 -> ___errno
1 <- ___errno
1 <- isseekable
1 <- _setbufend
1 <- _findbuf
1 -> _realbufend
1 -> getxfdat
1 <- getxfdat
1 <- _realbufend
1 -> memcpy
1 <- memmove
1 <- memcpy
1 -> memchr
1 <- memchr
1 -> ferror
1 <- ferror
1 <- _ndoprnt
1 -> ferror
1 <- ferror


Contents of the file gccOutput.txt is as follows:



CPU FUNCTION                                 
1 -> printf
1 -> _setorientation
1 <- _setorientation
1 -> _ndoprnt
1 -> _findbuf
1 -> isatty
1 -> ___errno
1 <- ___errno
1 -> ioctl
1 <- ioctl
1 <- isatty
1 -> _setbufend
1 -> getxfdat
1 <- getxfdat
1 -> isseekable
1 -> ___errno
1 <- ___errno
1 -> fstat64
1 <- fstat64
1 -> ___errno
1 <- ___errno
1 <- isseekable
1 <- _setbufend
1 <- _findbuf
1 -> _realbufend
1 -> getxfdat
1 <- getxfdat
1 <- _realbufend
1 -> memcpy
1 <- memmove
1 <- memcpy
1 -> memchr
1 <- memchr
1 -> ferror
1 <- ferror
1 <- _ndoprnt
1 -> ferror
1 <- ferror

So you can see that there is no difference between gcc and suncc when if comes to implement the printf function. But its is so interesting to see that there are 19 function calls made everytime I use a printf in my c program! WOW! ;) I wonder if I could know it this easily if there was no Dtrace!

Comments:

For this simple thing, 'strace' would probably do the job. But yeah, DTrace has its niche where strace can't do anything!

Posted by Amit Saha on December 20, 2009 at 07:59 AM IST #

>So you can see that there is no difference between gcc and suncc when if comes to implement the printf function

That is because the printf function is implemented by libc and not by the compiler :-)

Posted by anon on December 29, 2009 at 01:32 PM IST #

Post a Comment:
Comments are closed for this entry.
About

I am Abhishek and I work for Sun/Oracle! In this blog, I share my interest in systems, solaris, linux and other technologies :-)

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today