0
0
FreeRTOSprogramming~20 mins

Trace hooks and FreeRTOS+Trace - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trace Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a Trace Hook Callback
Consider a FreeRTOS trace hook function that logs task switches. What will be the output when the following trace hook is called during a context switch from TaskA to TaskB?
FreeRTOS
void traceTASK_SWITCHED_IN(void) {
    static char *lastTask = NULL;
    char *currentTask = pcTaskGetName(NULL);
    if (lastTask != currentTask) {
        printf("Switching from %s to %s\n", lastTask ? lastTask : "None", currentTask);
        lastTask = currentTask;
    }
}

// Assume TaskA runs first, then TaskB is switched in.
ASwitching from None to TaskA\nSwitching from TaskA to TaskB
BSwitching from TaskA to TaskB
CSwitching from None to TaskA
DNo output
Attempts:
2 left
💡 Hint
Remember that the static variable retains its value between calls.
🧠 Conceptual
intermediate
1:30remaining
Purpose of Trace Hooks in FreeRTOS
What is the main purpose of using trace hooks in FreeRTOS?
ATo modify the scheduler's behavior dynamically at runtime
BTo increase the priority of tasks automatically
CTo monitor and record kernel events such as task switches and interrupts
DTo allocate memory for new tasks
Attempts:
2 left
💡 Hint
Think about what trace hooks help you observe.
🔧 Debug
advanced
2:30remaining
Debugging a Trace Hook Causing System Crash
A developer added a trace hook to log task names on every context switch. After adding it, the system crashes intermittently. Which of the following is the most likely cause?
AUsing a static variable inside the trace hook
BForgetting to call vTaskStartScheduler() after adding the trace hook
CNot defining configUSE_TRACE_FACILITY as 1 in FreeRTOSConfig.h
DUsing printf inside the trace hook which is not thread-safe and may cause reentrancy issues
Attempts:
2 left
💡 Hint
Consider what functions are safe to call inside interrupt or trace hooks.
🚀 Application
advanced
3:00remaining
Using FreeRTOS+Trace to Analyze Task Behavior
You want to analyze task execution times using FreeRTOS+Trace. Which of the following steps is NOT required to correctly collect trace data?
AManually insert calls to traceTASK_SWITCHED_IN() in your application code
BCall vTraceEnable() before starting the scheduler to start trace recording
CEnable trace macros in FreeRTOSConfig.h such as configUSE_TRACE_FACILITY and configUSE_STATS_FORMATTING_FUNCTIONS
DUse the FreeRTOS+Trace recorder to export and visualize the collected data
Attempts:
2 left
💡 Hint
Think about how trace hooks are integrated with the kernel.
Predict Output
expert
3:00remaining
Output of Custom Trace Hook with Task Priorities
Given the following trace hook code that logs the priority of the task being switched in, what will be the output if Task1 (priority 3) is switched in, then Task2 (priority 5) is switched in?
FreeRTOS
void traceTASK_SWITCHED_IN(void) {
    TaskHandle_t current = xTaskGetCurrentTaskHandle();
    UBaseType_t priority = uxTaskPriorityGet(current);
    printf("Task switched in: %s with priority %u\n", pcTaskGetName(current), (unsigned int)priority);
}

// Assume Task1 runs first, then Task2 is switched in.
ATask switched in: Task2 with priority 5\nTask switched in: Task1 with priority 3
BTask switched in: Task1 with priority 3\nTask switched in: Task2 with priority 5
CTask switched in: Task1 with priority 5\nTask switched in: Task2 with priority 3
DTask switched in: Task1 with priority 3
Attempts:
2 left
💡 Hint
The hook runs every time a task is switched in, printing its name and priority.