02.01.2015 Views

CooCox CoOS User's Guide

CooCox CoOS User's Guide

CooCox CoOS User's Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.3 Stack Overflow Check<br />

Stack Overflow refers to that the size of the stack used when a task is<br />

running exceeds the size that assigned to the task, which results in writing data<br />

to the memory outside the stack. This may lead to the coverage of the system<br />

or other tasks’ data as well as the exception of memory access. The stack size<br />

assigned to each task is fixed in multi-tasking kernel. Once the stack overflow<br />

is not handled when the system is running, it may lead to system crashes.<br />

When creating a task in <strong>CooCox</strong> <strong>CoOS</strong>, the system will save the stack<br />

bottom address in the task control block and write a special value into the<br />

memory block of the stack bottom address in order to judge whether the stack<br />

overflows. <strong>CooCox</strong> <strong>CoOS</strong> will check whether there is a stack overflow during<br />

each task scheduling.<br />

Code 8 Stack overflow inspection<br />

if((pCurTcb->stkPtr < pCurTcb->stack)||(*(U32*)(pCurTcb->stack) != MAGIC_WORD))<br />

{<br />

CoStkOverflowHook(pCurTcb->taskID); /* Yes,call hander */<br />

}<br />

When stack overflow in a task, the system will call CoStkOverflowHook<br />

(taskID) automatically. You can add the handling of stack overflow in the<br />

function. The parameter of this function is the ID of the task which has stack<br />

overflow.<br />

Code 9 The handling function of stack overflow<br />

void CoStkOverflowHook(OS_TID taskID)<br />

{<br />

/* Process stack overflow in here */<br />

for(; ;)<br />

{<br />

…<br />

}<br />

}<br />

31

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!