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" |
Paolo Bonzini | 33c1187 | 2016-03-15 16:58:45 +0100 | [diff] [blame] | 23 | #include "cpu.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" |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 32 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 33 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 34 | #define MAX_PACKET_LENGTH 4096 |
| 35 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 36 | #include "qemu/sockets.h" |
Vincent Palatin | b394662 | 2017-01-10 11:59:55 +0100 | [diff] [blame] | 37 | #include "sysemu/hw_accel.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 38 | #include "sysemu/kvm.h" |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 39 | #include "exec/semihost.h" |
Paolo Bonzini | 63c9155 | 2016-03-15 13:18:37 +0100 | [diff] [blame] | 40 | #include "exec/exec-all.h" |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 41 | |
Jan Kiszka | a391938 | 2015-02-07 09:38:44 +0100 | [diff] [blame] | 42 | #ifdef CONFIG_USER_ONLY |
| 43 | #define GDB_ATTACHED "0" |
| 44 | #else |
| 45 | #define GDB_ATTACHED "1" |
| 46 | #endif |
| 47 | |
Andreas Färber | f3659ee | 2013-06-27 19:09:09 +0200 | [diff] [blame] | 48 | static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr, |
| 49 | uint8_t *buf, int len, bool is_write) |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 50 | { |
Andreas Färber | f3659ee | 2013-06-27 19:09:09 +0200 | [diff] [blame] | 51 | CPUClass *cc = CPU_GET_CLASS(cpu); |
| 52 | |
| 53 | if (cc->memory_rw_debug) { |
| 54 | return cc->memory_rw_debug(cpu, addr, buf, len, is_write); |
| 55 | } |
| 56 | return cpu_memory_rw_debug(cpu, addr, buf, len, is_write); |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 57 | } |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 58 | |
Alex Bennée | d2a6c85 | 2017-07-12 11:52:14 +0100 | [diff] [blame] | 59 | /* Return the GDB index for a given vCPU state. |
| 60 | * |
| 61 | * For user mode this is simply the thread id. In system mode GDB |
| 62 | * numbers CPUs from 1 as 0 is reserved as an "any cpu" index. |
| 63 | */ |
| 64 | static inline int cpu_gdb_index(CPUState *cpu) |
| 65 | { |
| 66 | #if defined(CONFIG_USER_ONLY) |
Alex Bennée | bd88c78 | 2017-07-12 11:52:15 +0100 | [diff] [blame] | 67 | TaskState *ts = (TaskState *) cpu->opaque; |
| 68 | return ts->ts_tid; |
Alex Bennée | d2a6c85 | 2017-07-12 11:52:14 +0100 | [diff] [blame] | 69 | #else |
| 70 | return cpu->cpu_index + 1; |
| 71 | #endif |
| 72 | } |
| 73 | |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 74 | enum { |
| 75 | GDB_SIGNAL_0 = 0, |
| 76 | GDB_SIGNAL_INT = 2, |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 77 | GDB_SIGNAL_QUIT = 3, |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 78 | GDB_SIGNAL_TRAP = 5, |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 79 | GDB_SIGNAL_ABRT = 6, |
| 80 | GDB_SIGNAL_ALRM = 14, |
| 81 | GDB_SIGNAL_IO = 23, |
| 82 | GDB_SIGNAL_XCPU = 24, |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 83 | GDB_SIGNAL_UNKNOWN = 143 |
| 84 | }; |
| 85 | |
| 86 | #ifdef CONFIG_USER_ONLY |
| 87 | |
| 88 | /* Map target signal numbers to GDB protocol signal numbers and vice |
| 89 | * versa. For user emulation's currently supported systems, we can |
| 90 | * assume most signals are defined. |
| 91 | */ |
| 92 | |
| 93 | static int gdb_signal_table[] = { |
| 94 | 0, |
| 95 | TARGET_SIGHUP, |
| 96 | TARGET_SIGINT, |
| 97 | TARGET_SIGQUIT, |
| 98 | TARGET_SIGILL, |
| 99 | TARGET_SIGTRAP, |
| 100 | TARGET_SIGABRT, |
| 101 | -1, /* SIGEMT */ |
| 102 | TARGET_SIGFPE, |
| 103 | TARGET_SIGKILL, |
| 104 | TARGET_SIGBUS, |
| 105 | TARGET_SIGSEGV, |
| 106 | TARGET_SIGSYS, |
| 107 | TARGET_SIGPIPE, |
| 108 | TARGET_SIGALRM, |
| 109 | TARGET_SIGTERM, |
| 110 | TARGET_SIGURG, |
| 111 | TARGET_SIGSTOP, |
| 112 | TARGET_SIGTSTP, |
| 113 | TARGET_SIGCONT, |
| 114 | TARGET_SIGCHLD, |
| 115 | TARGET_SIGTTIN, |
| 116 | TARGET_SIGTTOU, |
| 117 | TARGET_SIGIO, |
| 118 | TARGET_SIGXCPU, |
| 119 | TARGET_SIGXFSZ, |
| 120 | TARGET_SIGVTALRM, |
| 121 | TARGET_SIGPROF, |
| 122 | TARGET_SIGWINCH, |
| 123 | -1, /* SIGLOST */ |
| 124 | TARGET_SIGUSR1, |
| 125 | TARGET_SIGUSR2, |
blueswir1 | c72d5bf | 2009-01-15 17:27:45 +0000 | [diff] [blame] | 126 | #ifdef TARGET_SIGPWR |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 127 | TARGET_SIGPWR, |
blueswir1 | c72d5bf | 2009-01-15 17:27:45 +0000 | [diff] [blame] | 128 | #else |
| 129 | -1, |
| 130 | #endif |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 131 | -1, /* SIGPOLL */ |
| 132 | -1, |
| 133 | -1, |
| 134 | -1, |
| 135 | -1, |
| 136 | -1, |
| 137 | -1, |
| 138 | -1, |
| 139 | -1, |
| 140 | -1, |
| 141 | -1, |
| 142 | -1, |
blueswir1 | c72d5bf | 2009-01-15 17:27:45 +0000 | [diff] [blame] | 143 | #ifdef __SIGRTMIN |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 144 | __SIGRTMIN + 1, |
| 145 | __SIGRTMIN + 2, |
| 146 | __SIGRTMIN + 3, |
| 147 | __SIGRTMIN + 4, |
| 148 | __SIGRTMIN + 5, |
| 149 | __SIGRTMIN + 6, |
| 150 | __SIGRTMIN + 7, |
| 151 | __SIGRTMIN + 8, |
| 152 | __SIGRTMIN + 9, |
| 153 | __SIGRTMIN + 10, |
| 154 | __SIGRTMIN + 11, |
| 155 | __SIGRTMIN + 12, |
| 156 | __SIGRTMIN + 13, |
| 157 | __SIGRTMIN + 14, |
| 158 | __SIGRTMIN + 15, |
| 159 | __SIGRTMIN + 16, |
| 160 | __SIGRTMIN + 17, |
| 161 | __SIGRTMIN + 18, |
| 162 | __SIGRTMIN + 19, |
| 163 | __SIGRTMIN + 20, |
| 164 | __SIGRTMIN + 21, |
| 165 | __SIGRTMIN + 22, |
| 166 | __SIGRTMIN + 23, |
| 167 | __SIGRTMIN + 24, |
| 168 | __SIGRTMIN + 25, |
| 169 | __SIGRTMIN + 26, |
| 170 | __SIGRTMIN + 27, |
| 171 | __SIGRTMIN + 28, |
| 172 | __SIGRTMIN + 29, |
| 173 | __SIGRTMIN + 30, |
| 174 | __SIGRTMIN + 31, |
| 175 | -1, /* SIGCANCEL */ |
| 176 | __SIGRTMIN, |
| 177 | __SIGRTMIN + 32, |
| 178 | __SIGRTMIN + 33, |
| 179 | __SIGRTMIN + 34, |
| 180 | __SIGRTMIN + 35, |
| 181 | __SIGRTMIN + 36, |
| 182 | __SIGRTMIN + 37, |
| 183 | __SIGRTMIN + 38, |
| 184 | __SIGRTMIN + 39, |
| 185 | __SIGRTMIN + 40, |
| 186 | __SIGRTMIN + 41, |
| 187 | __SIGRTMIN + 42, |
| 188 | __SIGRTMIN + 43, |
| 189 | __SIGRTMIN + 44, |
| 190 | __SIGRTMIN + 45, |
| 191 | __SIGRTMIN + 46, |
| 192 | __SIGRTMIN + 47, |
| 193 | __SIGRTMIN + 48, |
| 194 | __SIGRTMIN + 49, |
| 195 | __SIGRTMIN + 50, |
| 196 | __SIGRTMIN + 51, |
| 197 | __SIGRTMIN + 52, |
| 198 | __SIGRTMIN + 53, |
| 199 | __SIGRTMIN + 54, |
| 200 | __SIGRTMIN + 55, |
| 201 | __SIGRTMIN + 56, |
| 202 | __SIGRTMIN + 57, |
| 203 | __SIGRTMIN + 58, |
| 204 | __SIGRTMIN + 59, |
| 205 | __SIGRTMIN + 60, |
| 206 | __SIGRTMIN + 61, |
| 207 | __SIGRTMIN + 62, |
| 208 | __SIGRTMIN + 63, |
| 209 | __SIGRTMIN + 64, |
| 210 | __SIGRTMIN + 65, |
| 211 | __SIGRTMIN + 66, |
| 212 | __SIGRTMIN + 67, |
| 213 | __SIGRTMIN + 68, |
| 214 | __SIGRTMIN + 69, |
| 215 | __SIGRTMIN + 70, |
| 216 | __SIGRTMIN + 71, |
| 217 | __SIGRTMIN + 72, |
| 218 | __SIGRTMIN + 73, |
| 219 | __SIGRTMIN + 74, |
| 220 | __SIGRTMIN + 75, |
| 221 | __SIGRTMIN + 76, |
| 222 | __SIGRTMIN + 77, |
| 223 | __SIGRTMIN + 78, |
| 224 | __SIGRTMIN + 79, |
| 225 | __SIGRTMIN + 80, |
| 226 | __SIGRTMIN + 81, |
| 227 | __SIGRTMIN + 82, |
| 228 | __SIGRTMIN + 83, |
| 229 | __SIGRTMIN + 84, |
| 230 | __SIGRTMIN + 85, |
| 231 | __SIGRTMIN + 86, |
| 232 | __SIGRTMIN + 87, |
| 233 | __SIGRTMIN + 88, |
| 234 | __SIGRTMIN + 89, |
| 235 | __SIGRTMIN + 90, |
| 236 | __SIGRTMIN + 91, |
| 237 | __SIGRTMIN + 92, |
| 238 | __SIGRTMIN + 93, |
| 239 | __SIGRTMIN + 94, |
| 240 | __SIGRTMIN + 95, |
| 241 | -1, /* SIGINFO */ |
| 242 | -1, /* UNKNOWN */ |
| 243 | -1, /* DEFAULT */ |
| 244 | -1, |
| 245 | -1, |
| 246 | -1, |
| 247 | -1, |
| 248 | -1, |
| 249 | -1 |
blueswir1 | c72d5bf | 2009-01-15 17:27:45 +0000 | [diff] [blame] | 250 | #endif |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 251 | }; |
bellard | 8f447cc | 2006-06-14 15:21:14 +0000 | [diff] [blame] | 252 | #else |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 253 | /* In system mode we only need SIGINT and SIGTRAP; other signals |
| 254 | are not yet supported. */ |
| 255 | |
| 256 | enum { |
| 257 | TARGET_SIGINT = 2, |
| 258 | TARGET_SIGTRAP = 5 |
| 259 | }; |
| 260 | |
| 261 | static int gdb_signal_table[] = { |
| 262 | -1, |
| 263 | -1, |
| 264 | TARGET_SIGINT, |
| 265 | -1, |
| 266 | -1, |
| 267 | TARGET_SIGTRAP |
| 268 | }; |
bellard | 8f447cc | 2006-06-14 15:21:14 +0000 | [diff] [blame] | 269 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 270 | |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 271 | #ifdef CONFIG_USER_ONLY |
| 272 | static int target_signal_to_gdb (int sig) |
| 273 | { |
| 274 | int i; |
| 275 | for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++) |
| 276 | if (gdb_signal_table[i] == sig) |
| 277 | return i; |
| 278 | return GDB_SIGNAL_UNKNOWN; |
| 279 | } |
| 280 | #endif |
| 281 | |
| 282 | static int gdb_signal_to_target (int sig) |
| 283 | { |
| 284 | if (sig < ARRAY_SIZE (gdb_signal_table)) |
| 285 | return gdb_signal_table[sig]; |
| 286 | else |
| 287 | return -1; |
| 288 | } |
| 289 | |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 290 | /* #define DEBUG_GDB */ |
| 291 | |
| 292 | #ifdef DEBUG_GDB |
| 293 | # define DEBUG_GDB_GATE 1 |
| 294 | #else |
| 295 | # define DEBUG_GDB_GATE 0 |
| 296 | #endif |
| 297 | |
| 298 | #define gdb_debug(fmt, ...) do { \ |
| 299 | if (DEBUG_GDB_GATE) { \ |
| 300 | fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \ |
| 301 | } \ |
| 302 | } while (0) |
| 303 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 304 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 305 | typedef struct GDBRegisterState { |
| 306 | int base_reg; |
| 307 | int num_regs; |
| 308 | gdb_reg_cb get_reg; |
| 309 | gdb_reg_cb set_reg; |
| 310 | const char *xml; |
| 311 | struct GDBRegisterState *next; |
| 312 | } GDBRegisterState; |
| 313 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 314 | enum RSState { |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 315 | RS_INACTIVE, |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 316 | RS_IDLE, |
| 317 | RS_GETLINE, |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 318 | RS_GETLINE_ESC, |
| 319 | RS_GETLINE_RLE, |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 320 | RS_CHKSUM1, |
| 321 | RS_CHKSUM2, |
| 322 | }; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 323 | typedef struct GDBState { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 324 | CPUState *c_cpu; /* current CPU for step/continue ops */ |
| 325 | CPUState *g_cpu; /* current CPU for other ops */ |
Andreas Färber | 52f3462 | 2013-06-27 13:44:40 +0200 | [diff] [blame] | 326 | CPUState *query_cpu; /* for q{f|s}ThreadInfo */ |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 327 | enum RSState state; /* parsing state */ |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 328 | char line_buf[MAX_PACKET_LENGTH]; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 329 | int line_buf_index; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 330 | int line_sum; /* running checksum */ |
| 331 | int line_csum; /* checksum at the end of the packet */ |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 332 | uint8_t last_packet[MAX_PACKET_LENGTH + 4]; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 333 | int last_packet_len; |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 334 | int signal; |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 335 | #ifdef CONFIG_USER_ONLY |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 336 | int fd; |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 337 | int running_state; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 338 | #else |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 339 | CharBackend chr; |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 340 | Chardev *mon_chr; |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 341 | #endif |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 342 | char syscall_buf[256]; |
| 343 | gdb_syscall_complete_cb current_syscall_cb; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 344 | } GDBState; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 345 | |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 346 | /* By default use no IRQs and no timers while single stepping so as to |
| 347 | * make single stepping like an ICE HW step. |
| 348 | */ |
| 349 | static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER; |
| 350 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 351 | static GDBState *gdbserver_state; |
| 352 | |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 353 | bool gdb_has_xml; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 354 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 355 | #ifdef CONFIG_USER_ONLY |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 356 | /* XXX: This is not thread safe. Do we care? */ |
| 357 | static int gdbserver_fd = -1; |
| 358 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 359 | static int get_char(GDBState *s) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 360 | { |
| 361 | uint8_t ch; |
| 362 | int ret; |
| 363 | |
| 364 | for(;;) { |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 365 | ret = qemu_recv(s->fd, &ch, 1, 0); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 366 | if (ret < 0) { |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 367 | if (errno == ECONNRESET) |
| 368 | s->fd = -1; |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 369 | if (errno != EINTR) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 370 | return -1; |
| 371 | } else if (ret == 0) { |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 372 | close(s->fd); |
| 373 | s->fd = -1; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 374 | return -1; |
| 375 | } else { |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | return ch; |
| 380 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 381 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 382 | |
blueswir1 | 654efcf | 2009-04-18 07:29:59 +0000 | [diff] [blame] | 383 | static enum { |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 384 | GDB_SYS_UNKNOWN, |
| 385 | GDB_SYS_ENABLED, |
| 386 | GDB_SYS_DISABLED, |
| 387 | } gdb_syscall_mode; |
| 388 | |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 389 | /* Decide if either remote gdb syscalls or native file IO should be used. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 390 | int use_gdb_syscalls(void) |
| 391 | { |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 392 | SemihostingTarget target = semihosting_get_target(); |
| 393 | if (target == SEMIHOSTING_TARGET_NATIVE) { |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 394 | /* -semihosting-config target=native */ |
| 395 | return false; |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 396 | } else if (target == SEMIHOSTING_TARGET_GDB) { |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 397 | /* -semihosting-config target=gdb */ |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | /* -semihosting-config target=auto */ |
| 402 | /* On the first call check if gdb is connected and remember. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 403 | if (gdb_syscall_mode == GDB_SYS_UNKNOWN) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 404 | gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED |
| 405 | : GDB_SYS_DISABLED); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 406 | } |
| 407 | return gdb_syscall_mode == GDB_SYS_ENABLED; |
| 408 | } |
| 409 | |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 410 | /* Resume execution. */ |
| 411 | static inline void gdb_continue(GDBState *s) |
| 412 | { |
| 413 | #ifdef CONFIG_USER_ONLY |
| 414 | s->running_state = 1; |
| 415 | #else |
Paolo Bonzini | 26ac7a3 | 2013-06-03 17:06:54 +0200 | [diff] [blame] | 416 | if (!runstate_needs_reset()) { |
Paolo Bonzini | 87f25c1 | 2013-05-30 13:20:40 +0200 | [diff] [blame] | 417 | vm_start(); |
| 418 | } |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 419 | #endif |
| 420 | } |
| 421 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 422 | /* |
| 423 | * Resume execution, per CPU actions. For user-mode emulation it's |
| 424 | * equivalent to gdb_continue. |
| 425 | */ |
| 426 | static int gdb_continue_partial(GDBState *s, char *newstates) |
| 427 | { |
| 428 | CPUState *cpu; |
| 429 | int res = 0; |
| 430 | #ifdef CONFIG_USER_ONLY |
| 431 | /* |
| 432 | * This is not exactly accurate, but it's an improvement compared to the |
| 433 | * previous situation, where only one CPU would be single-stepped. |
| 434 | */ |
| 435 | CPU_FOREACH(cpu) { |
| 436 | if (newstates[cpu->cpu_index] == 's') { |
| 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': |
| 455 | cpu_single_step(cpu, sstep_flags); |
| 456 | cpu_resume(cpu); |
| 457 | flag = 1; |
| 458 | break; |
| 459 | case 'c': |
| 460 | cpu_resume(cpu); |
| 461 | flag = 1; |
| 462 | break; |
| 463 | default: |
| 464 | res = -1; |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | if (flag) { |
| 470 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); |
| 471 | } |
| 472 | #endif |
| 473 | return res; |
| 474 | } |
| 475 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 476 | static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 477 | { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 478 | #ifdef CONFIG_USER_ONLY |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 479 | int ret; |
| 480 | |
| 481 | while (len > 0) { |
bellard | 8f447cc | 2006-06-14 15:21:14 +0000 | [diff] [blame] | 482 | ret = send(s->fd, buf, len, 0); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 483 | if (ret < 0) { |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 484 | if (errno != EINTR) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 485 | return; |
| 486 | } else { |
| 487 | buf += ret; |
| 488 | len -= ret; |
| 489 | } |
| 490 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 491 | #else |
Daniel P. Berrange | 6ab3fc3 | 2016-09-06 14:56:04 +0100 | [diff] [blame] | 492 | /* XXX this blocks entire thread. Rewrite to use |
| 493 | * qemu_chr_fe_write and background I/O callbacks */ |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 494 | qemu_chr_fe_write_all(&s->chr, buf, len); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 495 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static inline int fromhex(int v) |
| 499 | { |
| 500 | if (v >= '0' && v <= '9') |
| 501 | return v - '0'; |
| 502 | else if (v >= 'A' && v <= 'F') |
| 503 | return v - 'A' + 10; |
| 504 | else if (v >= 'a' && v <= 'f') |
| 505 | return v - 'a' + 10; |
| 506 | else |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static inline int tohex(int v) |
| 511 | { |
| 512 | if (v < 10) |
| 513 | return v + '0'; |
| 514 | else |
| 515 | return v - 10 + 'a'; |
| 516 | } |
| 517 | |
| 518 | static void memtohex(char *buf, const uint8_t *mem, int len) |
| 519 | { |
| 520 | int i, c; |
| 521 | char *q; |
| 522 | q = buf; |
| 523 | for(i = 0; i < len; i++) { |
| 524 | c = mem[i]; |
| 525 | *q++ = tohex(c >> 4); |
| 526 | *q++ = tohex(c & 0xf); |
| 527 | } |
| 528 | *q = '\0'; |
| 529 | } |
| 530 | |
| 531 | static void hextomem(uint8_t *mem, const char *buf, int len) |
| 532 | { |
| 533 | int i; |
| 534 | |
| 535 | for(i = 0; i < len; i++) { |
| 536 | mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); |
| 537 | buf += 2; |
| 538 | } |
| 539 | } |
| 540 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 541 | /* return -1 if error, 0 if OK */ |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 542 | static int put_packet_binary(GDBState *s, const char *buf, int len) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 543 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 544 | int csum, i; |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 545 | uint8_t *p; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 546 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 547 | for(;;) { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 548 | p = s->last_packet; |
| 549 | *(p++) = '$'; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 550 | memcpy(p, buf, len); |
| 551 | p += len; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 552 | csum = 0; |
| 553 | for(i = 0; i < len; i++) { |
| 554 | csum += buf[i]; |
| 555 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 556 | *(p++) = '#'; |
| 557 | *(p++) = tohex((csum >> 4) & 0xf); |
| 558 | *(p++) = tohex((csum) & 0xf); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 559 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 560 | s->last_packet_len = p - s->last_packet; |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 561 | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 562 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 563 | #ifdef CONFIG_USER_ONLY |
| 564 | i = get_char(s); |
| 565 | if (i < 0) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 566 | return -1; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 567 | if (i == '+') |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 568 | break; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 569 | #else |
| 570 | break; |
| 571 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 572 | } |
| 573 | return 0; |
| 574 | } |
| 575 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 576 | /* return -1 if error, 0 if OK */ |
| 577 | static int put_packet(GDBState *s, const char *buf) |
| 578 | { |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 579 | gdb_debug("reply='%s'\n", buf); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 580 | |
| 581 | return put_packet_binary(s, buf, strlen(buf)); |
| 582 | } |
| 583 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 584 | /* Encode data using the encoding for 'x' packets. */ |
| 585 | static int memtox(char *buf, const char *mem, int len) |
| 586 | { |
| 587 | char *p = buf; |
| 588 | char c; |
| 589 | |
| 590 | while (len--) { |
| 591 | c = *(mem++); |
| 592 | switch (c) { |
| 593 | case '#': case '$': case '*': case '}': |
| 594 | *(p++) = '}'; |
| 595 | *(p++) = c ^ 0x20; |
| 596 | break; |
| 597 | default: |
| 598 | *(p++) = c; |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | return p - buf; |
| 603 | } |
| 604 | |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 605 | static const char *get_feature_xml(const char *p, const char **newp, |
| 606 | CPUClass *cc) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 607 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 608 | size_t len; |
| 609 | int i; |
| 610 | const char *name; |
| 611 | static char target_xml[1024]; |
| 612 | |
| 613 | len = 0; |
| 614 | while (p[len] && p[len] != ':') |
| 615 | len++; |
| 616 | *newp = p + len; |
| 617 | |
| 618 | name = NULL; |
| 619 | if (strncmp(p, "target.xml", len) == 0) { |
| 620 | /* Generate the XML description for this CPU. */ |
| 621 | if (!target_xml[0]) { |
| 622 | GDBRegisterState *r; |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 623 | CPUState *cpu = first_cpu; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 624 | |
David Hildenbrand | b3820e6 | 2015-12-03 13:14:41 +0100 | [diff] [blame] | 625 | pstrcat(target_xml, sizeof(target_xml), |
| 626 | "<?xml version=\"1.0\"?>" |
| 627 | "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">" |
| 628 | "<target>"); |
| 629 | if (cc->gdb_arch_name) { |
| 630 | gchar *arch = cc->gdb_arch_name(cpu); |
| 631 | pstrcat(target_xml, sizeof(target_xml), "<architecture>"); |
| 632 | pstrcat(target_xml, sizeof(target_xml), arch); |
| 633 | pstrcat(target_xml, sizeof(target_xml), "</architecture>"); |
| 634 | g_free(arch); |
| 635 | } |
| 636 | pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\""); |
| 637 | pstrcat(target_xml, sizeof(target_xml), cc->gdb_core_xml_file); |
| 638 | pstrcat(target_xml, sizeof(target_xml), "\"/>"); |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 639 | for (r = cpu->gdb_regs; r; r = r->next) { |
blueswir1 | 2dc766d | 2009-04-13 16:06:19 +0000 | [diff] [blame] | 640 | pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\""); |
| 641 | pstrcat(target_xml, sizeof(target_xml), r->xml); |
| 642 | pstrcat(target_xml, sizeof(target_xml), "\"/>"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 643 | } |
blueswir1 | 2dc766d | 2009-04-13 16:06:19 +0000 | [diff] [blame] | 644 | pstrcat(target_xml, sizeof(target_xml), "</target>"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 645 | } |
| 646 | return target_xml; |
| 647 | } |
| 648 | for (i = 0; ; i++) { |
| 649 | name = xml_builtin[i][0]; |
| 650 | if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len)) |
| 651 | break; |
| 652 | } |
| 653 | return name ? xml_builtin[i][1] : NULL; |
| 654 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 655 | |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 656 | static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 657 | { |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 658 | CPUClass *cc = CPU_GET_CLASS(cpu); |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 659 | CPUArchState *env = cpu->env_ptr; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 660 | GDBRegisterState *r; |
| 661 | |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 662 | if (reg < cc->gdb_num_core_regs) { |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 663 | return cc->gdb_read_register(cpu, mem_buf, reg); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 664 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 665 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 666 | for (r = cpu->gdb_regs; r; r = r->next) { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 667 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
| 668 | return r->get_reg(env, mem_buf, reg - r->base_reg); |
| 669 | } |
| 670 | } |
| 671 | return 0; |
| 672 | } |
| 673 | |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 674 | static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 675 | { |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 676 | CPUClass *cc = CPU_GET_CLASS(cpu); |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 677 | CPUArchState *env = cpu->env_ptr; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 678 | GDBRegisterState *r; |
| 679 | |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 680 | if (reg < cc->gdb_num_core_regs) { |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 681 | return cc->gdb_write_register(cpu, mem_buf, reg); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 682 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 683 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 684 | for (r = cpu->gdb_regs; r; r = r->next) { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 685 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
| 686 | return r->set_reg(env, mem_buf, reg - r->base_reg); |
| 687 | } |
| 688 | } |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | /* Register a supplemental set of CPU registers. If g_pos is nonzero it |
| 693 | specifies the first register number and these registers are included in |
| 694 | a standard "g" packet. Direction is relative to gdb, i.e. get_reg is |
| 695 | gdb reading a CPU register, and set_reg is gdb modifying a CPU register. |
| 696 | */ |
| 697 | |
Andreas Färber | 22169d4 | 2013-06-28 21:27:39 +0200 | [diff] [blame] | 698 | void gdb_register_coprocessor(CPUState *cpu, |
| 699 | gdb_reg_cb get_reg, gdb_reg_cb set_reg, |
| 700 | int num_regs, const char *xml, int g_pos) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 701 | { |
| 702 | GDBRegisterState *s; |
| 703 | GDBRegisterState **p; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 704 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 705 | p = &cpu->gdb_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 706 | while (*p) { |
| 707 | /* Check for duplicates. */ |
| 708 | if (strcmp((*p)->xml, xml) == 0) |
| 709 | return; |
| 710 | p = &(*p)->next; |
| 711 | } |
Stefan Weil | 9643c25 | 2011-10-18 22:25:38 +0200 | [diff] [blame] | 712 | |
| 713 | s = g_new0(GDBRegisterState, 1); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 714 | s->base_reg = cpu->gdb_num_regs; |
Stefan Weil | 9643c25 | 2011-10-18 22:25:38 +0200 | [diff] [blame] | 715 | s->num_regs = num_regs; |
| 716 | s->get_reg = get_reg; |
| 717 | s->set_reg = set_reg; |
| 718 | s->xml = xml; |
| 719 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 720 | /* Add to end of list. */ |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 721 | cpu->gdb_num_regs += num_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 722 | *p = s; |
| 723 | if (g_pos) { |
| 724 | if (g_pos != s->base_reg) { |
Ziyue Yang | 7ae6c57 | 2017-01-18 16:03:29 +0800 | [diff] [blame] | 725 | error_report("Error: Bad gdb register numbering for '%s', " |
| 726 | "expected %d got %d", xml, g_pos, s->base_reg); |
Andreas Färber | 35143f0 | 2013-08-12 18:09:47 +0200 | [diff] [blame] | 727 | } else { |
| 728 | cpu->gdb_num_g_regs = cpu->gdb_num_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 733 | #ifndef CONFIG_USER_ONLY |
Peter Maydell | 2472b6c | 2014-09-12 19:04:17 +0100 | [diff] [blame] | 734 | /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */ |
| 735 | static inline int xlat_gdb_type(CPUState *cpu, int gdbtype) |
| 736 | { |
| 737 | static const int xlat[] = { |
| 738 | [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE, |
| 739 | [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ, |
| 740 | [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS, |
| 741 | }; |
| 742 | |
| 743 | CPUClass *cc = CPU_GET_CLASS(cpu); |
| 744 | int cputype = xlat[gdbtype]; |
| 745 | |
| 746 | if (cc->gdb_stop_before_watchpoint) { |
| 747 | cputype |= BP_STOP_BEFORE_ACCESS; |
| 748 | } |
| 749 | return cputype; |
| 750 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 751 | #endif |
| 752 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 753 | static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type) |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 754 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 755 | CPUState *cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 756 | int err = 0; |
| 757 | |
Andreas Färber | 6227881 | 2013-06-27 17:12:06 +0200 | [diff] [blame] | 758 | if (kvm_enabled()) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 759 | return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type); |
Andreas Färber | 6227881 | 2013-06-27 17:12:06 +0200 | [diff] [blame] | 760 | } |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 761 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 762 | switch (type) { |
| 763 | case GDB_BREAKPOINT_SW: |
| 764 | case GDB_BREAKPOINT_HW: |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 765 | CPU_FOREACH(cpu) { |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 766 | err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL); |
| 767 | if (err) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 768 | break; |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 769 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 770 | } |
| 771 | return err; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 772 | #ifndef CONFIG_USER_ONLY |
| 773 | case GDB_WATCHPOINT_WRITE: |
| 774 | case GDB_WATCHPOINT_READ: |
| 775 | case GDB_WATCHPOINT_ACCESS: |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 776 | CPU_FOREACH(cpu) { |
Peter Maydell | 2472b6c | 2014-09-12 19:04:17 +0100 | [diff] [blame] | 777 | err = cpu_watchpoint_insert(cpu, addr, len, |
| 778 | xlat_gdb_type(cpu, type), NULL); |
| 779 | if (err) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 780 | break; |
Peter Maydell | 2472b6c | 2014-09-12 19:04:17 +0100 | [diff] [blame] | 781 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 782 | } |
| 783 | return err; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 784 | #endif |
| 785 | default: |
| 786 | return -ENOSYS; |
| 787 | } |
| 788 | } |
| 789 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 790 | static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type) |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 791 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 792 | CPUState *cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 793 | int err = 0; |
| 794 | |
Andreas Färber | 6227881 | 2013-06-27 17:12:06 +0200 | [diff] [blame] | 795 | if (kvm_enabled()) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 796 | return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type); |
Andreas Färber | 6227881 | 2013-06-27 17:12:06 +0200 | [diff] [blame] | 797 | } |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 798 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 799 | switch (type) { |
| 800 | case GDB_BREAKPOINT_SW: |
| 801 | case GDB_BREAKPOINT_HW: |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 802 | CPU_FOREACH(cpu) { |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 803 | err = cpu_breakpoint_remove(cpu, addr, BP_GDB); |
| 804 | if (err) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 805 | break; |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 806 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 807 | } |
| 808 | return err; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 809 | #ifndef CONFIG_USER_ONLY |
| 810 | case GDB_WATCHPOINT_WRITE: |
| 811 | case GDB_WATCHPOINT_READ: |
| 812 | case GDB_WATCHPOINT_ACCESS: |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 813 | CPU_FOREACH(cpu) { |
Peter Maydell | 2472b6c | 2014-09-12 19:04:17 +0100 | [diff] [blame] | 814 | err = cpu_watchpoint_remove(cpu, addr, len, |
| 815 | xlat_gdb_type(cpu, type)); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 816 | if (err) |
| 817 | break; |
| 818 | } |
| 819 | return err; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 820 | #endif |
| 821 | default: |
| 822 | return -ENOSYS; |
| 823 | } |
| 824 | } |
| 825 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 826 | static void gdb_breakpoint_remove_all(void) |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 827 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 828 | CPUState *cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 829 | |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 830 | if (kvm_enabled()) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 831 | kvm_remove_all_breakpoints(gdbserver_state->c_cpu); |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 832 | return; |
| 833 | } |
| 834 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 835 | CPU_FOREACH(cpu) { |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 836 | cpu_breakpoint_remove_all(cpu, BP_GDB); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 837 | #ifndef CONFIG_USER_ONLY |
Andreas Färber | 75a3403 | 2013-09-02 16:57:02 +0200 | [diff] [blame] | 838 | cpu_watchpoint_remove_all(cpu, BP_GDB); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 839 | #endif |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 840 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 841 | } |
| 842 | |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 843 | static void gdb_set_cpu_pc(GDBState *s, target_ulong pc) |
| 844 | { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 845 | CPUState *cpu = s->c_cpu; |
Andreas Färber | f45748f | 2013-06-21 19:09:18 +0200 | [diff] [blame] | 846 | |
| 847 | cpu_synchronize_state(cpu); |
Peter Crosthwaite | 4a2b24e | 2015-06-23 20:19:21 -0700 | [diff] [blame] | 848 | cpu_set_pc(cpu, pc); |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 851 | static CPUState *find_cpu(uint32_t thread_id) |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 852 | { |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 853 | CPUState *cpu; |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 854 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 855 | CPU_FOREACH(cpu) { |
Alex Bennée | d2a6c85 | 2017-07-12 11:52:14 +0100 | [diff] [blame] | 856 | if (cpu_gdb_index(cpu) == thread_id) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 857 | return cpu; |
Andreas Färber | aa48dd9 | 2013-07-09 20:50:52 +0200 | [diff] [blame] | 858 | } |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 859 | } |
Andreas Färber | aa48dd9 | 2013-07-09 20:50:52 +0200 | [diff] [blame] | 860 | |
| 861 | return NULL; |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Jan Kiszka | 4dabe74 | 2015-02-07 09:38:43 +0100 | [diff] [blame] | 864 | static int is_query_packet(const char *p, const char *query, char separator) |
| 865 | { |
| 866 | unsigned int query_len = strlen(query); |
| 867 | |
| 868 | return strncmp(p, query, query_len) == 0 && |
| 869 | (p[query_len] == '\0' || p[query_len] == separator); |
| 870 | } |
| 871 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 872 | /** |
| 873 | * gdb_handle_vcont - Parses and handles a vCont packet. |
| 874 | * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is |
| 875 | * a format error, 0 on success. |
| 876 | */ |
| 877 | static int gdb_handle_vcont(GDBState *s, const char *p) |
| 878 | { |
| 879 | int res, idx, signal = 0; |
| 880 | char cur_action; |
| 881 | char *newstates; |
| 882 | unsigned long tmp; |
| 883 | CPUState *cpu; |
| 884 | #ifdef CONFIG_USER_ONLY |
| 885 | int max_cpus = 1; /* global variable max_cpus exists only in system mode */ |
| 886 | |
| 887 | CPU_FOREACH(cpu) { |
| 888 | max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus; |
| 889 | } |
| 890 | #endif |
| 891 | /* uninitialised CPUs stay 0 */ |
| 892 | newstates = g_new0(char, max_cpus); |
| 893 | |
| 894 | /* mark valid CPUs with 1 */ |
| 895 | CPU_FOREACH(cpu) { |
| 896 | newstates[cpu->cpu_index] = 1; |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * res keeps track of what error we are returning, with -ENOTSUP meaning |
| 901 | * that the command is unknown or unsupported, thus returning an empty |
| 902 | * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid, |
| 903 | * or incorrect parameters passed. |
| 904 | */ |
| 905 | res = 0; |
| 906 | while (*p) { |
| 907 | if (*p++ != ';') { |
| 908 | res = -ENOTSUP; |
| 909 | goto out; |
| 910 | } |
| 911 | |
| 912 | cur_action = *p++; |
| 913 | if (cur_action == 'C' || cur_action == 'S') { |
| 914 | cur_action = tolower(cur_action); |
| 915 | res = qemu_strtoul(p + 1, &p, 16, &tmp); |
| 916 | if (res) { |
| 917 | goto out; |
| 918 | } |
| 919 | signal = gdb_signal_to_target(tmp); |
| 920 | } else if (cur_action != 'c' && cur_action != 's') { |
| 921 | /* unknown/invalid/unsupported command */ |
| 922 | res = -ENOTSUP; |
| 923 | goto out; |
| 924 | } |
| 925 | /* thread specification. special values: (none), -1 = all; 0 = any */ |
| 926 | if ((p[0] == ':' && p[1] == '-' && p[2] == '1') || (p[0] != ':')) { |
| 927 | if (*p == ':') { |
| 928 | p += 3; |
| 929 | } |
| 930 | for (idx = 0; idx < max_cpus; idx++) { |
| 931 | if (newstates[idx] == 1) { |
| 932 | newstates[idx] = cur_action; |
| 933 | } |
| 934 | } |
| 935 | } else if (*p == ':') { |
| 936 | p++; |
| 937 | res = qemu_strtoul(p, &p, 16, &tmp); |
| 938 | if (res) { |
| 939 | goto out; |
| 940 | } |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 941 | |
Alex Bennée | 5a6a1ad | 2017-07-12 11:52:16 +0100 | [diff] [blame^] | 942 | /* 0 means any thread, so we pick the first valid CPU */ |
| 943 | cpu = tmp ? find_cpu(tmp) : first_cpu; |
| 944 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 945 | /* invalid CPU/thread specified */ |
Alex Bennée | 5a6a1ad | 2017-07-12 11:52:16 +0100 | [diff] [blame^] | 946 | if (!cpu) { |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 947 | res = -EINVAL; |
| 948 | goto out; |
| 949 | } |
Alex Bennée | 5a6a1ad | 2017-07-12 11:52:16 +0100 | [diff] [blame^] | 950 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 951 | /* only use if no previous match occourred */ |
| 952 | if (newstates[cpu->cpu_index] == 1) { |
| 953 | newstates[cpu->cpu_index] = cur_action; |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | s->signal = signal; |
| 958 | gdb_continue_partial(s, newstates); |
| 959 | |
| 960 | out: |
| 961 | g_free(newstates); |
| 962 | |
| 963 | return res; |
| 964 | } |
| 965 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 966 | static int gdb_handle_packet(GDBState *s, const char *line_buf) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 967 | { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 968 | CPUState *cpu; |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 969 | CPUClass *cc; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 970 | const char *p; |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 971 | uint32_t thread; |
| 972 | int ch, reg_size, type, res; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 973 | char buf[MAX_PACKET_LENGTH]; |
| 974 | uint8_t mem_buf[MAX_PACKET_LENGTH]; |
| 975 | uint8_t *registers; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 976 | target_ulong addr, len; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 977 | |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 978 | |
| 979 | gdb_debug("command='%s'\n", line_buf); |
| 980 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 981 | p = line_buf; |
| 982 | ch = *p++; |
| 983 | switch(ch) { |
| 984 | case '?': |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 985 | /* TODO: Make this return the correct value for user-mode. */ |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 986 | snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP, |
Alex Bennée | d2a6c85 | 2017-07-12 11:52:14 +0100 | [diff] [blame] | 987 | cpu_gdb_index(s->c_cpu)); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 988 | put_packet(s, buf); |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 989 | /* Remove all the breakpoints when this query is issued, |
| 990 | * because gdb is doing and initial connect and the state |
| 991 | * should be cleaned up. |
| 992 | */ |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 993 | gdb_breakpoint_remove_all(); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 994 | break; |
| 995 | case 'c': |
| 996 | if (*p != '\0') { |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 997 | addr = strtoull(p, (char **)&p, 16); |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 998 | gdb_set_cpu_pc(s, addr); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 999 | } |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1000 | s->signal = 0; |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 1001 | gdb_continue(s); |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 1002 | return RS_IDLE; |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 1003 | case 'C': |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1004 | s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16)); |
| 1005 | if (s->signal == -1) |
| 1006 | s->signal = 0; |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 1007 | gdb_continue(s); |
| 1008 | return RS_IDLE; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1009 | case 'v': |
| 1010 | if (strncmp(p, "Cont", 4) == 0) { |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1011 | p += 4; |
| 1012 | if (*p == '?') { |
| 1013 | put_packet(s, "vCont;c;C;s;S"); |
| 1014 | break; |
| 1015 | } |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1016 | |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1017 | res = gdb_handle_vcont(s, p); |
| 1018 | |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1019 | if (res) { |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1020 | if ((res == -EINVAL) || (res == -ERANGE)) { |
| 1021 | put_packet(s, "E22"); |
| 1022 | break; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1023 | } |
Claudio Imbrenda | 544177a | 2017-02-14 18:07:48 +0100 | [diff] [blame] | 1024 | goto unknown_command; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 1025 | } |
| 1026 | break; |
| 1027 | } else { |
| 1028 | goto unknown_command; |
| 1029 | } |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1030 | case 'k': |
| 1031 | /* Kill the target */ |
Ziyue Yang | 7ae6c57 | 2017-01-18 16:03:29 +0800 | [diff] [blame] | 1032 | error_report("QEMU: Terminated via GDBstub"); |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1033 | exit(0); |
| 1034 | case 'D': |
| 1035 | /* Detach packet */ |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1036 | gdb_breakpoint_remove_all(); |
Daniel Gutson | 7ea06da | 2010-02-26 14:13:50 -0300 | [diff] [blame] | 1037 | gdb_syscall_mode = GDB_SYS_DISABLED; |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1038 | gdb_continue(s); |
| 1039 | put_packet(s, "OK"); |
| 1040 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1041 | case 's': |
| 1042 | if (*p != '\0') { |
ths | 8fac580 | 2007-07-12 10:05:07 +0000 | [diff] [blame] | 1043 | addr = strtoull(p, (char **)&p, 16); |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 1044 | gdb_set_cpu_pc(s, addr); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1045 | } |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1046 | cpu_single_step(s->c_cpu, sstep_flags); |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 1047 | gdb_continue(s); |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 1048 | return RS_IDLE; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1049 | case 'F': |
| 1050 | { |
| 1051 | target_ulong ret; |
| 1052 | target_ulong err; |
| 1053 | |
| 1054 | ret = strtoull(p, (char **)&p, 16); |
| 1055 | if (*p == ',') { |
| 1056 | p++; |
| 1057 | err = strtoull(p, (char **)&p, 16); |
| 1058 | } else { |
| 1059 | err = 0; |
| 1060 | } |
| 1061 | if (*p == ',') |
| 1062 | p++; |
| 1063 | type = *p; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1064 | if (s->current_syscall_cb) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1065 | s->current_syscall_cb(s->c_cpu, ret, err); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1066 | s->current_syscall_cb = NULL; |
| 1067 | } |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1068 | if (type == 'C') { |
| 1069 | put_packet(s, "T02"); |
| 1070 | } else { |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 1071 | gdb_continue(s); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1075 | case 'g': |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1076 | cpu_synchronize_state(s->g_cpu); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1077 | len = 0; |
Andreas Färber | 35143f0 | 2013-08-12 18:09:47 +0200 | [diff] [blame] | 1078 | 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] | 1079 | reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1080 | len += reg_size; |
| 1081 | } |
| 1082 | memtohex(buf, mem_buf, len); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1083 | put_packet(s, buf); |
| 1084 | break; |
| 1085 | case 'G': |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1086 | cpu_synchronize_state(s->g_cpu); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1087 | registers = mem_buf; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1088 | len = strlen(p) / 2; |
| 1089 | hextomem((uint8_t *)registers, p, len); |
Andreas Färber | 35143f0 | 2013-08-12 18:09:47 +0200 | [diff] [blame] | 1090 | 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] | 1091 | reg_size = gdb_write_register(s->g_cpu, registers, addr); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1092 | len -= reg_size; |
| 1093 | registers += reg_size; |
| 1094 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1095 | put_packet(s, "OK"); |
| 1096 | break; |
| 1097 | case 'm': |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1098 | addr = strtoull(p, (char **)&p, 16); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1099 | if (*p == ',') |
| 1100 | p++; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1101 | len = strtoull(p, NULL, 16); |
Kevin Wolf | 5accecb | 2015-10-13 09:38:50 +0200 | [diff] [blame] | 1102 | |
| 1103 | /* memtohex() doubles the required space */ |
| 1104 | if (len > MAX_PACKET_LENGTH / 2) { |
| 1105 | put_packet (s, "E22"); |
| 1106 | break; |
| 1107 | } |
| 1108 | |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1109 | 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] | 1110 | put_packet (s, "E14"); |
| 1111 | } else { |
| 1112 | memtohex(buf, mem_buf, len); |
| 1113 | put_packet(s, buf); |
| 1114 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1115 | break; |
| 1116 | case 'M': |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1117 | addr = strtoull(p, (char **)&p, 16); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1118 | if (*p == ',') |
| 1119 | p++; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1120 | len = strtoull(p, (char **)&p, 16); |
bellard | b328f87 | 2005-01-17 22:03:16 +0000 | [diff] [blame] | 1121 | if (*p == ':') |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1122 | p++; |
Kevin Wolf | 5accecb | 2015-10-13 09:38:50 +0200 | [diff] [blame] | 1123 | |
| 1124 | /* hextomem() reads 2*len bytes */ |
| 1125 | if (len > strlen(p) / 2) { |
| 1126 | put_packet (s, "E22"); |
| 1127 | break; |
| 1128 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1129 | hextomem(mem_buf, p, len); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1130 | 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] | 1131 | true) != 0) { |
bellard | 905f20b | 2005-04-26 21:09:55 +0000 | [diff] [blame] | 1132 | put_packet(s, "E14"); |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 1133 | } else { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1134 | put_packet(s, "OK"); |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 1135 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1136 | break; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1137 | case 'p': |
| 1138 | /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable. |
| 1139 | This works, but can be very slow. Anything new enough to |
| 1140 | understand XML also knows how to use this properly. */ |
| 1141 | if (!gdb_has_xml) |
| 1142 | goto unknown_command; |
| 1143 | addr = strtoull(p, (char **)&p, 16); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1144 | reg_size = gdb_read_register(s->g_cpu, mem_buf, addr); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1145 | if (reg_size) { |
| 1146 | memtohex(buf, mem_buf, reg_size); |
| 1147 | put_packet(s, buf); |
| 1148 | } else { |
| 1149 | put_packet(s, "E14"); |
| 1150 | } |
| 1151 | break; |
| 1152 | case 'P': |
| 1153 | if (!gdb_has_xml) |
| 1154 | goto unknown_command; |
| 1155 | addr = strtoull(p, (char **)&p, 16); |
| 1156 | if (*p == '=') |
| 1157 | p++; |
| 1158 | reg_size = strlen(p) / 2; |
| 1159 | hextomem(mem_buf, p, reg_size); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1160 | gdb_write_register(s->g_cpu, mem_buf, addr); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1161 | put_packet(s, "OK"); |
| 1162 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1163 | case 'Z': |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1164 | case 'z': |
| 1165 | type = strtoul(p, (char **)&p, 16); |
| 1166 | if (*p == ',') |
| 1167 | p++; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1168 | addr = strtoull(p, (char **)&p, 16); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1169 | if (*p == ',') |
| 1170 | p++; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1171 | len = strtoull(p, (char **)&p, 16); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1172 | if (ch == 'Z') |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1173 | res = gdb_breakpoint_insert(addr, len, type); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1174 | else |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1175 | res = gdb_breakpoint_remove(addr, len, type); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1176 | if (res >= 0) |
| 1177 | put_packet(s, "OK"); |
| 1178 | else if (res == -ENOSYS) |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1179 | put_packet(s, ""); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1180 | else |
| 1181 | put_packet(s, "E22"); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1182 | break; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1183 | case 'H': |
| 1184 | type = *p++; |
| 1185 | thread = strtoull(p, (char **)&p, 16); |
| 1186 | if (thread == -1 || thread == 0) { |
| 1187 | put_packet(s, "OK"); |
| 1188 | break; |
| 1189 | } |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1190 | cpu = find_cpu(thread); |
| 1191 | if (cpu == NULL) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1192 | put_packet(s, "E22"); |
| 1193 | break; |
| 1194 | } |
| 1195 | switch (type) { |
| 1196 | case 'c': |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1197 | s->c_cpu = cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1198 | put_packet(s, "OK"); |
| 1199 | break; |
| 1200 | case 'g': |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1201 | s->g_cpu = cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1202 | put_packet(s, "OK"); |
| 1203 | break; |
| 1204 | default: |
| 1205 | put_packet(s, "E22"); |
| 1206 | break; |
| 1207 | } |
| 1208 | break; |
| 1209 | case 'T': |
| 1210 | thread = strtoull(p, (char **)&p, 16); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1211 | cpu = find_cpu(thread); |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1212 | |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1213 | if (cpu != NULL) { |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1214 | put_packet(s, "OK"); |
| 1215 | } else { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1216 | put_packet(s, "E22"); |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1217 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1218 | break; |
pbrook | 978efd6 | 2006-06-17 18:30:42 +0000 | [diff] [blame] | 1219 | case 'q': |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1220 | case 'Q': |
| 1221 | /* parse any 'q' packets here */ |
| 1222 | if (!strcmp(p,"qemu.sstepbits")) { |
| 1223 | /* Query Breakpoint bit definitions */ |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1224 | snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", |
| 1225 | SSTEP_ENABLE, |
| 1226 | SSTEP_NOIRQ, |
| 1227 | SSTEP_NOTIMER); |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1228 | put_packet(s, buf); |
| 1229 | break; |
Jan Kiszka | 4dabe74 | 2015-02-07 09:38:43 +0100 | [diff] [blame] | 1230 | } else if (is_query_packet(p, "qemu.sstep", '=')) { |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1231 | /* Display or change the sstep_flags */ |
| 1232 | p += 10; |
| 1233 | if (*p != '=') { |
| 1234 | /* Display current setting */ |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1235 | snprintf(buf, sizeof(buf), "0x%x", sstep_flags); |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1236 | put_packet(s, buf); |
| 1237 | break; |
| 1238 | } |
| 1239 | p++; |
| 1240 | type = strtoul(p, (char **)&p, 16); |
| 1241 | sstep_flags = type; |
| 1242 | put_packet(s, "OK"); |
| 1243 | break; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1244 | } else if (strcmp(p,"C") == 0) { |
| 1245 | /* "Current thread" remains vague in the spec, so always return |
| 1246 | * the first CPU (gdb returns the first thread). */ |
| 1247 | put_packet(s, "QC1"); |
| 1248 | break; |
| 1249 | } else if (strcmp(p,"fThreadInfo") == 0) { |
Andreas Färber | 52f3462 | 2013-06-27 13:44:40 +0200 | [diff] [blame] | 1250 | s->query_cpu = first_cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1251 | goto report_cpuinfo; |
| 1252 | } else if (strcmp(p,"sThreadInfo") == 0) { |
| 1253 | report_cpuinfo: |
| 1254 | if (s->query_cpu) { |
Alex Bennée | d2a6c85 | 2017-07-12 11:52:14 +0100 | [diff] [blame] | 1255 | snprintf(buf, sizeof(buf), "m%x", cpu_gdb_index(s->query_cpu)); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1256 | put_packet(s, buf); |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1257 | s->query_cpu = CPU_NEXT(s->query_cpu); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1258 | } else |
| 1259 | put_packet(s, "l"); |
| 1260 | break; |
| 1261 | } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) { |
| 1262 | thread = strtoull(p+16, (char **)&p, 16); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1263 | cpu = find_cpu(thread); |
| 1264 | if (cpu != NULL) { |
Andreas Färber | cb446ec | 2013-05-01 14:24:52 +0200 | [diff] [blame] | 1265 | cpu_synchronize_state(cpu); |
Kevin Wolf | 5accecb | 2015-10-13 09:38:50 +0200 | [diff] [blame] | 1266 | /* memtohex() doubles the required space */ |
| 1267 | len = snprintf((char *)mem_buf, sizeof(buf) / 2, |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 1268 | "CPU#%d [%s]", cpu->cpu_index, |
Andreas Färber | 259186a | 2013-01-17 18:51:17 +0100 | [diff] [blame] | 1269 | cpu->halted ? "halted " : "running"); |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1270 | memtohex(buf, mem_buf, len); |
| 1271 | put_packet(s, buf); |
| 1272 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1273 | break; |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1274 | } |
blueswir1 | 0b8a988 | 2009-03-07 10:51:36 +0000 | [diff] [blame] | 1275 | #ifdef CONFIG_USER_ONLY |
Jan Kiszka | 070949f | 2015-02-07 09:38:42 +0100 | [diff] [blame] | 1276 | else if (strcmp(p, "Offsets") == 0) { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 1277 | TaskState *ts = s->c_cpu->opaque; |
pbrook | 978efd6 | 2006-06-17 18:30:42 +0000 | [diff] [blame] | 1278 | |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1279 | snprintf(buf, sizeof(buf), |
| 1280 | "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx |
| 1281 | ";Bss=" TARGET_ABI_FMT_lx, |
| 1282 | ts->info->code_offset, |
| 1283 | ts->info->data_offset, |
| 1284 | ts->info->data_offset); |
pbrook | 978efd6 | 2006-06-17 18:30:42 +0000 | [diff] [blame] | 1285 | put_packet(s, buf); |
| 1286 | break; |
| 1287 | } |
blueswir1 | 0b8a988 | 2009-03-07 10:51:36 +0000 | [diff] [blame] | 1288 | #else /* !CONFIG_USER_ONLY */ |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1289 | else if (strncmp(p, "Rcmd,", 5) == 0) { |
| 1290 | int len = strlen(p + 5); |
| 1291 | |
| 1292 | if ((len % 2) != 0) { |
| 1293 | put_packet(s, "E01"); |
| 1294 | break; |
| 1295 | } |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1296 | len = len / 2; |
Kevin Wolf | 5accecb | 2015-10-13 09:38:50 +0200 | [diff] [blame] | 1297 | hextomem(mem_buf, p + 5, len); |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1298 | mem_buf[len++] = 0; |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 1299 | qemu_chr_be_write(s->mon_chr, mem_buf, len); |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1300 | put_packet(s, "OK"); |
| 1301 | break; |
| 1302 | } |
blueswir1 | 0b8a988 | 2009-03-07 10:51:36 +0000 | [diff] [blame] | 1303 | #endif /* !CONFIG_USER_ONLY */ |
Jan Kiszka | 4dabe74 | 2015-02-07 09:38:43 +0100 | [diff] [blame] | 1304 | if (is_query_packet(p, "Supported", ':')) { |
blueswir1 | 5b3715b | 2008-10-25 11:18:12 +0000 | [diff] [blame] | 1305 | snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH); |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 1306 | cc = CPU_GET_CLASS(first_cpu); |
| 1307 | if (cc->gdb_core_xml_file != NULL) { |
| 1308 | pstrcat(buf, sizeof(buf), ";qXfer:features:read+"); |
| 1309 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1310 | put_packet(s, buf); |
| 1311 | break; |
| 1312 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1313 | if (strncmp(p, "Xfer:features:read:", 19) == 0) { |
| 1314 | const char *xml; |
| 1315 | target_ulong total_len; |
| 1316 | |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 1317 | cc = CPU_GET_CLASS(first_cpu); |
| 1318 | if (cc->gdb_core_xml_file == NULL) { |
| 1319 | goto unknown_command; |
| 1320 | } |
| 1321 | |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 1322 | gdb_has_xml = true; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1323 | p += 19; |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 1324 | xml = get_feature_xml(p, &p, cc); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1325 | if (!xml) { |
blueswir1 | 5b3715b | 2008-10-25 11:18:12 +0000 | [diff] [blame] | 1326 | snprintf(buf, sizeof(buf), "E00"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1327 | put_packet(s, buf); |
| 1328 | break; |
| 1329 | } |
| 1330 | |
| 1331 | if (*p == ':') |
| 1332 | p++; |
| 1333 | addr = strtoul(p, (char **)&p, 16); |
| 1334 | if (*p == ',') |
| 1335 | p++; |
| 1336 | len = strtoul(p, (char **)&p, 16); |
| 1337 | |
| 1338 | total_len = strlen(xml); |
| 1339 | if (addr > total_len) { |
blueswir1 | 5b3715b | 2008-10-25 11:18:12 +0000 | [diff] [blame] | 1340 | snprintf(buf, sizeof(buf), "E00"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1341 | put_packet(s, buf); |
| 1342 | break; |
| 1343 | } |
| 1344 | if (len > (MAX_PACKET_LENGTH - 5) / 2) |
| 1345 | len = (MAX_PACKET_LENGTH - 5) / 2; |
| 1346 | if (len < total_len - addr) { |
| 1347 | buf[0] = 'm'; |
| 1348 | len = memtox(buf + 1, xml + addr, len); |
| 1349 | } else { |
| 1350 | buf[0] = 'l'; |
| 1351 | len = memtox(buf + 1, xml + addr, total_len - addr); |
| 1352 | } |
| 1353 | put_packet_binary(s, buf, len + 1); |
| 1354 | break; |
| 1355 | } |
Jan Kiszka | a391938 | 2015-02-07 09:38:44 +0100 | [diff] [blame] | 1356 | if (is_query_packet(p, "Attached", ':')) { |
| 1357 | put_packet(s, GDB_ATTACHED); |
| 1358 | break; |
| 1359 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1360 | /* Unrecognised 'q' command. */ |
| 1361 | goto unknown_command; |
| 1362 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1363 | default: |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1364 | unknown_command: |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1365 | /* put empty packet */ |
| 1366 | buf[0] = '\0'; |
| 1367 | put_packet(s, buf); |
| 1368 | break; |
| 1369 | } |
| 1370 | return RS_IDLE; |
| 1371 | } |
| 1372 | |
Andreas Färber | 64f6b34 | 2013-05-27 02:06:09 +0200 | [diff] [blame] | 1373 | void gdb_set_stop_cpu(CPUState *cpu) |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1374 | { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1375 | gdbserver_state->c_cpu = cpu; |
| 1376 | gdbserver_state->g_cpu = cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1379 | #ifndef CONFIG_USER_ONLY |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 1380 | static void gdb_vm_state_change(void *opaque, int running, RunState state) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1381 | { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1382 | GDBState *s = gdbserver_state; |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1383 | CPUState *cpu = s->c_cpu; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1384 | char buf[256]; |
aliguori | d6fc1b3 | 2008-11-18 19:55:44 +0000 | [diff] [blame] | 1385 | const char *type; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1386 | int ret; |
| 1387 | |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1388 | if (running || s->state == RS_INACTIVE) { |
| 1389 | return; |
| 1390 | } |
| 1391 | /* Is there a GDB syscall waiting to be sent? */ |
| 1392 | if (s->current_syscall_cb) { |
| 1393 | put_packet(s, s->syscall_buf); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1394 | return; |
Jan Kiszka | e07bbac | 2011-02-09 16:29:40 +0100 | [diff] [blame] | 1395 | } |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 1396 | switch (state) { |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1397 | case RUN_STATE_DEBUG: |
Andreas Färber | ff4700b | 2013-08-26 18:23:18 +0200 | [diff] [blame] | 1398 | if (cpu->watchpoint_hit) { |
| 1399 | switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1400 | case BP_MEM_READ: |
aliguori | d6fc1b3 | 2008-11-18 19:55:44 +0000 | [diff] [blame] | 1401 | type = "r"; |
| 1402 | break; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1403 | case BP_MEM_ACCESS: |
aliguori | d6fc1b3 | 2008-11-18 19:55:44 +0000 | [diff] [blame] | 1404 | type = "a"; |
| 1405 | break; |
| 1406 | default: |
| 1407 | type = ""; |
| 1408 | break; |
| 1409 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1410 | snprintf(buf, sizeof(buf), |
| 1411 | "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";", |
Alex Bennée | d2a6c85 | 2017-07-12 11:52:14 +0100 | [diff] [blame] | 1412 | GDB_SIGNAL_TRAP, cpu_gdb_index(cpu), type, |
Andreas Färber | ff4700b | 2013-08-26 18:23:18 +0200 | [diff] [blame] | 1413 | (target_ulong)cpu->watchpoint_hit->vaddr); |
| 1414 | cpu->watchpoint_hit = NULL; |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1415 | goto send_packet; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1416 | } |
Peter Crosthwaite | bbd77c1 | 2015-06-23 19:31:15 -0700 | [diff] [blame] | 1417 | tb_flush(cpu); |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1418 | ret = GDB_SIGNAL_TRAP; |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1419 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1420 | case RUN_STATE_PAUSED: |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 1421 | ret = GDB_SIGNAL_INT; |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1422 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1423 | case RUN_STATE_SHUTDOWN: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1424 | ret = GDB_SIGNAL_QUIT; |
| 1425 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1426 | case RUN_STATE_IO_ERROR: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1427 | ret = GDB_SIGNAL_IO; |
| 1428 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1429 | case RUN_STATE_WATCHDOG: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1430 | ret = GDB_SIGNAL_ALRM; |
| 1431 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1432 | case RUN_STATE_INTERNAL_ERROR: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1433 | ret = GDB_SIGNAL_ABRT; |
| 1434 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1435 | case RUN_STATE_SAVE_VM: |
| 1436 | case RUN_STATE_RESTORE_VM: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1437 | return; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1438 | case RUN_STATE_FINISH_MIGRATE: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1439 | ret = GDB_SIGNAL_XCPU; |
| 1440 | break; |
| 1441 | default: |
| 1442 | ret = GDB_SIGNAL_UNKNOWN; |
| 1443 | break; |
bellard | bbeb7b5 | 2006-04-23 18:42:15 +0000 | [diff] [blame] | 1444 | } |
Jan Kiszka | 226d007 | 2015-07-24 18:52:31 +0200 | [diff] [blame] | 1445 | gdb_set_stop_cpu(cpu); |
Alex Bennée | d2a6c85 | 2017-07-12 11:52:14 +0100 | [diff] [blame] | 1446 | snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_gdb_index(cpu)); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1447 | |
| 1448 | send_packet: |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1449 | put_packet(s, buf); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1450 | |
| 1451 | /* disable single step if it was enabled */ |
Andreas Färber | 3825b28 | 2013-06-24 18:41:06 +0200 | [diff] [blame] | 1452 | cpu_single_step(cpu, 0); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1453 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1454 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1455 | |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1456 | /* Send a gdb syscall request. |
| 1457 | This accepts limited printf-style format specifiers, specifically: |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1458 | %x - target_ulong argument printed in hex. |
| 1459 | %lx - 64-bit argument printed in hex. |
| 1460 | %s - string pointer (target_ulong) and length (int) pair. */ |
Peter Maydell | 19239b3 | 2015-09-07 10:39:27 +0100 | [diff] [blame] | 1461 | 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] | 1462 | { |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1463 | char *p; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1464 | char *p_end; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1465 | target_ulong addr; |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1466 | uint64_t i64; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1467 | GDBState *s; |
| 1468 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1469 | s = gdbserver_state; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1470 | if (!s) |
| 1471 | return; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1472 | s->current_syscall_cb = cb; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1473 | #ifndef CONFIG_USER_ONLY |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1474 | vm_stop(RUN_STATE_DEBUG); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1475 | #endif |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1476 | p = s->syscall_buf; |
| 1477 | p_end = &s->syscall_buf[sizeof(s->syscall_buf)]; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1478 | *(p++) = 'F'; |
| 1479 | while (*fmt) { |
| 1480 | if (*fmt == '%') { |
| 1481 | fmt++; |
| 1482 | switch (*fmt++) { |
| 1483 | case 'x': |
| 1484 | addr = va_arg(va, target_ulong); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1485 | p += snprintf(p, p_end - p, TARGET_FMT_lx, addr); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1486 | break; |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1487 | case 'l': |
| 1488 | if (*(fmt++) != 'x') |
| 1489 | goto bad_format; |
| 1490 | i64 = va_arg(va, uint64_t); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1491 | p += snprintf(p, p_end - p, "%" PRIx64, i64); |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1492 | break; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1493 | case 's': |
| 1494 | addr = va_arg(va, target_ulong); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1495 | p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x", |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1496 | addr, va_arg(va, int)); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1497 | break; |
| 1498 | default: |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1499 | bad_format: |
Ziyue Yang | 7ae6c57 | 2017-01-18 16:03:29 +0800 | [diff] [blame] | 1500 | error_report("gdbstub: Bad syscall format string '%s'", |
| 1501 | fmt - 1); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1502 | break; |
| 1503 | } |
| 1504 | } else { |
| 1505 | *(p++) = *(fmt++); |
| 1506 | } |
| 1507 | } |
pbrook | 8a93e02 | 2007-08-06 13:19:15 +0000 | [diff] [blame] | 1508 | *p = 0; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1509 | #ifdef CONFIG_USER_ONLY |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1510 | put_packet(s, s->syscall_buf); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1511 | gdb_handlesig(s->c_cpu, 0); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1512 | #else |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1513 | /* In this case wait to send the syscall packet until notification that |
| 1514 | the CPU has stopped. This must be done because if the packet is sent |
| 1515 | now the reply from the syscall request could be received while the CPU |
| 1516 | is still in the running state, which can cause packets to be dropped |
| 1517 | and state transition 'T' packets to be sent while the syscall is still |
| 1518 | being processed. */ |
Paolo Bonzini | 9102ded | 2015-08-18 06:52:09 -0700 | [diff] [blame] | 1519 | qemu_cpu_kick(s->c_cpu); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1520 | #endif |
| 1521 | } |
| 1522 | |
Peter Maydell | 19239b3 | 2015-09-07 10:39:27 +0100 | [diff] [blame] | 1523 | void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...) |
| 1524 | { |
| 1525 | va_list va; |
| 1526 | |
| 1527 | va_start(va, fmt); |
| 1528 | gdb_do_syscallv(cb, fmt, va); |
| 1529 | va_end(va); |
| 1530 | } |
| 1531 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1532 | static void gdb_read_byte(GDBState *s, int ch) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1533 | { |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 1534 | uint8_t reply; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1535 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1536 | #ifndef CONFIG_USER_ONLY |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1537 | if (s->last_packet_len) { |
| 1538 | /* Waiting for a response to the last packet. If we see the start |
| 1539 | of a new command then abandon the previous response. */ |
| 1540 | if (ch == '-') { |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1541 | gdb_debug("Got NACK, retransmitting\n"); |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 1542 | 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] | 1543 | } else if (ch == '+') { |
| 1544 | gdb_debug("Got ACK\n"); |
| 1545 | } else { |
| 1546 | gdb_debug("Got '%c' when expecting ACK/NACK\n", ch); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1547 | } |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1548 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1549 | if (ch == '+' || ch == '$') |
| 1550 | s->last_packet_len = 0; |
| 1551 | if (ch != '$') |
| 1552 | return; |
| 1553 | } |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 1554 | if (runstate_is_running()) { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1555 | /* when the CPU is running, we cannot do anything except stop |
| 1556 | it when receiving a char */ |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1557 | vm_stop(RUN_STATE_PAUSED); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1558 | } else |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1559 | #endif |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 1560 | { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1561 | switch(s->state) { |
| 1562 | case RS_IDLE: |
| 1563 | if (ch == '$') { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1564 | /* start of command packet */ |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1565 | s->line_buf_index = 0; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1566 | s->line_sum = 0; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1567 | s->state = RS_GETLINE; |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1568 | } else { |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1569 | gdb_debug("received garbage between packets: 0x%x\n", ch); |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1570 | } |
| 1571 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1572 | case RS_GETLINE: |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1573 | if (ch == '}') { |
| 1574 | /* start escape sequence */ |
| 1575 | s->state = RS_GETLINE_ESC; |
| 1576 | s->line_sum += ch; |
| 1577 | } else if (ch == '*') { |
| 1578 | /* start run length encoding sequence */ |
| 1579 | s->state = RS_GETLINE_RLE; |
| 1580 | s->line_sum += ch; |
| 1581 | } else if (ch == '#') { |
| 1582 | /* end of command, start of checksum*/ |
| 1583 | s->state = RS_CHKSUM1; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1584 | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1585 | gdb_debug("command buffer overrun, dropping command\n"); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1586 | s->state = RS_IDLE; |
| 1587 | } else { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1588 | /* unescaped command character */ |
| 1589 | s->line_buf[s->line_buf_index++] = ch; |
| 1590 | s->line_sum += ch; |
| 1591 | } |
| 1592 | break; |
| 1593 | case RS_GETLINE_ESC: |
| 1594 | if (ch == '#') { |
| 1595 | /* unexpected end of command in escape sequence */ |
| 1596 | s->state = RS_CHKSUM1; |
| 1597 | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
| 1598 | /* command buffer overrun */ |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1599 | gdb_debug("command buffer overrun, dropping command\n"); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1600 | s->state = RS_IDLE; |
| 1601 | } else { |
| 1602 | /* parse escaped character and leave escape state */ |
| 1603 | s->line_buf[s->line_buf_index++] = ch ^ 0x20; |
| 1604 | s->line_sum += ch; |
| 1605 | s->state = RS_GETLINE; |
| 1606 | } |
| 1607 | break; |
| 1608 | case RS_GETLINE_RLE: |
| 1609 | if (ch < ' ') { |
| 1610 | /* invalid RLE count encoding */ |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1611 | gdb_debug("got invalid RLE count: 0x%x\n", ch); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1612 | s->state = RS_GETLINE; |
| 1613 | } else { |
| 1614 | /* decode repeat length */ |
| 1615 | int repeat = (unsigned char)ch - ' ' + 3; |
| 1616 | if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) { |
| 1617 | /* that many repeats would overrun the command buffer */ |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1618 | gdb_debug("command buffer overrun, dropping command\n"); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1619 | s->state = RS_IDLE; |
| 1620 | } else if (s->line_buf_index < 1) { |
| 1621 | /* got a repeat but we have nothing to repeat */ |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1622 | gdb_debug("got invalid RLE sequence\n"); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1623 | s->state = RS_GETLINE; |
| 1624 | } else { |
| 1625 | /* repeat the last character */ |
| 1626 | memset(s->line_buf + s->line_buf_index, |
| 1627 | s->line_buf[s->line_buf_index - 1], repeat); |
| 1628 | s->line_buf_index += repeat; |
| 1629 | s->line_sum += ch; |
| 1630 | s->state = RS_GETLINE; |
| 1631 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1632 | } |
| 1633 | break; |
| 1634 | case RS_CHKSUM1: |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1635 | /* get high hex digit of checksum */ |
| 1636 | if (!isxdigit(ch)) { |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1637 | gdb_debug("got invalid command checksum digit\n"); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1638 | s->state = RS_GETLINE; |
| 1639 | break; |
| 1640 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1641 | s->line_buf[s->line_buf_index] = '\0'; |
| 1642 | s->line_csum = fromhex(ch) << 4; |
| 1643 | s->state = RS_CHKSUM2; |
| 1644 | break; |
| 1645 | case RS_CHKSUM2: |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1646 | /* get low hex digit of checksum */ |
| 1647 | if (!isxdigit(ch)) { |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1648 | gdb_debug("got invalid command checksum digit\n"); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1649 | s->state = RS_GETLINE; |
| 1650 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1651 | } |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1652 | s->line_csum |= fromhex(ch); |
| 1653 | |
| 1654 | if (s->line_csum != (s->line_sum & 0xff)) { |
Alex Bennée | 118e226 | 2017-07-12 11:52:13 +0100 | [diff] [blame] | 1655 | gdb_debug("got command packet with incorrect checksum\n"); |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1656 | /* send NAK reply */ |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 1657 | reply = '-'; |
| 1658 | put_buffer(s, &reply, 1); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1659 | s->state = RS_IDLE; |
| 1660 | } else { |
Doug Gale | 4bf4312 | 2017-05-01 12:22:10 -0400 | [diff] [blame] | 1661 | /* send ACK reply */ |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 1662 | reply = '+'; |
| 1663 | put_buffer(s, &reply, 1); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1664 | s->state = gdb_handle_packet(s, s->line_buf); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1665 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1666 | break; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1667 | default: |
| 1668 | abort(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1669 | } |
| 1670 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 1673 | /* Tell the remote gdb that the process has exited. */ |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 1674 | void gdb_exit(CPUArchState *env, int code) |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 1675 | { |
| 1676 | GDBState *s; |
| 1677 | char buf[4]; |
| 1678 | |
| 1679 | s = gdbserver_state; |
| 1680 | if (!s) { |
| 1681 | return; |
| 1682 | } |
| 1683 | #ifdef CONFIG_USER_ONLY |
| 1684 | if (gdbserver_fd < 0 || s->fd < 0) { |
| 1685 | return; |
| 1686 | } |
| 1687 | #endif |
| 1688 | |
| 1689 | snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code); |
| 1690 | put_packet(s, buf); |
Fabien Chouteau | e2af15b | 2011-01-13 12:46:57 +0100 | [diff] [blame] | 1691 | |
| 1692 | #ifndef CONFIG_USER_ONLY |
Marc-André Lureau | 1ce2610 | 2017-01-27 00:49:13 +0400 | [diff] [blame] | 1693 | qemu_chr_fe_deinit(&s->chr, true); |
Fabien Chouteau | e2af15b | 2011-01-13 12:46:57 +0100 | [diff] [blame] | 1694 | #endif |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 1695 | } |
| 1696 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1697 | #ifdef CONFIG_USER_ONLY |
| 1698 | int |
Andreas Färber | db6b81d | 2013-06-27 19:49:31 +0200 | [diff] [blame] | 1699 | gdb_handlesig(CPUState *cpu, int sig) |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1700 | { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1701 | GDBState *s; |
| 1702 | char buf[256]; |
| 1703 | int n; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1704 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1705 | s = gdbserver_state; |
| 1706 | if (gdbserver_fd < 0 || s->fd < 0) { |
| 1707 | return sig; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1710 | /* disable single step if it was enabled */ |
Andreas Färber | 3825b28 | 2013-06-24 18:41:06 +0200 | [diff] [blame] | 1711 | cpu_single_step(cpu, 0); |
Peter Crosthwaite | bbd77c1 | 2015-06-23 19:31:15 -0700 | [diff] [blame] | 1712 | tb_flush(cpu); |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1713 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1714 | if (sig != 0) { |
| 1715 | snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig)); |
| 1716 | put_packet(s, buf); |
| 1717 | } |
| 1718 | /* put_packet() might have detected that the peer terminated the |
| 1719 | connection. */ |
| 1720 | if (s->fd < 0) { |
| 1721 | return sig; |
| 1722 | } |
| 1723 | |
| 1724 | sig = 0; |
| 1725 | s->state = RS_IDLE; |
| 1726 | s->running_state = 0; |
| 1727 | while (s->running_state == 0) { |
| 1728 | n = read(s->fd, buf, 256); |
| 1729 | if (n > 0) { |
| 1730 | int i; |
| 1731 | |
| 1732 | for (i = 0; i < n; i++) { |
| 1733 | gdb_read_byte(s, buf[i]); |
| 1734 | } |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 1735 | } else { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1736 | /* XXX: Connection closed. Should probably wait for another |
| 1737 | connection before continuing. */ |
Peter Wu | 5819e3e | 2016-06-05 16:35:48 +0200 | [diff] [blame] | 1738 | if (n == 0) { |
| 1739 | close(s->fd); |
| 1740 | } |
| 1741 | s->fd = -1; |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1742 | return sig; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1743 | } |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1744 | } |
| 1745 | sig = s->signal; |
| 1746 | s->signal = 0; |
| 1747 | return sig; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1748 | } |
bellard | e900967 | 2005-04-26 20:42:36 +0000 | [diff] [blame] | 1749 | |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1750 | /* 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] | 1751 | void gdb_signalled(CPUArchState *env, int sig) |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1752 | { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1753 | GDBState *s; |
| 1754 | char buf[4]; |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1755 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1756 | s = gdbserver_state; |
| 1757 | if (gdbserver_fd < 0 || s->fd < 0) { |
| 1758 | return; |
| 1759 | } |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1760 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1761 | snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig)); |
| 1762 | put_packet(s, buf); |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1763 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1764 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1765 | static void gdb_accept(void) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1766 | { |
| 1767 | GDBState *s; |
| 1768 | struct sockaddr_in sockaddr; |
| 1769 | socklen_t len; |
MORITA Kazutaka | bf1c852 | 2013-02-22 12:39:50 +0900 | [diff] [blame] | 1770 | int fd; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1771 | |
| 1772 | for(;;) { |
| 1773 | len = sizeof(sockaddr); |
| 1774 | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len); |
| 1775 | if (fd < 0 && errno != EINTR) { |
| 1776 | perror("accept"); |
| 1777 | return; |
| 1778 | } else if (fd >= 0) { |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 1779 | #ifndef _WIN32 |
| 1780 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 1781 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1782 | break; |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | /* set short latency */ |
MORITA Kazutaka | bf1c852 | 2013-02-22 12:39:50 +0900 | [diff] [blame] | 1787 | socket_set_nodelay(fd); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1788 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1789 | s = g_malloc0(sizeof(GDBState)); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1790 | s->c_cpu = first_cpu; |
| 1791 | s->g_cpu = first_cpu; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1792 | s->fd = fd; |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 1793 | gdb_has_xml = false; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1794 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1795 | gdbserver_state = s; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | static int gdbserver_open(int port) |
| 1799 | { |
| 1800 | struct sockaddr_in sockaddr; |
Sebastian Ottlik | 6669ca1 | 2013-10-02 12:23:13 +0200 | [diff] [blame] | 1801 | int fd, ret; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1802 | |
| 1803 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 1804 | if (fd < 0) { |
| 1805 | perror("socket"); |
| 1806 | return -1; |
| 1807 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 1808 | #ifndef _WIN32 |
| 1809 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 1810 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1811 | |
Sebastian Ottlik | 6669ca1 | 2013-10-02 12:23:13 +0200 | [diff] [blame] | 1812 | socket_set_fast_reuse(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1813 | |
| 1814 | sockaddr.sin_family = AF_INET; |
| 1815 | sockaddr.sin_port = htons(port); |
| 1816 | sockaddr.sin_addr.s_addr = 0; |
| 1817 | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
| 1818 | if (ret < 0) { |
| 1819 | perror("bind"); |
Peter Maydell | bb16172 | 2011-12-24 23:37:24 +0000 | [diff] [blame] | 1820 | close(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1821 | return -1; |
| 1822 | } |
Peter Wu | 96165b9 | 2016-05-04 11:32:17 +0200 | [diff] [blame] | 1823 | ret = listen(fd, 1); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1824 | if (ret < 0) { |
| 1825 | perror("listen"); |
Peter Maydell | bb16172 | 2011-12-24 23:37:24 +0000 | [diff] [blame] | 1826 | close(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1827 | return -1; |
| 1828 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1829 | return fd; |
| 1830 | } |
| 1831 | |
| 1832 | int gdbserver_start(int port) |
| 1833 | { |
| 1834 | gdbserver_fd = gdbserver_open(port); |
| 1835 | if (gdbserver_fd < 0) |
| 1836 | return -1; |
| 1837 | /* accept connections */ |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1838 | gdb_accept(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1839 | return 0; |
| 1840 | } |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 1841 | |
| 1842 | /* Disable gdb stub for child processes. */ |
Peter Crosthwaite | f7ec7f7 | 2015-06-23 19:31:16 -0700 | [diff] [blame] | 1843 | void gdbserver_fork(CPUState *cpu) |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 1844 | { |
| 1845 | GDBState *s = gdbserver_state; |
Andreas Färber | 75a3403 | 2013-09-02 16:57:02 +0200 | [diff] [blame] | 1846 | |
| 1847 | if (gdbserver_fd < 0 || s->fd < 0) { |
| 1848 | return; |
| 1849 | } |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 1850 | close(s->fd); |
| 1851 | s->fd = -1; |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 1852 | cpu_breakpoint_remove_all(cpu, BP_GDB); |
Andreas Färber | 75a3403 | 2013-09-02 16:57:02 +0200 | [diff] [blame] | 1853 | cpu_watchpoint_remove_all(cpu, BP_GDB); |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 1854 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1855 | #else |
ths | aa1f17c | 2007-07-11 22:48:58 +0000 | [diff] [blame] | 1856 | static int gdb_chr_can_receive(void *opaque) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1857 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1858 | /* We can handle an arbitrarily large amount of data. |
| 1859 | Pick the maximum packet size, which is as good as anything. */ |
| 1860 | return MAX_PACKET_LENGTH; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
ths | aa1f17c | 2007-07-11 22:48:58 +0000 | [diff] [blame] | 1863 | static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1864 | { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1865 | int i; |
| 1866 | |
| 1867 | for (i = 0; i < size; i++) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1868 | gdb_read_byte(gdbserver_state, buf[i]); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | static void gdb_chr_event(void *opaque, int event) |
| 1873 | { |
| 1874 | switch (event) { |
Amit Shah | b6b8df5 | 2009-10-07 18:31:16 +0530 | [diff] [blame] | 1875 | case CHR_EVENT_OPENED: |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1876 | vm_stop(RUN_STATE_PAUSED); |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 1877 | gdb_has_xml = false; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1878 | break; |
| 1879 | default: |
| 1880 | break; |
| 1881 | } |
| 1882 | } |
| 1883 | |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1884 | static void gdb_monitor_output(GDBState *s, const char *msg, int len) |
| 1885 | { |
| 1886 | char buf[MAX_PACKET_LENGTH]; |
| 1887 | |
| 1888 | buf[0] = 'O'; |
| 1889 | if (len > (MAX_PACKET_LENGTH/2) - 1) |
| 1890 | len = (MAX_PACKET_LENGTH/2) - 1; |
| 1891 | memtohex(buf + 1, (uint8_t *)msg, len); |
| 1892 | put_packet(s, buf); |
| 1893 | } |
| 1894 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 1895 | static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len) |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1896 | { |
| 1897 | const char *p = (const char *)buf; |
| 1898 | int max_sz; |
| 1899 | |
| 1900 | max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2; |
| 1901 | for (;;) { |
| 1902 | if (len <= max_sz) { |
| 1903 | gdb_monitor_output(gdbserver_state, p, len); |
| 1904 | break; |
| 1905 | } |
| 1906 | gdb_monitor_output(gdbserver_state, p, max_sz); |
| 1907 | p += max_sz; |
| 1908 | len -= max_sz; |
| 1909 | } |
| 1910 | return len; |
| 1911 | } |
| 1912 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1913 | #ifndef _WIN32 |
| 1914 | static void gdb_sigterm_handler(int signal) |
| 1915 | { |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 1916 | if (runstate_is_running()) { |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1917 | vm_stop(RUN_STATE_PAUSED); |
Jan Kiszka | e07bbac | 2011-02-09 16:29:40 +0100 | [diff] [blame] | 1918 | } |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1919 | } |
| 1920 | #endif |
| 1921 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 1922 | static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend, |
| 1923 | bool *be_opened, Error **errp) |
| 1924 | { |
| 1925 | *be_opened = false; |
| 1926 | } |
| 1927 | |
| 1928 | static void char_gdb_class_init(ObjectClass *oc, void *data) |
| 1929 | { |
| 1930 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 1931 | |
| 1932 | cc->internal = true; |
| 1933 | cc->open = gdb_monitor_open; |
| 1934 | cc->chr_write = gdb_monitor_write; |
| 1935 | } |
| 1936 | |
| 1937 | #define TYPE_CHARDEV_GDB "chardev-gdb" |
| 1938 | |
| 1939 | static const TypeInfo char_gdb_type_info = { |
| 1940 | .name = TYPE_CHARDEV_GDB, |
| 1941 | .parent = TYPE_CHARDEV, |
| 1942 | .class_init = char_gdb_class_init, |
| 1943 | }; |
| 1944 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1945 | int gdbserver_start(const char *device) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1946 | { |
| 1947 | GDBState *s; |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1948 | char gdbstub_device_name[128]; |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 1949 | Chardev *chr = NULL; |
| 1950 | Chardev *mon_chr; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1951 | |
Ziyue Yang | 508b4ec | 2017-01-18 16:02:41 +0800 | [diff] [blame] | 1952 | if (!first_cpu) { |
| 1953 | error_report("gdbstub: meaningless to attach gdb to a " |
| 1954 | "machine without any CPU."); |
| 1955 | return -1; |
| 1956 | } |
| 1957 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1958 | if (!device) |
| 1959 | return -1; |
| 1960 | if (strcmp(device, "none") != 0) { |
| 1961 | if (strstart(device, "tcp:", NULL)) { |
| 1962 | /* enforce required TCP attributes */ |
| 1963 | snprintf(gdbstub_device_name, sizeof(gdbstub_device_name), |
| 1964 | "%s,nowait,nodelay,server", device); |
| 1965 | device = gdbstub_device_name; |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1966 | } |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1967 | #ifndef _WIN32 |
| 1968 | else if (strcmp(device, "stdio") == 0) { |
| 1969 | struct sigaction act; |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 1970 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1971 | memset(&act, 0, sizeof(act)); |
| 1972 | act.sa_handler = gdb_sigterm_handler; |
| 1973 | sigaction(SIGINT, &act, NULL); |
| 1974 | } |
| 1975 | #endif |
Marc-André Lureau | b4948be | 2016-10-22 12:52:46 +0300 | [diff] [blame] | 1976 | chr = qemu_chr_new_noreplay("gdb", device); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1977 | if (!chr) |
| 1978 | return -1; |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1981 | s = gdbserver_state; |
| 1982 | if (!s) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1983 | s = g_malloc0(sizeof(GDBState)); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1984 | gdbserver_state = s; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1985 | |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1986 | qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL); |
| 1987 | |
| 1988 | /* Initialize a monitor terminal for gdb */ |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 1989 | mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB, |
| 1990 | NULL, &error_abort); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1991 | monitor_init(mon_chr, 0); |
| 1992 | } else { |
Marc-André Lureau | 1ce2610 | 2017-01-27 00:49:13 +0400 | [diff] [blame] | 1993 | qemu_chr_fe_deinit(&s->chr, true); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1994 | mon_chr = s->mon_chr; |
| 1995 | memset(s, 0, sizeof(GDBState)); |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 1996 | s->mon_chr = mon_chr; |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1997 | } |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1998 | s->c_cpu = first_cpu; |
| 1999 | s->g_cpu = first_cpu; |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 2000 | if (chr) { |
| 2001 | qemu_chr_fe_init(&s->chr, chr, &error_abort); |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 2002 | qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive, |
Anton Nefedov | 81517ba | 2017-07-06 15:08:49 +0300 | [diff] [blame] | 2003 | gdb_chr_event, NULL, NULL, NULL, true); |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 2004 | } |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 2005 | s->state = chr ? RS_IDLE : RS_INACTIVE; |
| 2006 | s->mon_chr = mon_chr; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 2007 | s->current_syscall_cb = NULL; |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 2008 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2009 | return 0; |
| 2010 | } |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 2011 | |
| 2012 | static void register_types(void) |
| 2013 | { |
| 2014 | type_register_static(&char_gdb_type_info); |
| 2015 | } |
| 2016 | |
| 2017 | type_init(register_types); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 2018 | #endif |