Debugger not starting in CLion

Hello,

On a fresh project created via the CLion, when the Debug button at the top right corner is click a error message “Debugger process finished with exit code 1. Rerun ‘PlatformIO Debug’” is shown.

What’s the platformio.ini?

The default one:

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

That can’t work. The documentation says the default debug-protocol is avr-stub, which needs more platformio.ini instructions regarding adding the library, specyfing the debug port, etc. Also, it needs special code to initialize the software GDB stub on the device. It also has limitations that need to be considered carefully.

You should also setup the AVR-stub debugger in the CMake toolchain settings (tools are in C:\Users\<user>\.platformio\packages\toolchain-atmelavr@1.70300.191015\bin for me).

Once I do that, it works without problems on real hardware.

That is with

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

and code

#include "Arduino.h"
#include "avr8-stub.h"
static int i=0;
void setup()
{
    // initialize GDB stub
    debug_init();
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
    i++;
    digitalWrite(LED_BUILTIN, HIGH);
    delay(i * 50);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
}

If you do not want to debug on real hardware, use simavr, as stated in the board’s documentation that I have linked above.

debug_tool = simavr
1 Like

I have updated the configuration as yours. The debugger starts, but there are two issues.

  1. It doesn’t stop on any breakpoint. The console’s output says that they can’t be found:
    Console output: Preparing firmware for debugging...Processing uno (platform: atmelavr; board: - Pastebin.com
    Code:

    #include “Arduino.h”
    #include “avr8-stub.h”

    void setup() {
    debug_init();
    }

    void loop() {
    int a = 5;
    char k = ‘A’;
    }

  2. The #include of “avr8-stub.h” is marked in red saying that ‘avr8-stub.h’ file not found

The default optimization level -Og prevents any code from being emitted here, hence no breakpoint can be set. You need to add a function call here, e.g. delay(a);, or use different debug_build_flags with -O0.

1 Like

It is still now working, even combined with delay …


Now it says that there is no source file …

Weird. Can’t reproduce the problem.

All works nicely.

And that is with

[env:uno]
platform = atmelavr
board = uno
framework = arduino
debug_port = \\.\COM33
debug_tool = avr-stub
lib_deps =
    jdolinay/avr-debugger @ ~1.4
debug_build_flags = -O0 -ggdb3 -g3

You should also do a Tools → PlatformIO → Re-Init so that the red squiggly lines go away (index rebuild).

1 Like
  1. The problem was in the debug_build_flags.
    That will not work: debug_build_flags = -O0
    That will work: debug_build_flags = -O0 -ggdb3 -g3

  2. Also thanks for the index rebuild hint.
    :+1:

Looks like that thread is a perfect tutorial for someone like me, who doesn’t know how to setup the debugger and can hit something in the way :slight_smile: