Clion JTag debugging inside IDE not working - weird!

We are using CLion since a while also with platformio. Great !

Now we wanted to get a step further and tried to use JLink JTAG debugging with a Teensy 4.1 project.
The JTAG connection is a mod, but works !!

With CLion we have severe problems to get the gdb debugger running within the IDE. After circeling around for a while, we found that from the terminal command line of CLion the debugger and the complete rest of the configuration works without any problem !!!

This is an integration problem with CLion (we disabled also virus scanners, just because of the gdb server)

The phenomenons are multiple. Sometimes the connection is timed out. Sometimes - 1 time ! - the debugger works and then gives up.

When we tried to make a video to show all the effects, the behaviour was again different and the debugger was landing somewhere in nirwana.

In the video it can be seen that there is a yellow box coming up moaning about “pretty printers setup”, “performance optimizations” etc.etc.

regarding JTAG:

  • we had to take the newest JLink gdb server and jlink tools the delivered didn’t work for us

So, here is what we have:

The video
Be aware it doesn’t show the “normal” behaviour, but probably give some information for the developper.

The running command line debugger from a terminal:

This is the working scenario reduce due to the space here. So platform.ini and the rest is OK.

PS D:\work\Embedded\cliontest_teensy\teensyjtag> pio debug --interface=gdb -- -x .pioinit
Preparing firmware for debugging...

..... compiling cutted away

================================================================================== 1 succeeded in 00:00:02.701 ==================================================================================
SEGGER J-Link GDB Server V7.86e Command Line Version

JLinkARM.dll V7.86e (DLL compiled Mar 22 2023 16:27:41)

Command line: -singlerun -if JTAG -select USB -port 2331 -device MIMXRT1062xxx6B -speed 4000
-----GDB Server start settings-----
GDBInit file:                  none
GDB Server Listening port:     2331
SWO raw output listening port: 2332
Terminal I/O port:             2333
Accept remote connection:      localhost only
Generate logfile:              off
Verify download:               off
Init regs on start:            off
Silent mode:                   off
Single run mode:               on
Target connection timeout:     0 ms
------J-Link related settings------
J-Link Host interface:         USB
J-Link script:                 none
J-Link settings file:          none
------Target related settings------
Target device:                 MIMXRT1062xxx6B
Target device parameters:      none
Target interface:              JTAG
Target interface speed:        4000kHz
Target endian:                 little

Connecting to J-Link...
J-Link is connected.
Firmware: J-Link LPCXpresso V2 compiled Aug 23 2021 09:30:06
Hardware: V1.00
S/N: 728871513
Checking target voltage...
Target voltage: 3.30 V
Listening on TCP/IP port 2331
Connecting to target...

J-Link found 1 JTAG device, Total IRLen = 4
JTAG ID: 0x0BA02477 (Cortex-M7)
Halting core...
Connected to target
Waiting for GDB connection...Reading symbols from D:\work\Embedded\cliontest_teensy\teensyjtag\.pio\build\teensy41_debug\firmware.elf...
PlatformIO Unified Debugger -> https://bit.ly/pio-debug
PlatformIO: debug_tool = jlink
PlatformIO: Initializing remote target...
Connected to 0000:0000:0000:0000:0000:0000:0000:0001
GDB client (conn. 1000) requested target.xml from GDB Server
Reading common registers: Read register 'r0' (4 bytes) from hardware: 0x00000000
Read register 'r1' (4 bytes) from hardware: 0x00000000
Read register 'r2' (4 bytes) from hardware: 0x00000000
Read register 'r3' (4 bytes) from hardware: 0x00000000
Read register 'r4' (4 bytes) from hardware: 0x01140060
Read register 'r5' (4 bytes) from hardware: 0x703A2020
Read register 'r6' (4 bytes) from hardware: 0x90120020
Read register 'r7' (4 bytes) from hardware: 0x807F0720
Read register 'r8' (4 bytes) from hardware: 0x00032000
Read register 'r9' (4 bytes) from hardware: 0x00000000
Read register 'r10' (4 bytes) from hardware: 0x00000000
Read register 'r11' (4 bytes) from hardware: 0x00800F40
Read register 'r12' (4 bytes) from hardware: 0x00000000
Read register 'sp' (4 bytes) from hardware: 0x807F0720
Read register 'lr' (4 bytes) from hardware: 0x8B010000
Read register 'pc' (4 bytes) from hardware: 0xB2010000
Read register 'xpsr' (4 bytes) from hardware: 0x00000061
Read 4 bytes @ address 0x000001B2 (Data = 0x68BE60BE)
0x000001b2 in micros ()
    at C:\Users\tivi\.platformio\packages\framework-arduinoteensy\cores\teensy4\delay.c:77
77      {
Received monitor command: clrbp
Received monitor command: speed auto
SSelect auto target interface speed (4000 kHz)
Relect auto target interface speed (4000 kHz)
eceived monitor command: reset
Resetting target
Resetting target
Received monitor command: halt
Halting target CPU...
...Target halted (PC = 0x60001400)
Downloading 5120 bytes @ address 0x60000000
Downloading 3564 bytes @ address 0x60001400
Downloading 268 bytes @ address 0x600021EC
Downloading 11616 bytes @ address 0x600022F8
Downloading 4 bytes @ address 0x60005058
Downloading 3776 bytes @ address 0x6000505C
Downloading 3300 bytes @ address 0x60005F1C
Writing register 'pc' = 0x60001000
Loading section .text.headers, size 0x1400 lma 0x60000000
Loading section .text.code, size 0xdec lma 0x60001400
Loading section .text.progmem, size 0x10c lma 0x600021ec
Loading section .text.itcm, size 0x2d60 lma 0x600022f8
Loading section .fini, size 0x4 lma 0x60005058
Loading section .data, size 0xec0 lma 0x6000505c
Loading section .text.csf, size 0xce4 lma 0x60005f1c
Start address 0x60001000, load size 27648
Transfer rate: 13500 KB/sec, 3949 bytes/write.
Reading 64 bytes @ address 0x000003C0
Temporary breakpoint 1 at 0x3cc: file C:\Users\tivi\.platformio\packages\framework-arduinoteensy\cores\teensy4\main.cpp, line 51.
Read 2 bytes @ address 0x000003CC (Data = 0xF7FF)
PlatformIO: Initialization completed
(gdb) PlatformIO: Resume the execution to `debug_init_break = tbreak main`
PlatformIO: More configuration options -> https://bit.ly/pio-debug
Continuing.
Setting breakpoint @ address 0x000003CC, Kind = 2, Type = THUMB, BPHandle = 0x0001
Starting target CPU...
...Breakpoint reached @ address 0x000003CC
Reading common registers: Read register 'r0' (4 bytes) from hardware: 0xC00E0020
Read register 'r1' (4 bytes) from hardware: 0x00000000
Read register 'r2' (4 bytes) from hardware: 0x01000000
Read register 'r3' (4 bytes) from hardware: 0x00000000
Read register 'r4' (4 bytes) from hardware: 0x01140060
Read register 'r5' (4 bytes) from hardware: 0x703A2020
Read register 'r6' (4 bytes) from hardware: 0xFADFCF67
Read register 'r7' (4 bytes) from hardware: 0xE87F0720
Read register 'r8' (4 bytes) from hardware: 0x00032000
Read register 'r9' (4 bytes) from hardware: 0x00000000
Read register 'r10' (4 bytes) from hardware: 0x00000000
Read register 'r11' (4 bytes) from hardware: 0x00800F40
Read register 'r12' (4 bytes) from hardware: 0x00000000
Read register 'sp' (4 bytes) from hardware: 0xE87F0720
Read register 'lr' (4 bytes) from hardware: 0xB1150060
Read register 'pc' (4 bytes) from hardware: 0xCC030000
Read register 'xpsr' (4 bytes) from hardware: 0x00000061
Removing breakpoint @ address 0x000003CC, Size = 2

Temporary breakpoint 1, main () at C:\Users\tivi\.platformio\packages\framework-arduinoteensy\cores\teensy4\main.cpp:51
51              setup();
Read 4 bytes @ address 0x000003CC (Data = 0xFE5EF7FF)
(gdb) 

---> from here the gdb worked as it should be. repeatedly, reliable, with breakpoints and all

--------------------------------------------------------------------------------------------------------------------
The faulty console from within the CLion debugger console
--------------------------------------------------------------------------------------------------------------------

copy from the console windows of clion not working. It cancels with "command time out" and the gdb is saying "connecting":



Preparing firmware for debugging...
Processing teensy41_debug (platform: teensy; board: teensy41; framework: arduino)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/teensy/teensy41.html
PLATFORM: Teensy (4.18.0) > Teensy 4.1
HARDWARE: IMXRT1062 600MHz, 512KB RAM, 7.75MB Flash
DEBUG: Current (jlink) External (jlink)
PACKAGES: 
 - framework-arduinoteensy @ 1.158.0 (1.58) 
 - tool-teensy @ 1.158.0 (1.58) 
 - toolchain-gccarmnoneeabi-teensy @ 1.110301.0 (11.3.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 92 compatible libraries
Scanning dependencies...
No dependencies
Building in debug mode
Linking .pio\build\teensy41_debug\firmware.elf
Calculating size .pio\build\teensy41_debug\firmware.elf
Checking size .pio\build\teensy41_debug\firmware.elf
Building .pio\build\teensy41_debug\firmware.hex
teensy_size: Memory Usage on Teensy 4.1:
teensy_size:   FLASH: code:15180, data:4044, headers:8420   free for files:8098820
teensy_size:    RAM1: variables:4832, code:11616, padding:21152   free for local variables:486688
teensy_size:    RAM2: variables:12416  free for malloc/new:511872
========================= [SUCCESS] Took 2.71 seconds =========================

Environment     Status    Duration
--------------  --------  ------------
teensy41_debug  SUCCESS   00:00:02.706
========================= 1 succeeded in 00:00:02.706 =========================
SJCJFS/N: 728871513
CL
J-Link found 1 JTAG device, Total IRLen = 4
JTAG ID: 0x0BA02477 (Cortex-M7)
HCReading symbols from D:\work\Embedded\cliontest_teensy\teensyjtag\.pio\build\teensy41_debug\firmware.elf...
CGRRead register 'sp' (4 bytes) from hardware: 0xC07F0720
Read register 'lr' (4 bytes) from hardware: 0x21210000RRRRRRStarget interface speed (4000 kResetting target
Received monitor command: halt
HDDDDDDDWPlatformIO Unified Debugger -> https://bit.ly/pio-debug
PlatformIO: debug_tool = jlink
PlatformIO: Initializing remote target...
usb_serial_class::available (this=0x20203a70) at C:\Users\tivi\.platformio\packages\framework-arduinoteensy\cores\teensy4/usb_serial.h:98
98	        virtual int available() { return usb_serial_available(); }
RRRead 4 bytes @ address 0x60001000 (Data = 0x432000D1)
Read 2 bytes @ address 0x60001000 (Data = 0x00D1)
PlatformIO: Initialization completed
SSPlatformIO: Resume the execution to `debug_init_break = tbreak main`
PlatformIO: More configuration options -> https://bit.ly/pio-debug
.RRead register 'r5' (4 bytes) from hardware: 0x70R
Temporary breakpoint 1, main () at C:\Users\tivi\.platformio\packages\framework-arduinoteensy\cores\teensy4\main.cpp:51
51		setup();
Command timed out

platform.ini

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:teensy41]
platform = teensy
board = teensy41
framework = arduino
upload_protocol = jlink-jtag

[env:teensy41_debug]
platform = teensy
board = teensy41
framework = arduino

debug_build_flags =   -v   -O0 -g3 -gdwarf-4
debug_tool = jlink
debug_port = :2331
; JTAG interface
build_type = debug

debug_server =
    C:\JLink\JLinkGDBServerCL.exe
;    ${platformio.packages_dir}/tool-jlink/JLinkGDBServer
    -singlerun
    -if
    JTAG
    -select
    USB
    -port
    2331
    -device
    MIMXRT1062xxx6B
    -speed
    4000

upload_protocol = jlink-jtag

The tiny test program

#include <Arduino.h>

// just to be sure
//
#pragma GCC optimize ("O0")

/* LED Blink, Teensyduino Tutorial #1
   http://www.pjrc.com/teensy/tutorial.html

   This example code is in the public domain.
*/

// just a function for debug test
void justForCompilerTest(int i) ;

volatile const int ledPin = 13;

// the setup() method runs once, when the sketch starts

void setup() {

    pinMode(ledPin, OUTPUT);

}

// some variable protected against optimization
//
volatile int i=0;
volatile double ft = 0.0 ;
void loop() {

    i++;
    ft = 2 * i ;
    volatile static int n =ft;

    digitalWrite(ledPin, HIGH);   // set the LED on
    delay(200);                  // wait for a second
    digitalWrite(ledPin, LOW);    // set the LED off
    delay(100);                  // wait for a second

    justForCompilerTest(i) ;
}

void justForCompilerTest(int i)
{

    delay(100) ;
}

I found an intermediate solution within CLion itself.

CLion supports the Segger JTAG gdbserver on its own by a configuration wizzard.

So, I defined it like this

The monitor reset is mandatory. I tried first the arm-none-eabi-gdb.exe from the teensy toolchain.
It works - :thinking: - but gives these error lables coming up and seems not to support python. So the toolchain gdb is good for the command line but not capable enough for the GUI.
After introducing the reset also the bundled gdb multiarch from CLion works - and this is as I wanted it.

But - sadly - it works not out of the box with platformio.

pio or the arm-none-eabi-gdb seems to use a protocol that is not 100% working. There are these malformed lines in the debugger console like (see the last thread with the complete console log)

RRRead

or

I found also that within the CMakeListPrivate.txt no CLION_SVD_FILE_PATH was available as promised by the Documentation for CLion. So no SVD file, no registers.
I found a MIMXRT1062.svd, loaded it and this works fine as documented.

Now the debugging works like expected. Excellent. But the platformio support has definitely some gaps.

If you need testing I would provide a remote maintenance session to support you. You have probably no modded Teensy with a JTAG Segger at hand. I think more poeple will start to use this with Teensy 4.1 or other MCUs.

Ah I forgot the version. It is not the last one:
CLion 2023.1.1
Build #CL-231.8109.222, built on April 6, 2023