Buggy debugger?

I am using a J-Link Edu Mini to debug a Teensy 4.1 running a fairly large open source program. I am using platformio Teensy 4.17.0 platform on VSCode. It is too large to upload here so this is a link the Github repo. AOG-AiO-RVC-100hz/RVC_100hz_mixed at Keya · chriskinal/AOG-AiO-RVC-100hz · GitHub

If I set a breakpoint while the CPU is running the debugger will halt at the breakpoint. However, the debugger command buttons no longer work with the exception of STOP. If I try to restart debugging, I get an error message like the ones below. If I close the prject and then VSCode there is still a zombie gdb debug server running that I have to manually kill.

If I halt the CPU and insert the breakpoint it functions normally. However, If I dont stop the CPU when removing the breakpoint it will act a previously described. I have tried Segger Ozone and it works fine adding and removing breakpoints while the CPOU is running.

My platformio.ini is in the above linked repo. The standard debug_tool = jlink and upload_protocol = jlink-jtag do not work. I have custom settings in platformio.ini.

pio_reset_run_target

Warning! Undefined pio_reset_run_target command

Cannot execute this command while the target is running.

Use the “interrupt” command to stop the target

and then try again.

Cannot execute this command while the target is running. Use the “interrupt” command to stop the target and then try again.

Cannot execute this command while the target is running.

Use the “interrupt” command to stop the target

and then try again.

GDB closed TCP/IP connection (Socket 12)

undefinedLoop <_UnixSelectorEventLoop running=False closed=True debug=False> that handles pid 98681 is closed

Well you set the debug_tool to be custom

but it pretty much looks like you just want debug_tool = jlink. Because with that, you get the correct definition of debug_init_cmds.

The standard upload_protocol = jlink-jtag doesn’t work. It gives this error:

J-Link Command File read successfully.
Processing script file…
J-Link>h
J-Link connection not established yet but required for command.
Connecting to J-Link via USB…O.K.
Firmware: J-Link EDU Mini V1 compiled May 28 2024 15:38:10
Hardware version: V1.00
J-Link uptime (since boot): 0d 06h 12m 16s
S/N: 801049715
License(s): FlashBP, GDB
USB speed mode: Full speed (12 MBit/s)
VTref=3.291V
Target connection not established yet but required for command.
Device position in JTAG chain (IRPre,DRPre) : -1,-1 => Auto-detect

The custom upload_protocol in my platformio.ini does work.

The standard debug_tool = jlink doesn’t work It gives this error:

SEGGER J-Link GDB Server V7.88k Command Line Version

JLinkARM.dll V7.88k (DLL compiled Jul 5 2023 14:54:56)

Command line: -singlerun -if SWD -select USB -device MIMXRT1062xxxxA -port 2331
-----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: yes
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: MIMXRT1062xxxxA
Target device parameters: none
Target interface: SWD
Target interface speed: 4000kHz
Target endian: little

Connecting to J-Link…
J-Link is connected.
Failed to get index for device name ‘MIMXRT1062xxxxA’.GDBServer will be closed…
Shutting down…
undefinedCould not connect to target.
Please check power, connection and settings.
Reading symbols from /Users/chris/Code/AOG-AiO-RVC-100hz/RVC_100hz_mixed/.pio/build/teensy41/firmware.elf…
done.
PlatformIO Unified Debugger → Redirecting...
PlatformIO: debug_tool = jlink
PlatformIO: Initializing remote target…
.pioinit:16: Error in sourced command file:
:2331: Operation timed out.

It is the same issue as the Black Magic probe we worked on a few days ago. Platformio has hard coded SWD into their scripts for some reason.

When I put those commands into my platformio.ini so it looks like this:

[env:teensy41]
;build_type = release
platform = teensy
board = teensy41
framework = arduino

upload_protocol = custom
upload_flags =
-device
MIMXRT1062xxx6A
-speed
4000
-if
jtag
-jtagconf
-1,-1
-autoconnect
1
-NoGui
1
-CommanderScript
“upload.jlink”
upload_command = ${platformio.packages_dir}/tool-jlink/JLinkExe $UPLOAD_FLAGS

debug_tool = custom
debug_server =
${platformio.packages_dir}/tool-jlink/JLinkGDBServerCLExe
-singlerun
-if
jtag
-select
USB
-device
MIMXRT1062xxx6A
-port
2331
-speed
4000
debug_port = localhost:2331
debug_init_cmds =
define pio_reset_halt_target
monitor reset
monitor halt
end
define pio_reset_run_target
monitor clrbp
monitor reset
monitor go
end
target extended-remote $DEBUG_PORT
monitor clrbp
monitor speed auto
pio_reset_halt_target
$LOAD_CMDS
$INIT_BREAK

Debug does the compile and upload but the program starts in a halted state. The pause / run button is showing pause. Pressing it does not start the CPU. The step over, step into and step out buttons are greyed. The stop button does work.

With that it doesn’t complain about undefined pio_reset_halt_target function anymore though?

The CPU is halted with that configuration. The Pause, Restart and Stop buttons are available like the debugger thinks the CPU is running but it is not. Clicking the Pause or Restart buttons do nothing. Only the Stop button causes the debugger frontend to act like it killed the GDB process. However, I can see in process monitor it is still running. Only way to get the system back in sync is to use process monitor to kill the GDB process and close VSCode. Kind of painful when one sets a breakpoint but forgets to halt the CPU first.

Interesting observation. If I use PIO Debug or PIO Debug (skip Pre-Debug) it seems the system does not build a properly working binary. If I attach the debugger to this malfunctioning binary, I can see it is stuck in a tight loop that is part of the Arduino system tempmon library.

If I use the regular upload button on the upper right, the system builds a properly working binary. I can attach the debugger to it by using PIO Debug (without uploading).

The only difference between PIO Debug and a regular build should be the usage of debug optimizations, which make things slower but shouldn’t lead to completely hanging up.

You might want to write this in your platformio.ini to do a “Release+DebugInfo” build:

debug_build_flags = -Os -ggdb3 -g3

see docs. You can play with at which optimization level (-Og, -O0 to -O3, -Os) it crashes.

Changing the debug flags did the trick. debug_tool = jlink still doesn’t wotk. Get an error it could not find a device to connect to. I think it is trying to use SWD while I am using jtag. There is no debug_tool = jlink-jtag. The custom platformio.ini below is working well. Thaks again for your help.

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

upload_protocol = custom
upload_flags =
    -device
    MIMXRT1062xxx6B
    -speed
    4000
    -if
    jtag
    -jtagconf
    -1,-1
    -autoconnect
    1
    -NoGui
    1
    -CommanderScript
    "upload.jlink"
upload_command = ${platformio.packages_dir}/tool-jlink/JLinkExe $UPLOAD_FLAGS

debug_build_flags = -Os -ggdb3 -g3

debug_tool = custom
debug_server = 
    ${platformio.packages_dir}/tool-jlink/JLinkGDBServerCLExe 
    -singlerun 
    -if 
    jtag 
    -select 
    USB 
    -device 
    MIMXRT1062xxx6A 
    -port 
    2331
    -speed
    4000
debug_port = localhost:2331
debug_init_cmds =
    define pio_reset_halt_target
      monitor reset
      monitor halt
    end
    define pio_reset_run_target
        monitor clrbp
        monitor reset
        monitor go
    end
    target extended-remote $DEBUG_PORT
    monitor clrbp
    monitor speed auto
    pio_reset_halt_target
    $LOAD_CMDS
    $INIT_BREAK