加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 在Mingw打印堆栈跟踪

发布时间:2020-12-16 07:02:38 所属栏目:百科 来源:网络整理
导读:我需要修改现有的C app并在某个地方打印stacktrace.我怎样才能做到这一点? 我无法编译此源代码: #ifndef _GNU_SOURCE#define _GNU_SOURCE#endif#ifndef __USE_GNU#define __USE_GNU#endif#include execinfo.h#include signal.h#include stdio.h#include st
我需要修改现有的C app并在某个地方打印stacktrace.我怎样才能做到这一点?

我无法编译此源代码:

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef __USE_GNU
#define __USE_GNU
#endif

#include <execinfo.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ucontext.h>
#include <unistd.h>


int main(int argc,char ** argv)
{
 void *             array[50];
 void *             caller_address;
 char **            messages;

 int size = backtrace(array,50);

 /* overwrite sigaction with caller's address */
 array[1] = caller_address;

 messages = backtrace_symbols(array,size);

 /* skip first stack frame (points here) */
 for (int i = 1; i < size && messages != NULL; ++i)
 {
  fprintf(stderr,"[bt]: (%d) %sn",i,messages[i]);
 }

 free(messages);


}

因为它缺少一些符号:

$i586-mingw32msvc-gcc -rdynamic ./trace.cpp -I/usr/include/
i586-mingw32msvc-gcc: unrecognized option '-rdynamic'
./trace.cpp:39:2: warning: no newline at end of file
/tmp/cc6hCJtU.o:trace.cpp:(.text+0x26): undefined reference to `_backtrace'
/tmp/cc6hCJtU.o:trace.cpp:(.text+0x47): undefined reference to `_backtrace_symbols'
/tmp/cc6hCJtU.o:trace.cpp:(.text+0x67): undefined reference to `_stderr'

我没有设法提交正确的.a文件.

以下答案可以在stackoverflow上找到,几乎无法使用:

> libunwind在交叉编译期间导致编译错误:error:ucontext.h:没有这样的文件或目录
> http://www.codeproject.com/Articles/11132/Walking-the-callstack无法在MinGW中编译
> http://www.mr-edd.co.uk/code/dbg也不编译
> http://www.mr-edd.co.uk/code/stack_trace也没有编译并且有错误

有没有可行的解决方案?非常感谢提前.

解决方法

您的MinGW工具链不支持-rdynamic标志,无法使用backtrace()或backtrace_symbols()编译您的应用程序

不确定你使用的MinGW是什么,但我建议从这里获得一个新的:http://mingw-w64.sourceforge.net/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读