The easiest and cleanest way is to use inline assembler: then you can read the SP right away. In pure C language you would have to use some sort of dirty hack like this:
interrupt void isr (void)
{
volatile uint32 debug_sp = 0; /* volatile so that it wont be optimized away, uint32 so that it is guaranteed to end up on the stack and not in an accumulator. */
if( (uint16)&debug_sp - STACK_START > some_size)
{
do_something();
}
}
Another trick is to download the program with the BDM, then in the debugger set the whole stack area to some unlikely value like 0xAA. Let the program run for a while, halt it and check the stack, to see at which address 0xAA still exists without having been overwritten. If there is no 0xAA anywhere, the stack has overflowed.