How do I get the disassembled assembler code with my source code included?

I am on IDE 2.3.4 and I want to look at the assembler code of my compiled sketch with my source statements included.
I know I can use avr-objdump -S ...elf to disassemble the elf file, but I do not see any of my source statements in there.
I even copied the .ino sketch into the compile directory and also tried the -I{patchToMySketch} option, but nothing helped.

This seems to work in 2.2.1, perhaps something changed.

You can get this in WOKWI. It'll complicate matters a bit if you use libraries but the feature is available.

Press F1 to get editing menus then scroll to the bottom. Result appears with a .lst extension.

You can't disassemble something and get the original source back. But you can grab the assembler that the compiler generates.

The sketch's .ino file -- or files plural, which are all then glued together -- is translated into an .ino.cpp file, to produce an .ino.cpp.o file.

Enable Verbose compile output and do a build. You want the line after "Compiling sketch...". It might be easier to right-click in the Output window and Copy All, and find the line

$ xsel --clipboard --output | grep '\.ino\.cpp\.o$'
/home/kenb4/.arduino15/packages/esp32/tools/esp-x32/2302/bin/xtensa-esp32-elf-g++
blah blah blah
/home/kenb4/.cache/arduino/sketches/761B4D37150F0AAF8B97D39C2FAF75E0/sketch/whatever.ino.cpp
-o /home/kenb4/.cache/arduino/sketches/761B4D37150F0AAF8B97D39C2FAF75E0/sketch/whatever.ino.cpp.o 

(This uses xsel on Linux, which would be pbpaste on Mac, and good-luck-with-that on Windows.) Replace the -o part that generates the .o; instead

  • -S stop the compile after generating assember
  • -fverbose-asm include the source lines as comments

If you're lazy

$ xsel --clipboard --output | grep '\.ino\.cpp\.o$' | sed 's/-o .*$/-S -fverbose-asm/'

If that looks good, copy&paste to run it. (You can also try some shell trickery, but sometimes quoted arguments can complicate matters.) That should generate a .ino.s file in the current directory. Check if it includes your source, as comments. Different assemblers use various symbols to start a comment: #, @, ;, etc.

One other wrinkle: having tried this on a few different boards, AVR recognized, but did not seem to honor -fverbose-asm; no source comments. It worked with SAMD, R4, and ESP32. For example:

$ grep -n '^\s*#' whatever.ino.s
2:# GNU C++23 (crosstool-NG esp-12.2.0_20230208) version 12.2.0 (xtensa-esp32-elf)
3:#     compiled by GNU C version 4.9.2, GMP version 6.2.1, MPFR version 4.1.0, MPC version 1.2.1, isl version isl-0.24-GMP
5:# GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
6:# options passed: -mlongcalls -gdwarf-4 -ggdb -Os -std=gnu++23 -ffunction-sections -fdata-sections -freorder-blocks -fstack-protector -fstrict-volatile-bitfields -fno-jump-tables -fno-tree-switch-conversion -fexceptions -fno-rtti
25:# /home/kenb4/Arduino/whatever/whatever.ino:3:   Serial.begin(115200);
40:# /home/kenb4/Arduino/whatever/whatever.ino:4: }
60:# /home/kenb4/Arduino/whatever/whatever.ino:14:   int total = 0; // inline-foo
63:# /home/kenb4/Arduino/whatever/whatever.ino:16:   for (i = 0; i < n; i++)
71:# /home/kenb4/Arduino/whatever/whatever.ino:20: }
80:# /home/kenb4/Arduino/whatever/whatever.ino:17:     total += i * i;
83:# /home/kenb4/Arduino/whatever/whatever.ino:16:   for (i = 0; i < n; i++)
87:# /home/kenb4/Arduino/whatever/whatever.ino:17:     total += i * i;
108:# /home/kenb4/Arduino/whatever/whatever.ino:8:   Serial.println(test(millis()));
112:# /home/kenb4/Arduino/whatever/whatever.ino:8:   Serial.println(test(millis()));
121:# /home/kenb4/Arduino/whatever/whatever.ino:9: }

Note that it does not contain the original C-source comments.