Teensy 4.1 Uploading with JLink

Thanks for that great guide and all the information regarding the debugging.
I was able to set my teensy 4.1 up for the JLink connection.
With the platform.ini above, I was able to upload via JTAG and start the debugger, but the debugger kept jumping through the code in (at least for me) uncomprehensive steps.
As far as I understand, this is probably to some compiler optimization that does not work with the debugger. As this may be relevant to someone else trying this, here’s the platformio.ini that worked for me:

[env:teensy41]
platform = teensy
board = teensy41
framework = arduino
lib_extra_dirs = ~/Documents/Arduino/libraries
upload_protocol = custom
upload_flags =
   -device
   MIMXRT1062xxx6A
   -speed
   4000
   -if
   jtag
   -jtagconf
   -1,-1
   -autoconnect
   1
   -NoGui
   1
   -CommanderScript
   "upload.jlink"
upload_command = $PROJECT_PACKAGES_DIR\tool-jlink\JLink.exe $UPLOAD_FLAGS
;debug_tool = custom
debug_tool = jlink
debug_server = 
   ${platformio.packages_dir}\tool-jlink\JLinkGDBServerCL.exe ; edited as suggested by Ivan
   -singlerun
   -if 
   jtag 
   -select 
   USB 
   -device 
   MIMXRT1062xxx6A 
   -port 
   2331
   -speed
   4000
debug_port = localhost:2331

I changed the debug_protocol back to jlink, but overrode some of its parameters, which I guess left the rest of the compiler’s parameters untouched. Adding debug_build_flags = -O0 -ggdb3 -g3 did not work for me, but this did.
I used the upload.jlink file described here.

Thanks again for this thread, really glad I found it, if I had been successful at all, it would have taken a lot longer without it!

Cheers
Philipp