Incorrect C operator precedence in RTOS tasks.c?
Posted: Tue Mar 06, 2018 1:47 am
In my app I want to have a command that lists all the tasks in the system with the allocated stack size, amount of stack currently used, and the maximum stack used. I observed that uxTaskGetSystemState() does not set the pxStackBase member of the TaskStatus_t array, so I added the following hack code into prvListTaskWithinSingleList() in components/freertos/tasks.c:
This should return the allocated size of the stack in the high half of the pxStackBase member and the amount of the stack currently used in the low half. However, this returns bogus values. To get the correct values I need to add parentheses to force the subtraction to happen before the assignment by OR operation:
According to the C precedence rules, the parentheses should not be required. In fact, I tried to replicate this at the app level using the hello_world example but the problem was not observed there. Also, this used to work correctly in release/v2.1. Is something funny going on with the new version of the tools?
p.s. It would be really nice to have some official way to get the stack information I want without having to hack the system this way.
Code: Select all
poison_head_t* stackblk = (poison_head_t*)(pxNextTCB->pxStack - sizeof(poison_head_t));
uint32_t stackinfo = stackblk->alloc_size << 16;
stackinfo |= pxNextTCB->pxTopOfStack - pxNextTCB->pxStack;
pxTaskStatusArray[ uxTask ].pxStackBase = (StackType_t*)stackinfo;
Code: Select all
stackinfo |= (pxNextTCB->pxTopOfStack - pxNextTCB->pxStack);
p.s. It would be really nice to have some official way to get the stack information I want without having to hack the system this way.