This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Detect stack overflow

Other Parts Discussed in Thread: MSP430F5310

My application on MSP430F5310 showed weird behavior at the customer (stopped running after a few minutes). After quite some fiddling around, I suppose that the problem was due to insufficient stack size. The application makes heavy use of the stack, so the default setting might have been too small. Now the setting is at 1.5 kB stack, so far everything looks fine.

Does someone have a suggestion how to detect a stack overrun at runtime? Cyclically checking the stack pointer? Setting and checking a bit pattern just below the stack? Any other wonderful idea I did not come across?

Max

  • To easily check stack-use add the following lines to your program, immediately at the begin of main and just after stopping the watchdog. Let the program run a while and stop it, check the stack contents with the memory-browser, if all A5 are gone you can be sure you have a stack overflow and needs to increase stack-size.

    	extern unsigned int _stack;
    	extern unsigned int __STACK_END;
    	unsigned int* p;
    
    	p = &_stack;
    	while (p < (&__STACK_END -3))
    	{
    		*p = 0xA5A5;
    		p++;
    	}
    

  • Thank you for pointing out this approach, I get the idea. Do I understand correctly that the stack goes from _stack to __STACK_END (I am using CCS)? 

    Max

  • That’s correct. You can verify this in the “<configuration>\<project>.map” output file.

  • I would like to suggest that initialize your stack memory with predefined memory pattern before starting the application. By pattern, I mean is some value which you are not gonna use in your code. Then have a routine to check stack start and stack end are filled with predefined pattern and there is no overflow or underflow. Also have a routine to check for stack usage and make sure you are not exceeding the stack limit. Hope this helps.

**Attention** This is a public forum