+ pid = gdb_ctx->params[0].val_ul;
+ }
+
+ process = gdb_get_process(s, pid);
+ gdb_process_breakpoint_remove_all(s, process);
+ process->attached = false;
+
+ if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
+ s->c_cpu = gdb_first_attached_cpu(s);
+ }
+
+ if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
+ s->g_cpu = gdb_first_attached_cpu(s);
+ }
+
+ if (!s->c_cpu) {
+ /* No more process attached */
+ gdb_syscall_mode = GDB_SYS_DISABLED;
+ gdb_continue(s);
+ }
+ put_packet(s, "OK");
+}
+
+static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ CPUState *cpu;
+
+ if (!gdb_ctx->num_params) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
+ gdb_ctx->params[0].thread_id.tid);
+ if (!cpu) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ put_packet(gdb_ctx->s, "OK");
+}
+
+static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ if (gdb_ctx->num_params) {
+ gdb_set_cpu_pc(gdb_ctx->s, gdb_ctx->params[0].val_ull);
+ }
+
+ gdb_ctx->s->signal = 0;
+ gdb_continue(gdb_ctx->s);
+}
+
+static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ unsigned long signal = 0;
+
+ /*
+ * Note: C sig;[addr] is currently unsupported and we simply
+ * omit the addr parameter
+ */
+ if (gdb_ctx->num_params) {
+ signal = gdb_ctx->params[0].val_ul;
+ }
+
+ gdb_ctx->s->signal = gdb_signal_to_target(signal);
+ if (gdb_ctx->s->signal == -1) {
+ gdb_ctx->s->signal = 0;
+ }
+ gdb_continue(gdb_ctx->s);
+}
+
+static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ CPUState *cpu;
+
+ if (gdb_ctx->num_params != 2) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
+ put_packet(gdb_ctx->s, "OK");
+ return;
+ }
+
+ cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[1].thread_id.pid,
+ gdb_ctx->params[1].thread_id.tid);
+ if (!cpu) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ /*
+ * Note: This command is deprecated and modern gdb's will be using the
+ * vCont command instead.
+ */
+ switch (gdb_ctx->params[0].opcode) {
+ case 'c':
+ gdb_ctx->s->c_cpu = cpu;
+ put_packet(gdb_ctx->s, "OK");
+ break;
+ case 'g':
+ gdb_ctx->s->g_cpu = cpu;
+ put_packet(gdb_ctx->s, "OK");
+ break;
+ default:
+ put_packet(gdb_ctx->s, "E22");
+ break;
+ }
+}
+
+static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ int res;
+
+ if (gdb_ctx->num_params != 3) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
+ gdb_ctx->params[1].val_ull,
+ gdb_ctx->params[2].val_ull);
+ if (res >= 0) {
+ put_packet(gdb_ctx->s, "OK");
+ return;
+ } else if (res == -ENOSYS) {
+ put_packet(gdb_ctx->s, "");
+ return;
+ }
+
+ put_packet(gdb_ctx->s, "E22");
+}
+
+static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ int res;
+
+ if (gdb_ctx->num_params != 3) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
+ gdb_ctx->params[1].val_ull,
+ gdb_ctx->params[2].val_ull);
+ if (res >= 0) {
+ put_packet(gdb_ctx->s, "OK");
+ return;
+ } else if (res == -ENOSYS) {
+ put_packet(gdb_ctx->s, "");
+ return;
+ }
+
+ put_packet(gdb_ctx->s, "E22");
+}
+
+static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ int reg_size;
+
+ if (!gdb_has_xml) {
+ put_packet(gdb_ctx->s, "E00");
+ return;
+ }
+
+ if (gdb_ctx->num_params != 2) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ reg_size = strlen(gdb_ctx->params[1].data) / 2;
+ hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size);
+ gdb_write_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
+ gdb_ctx->params[0].val_ull);
+ put_packet(gdb_ctx->s, "OK");
+}
+
+static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ int reg_size;
+
+ /*
+ * Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
+ * This works, but can be very slow. Anything new enough to
+ * understand XML also knows how to use this properly.
+ */
+ if (!gdb_has_xml) {
+ put_packet(gdb_ctx->s, "");
+ return;
+ }
+
+ if (!gdb_ctx->num_params) {
+ put_packet(gdb_ctx->s, "E14");
+ return;
+ }
+
+ reg_size = gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
+ gdb_ctx->params[0].val_ull);
+ if (!reg_size) {
+ put_packet(gdb_ctx->s, "E14");
+ return;
+ }
+
+ memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, reg_size);
+ put_packet(gdb_ctx->s, gdb_ctx->str_buf);
+}
+
+static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ if (gdb_ctx->num_params != 3) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ /* hextomem() reads 2*len bytes */
+ if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data,
+ gdb_ctx->params[1].val_ull);
+ if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
+ gdb_ctx->mem_buf,
+ gdb_ctx->params[1].val_ull, true)) {
+ put_packet(gdb_ctx->s, "E14");
+ return;
+ }
+
+ put_packet(gdb_ctx->s, "OK");
+}
+
+static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ if (gdb_ctx->num_params != 2) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ /* memtohex() doubles the required space */
+ if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) {
+ put_packet(gdb_ctx->s, "E22");
+ return;
+ }
+
+ if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
+ gdb_ctx->mem_buf,
+ gdb_ctx->params[1].val_ull, false)) {
+ put_packet(gdb_ctx->s, "E14");
+ return;
+ }
+
+ memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1].val_ull);
+ put_packet(gdb_ctx->s, gdb_ctx->str_buf);
+}
+
+static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ target_ulong addr, len;
+ uint8_t *registers;
+ int reg_size;
+
+ if (!gdb_ctx->num_params) {
+ return;
+ }
+
+ cpu_synchronize_state(gdb_ctx->s->g_cpu);
+ registers = gdb_ctx->mem_buf;
+ len = strlen(gdb_ctx->params[0].data) / 2;
+ hextomem(registers, gdb_ctx->params[0].data, len);
+ for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs && len > 0;
+ addr++) {
+ reg_size = gdb_write_register(gdb_ctx->s->g_cpu, registers, addr);
+ len -= reg_size;
+ registers += reg_size;
+ }
+ put_packet(gdb_ctx->s, "OK");
+}
+
+static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ target_ulong addr, len;
+
+ cpu_synchronize_state(gdb_ctx->s->g_cpu);
+ len = 0;
+ for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs; addr++) {
+ len += gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf + len,
+ addr);
+ }
+
+ memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
+ put_packet(gdb_ctx->s, gdb_ctx->str_buf);
+}
+
+static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ if (gdb_ctx->num_params >= 2 && gdb_ctx->s->current_syscall_cb) {
+ target_ulong ret, err;
+
+ ret = (target_ulong)gdb_ctx->params[0].val_ull;
+ err = (target_ulong)gdb_ctx->params[1].val_ull;
+ gdb_ctx->s->current_syscall_cb(gdb_ctx->s->c_cpu, ret, err);
+ gdb_ctx->s->current_syscall_cb = NULL;
+ }
+
+ if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') {
+ put_packet(gdb_ctx->s, "T02");
+ return;
+ }
+
+ gdb_continue(gdb_ctx->s);
+}
+
+static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ if (gdb_ctx->num_params) {
+ gdb_set_cpu_pc(gdb_ctx->s, (target_ulong)gdb_ctx->params[0].val_ull);
+ }
+
+ cpu_single_step(gdb_ctx->s->c_cpu, sstep_flags);
+ gdb_continue(gdb_ctx->s);
+}
+
+static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ put_packet(gdb_ctx->s, "vCont;c;C;s;S");
+}
+
+static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ int res;
+
+ if (!gdb_ctx->num_params) {
+ return;
+ }
+
+ res = gdb_handle_vcont(gdb_ctx->s, gdb_ctx->params[0].data);
+ if ((res == -EINVAL) || (res == -ERANGE)) {
+ put_packet(gdb_ctx->s, "E22");
+ } else if (res) {
+ put_packet(gdb_ctx->s, "");
+ }
+}
+
+static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ GDBProcess *process;
+ CPUState *cpu;
+ char thread_id[16];
+
+ pstrcpy(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "E22");
+ if (!gdb_ctx->num_params) {
+ goto cleanup;
+ }
+
+ process = gdb_get_process(gdb_ctx->s, gdb_ctx->params[0].val_ul);
+ if (!process) {
+ goto cleanup;
+ }
+
+ cpu = get_first_cpu_in_process(gdb_ctx->s, process);
+ if (!cpu) {
+ goto cleanup;
+ }
+
+ process->attached = true;
+ gdb_ctx->s->g_cpu = cpu;
+ gdb_ctx->s->c_cpu = cpu;
+
+ gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
+ snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
+ GDB_SIGNAL_TRAP, thread_id);
+cleanup:
+ put_packet(gdb_ctx->s, gdb_ctx->str_buf);
+}
+
+static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ /* Kill the target */
+ put_packet(gdb_ctx->s, "OK");
+ error_report("QEMU: Terminated via GDBstub");
+ exit(0);
+}
+
+static GdbCmdParseEntry gdb_v_commands_table[] = {
+ /* Order is important if has same prefix */
+ {
+ .handler = handle_v_cont_query,
+ .cmd = "Cont?",
+ .cmd_startswith = 1
+ },
+ {
+ .handler = handle_v_cont,
+ .cmd = "Cont",
+ .cmd_startswith = 1,
+ .schema = "s0"
+ },
+ {
+ .handler = handle_v_attach,
+ .cmd = "Attach;",
+ .cmd_startswith = 1,
+ .schema = "l0"
+ },
+ {
+ .handler = handle_v_kill,
+ .cmd = "Kill;",
+ .cmd_startswith = 1
+ },
+};
+
+static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ if (!gdb_ctx->num_params) {
+ return;
+ }
+
+ if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
+ gdb_v_commands_table,
+ ARRAY_SIZE(gdb_v_commands_table))) {
+ put_packet(gdb_ctx->s, "");
+ }
+}
+
+static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
+ "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE,
+ SSTEP_NOIRQ, SSTEP_NOTIMER);
+ put_packet(gdb_ctx->s, gdb_ctx->str_buf);
+}
+
+static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ if (!gdb_ctx->num_params) {
+ return;
+ }
+
+ sstep_flags = gdb_ctx->params[0].val_ul;
+ put_packet(gdb_ctx->s, "OK");
+}
+
+static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "0x%x", sstep_flags);
+ put_packet(gdb_ctx->s, gdb_ctx->str_buf);
+}
+
+static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ CPUState *cpu;
+ GDBProcess *process;
+ char thread_id[16];
+
+ /*
+ * "Current thread" remains vague in the spec, so always return
+ * the first thread of the current process (gdb returns the
+ * first thread).
+ */
+ process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
+ cpu = get_first_cpu_in_process(gdb_ctx->s, process);
+ gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
+ snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "QC%s", thread_id);
+ put_packet(gdb_ctx->s, gdb_ctx->str_buf);
+}
+
+static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ char thread_id[16];
+
+ if (!gdb_ctx->s->query_cpu) {
+ put_packet(gdb_ctx->s, "l");
+ return;
+ }
+
+ gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->query_cpu, thread_id,
+ sizeof(thread_id));
+ snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "m%s", thread_id);
+ put_packet(gdb_ctx->s, gdb_ctx->str_buf);
+ gdb_ctx->s->query_cpu =
+ gdb_next_attached_cpu(gdb_ctx->s, gdb_ctx->s->query_cpu);
+}
+
+static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ gdb_ctx->s->query_cpu = gdb_first_attached_cpu(gdb_ctx->s);
+ handle_query_threads(gdb_ctx, user_ctx);
+}
+
+static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+ CPUState *cpu;
+ int len;