gdb - 列出所有函数调用

时间:2020-12-29 13:28:16
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] '
EG:
[root@monitor ~]# nm ./test | egrep ' [TW] '              
00000000004005f0 T __libc_csu_fini
0000000000400600 T __libc_csu_init
00000000004006c8 T _fini
00000000004003c8 T _init
0000000000400420 T _start
0000000000600990 W data_start
0000000000400504 T main
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.
record function-call-history should be a great hardware accelerated possibility 

if you are one of the few people () with a CPU that supports Intel Processor Tracing (Intel PT, intel_pt in /proc/cpuinfo).

GDB docs claim that it can produce output like:

(gdb) list ,
void foo (void)
{
} void bar (void)
{
...
foo ();
...
}
(gdb) record function-call-history /ilc
bar inst , at foo.c:,
foo inst , at foo.c:,
bar inst , at foo.c:,
Before using it you need to run: start
record btrace
which is where a non capable CPU fails with: Target does not support branch tracing.
CPU support is further discussed at: How to run record instruction-history and function-call-history in GDB?