+static int rtos_put_gdb_reg_list(struct connection *connection,
+ struct rtos_reg *reg_list, int num_regs)
+{
+ size_t num_bytes = 1; /* NUL */
+ for (int i = 0; i < num_regs; ++i)
+ num_bytes += DIV_ROUND_UP(reg_list[i].size, 8) * 2;
+
+ char *hex = malloc(num_bytes);
+ char *hex_p = hex;
+
+ for (int i = 0; i < num_regs; ++i) {
+ size_t count = DIV_ROUND_UP(reg_list[i].size, 8);
+ size_t n = hexify(hex_p, reg_list[i].value, count, num_bytes);
+ hex_p += n;
+ num_bytes -= n;
+ }
+
+ gdb_put_packet(connection, hex, strlen(hex));
+ free(hex);
+
+ return ERROR_OK;
+}
+
+/** Look through all registers to find this register. */
+int rtos_get_gdb_reg(struct connection *connection, int reg_num)
+{
+ struct target *target = get_target_from_connection(connection);
+ int64_t current_threadid = target->rtos->current_threadid;
+ if ((target->rtos != NULL) && (current_threadid != -1) &&
+ (current_threadid != 0) &&
+ ((current_threadid != target->rtos->current_thread) ||
+ (target->smp))) { /* in smp several current thread are possible */
+ struct rtos_reg *reg_list;
+ int num_regs;
+
+ LOG_DEBUG("getting register %d for thread 0x%" PRIx64
+ ", target->rtos->current_thread=0x%" PRIx64,
+ reg_num,
+ current_threadid,
+ target->rtos->current_thread);
+
+ int retval;
+ if (target->rtos->type->get_thread_reg) {
+ reg_list = calloc(1, sizeof(*reg_list));
+ num_regs = 1;
+ retval = target->rtos->type->get_thread_reg(target->rtos,
+ current_threadid, reg_num, ®_list[0]);
+ if (retval != ERROR_OK) {
+ LOG_ERROR("RTOS: failed to get register %d", reg_num);
+ return retval;
+ }
+ } else {
+ retval = target->rtos->type->get_thread_reg_list(target->rtos,
+ current_threadid,
+ ®_list,
+ &num_regs);
+ if (retval != ERROR_OK) {
+ LOG_ERROR("RTOS: failed to get register list");
+ return retval;
+ }
+ }
+
+ for (int i = 0; i < num_regs; ++i) {
+ if (reg_list[i].number == (uint32_t)reg_num) {
+ rtos_put_gdb_reg_list(connection, reg_list + i, 1);
+ free(reg_list);
+ return ERROR_OK;
+ }
+ }
+
+ free(reg_list);
+ }
+ return ERROR_FAIL;
+}
+
+/** Return a list of general registers. */