On Mon, Jul 14, 2025 at 2:40 PM Liviu Ionescu <i...@livius.net> wrote: > > > > > On 14 Jul 2025, at 13:02, Marek Vrbka <marek.vr...@codasip.com> wrote: > > > > Hello all, > > I'd like to propose improvements to OpenOCD's semihosting behavior for the > > SYS_EXIT and SYS_EXIT_EXTENDED syscalls. > > Currently, if GDB is not connected and the target calls `SYS_EXIT` or > > `SYS_EXIT_EXTENDED` (with the appropriate parameter): > > • If GDB is not connected - OpenOCD calls `exit(code)` immediately, > > without any cleanup. I believe this should be fixed - OpenOCD should go > > trough the standard shutdown sequence.
I was quickly checking this code while writing my patches https://review.openocd.org/c/openocd/+/9050 https://review.openocd.org/c/openocd/+/9051 and I think it's rotten as hell to have OpenOCD quitting for an action by the firmware under debug! The debugger has to debug and should not quit because the firmware asks for it, otherwise we cannot debug! Now, semihosting is a way for the firmware to ask the debugger to make some action on the host in place of the firmware itself, like accessing the file system. That's fine. But, please, not exit()! The comment in the code about SYS_EXIT is clear and reports: * An application calls this operation to report an exception * to the debugger directly. The most common use is to report * that execution has completed, ... So, the firmware execution has completed, not the debugger has to quit! > > • If GDB is connected - message is printed and target is either halted > > or resumed depending on `arm semihosting_resexit`. The command `arm semihosting_resexit` is an OpenOCD command, not a GDB one. OpenOCD should do what is required by that command, even when GDB is not connected. The exit status could be printed out in the OpenOCD log. If I understand correctly, Liviu uses this exit code in some test that runs OpenOCD, let OpenOCD start a FW on target, get the exit status of the FW and quit OpenOCD with that exit status. The test gets the exit status from OpenOCD. If this is the use case, it should be coded properly, e.g. having an OpenOCD script that intercepts the exit code (how ?) and calls "shutdown" or "shutdown error", depending on the exit code. As a simplification, should we replace `arm semihosting_resexit [enable|disable]` with e.g. `arm semihosting_on_exit [halt|resume|exit]` to shutdown automatically? But option `exit` should not be the default! Antonio > > I would like to make these improvements: > > 1. Avoid the direct call to `exit()` > > 2. Introduce a new command: `arm semihosting_exit_behavior` > > The options would be: > > • `always_halt` > > • `always_exit` > > • `exit_if_no_gdb` > > 3. Introduce commands to get the program exit code in Tcl scripts: > > • `arm semihosting_has_program_exited` - would return true if the > > semihosting exit already happened > > • `arm semihosting_exit_code` - returns exit code (from > > `SYS_EXIT_EXTENDED`) > > Would you be in favor of this proposal? I welcome any comments. > > > Just be sure that in the default configuration the exit code is finally > passed to exit(). > > The original Arm semihosting use case was intended to run portable unit tests > without a debugger (like gdb) connected, and the exit code 0 is paramount to > know that the test passed. > > > Regards, > > Liviu > >