Tasks backtrace switcher/viewer snippet for debugger (gcc, gdb, ARM Cortex-M3 MPU port, Eclipse support)

This solution allows switch to any task from almost any context while debugging via GNU GDB debugger (even in Eclipse IDE !).

Supported ports:
- ARM Cortex-M3
- ARM Cortex-M3 MPU
(Switching from system exceptions (first 15 vectors) IS NOT supported.)

Supported compiler: gcc

Archive contains .gdbinit script with commands and port-dependent source file (contains alternative context-switcher and debug-interface variables).

  1. add corresponding source file to your project (freertos port folder is good location for placing)
  2. add following replacement macro in FreeRTOSConfig.h:
    #define xPortPendSVHandler xPortPendSVHandler_native // any valid c identifier name

    Macro name must match PendSV vector name in startup file. Startup file should NOT #include FreeRTOSConfig.h (either directly, or indirectly through freertos headers and other headers). Example provided for CMSIS-based project.

  3. execute .gdbinit script file at start of debug session (source -v <script file>)

While debugging you can stop execution at almost any place (manually or via breakpoint hits) and use following commands in debug console:

  • freertos_show_threads

    Shows tasks table: handle(xTaskHandle) and name

  • freertos_switch_to_task handle
    Switches debugging context to specified task. Argument: task handle taken from freertos_show_threads output.

    You can switch to any task many times as desired to view their context. BUT don't step/execute anything from switched state because you will break freertos state machine and following program execution will be unpredictable (for example, task may be in blocked state waiting to receive item from a queue).

  • freertos_restore_running_context
    Restores current running context. This command will return you to initial state and you can resume execution !

Tested with:
- Yagarto GNU ARM toolchain (windows build yagarto-bu-2.23.1_gcc-4.7.2-c-c++_nl-1.20.0_gdb-7.5.1_eabi_20121222.exe), FreeRTOS V7.4.0;
- GCC ARM Embedded toolchain (windows build gnu_tools_arm_embedded_4.7 2013q1), FreeRTOS V7.4.2.
Guidelines for Eclipse (Indigo SR2, Juno SR2)
Go to debug configuration (GDB Hardware Debugging) -> Startup tab -> add following line to Initialization commands:
source -v <absolute path to .gdbinit file>
(make sure there are no trailing spaces and empty lines in textbox !)
Example: source -v C:\Users\Artem\Documents\test\.gdbinit-FreeRTOS-helpers
While debugging select gdb leaf ("arm-none-eabi-gdb.exe" in my case) in Debug view and use Console to execute commands.

Known issues:

  1. Backtrace looks broken in some situations (i.e. tasks switched out in vTaskDelay()). I described problem here:
    Question isn't answered yet. Current workaround is show_broken_backtrace command, but it's very specific for case I personally faced with. It may not work in your situation and may damage execution context (or even cause crash after you continue debugging).

  2. Sometimes (rarely) one instruction may be executed during switch.
  3. Sometimes Eclipse shows errors in Variables view. These variables are ok and may be inspected with any other method (but I don't know how to change their values, maybe via console only).

Enjoy !

UPDATED (13 june 2013):
- added ARM Cortex-M3 port (non-MPU);
- fixed critical error with integer size mismatching (might cause hard fault).

UPDATED (04 april 2013):
- added support for switching from exception context (except system exceptions);
- fixed case when interrupts globally disabled (PRIMASK set to 1 or context is within __disable_irq()/__enable_irq() section);
- fixed case when pending context switch (requested from interrupt) was lost;
- added validating of running context before trying to switch;
- added "RUNNING" label to task table;
- fixed error with nameless tasks output;
- added early checking for freertos_switch_to_task argument (before it fails in the middle of command execution);
- "freertos_restore_running_task" command renamed to "freertos_restore_running_context".




Please sign in to leave a comment.