Detecting reset reason with Optiboot bootloader

Hi all,

I want to detect and report watchdog-triggered reset in my sensors. The code is currently running on an UNO R3, and uploaded via USB and the optiboot bootloader.
Based on what I read in the forums I inserted a few lines of magic code to save the resetFlags from the bootloader before it resets them…

[Edit]: correction, I don’t really know which bootloader is installed, and it’s not that easy to find out w/o using an ISP programmer apparently.

…however the code below always prints out “Reset flag/MCUSR=72/0” no matter how I reset the board: program upload, pushing the reset button, or triggering the watchdog using an infinite loop.

What am I missing?

#include <Arduino.h>
#include <avr/wdt.h>

uint8_t _resetFlags __attribute__ ((section(".noinit")));
void resetFlagsInit(void) __attribute__ ((naked)) __attribute__ ((section (".init0")));
void resetFlagsInit(void)
  // save the reset flags passed from the bootloader
  __asm__ __volatile__ ("mov %0, r2\n" : "=r" (_resetFlags) :);

void setup()
  uint8_t _mcusr = MCUSR;
  MCUSR = 0;

  Serial.print("SETUP - SETUP - SETUP, Reset flags / MCUSR=");
  Serial.print(_resetFlags, HEX);Serial.print(" / ");Serial.println(_mcusr);

void loop()
  while (1) {}