[SOFT] [C] AVRGCC: Monitoring Stack Usage

34 posts / 0 new
Last post
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi folks,

This is my first ever posting here - I hope it is useful!

A common problem is wanting to know how much stack space a program uses. This can reveal how much valuable RAM is can be used for static variables, or whether the stack is growing too large and corrupting program data.

With AVR gcc, two symbols are defined by the linker that can make this easy. These are _end and __stack which define the first free byte of SRAM after program variables, and the starting address of the stack, respectively.

The stack starts at __stack, which is conventionally the highest byte of SRAM, and grows towards zero; _end will be somewhere between zero and __stack. If the stack ever falls below _end, it has almost certainly corrupted program data.

The following C declarations gain access to these linker symbols:

extern uint8_t _end;
extern uint8_t __stack;

Taking the address of these symbols (e.g. &_end) gives the memory addresses that bound the stack. The following function therefore 'paints' the stack with a known value:

void StackPaint(void) __attribute__ ((naked)) __attribute__ ((section (".init1")));

void StackPaint(void)
{
#if 0
    uint8_t *p = &_end;

    while(p <= &__stack)
    {
        *p = STACK_CANARY;
        p++;
    }
#else
    __asm volatile ("    ldi r30,lo8(_end)\n"
                    "    ldi r31,hi8(_end)\n"
                    "    ldi r24,lo8(0xc5)\n" /* STACK_CANARY = 0xc5 */
                    "    ldi r25,hi8(__stack)\n"
                    "    rjmp .cmp\n"
                    ".loop:\n"
                    "    st Z+,r24\n"
                    ".cmp:\n"
                    "    cpi r30,lo8(__stack)\n"
                    "    cpc r31,r25\n"
                    "    brlo .loop\n"
                    "    breq .loop"::);
#endif
}

This is declared in such a way that AVR-libc will execute the assembly before the program has started running or configured the stack. It also runs at a point before some of the normal runtime setup, hence assembly should be used as C maynot be fully reliable (this is discussed in the AVR libc manual).

The function itself simply fills the stack with 0xc5, the idea being that stack usage will overwrite this with some other value, hence making stack usage detectable.

Finally the following function can be used to count how many bytes of stack have not been overwritten:

uint16_t StackCount(void)
{
    const uint8_t *p = &_end;
    uint16_t       c = 0;

    while(*p == STACK_CANARY && p <= &__stack)
    {
        p++;
        c++;
    }

    return c;
}

This function can be called at any time to check how much stack space has never been over written. If it returns 0, you are probably in trouble as all the stack has been used, most likely destroying some program variables.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nice idea and clean implementation. Thanks for posting.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the kind comment :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the great post.
I was interested in monitoring the stack size of a subset of functions in my application.
Is there anyway I can paint the stack only up to the current stack pointer?
So that I can paint it right before calling the function I want to monitor and then doing the count after calling the function.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

great idea, saved a copy in case I ever need it! :)

(hopefully you put it in the avrfreaks projects section too)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The code may be really usefull as long no use is made from Malloc

pre malloc free stack: 3469 bytes
post malloc free stack: 0 bytes

printf_P(PSTR("pre malloc free stack: %u bytes\r\n"), StackCount());
	//
	// Allocate memory for the volume
	//
	Volume = malloc(sizeof(s_VOLUME));
	if (Volume != NULL)
	{
		printf_P(PSTR("post malloc free stack: %u bytes\r\n"), StackCount());

MY MICROCONTROLLER CAN BEAT THE HELL OUT OF YOUR MICROCONTROLLER /ATMEL

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quite interesting!
But is it correct, that _end and __stack are uint8_t?
The bigger controllers have more than 256 bytes internal RAM.

Michael

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But he's not using what they contain (as they don't "contain" anything) but the addresses where they are pointing.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sweet! I will definately use this when trying to optimize. Every little bit helps. I mean I guess I could program asm style and be super efficient, but this could at least show me any gross C memory mismanagement. Thanks bro!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the post. Very useful!

-Brad

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MaxK wrote:
The code may be really usefull as long no use is made from Malloc

pre malloc free stack: 3469 bytes
post malloc free stack: 0 bytes

Yup - that is a short coming of the code. As illustrated in the AVR libc manual, the malloc arena starts at __bss_end: http://www.nongnu.org/avr-libc/user-manual/malloc.html

If you really need malloc, you could use the malloc tunables to setup the stack painting to avoid the malloc arena - probably not too difficult to do.

Regards,

Mike

Pages