linux 获取函数堆栈

2015-01-14
#include
#include
#include
#include
#include
#include

using namespace std;


char** CallStack(int& num)
{
   int nptrs;
   #define SIZE 100
   void *buffer[100];
   char **strings;
   nptrs = backtrace(buffer, SIZE);
   printf("backtrace() returned %d addressesn", nptrs);

   strings = backtrace_symbols(buffer, nptrs);
   num  = nptrs;
   if (strings == NULL) {
       perror("backtrace_symbols");
       exit(EXIT_FAILURE);
   }

/*   for (j = 0; j < nptrs; j++)
   {
       printf("%sn", strings[j]);
       strcpy(prt[j], strings[j]);
   }

   free(strings);*/

   return strings;
}

void test1()
{
    try
    {
        throw(5);
    }
    catch(...)
    {
        int num = 0;
        char* *ptr  = CallStack(num);
        for (int j = 0; j < num; j++)
        {
           printf("%sn", ptr[j]);
        }
        free(ptr);
    }
}


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

上面打印出来的内容中,没有函数名,只有堆栈信息。加入-rdynamic 就可以输出函数名称了。

类别:编程相关 | 阅读:2173 | 评论:0 | 标签:linux c

想收藏或者和大家分享这篇好文章→

“linux 获取函数堆栈 ”共有0条留言

发表评论

姓名:

邮箱:

网址:

验证码:

公告

2014年10月4日创建

标签云

最近评论

友情链接