为什么"A stack overflow in task iot_thread has been detected"会源源不断的来?

why "A stack overflow in task iot_thread has been detected" is coming continuously?

我正在做一个 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: vApplicationWhosebugHook 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: vApplicationWhosebugHook 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

It simply means that the iot_thread ask stack size is not sufficient. [...] Is this the only reason why this error comes or can there be another reason for this?

要么是你不够用,要么是你的栈用量过多(由于递归错误,或者实例化大对象或数组的实例化。无论哪种方式,原因都是一样的。无论是由于栈不足还是栈用量过多,都是一个问题)设计意图的问题。

If yes then where should I increase the stack size of the iot_thread task?

线程的堆栈在任务创建函数中分配。对于将是 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() ulStackDepthpuxStackBuffer().

 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 (with plenty of documentation and tutorial content at https://www.freertos.org/RTOS.html 中有大量文档。阅读文档确实是无可替代的。