使用 GCC 编译器的 ARM 内核的堆栈回溯(当存在 MSP 到 PSP 切换时)

2023-12-03

核心 - ARM Cortex-M4

编译器 - GCC 5.3.0 ARM EABI

操作系统 - 免费 RTOS

我正在使用 gcc 库函数 _Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn,void*); 进行堆栈回溯

在我们的项目中,MSP堆栈用于异常处理。在其他情况下,使用PSP堆栈。当我在异常处理程序内调用 _Unwind_Backtrace() 时,我能够正确回溯到异常内调用的第一个函数。在此之前堆栈是 MSP。

但在异常发生之前,我们无法回溯。此时使用的堆栈是PSP。

例如:假设

Task1
{
    func1()
}



func1
{
  func2()
}

func2
{
  an exception occurs here
}

**Inside Exception**
{
  func1ex()
}

func1ex
{
   func2ex()
}



func2ex
{
  unwind backtrace()
}

Unwind backtrace 能够回溯到 func1ex(),但无法回溯路径 task1-->func1-->func2

由于异常期间 PSP 堆栈到 MSP 堆栈之间存在切换,因此无法回溯正在使用 PSP 的函数。

在控制权到达异常处理程序之前,寄存器 R0、R1、R2、R3、LR、PC 和 XPSR 由内核堆栈到 PSP 中。我能够看到这一点。但我不知道如何使用这个堆栈帧为 PSP 进行回溯。

有人能告诉我们在这种情况下该怎么做,以便我们可以回溯到任务级别吗?

Thanks,

Ashwin.


这是可行的,但需要访问 libgcc 如何实现 _Unwind_Backtrace 函数的内部细节。幸运的是,该代码是开源的,但依赖于此类内部细节是脆弱的,因为它可能会在未来版本的 armgcc 中崩溃,而不会发出任何通知。

一般来说,通过读取 libgcc 的源代码进行回溯,它会创建 CPU 核心寄存器的内存虚拟表示,然后使用该表示沿着堆栈向上移动,模拟异常抛出。 _Unwind_Backtrace 所做的第一件事是从当前 CPU 寄存器填充此上下文,然后调用内部实现函数。

在大多数情况下,从堆栈异常结构手动创建该上下文足以伪造从处理程序模式向上通过调用堆栈的回溯。这是一些示例代码(来自https://github.com/bakerstu/openmrn/blob/62683863e8621cef35e94c9dcfe5abcaf996d7a2/src/freertos_drivers/common/cpu_profile.hxx#L162):

/// This struct definition mimics the internal structures of libgcc in
/// arm-none-eabi binary. It's not portable and might break in the future.
struct core_regs
{
    unsigned r[16];
};

/// This struct definition mimics the internal structures of libgcc in
/// arm-none-eabi binary. It's not portable and might break in the future.
typedef struct
{
    unsigned demand_save_flags;
    struct core_regs core;
} phase2_vrs;

/// We store what we know about the external context at interrupt entry in this
/// structure.
phase2_vrs main_context;
/// Saved value of the lr register at the exception entry.
unsigned saved_lr;

/// Takes registers from the core state and the saved exception context and
/// fills in the structure necessary for the LIBGCC unwinder.
void fill_phase2_vrs(volatile unsigned *fault_args)
{
    main_context.demand_save_flags = 0;
    main_context.core.r[0] = fault_args[0];
    main_context.core.r[1] = fault_args[1];
    main_context.core.r[2] = fault_args[2];
    main_context.core.r[3] = fault_args[3];
    main_context.core.r[12] = fault_args[4];
    // We add +2 here because first thing libgcc does with the lr value is
    // subtract two, presuming that lr points to after a branch
    // instruction. However, exception entry's saved PC can point to the first
    // instruction of a function and we don't want to have the backtrace end up
    // showing the previous function.
    main_context.core.r[14] = fault_args[6] + 2;
    main_context.core.r[15] = fault_args[6];
    saved_lr = fault_args[5];
    main_context.core.r[13] = (unsigned)(fault_args + 8); // stack pointer
}
extern "C"
{
    _Unwind_Reason_Code __gnu_Unwind_Backtrace(
        _Unwind_Trace_Fn trace, void *trace_argument, phase2_vrs *entry_vrs);
}

/// Static variable for trace_func.
void *last_ip;

/// Callback from the unwind backtrace function.
_Unwind_Reason_Code trace_func(struct _Unwind_Context *context, void *arg)
{
    void *ip;
    ip = (void *)_Unwind_GetIP(context);
    if (strace_len == 0)
    {
        // stacktrace[strace_len++] = ip;
        // By taking the beginning of the function for the immediate interrupt
        // we will attempt to coalesce more traces.
        // ip = (void *)_Unwind_GetRegionStart(context);
    }
    else if (last_ip == ip)
    {
        if (strace_len == 1 && saved_lr != _Unwind_GetGR(context, 14))
        {
            _Unwind_SetGR(context, 14, saved_lr);
            allocator.singleLenHack++;
            return _URC_NO_REASON;
        }
        return _URC_END_OF_STACK;
    }
    if (strace_len >= MAX_STRACE - 1)
    {
        ++allocator.limitReached;
        return _URC_END_OF_STACK;
    }
    // stacktrace[strace_len++] = ip;
    last_ip = ip;
    ip = (void *)_Unwind_GetRegionStart(context);
    stacktrace[strace_len++] = ip;
    return _URC_NO_REASON;
}

/// Called from the interrupt handler to take a CPU trace for the current
/// exception.
void take_cpu_trace()
{
    memset(stacktrace, 0, sizeof(stacktrace));
    strace_len = 0;
    last_ip = nullptr;
    phase2_vrs first_context = main_context;
    __gnu_Unwind_Backtrace(&trace_func, 0, &first_context);
    // This is a workaround for the case when the function in which we had the
    // exception trigger does not have a stack saved LR. In this case the
    // backtrace will fail after the first step. We manually append the second
    // step to have at least some idea of what's going on.
    if (strace_len == 1)
    {
        main_context.core.r[14] = saved_lr;
        main_context.core.r[15] = saved_lr;
        __gnu_Unwind_Backtrace(&trace_func, 0, &main_context);
    }
    unsigned h = hash_trace(strace_len, (unsigned *)stacktrace);
    struct trace *t = find_current_trace(h);
    if (!t)
    {
        t = add_new_trace(h);
    }
    if (t)
    {
        t->total_size += 1;
    }
}

/// Change this value to runtime disable and enable the CPU profile gathering
/// code.
bool enable_profiling = 0;

/// Helper function to declare the CPU usage tick interrupt.
/// @param irq_handler_name is the name of the interrupt to declare, for example
/// timer4a_interrupt_handler.
/// @param CLEAR_IRQ_FLAG is a c++ statement or statements in { ... } that will
/// be executed before returning from the interrupt to clear the timer IRQ flag.
#define DEFINE_CPU_PROFILE_INTERRUPT_HANDLER(irq_handler_name, CLEAR_IRQ_FLAG) \
    extern "C"                                                                 \
    {                                                                          \
        void __attribute__((__noinline__)) load_monitor_interrupt_handler(     \
            volatile unsigned *exception_args, unsigned exception_return_code) \
        {                                                                      \
            if (enable_profiling)                                              \
            {                                                                  \
                fill_phase2_vrs(exception_args);                               \
                take_cpu_trace();                                              \
            }                                                                  \
            cpuload_tick(exception_return_code & 4 ? 0 : 255);                 \
            CLEAR_IRQ_FLAG;                                                    \
        }                                                                      \
        void __attribute__((__naked__)) irq_handler_name(void)                 \
        {                                                                      \
            __asm volatile("mov  r0, %0 \n"                                    \
                           "str  r4, [r0, 4*4] \n"                             \
                           "str  r5, [r0, 5*4] \n"                             \
                           "str  r6, [r0, 6*4] \n"                             \
                           "str  r7, [r0, 7*4] \n"                             \
                           "str  r8, [r0, 8*4] \n"                             \
                           "str  r9, [r0, 9*4] \n"                             \
                           "str  r10, [r0, 10*4] \n"                           \
                           "str  r11, [r0, 11*4] \n"                           \
                           "str  r12, [r0, 12*4] \n"                           \
                           "str  r13, [r0, 13*4] \n"                           \
                           "str  r14, [r0, 14*4] \n"                           \
                           :                                                   \
                           : "r"(main_context.core.r)                          \
                           : "r0");                                            \
            __asm volatile(" tst   lr, #4               \n"                    \
                           " ite   eq                   \n"                    \
                           " mrseq r0, msp              \n"                    \
                           " mrsne r0, psp              \n"                    \
                           " mov r1, lr \n"                                    \
                           " ldr r2,  =load_monitor_interrupt_handler  \n"     \
                           " bx  r2  \n"                                       \
                           :                                                   \
                           :                                                   \
                           : "r0", "r1", "r2");                                \
        }                                                                      \
    }

此代码旨在使用计时器中断获取 CPU 配置文件,但可以从任何处理程序(包括故障处理程序)重用回溯展开。从下到上阅读代码:

  • 使用属性定义 IRQ 函数非常重要__naked__,否则GCC的函数入口头将以不可预测的方式操纵CPU的状态,例如修改堆栈指针。
  • 首先,我们保存不在异常条目结构中的所有其他核心寄存器。我们需要从一开始就从汇编中执行此操作,因为当它们用作临时寄存器时,这些通常会被后续的 C 代码修改。
  • 然后我们重建中断之前的堆栈指针;无论处理器之前处于处理程序模式还是线程模式,代码都将起作用。该指针是异常入口结构。该代码不处理非 4 字节对齐的堆栈,但我从未见过 armgcc 这样做。
  • 其余代码是C/C++,我们填充从libgcc获取的内部结构,然后调用展开过程的内部实现。我们需要做一些调整来解决 libgcc 的某些假设,这些假设在异常进入时不成立。
  • 在一种特定情况下,展开不起作用,即异常发生在叶函数中,该叶函数在进入时未将 LR 保存到堆栈中。当您尝试从进程模式进行回溯时,这种情况永远不会发生,因为被调用的回溯函数将确保调用函数不是叶子函数。我尝试通过在回溯过程中调整 LR 寄存器来应用一些解决方法,但我不相信它每次都有效。我对如何做得更好的建议很感兴趣。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 GCC 编译器的 ARM 内核的堆栈回溯(当存在 MSP 到 PSP 切换时) 的相关文章

  • 为什么隐式类型转换没有警告?

    我终于发现了程序中的一个错误 该错误是由返回类型的隐式类型转换引起的 即使g Wall对此没有任何警告 我想知道是否有什么办法可以快速找出这种无意识的错误 include
  • 使用 -static-libgcc -static-libstdc++ 编译仍然会导致对 libc.so 的动态依赖

    我正在尝试制作一个尽可能可移植的可执行文件 删除一些依赖项后 我在另一个系统上运行二进制文件时遇到以下问题 lib x86 64 linux gnu libm so 6 version GLIBC 2 15 not found requir
  • 软件预取手动指令合理的场景

    我读过有关 x86 和 x86 64 Intel 的内容gcc提供特殊的预取指令 include
  • 用更有意义的文本替换 GCC 输出中的 a-hats [重复]

    这个问题在这里已经有答案了 可能的重复 为什么 gcc 的所有错误消息中都有 https stackoverflow com questions 547071 why does gcc have a in all its error mes
  • c - 将 .data 发送到不同的部分

    我想把其中的符号 data为特定 C 文件生成的节并将它们放在不同的节中 例如 mydata 在最终的可执行文件中 例如 normaldata c char my str this should appear in data special
  • `printf()` 中格式说明符“%qd”的用途是什么?

    我看到格式说明符 qd浏览时github https github com Microsoft clang blob master test Sema format strings c代码 然后我检查了 GCC 编译器 它工作正常 incl
  • ARM 系统调用的接口是什么?它在 Linux 内核中的何处定义?

    我读过有关 Linux 中的系统调用的内容 并且到处都给出了有关 x86 架构的描述 0x80中断和SYSENTER 但我无法追踪 ARM 架构中系统调用的文件和进程 任何人都可以帮忙吗 我知道的几个相关文件是 arch arm kerne
  • 如何在编译C代码时禁用警告?

    我正在使用 32 位 Fedora 14 系统 我正在使用编译我的源代码gcc 有谁知道如何在编译c代码时禁用警告 EDIT 是的 我知道 最好的办法是修复这些警告以避免任何未定义 未知的行为 但目前在这里 我第一次编写了巨大的代码 并且在
  • 如何设置 CMake 与 clang 交叉编译 Windows 上的 ARM 嵌入式系统?

    我正在尝试生成 Ninja makefile 以使用 Clang 为 ARM Cortex A5 CPU 交叉编译 C 项目 我为 CMake 创建了一个工具链文件 但似乎存在错误或缺少一些我无法找到的东西 当使用下面的工具链文件调用 CM
  • 为什么 ld 无法从 /etc/ld.so.conf 中的路径找到库?

    我想添加 opt vertica lib64进入系统库路径 所以我执行以下步骤 1 添加 opt vertica lib64 into etc ld so conf 然后运行ldconfig 2 检查 bash ldconfig p gre
  • 'goto *foo' 其中 foo 不是指针。这是什么?

    我正在玩标签作为值 https gcc gnu org onlinedocs gcc Labels as Values html并最终得到这段代码 int foo 0 goto foo 我的 C C 经验告诉我 foo means dere
  • saber sd 如何在没有 SPL 的情况下直接从 uboot 启动

    sabre sd 基于 imx 6 最大内部 RAM 约为 150Kb 然而 uboot 足够大 可以容纳在这个空间中 在这个场景中事情是如何进行的 https community freescale com docs DOC 95015
  • gcc 中 -g 选项的作用是什么

    我看到很多关于 gdb 的教程要求在编译 c 程序时使用 g 选项 我无法理解 g 选项的实际作用 它使编译器将调试信息添加到生成的二进制文件中 此信息允许调试器将代码中的指令与源代码文件和行号相关联 拥有调试符号可以使某些类型的调试 例如
  • 为什么 GCC 交叉编译不构建“crti.o”?

    在尝试为arm构建gcc 4 x x交叉编译器时 我陷入了缺失的困境crti o文件在 BUILD DIR gcc子目录 An strace在顶层Makefile表明编译后的xgcc正在调用交联器ld with crti o 作为一个论点
  • Qemu flash 启动不起作用

    我有一本相当旧的 2009 年出版 嵌入式 ARM Linux 书 其中使用u boot and qemu 的用法qemu与u boot书中对二进制的解释如下 qemu system arm M connex pflash u boot b
  • CPU Relax 指令和 C++11 原语

    我注意到许多使用特定于操作系统的原语实现的无锁算法 例如所描述的自旋锁here http locklessinc com articles locks 使用 Linux 特定的原子原语 经常使用 cpurelax 指令 使用 GCC 可以通
  • 在 C++17 中使用 成员的链接错误

    我在 Ubuntu 16 04 上使用 gcc 7 2 并且需要使用 C 17 中的新文件系统库 尽管确实有一个名为experimental filesystem的库 但我无法使用它的任何成员 例如 当我尝试编译此文件时 include
  • Ubuntu 11.10 上的 c 数学链接器问题 [重复]

    这个问题在这里已经有答案了 我从 Ubuntu 升级后出现了一些奇怪的错误 10 11 11 04 i dont know 到 11 10 我正在得到一个undefined reference to sqrt 使用 math h 时并与 l
  • arm-linux-gnueabi 编译器选项

    我在用 ARM Linux gnueabi gcc在 Linux 中为 ARM 处理器编译 C 程序 但是 我不确定它编译的默认 ARM 模式是什么 例如 对于 C 代码 test c unsigned int main return 0x
  • 两个程序对象运行时比较的方法

    我正在进行一种特定类型的代码测试 该测试相当麻烦并且可以自动化 但我不确定最佳实践 在描述问题之前 我想澄清一下 我正在寻找合适的术语和概念 以便我可以阅读有关如何实现它的更多信息 当然 欢迎就最佳实践提出建议 但我的目标很具体 这种方法叫

随机推荐