# Trace Task Context Switch

**URL:** <https://forums.freertos.org/t/trace-task-context-switch/15641>\
**Category:** Kernel\
**Tags:** debug, trace\
**Created:** [August 17, 2022, 8:37am UTC](https://forums.freertos.org/t/trace-task-context-switch/15641 "2022-08-17T08:37:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![itcoog](https://avatars.discourse-cdn.com/v4/letter/i/f1d935/32.png) [@itcoog](https://forums.freertos.org/u/itcoog)\
**Post date:** [August 17, 2022, 8:37am UTC](https://forums.freertos.org/t/trace-task-context-switch/15641/1 "2022-08-17T08:37:50Z")

</div>

I would like to know in which instruction within a switched-out Task a context switch happens,  
or retrieve the next executable instruction of the switched-in Task.  
I used the traceTaskSWITCHED\_OUT/IN macros to get the stored registers (e.g., LR, PC) at the end of the task stack (within Task stack initialization), but it is not accurate.  
What could be done to get the location of the Context Switch instruction (out or in)?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [August 17, 2022, 8:48am UTC](https://forums.freertos.org/t/trace-task-context-switch/15641/2 "2022-08-17T08:48:19Z")

</div>

It depends on the FreeRTOS port you are using. For Cortex-M4, here is the context switch code - [FreeRTOS-Kernel/port.c at main · FreeRTOS/FreeRTOS-Kernel · GitHub](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/GCC/ARM_CM4F/port.c#L439)

---

<div class="post-metadata">

**Author:** ![itcoog](https://avatars.discourse-cdn.com/v4/letter/i/f1d935/32.png) [@itcoog](https://forums.freertos.org/u/itcoog)\
**Post date:** [August 17, 2022, 9:04am UTC](https://forums.freertos.org/t/trace-task-context-switch/15641/3 "2022-08-17T09:04:35Z")

</div>

I’m running FreeRTOS on EFR32 MCU (Cortex-M4).  
Yes, I’m aware of this ISR, xPortPendSVHandler(), but even when I break in “bx LR” instruction at the end, I don’t see accurate instruction address either in LR (or other registers), nor in the stored registers at Task end-of-stack space (freeRTOS maintains 18 32-bit locations on top of stack for context-switch Registers - in the traceTaskSWITCHED\_OUT/IN macros I added code to copy the 18 locations into noinit section)

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [August 17, 2022, 10:41am UTC](https://forums.freertos.org/t/trace-task-context-switch/15641/4 "2022-08-17T10:41:09Z")

</div>

LR contains a special value known as EXC\_RETURN - [Documentation – Arm Developer](https://developer.arm.com/documentation/100701/0200/The-EXC-RETURN-register)

The registers are then popped from stack when `BX LR` is executed. This document explains exception entry and exit process - [Documentation – Arm Developer](https://developer.arm.com/documentation/dui0552/a/the-cortex-m3-processor/exception-model/exception-entry-and-return)

Thanks.
