Junky
Junky

Reputation: 21

How do I dump my stack in Arduino?

I'm looking for a way to dump the stack of my arduino. I know there is a stack pointer (SP) available, what I try at the moment is:

char* stack = (char*)SP;
int counter = 0;
strncpy(c, &stack[counter], 1);
while(counter < 200)
{
  counter++;
  strncat(c, &stack[counter], 1);
}
Serial.print(c);   

I don't get anything like a stack so I don't know if I'm doing it right. Please help!

Upvotes: 1

Views: 1110

Answers (2)

Armin J.
Armin J.

Reputation: 455

Call addresses on the stack are LSB first, so LSB has the higher stack address. And they are shifted right 1 bit.

uint8_t * tStackPtr = (uint8_t *) SP;
// We have 19 pushs and pops for this function so add 19+1
uint16_t tPC = *(tStackPtr + 20);
tPC <<= 8;
tPC |= *(tStackPtr + 21);
tPC <<= 1;
Serial.print(F("PC=0x"));
Serial.println(tPC, HEX);

Upvotes: 0

Casper
Casper

Reputation: 37

uint8_t stackArray[30];
void createStackDump() 
{
  volatile uint8_t* mSP = (uint8_t*)SP;

  for (int i = 0; i < 30; i++) 
  {
    stackArray[i] = *mSP;
    mSP++;
  }
}

Upvotes: 1

Related Questions