您尚未登录。

楼主 #1 2018-06-14 12:01:12

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

嵌入式Linux程序追踪调用栈的方法

#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void
myfunc3(void)
{
   int j, nptrs;
#define SIZE 100
   void *buffer[100];
   char **strings;

   nptrs = backtrace(buffer, SIZE);
   printf("backtrace() returned %d addresses\n", nptrs);

   /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
      would produce similar output to the following: */

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

   for (j = 0; j < nptrs; j++)
       printf("%s\n", strings[j]);

   free(strings);
}

static void   /* "static" means don't export the symbol... */
myfunc2(void)
{
   myfunc3();
}

void
myfunc(int ncalls)
{
   if (ncalls > 1)
       myfunc(ncalls - 1);
   else
       myfunc2();
}

int
main(int argc, char *argv[])
{
   if (argc != 2) {
       fprintf(stderr, "%s num-calls\n", argv[0]);
       exit(EXIT_FAILURE);
   }

   myfunc(atoi(argv[1]));
   exit(EXIT_SUCCESS);
}

编译指令:

arm-linux-gcc -o test test.c -g

执行:

$ ./test 10
backtrace() returned 15 addresses
./test() [0x4007d0]
./test() [0x40088d]
./test() [0x4008b4]
./test() [0x4008ad]
./test() [0x4008ad]
./test() [0x4008ad]
./test() [0x4008ad]
./test() [0x4008ad]
./test() [0x4008ad]
./test() [0x4008ad]
./test() [0x4008ad]
./test() [0x4008ad]
./test() [0x40090f]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7feddd188f45]
./test() [0x4006f9]

$ addr2line -e test 0x40088d
/home/hexing/build-test2-Qt5-Debug/test.c:37





离线

楼主 #2 2018-06-14 15:14:08

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 嵌入式Linux程序追踪调用栈的方法

朋友公司的一个嵌入式linux应用,
在客户的现场很容易出现,
但是在公司又不能重现,
按照上面的步骤,非常容易定位到问题所在.





离线

#3 2021-03-26 11:01:12

szchen2006
会员
注册时间: 2019-10-09
已发帖子: 216
积分: 166.5

Re: 嵌入式Linux程序追踪调用栈的方法

该评论内容与本帖子无关,鼓励各位坑友积极发言讨论与帖子有关的内容!

离线

  • 不通过:其他

#4 2021-03-26 11:27:11

JasonWoo
会员
注册时间: 2019-06-04
已发帖子: 84
积分: 55.5

Re: 嵌入式Linux程序追踪调用栈的方法

https://github.com/JasonOldWoo/arm-backtrace

贴上一段以前写的不依赖工具链和第三方工具arm下的backtrace实现。

离线

#5 2021-04-06 15:31:25

szchen2006
会员
注册时间: 2019-10-09
已发帖子: 216
积分: 166.5

Re: 嵌入式Linux程序追踪调用栈的方法

该评论内容与本帖子无关,鼓励各位坑友积极发言讨论与帖子有关的内容!

离线

  • 不通过:与技术无关

#6 2021-05-31 18:33:56

zjsx133
会员
注册时间: 2018-07-23
已发帖子: 63
积分: 48

Re: 嵌入式Linux程序追踪调用栈的方法

递归调用,现场程序不会写成这样啊

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn