X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fserver%2Fgdb_server.c;h=48015abcda1f8e4e65aace51151808f66bd9b911;hp=0a8babe2049673b625a869151938c23254158bf9;hb=0961987a1916a498e65240cded359ab3c9b73f95;hpb=4a5dc0988ac91d1b5e63a28688edc2c6e890c179 diff --git a/src/server/gdb_server.c b/src/server/gdb_server.c index 0a8babe204..48015abcda 100644 --- a/src/server/gdb_server.c +++ b/src/server/gdb_server.c @@ -14,6 +14,12 @@ * Copyright (C) ST-Ericsson SA 2011 * * michel.jaouen@stericsson.com : smp minimum support * * * + * Copyright (C) 2013 Andes Technology * + * Hsiangkai Wang * + * * + * Copyright (C) 2013 Franck Jullien * + * elec4fun@gmail.com * + * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * @@ -27,7 +33,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 +99,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); @@ -115,6 +120,14 @@ static int gdb_flash_program = 1; */ static int gdb_report_data_abort; +/* set if we are sending target descriptions to gdb + * via qXfer:features:read packet */ +/* disabled by default */ +static int gdb_use_target_description; + +/* current processing free-run type, used by file-I/O */ +static char gdb_running_type; + static int gdb_last_signal(struct target *target) { switch (target->debug_reason) { @@ -379,18 +392,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; @@ -662,20 +671,17 @@ static int gdb_get_packet(struct connection *connection, char *buffer, int *len) static int gdb_output_con(struct connection *connection, const char *line) { char *hex_buffer; - int i, bin_size; + int bin_size; bin_size = strlen(line); - hex_buffer = malloc(bin_size*2 + 2); + hex_buffer = malloc(bin_size * 2 + 2); if (hex_buffer == NULL) return ERROR_GDB_BUFFER_TOO_SMALL; hex_buffer[0] = 'O'; - for (i = 0; i < bin_size; i++) - snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]); - hex_buffer[bin_size*2 + 1] = 0; - - int retval = gdb_put_packet(connection, hex_buffer, bin_size*2 + 1); + int pkt_len = hexify(hex_buffer + 1, line, bin_size, bin_size * 2 + 1); + int retval = gdb_put_packet(connection, hex_buffer, pkt_len + 1); free(hex_buffer); return retval; @@ -688,6 +694,146 @@ static int gdb_output(struct command_context *context, const char *line) return ERROR_OK; } +static void gdb_signal_reply(struct target *target, struct connection *connection) +{ + struct gdb_connection *gdb_connection = connection->priv; + char sig_reply[20]; + char stop_reason[20]; + int sig_reply_len; + int signal_var; + + if (target->debug_reason == DBG_REASON_EXIT) { + sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "W00"); + } else { + if (gdb_connection->ctrl_c) { + signal_var = 0x2; + gdb_connection->ctrl_c = 0; + } else + signal_var = gdb_last_signal(target); + + stop_reason[0] = '\0'; + if (target->debug_reason == DBG_REASON_WATCHPOINT) { + enum watchpoint_rw hit_wp_type; + uint32_t hit_wp_address; + + if (watchpoint_hit(target, &hit_wp_type, &hit_wp_address) == ERROR_OK) { + + switch (hit_wp_type) { + case WPT_WRITE: + snprintf(stop_reason, sizeof(stop_reason), + "watch:%08x;", hit_wp_address); + break; + case WPT_READ: + snprintf(stop_reason, sizeof(stop_reason), + "rwatch:%08x;", hit_wp_address); + break; + case WPT_ACCESS: + snprintf(stop_reason, sizeof(stop_reason), + "awatch:%08x;", hit_wp_address); + break; + default: + break; + } + } + } + + sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "T%2.2x%s", + signal_var, stop_reason); + } + + gdb_put_packet(connection, sig_reply, sig_reply_len); + gdb_connection->frontend_state = TARGET_HALTED; + rtos_update_threads(target); +} + +static void gdb_fileio_reply(struct target *target, struct connection *connection) +{ + struct gdb_connection *gdb_connection = connection->priv; + char fileio_command[256]; + int command_len; + bool program_exited = false; + + if (strcmp(target->fileio_info->identifier, "open") == 0) + sprintf(fileio_command, "F%s,%x/%x,%x,%x", target->fileio_info->identifier, + target->fileio_info->param_1, + target->fileio_info->param_2, + target->fileio_info->param_3, + target->fileio_info->param_4); + else if (strcmp(target->fileio_info->identifier, "close") == 0) + sprintf(fileio_command, "F%s,%x", target->fileio_info->identifier, + target->fileio_info->param_1); + else if (strcmp(target->fileio_info->identifier, "read") == 0) + sprintf(fileio_command, "F%s,%x,%x,%x", target->fileio_info->identifier, + target->fileio_info->param_1, + target->fileio_info->param_2, + target->fileio_info->param_3); + else if (strcmp(target->fileio_info->identifier, "write") == 0) + sprintf(fileio_command, "F%s,%x,%x,%x", target->fileio_info->identifier, + target->fileio_info->param_1, + target->fileio_info->param_2, + target->fileio_info->param_3); + else if (strcmp(target->fileio_info->identifier, "lseek") == 0) + sprintf(fileio_command, "F%s,%x,%x,%x", target->fileio_info->identifier, + target->fileio_info->param_1, + target->fileio_info->param_2, + target->fileio_info->param_3); + else if (strcmp(target->fileio_info->identifier, "rename") == 0) + sprintf(fileio_command, "F%s,%x/%x,%x/%x", target->fileio_info->identifier, + target->fileio_info->param_1, + target->fileio_info->param_2, + target->fileio_info->param_3, + target->fileio_info->param_4); + else if (strcmp(target->fileio_info->identifier, "unlink") == 0) + sprintf(fileio_command, "F%s,%x/%x", target->fileio_info->identifier, + target->fileio_info->param_1, + target->fileio_info->param_2); + else if (strcmp(target->fileio_info->identifier, "stat") == 0) + sprintf(fileio_command, "F%s,%x/%x,%x", target->fileio_info->identifier, + target->fileio_info->param_1, + target->fileio_info->param_2, + target->fileio_info->param_3); + else if (strcmp(target->fileio_info->identifier, "fstat") == 0) + sprintf(fileio_command, "F%s,%x,%x", target->fileio_info->identifier, + target->fileio_info->param_1, + target->fileio_info->param_2); + else if (strcmp(target->fileio_info->identifier, "gettimeofday") == 0) + sprintf(fileio_command, "F%s,%x,%x", target->fileio_info->identifier, + target->fileio_info->param_1, + target->fileio_info->param_2); + else if (strcmp(target->fileio_info->identifier, "isatty") == 0) + sprintf(fileio_command, "F%s,%x", target->fileio_info->identifier, + target->fileio_info->param_1); + else if (strcmp(target->fileio_info->identifier, "system") == 0) + sprintf(fileio_command, "F%s,%x/%x", target->fileio_info->identifier, + target->fileio_info->param_1, + target->fileio_info->param_2); + else if (strcmp(target->fileio_info->identifier, "exit") == 0) { + /* If target hits exit syscall, report to GDB the program is terminated. + * In addition, let target run its own exit syscall handler. */ + program_exited = true; + sprintf(fileio_command, "W%02x", target->fileio_info->param_1); + } else { + LOG_DEBUG("Unknown syscall: %s", target->fileio_info->identifier); + + /* encounter unknown syscall, continue */ + gdb_connection->frontend_state = TARGET_RUNNING; + target_resume(target, 1, 0x0, 0, 0); + return; + } + + command_len = strlen(fileio_command); + gdb_put_packet(connection, fileio_command, command_len); + + if (program_exited) { + /* Use target_resume() to let target run its own exit syscall handler. */ + gdb_connection->frontend_state = TARGET_RUNNING; + target_resume(target, 1, 0x0, 0, 0); + } else { + gdb_connection->frontend_state = TARGET_HALTED; + rtos_update_threads(target); + } +} + static void gdb_frontend_halted(struct target *target, struct connection *connection) { struct gdb_connection *gdb_connection = connection->priv; @@ -702,22 +848,14 @@ static void gdb_frontend_halted(struct target *target, struct connection *connec * that are to be ignored. */ if (gdb_connection->frontend_state == TARGET_RUNNING) { - char sig_reply[4]; - int signal_var; - /* stop forwarding log packets! */ log_remove_callback(gdb_log_callback, connection); - if (gdb_connection->ctrl_c) { - signal_var = 0x2; - gdb_connection->ctrl_c = 0; - } else - signal_var = gdb_last_signal(target); - - snprintf(sig_reply, 4, "T%2.2x", signal_var); - gdb_put_packet(connection, sig_reply, 3); - gdb_connection->frontend_state = TARGET_HALTED; - rtos_update_threads(target); + /* check fileio first */ + if (target_get_gdb_fileio_info(target, target->fileio_info) == ERROR_OK) + gdb_fileio_reply(target, connection); + else + gdb_signal_reply(target, connection); } } @@ -726,8 +864,11 @@ static int gdb_target_callback_event_handler(struct target *target, { int retval; struct connection *connection = priv; + struct gdb_service *gdb_service = connection->service->priv; + + if (gdb_service->target != target) + return ERROR_OK; - target_handle_event(target, event); switch (event) { case TARGET_EVENT_GDB_HALT: gdb_frontend_halted(target, connection); @@ -903,7 +1044,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; @@ -932,22 +1073,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) @@ -959,8 +1088,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; @@ -986,7 +1118,8 @@ static int gdb_get_registers_packet(struct connection *connection, if ((target->rtos != NULL) && (ERROR_OK == rtos_get_gdb_reg_list(connection))) return ERROR_OK; - retval = target_get_gdb_reg_list(target, ®_list, ®_list_size); + retval = target_get_gdb_reg_list(target, ®_list, ®_list_size, + REG_CLASS_GENERAL); if (retval != ERROR_OK) return gdb_error(connection, retval); @@ -995,7 +1128,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++) { @@ -1045,7 +1178,8 @@ static int gdb_set_registers_packet(struct connection *connection, return ERROR_SERVER_REMOTE_CLOSED; } - retval = target_get_gdb_reg_list(target, ®_list, ®_list_size); + retval = target_get_gdb_reg_list(target, ®_list, ®_list_size, + REG_CLASS_GENERAL); if (retval != ERROR_OK) return gdb_error(connection, retval); @@ -1090,7 +1224,8 @@ static int gdb_get_register_packet(struct connection *connection, LOG_DEBUG("-"); #endif - retval = target_get_gdb_reg_list(target, ®_list, ®_list_size); + retval = target_get_gdb_reg_list(target, ®_list, ®_list_size, + REG_CLASS_ALL); if (retval != ERROR_OK) return gdb_error(connection, retval); @@ -1102,7 +1237,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]); @@ -1127,7 +1262,8 @@ static int gdb_set_register_packet(struct connection *connection, LOG_DEBUG("-"); - retval = target_get_gdb_reg_list(target, ®_list, ®_list_size); + retval = target_get_gdb_reg_list(target, ®_list, ®_list_size, + REG_CLASS_ALL); if (retval != ERROR_OK) return gdb_error(connection, retval); @@ -1231,14 +1367,9 @@ static int gdb_read_memory_packet(struct connection *connection, if (retval == ERROR_OK) { hex_buffer = malloc(len * 2 + 1); - uint32_t i; - for (i = 0; i < len; i++) { - uint8_t t = buffer[i]; - hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf]; - hex_buffer[2 * i + 1] = DIGITS[t & 0xf]; - } + int pkt_len = hexify(hex_buffer, (char *)buffer, len, len * 2 + 1); - gdb_put_packet(connection, hex_buffer, len * 2); + gdb_put_packet(connection, hex_buffer, pkt_len); free(hex_buffer); } else @@ -1258,8 +1389,6 @@ static int gdb_write_memory_packet(struct connection *connection, uint32_t len = 0; uint8_t *buffer; - - uint32_t i; int retval; /* skip command character */ @@ -1283,11 +1412,8 @@ static int gdb_write_memory_packet(struct connection *connection, LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len); - for (i = 0; i < len; i++) { - uint32_t tmp; - sscanf(separator + 2*i, "%2" SCNx32, &tmp); - buffer[i] = tmp; - } + if (unhexify((char *)buffer, separator, len) != (int)len) + LOG_ERROR("unable to decode memory packet"); retval = target_write_buffer(target, addr, len, buffer); @@ -1374,6 +1500,7 @@ static int gdb_step_continue_packet(struct connection *connection, } else current = 1; + gdb_running_type = packet[0]; if (packet[0] == 'c') { LOG_DEBUG("continue"); /* resume at current address, don't handle breakpoints, not debugging */ @@ -1698,6 +1825,331 @@ static int gdb_memory_map(struct connection *connection, return ERROR_OK; } +static const char *gdb_get_reg_type_name(enum reg_type type) +{ + switch (type) { + case REG_TYPE_INT8: + return "int8"; + case REG_TYPE_INT16: + return "int16"; + case REG_TYPE_INT32: + return "int32"; + case REG_TYPE_INT64: + return "int64"; + case REG_TYPE_INT128: + return "int128"; + case REG_TYPE_UINT8: + return "uint8"; + case REG_TYPE_UINT16: + return "uint16"; + case REG_TYPE_UINT32: + return "uint32"; + case REG_TYPE_UINT64: + return "uint64"; + case REG_TYPE_UINT128: + return "uint128"; + case REG_TYPE_CODE_PTR: + return "code_ptr"; + case REG_TYPE_DATA_PTR: + return "data_ptr"; + case REG_TYPE_IEEE_SINGLE: + return "ieee_single"; + case REG_TYPE_IEEE_DOUBLE: + return "ieee_double"; + case REG_TYPE_ARCH_DEFINED: + return "int"; /* return arbitrary string to avoid compile warning. */ + } + + return "int"; /* "int" as default value */ +} + +static int gdb_generate_reg_type_description(struct target *target, + char **tdesc, int *pos, int *size, struct reg_data_type *type) +{ + int retval = ERROR_OK; + + if (type->type_class == REG_TYPE_CLASS_VECTOR) { + /* */ + xml_printf(&retval, tdesc, pos, size, + "\n", + type->id, type->reg_type_vector->type->id, + type->reg_type_vector->count); + + } else if (type->type_class == REG_TYPE_CLASS_UNION) { + /* + * ... + * */ + xml_printf(&retval, tdesc, pos, size, + "\n", + type->id); + + struct reg_data_type_union_field *field; + field = type->reg_type_union->fields; + while (field != NULL) { + xml_printf(&retval, tdesc, pos, size, + "\n", + field->name, field->type->id); + + field = field->next; + } + + xml_printf(&retval, tdesc, pos, size, + "\n"); + + } else if (type->type_class == REG_TYPE_CLASS_STRUCT) { + struct reg_data_type_struct_field *field; + field = type->reg_type_struct->fields; + + if (field->use_bitfields) { + /* + * ... + * */ + xml_printf(&retval, tdesc, pos, size, + "\n", + type->id, type->reg_type_struct->size); + while (field != NULL) { + xml_printf(&retval, tdesc, pos, size, + "\n", + field->name, field->bitfield->start, + field->bitfield->end); + + field = field->next; + } + } else { + /* + * ... + * */ + xml_printf(&retval, tdesc, pos, size, + "\n", + type->id); + while (field != NULL) { + xml_printf(&retval, tdesc, pos, size, + "\n", + field->name, field->type->id); + + field = field->next; + } + } + + xml_printf(&retval, tdesc, pos, size, + "\n"); + + } else if (type->type_class == REG_TYPE_CLASS_FLAGS) { + /* + * ... + * */ + xml_printf(&retval, tdesc, pos, size, + "\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, + "\n", + field->name, field->bitfield->start, field->bitfield->end); + + field = field->next; + } + + xml_printf(&retval, tdesc, pos, size, + "\n"); + + } + + return ERROR_OK; +} + +/* Get a list of available target registers features. feature_list must + * be freed by caller. + */ +int get_reg_features_list(struct target *target, char **feature_list[], int *feature_list_size, + struct reg **reg_list, int reg_list_size) +{ + int tbl_sz = 0; + + /* Start with only one element */ + *feature_list = calloc(1, sizeof(char *)); + + for (int i = 0; i < reg_list_size; i++) { + if (reg_list[i]->exist == false) + continue; + + if ((reg_list[i]->feature->name != NULL) + && (strcmp(reg_list[i]->feature->name, ""))) { + /* We found a feature, check if the feature is already in the + * table. If not, allocate a new entry for the table and + * put the new feature in it. + */ + for (int j = 0; j < (tbl_sz + 1); j++) { + if (!((*feature_list)[j])) { + (*feature_list)[tbl_sz++] = strdup(reg_list[i]->feature->name); + *feature_list = realloc(*feature_list, sizeof(char *) * (tbl_sz + 1)); + (*feature_list)[tbl_sz] = NULL; + break; + } else { + if (!strcmp((*feature_list)[j], reg_list[i]->feature->name)) + break; + } + } + } + } + + if (feature_list_size) + *feature_list_size = tbl_sz; + + return ERROR_OK; +} + +static int gdb_generate_target_description(struct target *target, char **tdesc) +{ + int retval = ERROR_OK; + struct reg **reg_list; + int reg_list_size; + int pos = 0; + int size = 0; + + xml_printf(&retval, tdesc, &pos, &size, + "\n" + "\n" + "\n"); + + retval = target_get_gdb_reg_list(target, ®_list, + ®_list_size, REG_CLASS_ALL); + + if (retval != ERROR_OK) { + LOG_ERROR("get register list failed"); + return ERROR_FAIL; + } + + if (reg_list_size <= 0) + return ERROR_FAIL; + + char **features = NULL; + /* Get a list of available target registers features */ + retval = get_reg_features_list(target, &features, NULL, reg_list, reg_list_size); + if (retval != ERROR_OK) { + LOG_ERROR("Can't get the registers feature list"); + return ERROR_FAIL; + } + + /* If we found some features associated with registers, create sections */ + int current_feature = 0; + + /* generate target description according to register list */ + if (features != NULL) { + while (features[current_feature]) { + + xml_printf(&retval, tdesc, &pos, &size, + "\n", + features[current_feature]); + + int i; + for (i = 0; i < reg_list_size; i++) { + + if (reg_list[i]->exist == false) + continue; + + if (strcmp(reg_list[i]->feature->name, features[current_feature])) + continue; + + const char *type_str; + if (reg_list[i]->reg_data_type != NULL) { + if (reg_list[i]->reg_data_type->type == REG_TYPE_ARCH_DEFINED) { + /* generate reg_data_type); + + type_str = reg_list[i]->reg_data_type->id; + } else { + /* predefined type */ + type_str = gdb_get_reg_type_name( + reg_list[i]->reg_data_type->type); + } + } else { + /* Default type is "int" */ + type_str = "int"; + } + + xml_printf(&retval, tdesc, &pos, &size, + "name); + xml_printf(&retval, tdesc, &pos, &size, + " bitsize=\"%d\"", reg_list[i]->size); + xml_printf(&retval, tdesc, &pos, &size, + " regnum=\"%d\"", reg_list[i]->number); + if (reg_list[i]->caller_save) + xml_printf(&retval, tdesc, &pos, &size, + " save-restore=\"yes\""); + else + xml_printf(&retval, tdesc, &pos, &size, + " save-restore=\"no\""); + + xml_printf(&retval, tdesc, &pos, &size, + " type=\"%s\"", type_str); + + if (reg_list[i]->group != NULL) + xml_printf(&retval, tdesc, &pos, &size, + " group=\"%s\"", reg_list[i]->group); + + xml_printf(&retval, tdesc, &pos, &size, + "/>\n"); + } + + xml_printf(&retval, tdesc, &pos, &size, + "\n"); + + current_feature++; + } + } + + xml_printf(&retval, tdesc, &pos, &size, + "\n"); + + if (reg_list != NULL) + free(reg_list); + + if (features != NULL) + free(features); + + return ERROR_OK; +} + +static int gdb_get_target_description_chunk(struct target *target, char **chunk, + int32_t offset, uint32_t length) +{ + static char *tdesc; + static uint32_t tdesc_length; + + if (tdesc == NULL) { + gdb_generate_target_description(target, &tdesc); + tdesc_length = strlen(tdesc); + } + + char transfer_type; + + if (length < (tdesc_length - offset)) + transfer_type = 'm'; + else + transfer_type = 'l'; + + *chunk = malloc(length + 2); + (*chunk)[0] = transfer_type; + if (transfer_type == 'm') { + strncpy((*chunk) + 1, tdesc + offset, length); + (*chunk)[1 + length] = '\0'; + } else { + strncpy((*chunk) + 1, tdesc + offset, tdesc_length - offset); + (*chunk)[1 + (tdesc_length - offset)] = '\0'; + + /* After gdb-server sends out last chunk, invalidate tdesc. */ + free(tdesc); + tdesc = NULL; + tdesc_length = 0; + } + + return ERROR_OK; +} + static int gdb_query_packet(struct connection *connection, char *packet, int packet_size) { @@ -1705,17 +2157,12 @@ static int gdb_query_packet(struct connection *connection, struct gdb_connection *gdb_connection = connection->priv; struct target *target = get_target_from_connection(connection); - if (strstr(packet, "qRcmd,")) { + if (strncmp(packet, "qRcmd,", 6) == 0) { if (packet_size > 6) { char *cmd; - int i; - cmd = malloc((packet_size - 6)/2 + 1); - for (i = 0; i < (packet_size - 6)/2; i++) { - uint32_t tmp; - sscanf(packet + 6 + 2*i, "%2" SCNx32, &tmp); - cmd[i] = tmp; - } - cmd[(packet_size - 6)/2] = 0x0; + cmd = malloc((packet_size - 6) / 2 + 1); + int len = unhexify(cmd, packet + 6, (packet_size - 6) / 2); + cmd[len] = 0; /* We want to print all debug output to GDB connection */ log_add_callback(gdb_log_callback, connection); @@ -1731,7 +2178,7 @@ static int gdb_query_packet(struct connection *connection, } gdb_put_packet(connection, "OK", 2); return ERROR_OK; - } else if (strstr(packet, "qCRC:")) { + } else if (strncmp(packet, "qCRC:", 5) == 0) { if (packet_size > 5) { int retval; char gdb_reply[10]; @@ -1765,7 +2212,7 @@ static int gdb_query_packet(struct connection *connection, return ERROR_OK; } - } else if (strstr(packet, "qSupported")) { + } else if (strncmp(packet, "qSupported", 10) == 0) { /* we currently support packet size and qXfer:memory-map:read (if enabled) * disable qXfer:features:read for the moment */ int retval = ERROR_OK; @@ -1777,9 +2224,10 @@ static int gdb_query_packet(struct connection *connection, &buffer, &pos, &size, - "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+", + "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;QStartNoAckMode+", (GDB_BUFFER_SIZE - 1), - ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-'); + ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-', + (gdb_use_target_description == 1) ? '+' : '-'); if (retval != ERROR_OK) { gdb_send_error(connection, 01); @@ -1790,13 +2238,11 @@ static int gdb_query_packet(struct connection *connection, free(buffer); return ERROR_OK; - } else if (strstr(packet, "qXfer:memory-map:read::") + } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0) && (flash_get_bank_count() > 0)) return gdb_memory_map(connection, packet, packet_size); - else if (strstr(packet, "qXfer:features:read:")) { + else if (strncmp(packet, "qXfer:features:read:", 20) == 0) { char *xml = NULL; - int size = 0; - int pos = 0; int retval = ERROR_OK; int offset; @@ -1811,17 +2257,12 @@ static int gdb_query_packet(struct connection *connection, return ERROR_OK; } - if (strcmp(annex, "target.xml") != 0) { - gdb_send_error(connection, 01); - return ERROR_OK; - } - - xml_printf(&retval, - &xml, - &pos, - &size, \ - "l < target version=\"1.0\">\n < architecture > arm\n\n"); - + /* Target should prepare correct target description for annex. + * The first character of returned xml is 'm' or 'l'. 'm' for + * there are *more* chunks to transfer. 'l' for it is the *last* + * chunk of target description. + */ + retval = gdb_get_target_description_chunk(target, &xml, offset, length); if (retval != ERROR_OK) { gdb_error(connection, retval); return retval; @@ -1831,7 +2272,7 @@ static int gdb_query_packet(struct connection *connection, free(xml); return ERROR_OK; - } else if (strstr(packet, "QStartNoAckMode")) { + } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) { gdb_connection->noack_mode = 1; gdb_put_packet(connection, "OK", 2); return ERROR_OK; @@ -1855,7 +2296,7 @@ static int gdb_v_packet(struct connection *connection, return ERROR_OK; } - if (strstr(packet, "vFlashErase:")) { + if (strncmp(packet, "vFlashErase:", 12) == 0) { unsigned long addr; unsigned long length; @@ -1911,7 +2352,7 @@ static int gdb_v_packet(struct connection *connection, return ERROR_OK; } - if (strstr(packet, "vFlashWrite:")) { + if (strncmp(packet, "vFlashWrite:", 12) == 0) { int retval; unsigned long addr; unsigned long length; @@ -1945,7 +2386,7 @@ static int gdb_v_packet(struct connection *connection, return ERROR_OK; } - if (!strcmp(packet, "vFlashDone")) { + if (strncmp(packet, "vFlashDone", 10) == 0) { uint32_t written; /* process the flashing buffer. No need to erase as GDB @@ -1984,6 +2425,54 @@ static int gdb_detach(struct connection *connection) return gdb_put_packet(connection, "OK", 2); } +/* The format of 'F' response packet is + * Fretcode,errno,Ctrl-C flag;call-specific attachment + */ +static int gdb_fileio_response_packet(struct connection *connection, + char *packet, int packet_size) +{ + struct target *target = get_target_from_connection(connection); + char *separator; + char *parsing_point; + int fileio_retcode = strtoul(packet + 1, &separator, 16); + int fileio_errno = 0; + bool fileio_ctrl_c = false; + int retval; + + LOG_DEBUG("-"); + + if (*separator == ',') { + parsing_point = separator + 1; + fileio_errno = strtoul(parsing_point, &separator, 16); + if (*separator == ',') { + if (*(separator + 1) == 'C') { + /* TODO: process ctrl-c */ + fileio_ctrl_c = true; + } + } + } + + LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s", + fileio_retcode, fileio_errno, fileio_ctrl_c ? "true" : "false"); + + retval = target_gdb_fileio_end(target, fileio_retcode, fileio_errno, fileio_ctrl_c); + if (retval != ERROR_OK) + return ERROR_FAIL; + + /* After File-I/O ends, keep continue or step */ + if (gdb_running_type == 'c') + retval = target_resume(target, 1, 0x0, 0, 0); + else if (gdb_running_type == 's') + retval = target_step(target, 1, 0x0, 0); + else + retval = ERROR_FAIL; + + if (retval != ERROR_OK) + return ERROR_FAIL; + + return ERROR_OK; +} + static void gdb_log_callback(void *priv, const char *file, unsigned line, const char *function, const char *string) { @@ -2210,6 +2699,19 @@ static int gdb_input_inner(struct connection *connection) gdb_write_smp_packet(connection, packet, packet_size); break; + case 'F': + /* File-I/O extension */ + /* After gdb uses host-side syscall to complete target file + * I/O, gdb sends host-side syscall return value to target + * by 'F' packet. + * The format of 'F' response packet is + * Fretcode,errno,Ctrl-C flag;call-specific attachment + */ + gdb_con->frontend_state = TARGET_RUNNING; + log_add_callback(gdb_log_callback, connection); + gdb_fileio_response_packet(connection, packet, packet_size); + break; + default: /* ignore unknown packets */ LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]); @@ -2405,6 +2907,54 @@ COMMAND_HANDLER(handle_gdb_breakpoint_override_command) return ERROR_OK; } +COMMAND_HANDLER(handle_gdb_target_description_command) +{ + if (CMD_ARGC != 1) + return ERROR_COMMAND_SYNTAX_ERROR; + + COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_target_description); + return ERROR_OK; +} + +COMMAND_HANDLER(handle_gdb_save_tdesc_command) +{ + static char *tdesc; + static uint32_t tdesc_length; + struct target *target = get_current_target(CMD_CTX); + char *tdesc_filename; + + if (tdesc == NULL) { + gdb_generate_target_description(target, &tdesc); + tdesc_length = strlen(tdesc); + } + + struct fileio fileio; + size_t size_written; + + tdesc_filename = malloc(strlen(target_type_name(target)) + 5); + sprintf(tdesc_filename, "%s.xml", target_type_name(target)); + + int retval = fileio_open(&fileio, tdesc_filename, FILEIO_WRITE, FILEIO_TEXT); + + free(tdesc_filename); + + if (retval != ERROR_OK) { + LOG_WARNING("Can't open %s for writing", tdesc_filename); + return ERROR_FAIL; + } + + retval = fileio_write(&fileio, tdesc_length, tdesc, &size_written); + + fileio_close(&fileio); + + if (retval != ERROR_OK) { + LOG_WARNING("Error while writing the tdesc file"); + return ERROR_FAIL; + } + + return ERROR_OK; +} + static const struct command_registration gdb_command_handlers[] = { { .name = "gdb_sync", @@ -2457,6 +3007,19 @@ static const struct command_registration gdb_command_handlers[] = { "to be used by gdb 'break' commands.", .usage = "('hard'|'soft'|'disable')" }, + { + .name = "gdb_target_description", + .handler = handle_gdb_target_description_command, + .mode = COMMAND_CONFIG, + .help = "enable or disable target description", + .usage = "('enable'|'disable')" + }, + { + .name = "gdb_save_tdesc", + .handler = handle_gdb_save_tdesc_command, + .mode = COMMAND_EXEC, + .help = "Save the target description file", + }, COMMAND_REGISTRATION_DONE };