Semihosting scanf() broken with VScode

What is interpreter-exec console to appear on target’s stdin?
Extra strings make difficult to use scanf(), etc.

Example:

Notably, this is not happening when running pio debug --interface=gdb -x .pioinit, however, this one is also not completely free of issue, because after pressing q, the stdin receives “pio_reset_run_target q”.

I think it is coming either from platformio-vscode-debug or VScode debug adapter.
The only thing that makes me think maybe OpenOCD is involved somehow is that ststm32@16 behaves differently than ststm32@15.6, i.e. with v15.6 I get lines of extra garbage characters on stdin.

Extra details here:

Btw, it’s not the only case that causes the gdb commands to appear on stdin.
Pressing all sort of debugger controls send the commands to stdin too.
Here is a test of debug controls when waiting for user input - in short, controls don’t work when scanf() is waiting for input and instead all debugger actions end up on stdin:

Processing bluepill_f103c8 (platform: ststm32@16; board: bluepill_f103c8; framework: cmsis)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/bluepill_f103c8.html
PLATFORM: ST STM32 (16.0.0) > BluePill F103C8
HARDWARE: STM32F103C8T6 72MHz, 20KB RAM, 64KB Flash
DEBUG: Current (stlink) External (blackmagic, cmsis-dap, jlink, stlink)
PACKAGES:
 - framework-cmsis @ 2.50501.200527 (5.5.1)
 - framework-cmsis-stm32f1 @ 4.3.1
 - tool-ldscripts-ststm32 @ 0.2.0
 - toolchain-gccarmnoneeabi @ 1.70201.0 (7.2.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in debug mode
Checking size .pio\build\bluepill_f103c8\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   1.6% (used 328 bytes from 20480 bytes)
Flash: [=         ]  10.4% (used 6788 bytes from 65536 bytes)
========================= [SUCCESS] Took 0.81 seconds =========================
Reading symbols from c:\Users\dzidm\Downloads\STM32_BluePill_Semihosting\.pio\build\bluepill_f103c8\firmware.elf...
done.
PlatformIO Unified Debugger -> https://bit.ly/pio-debug
PlatformIO: debug_tool = stlink
PlatformIO: Initializing remote target...
xPack Open On-Chip Debugger 0.12.0-01004-g9ea7f3d64-dirty (2023-01-30-15:04)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
hla_swd
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
none separate

Info : tcl server disabled
Info : telnet server disabled
Info : clock speed 1000 kHz
Info : STLINK V2J34M25 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.235938
Info : [stm32f1x.cpu] Cortex-M3 r1p1 processor detected
Info : [stm32f1x.cpu] target has 6 breakpoints, 4 watchpoints
Info : starting gdb server for stm32f1x.cpu on pipe
Info : accepting 'gdb' connection from pipe
[stm32f1x.cpu] halted due to breakpoint, current mode: Thread
xPSR: 0x01000000 pc: 0x08001502 msp: 0x20004f70
Info : device id = 0x20036410
Info : flash size = 64 KiB
0x08001502 in _swiread ()
[stm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x0800015c msp: 0x20005000
Loading section .isr_vector, size 0x10c lma 0x8000000
Loading section .text, size 0x192c lma 0x800010c
Loading section .rodata, size 0xe8 lma 0x8001a38
Loading section .init_array, size 0x4 lma 0x8001b20
Loading section .fini_array, size 0x4 lma 0x8001b24
Loading section .data, size 0x70 lma 0x8001b28
[stm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x0800015c msp: 0x20005000
Start address 0x800015c, load size 7064
Transfer rate: 12 KB/sec, 1177 bytes/write.
[stm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x0800015c msp: 0x20005000
[stm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x0800015c msp: 0x20005000
Temporary breakpoint 1 at 0x8000266: file src\main.c, line 66.
semihosting is enabled
semihosting fileio is enabled
PlatformIO: Initialization completed
PlatformIO: Resume the execution to `debug_init_break = tbreak main`
PlatformIO: More configuration options -> https://bit.ly/pio-debug
Note: automatically using hardware breakpoints for read-only addresses.

Temporary breakpoint 1, main () at src\main.c:66
66	    initialise_monitor_handles();   /* This Function MUST come before the first printf() */
Hello, world
Press key
Additonally we get garbage strings when controlling debugger with gui

24-interpreter-exec console "Additonally we get garbage strings when controlling debugger with gui"Option 
Press key
E.g. adding a breakpoint
25-interpreter-exec console "E.g. adding a breakpoint"Option 00
Press key
26-exec-interruptOption 00
Press key
Or pausing the programm
27-interpreter-exec console "Or pausing the programm"Option 00
Press key
28-exec-interrupt --thread 1Option 00
Press key
It actually didn't pause, but it will if pause is pressed again
29-interpreter-exec console "It actually didn't pause, but it will if pause is pressed again"Option 00
Press key
30-exec-interrupt --thread 1Option 00
Press key
31-exec-interrupt --thread 1Option 00
Press key
32-exec-interrupt --thread 1Option 00
Press key
Or not...
33-interpreter-exec console "Or not..."Option 00
Press key
Let's try stepping
34-interpreter-exec console "Let's try stepping"Option 00
Press key
35-exec-interruptOption 00
Press key
36-interpreter-exec console ""Option 00
Press key
37-interpreter-exec console ""Option 00
Press key
38-interpreter-exec console ""Option 00
Press key
Nope, I guess controls don't work when scanf is consuming it
39-interpreter-exec console "Nope, I guess controls don't work when scanf is consuming it"Option 00
Press key
Let's stop the program. I know it will not stop after single press...
40-interpreter-exec console "Let's stop the program. I know it will not stop after single press..."Option 00
Press key
pio_reset_run_targetOption 00
Press key
-gdb-exitOption 00
Press key
pio_reset_run_targetOption 00
Press key
-gdb-exitOption 00
Press key
Canceled
Canceled
Canceled
Canceled
Canceled
Canceled
Canceled
Canceled
Canceled
Canceled

If it works on the CLI but not in the VSCode GUI, you might want to open an issue at

Part of the issue still relates to CLI:

so I am going to leave that one open.

But as suggested I created a new issue for the vscode-IDE: