[En-Nut-Discussion] Stack Overflow Check

Harald Kipp harald.kipp at egnite.de
Mon Sep 10 10:43:06 CEST 2007


Jared Broad schrieb:
> We believe we are encountering stack overflow issues with a
> multithreaded NutOS application on an ATMega128. Is there any way to
> check this?
If you add

#define NUTDEBUG
#include <stdio.h>
#define __heap_trs stdout
static u_char __heap_trf = 1;

at the top of os/heap.c and rebuild the system, two things will happen:

1. Additional debug output is created at stdout. Make sure that stdout 
is assigned to a debug device. Using a standard UART driver will not work.
2. When releasing a block, NutHeapFree will check for possible corruption.

This is a general heap check. In order to specifically check for thread 
stack overflow, you can fill the stack with a pattern after it is 
allocated in NutThreadCreate(). This way your application can check, how 
much stack space had been used at maximum.

The problem with stack overflow on systems without memory management is, 
that it is non-deterministic. It depends on subroutine nesting level, 
local storage _and_ interrupt handling. The worst case may appear every 
second, after one year or never. I remember a paper from the TinyOS 
project, which presents a tool that analyzes the code and determines the 
deepest possible stack usage. IMHO, this is the best approach, but 
definitely not trivial to implement.

Harald

P.S. I found the TinyOS document at
http://www.cs.utah.edu/flux/papers/emsoft03/emsoft03-preprint.pdf





More information about the En-Nut-Discussion mailing list