cancel
Showing results for 
Search instead for 
Did you mean: 

SEMIhosting doesn't work (ST-LINK OpenOCD)

chriskuku
Senior III

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.

8 REPLIES 8
mfgkw
Senior

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?

Andrew Neil
Super User

@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.

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.

mfgkw
Senior

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.

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'.

I owe you an answer to that question: before adding "mon arm semihosting enable" in the startup section the program flow was trapped down in "initialise_monitor_handles".
After that I could run the program. 

Then I got advised to use 

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

(without the -c init). But without the -c init I'm told that the options arm semihosting enable and arm semihosting_fileio enable must be preceded by -c init. Doing so, I'm told that 

gdb_report_data_abort'

has to be used before init. I have no access to the default OpenOCD command line.

I'm lost here now and need help.

 

I must say that I' still stuck with semihosting not working properly using ST-LINK OpenOCD. Still refering to the project uploaded in SEMI.zip:

With the command 

mon arm semihosting enable

 

I can run my program to the first "Enter string:" prompt, can enter a string, and got the response "Received char: helo". (see trace log)

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: 0x29000003 pc: 0x0800041c msp: 0x20007f5c
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: 0x29000003 pc: 0x0800041c msp: 0x20007f5c, 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! (10563 ms). Workaround: increase "set remotetimeout" in GDB

5 Received char: helo

What it's about this keep alive warning, I don't know whether it's significant.
I'd really appreciate if someone can make this example working. I'm clueless.

Furthermore, using OpenOCD command line options (instead) is leading to another chaos: 
sometimes the -c switches are flagged as errors. Seems like the command line options have a "memory" and don't get totally deleted, when deleting it in the setup panel. 

My statement until someone proves the contrary: OpenOCD semihosting is broken in STM32CubeIDE (1.19.0)