improve gdb_init() sequence
[openocd.git] / src / server / gdb_server.c
index 3c099fa00785e645efbbd2a447e3f8638be12ac6..7fb36e41023db465aedc242297e77bede0336ff8 100644 (file)
@@ -2189,55 +2189,68 @@ int gdb_input(struct connection *connection)
        return ERROR_OK;
 }
 
-int gdb_init(void)
+static int gdb_target_start(struct target *target, uint16_t port)
 {
-       struct gdb_service *gdb_service;
-       struct target *target = all_targets;
+       bool use_pipes = 0 == port;
+       struct gdb_service *gdb_service = malloc(sizeof(struct gdb_service));
+       if (NULL == gdb_service)
+               return -ENOMEM;
 
-       if (!target)
-       {
-               LOG_WARNING("no gdb ports allocated as no target has been specified");
-               return ERROR_OK;
-       }
+       gdb_service->target = target;
 
+       add_service("gdb", use_pipes ? CONNECTION_PIPE : CONNECTION_TCP,
+                       port, 1, &gdb_new_connection, &gdb_input,
+                       &gdb_connection_closed, gdb_service);
+
+       const char *name = target_name(target);
+       if (use_pipes)
+               LOG_DEBUG("gdb service for target '%s' using pipes", name);
+       else
+               LOG_DEBUG("gdb service for target '%s' on TCP port %u", name, port);
+       return ERROR_OK;
+}
+
+int gdb_target_add_one(struct target *target)
+{
        if (gdb_port == 0 && server_use_pipes == 0)
        {
                LOG_INFO("gdb port disabled");
                return ERROR_OK;
        }
 
-       if (server_use_pipes)
+       bool use_pipes = server_use_pipes;
+       static bool server_started_with_pipes = false;
+       if (server_started_with_pipes)
        {
-               /* only a single gdb connection when using a pipe */
+               LOG_WARNING("gdb service permits one target when using pipes");
+               if (0 == gdb_port)
+                       return ERROR_OK;
 
-               gdb_service = malloc(sizeof(struct gdb_service));
-               gdb_service->target = target;
+               use_pipes = false;
+       }
 
-               add_service("gdb", CONNECTION_PIPE, 0, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
+       int e = gdb_target_start(target, use_pipes ? 0 : gdb_port++);
+       if (ERROR_OK == e)
+               server_started_with_pipes |= use_pipes;
 
-               LOG_DEBUG("gdb service for target %s using pipes",
-                               target_name(target));
+       return e;
+}
+
+int gdb_target_add_all(struct target *target)
+{
+       if (NULL == target)
+       {
+               LOG_WARNING("gdb services need one or more targets defined");
+               return ERROR_OK;
        }
-       else
+
+       while (NULL != target)
        {
-               unsigned short port = gdb_port;
+               int retval = gdb_target_add_one(target);
+               if (ERROR_OK != retval)
+                       return retval;
 
-               while (target)
-               {
-                       gdb_service = malloc(sizeof(struct gdb_service));
-                       gdb_service->target = target;
-
-                       add_service("gdb", CONNECTION_TCP,
-                                       port, 1,
-                                       gdb_new_connection, gdb_input,
-                                       gdb_connection_closed, gdb_service);
-
-                       LOG_DEBUG("gdb service for target %s at TCP port %i",
-                                       target_name(target),
-                                       port);
-                       target = target->next;
-                       port++;
-               }
+               target = target->next;
        }
 
        return ERROR_OK;

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)