一点debug技巧

怎么样打印出一个函数的stack backtrace,也就是函数的调用路径?
可以使用glibc的backtrace()/backtrace_symbols()函数,info在这里
http://www.gnu.org/software/libc/manual/html_mono/libc.html#Backtraces
这里有一段代码,可以拿去用
#include<execinfo.h>
int i;
void *bt[128];
int bt_size;
char **bt_sym;
printf("\n########## Backtrace ##########\n");
bt_size = backtrace(bt, sizeof(bt) / sizeof(void *));
printf("Number of elements in backtrace: %d\n", bt_size);
bt_sym = backtrace_symbols(bt, bt_size);

for (i = 0; i < bt_size; i++) {
printf("%s\n", bt_sym[i]);
}
free(bt_sym);
printf("########## Done ##########\n");

我参考了这个
http://sources.redhat.com/ml/crossgcc/2003-02/msg00013.html
如果不幸是C++程序可以利用c++filt来得到真正的函数原型
这里有段shell代码可以拿去用:
sed -e 's/[^(]\+(/c++filt /' -e 's/+0x.*$//g' | xargs -0 sh -c

还有一个gcc内建函数,gcc的扩展,也可能有用,不过只能得到返回地址
http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Return-Address.html#Return-Address

Comments

Popular posts from this blog

The diff between the original SEC complaint against Ripple and the amended one

Send $SGB (Songbird) to another wallet using a script

Rippled thread cpu usage