Bird
Raised Fist0
FreeRTOSprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of trace hooks in FreeRTOS?
easy
A. To reduce memory usage of the FreeRTOS kernel
B. To increase the speed of task switching
C. To monitor tasks and events as they happen in the system
D. To automatically fix bugs in the code

Solution

  1. Step 1: Understand what trace hooks do

    Trace hooks are special points in FreeRTOS code that let you watch tasks and events when they occur.
  2. Step 2: Identify the main purpose

    The main use is to monitor and record system behavior for debugging or learning.
  3. Final Answer:

    To monitor tasks and events as they happen in the system -> Option C
  4. Quick Check:

    Trace hooks = monitor tasks/events [OK]
Hint: Trace hooks watch events live in FreeRTOS [OK]
Common Mistakes:
  • Thinking trace hooks speed up tasks
  • Confusing trace hooks with memory optimization
  • Believing trace hooks fix bugs automatically
2. Which configuration option must be enabled to use trace hooks in FreeRTOS?
easy
A. configTRACE_TASK_EVENTS
B. configENABLE_TASK_SWITCH
C. configUSE_FREERTOS_TRACE
D. configUSE_TRACE_HOOKS

Solution

  1. Step 1: Identify the config option for trace hooks

    The FreeRTOS configuration option to enable trace hooks is named configUSE_TRACE_HOOKS.
  2. Step 2: Confirm other options are incorrect

    Other options like configENABLE_TASK_SWITCH or configUSE_FREERTOS_TRACE do not exist or are unrelated.
  3. Final Answer:

    configUSE_TRACE_HOOKS -> Option D
  4. Quick Check:

    Enable trace hooks = configUSE_TRACE_HOOKS [OK]
Hint: Look for configUSE_TRACE_HOOKS to enable hooks [OK]
Common Mistakes:
  • Choosing options that sound similar but don't exist
  • Confusing trace hooks with task switching configs
  • Assuming FreeRTOS+Trace needs a different config
3. Given this snippet enabling trace hooks and starting FreeRTOS+Trace recording:
vTraceEnable(TRC_START);
vTaskStartScheduler();

What is the expected result?
medium
A. Trace recording starts and scheduler runs tasks
B. Scheduler starts but trace recording is disabled
C. Trace recording starts but scheduler does not run
D. Compilation error due to wrong function call

Solution

  1. Step 1: Understand vTraceEnable(TRC_START)

    This function call starts the FreeRTOS+Trace recording of events.
  2. Step 2: Understand vTaskStartScheduler()

    This function starts the FreeRTOS scheduler to run tasks.
  3. Final Answer:

    Trace recording starts and scheduler runs tasks -> Option A
  4. Quick Check:

    vTraceEnable + vTaskStartScheduler = recording + running [OK]
Hint: vTraceEnable(TRC_START) starts recording before scheduler [OK]
Common Mistakes:
  • Thinking scheduler won't run after vTraceEnable
  • Assuming trace recording is off by default
  • Confusing function names causing compile errors
4. You added trace hooks but see no data in FreeRTOS+Trace viewer. What is a likely cause?
medium
A. configUSE_TRACE_HOOKS is not set to 1 in FreeRTOSConfig.h
B. vTraceEnable(TRC_START) was called twice
C. FreeRTOS+Trace viewer is not installed
D. Scheduler was started before enabling trace hooks

Solution

  1. Step 1: Check configUSE_TRACE_HOOKS setting

    If this is not set to 1, trace hooks do not run, so no data is recorded.
  2. Step 2: Verify other options

    Calling vTraceEnable twice or starting scheduler early usually does not prevent data. Viewer missing is unrelated to data generation.
  3. Final Answer:

    configUSE_TRACE_HOOKS is not set to 1 in FreeRTOSConfig.h -> Option A
  4. Quick Check:

    Trace hooks disabled = no data [OK]
Hint: Always enable configUSE_TRACE_HOOKS = 1 [OK]
Common Mistakes:
  • Forgetting to enable trace hooks in config
  • Blaming viewer instead of config settings
  • Assuming multiple vTraceEnable calls cause issues
5. You want to record only task switch events using FreeRTOS+Trace. Which approach is best?
hard
A. Use configUSE_TRACE_HOOKS = 0 and enable task switch trace only
B. Enable trace hooks and filter events in FreeRTOS+Trace viewer
C. Disable trace hooks and use printf in task switch hook
D. Modify FreeRTOS kernel to record only task switches

Solution

  1. Step 1: Understand trace hooks and filtering

    Trace hooks record many events; filtering in the viewer lets you see only task switches easily.
  2. Step 2: Evaluate other options

    Modifying kernel is complex and error-prone. Using printf is inefficient. Disabling trace hooks stops all tracing.
  3. Final Answer:

    Enable trace hooks and filter events in FreeRTOS+Trace viewer -> Option B
  4. Quick Check:

    Filter in viewer = easiest selective trace [OK]
Hint: Filter events in viewer instead of changing kernel [OK]
Common Mistakes:
  • Trying to modify kernel for selective tracing
  • Disabling trace hooks disables all tracing
  • Using printf slows system and is not trace hook
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard