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