我正在开发一个 aws/amazon-freertos 项目。在那里我发现了一些不寻常的错误“已检测到任务 iot_thread 中的堆栈溢出”。
我多次遇到此错误,但我设法通过更改代码来删除它。
我只是想知道这个错误实际上意味着什么?
据我所知,这仅仅意味着 iot_thread 询问堆栈大小不够。所以它已经溢出了。
这是出现此错误的唯一原因还是还有其他原因?
如果是,那么我应该在哪里增加 iot_thread 任务的堆栈大小?
完整日志:
***ERROR*** A stack overflow in task iot_thread has been detected.
abort() was called at PC 0x4008cf94 on core 0
0x4008cf94: vApplicationStackOverflowHook at /home/horsemann/Desktop/WorkSpace/TestingRepo/vendors/espressif/esp-idf/components/esp32/panic.c:122
ELF file SHA256: 5252c38b96a3325472de5cf0f1bbc2cae90ffda1a169e71823c5aebbaa4fce2d
Backtrace: 0x4008cdac:0x3ffd74c0 0x4008cf7d:0x3ffd74e0 0x4008cf94:0x3ffd7500 0x40093e35:0x3ffd7520 0x400953d4:0x3ffd7540 0x4009538a:0x3ffd7560 0x401390e5:0x3ffc81bc
0x4008cdac: invoke_abort at /home/horsemann/Desktop/WorkSpace/TestingRepo/vendors/espressif/esp-idf/components/esp32/panic.c:136
0x4008cf7d: abort at /home/horsemann/Desktop/WorkSpace/TestingRepo/vendors/espressif/esp-idf/components/esp32/panic.c:171
0x4008cf94: vApplicationStackOverflowHook at /home/horsemann/Desktop/WorkSpace/TestingRepo/vendors/espressif/esp-idf/components/esp32/panic.c:122
0x40093e35: vTaskSwitchContext at /home/horsemann/Desktop/WorkSpace/TestingRepo/freertos_kernel/tasks.c:5068
0x400953d4: _frxt_dispatch at /home/horsemann/Desktop/WorkSpace/TestingRepo/freertos_kernel/portable/ThirdParty/GCC/Xtensa_ESP32/portasm.S:406
0x4009538a: _frxt_int_exit at /home/horsemann/Desktop/WorkSpace/TestingRepo/freertos_kernel/portable/ThirdParty/GCC/Xtensa_ESP32/portasm.S:206
这仅仅意味着iot_thread询问堆栈大小不够。 [...] 这是出现此错误的唯一原因还是还有其他原因?
要么是不足,要么是你的堆栈使用过多(由于递归错误或实例化大对象或数组的实例化。无论哪种原因,原因都是一样的。是由于堆栈不足还是堆栈使用过多,这是一个设计问题意图。
如果是,那么我应该在哪里增加 iot_thread 任务的堆栈大小?
xTaskCreate()
调用 usStackDepth
参数:
BaseType_t xTaskCreate( TaskFunction_t pvTaskCode,
const char * const pcName,
configSTACK_DEPTH_TYPE usStackDepth, // <<<<<<<<<<
void *pvParameters,
UBaseType_t uxPriority,
TaskHandle_t *pxCreatedTask
);
xTaskCreateStatic()
ulStackDepth
和puxStackBuffer()
。
TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
const char * const pcName,
const uint32_t ulStackDepth, //<<<<<<
void * const pvParameters,
UBaseType_t uxPriority,
StackType_t * const puxStackBuffer, //<<<<<<<
StaticTask_t * const pxTaskBuffer );
xTaskCreateRestrictedStatic()
但这更复杂 - 如果您正在这样做,请阅读文档。
任务堆栈的适当分配是任何 RTOS 应用程序设计的基础。您需要阅读文档并理解概念。
FreeRTOS API 在 https://www.freertos.org/a00106.html 上有大量文档(此外,在 https://www.freertos.org/RTOS.html 上还有大量文档和教程内容。还有)确实无法替代阅读文档。