没有图书馆的支持

没有图书馆的支持

本文介绍了如何获得调用堆栈追踪? (深嵌,没有图书馆的支持)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我希望我的异常处理和调试功能,能够打印调用栈回溯,基本上就像在glibc的回溯()库函数。不幸的是,我的C库(Newlib)不提供这样的电话。

I want my exception handlers and debug functions to be able to print call stack backtraces, basically just like the backtrace() library function in glibc. Unfortunately, my C library (Newlib) doesn't provide such a call.

我有这样的事情:

#include <unwind.h> // GCC's internal unwinder, part of libgcc
_Unwind_Reason_Code trace_fcn(_Unwind_Context *ctx, void *d)
{
    int *depth = (int*)d;
    printf("\t#%d: program counter at %08x\n", *depth, _Unwind_GetIP(ctx));
    (*depth)++;
    return _URC_NO_REASON;
}

void print_backtrace_here()
{
    int depth = 0;
    _Unwind_Backtrace(&trace_fcn, &depth);
}

基本上工作,但所产生的痕迹是不完全的。例如,如果我这样做

which basically works but the resulting traces aren't always complete. For example, if I do

int func3() { print_backtrace_here(); return 0; }
int func2() { return func3(); }
int func1() { return func2(); }
int main()  { return func1(); }

回溯只显示FUNC3()和主要的()。 (这是OBV,一个玩具的例子,但我已经检查了拆解,并确认这些功能都在这里完全不优化,或内联。)

the backtrace only shows func3() and main(). (This is obv. a toy example, but I have checked the disassembly and confirmed that these functions are all here in full and not optimized out or inlined.)

更新:我试过这个回溯code旧ARM7的系统上,但具有相同(或至少等同越好)编译器选项和链接器脚本,它打印正确,全回溯(即func1的和FUNC2不会丢失),实际上它甚至回溯过去起来主要进入开机初始化code。所以presumably的问题是不是与链接脚本或编译器选项。 (另外,从拆卸确认没有帧指针在这个ARM7测试或者使用)。

Update: I tried this backtrace code on the old ARM7 system but with the same (or at least, as equivalent as possible) compiler options and linker script and it prints a correct, full backtrace (i.e. func1 and func2 aren't missing) and indeed it even backtraces up past main into the boot initialization code. So presumably the problem isn't with the linker script or compiler options. (Also, confirmed from disassembly that no frame pointer is used in this ARM7 test either).

在code编译时-fomit帧指针,但我的平台(裸机的ARM Cortex M3)定义了一个ABI不反正使用帧指针。 (该系统的previous版本上使用ARM7老APCS ABI强迫堆栈帧和帧指针,而像一个的,它完美地工作)。

The code is compiled with -fomit-frame-pointer, but my platform (bare metal ARM Cortex M3) defines an ABI that does not use a frame pointer anyway. (A previous version of this system used the old APCS ABI on ARM7 with forced stack frames and frame pointer, and an backtrace like the one here, which worked perfectly).

整个系统编译-fexception,这确保_Unwind使用包含在ELF文件所需的元数据。 (_Unwind是专为异常处理,我认为)。

The whole system is compiled with -fexception, which ensures the necessary metadata that _Unwind uses is included in the ELF file. (_Unwind is designed for exception handling I think).

所以,我的问题是:
是否有一个标准,公认是使用GCC的嵌入式系统可靠回溯方式?

我不介意与链接器脚本和信息crt0 $ C $如果必需C不必浪费时间,但不希望有作出该工具链本身的任何机会。

I don't mind having to mess around with the linker scripts and crt0 code if necessary, but don't want to have to make any chances to the toolchain itself.

谢谢!

推荐答案

为此,您需要 -funwind桌 -fasynchronous-unwind-表
在一些目标这样做是为了要求 _Unwind_Backtrace 正常工作!

For this you need -funwind-tables or -fasynchronous-unwind-tablesIn some targets this is required in order for _Unwind_Backtrace work properly!

这篇关于如何获得调用堆栈追踪? (深嵌,没有图书馆的支持)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:31