openocd.c: 'init' should fail if GDB service cannot be created

If it is not possible to create a GDB service for a certain target
(for example the given TCP port is already occupied), the "init"
command should fail, but it currently does not.

Fix this by checking the return code of gdb_target_add_all().

Steps to reproduce:

1) Make the port 3333/tcp occupied. For example by:

   nc -l 3333

2) In another terminal, launch OpenOCD. Use the gdb_port
   3333 (which is the default). For example:

   path/to/your/openocd \
      -c "adapter driver ..." \
      -c "jtag newtap ..."
      -c "target create ..."

3) Observe the outcome:

   Before this patch:

   Error "couldn't bind gdb to socket on port 3333: Address already in use"
   is displayed but OpenOCD keeps running.

   After this patch:

   The error message is displayed and OpenOCD exits - as expected.

Change-Id: I63c283a9a1095167b78e69e9ee879c378a6b9f2a
Signed-off-by: Jan Matyas <jan.matyas@codasip.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/8957
Tested-by: jenkins
Reviewed-by: zapb <dev@zapb.de>
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
This commit is contained in:
Jan Matyas
2025-06-17 13:17:23 +02:00
committed by Antonio Borneo
parent 9b660bbd19
commit 46aa9c0e52

View File

@@ -170,7 +170,8 @@ COMMAND_HANDLER(handle_init_command)
jtag_poll_unmask(save_poll_mask);
/* initialize telnet subsystem */
gdb_target_add_all(all_targets);
if (gdb_target_add_all(all_targets) != ERROR_OK)
return ERROR_FAIL;
target_register_event_callback(log_target_callback_event_handler, CMD_CTX);