CmBacktrace: An Open Source Error Tracking Library for ARM Cortex-M Microcontrollers

What is CmBacktrace Let’s break down the name: Cortex Microcontroller Backtrace, sounds impressive. In simple terms, it is an open-source error tracking library specifically designed for ARM Cortex-M microcontrollers, capable of automatically capturing fault causes in various “crash” scenarios such as assertions (assert), hard faults (HardFault), memory management faults, bus faults, and usage faults. It helps you:

  • • Automatically analyze the fault causes in the registers
  • • Extract and output the complete function call stack
  • • Obtain precise line numbers in the C source code (in conjunction with the addr2line tool) With it, you no longer need to guess the registers left and right during debugging, nor do you have to step through with the emulator F10/F11; it directly provides you with the error stack, which is easy to understand at a glance.

What Pain Points Does It Solve Many people transitioning from 51 or MSP430 to ARM often get confused by HardFault:

  • • “I clearly didn’t make a mistake, why did it crash?”
  • • Connecting the emulator to run breakpoints is time-consuming and hard to reproduce
  • • The fault register codes need to be looked up in the manual, and you have to backtrack through a lot of stacks While experienced developers may have methods for analyzing registers, they still have to manually dissect the logic each time, which is inefficient. Some products even lose the error once debugging is disconnected on-site. CmBacktrace organizes everything: during a crash, you can see the complete call chain, register status, firmware version, and other information in the serial/Flash logs, making it clear which logic has a problem.

Core Features Overview

Feature Description
Automatic Diagnosis Covers assertions, HardFault, MemManage, BusFault, and UsageFault
Call Stack Backtrace Outputs a list of PC addresses, in conjunction with addr2line to locate function names and source line numbers
Multi-Platform Support Bare-Metal, RT-Thread, UCOSII, FreeRTOS
Multi-MCU/Compiler Support Cortex-M0/M3/M4/M7, IAR/Keil/GCC
Multi-Language Output Simplified Chinese, English
Crash Log Retention Supports saving the last crash information in Flash (EasyFlash) for retrieval after reboot

Quick Start Example The following small demo demonstrates a typical division by zero fault:

#include "cm_backtrace.h"

int main(void){
// Initialization: firmware name, hardware version, software version
    cm_backtrace_init("my_firmware.bin","HW1.0","SW1.0");

    int a =100, b =0;
    if (b ==0){
        cm_backtrace_assert(cmb_get_sp());// Trigger assertion
    }
    int c = a / b;// This will trigger a hard fault
    (void) c;
    while(1);
    return 0;
}

When the program reaches<span>a/b</span>, a hard fault is thrown, and the serial port will immediately print:

[HardFault] Reason: Division by zero UsageFault
Stack: 0x08001234 0x08001100 ...
CPU Regs: LR=0xFFFFFFFF, SP=0x20002000 ...
Firmware: my_firmware.bin HW1.0 SW1.0

In conjunction with<span>addr2line -e my_firmware.bin 0x08001234</span>, you can directly locate the line where the error occurred.

Advantages and Disadvantages Analysis

Advantages Disadvantages
High degree of automation, one-click output of fault scene Initial porting requires configuration of<span>cmb_cfg.h</span>, with a slightly large number of configuration items
High precision in call stack backtrace, supports multiple operating systems FreeRTOS source code requires slight modifications
Supports Flash log saving, no loss during power outages Cortex-M0 has limited support for stack backtrace (instruction set limitations)
Wide support for languages/compilers/MCUs For beginners, care must be taken to adjust SP/LR register parameters

Conclusion and Outlook Overall, CmBacktrace adds a pair of “smart wings” to ARM debugging, whether you are a beginner or an experienced debugger, it can instantly improve efficiency. From now on, you can spend less time scratching your head and more time getting work done; hard faults are no longer something to fear. If you are also troubled by crash troubleshooting in ARM systems, you might want to give this tool a try.

Project Address: https://github.com/armink/CmBacktrace

Leave a Comment