How to measure task stack size?

nobody wrote on Wednesday, January 04, 2006:

How we can evaluate the task stack size? Is there anyway to find task stack overflow?Ways to preventing this problem?

rtel wrote on Wednesday, January 04, 2006:

Which processor are you using?

There are a couple of options.

Take a look at the function usTaskCheckFreeStackSpace() in tasks.c.  This is what the uIP demo (http://rtosdemo3.redirectme.net) uses to print out the free stack space of every task (link will probably only work from residential computers as most office firewalls will block the nonstandard port number).  Note this function disables interrupts for a while so is not a good idea other than for testing.

Secondly the TCB contains pointers to both ends of the stack, so it is easy to see if the two are getting lose.  In the task context switch function you could check for this.  I don’t do it in the download code because obviously it increases the context switch time - but it may be a trade of you like.

Regards.

nobody wrote on Wednesday, January 04, 2006:

you said:
“Secondly the TCB contains pointers to both ends of the stack, so it is easy to see if the two are getting lose. In the task context switch function you could check for this. I don’t do it in the download code because obviously it increases the context switch time - but it may be a trade of you like.”

In debug mode using AVR Jtag,How we can it what variables I should check? where I can place breakpoint?

imajeff wrote on Wednesday, January 04, 2006:

CurrentStackSpace =
pxCurrentTCB->pxTopOfStack - pxCurrentTCB->pxStack;

-or-

I often wonder how full it got, not just how full it is now. My port fills 0xA5 to initialize stack space. Most often it works to start at *pxCurrentTCB->pxStack and count all the ‘\xa5’ bytes to see how close I’ve been to overflowing. And it’s always better to have a little more than you ever see used.

nobody wrote on Thursday, January 05, 2006:

CurrentStackSpace =
pxCurrentTCB->pxTopOfStack - pxCurrentTCB->pxStack;

Assuming the stack is growing down :wink:

You can place this code immediately before the context switch.