简体   繁体   中英

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!

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

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM