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 | * |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2005 Fabrice Bellard |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 18 | */ |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 19 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 20 | #include "qapi/error.h" |
Ziyue Yang | 508b4ec | 2017-01-18 16:02:41 +0800 | [diff] [blame] | 21 | #include "qemu/error-report.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 22 | #include "qemu/cutils.h" |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 23 | #include "trace-root.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 24 | #ifdef CONFIG_USER_ONLY |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 25 | #include "qemu.h" |
| 26 | #else |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 27 | #include "monitor/monitor.h" |
Marc-André Lureau | 8228e35 | 2017-01-26 17:19:46 +0400 | [diff] [blame] | 28 | #include "chardev/char.h" |
Marc-André Lureau | 4d43a60 | 2017-01-26 18:26:44 +0400 | [diff] [blame] | 29 | #include "chardev/char-fe.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 30 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 31 | #include "exec/gdbstub.h" |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 32 | #include "hw/cpu/cluster.h" |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 33 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 34 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 35 | #define MAX_PACKET_LENGTH 4096 |
| 36 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 37 | #include "qemu/sockets.h" |
Vincent Palatin | b394662 | 2017-01-10 11:59:55 +0100 | [diff] [blame] | 38 | #include "sysemu/hw_accel.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 39 | #include "sysemu/kvm.h" |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 40 | #include "exec/semihost.h" |
Paolo Bonzini | 63c9155 | 2016-03-15 13:18:37 +0100 | [diff] [blame] | 41 | #include "exec/exec-all.h" |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 42 | |
Jan Kiszka | a391938 | 2015-02-07 09:38:44 +0100 | [diff] [blame] | 43 | #ifdef CONFIG_USER_ONLY |
| 44 | #define GDB_ATTACHED "0" |
| 45 | #else |
| 46 | #define GDB_ATTACHED "1" |
| 47 | #endif |
| 48 | |
Andreas Färber | f3659ee | 2013-06-27 19:09:09 +0200 | [diff] [blame] | 49 | static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr, |
| 50 | uint8_t *buf, int len, bool is_write) |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 51 | { |
Andreas Färber | f3659ee | 2013-06-27 19:09:09 +0200 | [diff] [blame] | 52 | CPUClass *cc = CPU_GET_CLASS(cpu); |
| 53 | |
| 54 | if (cc->memory_rw_debug) { |
| 55 | return cc->memory_rw_debug(cpu, addr, buf, len, is_write); |
| 56 | } |
| 57 | return cpu_memory_rw_debug(cpu, addr, buf, len, is_write); |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 58 | } |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 59 | |
Alex Bennée | d2a6c85 | 2017-07-12 11:52:14 +0100 | [diff] [blame] | 60 | /* Return the GDB index for a given vCPU state. |
| 61 | * |
| 62 | * For user mode this is simply the thread id. In system mode GDB |
| 63 | * numbers CPUs from 1 as 0 is reserved as an "any cpu" index. |
| 64 | */ |
| 65 | static inline int cpu_gdb_index(CPUState *cpu) |
| 66 | { |
| 67 | #if defined(CONFIG_USER_ONLY) |
Alex Bennée | bd88c78 | 2017-07-12 11:52:15 +0100 | [diff] [blame] | 68 | TaskState *ts = (TaskState *) cpu->opaque; |
| 69 | return ts->ts_tid; |
Alex Bennée | d2a6c85 | 2017-07-12 11:52:14 +0100 | [diff] [blame] | 70 | #else |
| 71 | return cpu->cpu_index + 1; |
| 72 | #endif |
| 73 | } |
| 74 | |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 75 | enum { |
| 76 | GDB_SIGNAL_0 = 0, |
| 77 | GDB_SIGNAL_INT = 2, |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 78 | GDB_SIGNAL_QUIT = 3, |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 79 | GDB_SIGNAL_TRAP = 5, |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 80 | GDB_SIGNAL_ABRT = 6, |
| 81 | GDB_SIGNAL_ALRM = 14, |
| 82 | GDB_SIGNAL_IO = 23, |
| 83 | GDB_SIGNAL_XCPU = 24, |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 84 | GDB_SIGNAL_UNKNOWN = 143 |
| 85 | }; |
| 86 | |
| 87 | #ifdef CONFIG_USER_ONLY |
| 88 | |
| 89 | /* Map target signal numbers to GDB protocol signal numbers and vice |
| 90 | * versa. For user emulation's currently supported systems, we can |
| 91 | * assume most signals are defined. |
| 92 | */ |
| 93 | |
| 94 | static int gdb_signal_table[] = { |
| 95 | 0, |
| 96 | TARGET_SIGHUP, |
| 97 | TARGET_SIGINT, |
| 98 | TARGET_SIGQUIT, |
| 99 | TARGET_SIGILL, |
| 100 | TARGET_SIGTRAP, |
| 101 | TARGET_SIGABRT, |
| 102 | -1, /* SIGEMT */ |
| 103 | TARGET_SIGFPE, |
| 104 | TARGET_SIGKILL, |
| 105 | TARGET_SIGBUS, |
| 106 | TARGET_SIGSEGV, |
| 107 | TARGET_SIGSYS, |
| 108 | TARGET_SIGPIPE, |
| 109 | TARGET_SIGALRM, |
| 110 | TARGET_SIGTERM, |
| 111 | TARGET_SIGURG, |
| 112 | TARGET_SIGSTOP, |
| 113 | TARGET_SIGTSTP, |
| 114 | TARGET_SIGCONT, |
| 115 | TARGET_SIGCHLD, |
| 116 | TARGET_SIGTTIN, |
| 117 | TARGET_SIGTTOU, |
| 118 | TARGET_SIGIO, |
| 119 | TARGET_SIGXCPU, |
| 120 | TARGET_SIGXFSZ, |
| 121 | TARGET_SIGVTALRM, |
| 122 | TARGET_SIGPROF, |
| 123 | TARGET_SIGWINCH, |
| 124 | -1, /* SIGLOST */ |
| 125 | TARGET_SIGUSR1, |
| 126 | TARGET_SIGUSR2, |
blueswir1 | c72d5bf | 2009-01-15 17:27:45 +0000 | [diff] [blame] | 127 | #ifdef TARGET_SIGPWR |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 128 | TARGET_SIGPWR, |
blueswir1 | c72d5bf | 2009-01-15 17:27:45 +0000 | [diff] [blame] | 129 | #else |
| 130 | -1, |
| 131 | #endif |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 132 | -1, /* SIGPOLL */ |
| 133 | -1, |
| 134 | -1, |
| 135 | -1, |
| 136 | -1, |
| 137 | -1, |
| 138 | -1, |
| 139 | -1, |
| 140 | -1, |
| 141 | -1, |
| 142 | -1, |
| 143 | -1, |
blueswir1 | c72d5bf | 2009-01-15 17:27:45 +0000 | [diff] [blame] | 144 | #ifdef __SIGRTMIN |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 145 | __SIGRTMIN + 1, |
| 146 | __SIGRTMIN + 2, |
| 147 | __SIGRTMIN + 3, |
| 148 | __SIGRTMIN + 4, |
| 149 | __SIGRTMIN + 5, |
| 150 | __SIGRTMIN + 6, |
| 151 | __SIGRTMIN + 7, |
| 152 | __SIGRTMIN + 8, |
| 153 | __SIGRTMIN + 9, |
| 154 | __SIGRTMIN + 10, |
| 155 | __SIGRTMIN + 11, |
| 156 | __SIGRTMIN + 12, |
| 157 | __SIGRTMIN + 13, |
| 158 | __SIGRTMIN + 14, |
| 159 | __SIGRTMIN + 15, |
| 160 | __SIGRTMIN + 16, |
| 161 | __SIGRTMIN + 17, |
| 162 | __SIGRTMIN + 18, |
| 163 | __SIGRTMIN + 19, |
| 164 | __SIGRTMIN + 20, |
| 165 | __SIGRTMIN + 21, |
| 166 | __SIGRTMIN + 22, |
| 167 | __SIGRTMIN + 23, |
| 168 | __SIGRTMIN + 24, |
| 169 | __SIGRTMIN + 25, |
| 170 | __SIGRTMIN + 26, |
| 171 | __SIGRTMIN + 27, |
| 172 | __SIGRTMIN + 28, |
| 173 | __SIGRTMIN + 29, |
| 174 | __SIGRTMIN + 30, |
| 175 | __SIGRTMIN + 31, |
| 176 | -1, /* SIGCANCEL */ |
| 177 | __SIGRTMIN, |
| 178 | __SIGRTMIN + 32, |
| 179 | __SIGRTMIN + 33, |
| 180 | __SIGRTMIN + 34, |
| 181 | __SIGRTMIN + 35, |
| 182 | __SIGRTMIN + 36, |
| 183 | __SIGRTMIN + 37, |
| 184 | __SIGRTMIN + 38, |
| 185 | __SIGRTMIN + 39, |
| 186 | __SIGRTMIN + 40, |
| 187 | __SIGRTMIN + 41, |
| 188 | __SIGRTMIN + 42, |
| 189 | __SIGRTMIN + 43, |
| 190 | __SIGRTMIN + 44, |
| 191 | __SIGRTMIN + 45, |
| 192 | __SIGRTMIN + 46, |
| 193 | __SIGRTMIN + 47, |
| 194 | __SIGRTMIN + 48, |
| 195 | __SIGRTMIN + 49, |
| 196 | __SIGRTMIN + 50, |
| 197 | __SIGRTMIN + 51, |
| 198 | __SIGRTMIN + 52, |
| 199 | __SIGRTMIN + 53, |
| 200 | __SIGRTMIN + 54, |
| 201 | __SIGRTMIN + 55, |
| 202 | __SIGRTMIN + 56, |
| 203 | __SIGRTMIN + 57, |
| 204 | __SIGRTMIN + 58, |
| 205 | __SIGRTMIN + 59, |
| 206 | __SIGRTMIN + 60, |
| 207 | __SIGRTMIN + 61, |
| 208 | __SIGRTMIN + 62, |
| 209 | __SIGRTMIN + 63, |
| 210 | __SIGRTMIN + 64, |
| 211 | __SIGRTMIN + 65, |
| 212 | __SIGRTMIN + 66, |
| 213 | __SIGRTMIN + 67, |
| 214 | __SIGRTMIN + 68, |
| 215 | __SIGRTMIN + 69, |
| 216 | __SIGRTMIN + 70, |
| 217 | __SIGRTMIN + 71, |
| 218 | __SIGRTMIN + 72, |
| 219 | __SIGRTMIN + 73, |
| 220 | __SIGRTMIN + 74, |
| 221 | __SIGRTMIN + 75, |
| 222 | __SIGRTMIN + 76, |
| 223 | __SIGRTMIN + 77, |
| 224 | __SIGRTMIN + 78, |
| 225 | __SIGRTMIN + 79, |
| 226 | __SIGRTMIN + 80, |
| 227 | __SIGRTMIN + 81, |
| 228 | __SIGRTMIN + 82, |
| 229 | __SIGRTMIN + 83, |
| 230 | __SIGRTMIN + 84, |
| 231 | __SIGRTMIN + 85, |
| 232 | __SIGRTMIN + 86, |
| 233 | __SIGRTMIN + 87, |
| 234 | __SIGRTMIN + 88, |
| 235 | __SIGRTMIN + 89, |
| 236 | __SIGRTMIN + 90, |
| 237 | __SIGRTMIN + 91, |
| 238 | __SIGRTMIN + 92, |
| 239 | __SIGRTMIN + 93, |
| 240 | __SIGRTMIN + 94, |
| 241 | __SIGRTMIN + 95, |
| 242 | -1, /* SIGINFO */ |
| 243 | -1, /* UNKNOWN */ |
| 244 | -1, /* DEFAULT */ |
| 245 | -1, |
| 246 | -1, |
| 247 | -1, |
| 248 | -1, |
| 249 | -1, |
| 250 | -1 |
blueswir1 | c72d5bf | 2009-01-15 17:27:45 +0000 | [diff] [blame] | 251 | #endif |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 252 | }; |
bellard | 8f447cc | 2006-06-14 15:21:14 +0000 | [diff] [blame] | 253 | #else |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 254 | /* In system mode we only need SIGINT and SIGTRAP; other signals |
| 255 | are not yet supported. */ |
| 256 | |
| 257 | enum { |
| 258 | TARGET_SIGINT = 2, |
| 259 | TARGET_SIGTRAP = 5 |
| 260 | }; |
| 261 | |
| 262 | static int gdb_signal_table[] = { |
| 263 | -1, |
| 264 | -1, |
| 265 | TARGET_SIGINT, |
| 266 | -1, |
| 267 | -1, |
| 268 | TARGET_SIGTRAP |
| 269 | }; |
bellard | 8f447cc | 2006-06-14 15:21:14 +0000 | [diff] [blame] | 270 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 271 | |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 272 | #ifdef CONFIG_USER_ONLY |
| 273 | static int target_signal_to_gdb (int sig) |
| 274 | { |
| 275 | int i; |
| 276 | for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++) |
| 277 | if (gdb_signal_table[i] == sig) |
| 278 | return i; |
| 279 | return GDB_SIGNAL_UNKNOWN; |
| 280 | } |
| 281 | #endif |
| 282 | |
| 283 | static int gdb_signal_to_target (int sig) |
| 284 | { |
| 285 | if (sig < ARRAY_SIZE (gdb_signal_table)) |
| 286 | return gdb_signal_table[sig]; |
| 287 | else |
| 288 | return -1; |
| 289 | } |
| 290 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 291 | typedef struct GDBRegisterState { |
| 292 | int base_reg; |
| 293 | int num_regs; |
| 294 | gdb_reg_cb get_reg; |
| 295 | gdb_reg_cb set_reg; |
| 296 | const char *xml; |
| 297 | struct GDBRegisterState *next; |
| 298 | } GDBRegisterState; |
| 299 | |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 300 | typedef struct GDBProcess { |
| 301 | uint32_t pid; |
| 302 | bool attached; |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 303 | |
| 304 | char target_xml[1024]; |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 305 | } GDBProcess; |
| 306 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 307 | enum RSState { |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 308 | RS_INACTIVE, |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 309 | RS_IDLE, |
| 310 | RS_GETLINE, |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 311 | RS_GETLINE_ESC, |
| 312 | RS_GETLINE_RLE, |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 313 | RS_CHKSUM1, |
| 314 | RS_CHKSUM2, |
| 315 | }; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 316 | typedef struct GDBState { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 317 | CPUState *c_cpu; /* current CPU for step/continue ops */ |
| 318 | CPUState *g_cpu; /* current CPU for other ops */ |
Andreas Färber | 52f3462 | 2013-06-27 13:44:40 +0200 | [diff] [blame] | 319 | CPUState *query_cpu; /* for q{f|s}ThreadInfo */ |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 320 | enum RSState state; /* parsing state */ |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 321 | char line_buf[MAX_PACKET_LENGTH]; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 322 | int line_buf_index; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 323 | int line_sum; /* running checksum */ |
| 324 | int line_csum; /* checksum at the end of the packet */ |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 325 | uint8_t last_packet[MAX_PACKET_LENGTH + 4]; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 326 | int last_packet_len; |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 327 | int signal; |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 328 | #ifdef CONFIG_USER_ONLY |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 329 | int fd; |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 330 | int running_state; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 331 | #else |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 332 | CharBackend chr; |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 333 | Chardev *mon_chr; |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 334 | #endif |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 335 | bool multiprocess; |
| 336 | GDBProcess *processes; |
| 337 | int process_num; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 338 | char syscall_buf[256]; |
| 339 | gdb_syscall_complete_cb current_syscall_cb; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 340 | } GDBState; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 341 | |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 342 | /* By default use no IRQs and no timers while single stepping so as to |
| 343 | * make single stepping like an ICE HW step. |
| 344 | */ |
| 345 | static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER; |
| 346 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 347 | static GDBState *gdbserver_state; |
| 348 | |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 349 | bool gdb_has_xml; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 350 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 351 | #ifdef CONFIG_USER_ONLY |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 352 | /* XXX: This is not thread safe. Do we care? */ |
| 353 | static int gdbserver_fd = -1; |
| 354 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 355 | static int get_char(GDBState *s) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 356 | { |
| 357 | uint8_t ch; |
| 358 | int ret; |
| 359 | |
| 360 | for(;;) { |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 361 | ret = qemu_recv(s->fd, &ch, 1, 0); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 362 | if (ret < 0) { |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 363 | if (errno == ECONNRESET) |
| 364 | s->fd = -1; |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 365 | if (errno != EINTR) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 366 | return -1; |
| 367 | } else if (ret == 0) { |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 368 | close(s->fd); |
| 369 | s->fd = -1; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 370 | return -1; |
| 371 | } else { |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | return ch; |
| 376 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 377 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 378 | |
blueswir1 | 654efcf | 2009-04-18 07:29:59 +0000 | [diff] [blame] | 379 | static enum { |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 380 | GDB_SYS_UNKNOWN, |
| 381 | GDB_SYS_ENABLED, |
| 382 | GDB_SYS_DISABLED, |
| 383 | } gdb_syscall_mode; |
| 384 | |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 385 | /* Decide if either remote gdb syscalls or native file IO should be used. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 386 | int use_gdb_syscalls(void) |
| 387 | { |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 388 | SemihostingTarget target = semihosting_get_target(); |
| 389 | if (target == SEMIHOSTING_TARGET_NATIVE) { |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 390 | /* -semihosting-config target=native */ |
| 391 | return false; |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 392 | } else if (target == SEMIHOSTING_TARGET_GDB) { |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 393 | /* -semihosting-config target=gdb */ |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | /* -semihosting-config target=auto */ |
| 398 | /* On the first call check if gdb is connected and remember. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 399 | if (gdb_syscall_mode == GDB_SYS_UNKNOWN) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 400 | gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED |
| 401 | : GDB_SYS_DISABLED); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 402 | } |
| 403 | return gdb_syscall_mode == GDB_SYS_ENABLED; |
| 404 | } |
| 405 | |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 406 | /* Resume execution. */ |
| 407 | static inline void gdb_continue(GDBState *s) |
| 408 | { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 409 | |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 410 | #ifdef CONFIG_USER_ONLY |
| 411 | s->running_state = 1; |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 412 | trace_gdbstub_op_continue(); |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 413 | #else |
Paolo Bonzini | 26ac7a3 | 2013-06-03 17:06:54 +0200 | [diff] [blame] | 414 | if (!runstate_needs_reset()) { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 415 | trace_gdbstub_op_continue(); |
Paolo Bonzini | 87f25c1 | 2013-05-30 13:20:40 +0200 | [diff] [blame] | 416 | vm_start(); |
| 417 | } |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 418 | #endif |
| 419 | } |
| 420 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 421 | /* |
| 422 | * Resume execution, per CPU actions. For user-mode emulation it's |
| 423 | * equivalent to gdb_continue. |
| 424 | */ |
| 425 | static int gdb_continue_partial(GDBState *s, char *newstates) |
| 426 | { |
| 427 | CPUState *cpu; |
| 428 | int res = 0; |
| 429 | #ifdef CONFIG_USER_ONLY |
| 430 | /* |
| 431 | * This is not exactly accurate, but it's an improvement compared to the |
| 432 | * previous situation, where only one CPU would be single-stepped. |
| 433 | */ |
| 434 | CPU_FOREACH(cpu) { |
| 435 | if (newstates[cpu->cpu_index] == 's') { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 436 | trace_gdbstub_op_stepping(cpu->cpu_index); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 437 | cpu_single_step(cpu, sstep_flags); |
| 438 | } |
| 439 | } |
| 440 | s->running_state = 1; |
| 441 | #else |
| 442 | int flag = 0; |
| 443 | |
| 444 | if (!runstate_needs_reset()) { |
| 445 | if (vm_prepare_start()) { |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | CPU_FOREACH(cpu) { |
| 450 | switch (newstates[cpu->cpu_index]) { |
| 451 | case 0: |
| 452 | case 1: |
| 453 | break; /* nothing to do here */ |
| 454 | case 's': |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 455 | trace_gdbstub_op_stepping(cpu->cpu_index); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 456 | cpu_single_step(cpu, sstep_flags); |
| 457 | cpu_resume(cpu); |
| 458 | flag = 1; |
| 459 | break; |
| 460 | case 'c': |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 461 | trace_gdbstub_op_continue_cpu(cpu->cpu_index); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 462 | cpu_resume(cpu); |
| 463 | flag = 1; |
| 464 | break; |
| 465 | default: |
| 466 | res = -1; |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | if (flag) { |
| 472 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); |
| 473 | } |
| 474 | #endif |
| 475 | return res; |
| 476 | } |
| 477 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 478 | static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 479 | { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 480 | #ifdef CONFIG_USER_ONLY |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 481 | int ret; |
| 482 | |
| 483 | while (len > 0) { |
bellard | 8f447cc | 2006-06-14 15:21:14 +0000 | [diff] [blame] | 484 | ret = send(s->fd, buf, len, 0); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 485 | if (ret < 0) { |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 486 | if (errno != EINTR) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 487 | return; |
| 488 | } else { |
| 489 | buf += ret; |
| 490 | len -= ret; |
| 491 | } |
| 492 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 493 | #else |
Daniel P. Berrange | 6ab3fc3 | 2016-09-06 14:56:04 +0100 | [diff] [blame] | 494 | /* XXX this blocks entire thread. Rewrite to use |
| 495 | * qemu_chr_fe_write and background I/O callbacks */ |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 496 | qemu_chr_fe_write_all(&s->chr, buf, len); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 497 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static inline int fromhex(int v) |
| 501 | { |
| 502 | if (v >= '0' && v <= '9') |
| 503 | return v - '0'; |
| 504 | else if (v >= 'A' && v <= 'F') |
| 505 | return v - 'A' + 10; |
| 506 | else if (v >= 'a' && v <= 'f') |
| 507 | return v - 'a' + 10; |
| 508 | else |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static inline int tohex(int v) |
| 513 | { |
| 514 | if (v < 10) |
| 515 | return v + '0'; |
| 516 | else |
| 517 | return v - 10 + 'a'; |
| 518 | } |
| 519 | |
Philippe Mathieu-Daudé | 9005774 | 2018-04-08 11:59:33 -0300 | [diff] [blame] | 520 | /* writes 2*len+1 bytes in buf */ |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 521 | static void memtohex(char *buf, const uint8_t *mem, int len) |
| 522 | { |
| 523 | int i, c; |
| 524 | char *q; |
| 525 | q = buf; |
| 526 | for(i = 0; i < len; i++) { |
| 527 | c = mem[i]; |
| 528 | *q++ = tohex(c >> 4); |
| 529 | *q++ = tohex(c & 0xf); |
| 530 | } |
| 531 | *q = '\0'; |
| 532 | } |
| 533 | |
| 534 | static void hextomem(uint8_t *mem, const char *buf, int len) |
| 535 | { |
| 536 | int i; |
| 537 | |
| 538 | for(i = 0; i < len; i++) { |
| 539 | mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); |
| 540 | buf += 2; |
| 541 | } |
| 542 | } |
| 543 | |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 544 | static void hexdump(const char *buf, int len, |
| 545 | void (*trace_fn)(size_t ofs, char const *text)) |
| 546 | { |
| 547 | char line_buffer[3 * 16 + 4 + 16 + 1]; |
| 548 | |
| 549 | size_t i; |
| 550 | for (i = 0; i < len || (i & 0xF); ++i) { |
| 551 | size_t byte_ofs = i & 15; |
| 552 | |
| 553 | if (byte_ofs == 0) { |
| 554 | memset(line_buffer, ' ', 3 * 16 + 4 + 16); |
| 555 | line_buffer[3 * 16 + 4 + 16] = 0; |
| 556 | } |
| 557 | |
| 558 | size_t col_group = (i >> 2) & 3; |
| 559 | size_t hex_col = byte_ofs * 3 + col_group; |
| 560 | size_t txt_col = 3 * 16 + 4 + byte_ofs; |
| 561 | |
| 562 | if (i < len) { |
| 563 | char value = buf[i]; |
| 564 | |
| 565 | line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF); |
| 566 | line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF); |
| 567 | line_buffer[txt_col + 0] = (value >= ' ' && value < 127) |
| 568 | ? value |
| 569 | : '.'; |
| 570 | } |
| 571 | |
| 572 | if (byte_ofs == 0xF) |
| 573 | trace_fn(i & -16, line_buffer); |
| 574 | } |
| 575 | } |
| 576 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 577 | /* return -1 if error, 0 if OK */ |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 578 | static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 579 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 580 | int csum, i; |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 581 | uint8_t *p; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 582 | |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 583 | if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) { |
| 584 | hexdump(buf, len, trace_gdbstub_io_binaryreply); |
| 585 | } |
| 586 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 587 | for(;;) { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 588 | p = s->last_packet; |
| 589 | *(p++) = '$'; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 590 | memcpy(p, buf, len); |
| 591 | p += len; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 592 | csum = 0; |
| 593 | for(i = 0; i < len; i++) { |
| 594 | csum += buf[i]; |
| 595 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 596 | *(p++) = '#'; |
| 597 | *(p++) = tohex((csum >> 4) & 0xf); |
| 598 | *(p++) = tohex((csum) & 0xf); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 599 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 600 | s->last_packet_len = p - s->last_packet; |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 601 | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 602 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 603 | #ifdef CONFIG_USER_ONLY |
| 604 | i = get_char(s); |
| 605 | if (i < 0) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 606 | return -1; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 607 | if (i == '+') |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 608 | break; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 609 | #else |
| 610 | break; |
| 611 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 612 | } |
| 613 | return 0; |
| 614 | } |
| 615 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 616 | /* return -1 if error, 0 if OK */ |
| 617 | static int put_packet(GDBState *s, const char *buf) |
| 618 | { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 619 | trace_gdbstub_io_reply(buf); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 620 | |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 621 | return put_packet_binary(s, buf, strlen(buf), false); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 622 | } |
| 623 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 624 | /* Encode data using the encoding for 'x' packets. */ |
| 625 | static int memtox(char *buf, const char *mem, int len) |
| 626 | { |
| 627 | char *p = buf; |
| 628 | char c; |
| 629 | |
| 630 | while (len--) { |
| 631 | c = *(mem++); |
| 632 | switch (c) { |
| 633 | case '#': case '$': case '*': case '}': |
| 634 | *(p++) = '}'; |
| 635 | *(p++) = c ^ 0x20; |
| 636 | break; |
| 637 | default: |
| 638 | *(p++) = c; |
| 639 | break; |
| 640 | } |
| 641 | } |
| 642 | return p - buf; |
| 643 | } |
| 644 | |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 645 | static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu) |
| 646 | { |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 647 | /* TODO: In user mode, we should use the task state PID */ |
Peter Maydell | 46f5abc | 2019-01-29 11:46:06 +0000 | [diff] [blame] | 648 | if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) { |
| 649 | /* Return the default process' PID */ |
| 650 | return s->processes[s->process_num - 1].pid; |
| 651 | } |
| 652 | return cpu->cluster_index + 1; |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 655 | static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid) |
| 656 | { |
| 657 | int i; |
| 658 | |
| 659 | if (!pid) { |
| 660 | /* 0 means any process, we take the first one */ |
| 661 | return &s->processes[0]; |
| 662 | } |
| 663 | |
| 664 | for (i = 0; i < s->process_num; i++) { |
| 665 | if (s->processes[i].pid == pid) { |
| 666 | return &s->processes[i]; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | return NULL; |
| 671 | } |
| 672 | |
| 673 | static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu) |
| 674 | { |
| 675 | return gdb_get_process(s, gdb_get_cpu_pid(s, cpu)); |
| 676 | } |
| 677 | |
| 678 | static CPUState *find_cpu(uint32_t thread_id) |
| 679 | { |
| 680 | CPUState *cpu; |
| 681 | |
| 682 | CPU_FOREACH(cpu) { |
| 683 | if (cpu_gdb_index(cpu) == thread_id) { |
| 684 | return cpu; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | return NULL; |
| 689 | } |
| 690 | |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 691 | static CPUState *get_first_cpu_in_process(const GDBState *s, |
| 692 | GDBProcess *process) |
| 693 | { |
| 694 | CPUState *cpu; |
| 695 | |
| 696 | CPU_FOREACH(cpu) { |
| 697 | if (gdb_get_cpu_pid(s, cpu) == process->pid) { |
| 698 | return cpu; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return NULL; |
| 703 | } |
| 704 | |
| 705 | static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu) |
| 706 | { |
| 707 | uint32_t pid = gdb_get_cpu_pid(s, cpu); |
| 708 | cpu = CPU_NEXT(cpu); |
| 709 | |
| 710 | while (cpu) { |
| 711 | if (gdb_get_cpu_pid(s, cpu) == pid) { |
| 712 | break; |
| 713 | } |
| 714 | |
| 715 | cpu = CPU_NEXT(cpu); |
| 716 | } |
| 717 | |
| 718 | return cpu; |
| 719 | } |
| 720 | |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 721 | /* Return the cpu following @cpu, while ignoring unattached processes. */ |
| 722 | static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu) |
| 723 | { |
| 724 | cpu = CPU_NEXT(cpu); |
| 725 | |
| 726 | while (cpu) { |
| 727 | if (gdb_get_cpu_process(s, cpu)->attached) { |
| 728 | break; |
| 729 | } |
| 730 | |
| 731 | cpu = CPU_NEXT(cpu); |
| 732 | } |
| 733 | |
| 734 | return cpu; |
| 735 | } |
| 736 | |
| 737 | /* Return the first attached cpu */ |
| 738 | static CPUState *gdb_first_attached_cpu(const GDBState *s) |
| 739 | { |
| 740 | CPUState *cpu = first_cpu; |
| 741 | GDBProcess *process = gdb_get_cpu_process(s, cpu); |
| 742 | |
| 743 | if (!process->attached) { |
| 744 | return gdb_next_attached_cpu(s, cpu); |
| 745 | } |
| 746 | |
| 747 | return cpu; |
| 748 | } |
| 749 | |
Luc Michel | ab65eed | 2019-01-29 11:46:03 +0000 | [diff] [blame] | 750 | static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid) |
| 751 | { |
| 752 | GDBProcess *process; |
| 753 | CPUState *cpu; |
| 754 | |
| 755 | if (!pid && !tid) { |
| 756 | /* 0 means any process/thread, we take the first attached one */ |
| 757 | return gdb_first_attached_cpu(s); |
| 758 | } else if (pid && !tid) { |
| 759 | /* any thread in a specific process */ |
| 760 | process = gdb_get_process(s, pid); |
| 761 | |
| 762 | if (process == NULL) { |
| 763 | return NULL; |
| 764 | } |
| 765 | |
| 766 | if (!process->attached) { |
| 767 | return NULL; |
| 768 | } |
| 769 | |
| 770 | return get_first_cpu_in_process(s, process); |
| 771 | } else { |
| 772 | /* a specific thread */ |
| 773 | cpu = find_cpu(tid); |
| 774 | |
| 775 | if (cpu == NULL) { |
| 776 | return NULL; |
| 777 | } |
| 778 | |
| 779 | process = gdb_get_cpu_process(s, cpu); |
| 780 | |
| 781 | if (pid && process->pid != pid) { |
| 782 | return NULL; |
| 783 | } |
| 784 | |
| 785 | if (!process->attached) { |
| 786 | return NULL; |
| 787 | } |
| 788 | |
| 789 | return cpu; |
| 790 | } |
| 791 | } |
| 792 | |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 793 | static const char *get_feature_xml(const GDBState *s, const char *p, |
| 794 | const char **newp, GDBProcess *process) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 795 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 796 | size_t len; |
| 797 | int i; |
| 798 | const char *name; |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 799 | CPUState *cpu = get_first_cpu_in_process(s, process); |
| 800 | CPUClass *cc = CPU_GET_CLASS(cpu); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 801 | |
| 802 | len = 0; |
| 803 | while (p[len] && p[len] != ':') |
| 804 | len++; |
| 805 | *newp = p + len; |
| 806 | |
| 807 | name = NULL; |
| 808 | if (strncmp(p, "target.xml", len) == 0) { |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 809 | char *buf = process->target_xml; |
| 810 | const size_t buf_sz = sizeof(process->target_xml); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 811 | |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 812 | /* Generate the XML description for this CPU. */ |
| 813 | if (!buf[0]) { |
| 814 | GDBRegisterState *r; |
| 815 | |
| 816 | pstrcat(buf, buf_sz, |
David Hildenbrand | b3820e6 | 2015-12-03 13:14:41 +0100 | [diff] [blame] | 817 | "<?xml version=\"1.0\"?>" |
| 818 | "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">" |
| 819 | "<target>"); |
| 820 | if (cc->gdb_arch_name) { |
| 821 | gchar *arch = cc->gdb_arch_name(cpu); |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 822 | pstrcat(buf, buf_sz, "<architecture>"); |
| 823 | pstrcat(buf, buf_sz, arch); |
| 824 | pstrcat(buf, buf_sz, "</architecture>"); |
David Hildenbrand | b3820e6 | 2015-12-03 13:14:41 +0100 | [diff] [blame] | 825 | g_free(arch); |
| 826 | } |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 827 | pstrcat(buf, buf_sz, "<xi:include href=\""); |
| 828 | pstrcat(buf, buf_sz, cc->gdb_core_xml_file); |
| 829 | pstrcat(buf, buf_sz, "\"/>"); |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 830 | for (r = cpu->gdb_regs; r; r = r->next) { |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 831 | pstrcat(buf, buf_sz, "<xi:include href=\""); |
| 832 | pstrcat(buf, buf_sz, r->xml); |
| 833 | pstrcat(buf, buf_sz, "\"/>"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 834 | } |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 835 | pstrcat(buf, buf_sz, "</target>"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 836 | } |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 837 | return buf; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 838 | } |
Abdallah Bouassida | 200bf5b | 2018-05-18 17:48:07 +0100 | [diff] [blame] | 839 | if (cc->gdb_get_dynamic_xml) { |
Abdallah Bouassida | 200bf5b | 2018-05-18 17:48:07 +0100 | [diff] [blame] | 840 | char *xmlname = g_strndup(p, len); |
| 841 | const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname); |
| 842 | |
| 843 | g_free(xmlname); |
| 844 | if (xml) { |
| 845 | return xml; |
| 846 | } |
| 847 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 848 | for (i = 0; ; i++) { |
| 849 | name = xml_builtin[i][0]; |
| 850 | if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len)) |
| 851 | break; |
| 852 | } |
| 853 | return name ? xml_builtin[i][1] : NULL; |
| 854 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 855 | |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 856 | static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 857 | { |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 858 | CPUClass *cc = CPU_GET_CLASS(cpu); |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 859 | CPUArchState *env = cpu->env_ptr; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 860 | GDBRegisterState *r; |
| 861 | |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 862 | if (reg < cc->gdb_num_core_regs) { |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 863 | return cc->gdb_read_register(cpu, mem_buf, reg); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 864 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 865 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 866 | for (r = cpu->gdb_regs; r; r = r->next) { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 867 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
| 868 | return r->get_reg(env, mem_buf, reg - r->base_reg); |
| 869 | } |
| 870 | } |
| 871 | return 0; |
| 872 | } |
| 873 | |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 874 | static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 875 | { |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 876 | CPUClass *cc = CPU_GET_CLASS(cpu); |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 877 | CPUArchState *env = cpu->env_ptr; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 878 | GDBRegisterState *r; |
| 879 | |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 880 | if (reg < cc->gdb_num_core_regs) { |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 881 | return cc->gdb_write_register(cpu, mem_buf, reg); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 882 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 883 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 884 | for (r = cpu->gdb_regs; r; r = r->next) { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 885 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
| 886 | return r->set_reg(env, mem_buf, reg - r->base_reg); |
| 887 | } |
| 888 | } |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | /* Register a supplemental set of CPU registers. If g_pos is nonzero it |
| 893 | specifies the first register number and these registers are included in |
| 894 | a standard "g" packet. Direction is relative to gdb, i.e. get_reg is |
| 895 | gdb reading a CPU register, and set_reg is gdb modifying a CPU register. |
| 896 | */ |
| 897 | |
Andreas Färber | 22169d4 | 2013-06-28 21:27:39 +0200 | [diff] [blame] | 898 | void gdb_register_coprocessor(CPUState *cpu, |
| 899 | gdb_reg_cb get_reg, gdb_reg_cb set_reg, |
| 900 | int num_regs, const char *xml, int g_pos) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 901 | { |
| 902 | GDBRegisterState *s; |
| 903 | GDBRegisterState **p; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 904 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 905 | p = &cpu->gdb_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 906 | while (*p) { |
| 907 | /* Check for duplicates. */ |
| 908 | if (strcmp((*p)->xml, xml) == 0) |
| 909 | return; |
| 910 | p = &(*p)->next; |
| 911 | } |
Stefan Weil | 9643c25 | 2011-10-18 22:25:38 +0200 | [diff] [blame] | 912 | |
| 913 | s = g_new0(GDBRegisterState, 1); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 914 | s->base_reg = cpu->gdb_num_regs; |
Stefan Weil | 9643c25 | 2011-10-18 22:25:38 +0200 | [diff] [blame] | 915 | s->num_regs = num_regs; |
| 916 | s->get_reg = get_reg; |
| 917 | s->set_reg = set_reg; |
| 918 | s->xml = xml; |
| 919 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 920 | /* Add to end of list. */ |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 921 | cpu->gdb_num_regs += num_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 922 | *p = s; |
| 923 | if (g_pos) { |
| 924 | if (g_pos != s->base_reg) { |
Ziyue Yang | 7ae6c57 | 2017-01-18 16:03:29 +0800 | [diff] [blame] | 925 | error_report("Error: Bad gdb register numbering for '%s', " |
| 926 | "expected %d got %d", xml, g_pos, s->base_reg); |
Andreas Färber | 35143f0 | 2013-08-12 18:09:47 +0200 | [diff] [blame] | 927 | } else { |
| 928 | cpu->gdb_num_g_regs = cpu->gdb_num_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 933 | #ifndef CONFIG_USER_ONLY |
Peter Maydell | 2472b6c | 2014-09-12 19:04:17 +0100 | [diff] [blame] | 934 | /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */ |
| 935 | static inline int xlat_gdb_type(CPUState *cpu, int gdbtype) |
| 936 | { |
| 937 | static const int xlat[] = { |
| 938 | [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE, |
| 939 | [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ, |
| 940 | [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS, |
| 941 | }; |
| 942 | |
| 943 | CPUClass *cc = CPU_GET_CLASS(cpu); |
| 944 | int cputype = xlat[gdbtype]; |
| 945 | |
| 946 | if (cc->gdb_stop_before_watchpoint) { |
| 947 | cputype |= BP_STOP_BEFORE_ACCESS; |
| 948 | } |
| 949 | return cputype; |
| 950 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 951 | #endif |
| 952 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 953 | static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type) |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 954 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 955 | CPUState *cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 956 | int err = 0; |
| 957 | |
Andreas Färber | 6227881 | 2013-06-27 17:12:06 +0200 | [diff] [blame] | 958 | if (kvm_enabled()) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 959 | return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type); |
Andreas Färber | 6227881 | 2013-06-27 17:12:06 +0200 | [diff] [blame] | 960 | } |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 961 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 962 | switch (type) { |
| 963 | case GDB_BREAKPOINT_SW: |
| 964 | case GDB_BREAKPOINT_HW: |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 965 | CPU_FOREACH(cpu) { |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 966 | err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL); |
| 967 | if (err) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 968 | break; |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 969 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 970 | } |
| 971 | return err; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 972 | #ifndef CONFIG_USER_ONLY |
| 973 | case GDB_WATCHPOINT_WRITE: |
| 974 | case GDB_WATCHPOINT_READ: |
| 975 | case GDB_WATCHPOINT_ACCESS: |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 976 | CPU_FOREACH(cpu) { |
Peter Maydell | 2472b6c | 2014-09-12 19:04:17 +0100 | [diff] [blame] | 977 | err = cpu_watchpoint_insert(cpu, addr, len, |
| 978 | xlat_gdb_type(cpu, type), NULL); |
| 979 | if (err) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 980 | break; |
Peter Maydell | 2472b6c | 2014-09-12 19:04:17 +0100 | [diff] [blame] | 981 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 982 | } |
| 983 | return err; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 984 | #endif |
| 985 | default: |
| 986 | return -ENOSYS; |
| 987 | } |
| 988 | } |
| 989 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 990 | static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type) |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 991 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 992 | CPUState *cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 993 | int err = 0; |
| 994 | |
Andreas Färber | 6227881 | 2013-06-27 17:12:06 +0200 | [diff] [blame] | 995 | if (kvm_enabled()) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 996 | return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type); |
Andreas Färber | 6227881 | 2013-06-27 17:12:06 +0200 | [diff] [blame] | 997 | } |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 998 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 999 | switch (type) { |
| 1000 | case GDB_BREAKPOINT_SW: |
| 1001 | case GDB_BREAKPOINT_HW: |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1002 | CPU_FOREACH(cpu) { |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 1003 | err = cpu_breakpoint_remove(cpu, addr, BP_GDB); |
| 1004 | if (err) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1005 | break; |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 1006 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1007 | } |
| 1008 | return err; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1009 | #ifndef CONFIG_USER_ONLY |
| 1010 | case GDB_WATCHPOINT_WRITE: |
| 1011 | case GDB_WATCHPOINT_READ: |
| 1012 | case GDB_WATCHPOINT_ACCESS: |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1013 | CPU_FOREACH(cpu) { |
Peter Maydell | 2472b6c | 2014-09-12 19:04:17 +0100 | [diff] [blame] | 1014 | err = cpu_watchpoint_remove(cpu, addr, len, |
| 1015 | xlat_gdb_type(cpu, type)); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1016 | if (err) |
| 1017 | break; |
| 1018 | } |
| 1019 | return err; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1020 | #endif |
| 1021 | default: |
| 1022 | return -ENOSYS; |
| 1023 | } |
| 1024 | } |
| 1025 | |
Luc Michel | 546f3c6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1026 | static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu) |
| 1027 | { |
| 1028 | cpu_breakpoint_remove_all(cpu, BP_GDB); |
| 1029 | #ifndef CONFIG_USER_ONLY |
| 1030 | cpu_watchpoint_remove_all(cpu, BP_GDB); |
| 1031 | #endif |
| 1032 | } |
| 1033 | |
| 1034 | static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p) |
| 1035 | { |
| 1036 | CPUState *cpu = get_first_cpu_in_process(s, p); |
| 1037 | |
| 1038 | while (cpu) { |
| 1039 | gdb_cpu_breakpoint_remove_all(cpu); |
| 1040 | cpu = gdb_next_cpu_in_process(s, cpu); |
| 1041 | } |
| 1042 | } |
| 1043 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1044 | static void gdb_breakpoint_remove_all(void) |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1045 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1046 | CPUState *cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1047 | |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 1048 | if (kvm_enabled()) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1049 | kvm_remove_all_breakpoints(gdbserver_state->c_cpu); |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 1050 | return; |
| 1051 | } |
| 1052 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1053 | CPU_FOREACH(cpu) { |
Luc Michel | 546f3c6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1054 | gdb_cpu_breakpoint_remove_all(cpu); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1055 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 1058 | static void gdb_set_cpu_pc(GDBState *s, target_ulong pc) |
| 1059 | { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1060 | CPUState *cpu = s->c_cpu; |
Andreas Färber | f45748f | 2013-06-21 19:09:18 +0200 | [diff] [blame] | 1061 | |
| 1062 | cpu_synchronize_state(cpu); |
Peter Crosthwaite | 4a2b24e | 2015-06-23 20:19:21 -0700 | [diff] [blame] | 1063 | cpu_set_pc(cpu, pc); |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1066 | static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu, |
| 1067 | char *buf, size_t buf_size) |
| 1068 | { |
| 1069 | if (s->multiprocess) { |
| 1070 | snprintf(buf, buf_size, "p%02x.%02x", |
| 1071 | gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu)); |
| 1072 | } else { |
| 1073 | snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu)); |
| 1074 | } |
| 1075 | |
| 1076 | return buf; |
| 1077 | } |
| 1078 | |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1079 | typedef enum GDBThreadIdKind { |
| 1080 | GDB_ONE_THREAD = 0, |
| 1081 | GDB_ALL_THREADS, /* One process, all threads */ |
| 1082 | GDB_ALL_PROCESSES, |
| 1083 | GDB_READ_THREAD_ERR |
| 1084 | } GDBThreadIdKind; |
| 1085 | |
| 1086 | static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf, |
| 1087 | uint32_t *pid, uint32_t *tid) |
| 1088 | { |
| 1089 | unsigned long p, t; |
| 1090 | int ret; |
| 1091 | |
| 1092 | if (*buf == 'p') { |
| 1093 | buf++; |
| 1094 | ret = qemu_strtoul(buf, &buf, 16, &p); |
| 1095 | |
| 1096 | if (ret) { |
| 1097 | return GDB_READ_THREAD_ERR; |
| 1098 | } |
| 1099 | |
| 1100 | /* Skip '.' */ |
| 1101 | buf++; |
| 1102 | } else { |
| 1103 | p = 1; |
| 1104 | } |
| 1105 | |
| 1106 | ret = qemu_strtoul(buf, &buf, 16, &t); |
| 1107 | |
| 1108 | if (ret) { |
| 1109 | return GDB_READ_THREAD_ERR; |
| 1110 | } |
| 1111 | |
| 1112 | *end_buf = buf; |
| 1113 | |
| 1114 | if (p == -1) { |
| 1115 | return GDB_ALL_PROCESSES; |
| 1116 | } |
| 1117 | |
| 1118 | if (pid) { |
| 1119 | *pid = p; |
| 1120 | } |
| 1121 | |
| 1122 | if (t == -1) { |
| 1123 | return GDB_ALL_THREADS; |
| 1124 | } |
| 1125 | |
| 1126 | if (tid) { |
| 1127 | *tid = t; |
| 1128 | } |
| 1129 | |
| 1130 | return GDB_ONE_THREAD; |
| 1131 | } |
| 1132 | |
Jan Kiszka | 4dabe74 | 2015-02-07 09:38:43 +0100 | [diff] [blame] | 1133 | static int is_query_packet(const char *p, const char *query, char separator) |
| 1134 | { |
| 1135 | unsigned int query_len = strlen(query); |
| 1136 | |
| 1137 | return strncmp(p, query, query_len) == 0 && |
| 1138 | (p[query_len] == '\0' || p[query_len] == separator); |
| 1139 | } |
| 1140 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1141 | /** |
| 1142 | * gdb_handle_vcont - Parses and handles a vCont packet. |
| 1143 | * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is |
| 1144 | * a format error, 0 on success. |
| 1145 | */ |
| 1146 | static int gdb_handle_vcont(GDBState *s, const char *p) |
| 1147 | { |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1148 | int res, signal = 0; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1149 | char cur_action; |
| 1150 | char *newstates; |
| 1151 | unsigned long tmp; |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1152 | uint32_t pid, tid; |
| 1153 | GDBProcess *process; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1154 | CPUState *cpu; |
| 1155 | #ifdef CONFIG_USER_ONLY |
| 1156 | int max_cpus = 1; /* global variable max_cpus exists only in system mode */ |
| 1157 | |
| 1158 | CPU_FOREACH(cpu) { |
| 1159 | max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus; |
| 1160 | } |
| 1161 | #endif |
| 1162 | /* uninitialised CPUs stay 0 */ |
| 1163 | newstates = g_new0(char, max_cpus); |
| 1164 | |
| 1165 | /* mark valid CPUs with 1 */ |
| 1166 | CPU_FOREACH(cpu) { |
| 1167 | newstates[cpu->cpu_index] = 1; |
| 1168 | } |
| 1169 | |
| 1170 | /* |
| 1171 | * res keeps track of what error we are returning, with -ENOTSUP meaning |
| 1172 | * that the command is unknown or unsupported, thus returning an empty |
| 1173 | * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid, |
| 1174 | * or incorrect parameters passed. |
| 1175 | */ |
| 1176 | res = 0; |
| 1177 | while (*p) { |
| 1178 | if (*p++ != ';') { |
| 1179 | res = -ENOTSUP; |
| 1180 | goto out; |
| 1181 | } |
| 1182 | |
| 1183 | cur_action = *p++; |
| 1184 | if (cur_action == 'C' || cur_action == 'S') { |
Peter Maydell | 95a5bef | 2017-07-20 17:31:30 +0100 | [diff] [blame] | 1185 | cur_action = qemu_tolower(cur_action); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1186 | res = qemu_strtoul(p + 1, &p, 16, &tmp); |
| 1187 | if (res) { |
| 1188 | goto out; |
| 1189 | } |
| 1190 | signal = gdb_signal_to_target(tmp); |
| 1191 | } else if (cur_action != 'c' && cur_action != 's') { |
| 1192 | /* unknown/invalid/unsupported command */ |
| 1193 | res = -ENOTSUP; |
| 1194 | goto out; |
| 1195 | } |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1196 | |
| 1197 | if (*p++ != ':') { |
| 1198 | res = -ENOTSUP; |
| 1199 | goto out; |
| 1200 | } |
| 1201 | |
| 1202 | switch (read_thread_id(p, &p, &pid, &tid)) { |
| 1203 | case GDB_READ_THREAD_ERR: |
| 1204 | res = -EINVAL; |
| 1205 | goto out; |
| 1206 | |
| 1207 | case GDB_ALL_PROCESSES: |
| 1208 | cpu = gdb_first_attached_cpu(s); |
| 1209 | while (cpu) { |
| 1210 | if (newstates[cpu->cpu_index] == 1) { |
| 1211 | newstates[cpu->cpu_index] = cur_action; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1212 | } |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1213 | |
| 1214 | cpu = gdb_next_attached_cpu(s, cpu); |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1215 | } |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1216 | break; |
| 1217 | |
| 1218 | case GDB_ALL_THREADS: |
| 1219 | process = gdb_get_process(s, pid); |
| 1220 | |
| 1221 | if (!process->attached) { |
| 1222 | res = -EINVAL; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1223 | goto out; |
| 1224 | } |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1225 | |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1226 | cpu = get_first_cpu_in_process(s, process); |
| 1227 | while (cpu) { |
| 1228 | if (newstates[cpu->cpu_index] == 1) { |
| 1229 | newstates[cpu->cpu_index] = cur_action; |
| 1230 | } |
| 1231 | |
| 1232 | cpu = gdb_next_cpu_in_process(s, cpu); |
| 1233 | } |
| 1234 | break; |
| 1235 | |
| 1236 | case GDB_ONE_THREAD: |
| 1237 | cpu = gdb_get_cpu(s, pid, tid); |
Alex Bennée | 5a6a1ad | 2017-07-12 11:52:16 +0100 | [diff] [blame] | 1238 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1239 | /* invalid CPU/thread specified */ |
Alex Bennée | 5a6a1ad | 2017-07-12 11:52:16 +0100 | [diff] [blame] | 1240 | if (!cpu) { |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1241 | res = -EINVAL; |
| 1242 | goto out; |
| 1243 | } |
Alex Bennée | 5a6a1ad | 2017-07-12 11:52:16 +0100 | [diff] [blame] | 1244 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1245 | /* only use if no previous match occourred */ |
| 1246 | if (newstates[cpu->cpu_index] == 1) { |
| 1247 | newstates[cpu->cpu_index] = cur_action; |
| 1248 | } |
Luc Michel | e40e520 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1249 | break; |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | s->signal = signal; |
| 1253 | gdb_continue_partial(s, newstates); |
| 1254 | |
| 1255 | out: |
| 1256 | g_free(newstates); |
| 1257 | |
| 1258 | return res; |
| 1259 | } |
| 1260 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1261 | static int gdb_handle_packet(GDBState *s, const char *line_buf) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1262 | { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1263 | CPUState *cpu; |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1264 | GDBProcess *process; |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 1265 | CPUClass *cc; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1266 | const char *p; |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1267 | uint32_t pid, tid; |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1268 | int ch, reg_size, type, res; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1269 | uint8_t mem_buf[MAX_PACKET_LENGTH]; |
Philippe Mathieu-Daudé | 9005774 | 2018-04-08 11:59:33 -0300 | [diff] [blame] | 1270 | char buf[sizeof(mem_buf) + 1 /* trailing NUL */]; |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1271 | char thread_id[16]; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1272 | uint8_t *registers; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1273 | target_ulong addr, len; |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1274 | GDBThreadIdKind thread_kind; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1275 | |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1276 | trace_gdbstub_io_command(line_buf); |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1277 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1278 | p = line_buf; |
| 1279 | ch = *p++; |
| 1280 | switch(ch) { |
Luc Michel | 53fd655 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1281 | case '!': |
| 1282 | put_packet(s, "OK"); |
| 1283 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1284 | case '?': |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1285 | /* TODO: Make this return the correct value for user-mode. */ |
Luc Michel | 1a22733 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1286 | snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP, |
| 1287 | gdb_fmt_thread_id(s, s->c_cpu, thread_id, sizeof(thread_id))); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1288 | put_packet(s, buf); |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1289 | /* Remove all the breakpoints when this query is issued, |
| 1290 | * because gdb is doing and initial connect and the state |
| 1291 | * should be cleaned up. |
| 1292 | */ |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1293 | gdb_breakpoint_remove_all(); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1294 | break; |
| 1295 | case 'c': |
| 1296 | if (*p != '\0') { |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1297 | addr = strtoull(p, (char **)&p, 16); |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 1298 | gdb_set_cpu_pc(s, addr); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1299 | } |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1300 | s->signal = 0; |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 1301 | gdb_continue(s); |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1302 | return RS_IDLE; |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 1303 | case 'C': |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1304 | s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16)); |
| 1305 | if (s->signal == -1) |
| 1306 | s->signal = 0; |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 1307 | gdb_continue(s); |
| 1308 | return RS_IDLE; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1309 | case 'v': |
| 1310 | if (strncmp(p, "Cont", 4) == 0) { |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1311 | p += 4; |
| 1312 | if (*p == '?') { |
| 1313 | put_packet(s, "vCont;c;C;s;S"); |
| 1314 | break; |
| 1315 | } |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1316 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1317 | res = gdb_handle_vcont(s, p); |
| 1318 | |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1319 | if (res) { |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1320 | if ((res == -EINVAL) || (res == -ERANGE)) { |
| 1321 | put_packet(s, "E22"); |
| 1322 | break; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1323 | } |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1324 | goto unknown_command; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1325 | } |
| 1326 | break; |
Luc Michel | 3f940dc | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1327 | } else if (strncmp(p, "Attach;", 7) == 0) { |
| 1328 | unsigned long pid; |
| 1329 | |
| 1330 | p += 7; |
| 1331 | |
| 1332 | if (qemu_strtoul(p, &p, 16, &pid)) { |
| 1333 | put_packet(s, "E22"); |
| 1334 | break; |
| 1335 | } |
| 1336 | |
| 1337 | process = gdb_get_process(s, pid); |
| 1338 | |
| 1339 | if (process == NULL) { |
| 1340 | put_packet(s, "E22"); |
| 1341 | break; |
| 1342 | } |
| 1343 | |
| 1344 | cpu = get_first_cpu_in_process(s, process); |
| 1345 | |
| 1346 | if (cpu == NULL) { |
| 1347 | /* Refuse to attach an empty process */ |
| 1348 | put_packet(s, "E22"); |
| 1349 | break; |
| 1350 | } |
| 1351 | |
| 1352 | process->attached = true; |
| 1353 | |
| 1354 | s->g_cpu = cpu; |
| 1355 | s->c_cpu = cpu; |
| 1356 | |
| 1357 | snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP, |
| 1358 | gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id))); |
| 1359 | |
| 1360 | put_packet(s, buf); |
| 1361 | break; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1362 | } else { |
| 1363 | goto unknown_command; |
| 1364 | } |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1365 | case 'k': |
| 1366 | /* Kill the target */ |
Ziyue Yang | 7ae6c57 | 2017-01-18 16:03:29 +0800 | [diff] [blame] | 1367 | error_report("QEMU: Terminated via GDBstub"); |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1368 | exit(0); |
| 1369 | case 'D': |
| 1370 | /* Detach packet */ |
Luc Michel | 546f3c6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1371 | pid = 1; |
| 1372 | |
| 1373 | if (s->multiprocess) { |
| 1374 | unsigned long lpid; |
| 1375 | if (*p != ';') { |
| 1376 | put_packet(s, "E22"); |
| 1377 | break; |
| 1378 | } |
| 1379 | |
| 1380 | if (qemu_strtoul(p + 1, &p, 16, &lpid)) { |
| 1381 | put_packet(s, "E22"); |
| 1382 | break; |
| 1383 | } |
| 1384 | |
| 1385 | pid = lpid; |
| 1386 | } |
| 1387 | |
| 1388 | process = gdb_get_process(s, pid); |
| 1389 | gdb_process_breakpoint_remove_all(s, process); |
| 1390 | process->attached = false; |
| 1391 | |
| 1392 | if (pid == gdb_get_cpu_pid(s, s->c_cpu)) { |
| 1393 | s->c_cpu = gdb_first_attached_cpu(s); |
| 1394 | } |
| 1395 | |
| 1396 | if (pid == gdb_get_cpu_pid(s, s->g_cpu)) { |
| 1397 | s->g_cpu = gdb_first_attached_cpu(s); |
| 1398 | } |
| 1399 | |
| 1400 | if (s->c_cpu == NULL) { |
| 1401 | /* No more process attached */ |
| 1402 | gdb_syscall_mode = GDB_SYS_DISABLED; |
| 1403 | gdb_continue(s); |
| 1404 | } |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1405 | put_packet(s, "OK"); |
| 1406 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1407 | case 's': |
| 1408 | if (*p != '\0') { |
ths | 8fac580 | 2007-07-12 10:05:07 +0000 | [diff] [blame] | 1409 | addr = strtoull(p, (char **)&p, 16); |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 1410 | gdb_set_cpu_pc(s, addr); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1411 | } |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1412 | cpu_single_step(s->c_cpu, sstep_flags); |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 1413 | gdb_continue(s); |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1414 | return RS_IDLE; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1415 | case 'F': |
| 1416 | { |
| 1417 | target_ulong ret; |
| 1418 | target_ulong err; |
| 1419 | |
| 1420 | ret = strtoull(p, (char **)&p, 16); |
| 1421 | if (*p == ',') { |
| 1422 | p++; |
| 1423 | err = strtoull(p, (char **)&p, 16); |
| 1424 | } else { |
| 1425 | err = 0; |
| 1426 | } |
| 1427 | if (*p == ',') |
| 1428 | p++; |
| 1429 | type = *p; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1430 | if (s->current_syscall_cb) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1431 | s->current_syscall_cb(s->c_cpu, ret, err); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1432 | s->current_syscall_cb = NULL; |
| 1433 | } |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1434 | if (type == 'C') { |
| 1435 | put_packet(s, "T02"); |
| 1436 | } else { |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 1437 | gdb_continue(s); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1441 | case 'g': |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1442 | cpu_synchronize_state(s->g_cpu); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1443 | len = 0; |
Andreas Färber | 35143f0 | 2013-08-12 18:09:47 +0200 | [diff] [blame] | 1444 | for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1445 | reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1446 | len += reg_size; |
| 1447 | } |
| 1448 | memtohex(buf, mem_buf, len); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1449 | put_packet(s, buf); |
| 1450 | break; |
| 1451 | case 'G': |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1452 | cpu_synchronize_state(s->g_cpu); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1453 | registers = mem_buf; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1454 | len = strlen(p) / 2; |
| 1455 | hextomem((uint8_t *)registers, p, len); |
Andreas Färber | 35143f0 | 2013-08-12 18:09:47 +0200 | [diff] [blame] | 1456 | for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1457 | reg_size = gdb_write_register(s->g_cpu, registers, addr); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1458 | len -= reg_size; |
| 1459 | registers += reg_size; |
| 1460 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1461 | put_packet(s, "OK"); |
| 1462 | break; |
| 1463 | case 'm': |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1464 | addr = strtoull(p, (char **)&p, 16); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1465 | if (*p == ',') |
| 1466 | p++; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1467 | len = strtoull(p, NULL, 16); |
Kevin Wolf | 5accecb | 2015-10-13 09:38:50 +0200 | [diff] [blame] | 1468 | |
| 1469 | /* memtohex() doubles the required space */ |
| 1470 | if (len > MAX_PACKET_LENGTH / 2) { |
| 1471 | put_packet (s, "E22"); |
| 1472 | break; |
| 1473 | } |
| 1474 | |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1475 | if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) { |
bellard | 6f970bd | 2005-12-05 19:55:19 +0000 | [diff] [blame] | 1476 | put_packet (s, "E14"); |
| 1477 | } else { |
| 1478 | memtohex(buf, mem_buf, len); |
| 1479 | put_packet(s, buf); |
| 1480 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1481 | break; |
| 1482 | case 'M': |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1483 | addr = strtoull(p, (char **)&p, 16); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1484 | if (*p == ',') |
| 1485 | p++; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1486 | len = strtoull(p, (char **)&p, 16); |
bellard | b328f87 | 2005-01-17 22:03:16 +0000 | [diff] [blame] | 1487 | if (*p == ':') |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1488 | p++; |
Kevin Wolf | 5accecb | 2015-10-13 09:38:50 +0200 | [diff] [blame] | 1489 | |
| 1490 | /* hextomem() reads 2*len bytes */ |
| 1491 | if (len > strlen(p) / 2) { |
| 1492 | put_packet (s, "E22"); |
| 1493 | break; |
| 1494 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1495 | hextomem(mem_buf, p, len); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1496 | if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, |
Andreas Färber | f3659ee | 2013-06-27 19:09:09 +0200 | [diff] [blame] | 1497 | true) != 0) { |
bellard | 905f20b | 2005-04-26 21:09:55 +0000 | [diff] [blame] | 1498 | put_packet(s, "E14"); |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 1499 | } else { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1500 | put_packet(s, "OK"); |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 1501 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1502 | break; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1503 | case 'p': |
| 1504 | /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable. |
| 1505 | This works, but can be very slow. Anything new enough to |
| 1506 | understand XML also knows how to use this properly. */ |
| 1507 | if (!gdb_has_xml) |
| 1508 | goto unknown_command; |
| 1509 | addr = strtoull(p, (char **)&p, 16); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1510 | reg_size = gdb_read_register(s->g_cpu, mem_buf, addr); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1511 | if (reg_size) { |
| 1512 | memtohex(buf, mem_buf, reg_size); |
| 1513 | put_packet(s, buf); |
| 1514 | } else { |
| 1515 | put_packet(s, "E14"); |
| 1516 | } |
| 1517 | break; |
| 1518 | case 'P': |
| 1519 | if (!gdb_has_xml) |
| 1520 | goto unknown_command; |
| 1521 | addr = strtoull(p, (char **)&p, 16); |
| 1522 | if (*p == '=') |
| 1523 | p++; |
| 1524 | reg_size = strlen(p) / 2; |
| 1525 | hextomem(mem_buf, p, reg_size); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1526 | gdb_write_register(s->g_cpu, mem_buf, addr); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1527 | put_packet(s, "OK"); |
| 1528 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1529 | case 'Z': |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1530 | case 'z': |
| 1531 | type = strtoul(p, (char **)&p, 16); |
| 1532 | if (*p == ',') |
| 1533 | p++; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1534 | addr = strtoull(p, (char **)&p, 16); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1535 | if (*p == ',') |
| 1536 | p++; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1537 | len = strtoull(p, (char **)&p, 16); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1538 | if (ch == 'Z') |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1539 | res = gdb_breakpoint_insert(addr, len, type); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1540 | else |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1541 | res = gdb_breakpoint_remove(addr, len, type); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1542 | if (res >= 0) |
| 1543 | put_packet(s, "OK"); |
| 1544 | else if (res == -ENOSYS) |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1545 | put_packet(s, ""); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1546 | else |
| 1547 | put_packet(s, "E22"); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1548 | break; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1549 | case 'H': |
| 1550 | type = *p++; |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1551 | |
| 1552 | thread_kind = read_thread_id(p, &p, &pid, &tid); |
| 1553 | if (thread_kind == GDB_READ_THREAD_ERR) { |
| 1554 | put_packet(s, "E22"); |
| 1555 | break; |
| 1556 | } |
| 1557 | |
| 1558 | if (thread_kind != GDB_ONE_THREAD) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1559 | put_packet(s, "OK"); |
| 1560 | break; |
| 1561 | } |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1562 | cpu = gdb_get_cpu(s, pid, tid); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1563 | if (cpu == NULL) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1564 | put_packet(s, "E22"); |
| 1565 | break; |
| 1566 | } |
| 1567 | switch (type) { |
| 1568 | case 'c': |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1569 | s->c_cpu = cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1570 | put_packet(s, "OK"); |
| 1571 | break; |
| 1572 | case 'g': |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1573 | s->g_cpu = cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1574 | put_packet(s, "OK"); |
| 1575 | break; |
| 1576 | default: |
| 1577 | put_packet(s, "E22"); |
| 1578 | break; |
| 1579 | } |
| 1580 | break; |
| 1581 | case 'T': |
Luc Michel | 7d8c87d | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 1582 | thread_kind = read_thread_id(p, &p, &pid, &tid); |
| 1583 | if (thread_kind == GDB_READ_THREAD_ERR) { |
| 1584 | put_packet(s, "E22"); |
| 1585 | break; |
| 1586 | } |
| 1587 | cpu = gdb_get_cpu(s, pid, tid); |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1588 | |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1589 | if (cpu != NULL) { |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1590 | put_packet(s, "OK"); |
| 1591 | } else { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1592 | put_packet(s, "E22"); |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1593 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1594 | break; |
pbrook | 978efd6 | 2006-06-17 18:30:42 +0000 | [diff] [blame] | 1595 | case 'q': |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1596 | case 'Q': |
| 1597 | /* parse any 'q' packets here */ |
| 1598 | if (!strcmp(p,"qemu.sstepbits")) { |
| 1599 | /* Query Breakpoint bit definitions */ |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1600 | snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", |
| 1601 | SSTEP_ENABLE, |
| 1602 | SSTEP_NOIRQ, |
| 1603 | SSTEP_NOTIMER); |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1604 | put_packet(s, buf); |
| 1605 | break; |
Jan Kiszka | 4dabe74 | 2015-02-07 09:38:43 +0100 | [diff] [blame] | 1606 | } else if (is_query_packet(p, "qemu.sstep", '=')) { |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1607 | /* Display or change the sstep_flags */ |
| 1608 | p += 10; |
| 1609 | if (*p != '=') { |
| 1610 | /* Display current setting */ |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1611 | snprintf(buf, sizeof(buf), "0x%x", sstep_flags); |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1612 | put_packet(s, buf); |
| 1613 | break; |
| 1614 | } |
| 1615 | p++; |
| 1616 | type = strtoul(p, (char **)&p, 16); |
| 1617 | sstep_flags = type; |
| 1618 | put_packet(s, "OK"); |
| 1619 | break; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1620 | } else if (strcmp(p,"C") == 0) { |
Luc Michel | 8dbbe9a | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1621 | /* |
| 1622 | * "Current thread" remains vague in the spec, so always return |
| 1623 | * the first thread of the current process (gdb returns the |
| 1624 | * first thread). |
| 1625 | */ |
| 1626 | cpu = get_first_cpu_in_process(s, gdb_get_cpu_process(s, s->g_cpu)); |
| 1627 | snprintf(buf, sizeof(buf), "QC%s", |
| 1628 | gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id))); |
| 1629 | put_packet(s, buf); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1630 | break; |
| 1631 | } else if (strcmp(p,"fThreadInfo") == 0) { |
Luc Michel | 7cf48f6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1632 | s->query_cpu = gdb_first_attached_cpu(s); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1633 | goto report_cpuinfo; |
| 1634 | } else if (strcmp(p,"sThreadInfo") == 0) { |
| 1635 | report_cpuinfo: |
| 1636 | if (s->query_cpu) { |
Luc Michel | 7cf48f6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1637 | snprintf(buf, sizeof(buf), "m%s", |
| 1638 | gdb_fmt_thread_id(s, s->query_cpu, |
| 1639 | thread_id, sizeof(thread_id))); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1640 | put_packet(s, buf); |
Luc Michel | 7cf48f6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1641 | s->query_cpu = gdb_next_attached_cpu(s, s->query_cpu); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1642 | } else |
| 1643 | put_packet(s, "l"); |
| 1644 | break; |
| 1645 | } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) { |
Luc Michel | 7cf48f6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1646 | if (read_thread_id(p + 16, &p, &pid, &tid) == GDB_READ_THREAD_ERR) { |
| 1647 | put_packet(s, "E22"); |
| 1648 | break; |
| 1649 | } |
| 1650 | cpu = gdb_get_cpu(s, pid, tid); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1651 | if (cpu != NULL) { |
Andreas Färber | cb446ec | 2013-05-01 14:24:52 +0200 | [diff] [blame] | 1652 | cpu_synchronize_state(cpu); |
Luc Michel | 7cf48f6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1653 | |
| 1654 | if (s->multiprocess && (s->process_num > 1)) { |
| 1655 | /* Print the CPU model and name in multiprocess mode */ |
| 1656 | ObjectClass *oc = object_get_class(OBJECT(cpu)); |
| 1657 | const char *cpu_model = object_class_get_name(oc); |
| 1658 | char *cpu_name = |
| 1659 | object_get_canonical_path_component(OBJECT(cpu)); |
| 1660 | len = snprintf((char *)mem_buf, sizeof(buf) / 2, |
| 1661 | "%s %s [%s]", cpu_model, cpu_name, |
| 1662 | cpu->halted ? "halted " : "running"); |
| 1663 | g_free(cpu_name); |
| 1664 | } else { |
| 1665 | /* memtohex() doubles the required space */ |
| 1666 | len = snprintf((char *)mem_buf, sizeof(buf) / 2, |
| 1667 | "CPU#%d [%s]", cpu->cpu_index, |
| 1668 | cpu->halted ? "halted " : "running"); |
| 1669 | } |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1670 | trace_gdbstub_op_extra_info((char *)mem_buf); |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1671 | memtohex(buf, mem_buf, len); |
| 1672 | put_packet(s, buf); |
| 1673 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1674 | break; |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1675 | } |
blueswir1 | 0b8a988 | 2009-03-07 10:51:36 +0000 | [diff] [blame] | 1676 | #ifdef CONFIG_USER_ONLY |
Jan Kiszka | 070949f | 2015-02-07 09:38:42 +0100 | [diff] [blame] | 1677 | else if (strcmp(p, "Offsets") == 0) { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 1678 | TaskState *ts = s->c_cpu->opaque; |
pbrook | 978efd6 | 2006-06-17 18:30:42 +0000 | [diff] [blame] | 1679 | |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1680 | snprintf(buf, sizeof(buf), |
| 1681 | "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx |
| 1682 | ";Bss=" TARGET_ABI_FMT_lx, |
| 1683 | ts->info->code_offset, |
| 1684 | ts->info->data_offset, |
| 1685 | ts->info->data_offset); |
pbrook | 978efd6 | 2006-06-17 18:30:42 +0000 | [diff] [blame] | 1686 | put_packet(s, buf); |
| 1687 | break; |
| 1688 | } |
blueswir1 | 0b8a988 | 2009-03-07 10:51:36 +0000 | [diff] [blame] | 1689 | #else /* !CONFIG_USER_ONLY */ |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1690 | else if (strncmp(p, "Rcmd,", 5) == 0) { |
| 1691 | int len = strlen(p + 5); |
| 1692 | |
| 1693 | if ((len % 2) != 0) { |
| 1694 | put_packet(s, "E01"); |
| 1695 | break; |
| 1696 | } |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1697 | len = len / 2; |
Kevin Wolf | 5accecb | 2015-10-13 09:38:50 +0200 | [diff] [blame] | 1698 | hextomem(mem_buf, p + 5, len); |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1699 | mem_buf[len++] = 0; |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 1700 | qemu_chr_be_write(s->mon_chr, mem_buf, len); |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1701 | put_packet(s, "OK"); |
| 1702 | break; |
| 1703 | } |
blueswir1 | 0b8a988 | 2009-03-07 10:51:36 +0000 | [diff] [blame] | 1704 | #endif /* !CONFIG_USER_ONLY */ |
Jan Kiszka | 4dabe74 | 2015-02-07 09:38:43 +0100 | [diff] [blame] | 1705 | if (is_query_packet(p, "Supported", ':')) { |
blueswir1 | 5b3715b | 2008-10-25 11:18:12 +0000 | [diff] [blame] | 1706 | snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH); |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 1707 | cc = CPU_GET_CLASS(first_cpu); |
| 1708 | if (cc->gdb_core_xml_file != NULL) { |
| 1709 | pstrcat(buf, sizeof(buf), ";qXfer:features:read+"); |
| 1710 | } |
Luc Michel | 364fce6 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1711 | |
| 1712 | if (strstr(p, "multiprocess+")) { |
| 1713 | s->multiprocess = true; |
| 1714 | } |
| 1715 | pstrcat(buf, sizeof(buf), ";multiprocess+"); |
| 1716 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1717 | put_packet(s, buf); |
| 1718 | break; |
| 1719 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1720 | if (strncmp(p, "Xfer:features:read:", 19) == 0) { |
| 1721 | const char *xml; |
| 1722 | target_ulong total_len; |
| 1723 | |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1724 | process = gdb_get_cpu_process(s, s->g_cpu); |
| 1725 | cc = CPU_GET_CLASS(s->g_cpu); |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 1726 | if (cc->gdb_core_xml_file == NULL) { |
| 1727 | goto unknown_command; |
| 1728 | } |
| 1729 | |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 1730 | gdb_has_xml = true; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1731 | p += 19; |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1732 | xml = get_feature_xml(s, p, &p, process); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1733 | if (!xml) { |
blueswir1 | 5b3715b | 2008-10-25 11:18:12 +0000 | [diff] [blame] | 1734 | snprintf(buf, sizeof(buf), "E00"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1735 | put_packet(s, buf); |
| 1736 | break; |
| 1737 | } |
| 1738 | |
| 1739 | if (*p == ':') |
| 1740 | p++; |
| 1741 | addr = strtoul(p, (char **)&p, 16); |
| 1742 | if (*p == ',') |
| 1743 | p++; |
| 1744 | len = strtoul(p, (char **)&p, 16); |
| 1745 | |
| 1746 | total_len = strlen(xml); |
| 1747 | if (addr > total_len) { |
blueswir1 | 5b3715b | 2008-10-25 11:18:12 +0000 | [diff] [blame] | 1748 | snprintf(buf, sizeof(buf), "E00"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1749 | put_packet(s, buf); |
| 1750 | break; |
| 1751 | } |
| 1752 | if (len > (MAX_PACKET_LENGTH - 5) / 2) |
| 1753 | len = (MAX_PACKET_LENGTH - 5) / 2; |
| 1754 | if (len < total_len - addr) { |
| 1755 | buf[0] = 'm'; |
| 1756 | len = memtox(buf + 1, xml + addr, len); |
| 1757 | } else { |
| 1758 | buf[0] = 'l'; |
| 1759 | len = memtox(buf + 1, xml + addr, total_len - addr); |
| 1760 | } |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1761 | put_packet_binary(s, buf, len + 1, true); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1762 | break; |
| 1763 | } |
Jan Kiszka | a391938 | 2015-02-07 09:38:44 +0100 | [diff] [blame] | 1764 | if (is_query_packet(p, "Attached", ':')) { |
| 1765 | put_packet(s, GDB_ATTACHED); |
| 1766 | break; |
| 1767 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1768 | /* Unrecognised 'q' command. */ |
| 1769 | goto unknown_command; |
| 1770 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1771 | default: |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1772 | unknown_command: |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1773 | /* put empty packet */ |
| 1774 | buf[0] = '\0'; |
| 1775 | put_packet(s, buf); |
| 1776 | break; |
| 1777 | } |
| 1778 | return RS_IDLE; |
| 1779 | } |
| 1780 | |
Andreas Färber | 64f6b34 | 2013-05-27 02:06:09 +0200 | [diff] [blame] | 1781 | void gdb_set_stop_cpu(CPUState *cpu) |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1782 | { |
Luc Michel | 160d858 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1783 | GDBProcess *p = gdb_get_cpu_process(gdbserver_state, cpu); |
| 1784 | |
| 1785 | if (!p->attached) { |
| 1786 | /* |
| 1787 | * Having a stop CPU corresponding to a process that is not attached |
| 1788 | * confuses GDB. So we ignore the request. |
| 1789 | */ |
| 1790 | return; |
| 1791 | } |
| 1792 | |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1793 | gdbserver_state->c_cpu = cpu; |
| 1794 | gdbserver_state->g_cpu = cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1797 | #ifndef CONFIG_USER_ONLY |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 1798 | static void gdb_vm_state_change(void *opaque, int running, RunState state) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1799 | { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1800 | GDBState *s = gdbserver_state; |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1801 | CPUState *cpu = s->c_cpu; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1802 | char buf[256]; |
Luc Michel | 95567c2 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1803 | char thread_id[16]; |
aliguori | d6fc1b3 | 2008-11-18 19:55:44 +0000 | [diff] [blame] | 1804 | const char *type; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1805 | int ret; |
| 1806 | |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1807 | if (running || s->state == RS_INACTIVE) { |
| 1808 | return; |
| 1809 | } |
| 1810 | /* Is there a GDB syscall waiting to be sent? */ |
| 1811 | if (s->current_syscall_cb) { |
| 1812 | put_packet(s, s->syscall_buf); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1813 | return; |
Jan Kiszka | e07bbac | 2011-02-09 16:29:40 +0100 | [diff] [blame] | 1814 | } |
Luc Michel | 95567c2 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1815 | |
| 1816 | if (cpu == NULL) { |
| 1817 | /* No process attached */ |
| 1818 | return; |
| 1819 | } |
| 1820 | |
| 1821 | gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)); |
| 1822 | |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 1823 | switch (state) { |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1824 | case RUN_STATE_DEBUG: |
Andreas Färber | ff4700b | 2013-08-26 18:23:18 +0200 | [diff] [blame] | 1825 | if (cpu->watchpoint_hit) { |
| 1826 | switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1827 | case BP_MEM_READ: |
aliguori | d6fc1b3 | 2008-11-18 19:55:44 +0000 | [diff] [blame] | 1828 | type = "r"; |
| 1829 | break; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1830 | case BP_MEM_ACCESS: |
aliguori | d6fc1b3 | 2008-11-18 19:55:44 +0000 | [diff] [blame] | 1831 | type = "a"; |
| 1832 | break; |
| 1833 | default: |
| 1834 | type = ""; |
| 1835 | break; |
| 1836 | } |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1837 | trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu), |
| 1838 | (target_ulong)cpu->watchpoint_hit->vaddr); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1839 | snprintf(buf, sizeof(buf), |
Luc Michel | 95567c2 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1840 | "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";", |
| 1841 | GDB_SIGNAL_TRAP, thread_id, type, |
Andreas Färber | ff4700b | 2013-08-26 18:23:18 +0200 | [diff] [blame] | 1842 | (target_ulong)cpu->watchpoint_hit->vaddr); |
| 1843 | cpu->watchpoint_hit = NULL; |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1844 | goto send_packet; |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1845 | } else { |
| 1846 | trace_gdbstub_hit_break(); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1847 | } |
Peter Crosthwaite | bbd77c1 | 2015-06-23 19:31:15 -0700 | [diff] [blame] | 1848 | tb_flush(cpu); |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1849 | ret = GDB_SIGNAL_TRAP; |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1850 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1851 | case RUN_STATE_PAUSED: |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1852 | trace_gdbstub_hit_paused(); |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 1853 | ret = GDB_SIGNAL_INT; |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1854 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1855 | case RUN_STATE_SHUTDOWN: |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1856 | trace_gdbstub_hit_shutdown(); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1857 | ret = GDB_SIGNAL_QUIT; |
| 1858 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1859 | case RUN_STATE_IO_ERROR: |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1860 | trace_gdbstub_hit_io_error(); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1861 | ret = GDB_SIGNAL_IO; |
| 1862 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1863 | case RUN_STATE_WATCHDOG: |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1864 | trace_gdbstub_hit_watchdog(); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1865 | ret = GDB_SIGNAL_ALRM; |
| 1866 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1867 | case RUN_STATE_INTERNAL_ERROR: |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1868 | trace_gdbstub_hit_internal_error(); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1869 | ret = GDB_SIGNAL_ABRT; |
| 1870 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1871 | case RUN_STATE_SAVE_VM: |
| 1872 | case RUN_STATE_RESTORE_VM: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1873 | return; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1874 | case RUN_STATE_FINISH_MIGRATE: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1875 | ret = GDB_SIGNAL_XCPU; |
| 1876 | break; |
| 1877 | default: |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1878 | trace_gdbstub_hit_unknown(state); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1879 | ret = GDB_SIGNAL_UNKNOWN; |
| 1880 | break; |
bellard | bbeb7b5 | 2006-04-23 18:42:15 +0000 | [diff] [blame] | 1881 | } |
Jan Kiszka | 226d007 | 2015-07-24 18:52:31 +0200 | [diff] [blame] | 1882 | gdb_set_stop_cpu(cpu); |
Luc Michel | 95567c2 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 1883 | snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1884 | |
| 1885 | send_packet: |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1886 | put_packet(s, buf); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1887 | |
| 1888 | /* disable single step if it was enabled */ |
Andreas Färber | 3825b28 | 2013-06-24 18:41:06 +0200 | [diff] [blame] | 1889 | cpu_single_step(cpu, 0); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1890 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1891 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1892 | |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1893 | /* Send a gdb syscall request. |
| 1894 | This accepts limited printf-style format specifiers, specifically: |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1895 | %x - target_ulong argument printed in hex. |
| 1896 | %lx - 64-bit argument printed in hex. |
| 1897 | %s - string pointer (target_ulong) and length (int) pair. */ |
Peter Maydell | 19239b3 | 2015-09-07 10:39:27 +0100 | [diff] [blame] | 1898 | 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] | 1899 | { |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1900 | char *p; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1901 | char *p_end; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1902 | target_ulong addr; |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1903 | uint64_t i64; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1904 | GDBState *s; |
| 1905 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1906 | s = gdbserver_state; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1907 | if (!s) |
| 1908 | return; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1909 | s->current_syscall_cb = cb; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1910 | #ifndef CONFIG_USER_ONLY |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1911 | vm_stop(RUN_STATE_DEBUG); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1912 | #endif |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1913 | p = s->syscall_buf; |
| 1914 | p_end = &s->syscall_buf[sizeof(s->syscall_buf)]; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1915 | *(p++) = 'F'; |
| 1916 | while (*fmt) { |
| 1917 | if (*fmt == '%') { |
| 1918 | fmt++; |
| 1919 | switch (*fmt++) { |
| 1920 | case 'x': |
| 1921 | addr = va_arg(va, target_ulong); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1922 | p += snprintf(p, p_end - p, TARGET_FMT_lx, addr); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1923 | break; |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1924 | case 'l': |
| 1925 | if (*(fmt++) != 'x') |
| 1926 | goto bad_format; |
| 1927 | i64 = va_arg(va, uint64_t); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1928 | p += snprintf(p, p_end - p, "%" PRIx64, i64); |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1929 | break; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1930 | case 's': |
| 1931 | addr = va_arg(va, target_ulong); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1932 | p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x", |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1933 | addr, va_arg(va, int)); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1934 | break; |
| 1935 | default: |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1936 | bad_format: |
Ziyue Yang | 7ae6c57 | 2017-01-18 16:03:29 +0800 | [diff] [blame] | 1937 | error_report("gdbstub: Bad syscall format string '%s'", |
| 1938 | fmt - 1); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1939 | break; |
| 1940 | } |
| 1941 | } else { |
| 1942 | *(p++) = *(fmt++); |
| 1943 | } |
| 1944 | } |
pbrook | 8a93e02 | 2007-08-06 13:19:15 +0000 | [diff] [blame] | 1945 | *p = 0; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1946 | #ifdef CONFIG_USER_ONLY |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1947 | put_packet(s, s->syscall_buf); |
Peter Maydell | 4f71086 | 2018-05-15 19:19:58 +0100 | [diff] [blame] | 1948 | /* Return control to gdb for it to process the syscall request. |
| 1949 | * Since the protocol requires that gdb hands control back to us |
| 1950 | * using a "here are the results" F packet, we don't need to check |
| 1951 | * gdb_handlesig's return value (which is the signal to deliver if |
| 1952 | * execution was resumed via a continue packet). |
| 1953 | */ |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1954 | gdb_handlesig(s->c_cpu, 0); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1955 | #else |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1956 | /* In this case wait to send the syscall packet until notification that |
| 1957 | the CPU has stopped. This must be done because if the packet is sent |
| 1958 | now the reply from the syscall request could be received while the CPU |
| 1959 | is still in the running state, which can cause packets to be dropped |
| 1960 | and state transition 'T' packets to be sent while the syscall is still |
| 1961 | being processed. */ |
Paolo Bonzini | 9102ded | 2015-08-18 06:52:09 -0700 | [diff] [blame] | 1962 | qemu_cpu_kick(s->c_cpu); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1963 | #endif |
| 1964 | } |
| 1965 | |
Peter Maydell | 19239b3 | 2015-09-07 10:39:27 +0100 | [diff] [blame] | 1966 | void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...) |
| 1967 | { |
| 1968 | va_list va; |
| 1969 | |
| 1970 | va_start(va, fmt); |
| 1971 | gdb_do_syscallv(cb, fmt, va); |
| 1972 | va_end(va); |
| 1973 | } |
| 1974 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1975 | static void gdb_read_byte(GDBState *s, int ch) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1976 | { |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 1977 | uint8_t reply; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1978 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1979 | #ifndef CONFIG_USER_ONLY |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1980 | if (s->last_packet_len) { |
| 1981 | /* Waiting for a response to the last packet. If we see the start |
| 1982 | of a new command then abandon the previous response. */ |
| 1983 | if (ch == '-') { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1984 | trace_gdbstub_err_got_nack(); |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 1985 | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1986 | } else if (ch == '+') { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1987 | trace_gdbstub_io_got_ack(); |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1988 | } else { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 1989 | trace_gdbstub_io_got_unexpected((uint8_t)ch); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1990 | } |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1991 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1992 | if (ch == '+' || ch == '$') |
| 1993 | s->last_packet_len = 0; |
| 1994 | if (ch != '$') |
| 1995 | return; |
| 1996 | } |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 1997 | if (runstate_is_running()) { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1998 | /* when the CPU is running, we cannot do anything except stop |
| 1999 | it when receiving a char */ |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2000 | vm_stop(RUN_STATE_PAUSED); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2001 | } else |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2002 | #endif |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 2003 | { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2004 | switch(s->state) { |
| 2005 | case RS_IDLE: |
| 2006 | if (ch == '$') { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2007 | /* start of command packet */ |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2008 | s->line_buf_index = 0; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2009 | s->line_sum = 0; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2010 | s->state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2011 | } else { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2012 | trace_gdbstub_err_garbage((uint8_t)ch); |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 2013 | } |
| 2014 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2015 | case RS_GETLINE: |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2016 | if (ch == '}') { |
| 2017 | /* start escape sequence */ |
| 2018 | s->state = RS_GETLINE_ESC; |
| 2019 | s->line_sum += ch; |
| 2020 | } else if (ch == '*') { |
| 2021 | /* start run length encoding sequence */ |
| 2022 | s->state = RS_GETLINE_RLE; |
| 2023 | s->line_sum += ch; |
| 2024 | } else if (ch == '#') { |
| 2025 | /* end of command, start of checksum*/ |
| 2026 | s->state = RS_CHKSUM1; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2027 | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2028 | trace_gdbstub_err_overrun(); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2029 | s->state = RS_IDLE; |
| 2030 | } else { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2031 | /* unescaped command character */ |
| 2032 | s->line_buf[s->line_buf_index++] = ch; |
| 2033 | s->line_sum += ch; |
| 2034 | } |
| 2035 | break; |
| 2036 | case RS_GETLINE_ESC: |
| 2037 | if (ch == '#') { |
| 2038 | /* unexpected end of command in escape sequence */ |
| 2039 | s->state = RS_CHKSUM1; |
| 2040 | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
| 2041 | /* command buffer overrun */ |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2042 | trace_gdbstub_err_overrun(); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2043 | s->state = RS_IDLE; |
| 2044 | } else { |
| 2045 | /* parse escaped character and leave escape state */ |
| 2046 | s->line_buf[s->line_buf_index++] = ch ^ 0x20; |
| 2047 | s->line_sum += ch; |
| 2048 | s->state = RS_GETLINE; |
| 2049 | } |
| 2050 | break; |
| 2051 | case RS_GETLINE_RLE: |
| 2052 | if (ch < ' ') { |
| 2053 | /* invalid RLE count encoding */ |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2054 | trace_gdbstub_err_invalid_repeat((uint8_t)ch); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2055 | s->state = RS_GETLINE; |
| 2056 | } else { |
| 2057 | /* decode repeat length */ |
| 2058 | int repeat = (unsigned char)ch - ' ' + 3; |
| 2059 | if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) { |
| 2060 | /* that many repeats would overrun the command buffer */ |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2061 | trace_gdbstub_err_overrun(); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2062 | s->state = RS_IDLE; |
| 2063 | } else if (s->line_buf_index < 1) { |
| 2064 | /* got a repeat but we have nothing to repeat */ |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2065 | trace_gdbstub_err_invalid_rle(); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2066 | s->state = RS_GETLINE; |
| 2067 | } else { |
| 2068 | /* repeat the last character */ |
| 2069 | memset(s->line_buf + s->line_buf_index, |
| 2070 | s->line_buf[s->line_buf_index - 1], repeat); |
| 2071 | s->line_buf_index += repeat; |
| 2072 | s->line_sum += ch; |
| 2073 | s->state = RS_GETLINE; |
| 2074 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2075 | } |
| 2076 | break; |
| 2077 | case RS_CHKSUM1: |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2078 | /* get high hex digit of checksum */ |
| 2079 | if (!isxdigit(ch)) { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2080 | trace_gdbstub_err_checksum_invalid((uint8_t)ch); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2081 | s->state = RS_GETLINE; |
| 2082 | break; |
| 2083 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2084 | s->line_buf[s->line_buf_index] = '\0'; |
| 2085 | s->line_csum = fromhex(ch) << 4; |
| 2086 | s->state = RS_CHKSUM2; |
| 2087 | break; |
| 2088 | case RS_CHKSUM2: |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2089 | /* get low hex digit of checksum */ |
| 2090 | if (!isxdigit(ch)) { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2091 | trace_gdbstub_err_checksum_invalid((uint8_t)ch); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2092 | s->state = RS_GETLINE; |
| 2093 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2094 | } |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2095 | s->line_csum |= fromhex(ch); |
| 2096 | |
| 2097 | if (s->line_csum != (s->line_sum & 0xff)) { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2098 | trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2099 | /* send NAK reply */ |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 2100 | reply = '-'; |
| 2101 | put_buffer(s, &reply, 1); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2102 | s->state = RS_IDLE; |
| 2103 | } else { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 2104 | /* send ACK reply */ |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 2105 | reply = '+'; |
| 2106 | put_buffer(s, &reply, 1); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 2107 | s->state = gdb_handle_packet(s, s->line_buf); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2108 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2109 | break; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 2110 | default: |
| 2111 | abort(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2112 | } |
| 2113 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 2116 | /* Tell the remote gdb that the process has exited. */ |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 2117 | void gdb_exit(CPUArchState *env, int code) |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 2118 | { |
| 2119 | GDBState *s; |
| 2120 | char buf[4]; |
| 2121 | |
| 2122 | s = gdbserver_state; |
| 2123 | if (!s) { |
| 2124 | return; |
| 2125 | } |
| 2126 | #ifdef CONFIG_USER_ONLY |
| 2127 | if (gdbserver_fd < 0 || s->fd < 0) { |
| 2128 | return; |
| 2129 | } |
| 2130 | #endif |
| 2131 | |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2132 | trace_gdbstub_op_exiting((uint8_t)code); |
| 2133 | |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 2134 | snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code); |
| 2135 | put_packet(s, buf); |
Fabien Chouteau | e2af15b | 2011-01-13 12:46:57 +0100 | [diff] [blame] | 2136 | |
| 2137 | #ifndef CONFIG_USER_ONLY |
Marc-André Lureau | 1ce2610 | 2017-01-27 00:49:13 +0400 | [diff] [blame] | 2138 | qemu_chr_fe_deinit(&s->chr, true); |
Fabien Chouteau | e2af15b | 2011-01-13 12:46:57 +0100 | [diff] [blame] | 2139 | #endif |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 2140 | } |
| 2141 | |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 2142 | /* |
| 2143 | * Create the process that will contain all the "orphan" CPUs (that are not |
| 2144 | * part of a CPU cluster). Note that if this process contains no CPUs, it won't |
| 2145 | * be attachable and thus will be invisible to the user. |
| 2146 | */ |
| 2147 | static void create_default_process(GDBState *s) |
| 2148 | { |
| 2149 | GDBProcess *process; |
| 2150 | int max_pid = 0; |
| 2151 | |
| 2152 | if (s->process_num) { |
| 2153 | max_pid = s->processes[s->process_num - 1].pid; |
| 2154 | } |
| 2155 | |
| 2156 | s->processes = g_renew(GDBProcess, s->processes, ++s->process_num); |
| 2157 | process = &s->processes[s->process_num - 1]; |
| 2158 | |
| 2159 | /* We need an available PID slot for this process */ |
| 2160 | assert(max_pid < UINT32_MAX); |
| 2161 | |
| 2162 | process->pid = max_pid + 1; |
| 2163 | process->attached = false; |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2164 | process->target_xml[0] = '\0'; |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2167 | #ifdef CONFIG_USER_ONLY |
| 2168 | int |
Andreas Färber | db6b81d | 2013-06-27 19:49:31 +0200 | [diff] [blame] | 2169 | gdb_handlesig(CPUState *cpu, int sig) |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2170 | { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 2171 | GDBState *s; |
| 2172 | char buf[256]; |
| 2173 | int n; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2174 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 2175 | s = gdbserver_state; |
| 2176 | if (gdbserver_fd < 0 || s->fd < 0) { |
| 2177 | return sig; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 2180 | /* disable single step if it was enabled */ |
Andreas Färber | 3825b28 | 2013-06-24 18:41:06 +0200 | [diff] [blame] | 2181 | cpu_single_step(cpu, 0); |
Peter Crosthwaite | bbd77c1 | 2015-06-23 19:31:15 -0700 | [diff] [blame] | 2182 | tb_flush(cpu); |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2183 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 2184 | if (sig != 0) { |
| 2185 | snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig)); |
| 2186 | put_packet(s, buf); |
| 2187 | } |
| 2188 | /* put_packet() might have detected that the peer terminated the |
| 2189 | connection. */ |
| 2190 | if (s->fd < 0) { |
| 2191 | return sig; |
| 2192 | } |
| 2193 | |
| 2194 | sig = 0; |
| 2195 | s->state = RS_IDLE; |
| 2196 | s->running_state = 0; |
| 2197 | while (s->running_state == 0) { |
| 2198 | n = read(s->fd, buf, 256); |
| 2199 | if (n > 0) { |
| 2200 | int i; |
| 2201 | |
| 2202 | for (i = 0; i < n; i++) { |
| 2203 | gdb_read_byte(s, buf[i]); |
| 2204 | } |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 2205 | } else { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 2206 | /* XXX: Connection closed. Should probably wait for another |
| 2207 | connection before continuing. */ |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 2208 | if (n == 0) { |
| 2209 | close(s->fd); |
| 2210 | } |
| 2211 | s->fd = -1; |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 2212 | return sig; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2213 | } |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 2214 | } |
| 2215 | sig = s->signal; |
| 2216 | s->signal = 0; |
| 2217 | return sig; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2218 | } |
bellard | e900967 | 2005-04-26 20:42:36 +0000 | [diff] [blame] | 2219 | |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 2220 | /* 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] | 2221 | void gdb_signalled(CPUArchState *env, int sig) |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 2222 | { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 2223 | GDBState *s; |
| 2224 | char buf[4]; |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 2225 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 2226 | s = gdbserver_state; |
| 2227 | if (gdbserver_fd < 0 || s->fd < 0) { |
| 2228 | return; |
| 2229 | } |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 2230 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 2231 | snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig)); |
| 2232 | put_packet(s, buf); |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 2233 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 2234 | |
Peter Maydell | 2f65222 | 2018-05-14 18:30:44 +0100 | [diff] [blame] | 2235 | static bool gdb_accept(void) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2236 | { |
| 2237 | GDBState *s; |
| 2238 | struct sockaddr_in sockaddr; |
| 2239 | socklen_t len; |
MORITA Kazutaka | bf1c852 | 2013-02-22 12:39:50 +0900 | [diff] [blame] | 2240 | int fd; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2241 | |
| 2242 | for(;;) { |
| 2243 | len = sizeof(sockaddr); |
| 2244 | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len); |
| 2245 | if (fd < 0 && errno != EINTR) { |
| 2246 | perror("accept"); |
Peter Maydell | 2f65222 | 2018-05-14 18:30:44 +0100 | [diff] [blame] | 2247 | return false; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2248 | } else if (fd >= 0) { |
Peter Maydell | f5bdd78 | 2018-05-14 18:30:43 +0100 | [diff] [blame] | 2249 | qemu_set_cloexec(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2250 | break; |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | /* set short latency */ |
Peter Maydell | 2f65222 | 2018-05-14 18:30:44 +0100 | [diff] [blame] | 2255 | if (socket_set_nodelay(fd)) { |
| 2256 | perror("setsockopt"); |
Philippe Mathieu-Daudé | ead75d8 | 2018-05-24 19:34:58 -0300 | [diff] [blame] | 2257 | close(fd); |
Peter Maydell | 2f65222 | 2018-05-14 18:30:44 +0100 | [diff] [blame] | 2258 | return false; |
| 2259 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2260 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2261 | s = g_malloc0(sizeof(GDBState)); |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 2262 | create_default_process(s); |
Luc Michel | 970ed90 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2263 | s->processes[0].attached = true; |
| 2264 | s->c_cpu = gdb_first_attached_cpu(s); |
| 2265 | s->g_cpu = s->c_cpu; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2266 | s->fd = fd; |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 2267 | gdb_has_xml = false; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2268 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 2269 | gdbserver_state = s; |
Peter Maydell | 2f65222 | 2018-05-14 18:30:44 +0100 | [diff] [blame] | 2270 | return true; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | static int gdbserver_open(int port) |
| 2274 | { |
| 2275 | struct sockaddr_in sockaddr; |
Sebastian Ottlik | 6669ca1 | 2013-10-02 12:23:13 +0200 | [diff] [blame] | 2276 | int fd, ret; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2277 | |
| 2278 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 2279 | if (fd < 0) { |
| 2280 | perror("socket"); |
| 2281 | return -1; |
| 2282 | } |
Peter Maydell | f5bdd78 | 2018-05-14 18:30:43 +0100 | [diff] [blame] | 2283 | qemu_set_cloexec(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2284 | |
Sebastian Ottlik | 6669ca1 | 2013-10-02 12:23:13 +0200 | [diff] [blame] | 2285 | socket_set_fast_reuse(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2286 | |
| 2287 | sockaddr.sin_family = AF_INET; |
| 2288 | sockaddr.sin_port = htons(port); |
| 2289 | sockaddr.sin_addr.s_addr = 0; |
| 2290 | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
| 2291 | if (ret < 0) { |
| 2292 | perror("bind"); |
Peter Maydell | bb16172 | 2011-12-24 23:37:24 +0000 | [diff] [blame] | 2293 | close(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2294 | return -1; |
| 2295 | } |
Peter Wu | 96165b9 | 2016-05-04 11:32:17 +0200 | [diff] [blame] | 2296 | ret = listen(fd, 1); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2297 | if (ret < 0) { |
| 2298 | perror("listen"); |
Peter Maydell | bb16172 | 2011-12-24 23:37:24 +0000 | [diff] [blame] | 2299 | close(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2300 | return -1; |
| 2301 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 2302 | return fd; |
| 2303 | } |
| 2304 | |
| 2305 | int gdbserver_start(int port) |
| 2306 | { |
| 2307 | gdbserver_fd = gdbserver_open(port); |
| 2308 | if (gdbserver_fd < 0) |
| 2309 | return -1; |
| 2310 | /* accept connections */ |
Peter Maydell | 2f65222 | 2018-05-14 18:30:44 +0100 | [diff] [blame] | 2311 | if (!gdb_accept()) { |
| 2312 | close(gdbserver_fd); |
| 2313 | gdbserver_fd = -1; |
| 2314 | return -1; |
| 2315 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2316 | return 0; |
| 2317 | } |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 2318 | |
| 2319 | /* Disable gdb stub for child processes. */ |
Peter Crosthwaite | f7ec7f7 | 2015-06-23 19:31:16 -0700 | [diff] [blame] | 2320 | void gdbserver_fork(CPUState *cpu) |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 2321 | { |
| 2322 | GDBState *s = gdbserver_state; |
Andreas Färber | 75a3403 | 2013-09-02 16:57:02 +0200 | [diff] [blame] | 2323 | |
| 2324 | if (gdbserver_fd < 0 || s->fd < 0) { |
| 2325 | return; |
| 2326 | } |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 2327 | close(s->fd); |
| 2328 | s->fd = -1; |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 2329 | cpu_breakpoint_remove_all(cpu, BP_GDB); |
Andreas Färber | 75a3403 | 2013-09-02 16:57:02 +0200 | [diff] [blame] | 2330 | cpu_watchpoint_remove_all(cpu, BP_GDB); |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 2331 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2332 | #else |
ths | aa1f17c | 2007-07-11 22:48:58 +0000 | [diff] [blame] | 2333 | static int gdb_chr_can_receive(void *opaque) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2334 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 2335 | /* We can handle an arbitrarily large amount of data. |
| 2336 | Pick the maximum packet size, which is as good as anything. */ |
| 2337 | return MAX_PACKET_LENGTH; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
ths | aa1f17c | 2007-07-11 22:48:58 +0000 | [diff] [blame] | 2340 | static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2341 | { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2342 | int i; |
| 2343 | |
| 2344 | for (i = 0; i < size; i++) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 2345 | gdb_read_byte(gdbserver_state, buf[i]); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2346 | } |
| 2347 | } |
| 2348 | |
| 2349 | static void gdb_chr_event(void *opaque, int event) |
| 2350 | { |
Luc Michel | 970ed90 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2351 | int i; |
| 2352 | GDBState *s = (GDBState *) opaque; |
| 2353 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2354 | switch (event) { |
Amit Shah | b6b8df5 | 2009-10-07 18:31:16 +0530 | [diff] [blame] | 2355 | case CHR_EVENT_OPENED: |
Luc Michel | 970ed90 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2356 | /* Start with first process attached, others detached */ |
| 2357 | for (i = 0; i < s->process_num; i++) { |
| 2358 | s->processes[i].attached = !i; |
| 2359 | } |
| 2360 | |
| 2361 | s->c_cpu = gdb_first_attached_cpu(s); |
| 2362 | s->g_cpu = s->c_cpu; |
| 2363 | |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2364 | vm_stop(RUN_STATE_PAUSED); |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 2365 | gdb_has_xml = false; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2366 | break; |
| 2367 | default: |
| 2368 | break; |
| 2369 | } |
| 2370 | } |
| 2371 | |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 2372 | static void gdb_monitor_output(GDBState *s, const char *msg, int len) |
| 2373 | { |
| 2374 | char buf[MAX_PACKET_LENGTH]; |
| 2375 | |
| 2376 | buf[0] = 'O'; |
| 2377 | if (len > (MAX_PACKET_LENGTH/2) - 1) |
| 2378 | len = (MAX_PACKET_LENGTH/2) - 1; |
| 2379 | memtohex(buf + 1, (uint8_t *)msg, len); |
| 2380 | put_packet(s, buf); |
| 2381 | } |
| 2382 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 2383 | static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len) |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 2384 | { |
| 2385 | const char *p = (const char *)buf; |
| 2386 | int max_sz; |
| 2387 | |
| 2388 | max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2; |
| 2389 | for (;;) { |
| 2390 | if (len <= max_sz) { |
| 2391 | gdb_monitor_output(gdbserver_state, p, len); |
| 2392 | break; |
| 2393 | } |
| 2394 | gdb_monitor_output(gdbserver_state, p, max_sz); |
| 2395 | p += max_sz; |
| 2396 | len -= max_sz; |
| 2397 | } |
| 2398 | return len; |
| 2399 | } |
| 2400 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 2401 | #ifndef _WIN32 |
| 2402 | static void gdb_sigterm_handler(int signal) |
| 2403 | { |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 2404 | if (runstate_is_running()) { |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 2405 | vm_stop(RUN_STATE_PAUSED); |
Jan Kiszka | e07bbac | 2011-02-09 16:29:40 +0100 | [diff] [blame] | 2406 | } |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 2407 | } |
| 2408 | #endif |
| 2409 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 2410 | static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend, |
| 2411 | bool *be_opened, Error **errp) |
| 2412 | { |
| 2413 | *be_opened = false; |
| 2414 | } |
| 2415 | |
| 2416 | static void char_gdb_class_init(ObjectClass *oc, void *data) |
| 2417 | { |
| 2418 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 2419 | |
| 2420 | cc->internal = true; |
| 2421 | cc->open = gdb_monitor_open; |
| 2422 | cc->chr_write = gdb_monitor_write; |
| 2423 | } |
| 2424 | |
| 2425 | #define TYPE_CHARDEV_GDB "chardev-gdb" |
| 2426 | |
| 2427 | static const TypeInfo char_gdb_type_info = { |
| 2428 | .name = TYPE_CHARDEV_GDB, |
| 2429 | .parent = TYPE_CHARDEV, |
| 2430 | .class_init = char_gdb_class_init, |
| 2431 | }; |
| 2432 | |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 2433 | static int find_cpu_clusters(Object *child, void *opaque) |
| 2434 | { |
| 2435 | if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) { |
| 2436 | GDBState *s = (GDBState *) opaque; |
| 2437 | CPUClusterState *cluster = CPU_CLUSTER(child); |
| 2438 | GDBProcess *process; |
| 2439 | |
| 2440 | s->processes = g_renew(GDBProcess, s->processes, ++s->process_num); |
| 2441 | |
| 2442 | process = &s->processes[s->process_num - 1]; |
| 2443 | |
| 2444 | /* |
| 2445 | * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at |
| 2446 | * runtime, we enforce here that the machine does not use a cluster ID |
| 2447 | * that would lead to PID 0. |
| 2448 | */ |
| 2449 | assert(cluster->cluster_id != UINT32_MAX); |
| 2450 | process->pid = cluster->cluster_id + 1; |
| 2451 | process->attached = false; |
Luc Michel | c145eea | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2452 | process->target_xml[0] = '\0'; |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 2453 | |
| 2454 | return 0; |
| 2455 | } |
| 2456 | |
| 2457 | return object_child_foreach(child, find_cpu_clusters, opaque); |
| 2458 | } |
| 2459 | |
| 2460 | static int pid_order(const void *a, const void *b) |
| 2461 | { |
| 2462 | GDBProcess *pa = (GDBProcess *) a; |
| 2463 | GDBProcess *pb = (GDBProcess *) b; |
| 2464 | |
| 2465 | if (pa->pid < pb->pid) { |
| 2466 | return -1; |
| 2467 | } else if (pa->pid > pb->pid) { |
| 2468 | return 1; |
| 2469 | } else { |
| 2470 | return 0; |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2474 | static void create_processes(GDBState *s) |
| 2475 | { |
| 2476 | object_child_foreach(object_get_root(), find_cpu_clusters, s); |
| 2477 | |
| 2478 | if (s->processes) { |
| 2479 | /* Sort by PID */ |
| 2480 | qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order); |
| 2481 | } |
| 2482 | |
| 2483 | create_default_process(s); |
| 2484 | } |
| 2485 | |
| 2486 | static void cleanup_processes(GDBState *s) |
| 2487 | { |
| 2488 | g_free(s->processes); |
| 2489 | s->process_num = 0; |
| 2490 | s->processes = NULL; |
| 2491 | } |
| 2492 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 2493 | int gdbserver_start(const char *device) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2494 | { |
Doug Gale | 5c9522b | 2017-12-02 20:30:37 -0500 | [diff] [blame] | 2495 | trace_gdbstub_op_start(device); |
| 2496 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2497 | GDBState *s; |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 2498 | char gdbstub_device_name[128]; |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 2499 | Chardev *chr = NULL; |
| 2500 | Chardev *mon_chr; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2501 | |
Ziyue Yang | 508b4ec | 2017-01-18 16:02:41 +0800 | [diff] [blame] | 2502 | if (!first_cpu) { |
| 2503 | error_report("gdbstub: meaningless to attach gdb to a " |
| 2504 | "machine without any CPU."); |
| 2505 | return -1; |
| 2506 | } |
| 2507 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 2508 | if (!device) |
| 2509 | return -1; |
| 2510 | if (strcmp(device, "none") != 0) { |
| 2511 | if (strstart(device, "tcp:", NULL)) { |
| 2512 | /* enforce required TCP attributes */ |
| 2513 | snprintf(gdbstub_device_name, sizeof(gdbstub_device_name), |
| 2514 | "%s,nowait,nodelay,server", device); |
| 2515 | device = gdbstub_device_name; |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 2516 | } |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 2517 | #ifndef _WIN32 |
| 2518 | else if (strcmp(device, "stdio") == 0) { |
| 2519 | struct sigaction act; |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 2520 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 2521 | memset(&act, 0, sizeof(act)); |
| 2522 | act.sa_handler = gdb_sigterm_handler; |
| 2523 | sigaction(SIGINT, &act, NULL); |
| 2524 | } |
| 2525 | #endif |
Marc-André Lureau | 95e30b2 | 2018-08-22 19:19:42 +0200 | [diff] [blame] | 2526 | /* |
| 2527 | * FIXME: it's a bit weird to allow using a mux chardev here |
| 2528 | * and implicitly setup a monitor. We may want to break this. |
| 2529 | */ |
| 2530 | chr = qemu_chr_new_noreplay("gdb", device, true); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 2531 | if (!chr) |
| 2532 | return -1; |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 2535 | s = gdbserver_state; |
| 2536 | if (!s) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2537 | s = g_malloc0(sizeof(GDBState)); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 2538 | gdbserver_state = s; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2539 | |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 2540 | qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL); |
| 2541 | |
| 2542 | /* Initialize a monitor terminal for gdb */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 2543 | mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB, |
| 2544 | NULL, &error_abort); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 2545 | monitor_init(mon_chr, 0); |
| 2546 | } else { |
Marc-André Lureau | 1ce2610 | 2017-01-27 00:49:13 +0400 | [diff] [blame] | 2547 | qemu_chr_fe_deinit(&s->chr, true); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 2548 | mon_chr = s->mon_chr; |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 2549 | cleanup_processes(s); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 2550 | memset(s, 0, sizeof(GDBState)); |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 2551 | s->mon_chr = mon_chr; |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 2552 | } |
Luc Michel | 8f46863 | 2019-01-07 15:23:45 +0000 | [diff] [blame] | 2553 | |
| 2554 | create_processes(s); |
| 2555 | |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 2556 | if (chr) { |
| 2557 | qemu_chr_fe_init(&s->chr, chr, &error_abort); |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 2558 | qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive, |
Luc Michel | 970ed90 | 2019-01-07 15:23:46 +0000 | [diff] [blame] | 2559 | gdb_chr_event, NULL, s, NULL, true); |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 2560 | } |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 2561 | s->state = chr ? RS_IDLE : RS_INACTIVE; |
| 2562 | s->mon_chr = mon_chr; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2563 | s->current_syscall_cb = NULL; |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 2564 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2565 | return 0; |
| 2566 | } |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 2567 | |
KONRAD Frederic | 1bb982b | 2018-03-20 10:39:33 +0100 | [diff] [blame] | 2568 | void gdbserver_cleanup(void) |
| 2569 | { |
| 2570 | if (gdbserver_state) { |
| 2571 | put_packet(gdbserver_state, "W00"); |
| 2572 | } |
| 2573 | } |
| 2574 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 2575 | static void register_types(void) |
| 2576 | { |
| 2577 | type_register_static(&char_gdb_type_info); |
| 2578 | } |
| 2579 | |
| 2580 | type_init(register_types); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2581 | #endif |