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 & 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 
printf("Sum of i & j is : %d\n", 35Sum of i & j is : 35
)                                                                                          = 21
printf("sqrtof of 225 is : %d\n", 15sqrtof of 225 is : 15
)                                                                                         = 22
+++ exited (status 0) +++

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

 
$ ltrace -c ./program_for_ltrace 
Sum of i & 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”

Android Android Build system Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Content Management System - CMS Core Kernel C Programs Development Environment Setup Errors & Failures git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense Moodle NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question