bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * gdb server stub |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
Alex Bennée | 42a0959 | 2019-07-05 13:28:19 +0100 | [diff] [blame] | 4 | * This implements a subset of the remote protocol as described in: |
| 5 | * |
| 6 | * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html |
| 7 | * |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 8 | * Copyright (c) 2003-2005 Fabrice Bellard |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Lesser General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 21 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
Alex Bennée | 42a0959 | 2019-07-05 13:28:19 +0100 | [diff] [blame] | 22 | * |
| 23 | * SPDX-License-Identifier: LGPL-2.0+ |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 24 | */ |
Markus Armbruster | 856dfd8 | 2019-05-23 16:35:06 +0200 | [diff] [blame] | 25 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Markus Armbruster | 856dfd8 | 2019-05-23 16:35:06 +0200 | [diff] [blame] | 27 | #include "qemu/ctype.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 28 | #include "qemu/cutils.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 29 | #include "qemu/module.h" |
Alex Bennée | 842b42d | 2022-09-29 12:42:22 +0100 | [diff] [blame] | 30 | #include "trace.h" |
Peter Maydell | 85b4fa0 | 2021-09-08 16:44:04 +0100 | [diff] [blame] | 31 | #include "exec/gdbstub.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_USER_ONLY |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 33 | #include "gdbstub/user.h" |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 34 | #else |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 35 | #include "hw/cpu/cluster.h" |
Like Xu | 5cc8767 | 2019-05-19 04:54:21 +0800 | [diff] [blame] | 36 | #include "hw/boards.h" |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 37 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 38 | |
Vincent Palatin | b394662 | 2017-01-10 11:59:55 +0100 | [diff] [blame] | 39 | #include "sysemu/hw_accel.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 40 | #include "sysemu/runstate.h" |
Philippe Mathieu-Daudé | 6b5fe13 | 2021-03-05 13:54:49 +0000 | [diff] [blame] | 41 | #include "semihosting/semihost.h" |
Paolo Bonzini | 63c9155 | 2016-03-15 13:18:37 +0100 | [diff] [blame] | 42 | #include "exec/exec-all.h" |
Philippe Mathieu-Daudé | 5b5968c | 2022-12-19 18:09:43 +0100 | [diff] [blame] | 43 | #include "exec/replay-core.h" |
Alex Bennée | 548c960 | 2023-03-02 18:57:43 -0800 | [diff] [blame] | 44 | #include "exec/tb-flush.h" |
| 45 | #include "exec/hwaddr.h" |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 46 | |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 47 | #include "internals.h" |
| 48 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 49 | typedef struct GDBRegisterState { |
| 50 | int base_reg; |
| 51 | int num_regs; |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 52 | gdb_get_reg_cb get_reg; |
| 53 | gdb_set_reg_cb set_reg; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 54 | const char *xml; |
| 55 | struct GDBRegisterState *next; |
| 56 | } GDBRegisterState; |
| 57 | |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 58 | GDBState gdbserver_state; |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 59 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 60 | void gdb_init_gdbserver_state(void) |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 61 | { |
| 62 | g_assert(!gdbserver_state.init); |
| 63 | memset(&gdbserver_state, 0, sizeof(GDBState)); |
| 64 | gdbserver_state.init = true; |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 65 | gdbserver_state.str_buf = g_string_new(NULL); |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 66 | gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH); |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 67 | gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4); |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 68 | |
| 69 | /* |
Alex Bennée | 3b7a938 | 2022-09-29 12:42:23 +0100 | [diff] [blame] | 70 | * What single-step modes are supported is accelerator dependent. |
| 71 | * By default try to use no IRQs and no timers while single |
| 72 | * stepping so as to make single stepping like a typical ICE HW step. |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 73 | */ |
Alex Bennée | 3b7a938 | 2022-09-29 12:42:23 +0100 | [diff] [blame] | 74 | gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags(); |
Maxim Levitsky | 12bc5b4 | 2021-11-11 12:06:03 +0100 | [diff] [blame] | 75 | gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER; |
| 76 | gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags; |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 79 | bool gdb_has_xml; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 80 | |
Peter Maydell | ebf1b4c | 2022-06-10 14:32:36 +0100 | [diff] [blame] | 81 | /* |
| 82 | * Return true if there is a GDB currently connected to the stub |
| 83 | * and attached to a CPU |
| 84 | */ |
| 85 | static bool gdb_attached(void) |
| 86 | { |
| 87 | return gdbserver_state.init && gdbserver_state.c_cpu; |
| 88 | } |
| 89 | |
blueswir1 | 654efcf | 2009-04-18 07:29:59 +0000 | [diff] [blame] | 90 | static enum { |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 91 | GDB_SYS_UNKNOWN, |
| 92 | GDB_SYS_ENABLED, |
| 93 | GDB_SYS_DISABLED, |
| 94 | } gdb_syscall_mode; |
| 95 | |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 96 | /* Decide if either remote gdb syscalls or native file IO should be used. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 97 | int use_gdb_syscalls(void) |
| 98 | { |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 99 | SemihostingTarget target = semihosting_get_target(); |
| 100 | if (target == SEMIHOSTING_TARGET_NATIVE) { |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 101 | /* -semihosting-config target=native */ |
| 102 | return false; |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 103 | } else if (target == SEMIHOSTING_TARGET_GDB) { |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 104 | /* -semihosting-config target=gdb */ |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | /* -semihosting-config target=auto */ |
| 109 | /* On the first call check if gdb is connected and remember. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 110 | if (gdb_syscall_mode == GDB_SYS_UNKNOWN) { |
Peter Maydell | ebf1b4c | 2022-06-10 14:32:36 +0100 | [diff] [blame] | 111 | gdb_syscall_mode = gdb_attached() ? GDB_SYS_ENABLED : GDB_SYS_DISABLED; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 112 | } |
| 113 | return gdb_syscall_mode == GDB_SYS_ENABLED; |
| 114 | } |
| 115 | |
Philippe Mathieu-Daudé | 9005774 | 2018-04-08 11:59:33 -0300 | [diff] [blame] | 116 | /* writes 2*len+1 bytes in buf */ |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 117 | void gdb_memtohex(GString *buf, const uint8_t *mem, int len) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 118 | { |
| 119 | int i, c; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 120 | for(i = 0; i < len; i++) { |
| 121 | c = mem[i]; |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 122 | g_string_append_c(buf, tohex(c >> 4)); |
| 123 | g_string_append_c(buf, tohex(c & 0xf)); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 124 | } |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 125 | g_string_append_c(buf, '\0'); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 128 | void gdb_hextomem(GByteArray *mem, const char *buf, int len) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 129 | { |
| 130 | int i; |
| 131 | |
| 132 | for(i = 0; i < len; i++) { |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 133 | guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]); |
| 134 | g_byte_array_append(mem, &byte, 1); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 135 | buf += 2; |
| 136 | } |
| 137 | } |
| 138 | |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 139 | static void hexdump(const char *buf, int len, |
| 140 | void (*trace_fn)(size_t ofs, char const *text)) |
| 141 | { |
| 142 | char line_buffer[3 * 16 + 4 + 16 + 1]; |
| 143 | |
| 144 | size_t i; |
| 145 | for (i = 0; i < len || (i & 0xF); ++i) { |
| 146 | size_t byte_ofs = i & 15; |
| 147 | |
| 148 | if (byte_ofs == 0) { |
| 149 | memset(line_buffer, ' ', 3 * 16 + 4 + 16); |
| 150 | line_buffer[3 * 16 + 4 + 16] = 0; |
| 151 | } |
| 152 | |
| 153 | size_t col_group = (i >> 2) & 3; |
| 154 | size_t hex_col = byte_ofs * 3 + col_group; |
| 155 | size_t txt_col = 3 * 16 + 4 + byte_ofs; |
| 156 | |
| 157 | if (i < len) { |
| 158 | char value = buf[i]; |
| 159 | |
| 160 | line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF); |
| 161 | line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF); |
| 162 | line_buffer[txt_col + 0] = (value >= ' ' && value < 127) |
| 163 | ? value |
| 164 | : '.'; |
| 165 | } |
| 166 | |
| 167 | if (byte_ofs == 0xF) |
| 168 | trace_fn(i & -16, line_buffer); |
| 169 | } |
| 170 | } |
| 171 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 172 | /* return -1 if error, 0 if OK */ |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 173 | int gdb_put_packet_binary(const char *buf, int len, bool dump) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 174 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 175 | int csum, i; |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 176 | uint8_t footer[3]; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 177 | |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 178 | if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) { |
| 179 | hexdump(buf, len, trace_gdbstub_io_binaryreply); |
| 180 | } |
| 181 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 182 | for(;;) { |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 183 | g_byte_array_set_size(gdbserver_state.last_packet, 0); |
| 184 | g_byte_array_append(gdbserver_state.last_packet, |
| 185 | (const uint8_t *) "$", 1); |
| 186 | g_byte_array_append(gdbserver_state.last_packet, |
| 187 | (const uint8_t *) buf, len); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 188 | csum = 0; |
| 189 | for(i = 0; i < len; i++) { |
| 190 | csum += buf[i]; |
| 191 | } |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 192 | footer[0] = '#'; |
| 193 | footer[1] = tohex((csum >> 4) & 0xf); |
| 194 | footer[2] = tohex((csum) & 0xf); |
| 195 | g_byte_array_append(gdbserver_state.last_packet, footer, 3); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 196 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 197 | gdb_put_buffer(gdbserver_state.last_packet->data, |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 198 | gdbserver_state.last_packet->len); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 199 | |
Alex Bennée | a7e0f9b | 2023-03-02 18:57:49 -0800 | [diff] [blame] | 200 | if (gdb_got_immediate_ack()) { |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 201 | break; |
Alex Bennée | a7e0f9b | 2023-03-02 18:57:49 -0800 | [diff] [blame] | 202 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 203 | } |
| 204 | return 0; |
| 205 | } |
| 206 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 207 | /* return -1 if error, 0 if OK */ |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 208 | int gdb_put_packet(const char *buf) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 209 | { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 210 | trace_gdbstub_io_reply(buf); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 211 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 212 | return gdb_put_packet_binary(buf, strlen(buf), false); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 215 | void gdb_put_strbuf(void) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 216 | { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 217 | gdb_put_packet(gdbserver_state.str_buf->str); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* Encode data using the encoding for 'x' packets. */ |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 221 | void gdb_memtox(GString *buf, const char *mem, int len) |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 222 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 223 | char c; |
| 224 | |
| 225 | while (len--) { |
| 226 | c = *(mem++); |
| 227 | switch (c) { |
| 228 | case '#': case '$': case '*': case '}': |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 229 | g_string_append_c(buf, '}'); |
| 230 | g_string_append_c(buf, c ^ 0x20); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 231 | break; |
| 232 | default: |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 233 | g_string_append_c(buf, c); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 234 | break; |
| 235 | } |
| 236 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 239 | static uint32_t gdb_get_cpu_pid(CPUState *cpu) |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 240 | { |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 241 | /* TODO: In user mode, we should use the task state PID */ |
Peter Maydell | 46f5abc | 2019-01-29 11:46:06 +0000 | [diff] [blame] | 242 | if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) { |
| 243 | /* Return the default process' PID */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 244 | int index = gdbserver_state.process_num - 1; |
| 245 | return gdbserver_state.processes[index].pid; |
Peter Maydell | 46f5abc | 2019-01-29 11:46:06 +0000 | [diff] [blame] | 246 | } |
| 247 | return cpu->cluster_index + 1; |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 250 | static GDBProcess *gdb_get_process(uint32_t pid) |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 251 | { |
| 252 | int i; |
| 253 | |
| 254 | if (!pid) { |
| 255 | /* 0 means any process, we take the first one */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 256 | return &gdbserver_state.processes[0]; |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 259 | for (i = 0; i < gdbserver_state.process_num; i++) { |
| 260 | if (gdbserver_state.processes[i].pid == pid) { |
| 261 | return &gdbserver_state.processes[i]; |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | return NULL; |
| 266 | } |
| 267 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 268 | static GDBProcess *gdb_get_cpu_process(CPUState *cpu) |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 269 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 270 | return gdb_get_process(gdb_get_cpu_pid(cpu)); |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static CPUState *find_cpu(uint32_t thread_id) |
| 274 | { |
| 275 | CPUState *cpu; |
| 276 | |
| 277 | CPU_FOREACH(cpu) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 278 | if (gdb_get_cpu_index(cpu) == thread_id) { |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 279 | return cpu; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | return NULL; |
| 284 | } |
| 285 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 286 | static CPUState *get_first_cpu_in_process(GDBProcess *process) |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 287 | { |
| 288 | CPUState *cpu; |
| 289 | |
| 290 | CPU_FOREACH(cpu) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 291 | if (gdb_get_cpu_pid(cpu) == process->pid) { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 292 | return cpu; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return NULL; |
| 297 | } |
| 298 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 299 | static CPUState *gdb_next_cpu_in_process(CPUState *cpu) |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 300 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 301 | uint32_t pid = gdb_get_cpu_pid(cpu); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 302 | cpu = CPU_NEXT(cpu); |
| 303 | |
| 304 | while (cpu) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 305 | if (gdb_get_cpu_pid(cpu) == pid) { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 306 | break; |
| 307 | } |
| 308 | |
| 309 | cpu = CPU_NEXT(cpu); |
| 310 | } |
| 311 | |
| 312 | return cpu; |
| 313 | } |
| 314 | |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 315 | /* Return the cpu following @cpu, while ignoring unattached processes. */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 316 | static CPUState *gdb_next_attached_cpu(CPUState *cpu) |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 317 | { |
| 318 | cpu = CPU_NEXT(cpu); |
| 319 | |
| 320 | while (cpu) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 321 | if (gdb_get_cpu_process(cpu)->attached) { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | |
| 325 | cpu = CPU_NEXT(cpu); |
| 326 | } |
| 327 | |
| 328 | return cpu; |
| 329 | } |
| 330 | |
| 331 | /* Return the first attached cpu */ |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 332 | CPUState *gdb_first_attached_cpu(void) |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 333 | { |
| 334 | CPUState *cpu = first_cpu; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 335 | GDBProcess *process = gdb_get_cpu_process(cpu); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 336 | |
| 337 | if (!process->attached) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 338 | return gdb_next_attached_cpu(cpu); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | return cpu; |
| 342 | } |
| 343 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 344 | static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid) |
Luc Michel | ab65eed | 2019-01-29 11:46:03 +0000 | [diff] [blame] | 345 | { |
| 346 | GDBProcess *process; |
| 347 | CPUState *cpu; |
| 348 | |
| 349 | if (!pid && !tid) { |
| 350 | /* 0 means any process/thread, we take the first attached one */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 351 | return gdb_first_attached_cpu(); |
Luc Michel | ab65eed | 2019-01-29 11:46:03 +0000 | [diff] [blame] | 352 | } else if (pid && !tid) { |
| 353 | /* any thread in a specific process */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 354 | process = gdb_get_process(pid); |
Luc Michel | ab65eed | 2019-01-29 11:46:03 +0000 | [diff] [blame] | 355 | |
| 356 | if (process == NULL) { |
| 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | if (!process->attached) { |
| 361 | return NULL; |
| 362 | } |
| 363 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 364 | return get_first_cpu_in_process(process); |
Luc Michel | ab65eed | 2019-01-29 11:46:03 +0000 | [diff] [blame] | 365 | } else { |
| 366 | /* a specific thread */ |
| 367 | cpu = find_cpu(tid); |
| 368 | |
| 369 | if (cpu == NULL) { |
| 370 | return NULL; |
| 371 | } |
| 372 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 373 | process = gdb_get_cpu_process(cpu); |
Luc Michel | ab65eed | 2019-01-29 11:46:03 +0000 | [diff] [blame] | 374 | |
| 375 | if (pid && process->pid != pid) { |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | if (!process->attached) { |
| 380 | return NULL; |
| 381 | } |
| 382 | |
| 383 | return cpu; |
| 384 | } |
| 385 | } |
| 386 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 387 | static const char *get_feature_xml(const char *p, const char **newp, |
| 388 | GDBProcess *process) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 389 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 390 | size_t len; |
| 391 | int i; |
| 392 | const char *name; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 393 | CPUState *cpu = get_first_cpu_in_process(process); |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 394 | CPUClass *cc = CPU_GET_CLASS(cpu); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 395 | |
| 396 | len = 0; |
| 397 | while (p[len] && p[len] != ':') |
| 398 | len++; |
| 399 | *newp = p + len; |
| 400 | |
| 401 | name = NULL; |
| 402 | if (strncmp(p, "target.xml", len) == 0) { |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 403 | char *buf = process->target_xml; |
| 404 | const size_t buf_sz = sizeof(process->target_xml); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 405 | |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 406 | /* Generate the XML description for this CPU. */ |
| 407 | if (!buf[0]) { |
| 408 | GDBRegisterState *r; |
| 409 | |
| 410 | pstrcat(buf, buf_sz, |
David Hildenbrand | b3820e6 | 2015-12-03 13:14:41 +0100 | [diff] [blame] | 411 | "<?xml version=\"1.0\"?>" |
| 412 | "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">" |
| 413 | "<target>"); |
| 414 | if (cc->gdb_arch_name) { |
| 415 | gchar *arch = cc->gdb_arch_name(cpu); |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 416 | pstrcat(buf, buf_sz, "<architecture>"); |
| 417 | pstrcat(buf, buf_sz, arch); |
| 418 | pstrcat(buf, buf_sz, "</architecture>"); |
David Hildenbrand | b3820e6 | 2015-12-03 13:14:41 +0100 | [diff] [blame] | 419 | g_free(arch); |
| 420 | } |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 421 | pstrcat(buf, buf_sz, "<xi:include href=\""); |
| 422 | pstrcat(buf, buf_sz, cc->gdb_core_xml_file); |
| 423 | pstrcat(buf, buf_sz, "\"/>"); |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 424 | for (r = cpu->gdb_regs; r; r = r->next) { |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 425 | pstrcat(buf, buf_sz, "<xi:include href=\""); |
| 426 | pstrcat(buf, buf_sz, r->xml); |
| 427 | pstrcat(buf, buf_sz, "\"/>"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 428 | } |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 429 | pstrcat(buf, buf_sz, "</target>"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 430 | } |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 431 | return buf; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 432 | } |
Abdallah Bouassida | 200bf5b | 2018-05-18 17:48:07 +0100 | [diff] [blame] | 433 | if (cc->gdb_get_dynamic_xml) { |
Abdallah Bouassida | 200bf5b | 2018-05-18 17:48:07 +0100 | [diff] [blame] | 434 | char *xmlname = g_strndup(p, len); |
| 435 | const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname); |
| 436 | |
| 437 | g_free(xmlname); |
| 438 | if (xml) { |
| 439 | return xml; |
| 440 | } |
| 441 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 442 | for (i = 0; ; i++) { |
| 443 | name = xml_builtin[i][0]; |
| 444 | if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len)) |
| 445 | break; |
| 446 | } |
| 447 | return name ? xml_builtin[i][1] : NULL; |
| 448 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 449 | |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 450 | static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 451 | { |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 452 | CPUClass *cc = CPU_GET_CLASS(cpu); |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 453 | CPUArchState *env = cpu->env_ptr; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 454 | GDBRegisterState *r; |
| 455 | |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 456 | if (reg < cc->gdb_num_core_regs) { |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 457 | return cc->gdb_read_register(cpu, buf, reg); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 458 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 459 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 460 | for (r = cpu->gdb_regs; r; r = r->next) { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 461 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 462 | return r->get_reg(env, buf, reg - r->base_reg); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 468 | static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 469 | { |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 470 | CPUClass *cc = CPU_GET_CLASS(cpu); |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 471 | CPUArchState *env = cpu->env_ptr; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 472 | GDBRegisterState *r; |
| 473 | |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 474 | if (reg < cc->gdb_num_core_regs) { |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 475 | return cc->gdb_write_register(cpu, mem_buf, reg); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 476 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 477 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 478 | for (r = cpu->gdb_regs; r; r = r->next) { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 479 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
| 480 | return r->set_reg(env, mem_buf, reg - r->base_reg); |
| 481 | } |
| 482 | } |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /* Register a supplemental set of CPU registers. If g_pos is nonzero it |
| 487 | specifies the first register number and these registers are included in |
| 488 | a standard "g" packet. Direction is relative to gdb, i.e. get_reg is |
| 489 | gdb reading a CPU register, and set_reg is gdb modifying a CPU register. |
| 490 | */ |
| 491 | |
Andreas Färber | 22169d4 | 2013-06-28 21:27:39 +0200 | [diff] [blame] | 492 | void gdb_register_coprocessor(CPUState *cpu, |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 493 | gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg, |
Andreas Färber | 22169d4 | 2013-06-28 21:27:39 +0200 | [diff] [blame] | 494 | int num_regs, const char *xml, int g_pos) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 495 | { |
| 496 | GDBRegisterState *s; |
| 497 | GDBRegisterState **p; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 498 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 499 | p = &cpu->gdb_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 500 | while (*p) { |
| 501 | /* Check for duplicates. */ |
| 502 | if (strcmp((*p)->xml, xml) == 0) |
| 503 | return; |
| 504 | p = &(*p)->next; |
| 505 | } |
Stefan Weil | 9643c25 | 2011-10-18 22:25:38 +0200 | [diff] [blame] | 506 | |
| 507 | s = g_new0(GDBRegisterState, 1); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 508 | s->base_reg = cpu->gdb_num_regs; |
Stefan Weil | 9643c25 | 2011-10-18 22:25:38 +0200 | [diff] [blame] | 509 | s->num_regs = num_regs; |
| 510 | s->get_reg = get_reg; |
| 511 | s->set_reg = set_reg; |
| 512 | s->xml = xml; |
| 513 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 514 | /* Add to end of list. */ |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 515 | cpu->gdb_num_regs += num_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 516 | *p = s; |
| 517 | if (g_pos) { |
| 518 | if (g_pos != s->base_reg) { |
Ziyue Yang | 7ae6c57 | 2017-01-18 16:03:29 +0800 | [diff] [blame] | 519 | error_report("Error: Bad gdb register numbering for '%s', " |
| 520 | "expected %d got %d", xml, g_pos, s->base_reg); |
Andreas Färber | 35143f0 | 2013-08-12 18:09:47 +0200 | [diff] [blame] | 521 | } else { |
| 522 | cpu->gdb_num_g_regs = cpu->gdb_num_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 527 | static void gdb_process_breakpoint_remove_all(GDBProcess *p) |
Luc Michel | 546f3c6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 528 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 529 | CPUState *cpu = get_first_cpu_in_process(p); |
Luc Michel | 546f3c6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 530 | |
| 531 | while (cpu) { |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 532 | gdb_breakpoint_remove_all(cpu); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 533 | cpu = gdb_next_cpu_in_process(cpu); |
Luc Michel | 546f3c6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 537 | |
Alex Bennée | b428ad1 | 2023-03-02 18:57:54 -0800 | [diff] [blame] | 538 | static void gdb_set_cpu_pc(vaddr pc) |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 539 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 540 | CPUState *cpu = gdbserver_state.c_cpu; |
Andreas Färber | f45748f | 2013-06-21 19:09:18 +0200 | [diff] [blame] | 541 | |
| 542 | cpu_synchronize_state(cpu); |
Peter Crosthwaite | 4a2b24e | 2015-06-23 20:19:21 -0700 | [diff] [blame] | 543 | cpu_set_pc(cpu, pc); |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 546 | void gdb_append_thread_id(CPUState *cpu, GString *buf) |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 547 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 548 | if (gdbserver_state.multiprocess) { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 549 | g_string_append_printf(buf, "p%02x.%02x", |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 550 | gdb_get_cpu_pid(cpu), gdb_get_cpu_index(cpu)); |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 551 | } else { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 552 | g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu)); |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 553 | } |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 556 | static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf, |
| 557 | uint32_t *pid, uint32_t *tid) |
| 558 | { |
| 559 | unsigned long p, t; |
| 560 | int ret; |
| 561 | |
| 562 | if (*buf == 'p') { |
| 563 | buf++; |
| 564 | ret = qemu_strtoul(buf, &buf, 16, &p); |
| 565 | |
| 566 | if (ret) { |
| 567 | return GDB_READ_THREAD_ERR; |
| 568 | } |
| 569 | |
| 570 | /* Skip '.' */ |
| 571 | buf++; |
| 572 | } else { |
| 573 | p = 1; |
| 574 | } |
| 575 | |
| 576 | ret = qemu_strtoul(buf, &buf, 16, &t); |
| 577 | |
| 578 | if (ret) { |
| 579 | return GDB_READ_THREAD_ERR; |
| 580 | } |
| 581 | |
| 582 | *end_buf = buf; |
| 583 | |
| 584 | if (p == -1) { |
| 585 | return GDB_ALL_PROCESSES; |
| 586 | } |
| 587 | |
| 588 | if (pid) { |
| 589 | *pid = p; |
| 590 | } |
| 591 | |
| 592 | if (t == -1) { |
| 593 | return GDB_ALL_THREADS; |
| 594 | } |
| 595 | |
| 596 | if (tid) { |
| 597 | *tid = t; |
| 598 | } |
| 599 | |
| 600 | return GDB_ONE_THREAD; |
| 601 | } |
| 602 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 603 | /** |
| 604 | * gdb_handle_vcont - Parses and handles a vCont packet. |
| 605 | * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is |
| 606 | * a format error, 0 on success. |
| 607 | */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 608 | static int gdb_handle_vcont(const char *p) |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 609 | { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 610 | int res, signal = 0; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 611 | char cur_action; |
| 612 | char *newstates; |
| 613 | unsigned long tmp; |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 614 | uint32_t pid, tid; |
| 615 | GDBProcess *process; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 616 | CPUState *cpu; |
Luc Michel | c99ef79 | 2019-03-26 12:53:26 +0000 | [diff] [blame] | 617 | GDBThreadIdKind kind; |
Alex Bennée | 7ea0c33 | 2023-03-02 18:57:52 -0800 | [diff] [blame] | 618 | unsigned int max_cpus = gdb_get_max_cpus(); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 619 | /* uninitialised CPUs stay 0 */ |
| 620 | newstates = g_new0(char, max_cpus); |
| 621 | |
| 622 | /* mark valid CPUs with 1 */ |
| 623 | CPU_FOREACH(cpu) { |
| 624 | newstates[cpu->cpu_index] = 1; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * res keeps track of what error we are returning, with -ENOTSUP meaning |
| 629 | * that the command is unknown or unsupported, thus returning an empty |
| 630 | * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid, |
| 631 | * or incorrect parameters passed. |
| 632 | */ |
| 633 | res = 0; |
| 634 | while (*p) { |
| 635 | if (*p++ != ';') { |
| 636 | res = -ENOTSUP; |
| 637 | goto out; |
| 638 | } |
| 639 | |
| 640 | cur_action = *p++; |
| 641 | if (cur_action == 'C' || cur_action == 'S') { |
Peter Maydell | 95a5bef | 2017-07-20 17:31:30 +0100 | [diff] [blame] | 642 | cur_action = qemu_tolower(cur_action); |
Peter Maydell | 3ddd903 | 2020-11-21 21:03:42 +0000 | [diff] [blame] | 643 | res = qemu_strtoul(p, &p, 16, &tmp); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 644 | if (res) { |
| 645 | goto out; |
| 646 | } |
| 647 | signal = gdb_signal_to_target(tmp); |
| 648 | } else if (cur_action != 'c' && cur_action != 's') { |
| 649 | /* unknown/invalid/unsupported command */ |
| 650 | res = -ENOTSUP; |
| 651 | goto out; |
| 652 | } |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 653 | |
Luc Michel | c99ef79 | 2019-03-26 12:53:26 +0000 | [diff] [blame] | 654 | if (*p == '\0' || *p == ';') { |
| 655 | /* |
| 656 | * No thread specifier, action is on "all threads". The |
| 657 | * specification is unclear regarding the process to act on. We |
| 658 | * choose all processes. |
| 659 | */ |
| 660 | kind = GDB_ALL_PROCESSES; |
| 661 | } else if (*p++ == ':') { |
| 662 | kind = read_thread_id(p, &p, &pid, &tid); |
| 663 | } else { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 664 | res = -ENOTSUP; |
| 665 | goto out; |
| 666 | } |
| 667 | |
Luc Michel | c99ef79 | 2019-03-26 12:53:26 +0000 | [diff] [blame] | 668 | switch (kind) { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 669 | case GDB_READ_THREAD_ERR: |
| 670 | res = -EINVAL; |
| 671 | goto out; |
| 672 | |
| 673 | case GDB_ALL_PROCESSES: |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 674 | cpu = gdb_first_attached_cpu(); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 675 | while (cpu) { |
| 676 | if (newstates[cpu->cpu_index] == 1) { |
| 677 | newstates[cpu->cpu_index] = cur_action; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 678 | } |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 679 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 680 | cpu = gdb_next_attached_cpu(cpu); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 681 | } |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 682 | break; |
| 683 | |
| 684 | case GDB_ALL_THREADS: |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 685 | process = gdb_get_process(pid); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 686 | |
| 687 | if (!process->attached) { |
| 688 | res = -EINVAL; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 689 | goto out; |
| 690 | } |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 691 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 692 | cpu = get_first_cpu_in_process(process); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 693 | while (cpu) { |
| 694 | if (newstates[cpu->cpu_index] == 1) { |
| 695 | newstates[cpu->cpu_index] = cur_action; |
| 696 | } |
| 697 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 698 | cpu = gdb_next_cpu_in_process(cpu); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 699 | } |
| 700 | break; |
| 701 | |
| 702 | case GDB_ONE_THREAD: |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 703 | cpu = gdb_get_cpu(pid, tid); |
Alex Bennée | 5a6a1ad | 2017-07-12 11:52:16 +0100 | [diff] [blame] | 704 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 705 | /* invalid CPU/thread specified */ |
Alex Bennée | 5a6a1ad | 2017-07-12 11:52:16 +0100 | [diff] [blame] | 706 | if (!cpu) { |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 707 | res = -EINVAL; |
| 708 | goto out; |
| 709 | } |
Alex Bennée | 5a6a1ad | 2017-07-12 11:52:16 +0100 | [diff] [blame] | 710 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 711 | /* only use if no previous match occourred */ |
| 712 | if (newstates[cpu->cpu_index] == 1) { |
| 713 | newstates[cpu->cpu_index] = cur_action; |
| 714 | } |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 715 | break; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 716 | } |
| 717 | } |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 718 | gdbserver_state.signal = signal; |
| 719 | gdb_continue_partial(newstates); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 720 | |
| 721 | out: |
| 722 | g_free(newstates); |
| 723 | |
| 724 | return res; |
| 725 | } |
| 726 | |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 727 | static const char *cmd_next_param(const char *param, const char delimiter) |
| 728 | { |
| 729 | static const char all_delimiters[] = ",;:="; |
| 730 | char curr_delimiters[2] = {0}; |
| 731 | const char *delimiters; |
| 732 | |
| 733 | if (delimiter == '?') { |
| 734 | delimiters = all_delimiters; |
| 735 | } else if (delimiter == '0') { |
| 736 | return strchr(param, '\0'); |
| 737 | } else if (delimiter == '.' && *param) { |
| 738 | return param + 1; |
| 739 | } else { |
| 740 | curr_delimiters[0] = delimiter; |
| 741 | delimiters = curr_delimiters; |
| 742 | } |
| 743 | |
| 744 | param += strcspn(param, delimiters); |
| 745 | if (*param) { |
| 746 | param++; |
| 747 | } |
| 748 | return param; |
| 749 | } |
| 750 | |
| 751 | static int cmd_parse_params(const char *data, const char *schema, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 752 | GArray *params) |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 753 | { |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 754 | const char *curr_schema, *curr_data; |
| 755 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 756 | g_assert(schema); |
| 757 | g_assert(params->len == 0); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 758 | |
| 759 | curr_schema = schema; |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 760 | curr_data = data; |
| 761 | while (curr_schema[0] && curr_schema[1] && *curr_data) { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 762 | GdbCmdVariant this_param; |
| 763 | |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 764 | switch (curr_schema[0]) { |
| 765 | case 'l': |
| 766 | if (qemu_strtoul(curr_data, &curr_data, 16, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 767 | &this_param.val_ul)) { |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 768 | return -EINVAL; |
| 769 | } |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 770 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 771 | g_array_append_val(params, this_param); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 772 | break; |
| 773 | case 'L': |
| 774 | if (qemu_strtou64(curr_data, &curr_data, 16, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 775 | (uint64_t *)&this_param.val_ull)) { |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 776 | return -EINVAL; |
| 777 | } |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 778 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 779 | g_array_append_val(params, this_param); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 780 | break; |
| 781 | case 's': |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 782 | this_param.data = curr_data; |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 783 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 784 | g_array_append_val(params, this_param); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 785 | break; |
| 786 | case 'o': |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 787 | this_param.opcode = *(uint8_t *)curr_data; |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 788 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 789 | g_array_append_val(params, this_param); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 790 | break; |
| 791 | case 't': |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 792 | this_param.thread_id.kind = |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 793 | read_thread_id(curr_data, &curr_data, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 794 | &this_param.thread_id.pid, |
| 795 | &this_param.thread_id.tid); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 796 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 797 | g_array_append_val(params, this_param); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 798 | break; |
| 799 | case '?': |
| 800 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
| 801 | break; |
| 802 | default: |
| 803 | return -EINVAL; |
| 804 | } |
| 805 | curr_schema += 2; |
| 806 | } |
| 807 | |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 808 | return 0; |
| 809 | } |
| 810 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 811 | typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 812 | |
| 813 | /* |
| 814 | * cmd_startswith -> cmd is compared using startswith |
| 815 | * |
| 816 | * |
| 817 | * schema definitions: |
| 818 | * Each schema parameter entry consists of 2 chars, |
| 819 | * the first char represents the parameter type handling |
| 820 | * the second char represents the delimiter for the next parameter |
| 821 | * |
| 822 | * Currently supported schema types: |
| 823 | * 'l' -> unsigned long (stored in .val_ul) |
| 824 | * 'L' -> unsigned long long (stored in .val_ull) |
| 825 | * 's' -> string (stored in .data) |
| 826 | * 'o' -> single char (stored in .opcode) |
| 827 | * 't' -> thread id (stored in .thread_id) |
| 828 | * '?' -> skip according to delimiter |
| 829 | * |
| 830 | * Currently supported delimiters: |
| 831 | * '?' -> Stop at any delimiter (",;:=\0") |
| 832 | * '0' -> Stop at "\0" |
| 833 | * '.' -> Skip 1 char unless reached "\0" |
| 834 | * Any other value is treated as the delimiter value itself |
| 835 | */ |
| 836 | typedef struct GdbCmdParseEntry { |
| 837 | GdbCmdHandler handler; |
| 838 | const char *cmd; |
| 839 | bool cmd_startswith; |
| 840 | const char *schema; |
| 841 | } GdbCmdParseEntry; |
| 842 | |
| 843 | static inline int startswith(const char *string, const char *pattern) |
| 844 | { |
| 845 | return !strncmp(string, pattern, strlen(pattern)); |
| 846 | } |
| 847 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 848 | static int process_string_cmd(void *user_ctx, const char *data, |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 849 | const GdbCmdParseEntry *cmds, int num_cmds) |
| 850 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 851 | int i; |
| 852 | g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant)); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 853 | |
| 854 | if (!cmds) { |
| 855 | return -1; |
| 856 | } |
| 857 | |
| 858 | for (i = 0; i < num_cmds; i++) { |
| 859 | const GdbCmdParseEntry *cmd = &cmds[i]; |
| 860 | g_assert(cmd->handler && cmd->cmd); |
| 861 | |
| 862 | if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) || |
| 863 | (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) { |
| 864 | continue; |
| 865 | } |
| 866 | |
| 867 | if (cmd->schema) { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 868 | if (cmd_parse_params(&data[strlen(cmd->cmd)], |
| 869 | cmd->schema, params)) { |
| 870 | return -1; |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 871 | } |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 872 | } |
| 873 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 874 | cmd->handler(params, user_ctx); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | return -1; |
| 879 | } |
| 880 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 881 | static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd) |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 882 | { |
| 883 | if (!data) { |
| 884 | return; |
| 885 | } |
| 886 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 887 | g_string_set_size(gdbserver_state.str_buf, 0); |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 888 | g_byte_array_set_size(gdbserver_state.mem_buf, 0); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 889 | |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 890 | /* In case there was an error during the command parsing we must |
| 891 | * send a NULL packet to indicate the command is not supported */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 892 | if (process_string_cmd(NULL, data, cmd, 1)) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 893 | gdb_put_packet(""); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 897 | static void handle_detach(GArray *params, void *user_ctx) |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 898 | { |
| 899 | GDBProcess *process; |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 900 | uint32_t pid = 1; |
| 901 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 902 | if (gdbserver_state.multiprocess) { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 903 | if (!params->len) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 904 | gdb_put_packet("E22"); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 905 | return; |
| 906 | } |
| 907 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 908 | pid = get_param(params, 0)->val_ul; |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 909 | } |
| 910 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 911 | process = gdb_get_process(pid); |
| 912 | gdb_process_breakpoint_remove_all(process); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 913 | process->attached = false; |
| 914 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 915 | if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) { |
| 916 | gdbserver_state.c_cpu = gdb_first_attached_cpu(); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 917 | } |
| 918 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 919 | if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) { |
| 920 | gdbserver_state.g_cpu = gdb_first_attached_cpu(); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 921 | } |
| 922 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 923 | if (!gdbserver_state.c_cpu) { |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 924 | /* No more process attached */ |
| 925 | gdb_syscall_mode = GDB_SYS_DISABLED; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 926 | gdb_continue(); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 927 | } |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 928 | gdb_put_packet("OK"); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 929 | } |
| 930 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 931 | static void handle_thread_alive(GArray *params, void *user_ctx) |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 932 | { |
| 933 | CPUState *cpu; |
| 934 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 935 | if (!params->len) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 936 | gdb_put_packet("E22"); |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 937 | return; |
| 938 | } |
| 939 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 940 | if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 941 | gdb_put_packet("E22"); |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 942 | return; |
| 943 | } |
| 944 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 945 | cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid, |
| 946 | get_param(params, 0)->thread_id.tid); |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 947 | if (!cpu) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 948 | gdb_put_packet("E22"); |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 949 | return; |
| 950 | } |
| 951 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 952 | gdb_put_packet("OK"); |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 953 | } |
| 954 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 955 | static void handle_continue(GArray *params, void *user_ctx) |
Jon Doron | 4d6e3fe | 2019-05-29 09:41:32 +0300 | [diff] [blame] | 956 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 957 | if (params->len) { |
| 958 | gdb_set_cpu_pc(get_param(params, 0)->val_ull); |
Jon Doron | 4d6e3fe | 2019-05-29 09:41:32 +0300 | [diff] [blame] | 959 | } |
| 960 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 961 | gdbserver_state.signal = 0; |
| 962 | gdb_continue(); |
Jon Doron | 4d6e3fe | 2019-05-29 09:41:32 +0300 | [diff] [blame] | 963 | } |
| 964 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 965 | static void handle_cont_with_sig(GArray *params, void *user_ctx) |
Jon Doron | ccc47d5 | 2019-05-29 09:41:33 +0300 | [diff] [blame] | 966 | { |
| 967 | unsigned long signal = 0; |
| 968 | |
| 969 | /* |
| 970 | * Note: C sig;[addr] is currently unsupported and we simply |
| 971 | * omit the addr parameter |
| 972 | */ |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 973 | if (params->len) { |
| 974 | signal = get_param(params, 0)->val_ul; |
Jon Doron | ccc47d5 | 2019-05-29 09:41:33 +0300 | [diff] [blame] | 975 | } |
| 976 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 977 | gdbserver_state.signal = gdb_signal_to_target(signal); |
| 978 | if (gdbserver_state.signal == -1) { |
| 979 | gdbserver_state.signal = 0; |
Jon Doron | ccc47d5 | 2019-05-29 09:41:33 +0300 | [diff] [blame] | 980 | } |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 981 | gdb_continue(); |
Jon Doron | ccc47d5 | 2019-05-29 09:41:33 +0300 | [diff] [blame] | 982 | } |
| 983 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 984 | static void handle_set_thread(GArray *params, void *user_ctx) |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 985 | { |
| 986 | CPUState *cpu; |
| 987 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 988 | if (params->len != 2) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 989 | gdb_put_packet("E22"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 990 | return; |
| 991 | } |
| 992 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 993 | if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 994 | gdb_put_packet("E22"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 995 | return; |
| 996 | } |
| 997 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 998 | if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 999 | gdb_put_packet("OK"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1000 | return; |
| 1001 | } |
| 1002 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1003 | cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid, |
| 1004 | get_param(params, 1)->thread_id.tid); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1005 | if (!cpu) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1006 | gdb_put_packet("E22"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1007 | return; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * Note: This command is deprecated and modern gdb's will be using the |
| 1012 | * vCont command instead. |
| 1013 | */ |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1014 | switch (get_param(params, 0)->opcode) { |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1015 | case 'c': |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1016 | gdbserver_state.c_cpu = cpu; |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1017 | gdb_put_packet("OK"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1018 | break; |
| 1019 | case 'g': |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1020 | gdbserver_state.g_cpu = cpu; |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1021 | gdb_put_packet("OK"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1022 | break; |
| 1023 | default: |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1024 | gdb_put_packet("E22"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1025 | break; |
| 1026 | } |
| 1027 | } |
| 1028 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1029 | static void handle_insert_bp(GArray *params, void *user_ctx) |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1030 | { |
| 1031 | int res; |
| 1032 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1033 | if (params->len != 3) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1034 | gdb_put_packet("E22"); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1035 | return; |
| 1036 | } |
| 1037 | |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 1038 | res = gdb_breakpoint_insert(gdbserver_state.c_cpu, |
| 1039 | get_param(params, 0)->val_ul, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1040 | get_param(params, 1)->val_ull, |
| 1041 | get_param(params, 2)->val_ull); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1042 | if (res >= 0) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1043 | gdb_put_packet("OK"); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1044 | return; |
| 1045 | } else if (res == -ENOSYS) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1046 | gdb_put_packet(""); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1047 | return; |
| 1048 | } |
| 1049 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1050 | gdb_put_packet("E22"); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1051 | } |
| 1052 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1053 | static void handle_remove_bp(GArray *params, void *user_ctx) |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1054 | { |
| 1055 | int res; |
| 1056 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1057 | if (params->len != 3) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1058 | gdb_put_packet("E22"); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1059 | return; |
| 1060 | } |
| 1061 | |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 1062 | res = gdb_breakpoint_remove(gdbserver_state.c_cpu, |
| 1063 | get_param(params, 0)->val_ul, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1064 | get_param(params, 1)->val_ull, |
| 1065 | get_param(params, 2)->val_ull); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1066 | if (res >= 0) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1067 | gdb_put_packet("OK"); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1068 | return; |
| 1069 | } else if (res == -ENOSYS) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1070 | gdb_put_packet(""); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1071 | return; |
| 1072 | } |
| 1073 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1074 | gdb_put_packet("E22"); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1075 | } |
| 1076 | |
Alex Bennée | 94b2a62 | 2019-07-05 14:23:07 +0100 | [diff] [blame] | 1077 | /* |
| 1078 | * handle_set/get_reg |
| 1079 | * |
| 1080 | * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available. |
| 1081 | * This works, but can be very slow. Anything new enough to understand |
| 1082 | * XML also knows how to use this properly. However to use this we |
| 1083 | * need to define a local XML file as well as be talking to a |
| 1084 | * reasonably modern gdb. Responding with an empty packet will cause |
| 1085 | * the remote gdb to fallback to older methods. |
| 1086 | */ |
| 1087 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1088 | static void handle_set_reg(GArray *params, void *user_ctx) |
Jon Doron | 62b3320 | 2019-05-29 09:41:36 +0300 | [diff] [blame] | 1089 | { |
| 1090 | int reg_size; |
| 1091 | |
| 1092 | if (!gdb_has_xml) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1093 | gdb_put_packet(""); |
Jon Doron | 62b3320 | 2019-05-29 09:41:36 +0300 | [diff] [blame] | 1094 | return; |
| 1095 | } |
| 1096 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1097 | if (params->len != 2) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1098 | gdb_put_packet("E22"); |
Jon Doron | 62b3320 | 2019-05-29 09:41:36 +0300 | [diff] [blame] | 1099 | return; |
| 1100 | } |
| 1101 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1102 | reg_size = strlen(get_param(params, 1)->data) / 2; |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1103 | gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size); |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1104 | gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1105 | get_param(params, 0)->val_ull); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1106 | gdb_put_packet("OK"); |
Jon Doron | 62b3320 | 2019-05-29 09:41:36 +0300 | [diff] [blame] | 1107 | } |
| 1108 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1109 | static void handle_get_reg(GArray *params, void *user_ctx) |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1110 | { |
| 1111 | int reg_size; |
| 1112 | |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1113 | if (!gdb_has_xml) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1114 | gdb_put_packet(""); |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1115 | return; |
| 1116 | } |
| 1117 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1118 | if (!params->len) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1119 | gdb_put_packet("E14"); |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1120 | return; |
| 1121 | } |
| 1122 | |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1123 | reg_size = gdb_read_register(gdbserver_state.g_cpu, |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 1124 | gdbserver_state.mem_buf, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1125 | get_param(params, 0)->val_ull); |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1126 | if (!reg_size) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1127 | gdb_put_packet("E14"); |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1128 | return; |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1129 | } else { |
| 1130 | g_byte_array_set_size(gdbserver_state.mem_buf, reg_size); |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1131 | } |
| 1132 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1133 | gdb_memtohex(gdbserver_state.str_buf, |
| 1134 | gdbserver_state.mem_buf->data, reg_size); |
| 1135 | gdb_put_strbuf(); |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1136 | } |
| 1137 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1138 | static void handle_write_mem(GArray *params, void *user_ctx) |
Jon Doron | cc0ecc7 | 2019-05-29 09:41:38 +0300 | [diff] [blame] | 1139 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1140 | if (params->len != 3) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1141 | gdb_put_packet("E22"); |
Jon Doron | cc0ecc7 | 2019-05-29 09:41:38 +0300 | [diff] [blame] | 1142 | return; |
| 1143 | } |
| 1144 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1145 | /* gdb_hextomem() reads 2*len bytes */ |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1146 | if (get_param(params, 1)->val_ull > |
| 1147 | strlen(get_param(params, 2)->data) / 2) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1148 | gdb_put_packet("E22"); |
Jon Doron | cc0ecc7 | 2019-05-29 09:41:38 +0300 | [diff] [blame] | 1149 | return; |
| 1150 | } |
| 1151 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1152 | gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data, |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 1153 | get_param(params, 1)->val_ull); |
| 1154 | if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu, |
| 1155 | get_param(params, 0)->val_ull, |
| 1156 | gdbserver_state.mem_buf->data, |
| 1157 | gdbserver_state.mem_buf->len, true)) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1158 | gdb_put_packet("E14"); |
Jon Doron | cc0ecc7 | 2019-05-29 09:41:38 +0300 | [diff] [blame] | 1159 | return; |
| 1160 | } |
| 1161 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1162 | gdb_put_packet("OK"); |
Jon Doron | cc0ecc7 | 2019-05-29 09:41:38 +0300 | [diff] [blame] | 1163 | } |
| 1164 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1165 | static void handle_read_mem(GArray *params, void *user_ctx) |
Jon Doron | da92e23 | 2019-05-29 09:41:39 +0300 | [diff] [blame] | 1166 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1167 | if (params->len != 2) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1168 | gdb_put_packet("E22"); |
Jon Doron | da92e23 | 2019-05-29 09:41:39 +0300 | [diff] [blame] | 1169 | return; |
| 1170 | } |
| 1171 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1172 | /* gdb_memtohex() doubles the required space */ |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1173 | if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1174 | gdb_put_packet("E22"); |
Jon Doron | da92e23 | 2019-05-29 09:41:39 +0300 | [diff] [blame] | 1175 | return; |
| 1176 | } |
| 1177 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1178 | g_byte_array_set_size(gdbserver_state.mem_buf, |
| 1179 | get_param(params, 1)->val_ull); |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1180 | |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 1181 | if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu, |
| 1182 | get_param(params, 0)->val_ull, |
| 1183 | gdbserver_state.mem_buf->data, |
| 1184 | gdbserver_state.mem_buf->len, false)) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1185 | gdb_put_packet("E14"); |
Jon Doron | da92e23 | 2019-05-29 09:41:39 +0300 | [diff] [blame] | 1186 | return; |
| 1187 | } |
| 1188 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1189 | gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1190 | gdbserver_state.mem_buf->len); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1191 | gdb_put_strbuf(); |
Jon Doron | da92e23 | 2019-05-29 09:41:39 +0300 | [diff] [blame] | 1192 | } |
| 1193 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1194 | static void handle_write_all_regs(GArray *params, void *user_ctx) |
Jon Doron | 287ca12 | 2019-05-29 09:41:40 +0300 | [diff] [blame] | 1195 | { |
Alex Bennée | 379b42e | 2023-03-02 18:57:55 -0800 | [diff] [blame^] | 1196 | int reg_id; |
| 1197 | size_t len; |
Jon Doron | 287ca12 | 2019-05-29 09:41:40 +0300 | [diff] [blame] | 1198 | uint8_t *registers; |
| 1199 | int reg_size; |
| 1200 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1201 | if (!params->len) { |
Jon Doron | 287ca12 | 2019-05-29 09:41:40 +0300 | [diff] [blame] | 1202 | return; |
| 1203 | } |
| 1204 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1205 | cpu_synchronize_state(gdbserver_state.g_cpu); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1206 | len = strlen(get_param(params, 0)->data) / 2; |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1207 | gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len); |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1208 | registers = gdbserver_state.mem_buf->data; |
Alex Bennée | 379b42e | 2023-03-02 18:57:55 -0800 | [diff] [blame^] | 1209 | for (reg_id = 0; |
| 1210 | reg_id < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0; |
| 1211 | reg_id++) { |
| 1212 | reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, reg_id); |
Jon Doron | 287ca12 | 2019-05-29 09:41:40 +0300 | [diff] [blame] | 1213 | len -= reg_size; |
| 1214 | registers += reg_size; |
| 1215 | } |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1216 | gdb_put_packet("OK"); |
Jon Doron | 287ca12 | 2019-05-29 09:41:40 +0300 | [diff] [blame] | 1217 | } |
| 1218 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1219 | static void handle_read_all_regs(GArray *params, void *user_ctx) |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 1220 | { |
Alex Bennée | 379b42e | 2023-03-02 18:57:55 -0800 | [diff] [blame^] | 1221 | int reg_id; |
| 1222 | size_t len; |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 1223 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1224 | cpu_synchronize_state(gdbserver_state.g_cpu); |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 1225 | g_byte_array_set_size(gdbserver_state.mem_buf, 0); |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 1226 | len = 0; |
Alex Bennée | 379b42e | 2023-03-02 18:57:55 -0800 | [diff] [blame^] | 1227 | for (reg_id = 0; reg_id < gdbserver_state.g_cpu->gdb_num_g_regs; reg_id++) { |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1228 | len += gdb_read_register(gdbserver_state.g_cpu, |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 1229 | gdbserver_state.mem_buf, |
Alex Bennée | 379b42e | 2023-03-02 18:57:55 -0800 | [diff] [blame^] | 1230 | reg_id); |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 1231 | } |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 1232 | g_assert(len == gdbserver_state.mem_buf->len); |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 1233 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1234 | gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len); |
| 1235 | gdb_put_strbuf(); |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 1236 | } |
| 1237 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1238 | static void handle_file_io(GArray *params, void *user_ctx) |
Jon Doron | 4b20fab | 2019-05-29 09:41:42 +0300 | [diff] [blame] | 1239 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1240 | if (params->len >= 1 && gdbserver_state.current_syscall_cb) { |
Richard Henderson | 64c8c6a | 2022-04-29 16:21:43 -0700 | [diff] [blame] | 1241 | uint64_t ret; |
| 1242 | int err; |
Jon Doron | 4b20fab | 2019-05-29 09:41:42 +0300 | [diff] [blame] | 1243 | |
Richard Henderson | 64c8c6a | 2022-04-29 16:21:43 -0700 | [diff] [blame] | 1244 | ret = get_param(params, 0)->val_ull; |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1245 | if (params->len >= 2) { |
Richard Henderson | 64c8c6a | 2022-04-29 16:21:43 -0700 | [diff] [blame] | 1246 | err = get_param(params, 1)->val_ull; |
Sandra Loosemore | c6ee952 | 2019-08-27 16:33:17 -0600 | [diff] [blame] | 1247 | } else { |
| 1248 | err = 0; |
| 1249 | } |
Richard Henderson | c805e11 | 2022-06-07 12:38:26 -0700 | [diff] [blame] | 1250 | |
| 1251 | /* Convert GDB error numbers back to host error numbers. */ |
| 1252 | #define E(X) case GDB_E##X: err = E##X; break |
| 1253 | switch (err) { |
| 1254 | case 0: |
| 1255 | break; |
| 1256 | E(PERM); |
| 1257 | E(NOENT); |
| 1258 | E(INTR); |
| 1259 | E(BADF); |
| 1260 | E(ACCES); |
| 1261 | E(FAULT); |
| 1262 | E(BUSY); |
| 1263 | E(EXIST); |
| 1264 | E(NODEV); |
| 1265 | E(NOTDIR); |
| 1266 | E(ISDIR); |
| 1267 | E(INVAL); |
| 1268 | E(NFILE); |
| 1269 | E(MFILE); |
| 1270 | E(FBIG); |
| 1271 | E(NOSPC); |
| 1272 | E(SPIPE); |
| 1273 | E(ROFS); |
| 1274 | E(NAMETOOLONG); |
| 1275 | default: |
| 1276 | err = EINVAL; |
| 1277 | break; |
| 1278 | } |
| 1279 | #undef E |
| 1280 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1281 | gdbserver_state.current_syscall_cb(gdbserver_state.c_cpu, ret, err); |
| 1282 | gdbserver_state.current_syscall_cb = NULL; |
Jon Doron | 4b20fab | 2019-05-29 09:41:42 +0300 | [diff] [blame] | 1283 | } |
| 1284 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1285 | if (params->len >= 3 && get_param(params, 2)->opcode == (uint8_t)'C') { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1286 | gdb_put_packet("T02"); |
Jon Doron | 4b20fab | 2019-05-29 09:41:42 +0300 | [diff] [blame] | 1287 | return; |
| 1288 | } |
| 1289 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1290 | gdb_continue(); |
Jon Doron | 4b20fab | 2019-05-29 09:41:42 +0300 | [diff] [blame] | 1291 | } |
| 1292 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1293 | static void handle_step(GArray *params, void *user_ctx) |
Jon Doron | 933f80d | 2019-05-29 09:41:43 +0300 | [diff] [blame] | 1294 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1295 | if (params->len) { |
Alex Bennée | b428ad1 | 2023-03-02 18:57:54 -0800 | [diff] [blame] | 1296 | gdb_set_cpu_pc(get_param(params, 0)->val_ull); |
Jon Doron | 933f80d | 2019-05-29 09:41:43 +0300 | [diff] [blame] | 1297 | } |
| 1298 | |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 1299 | cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1300 | gdb_continue(); |
Jon Doron | 933f80d | 2019-05-29 09:41:43 +0300 | [diff] [blame] | 1301 | } |
| 1302 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1303 | static void handle_backward(GArray *params, void *user_ctx) |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 1304 | { |
Alex Bennée | 505601d | 2023-03-02 18:57:53 -0800 | [diff] [blame] | 1305 | if (!gdb_can_reverse()) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1306 | gdb_put_packet("E22"); |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 1307 | } |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1308 | if (params->len == 1) { |
| 1309 | switch (get_param(params, 0)->opcode) { |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 1310 | case 's': |
| 1311 | if (replay_reverse_step()) { |
| 1312 | gdb_continue(); |
| 1313 | } else { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1314 | gdb_put_packet("E14"); |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 1315 | } |
| 1316 | return; |
Pavel Dovgalyuk | cda3825 | 2020-10-03 20:13:49 +0300 | [diff] [blame] | 1317 | case 'c': |
| 1318 | if (replay_reverse_continue()) { |
| 1319 | gdb_continue(); |
| 1320 | } else { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1321 | gdb_put_packet("E14"); |
Pavel Dovgalyuk | cda3825 | 2020-10-03 20:13:49 +0300 | [diff] [blame] | 1322 | } |
| 1323 | return; |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | /* Default invalid command */ |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1328 | gdb_put_packet(""); |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 1329 | } |
| 1330 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1331 | static void handle_v_cont_query(GArray *params, void *user_ctx) |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1332 | { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1333 | gdb_put_packet("vCont;c;C;s;S"); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1334 | } |
| 1335 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1336 | static void handle_v_cont(GArray *params, void *user_ctx) |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1337 | { |
| 1338 | int res; |
| 1339 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1340 | if (!params->len) { |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1341 | return; |
| 1342 | } |
| 1343 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1344 | res = gdb_handle_vcont(get_param(params, 0)->data); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1345 | if ((res == -EINVAL) || (res == -ERANGE)) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1346 | gdb_put_packet("E22"); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1347 | } else if (res) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1348 | gdb_put_packet(""); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1352 | static void handle_v_attach(GArray *params, void *user_ctx) |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1353 | { |
| 1354 | GDBProcess *process; |
| 1355 | CPUState *cpu; |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1356 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1357 | g_string_assign(gdbserver_state.str_buf, "E22"); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1358 | if (!params->len) { |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1359 | goto cleanup; |
| 1360 | } |
| 1361 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1362 | process = gdb_get_process(get_param(params, 0)->val_ul); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1363 | if (!process) { |
| 1364 | goto cleanup; |
| 1365 | } |
| 1366 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1367 | cpu = get_first_cpu_in_process(process); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1368 | if (!cpu) { |
| 1369 | goto cleanup; |
| 1370 | } |
| 1371 | |
| 1372 | process->attached = true; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1373 | gdbserver_state.g_cpu = cpu; |
| 1374 | gdbserver_state.c_cpu = cpu; |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1375 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1376 | g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP); |
| 1377 | gdb_append_thread_id(cpu, gdbserver_state.str_buf); |
| 1378 | g_string_append_c(gdbserver_state.str_buf, ';'); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1379 | cleanup: |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1380 | gdb_put_strbuf(); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1381 | } |
| 1382 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1383 | static void handle_v_kill(GArray *params, void *user_ctx) |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1384 | { |
| 1385 | /* Kill the target */ |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1386 | gdb_put_packet("OK"); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1387 | error_report("QEMU: Terminated via GDBstub"); |
Alex Bennée | b9e10c6 | 2021-01-08 22:42:45 +0000 | [diff] [blame] | 1388 | gdb_exit(0); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1389 | exit(0); |
| 1390 | } |
| 1391 | |
Philippe Mathieu-Daudé | 305bea0 | 2021-05-20 18:42:59 +0100 | [diff] [blame] | 1392 | static const GdbCmdParseEntry gdb_v_commands_table[] = { |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1393 | /* Order is important if has same prefix */ |
| 1394 | { |
| 1395 | .handler = handle_v_cont_query, |
| 1396 | .cmd = "Cont?", |
| 1397 | .cmd_startswith = 1 |
| 1398 | }, |
| 1399 | { |
| 1400 | .handler = handle_v_cont, |
| 1401 | .cmd = "Cont", |
| 1402 | .cmd_startswith = 1, |
| 1403 | .schema = "s0" |
| 1404 | }, |
| 1405 | { |
| 1406 | .handler = handle_v_attach, |
| 1407 | .cmd = "Attach;", |
| 1408 | .cmd_startswith = 1, |
| 1409 | .schema = "l0" |
| 1410 | }, |
| 1411 | { |
| 1412 | .handler = handle_v_kill, |
| 1413 | .cmd = "Kill;", |
| 1414 | .cmd_startswith = 1 |
| 1415 | }, |
| 1416 | }; |
| 1417 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1418 | static void handle_v_commands(GArray *params, void *user_ctx) |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1419 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1420 | if (!params->len) { |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1421 | return; |
| 1422 | } |
| 1423 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1424 | if (process_string_cmd(NULL, get_param(params, 0)->data, |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1425 | gdb_v_commands_table, |
| 1426 | ARRAY_SIZE(gdb_v_commands_table))) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1427 | gdb_put_packet(""); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1431 | static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1432 | { |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 1433 | g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE); |
| 1434 | |
| 1435 | if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) { |
| 1436 | g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x", |
| 1437 | SSTEP_NOIRQ); |
| 1438 | } |
| 1439 | |
| 1440 | if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) { |
| 1441 | g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x", |
| 1442 | SSTEP_NOTIMER); |
| 1443 | } |
| 1444 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1445 | gdb_put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1446 | } |
| 1447 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1448 | static void handle_set_qemu_sstep(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1449 | { |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 1450 | int new_sstep_flags; |
| 1451 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1452 | if (!params->len) { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1453 | return; |
| 1454 | } |
| 1455 | |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 1456 | new_sstep_flags = get_param(params, 0)->val_ul; |
| 1457 | |
| 1458 | if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1459 | gdb_put_packet("E22"); |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 1460 | return; |
| 1461 | } |
| 1462 | |
| 1463 | gdbserver_state.sstep_flags = new_sstep_flags; |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1464 | gdb_put_packet("OK"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1465 | } |
| 1466 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1467 | static void handle_query_qemu_sstep(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1468 | { |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 1469 | g_string_printf(gdbserver_state.str_buf, "0x%x", |
| 1470 | gdbserver_state.sstep_flags); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1471 | gdb_put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1472 | } |
| 1473 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1474 | static void handle_query_curr_tid(GArray *params, void *user_ctx) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1475 | { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1476 | CPUState *cpu; |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1477 | GDBProcess *process; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1478 | |
| 1479 | /* |
| 1480 | * "Current thread" remains vague in the spec, so always return |
| 1481 | * the first thread of the current process (gdb returns the |
| 1482 | * first thread). |
| 1483 | */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1484 | process = gdb_get_cpu_process(gdbserver_state.g_cpu); |
| 1485 | cpu = get_first_cpu_in_process(process); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1486 | g_string_assign(gdbserver_state.str_buf, "QC"); |
| 1487 | gdb_append_thread_id(cpu, gdbserver_state.str_buf); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1488 | gdb_put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1489 | } |
| 1490 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1491 | static void handle_query_threads(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1492 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1493 | if (!gdbserver_state.query_cpu) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1494 | gdb_put_packet("l"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1495 | return; |
| 1496 | } |
| 1497 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1498 | g_string_assign(gdbserver_state.str_buf, "m"); |
| 1499 | gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1500 | gdb_put_strbuf(); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1501 | gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1502 | } |
| 1503 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1504 | static void handle_query_first_threads(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1505 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1506 | gdbserver_state.query_cpu = gdb_first_attached_cpu(); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1507 | handle_query_threads(params, user_ctx); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1508 | } |
| 1509 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1510 | static void handle_query_thread_extra(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1511 | { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1512 | g_autoptr(GString) rs = g_string_new(NULL); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1513 | CPUState *cpu; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1514 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1515 | if (!params->len || |
| 1516 | get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1517 | gdb_put_packet("E22"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1518 | return; |
| 1519 | } |
| 1520 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1521 | cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid, |
| 1522 | get_param(params, 0)->thread_id.tid); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1523 | if (!cpu) { |
| 1524 | return; |
| 1525 | } |
| 1526 | |
| 1527 | cpu_synchronize_state(cpu); |
| 1528 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1529 | if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1530 | /* Print the CPU model and name in multiprocess mode */ |
| 1531 | ObjectClass *oc = object_get_class(OBJECT(cpu)); |
| 1532 | const char *cpu_model = object_class_get_name(oc); |
Markus Armbruster | 7a309cc | 2020-07-14 18:02:00 +0200 | [diff] [blame] | 1533 | const char *cpu_name = |
Denis Plotnikov | 076b2fa | 2020-04-03 20:11:44 +0100 | [diff] [blame] | 1534 | object_get_canonical_path_component(OBJECT(cpu)); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1535 | g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name, |
| 1536 | cpu->halted ? "halted " : "running"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1537 | } else { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1538 | g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1539 | cpu->halted ? "halted " : "running"); |
| 1540 | } |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1541 | trace_gdbstub_op_extra_info(rs->str); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1542 | gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len); |
| 1543 | gdb_put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1544 | } |
| 1545 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1546 | static void handle_query_supported(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1547 | { |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 1548 | CPUClass *cc; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1549 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1550 | g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1551 | cc = CPU_GET_CLASS(first_cpu); |
| 1552 | if (cc->gdb_core_xml_file) { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1553 | g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1554 | } |
| 1555 | |
Alex Bennée | 505601d | 2023-03-02 18:57:53 -0800 | [diff] [blame] | 1556 | if (gdb_can_reverse()) { |
Pavel Dovgalyuk | cda3825 | 2020-10-03 20:13:49 +0300 | [diff] [blame] | 1557 | g_string_append(gdbserver_state.str_buf, |
| 1558 | ";ReverseStep+;ReverseContinue+"); |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 1559 | } |
| 1560 | |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 1561 | #ifdef CONFIG_USER_ONLY |
| 1562 | if (gdbserver_state.c_cpu->opaque) { |
| 1563 | g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+"); |
| 1564 | } |
| 1565 | #endif |
| 1566 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1567 | if (params->len && |
| 1568 | strstr(get_param(params, 0)->data, "multiprocess+")) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1569 | gdbserver_state.multiprocess = true; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1570 | } |
| 1571 | |
Changbin Du | 3bc2609 | 2020-03-16 17:21:55 +0000 | [diff] [blame] | 1572 | g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+"); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1573 | gdb_put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1574 | } |
| 1575 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1576 | static void handle_query_xfer_features(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1577 | { |
| 1578 | GDBProcess *process; |
| 1579 | CPUClass *cc; |
| 1580 | unsigned long len, total_len, addr; |
| 1581 | const char *xml; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1582 | const char *p; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1583 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1584 | if (params->len < 3) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1585 | gdb_put_packet("E22"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1586 | return; |
| 1587 | } |
| 1588 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1589 | process = gdb_get_cpu_process(gdbserver_state.g_cpu); |
| 1590 | cc = CPU_GET_CLASS(gdbserver_state.g_cpu); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1591 | if (!cc->gdb_core_xml_file) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1592 | gdb_put_packet(""); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1593 | return; |
| 1594 | } |
| 1595 | |
| 1596 | gdb_has_xml = true; |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1597 | p = get_param(params, 0)->data; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1598 | xml = get_feature_xml(p, &p, process); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1599 | if (!xml) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1600 | gdb_put_packet("E00"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1601 | return; |
| 1602 | } |
| 1603 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1604 | addr = get_param(params, 1)->val_ul; |
| 1605 | len = get_param(params, 2)->val_ul; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1606 | total_len = strlen(xml); |
| 1607 | if (addr > total_len) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1608 | gdb_put_packet("E00"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1609 | return; |
| 1610 | } |
| 1611 | |
| 1612 | if (len > (MAX_PACKET_LENGTH - 5) / 2) { |
| 1613 | len = (MAX_PACKET_LENGTH - 5) / 2; |
| 1614 | } |
| 1615 | |
| 1616 | if (len < total_len - addr) { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1617 | g_string_assign(gdbserver_state.str_buf, "m"); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1618 | gdb_memtox(gdbserver_state.str_buf, xml + addr, len); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1619 | } else { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1620 | g_string_assign(gdbserver_state.str_buf, "l"); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1621 | gdb_memtox(gdbserver_state.str_buf, xml + addr, total_len - addr); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1622 | } |
| 1623 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1624 | gdb_put_packet_binary(gdbserver_state.str_buf->str, |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1625 | gdbserver_state.str_buf->len, true); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1626 | } |
| 1627 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1628 | static void handle_query_qemu_supported(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1629 | { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1630 | g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep"); |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 1631 | #ifndef CONFIG_USER_ONLY |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1632 | g_string_append(gdbserver_state.str_buf, ";PhyMemMode"); |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 1633 | #endif |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1634 | gdb_put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1635 | } |
| 1636 | |
Philippe Mathieu-Daudé | 305bea0 | 2021-05-20 18:42:59 +0100 | [diff] [blame] | 1637 | static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1638 | /* Order is important if has same prefix */ |
| 1639 | { |
| 1640 | .handler = handle_query_qemu_sstepbits, |
| 1641 | .cmd = "qemu.sstepbits", |
| 1642 | }, |
| 1643 | { |
| 1644 | .handler = handle_query_qemu_sstep, |
| 1645 | .cmd = "qemu.sstep", |
| 1646 | }, |
| 1647 | { |
| 1648 | .handler = handle_set_qemu_sstep, |
| 1649 | .cmd = "qemu.sstep=", |
| 1650 | .cmd_startswith = 1, |
| 1651 | .schema = "l0" |
| 1652 | }, |
| 1653 | }; |
| 1654 | |
Philippe Mathieu-Daudé | 305bea0 | 2021-05-20 18:42:59 +0100 | [diff] [blame] | 1655 | static const GdbCmdParseEntry gdb_gen_query_table[] = { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1656 | { |
| 1657 | .handler = handle_query_curr_tid, |
| 1658 | .cmd = "C", |
| 1659 | }, |
| 1660 | { |
| 1661 | .handler = handle_query_threads, |
| 1662 | .cmd = "sThreadInfo", |
| 1663 | }, |
| 1664 | { |
| 1665 | .handler = handle_query_first_threads, |
| 1666 | .cmd = "fThreadInfo", |
| 1667 | }, |
| 1668 | { |
| 1669 | .handler = handle_query_thread_extra, |
| 1670 | .cmd = "ThreadExtraInfo,", |
| 1671 | .cmd_startswith = 1, |
| 1672 | .schema = "t0" |
| 1673 | }, |
| 1674 | #ifdef CONFIG_USER_ONLY |
| 1675 | { |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 1676 | .handler = gdb_handle_query_offsets, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1677 | .cmd = "Offsets", |
| 1678 | }, |
| 1679 | #else |
| 1680 | { |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 1681 | .handler = gdb_handle_query_rcmd, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1682 | .cmd = "Rcmd,", |
| 1683 | .cmd_startswith = 1, |
| 1684 | .schema = "s0" |
| 1685 | }, |
| 1686 | #endif |
| 1687 | { |
| 1688 | .handler = handle_query_supported, |
| 1689 | .cmd = "Supported:", |
| 1690 | .cmd_startswith = 1, |
| 1691 | .schema = "s0" |
| 1692 | }, |
| 1693 | { |
| 1694 | .handler = handle_query_supported, |
| 1695 | .cmd = "Supported", |
| 1696 | .schema = "s0" |
| 1697 | }, |
| 1698 | { |
| 1699 | .handler = handle_query_xfer_features, |
| 1700 | .cmd = "Xfer:features:read:", |
| 1701 | .cmd_startswith = 1, |
| 1702 | .schema = "s:l,l0" |
| 1703 | }, |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 1704 | #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER) |
| 1705 | { |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 1706 | .handler = gdb_handle_query_xfer_auxv, |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 1707 | .cmd = "Xfer:auxv:read::", |
| 1708 | .cmd_startswith = 1, |
| 1709 | .schema = "l,l0" |
| 1710 | }, |
| 1711 | #endif |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1712 | { |
Alex Bennée | 8a2025b | 2023-03-02 18:57:50 -0800 | [diff] [blame] | 1713 | .handler = gdb_handle_query_attached, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1714 | .cmd = "Attached:", |
| 1715 | .cmd_startswith = 1 |
| 1716 | }, |
| 1717 | { |
Alex Bennée | 8a2025b | 2023-03-02 18:57:50 -0800 | [diff] [blame] | 1718 | .handler = gdb_handle_query_attached, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1719 | .cmd = "Attached", |
| 1720 | }, |
| 1721 | { |
| 1722 | .handler = handle_query_qemu_supported, |
| 1723 | .cmd = "qemu.Supported", |
| 1724 | }, |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 1725 | #ifndef CONFIG_USER_ONLY |
| 1726 | { |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 1727 | .handler = gdb_handle_query_qemu_phy_mem_mode, |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 1728 | .cmd = "qemu.PhyMemMode", |
| 1729 | }, |
| 1730 | #endif |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1731 | }; |
| 1732 | |
Philippe Mathieu-Daudé | 305bea0 | 2021-05-20 18:42:59 +0100 | [diff] [blame] | 1733 | static const GdbCmdParseEntry gdb_gen_set_table[] = { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1734 | /* Order is important if has same prefix */ |
| 1735 | { |
| 1736 | .handler = handle_set_qemu_sstep, |
| 1737 | .cmd = "qemu.sstep:", |
| 1738 | .cmd_startswith = 1, |
| 1739 | .schema = "l0" |
| 1740 | }, |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 1741 | #ifndef CONFIG_USER_ONLY |
| 1742 | { |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 1743 | .handler = gdb_handle_set_qemu_phy_mem_mode, |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 1744 | .cmd = "qemu.PhyMemMode:", |
| 1745 | .cmd_startswith = 1, |
| 1746 | .schema = "l0" |
| 1747 | }, |
| 1748 | #endif |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1749 | }; |
| 1750 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1751 | static void handle_gen_query(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1752 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1753 | if (!params->len) { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1754 | return; |
| 1755 | } |
| 1756 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1757 | if (!process_string_cmd(NULL, get_param(params, 0)->data, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1758 | gdb_gen_query_set_common_table, |
| 1759 | ARRAY_SIZE(gdb_gen_query_set_common_table))) { |
| 1760 | return; |
| 1761 | } |
| 1762 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1763 | if (process_string_cmd(NULL, get_param(params, 0)->data, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1764 | gdb_gen_query_table, |
| 1765 | ARRAY_SIZE(gdb_gen_query_table))) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1766 | gdb_put_packet(""); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1767 | } |
| 1768 | } |
| 1769 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1770 | static void handle_gen_set(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1771 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1772 | if (!params->len) { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1773 | return; |
| 1774 | } |
| 1775 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1776 | if (!process_string_cmd(NULL, get_param(params, 0)->data, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1777 | gdb_gen_query_set_common_table, |
| 1778 | ARRAY_SIZE(gdb_gen_query_set_common_table))) { |
| 1779 | return; |
| 1780 | } |
| 1781 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1782 | if (process_string_cmd(NULL, get_param(params, 0)->data, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1783 | gdb_gen_set_table, |
| 1784 | ARRAY_SIZE(gdb_gen_set_table))) { |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1785 | gdb_put_packet(""); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1786 | } |
| 1787 | } |
| 1788 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1789 | static void handle_target_halt(GArray *params, void *user_ctx) |
Jon Doron | 7009d57 | 2019-05-29 09:41:46 +0300 | [diff] [blame] | 1790 | { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1791 | g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP); |
| 1792 | gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf); |
| 1793 | g_string_append_c(gdbserver_state.str_buf, ';'); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1794 | gdb_put_strbuf(); |
Jon Doron | 7009d57 | 2019-05-29 09:41:46 +0300 | [diff] [blame] | 1795 | /* |
| 1796 | * Remove all the breakpoints when this query is issued, |
| 1797 | * because gdb is doing an initial connect and the state |
| 1798 | * should be cleaned up. |
| 1799 | */ |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 1800 | gdb_breakpoint_remove_all(gdbserver_state.c_cpu); |
Jon Doron | 7009d57 | 2019-05-29 09:41:46 +0300 | [diff] [blame] | 1801 | } |
| 1802 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1803 | static int gdb_handle_packet(const char *line_buf) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1804 | { |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1805 | const GdbCmdParseEntry *cmd_parser = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1806 | |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1807 | trace_gdbstub_io_command(line_buf); |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1808 | |
Jon Doron | 3f1cbac | 2019-05-29 09:41:47 +0300 | [diff] [blame] | 1809 | switch (line_buf[0]) { |
Luc Michel | 53fd655 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1810 | case '!': |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 1811 | gdb_put_packet("OK"); |
Luc Michel | 53fd655 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1812 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1813 | case '?': |
Jon Doron | 7009d57 | 2019-05-29 09:41:46 +0300 | [diff] [blame] | 1814 | { |
| 1815 | static const GdbCmdParseEntry target_halted_cmd_desc = { |
| 1816 | .handler = handle_target_halt, |
| 1817 | .cmd = "?", |
| 1818 | .cmd_startswith = 1 |
| 1819 | }; |
| 1820 | cmd_parser = &target_halted_cmd_desc; |
| 1821 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1822 | break; |
| 1823 | case 'c': |
Jon Doron | 4d6e3fe | 2019-05-29 09:41:32 +0300 | [diff] [blame] | 1824 | { |
| 1825 | static const GdbCmdParseEntry continue_cmd_desc = { |
| 1826 | .handler = handle_continue, |
| 1827 | .cmd = "c", |
| 1828 | .cmd_startswith = 1, |
| 1829 | .schema = "L0" |
| 1830 | }; |
| 1831 | cmd_parser = &continue_cmd_desc; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1832 | } |
Jon Doron | 4d6e3fe | 2019-05-29 09:41:32 +0300 | [diff] [blame] | 1833 | break; |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 1834 | case 'C': |
Jon Doron | ccc47d5 | 2019-05-29 09:41:33 +0300 | [diff] [blame] | 1835 | { |
| 1836 | static const GdbCmdParseEntry cont_with_sig_cmd_desc = { |
| 1837 | .handler = handle_cont_with_sig, |
| 1838 | .cmd = "C", |
| 1839 | .cmd_startswith = 1, |
| 1840 | .schema = "l0" |
| 1841 | }; |
| 1842 | cmd_parser = &cont_with_sig_cmd_desc; |
| 1843 | } |
| 1844 | break; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1845 | case 'v': |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1846 | { |
| 1847 | static const GdbCmdParseEntry v_cmd_desc = { |
| 1848 | .handler = handle_v_commands, |
| 1849 | .cmd = "v", |
| 1850 | .cmd_startswith = 1, |
| 1851 | .schema = "s0" |
| 1852 | }; |
| 1853 | cmd_parser = &v_cmd_desc; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1854 | } |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1855 | break; |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1856 | case 'k': |
| 1857 | /* Kill the target */ |
Ziyue Yang | 7ae6c57 | 2017-01-18 16:03:29 +0800 | [diff] [blame] | 1858 | error_report("QEMU: Terminated via GDBstub"); |
Alex Bennée | b9e10c6 | 2021-01-08 22:42:45 +0000 | [diff] [blame] | 1859 | gdb_exit(0); |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1860 | exit(0); |
| 1861 | case 'D': |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1862 | { |
| 1863 | static const GdbCmdParseEntry detach_cmd_desc = { |
| 1864 | .handler = handle_detach, |
| 1865 | .cmd = "D", |
| 1866 | .cmd_startswith = 1, |
| 1867 | .schema = "?.l0" |
| 1868 | }; |
| 1869 | cmd_parser = &detach_cmd_desc; |
Luc Michel | 546f3c6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1870 | } |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1871 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1872 | case 's': |
Jon Doron | 933f80d | 2019-05-29 09:41:43 +0300 | [diff] [blame] | 1873 | { |
| 1874 | static const GdbCmdParseEntry step_cmd_desc = { |
| 1875 | .handler = handle_step, |
| 1876 | .cmd = "s", |
| 1877 | .cmd_startswith = 1, |
| 1878 | .schema = "L0" |
| 1879 | }; |
| 1880 | cmd_parser = &step_cmd_desc; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1881 | } |
Jon Doron | 933f80d | 2019-05-29 09:41:43 +0300 | [diff] [blame] | 1882 | break; |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 1883 | case 'b': |
| 1884 | { |
| 1885 | static const GdbCmdParseEntry backward_cmd_desc = { |
| 1886 | .handler = handle_backward, |
| 1887 | .cmd = "b", |
| 1888 | .cmd_startswith = 1, |
| 1889 | .schema = "o0" |
| 1890 | }; |
| 1891 | cmd_parser = &backward_cmd_desc; |
| 1892 | } |
| 1893 | break; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1894 | case 'F': |
| 1895 | { |
Jon Doron | 4b20fab | 2019-05-29 09:41:42 +0300 | [diff] [blame] | 1896 | static const GdbCmdParseEntry file_io_cmd_desc = { |
| 1897 | .handler = handle_file_io, |
| 1898 | .cmd = "F", |
| 1899 | .cmd_startswith = 1, |
| 1900 | .schema = "L,L,o0" |
| 1901 | }; |
| 1902 | cmd_parser = &file_io_cmd_desc; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1903 | } |
| 1904 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1905 | case 'g': |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 1906 | { |
| 1907 | static const GdbCmdParseEntry read_all_regs_cmd_desc = { |
| 1908 | .handler = handle_read_all_regs, |
| 1909 | .cmd = "g", |
| 1910 | .cmd_startswith = 1 |
| 1911 | }; |
| 1912 | cmd_parser = &read_all_regs_cmd_desc; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1913 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1914 | break; |
| 1915 | case 'G': |
Jon Doron | 287ca12 | 2019-05-29 09:41:40 +0300 | [diff] [blame] | 1916 | { |
| 1917 | static const GdbCmdParseEntry write_all_regs_cmd_desc = { |
| 1918 | .handler = handle_write_all_regs, |
| 1919 | .cmd = "G", |
| 1920 | .cmd_startswith = 1, |
| 1921 | .schema = "s0" |
| 1922 | }; |
| 1923 | cmd_parser = &write_all_regs_cmd_desc; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1924 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1925 | break; |
| 1926 | case 'm': |
Jon Doron | da92e23 | 2019-05-29 09:41:39 +0300 | [diff] [blame] | 1927 | { |
| 1928 | static const GdbCmdParseEntry read_mem_cmd_desc = { |
| 1929 | .handler = handle_read_mem, |
| 1930 | .cmd = "m", |
| 1931 | .cmd_startswith = 1, |
| 1932 | .schema = "L,L0" |
| 1933 | }; |
| 1934 | cmd_parser = &read_mem_cmd_desc; |
bellard | 6f970bd | 2005-12-05 19:55:19 +0000 | [diff] [blame] | 1935 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1936 | break; |
| 1937 | case 'M': |
Jon Doron | cc0ecc7 | 2019-05-29 09:41:38 +0300 | [diff] [blame] | 1938 | { |
| 1939 | static const GdbCmdParseEntry write_mem_cmd_desc = { |
| 1940 | .handler = handle_write_mem, |
| 1941 | .cmd = "M", |
| 1942 | .cmd_startswith = 1, |
| 1943 | .schema = "L,L:s0" |
| 1944 | }; |
| 1945 | cmd_parser = &write_mem_cmd_desc; |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 1946 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1947 | break; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1948 | case 'p': |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1949 | { |
| 1950 | static const GdbCmdParseEntry get_reg_cmd_desc = { |
| 1951 | .handler = handle_get_reg, |
| 1952 | .cmd = "p", |
| 1953 | .cmd_startswith = 1, |
| 1954 | .schema = "L0" |
| 1955 | }; |
| 1956 | cmd_parser = &get_reg_cmd_desc; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1957 | } |
| 1958 | break; |
| 1959 | case 'P': |
Jon Doron | 62b3320 | 2019-05-29 09:41:36 +0300 | [diff] [blame] | 1960 | { |
| 1961 | static const GdbCmdParseEntry set_reg_cmd_desc = { |
| 1962 | .handler = handle_set_reg, |
| 1963 | .cmd = "P", |
| 1964 | .cmd_startswith = 1, |
| 1965 | .schema = "L?s0" |
| 1966 | }; |
| 1967 | cmd_parser = &set_reg_cmd_desc; |
| 1968 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1969 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1970 | case 'Z': |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1971 | { |
| 1972 | static const GdbCmdParseEntry insert_bp_cmd_desc = { |
| 1973 | .handler = handle_insert_bp, |
| 1974 | .cmd = "Z", |
| 1975 | .cmd_startswith = 1, |
| 1976 | .schema = "l?L?L0" |
| 1977 | }; |
| 1978 | cmd_parser = &insert_bp_cmd_desc; |
| 1979 | } |
| 1980 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1981 | case 'z': |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1982 | { |
| 1983 | static const GdbCmdParseEntry remove_bp_cmd_desc = { |
| 1984 | .handler = handle_remove_bp, |
| 1985 | .cmd = "z", |
| 1986 | .cmd_startswith = 1, |
| 1987 | .schema = "l?L?L0" |
| 1988 | }; |
| 1989 | cmd_parser = &remove_bp_cmd_desc; |
| 1990 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1991 | break; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1992 | case 'H': |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1993 | { |
| 1994 | static const GdbCmdParseEntry set_thread_cmd_desc = { |
| 1995 | .handler = handle_set_thread, |
| 1996 | .cmd = "H", |
| 1997 | .cmd_startswith = 1, |
| 1998 | .schema = "o.t0" |
| 1999 | }; |
| 2000 | cmd_parser = &set_thread_cmd_desc; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 2001 | } |
| 2002 | break; |
| 2003 | case 'T': |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 2004 | { |
| 2005 | static const GdbCmdParseEntry thread_alive_cmd_desc = { |
| 2006 | .handler = handle_thread_alive, |
| 2007 | .cmd = "T", |
| 2008 | .cmd_startswith = 1, |
| 2009 | .schema = "t0" |
| 2010 | }; |
| 2011 | cmd_parser = &thread_alive_cmd_desc; |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 2012 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 2013 | break; |
pbrook | 978efd6 | 2006-06-17 18:30:42 +0000 | [diff] [blame] | 2014 | case 'q': |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2015 | { |
| 2016 | static const GdbCmdParseEntry gen_query_cmd_desc = { |
| 2017 | .handler = handle_gen_query, |
| 2018 | .cmd = "q", |
| 2019 | .cmd_startswith = 1, |
| 2020 | .schema = "s0" |
| 2021 | }; |
| 2022 | cmd_parser = &gen_query_cmd_desc; |
| 2023 | } |
| 2024 | break; |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 2025 | case 'Q': |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2026 | { |
| 2027 | static const GdbCmdParseEntry gen_set_cmd_desc = { |
| 2028 | .handler = handle_gen_set, |
| 2029 | .cmd = "Q", |
| 2030 | .cmd_startswith = 1, |
| 2031 | .schema = "s0" |
| 2032 | }; |
| 2033 | cmd_parser = &gen_set_cmd_desc; |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 2034 | } |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2035 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2036 | default: |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2037 | /* put empty packet */ |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 2038 | gdb_put_packet(""); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2039 | break; |
| 2040 | } |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 2041 | |
Ramiro Polla | 2bdec39 | 2019-08-05 21:09:01 +0200 | [diff] [blame] | 2042 | if (cmd_parser) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2043 | run_cmd_parser(line_buf, cmd_parser); |
Ramiro Polla | 2bdec39 | 2019-08-05 21:09:01 +0200 | [diff] [blame] | 2044 | } |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 2045 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2046 | return RS_IDLE; |
| 2047 | } |
| 2048 | |
Andreas Färber | 64f6b34 | 2013-05-27 02:06:09 +0200 | [diff] [blame] | 2049 | void gdb_set_stop_cpu(CPUState *cpu) |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 2050 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2051 | GDBProcess *p = gdb_get_cpu_process(cpu); |
Luc Michel | 160d858 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2052 | |
| 2053 | if (!p->attached) { |
| 2054 | /* |
| 2055 | * Having a stop CPU corresponding to a process that is not attached |
| 2056 | * confuses GDB. So we ignore the request. |
| 2057 | */ |
| 2058 | return; |
| 2059 | } |
| 2060 | |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 2061 | gdbserver_state.c_cpu = cpu; |
| 2062 | gdbserver_state.g_cpu = cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2065 | /* Send a gdb syscall request. |
| 2066 | This accepts limited printf-style format specifiers, specifically: |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 2067 | %x - target_ulong argument printed in hex. |
| 2068 | %lx - 64-bit argument printed in hex. |
| 2069 | %s - string pointer (target_ulong) and length (int) pair. */ |
Peter Maydell | 19239b3 | 2015-09-07 10:39:27 +0100 | [diff] [blame] | 2070 | void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va) |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2071 | { |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2072 | char *p; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2073 | char *p_end; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2074 | target_ulong addr; |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 2075 | uint64_t i64; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2076 | |
Peter Maydell | ebf1b4c | 2022-06-10 14:32:36 +0100 | [diff] [blame] | 2077 | if (!gdb_attached()) { |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2078 | return; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2079 | } |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 2080 | |
| 2081 | gdbserver_state.current_syscall_cb = cb; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2082 | #ifndef CONFIG_USER_ONLY |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2083 | vm_stop(RUN_STATE_DEBUG); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2084 | #endif |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 2085 | p = &gdbserver_state.syscall_buf[0]; |
| 2086 | p_end = &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_buf)]; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2087 | *(p++) = 'F'; |
| 2088 | while (*fmt) { |
| 2089 | if (*fmt == '%') { |
| 2090 | fmt++; |
| 2091 | switch (*fmt++) { |
| 2092 | case 'x': |
| 2093 | addr = va_arg(va, target_ulong); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2094 | p += snprintf(p, p_end - p, TARGET_FMT_lx, addr); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2095 | break; |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 2096 | case 'l': |
| 2097 | if (*(fmt++) != 'x') |
| 2098 | goto bad_format; |
| 2099 | i64 = va_arg(va, uint64_t); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2100 | p += snprintf(p, p_end - p, "%" PRIx64, i64); |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 2101 | break; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2102 | case 's': |
| 2103 | addr = va_arg(va, target_ulong); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2104 | p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x", |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 2105 | addr, va_arg(va, int)); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2106 | break; |
| 2107 | default: |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 2108 | bad_format: |
Ziyue Yang | 7ae6c57 | 2017-01-18 16:03:29 +0800 | [diff] [blame] | 2109 | error_report("gdbstub: Bad syscall format string '%s'", |
| 2110 | fmt - 1); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2111 | break; |
| 2112 | } |
| 2113 | } else { |
| 2114 | *(p++) = *(fmt++); |
| 2115 | } |
| 2116 | } |
pbrook | 8a93e02 | 2007-08-06 13:19:15 +0000 | [diff] [blame] | 2117 | *p = 0; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2118 | #ifdef CONFIG_USER_ONLY |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 2119 | gdb_put_packet(gdbserver_state.syscall_buf); |
Peter Maydell | 4f71086 | 2018-05-15 19:19:58 +0100 | [diff] [blame] | 2120 | /* Return control to gdb for it to process the syscall request. |
| 2121 | * Since the protocol requires that gdb hands control back to us |
| 2122 | * using a "here are the results" F packet, we don't need to check |
| 2123 | * gdb_handlesig's return value (which is the signal to deliver if |
| 2124 | * execution was resumed via a continue packet). |
| 2125 | */ |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 2126 | gdb_handlesig(gdbserver_state.c_cpu, 0); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2127 | #else |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2128 | /* In this case wait to send the syscall packet until notification that |
| 2129 | the CPU has stopped. This must be done because if the packet is sent |
| 2130 | now the reply from the syscall request could be received while the CPU |
| 2131 | is still in the running state, which can cause packets to be dropped |
| 2132 | and state transition 'T' packets to be sent while the syscall is still |
| 2133 | being processed. */ |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 2134 | qemu_cpu_kick(gdbserver_state.c_cpu); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2135 | #endif |
| 2136 | } |
| 2137 | |
Peter Maydell | 19239b3 | 2015-09-07 10:39:27 +0100 | [diff] [blame] | 2138 | void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...) |
| 2139 | { |
| 2140 | va_list va; |
| 2141 | |
| 2142 | va_start(va, fmt); |
| 2143 | gdb_do_syscallv(cb, fmt, va); |
| 2144 | va_end(va); |
| 2145 | } |
| 2146 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 2147 | void gdb_read_byte(uint8_t ch) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2148 | { |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 2149 | uint8_t reply; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2150 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2151 | #ifndef CONFIG_USER_ONLY |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 2152 | if (gdbserver_state.last_packet->len) { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2153 | /* Waiting for a response to the last packet. If we see the start |
| 2154 | of a new command then abandon the previous response. */ |
| 2155 | if (ch == '-') { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2156 | trace_gdbstub_err_got_nack(); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 2157 | gdb_put_buffer(gdbserver_state.last_packet->data, |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 2158 | gdbserver_state.last_packet->len); |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 2159 | } else if (ch == '+') { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2160 | trace_gdbstub_io_got_ack(); |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 2161 | } else { |
Markus Armbruster | 33c846e | 2019-05-14 20:03:09 +0200 | [diff] [blame] | 2162 | trace_gdbstub_io_got_unexpected(ch); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2163 | } |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 2164 | |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 2165 | if (ch == '+' || ch == '$') { |
| 2166 | g_byte_array_set_size(gdbserver_state.last_packet, 0); |
| 2167 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2168 | if (ch != '$') |
| 2169 | return; |
| 2170 | } |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 2171 | if (runstate_is_running()) { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2172 | /* when the CPU is running, we cannot do anything except stop |
| 2173 | it when receiving a char */ |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2174 | vm_stop(RUN_STATE_PAUSED); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2175 | } else |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2176 | #endif |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 2177 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2178 | switch(gdbserver_state.state) { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2179 | case RS_IDLE: |
| 2180 | if (ch == '$') { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2181 | /* start of command packet */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2182 | gdbserver_state.line_buf_index = 0; |
| 2183 | gdbserver_state.line_sum = 0; |
| 2184 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2185 | } else { |
Markus Armbruster | 33c846e | 2019-05-14 20:03:09 +0200 | [diff] [blame] | 2186 | trace_gdbstub_err_garbage(ch); |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 2187 | } |
| 2188 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2189 | case RS_GETLINE: |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2190 | if (ch == '}') { |
| 2191 | /* start escape sequence */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2192 | gdbserver_state.state = RS_GETLINE_ESC; |
| 2193 | gdbserver_state.line_sum += ch; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2194 | } else if (ch == '*') { |
| 2195 | /* start run length encoding sequence */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2196 | gdbserver_state.state = RS_GETLINE_RLE; |
| 2197 | gdbserver_state.line_sum += ch; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2198 | } else if (ch == '#') { |
| 2199 | /* end of command, start of checksum*/ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2200 | gdbserver_state.state = RS_CHKSUM1; |
| 2201 | } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2202 | trace_gdbstub_err_overrun(); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2203 | gdbserver_state.state = RS_IDLE; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2204 | } else { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2205 | /* unescaped command character */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2206 | gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch; |
| 2207 | gdbserver_state.line_sum += ch; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2208 | } |
| 2209 | break; |
| 2210 | case RS_GETLINE_ESC: |
| 2211 | if (ch == '#') { |
| 2212 | /* unexpected end of command in escape sequence */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2213 | gdbserver_state.state = RS_CHKSUM1; |
| 2214 | } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2215 | /* command buffer overrun */ |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2216 | trace_gdbstub_err_overrun(); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2217 | gdbserver_state.state = RS_IDLE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2218 | } else { |
| 2219 | /* parse escaped character and leave escape state */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2220 | gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20; |
| 2221 | gdbserver_state.line_sum += ch; |
| 2222 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2223 | } |
| 2224 | break; |
| 2225 | case RS_GETLINE_RLE: |
Markus Armbruster | 046aba1 | 2019-05-14 20:03:08 +0200 | [diff] [blame] | 2226 | /* |
| 2227 | * Run-length encoding is explained in "Debugging with GDB / |
| 2228 | * Appendix E GDB Remote Serial Protocol / Overview". |
| 2229 | */ |
| 2230 | if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2231 | /* invalid RLE count encoding */ |
Markus Armbruster | 33c846e | 2019-05-14 20:03:09 +0200 | [diff] [blame] | 2232 | trace_gdbstub_err_invalid_repeat(ch); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2233 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2234 | } else { |
| 2235 | /* decode repeat length */ |
Markus Armbruster | 33c846e | 2019-05-14 20:03:09 +0200 | [diff] [blame] | 2236 | int repeat = ch - ' ' + 3; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2237 | if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2238 | /* that many repeats would overrun the command buffer */ |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2239 | trace_gdbstub_err_overrun(); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2240 | gdbserver_state.state = RS_IDLE; |
| 2241 | } else if (gdbserver_state.line_buf_index < 1) { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2242 | /* got a repeat but we have nothing to repeat */ |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2243 | trace_gdbstub_err_invalid_rle(); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2244 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2245 | } else { |
| 2246 | /* repeat the last character */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2247 | memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index, |
| 2248 | gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat); |
| 2249 | gdbserver_state.line_buf_index += repeat; |
| 2250 | gdbserver_state.line_sum += ch; |
| 2251 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2252 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2253 | } |
| 2254 | break; |
| 2255 | case RS_CHKSUM1: |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2256 | /* get high hex digit of checksum */ |
| 2257 | if (!isxdigit(ch)) { |
Markus Armbruster | 33c846e | 2019-05-14 20:03:09 +0200 | [diff] [blame] | 2258 | trace_gdbstub_err_checksum_invalid(ch); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2259 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2260 | break; |
| 2261 | } |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2262 | gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0'; |
| 2263 | gdbserver_state.line_csum = fromhex(ch) << 4; |
| 2264 | gdbserver_state.state = RS_CHKSUM2; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2265 | break; |
| 2266 | case RS_CHKSUM2: |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2267 | /* get low hex digit of checksum */ |
| 2268 | if (!isxdigit(ch)) { |
Markus Armbruster | 33c846e | 2019-05-14 20:03:09 +0200 | [diff] [blame] | 2269 | trace_gdbstub_err_checksum_invalid(ch); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2270 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2271 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2272 | } |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2273 | gdbserver_state.line_csum |= fromhex(ch); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2274 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2275 | if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) { |
| 2276 | trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2277 | /* send NAK reply */ |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 2278 | reply = '-'; |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 2279 | gdb_put_buffer(&reply, 1); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2280 | gdbserver_state.state = RS_IDLE; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2281 | } else { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2282 | /* send ACK reply */ |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 2283 | reply = '+'; |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 2284 | gdb_put_buffer(&reply, 1); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2285 | gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2286 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2287 | break; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2288 | default: |
| 2289 | abort(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2290 | } |
| 2291 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 2294 | /* |
| 2295 | * Create the process that will contain all the "orphan" CPUs (that are not |
| 2296 | * part of a CPU cluster). Note that if this process contains no CPUs, it won't |
| 2297 | * be attachable and thus will be invisible to the user. |
| 2298 | */ |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 2299 | void gdb_create_default_process(GDBState *s) |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 2300 | { |
| 2301 | GDBProcess *process; |
| 2302 | int max_pid = 0; |
| 2303 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2304 | if (gdbserver_state.process_num) { |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 2305 | max_pid = s->processes[s->process_num - 1].pid; |
| 2306 | } |
| 2307 | |
| 2308 | s->processes = g_renew(GDBProcess, s->processes, ++s->process_num); |
| 2309 | process = &s->processes[s->process_num - 1]; |
| 2310 | |
| 2311 | /* We need an available PID slot for this process */ |
| 2312 | assert(max_pid < UINT32_MAX); |
| 2313 | |
| 2314 | process->pid = max_pid + 1; |
| 2315 | process->attached = false; |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2316 | process->target_xml[0] = '\0'; |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |