get current task name

wella wrote on Friday, March 05, 2010:

Is it possible to get current task name in a user application? In the task.c is stated tskTaskControlBlock.pcTaskName is for debugging only but it is not surrounded by any #if …. #endif. So it exists all the time.
I have tried to do extern tskTCB pxCurrentTCB but the struck tskTCB is defined only in task.c and is not exported.
If possible I would like not to do some kernel hacks(modify kernel files).

Thank you for your help.
Martin

richard_damon wrote on Friday, March 05, 2010:

The variable is declared static, so it can not be accessed outside that file. if you add a
#define portREMOVE_STATIC_QUALIFIER
to FreeRTOSConfig.h then the static will be removed and it will be accessable.

wella wrote on Friday, March 05, 2010:

Thanks, but still no success.

line 191: extern tskTCB pxCurrentTCB;

…/uEZ_SRC/Source/RTOS/uEZRTOS_FreeRTOS.c:191: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘pxCurrentTCB’
…/uEZ_SRC/Source/RTOS/uEZRTOS_FreeRTOS.c:191: error: ‘pxCurrentTCB’ undeclared (first use in this function)
…/uEZ_SRC/Source/RTOS/uEZRTOS_FreeRTOS.c:191: error: (Each undeclared identifier is reported only once
…/uEZ_SRC/Source/RTOS/uEZRTOS_FreeRTOS.c:191: error: for each function it appears in.)

I do not want to copy struct tskTCB to uEZRTOS_FreeRTOS.c because the FreeRTOS can change in the future and I would have to maintain two files. Maybe a feature request?

rtel wrote on Friday, March 05, 2010:

line 191: extern tskTCB pxCurrentTCB

I’m not sure what I can add to that, other than it looks like it doesn’t know what the tskTCB type is.  Is uEZRTOS_FreeRTOS.c the FreeRTOS implementation of the uEZ RTOS abstraction?

Regards.

richard_damon wrote on Friday, March 05, 2010:

The problem is that FreeRTOS hides the definitions of its structures in the c files, for data hiding. A way to fix this and allow selected user files to be able to see the structure information, while still keeping the hiding for most user files, would be to move the structure definition to the header files, but use a preprocessor definition to control whether the structure is defined with details or opaquely. The FreeRTOS files would define this definition, and any use file that for debugging needed it could define it, but the reset get the void* typedef.

I do something like this in the version I use. To help with updating, I didn’t remove the definition from the c files, just bracketed it with #if 0 and #endif, that way when I diff the current code with the old, I can detect any changes in the data structures and move the changes over. Perhaps I should clean up how I do this and submit it to Richard as a suggested improvement.

I understand that the current method being used grew out of a need to support a number of somewhat broken C compilers (the embedded market is know for this problem, many chips with smaller market share don’t generate enough interest for the manufactures to get a fully working compiler up, so they often don’t support the full language). Technically, the technique used invokes undefined behavior, since the prototype used by the user code passes void* pointers, but the functions are declared with struct* pointers, which are not guaranteed by the language to look the same, but for practical purposes will be as long as the machine is inherently “byte addressable”, which all the target processors are.

preetpal wrote on Tuesday, March 09, 2010:

You can add a function :
void getTaskName(char* name)
{
  strcpy(name, pxCurrentTCB->taskname);
}

at tasks.c and task.h

Preet

wella wrote on Tuesday, March 09, 2010:

Thank you Preet,
a similar function exist in my project. However my FreeRTOS upgrade procedure consist of copying files if the new version is declared backward compatible. It is hard to keep track. Currently I have only one line at the end of tasks.c, #include “FreeRTOSExtensions.c” and function prototype is in the FreeRTOS.h. I think that both way are not the right solution to the future.

Martin

preetpal wrote on Tuesday, March 09, 2010:

Martin,
Try one of these (they both compiled fine on my GCC, but I didn’t try to use it):

extern struct tskTaskControlBlock* volatile pxCurrentTCB;
extern volatile void * volatile pxCurrentTCB;

Preet

tyski34 wrote on Wednesday, March 10, 2010:

I ran into this same problem while I was debugging modifications to a port and I simply added a function like the one Preet mentioned above in both the tasks.c and task.h file. Other than the #include .c file at the end of tasks.c, the only way to cleanly do this is to manually edit tasks.c, since this is where the struct is FULLY defined WITH the task name field. Any other extern struct defintion method should result in a compiler error since it won’t know where the task name field is within the structure. So, count me for a vote to either move the struct definition to a header file or add a few struct field get accessor functions to tasks.c and task.h.