cancel
Showing results for 
Search instead for 
Did you mean: 

debugee hangs in "initialise_monitor_handles()" (semihosting)

chriskuku
Senior III

I'm trying out semihosting with openocd in STM32CubeIDE.  For this purpose I added the call 

initialise_monitor_handles();

 

in main.c as the second call after HAL_Init(); 

I have STLINK(gdb server) configured. The program goes into that call (initialise_monitor_handles) but never returns.

 

Trying OpenOCD gives another error:

chriskuku_0-1757775410232.png

Open On-Chip Debugger 0.12.0+dev-00623-g0ba753ca7 (2025-04-30-14:20) [https://github.com/STMicroelectronics/OpenOCD]
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : STLINK V2J46M33 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.224068
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : clock speed 4000 kHz
Info : stlink_dap_op_connect(connect)
Info : SWD DPIDR 0x6ba02477
Info : [STM32H503CBTx.ap0] Examination succeed
Info : [STM32H503CBTx.cpu] Cortex-M33 r0p4 processor detected
Info : [STM32H503CBTx.cpu] target has 8 breakpoints, 4 watchpoints
STM32H503CBTx.cpu in Non-Secure state
STM32H503CBTx TrustZone disabled
STM32H503CBTx.cpu work-area address is set to 0x20000000
STM32H503CBTx.cpu work-area is enabled
Info : [STM32H503CBTx.cpu] Examination succeed
Info : gdb port disabled
Info : starting gdb server for STM32H503CBTx.cpu on 3333
Info : Listening on port 3333 for gdb connections
Info : accepting 'gdb' connection on tcp/3333
Error: timed out while waiting for target halted
Error executing event gdb-attach on target STM32H503CBTx.cpu:
/Applications/STM32CubeIDE.app/Contents/Eclipse/plugins/com.st.stm32cube.ide.mcu.debug.openocd_2.3.100.202501240831/resources/openocd/st_scripts/gdb_helper.tcl:18: Error: TARGET: STM32H503CBTx.cpu - Not halted
in procedure 'gdb_attach_hook' called at file "/Applications/STM32CubeIDE.app/Contents/Eclipse/plugins/com.st.stm32cube.ide.mcu.debug.openocd_2.3.100.202501240831/resources/openocd/st_scripts/target/stm32h5x.cfg", line 379
in procedure 'first_gdb_attach_hook' called at file "/Applications/STM32CubeIDE.app/Contents/Eclipse/plugins/com.st.stm32cube.ide.mcu.debug.openocd_2.3.100.202501240831/resources/openocd/st_scripts/gdb_helper.tcl", line 18
Info : device idcode = 0x10036474 (STM32H50xx - Rev Y : 0x1003)
Info : Product State = 0xED : 'Open'
Info : flash size = 128kbytes
Info : flash mode : dual-bank
Info : device idcode = 0x10036474 (STM32H50xx - Rev Y : 0x1003)
Info : Product State = 0xED : 'Open'
Info : flash size = 128kbytes
Info : flash mode : dual-bank
Info : New GDB Connection: 1, Target STM32H503CBTx.cpu, state: running
Warn : GDB connection 1 on target STM32H503CBTx.cpu not halted
undefined debug reason 8 (UNDEFINED) - target needs reset
Error: Failed to read memory at 0x00000000
Info : accepting 'gdb' connection on tcp/3333
Info : New GDB Connection: 2, Target STM32H503CBTx.cpu, state: running
Warn : GDB connection 2 on target STM32H503CBTx.cpu not halted
Error: Failed to read memory at 0x00000000
undefined debug reason 8 (UNDEFINED) - target needs reset
Error: Failed to read memory at 0x00000000
Error: timed out while waiting for target halted
Error executing event gdb-flash-erase-start on target STM32H503CBTx.cpu:
TARGET: STM32H503CBTx.cpu - Not halted
Error: Target not halted
Error: failed erasing sectors 0 to 2
Error: flash_erase returned -304
Info : dropped 'gdb' connection
shutdown command invoked
Info : dropped 'gdb' connection
3 REPLIES 3
Ghofrane GSOURI
ST Employee

Hello @chriskuku 

To enable semihosting for printf and similar functions on your STM32H5 device using STM32CubeIDE and OpenOCD, first ensure you call initialise_monitor_handles() after HAL_Init() in your code. In STM32CubeIDE, open your debug configuration, go to the Debugger tab, and in the OpenOCD Options field, add -c "arm semihosting enable" -c "arm semihosting_fileio enable". This configures OpenOCD to handle semihosting calls, allowing your program’s output to appear in the debugger console.

More details could be found in OpenOCD User’s Guide

THX

Ghofrqne

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

I'm getting:

Open On-Chip Debugger 0.12.0+dev-00623-g0ba753ca7 (2025-04-30-14:20) [https://github.com/STMicroelectronics/OpenOCD]
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Unexpected command line argument:  -c

 

Maybe I had some typo in specifying the -c flags. Anyway, this time the error message from OpenOCD is different::

Open On-Chip Debugger 0.12.0+dev-00623-g0ba753ca7 (2025-04-30-14:20) [https://github.com/STMicroelectronics/OpenOCD]
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Error: The 'arm semihosting' command must be used after 'init'.

Hello @chriskuku 

Based on the error message, we need to ensure that the semihosting commands are executed after OpenOCD has been initialized. To do this, enter the following in the OpenOCD Options field:

-c "init" -c "arm semihosting enable" -c "arm semihosting_fileio enable"

If you continue to encounter errors, carefully check for any typos or unintended spaces in the options field. Make sure each command is enclosed in its own set of double quotes

THX

Ghofrane

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.