How to debug on Arduino mega 2560

Failed to launch GDB: Error: write EPIPE

Have a blinky project with debug_init() in setup()
Compiles and uploads fine.

And my platformio.ini

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino
upload_port = COM8

debug_tool = avr-stub
debug_port = COM8

lib_deps = 
    jdolinay/avr-debugger @ ~1.1

PIO 2.2.1
VSCode 1.52.1
Win10

Have you tried replacing that with \\.\COM8 per edit above?

Yes - no change…
(20-chars)

In the debug console tab there should be more output than that – an you post it?

Could not start debugger process > Error: spawn platformio ENOENT

Could it be a %PATH% problem?

Is there no more output? There should be something like

Reading symbols from C:\Users\Max\Documents\PlatformIO\Projects\attiny\.pio\build\uno\firmware.elf...
done.
PlatformIO Unified Debugger -> http://bit.ly/pio-debug
PlatformIO: debug_tool = avr-stub
PlatformIO: Initializing remote target...
0x0000033a in micros () at C:\Users\Max\.platformio\packages\framework-arduino-avr\cores\arduino/wiring.c:103

and possible error messages.

My example still works fine for me, so I can’t reproduce the error you’re getting.

You could also try the suggestion in Cannot debug STM32: "Failed to launch GDB: Error: write EPIPE" - #4 by maxgerhardt or blatantly re-installing PlatformIO (remove all system-installed PlatformIO cores ever installed, especially if done globally via pip, remove C:\Users\<user>\.platformio\ completely, remove VSCode PlatformIO extension, then reinstall it and retry).

Also sometimes antivirus software can interfer with it, you might want to temporarily disable it.

No, there is nothing more in the debug tab…

I de-installed platformIO and removed the .platformio user folder.
Reinstalled the extension in vscode and now its broken…

Platform Manager: atmelavr @ 3.1.0 is already installed
Error: Unknown development platform ‘PackageItem <path=C:\Users\marc.platformio\platforms\atmelavr metadata=PackageMetaData <type=platform name=atmelavr version=3.1.0 spec={‘owner’: ‘platformio’, ‘id’: 8006, ‘name’: ‘atmelavr’, ‘requirements’: None, ‘url’: None}’

Remove the VSCode PlatformIO extension, then close VSCode, then remove the .platformio again fully, then reopen VSCode, then reinstall the PlatformIO extension.

Nope same problem. I even deleted C:\Users\<user>\.vscode\extensions\platformio.platformio-ide-2.2.1

I also see Unable to resolve configuration with compilerPath "C:/Users/<user>/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe". Using "cl.exe" instead.

The toolchain folder does not exist…

Reboot before reinstall fixed it. Pfew!

Debugging still not working.

In the upper left corner I can see that you’ve selected “PIO Debug (sk…”, presumably skip-predebug. Can you click on that config and make sure that you’ve select the right configuration for the right project, and also the normal variant, not the skip-predebug variant?

Yeah - I’ve tried them both - made a screen shot at the end with the one I’ve tried last.
The launch.json file is unchanged from how it was generated for the project.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "platformio-debug",
            "request": "launch",
            "name": "PIO Debug",
            "executable": "c:/Users/marc/Documents/PlatformIO/Projects/blinky/.pio/build/megaatmega2560/firmware.elf",
            "projectEnvName": "megaatmega2560",
            "toolchainBinDir": "C:/Users/marc/.platformio/packages/toolchain-atmelavr/bin",
            "preLaunchTask": {
                "type": "PlatformIO",
                "task": "Pre-Debug"
            },
            "internalConsoleOptions": "openOnSessionStart"
        },
        {
            "type": "platformio-debug",
            "request": "launch",
            "name": "PIO Debug (skip Pre-Debug)",
            "executable": "c:/Users/marc/Documents/PlatformIO/Projects/blinky/.pio/build/megaatmega2560/firmware.elf",
            "projectEnvName": "megaatmega2560",
            "toolchainBinDir": "C:/Users/marc/.platformio/packages/toolchain-atmelavr/bin",
            "internalConsoleOptions": "openOnSessionStart"
        }
    ]
}

Are you still able to build the firmware and upload normally?

Yeah - build and upload both are working (again)

And downgrading the PlatformIO extension to version 2.1.0 or 2.0.1 in VSCode isn’t working as well?

I get the same PackageItem problems when trying to install a previous version.
I don’t think this is going anywhere…
Clearly the install/uninstall logic is not quite there yet.
I guess I have to live without debugging a while longer…

Thanks for your help - appreciate it.

Okay then the previous VSCode extension is just not compatible with the current core. In previous topics it helped to downgrade that. Sadly you’ll have to do a reinstall again :sweat_smile:.

Please file an issue at Issues · platformio/platformio-vscode-ide · GitHub when using the newest extension + core version – this should work out of the box as I’ve shown, and I have no idea why that’s not working for you.

Hi,

For me, debugging the “blink” example is not working neither. I get the following in the debug console after launching a debug session:

avrdude done.  Thank you.

================= [SUCCESS] Took 3.77 seconds =========================

Reading symbols from c:\Users\...\PIO Test2\.pio\build\megaatmega2560\firmware.elf...
done.
PlatformIO Unified Debugger -> https://bit.ly/pio-debug
PlatformIO: debug_tool = avr-stub
PlatformIO: Initializing remote target...
Ignoring packet error, continuing...
warning: unrecognized item "timeout" in "qSupported" response
2
Ignoring packet error, continuing...
.pioinit:13: Error in sourced command file:
Bogus trace status reply from target: timeout

I think, I did all the config in platformio.ini correct. Uploading and executing works fine. Any idea what I could try to make it work?

Thanx,
Marco

Show the full platformio.ini and code so that we can judge that.

Hi,
Thank you for the quick reply!
Please find the project files below.

On avr-stub.html / [Debugger limitations] is written:

  • One external interrupt pin must be reserved for the debugger

What does that mean? My board is a Industrial Shields M-Duino 58+ with Arduino Mega 2560 Board / ATmega2560 MCU. It is connected through USB virtual COM-Port (COM4) with the PC. No other connections.

Project files:

platformio.ini:

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino

debug_tool = avr-stub
debug_port = COM4        ;also tried\\.\COM4

; GDB stub implementation
lib_deps =
    jdolinay/avr-debugger @ ~1.4

main.cpp:

#include "Arduino.h"
#include "avr8-stub.h"

void setup() {
  // initialize GDB stub
  debug_init();
  pinMode(LED_BUILTIN, OUTPUT);
}


void loop() {
  //breakpoint(); //also tried
  digitalWrite(LED_BUILTIN, HIGH);
  delay(100);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}

whole output from DEBUG CONSOLE:

Processing megaatmega2560 (platform: atmelavr; board: megaatmega2560; framework: arduino)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/megaatmega2560.html
PLATFORM: Atmel AVR (4.0.0) > Arduino Mega or Mega 2560 ATmega2560 (Mega 2560)
HARDWARE: ATMEGA2560 16MHz, 8KB RAM, 248KB Flash
DEBUG: Current (avr-stub) On-board (avr-stub, simavr)
PACKAGES: 
 - framework-arduino-avr @ 5.1.0 
 - tool-avrdude @ 1.60300.200527 (6.3.0) 
 - toolchain-atmelavr @ 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 6 compatible libraries
Scanning dependencies...
Dependency Graph
|-- avr-debugger @ 1.4.0
Building in debug mode
Compiling .pio\build\megaatmega2560\lib782\avr-debugger\app_api.c.o
Compiling .pio\build\megaatmega2560\lib782\avr-debugger\avr8-stub.c.o
Archiving .pio\build\megaatmega2560\lib782\libavr-debugger.a
Linking .pio\build\megaatmega2560\firmware.elf
Checking size .pio\build\megaatmega2560\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.1% (used 339 bytes from 8192 bytes)
Flash: [          ]   2.7% (used 6840 bytes from 253952 bytes)
Building .pio\build\megaatmega2560\firmware.hex
Configuring upload protocol...
AVAILABLE: wiring
CURRENT: upload_protocol = wiring
Looking for upload port...
Auto-detected: COM4
Uploading .pio\build\megaatmega2560\firmware.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file ".pio\build\megaatmega2560\firmware.hex"
avrdude: writing flash (6840 bytes):

Writing | ################################################## | 100% 1.10s

avrdude: 6840 bytes of flash written
avrdude: verifying flash memory against .pio\build\megaatmega2560\firmware.hex:
avrdude: load data flash data from input file .pio\build\megaatmega2560\firmware.hex:
avrdude: input file .pio\build\megaatmega2560\firmware.hex contains 6840 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.88s

avrdude: verifying ...
avrdude: 6840 bytes of flash verified

avrdude done.  Thank you.

========================= [SUCCESS] Took 4.01 seconds =========================
Reading symbols from c:\Users\marco\Documents\PlatformIO\Projects\PIO Test5\.pio\build\megaatmega2560\firmware.elf...
done.
PlatformIO Unified Debugger -> https://bit.ly/pio-debug
PlatformIO: debug_tool = avr-stub
PlatformIO: Initializing remote target...
Ignoring packet error, continuing...
warning: unrecognized item "timeout" in "qSupported" response
2
Ignoring packet error, continuing...
.pioinit:13: Error in sourced command file:
Bogus trace status reply from target: timeout