- gdb server was incorrectly sending null terminator on qXfer:features:read: packet
[fw/openocd] / src / server / gdb_server.c
index f01d638fddd2cac05e5c5b9818e66c2e463b1ded..b3c6c9f585c5465d0537dcdcd5991ec8a105131a 100644 (file)
@@ -678,25 +678,23 @@ int gdb_new_connection(connection_t *connection)
        /* register callback to be informed about target events */
        target_register_event_callback(gdb_target_callback_event_handler, connection);
 
-       /* a gdb session just attached, try to put the target in halt mode
-        * or alterantively try to issue a reset.
-        *
-        * GDB connection will fail if e.g. register read packets fail,
-        * otherwise resetting/halting the target could have been left to GDB init
-        * scripts
+       /* a gdb session just attached, try to put the target in halt mode.
         * 
         * DANGER!!!! 
-        * We need a synchronous halt, lest connect will fail.
-        * Also there is no guarantee that poll() will be invoked
-        * between here and serving the first packet, so the halt()
-        * statement above is *NOT* sufficient
+        * 
+        * If the halt fails(e.g. target needs a reset, JTAG communication not
+        * working, etc.), then the GDB connect will succeed as
+        * the get_gdb_reg_list() will lie and return a register list with
+        * dummy values.
+        * 
+        * This allows GDB monitor commands to be run from a GDB init script to
+        * initialize the target
+        * 
+        * Also, since the halt() is asynchronous target connect will be
+        * instantaneous and thus avoiding annoying timeout problems during
+        * connect. 
         */
-       if ((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK)
-       {
-               ERROR("error(%d) when trying to halt target, falling back to \"reset\"", retval);
-               command_run_line(connection->cmd_ctx, "reset");
-       }
-       command_run_line(connection->cmd_ctx, "halt");
+       gdb_service->target->type->halt(gdb_service->target);
        
        /* remove the initial ACK from the incoming buffer */
        if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
@@ -775,21 +773,10 @@ void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
        buf = reg->value;
        buf_len = CEIL(reg->size, 8);
 
-       if (target->endianness == TARGET_LITTLE_ENDIAN)
-       {
-               for (i = 0; i < buf_len; i++)
-               {
-                       tstr[i*2]   = DIGITS[(buf[i]>>4) & 0xf];
-                       tstr[i*2+1] = DIGITS[buf[i]&0xf];
-               }
-       }
-       else
+       for (i = 0; i < buf_len; i++)
        {
-               for (i = 0; i < buf_len; i++)
-               {
-                       tstr[(buf_len-1-i)*2]   = DIGITS[(buf[i]>>4)&0xf];
-                       tstr[(buf_len-1-i)*2+1] = DIGITS[buf[i]&0xf];
-               }
+               tstr[i*2]   = DIGITS[(buf[i]>>4) & 0xf];
+               tstr[i*2+1] = DIGITS[buf[i]&0xf];
        }
 }
 
@@ -804,20 +791,10 @@ void gdb_target_to_str(target_t *target, char *tstr, char *str)
                exit(-1);
        }
 
-       if (target->endianness == TARGET_LITTLE_ENDIAN)
+       for (i = 0; i < str_len; i+=2)
        {
-               for (i = 0; i < str_len; i+=2)
-               {
-                       str[str_len - i - 1] = tstr[i + 1];
-                       str[str_len - i - 2] = tstr[i];
-               }
-       }
-       else
-       {
-               for (i = 0; i < str_len; i++)
-               {
-                       str[i] = tstr[i];
-               }
+               str[str_len - i - 1] = tstr[i + 1];
+               str[str_len - i - 2] = tstr[i];
        }
 }
 
@@ -1182,13 +1159,12 @@ int gdb_write_memory_packet(connection_t *connection, target_t *target, char *pa
        }
        else
        {
-               if ((retval = gdb_error(connection, retval)) != ERROR_OK)
-                       return retval;
+               retval = gdb_error(connection, retval);
        }
 
        free(buffer);
 
-       return ERROR_OK;
+       return retval;
 }
 
 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
@@ -1466,9 +1442,9 @@ int gdb_query_packet(connection_t *connection, target_t *target, char *packet, i
 
                        /* We want to print all debug output to GDB connection */
                        log_add_callback(gdb_log_callback, connection);
-                       target_call_timer_callbacks();
+                       target_call_timer_callbacks_now();
                        command_run_line(cmd_ctx, cmd);
-                       target_call_timer_callbacks();
+                       target_call_timer_callbacks_now();
                        log_remove_callback(gdb_log_callback, connection);
                        free(cmd);
                }
@@ -1640,7 +1616,7 @@ int gdb_query_packet(connection_t *connection, target_t *target, char *packet, i
                        return retval;
                }
 
-               gdb_put_packet(connection, xml, strlen(xml) + 1);
+               gdb_put_packet(connection, xml, strlen(xml));
 
                free(xml);
                return ERROR_OK;