+}
+
+static int gdb_memory_map(struct connection *connection,
+ char const *packet, int packet_size)
+{
+ /* We get away with only specifying flash here. Regions that are not
+ * specified are treated as if we provided no memory map(if not we
+ * could detect the holes and mark them as RAM).
+ * Normally we only execute this code once, but no big deal if we
+ * have to regenerate it a couple of times.
+ */
+
+ struct target *target = get_target_from_connection(connection);
+ struct flash_bank *p;
+ char *xml = NULL;
+ int size = 0;
+ int pos = 0;
+ int retval = ERROR_OK;
+ struct flash_bank **banks;
+ int offset;
+ int length;
+ char *separator;
+ uint32_t ram_start = 0;
+ int i;
+ int target_flash_banks = 0;
+
+ /* skip command character */
+ packet += 23;
+
+ offset = strtoul(packet, &separator, 16);
+ length = strtoul(separator + 1, &separator, 16);
+
+ xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
+
+ /* Sort banks in ascending order. We need to report non-flash
+ * memory as ram (or rather read/write) by default for GDB, since
+ * it has no concept of non-cacheable read/write memory (i/o etc).
+ *
+ * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
+ * Current versions of GDB assume unlisted addresses are RAM...
+ */
+ banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
+
+ for (i = 0; i < flash_get_bank_count(); i++) {
+ p = get_flash_bank_by_num_noprobe(i);
+ if (p->target != target)
+ continue;
+ retval = get_flash_bank_by_num(i, &p);
+ if (retval != ERROR_OK) {
+ free(banks);
+ gdb_error(connection, retval);
+ return retval;
+ }
+ banks[target_flash_banks++] = p;
+ }
+
+ qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
+ compare_bank);
+
+ for (i = 0; i < target_flash_banks; i++) {
+ int j;
+ unsigned sector_size = 0;
+ uint32_t start;
+
+ p = banks[i];
+ start = p->base;
+
+ if (ram_start < p->base)
+ xml_printf(&retval, &xml, &pos, &size,
+ "<memory type=\"ram\" start=\"0x%x\" "
+ "length=\"0x%x\"/>\n",
+ ram_start, p->base - ram_start);
+
+ /* Report adjacent groups of same-size sectors. So for
+ * example top boot CFI flash will list an initial region
+ * with several large sectors (maybe 128KB) and several
+ * 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++) {
+ unsigned group_len;
+
+ /* Maybe start a new group of sectors. */
+ if (sector_size == 0) {
+ start = p->base + p->sectors[j].offset;
+ xml_printf(&retval, &xml, &pos, &size,
+ "<memory type=\"flash\" "
+ "start=\"0x%x\" ",
+ start);
+ sector_size = p->sectors[j].size;
+ }
+
+ /* Does this finish a group of sectors?
+ * If not, continue an already-started group.
+ */
+ if (j == p->num_sectors - 1)
+ group_len = (p->base + p->size) - start;
+ else if (p->sectors[j + 1].size != sector_size)
+ group_len = p->base + p->sectors[j + 1].offset
+ - start;
+ else
+ continue;
+
+ xml_printf(&retval, &xml, &pos, &size,
+ "length=\"0x%x\">\n"
+ "<property name=\"blocksize\">"
+ "0x%x</property>\n"
+ "</memory>\n",
+ group_len,
+ sector_size);
+ sector_size = 0;
+ }
+
+ ram_start = p->base + p->size;
+ }
+
+ if (ram_start != 0)
+ xml_printf(&retval, &xml, &pos, &size,
+ "<memory type=\"ram\" start=\"0x%x\" "
+ "length=\"0x%x\"/>\n",
+ ram_start, 0-ram_start);
+ /* ELSE a flash chip could be at the very end of the 32 bit address
+ * space, in which case ram_start will be precisely 0
+ */
+
+ free(banks);
+ banks = NULL;
+
+ xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
+
+ if (retval != ERROR_OK) {
+ gdb_error(connection, retval);
+ return retval;
+ }
+
+ if (offset + length > pos)
+ length = pos - offset;
+
+ char *t = malloc(length + 1);
+ t[0] = 'l';
+ memcpy(t + 1, xml + offset, length);
+ gdb_put_packet(connection, t, length + 1);
+
+ free(t);
+ free(xml);
+ return ERROR_OK;
+}
+
+static const char *gdb_get_reg_type_name(enum reg_type type)
+{
+ switch (type) {
+ case REG_TYPE_INT:
+ return "int";
+ 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_FLOAT:
+ return "float";
+ 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) {
+ /* <vector id="id" type="type" count="count"/> */
+ xml_printf(&retval, tdesc, pos, size,
+ "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
+ type->id, type->reg_type_vector->type->id,
+ type->reg_type_vector->count);
+
+ } else if (type->type_class == REG_TYPE_CLASS_UNION) {
+ /* <union id="id">
+ * <field name="name" type="type"/> ...
+ * </union> */
+ xml_printf(&retval, tdesc, pos, size,
+ "<union id=\"%s\">\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,
+ "<field name=\"%s\" type=\"%s\"/>\n",
+ field->name, field->type->id);
+
+ field = field->next;
+ }
+
+ xml_printf(&retval, tdesc, pos, size,
+ "</union>\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) {
+ /* <struct id="id" size="size">
+ * <field name="name" start="start" end="end"/> ...
+ * </struct> */
+ xml_printf(&retval, tdesc, pos, size,
+ "<struct id=\"%s\" size=\"%d\">\n",
+ type->id, type->reg_type_struct->size);
+ while (field != NULL) {
+ xml_printf(&retval, tdesc, pos, size,
+ "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
+ field->name, field->bitfield->start,
+ field->bitfield->end);
+
+ field = field->next;
+ }
+ } else {
+ /* <struct id="id">
+ * <field name="name" type="type"/> ...
+ * </struct> */
+ xml_printf(&retval, tdesc, pos, size,
+ "<struct id=\"%s\">\n",
+ type->id);
+ while (field != NULL) {
+ xml_printf(&retval, tdesc, pos, size,
+ "<field name=\"%s\" type=\"%s\"/>\n",
+ field->name, field->type->id);
+
+ field = field->next;
+ }
+ }
+
+ xml_printf(&retval, tdesc, pos, size,
+ "</struct>\n");
+
+ } else if (type->type_class == REG_TYPE_CLASS_FLAGS) {
+ /* <flags id="id" size="size">
+ * <field name="name" start="start" end="end"/> ...
+ * </flags> */
+ xml_printf(&retval, tdesc, pos, size,
+ "<flags id=\"%s\" size=\"%d\">\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\"/>\n",
+ field->name, field->bitfield->start, field->bitfield->end);
+
+ field = field->next;
+ }
+
+ xml_printf(&retval, tdesc, pos, size,
+ "</flags>\n");
+
+ }
+
+ return ERROR_OK;
+}
+
+/* Get a list of available target registers features. feature_list must
+ * be freed by caller.
+ */
+static int get_reg_features_list(struct target *target, char const **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 != NULL
+ && 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++] = 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_out)
+{
+ int retval = ERROR_OK;
+ struct reg **reg_list = NULL;
+ int reg_list_size;
+ char const **features = NULL;
+ int feature_list_size = 0;
+ char *tdesc = NULL;
+ int pos = 0;
+ int size = 0;
+
+ retval = target_get_gdb_reg_list(target, ®_list,
+ ®_list_size, REG_CLASS_ALL);
+
+ if (retval != ERROR_OK) {
+ LOG_ERROR("get register list failed");
+ retval = ERROR_FAIL;
+ goto error;
+ }
+
+ if (reg_list_size <= 0) {
+ LOG_ERROR("get register list failed");
+ retval = ERROR_FAIL;
+ goto error;
+ }
+
+ /* Get a list of available target registers features */
+ retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
+ if (retval != ERROR_OK) {
+ LOG_ERROR("Can't get the registers feature list");
+ retval = ERROR_FAIL;
+ goto error;
+ }
+
+ /* If we found some features associated with registers, create sections */
+ int current_feature = 0;
+
+ xml_printf(&retval, &tdesc, &pos, &size,
+ "<?xml version=\"1.0\"?>\n"
+ "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
+ "<target version=\"1.0\">\n");
+
+ /* generate target description according to register list */
+ if (features != NULL) {
+ while (features[current_feature]) {
+
+ xml_printf(&retval, &tdesc, &pos, &size,
+ "<feature name=\"%s\">\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 <type... first, if there are architecture-defined types. */
+ gdb_generate_reg_type_description(target, &tdesc, &pos, &size,
+ reg_list[i]->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,
+ "<reg name=\"%s\"", reg_list[i]->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,
+ "</feature>\n");
+
+ current_feature++;
+ }
+ }
+
+ xml_printf(&retval, &tdesc, &pos, &size,
+ "</target>\n");
+
+error:
+ free(features);
+ free(reg_list);
+
+ if (retval == ERROR_OK)
+ *tdesc_out = tdesc;
+ else
+ free(tdesc);
+
+ return retval;
+}
+
+static int gdb_get_target_description_chunk(struct target *target, struct target_desc_format *target_desc,
+ char **chunk, int32_t offset, uint32_t length)
+{
+ if (target_desc == NULL) {
+ LOG_ERROR("Unable to Generate Target Description");
+ return ERROR_FAIL;
+ }
+
+ char *tdesc = target_desc->tdesc;
+ uint32_t tdesc_length = target_desc->tdesc_length;
+
+ if (tdesc == NULL) {
+ int retval = gdb_generate_target_description(target, &tdesc);
+ if (retval != ERROR_OK) {
+ LOG_ERROR("Unable to Generate Target Description");
+ return ERROR_FAIL;
+ }
+
+ tdesc_length = strlen(tdesc);
+ }
+
+ char transfer_type;
+
+ if (length < (tdesc_length - offset))
+ transfer_type = 'm';
+ else
+ transfer_type = 'l';
+
+ *chunk = malloc(length + 2);
+ if (*chunk == NULL) {
+ LOG_ERROR("Unable to allocate memory");
+ return ERROR_FAIL;
+ }
+
+ (*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;
+ }
+
+ target_desc->tdesc = tdesc;
+ target_desc->tdesc_length = tdesc_length;
+
+ return ERROR_OK;
+}
+
+static int gdb_target_description_supported(struct target *target, int *supported)
+{
+ int retval = ERROR_OK;
+ struct reg **reg_list = NULL;
+ int reg_list_size = 0;
+ char const **features = NULL;
+ int feature_list_size = 0;
+
+ retval = target_get_gdb_reg_list(target, ®_list,
+ ®_list_size, REG_CLASS_ALL);
+ if (retval != ERROR_OK) {
+ LOG_ERROR("get register list failed");
+ goto error;
+ }
+
+ if (reg_list_size <= 0) {
+ LOG_ERROR("get register list failed");
+ retval = ERROR_FAIL;
+ goto error;
+ }
+
+ /* Get a list of available target registers features */
+ retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
+ if (retval != ERROR_OK) {
+ LOG_ERROR("Can't get the registers feature list");
+ goto error;
+ }
+
+ if (supported) {
+ if (feature_list_size)
+ *supported = 1;
+ else
+ *supported = 0;
+ }
+
+error:
+ free(features);
+
+ free(reg_list);
+
+ return retval;
+}
+
+static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
+{
+ struct rtos *rtos = target->rtos;
+ int retval = ERROR_OK;
+ char *thread_list = NULL;
+ int pos = 0;
+ int size = 0;
+
+ xml_printf(&retval, &thread_list, &pos, &size,
+ "<?xml version=\"1.0\"?>\n"
+ "<threads>\n");
+
+ if (rtos != NULL) {
+ for (int i = 0; i < rtos->thread_count; i++) {
+ struct thread_detail *thread_detail = &rtos->thread_details[i];
+
+ if (!thread_detail->exists)
+ continue;
+
+ xml_printf(&retval, &thread_list, &pos, &size,
+ "<thread id=\"%" PRIx64 "\">", thread_detail->threadid);
+
+ if (thread_detail->thread_name_str != NULL)
+ xml_printf(&retval, &thread_list, &pos, &size,
+ "Name: %s", thread_detail->thread_name_str);
+
+ if (thread_detail->extra_info_str != NULL) {
+ if (thread_detail->thread_name_str != NULL)
+ xml_printf(&retval, &thread_list, &pos, &size,
+ ", ");
+ xml_printf(&retval, &thread_list, &pos, &size,
+ thread_detail->extra_info_str);
+ }
+
+ xml_printf(&retval, &thread_list, &pos, &size,
+ "</thread>\n");
+ }
+ }
+
+ xml_printf(&retval, &thread_list, &pos, &size,
+ "</threads>\n");
+
+ if (retval == ERROR_OK)
+ *thread_list_out = thread_list;
+ else
+ free(thread_list);
+
+ return retval;
+}
+
+static int gdb_get_thread_list_chunk(struct target *target, char **thread_list,
+ char **chunk, int32_t offset, uint32_t length)
+{
+ if (*thread_list == NULL) {
+ int retval = gdb_generate_thread_list(target, thread_list);
+ if (retval != ERROR_OK) {
+ LOG_ERROR("Unable to Generate Thread List");
+ return ERROR_FAIL;
+ }
+ }
+
+ size_t thread_list_length = strlen(*thread_list);
+ char transfer_type;
+
+ length = MIN(length, thread_list_length - offset);
+ if (length < (thread_list_length - offset))
+ transfer_type = 'm';
+ else
+ transfer_type = 'l';
+
+ *chunk = malloc(length + 2);
+ if (*chunk == NULL) {
+ LOG_ERROR("Unable to allocate memory");
+ return ERROR_FAIL;
+ }
+
+ (*chunk)[0] = transfer_type;
+ strncpy((*chunk) + 1, (*thread_list) + offset, length);
+ (*chunk)[1 + length] = '\0';
+
+ /* After gdb-server sends out last chunk, invalidate thread list. */
+ if (transfer_type == 'l') {
+ free(*thread_list);
+ *thread_list = NULL;