bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * gdb server stub |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
Alex Bennée | 42a0959 | 2019-07-05 13:28:19 +0100 | [diff] [blame] | 4 | * This implements a subset of the remote protocol as described in: |
| 5 | * |
| 6 | * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html |
| 7 | * |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 8 | * Copyright (c) 2003-2005 Fabrice Bellard |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Lesser General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 21 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
Alex Bennée | 42a0959 | 2019-07-05 13:28:19 +0100 | [diff] [blame] | 22 | * |
| 23 | * SPDX-License-Identifier: LGPL-2.0+ |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 24 | */ |
Markus Armbruster | 856dfd8 | 2019-05-23 16:35:06 +0200 | [diff] [blame] | 25 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 27 | #include "qapi/error.h" |
Ziyue Yang | 508b4ec | 2017-01-18 16:02:41 +0800 | [diff] [blame] | 28 | #include "qemu/error-report.h" |
Markus Armbruster | 856dfd8 | 2019-05-23 16:35:06 +0200 | [diff] [blame] | 29 | #include "qemu/ctype.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 30 | #include "qemu/cutils.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 31 | #include "qemu/module.h" |
Alex Bennée | 842b42d | 2022-09-29 12:42:22 +0100 | [diff] [blame] | 32 | #include "trace.h" |
Peter Maydell | 85b4fa0 | 2021-09-08 16:44:04 +0100 | [diff] [blame] | 33 | #include "exec/gdbstub.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 34 | #ifdef CONFIG_USER_ONLY |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 35 | #include "qemu.h" |
| 36 | #else |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 37 | #include "monitor/monitor.h" |
Marc-André Lureau | 8228e35 | 2017-01-26 17:19:46 +0400 | [diff] [blame] | 38 | #include "chardev/char.h" |
Marc-André Lureau | 4d43a60 | 2017-01-26 18:26:44 +0400 | [diff] [blame] | 39 | #include "chardev/char-fe.h" |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 40 | #include "hw/cpu/cluster.h" |
Like Xu | 5cc8767 | 2019-05-19 04:54:21 +0800 | [diff] [blame] | 41 | #include "hw/boards.h" |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 42 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 43 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 44 | #define MAX_PACKET_LENGTH 4096 |
| 45 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 46 | #include "qemu/sockets.h" |
Vincent Palatin | b394662 | 2017-01-10 11:59:55 +0100 | [diff] [blame] | 47 | #include "sysemu/hw_accel.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 48 | #include "sysemu/runstate.h" |
Philippe Mathieu-Daudé | 6b5fe13 | 2021-03-05 13:54:49 +0000 | [diff] [blame] | 49 | #include "semihosting/semihost.h" |
Paolo Bonzini | 63c9155 | 2016-03-15 13:18:37 +0100 | [diff] [blame] | 50 | #include "exec/exec-all.h" |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 51 | #include "exec/hwaddr.h" |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 52 | #include "sysemu/replay.h" |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 53 | |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 54 | #include "internals.h" |
| 55 | |
Jan Kiszka | a391938 | 2015-02-07 09:38:44 +0100 | [diff] [blame] | 56 | #ifdef CONFIG_USER_ONLY |
| 57 | #define GDB_ATTACHED "0" |
| 58 | #else |
| 59 | #define GDB_ATTACHED "1" |
| 60 | #endif |
| 61 | |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 62 | #ifndef CONFIG_USER_ONLY |
| 63 | static int phy_memory_mode; |
| 64 | #endif |
| 65 | |
Andreas Färber | f3659ee | 2013-06-27 19:09:09 +0200 | [diff] [blame] | 66 | static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr, |
| 67 | uint8_t *buf, int len, bool is_write) |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 68 | { |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 69 | CPUClass *cc; |
Andreas Färber | f3659ee | 2013-06-27 19:09:09 +0200 | [diff] [blame] | 70 | |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 71 | #ifndef CONFIG_USER_ONLY |
| 72 | if (phy_memory_mode) { |
| 73 | if (is_write) { |
| 74 | cpu_physical_memory_write(addr, buf, len); |
| 75 | } else { |
| 76 | cpu_physical_memory_read(addr, buf, len); |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | #endif |
| 81 | |
| 82 | cc = CPU_GET_CLASS(cpu); |
Andreas Färber | f3659ee | 2013-06-27 19:09:09 +0200 | [diff] [blame] | 83 | if (cc->memory_rw_debug) { |
| 84 | return cc->memory_rw_debug(cpu, addr, buf, len, is_write); |
| 85 | } |
| 86 | return cpu_memory_rw_debug(cpu, addr, buf, len, is_write); |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 87 | } |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 88 | |
Alex Bennée | d2a6c85 | 2017-07-12 11:52:14 +0100 | [diff] [blame] | 89 | /* Return the GDB index for a given vCPU state. |
| 90 | * |
| 91 | * For user mode this is simply the thread id. In system mode GDB |
| 92 | * numbers CPUs from 1 as 0 is reserved as an "any cpu" index. |
| 93 | */ |
| 94 | static inline int cpu_gdb_index(CPUState *cpu) |
| 95 | { |
| 96 | #if defined(CONFIG_USER_ONLY) |
Alex Bennée | bd88c78 | 2017-07-12 11:52:15 +0100 | [diff] [blame] | 97 | TaskState *ts = (TaskState *) cpu->opaque; |
Alex Bennée | a8e537f | 2021-11-29 14:09:29 +0000 | [diff] [blame] | 98 | return ts ? ts->ts_tid : -1; |
Alex Bennée | d2a6c85 | 2017-07-12 11:52:14 +0100 | [diff] [blame] | 99 | #else |
| 100 | return cpu->cpu_index + 1; |
| 101 | #endif |
| 102 | } |
| 103 | |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 104 | enum { |
| 105 | GDB_SIGNAL_0 = 0, |
| 106 | GDB_SIGNAL_INT = 2, |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 107 | GDB_SIGNAL_QUIT = 3, |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 108 | GDB_SIGNAL_TRAP = 5, |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 109 | GDB_SIGNAL_ABRT = 6, |
| 110 | GDB_SIGNAL_ALRM = 14, |
| 111 | GDB_SIGNAL_IO = 23, |
| 112 | GDB_SIGNAL_XCPU = 24, |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 113 | GDB_SIGNAL_UNKNOWN = 143 |
| 114 | }; |
| 115 | |
| 116 | #ifdef CONFIG_USER_ONLY |
| 117 | |
| 118 | /* Map target signal numbers to GDB protocol signal numbers and vice |
| 119 | * versa. For user emulation's currently supported systems, we can |
| 120 | * assume most signals are defined. |
| 121 | */ |
| 122 | |
| 123 | static int gdb_signal_table[] = { |
| 124 | 0, |
| 125 | TARGET_SIGHUP, |
| 126 | TARGET_SIGINT, |
| 127 | TARGET_SIGQUIT, |
| 128 | TARGET_SIGILL, |
| 129 | TARGET_SIGTRAP, |
| 130 | TARGET_SIGABRT, |
| 131 | -1, /* SIGEMT */ |
| 132 | TARGET_SIGFPE, |
| 133 | TARGET_SIGKILL, |
| 134 | TARGET_SIGBUS, |
| 135 | TARGET_SIGSEGV, |
| 136 | TARGET_SIGSYS, |
| 137 | TARGET_SIGPIPE, |
| 138 | TARGET_SIGALRM, |
| 139 | TARGET_SIGTERM, |
| 140 | TARGET_SIGURG, |
| 141 | TARGET_SIGSTOP, |
| 142 | TARGET_SIGTSTP, |
| 143 | TARGET_SIGCONT, |
| 144 | TARGET_SIGCHLD, |
| 145 | TARGET_SIGTTIN, |
| 146 | TARGET_SIGTTOU, |
| 147 | TARGET_SIGIO, |
| 148 | TARGET_SIGXCPU, |
| 149 | TARGET_SIGXFSZ, |
| 150 | TARGET_SIGVTALRM, |
| 151 | TARGET_SIGPROF, |
| 152 | TARGET_SIGWINCH, |
| 153 | -1, /* SIGLOST */ |
| 154 | TARGET_SIGUSR1, |
| 155 | TARGET_SIGUSR2, |
blueswir1 | c72d5bf | 2009-01-15 17:27:45 +0000 | [diff] [blame] | 156 | #ifdef TARGET_SIGPWR |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 157 | TARGET_SIGPWR, |
blueswir1 | c72d5bf | 2009-01-15 17:27:45 +0000 | [diff] [blame] | 158 | #else |
| 159 | -1, |
| 160 | #endif |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 161 | -1, /* SIGPOLL */ |
| 162 | -1, |
| 163 | -1, |
| 164 | -1, |
| 165 | -1, |
| 166 | -1, |
| 167 | -1, |
| 168 | -1, |
| 169 | -1, |
| 170 | -1, |
| 171 | -1, |
| 172 | -1, |
blueswir1 | c72d5bf | 2009-01-15 17:27:45 +0000 | [diff] [blame] | 173 | #ifdef __SIGRTMIN |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 174 | __SIGRTMIN + 1, |
| 175 | __SIGRTMIN + 2, |
| 176 | __SIGRTMIN + 3, |
| 177 | __SIGRTMIN + 4, |
| 178 | __SIGRTMIN + 5, |
| 179 | __SIGRTMIN + 6, |
| 180 | __SIGRTMIN + 7, |
| 181 | __SIGRTMIN + 8, |
| 182 | __SIGRTMIN + 9, |
| 183 | __SIGRTMIN + 10, |
| 184 | __SIGRTMIN + 11, |
| 185 | __SIGRTMIN + 12, |
| 186 | __SIGRTMIN + 13, |
| 187 | __SIGRTMIN + 14, |
| 188 | __SIGRTMIN + 15, |
| 189 | __SIGRTMIN + 16, |
| 190 | __SIGRTMIN + 17, |
| 191 | __SIGRTMIN + 18, |
| 192 | __SIGRTMIN + 19, |
| 193 | __SIGRTMIN + 20, |
| 194 | __SIGRTMIN + 21, |
| 195 | __SIGRTMIN + 22, |
| 196 | __SIGRTMIN + 23, |
| 197 | __SIGRTMIN + 24, |
| 198 | __SIGRTMIN + 25, |
| 199 | __SIGRTMIN + 26, |
| 200 | __SIGRTMIN + 27, |
| 201 | __SIGRTMIN + 28, |
| 202 | __SIGRTMIN + 29, |
| 203 | __SIGRTMIN + 30, |
| 204 | __SIGRTMIN + 31, |
| 205 | -1, /* SIGCANCEL */ |
| 206 | __SIGRTMIN, |
| 207 | __SIGRTMIN + 32, |
| 208 | __SIGRTMIN + 33, |
| 209 | __SIGRTMIN + 34, |
| 210 | __SIGRTMIN + 35, |
| 211 | __SIGRTMIN + 36, |
| 212 | __SIGRTMIN + 37, |
| 213 | __SIGRTMIN + 38, |
| 214 | __SIGRTMIN + 39, |
| 215 | __SIGRTMIN + 40, |
| 216 | __SIGRTMIN + 41, |
| 217 | __SIGRTMIN + 42, |
| 218 | __SIGRTMIN + 43, |
| 219 | __SIGRTMIN + 44, |
| 220 | __SIGRTMIN + 45, |
| 221 | __SIGRTMIN + 46, |
| 222 | __SIGRTMIN + 47, |
| 223 | __SIGRTMIN + 48, |
| 224 | __SIGRTMIN + 49, |
| 225 | __SIGRTMIN + 50, |
| 226 | __SIGRTMIN + 51, |
| 227 | __SIGRTMIN + 52, |
| 228 | __SIGRTMIN + 53, |
| 229 | __SIGRTMIN + 54, |
| 230 | __SIGRTMIN + 55, |
| 231 | __SIGRTMIN + 56, |
| 232 | __SIGRTMIN + 57, |
| 233 | __SIGRTMIN + 58, |
| 234 | __SIGRTMIN + 59, |
| 235 | __SIGRTMIN + 60, |
| 236 | __SIGRTMIN + 61, |
| 237 | __SIGRTMIN + 62, |
| 238 | __SIGRTMIN + 63, |
| 239 | __SIGRTMIN + 64, |
| 240 | __SIGRTMIN + 65, |
| 241 | __SIGRTMIN + 66, |
| 242 | __SIGRTMIN + 67, |
| 243 | __SIGRTMIN + 68, |
| 244 | __SIGRTMIN + 69, |
| 245 | __SIGRTMIN + 70, |
| 246 | __SIGRTMIN + 71, |
| 247 | __SIGRTMIN + 72, |
| 248 | __SIGRTMIN + 73, |
| 249 | __SIGRTMIN + 74, |
| 250 | __SIGRTMIN + 75, |
| 251 | __SIGRTMIN + 76, |
| 252 | __SIGRTMIN + 77, |
| 253 | __SIGRTMIN + 78, |
| 254 | __SIGRTMIN + 79, |
| 255 | __SIGRTMIN + 80, |
| 256 | __SIGRTMIN + 81, |
| 257 | __SIGRTMIN + 82, |
| 258 | __SIGRTMIN + 83, |
| 259 | __SIGRTMIN + 84, |
| 260 | __SIGRTMIN + 85, |
| 261 | __SIGRTMIN + 86, |
| 262 | __SIGRTMIN + 87, |
| 263 | __SIGRTMIN + 88, |
| 264 | __SIGRTMIN + 89, |
| 265 | __SIGRTMIN + 90, |
| 266 | __SIGRTMIN + 91, |
| 267 | __SIGRTMIN + 92, |
| 268 | __SIGRTMIN + 93, |
| 269 | __SIGRTMIN + 94, |
| 270 | __SIGRTMIN + 95, |
| 271 | -1, /* SIGINFO */ |
| 272 | -1, /* UNKNOWN */ |
| 273 | -1, /* DEFAULT */ |
| 274 | -1, |
| 275 | -1, |
| 276 | -1, |
| 277 | -1, |
| 278 | -1, |
| 279 | -1 |
blueswir1 | c72d5bf | 2009-01-15 17:27:45 +0000 | [diff] [blame] | 280 | #endif |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 281 | }; |
bellard | 8f447cc | 2006-06-14 15:21:14 +0000 | [diff] [blame] | 282 | #else |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 283 | /* In system mode we only need SIGINT and SIGTRAP; other signals |
| 284 | are not yet supported. */ |
| 285 | |
| 286 | enum { |
| 287 | TARGET_SIGINT = 2, |
| 288 | TARGET_SIGTRAP = 5 |
| 289 | }; |
| 290 | |
| 291 | static int gdb_signal_table[] = { |
| 292 | -1, |
| 293 | -1, |
| 294 | TARGET_SIGINT, |
| 295 | -1, |
| 296 | -1, |
| 297 | TARGET_SIGTRAP |
| 298 | }; |
bellard | 8f447cc | 2006-06-14 15:21:14 +0000 | [diff] [blame] | 299 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 300 | |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 301 | #ifdef CONFIG_USER_ONLY |
| 302 | static int target_signal_to_gdb (int sig) |
| 303 | { |
| 304 | int i; |
| 305 | for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++) |
| 306 | if (gdb_signal_table[i] == sig) |
| 307 | return i; |
| 308 | return GDB_SIGNAL_UNKNOWN; |
| 309 | } |
| 310 | #endif |
| 311 | |
| 312 | static int gdb_signal_to_target (int sig) |
| 313 | { |
| 314 | if (sig < ARRAY_SIZE (gdb_signal_table)) |
| 315 | return gdb_signal_table[sig]; |
| 316 | else |
| 317 | return -1; |
| 318 | } |
| 319 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 320 | typedef struct GDBRegisterState { |
| 321 | int base_reg; |
| 322 | int num_regs; |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 323 | gdb_get_reg_cb get_reg; |
| 324 | gdb_set_reg_cb set_reg; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 325 | const char *xml; |
| 326 | struct GDBRegisterState *next; |
| 327 | } GDBRegisterState; |
| 328 | |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 329 | typedef struct GDBProcess { |
| 330 | uint32_t pid; |
| 331 | bool attached; |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 332 | |
| 333 | char target_xml[1024]; |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 334 | } GDBProcess; |
| 335 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 336 | enum RSState { |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 337 | RS_INACTIVE, |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 338 | RS_IDLE, |
| 339 | RS_GETLINE, |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 340 | RS_GETLINE_ESC, |
| 341 | RS_GETLINE_RLE, |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 342 | RS_CHKSUM1, |
| 343 | RS_CHKSUM2, |
| 344 | }; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 345 | typedef struct GDBState { |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 346 | bool init; /* have we been initialised? */ |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 347 | CPUState *c_cpu; /* current CPU for step/continue ops */ |
| 348 | CPUState *g_cpu; /* current CPU for other ops */ |
Andreas Färber | 52f3462 | 2013-06-27 13:44:40 +0200 | [diff] [blame] | 349 | CPUState *query_cpu; /* for q{f|s}ThreadInfo */ |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 350 | enum RSState state; /* parsing state */ |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 351 | char line_buf[MAX_PACKET_LENGTH]; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 352 | int line_buf_index; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 353 | int line_sum; /* running checksum */ |
| 354 | int line_csum; /* checksum at the end of the packet */ |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 355 | GByteArray *last_packet; |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 356 | int signal; |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 357 | #ifdef CONFIG_USER_ONLY |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 358 | int fd; |
Alex Bennée | fcedd92 | 2020-04-30 20:01:19 +0100 | [diff] [blame] | 359 | char *socket_path; |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 360 | int running_state; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 361 | #else |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 362 | CharBackend chr; |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 363 | Chardev *mon_chr; |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 364 | #endif |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 365 | bool multiprocess; |
| 366 | GDBProcess *processes; |
| 367 | int process_num; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 368 | char syscall_buf[256]; |
| 369 | gdb_syscall_complete_cb current_syscall_cb; |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 370 | GString *str_buf; |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 371 | GByteArray *mem_buf; |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 372 | int sstep_flags; |
| 373 | int supported_sstep_flags; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 374 | } GDBState; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 375 | |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 376 | static GDBState gdbserver_state; |
| 377 | |
| 378 | static void init_gdbserver_state(void) |
| 379 | { |
| 380 | g_assert(!gdbserver_state.init); |
| 381 | memset(&gdbserver_state, 0, sizeof(GDBState)); |
| 382 | gdbserver_state.init = true; |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 383 | gdbserver_state.str_buf = g_string_new(NULL); |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 384 | gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH); |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 385 | 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] | 386 | |
| 387 | /* |
Alex Bennée | 3b7a938 | 2022-09-29 12:42:23 +0100 | [diff] [blame] | 388 | * What single-step modes are supported is accelerator dependent. |
| 389 | * By default try to use no IRQs and no timers while single |
| 390 | * 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] | 391 | */ |
Alex Bennée | 3b7a938 | 2022-09-29 12:42:23 +0100 | [diff] [blame] | 392 | gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags(); |
Maxim Levitsky | 12bc5b4 | 2021-11-11 12:06:03 +0100 | [diff] [blame] | 393 | gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER; |
| 394 | gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags; |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | #ifndef CONFIG_USER_ONLY |
| 398 | static void reset_gdbserver_state(void) |
| 399 | { |
| 400 | g_free(gdbserver_state.processes); |
| 401 | gdbserver_state.processes = NULL; |
| 402 | gdbserver_state.process_num = 0; |
| 403 | } |
| 404 | #endif |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 405 | |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 406 | bool gdb_has_xml; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 407 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 408 | #ifdef CONFIG_USER_ONLY |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 409 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 410 | static int get_char(void) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 411 | { |
| 412 | uint8_t ch; |
| 413 | int ret; |
| 414 | |
| 415 | for(;;) { |
Marc-André Lureau | e7b7942 | 2022-02-19 01:34:50 +0400 | [diff] [blame] | 416 | ret = recv(gdbserver_state.fd, &ch, 1, 0); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 417 | if (ret < 0) { |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 418 | if (errno == ECONNRESET) |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 419 | gdbserver_state.fd = -1; |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 420 | if (errno != EINTR) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 421 | return -1; |
| 422 | } else if (ret == 0) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 423 | close(gdbserver_state.fd); |
| 424 | gdbserver_state.fd = -1; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 425 | return -1; |
| 426 | } else { |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 | return ch; |
| 431 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 432 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 433 | |
Peter Maydell | ebf1b4c | 2022-06-10 14:32:36 +0100 | [diff] [blame] | 434 | /* |
| 435 | * Return true if there is a GDB currently connected to the stub |
| 436 | * and attached to a CPU |
| 437 | */ |
| 438 | static bool gdb_attached(void) |
| 439 | { |
| 440 | return gdbserver_state.init && gdbserver_state.c_cpu; |
| 441 | } |
| 442 | |
blueswir1 | 654efcf | 2009-04-18 07:29:59 +0000 | [diff] [blame] | 443 | static enum { |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 444 | GDB_SYS_UNKNOWN, |
| 445 | GDB_SYS_ENABLED, |
| 446 | GDB_SYS_DISABLED, |
| 447 | } gdb_syscall_mode; |
| 448 | |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 449 | /* Decide if either remote gdb syscalls or native file IO should be used. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 450 | int use_gdb_syscalls(void) |
| 451 | { |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 452 | SemihostingTarget target = semihosting_get_target(); |
| 453 | if (target == SEMIHOSTING_TARGET_NATIVE) { |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 454 | /* -semihosting-config target=native */ |
| 455 | return false; |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 456 | } else if (target == SEMIHOSTING_TARGET_GDB) { |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 457 | /* -semihosting-config target=gdb */ |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | /* -semihosting-config target=auto */ |
| 462 | /* On the first call check if gdb is connected and remember. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 463 | if (gdb_syscall_mode == GDB_SYS_UNKNOWN) { |
Peter Maydell | ebf1b4c | 2022-06-10 14:32:36 +0100 | [diff] [blame] | 464 | gdb_syscall_mode = gdb_attached() ? GDB_SYS_ENABLED : GDB_SYS_DISABLED; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 465 | } |
| 466 | return gdb_syscall_mode == GDB_SYS_ENABLED; |
| 467 | } |
| 468 | |
Alex Bennée | ed12f5b | 2021-05-20 18:43:02 +0100 | [diff] [blame] | 469 | static bool stub_can_reverse(void) |
| 470 | { |
| 471 | #ifdef CONFIG_USER_ONLY |
| 472 | return false; |
| 473 | #else |
| 474 | return replay_mode == REPLAY_MODE_PLAY; |
| 475 | #endif |
| 476 | } |
| 477 | |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 478 | /* Resume execution. */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 479 | static inline void gdb_continue(void) |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 480 | { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 481 | |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 482 | #ifdef CONFIG_USER_ONLY |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 483 | gdbserver_state.running_state = 1; |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 484 | trace_gdbstub_op_continue(); |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 485 | #else |
Paolo Bonzini | 26ac7a3 | 2013-06-03 17:06:54 +0200 | [diff] [blame] | 486 | if (!runstate_needs_reset()) { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 487 | trace_gdbstub_op_continue(); |
Paolo Bonzini | 87f25c1 | 2013-05-30 13:20:40 +0200 | [diff] [blame] | 488 | vm_start(); |
| 489 | } |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 490 | #endif |
| 491 | } |
| 492 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 493 | /* |
| 494 | * Resume execution, per CPU actions. For user-mode emulation it's |
| 495 | * equivalent to gdb_continue. |
| 496 | */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 497 | static int gdb_continue_partial(char *newstates) |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 498 | { |
| 499 | CPUState *cpu; |
| 500 | int res = 0; |
| 501 | #ifdef CONFIG_USER_ONLY |
| 502 | /* |
| 503 | * This is not exactly accurate, but it's an improvement compared to the |
| 504 | * previous situation, where only one CPU would be single-stepped. |
| 505 | */ |
| 506 | CPU_FOREACH(cpu) { |
| 507 | if (newstates[cpu->cpu_index] == 's') { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 508 | trace_gdbstub_op_stepping(cpu->cpu_index); |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 509 | cpu_single_step(cpu, gdbserver_state.sstep_flags); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 510 | } |
| 511 | } |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 512 | gdbserver_state.running_state = 1; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 513 | #else |
| 514 | int flag = 0; |
| 515 | |
| 516 | if (!runstate_needs_reset()) { |
Ivan Shcherbakov | d7482ff | 2022-03-02 17:28:33 -0800 | [diff] [blame] | 517 | bool step_requested = false; |
| 518 | CPU_FOREACH(cpu) { |
| 519 | if (newstates[cpu->cpu_index] == 's') { |
| 520 | step_requested = true; |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | if (vm_prepare_start(step_requested)) { |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | CPU_FOREACH(cpu) { |
| 530 | switch (newstates[cpu->cpu_index]) { |
| 531 | case 0: |
| 532 | case 1: |
| 533 | break; /* nothing to do here */ |
| 534 | case 's': |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 535 | trace_gdbstub_op_stepping(cpu->cpu_index); |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 536 | cpu_single_step(cpu, gdbserver_state.sstep_flags); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 537 | cpu_resume(cpu); |
| 538 | flag = 1; |
| 539 | break; |
| 540 | case 'c': |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 541 | trace_gdbstub_op_continue_cpu(cpu->cpu_index); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 542 | cpu_resume(cpu); |
| 543 | flag = 1; |
| 544 | break; |
| 545 | default: |
| 546 | res = -1; |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | if (flag) { |
| 552 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); |
| 553 | } |
| 554 | #endif |
| 555 | return res; |
| 556 | } |
| 557 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 558 | static void put_buffer(const uint8_t *buf, int len) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 559 | { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 560 | #ifdef CONFIG_USER_ONLY |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 561 | int ret; |
| 562 | |
| 563 | while (len > 0) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 564 | ret = send(gdbserver_state.fd, buf, len, 0); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 565 | if (ret < 0) { |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 566 | if (errno != EINTR) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 567 | return; |
| 568 | } else { |
| 569 | buf += ret; |
| 570 | len -= ret; |
| 571 | } |
| 572 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 573 | #else |
Daniel P. Berrange | 6ab3fc3 | 2016-09-06 14:56:04 +0100 | [diff] [blame] | 574 | /* XXX this blocks entire thread. Rewrite to use |
| 575 | * qemu_chr_fe_write and background I/O callbacks */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 576 | qemu_chr_fe_write_all(&gdbserver_state.chr, buf, len); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 577 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static inline int fromhex(int v) |
| 581 | { |
| 582 | if (v >= '0' && v <= '9') |
| 583 | return v - '0'; |
| 584 | else if (v >= 'A' && v <= 'F') |
| 585 | return v - 'A' + 10; |
| 586 | else if (v >= 'a' && v <= 'f') |
| 587 | return v - 'a' + 10; |
| 588 | else |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static inline int tohex(int v) |
| 593 | { |
| 594 | if (v < 10) |
| 595 | return v + '0'; |
| 596 | else |
| 597 | return v - 10 + 'a'; |
| 598 | } |
| 599 | |
Philippe Mathieu-Daudé | 9005774 | 2018-04-08 11:59:33 -0300 | [diff] [blame] | 600 | /* writes 2*len+1 bytes in buf */ |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 601 | static void memtohex(GString *buf, const uint8_t *mem, int len) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 602 | { |
| 603 | int i, c; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 604 | for(i = 0; i < len; i++) { |
| 605 | c = mem[i]; |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 606 | g_string_append_c(buf, tohex(c >> 4)); |
| 607 | g_string_append_c(buf, tohex(c & 0xf)); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 608 | } |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 609 | g_string_append_c(buf, '\0'); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 612 | static void hextomem(GByteArray *mem, const char *buf, int len) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 613 | { |
| 614 | int i; |
| 615 | |
| 616 | for(i = 0; i < len; i++) { |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 617 | guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]); |
| 618 | g_byte_array_append(mem, &byte, 1); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 619 | buf += 2; |
| 620 | } |
| 621 | } |
| 622 | |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 623 | static void hexdump(const char *buf, int len, |
| 624 | void (*trace_fn)(size_t ofs, char const *text)) |
| 625 | { |
| 626 | char line_buffer[3 * 16 + 4 + 16 + 1]; |
| 627 | |
| 628 | size_t i; |
| 629 | for (i = 0; i < len || (i & 0xF); ++i) { |
| 630 | size_t byte_ofs = i & 15; |
| 631 | |
| 632 | if (byte_ofs == 0) { |
| 633 | memset(line_buffer, ' ', 3 * 16 + 4 + 16); |
| 634 | line_buffer[3 * 16 + 4 + 16] = 0; |
| 635 | } |
| 636 | |
| 637 | size_t col_group = (i >> 2) & 3; |
| 638 | size_t hex_col = byte_ofs * 3 + col_group; |
| 639 | size_t txt_col = 3 * 16 + 4 + byte_ofs; |
| 640 | |
| 641 | if (i < len) { |
| 642 | char value = buf[i]; |
| 643 | |
| 644 | line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF); |
| 645 | line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF); |
| 646 | line_buffer[txt_col + 0] = (value >= ' ' && value < 127) |
| 647 | ? value |
| 648 | : '.'; |
| 649 | } |
| 650 | |
| 651 | if (byte_ofs == 0xF) |
| 652 | trace_fn(i & -16, line_buffer); |
| 653 | } |
| 654 | } |
| 655 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 656 | /* return -1 if error, 0 if OK */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 657 | static int put_packet_binary(const char *buf, int len, bool dump) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 658 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 659 | int csum, i; |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 660 | uint8_t footer[3]; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 661 | |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 662 | if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) { |
| 663 | hexdump(buf, len, trace_gdbstub_io_binaryreply); |
| 664 | } |
| 665 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 666 | for(;;) { |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 667 | g_byte_array_set_size(gdbserver_state.last_packet, 0); |
| 668 | g_byte_array_append(gdbserver_state.last_packet, |
| 669 | (const uint8_t *) "$", 1); |
| 670 | g_byte_array_append(gdbserver_state.last_packet, |
| 671 | (const uint8_t *) buf, len); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 672 | csum = 0; |
| 673 | for(i = 0; i < len; i++) { |
| 674 | csum += buf[i]; |
| 675 | } |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 676 | footer[0] = '#'; |
| 677 | footer[1] = tohex((csum >> 4) & 0xf); |
| 678 | footer[2] = tohex((csum) & 0xf); |
| 679 | g_byte_array_append(gdbserver_state.last_packet, footer, 3); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 680 | |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 681 | put_buffer(gdbserver_state.last_packet->data, |
| 682 | gdbserver_state.last_packet->len); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 683 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 684 | #ifdef CONFIG_USER_ONLY |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 685 | i = get_char(); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 686 | if (i < 0) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 687 | return -1; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 688 | if (i == '+') |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 689 | break; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 690 | #else |
| 691 | break; |
| 692 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 693 | } |
| 694 | return 0; |
| 695 | } |
| 696 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 697 | /* return -1 if error, 0 if OK */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 698 | static int put_packet(const char *buf) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 699 | { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 700 | trace_gdbstub_io_reply(buf); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 701 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 702 | return put_packet_binary(buf, strlen(buf), false); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 705 | static void put_strbuf(void) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 706 | { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 707 | put_packet(gdbserver_state.str_buf->str); |
| 708 | } |
| 709 | |
| 710 | /* Encode data using the encoding for 'x' packets. */ |
| 711 | static void memtox(GString *buf, const char *mem, int len) |
| 712 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 713 | char c; |
| 714 | |
| 715 | while (len--) { |
| 716 | c = *(mem++); |
| 717 | switch (c) { |
| 718 | case '#': case '$': case '*': case '}': |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 719 | g_string_append_c(buf, '}'); |
| 720 | g_string_append_c(buf, c ^ 0x20); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 721 | break; |
| 722 | default: |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 723 | g_string_append_c(buf, c); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 724 | break; |
| 725 | } |
| 726 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 729 | static uint32_t gdb_get_cpu_pid(CPUState *cpu) |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 730 | { |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 731 | /* TODO: In user mode, we should use the task state PID */ |
Peter Maydell | 46f5abc | 2019-01-29 11:46:06 +0000 | [diff] [blame] | 732 | if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) { |
| 733 | /* Return the default process' PID */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 734 | int index = gdbserver_state.process_num - 1; |
| 735 | return gdbserver_state.processes[index].pid; |
Peter Maydell | 46f5abc | 2019-01-29 11:46:06 +0000 | [diff] [blame] | 736 | } |
| 737 | return cpu->cluster_index + 1; |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 740 | static GDBProcess *gdb_get_process(uint32_t pid) |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 741 | { |
| 742 | int i; |
| 743 | |
| 744 | if (!pid) { |
| 745 | /* 0 means any process, we take the first one */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 746 | return &gdbserver_state.processes[0]; |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 749 | for (i = 0; i < gdbserver_state.process_num; i++) { |
| 750 | if (gdbserver_state.processes[i].pid == pid) { |
| 751 | return &gdbserver_state.processes[i]; |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
| 755 | return NULL; |
| 756 | } |
| 757 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 758 | static GDBProcess *gdb_get_cpu_process(CPUState *cpu) |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 759 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 760 | return gdb_get_process(gdb_get_cpu_pid(cpu)); |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | static CPUState *find_cpu(uint32_t thread_id) |
| 764 | { |
| 765 | CPUState *cpu; |
| 766 | |
| 767 | CPU_FOREACH(cpu) { |
| 768 | if (cpu_gdb_index(cpu) == thread_id) { |
| 769 | return cpu; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | return NULL; |
| 774 | } |
| 775 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 776 | static CPUState *get_first_cpu_in_process(GDBProcess *process) |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 777 | { |
| 778 | CPUState *cpu; |
| 779 | |
| 780 | CPU_FOREACH(cpu) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 781 | if (gdb_get_cpu_pid(cpu) == process->pid) { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 782 | return cpu; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | return NULL; |
| 787 | } |
| 788 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 789 | static CPUState *gdb_next_cpu_in_process(CPUState *cpu) |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 790 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 791 | uint32_t pid = gdb_get_cpu_pid(cpu); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 792 | cpu = CPU_NEXT(cpu); |
| 793 | |
| 794 | while (cpu) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 795 | if (gdb_get_cpu_pid(cpu) == pid) { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 796 | break; |
| 797 | } |
| 798 | |
| 799 | cpu = CPU_NEXT(cpu); |
| 800 | } |
| 801 | |
| 802 | return cpu; |
| 803 | } |
| 804 | |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 805 | /* Return the cpu following @cpu, while ignoring unattached processes. */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 806 | static CPUState *gdb_next_attached_cpu(CPUState *cpu) |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 807 | { |
| 808 | cpu = CPU_NEXT(cpu); |
| 809 | |
| 810 | while (cpu) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 811 | if (gdb_get_cpu_process(cpu)->attached) { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 812 | break; |
| 813 | } |
| 814 | |
| 815 | cpu = CPU_NEXT(cpu); |
| 816 | } |
| 817 | |
| 818 | return cpu; |
| 819 | } |
| 820 | |
| 821 | /* Return the first attached cpu */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 822 | static CPUState *gdb_first_attached_cpu(void) |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 823 | { |
| 824 | CPUState *cpu = first_cpu; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 825 | GDBProcess *process = gdb_get_cpu_process(cpu); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 826 | |
| 827 | if (!process->attached) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 828 | return gdb_next_attached_cpu(cpu); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | return cpu; |
| 832 | } |
| 833 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 834 | static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid) |
Luc Michel | ab65eed | 2019-01-29 11:46:03 +0000 | [diff] [blame] | 835 | { |
| 836 | GDBProcess *process; |
| 837 | CPUState *cpu; |
| 838 | |
| 839 | if (!pid && !tid) { |
| 840 | /* 0 means any process/thread, we take the first attached one */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 841 | return gdb_first_attached_cpu(); |
Luc Michel | ab65eed | 2019-01-29 11:46:03 +0000 | [diff] [blame] | 842 | } else if (pid && !tid) { |
| 843 | /* any thread in a specific process */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 844 | process = gdb_get_process(pid); |
Luc Michel | ab65eed | 2019-01-29 11:46:03 +0000 | [diff] [blame] | 845 | |
| 846 | if (process == NULL) { |
| 847 | return NULL; |
| 848 | } |
| 849 | |
| 850 | if (!process->attached) { |
| 851 | return NULL; |
| 852 | } |
| 853 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 854 | return get_first_cpu_in_process(process); |
Luc Michel | ab65eed | 2019-01-29 11:46:03 +0000 | [diff] [blame] | 855 | } else { |
| 856 | /* a specific thread */ |
| 857 | cpu = find_cpu(tid); |
| 858 | |
| 859 | if (cpu == NULL) { |
| 860 | return NULL; |
| 861 | } |
| 862 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 863 | process = gdb_get_cpu_process(cpu); |
Luc Michel | ab65eed | 2019-01-29 11:46:03 +0000 | [diff] [blame] | 864 | |
| 865 | if (pid && process->pid != pid) { |
| 866 | return NULL; |
| 867 | } |
| 868 | |
| 869 | if (!process->attached) { |
| 870 | return NULL; |
| 871 | } |
| 872 | |
| 873 | return cpu; |
| 874 | } |
| 875 | } |
| 876 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 877 | static const char *get_feature_xml(const char *p, const char **newp, |
| 878 | GDBProcess *process) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 879 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 880 | size_t len; |
| 881 | int i; |
| 882 | const char *name; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 883 | CPUState *cpu = get_first_cpu_in_process(process); |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 884 | CPUClass *cc = CPU_GET_CLASS(cpu); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 885 | |
| 886 | len = 0; |
| 887 | while (p[len] && p[len] != ':') |
| 888 | len++; |
| 889 | *newp = p + len; |
| 890 | |
| 891 | name = NULL; |
| 892 | if (strncmp(p, "target.xml", len) == 0) { |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 893 | char *buf = process->target_xml; |
| 894 | const size_t buf_sz = sizeof(process->target_xml); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 895 | |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 896 | /* Generate the XML description for this CPU. */ |
| 897 | if (!buf[0]) { |
| 898 | GDBRegisterState *r; |
| 899 | |
| 900 | pstrcat(buf, buf_sz, |
David Hildenbrand | b3820e6 | 2015-12-03 13:14:41 +0100 | [diff] [blame] | 901 | "<?xml version=\"1.0\"?>" |
| 902 | "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">" |
| 903 | "<target>"); |
| 904 | if (cc->gdb_arch_name) { |
| 905 | gchar *arch = cc->gdb_arch_name(cpu); |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 906 | pstrcat(buf, buf_sz, "<architecture>"); |
| 907 | pstrcat(buf, buf_sz, arch); |
| 908 | pstrcat(buf, buf_sz, "</architecture>"); |
David Hildenbrand | b3820e6 | 2015-12-03 13:14:41 +0100 | [diff] [blame] | 909 | g_free(arch); |
| 910 | } |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 911 | pstrcat(buf, buf_sz, "<xi:include href=\""); |
| 912 | pstrcat(buf, buf_sz, cc->gdb_core_xml_file); |
| 913 | pstrcat(buf, buf_sz, "\"/>"); |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 914 | for (r = cpu->gdb_regs; r; r = r->next) { |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 915 | pstrcat(buf, buf_sz, "<xi:include href=\""); |
| 916 | pstrcat(buf, buf_sz, r->xml); |
| 917 | pstrcat(buf, buf_sz, "\"/>"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 918 | } |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 919 | pstrcat(buf, buf_sz, "</target>"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 920 | } |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 921 | return buf; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 922 | } |
Abdallah Bouassida | 200bf5b | 2018-05-18 17:48:07 +0100 | [diff] [blame] | 923 | if (cc->gdb_get_dynamic_xml) { |
Abdallah Bouassida | 200bf5b | 2018-05-18 17:48:07 +0100 | [diff] [blame] | 924 | char *xmlname = g_strndup(p, len); |
| 925 | const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname); |
| 926 | |
| 927 | g_free(xmlname); |
| 928 | if (xml) { |
| 929 | return xml; |
| 930 | } |
| 931 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 932 | for (i = 0; ; i++) { |
| 933 | name = xml_builtin[i][0]; |
| 934 | if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len)) |
| 935 | break; |
| 936 | } |
| 937 | return name ? xml_builtin[i][1] : NULL; |
| 938 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 939 | |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 940 | static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 941 | { |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 942 | CPUClass *cc = CPU_GET_CLASS(cpu); |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 943 | CPUArchState *env = cpu->env_ptr; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 944 | GDBRegisterState *r; |
| 945 | |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 946 | if (reg < cc->gdb_num_core_regs) { |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 947 | return cc->gdb_read_register(cpu, buf, reg); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 948 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 949 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 950 | for (r = cpu->gdb_regs; r; r = r->next) { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 951 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 952 | return r->get_reg(env, buf, reg - r->base_reg); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | return 0; |
| 956 | } |
| 957 | |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 958 | static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 959 | { |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 960 | CPUClass *cc = CPU_GET_CLASS(cpu); |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 961 | CPUArchState *env = cpu->env_ptr; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 962 | GDBRegisterState *r; |
| 963 | |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 964 | if (reg < cc->gdb_num_core_regs) { |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 965 | return cc->gdb_write_register(cpu, mem_buf, reg); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 966 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 967 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 968 | for (r = cpu->gdb_regs; r; r = r->next) { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 969 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
| 970 | return r->set_reg(env, mem_buf, reg - r->base_reg); |
| 971 | } |
| 972 | } |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | /* Register a supplemental set of CPU registers. If g_pos is nonzero it |
| 977 | specifies the first register number and these registers are included in |
| 978 | a standard "g" packet. Direction is relative to gdb, i.e. get_reg is |
| 979 | gdb reading a CPU register, and set_reg is gdb modifying a CPU register. |
| 980 | */ |
| 981 | |
Andreas Färber | 22169d4 | 2013-06-28 21:27:39 +0200 | [diff] [blame] | 982 | void gdb_register_coprocessor(CPUState *cpu, |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 983 | gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg, |
Andreas Färber | 22169d4 | 2013-06-28 21:27:39 +0200 | [diff] [blame] | 984 | int num_regs, const char *xml, int g_pos) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 985 | { |
| 986 | GDBRegisterState *s; |
| 987 | GDBRegisterState **p; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 988 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 989 | p = &cpu->gdb_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 990 | while (*p) { |
| 991 | /* Check for duplicates. */ |
| 992 | if (strcmp((*p)->xml, xml) == 0) |
| 993 | return; |
| 994 | p = &(*p)->next; |
| 995 | } |
Stefan Weil | 9643c25 | 2011-10-18 22:25:38 +0200 | [diff] [blame] | 996 | |
| 997 | s = g_new0(GDBRegisterState, 1); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 998 | s->base_reg = cpu->gdb_num_regs; |
Stefan Weil | 9643c25 | 2011-10-18 22:25:38 +0200 | [diff] [blame] | 999 | s->num_regs = num_regs; |
| 1000 | s->get_reg = get_reg; |
| 1001 | s->set_reg = set_reg; |
| 1002 | s->xml = xml; |
| 1003 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1004 | /* Add to end of list. */ |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 1005 | cpu->gdb_num_regs += num_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1006 | *p = s; |
| 1007 | if (g_pos) { |
| 1008 | if (g_pos != s->base_reg) { |
Ziyue Yang | 7ae6c57 | 2017-01-18 16:03:29 +0800 | [diff] [blame] | 1009 | error_report("Error: Bad gdb register numbering for '%s', " |
| 1010 | "expected %d got %d", xml, g_pos, s->base_reg); |
Andreas Färber | 35143f0 | 2013-08-12 18:09:47 +0200 | [diff] [blame] | 1011 | } else { |
| 1012 | cpu->gdb_num_g_regs = cpu->gdb_num_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1017 | static void gdb_process_breakpoint_remove_all(GDBProcess *p) |
Luc Michel | 546f3c6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1018 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1019 | CPUState *cpu = get_first_cpu_in_process(p); |
Luc Michel | 546f3c6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1020 | |
| 1021 | while (cpu) { |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 1022 | gdb_breakpoint_remove_all(cpu); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1023 | cpu = gdb_next_cpu_in_process(cpu); |
Luc Michel | 546f3c6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1027 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1028 | static void gdb_set_cpu_pc(target_ulong pc) |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 1029 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1030 | CPUState *cpu = gdbserver_state.c_cpu; |
Andreas Färber | f45748f | 2013-06-21 19:09:18 +0200 | [diff] [blame] | 1031 | |
| 1032 | cpu_synchronize_state(cpu); |
Peter Crosthwaite | 4a2b24e | 2015-06-23 20:19:21 -0700 | [diff] [blame] | 1033 | cpu_set_pc(cpu, pc); |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1036 | static void gdb_append_thread_id(CPUState *cpu, GString *buf) |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1037 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1038 | if (gdbserver_state.multiprocess) { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1039 | g_string_append_printf(buf, "p%02x.%02x", |
| 1040 | gdb_get_cpu_pid(cpu), cpu_gdb_index(cpu)); |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1041 | } else { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1042 | g_string_append_printf(buf, "%02x", cpu_gdb_index(cpu)); |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1043 | } |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1046 | typedef enum GDBThreadIdKind { |
| 1047 | GDB_ONE_THREAD = 0, |
| 1048 | GDB_ALL_THREADS, /* One process, all threads */ |
| 1049 | GDB_ALL_PROCESSES, |
| 1050 | GDB_READ_THREAD_ERR |
| 1051 | } GDBThreadIdKind; |
| 1052 | |
| 1053 | static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf, |
| 1054 | uint32_t *pid, uint32_t *tid) |
| 1055 | { |
| 1056 | unsigned long p, t; |
| 1057 | int ret; |
| 1058 | |
| 1059 | if (*buf == 'p') { |
| 1060 | buf++; |
| 1061 | ret = qemu_strtoul(buf, &buf, 16, &p); |
| 1062 | |
| 1063 | if (ret) { |
| 1064 | return GDB_READ_THREAD_ERR; |
| 1065 | } |
| 1066 | |
| 1067 | /* Skip '.' */ |
| 1068 | buf++; |
| 1069 | } else { |
| 1070 | p = 1; |
| 1071 | } |
| 1072 | |
| 1073 | ret = qemu_strtoul(buf, &buf, 16, &t); |
| 1074 | |
| 1075 | if (ret) { |
| 1076 | return GDB_READ_THREAD_ERR; |
| 1077 | } |
| 1078 | |
| 1079 | *end_buf = buf; |
| 1080 | |
| 1081 | if (p == -1) { |
| 1082 | return GDB_ALL_PROCESSES; |
| 1083 | } |
| 1084 | |
| 1085 | if (pid) { |
| 1086 | *pid = p; |
| 1087 | } |
| 1088 | |
| 1089 | if (t == -1) { |
| 1090 | return GDB_ALL_THREADS; |
| 1091 | } |
| 1092 | |
| 1093 | if (tid) { |
| 1094 | *tid = t; |
| 1095 | } |
| 1096 | |
| 1097 | return GDB_ONE_THREAD; |
| 1098 | } |
| 1099 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1100 | /** |
| 1101 | * gdb_handle_vcont - Parses and handles a vCont packet. |
| 1102 | * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is |
| 1103 | * a format error, 0 on success. |
| 1104 | */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1105 | static int gdb_handle_vcont(const char *p) |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1106 | { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1107 | int res, signal = 0; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1108 | char cur_action; |
| 1109 | char *newstates; |
| 1110 | unsigned long tmp; |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1111 | uint32_t pid, tid; |
| 1112 | GDBProcess *process; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1113 | CPUState *cpu; |
Luc Michel | c99ef79 | 2019-03-26 12:53:26 +0000 | [diff] [blame] | 1114 | GDBThreadIdKind kind; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1115 | #ifdef CONFIG_USER_ONLY |
| 1116 | int max_cpus = 1; /* global variable max_cpus exists only in system mode */ |
| 1117 | |
| 1118 | CPU_FOREACH(cpu) { |
| 1119 | max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus; |
| 1120 | } |
Like Xu | 5cc8767 | 2019-05-19 04:54:21 +0800 | [diff] [blame] | 1121 | #else |
| 1122 | MachineState *ms = MACHINE(qdev_get_machine()); |
| 1123 | unsigned int max_cpus = ms->smp.max_cpus; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1124 | #endif |
| 1125 | /* uninitialised CPUs stay 0 */ |
| 1126 | newstates = g_new0(char, max_cpus); |
| 1127 | |
| 1128 | /* mark valid CPUs with 1 */ |
| 1129 | CPU_FOREACH(cpu) { |
| 1130 | newstates[cpu->cpu_index] = 1; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * res keeps track of what error we are returning, with -ENOTSUP meaning |
| 1135 | * that the command is unknown or unsupported, thus returning an empty |
| 1136 | * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid, |
| 1137 | * or incorrect parameters passed. |
| 1138 | */ |
| 1139 | res = 0; |
| 1140 | while (*p) { |
| 1141 | if (*p++ != ';') { |
| 1142 | res = -ENOTSUP; |
| 1143 | goto out; |
| 1144 | } |
| 1145 | |
| 1146 | cur_action = *p++; |
| 1147 | if (cur_action == 'C' || cur_action == 'S') { |
Peter Maydell | 95a5bef | 2017-07-20 17:31:30 +0100 | [diff] [blame] | 1148 | cur_action = qemu_tolower(cur_action); |
Peter Maydell | 3ddd903 | 2020-11-21 21:03:42 +0000 | [diff] [blame] | 1149 | res = qemu_strtoul(p, &p, 16, &tmp); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1150 | if (res) { |
| 1151 | goto out; |
| 1152 | } |
| 1153 | signal = gdb_signal_to_target(tmp); |
| 1154 | } else if (cur_action != 'c' && cur_action != 's') { |
| 1155 | /* unknown/invalid/unsupported command */ |
| 1156 | res = -ENOTSUP; |
| 1157 | goto out; |
| 1158 | } |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1159 | |
Luc Michel | c99ef79 | 2019-03-26 12:53:26 +0000 | [diff] [blame] | 1160 | if (*p == '\0' || *p == ';') { |
| 1161 | /* |
| 1162 | * No thread specifier, action is on "all threads". The |
| 1163 | * specification is unclear regarding the process to act on. We |
| 1164 | * choose all processes. |
| 1165 | */ |
| 1166 | kind = GDB_ALL_PROCESSES; |
| 1167 | } else if (*p++ == ':') { |
| 1168 | kind = read_thread_id(p, &p, &pid, &tid); |
| 1169 | } else { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1170 | res = -ENOTSUP; |
| 1171 | goto out; |
| 1172 | } |
| 1173 | |
Luc Michel | c99ef79 | 2019-03-26 12:53:26 +0000 | [diff] [blame] | 1174 | switch (kind) { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1175 | case GDB_READ_THREAD_ERR: |
| 1176 | res = -EINVAL; |
| 1177 | goto out; |
| 1178 | |
| 1179 | case GDB_ALL_PROCESSES: |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1180 | cpu = gdb_first_attached_cpu(); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1181 | while (cpu) { |
| 1182 | if (newstates[cpu->cpu_index] == 1) { |
| 1183 | newstates[cpu->cpu_index] = cur_action; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1184 | } |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1185 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1186 | cpu = gdb_next_attached_cpu(cpu); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1187 | } |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1188 | break; |
| 1189 | |
| 1190 | case GDB_ALL_THREADS: |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1191 | process = gdb_get_process(pid); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1192 | |
| 1193 | if (!process->attached) { |
| 1194 | res = -EINVAL; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1195 | goto out; |
| 1196 | } |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1197 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1198 | cpu = get_first_cpu_in_process(process); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1199 | while (cpu) { |
| 1200 | if (newstates[cpu->cpu_index] == 1) { |
| 1201 | newstates[cpu->cpu_index] = cur_action; |
| 1202 | } |
| 1203 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1204 | cpu = gdb_next_cpu_in_process(cpu); |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1205 | } |
| 1206 | break; |
| 1207 | |
| 1208 | case GDB_ONE_THREAD: |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1209 | cpu = gdb_get_cpu(pid, tid); |
Alex Bennée | 5a6a1ad | 2017-07-12 11:52:16 +0100 | [diff] [blame] | 1210 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1211 | /* invalid CPU/thread specified */ |
Alex Bennée | 5a6a1ad | 2017-07-12 11:52:16 +0100 | [diff] [blame] | 1212 | if (!cpu) { |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1213 | res = -EINVAL; |
| 1214 | goto out; |
| 1215 | } |
Alex Bennée | 5a6a1ad | 2017-07-12 11:52:16 +0100 | [diff] [blame] | 1216 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1217 | /* only use if no previous match occourred */ |
| 1218 | if (newstates[cpu->cpu_index] == 1) { |
| 1219 | newstates[cpu->cpu_index] = cur_action; |
| 1220 | } |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1221 | break; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1222 | } |
| 1223 | } |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1224 | gdbserver_state.signal = signal; |
| 1225 | gdb_continue_partial(newstates); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1226 | |
| 1227 | out: |
| 1228 | g_free(newstates); |
| 1229 | |
| 1230 | return res; |
| 1231 | } |
| 1232 | |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1233 | typedef union GdbCmdVariant { |
| 1234 | const char *data; |
| 1235 | uint8_t opcode; |
| 1236 | unsigned long val_ul; |
| 1237 | unsigned long long val_ull; |
| 1238 | struct { |
| 1239 | GDBThreadIdKind kind; |
| 1240 | uint32_t pid; |
| 1241 | uint32_t tid; |
| 1242 | } thread_id; |
| 1243 | } GdbCmdVariant; |
| 1244 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1245 | #define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i)) |
| 1246 | |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1247 | static const char *cmd_next_param(const char *param, const char delimiter) |
| 1248 | { |
| 1249 | static const char all_delimiters[] = ",;:="; |
| 1250 | char curr_delimiters[2] = {0}; |
| 1251 | const char *delimiters; |
| 1252 | |
| 1253 | if (delimiter == '?') { |
| 1254 | delimiters = all_delimiters; |
| 1255 | } else if (delimiter == '0') { |
| 1256 | return strchr(param, '\0'); |
| 1257 | } else if (delimiter == '.' && *param) { |
| 1258 | return param + 1; |
| 1259 | } else { |
| 1260 | curr_delimiters[0] = delimiter; |
| 1261 | delimiters = curr_delimiters; |
| 1262 | } |
| 1263 | |
| 1264 | param += strcspn(param, delimiters); |
| 1265 | if (*param) { |
| 1266 | param++; |
| 1267 | } |
| 1268 | return param; |
| 1269 | } |
| 1270 | |
| 1271 | static int cmd_parse_params(const char *data, const char *schema, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1272 | GArray *params) |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1273 | { |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1274 | const char *curr_schema, *curr_data; |
| 1275 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1276 | g_assert(schema); |
| 1277 | g_assert(params->len == 0); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1278 | |
| 1279 | curr_schema = schema; |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1280 | curr_data = data; |
| 1281 | while (curr_schema[0] && curr_schema[1] && *curr_data) { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1282 | GdbCmdVariant this_param; |
| 1283 | |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1284 | switch (curr_schema[0]) { |
| 1285 | case 'l': |
| 1286 | if (qemu_strtoul(curr_data, &curr_data, 16, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1287 | &this_param.val_ul)) { |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1288 | return -EINVAL; |
| 1289 | } |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1290 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1291 | g_array_append_val(params, this_param); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1292 | break; |
| 1293 | case 'L': |
| 1294 | if (qemu_strtou64(curr_data, &curr_data, 16, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1295 | (uint64_t *)&this_param.val_ull)) { |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1296 | return -EINVAL; |
| 1297 | } |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1298 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1299 | g_array_append_val(params, this_param); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1300 | break; |
| 1301 | case 's': |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1302 | this_param.data = curr_data; |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1303 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1304 | g_array_append_val(params, this_param); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1305 | break; |
| 1306 | case 'o': |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1307 | this_param.opcode = *(uint8_t *)curr_data; |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1308 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1309 | g_array_append_val(params, this_param); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1310 | break; |
| 1311 | case 't': |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1312 | this_param.thread_id.kind = |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1313 | read_thread_id(curr_data, &curr_data, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1314 | &this_param.thread_id.pid, |
| 1315 | &this_param.thread_id.tid); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1316 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1317 | g_array_append_val(params, this_param); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1318 | break; |
| 1319 | case '?': |
| 1320 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
| 1321 | break; |
| 1322 | default: |
| 1323 | return -EINVAL; |
| 1324 | } |
| 1325 | curr_schema += 2; |
| 1326 | } |
| 1327 | |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1328 | return 0; |
| 1329 | } |
| 1330 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1331 | typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1332 | |
| 1333 | /* |
| 1334 | * cmd_startswith -> cmd is compared using startswith |
| 1335 | * |
| 1336 | * |
| 1337 | * schema definitions: |
| 1338 | * Each schema parameter entry consists of 2 chars, |
| 1339 | * the first char represents the parameter type handling |
| 1340 | * the second char represents the delimiter for the next parameter |
| 1341 | * |
| 1342 | * Currently supported schema types: |
| 1343 | * 'l' -> unsigned long (stored in .val_ul) |
| 1344 | * 'L' -> unsigned long long (stored in .val_ull) |
| 1345 | * 's' -> string (stored in .data) |
| 1346 | * 'o' -> single char (stored in .opcode) |
| 1347 | * 't' -> thread id (stored in .thread_id) |
| 1348 | * '?' -> skip according to delimiter |
| 1349 | * |
| 1350 | * Currently supported delimiters: |
| 1351 | * '?' -> Stop at any delimiter (",;:=\0") |
| 1352 | * '0' -> Stop at "\0" |
| 1353 | * '.' -> Skip 1 char unless reached "\0" |
| 1354 | * Any other value is treated as the delimiter value itself |
| 1355 | */ |
| 1356 | typedef struct GdbCmdParseEntry { |
| 1357 | GdbCmdHandler handler; |
| 1358 | const char *cmd; |
| 1359 | bool cmd_startswith; |
| 1360 | const char *schema; |
| 1361 | } GdbCmdParseEntry; |
| 1362 | |
| 1363 | static inline int startswith(const char *string, const char *pattern) |
| 1364 | { |
| 1365 | return !strncmp(string, pattern, strlen(pattern)); |
| 1366 | } |
| 1367 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1368 | static int process_string_cmd(void *user_ctx, const char *data, |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1369 | const GdbCmdParseEntry *cmds, int num_cmds) |
| 1370 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1371 | int i; |
| 1372 | g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant)); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1373 | |
| 1374 | if (!cmds) { |
| 1375 | return -1; |
| 1376 | } |
| 1377 | |
| 1378 | for (i = 0; i < num_cmds; i++) { |
| 1379 | const GdbCmdParseEntry *cmd = &cmds[i]; |
| 1380 | g_assert(cmd->handler && cmd->cmd); |
| 1381 | |
| 1382 | if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) || |
| 1383 | (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) { |
| 1384 | continue; |
| 1385 | } |
| 1386 | |
| 1387 | if (cmd->schema) { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1388 | if (cmd_parse_params(&data[strlen(cmd->cmd)], |
| 1389 | cmd->schema, params)) { |
| 1390 | return -1; |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1391 | } |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1392 | } |
| 1393 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1394 | cmd->handler(params, user_ctx); |
Jon Doron | d14055d | 2019-05-29 09:41:29 +0300 | [diff] [blame] | 1395 | return 0; |
| 1396 | } |
| 1397 | |
| 1398 | return -1; |
| 1399 | } |
| 1400 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1401 | static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd) |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1402 | { |
| 1403 | if (!data) { |
| 1404 | return; |
| 1405 | } |
| 1406 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1407 | g_string_set_size(gdbserver_state.str_buf, 0); |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1408 | g_byte_array_set_size(gdbserver_state.mem_buf, 0); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1409 | |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1410 | /* In case there was an error during the command parsing we must |
| 1411 | * send a NULL packet to indicate the command is not supported */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1412 | if (process_string_cmd(NULL, data, cmd, 1)) { |
| 1413 | put_packet(""); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1417 | static void handle_detach(GArray *params, void *user_ctx) |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1418 | { |
| 1419 | GDBProcess *process; |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1420 | uint32_t pid = 1; |
| 1421 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1422 | if (gdbserver_state.multiprocess) { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1423 | if (!params->len) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1424 | put_packet("E22"); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1425 | return; |
| 1426 | } |
| 1427 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1428 | pid = get_param(params, 0)->val_ul; |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1429 | } |
| 1430 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1431 | process = gdb_get_process(pid); |
| 1432 | gdb_process_breakpoint_remove_all(process); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1433 | process->attached = false; |
| 1434 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1435 | if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) { |
| 1436 | gdbserver_state.c_cpu = gdb_first_attached_cpu(); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1437 | } |
| 1438 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1439 | if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) { |
| 1440 | gdbserver_state.g_cpu = gdb_first_attached_cpu(); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1441 | } |
| 1442 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1443 | if (!gdbserver_state.c_cpu) { |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1444 | /* No more process attached */ |
| 1445 | gdb_syscall_mode = GDB_SYS_DISABLED; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1446 | gdb_continue(); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1447 | } |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1448 | put_packet("OK"); |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 1449 | } |
| 1450 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1451 | static void handle_thread_alive(GArray *params, void *user_ctx) |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 1452 | { |
| 1453 | CPUState *cpu; |
| 1454 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1455 | if (!params->len) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1456 | put_packet("E22"); |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 1457 | return; |
| 1458 | } |
| 1459 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1460 | if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1461 | put_packet("E22"); |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 1462 | return; |
| 1463 | } |
| 1464 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1465 | cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid, |
| 1466 | get_param(params, 0)->thread_id.tid); |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 1467 | if (!cpu) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1468 | put_packet("E22"); |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 1469 | return; |
| 1470 | } |
| 1471 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1472 | put_packet("OK"); |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 1473 | } |
| 1474 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1475 | static void handle_continue(GArray *params, void *user_ctx) |
Jon Doron | 4d6e3fe | 2019-05-29 09:41:32 +0300 | [diff] [blame] | 1476 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1477 | if (params->len) { |
| 1478 | gdb_set_cpu_pc(get_param(params, 0)->val_ull); |
Jon Doron | 4d6e3fe | 2019-05-29 09:41:32 +0300 | [diff] [blame] | 1479 | } |
| 1480 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1481 | gdbserver_state.signal = 0; |
| 1482 | gdb_continue(); |
Jon Doron | 4d6e3fe | 2019-05-29 09:41:32 +0300 | [diff] [blame] | 1483 | } |
| 1484 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1485 | static void handle_cont_with_sig(GArray *params, void *user_ctx) |
Jon Doron | ccc47d5 | 2019-05-29 09:41:33 +0300 | [diff] [blame] | 1486 | { |
| 1487 | unsigned long signal = 0; |
| 1488 | |
| 1489 | /* |
| 1490 | * Note: C sig;[addr] is currently unsupported and we simply |
| 1491 | * omit the addr parameter |
| 1492 | */ |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1493 | if (params->len) { |
| 1494 | signal = get_param(params, 0)->val_ul; |
Jon Doron | ccc47d5 | 2019-05-29 09:41:33 +0300 | [diff] [blame] | 1495 | } |
| 1496 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1497 | gdbserver_state.signal = gdb_signal_to_target(signal); |
| 1498 | if (gdbserver_state.signal == -1) { |
| 1499 | gdbserver_state.signal = 0; |
Jon Doron | ccc47d5 | 2019-05-29 09:41:33 +0300 | [diff] [blame] | 1500 | } |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1501 | gdb_continue(); |
Jon Doron | ccc47d5 | 2019-05-29 09:41:33 +0300 | [diff] [blame] | 1502 | } |
| 1503 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1504 | static void handle_set_thread(GArray *params, void *user_ctx) |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1505 | { |
| 1506 | CPUState *cpu; |
| 1507 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1508 | if (params->len != 2) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1509 | put_packet("E22"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1510 | return; |
| 1511 | } |
| 1512 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1513 | if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1514 | put_packet("E22"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1515 | return; |
| 1516 | } |
| 1517 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1518 | if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1519 | put_packet("OK"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1520 | return; |
| 1521 | } |
| 1522 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1523 | cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid, |
| 1524 | get_param(params, 1)->thread_id.tid); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1525 | if (!cpu) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1526 | put_packet("E22"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1527 | return; |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * Note: This command is deprecated and modern gdb's will be using the |
| 1532 | * vCont command instead. |
| 1533 | */ |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1534 | switch (get_param(params, 0)->opcode) { |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1535 | case 'c': |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1536 | gdbserver_state.c_cpu = cpu; |
| 1537 | put_packet("OK"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1538 | break; |
| 1539 | case 'g': |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1540 | gdbserver_state.g_cpu = cpu; |
| 1541 | put_packet("OK"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1542 | break; |
| 1543 | default: |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1544 | put_packet("E22"); |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 1545 | break; |
| 1546 | } |
| 1547 | } |
| 1548 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1549 | static void handle_insert_bp(GArray *params, void *user_ctx) |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1550 | { |
| 1551 | int res; |
| 1552 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1553 | if (params->len != 3) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1554 | put_packet("E22"); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1555 | return; |
| 1556 | } |
| 1557 | |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 1558 | res = gdb_breakpoint_insert(gdbserver_state.c_cpu, |
| 1559 | get_param(params, 0)->val_ul, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1560 | get_param(params, 1)->val_ull, |
| 1561 | get_param(params, 2)->val_ull); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1562 | if (res >= 0) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1563 | put_packet("OK"); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1564 | return; |
| 1565 | } else if (res == -ENOSYS) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1566 | put_packet(""); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1567 | return; |
| 1568 | } |
| 1569 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1570 | put_packet("E22"); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1571 | } |
| 1572 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1573 | static void handle_remove_bp(GArray *params, void *user_ctx) |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1574 | { |
| 1575 | int res; |
| 1576 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1577 | if (params->len != 3) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1578 | put_packet("E22"); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1579 | return; |
| 1580 | } |
| 1581 | |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 1582 | res = gdb_breakpoint_remove(gdbserver_state.c_cpu, |
| 1583 | get_param(params, 0)->val_ul, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1584 | get_param(params, 1)->val_ull, |
| 1585 | get_param(params, 2)->val_ull); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1586 | if (res >= 0) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1587 | put_packet("OK"); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1588 | return; |
| 1589 | } else if (res == -ENOSYS) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1590 | put_packet(""); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1591 | return; |
| 1592 | } |
| 1593 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1594 | put_packet("E22"); |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 1595 | } |
| 1596 | |
Alex Bennée | 94b2a62 | 2019-07-05 14:23:07 +0100 | [diff] [blame] | 1597 | /* |
| 1598 | * handle_set/get_reg |
| 1599 | * |
| 1600 | * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available. |
| 1601 | * This works, but can be very slow. Anything new enough to understand |
| 1602 | * XML also knows how to use this properly. However to use this we |
| 1603 | * need to define a local XML file as well as be talking to a |
| 1604 | * reasonably modern gdb. Responding with an empty packet will cause |
| 1605 | * the remote gdb to fallback to older methods. |
| 1606 | */ |
| 1607 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1608 | static void handle_set_reg(GArray *params, void *user_ctx) |
Jon Doron | 62b3320 | 2019-05-29 09:41:36 +0300 | [diff] [blame] | 1609 | { |
| 1610 | int reg_size; |
| 1611 | |
| 1612 | if (!gdb_has_xml) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1613 | put_packet(""); |
Jon Doron | 62b3320 | 2019-05-29 09:41:36 +0300 | [diff] [blame] | 1614 | return; |
| 1615 | } |
| 1616 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1617 | if (params->len != 2) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1618 | put_packet("E22"); |
Jon Doron | 62b3320 | 2019-05-29 09:41:36 +0300 | [diff] [blame] | 1619 | return; |
| 1620 | } |
| 1621 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1622 | reg_size = strlen(get_param(params, 1)->data) / 2; |
| 1623 | hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size); |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1624 | gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1625 | get_param(params, 0)->val_ull); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1626 | put_packet("OK"); |
Jon Doron | 62b3320 | 2019-05-29 09:41:36 +0300 | [diff] [blame] | 1627 | } |
| 1628 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1629 | static void handle_get_reg(GArray *params, void *user_ctx) |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1630 | { |
| 1631 | int reg_size; |
| 1632 | |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1633 | if (!gdb_has_xml) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1634 | put_packet(""); |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1635 | return; |
| 1636 | } |
| 1637 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1638 | if (!params->len) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1639 | put_packet("E14"); |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1640 | return; |
| 1641 | } |
| 1642 | |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1643 | reg_size = gdb_read_register(gdbserver_state.g_cpu, |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 1644 | gdbserver_state.mem_buf, |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1645 | get_param(params, 0)->val_ull); |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1646 | if (!reg_size) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1647 | put_packet("E14"); |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1648 | return; |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1649 | } else { |
| 1650 | g_byte_array_set_size(gdbserver_state.mem_buf, reg_size); |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1651 | } |
| 1652 | |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1653 | memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, reg_size); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1654 | put_strbuf(); |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 1655 | } |
| 1656 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1657 | static void handle_write_mem(GArray *params, void *user_ctx) |
Jon Doron | cc0ecc7 | 2019-05-29 09:41:38 +0300 | [diff] [blame] | 1658 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1659 | if (params->len != 3) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1660 | put_packet("E22"); |
Jon Doron | cc0ecc7 | 2019-05-29 09:41:38 +0300 | [diff] [blame] | 1661 | return; |
| 1662 | } |
| 1663 | |
| 1664 | /* hextomem() reads 2*len bytes */ |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1665 | if (get_param(params, 1)->val_ull > |
| 1666 | strlen(get_param(params, 2)->data) / 2) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1667 | put_packet("E22"); |
Jon Doron | cc0ecc7 | 2019-05-29 09:41:38 +0300 | [diff] [blame] | 1668 | return; |
| 1669 | } |
| 1670 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1671 | hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data, |
| 1672 | get_param(params, 1)->val_ull); |
| 1673 | if (target_memory_rw_debug(gdbserver_state.g_cpu, |
| 1674 | get_param(params, 0)->val_ull, |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1675 | gdbserver_state.mem_buf->data, |
| 1676 | gdbserver_state.mem_buf->len, true)) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1677 | put_packet("E14"); |
Jon Doron | cc0ecc7 | 2019-05-29 09:41:38 +0300 | [diff] [blame] | 1678 | return; |
| 1679 | } |
| 1680 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1681 | put_packet("OK"); |
Jon Doron | cc0ecc7 | 2019-05-29 09:41:38 +0300 | [diff] [blame] | 1682 | } |
| 1683 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1684 | static void handle_read_mem(GArray *params, void *user_ctx) |
Jon Doron | da92e23 | 2019-05-29 09:41:39 +0300 | [diff] [blame] | 1685 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1686 | if (params->len != 2) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1687 | put_packet("E22"); |
Jon Doron | da92e23 | 2019-05-29 09:41:39 +0300 | [diff] [blame] | 1688 | return; |
| 1689 | } |
| 1690 | |
| 1691 | /* memtohex() doubles the required space */ |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1692 | if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1693 | put_packet("E22"); |
Jon Doron | da92e23 | 2019-05-29 09:41:39 +0300 | [diff] [blame] | 1694 | return; |
| 1695 | } |
| 1696 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1697 | g_byte_array_set_size(gdbserver_state.mem_buf, |
| 1698 | get_param(params, 1)->val_ull); |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1699 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1700 | if (target_memory_rw_debug(gdbserver_state.g_cpu, |
| 1701 | get_param(params, 0)->val_ull, |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1702 | gdbserver_state.mem_buf->data, |
| 1703 | gdbserver_state.mem_buf->len, false)) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1704 | put_packet("E14"); |
Jon Doron | da92e23 | 2019-05-29 09:41:39 +0300 | [diff] [blame] | 1705 | return; |
| 1706 | } |
| 1707 | |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1708 | memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, |
| 1709 | gdbserver_state.mem_buf->len); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1710 | put_strbuf(); |
Jon Doron | da92e23 | 2019-05-29 09:41:39 +0300 | [diff] [blame] | 1711 | } |
| 1712 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1713 | static void handle_write_all_regs(GArray *params, void *user_ctx) |
Jon Doron | 287ca12 | 2019-05-29 09:41:40 +0300 | [diff] [blame] | 1714 | { |
| 1715 | target_ulong addr, len; |
| 1716 | uint8_t *registers; |
| 1717 | int reg_size; |
| 1718 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1719 | if (!params->len) { |
Jon Doron | 287ca12 | 2019-05-29 09:41:40 +0300 | [diff] [blame] | 1720 | return; |
| 1721 | } |
| 1722 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1723 | cpu_synchronize_state(gdbserver_state.g_cpu); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1724 | len = strlen(get_param(params, 0)->data) / 2; |
| 1725 | hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len); |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1726 | registers = gdbserver_state.mem_buf->data; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1727 | for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0; |
Jon Doron | 287ca12 | 2019-05-29 09:41:40 +0300 | [diff] [blame] | 1728 | addr++) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1729 | reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, addr); |
Jon Doron | 287ca12 | 2019-05-29 09:41:40 +0300 | [diff] [blame] | 1730 | len -= reg_size; |
| 1731 | registers += reg_size; |
| 1732 | } |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1733 | put_packet("OK"); |
Jon Doron | 287ca12 | 2019-05-29 09:41:40 +0300 | [diff] [blame] | 1734 | } |
| 1735 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1736 | static void handle_read_all_regs(GArray *params, void *user_ctx) |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 1737 | { |
| 1738 | target_ulong addr, len; |
| 1739 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1740 | cpu_synchronize_state(gdbserver_state.g_cpu); |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 1741 | g_byte_array_set_size(gdbserver_state.mem_buf, 0); |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 1742 | len = 0; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1743 | for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs; addr++) { |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1744 | len += gdb_read_register(gdbserver_state.g_cpu, |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 1745 | gdbserver_state.mem_buf, |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 1746 | addr); |
| 1747 | } |
Alex Bennée | a010bdb | 2020-03-16 17:21:41 +0000 | [diff] [blame] | 1748 | g_assert(len == gdbserver_state.mem_buf->len); |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 1749 | |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 1750 | memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1751 | put_strbuf(); |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 1752 | } |
| 1753 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1754 | static void handle_file_io(GArray *params, void *user_ctx) |
Jon Doron | 4b20fab | 2019-05-29 09:41:42 +0300 | [diff] [blame] | 1755 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1756 | if (params->len >= 1 && gdbserver_state.current_syscall_cb) { |
Richard Henderson | 64c8c6a | 2022-04-29 16:21:43 -0700 | [diff] [blame] | 1757 | uint64_t ret; |
| 1758 | int err; |
Jon Doron | 4b20fab | 2019-05-29 09:41:42 +0300 | [diff] [blame] | 1759 | |
Richard Henderson | 64c8c6a | 2022-04-29 16:21:43 -0700 | [diff] [blame] | 1760 | ret = get_param(params, 0)->val_ull; |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1761 | if (params->len >= 2) { |
Richard Henderson | 64c8c6a | 2022-04-29 16:21:43 -0700 | [diff] [blame] | 1762 | err = get_param(params, 1)->val_ull; |
Sandra Loosemore | c6ee952 | 2019-08-27 16:33:17 -0600 | [diff] [blame] | 1763 | } else { |
| 1764 | err = 0; |
| 1765 | } |
Richard Henderson | c805e11 | 2022-06-07 12:38:26 -0700 | [diff] [blame] | 1766 | |
| 1767 | /* Convert GDB error numbers back to host error numbers. */ |
| 1768 | #define E(X) case GDB_E##X: err = E##X; break |
| 1769 | switch (err) { |
| 1770 | case 0: |
| 1771 | break; |
| 1772 | E(PERM); |
| 1773 | E(NOENT); |
| 1774 | E(INTR); |
| 1775 | E(BADF); |
| 1776 | E(ACCES); |
| 1777 | E(FAULT); |
| 1778 | E(BUSY); |
| 1779 | E(EXIST); |
| 1780 | E(NODEV); |
| 1781 | E(NOTDIR); |
| 1782 | E(ISDIR); |
| 1783 | E(INVAL); |
| 1784 | E(NFILE); |
| 1785 | E(MFILE); |
| 1786 | E(FBIG); |
| 1787 | E(NOSPC); |
| 1788 | E(SPIPE); |
| 1789 | E(ROFS); |
| 1790 | E(NAMETOOLONG); |
| 1791 | default: |
| 1792 | err = EINVAL; |
| 1793 | break; |
| 1794 | } |
| 1795 | #undef E |
| 1796 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1797 | gdbserver_state.current_syscall_cb(gdbserver_state.c_cpu, ret, err); |
| 1798 | gdbserver_state.current_syscall_cb = NULL; |
Jon Doron | 4b20fab | 2019-05-29 09:41:42 +0300 | [diff] [blame] | 1799 | } |
| 1800 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1801 | if (params->len >= 3 && get_param(params, 2)->opcode == (uint8_t)'C') { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1802 | put_packet("T02"); |
Jon Doron | 4b20fab | 2019-05-29 09:41:42 +0300 | [diff] [blame] | 1803 | return; |
| 1804 | } |
| 1805 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1806 | gdb_continue(); |
Jon Doron | 4b20fab | 2019-05-29 09:41:42 +0300 | [diff] [blame] | 1807 | } |
| 1808 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1809 | static void handle_step(GArray *params, void *user_ctx) |
Jon Doron | 933f80d | 2019-05-29 09:41:43 +0300 | [diff] [blame] | 1810 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1811 | if (params->len) { |
| 1812 | gdb_set_cpu_pc((target_ulong)get_param(params, 0)->val_ull); |
Jon Doron | 933f80d | 2019-05-29 09:41:43 +0300 | [diff] [blame] | 1813 | } |
| 1814 | |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 1815 | cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1816 | gdb_continue(); |
Jon Doron | 933f80d | 2019-05-29 09:41:43 +0300 | [diff] [blame] | 1817 | } |
| 1818 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1819 | static void handle_backward(GArray *params, void *user_ctx) |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 1820 | { |
Alex Bennée | ed12f5b | 2021-05-20 18:43:02 +0100 | [diff] [blame] | 1821 | if (!stub_can_reverse()) { |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 1822 | put_packet("E22"); |
| 1823 | } |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1824 | if (params->len == 1) { |
| 1825 | switch (get_param(params, 0)->opcode) { |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 1826 | case 's': |
| 1827 | if (replay_reverse_step()) { |
| 1828 | gdb_continue(); |
| 1829 | } else { |
| 1830 | put_packet("E14"); |
| 1831 | } |
| 1832 | return; |
Pavel Dovgalyuk | cda3825 | 2020-10-03 20:13:49 +0300 | [diff] [blame] | 1833 | case 'c': |
| 1834 | if (replay_reverse_continue()) { |
| 1835 | gdb_continue(); |
| 1836 | } else { |
| 1837 | put_packet("E14"); |
| 1838 | } |
| 1839 | return; |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | /* Default invalid command */ |
| 1844 | put_packet(""); |
| 1845 | } |
| 1846 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1847 | static void handle_v_cont_query(GArray *params, void *user_ctx) |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1848 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1849 | put_packet("vCont;c;C;s;S"); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1850 | } |
| 1851 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1852 | static void handle_v_cont(GArray *params, void *user_ctx) |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1853 | { |
| 1854 | int res; |
| 1855 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1856 | if (!params->len) { |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1857 | return; |
| 1858 | } |
| 1859 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1860 | res = gdb_handle_vcont(get_param(params, 0)->data); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1861 | if ((res == -EINVAL) || (res == -ERANGE)) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1862 | put_packet("E22"); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1863 | } else if (res) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1864 | put_packet(""); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1868 | static void handle_v_attach(GArray *params, void *user_ctx) |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1869 | { |
| 1870 | GDBProcess *process; |
| 1871 | CPUState *cpu; |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1872 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1873 | g_string_assign(gdbserver_state.str_buf, "E22"); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1874 | if (!params->len) { |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1875 | goto cleanup; |
| 1876 | } |
| 1877 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1878 | process = gdb_get_process(get_param(params, 0)->val_ul); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1879 | if (!process) { |
| 1880 | goto cleanup; |
| 1881 | } |
| 1882 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1883 | cpu = get_first_cpu_in_process(process); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1884 | if (!cpu) { |
| 1885 | goto cleanup; |
| 1886 | } |
| 1887 | |
| 1888 | process->attached = true; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1889 | gdbserver_state.g_cpu = cpu; |
| 1890 | gdbserver_state.c_cpu = cpu; |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1891 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1892 | g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP); |
| 1893 | gdb_append_thread_id(cpu, gdbserver_state.str_buf); |
| 1894 | g_string_append_c(gdbserver_state.str_buf, ';'); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1895 | cleanup: |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1896 | put_strbuf(); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1897 | } |
| 1898 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1899 | static void handle_v_kill(GArray *params, void *user_ctx) |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1900 | { |
| 1901 | /* Kill the target */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1902 | put_packet("OK"); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1903 | error_report("QEMU: Terminated via GDBstub"); |
Alex Bennée | b9e10c6 | 2021-01-08 22:42:45 +0000 | [diff] [blame] | 1904 | gdb_exit(0); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1905 | exit(0); |
| 1906 | } |
| 1907 | |
Philippe Mathieu-Daudé | 305bea0 | 2021-05-20 18:42:59 +0100 | [diff] [blame] | 1908 | static const GdbCmdParseEntry gdb_v_commands_table[] = { |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1909 | /* Order is important if has same prefix */ |
| 1910 | { |
| 1911 | .handler = handle_v_cont_query, |
| 1912 | .cmd = "Cont?", |
| 1913 | .cmd_startswith = 1 |
| 1914 | }, |
| 1915 | { |
| 1916 | .handler = handle_v_cont, |
| 1917 | .cmd = "Cont", |
| 1918 | .cmd_startswith = 1, |
| 1919 | .schema = "s0" |
| 1920 | }, |
| 1921 | { |
| 1922 | .handler = handle_v_attach, |
| 1923 | .cmd = "Attach;", |
| 1924 | .cmd_startswith = 1, |
| 1925 | .schema = "l0" |
| 1926 | }, |
| 1927 | { |
| 1928 | .handler = handle_v_kill, |
| 1929 | .cmd = "Kill;", |
| 1930 | .cmd_startswith = 1 |
| 1931 | }, |
| 1932 | }; |
| 1933 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1934 | static void handle_v_commands(GArray *params, void *user_ctx) |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1935 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1936 | if (!params->len) { |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1937 | return; |
| 1938 | } |
| 1939 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1940 | if (process_string_cmd(NULL, get_param(params, 0)->data, |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1941 | gdb_v_commands_table, |
| 1942 | ARRAY_SIZE(gdb_v_commands_table))) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1943 | put_packet(""); |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 1944 | } |
| 1945 | } |
| 1946 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1947 | static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1948 | { |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 1949 | g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE); |
| 1950 | |
| 1951 | if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) { |
| 1952 | g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x", |
| 1953 | SSTEP_NOIRQ); |
| 1954 | } |
| 1955 | |
| 1956 | if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) { |
| 1957 | g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x", |
| 1958 | SSTEP_NOTIMER); |
| 1959 | } |
| 1960 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1961 | put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1962 | } |
| 1963 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1964 | static void handle_set_qemu_sstep(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1965 | { |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 1966 | int new_sstep_flags; |
| 1967 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1968 | if (!params->len) { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1969 | return; |
| 1970 | } |
| 1971 | |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 1972 | new_sstep_flags = get_param(params, 0)->val_ul; |
| 1973 | |
| 1974 | if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) { |
| 1975 | put_packet("E22"); |
| 1976 | return; |
| 1977 | } |
| 1978 | |
| 1979 | gdbserver_state.sstep_flags = new_sstep_flags; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 1980 | put_packet("OK"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1981 | } |
| 1982 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1983 | static void handle_query_qemu_sstep(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1984 | { |
Maxim Levitsky | ecd39d6 | 2021-11-11 12:06:02 +0100 | [diff] [blame] | 1985 | g_string_printf(gdbserver_state.str_buf, "0x%x", |
| 1986 | gdbserver_state.sstep_flags); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 1987 | put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1988 | } |
| 1989 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 1990 | static void handle_query_curr_tid(GArray *params, void *user_ctx) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1991 | { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1992 | CPUState *cpu; |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1993 | GDBProcess *process; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 1994 | |
| 1995 | /* |
| 1996 | * "Current thread" remains vague in the spec, so always return |
| 1997 | * the first thread of the current process (gdb returns the |
| 1998 | * first thread). |
| 1999 | */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2000 | process = gdb_get_cpu_process(gdbserver_state.g_cpu); |
| 2001 | cpu = get_first_cpu_in_process(process); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2002 | g_string_assign(gdbserver_state.str_buf, "QC"); |
| 2003 | gdb_append_thread_id(cpu, gdbserver_state.str_buf); |
| 2004 | put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2005 | } |
| 2006 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2007 | static void handle_query_threads(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2008 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2009 | if (!gdbserver_state.query_cpu) { |
| 2010 | put_packet("l"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2011 | return; |
| 2012 | } |
| 2013 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2014 | g_string_assign(gdbserver_state.str_buf, "m"); |
| 2015 | gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf); |
| 2016 | put_strbuf(); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2017 | gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2018 | } |
| 2019 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2020 | static void handle_query_first_threads(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2021 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2022 | gdbserver_state.query_cpu = gdb_first_attached_cpu(); |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2023 | handle_query_threads(params, user_ctx); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2024 | } |
| 2025 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2026 | static void handle_query_thread_extra(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2027 | { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2028 | g_autoptr(GString) rs = g_string_new(NULL); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2029 | CPUState *cpu; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2030 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2031 | if (!params->len || |
| 2032 | get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2033 | put_packet("E22"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2034 | return; |
| 2035 | } |
| 2036 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2037 | cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid, |
| 2038 | get_param(params, 0)->thread_id.tid); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2039 | if (!cpu) { |
| 2040 | return; |
| 2041 | } |
| 2042 | |
| 2043 | cpu_synchronize_state(cpu); |
| 2044 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2045 | if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2046 | /* Print the CPU model and name in multiprocess mode */ |
| 2047 | ObjectClass *oc = object_get_class(OBJECT(cpu)); |
| 2048 | const char *cpu_model = object_class_get_name(oc); |
Markus Armbruster | 7a309cc | 2020-07-14 18:02:00 +0200 | [diff] [blame] | 2049 | const char *cpu_name = |
Denis Plotnikov | 076b2fa | 2020-04-03 20:11:44 +0100 | [diff] [blame] | 2050 | object_get_canonical_path_component(OBJECT(cpu)); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2051 | g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name, |
| 2052 | cpu->halted ? "halted " : "running"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2053 | } else { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2054 | g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2055 | cpu->halted ? "halted " : "running"); |
| 2056 | } |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2057 | trace_gdbstub_op_extra_info(rs->str); |
| 2058 | memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len); |
| 2059 | put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | #ifdef CONFIG_USER_ONLY |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2063 | static void handle_query_offsets(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2064 | { |
| 2065 | TaskState *ts; |
| 2066 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2067 | ts = gdbserver_state.c_cpu->opaque; |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2068 | g_string_printf(gdbserver_state.str_buf, |
| 2069 | "Text=" TARGET_ABI_FMT_lx |
| 2070 | ";Data=" TARGET_ABI_FMT_lx |
| 2071 | ";Bss=" TARGET_ABI_FMT_lx, |
| 2072 | ts->info->code_offset, |
| 2073 | ts->info->data_offset, |
| 2074 | ts->info->data_offset); |
| 2075 | put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2076 | } |
| 2077 | #else |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2078 | static void handle_query_rcmd(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2079 | { |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 2080 | const guint8 zero = 0; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2081 | int len; |
| 2082 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2083 | if (!params->len) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2084 | put_packet("E22"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2085 | return; |
| 2086 | } |
| 2087 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2088 | len = strlen(get_param(params, 0)->data); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2089 | if (len % 2) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2090 | put_packet("E01"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2091 | return; |
| 2092 | } |
| 2093 | |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 2094 | g_assert(gdbserver_state.mem_buf->len == 0); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2095 | len = len / 2; |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2096 | hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len); |
Alex Bennée | 4a25f1b | 2020-03-16 17:21:36 +0000 | [diff] [blame] | 2097 | g_byte_array_append(gdbserver_state.mem_buf, &zero, 1); |
| 2098 | qemu_chr_be_write(gdbserver_state.mon_chr, gdbserver_state.mem_buf->data, |
| 2099 | gdbserver_state.mem_buf->len); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2100 | put_packet("OK"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2101 | } |
| 2102 | #endif |
| 2103 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2104 | static void handle_query_supported(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2105 | { |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 2106 | CPUClass *cc; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2107 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2108 | g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2109 | cc = CPU_GET_CLASS(first_cpu); |
| 2110 | if (cc->gdb_core_xml_file) { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2111 | g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2112 | } |
| 2113 | |
Alex Bennée | ed12f5b | 2021-05-20 18:43:02 +0100 | [diff] [blame] | 2114 | if (stub_can_reverse()) { |
Pavel Dovgalyuk | cda3825 | 2020-10-03 20:13:49 +0300 | [diff] [blame] | 2115 | g_string_append(gdbserver_state.str_buf, |
| 2116 | ";ReverseStep+;ReverseContinue+"); |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 2117 | } |
| 2118 | |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 2119 | #ifdef CONFIG_USER_ONLY |
| 2120 | if (gdbserver_state.c_cpu->opaque) { |
| 2121 | g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+"); |
| 2122 | } |
| 2123 | #endif |
| 2124 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2125 | if (params->len && |
| 2126 | strstr(get_param(params, 0)->data, "multiprocess+")) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2127 | gdbserver_state.multiprocess = true; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2128 | } |
| 2129 | |
Changbin Du | 3bc2609 | 2020-03-16 17:21:55 +0000 | [diff] [blame] | 2130 | g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+"); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2131 | put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2132 | } |
| 2133 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2134 | static void handle_query_xfer_features(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2135 | { |
| 2136 | GDBProcess *process; |
| 2137 | CPUClass *cc; |
| 2138 | unsigned long len, total_len, addr; |
| 2139 | const char *xml; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2140 | const char *p; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2141 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2142 | if (params->len < 3) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2143 | put_packet("E22"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2144 | return; |
| 2145 | } |
| 2146 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2147 | process = gdb_get_cpu_process(gdbserver_state.g_cpu); |
| 2148 | cc = CPU_GET_CLASS(gdbserver_state.g_cpu); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2149 | if (!cc->gdb_core_xml_file) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2150 | put_packet(""); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2151 | return; |
| 2152 | } |
| 2153 | |
| 2154 | gdb_has_xml = true; |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2155 | p = get_param(params, 0)->data; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2156 | xml = get_feature_xml(p, &p, process); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2157 | if (!xml) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2158 | put_packet("E00"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2159 | return; |
| 2160 | } |
| 2161 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2162 | addr = get_param(params, 1)->val_ul; |
| 2163 | len = get_param(params, 2)->val_ul; |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2164 | total_len = strlen(xml); |
| 2165 | if (addr > total_len) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2166 | put_packet("E00"); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2167 | return; |
| 2168 | } |
| 2169 | |
| 2170 | if (len > (MAX_PACKET_LENGTH - 5) / 2) { |
| 2171 | len = (MAX_PACKET_LENGTH - 5) / 2; |
| 2172 | } |
| 2173 | |
| 2174 | if (len < total_len - addr) { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2175 | g_string_assign(gdbserver_state.str_buf, "m"); |
| 2176 | memtox(gdbserver_state.str_buf, xml + addr, len); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2177 | } else { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2178 | g_string_assign(gdbserver_state.str_buf, "l"); |
| 2179 | memtox(gdbserver_state.str_buf, xml + addr, total_len - addr); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2180 | } |
| 2181 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2182 | put_packet_binary(gdbserver_state.str_buf->str, |
| 2183 | gdbserver_state.str_buf->len, true); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2184 | } |
| 2185 | |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 2186 | #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER) |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2187 | static void handle_query_xfer_auxv(GArray *params, void *user_ctx) |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 2188 | { |
| 2189 | TaskState *ts; |
| 2190 | unsigned long offset, len, saved_auxv, auxv_len; |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 2191 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2192 | if (params->len < 2) { |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 2193 | put_packet("E22"); |
| 2194 | return; |
| 2195 | } |
| 2196 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2197 | offset = get_param(params, 0)->val_ul; |
| 2198 | len = get_param(params, 1)->val_ul; |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 2199 | ts = gdbserver_state.c_cpu->opaque; |
| 2200 | saved_auxv = ts->info->saved_auxv; |
| 2201 | auxv_len = ts->info->auxv_len; |
Richard Henderson | 6e3dd75 | 2021-02-02 13:39:55 +0000 | [diff] [blame] | 2202 | |
| 2203 | if (offset >= auxv_len) { |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 2204 | put_packet("E00"); |
| 2205 | return; |
| 2206 | } |
| 2207 | |
| 2208 | if (len > (MAX_PACKET_LENGTH - 5) / 2) { |
| 2209 | len = (MAX_PACKET_LENGTH - 5) / 2; |
| 2210 | } |
| 2211 | |
| 2212 | if (len < auxv_len - offset) { |
| 2213 | g_string_assign(gdbserver_state.str_buf, "m"); |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 2214 | } else { |
| 2215 | g_string_assign(gdbserver_state.str_buf, "l"); |
Richard Henderson | 6e3dd75 | 2021-02-02 13:39:55 +0000 | [diff] [blame] | 2216 | len = auxv_len - offset; |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
Richard Henderson | 6e3dd75 | 2021-02-02 13:39:55 +0000 | [diff] [blame] | 2219 | g_byte_array_set_size(gdbserver_state.mem_buf, len); |
| 2220 | if (target_memory_rw_debug(gdbserver_state.g_cpu, saved_auxv + offset, |
| 2221 | gdbserver_state.mem_buf->data, len, false)) { |
| 2222 | put_packet("E14"); |
| 2223 | return; |
| 2224 | } |
| 2225 | |
| 2226 | memtox(gdbserver_state.str_buf, |
| 2227 | (const char *)gdbserver_state.mem_buf->data, len); |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 2228 | put_packet_binary(gdbserver_state.str_buf->str, |
| 2229 | gdbserver_state.str_buf->len, true); |
| 2230 | } |
| 2231 | #endif |
| 2232 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2233 | static void handle_query_attached(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2234 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2235 | put_packet(GDB_ATTACHED); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2236 | } |
| 2237 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2238 | static void handle_query_qemu_supported(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2239 | { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2240 | g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep"); |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 2241 | #ifndef CONFIG_USER_ONLY |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2242 | g_string_append(gdbserver_state.str_buf, ";PhyMemMode"); |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 2243 | #endif |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2244 | put_strbuf(); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2245 | } |
| 2246 | |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 2247 | #ifndef CONFIG_USER_ONLY |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2248 | static void handle_query_qemu_phy_mem_mode(GArray *params, |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 2249 | void *user_ctx) |
| 2250 | { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2251 | g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode); |
| 2252 | put_strbuf(); |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 2253 | } |
| 2254 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2255 | static void handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx) |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 2256 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2257 | if (!params->len) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2258 | put_packet("E22"); |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 2259 | return; |
| 2260 | } |
| 2261 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2262 | if (!get_param(params, 0)->val_ul) { |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 2263 | phy_memory_mode = 0; |
| 2264 | } else { |
| 2265 | phy_memory_mode = 1; |
| 2266 | } |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2267 | put_packet("OK"); |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 2268 | } |
| 2269 | #endif |
| 2270 | |
Philippe Mathieu-Daudé | 305bea0 | 2021-05-20 18:42:59 +0100 | [diff] [blame] | 2271 | static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2272 | /* Order is important if has same prefix */ |
| 2273 | { |
| 2274 | .handler = handle_query_qemu_sstepbits, |
| 2275 | .cmd = "qemu.sstepbits", |
| 2276 | }, |
| 2277 | { |
| 2278 | .handler = handle_query_qemu_sstep, |
| 2279 | .cmd = "qemu.sstep", |
| 2280 | }, |
| 2281 | { |
| 2282 | .handler = handle_set_qemu_sstep, |
| 2283 | .cmd = "qemu.sstep=", |
| 2284 | .cmd_startswith = 1, |
| 2285 | .schema = "l0" |
| 2286 | }, |
| 2287 | }; |
| 2288 | |
Philippe Mathieu-Daudé | 305bea0 | 2021-05-20 18:42:59 +0100 | [diff] [blame] | 2289 | static const GdbCmdParseEntry gdb_gen_query_table[] = { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2290 | { |
| 2291 | .handler = handle_query_curr_tid, |
| 2292 | .cmd = "C", |
| 2293 | }, |
| 2294 | { |
| 2295 | .handler = handle_query_threads, |
| 2296 | .cmd = "sThreadInfo", |
| 2297 | }, |
| 2298 | { |
| 2299 | .handler = handle_query_first_threads, |
| 2300 | .cmd = "fThreadInfo", |
| 2301 | }, |
| 2302 | { |
| 2303 | .handler = handle_query_thread_extra, |
| 2304 | .cmd = "ThreadExtraInfo,", |
| 2305 | .cmd_startswith = 1, |
| 2306 | .schema = "t0" |
| 2307 | }, |
| 2308 | #ifdef CONFIG_USER_ONLY |
| 2309 | { |
| 2310 | .handler = handle_query_offsets, |
| 2311 | .cmd = "Offsets", |
| 2312 | }, |
| 2313 | #else |
| 2314 | { |
| 2315 | .handler = handle_query_rcmd, |
| 2316 | .cmd = "Rcmd,", |
| 2317 | .cmd_startswith = 1, |
| 2318 | .schema = "s0" |
| 2319 | }, |
| 2320 | #endif |
| 2321 | { |
| 2322 | .handler = handle_query_supported, |
| 2323 | .cmd = "Supported:", |
| 2324 | .cmd_startswith = 1, |
| 2325 | .schema = "s0" |
| 2326 | }, |
| 2327 | { |
| 2328 | .handler = handle_query_supported, |
| 2329 | .cmd = "Supported", |
| 2330 | .schema = "s0" |
| 2331 | }, |
| 2332 | { |
| 2333 | .handler = handle_query_xfer_features, |
| 2334 | .cmd = "Xfer:features:read:", |
| 2335 | .cmd_startswith = 1, |
| 2336 | .schema = "s:l,l0" |
| 2337 | }, |
Lirong Yuan | 51c623b | 2021-01-08 22:42:42 +0000 | [diff] [blame] | 2338 | #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER) |
| 2339 | { |
| 2340 | .handler = handle_query_xfer_auxv, |
| 2341 | .cmd = "Xfer:auxv:read::", |
| 2342 | .cmd_startswith = 1, |
| 2343 | .schema = "l,l0" |
| 2344 | }, |
| 2345 | #endif |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2346 | { |
| 2347 | .handler = handle_query_attached, |
| 2348 | .cmd = "Attached:", |
| 2349 | .cmd_startswith = 1 |
| 2350 | }, |
| 2351 | { |
| 2352 | .handler = handle_query_attached, |
| 2353 | .cmd = "Attached", |
| 2354 | }, |
| 2355 | { |
| 2356 | .handler = handle_query_qemu_supported, |
| 2357 | .cmd = "qemu.Supported", |
| 2358 | }, |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 2359 | #ifndef CONFIG_USER_ONLY |
| 2360 | { |
| 2361 | .handler = handle_query_qemu_phy_mem_mode, |
| 2362 | .cmd = "qemu.PhyMemMode", |
| 2363 | }, |
| 2364 | #endif |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2365 | }; |
| 2366 | |
Philippe Mathieu-Daudé | 305bea0 | 2021-05-20 18:42:59 +0100 | [diff] [blame] | 2367 | static const GdbCmdParseEntry gdb_gen_set_table[] = { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2368 | /* Order is important if has same prefix */ |
| 2369 | { |
| 2370 | .handler = handle_set_qemu_sstep, |
| 2371 | .cmd = "qemu.sstep:", |
| 2372 | .cmd_startswith = 1, |
| 2373 | .schema = "l0" |
| 2374 | }, |
Jon Doron | ab4752e | 2019-05-29 09:41:48 +0300 | [diff] [blame] | 2375 | #ifndef CONFIG_USER_ONLY |
| 2376 | { |
| 2377 | .handler = handle_set_qemu_phy_mem_mode, |
| 2378 | .cmd = "qemu.PhyMemMode:", |
| 2379 | .cmd_startswith = 1, |
| 2380 | .schema = "l0" |
| 2381 | }, |
| 2382 | #endif |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2383 | }; |
| 2384 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2385 | static void handle_gen_query(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2386 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2387 | if (!params->len) { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2388 | return; |
| 2389 | } |
| 2390 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2391 | if (!process_string_cmd(NULL, get_param(params, 0)->data, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2392 | gdb_gen_query_set_common_table, |
| 2393 | ARRAY_SIZE(gdb_gen_query_set_common_table))) { |
| 2394 | return; |
| 2395 | } |
| 2396 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2397 | if (process_string_cmd(NULL, get_param(params, 0)->data, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2398 | gdb_gen_query_table, |
| 2399 | ARRAY_SIZE(gdb_gen_query_table))) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2400 | put_packet(""); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2404 | static void handle_gen_set(GArray *params, void *user_ctx) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2405 | { |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2406 | if (!params->len) { |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2407 | return; |
| 2408 | } |
| 2409 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2410 | if (!process_string_cmd(NULL, get_param(params, 0)->data, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2411 | gdb_gen_query_set_common_table, |
| 2412 | ARRAY_SIZE(gdb_gen_query_set_common_table))) { |
| 2413 | return; |
| 2414 | } |
| 2415 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2416 | if (process_string_cmd(NULL, get_param(params, 0)->data, |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2417 | gdb_gen_set_table, |
| 2418 | ARRAY_SIZE(gdb_gen_set_table))) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2419 | put_packet(""); |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2420 | } |
| 2421 | } |
| 2422 | |
Alex Bennée | 26a1618 | 2021-05-25 09:24:14 +0100 | [diff] [blame] | 2423 | static void handle_target_halt(GArray *params, void *user_ctx) |
Jon Doron | 7009d57 | 2019-05-29 09:41:46 +0300 | [diff] [blame] | 2424 | { |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2425 | g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP); |
| 2426 | gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf); |
| 2427 | g_string_append_c(gdbserver_state.str_buf, ';'); |
| 2428 | put_strbuf(); |
Jon Doron | 7009d57 | 2019-05-29 09:41:46 +0300 | [diff] [blame] | 2429 | /* |
| 2430 | * Remove all the breakpoints when this query is issued, |
| 2431 | * because gdb is doing an initial connect and the state |
| 2432 | * should be cleaned up. |
| 2433 | */ |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 2434 | gdb_breakpoint_remove_all(gdbserver_state.c_cpu); |
Jon Doron | 7009d57 | 2019-05-29 09:41:46 +0300 | [diff] [blame] | 2435 | } |
| 2436 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2437 | static int gdb_handle_packet(const char *line_buf) |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2438 | { |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 2439 | const GdbCmdParseEntry *cmd_parser = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2440 | |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2441 | trace_gdbstub_io_command(line_buf); |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 2442 | |
Jon Doron | 3f1cbac | 2019-05-29 09:41:47 +0300 | [diff] [blame] | 2443 | switch (line_buf[0]) { |
Luc Michel | 53fd655 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2444 | case '!': |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2445 | put_packet("OK"); |
Luc Michel | 53fd655 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2446 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2447 | case '?': |
Jon Doron | 7009d57 | 2019-05-29 09:41:46 +0300 | [diff] [blame] | 2448 | { |
| 2449 | static const GdbCmdParseEntry target_halted_cmd_desc = { |
| 2450 | .handler = handle_target_halt, |
| 2451 | .cmd = "?", |
| 2452 | .cmd_startswith = 1 |
| 2453 | }; |
| 2454 | cmd_parser = &target_halted_cmd_desc; |
| 2455 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2456 | break; |
| 2457 | case 'c': |
Jon Doron | 4d6e3fe | 2019-05-29 09:41:32 +0300 | [diff] [blame] | 2458 | { |
| 2459 | static const GdbCmdParseEntry continue_cmd_desc = { |
| 2460 | .handler = handle_continue, |
| 2461 | .cmd = "c", |
| 2462 | .cmd_startswith = 1, |
| 2463 | .schema = "L0" |
| 2464 | }; |
| 2465 | cmd_parser = &continue_cmd_desc; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2466 | } |
Jon Doron | 4d6e3fe | 2019-05-29 09:41:32 +0300 | [diff] [blame] | 2467 | break; |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 2468 | case 'C': |
Jon Doron | ccc47d5 | 2019-05-29 09:41:33 +0300 | [diff] [blame] | 2469 | { |
| 2470 | static const GdbCmdParseEntry cont_with_sig_cmd_desc = { |
| 2471 | .handler = handle_cont_with_sig, |
| 2472 | .cmd = "C", |
| 2473 | .cmd_startswith = 1, |
| 2474 | .schema = "l0" |
| 2475 | }; |
| 2476 | cmd_parser = &cont_with_sig_cmd_desc; |
| 2477 | } |
| 2478 | break; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 2479 | case 'v': |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 2480 | { |
| 2481 | static const GdbCmdParseEntry v_cmd_desc = { |
| 2482 | .handler = handle_v_commands, |
| 2483 | .cmd = "v", |
| 2484 | .cmd_startswith = 1, |
| 2485 | .schema = "s0" |
| 2486 | }; |
| 2487 | cmd_parser = &v_cmd_desc; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 2488 | } |
Jon Doron | 8536ec0 | 2019-05-29 09:41:44 +0300 | [diff] [blame] | 2489 | break; |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 2490 | case 'k': |
| 2491 | /* Kill the target */ |
Ziyue Yang | 7ae6c57 | 2017-01-18 16:03:29 +0800 | [diff] [blame] | 2492 | error_report("QEMU: Terminated via GDBstub"); |
Alex Bennée | b9e10c6 | 2021-01-08 22:42:45 +0000 | [diff] [blame] | 2493 | gdb_exit(0); |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 2494 | exit(0); |
| 2495 | case 'D': |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 2496 | { |
| 2497 | static const GdbCmdParseEntry detach_cmd_desc = { |
| 2498 | .handler = handle_detach, |
| 2499 | .cmd = "D", |
| 2500 | .cmd_startswith = 1, |
| 2501 | .schema = "?.l0" |
| 2502 | }; |
| 2503 | cmd_parser = &detach_cmd_desc; |
Luc Michel | 546f3c6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2504 | } |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 2505 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2506 | case 's': |
Jon Doron | 933f80d | 2019-05-29 09:41:43 +0300 | [diff] [blame] | 2507 | { |
| 2508 | static const GdbCmdParseEntry step_cmd_desc = { |
| 2509 | .handler = handle_step, |
| 2510 | .cmd = "s", |
| 2511 | .cmd_startswith = 1, |
| 2512 | .schema = "L0" |
| 2513 | }; |
| 2514 | cmd_parser = &step_cmd_desc; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2515 | } |
Jon Doron | 933f80d | 2019-05-29 09:41:43 +0300 | [diff] [blame] | 2516 | break; |
Pavel Dovgalyuk | fda8458 | 2020-10-03 20:13:43 +0300 | [diff] [blame] | 2517 | case 'b': |
| 2518 | { |
| 2519 | static const GdbCmdParseEntry backward_cmd_desc = { |
| 2520 | .handler = handle_backward, |
| 2521 | .cmd = "b", |
| 2522 | .cmd_startswith = 1, |
| 2523 | .schema = "o0" |
| 2524 | }; |
| 2525 | cmd_parser = &backward_cmd_desc; |
| 2526 | } |
| 2527 | break; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2528 | case 'F': |
| 2529 | { |
Jon Doron | 4b20fab | 2019-05-29 09:41:42 +0300 | [diff] [blame] | 2530 | static const GdbCmdParseEntry file_io_cmd_desc = { |
| 2531 | .handler = handle_file_io, |
| 2532 | .cmd = "F", |
| 2533 | .cmd_startswith = 1, |
| 2534 | .schema = "L,L,o0" |
| 2535 | }; |
| 2536 | cmd_parser = &file_io_cmd_desc; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2537 | } |
| 2538 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2539 | case 'g': |
Jon Doron | 397d137 | 2019-05-29 09:41:41 +0300 | [diff] [blame] | 2540 | { |
| 2541 | static const GdbCmdParseEntry read_all_regs_cmd_desc = { |
| 2542 | .handler = handle_read_all_regs, |
| 2543 | .cmd = "g", |
| 2544 | .cmd_startswith = 1 |
| 2545 | }; |
| 2546 | cmd_parser = &read_all_regs_cmd_desc; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 2547 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2548 | break; |
| 2549 | case 'G': |
Jon Doron | 287ca12 | 2019-05-29 09:41:40 +0300 | [diff] [blame] | 2550 | { |
| 2551 | static const GdbCmdParseEntry write_all_regs_cmd_desc = { |
| 2552 | .handler = handle_write_all_regs, |
| 2553 | .cmd = "G", |
| 2554 | .cmd_startswith = 1, |
| 2555 | .schema = "s0" |
| 2556 | }; |
| 2557 | cmd_parser = &write_all_regs_cmd_desc; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 2558 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2559 | break; |
| 2560 | case 'm': |
Jon Doron | da92e23 | 2019-05-29 09:41:39 +0300 | [diff] [blame] | 2561 | { |
| 2562 | static const GdbCmdParseEntry read_mem_cmd_desc = { |
| 2563 | .handler = handle_read_mem, |
| 2564 | .cmd = "m", |
| 2565 | .cmd_startswith = 1, |
| 2566 | .schema = "L,L0" |
| 2567 | }; |
| 2568 | cmd_parser = &read_mem_cmd_desc; |
bellard | 6f970bd | 2005-12-05 19:55:19 +0000 | [diff] [blame] | 2569 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2570 | break; |
| 2571 | case 'M': |
Jon Doron | cc0ecc7 | 2019-05-29 09:41:38 +0300 | [diff] [blame] | 2572 | { |
| 2573 | static const GdbCmdParseEntry write_mem_cmd_desc = { |
| 2574 | .handler = handle_write_mem, |
| 2575 | .cmd = "M", |
| 2576 | .cmd_startswith = 1, |
| 2577 | .schema = "L,L:s0" |
| 2578 | }; |
| 2579 | cmd_parser = &write_mem_cmd_desc; |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 2580 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2581 | break; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 2582 | case 'p': |
Jon Doron | 5d0e57b | 2019-05-29 09:41:37 +0300 | [diff] [blame] | 2583 | { |
| 2584 | static const GdbCmdParseEntry get_reg_cmd_desc = { |
| 2585 | .handler = handle_get_reg, |
| 2586 | .cmd = "p", |
| 2587 | .cmd_startswith = 1, |
| 2588 | .schema = "L0" |
| 2589 | }; |
| 2590 | cmd_parser = &get_reg_cmd_desc; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 2591 | } |
| 2592 | break; |
| 2593 | case 'P': |
Jon Doron | 62b3320 | 2019-05-29 09:41:36 +0300 | [diff] [blame] | 2594 | { |
| 2595 | static const GdbCmdParseEntry set_reg_cmd_desc = { |
| 2596 | .handler = handle_set_reg, |
| 2597 | .cmd = "P", |
| 2598 | .cmd_startswith = 1, |
| 2599 | .schema = "L?s0" |
| 2600 | }; |
| 2601 | cmd_parser = &set_reg_cmd_desc; |
| 2602 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 2603 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2604 | case 'Z': |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 2605 | { |
| 2606 | static const GdbCmdParseEntry insert_bp_cmd_desc = { |
| 2607 | .handler = handle_insert_bp, |
| 2608 | .cmd = "Z", |
| 2609 | .cmd_startswith = 1, |
| 2610 | .schema = "l?L?L0" |
| 2611 | }; |
| 2612 | cmd_parser = &insert_bp_cmd_desc; |
| 2613 | } |
| 2614 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2615 | case 'z': |
Jon Doron | 77f6ce5 | 2019-05-29 09:41:35 +0300 | [diff] [blame] | 2616 | { |
| 2617 | static const GdbCmdParseEntry remove_bp_cmd_desc = { |
| 2618 | .handler = handle_remove_bp, |
| 2619 | .cmd = "z", |
| 2620 | .cmd_startswith = 1, |
| 2621 | .schema = "l?L?L0" |
| 2622 | }; |
| 2623 | cmd_parser = &remove_bp_cmd_desc; |
| 2624 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2625 | break; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 2626 | case 'H': |
Jon Doron | 3a9651d | 2019-05-29 09:41:34 +0300 | [diff] [blame] | 2627 | { |
| 2628 | static const GdbCmdParseEntry set_thread_cmd_desc = { |
| 2629 | .handler = handle_set_thread, |
| 2630 | .cmd = "H", |
| 2631 | .cmd_startswith = 1, |
| 2632 | .schema = "o.t0" |
| 2633 | }; |
| 2634 | cmd_parser = &set_thread_cmd_desc; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 2635 | } |
| 2636 | break; |
| 2637 | case 'T': |
Jon Doron | 44ffded | 2019-05-29 09:41:31 +0300 | [diff] [blame] | 2638 | { |
| 2639 | static const GdbCmdParseEntry thread_alive_cmd_desc = { |
| 2640 | .handler = handle_thread_alive, |
| 2641 | .cmd = "T", |
| 2642 | .cmd_startswith = 1, |
| 2643 | .schema = "t0" |
| 2644 | }; |
| 2645 | cmd_parser = &thread_alive_cmd_desc; |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 2646 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 2647 | break; |
pbrook | 978efd6 | 2006-06-17 18:30:42 +0000 | [diff] [blame] | 2648 | case 'q': |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2649 | { |
| 2650 | static const GdbCmdParseEntry gen_query_cmd_desc = { |
| 2651 | .handler = handle_gen_query, |
| 2652 | .cmd = "q", |
| 2653 | .cmd_startswith = 1, |
| 2654 | .schema = "s0" |
| 2655 | }; |
| 2656 | cmd_parser = &gen_query_cmd_desc; |
| 2657 | } |
| 2658 | break; |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 2659 | case 'Q': |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2660 | { |
| 2661 | static const GdbCmdParseEntry gen_set_cmd_desc = { |
| 2662 | .handler = handle_gen_set, |
| 2663 | .cmd = "Q", |
| 2664 | .cmd_startswith = 1, |
| 2665 | .schema = "s0" |
| 2666 | }; |
| 2667 | cmd_parser = &gen_set_cmd_desc; |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 2668 | } |
Jon Doron | 2704efa | 2019-05-29 09:41:45 +0300 | [diff] [blame] | 2669 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2670 | default: |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2671 | /* put empty packet */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2672 | put_packet(""); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2673 | break; |
| 2674 | } |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 2675 | |
Ramiro Polla | 2bdec39 | 2019-08-05 21:09:01 +0200 | [diff] [blame] | 2676 | if (cmd_parser) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2677 | run_cmd_parser(line_buf, cmd_parser); |
Ramiro Polla | 2bdec39 | 2019-08-05 21:09:01 +0200 | [diff] [blame] | 2678 | } |
Jon Doron | 3e2c126 | 2019-05-29 09:41:30 +0300 | [diff] [blame] | 2679 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2680 | return RS_IDLE; |
| 2681 | } |
| 2682 | |
Andreas Färber | 64f6b34 | 2013-05-27 02:06:09 +0200 | [diff] [blame] | 2683 | void gdb_set_stop_cpu(CPUState *cpu) |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 2684 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2685 | GDBProcess *p = gdb_get_cpu_process(cpu); |
Luc Michel | 160d858 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2686 | |
| 2687 | if (!p->attached) { |
| 2688 | /* |
| 2689 | * Having a stop CPU corresponding to a process that is not attached |
| 2690 | * confuses GDB. So we ignore the request. |
| 2691 | */ |
| 2692 | return; |
| 2693 | } |
| 2694 | |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 2695 | gdbserver_state.c_cpu = cpu; |
| 2696 | gdbserver_state.g_cpu = cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2699 | #ifndef CONFIG_USER_ONLY |
Philippe Mathieu-Daudé | 538f049 | 2021-01-11 16:20:20 +0100 | [diff] [blame] | 2700 | static void gdb_vm_state_change(void *opaque, bool running, RunState state) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2701 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2702 | CPUState *cpu = gdbserver_state.c_cpu; |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2703 | g_autoptr(GString) buf = g_string_new(NULL); |
| 2704 | g_autoptr(GString) tid = g_string_new(NULL); |
aliguori | d6fc1b3 | 2008-11-18 19:55:44 +0000 | [diff] [blame] | 2705 | const char *type; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2706 | int ret; |
| 2707 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2708 | if (running || gdbserver_state.state == RS_INACTIVE) { |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2709 | return; |
| 2710 | } |
| 2711 | /* Is there a GDB syscall waiting to be sent? */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2712 | if (gdbserver_state.current_syscall_cb) { |
| 2713 | put_packet(gdbserver_state.syscall_buf); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2714 | return; |
Jan Kiszka | e07bbac | 2011-02-09 16:29:40 +0100 | [diff] [blame] | 2715 | } |
Luc Michel | 95567c2 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2716 | |
| 2717 | if (cpu == NULL) { |
| 2718 | /* No process attached */ |
| 2719 | return; |
| 2720 | } |
| 2721 | |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2722 | gdb_append_thread_id(cpu, tid); |
Luc Michel | 95567c2 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2723 | |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 2724 | switch (state) { |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2725 | case RUN_STATE_DEBUG: |
Andreas Färber | ff4700b | 2013-08-26 18:23:18 +0200 | [diff] [blame] | 2726 | if (cpu->watchpoint_hit) { |
| 2727 | switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 2728 | case BP_MEM_READ: |
aliguori | d6fc1b3 | 2008-11-18 19:55:44 +0000 | [diff] [blame] | 2729 | type = "r"; |
| 2730 | break; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 2731 | case BP_MEM_ACCESS: |
aliguori | d6fc1b3 | 2008-11-18 19:55:44 +0000 | [diff] [blame] | 2732 | type = "a"; |
| 2733 | break; |
| 2734 | default: |
| 2735 | type = ""; |
| 2736 | break; |
| 2737 | } |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2738 | trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu), |
| 2739 | (target_ulong)cpu->watchpoint_hit->vaddr); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2740 | g_string_printf(buf, "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";", |
| 2741 | GDB_SIGNAL_TRAP, tid->str, type, |
| 2742 | (target_ulong)cpu->watchpoint_hit->vaddr); |
Andreas Färber | ff4700b | 2013-08-26 18:23:18 +0200 | [diff] [blame] | 2743 | cpu->watchpoint_hit = NULL; |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 2744 | goto send_packet; |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2745 | } else { |
| 2746 | trace_gdbstub_hit_break(); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2747 | } |
Peter Crosthwaite | bbd77c1 | 2015-06-23 19:31:15 -0700 | [diff] [blame] | 2748 | tb_flush(cpu); |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 2749 | ret = GDB_SIGNAL_TRAP; |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 2750 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2751 | case RUN_STATE_PAUSED: |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2752 | trace_gdbstub_hit_paused(); |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 2753 | ret = GDB_SIGNAL_INT; |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 2754 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2755 | case RUN_STATE_SHUTDOWN: |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2756 | trace_gdbstub_hit_shutdown(); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 2757 | ret = GDB_SIGNAL_QUIT; |
| 2758 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2759 | case RUN_STATE_IO_ERROR: |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2760 | trace_gdbstub_hit_io_error(); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 2761 | ret = GDB_SIGNAL_IO; |
| 2762 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2763 | case RUN_STATE_WATCHDOG: |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2764 | trace_gdbstub_hit_watchdog(); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 2765 | ret = GDB_SIGNAL_ALRM; |
| 2766 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2767 | case RUN_STATE_INTERNAL_ERROR: |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2768 | trace_gdbstub_hit_internal_error(); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 2769 | ret = GDB_SIGNAL_ABRT; |
| 2770 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2771 | case RUN_STATE_SAVE_VM: |
| 2772 | case RUN_STATE_RESTORE_VM: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 2773 | return; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2774 | case RUN_STATE_FINISH_MIGRATE: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 2775 | ret = GDB_SIGNAL_XCPU; |
| 2776 | break; |
| 2777 | default: |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2778 | trace_gdbstub_hit_unknown(state); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 2779 | ret = GDB_SIGNAL_UNKNOWN; |
| 2780 | break; |
bellard | bbeb7b5 | 2006-04-23 18:42:15 +0000 | [diff] [blame] | 2781 | } |
Jan Kiszka | 226d007 | 2015-07-24 18:52:31 +0200 | [diff] [blame] | 2782 | gdb_set_stop_cpu(cpu); |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2783 | g_string_printf(buf, "T%02xthread:%s;", ret, tid->str); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 2784 | |
| 2785 | send_packet: |
Alex Bennée | 308f9e8 | 2020-03-16 17:21:35 +0000 | [diff] [blame] | 2786 | put_packet(buf->str); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 2787 | |
| 2788 | /* disable single step if it was enabled */ |
Andreas Färber | 3825b28 | 2013-06-24 18:41:06 +0200 | [diff] [blame] | 2789 | cpu_single_step(cpu, 0); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2790 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2791 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2792 | |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2793 | /* Send a gdb syscall request. |
| 2794 | This accepts limited printf-style format specifiers, specifically: |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 2795 | %x - target_ulong argument printed in hex. |
| 2796 | %lx - 64-bit argument printed in hex. |
| 2797 | %s - string pointer (target_ulong) and length (int) pair. */ |
Peter Maydell | 19239b3 | 2015-09-07 10:39:27 +0100 | [diff] [blame] | 2798 | void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va) |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2799 | { |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2800 | char *p; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2801 | char *p_end; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2802 | target_ulong addr; |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 2803 | uint64_t i64; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2804 | |
Peter Maydell | ebf1b4c | 2022-06-10 14:32:36 +0100 | [diff] [blame] | 2805 | if (!gdb_attached()) { |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2806 | return; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2807 | } |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 2808 | |
| 2809 | gdbserver_state.current_syscall_cb = cb; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2810 | #ifndef CONFIG_USER_ONLY |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2811 | vm_stop(RUN_STATE_DEBUG); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2812 | #endif |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 2813 | p = &gdbserver_state.syscall_buf[0]; |
| 2814 | p_end = &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_buf)]; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2815 | *(p++) = 'F'; |
| 2816 | while (*fmt) { |
| 2817 | if (*fmt == '%') { |
| 2818 | fmt++; |
| 2819 | switch (*fmt++) { |
| 2820 | case 'x': |
| 2821 | addr = va_arg(va, target_ulong); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2822 | p += snprintf(p, p_end - p, TARGET_FMT_lx, addr); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2823 | break; |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 2824 | case 'l': |
| 2825 | if (*(fmt++) != 'x') |
| 2826 | goto bad_format; |
| 2827 | i64 = va_arg(va, uint64_t); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2828 | p += snprintf(p, p_end - p, "%" PRIx64, i64); |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 2829 | break; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2830 | case 's': |
| 2831 | addr = va_arg(va, target_ulong); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2832 | p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x", |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 2833 | addr, va_arg(va, int)); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2834 | break; |
| 2835 | default: |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 2836 | bad_format: |
Ziyue Yang | 7ae6c57 | 2017-01-18 16:03:29 +0800 | [diff] [blame] | 2837 | error_report("gdbstub: Bad syscall format string '%s'", |
| 2838 | fmt - 1); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2839 | break; |
| 2840 | } |
| 2841 | } else { |
| 2842 | *(p++) = *(fmt++); |
| 2843 | } |
| 2844 | } |
pbrook | 8a93e02 | 2007-08-06 13:19:15 +0000 | [diff] [blame] | 2845 | *p = 0; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2846 | #ifdef CONFIG_USER_ONLY |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2847 | put_packet(gdbserver_state.syscall_buf); |
Peter Maydell | 4f71086 | 2018-05-15 19:19:58 +0100 | [diff] [blame] | 2848 | /* Return control to gdb for it to process the syscall request. |
| 2849 | * Since the protocol requires that gdb hands control back to us |
| 2850 | * using a "here are the results" F packet, we don't need to check |
| 2851 | * gdb_handlesig's return value (which is the signal to deliver if |
| 2852 | * execution was resumed via a continue packet). |
| 2853 | */ |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 2854 | gdb_handlesig(gdbserver_state.c_cpu, 0); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2855 | #else |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2856 | /* In this case wait to send the syscall packet until notification that |
| 2857 | the CPU has stopped. This must be done because if the packet is sent |
| 2858 | now the reply from the syscall request could be received while the CPU |
| 2859 | is still in the running state, which can cause packets to be dropped |
| 2860 | and state transition 'T' packets to be sent while the syscall is still |
| 2861 | being processed. */ |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 2862 | qemu_cpu_kick(gdbserver_state.c_cpu); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2863 | #endif |
| 2864 | } |
| 2865 | |
Peter Maydell | 19239b3 | 2015-09-07 10:39:27 +0100 | [diff] [blame] | 2866 | void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...) |
| 2867 | { |
| 2868 | va_list va; |
| 2869 | |
| 2870 | va_start(va, fmt); |
| 2871 | gdb_do_syscallv(cb, fmt, va); |
| 2872 | va_end(va); |
| 2873 | } |
| 2874 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2875 | static void gdb_read_byte(uint8_t ch) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2876 | { |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 2877 | uint8_t reply; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2878 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2879 | #ifndef CONFIG_USER_ONLY |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 2880 | if (gdbserver_state.last_packet->len) { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2881 | /* Waiting for a response to the last packet. If we see the start |
| 2882 | of a new command then abandon the previous response. */ |
| 2883 | if (ch == '-') { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2884 | trace_gdbstub_err_got_nack(); |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 2885 | put_buffer(gdbserver_state.last_packet->data, |
| 2886 | gdbserver_state.last_packet->len); |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 2887 | } else if (ch == '+') { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2888 | trace_gdbstub_io_got_ack(); |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 2889 | } else { |
Markus Armbruster | 33c846e | 2019-05-14 20:03:09 +0200 | [diff] [blame] | 2890 | trace_gdbstub_io_got_unexpected(ch); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2891 | } |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 2892 | |
Damien Hedde | d116e81 | 2020-03-16 17:21:53 +0000 | [diff] [blame] | 2893 | if (ch == '+' || ch == '$') { |
| 2894 | g_byte_array_set_size(gdbserver_state.last_packet, 0); |
| 2895 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2896 | if (ch != '$') |
| 2897 | return; |
| 2898 | } |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 2899 | if (runstate_is_running()) { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2900 | /* when the CPU is running, we cannot do anything except stop |
| 2901 | it when receiving a char */ |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2902 | vm_stop(RUN_STATE_PAUSED); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2903 | } else |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2904 | #endif |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 2905 | { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2906 | switch(gdbserver_state.state) { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2907 | case RS_IDLE: |
| 2908 | if (ch == '$') { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2909 | /* start of command packet */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2910 | gdbserver_state.line_buf_index = 0; |
| 2911 | gdbserver_state.line_sum = 0; |
| 2912 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2913 | } else { |
Markus Armbruster | 33c846e | 2019-05-14 20:03:09 +0200 | [diff] [blame] | 2914 | trace_gdbstub_err_garbage(ch); |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 2915 | } |
| 2916 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2917 | case RS_GETLINE: |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2918 | if (ch == '}') { |
| 2919 | /* start escape sequence */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2920 | gdbserver_state.state = RS_GETLINE_ESC; |
| 2921 | gdbserver_state.line_sum += ch; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2922 | } else if (ch == '*') { |
| 2923 | /* start run length encoding sequence */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2924 | gdbserver_state.state = RS_GETLINE_RLE; |
| 2925 | gdbserver_state.line_sum += ch; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2926 | } else if (ch == '#') { |
| 2927 | /* end of command, start of checksum*/ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2928 | gdbserver_state.state = RS_CHKSUM1; |
| 2929 | } 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] | 2930 | trace_gdbstub_err_overrun(); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2931 | gdbserver_state.state = RS_IDLE; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2932 | } else { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2933 | /* unescaped command character */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2934 | gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch; |
| 2935 | gdbserver_state.line_sum += ch; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2936 | } |
| 2937 | break; |
| 2938 | case RS_GETLINE_ESC: |
| 2939 | if (ch == '#') { |
| 2940 | /* unexpected end of command in escape sequence */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2941 | gdbserver_state.state = RS_CHKSUM1; |
| 2942 | } 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] | 2943 | /* command buffer overrun */ |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2944 | trace_gdbstub_err_overrun(); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2945 | gdbserver_state.state = RS_IDLE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2946 | } else { |
| 2947 | /* parse escaped character and leave escape state */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2948 | gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20; |
| 2949 | gdbserver_state.line_sum += ch; |
| 2950 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2951 | } |
| 2952 | break; |
| 2953 | case RS_GETLINE_RLE: |
Markus Armbruster | 046aba1 | 2019-05-14 20:03:08 +0200 | [diff] [blame] | 2954 | /* |
| 2955 | * Run-length encoding is explained in "Debugging with GDB / |
| 2956 | * Appendix E GDB Remote Serial Protocol / Overview". |
| 2957 | */ |
| 2958 | if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2959 | /* invalid RLE count encoding */ |
Markus Armbruster | 33c846e | 2019-05-14 20:03:09 +0200 | [diff] [blame] | 2960 | trace_gdbstub_err_invalid_repeat(ch); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2961 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2962 | } else { |
| 2963 | /* decode repeat length */ |
Markus Armbruster | 33c846e | 2019-05-14 20:03:09 +0200 | [diff] [blame] | 2964 | int repeat = ch - ' ' + 3; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2965 | 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] | 2966 | /* that many repeats would overrun the command buffer */ |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2967 | trace_gdbstub_err_overrun(); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2968 | gdbserver_state.state = RS_IDLE; |
| 2969 | } else if (gdbserver_state.line_buf_index < 1) { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2970 | /* got a repeat but we have nothing to repeat */ |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2971 | trace_gdbstub_err_invalid_rle(); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2972 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2973 | } else { |
| 2974 | /* repeat the last character */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2975 | memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index, |
| 2976 | gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat); |
| 2977 | gdbserver_state.line_buf_index += repeat; |
| 2978 | gdbserver_state.line_sum += ch; |
| 2979 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2980 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2981 | } |
| 2982 | break; |
| 2983 | case RS_CHKSUM1: |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2984 | /* get high hex digit of checksum */ |
| 2985 | if (!isxdigit(ch)) { |
Markus Armbruster | 33c846e | 2019-05-14 20:03:09 +0200 | [diff] [blame] | 2986 | trace_gdbstub_err_checksum_invalid(ch); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2987 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2988 | break; |
| 2989 | } |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2990 | gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0'; |
| 2991 | gdbserver_state.line_csum = fromhex(ch) << 4; |
| 2992 | gdbserver_state.state = RS_CHKSUM2; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2993 | break; |
| 2994 | case RS_CHKSUM2: |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2995 | /* get low hex digit of checksum */ |
| 2996 | if (!isxdigit(ch)) { |
Markus Armbruster | 33c846e | 2019-05-14 20:03:09 +0200 | [diff] [blame] | 2997 | trace_gdbstub_err_checksum_invalid(ch); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 2998 | gdbserver_state.state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2999 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3000 | } |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 3001 | gdbserver_state.line_csum |= fromhex(ch); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 3002 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 3003 | if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) { |
| 3004 | 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] | 3005 | /* send NAK reply */ |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 3006 | reply = '-'; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 3007 | put_buffer(&reply, 1); |
| 3008 | gdbserver_state.state = RS_IDLE; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3009 | } else { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 3010 | /* send ACK reply */ |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 3011 | reply = '+'; |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 3012 | put_buffer(&reply, 1); |
| 3013 | gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3014 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 3015 | break; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 3016 | default: |
| 3017 | abort(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 3018 | } |
| 3019 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 3022 | /* Tell the remote gdb that the process has exited. */ |
Alex Bennée | ad9dcb2 | 2021-01-08 22:42:43 +0000 | [diff] [blame] | 3023 | void gdb_exit(int code) |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 3024 | { |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 3025 | char buf[4]; |
| 3026 | |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3027 | if (!gdbserver_state.init) { |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 3028 | return; |
| 3029 | } |
| 3030 | #ifdef CONFIG_USER_ONLY |
Alex Bennée | fcedd92 | 2020-04-30 20:01:19 +0100 | [diff] [blame] | 3031 | if (gdbserver_state.socket_path) { |
| 3032 | unlink(gdbserver_state.socket_path); |
| 3033 | } |
Alex Bennée | e0a1e20 | 2020-04-30 20:01:18 +0100 | [diff] [blame] | 3034 | if (gdbserver_state.fd < 0) { |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 3035 | return; |
| 3036 | } |
| 3037 | #endif |
| 3038 | |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 3039 | trace_gdbstub_op_exiting((uint8_t)code); |
| 3040 | |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 3041 | snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 3042 | put_packet(buf); |
Fabien Chouteau | e2af15b | 2011-01-13 12:46:57 +0100 | [diff] [blame] | 3043 | |
| 3044 | #ifndef CONFIG_USER_ONLY |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3045 | qemu_chr_fe_deinit(&gdbserver_state.chr, true); |
Fabien Chouteau | e2af15b | 2011-01-13 12:46:57 +0100 | [diff] [blame] | 3046 | #endif |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 3047 | } |
| 3048 | |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 3049 | /* |
| 3050 | * Create the process that will contain all the "orphan" CPUs (that are not |
| 3051 | * part of a CPU cluster). Note that if this process contains no CPUs, it won't |
| 3052 | * be attachable and thus will be invisible to the user. |
| 3053 | */ |
| 3054 | static void create_default_process(GDBState *s) |
| 3055 | { |
| 3056 | GDBProcess *process; |
| 3057 | int max_pid = 0; |
| 3058 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 3059 | if (gdbserver_state.process_num) { |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 3060 | max_pid = s->processes[s->process_num - 1].pid; |
| 3061 | } |
| 3062 | |
| 3063 | s->processes = g_renew(GDBProcess, s->processes, ++s->process_num); |
| 3064 | process = &s->processes[s->process_num - 1]; |
| 3065 | |
| 3066 | /* We need an available PID slot for this process */ |
| 3067 | assert(max_pid < UINT32_MAX); |
| 3068 | |
| 3069 | process->pid = max_pid + 1; |
| 3070 | process->attached = false; |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 3071 | process->target_xml[0] = '\0'; |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 3074 | #ifdef CONFIG_USER_ONLY |
| 3075 | int |
Andreas Färber | db6b81d | 2013-06-27 19:49:31 +0200 | [diff] [blame] | 3076 | gdb_handlesig(CPUState *cpu, int sig) |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 3077 | { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3078 | char buf[256]; |
| 3079 | int n; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 3080 | |
Alex Bennée | e0a1e20 | 2020-04-30 20:01:18 +0100 | [diff] [blame] | 3081 | if (!gdbserver_state.init || gdbserver_state.fd < 0) { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3082 | return sig; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3085 | /* disable single step if it was enabled */ |
Andreas Färber | 3825b28 | 2013-06-24 18:41:06 +0200 | [diff] [blame] | 3086 | cpu_single_step(cpu, 0); |
Peter Crosthwaite | bbd77c1 | 2015-06-23 19:31:15 -0700 | [diff] [blame] | 3087 | tb_flush(cpu); |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 3088 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3089 | if (sig != 0) { |
Pavel Labath | 4a82be7 | 2021-10-26 11:22:34 +0100 | [diff] [blame] | 3090 | gdb_set_stop_cpu(cpu); |
| 3091 | g_string_printf(gdbserver_state.str_buf, |
| 3092 | "T%02xthread:", target_signal_to_gdb(sig)); |
| 3093 | gdb_append_thread_id(cpu, gdbserver_state.str_buf); |
| 3094 | g_string_append_c(gdbserver_state.str_buf, ';'); |
| 3095 | put_strbuf(); |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3096 | } |
| 3097 | /* put_packet() might have detected that the peer terminated the |
| 3098 | connection. */ |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3099 | if (gdbserver_state.fd < 0) { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3100 | return sig; |
| 3101 | } |
| 3102 | |
| 3103 | sig = 0; |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3104 | gdbserver_state.state = RS_IDLE; |
| 3105 | gdbserver_state.running_state = 0; |
| 3106 | while (gdbserver_state.running_state == 0) { |
| 3107 | n = read(gdbserver_state.fd, buf, 256); |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3108 | if (n > 0) { |
| 3109 | int i; |
| 3110 | |
| 3111 | for (i = 0; i < n; i++) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 3112 | gdb_read_byte(buf[i]); |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3113 | } |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 3114 | } else { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3115 | /* XXX: Connection closed. Should probably wait for another |
| 3116 | connection before continuing. */ |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 3117 | if (n == 0) { |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3118 | close(gdbserver_state.fd); |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 3119 | } |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3120 | gdbserver_state.fd = -1; |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3121 | return sig; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 3122 | } |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3123 | } |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3124 | sig = gdbserver_state.signal; |
| 3125 | gdbserver_state.signal = 0; |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3126 | return sig; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 3127 | } |
bellard | e900967 | 2005-04-26 20:42:36 +0000 | [diff] [blame] | 3128 | |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 3129 | /* Tell the remote gdb that the process has exited due to SIG. */ |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 3130 | void gdb_signalled(CPUArchState *env, int sig) |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 3131 | { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3132 | char buf[4]; |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 3133 | |
Alex Bennée | e0a1e20 | 2020-04-30 20:01:18 +0100 | [diff] [blame] | 3134 | if (!gdbserver_state.init || gdbserver_state.fd < 0) { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3135 | return; |
| 3136 | } |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 3137 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 3138 | snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig)); |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 3139 | put_packet(buf); |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 3140 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 3141 | |
Alex Bennée | fcedd92 | 2020-04-30 20:01:19 +0100 | [diff] [blame] | 3142 | static void gdb_accept_init(int fd) |
| 3143 | { |
| 3144 | init_gdbserver_state(); |
| 3145 | create_default_process(&gdbserver_state); |
| 3146 | gdbserver_state.processes[0].attached = true; |
| 3147 | gdbserver_state.c_cpu = gdb_first_attached_cpu(); |
| 3148 | gdbserver_state.g_cpu = gdbserver_state.c_cpu; |
| 3149 | gdbserver_state.fd = fd; |
| 3150 | gdb_has_xml = false; |
| 3151 | } |
| 3152 | |
| 3153 | static bool gdb_accept_socket(int gdb_fd) |
| 3154 | { |
| 3155 | int fd; |
| 3156 | |
| 3157 | for(;;) { |
| 3158 | fd = accept(gdb_fd, NULL, NULL); |
| 3159 | if (fd < 0 && errno != EINTR) { |
| 3160 | perror("accept socket"); |
| 3161 | return false; |
| 3162 | } else if (fd >= 0) { |
| 3163 | qemu_set_cloexec(fd); |
| 3164 | break; |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | gdb_accept_init(fd); |
| 3169 | return true; |
| 3170 | } |
| 3171 | |
| 3172 | static int gdbserver_open_socket(const char *path) |
| 3173 | { |
Peter Maydell | fdcdf54 | 2021-08-13 16:05:04 +0100 | [diff] [blame] | 3174 | struct sockaddr_un sockaddr = {}; |
Alex Bennée | fcedd92 | 2020-04-30 20:01:19 +0100 | [diff] [blame] | 3175 | int fd, ret; |
| 3176 | |
| 3177 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 3178 | if (fd < 0) { |
| 3179 | perror("create socket"); |
| 3180 | return -1; |
| 3181 | } |
| 3182 | |
| 3183 | sockaddr.sun_family = AF_UNIX; |
| 3184 | pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path); |
| 3185 | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
| 3186 | if (ret < 0) { |
| 3187 | perror("bind socket"); |
| 3188 | close(fd); |
| 3189 | return -1; |
| 3190 | } |
| 3191 | ret = listen(fd, 1); |
| 3192 | if (ret < 0) { |
| 3193 | perror("listen socket"); |
| 3194 | close(fd); |
| 3195 | return -1; |
| 3196 | } |
| 3197 | |
| 3198 | return fd; |
| 3199 | } |
| 3200 | |
| 3201 | static bool gdb_accept_tcp(int gdb_fd) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3202 | { |
Peter Maydell | fdcdf54 | 2021-08-13 16:05:04 +0100 | [diff] [blame] | 3203 | struct sockaddr_in sockaddr = {}; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3204 | socklen_t len; |
MORITA Kazutaka | bf1c852 | 2013-02-22 12:39:50 +0900 | [diff] [blame] | 3205 | int fd; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3206 | |
| 3207 | for(;;) { |
| 3208 | len = sizeof(sockaddr); |
Alex Bennée | e0a1e20 | 2020-04-30 20:01:18 +0100 | [diff] [blame] | 3209 | fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3210 | if (fd < 0 && errno != EINTR) { |
| 3211 | perror("accept"); |
Peter Maydell | 2f65222 | 2018-05-14 18:30:44 +0100 | [diff] [blame] | 3212 | return false; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3213 | } else if (fd >= 0) { |
Peter Maydell | f5bdd78 | 2018-05-14 18:30:43 +0100 | [diff] [blame] | 3214 | qemu_set_cloexec(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3215 | break; |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | /* set short latency */ |
Peter Maydell | 2f65222 | 2018-05-14 18:30:44 +0100 | [diff] [blame] | 3220 | if (socket_set_nodelay(fd)) { |
| 3221 | perror("setsockopt"); |
Philippe Mathieu-Daudé | ead75d8 | 2018-05-24 19:34:58 -0300 | [diff] [blame] | 3222 | close(fd); |
Peter Maydell | 2f65222 | 2018-05-14 18:30:44 +0100 | [diff] [blame] | 3223 | return false; |
| 3224 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3225 | |
Alex Bennée | fcedd92 | 2020-04-30 20:01:19 +0100 | [diff] [blame] | 3226 | gdb_accept_init(fd); |
Peter Maydell | 2f65222 | 2018-05-14 18:30:44 +0100 | [diff] [blame] | 3227 | return true; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
Alex Bennée | fcedd92 | 2020-04-30 20:01:19 +0100 | [diff] [blame] | 3230 | static int gdbserver_open_port(int port) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3231 | { |
| 3232 | struct sockaddr_in sockaddr; |
Sebastian Ottlik | 6669ca1 | 2013-10-02 12:23:13 +0200 | [diff] [blame] | 3233 | int fd, ret; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3234 | |
| 3235 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 3236 | if (fd < 0) { |
| 3237 | perror("socket"); |
| 3238 | return -1; |
| 3239 | } |
Peter Maydell | f5bdd78 | 2018-05-14 18:30:43 +0100 | [diff] [blame] | 3240 | qemu_set_cloexec(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3241 | |
Sebastian Ottlik | 6669ca1 | 2013-10-02 12:23:13 +0200 | [diff] [blame] | 3242 | socket_set_fast_reuse(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3243 | |
| 3244 | sockaddr.sin_family = AF_INET; |
| 3245 | sockaddr.sin_port = htons(port); |
| 3246 | sockaddr.sin_addr.s_addr = 0; |
| 3247 | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
| 3248 | if (ret < 0) { |
| 3249 | perror("bind"); |
Peter Maydell | bb16172 | 2011-12-24 23:37:24 +0000 | [diff] [blame] | 3250 | close(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3251 | return -1; |
| 3252 | } |
Peter Wu | 96165b9 | 2016-05-04 11:32:17 +0200 | [diff] [blame] | 3253 | ret = listen(fd, 1); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3254 | if (ret < 0) { |
| 3255 | perror("listen"); |
Peter Maydell | bb16172 | 2011-12-24 23:37:24 +0000 | [diff] [blame] | 3256 | close(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3257 | return -1; |
| 3258 | } |
Alex Bennée | fcedd92 | 2020-04-30 20:01:19 +0100 | [diff] [blame] | 3259 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3260 | return fd; |
| 3261 | } |
| 3262 | |
Alex Bennée | fcedd92 | 2020-04-30 20:01:19 +0100 | [diff] [blame] | 3263 | int gdbserver_start(const char *port_or_path) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3264 | { |
Alex Bennée | fcedd92 | 2020-04-30 20:01:19 +0100 | [diff] [blame] | 3265 | int port = g_ascii_strtoull(port_or_path, NULL, 10); |
| 3266 | int gdb_fd; |
| 3267 | |
| 3268 | if (port > 0) { |
| 3269 | gdb_fd = gdbserver_open_port(port); |
| 3270 | } else { |
| 3271 | gdb_fd = gdbserver_open_socket(port_or_path); |
| 3272 | } |
| 3273 | |
Alex Bennée | e0a1e20 | 2020-04-30 20:01:18 +0100 | [diff] [blame] | 3274 | if (gdb_fd < 0) { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 3275 | return -1; |
Alex Bennée | e0a1e20 | 2020-04-30 20:01:18 +0100 | [diff] [blame] | 3276 | } |
Alex Bennée | fcedd92 | 2020-04-30 20:01:19 +0100 | [diff] [blame] | 3277 | |
| 3278 | if (port > 0 && gdb_accept_tcp(gdb_fd)) { |
| 3279 | return 0; |
| 3280 | } else if (gdb_accept_socket(gdb_fd)) { |
| 3281 | gdbserver_state.socket_path = g_strdup(port_or_path); |
| 3282 | return 0; |
Peter Maydell | 2f65222 | 2018-05-14 18:30:44 +0100 | [diff] [blame] | 3283 | } |
Alex Bennée | fcedd92 | 2020-04-30 20:01:19 +0100 | [diff] [blame] | 3284 | |
| 3285 | /* gone wrong */ |
| 3286 | close(gdb_fd); |
| 3287 | return -1; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 3288 | } |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 3289 | |
| 3290 | /* Disable gdb stub for child processes. */ |
Peter Crosthwaite | f7ec7f7 | 2015-06-23 19:31:16 -0700 | [diff] [blame] | 3291 | void gdbserver_fork(CPUState *cpu) |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 3292 | { |
Alex Bennée | e0a1e20 | 2020-04-30 20:01:18 +0100 | [diff] [blame] | 3293 | if (!gdbserver_state.init || gdbserver_state.fd < 0) { |
Andreas Färber | 75a3403 | 2013-09-02 16:57:02 +0200 | [diff] [blame] | 3294 | return; |
| 3295 | } |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3296 | close(gdbserver_state.fd); |
| 3297 | gdbserver_state.fd = -1; |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 3298 | cpu_breakpoint_remove_all(cpu, BP_GDB); |
Andreas Färber | 75a3403 | 2013-09-02 16:57:02 +0200 | [diff] [blame] | 3299 | cpu_watchpoint_remove_all(cpu, BP_GDB); |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 3300 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3301 | #else |
ths | aa1f17c | 2007-07-11 22:48:58 +0000 | [diff] [blame] | 3302 | static int gdb_chr_can_receive(void *opaque) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3303 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 3304 | /* We can handle an arbitrarily large amount of data. |
| 3305 | Pick the maximum packet size, which is as good as anything. */ |
| 3306 | return MAX_PACKET_LENGTH; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
ths | aa1f17c | 2007-07-11 22:48:58 +0000 | [diff] [blame] | 3309 | static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3310 | { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3311 | int i; |
| 3312 | |
| 3313 | for (i = 0; i < size; i++) { |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 3314 | gdb_read_byte(buf[i]); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3315 | } |
| 3316 | } |
| 3317 | |
Philippe Mathieu-Daudé | 083b266 | 2019-12-18 18:20:09 +0100 | [diff] [blame] | 3318 | static void gdb_chr_event(void *opaque, QEMUChrEvent event) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3319 | { |
Luc Michel | 970ed90 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 3320 | int i; |
| 3321 | GDBState *s = (GDBState *) opaque; |
| 3322 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3323 | switch (event) { |
Amit Shah | b6b8df5 | 2009-10-07 18:31:16 +0530 | [diff] [blame] | 3324 | case CHR_EVENT_OPENED: |
Luc Michel | 970ed90 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 3325 | /* Start with first process attached, others detached */ |
| 3326 | for (i = 0; i < s->process_num; i++) { |
| 3327 | s->processes[i].attached = !i; |
| 3328 | } |
| 3329 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 3330 | s->c_cpu = gdb_first_attached_cpu(); |
Luc Michel | 970ed90 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 3331 | s->g_cpu = s->c_cpu; |
| 3332 | |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 3333 | vm_stop(RUN_STATE_PAUSED); |
Pavel Dovgalyuk | 56357d8 | 2020-10-03 20:14:01 +0300 | [diff] [blame] | 3334 | replay_gdb_attached(); |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 3335 | gdb_has_xml = false; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3336 | break; |
| 3337 | default: |
| 3338 | break; |
| 3339 | } |
| 3340 | } |
| 3341 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 3342 | static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len) |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 3343 | { |
Damien Hedde | d86b467 | 2020-03-16 17:21:54 +0000 | [diff] [blame] | 3344 | g_autoptr(GString) hex_buf = g_string_new("O"); |
| 3345 | memtohex(hex_buf, buf, len); |
| 3346 | put_packet(hex_buf->str); |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 3347 | return len; |
| 3348 | } |
| 3349 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 3350 | #ifndef _WIN32 |
| 3351 | static void gdb_sigterm_handler(int signal) |
| 3352 | { |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 3353 | if (runstate_is_running()) { |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 3354 | vm_stop(RUN_STATE_PAUSED); |
Jan Kiszka | e07bbac | 2011-02-09 16:29:40 +0100 | [diff] [blame] | 3355 | } |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 3356 | } |
| 3357 | #endif |
| 3358 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 3359 | static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend, |
| 3360 | bool *be_opened, Error **errp) |
| 3361 | { |
| 3362 | *be_opened = false; |
| 3363 | } |
| 3364 | |
| 3365 | static void char_gdb_class_init(ObjectClass *oc, void *data) |
| 3366 | { |
| 3367 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 3368 | |
| 3369 | cc->internal = true; |
| 3370 | cc->open = gdb_monitor_open; |
| 3371 | cc->chr_write = gdb_monitor_write; |
| 3372 | } |
| 3373 | |
| 3374 | #define TYPE_CHARDEV_GDB "chardev-gdb" |
| 3375 | |
| 3376 | static const TypeInfo char_gdb_type_info = { |
| 3377 | .name = TYPE_CHARDEV_GDB, |
| 3378 | .parent = TYPE_CHARDEV, |
| 3379 | .class_init = char_gdb_class_init, |
| 3380 | }; |
| 3381 | |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 3382 | static int find_cpu_clusters(Object *child, void *opaque) |
| 3383 | { |
| 3384 | if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) { |
| 3385 | GDBState *s = (GDBState *) opaque; |
| 3386 | CPUClusterState *cluster = CPU_CLUSTER(child); |
| 3387 | GDBProcess *process; |
| 3388 | |
| 3389 | s->processes = g_renew(GDBProcess, s->processes, ++s->process_num); |
| 3390 | |
| 3391 | process = &s->processes[s->process_num - 1]; |
| 3392 | |
| 3393 | /* |
| 3394 | * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at |
| 3395 | * runtime, we enforce here that the machine does not use a cluster ID |
| 3396 | * that would lead to PID 0. |
| 3397 | */ |
| 3398 | assert(cluster->cluster_id != UINT32_MAX); |
| 3399 | process->pid = cluster->cluster_id + 1; |
| 3400 | process->attached = false; |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 3401 | process->target_xml[0] = '\0'; |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 3402 | |
| 3403 | return 0; |
| 3404 | } |
| 3405 | |
| 3406 | return object_child_foreach(child, find_cpu_clusters, opaque); |
| 3407 | } |
| 3408 | |
| 3409 | static int pid_order(const void *a, const void *b) |
| 3410 | { |
| 3411 | GDBProcess *pa = (GDBProcess *) a; |
| 3412 | GDBProcess *pb = (GDBProcess *) b; |
| 3413 | |
| 3414 | if (pa->pid < pb->pid) { |
| 3415 | return -1; |
| 3416 | } else if (pa->pid > pb->pid) { |
| 3417 | return 1; |
| 3418 | } else { |
| 3419 | return 0; |
| 3420 | } |
| 3421 | } |
| 3422 | |
| 3423 | static void create_processes(GDBState *s) |
| 3424 | { |
| 3425 | object_child_foreach(object_get_root(), find_cpu_clusters, s); |
| 3426 | |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 3427 | if (gdbserver_state.processes) { |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 3428 | /* Sort by PID */ |
Alex Bennée | a346af3 | 2020-03-16 17:21:34 +0000 | [diff] [blame] | 3429 | qsort(gdbserver_state.processes, gdbserver_state.process_num, sizeof(gdbserver_state.processes[0]), pid_order); |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | create_default_process(s); |
| 3433 | } |
| 3434 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 3435 | int gdbserver_start(const char *device) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3436 | { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 3437 | trace_gdbstub_op_start(device); |
| 3438 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 3439 | char gdbstub_device_name[128]; |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 3440 | Chardev *chr = NULL; |
| 3441 | Chardev *mon_chr; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3442 | |
Ziyue Yang | 508b4ec | 2017-01-18 16:02:41 +0800 | [diff] [blame] | 3443 | if (!first_cpu) { |
| 3444 | error_report("gdbstub: meaningless to attach gdb to a " |
| 3445 | "machine without any CPU."); |
| 3446 | return -1; |
| 3447 | } |
| 3448 | |
Alex Bennée | a48e7d9 | 2022-09-29 12:42:25 +0100 | [diff] [blame] | 3449 | if (!gdb_supports_guest_debug()) { |
| 3450 | error_report("gdbstub: current accelerator doesn't support guest debugging"); |
Maxim Levitsky | 12bc5b4 | 2021-11-11 12:06:03 +0100 | [diff] [blame] | 3451 | return -1; |
| 3452 | } |
| 3453 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 3454 | if (!device) |
| 3455 | return -1; |
| 3456 | if (strcmp(device, "none") != 0) { |
| 3457 | if (strstart(device, "tcp:", NULL)) { |
| 3458 | /* enforce required TCP attributes */ |
| 3459 | snprintf(gdbstub_device_name, sizeof(gdbstub_device_name), |
Paolo Bonzini | a9b1315 | 2021-02-25 11:47:52 +0100 | [diff] [blame] | 3460 | "%s,wait=off,nodelay=on,server=on", device); |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 3461 | device = gdbstub_device_name; |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 3462 | } |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 3463 | #ifndef _WIN32 |
| 3464 | else if (strcmp(device, "stdio") == 0) { |
| 3465 | struct sigaction act; |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 3466 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 3467 | memset(&act, 0, sizeof(act)); |
| 3468 | act.sa_handler = gdb_sigterm_handler; |
| 3469 | sigaction(SIGINT, &act, NULL); |
| 3470 | } |
| 3471 | #endif |
Marc-André Lureau | 95e30b2 | 2018-08-22 19:19:42 +0200 | [diff] [blame] | 3472 | /* |
| 3473 | * FIXME: it's a bit weird to allow using a mux chardev here |
| 3474 | * and implicitly setup a monitor. We may want to break this. |
| 3475 | */ |
Paolo Bonzini | 4ad6f6c | 2019-02-13 14:18:13 +0100 | [diff] [blame] | 3476 | chr = qemu_chr_new_noreplay("gdb", device, true, NULL); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 3477 | if (!chr) |
| 3478 | return -1; |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3481 | if (!gdbserver_state.init) { |
| 3482 | init_gdbserver_state(); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3483 | |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 3484 | qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL); |
| 3485 | |
| 3486 | /* Initialize a monitor terminal for gdb */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 3487 | mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB, |
Paolo Bonzini | 4ad6f6c | 2019-02-13 14:18:13 +0100 | [diff] [blame] | 3488 | NULL, NULL, &error_abort); |
Kevin Wolf | 8e9119a | 2020-02-24 15:30:06 +0100 | [diff] [blame] | 3489 | monitor_init_hmp(mon_chr, false, &error_abort); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 3490 | } else { |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3491 | qemu_chr_fe_deinit(&gdbserver_state.chr, true); |
| 3492 | mon_chr = gdbserver_state.mon_chr; |
| 3493 | reset_gdbserver_state(); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 3494 | } |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 3495 | |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3496 | create_processes(&gdbserver_state); |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 3497 | |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 3498 | if (chr) { |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3499 | qemu_chr_fe_init(&gdbserver_state.chr, chr, &error_abort); |
| 3500 | qemu_chr_fe_set_handlers(&gdbserver_state.chr, gdb_chr_can_receive, |
| 3501 | gdb_chr_receive, gdb_chr_event, |
| 3502 | NULL, &gdbserver_state, NULL, true); |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 3503 | } |
Alex Bennée | 8d98c44 | 2020-03-16 17:21:33 +0000 | [diff] [blame] | 3504 | gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE; |
| 3505 | gdbserver_state.mon_chr = mon_chr; |
| 3506 | gdbserver_state.current_syscall_cb = NULL; |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 3507 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3508 | return 0; |
| 3509 | } |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 3510 | |
| 3511 | static void register_types(void) |
| 3512 | { |
| 3513 | type_register_static(&char_gdb_type_info); |
| 3514 | } |
| 3515 | |
| 3516 | type_init(register_types); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 3517 | #endif |