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