使用-finstrument进行函数调用追踪

假设有个场景,希望在程序在执行的时候,调用函数的时候可以自动打印出它的调用栈。或者说希望自动打印出在这个函数中的执行时间。比如这段程序,希望执行到任何函数的时候,都打印出它的调用堆栈。

#include  

void foo4() {
    printf("foo()\n");
}

void foo3() {
    foo4();
}

void foo2() {
    foo3();
}

void foo1() {
    foo2();
}

int main() {
    foo1();
    return 0;
}
➜  test_test ./a.out                                             
 {  // trace begin 
   ===> 0x40125c : ./a.out(__cyg_profile_func_enter+0x38) [0x40125c]
   ===> 0x4013e2 : ./a.out(main+0x1a) [0x4013e2]
   ===> 0x7ffff7a356a3 : /lib64/libc.so.6(__libc_start_main+0xf3) [0x7ffff7a356a3]
   ===> 0x4010ae : ./a.out(_start+0x2e) [0x4010ae]
 }
 {  // trace begin 
   ===> 0x40125c : ./a.out(__cyg_profile_func_enter+0x38) [0x40125c]
   ===> 0x4013aa : ./a.out(foo1+0x15) [0x4013aa]
   ===> 0x4013ec : ./a.out(main+0x24) [0x4013ec]
   ===> 0x7ffff7a356a3 : /lib64/libc.so.6(__libc_start_main+0xf3) [0x7ffff7a356a3]
   ===> 0x4010ae : ./a.out(_start+0x2e) [0x4010ae]
 }
 {  // trace begin 
   ===> 0x40125c : ./a.out(__cyg_profile_func_enter+0x38) [0x40125c]
   ===> 0x401377 : ./a.out(foo2+0x15) [0x401377]
   ===> 0x4013b4 : ./a.out(foo1+0x1f) [0x4013b4]
   ===> 0x4013ec : ./a.out(main+0x24) [0x4013ec]
   ===> 0x7ffff7a356a3 : /lib64/libc.so.6(__libc_start_main+0xf3) [0x7ffff7a356a3]
   ===> 0x4010ae : ./a.out(_start+0x2e) [0x4010ae]
 }
 {  // trace begin 
   ===> 0x40125c : ./a.out(__cyg_profile_func_enter+0x38) [0x40125c]
   ===> 0x401344 : ./a.out(foo3+0x15) [0x401344]
   ===> 0x401381 : ./a.out(foo2+0x1f) [0x401381]
   ===> 0x4013b4 : ./a.out(foo1+0x1f) [0x4013b4]
   ===> 0x4013ec : ./a.out(main+0x24) [0x4013ec]
   ===> 0x7ffff7a356a3 : /lib64/libc.so.6(__libc_start_main+0xf3) [0x7ffff7a356a3]
   ===> 0x4010ae : ./a.out(_start+0x2e) [0x4010ae]
 }
 {  // trace begin 
   ===> 0x40125c : ./a.out(__cyg_profile_func_enter+0x38) [0x40125c]
   ===> 0x401311 : ./a.out(foo4+0x15) [0x401311]
   ===> 0x40134e : ./a.out(foo3+0x1f) [0x40134e]
   ===> 0x401381 : ./a.out(foo2+0x1f) [0x401381]
   ===> 0x4013b4 : ./a.out(foo1+0x1f) [0x4013b4]
   ===> 0x4013ec : ./a.out(main+0x24) [0x4013ec]
   ===> 0x7ffff7a356a3 : /lib64/libc.so.6(__libc_start_main+0xf3) [0x7ffff7a356a3]
   ===> 0x4010ae : ./a.out(_start+0x2e) [0x4010ae]
 }
foo()

那要如何实现呢?

最简单的方式是在每个函数里面都插入一个打印堆栈的逻辑,但是会非常麻烦。发现gcc有个特性,可以巧妙的做到这一点。利用__attribute__可以用来设置 Function-Attributes函数属性。在gcc编译的时候加上:-finstrument-functions编译选项就会在每一个用户自定义函数中添加下面两个函数调用:

void __cyg_profile_func_enter(void *this, void *callsite);
void __cyg_profile_func_exit(void *this, void *callsite);

// 这两个函数我们用户可以自己实现
// 其中`this`指针指向当前函数的地址,`callsite`是指向上一级调用函数的地址

修改下代码,实现下这个函数。

#include 
#include 
#include 
#include 

void __cyg_profile_func_exit(void* callee, void* callsite) __attribute__((no_instrument_function));
void __cyg_profile_func_enter(void* callee, void* callsite) __attribute__((no_instrument_function));

void __cyg_profile_func_enter(void* callee, void* callsite) {
    void    *funptr = callee;
    char **p = backtrace_symbols(&funptr, 1);
    printf("Entering: %s\n", *p);
    free(p);
}

void __cyg_profile_func_exit(void* callee, void* callsite) {
    void    *funptr = callee;
    char **p = backtrace_symbols(&funptr, 1);
    printf("Exiting: %s\n", *p);
    free(p);
}

void foo4() {
    printf("foo()\n");
}

int main() {
    foo4();
    return 0;
}

// gcc trace_func.c -rdynamic -finstrument-functions 
./a.out 
Entering: ./a.out(main+0) [0x401233]
Entering: ./a.out(foo4+0) [0x401200]
foo()
Exiting: ./a.out(foo4+0) [0x401200]
Exiting: ./a.out(main+0) [0x401233]

查看下编译后的汇编代码,foo函数中已经被插入了两个函数,分别是__cyg_profile_func_enter和__cyg_profile_func_exit。也就是说,编译器已经在编译的时候,替我们生成了插桩代码了。



如果编译器使用的是g++,直接用g++编译会报错。当然,也可以稍微改动下,这样就可以使用g++编译了。

extern "C" __attribute__((no_instrument_function)) 
void __cyg_profile_func_enter(void *callee, void *caller) {
        void    *funptr = callee;
    char **p = backtrace_symbols(&funptr, 1);
    printf("Entering: %s\n", *p);
    free(p);
}

extern "C" __attribute__((no_instrument_function)) 
void __cyg_profile_func_exit(void *callee, void *caller) {
    void    *funptr = callee;
    char **p = backtrace_symbols(&funptr, 1);
    printf("Exiting: %s\n", *p);
    free(p);
}
// g++ trace_func.c -o test -rdynamic -finstrument-functions 

不过,恰好我的编译器版本比较低,网上搜了一下,在g++版本较低的机器上就刚才的操作就搞不定了。于是,尝试把这个文件用gcc单独编译成一个so。其他的代码用g++编译,程序执行的时候,preload这个so库。这个代码验证下思路:

__attribute__((no_instrument_function))
void __cyg_profile_func_enter(void *this_fn, void *call_site) {
    printf("enter func => %p\n", this_fn);
}

__attribute__((no_instrument_function))
void __cyg_profile_func_exit(void *this_fn, void *call_site) {
    printf("exit func <= %p\n", this_fn);
}
.PHONY: all clean

APP_CFILE=$(wildcard *.c)

FUNC_TRACE_LIB_SO=libfunc_trace.so

all: $(FUNC_TRACE_LIB_SO)

$(FUNC_TRACE_LIB_SO) : $(APP_CFILE)
    gcc -fPIC -shared -o $(FUNC_TRACE_LIB_SO) func_trace.c

clean:
    @$(RM) *.o $(FUNC_TRACE_LIB_SO);

在其他程序需要使用的时候,就可以实现这个功能了。

 LD_PRELOAD=libfunc_trace.so ./a.out

小结:
gcc 提供了一个编译选项,-finstrument-function,编译器在编译代码的时候,可以给用户自定义的函数中插入两个函数,分别在他们进入和离开的时候进行调用。利用这个机制,可以实现很多功能。比如打印函数的调用栈或者统计函数的执行时间等。

不过,这个也可能会带来写性能问题,毕竟没发生一次函数调用都会带来一次额外的开销。需要在评估下在合理使用。

附上一个统计函数执行时间的例子

unsigned int _time_begin = 0;
unsigned int _time_end   = 0;

#define rdtsc(val) do {\
          unsigned int __a,__d; \
          __asm__ __volatile__("rdtsc" : "=a" (__a), "=d" (__d)); \
          (val) = (((unsigned long long)__d)<<32) | (__a); \
        } while(0)

__attribute__((no_instrument_function))
void __cyg_profile_func_enter(void *this_fn, void *call_site) {
    rdtsc(_time_begin);
}

__attribute__((no_instrument_function))
void __cyg_profile_func_exit(void *this_fn, void *call_site) {
    void    *funptr = call_site;
    char **p = backtrace_symbols(&funptr, 1);

    rdtsc(_time_end);
    unsigned int cost = _time_end - _time_begin;
    printf("exec func %s cost %d\n", *p, cost);
    free(p);
}

Google搞的类似的工具 :

[https://llvm.org/docs/XRayExample.html] (XRay轻量级的 C/C++ 函数调用跟踪系统)
https://zhuanlan.zhihu.com/p/565749318

你可能感兴趣的:(使用-finstrument进行函数调用追踪)