0

If the WDT ever fires, from the ISR I save some info in EEPROM for later diagnostics before I drive the reset pin. Stack size, heap size, etc.

It might also be useful to know where the code was when the WDT fired, i.e. the ISR's return address. How do I get that?

2
  • What architrcture?
    – Majenko
    Commented Jul 8, 2021 at 20:33
  • AVR… Arduino Pro Mini atmega328p Commented Jul 8, 2021 at 23:11

1 Answer 1

5

GCC has a handy function __builtin_return_address(depth) which should do what you want. To get the return address of the current function (which should be the same whether it's a normal function or an ISR on AVR) you set the depth to 0:

void *addr = __builtin_return_address(0);

EDIT

The address returned above is a word address. Multiply by two to get a byte address as found in the disassembly from avr-objdump -SC firmware.elf >disassembled.txt

The latter is found at ~/.platformio/packages/toolchain-atmelavr/bin/avr-objdump

5
  • Well, that was easy… thanks! Next exercise is to figure out where that is in the code. Commented Jul 8, 2021 at 23:08
  • I didn't articulate that very well… next exercise is to find out where that return address is in the code (or in space) to get a clue what went wrong. Commented Jul 9, 2021 at 0:13
  • 3
    @EricNelson: Disassemble the .elf file using avr-objdump -SC. Commented Jul 9, 2021 at 7:22
  • 1
    @EdgarBonet YES!! Oh, man, does this ever take me back to the 90s. Poring over disassembled code, counting cycles, rearranging code to see if we can eke out a few more whatevers per second to win a benchmark. Commented Jul 9, 2021 at 18:26
  • "addr2line" might be a handy tool, too. Commented Aug 6, 2021 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.