Consider this FreeRTOS code snippet using stack overflow detection Method 1. What will be the output when a stack overflow is detected?
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
printf("Stack overflow detected in task: %s\n", pcTaskName);
for(;;); // Infinite loop to halt system
}
// Assume a task named "TaskA" causes stack overflow triggering this hook.Method 1 uses a hook function called automatically on stack overflow with task name info.
Method 1 defines vApplicationStackOverflowHook which FreeRTOS calls on stack overflow, passing the task handle and name. The example prints the task name and then halts in an infinite loop.
Given FreeRTOS configured with stack overflow detection Method 2, what is the expected behavior when a task overflows its stack?
#define configCHECK_FOR_STACK_OVERFLOW 2 void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) { printf("Overflow detected in %s\n", pcTaskName); taskDISABLE_INTERRUPTS(); for(;;); // Halt } // Assume "TaskB" causes overflow.
Method 2 checks stack overflow on each context switch and calls the hook.
Method 2 performs a check on each context switch and calls vApplicationStackOverflowHook if overflow is detected, printing the task name and halting.
Choose the best explanation for why FreeRTOS stack overflow detection Method 2 causes more CPU overhead than Method 1.
Think about when the overflow check happens in each method.
Method 2 performs stack overflow checks on every context switch by inspecting stack pointers, which adds CPU cycles each time a task switch occurs, increasing overhead compared to Method 1.
Given FreeRTOS with configCHECK_FOR_STACK_OVERFLOW = 1 but no vApplicationStackOverflowHook defined, what happens when a stack overflow occurs?
Check if the hook function is required by the FreeRTOS config.
When configCHECK_FOR_STACK_OVERFLOW is 1 or 2, FreeRTOS expects vApplicationStackOverflowHook to be defined. If missing, the linker fails with an undefined reference error.
Review this Method 1 stack overflow hook code. Why might the stack overflow not be detected correctly?
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
// Incorrectly uses pcTaskName pointer without checking
if(pcTaskName != NULL) {
printf("Overflow in task: %s\n", pcTaskName);
} else {
printf("Overflow detected but task name missing\n");
}
}
// Stack overflow occurs but pcTaskName is NULL.Consider how task names are configured and passed to the hook.
If task names are not enabled in FreeRTOS config (configUSE_TRACE_FACILITY or configMAX_TASK_NAME_LEN is zero), pcTaskName may be NULL, so the hook cannot print the name, causing confusion about detection.