![]() |
ltrace - Linux Debugging Utility Tutorial
'ltrace' is another Linux Utility similar to 'strace'. However, ltrace lists all the library calls being called in an executable or a running process. Its name itself comes from library-call tracing. This tool is very useful for debugging user-space applications to determine which library call is failing. It is also capable of receiving signals for segmentation faults, etc.
Using ltraceOne does NOT need any root privileges to run ltrace. Using ltrace to debug an application executable Code:
ltrace ./executable <parameters>Code:
ltrace -p <PID>Code:
rupali@ubuntu:~/source$ ltrace timeRefer ltrace man page for details to run ltrace with various other options offered. Man page can be found by typing Code:
man ltraceDebugging with ltraceThe ltrace utility is most useful for debugging user-space application programs as the bugs could be surfaced out seeing the dynamic records of the library calls. Here, let us take an scenario where we'll see what kind of errors we see and how we debug while using ltrace. Lets take a sample program. This program has a bug, and I suggest you not to even try to find the bug in this program on your own. Let ltrace help us to find the bug, as that is what we are here to learn. :) Code:
1. #include <stdio.h>Code:
rupali@ubuntu:~/source$ gcc file.c -Wall -o fileCode:
rupali@ubuntu:~/source$ltrace ./fileNext line has listed the fopen() call to open 'rfile.txt' in write plus mode and returning a valid file pointer address. Moving on to the next library call, we have fwrite which is unfinished. Here is the bug because of which program crashed. So focusing on this call and analyzing, notice the file pointer containing the address in fwrite() call doesnt match the one in fopen() call. Hence, the crash. The program is trying to write onto a file using file pointer which is invalid following a SIGSEGV signal. Getting back to our program, the bug is in line 7 of our program where the file pointer being passed to the fprintf() call is tampered. So, we just found the bug. Here is the corrected code Code:
1. #include <stdio.h>ConclusionNow we know how to use ltrace for debugging applications. For more better intricate functionalities like breakpoints, stepping through, etc use GDB. However, most of the bugs can be resolved using ltrace/strace by knowing and analysing the calls and their success and failure errors. |
| All times are GMT +5.5. The time now is 08:24. |