- Part 0 開(kāi)發(fā)工具安裝
- Part 1 編譯環(huán)境搭建
- Part 2 調(diào)試環(huán)境搭建
- Part 3 FreeRTOS Mulit-threads Debug
VSCode下 搭建 ARM Cortex-M 開(kāi)發(fā)環(huán)境 -- Part 3 FreeRTOS Multi-threads Debug
前言
本章旨在記錄如何在VSCode Debug環(huán)境下打開(kāi)FreeRTOS Multi-threads Debug功能,包含以下內(nèi)容:
- 修改OpenOCD config文件從而打開(kāi)OpenOCD RTOS Support功能
- 定義uxTopUsedPriority變量
- 修改LinkScript避免uxTopUsedPriority變量被優(yōu)化掉
- VSCode Debug時(shí)查看FreeRTOS下各個(gè)task狀況
修改OpenOCD config文件從而打開(kāi)OpenOCD RTOS Support功能
GDB本身是Support Multi-threads Debug的(例如info threads)。為了實(shí)現(xiàn)FreeRTOS下的Multi-threads debug,我們需要首先打開(kāi)OpenOCD的RTOS Support功能:
-
在OpenOCD Config文件-rtos FreeRTOS Option
該步要做的事情很簡(jiǎn)單:只需要在openocd config文件中增加“-rtos FreeRTOS”即可(如下代碼)
set _TARGETNAME $_CHIPNAME.cpu
target create $_TARGETNAME cortex_m -endian $_ENDIAN -dap $_CHIPNAME.dap
$_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0 -rtos FreeRTOS
set _FLASHNAME $_CHIPNAME.flash
flash bank $_FLASHNAME stm32f2x 0 0 0 0 $_TARGETNAME
定義uxTopUsedPriority變量
-
在Kernel/FreeRTOS/portable/GCC/ARM_CM4F/port.c中定義uxTopUsedPriority變量
該步要做的事情也很簡(jiǎn)單:只需要在Kernel/FreeRTOS/portable/GCC/ARM_CM4F/port.c中定義uxTopUsedPriority變量(如下代碼)
要注意的是:uxTopUsedPriority變量 一定需要用 __attribute__((used))修飾,從而避免uxTopUsedPriority變量在編譯階段就被優(yōu)化掉了
/* Each task maintains its own interrupt status in the critical nesting
variable. */
static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
__attribute__((used)) const UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1;
修改LinkScript避免uxTopUsedPriority變量被優(yōu)化掉
-
在頂層Makefile文件中增加--undefined=uxTopUsedPriority
該步要做的事情也很簡(jiǎn)單:只需要在在頂層Makefile文件中增加--undefined=uxTopUsedPriority(如下代碼)
該步的作用是避免uxTopUsedPriority變量在Link階段被優(yōu)化掉
# LDFLAGS = --specs=nano.specs -lnosys -nostartfiles
# LDFLAGS += -Wl,-wrap=malloc -Wl,-wrap=calloc -Wl,-wrap=realloc -Wl,-wrap=free
# LDFLAGS += -Wl,-T./LinkerScripts/STM32F429ZI_FLASH.ld -Wl,--gc-sections
LDFLAGS += -nostartfiles --gc-sections
LDFLAGS += --undefined=uxTopUsedPriority
VSCode Debug時(shí)查看FreeRTOS下各個(gè)task狀況
以上步驟完成后,我們?cè)侔凑誔art3的內(nèi)容Debug時(shí)就會(huì)發(fā)現(xiàn):暫停Target后,在VSCode Debug界面的調(diào)用堆棧下可以顯示該P(yáng)roject中各個(gè)Tasks的callgraph可
需要注意的時(shí),Multi-Thread debug下第一次按“開(kāi)始調(diào)試”會(huì)連上后自動(dòng)斷開(kāi)。遇到這個(gè)狀況不用擔(dān)心,只需要再按一次“開(kāi)始調(diào)試”即可。(這個(gè)現(xiàn)象是VSCode已知bug:https://github.com/Microsoft/vscode-cpptools/issues/1075)

本章總結(jié)
至此,我們?cè)赩SCode下debug Freertos multi-threads又更加方便了一點(diǎn):可以在debug的時(shí)候同時(shí)查看各個(gè)task callgraph了^_^。
整個(gè)工程我已經(jīng)Upload到Github, 歡迎大家下載!
https://github.com/TuringChenChao/STM32-VSCode