Why do I get the Debug exception reason: Stack canary watchpoint triggered (main)?
Asked Answered
L

1

15

I'm writing a program for esp32-wroom-32 using esp-idf-v3.0.
I'm trying to add logs, which will be saved in fatfs.
After some logs I get:

21:54:21.306 -> Debug exception reason: Stack canary watchpoint triggered (main) 
21:54:21.306 -> Register dump:
21:54:21.306 -> PC      : 0x40089827  PS      : 0x00060b36  A0      : 0x40082179  A1      : 0x3ffd3860  
21:54:21.340 -> A2      : 0x3ff40000  A3      : 0x00000033  A4      : 0x00000033  A5      : 0x00000000  
21:54:21.340 -> A6      : 0x00000024  A7      : 0xff000000  A8      : 0xe37fc000  A9      : 0x0000007e  
21:54:21.340 -> A10     : 0x00000000  A11     : 0xffffffff  A12     : 0x00000004  A13     : 0x00000001  
21:54:21.340 -> A14     : 0x00000005  A15     : 0x00000000  SAR     : 0x00000004  EXCCAUSE: 0x00000001  
21:54:21.340 -> EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffff6  

Why does it happen to main?

Leicester answered 26/6, 2019 at 19:0 Comment(0)
K
28

FreeRTOS task stack depth

This is quite likely caused by a stack overflow in your FreeRTOS task.

Increase the stack depth

The first thing I would do is increase the depth of the stack for your FreeRTOS task. E.g., if you created your task with a stack size of configMINIMAL_STACK_SIZE, this might be as low as 768 bytes - which is not adequate for a lot of common requirements.

How much to increase the stack depth by?

It is not easy to answer this, but - in this case - it may be adequate to simply increase it until you no longer have stack overflows. If you are concerned about not needlessly wasting memory, FreeRTOS includes a mechanism to let you know how close a task has come to overflowing its stack.

Buffers and canaries

A canary is just a marker at the end of a buffer - which is checked periodically. If it is changed from its default value, it means that the program has attempted to write beyond the end of the buffer - i.e. there has been a buffer overflow.

Detection of stack overflow using canaries is enabled in ESP IDF by changing two options in configuration (under Component Config -> FreeRTOS section):

  • 'Check for stack overflow' -> 'using canary bytes'
  • 'Set a debug watchpoint as a stack overflow check' -> enabled

enter image description here

If you disable the second option, you will instead get a Guru Meditation error - a LoadProhibited exception - in the case of a stack overflow.

xTaskCreate() and stack depth

Bear in mind that the version of xTaskCreate() in the ESP IDF differs from that in the original FreeRTOS. In the original FreeRTOS, the stack depth is specified in words. In the ESP IDF, it's specified in bytes. A very important distinction!

Konstanz answered 27/6, 2019 at 11:35 Comment(1)
Thank you very much, I tried to change my tasks stack sizes and it works!Leicester

© 2022 - 2024 — McMap. All rights reserved.