Understanding execution and debugging of C program using ltrace

As we seen in previous post “Understanding Very Minimal C Program and its execution in Ubuntu Linux” , we tried to understand the basic minimum C program. Here we will try to understand, how its actually getting executed, when we tried to run this program with debugging tool ltrace as below,

ltrace is a “A library call tracer”,. ltrace is a program that simply runs the specified command until it exits. It intercepts and records the dynamic library calls which are called by the executed process and the signals which are received by that process. It can also intercept and print the system calls executed by the program.

$ vim program_for_ltrace.c
#include <stdio.h>
#include <math.h>

int main(int argc, char **argv) {
        int i=10, j=25, sqrtof=0;
        printf("Sum of i and j is : %d\n", i+j);
        sqrtof = sqrt(225.0);
        printf("sqrtof of 225 is : %d\n", sqrtof);
        return 0;
}
$ gcc -o program_for_ltrace program_for_ltrace.c
$ ltrace ./program_for_ltrace 
$ ltrace ./program_for_ltrace 
__libc_start_main(0x804840b, 1, 0xbf885c54, 0x8048480 <unfinished ...="">
printf("Sum of i &amp; j is : %d\n", 35Sum of i &amp; j is : 35
)                                                                                          = 21
printf("sqrtof of 225 is : %d\n", 15sqrtof of 225 is : 15
)                                                                                         = 22
+++ exited (status 0) +++

</unfinished>

-c option in ltrace decode low-level symbol names into user-level names.

$ ltrace -c ./program_for_ltrace 
Sum of i &amp; j is : 35
sqrtof of 225 is : 15
% time     seconds  usecs/call     calls      function
------ ----------- ----------- --------- --------------------
100.00    0.000577         288         2 printf
------ ----------- ----------- --------- --------------------
100.00    0.000577                     2 total

For other command line arguments to try, check using “ltrace –help”

Leave a Comment