您的位置:首页 > 其它

使用backtrace机制帮助debug信息

2011-09-08 15:02 176 查看
线上系统如果出现了错误而且没有堆栈信息,那将是多么的恐怖!

所以在系统可能出现错误的地方我们都要尽可能的将信息描述清楚,其中最重要的信息包括堆栈调用以及堆栈变量;

堆栈调用的打印是可以借助backtrace机制实现的,下面记录一下其用法。

以下函数说明来自:http://www.gnu.org/s/libc/manual/html_node/Backtraces.html

int backtrace (void **buffer, int size)
The
backtrace
function obtains a backtrace for the current thread, as a list of pointers, and places the information into buffer.

char ** backtrace_symbols (void *const *buffer, int size)

The
backtrace_symbols
function translates the information obtained from the
backtrace
function into an array of strings. The argument buffer should be a pointer to an array of addresses obtained via the
backtrace
function, and size is the number of entries in that array (the return value of
backtrace
).

abi::__cxa_demangle:
http://www.ib.cnea.gov.ar/~oop/biblio/libstdc++/namespaceabi.html
1. backtrace可以在程序运行的任何地方被调用,返回各个调用函数的返回地址,可以限制最大调用栈返回层数。

2. 在backtrace拿到函数返回地址之后,backtrace_symbols可以将其转换为编译符号,这些符号是编译期间就确定的

3. 根据backtrace_symbols返回的编译符号,abi::__cxa_demangle可以找到具体地函数方法

根据以上三个函数我们就可以非常方便的在程序运行的任何时间点拿到调用信息从而辅助调试。

该方法还可以帮助我们调试信号相关功能:http://blogold.chinaunix.net/u/3425/showart_263408.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: