Tool to trace local function calls in Linux
Assuming you only want to be notified for specific functions, you can do it like this:
compile with debug informations (as you already have symbol informations, you probably also have enough debugs in)
given
#include <iostream>
int fac(int n) {
if(n == 0)
return 1;
return n * fac(n-1);
}
int main()
{
for(int i=0;i<4;i++)
std::cout << fac(i) << std::endl;
}
Use gdb to trace:
[js@HOST2 cpp]$ g++ -g3 test.cpp
[js@HOST2 cpp]$ gdb ./a.out
(gdb) b fac
Breakpoint 1 at 0x804866a: file test.cpp, line 4.
(gdb) commands 1
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end".
>silent
>bt 1
>c
>end
(gdb) run
Starting program: /home/js/cpp/a.out
#0 fac (n=0) at test.cpp:4
1
#0 fac (n=1) at test.cpp:4
#0 fac (n=0) at test.cpp:4
1
#0 fac (n=2) at test.cpp:4
#0 fac (n=1) at test.cpp:4
#0 fac (n=0) at test.cpp:4
2
#0 fac (n=3) at test.cpp:4
#0 fac (n=2) at test.cpp:4
#0 fac (n=1) at test.cpp:4
#0 fac (n=0) at test.cpp:4
6
Program exited normally.
(gdb)
Here is what i do to collect all function's addresses:
tmp=$(mktemp)
readelf -s ./a.out | gawk '
{
if($4 == "FUNC" && $2 != 0) {
print "# code for " $NF;
print "b *0x" $2;
print "commands";
print "silent";
print "bt 1";
print "c";
print "end";
print "";
}
}' > $tmp;
gdb --command=$tmp ./a.out;
rm -f $tmp
Note that instead of just printing the current frame(bt 1
), you can do anything you like, printing the value of some global, executing some shell command or mailing something if it hits the fatal_bomb_exploded
function :) Sadly, gcc outputs some "Current Language changed" messages in between. But that's easily grepped out. No big deal.
tracing program function calls
Yes, profiling is the only way to do this.
All you will get is the function address, which you can map again to its name by dladdr() API.
All the best!!!!
linux tool to show c function call from source
GNU cflow is your good friend.
https://www.gnu.org/software/cflow/manual/cflow.html
List of all function calls made in an application
How can we list all the functions being called in an application
For any realistically sized application, this list will have thousands of entries, which will probably make it useless.
You can find out all functions defined (but not necessarily called) in an application with the nm
command, e.g.
nm /path/to/a.out | egrep ' [TW] '
You can also use GDB to set a breakpoint on each function:
(gdb) set logging on # collect trace in gdb.txt
(gdb) set confirm off # you wouldn't want to confirm every one of them
(gdb) rbreak . # set a breakpoint on each function
Once you continue, you'll hit a breakpoint for each function called. Use the disable
and continue
commands to move forward. I don't believe there is an easy way to automate that, unless you want to use Python scripting.
Already mentioned gprof
is another good option.
Related Topics
How to Use Sed to Extract Substring
Linux: Find File Names With 4 or 5 Characters
How to Prompt For Yes/No/Cancel Input in a Linux Shell Script
How to Kill a Child Process After a Given Timeout in Bash
Pseudo-Terminal Will Not Be Allocated Because Stdin Is Not a Terminal
How to Join Multiple Lines of File Names into One With Custom Delimiter
Appending a Line to a File Only If It Does Not Already Exist
How to Build & Install Glfw 3 and Use It in a Linux Project
Shell Command to Tar Directory Excluding Certain Files/Folders
Why Data and Stack Segments Are Executable
How to Shutdown a Spring Boot Application in a Correct Way
How to Use Variables in Bash Sed Command, Specific Example
How to Compare Two Strings in Dot Separated Version Format in Bash
How to Change the Environment Variables of Another Process in Unix
What's the Difference Between Nohup and Ampersand
How to Quickly Sum All Numbers in a File
How to Redirect the Output of the Time Command to a File in Linux