很多的时候,由于数组越界导致,函数栈别损坏,导致无法查找bug,gcc提供了一个编译功能, -finstrument-functions 在函数的进入跟出口出加入对 __cyg_profile_func_enter 及 __cyg_profile_func_exit 的调用。我们可以听过对这两个函数的调用,在堆中重建出一个栈,通过这个栈结构来查找问题的所在,下面是是示例代码
#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);
}
main()
{
puts("Hello World!");
return 0;
}
编译并执行:
$ gcc -finstrument-functions hello.c -o hello
$ ./hello
__cyg_profile_func_enter: func = 0x8048468, called by = 0xb7e36ebc
Hello World!
__cyg_profile_func_exit: func = 0x8048468, called by = 0xb7e36ebc