使用gcc的-finstrument-functions选项进行函数跟踪
GCC Function instrumentation机制可以用来跟踪函数的调用关系,在gcc中对应的选项为“-finstrument-functions”。可查看gcc的man page来获取更详细信息。
编译时如果为gcc加上“-finstrument-functions”选项,那在每个函数的入口和出口处会各增加一个额外的hook函数的调用,增加的这两个函数分别为:
- void __cyg_profile_func_enter (void *this_fn, void *call_site);
- void __cyg_profile_func_exit (void *this_fn, void *call_site);
其中第一个参数为当前函数的起始地址,第二个参数为返回地址,即caller函数中的地址。
这是什么意思呢?例如我们写了一个函数func_test(),定义如下:
- static void func_test(v)
- {
- /* your code... */
- }
那通过-finstrument-functions选项编译后,这个函数的定义就变成了:
- static void func_test(v)
- {
- __cyg_profile_func_enter(this_fn, call_site);
- /* your code... */
- __cyg_profile_func_exit(this_fn, call_site);
- }
我们可以按照自己的需要去实现这两个hook函数,这样我们就可以利用this_fn和call_site这两个参数大做文章。
例如下面这段代码:
- instrfunc.c:
- #include <stdio.h>
- #define DUMP(func, call) \
- printf("%s: func = %p, called by = %p\n", __FUNCTION__, func, call)
- void __attribute__((no_instrument_function))
- __cyg_profile_func_enter(void *this_func, void *call_site)
- {
- DUMP(this_func, call_site);
- }
- void __attribute__((no_instrument_function))
- __cyg_profile_func_exit(void *this_func, void *call_site)
- {
- DUMP(this_func, call_site);
- }
- int do_multi(int a, int b)
- {
- return a * b;
- }
- int do_calc(int a, int b)
- {
- return do_multi(a, b);
- }
- int main()
- {
- int a = 4, b = 5;
- printf("result: %d\n", do_calc(a, b));
- return 0;
- }
这段代码中实现了两个hook函数,即打印出所在函数的函数地址以及返回地址。
编译代码:
- [zhenfg@ubuntu]code:$ gcc -finstrument-functions instrfunc.c -o instrfunc
- [zhenfg@ubuntu]code:$ ./instrfunc
- __cyg_profile_func_enter: func = 0x8048521, called by = 0xb75554e3
- __cyg_profile_func_enter: func = 0x80484d8, called by = 0x8048562
- __cyg_profile_func_enter: func = 0x804849a, called by = 0x8048504
- __cyg_profile_func_exit: func = 0x804849a, called by = 0x8048504
- __cyg_profile_func_exit: func = 0x80484d8, called by = 0x8048562
- result: 20
- __cyg_profile_func_exit: func = 0x8048521, called by = 0xb75554e3
通过反汇编的代码(objdump -D instrfunc)可以看到,这些地址和函数的对应关系为:
- __cyg_profile_func_enter: func = 0x8048521(main), called by = 0xb75554e3
- __cyg_profile_func_enter: func = 0x80484d8(do_calc), called by = 0x8048562(main)
- __cyg_profile_func_enter: func = 0x804849a(do_multi), called by = 0x8048504(do_calc)
- __cyg_profile_func_exit: func = 0x804849a(do_multi), called by = 0x8048504(do_calc)
- __cyg_profile_func_exit: func = 0x80484d8(do_calc), called by = 0x8048562(main)
- result: 20
- __cyg_profile_func_exit: func = 0x8048521(main), called by = 0xb75554e3
实际上这就给出了函数的调用关系。
如果不想跟踪某个函数,可以给该函数指定“no_instrument_function”属性。需要注意的是,__cyg_profile_func_enter()和__cyg_profile_func_exit()这两个hook函数是一定要加上“no_instrument_function”属性的,不然,自己跟踪自己就会无限循环导致程序崩溃,当然,也不能在这两个hook函数中调用其他需要被跟踪的函数。
得到一系列的地址看起来不太直观,我们更希望看到函数名,幸运的是,addr2line工具为我们提供了这种可能。我们先看一下addr2line的使用方法:
- [zhenfg@ubuntu]code:$ addr2line --help
- Usage: addr2line [option(s)] [addr(s)]
- Convert addresses into line number/file name pairs.
- If no addresses are specified on the command line, they will be read from stdin
- The options are:
- @<file> Read options from <file>
- -a --addresses Show addresses
- -b --target=<bfdname> Set the binary file format
- -e --exe=<executable> Set the input file name (default is a.out)
- -i --inlines Unwind inlined functions
- -j --section=<name> Read section-relative offsets instead of addresses
- -p --pretty-print Make the output easier to read for humans
- -s --basenames Strip directory names
- -f --functions Show function names
- -C --demangle[=style] Demangle function names
- -h --help Display this information
- -v --version Display the program's version
首先要注意,使用addr2line工具时,需要用gcc的“-g”选项编译程序增加调试信息。
同样是上面的程序,我们加上-g选项再编译一次:
- [zhenfg@ubuntu]code:$ gcc -g -finstrument-functions instrfunc.c -o instrfunc
- [zhenfg@ubuntu]code:$ ./instrfunc
- __cyg_profile_func_enter: func = 0x8048521, called by = 0xb757d4e3
- __cyg_profile_func_enter: func = 0x80484d8, called by = 0x8048562
- __cyg_profile_func_enter: func = 0x804849a, called by = 0x8048504
- __cyg_profile_func_exit: func = 0x804849a, called by = 0x8048504
- __cyg_profile_func_exit: func = 0x80484d8, called by = 0x8048562
- result: 20
- __cyg_profile_func_exit: func = 0x8048521, called by = 0xb757d4e3
使用addr2line尝试查找0x8048504地址所在的函数:
- [zhenfg@ubuntu]code:$ addr2line -e instrfunc -a 0x8048504 -fp -s
- 0x08048504: do_calc at instrfunc.c:25
这样一来,就可以通过gcc的“-finstrument-functions”选项结合addr2line工具,方便的对一个程序中的函数进行跟踪。并且既然我们可以自己实现hook函数,那不仅仅可以用来跟踪函数调用关系,你可以在hook函数中添加自己想做的事情,例如添加一些统计信息。
另外,我们知道__builtin_return_address(level)宏可以获得不同层级的函数返回地址,但是在某些体系架构(如mips)中,__builtin_return_address(level)只能获得当前函数的直接调用者的地址,即level只能是0,那这时,就可使用上述方法来跟踪函数调用关系(mips中竟然能用,确实有些小吃惊)。
接下来可以看一下gcc是如何将hook函数嵌入各个函数中的,以反汇编代码中的do_multi()函数为例(这是mips的汇编代码),在mips中,ra寄存器用来存储返回地址,a0-a3用来做函数参数。
- 004006c8 <do_multi>:
- 4006c8: 27bdffd8 addiu sp,sp,-40
- 4006cc: afbf0024 sw ra,36(sp) ;;存储ra寄存器(返回地址)的值
- 4006d0: afbe0020 sw s8,32(sp)
- 4006d4: afb1001c sw s1,28(sp)
- 4006d8: afb00018 sw s0,24(sp)
- 4006dc: 03a0f021 move s8,sp
- 4006e0: 03e08021 move s0,ra ;;s0 = ra
- 4006e4: afc40028 sw a0,40(s8)
- 4006e8: afc5002c sw a1,44(s8)
- 4006ec: 02001021 move v0,s0 ;;v0 = s0
- 4006f0: 3c030040 lui v1,0x40
- 4006f4: 246406c8 addiu a0,v1,1736 ;;将本函数的地址赋值给a0寄存器
- 4006f8: 00402821 move a1,v0 ;;将返回地址ra的值赋值给a1寄存器
- 4006fc: 0c100188 jal 400620 <__cyg_profile_func_enter> ;;调用hook函数
- 400700: 00000000 nop
- 400704: 8fc30028 lw v1,40(s8)
- 400708: 8fc2002c lw v0,44(s8)
- 40070c: 00000000 nop
- 400710: 00620018 mult v1,v0
- 400714: 00008812 mflo s1
- 400718: 02001021 move v0,s0
- 40071c: 3c030040 lui v1,0x40
- 400720: 246406c8 addiu a0,v1,1736 ;;将本函数的地址赋值给a0寄存器
- 400724: 00402821 move a1,v0 ;;将返回地址ra的值赋值给a1寄存器
- 400728: 0c10019d jal 400674 <__cyg_profile_func_exit> ;;调用hook函数
- 40072c: 00000000 nop
- 400730: 02201021 move v0,s1
- 400734: 03c0e821 move sp,s8
- 400738: 8fbf0024 lw ra,36(sp) ;;恢复ra寄存器(返回地址)的值
- 40073c: 8fbe0020 lw s8,32(sp)
- 400740: 8fb1001c lw s1,28(sp)
- 400744: 8fb00018 lw s0,24(sp)
- 400748: 27bd0028 addiu sp,sp,40
- 40074c: 03e00008 jr ra
- 400750: 00000000 nop
上述反汇编的代码中,使用“-finstrument-functions”选项编译程序所增加的指令都已注释出来,实现没什么复杂的,在函数中获得自己的地址和上一级caller的地址并不是什么难事,然后将这两个地址传给__cyg_profile_func_enter和__cyg_profile_func_exit就好了。
使用gcc的-finstrument-functions选项进行函数跟踪相关推荐
- VScode检测到#include 错误,请更新includepath。已为此翻译单元 禁用波形曲线//gcc : 无法将“gcc”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
VScode检测到#include 错误,请更新includepath. 可能会遇到的问题: 1. VScode检测到#include 错误,请更新includepath. 2.gcc : 无法将&q ...
- gcc ------ 编译与链接选项及CFLAGS、LDFLAGS、LIBS
gcc ------ 编译与链接选项及CFLAGS.LDFLAGS.LIBS GCC手册:https://gcc.gnu.org/onlinedocs/ GCC编译选项CFLAGS参数 选项 说明 - ...
- gcc/g++ -O 优化选项说明
查查gcc手册就知道了,每个编译选项都控制着不同的优化选项 下面从网络上copy过来的,真要用到这些还是推荐查阅手册 -O设置一共有五种:-O0.-O1.-O2.-O3和-Os. 除了-O0以外,每 ...
- 使用gcc的-E -P选项展开源代码中的宏
使用gcc的-E -P选项展开源代码中的宏- - 原文出处:http://journeyboy.bokee.com/614 ...
- gcc/g++ -O 优化选项说明
查查gcc手册就知道了,每个编译选项都控制着不同的优化选项 下面从网络上copy过来的,真要用到这些还是推荐查阅手册 -O设置一共有五种:-O0.-O1.-O2.-O3和-Os. 除了-O0以外,每一 ...
- Built-in Functions - 内置函数 - print()
Built-in Functions - 内置函数 - print() https://docs.python.org/zh-cn/3/library/functions.html https://d ...
- php xdebug 中文手册,Xdebug文档(四)函数跟踪
Xdebug能让你把所有函数调用,包括参数和返回值以不同的格式记录到文件中. 这些号称"函数跟踪"功能能帮助你面对一个新应用程序,亦或者在程序运行时你想弄清楚它在做什么.函数跟踪功 ...
- GCC中通过--wrap选项使用包装函数
在使用GCC编译器时,如果不想工程使用系统的库函数,例如在自己的工程中可以根据选项来控制是否使用系统中提供的malloc/free, new/delete函数,可以有两种方法: (1). 使用LD_P ...
- linux gcc g++编译命令选项
gcc/g++在执行编译工作的时候,总共需要4步 1.预处理,生成.i的文件[预处理器cpp] 2.将预处理后的文件不转换成汇编语言,生成文件.s[编译器egcs] 3.有汇编变为目标代码(机器代码) ...
最新文章
- 【机器学习算法-python实现】矩阵去噪以及归一化
- [AGC001 D]Arrays and Palindrome
- mysql的优化总结
- python拼图游戏代码_GitHub 上哪些勾起回忆的经典小游戏(Python)
- SMT32H743+CubeMX-配置MPU后,在Keil上的程序卡死
- 【概率论】对弈输光模型,ruin model
- 什么是php渲染,php数据渲染输出
- android人脸识别demo_C#开发实录:基于免费SDK实现人脸识别应用开发
- python教程-4.数据处理numpy-pandas
- android获取当前显示的view,Android中ViewPager获取当前显示的Fragment
- spss可以关键词词频分析吗_词频分析及常用工具比较研究.pdf
- 实战!使用Docker安装OnlyOffice
- U盘拷贝者MBR勒索木马分析
- 人工智能项目案例:AI+企业智能化管理
- 苹果手机怎么投屏不了,苹果手机怎么投屏电脑
- 悲伤的时候总会想起什么
- 【黑马旅游网】项目完结+未完成功能实现+个人总结+bug记录
- php怎么把中文转,PHP如何将中文转为拼音?
- 卡尔曼滤波(Kalman filter)算法
- 文件集群服务器怎么搭建,一台云服务器怎么搭建集群
热门文章
- MySQL每秒57万的写入,带你装逼,带你飞 !!
- Kubernetes 最佳安全实践指南
- powershell禁用计算机,PowerShell 因为在此系统中禁止执行脚本 解决方法
- linux内核地址映射,Linux内核设备驱动地址映射笔记整理
- C#机房重构-总结(二)
- torch 归一化,momentum用法详解
- module 'paddle.fluid' has no attribute 'data'
- VS2010-2015对C++11/14/17特性的支持
- pyhon字典后跟中括号
- torch view view_as