How to debug on Arduino mega 2560

The main documentation for the avr-stub debugging tool, which is the only real-hw debugging tool implemented right now, is at avr-stub — PlatformIO latest documentation.

PlatformIO will install everything as needed, and as instructed per the platformio.ini. Configuration of that can be seen above.

Per docs

  • Any part of your application that uses the UART module (e.g. Arduino Serial class) cannot be used in your program together with the debugger.

So you need to move your normal Serial output to another hardware serial (Serial2?) on the Mega.

As a start I’d suggest you debug just a blink sketch to verify the setup.

So a platformio.ini of

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

debug_tool = avr-stub
debug_port = SERIAL_PORT

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

Whereas you have to replace SERIAL_PORT with the actual serial port name of the Mega on your computer, e.g. COM3. (see Windows device manager).

EDIT: In my test I had to say debug_port = \\.\COM14 when using COM14 so that GDB is happy.

And as code / src\main.cpp

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

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

void loop()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}

Works on my Uno with a similiar platformio.ini

[env:uno]
platform = atmelavr
board = uno
framework = arduino
debug_tool = avr-stub
debug_port = \\.\COM14
lib_deps =
    jdolinay/avr-debugger @ ~1.1