...find out how much stack my program uses? [ARM]
Is there any way to determine how much stack my program is using?
You can determine a "high water mark" for stack usage by enabling the stack initialization option when building the C runtime startup code. This will initialize all the stacks with the value 0xCC. You can then use the following function determine the maximum amount of stack used:
unsigned long
get_stack_used(unsigned char *stack_start, unsigned char *stack_end)
{
unsigned long size;
for (size = stack_end - stack_start; size && *stack_start++ == 0xCC; size--);
return size;
}
For example, on an ARM7, ARM9 and XScale target you would add INITIALIZE_STACKS to the crt0.s file's Preprocessor Definitions project property and use the following code to determine your stack usage:
extern unsigned char __stack_start__, __stack_end__;
extern unsigned char __stack_irq_start__, __stack_irq_end__;
extern unsigned char __stack_fiq_start__, __stack_fiq_end__;
extern unsigned char __stack_svc_start__, __stack_svc_end__;
extern unsigned char __stack_und_start__, __stack_und_end__;
extern unsigned char __stack_abt_start__, __stack_abt_end__;
int
main(void)
{
unsigned long stack_used, irq_stack_used, fiq_stack_used, svc_stack_used, und_stack_used, abt_stack_used;
stack_used = get_stack_used(&__stack_start__, &__stack_end__);
irq_stack_used = get_stack_used(&__stack_irq_start__, &__stack_irq_end__);
fiq_stack_used = get_stack_used(&__stack_fiq_start__, &__stack_fiq_end__);
svc_stack_used = get_stack_used(&__stack_svc_start__, &__stack_svc_end__);
und_stack_used = get_stack_used(&__stack_und_start__, &__stack_und_end__);
abt_stack_used = get_stack_used(&__stack_abt_start__, &__stack_abt_end__);
return 0;
}
Similarly, on a Cortex-M target you would add INITIALIZE_STACK to the thumb_crt0.s file's Preprocessor Definitions project property and use the following code to determine your stack usage:
extern unsigned char __stack_start__, __stack_end__;
extern unsigned char __stack_process_start__, __stack_process_end__; int main(void) { unsigned long stack_used, process_stack_used; stack_used = get_stack_used(&__stack_start__, &__stack_end__);
process_stack_used = get_stack_used(&__stack_process_start__, &__stack_process_end__); return 0; }
-
Hi Michael,
The linker script generator adds start and end symbols for all sections described in the project's section placement file (it also adds start, end and used symbols for all segments described in the project's memory map file). You can use the symbol browser window to view your program's symbols.
Regards,
Jon
-
Hello.
Is this feature (stack initialization option) available in CrossStudio for MSP430? I'm asking because I searched for INITIALIZE_STACKS in the msp430 crossworks reference manual but found nothing. If it is available, where is the code that actually fills the stack? Is it in the crt0.asm file? I see the stack pointer initialization there but not the stack initialization code.
Thanks a lot.
Regards,
Miguel
Please sign in to leave a comment.
Comments
6 comments