# Question:vApplicationStackOverflowHook

**URL:** <https://forums.freertos.org/t/question-vapplicationstackoverflowhook/980>\
**Category:** Kernel\
**Created:** [October 15, 2015, 1:30am UTC](https://forums.freertos.org/t/question-vapplicationstackoverflowhook/980 "2015-10-15T01:30:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2015, 1:30am UTC](https://forums.freertos.org/t/question-vapplicationstackoverflowhook/980/1 "2015-10-15T01:30:06Z")

</div>

**[yukunduan](https://sourceforge.net/u/yukunduan/)** wrote on [Thursday, October 15, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/2e114bc2/#a832):

I met a problem about vApplicationStackOverflowHook.  
First my colleague creates a task using printf function.And I also use printf function to print the name of overflow task  
in vApplicationStackOverflowHook .  
The best solution I can think out is create a mutex at first of all.Then adding a macro like this :

```auto
	#define _USER_DEBUG_PRINTF(...)			
                                                       xSemaphoreTake( gsv_printf_semaphore, MAX_BLOCK_TIME );\
														printf( __VA_ARGS__ );\
                                                        xSemaphoreGive( gsv_printf_semaphore)

```

So anywhere i need to print some message ,I use \_USER\_DEBUG\_PRINTF() instand of printf();

Well Do you have some idear to protect printf in a new way?

Besides,if there is any way to create a protect about printf without change my colleague’s code?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2015, 3:41am UTC](https://forums.freertos.org/t/question-vapplicationstackoverflowhook/980/2 "2015-10-15T03:41:23Z")

</div>

**[yukunduan](https://sourceforge.net/u/yukunduan/)** wrote on [Thursday, October 15, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/2e114bc2/#dad2):

Well,when i change all printf function that i use in the project to \_USER\_DEBUG\_PRINTF.  
Then I find It,s still crash at the same character printed in the serial port.  
But It,s go to a configASSERT:in function vPortEnterCritical  
configASSERT( ( portNVIC\_INT\_CTRL\_REG & portVECTACTIVE\_MASK ) == 0 );

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2015, 4:07am UTC](https://forums.freertos.org/t/question-vapplicationstackoverflowhook/980/3 "2015-10-15T04:07:26Z")

</div>

**[yukunduan](https://sourceforge.net/u/yukunduan/)** wrote on [Thursday, October 15, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/2e114bc2/#d2ed):

Well if there is any possiable the the task’s stack is over flow but don’t call fuction vApplicationStackOverflowHook?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2015, 6:08am UTC](https://forums.freertos.org/t/question-vapplicationstackoverflowhook/980/4 "2015-10-15T06:08:09Z")

</div>

**[yukunduan](https://sourceforge.net/u/yukunduan/)** wrote on [Thursday, October 15, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/2e114bc2/#f987):

and what’s mean of

```auto
configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );

```

?  
and why portVECTACTIVE\_MASK if 0x1fUL?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2015, 7:09am UTC](https://forums.freertos.org/t/question-vapplicationstackoverflowhook/980/5 "2015-10-15T07:09:13Z")

</div>

**[yukunduan](https://sourceforge.net/u/yukunduan/)** wrote on [Thursday, October 15, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/2e114bc2/#d98d):

I enlarge the stack space when I create the task.The work is going successfully.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [October 15, 2015, 8:18am UTC](https://forums.freertos.org/t/question-vapplicationstackoverflowhook/980/6 "2015-10-15T08:18:29Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, October 15, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/2e114bc2/#d98d/d67f):

> Well Do you have some idear to protect printf in a new way?

If you are in the stack overflow hook, then you have a fatal error, so  
no other tasks should be running - so in that case you don’t need to  
protect the function.

If two tasks want to use the function in normal operation then you could  
use a mutex, or scheduler suspension - although scheduler suspension  
will prevent task switches for the duration of the printf call.

printf() is a common source of problems - especially in generic GCC  
libraries which will use masses of stack. If printf() causes an  
application to crash then using it again to try to diagnose the crash  
can make things worse.

> But It,s go to a configASSERT:in function vPortEnterCritical  
> configASSERT( ( portNVIC\_INT\_CTRL\_REG & portVECTACTIVE\_MASK ) == 0 );

This is telling you that you have called a non interrupt-safe API  
function from an interrupt - information that can be gleaned from the  
comment immediately above that line:

```
/* This is not the interrupt safe version of the enter critical function so
assert() if it is being called from an interrupt context. Only API
functions that end in "FromISR" can be used in an interrupt. Only assert if
the critical nesting count is 1 to protect against recursive calls if the
assert function also uses a critical section. */
```

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2015, 8:48am UTC](https://forums.freertos.org/t/question-vapplicationstackoverflowhook/980/7 "2015-10-15T08:48:41Z")

</div>

**[yukunduan](https://sourceforge.net/u/yukunduan/)** wrote on [Thursday, October 15, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/2e114bc2/#a344):

Thank you.  
First:  
Well Do you have some idear to protect printf in a new way?  
First.beacuse i’m using the HAL borad dirver provided by ST.The new versions provide uart transfer function like this HAL\_UART\_Transmit ,and there is a struct to save the uart’s state HAL\_UART\_STATE\_READY or HAL\_UART\_STATE\_BUSY\_RX etc.And I fear when overflow hook function using uart,but other function change the uart sate to BUSY.So,I add the mutex in andwhere i using printf.  
And I quite agree with your point.I will cancel the protect of printf in overflow hook function.  
Second:

```auto
    But It,s go to a configASSERT:in function vPortEnterCritical
    configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );

```

I mean why  
#define portVECTACTIVE\_MASK 0x01f?  
0x1f is 31 in decimalism.So I saw the nvic Vector table of STM\_F4 datasheet is DMA1\_Stream4.So I think if it should be 0x0e or 0x7ff?  
Third:  
Why I met two problem :  
1.error in assert ( portNVIC\_INT\_CTRL\_REG & portVECTACTIVE\_MASK ) == 0 ;  
2.error in hardfault  
But I enlarge the stack of this two tasks,The system going succesfully.  
And I define configCHECK\_FOR\_STACK\_OVERFLOW 2  
Why they didn’t run into this function vApplicationStackOverflowHook?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 15, 2015, 8:58am UTC](https://forums.freertos.org/t/question-vapplicationstackoverflowhook/980/8 "2015-10-15T08:58:36Z")

</div>

**[yukunduan](https://sourceforge.net/u/yukunduan/)** wrote on [Thursday, October 15, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/2e114bc2/#dcf0):

And how to allocate a suitable stack for my task?

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [October 15, 2015, 12:12pm UTC](https://forums.freertos.org/t/question-vapplicationstackoverflowhook/980/9 "2015-10-15T12:12:57Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Thursday, October 15, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/2e114bc2/#15cf):

To expand a bit on what RTE said, the stack overflow hook is called on a task reschedule, which (on some ports) can be either in an interrupt or not (I beleive in some it is always in interrupt context). Also, a stack HAS overflowed at this point, so the system is likely unstable, so you shouldn’t do much with normal calls, but disable interrupts and halt the system and maybe print a simple message with a simple I/O driver that uses polling (printf is likely not a good choice here, in fact I rarely find it the right choice in embedded projects).

When you create your tasks, there is a parameter to control how much space is allocated to its stack. It is a bit of an art to set this,you need to look at your task design and estimate how much it is going to need, and add a bit of a safety margin. You can then when the system is running check the slack and see if you have grossly over allocated some task (make sure you have exercised it enough to get it near worse case though) and back off.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 16, 2015, 1:23am UTC](https://forums.freertos.org/t/question-vapplicationstackoverflowhook/980/10 "2015-10-16T01:23:02Z")

</div>

**[yukunduan](https://sourceforge.net/u/yukunduan/)** wrote on [Friday, October 16, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/2e114bc2/#cd8a):

Thank you.
