1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "FreeRTOS.h"
16 #include "pw_assert/check.h"
17 #include "pw_string/util.h"
18 #include "task.h"
19
20 #if configCHECK_FOR_STACK_OVERFLOW != 0
21
22 /// @ingroup FreeRTOS_application_functions
23 ///
24 /// If `configCHECK_FOR_STACK_OVERFLOW` is enabled, FreeRTOS requires
25 /// applications to implement `vApplicationStackOverflowHook`, which is called
26 /// when a stack overflow is detected. This implementation invokes
27 /// @c_macro{PW_CRASH} with the task name.
vApplicationStackOverflowHook(TaskHandle_t,char * pcTaskName)28 extern "C" void vApplicationStackOverflowHook(TaskHandle_t, char* pcTaskName) {
29 // Copy the task name to a buffer in case it is corrupted.
30 static char temp_thread_name_buffer[configMAX_TASK_NAME_LEN];
31 pw::string::Copy(pcTaskName, temp_thread_name_buffer).IgnoreError();
32 PW_CRASH("Stack overflow for task %s", temp_thread_name_buffer);
33 }
34
35 #endif // configCHECK_FOR_STACK_OVERFLOW != 0
36