主要使用backtrace和backtrace_symbols两个函数。
- #include
- #include
- using namespace std;
//返回当前堆栈调用列表
- string print_trace(int fd, int filter_first_lines)
-
{
-
void *array[MAX_STACK_LAYERS];
-
size_t size;
-
char **strings;
-
size_t i;
-
string strRet("");
-
-
size = backtrace (array, MAX_STACK_LAYERS);
-
strings = backtrace_symbols (array, size);
-
-
for (i = 0; i < size; i++)
-
{
-
if(i < filter_first_lines)
-
continue;
-
if(fd > 0)
-
{
-
write(fd, strings[i], strlen(strings[i]));
-
write(fd, "\n", 1);
-
}
-
strRet.append(strings[i]);
-
if(i < size-1)
-
strRet.append(1, '\n');
-
}
-
if(fd > 0)
-
write(fd, "\n", 1);
-
-
free (strings);
-
return strRet;
-
}
一个示例输出结果是:
/opt/ibm/lotus801/lotus/notes/80010/linux/iwagent(_Z11print_tracei+0x5c) [0x81798e4]
/opt/ibm/lotus801/lotus/notes/80010/linux/iwagent(_ZN10MemTracker16RetriveCallStackEiRSs+0x2d) [0x8179ab5]
/opt/ibm/lotus801/lotus/notes/80010/linux/iwagent(_ZN10MemTracker9AddRecordEjPvi+0xc1) [0x817a9b7]
/opt/ibm/lotus801/lotus/notes/80010/linux/iwagent(_Znwj+0x5b) [0x817b02d]
/opt/ibm/lotus801/lotus/notes/80010/linux/iwagent(_Z19IWCreateNotesThreadP17IWNotesThreadInfo+0x5b) [0x80bfc63]
/opt/ibm/lotus801/lotus/notes/80010/linux/iwagent(_Z11IWNotesMainR14LNNotesSessioniPPc+0x525) [0x81624af]
/opt/ibm/lotus801/lotus/notes/80010/linux/iwagent(_Z6iwmainiPPc13iwmain_errors+0x83) [0x8162a01]
/opt/ibm/lotus801/lotus/notes/80010/linux/iwagent(main+0x41) [0x8162b79]
/lib/libc.so.6(__libc_start_main+0xdc) [0x2e5e9c]
/opt/ibm/lotus801/lotus/notes/80010/linux/iwagent [0x806c7c1]
相关链接:
《返璞归真--UNIX技术内幕》在Solaris下如何在程序中获得当前调用栈信息 在AIX下如何在程序中获得当前调用栈信息 在Windows下如何在程序中获得当前调用栈信息
阅读(17236) | 评论(0) | 转发(1) |