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