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