Skip to main content
chriskuku
Senior II
September 15, 2025
Question

SEMIhosting doesn't work (ST-LINK OpenOCD)

  • September 15, 2025
  • 3 replies
  • 709 views

I'm trying to get semihosting to work under STM32CubeIDE with ST-LINK(OpenOCD) debugging.
After being able to connect to the target (an STM32H503), I cannot step over the function initialise_monitor_handles()  (see source code). A lot of traps occur but I can't get into the real code to debug. (the lines in the main.c while loop).

Bear with me but the code looks a bit like swiss cheese. Tried various reads, scanf etc. But at the moment I cannot get across the "initialise_monitor_handles();" call.

3 replies

Associate II
September 15, 2025

Without reading everything I noticed you renamed syscalls.c to syscalls.c.disabled.

This makes your functions like _read() and _write() NOT to overwrite the default weak functions.

Is this intended?

chriskuku
chriskukuAuthor
Senior II
September 15, 2025

I was following the KB article here and there is says in 2.1:

2.1. Remove default syscalls.c

Remove default <syscalls.c> file from build if this file is included in STM32CubeIDE project.

 

which I devotely did.

Andrew Neil
Super User
September 15, 2025

@chriskuku wrote:

 I cannot step over the function initialise_monitor_handles() 


Does it work if you don't try to step over it?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
chriskuku
chriskukuAuthor
Senior II
September 15, 2025

No. It vanishes in the call.
But I found that I forgot to put

mon arm semihosting enable

 

With this in the startup I can step over until the loop in which I want to read a line of input. This read now works exctly once.

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.223529
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
[STM32H503CBTx.cpu] halted due to undefined, current mode: Handler HardFault
xPSR: 0x21000003 pc: 0x080007b8 msp: 0x20007fa0
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
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: halted
undefined debug reason 8 (UNDEFINED) - target needs reset
Info : accepting 'gdb' connection on tcp/3333
Info : New GDB Connection: 2, Target STM32H503CBTx.cpu, state: halted
undefined debug reason 8 (UNDEFINED) - target needs reset
[STM32H503CBTx.cpu] halted due to undefined, current mode: Handler HardFault
xPSR: 0x21000003 pc: 0x080007b8 msp: 0x20007fa0, semihosting
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Padding image section 0 at 0x08002e44 with 12 bytes (bank write end alignment)
[STM32H503CBTx.cpu] halted due to undefined, current mode: Handler HardFault
xPSR: 0x61000003 pc: 0x2000005a msp: 0x200000d0, semihosting
1 Enter string:
helo
Warn : keep_alive() was not invoked in the 1000 ms timelimit. GDB alive packet not sent! (8295 ms). Workaround: increase "set remotetimeout" in GDB

5 Received char: helo
read-select 1

The next printf("%d Enter string:\n",i) is not executed, as it seems.

Associate II
September 15, 2025
chriskuku
chriskukuAuthor
Senior II
September 16, 2025

At the moment I cannot find the most recent post, where I got advised to use OpenOCD options:

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

and ended up in:

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 : STLINK V2J46M33 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.225098
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
semihosting is enabled
semihosting fileio is enabled
Error: The 'gdb_report_data_abort' command must be used before 'init'.