X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fserver%2Fgdb_server.c;h=e17ccffa9f3eefa65336880be46fed7a325276c5;hp=5e570f5d5302df3903bb81f05e347a7b4e5bb336;hb=08d4411b59dd8bd0e7d8009003b71d23acbf6eee;hpb=3d62c3df6ddb09f1485c25d665e248856989d180 diff --git a/src/server/gdb_server.c b/src/server/gdb_server.c index 5e570f5d53..e17ccffa9f 100644 --- a/src/server/gdb_server.c +++ b/src/server/gdb_server.c @@ -27,7 +27,7 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * ***************************************************************************/ #ifdef HAVE_CONFIG_H @@ -93,7 +93,6 @@ static enum breakpoint_type gdb_breakpoint_override_type; static int gdb_error(struct connection *connection, int retval); static const char *gdb_port; static const char *gdb_port_next; -static const char DIGITS[16] = "0123456789abcdef"; static void gdb_log_callback(void *priv, const char *file, unsigned line, const char *function, const char *string); @@ -379,18 +378,14 @@ static int gdb_put_packet_inner(struct connection *connection, if ((size_t)len + 4 <= sizeof(local_buffer)) { /* performance gain on smaller packets by only a single call to gdb_write() */ memcpy(local_buffer + 1, buffer, len++); - local_buffer[len++] = '#'; - local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf]; - local_buffer[len++] = DIGITS[my_checksum & 0xf]; + len += snprintf(local_buffer + len, sizeof(local_buffer) - len, "#%02x", my_checksum); retval = gdb_write(connection, local_buffer, len); if (retval != ERROR_OK) return retval; } else { /* larger packets are transmitted directly from caller supplied buffer * by several calls to gdb_write() to avoid dynamic allocation */ - local_buffer[1] = '#'; - local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf]; - local_buffer[3] = DIGITS[my_checksum & 0xf]; + snprintf(local_buffer + 1, sizeof(local_buffer) - 1, "#%02x", my_checksum); retval = gdb_write(connection, local_buffer, 1); if (retval != ERROR_OK) return retval; @@ -724,7 +719,6 @@ static int gdb_target_callback_event_handler(struct target *target, int retval; struct connection *connection = priv; - target_handle_event(target, event); switch (event) { case TARGET_EVENT_GDB_HALT: gdb_frontend_halted(target, connection); @@ -900,7 +894,7 @@ static int gdb_last_signal_packet(struct connection *connection, return ERROR_OK; } -static int gdb_reg_pos(struct target *target, int pos, int len) +static inline int gdb_reg_pos(struct target *target, int pos, int len) { if (target->endianness == TARGET_LITTLE_ENDIAN) return pos; @@ -929,22 +923,10 @@ static void gdb_str_to_target(struct target *target, for (i = 0; i < buf_len; i++) { int j = gdb_reg_pos(target, i, buf_len); - tstr[i*2] = DIGITS[(buf[j]>>4) & 0xf]; - tstr[i*2 + 1] = DIGITS[buf[j]&0xf]; + tstr += sprintf(tstr, "%02x", buf[j]); } } -static int hextoint(int c) -{ - if (c >= '0' && c <= '9') - return c - '0'; - c = toupper(c); - if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - LOG_ERROR("BUG: invalid register value %08x", c); - return 0; -} - /* copy over in register buffer */ static void gdb_target_to_reg(struct target *target, char *tstr, int str_len, uint8_t *bin) @@ -956,8 +938,11 @@ static void gdb_target_to_reg(struct target *target, int i; for (i = 0; i < str_len; i += 2) { - uint8_t t = hextoint(tstr[i]) << 4; - t |= hextoint(tstr[i + 1]); + unsigned t; + if (sscanf(tstr + i, "%02x", &t) != 1) { + LOG_ERROR("BUG: unable to convert register value"); + exit(-1); + } int j = gdb_reg_pos(target, i/2, str_len/2); bin[j] = t; @@ -992,7 +977,7 @@ static int gdb_get_registers_packet(struct connection *connection, assert(reg_packet_size > 0); - reg_packet = malloc(reg_packet_size); + reg_packet = malloc(reg_packet_size + 1); /* plus one for string termination null */ reg_packet_p = reg_packet; for (i = 0; i < reg_list_size; i++) { @@ -1099,7 +1084,7 @@ static int gdb_get_register_packet(struct connection *connection, if (!reg_list[reg_num]->valid) reg_list[reg_num]->type->get(reg_list[reg_num]); - reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2); + reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2 + 1); /* plus one for string termination null */ gdb_str_to_target(target, reg_packet, reg_list[reg_num]); @@ -1273,7 +1258,7 @@ static int gdb_write_memory_packet(struct connection *connection, LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len); - if (unhexify((char *)buffer, separator + 2, len) != (int)len) + if (unhexify((char *)buffer, separator, len) != (int)len) LOG_ERROR("unable to decode memory packet"); retval = target_write_buffer(target, addr, len, buffer);