openocd: use proper format with uint32_t
[openocd.git] / src / server / gdb_server.c
index 6a9e261cd8aade3c155a4dd6a9ce40d5bee7f0c0..c369665547f14c545a832c824fea5b87a23759d0 100644 (file)
@@ -68,7 +68,7 @@ struct target_desc_format {
 
 /* private connection data for GDB */
 struct gdb_connection {
-       char buffer[GDB_BUFFER_SIZE + 1]; /* Extra byte for nul-termination */
+       char buffer[GDB_BUFFER_SIZE + 1]; /* Extra byte for null-termination */
        char *buf_p;
        int buf_cnt;
        bool ctrl_c;
@@ -996,8 +996,7 @@ static int gdb_new_connection(struct connection *connection)
                 * This will cause an auto_probe to be invoked, which is either
                 * a no-op or it will fail when the target isn't ready(e.g. not halted).
                 */
-               int i;
-               for (i = 0; i < flash_get_bank_count(); i++) {
+               for (unsigned int i = 0; i < flash_get_bank_count(); i++) {
                        struct flash_bank *p;
                        p = get_flash_bank_by_num_noprobe(i);
                        if (p->target != target)
@@ -1019,6 +1018,17 @@ static int gdb_new_connection(struct connection *connection)
                        target_name(target),
                        target_state_name(target));
 
+       if (!target_was_examined(target)) {
+               LOG_ERROR("Target %s not examined yet, refuse gdb connection %d!",
+                                 target_name(target), gdb_actual_connections);
+               gdb_actual_connections--;
+               return ERROR_TARGET_NOT_EXAMINED;
+       }
+
+       if (target->state != TARGET_HALTED)
+               LOG_WARNING("GDB connection %d on target %s not halted",
+                                       gdb_actual_connections, target_name(target));
+
        /* DANGER! If we fail subsequently, we must remove this handler,
         * otherwise we occasionally see crashes as the timer can invoke the
         * callback fn.
@@ -1057,11 +1067,8 @@ static int gdb_connection_closed(struct connection *connection)
        /* if this connection registered a debug-message receiver delete it */
        delete_debug_msg_receiver(connection->cmd_ctx, target);
 
-       if (connection->priv) {
-               free(connection->priv);
-               connection->priv = NULL;
-       } else
-               LOG_ERROR("BUG: connection->priv == NULL");
+       free(connection->priv);
+       connection->priv = NULL;
 
        target_unregister_event_callback(gdb_target_callback_event_handler, connection);
 
@@ -1381,8 +1388,8 @@ static int gdb_set_register_packet(struct connection *connection,
        }
 
        if (chars != (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2)) {
-               LOG_ERROR("gdb sent %d bits for a %d-bit register (%s)",
-                               (int) chars * 4, reg_list[reg_num]->size, reg_list[reg_num]->name);
+               LOG_ERROR("gdb sent %zu bits for a %" PRIu32 "-bit register (%s)",
+                               chars * 4, reg_list[reg_num]->size, reg_list[reg_num]->name);
                free(bin_buf);
                free(reg_list);
                return ERROR_SERVER_REMOTE_CLOSED;
@@ -1748,8 +1755,7 @@ static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 5, 6))) void xml_printf(
                        char *t = *xml;
                        *xml = realloc(*xml, *size);
                        if (*xml == NULL) {
-                               if (t)
-                                       free(t);
+                               free(t);
                                *retval = ERROR_SERVER_REMOTE_CLOSED;
                                return;
                        }
@@ -1831,8 +1837,7 @@ static int gdb_memory_map(struct connection *connection,
        int length;
        char *separator;
        target_addr_t ram_start = 0;
-       int i;
-       int target_flash_banks = 0;
+       unsigned int target_flash_banks = 0;
 
        /* skip command character */
        packet += 23;
@@ -1848,7 +1853,7 @@ static int gdb_memory_map(struct connection *connection,
         */
        banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
 
-       for (i = 0; i < flash_get_bank_count(); i++) {
+       for (unsigned int i = 0; i < flash_get_bank_count(); i++) {
                p = get_flash_bank_by_num_noprobe(i);
                if (p->target != target)
                        continue;
@@ -1864,8 +1869,7 @@ static int gdb_memory_map(struct connection *connection,
        qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
                compare_bank);
 
-       for (i = 0; i < target_flash_banks; i++) {
-               int j;
+       for (unsigned int i = 0; i < target_flash_banks; i++) {
                unsigned sector_size = 0;
                unsigned group_len = 0;
 
@@ -1883,7 +1887,7 @@ static int gdb_memory_map(struct connection *connection,
                 * smaller ones at the end (maybe 32KB).  STR7 will have
                 * regions with 8KB, 32KB, and 64KB sectors; etc.
                 */
-               for (j = 0; j < p->num_sectors; j++) {
+               for (unsigned int j = 0; j < p->num_sectors; j++) {
 
                        /* Maybe start a new group of sectors. */
                        if (sector_size == 0) {
@@ -2046,7 +2050,7 @@ static int gdb_generate_reg_type_description(struct target *target,
                }
                /* <vector id="id" type="type" count="count"/> */
                xml_printf(&retval, tdesc, pos, size,
-                               "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
+                               "<vector id=\"%s\" type=\"%s\" count=\"%" PRIu32 "\"/>\n",
                                type->id, type->reg_type_vector->type->id,
                                type->reg_type_vector->count);
 
@@ -2093,11 +2097,11 @@ static int gdb_generate_reg_type_description(struct target *target,
                         *  <field name="name" start="start" end="end"/> ...
                         * </struct> */
                        xml_printf(&retval, tdesc, pos, size,
-                                       "<struct id=\"%s\" size=\"%d\">\n",
+                                       "<struct id=\"%s\" size=\"%" PRIu32 "\">\n",
                                        type->id, type->reg_type_struct->size);
                        while (field != NULL) {
                                xml_printf(&retval, tdesc, pos, size,
-                                               "<field name=\"%s\" start=\"%d\" end=\"%d\" type=\"%s\" />\n",
+                                               "<field name=\"%s\" start=\"%" PRIu32 "\" end=\"%" PRIu32 "\" type=\"%s\" />\n",
                                                field->name, field->bitfield->start, field->bitfield->end,
                                                gdb_get_reg_type_name(field->bitfield->type));
 
@@ -2138,14 +2142,14 @@ static int gdb_generate_reg_type_description(struct target *target,
                 *  <field name="name" start="start" end="end"/> ...
                 * </flags> */
                xml_printf(&retval, tdesc, pos, size,
-                               "<flags id=\"%s\" size=\"%d\">\n",
+                               "<flags id=\"%s\" size=\"%" PRIu32 "\">\n",
                                type->id, type->reg_type_flags->size);
 
                struct reg_data_type_flags_field *field;
                field = type->reg_type_flags->fields;
                while (field != NULL) {
                        xml_printf(&retval, tdesc, pos, size,
-                                       "<field name=\"%s\" start=\"%d\" end=\"%d\" type=\"%s\" />\n",
+                                       "<field name=\"%s\" start=\"%" PRIu32 "\" end=\"%" PRIu32 "\" type=\"%s\" />\n",
                                        field->name, field->bitfield->start, field->bitfield->end,
                                        gdb_get_reg_type_name(field->bitfield->type));
 
@@ -2298,9 +2302,9 @@ static int gdb_generate_target_description(struct target *target, char **tdesc_o
                                xml_printf(&retval, &tdesc, &pos, &size,
                                                "<reg name=\"%s\"", reg_list[i]->name);
                                xml_printf(&retval, &tdesc, &pos, &size,
-                                               " bitsize=\"%d\"", reg_list[i]->size);
+                                               " bitsize=\"%" PRIu32 "\"", reg_list[i]->size);
                                xml_printf(&retval, &tdesc, &pos, &size,
-                                               " regnum=\"%d\"", reg_list[i]->number);
+                                               " regnum=\"%" PRIu32 "\"", reg_list[i]->number);
                                if (reg_list[i]->caller_save)
                                        xml_printf(&retval, &tdesc, &pos, &size,
                                                        " save-restore=\"yes\"");
@@ -3117,7 +3121,7 @@ static int gdb_v_packet(struct connection *connection,
                target_call_event_callbacks(target,
                                TARGET_EVENT_GDB_FLASH_WRITE_START);
                result = flash_write(target, gdb_connection->vflash_image,
-                       &written, 0);
+                       &written, false);
                target_call_event_callbacks(target,
                        TARGET_EVENT_GDB_FLASH_WRITE_END);
                if (result != ERROR_OK) {
@@ -3224,13 +3228,14 @@ static void gdb_sig_halted(struct connection *connection)
 static int gdb_input_inner(struct connection *connection)
 {
        /* Do not allocate this on the stack */
-       static char gdb_packet_buffer[GDB_BUFFER_SIZE + 1]; /* Extra byte for nul-termination */
+       static char gdb_packet_buffer[GDB_BUFFER_SIZE + 1]; /* Extra byte for null-termination */
 
        struct target *target;
        char const *packet = gdb_packet_buffer;
        int packet_size;
        int retval;
        struct gdb_connection *gdb_con = connection->priv;
+       static bool warn_use_ext;
 
        target = get_target_from_connection(connection);
 
@@ -3307,6 +3312,12 @@ static int gdb_input_inner(struct connection *connection)
                                        break;
                                case '?':
                                        gdb_last_signal_packet(connection, packet, packet_size);
+                                       /* '?' is sent after the eventual '!' */
+                                       if (!warn_use_ext && !gdb_con->extended_protocol) {
+                                               warn_use_ext = true;
+                                               LOG_WARNING("Prefer GDB command \"target extended-remote %s\" instead of \"target remote %s\"",
+                                                                       connection->service->port, connection->service->port);
+                                       }
                                        break;
                                case 'c':
                                case 's':
@@ -3499,7 +3510,7 @@ static int gdb_target_start(struct target *target, const char *port)
        ret = add_service("gdb",
                        port, 1, &gdb_new_connection, &gdb_input,
                        &gdb_connection_closed, gdb_service);
-       /* initialialize all targets gdb service with the same pointer */
+       /* initialize all targets gdb service with the same pointer */
        {
                struct target_list *head;
                struct target *curr;

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)