]> begriffs open source - freertos/blob - include/stack_macros.h
Update instruction to suppress SIGUSR1 in Posix with LLDB debugger (#1248)
[freertos] / include / stack_macros.h
1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28
29 #ifndef STACK_MACROS_H
30 #define STACK_MACROS_H
31
32 /*
33  * Call the stack overflow hook function if the stack of the task being swapped
34  * out is currently overflowed, or looks like it might have overflowed in the
35  * past.
36  *
37  * Setting configCHECK_FOR_STACK_OVERFLOW to 1 will cause the macro to check
38  * the current stack state only - comparing the current top of stack value to
39  * the stack limit.  Setting configCHECK_FOR_STACK_OVERFLOW to greater than 1
40  * will also cause the last few stack bytes to be checked to ensure the value
41  * to which the bytes were set when the task was created have not been
42  * overwritten.  Note this second test does not guarantee that an overflowed
43  * stack will always be recognised.
44  */
45
46 /*-----------------------------------------------------------*/
47
48 /*
49  * portSTACK_LIMIT_PADDING is a number of extra words to consider to be in
50  * use on the stack.
51  */
52 #ifndef portSTACK_LIMIT_PADDING
53     #define portSTACK_LIMIT_PADDING    0
54 #endif
55
56 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) )
57
58 /* Only the current stack state is to be checked. */
59     #define taskCHECK_FOR_STACK_OVERFLOW()                                                      \
60     do                                                                                          \
61     {                                                                                           \
62         StackType_t * pxCurrentTopOfStack;                                                      \
63         portGET_CURRENT_TOP_OF_STACK( pxCurrentTopOfStack );                                    \
64                                                                                                 \
65         /* Is the currently saved stack pointer within the stack limit? */                      \
66         if( pxCurrentTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING )            \
67         {                                                                                       \
68             char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                               \
69             vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
70         }                                                                                       \
71     } while( 0 )
72
73 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
74 /*-----------------------------------------------------------*/
75
76 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) )
77
78 /* Only the current stack state is to be checked. */
79     #define taskCHECK_FOR_STACK_OVERFLOW()                                                      \
80     do                                                                                          \
81     {                                                                                           \
82         StackType_t * pxCurrentTopOfStack;                                                      \
83         portGET_CURRENT_TOP_OF_STACK( pxCurrentTopOfStack );                                    \
84                                                                                                 \
85         /* Is the currently saved stack pointer within the stack limit? */                      \
86         if( pxCurrentTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING )       \
87         {                                                                                       \
88             char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                               \
89             vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
90         }                                                                                       \
91     } while( 0 )
92
93 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
94 /*-----------------------------------------------------------*/
95
96 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) )
97
98     #define taskCHECK_FOR_STACK_OVERFLOW()                                                      \
99     do                                                                                          \
100     {                                                                                           \
101         const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;                 \
102         const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U;                                 \
103         StackType_t * pxCurrentTopOfStack;                                                      \
104         portGET_CURRENT_TOP_OF_STACK( pxCurrentTopOfStack );                                    \
105                                                                                                 \
106         if( ( pxCurrentTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) ||       \
107             ( pulStack[ 0 ] != ulCheckValue ) ||                                                \
108             ( pulStack[ 1 ] != ulCheckValue ) ||                                                \
109             ( pulStack[ 2 ] != ulCheckValue ) ||                                                \
110             ( pulStack[ 3 ] != ulCheckValue ) )                                                 \
111         {                                                                                       \
112             char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                               \
113             vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
114         }                                                                                       \
115     } while( 0 )
116
117 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
118 /*-----------------------------------------------------------*/
119
120 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) )
121
122     #define taskCHECK_FOR_STACK_OVERFLOW()                                                                                                \
123     do                                                                                                                                    \
124     {                                                                                                                                     \
125         int8_t * pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack;                                                                  \
126         static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
127                                                         tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
128                                                         tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
129                                                         tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
130                                                         tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \
131         StackType_t * pxCurrentTopOfStack;                                                                                                \
132         portGET_CURRENT_TOP_OF_STACK( pxCurrentTopOfStack );                                                                              \
133                                                                                                                                           \
134         pcEndOfStack -= sizeof( ucExpectedStackBytes );                                                                                   \
135                                                                                                                                           \
136         if( ( pxCurrentTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) ||                                            \
137             ( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) )                 \
138         {                                                                                                                                 \
139             char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                                                         \
140             vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );                                           \
141         }                                                                                                                                 \
142     } while( 0 )
143
144 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
145 /*-----------------------------------------------------------*/
146
147 /* Remove stack overflow macro if not being used. */
148 #ifndef taskCHECK_FOR_STACK_OVERFLOW
149     #define taskCHECK_FOR_STACK_OVERFLOW()
150 #endif
151
152
153
154 #endif /* STACK_MACROS_H */