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