Is there any API to store the task information while switching task


I’d like to know if there is any API which can store the information of the previous and the next task while switching tasks, like task priority, task name, etc.

I am facing a problem that our project crashed but we don’t know in which task the crash occurs, so I’d like to know the task switch information to locate the crash.

If there is no such API, how can I know where the crash occurs?

I am using FreeRTOS v10.4.3 on Cortex-R5 core.

Really thanks for your time!

Look at the TRACE macros that can be defined to record information.

Hello @Shawn-Bo,

I would also suggest the same thing as @richard-damon. Use trace macros which can tell you which task switched out and which task switched in. See the documentation here.


Two particular trace macros of your interest are traceTASK_SWITCHED_OUT and traceTASK_SWITCHED_IN.

Depending on what you are doing - using thread local storage may also be an option: FreeRTOS Thread Local Storage Pointers

Thanks for all your suggestions.

I declare my own C++ Task class (actually a template) that inherits from StaticTask_t. Then I added any task context variables I need to that class.

@dc42, I would be careful about that, you code is making thus making the assumption that the handle to a task is actually the address of the StaticTask_t structure, which while currently true, isn’t actually promised.

It also says that you can only make your tasks statically, and can not make a dynamic task.

My Task Class templates start with a base that holds a task handle as a member, and if the task is being made statically, the StaticTask_t and StackArea as additional member variables.

@richard-damon yes I am aware of that, and I have documented that (and similar assumptions regarding semaphore handles) in the code. I create tasks dynamically by instantiating my C++ Task objects using new, even though to FreeRTOS they are static. The reason I did it this way is so that I can use a wrapper around xTaskGetCurrentTaskHandle to get a pointer to my C++ Task object, without having to search a task list.

Except that you can’t convert a task handle to a Task Object unless it actually was created as a Task Object (so in particular, you need to be careful of the Idle and Timer tasks that will NOT have been created your way).

I suppose I have never seen a need to convert the Current Task Handle into a pointer to a wrapper, as if you just make it a base wrapper class, there isn’t anything significant you can do that you couldn’t do with the raw handle, and code that wants to know its current handle for higher purposes is likely a member function of the class, so can just use “this” to get the wrapper pointer.

In my Cortex GNAT RTS, an Ada runtime built over FreeRTOS, the Ada Task Control Block (ATCB) contains the FreeRTOS task handle, and the FreeRTOS application task tag contains the address of the ATCB.

Actually, no. I have configSUPPORT_DYNAMIC_ALLOCATION defined as 0 so that all tasks are created using memory under my control. The vApplicationGetIdleTaskMemory and vApplicationGetTimerTaskMemory functions I supply provide the address of an appropriate Task object.

I don’t think the application task tag existed when I started using FreeRTOS, or if it did then I wasn’t aware of it.