]> www.infradead.org Git - users/borneoa/openocd-next.git/commitdiff
openocd.c: 'init' should fail if GDB service cannot be created
authorJan Matyas <jan.matyas@codasip.com>
Tue, 17 Jun 2025 11:17:23 +0000 (13:17 +0200)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sun, 29 Jun 2025 07:33:50 +0000 (07:33 +0000)
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>
src/openocd.c

index 3fbece3955378033c24d4fda31ee18268af70c31..e63a9661a3f3f6b61e42a5ca66e29df3e58b4b0b 100644 (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);