Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ideally you write your code with static stack usage (no recursive calls). Then you can evaluate maximum stack usage by:</p> <ol> <li>static analysis (using tools)</li> <li>measurement of stack usage while running your code with complete code coverage (or as high as possible code coverage until you have a reasonable confidence you've established the extent of stack usage, as long as your rarely-run code doesn't use particularly more stack than the normal execution paths)</li> </ol> <p>But even with that, you still want to have a means of <strong>detecting</strong> and then <strong>handling</strong> stack overflow if it occurs, if at all possible, for more robustness. This can be especially helpful during the project's development phase. Some methods to <strong>detect</strong> overflow:</p> <ol> <li>If the processor supports a memory read/write interrupt (i.e. memory access breakpoint interrupt) then it can be configured to point to the furthest extent of the stack area.</li> <li>In the memory map configuration, set up a small (or large) block of RAM that is a "stack guard" area. Fill it with known values. In the embedded software, regularly (as often as reasonably possible) check the contents of this area. If it ever changes, assume a stack overflow.</li> </ol> <p>Once you've detected it, then you need to <strong>handle</strong> it. I don't know of many ways that code can gracefully recover from a stack overflow, because once it's happened, your program logic is almost certainly invalidated. So all you can do is</p> <ol> <li>log the error <ol> <li>Logging the error is very useful, because otherwise the symptoms (unexpected reboots) can be very hard to diagnose.</li> <li>Caveat: The logging routine must be able to run reliably even in a corrupted-stack scenario. The routine should be simple. I.e. with a corrupted stack, you probably can't try to write to EEPROM using your fancy EEPROM writing background task. Maybe just log the error into a struct that is reserved for this purpose, in non-init RAM, which can then be checked after reboot.</li> </ol></li> <li>Reboot (or perhaps shutdown, especially if the error reoccurs repeatedly) <ol> <li>Possible alternative: restart just the particular task, if you're using an RTOS, and your system is designed so the stack corruption is isolated, and all the other tasks are able to handle that task restarting. This would take some serious design consideration.</li> </ol></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload