GDB Stack unfolding in cortex M4F port

sblancodiez wrote on Thursday, September 11, 2014:

Hello there,

For a while now I have been developing a project for an Infineon XMC4200 and I had a problem with arm-none-eabi-gdb when it tried to examine the stack. The problem was that when I paused the debugging session gdb reported that it could not read an invalid address (0xa5a5a5b9). I experimented on using gdb commands to see where the problem was and I saw that gdb actually descended through the stack frame but tried to go one level further from the stack base and failed there (this happens in FreeRTOS tasks, not in the main stack).

At first I thought it was a problem with Eclipse/debugging plug-in (see https://sourceforge.net/p/gnuarmeclipse/bugs/111/ to view the problem in detail).

I continued working without being able to see the stack frames in Eclipse, waiting for a better moment to try and fix it. Today I have tried and compared the main function stack frame with the stack frame generated by FreeRTOS for the stacks and I think the problem is putting the return address to portTASK_RETURN_ADDRESS

I changed pxPortInitialiseStack:

:::C
*pxTopOfStack = portINITIAL_XPSR;	/* xPSR */
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) pxCode;	/* PC */
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;	/* LR */

for:

:::C
*pxTopOfStack = portINITIAL_XPSR;	/* xPSR */
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) pxCode;	/* PC */
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) NULL;	/* LR */

And now gdb unfolds the stack correctly (I just get another stack frame for 0x0 address).

As far as I understand, the real problem is that FreeRTOS is making the task think it has been called from portTASK_RETURN_ADDRESS but there isn’t a stack frame in the stack for that function, so it is reading garbabe that breaks stack unfolding. I am no expert on this but I am baffled at seing there is not much people with this problem. I found this bug report for the toolchain (https://answers.launchpad.net/gcc-arm-embedded/+question/247765) that is actually the problem I am describing, but I find creating veneers for tasks is not much of a solution.

Perhaps it would be good, when in debug mode or at least if portTASK_RETURN_ADDRESS != NULL, making pxPortInitialiseStack put another fake stack level to allow both stack exit catching and stack unfolding? Or am I missing something?

Best regards,

rtel wrote on Thursday, September 11, 2014:

FreeRTOS already has a facility that allows you change the return address exactly for this reason. Just add the following line to FreeRTOSConfig.h:

#define configTASK_RETURN_ADDRESS NULL

Regards.

sblancodiez wrote on Thursday, September 11, 2014:

Yes, I know that. The problem is if you want to have a task exit catcher and debug your code normally.

rtel wrote on Thursday, September 11, 2014:

I believe there are some assembler directives you can use to provide GDB with information it cannot glean itself from the source code with regards to how it interprets stack frames. In this case however everything is written in .c files, albeit with naked functions and inline assembler, so I’m not sure if (even if you can interpret how to use the directives) they would necessarily work.

If you work it out be sure to let us know.

Regards.