Page 1 of 1

How to check thread memory stack in runtime?

Posted: Sat Jul 22, 2017 2:14 pm
by kreyl
I found same topic in user projects, but it seems that thread workspace has changed since 2011, and mentioned solution outputs something weird.

stm32l151c8, Chibios v 17.6.0

Here is the code I use now (it is definitely something wrong in boundaries). What is wrong?

Code: Select all

uint32_t GetThdFreeStack(void *wsp, uint32_t size) {
    uint32_t n = 0;
#if CH_DBG_FILL_THREADS
    uint8_t *startp = (uint8_t *)wsp + sizeof(thread_t);
    uint8_t *endp = (uint8_t *)wsp + size;
    while (startp < endp)
        if(*startp++ == CH_DBG_STACK_FILL_VALUE) ++n;
#endif
    return n;
}

void PrintThdFreeStack(void *wsp, uint32_t size) {
    Printf("Free stack memory: %u of %u bytes\r",
            GetThdFreeStack(wsp, size) - sizeof(thread_t),
            size - sizeof(thread_t));
}
...
static THD_WORKING_AREA(waEffectsThread, 128);
...
PrintThdFreeStack(waEffectsThread, sizeof(waEffectsThread));

Re: How to check thread memory stack in runtime?

Posted: Sat Jul 22, 2017 6:02 pm
by Giovanni
Hi,

The thread_t is no more placed at base of working area (too vulnerable place) so you have to remove that "+ sizeof(thread_t)" when calculating the base address.

Giovanni