Starting off working on an existing project can be tedious, specially when you are trying to implement a new feature. In case of many projects, the debug mode becomes handy. But what if the logs aren’t enough to get you to the point of execution? I am working on a similar project where I needed to understand the code flow to capture a filesystem event. The program would wait till such an event occurs.
Before someone suggests strace land ltrace, here’s what these two utilities do:
strace: trace system calls and signals
ltrace: 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.
In my case I need the function call trace of the program in a specific scenario for better understanding of the code.
I started with the usual tool, GDB, and ran the program. As I didn’t know where to add a breakpoint, I let the program run till it waits, crashed it using
<Ctrl-c>, and issued bt full. That took me a little past main().
I was wondering if there’s a tool that can trace the full sequence of calls by automatically stepping in each function call. Eventually, I found a very helpful C program called gdbwalkthrough. I have uploaded a copy here, feel free to download it. It automates GDB to send ‘s’ and then read GDB’s output, continuously, and then output the result to a text file (myfile2.txt) as well as the terminal.
To compile the program, run:
$ gcc -o gdbwalkthrough gdbwalkthrough.c
$ ./gdbwalkthrough <application full path> [application arguments]
$ sudo ./gdbwalkthrough /usr/local/bin/sysdig -A -c echo_fds "fd.filename=passwd"
This script can be modified easily to step only through a set of lines, or to step only a predefined number of times. Replace
int x=1000; while(x--) writeDebugger("s",NULL);
[Original source, the script is a modified version]