Wil*_*iam 3 c parameters logging function
我有一个C二进制文件(可用的源代码)和预定义的工作负载.我想以调用顺序记录所有函数调用及其各自的参数,以及返回值.例如,在下面的代码中:
int myfunc(int value){
return value*2;
}
int main(void){
int i;
i = myfunc(10);
i = myfunc(12);
i = myfunc(20);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它会导致像这样的日志文件:
int main()
int myfunc(10)
myfunc return 20
int myfunc(12)
myfunc return 24
int myfunc(21)
myfunc return 42
main return 0
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用英特尔PIN来完成它,并且它工作得非常好,除了当变量是指针,数组,结构或typedef时.我需要所有变量.
我有源代码,我可以使用调试选项编译它.
有任何想法吗?
手动PS调试是不可行的,因为我的实际工作负载有来自90个不同功能的大约5000个调用.
编辑:操作系统是Arch Linux 64bit
您可以使用gdb在运行时使用断点"事件"来跟踪函数调用及其返回值:
(gdb) help command
Set commands to be executed when a breakpoint is hit.
Give breakpoint number as argument after "commands".
With no argument, the targeted breakpoint is the last one set.
The commands themselves follow starting on the next line.
Type a line containing "end" to indicate the end of them.
Give "silent" as the first line to make the breakpoint silent;
then no output is printed when it is hit, except what the commands print.
Run Code Online (Sandbox Code Playgroud)
看看我的方法:
源代码:
int myfunc( int n )
{
int val = n * 2;
return val;
}
int main(void)
{
int i;
i = myfunc(10);
i = myfunc(12);
i = myfunc(20);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
1)使用-g选项在调试模式下编译程序:
$ gcc -g program.c -o program
Run Code Online (Sandbox Code Playgroud)
2)运行gdb:
$ gdb ./program
Run Code Online (Sandbox Code Playgroud)
3)在gdbshell中,您可以为要跟踪的每个函数和/或行设置断点:
(gdb) break myfunc
Breakpoint 1 at 0x4011d6: file program.c, line 3.
(gdb) break program.c:5
Breakpoint 2 at 0x4011de: file program.c, line 5.
Run Code Online (Sandbox Code Playgroud)
4)使用以下silent命令设置静默断点事件:
(gdb) command 1
Type commands for breakpoint(s) 1, one per line.
End with a line saying just "end".
>silent
>printf "calling myfunc( %d )\n", n
>cont
>end
(gdb) command 2
Type commands for breakpoint(s) 2, one per line.
End with a line saying just "end".
>silent
>printf "myfunc return %d\n", val
>cont
>end
Run Code Online (Sandbox Code Playgroud)
5)运行程序:
(gdb) run
Starting program: /home/Administrator/program
[New Thread 192.0xe00]
[New Thread 192.0x55c]
calling myfunc( 10 )
myfunc return 20
calling myfunc( 12 )
myfunc return 24
calling myfunc( 20 )
myfunc return 40
[Inferior 1 (process 192) exited normally]
Run Code Online (Sandbox Code Playgroud)
Obs:
gdb读取调用.gdbinit时调用的脚本文件.此文件包含gdb在gdb启动期间自动执行的命令,可用于自动执行调试操作.
希望能帮助到你!