bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * gdb server stub |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2005 Fabrice Bellard |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 18 | */ |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 19 | #include "qemu/osdep.h" |
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 |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 22 | |
| 23 | #include "qemu.h" |
| 24 | #else |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 25 | #include "monitor/monitor.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 26 | #include "sysemu/char.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 27 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 28 | #include "exec/gdbstub.h" |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 29 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 30 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 31 | #define MAX_PACKET_LENGTH 4096 |
| 32 | |
Blue Swirl | 2b41f10 | 2011-06-19 20:38:22 +0000 | [diff] [blame] | 33 | #include "cpu.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 34 | #include "qemu/sockets.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 35 | #include "sysemu/kvm.h" |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 36 | #include "exec/semihost.h" |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 37 | |
Jan Kiszka | a391938 | 2015-02-07 09:38:44 +0100 | [diff] [blame] | 38 | #ifdef CONFIG_USER_ONLY |
| 39 | #define GDB_ATTACHED "0" |
| 40 | #else |
| 41 | #define GDB_ATTACHED "1" |
| 42 | #endif |
| 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 | |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 354 | /* Decide if either remote gdb syscalls or native file IO should be used. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 355 | int use_gdb_syscalls(void) |
| 356 | { |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 357 | SemihostingTarget target = semihosting_get_target(); |
| 358 | if (target == SEMIHOSTING_TARGET_NATIVE) { |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 359 | /* -semihosting-config target=native */ |
| 360 | return false; |
Leon Alrae | cfe67ce | 2015-06-19 14:17:45 +0100 | [diff] [blame] | 361 | } else if (target == SEMIHOSTING_TARGET_GDB) { |
Liviu Ionescu | a38bb07 | 2014-12-11 12:07:48 +0000 | [diff] [blame] | 362 | /* -semihosting-config target=gdb */ |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | /* -semihosting-config target=auto */ |
| 367 | /* On the first call check if gdb is connected and remember. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 368 | if (gdb_syscall_mode == GDB_SYS_UNKNOWN) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 369 | gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED |
| 370 | : GDB_SYS_DISABLED); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 371 | } |
| 372 | return gdb_syscall_mode == GDB_SYS_ENABLED; |
| 373 | } |
| 374 | |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 375 | /* Resume execution. */ |
| 376 | static inline void gdb_continue(GDBState *s) |
| 377 | { |
| 378 | #ifdef CONFIG_USER_ONLY |
| 379 | s->running_state = 1; |
| 380 | #else |
Paolo Bonzini | 26ac7a3 | 2013-06-03 17:06:54 +0200 | [diff] [blame] | 381 | if (!runstate_needs_reset()) { |
Paolo Bonzini | 87f25c1 | 2013-05-30 13:20:40 +0200 | [diff] [blame] | 382 | vm_start(); |
| 383 | } |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 384 | #endif |
| 385 | } |
| 386 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 387 | static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 388 | { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 389 | #ifdef CONFIG_USER_ONLY |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 390 | int ret; |
| 391 | |
| 392 | while (len > 0) { |
bellard | 8f447cc | 2006-06-14 15:21:14 +0000 | [diff] [blame] | 393 | ret = send(s->fd, buf, len, 0); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 394 | if (ret < 0) { |
| 395 | if (errno != EINTR && errno != EAGAIN) |
| 396 | return; |
| 397 | } else { |
| 398 | buf += ret; |
| 399 | len -= ret; |
| 400 | } |
| 401 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 402 | #else |
Anthony Liguori | 2cc6e0a | 2011-08-15 11:17:28 -0500 | [diff] [blame] | 403 | qemu_chr_fe_write(s->chr, buf, len); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 404 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static inline int fromhex(int v) |
| 408 | { |
| 409 | if (v >= '0' && v <= '9') |
| 410 | return v - '0'; |
| 411 | else if (v >= 'A' && v <= 'F') |
| 412 | return v - 'A' + 10; |
| 413 | else if (v >= 'a' && v <= 'f') |
| 414 | return v - 'a' + 10; |
| 415 | else |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static inline int tohex(int v) |
| 420 | { |
| 421 | if (v < 10) |
| 422 | return v + '0'; |
| 423 | else |
| 424 | return v - 10 + 'a'; |
| 425 | } |
| 426 | |
| 427 | static void memtohex(char *buf, const uint8_t *mem, int len) |
| 428 | { |
| 429 | int i, c; |
| 430 | char *q; |
| 431 | q = buf; |
| 432 | for(i = 0; i < len; i++) { |
| 433 | c = mem[i]; |
| 434 | *q++ = tohex(c >> 4); |
| 435 | *q++ = tohex(c & 0xf); |
| 436 | } |
| 437 | *q = '\0'; |
| 438 | } |
| 439 | |
| 440 | static void hextomem(uint8_t *mem, const char *buf, int len) |
| 441 | { |
| 442 | int i; |
| 443 | |
| 444 | for(i = 0; i < len; i++) { |
| 445 | mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); |
| 446 | buf += 2; |
| 447 | } |
| 448 | } |
| 449 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 450 | /* return -1 if error, 0 if OK */ |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 451 | static int put_packet_binary(GDBState *s, const char *buf, int len) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 452 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 453 | int csum, i; |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 454 | uint8_t *p; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 455 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 456 | for(;;) { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 457 | p = s->last_packet; |
| 458 | *(p++) = '$'; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 459 | memcpy(p, buf, len); |
| 460 | p += len; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 461 | csum = 0; |
| 462 | for(i = 0; i < len; i++) { |
| 463 | csum += buf[i]; |
| 464 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 465 | *(p++) = '#'; |
| 466 | *(p++) = tohex((csum >> 4) & 0xf); |
| 467 | *(p++) = tohex((csum) & 0xf); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 468 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 469 | s->last_packet_len = p - s->last_packet; |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 470 | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 471 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 472 | #ifdef CONFIG_USER_ONLY |
| 473 | i = get_char(s); |
| 474 | if (i < 0) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 475 | return -1; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 476 | if (i == '+') |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 477 | break; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 478 | #else |
| 479 | break; |
| 480 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 481 | } |
| 482 | return 0; |
| 483 | } |
| 484 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 485 | /* return -1 if error, 0 if OK */ |
| 486 | static int put_packet(GDBState *s, const char *buf) |
| 487 | { |
| 488 | #ifdef DEBUG_GDB |
| 489 | printf("reply='%s'\n", buf); |
| 490 | #endif |
| 491 | |
| 492 | return put_packet_binary(s, buf, strlen(buf)); |
| 493 | } |
| 494 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 495 | /* Encode data using the encoding for 'x' packets. */ |
| 496 | static int memtox(char *buf, const char *mem, int len) |
| 497 | { |
| 498 | char *p = buf; |
| 499 | char c; |
| 500 | |
| 501 | while (len--) { |
| 502 | c = *(mem++); |
| 503 | switch (c) { |
| 504 | case '#': case '$': case '*': case '}': |
| 505 | *(p++) = '}'; |
| 506 | *(p++) = c ^ 0x20; |
| 507 | break; |
| 508 | default: |
| 509 | *(p++) = c; |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | return p - buf; |
| 514 | } |
| 515 | |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 516 | static const char *get_feature_xml(const char *p, const char **newp, |
| 517 | CPUClass *cc) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 518 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 519 | size_t len; |
| 520 | int i; |
| 521 | const char *name; |
| 522 | static char target_xml[1024]; |
| 523 | |
| 524 | len = 0; |
| 525 | while (p[len] && p[len] != ':') |
| 526 | len++; |
| 527 | *newp = p + len; |
| 528 | |
| 529 | name = NULL; |
| 530 | if (strncmp(p, "target.xml", len) == 0) { |
| 531 | /* Generate the XML description for this CPU. */ |
| 532 | if (!target_xml[0]) { |
| 533 | GDBRegisterState *r; |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 534 | CPUState *cpu = first_cpu; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 535 | |
David Hildenbrand | b3820e6 | 2015-12-03 13:14:41 +0100 | [diff] [blame] | 536 | pstrcat(target_xml, sizeof(target_xml), |
| 537 | "<?xml version=\"1.0\"?>" |
| 538 | "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">" |
| 539 | "<target>"); |
| 540 | if (cc->gdb_arch_name) { |
| 541 | gchar *arch = cc->gdb_arch_name(cpu); |
| 542 | pstrcat(target_xml, sizeof(target_xml), "<architecture>"); |
| 543 | pstrcat(target_xml, sizeof(target_xml), arch); |
| 544 | pstrcat(target_xml, sizeof(target_xml), "</architecture>"); |
| 545 | g_free(arch); |
| 546 | } |
| 547 | pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\""); |
| 548 | pstrcat(target_xml, sizeof(target_xml), cc->gdb_core_xml_file); |
| 549 | pstrcat(target_xml, sizeof(target_xml), "\"/>"); |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 550 | for (r = cpu->gdb_regs; r; r = r->next) { |
blueswir1 | 2dc766d | 2009-04-13 16:06:19 +0000 | [diff] [blame] | 551 | pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\""); |
| 552 | pstrcat(target_xml, sizeof(target_xml), r->xml); |
| 553 | pstrcat(target_xml, sizeof(target_xml), "\"/>"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 554 | } |
blueswir1 | 2dc766d | 2009-04-13 16:06:19 +0000 | [diff] [blame] | 555 | pstrcat(target_xml, sizeof(target_xml), "</target>"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 556 | } |
| 557 | return target_xml; |
| 558 | } |
| 559 | for (i = 0; ; i++) { |
| 560 | name = xml_builtin[i][0]; |
| 561 | if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len)) |
| 562 | break; |
| 563 | } |
| 564 | return name ? xml_builtin[i][1] : NULL; |
| 565 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 566 | |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 567 | static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 568 | { |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 569 | CPUClass *cc = CPU_GET_CLASS(cpu); |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 570 | CPUArchState *env = cpu->env_ptr; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 571 | GDBRegisterState *r; |
| 572 | |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 573 | if (reg < cc->gdb_num_core_regs) { |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 574 | return cc->gdb_read_register(cpu, mem_buf, reg); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 575 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 576 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 577 | for (r = cpu->gdb_regs; r; r = r->next) { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 578 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
| 579 | return r->get_reg(env, mem_buf, reg - r->base_reg); |
| 580 | } |
| 581 | } |
| 582 | return 0; |
| 583 | } |
| 584 | |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 585 | static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 586 | { |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 587 | CPUClass *cc = CPU_GET_CLASS(cpu); |
Andreas Färber | 385b9f0 | 2013-06-27 18:25:36 +0200 | [diff] [blame] | 588 | CPUArchState *env = cpu->env_ptr; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 589 | GDBRegisterState *r; |
| 590 | |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 591 | if (reg < cc->gdb_num_core_regs) { |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 592 | return cc->gdb_write_register(cpu, mem_buf, reg); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 593 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 594 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 595 | for (r = cpu->gdb_regs; r; r = r->next) { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 596 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
| 597 | return r->set_reg(env, mem_buf, reg - r->base_reg); |
| 598 | } |
| 599 | } |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /* Register a supplemental set of CPU registers. If g_pos is nonzero it |
| 604 | specifies the first register number and these registers are included in |
| 605 | a standard "g" packet. Direction is relative to gdb, i.e. get_reg is |
| 606 | gdb reading a CPU register, and set_reg is gdb modifying a CPU register. |
| 607 | */ |
| 608 | |
Andreas Färber | 22169d4 | 2013-06-28 21:27:39 +0200 | [diff] [blame] | 609 | void gdb_register_coprocessor(CPUState *cpu, |
| 610 | gdb_reg_cb get_reg, gdb_reg_cb set_reg, |
| 611 | int num_regs, const char *xml, int g_pos) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 612 | { |
| 613 | GDBRegisterState *s; |
| 614 | GDBRegisterState **p; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 615 | |
Andreas Färber | eac8b35 | 2013-06-28 21:11:37 +0200 | [diff] [blame] | 616 | p = &cpu->gdb_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 617 | while (*p) { |
| 618 | /* Check for duplicates. */ |
| 619 | if (strcmp((*p)->xml, xml) == 0) |
| 620 | return; |
| 621 | p = &(*p)->next; |
| 622 | } |
Stefan Weil | 9643c25 | 2011-10-18 22:25:38 +0200 | [diff] [blame] | 623 | |
| 624 | s = g_new0(GDBRegisterState, 1); |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 625 | s->base_reg = cpu->gdb_num_regs; |
Stefan Weil | 9643c25 | 2011-10-18 22:25:38 +0200 | [diff] [blame] | 626 | s->num_regs = num_regs; |
| 627 | s->get_reg = get_reg; |
| 628 | s->set_reg = set_reg; |
| 629 | s->xml = xml; |
| 630 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 631 | /* Add to end of list. */ |
Andreas Färber | a0e372f | 2013-06-28 23:18:47 +0200 | [diff] [blame] | 632 | cpu->gdb_num_regs += num_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 633 | *p = s; |
| 634 | if (g_pos) { |
| 635 | if (g_pos != s->base_reg) { |
| 636 | fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n" |
| 637 | "Expected %d got %d\n", xml, g_pos, s->base_reg); |
Andreas Färber | 35143f0 | 2013-08-12 18:09:47 +0200 | [diff] [blame] | 638 | } else { |
| 639 | cpu->gdb_num_g_regs = cpu->gdb_num_regs; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 644 | #ifndef CONFIG_USER_ONLY |
Peter Maydell | 2472b6c | 2014-09-12 19:04:17 +0100 | [diff] [blame] | 645 | /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */ |
| 646 | static inline int xlat_gdb_type(CPUState *cpu, int gdbtype) |
| 647 | { |
| 648 | static const int xlat[] = { |
| 649 | [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE, |
| 650 | [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ, |
| 651 | [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS, |
| 652 | }; |
| 653 | |
| 654 | CPUClass *cc = CPU_GET_CLASS(cpu); |
| 655 | int cputype = xlat[gdbtype]; |
| 656 | |
| 657 | if (cc->gdb_stop_before_watchpoint) { |
| 658 | cputype |= BP_STOP_BEFORE_ACCESS; |
| 659 | } |
| 660 | return cputype; |
| 661 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 662 | #endif |
| 663 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 664 | static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type) |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 665 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 666 | CPUState *cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 667 | int err = 0; |
| 668 | |
Andreas Färber | 6227881 | 2013-06-27 17:12:06 +0200 | [diff] [blame] | 669 | if (kvm_enabled()) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 670 | return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type); |
Andreas Färber | 6227881 | 2013-06-27 17:12:06 +0200 | [diff] [blame] | 671 | } |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 672 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 673 | switch (type) { |
| 674 | case GDB_BREAKPOINT_SW: |
| 675 | case GDB_BREAKPOINT_HW: |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 676 | CPU_FOREACH(cpu) { |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 677 | err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL); |
| 678 | if (err) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 679 | break; |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 680 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 681 | } |
| 682 | return err; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 683 | #ifndef CONFIG_USER_ONLY |
| 684 | case GDB_WATCHPOINT_WRITE: |
| 685 | case GDB_WATCHPOINT_READ: |
| 686 | case GDB_WATCHPOINT_ACCESS: |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 687 | CPU_FOREACH(cpu) { |
Peter Maydell | 2472b6c | 2014-09-12 19:04:17 +0100 | [diff] [blame] | 688 | err = cpu_watchpoint_insert(cpu, addr, len, |
| 689 | xlat_gdb_type(cpu, type), NULL); |
| 690 | if (err) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 691 | break; |
Peter Maydell | 2472b6c | 2014-09-12 19:04:17 +0100 | [diff] [blame] | 692 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 693 | } |
| 694 | return err; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 695 | #endif |
| 696 | default: |
| 697 | return -ENOSYS; |
| 698 | } |
| 699 | } |
| 700 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 701 | static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type) |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 702 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 703 | CPUState *cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 704 | int err = 0; |
| 705 | |
Andreas Färber | 6227881 | 2013-06-27 17:12:06 +0200 | [diff] [blame] | 706 | if (kvm_enabled()) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 707 | return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type); |
Andreas Färber | 6227881 | 2013-06-27 17:12:06 +0200 | [diff] [blame] | 708 | } |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 709 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 710 | switch (type) { |
| 711 | case GDB_BREAKPOINT_SW: |
| 712 | case GDB_BREAKPOINT_HW: |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 713 | CPU_FOREACH(cpu) { |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 714 | err = cpu_breakpoint_remove(cpu, addr, BP_GDB); |
| 715 | if (err) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 716 | break; |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 717 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 718 | } |
| 719 | return err; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 720 | #ifndef CONFIG_USER_ONLY |
| 721 | case GDB_WATCHPOINT_WRITE: |
| 722 | case GDB_WATCHPOINT_READ: |
| 723 | case GDB_WATCHPOINT_ACCESS: |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 724 | CPU_FOREACH(cpu) { |
Peter Maydell | 2472b6c | 2014-09-12 19:04:17 +0100 | [diff] [blame] | 725 | err = cpu_watchpoint_remove(cpu, addr, len, |
| 726 | xlat_gdb_type(cpu, type)); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 727 | if (err) |
| 728 | break; |
| 729 | } |
| 730 | return err; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 731 | #endif |
| 732 | default: |
| 733 | return -ENOSYS; |
| 734 | } |
| 735 | } |
| 736 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 737 | static void gdb_breakpoint_remove_all(void) |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 738 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 739 | CPUState *cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 740 | |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 741 | if (kvm_enabled()) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 742 | kvm_remove_all_breakpoints(gdbserver_state->c_cpu); |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 743 | return; |
| 744 | } |
| 745 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 746 | CPU_FOREACH(cpu) { |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 747 | cpu_breakpoint_remove_all(cpu, BP_GDB); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 748 | #ifndef CONFIG_USER_ONLY |
Andreas Färber | 75a3403 | 2013-09-02 16:57:02 +0200 | [diff] [blame] | 749 | cpu_watchpoint_remove_all(cpu, BP_GDB); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 750 | #endif |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 751 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 752 | } |
| 753 | |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 754 | static void gdb_set_cpu_pc(GDBState *s, target_ulong pc) |
| 755 | { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 756 | CPUState *cpu = s->c_cpu; |
Andreas Färber | f45748f | 2013-06-21 19:09:18 +0200 | [diff] [blame] | 757 | |
| 758 | cpu_synchronize_state(cpu); |
Peter Crosthwaite | 4a2b24e | 2015-06-23 20:19:21 -0700 | [diff] [blame] | 759 | cpu_set_pc(cpu, pc); |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 762 | static CPUState *find_cpu(uint32_t thread_id) |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 763 | { |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 764 | CPUState *cpu; |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 765 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 766 | CPU_FOREACH(cpu) { |
Andreas Färber | aa48dd9 | 2013-07-09 20:50:52 +0200 | [diff] [blame] | 767 | if (cpu_index(cpu) == thread_id) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 768 | return cpu; |
Andreas Färber | aa48dd9 | 2013-07-09 20:50:52 +0200 | [diff] [blame] | 769 | } |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 770 | } |
Andreas Färber | aa48dd9 | 2013-07-09 20:50:52 +0200 | [diff] [blame] | 771 | |
| 772 | return NULL; |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Jan Kiszka | 4dabe74 | 2015-02-07 09:38:43 +0100 | [diff] [blame] | 775 | static int is_query_packet(const char *p, const char *query, char separator) |
| 776 | { |
| 777 | unsigned int query_len = strlen(query); |
| 778 | |
| 779 | return strncmp(p, query, query_len) == 0 && |
| 780 | (p[query_len] == '\0' || p[query_len] == separator); |
| 781 | } |
| 782 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 783 | static int gdb_handle_packet(GDBState *s, const char *line_buf) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 784 | { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 785 | CPUState *cpu; |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 786 | CPUClass *cc; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 787 | const char *p; |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 788 | uint32_t thread; |
| 789 | int ch, reg_size, type, res; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 790 | char buf[MAX_PACKET_LENGTH]; |
| 791 | uint8_t mem_buf[MAX_PACKET_LENGTH]; |
| 792 | uint8_t *registers; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 793 | target_ulong addr, len; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 794 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 795 | #ifdef DEBUG_GDB |
| 796 | printf("command='%s'\n", line_buf); |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 797 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 798 | p = line_buf; |
| 799 | ch = *p++; |
| 800 | switch(ch) { |
| 801 | case '?': |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 802 | /* TODO: Make this return the correct value for user-mode. */ |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 803 | snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP, |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 804 | cpu_index(s->c_cpu)); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 805 | put_packet(s, buf); |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 806 | /* Remove all the breakpoints when this query is issued, |
| 807 | * because gdb is doing and initial connect and the state |
| 808 | * should be cleaned up. |
| 809 | */ |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 810 | gdb_breakpoint_remove_all(); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 811 | break; |
| 812 | case 'c': |
| 813 | if (*p != '\0') { |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 814 | addr = strtoull(p, (char **)&p, 16); |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 815 | gdb_set_cpu_pc(s, addr); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 816 | } |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 817 | s->signal = 0; |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 818 | gdb_continue(s); |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 819 | return RS_IDLE; |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 820 | case 'C': |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 821 | s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16)); |
| 822 | if (s->signal == -1) |
| 823 | s->signal = 0; |
edgar_igl | 1f487ee | 2008-05-17 22:20:53 +0000 | [diff] [blame] | 824 | gdb_continue(s); |
| 825 | return RS_IDLE; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 826 | case 'v': |
| 827 | if (strncmp(p, "Cont", 4) == 0) { |
| 828 | int res_signal, res_thread; |
| 829 | |
| 830 | p += 4; |
| 831 | if (*p == '?') { |
| 832 | put_packet(s, "vCont;c;C;s;S"); |
| 833 | break; |
| 834 | } |
| 835 | res = 0; |
| 836 | res_signal = 0; |
| 837 | res_thread = 0; |
| 838 | while (*p) { |
| 839 | int action, signal; |
| 840 | |
| 841 | if (*p++ != ';') { |
| 842 | res = 0; |
| 843 | break; |
| 844 | } |
| 845 | action = *p++; |
| 846 | signal = 0; |
| 847 | if (action == 'C' || action == 'S') { |
Martin Simmons | f17b069 | 2014-11-05 14:47:39 +0000 | [diff] [blame] | 848 | signal = gdb_signal_to_target(strtoul(p, (char **)&p, 16)); |
| 849 | if (signal == -1) { |
| 850 | signal = 0; |
| 851 | } |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 852 | } else if (action != 'c' && action != 's') { |
| 853 | res = 0; |
| 854 | break; |
| 855 | } |
| 856 | thread = 0; |
| 857 | if (*p == ':') { |
| 858 | thread = strtoull(p+1, (char **)&p, 16); |
| 859 | } |
| 860 | action = tolower(action); |
| 861 | if (res == 0 || (res == 'c' && action == 's')) { |
| 862 | res = action; |
| 863 | res_signal = signal; |
| 864 | res_thread = thread; |
| 865 | } |
| 866 | } |
| 867 | if (res) { |
| 868 | if (res_thread != -1 && res_thread != 0) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 869 | cpu = find_cpu(res_thread); |
| 870 | if (cpu == NULL) { |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 871 | put_packet(s, "E22"); |
| 872 | break; |
| 873 | } |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 874 | s->c_cpu = cpu; |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 875 | } |
| 876 | if (res == 's') { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 877 | cpu_single_step(s->c_cpu, sstep_flags); |
Jan Kiszka | dd32aa1 | 2009-06-27 09:53:51 +0200 | [diff] [blame] | 878 | } |
| 879 | s->signal = res_signal; |
| 880 | gdb_continue(s); |
| 881 | return RS_IDLE; |
| 882 | } |
| 883 | break; |
| 884 | } else { |
| 885 | goto unknown_command; |
| 886 | } |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 887 | case 'k': |
| 888 | /* Kill the target */ |
| 889 | fprintf(stderr, "\nQEMU: Terminated via GDBstub\n"); |
| 890 | exit(0); |
| 891 | case 'D': |
| 892 | /* Detach packet */ |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 893 | gdb_breakpoint_remove_all(); |
Daniel Gutson | 7ea06da | 2010-02-26 14:13:50 -0300 | [diff] [blame] | 894 | gdb_syscall_mode = GDB_SYS_DISABLED; |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 895 | gdb_continue(s); |
| 896 | put_packet(s, "OK"); |
| 897 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 898 | case 's': |
| 899 | if (*p != '\0') { |
ths | 8fac580 | 2007-07-12 10:05:07 +0000 | [diff] [blame] | 900 | addr = strtoull(p, (char **)&p, 16); |
aurel32 | fab9d28 | 2009-04-08 21:29:37 +0000 | [diff] [blame] | 901 | gdb_set_cpu_pc(s, addr); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 902 | } |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 903 | cpu_single_step(s->c_cpu, sstep_flags); |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 904 | gdb_continue(s); |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 905 | return RS_IDLE; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 906 | case 'F': |
| 907 | { |
| 908 | target_ulong ret; |
| 909 | target_ulong err; |
| 910 | |
| 911 | ret = strtoull(p, (char **)&p, 16); |
| 912 | if (*p == ',') { |
| 913 | p++; |
| 914 | err = strtoull(p, (char **)&p, 16); |
| 915 | } else { |
| 916 | err = 0; |
| 917 | } |
| 918 | if (*p == ',') |
| 919 | p++; |
| 920 | type = *p; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 921 | if (s->current_syscall_cb) { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 922 | s->current_syscall_cb(s->c_cpu, ret, err); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 923 | s->current_syscall_cb = NULL; |
| 924 | } |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 925 | if (type == 'C') { |
| 926 | put_packet(s, "T02"); |
| 927 | } else { |
edgar_igl | ba70a62 | 2008-03-14 06:10:42 +0000 | [diff] [blame] | 928 | gdb_continue(s); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 932 | case 'g': |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 933 | cpu_synchronize_state(s->g_cpu); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 934 | len = 0; |
Andreas Färber | 35143f0 | 2013-08-12 18:09:47 +0200 | [diff] [blame] | 935 | 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] | 936 | reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 937 | len += reg_size; |
| 938 | } |
| 939 | memtohex(buf, mem_buf, len); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 940 | put_packet(s, buf); |
| 941 | break; |
| 942 | case 'G': |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 943 | cpu_synchronize_state(s->g_cpu); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 944 | registers = mem_buf; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 945 | len = strlen(p) / 2; |
| 946 | hextomem((uint8_t *)registers, p, len); |
Andreas Färber | 35143f0 | 2013-08-12 18:09:47 +0200 | [diff] [blame] | 947 | 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] | 948 | reg_size = gdb_write_register(s->g_cpu, registers, addr); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 949 | len -= reg_size; |
| 950 | registers += reg_size; |
| 951 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 952 | put_packet(s, "OK"); |
| 953 | break; |
| 954 | case 'm': |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 955 | addr = strtoull(p, (char **)&p, 16); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 956 | if (*p == ',') |
| 957 | p++; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 958 | len = strtoull(p, NULL, 16); |
Kevin Wolf | 5accecb | 2015-10-13 09:38:50 +0200 | [diff] [blame] | 959 | |
| 960 | /* memtohex() doubles the required space */ |
| 961 | if (len > MAX_PACKET_LENGTH / 2) { |
| 962 | put_packet (s, "E22"); |
| 963 | break; |
| 964 | } |
| 965 | |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 966 | 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] | 967 | put_packet (s, "E14"); |
| 968 | } else { |
| 969 | memtohex(buf, mem_buf, len); |
| 970 | put_packet(s, buf); |
| 971 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 972 | break; |
| 973 | case 'M': |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 974 | addr = strtoull(p, (char **)&p, 16); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 975 | if (*p == ',') |
| 976 | p++; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 977 | len = strtoull(p, (char **)&p, 16); |
bellard | b328f87 | 2005-01-17 22:03:16 +0000 | [diff] [blame] | 978 | if (*p == ':') |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 979 | p++; |
Kevin Wolf | 5accecb | 2015-10-13 09:38:50 +0200 | [diff] [blame] | 980 | |
| 981 | /* hextomem() reads 2*len bytes */ |
| 982 | if (len > strlen(p) / 2) { |
| 983 | put_packet (s, "E22"); |
| 984 | break; |
| 985 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 986 | hextomem(mem_buf, p, len); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 987 | 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] | 988 | true) != 0) { |
bellard | 905f20b | 2005-04-26 21:09:55 +0000 | [diff] [blame] | 989 | put_packet(s, "E14"); |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 990 | } else { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 991 | put_packet(s, "OK"); |
Fabien Chouteau | 44520db | 2011-09-08 12:48:16 +0200 | [diff] [blame] | 992 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 993 | break; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 994 | case 'p': |
| 995 | /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable. |
| 996 | This works, but can be very slow. Anything new enough to |
| 997 | understand XML also knows how to use this properly. */ |
| 998 | if (!gdb_has_xml) |
| 999 | goto unknown_command; |
| 1000 | addr = strtoull(p, (char **)&p, 16); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1001 | reg_size = gdb_read_register(s->g_cpu, mem_buf, addr); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1002 | if (reg_size) { |
| 1003 | memtohex(buf, mem_buf, reg_size); |
| 1004 | put_packet(s, buf); |
| 1005 | } else { |
| 1006 | put_packet(s, "E14"); |
| 1007 | } |
| 1008 | break; |
| 1009 | case 'P': |
| 1010 | if (!gdb_has_xml) |
| 1011 | goto unknown_command; |
| 1012 | addr = strtoull(p, (char **)&p, 16); |
| 1013 | if (*p == '=') |
| 1014 | p++; |
| 1015 | reg_size = strlen(p) / 2; |
| 1016 | hextomem(mem_buf, p, reg_size); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1017 | gdb_write_register(s->g_cpu, mem_buf, addr); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1018 | put_packet(s, "OK"); |
| 1019 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1020 | case 'Z': |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1021 | case 'z': |
| 1022 | type = strtoul(p, (char **)&p, 16); |
| 1023 | if (*p == ',') |
| 1024 | p++; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1025 | addr = strtoull(p, (char **)&p, 16); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1026 | if (*p == ',') |
| 1027 | p++; |
bellard | 9d9754a | 2006-06-25 15:32:37 +0000 | [diff] [blame] | 1028 | len = strtoull(p, (char **)&p, 16); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1029 | if (ch == 'Z') |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1030 | res = gdb_breakpoint_insert(addr, len, type); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1031 | else |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1032 | res = gdb_breakpoint_remove(addr, len, type); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1033 | if (res >= 0) |
| 1034 | put_packet(s, "OK"); |
| 1035 | else if (res == -ENOSYS) |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1036 | put_packet(s, ""); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1037 | else |
| 1038 | put_packet(s, "E22"); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1039 | break; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1040 | case 'H': |
| 1041 | type = *p++; |
| 1042 | thread = strtoull(p, (char **)&p, 16); |
| 1043 | if (thread == -1 || thread == 0) { |
| 1044 | put_packet(s, "OK"); |
| 1045 | break; |
| 1046 | } |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1047 | cpu = find_cpu(thread); |
| 1048 | if (cpu == NULL) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1049 | put_packet(s, "E22"); |
| 1050 | break; |
| 1051 | } |
| 1052 | switch (type) { |
| 1053 | case 'c': |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1054 | s->c_cpu = cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1055 | put_packet(s, "OK"); |
| 1056 | break; |
| 1057 | case 'g': |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1058 | s->g_cpu = cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1059 | put_packet(s, "OK"); |
| 1060 | break; |
| 1061 | default: |
| 1062 | put_packet(s, "E22"); |
| 1063 | break; |
| 1064 | } |
| 1065 | break; |
| 1066 | case 'T': |
| 1067 | thread = strtoull(p, (char **)&p, 16); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1068 | cpu = find_cpu(thread); |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1069 | |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1070 | if (cpu != NULL) { |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1071 | put_packet(s, "OK"); |
| 1072 | } else { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1073 | put_packet(s, "E22"); |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1074 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1075 | break; |
pbrook | 978efd6 | 2006-06-17 18:30:42 +0000 | [diff] [blame] | 1076 | case 'q': |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1077 | case 'Q': |
| 1078 | /* parse any 'q' packets here */ |
| 1079 | if (!strcmp(p,"qemu.sstepbits")) { |
| 1080 | /* Query Breakpoint bit definitions */ |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1081 | snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", |
| 1082 | SSTEP_ENABLE, |
| 1083 | SSTEP_NOIRQ, |
| 1084 | SSTEP_NOTIMER); |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1085 | put_packet(s, buf); |
| 1086 | break; |
Jan Kiszka | 4dabe74 | 2015-02-07 09:38:43 +0100 | [diff] [blame] | 1087 | } else if (is_query_packet(p, "qemu.sstep", '=')) { |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1088 | /* Display or change the sstep_flags */ |
| 1089 | p += 10; |
| 1090 | if (*p != '=') { |
| 1091 | /* Display current setting */ |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1092 | snprintf(buf, sizeof(buf), "0x%x", sstep_flags); |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1093 | put_packet(s, buf); |
| 1094 | break; |
| 1095 | } |
| 1096 | p++; |
| 1097 | type = strtoul(p, (char **)&p, 16); |
| 1098 | sstep_flags = type; |
| 1099 | put_packet(s, "OK"); |
| 1100 | break; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1101 | } else if (strcmp(p,"C") == 0) { |
| 1102 | /* "Current thread" remains vague in the spec, so always return |
| 1103 | * the first CPU (gdb returns the first thread). */ |
| 1104 | put_packet(s, "QC1"); |
| 1105 | break; |
| 1106 | } else if (strcmp(p,"fThreadInfo") == 0) { |
Andreas Färber | 52f3462 | 2013-06-27 13:44:40 +0200 | [diff] [blame] | 1107 | s->query_cpu = first_cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1108 | goto report_cpuinfo; |
| 1109 | } else if (strcmp(p,"sThreadInfo") == 0) { |
| 1110 | report_cpuinfo: |
| 1111 | if (s->query_cpu) { |
Andreas Färber | 52f3462 | 2013-06-27 13:44:40 +0200 | [diff] [blame] | 1112 | snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu)); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1113 | put_packet(s, buf); |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1114 | s->query_cpu = CPU_NEXT(s->query_cpu); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1115 | } else |
| 1116 | put_packet(s, "l"); |
| 1117 | break; |
| 1118 | } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) { |
| 1119 | thread = strtoull(p+16, (char **)&p, 16); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1120 | cpu = find_cpu(thread); |
| 1121 | if (cpu != NULL) { |
Andreas Färber | cb446ec | 2013-05-01 14:24:52 +0200 | [diff] [blame] | 1122 | cpu_synchronize_state(cpu); |
Kevin Wolf | 5accecb | 2015-10-13 09:38:50 +0200 | [diff] [blame] | 1123 | /* memtohex() doubles the required space */ |
| 1124 | len = snprintf((char *)mem_buf, sizeof(buf) / 2, |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 1125 | "CPU#%d [%s]", cpu->cpu_index, |
Andreas Färber | 259186a | 2013-01-17 18:51:17 +0100 | [diff] [blame] | 1126 | cpu->halted ? "halted " : "running"); |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 1127 | memtohex(buf, mem_buf, len); |
| 1128 | put_packet(s, buf); |
| 1129 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1130 | break; |
edgar_igl | 60897d3 | 2008-05-09 08:25:14 +0000 | [diff] [blame] | 1131 | } |
blueswir1 | 0b8a988 | 2009-03-07 10:51:36 +0000 | [diff] [blame] | 1132 | #ifdef CONFIG_USER_ONLY |
Jan Kiszka | 070949f | 2015-02-07 09:38:42 +0100 | [diff] [blame] | 1133 | else if (strcmp(p, "Offsets") == 0) { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 1134 | TaskState *ts = s->c_cpu->opaque; |
pbrook | 978efd6 | 2006-06-17 18:30:42 +0000 | [diff] [blame] | 1135 | |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1136 | snprintf(buf, sizeof(buf), |
| 1137 | "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx |
| 1138 | ";Bss=" TARGET_ABI_FMT_lx, |
| 1139 | ts->info->code_offset, |
| 1140 | ts->info->data_offset, |
| 1141 | ts->info->data_offset); |
pbrook | 978efd6 | 2006-06-17 18:30:42 +0000 | [diff] [blame] | 1142 | put_packet(s, buf); |
| 1143 | break; |
| 1144 | } |
blueswir1 | 0b8a988 | 2009-03-07 10:51:36 +0000 | [diff] [blame] | 1145 | #else /* !CONFIG_USER_ONLY */ |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1146 | else if (strncmp(p, "Rcmd,", 5) == 0) { |
| 1147 | int len = strlen(p + 5); |
| 1148 | |
| 1149 | if ((len % 2) != 0) { |
| 1150 | put_packet(s, "E01"); |
| 1151 | break; |
| 1152 | } |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1153 | len = len / 2; |
Kevin Wolf | 5accecb | 2015-10-13 09:38:50 +0200 | [diff] [blame] | 1154 | hextomem(mem_buf, p + 5, len); |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1155 | mem_buf[len++] = 0; |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 1156 | qemu_chr_be_write(s->mon_chr, mem_buf, len); |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1157 | put_packet(s, "OK"); |
| 1158 | break; |
| 1159 | } |
blueswir1 | 0b8a988 | 2009-03-07 10:51:36 +0000 | [diff] [blame] | 1160 | #endif /* !CONFIG_USER_ONLY */ |
Jan Kiszka | 4dabe74 | 2015-02-07 09:38:43 +0100 | [diff] [blame] | 1161 | if (is_query_packet(p, "Supported", ':')) { |
blueswir1 | 5b3715b | 2008-10-25 11:18:12 +0000 | [diff] [blame] | 1162 | snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH); |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 1163 | cc = CPU_GET_CLASS(first_cpu); |
| 1164 | if (cc->gdb_core_xml_file != NULL) { |
| 1165 | pstrcat(buf, sizeof(buf), ";qXfer:features:read+"); |
| 1166 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1167 | put_packet(s, buf); |
| 1168 | break; |
| 1169 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1170 | if (strncmp(p, "Xfer:features:read:", 19) == 0) { |
| 1171 | const char *xml; |
| 1172 | target_ulong total_len; |
| 1173 | |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 1174 | cc = CPU_GET_CLASS(first_cpu); |
| 1175 | if (cc->gdb_core_xml_file == NULL) { |
| 1176 | goto unknown_command; |
| 1177 | } |
| 1178 | |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 1179 | gdb_has_xml = true; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1180 | p += 19; |
Andreas Färber | 5b24c64 | 2013-07-07 15:08:22 +0200 | [diff] [blame] | 1181 | xml = get_feature_xml(p, &p, cc); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1182 | if (!xml) { |
blueswir1 | 5b3715b | 2008-10-25 11:18:12 +0000 | [diff] [blame] | 1183 | snprintf(buf, sizeof(buf), "E00"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1184 | put_packet(s, buf); |
| 1185 | break; |
| 1186 | } |
| 1187 | |
| 1188 | if (*p == ':') |
| 1189 | p++; |
| 1190 | addr = strtoul(p, (char **)&p, 16); |
| 1191 | if (*p == ',') |
| 1192 | p++; |
| 1193 | len = strtoul(p, (char **)&p, 16); |
| 1194 | |
| 1195 | total_len = strlen(xml); |
| 1196 | if (addr > total_len) { |
blueswir1 | 5b3715b | 2008-10-25 11:18:12 +0000 | [diff] [blame] | 1197 | snprintf(buf, sizeof(buf), "E00"); |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1198 | put_packet(s, buf); |
| 1199 | break; |
| 1200 | } |
| 1201 | if (len > (MAX_PACKET_LENGTH - 5) / 2) |
| 1202 | len = (MAX_PACKET_LENGTH - 5) / 2; |
| 1203 | if (len < total_len - addr) { |
| 1204 | buf[0] = 'm'; |
| 1205 | len = memtox(buf + 1, xml + addr, len); |
| 1206 | } else { |
| 1207 | buf[0] = 'l'; |
| 1208 | len = memtox(buf + 1, xml + addr, total_len - addr); |
| 1209 | } |
| 1210 | put_packet_binary(s, buf, len + 1); |
| 1211 | break; |
| 1212 | } |
Jan Kiszka | a391938 | 2015-02-07 09:38:44 +0100 | [diff] [blame] | 1213 | if (is_query_packet(p, "Attached", ':')) { |
| 1214 | put_packet(s, GDB_ATTACHED); |
| 1215 | break; |
| 1216 | } |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1217 | /* Unrecognised 'q' command. */ |
| 1218 | goto unknown_command; |
| 1219 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1220 | default: |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1221 | unknown_command: |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1222 | /* put empty packet */ |
| 1223 | buf[0] = '\0'; |
| 1224 | put_packet(s, buf); |
| 1225 | break; |
| 1226 | } |
| 1227 | return RS_IDLE; |
| 1228 | } |
| 1229 | |
Andreas Färber | 64f6b34 | 2013-05-27 02:06:09 +0200 | [diff] [blame] | 1230 | void gdb_set_stop_cpu(CPUState *cpu) |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1231 | { |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1232 | gdbserver_state->c_cpu = cpu; |
| 1233 | gdbserver_state->g_cpu = cpu; |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1236 | #ifndef CONFIG_USER_ONLY |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 1237 | static void gdb_vm_state_change(void *opaque, int running, RunState state) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1238 | { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1239 | GDBState *s = gdbserver_state; |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1240 | CPUState *cpu = s->c_cpu; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1241 | char buf[256]; |
aliguori | d6fc1b3 | 2008-11-18 19:55:44 +0000 | [diff] [blame] | 1242 | const char *type; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1243 | int ret; |
| 1244 | |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1245 | if (running || s->state == RS_INACTIVE) { |
| 1246 | return; |
| 1247 | } |
| 1248 | /* Is there a GDB syscall waiting to be sent? */ |
| 1249 | if (s->current_syscall_cb) { |
| 1250 | put_packet(s, s->syscall_buf); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1251 | return; |
Jan Kiszka | e07bbac | 2011-02-09 16:29:40 +0100 | [diff] [blame] | 1252 | } |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 1253 | switch (state) { |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1254 | case RUN_STATE_DEBUG: |
Andreas Färber | ff4700b | 2013-08-26 18:23:18 +0200 | [diff] [blame] | 1255 | if (cpu->watchpoint_hit) { |
| 1256 | switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1257 | case BP_MEM_READ: |
aliguori | d6fc1b3 | 2008-11-18 19:55:44 +0000 | [diff] [blame] | 1258 | type = "r"; |
| 1259 | break; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1260 | case BP_MEM_ACCESS: |
aliguori | d6fc1b3 | 2008-11-18 19:55:44 +0000 | [diff] [blame] | 1261 | type = "a"; |
| 1262 | break; |
| 1263 | default: |
| 1264 | type = ""; |
| 1265 | break; |
| 1266 | } |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1267 | snprintf(buf, sizeof(buf), |
| 1268 | "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";", |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 1269 | GDB_SIGNAL_TRAP, cpu_index(cpu), type, |
Andreas Färber | ff4700b | 2013-08-26 18:23:18 +0200 | [diff] [blame] | 1270 | (target_ulong)cpu->watchpoint_hit->vaddr); |
| 1271 | cpu->watchpoint_hit = NULL; |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1272 | goto send_packet; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1273 | } |
Peter Crosthwaite | bbd77c1 | 2015-06-23 19:31:15 -0700 | [diff] [blame] | 1274 | tb_flush(cpu); |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1275 | ret = GDB_SIGNAL_TRAP; |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1276 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1277 | case RUN_STATE_PAUSED: |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 1278 | ret = GDB_SIGNAL_INT; |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1279 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1280 | case RUN_STATE_SHUTDOWN: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1281 | ret = GDB_SIGNAL_QUIT; |
| 1282 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1283 | case RUN_STATE_IO_ERROR: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1284 | ret = GDB_SIGNAL_IO; |
| 1285 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1286 | case RUN_STATE_WATCHDOG: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1287 | ret = GDB_SIGNAL_ALRM; |
| 1288 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1289 | case RUN_STATE_INTERNAL_ERROR: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1290 | ret = GDB_SIGNAL_ABRT; |
| 1291 | break; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1292 | case RUN_STATE_SAVE_VM: |
| 1293 | case RUN_STATE_RESTORE_VM: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1294 | return; |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1295 | case RUN_STATE_FINISH_MIGRATE: |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1296 | ret = GDB_SIGNAL_XCPU; |
| 1297 | break; |
| 1298 | default: |
| 1299 | ret = GDB_SIGNAL_UNKNOWN; |
| 1300 | break; |
bellard | bbeb7b5 | 2006-04-23 18:42:15 +0000 | [diff] [blame] | 1301 | } |
Jan Kiszka | 226d007 | 2015-07-24 18:52:31 +0200 | [diff] [blame] | 1302 | gdb_set_stop_cpu(cpu); |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 1303 | snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu)); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1304 | |
| 1305 | send_packet: |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1306 | put_packet(s, buf); |
Jan Kiszka | 425189a | 2011-03-22 11:02:09 +0100 | [diff] [blame] | 1307 | |
| 1308 | /* disable single step if it was enabled */ |
Andreas Färber | 3825b28 | 2013-06-24 18:41:06 +0200 | [diff] [blame] | 1309 | cpu_single_step(cpu, 0); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1310 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1311 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1312 | |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1313 | /* Send a gdb syscall request. |
| 1314 | This accepts limited printf-style format specifiers, specifically: |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1315 | %x - target_ulong argument printed in hex. |
| 1316 | %lx - 64-bit argument printed in hex. |
| 1317 | %s - string pointer (target_ulong) and length (int) pair. */ |
Peter Maydell | 19239b3 | 2015-09-07 10:39:27 +0100 | [diff] [blame] | 1318 | void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va) |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1319 | { |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1320 | char *p; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1321 | char *p_end; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1322 | target_ulong addr; |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1323 | uint64_t i64; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1324 | GDBState *s; |
| 1325 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1326 | s = gdbserver_state; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1327 | if (!s) |
| 1328 | return; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1329 | s->current_syscall_cb = cb; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1330 | #ifndef CONFIG_USER_ONLY |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1331 | vm_stop(RUN_STATE_DEBUG); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1332 | #endif |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1333 | p = s->syscall_buf; |
| 1334 | p_end = &s->syscall_buf[sizeof(s->syscall_buf)]; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1335 | *(p++) = 'F'; |
| 1336 | while (*fmt) { |
| 1337 | if (*fmt == '%') { |
| 1338 | fmt++; |
| 1339 | switch (*fmt++) { |
| 1340 | case 'x': |
| 1341 | addr = va_arg(va, target_ulong); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1342 | p += snprintf(p, p_end - p, TARGET_FMT_lx, addr); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1343 | break; |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1344 | case 'l': |
| 1345 | if (*(fmt++) != 'x') |
| 1346 | goto bad_format; |
| 1347 | i64 = va_arg(va, uint64_t); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1348 | p += snprintf(p, p_end - p, "%" PRIx64, i64); |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1349 | break; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1350 | case 's': |
| 1351 | addr = va_arg(va, target_ulong); |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1352 | p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x", |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1353 | addr, va_arg(va, int)); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1354 | break; |
| 1355 | default: |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 1356 | bad_format: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1357 | fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n", |
| 1358 | fmt - 1); |
| 1359 | break; |
| 1360 | } |
| 1361 | } else { |
| 1362 | *(p++) = *(fmt++); |
| 1363 | } |
| 1364 | } |
pbrook | 8a93e02 | 2007-08-06 13:19:15 +0000 | [diff] [blame] | 1365 | *p = 0; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1366 | #ifdef CONFIG_USER_ONLY |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1367 | put_packet(s, s->syscall_buf); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1368 | gdb_handlesig(s->c_cpu, 0); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1369 | #else |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1370 | /* In this case wait to send the syscall packet until notification that |
| 1371 | the CPU has stopped. This must be done because if the packet is sent |
| 1372 | now the reply from the syscall request could be received while the CPU |
| 1373 | is still in the running state, which can cause packets to be dropped |
| 1374 | and state transition 'T' packets to be sent while the syscall is still |
| 1375 | being processed. */ |
Paolo Bonzini | 9102ded | 2015-08-18 06:52:09 -0700 | [diff] [blame] | 1376 | qemu_cpu_kick(s->c_cpu); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1377 | #endif |
| 1378 | } |
| 1379 | |
Peter Maydell | 19239b3 | 2015-09-07 10:39:27 +0100 | [diff] [blame] | 1380 | void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...) |
| 1381 | { |
| 1382 | va_list va; |
| 1383 | |
| 1384 | va_start(va, fmt); |
| 1385 | gdb_do_syscallv(cb, fmt, va); |
| 1386 | va_end(va); |
| 1387 | } |
| 1388 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1389 | static void gdb_read_byte(GDBState *s, int ch) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1390 | { |
| 1391 | int i, csum; |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 1392 | uint8_t reply; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1393 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1394 | #ifndef CONFIG_USER_ONLY |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1395 | if (s->last_packet_len) { |
| 1396 | /* Waiting for a response to the last packet. If we see the start |
| 1397 | of a new command then abandon the previous response. */ |
| 1398 | if (ch == '-') { |
| 1399 | #ifdef DEBUG_GDB |
| 1400 | printf("Got NACK, retransmitting\n"); |
| 1401 | #endif |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 1402 | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1403 | } |
| 1404 | #ifdef DEBUG_GDB |
| 1405 | else if (ch == '+') |
| 1406 | printf("Got ACK\n"); |
| 1407 | else |
| 1408 | printf("Got '%c' when expecting ACK/NACK\n", ch); |
| 1409 | #endif |
| 1410 | if (ch == '+' || ch == '$') |
| 1411 | s->last_packet_len = 0; |
| 1412 | if (ch != '$') |
| 1413 | return; |
| 1414 | } |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 1415 | if (runstate_is_running()) { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1416 | /* when the CPU is running, we cannot do anything except stop |
| 1417 | it when receiving a char */ |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1418 | vm_stop(RUN_STATE_PAUSED); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1419 | } else |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1420 | #endif |
bellard | 4162503 | 2005-04-24 10:07:11 +0000 | [diff] [blame] | 1421 | { |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1422 | switch(s->state) { |
| 1423 | case RS_IDLE: |
| 1424 | if (ch == '$') { |
| 1425 | s->line_buf_index = 0; |
| 1426 | s->state = RS_GETLINE; |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1427 | } |
| 1428 | break; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1429 | case RS_GETLINE: |
| 1430 | if (ch == '#') { |
| 1431 | s->state = RS_CHKSUM1; |
| 1432 | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
| 1433 | s->state = RS_IDLE; |
| 1434 | } else { |
| 1435 | s->line_buf[s->line_buf_index++] = ch; |
| 1436 | } |
| 1437 | break; |
| 1438 | case RS_CHKSUM1: |
| 1439 | s->line_buf[s->line_buf_index] = '\0'; |
| 1440 | s->line_csum = fromhex(ch) << 4; |
| 1441 | s->state = RS_CHKSUM2; |
| 1442 | break; |
| 1443 | case RS_CHKSUM2: |
| 1444 | s->line_csum |= fromhex(ch); |
| 1445 | csum = 0; |
| 1446 | for(i = 0; i < s->line_buf_index; i++) { |
| 1447 | csum += s->line_buf[i]; |
| 1448 | } |
| 1449 | if (s->line_csum != (csum & 0xff)) { |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 1450 | reply = '-'; |
| 1451 | put_buffer(s, &reply, 1); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1452 | s->state = RS_IDLE; |
| 1453 | } else { |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 1454 | reply = '+'; |
| 1455 | put_buffer(s, &reply, 1); |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1456 | s->state = gdb_handle_packet(s, s->line_buf); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1457 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1458 | break; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1459 | default: |
| 1460 | abort(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1461 | } |
| 1462 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 1465 | /* Tell the remote gdb that the process has exited. */ |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 1466 | void gdb_exit(CPUArchState *env, int code) |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 1467 | { |
| 1468 | GDBState *s; |
| 1469 | char buf[4]; |
| 1470 | |
| 1471 | s = gdbserver_state; |
| 1472 | if (!s) { |
| 1473 | return; |
| 1474 | } |
| 1475 | #ifdef CONFIG_USER_ONLY |
| 1476 | if (gdbserver_fd < 0 || s->fd < 0) { |
| 1477 | return; |
| 1478 | } |
Paolo Bonzini | 3d0f441 | 2015-03-02 13:26:58 +0100 | [diff] [blame] | 1479 | #else |
| 1480 | if (!s->chr) { |
| 1481 | return; |
| 1482 | } |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 1483 | #endif |
| 1484 | |
| 1485 | snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code); |
| 1486 | put_packet(s, buf); |
Fabien Chouteau | e2af15b | 2011-01-13 12:46:57 +0100 | [diff] [blame] | 1487 | |
| 1488 | #ifndef CONFIG_USER_ONLY |
Paolo Bonzini | 3d0f441 | 2015-03-02 13:26:58 +0100 | [diff] [blame] | 1489 | qemu_chr_delete(s->chr); |
Fabien Chouteau | e2af15b | 2011-01-13 12:46:57 +0100 | [diff] [blame] | 1490 | #endif |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 1491 | } |
| 1492 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1493 | #ifdef CONFIG_USER_ONLY |
| 1494 | int |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1495 | gdb_queuesig (void) |
| 1496 | { |
| 1497 | GDBState *s; |
| 1498 | |
| 1499 | s = gdbserver_state; |
| 1500 | |
| 1501 | if (gdbserver_fd < 0 || s->fd < 0) |
| 1502 | return 0; |
| 1503 | else |
| 1504 | return 1; |
| 1505 | } |
| 1506 | |
| 1507 | int |
Andreas Färber | db6b81d | 2013-06-27 19:49:31 +0200 | [diff] [blame] | 1508 | gdb_handlesig(CPUState *cpu, int sig) |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1509 | { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1510 | GDBState *s; |
| 1511 | char buf[256]; |
| 1512 | int n; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1513 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1514 | s = gdbserver_state; |
| 1515 | if (gdbserver_fd < 0 || s->fd < 0) { |
| 1516 | return sig; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1519 | /* disable single step if it was enabled */ |
Andreas Färber | 3825b28 | 2013-06-24 18:41:06 +0200 | [diff] [blame] | 1520 | cpu_single_step(cpu, 0); |
Peter Crosthwaite | bbd77c1 | 2015-06-23 19:31:15 -0700 | [diff] [blame] | 1521 | tb_flush(cpu); |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1522 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1523 | if (sig != 0) { |
| 1524 | snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig)); |
| 1525 | put_packet(s, buf); |
| 1526 | } |
| 1527 | /* put_packet() might have detected that the peer terminated the |
| 1528 | connection. */ |
| 1529 | if (s->fd < 0) { |
| 1530 | return sig; |
| 1531 | } |
| 1532 | |
| 1533 | sig = 0; |
| 1534 | s->state = RS_IDLE; |
| 1535 | s->running_state = 0; |
| 1536 | while (s->running_state == 0) { |
| 1537 | n = read(s->fd, buf, 256); |
| 1538 | if (n > 0) { |
| 1539 | int i; |
| 1540 | |
| 1541 | for (i = 0; i < n; i++) { |
| 1542 | gdb_read_byte(s, buf[i]); |
| 1543 | } |
| 1544 | } else if (n == 0 || errno != EAGAIN) { |
| 1545 | /* XXX: Connection closed. Should probably wait for another |
| 1546 | connection before continuing. */ |
| 1547 | return sig; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1548 | } |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1549 | } |
| 1550 | sig = s->signal; |
| 1551 | s->signal = 0; |
| 1552 | return sig; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1553 | } |
bellard | e900967 | 2005-04-26 20:42:36 +0000 | [diff] [blame] | 1554 | |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1555 | /* 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] | 1556 | void gdb_signalled(CPUArchState *env, int sig) |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1557 | { |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1558 | GDBState *s; |
| 1559 | char buf[4]; |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1560 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1561 | s = gdbserver_state; |
| 1562 | if (gdbserver_fd < 0 || s->fd < 0) { |
| 1563 | return; |
| 1564 | } |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1565 | |
Andreas Färber | 5ca666c | 2013-06-24 19:20:57 +0200 | [diff] [blame] | 1566 | snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig)); |
| 1567 | put_packet(s, buf); |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 1568 | } |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1569 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1570 | static void gdb_accept(void) |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1571 | { |
| 1572 | GDBState *s; |
| 1573 | struct sockaddr_in sockaddr; |
| 1574 | socklen_t len; |
MORITA Kazutaka | bf1c852 | 2013-02-22 12:39:50 +0900 | [diff] [blame] | 1575 | int fd; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1576 | |
| 1577 | for(;;) { |
| 1578 | len = sizeof(sockaddr); |
| 1579 | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len); |
| 1580 | if (fd < 0 && errno != EINTR) { |
| 1581 | perror("accept"); |
| 1582 | return; |
| 1583 | } else if (fd >= 0) { |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 1584 | #ifndef _WIN32 |
| 1585 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 1586 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1587 | break; |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | /* set short latency */ |
MORITA Kazutaka | bf1c852 | 2013-02-22 12:39:50 +0900 | [diff] [blame] | 1592 | socket_set_nodelay(fd); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1593 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1594 | s = g_malloc0(sizeof(GDBState)); |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1595 | s->c_cpu = first_cpu; |
| 1596 | s->g_cpu = first_cpu; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1597 | s->fd = fd; |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 1598 | gdb_has_xml = false; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1599 | |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1600 | gdbserver_state = s; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 1601 | |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1602 | fcntl(fd, F_SETFL, O_NONBLOCK); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | static int gdbserver_open(int port) |
| 1606 | { |
| 1607 | struct sockaddr_in sockaddr; |
Sebastian Ottlik | 6669ca1 | 2013-10-02 12:23:13 +0200 | [diff] [blame] | 1608 | int fd, ret; |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1609 | |
| 1610 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 1611 | if (fd < 0) { |
| 1612 | perror("socket"); |
| 1613 | return -1; |
| 1614 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 1615 | #ifndef _WIN32 |
| 1616 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 1617 | #endif |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1618 | |
Sebastian Ottlik | 6669ca1 | 2013-10-02 12:23:13 +0200 | [diff] [blame] | 1619 | socket_set_fast_reuse(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1620 | |
| 1621 | sockaddr.sin_family = AF_INET; |
| 1622 | sockaddr.sin_port = htons(port); |
| 1623 | sockaddr.sin_addr.s_addr = 0; |
| 1624 | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
| 1625 | if (ret < 0) { |
| 1626 | perror("bind"); |
Peter Maydell | bb16172 | 2011-12-24 23:37:24 +0000 | [diff] [blame] | 1627 | close(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1628 | return -1; |
| 1629 | } |
| 1630 | ret = listen(fd, 0); |
| 1631 | if (ret < 0) { |
| 1632 | perror("listen"); |
Peter Maydell | bb16172 | 2011-12-24 23:37:24 +0000 | [diff] [blame] | 1633 | close(fd); |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1634 | return -1; |
| 1635 | } |
bellard | 858693c | 2004-03-31 18:52:07 +0000 | [diff] [blame] | 1636 | return fd; |
| 1637 | } |
| 1638 | |
| 1639 | int gdbserver_start(int port) |
| 1640 | { |
| 1641 | gdbserver_fd = gdbserver_open(port); |
| 1642 | if (gdbserver_fd < 0) |
| 1643 | return -1; |
| 1644 | /* accept connections */ |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1645 | gdb_accept(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1646 | return 0; |
| 1647 | } |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 1648 | |
| 1649 | /* Disable gdb stub for child processes. */ |
Peter Crosthwaite | f7ec7f7 | 2015-06-23 19:31:16 -0700 | [diff] [blame] | 1650 | void gdbserver_fork(CPUState *cpu) |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 1651 | { |
| 1652 | GDBState *s = gdbserver_state; |
Andreas Färber | 75a3403 | 2013-09-02 16:57:02 +0200 | [diff] [blame] | 1653 | |
| 1654 | if (gdbserver_fd < 0 || s->fd < 0) { |
| 1655 | return; |
| 1656 | } |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 1657 | close(s->fd); |
| 1658 | s->fd = -1; |
Andreas Färber | b3310ab | 2013-09-02 17:26:20 +0200 | [diff] [blame] | 1659 | cpu_breakpoint_remove_all(cpu, BP_GDB); |
Andreas Färber | 75a3403 | 2013-09-02 16:57:02 +0200 | [diff] [blame] | 1660 | cpu_watchpoint_remove_all(cpu, BP_GDB); |
aurel32 | 2b1319c | 2008-12-18 22:44:04 +0000 | [diff] [blame] | 1661 | } |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1662 | #else |
ths | aa1f17c | 2007-07-11 22:48:58 +0000 | [diff] [blame] | 1663 | static int gdb_chr_can_receive(void *opaque) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1664 | { |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1665 | /* We can handle an arbitrarily large amount of data. |
| 1666 | Pick the maximum packet size, which is as good as anything. */ |
| 1667 | return MAX_PACKET_LENGTH; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
ths | aa1f17c | 2007-07-11 22:48:58 +0000 | [diff] [blame] | 1670 | static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1671 | { |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1672 | int i; |
| 1673 | |
| 1674 | for (i = 0; i < size; i++) { |
aliguori | 880a757 | 2008-11-18 20:30:24 +0000 | [diff] [blame] | 1675 | gdb_read_byte(gdbserver_state, buf[i]); |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | static void gdb_chr_event(void *opaque, int event) |
| 1680 | { |
| 1681 | switch (event) { |
Amit Shah | b6b8df5 | 2009-10-07 18:31:16 +0530 | [diff] [blame] | 1682 | case CHR_EVENT_OPENED: |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1683 | vm_stop(RUN_STATE_PAUSED); |
Andreas Färber | 5b50e79 | 2013-06-29 04:18:45 +0200 | [diff] [blame] | 1684 | gdb_has_xml = false; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1685 | break; |
| 1686 | default: |
| 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1691 | static void gdb_monitor_output(GDBState *s, const char *msg, int len) |
| 1692 | { |
| 1693 | char buf[MAX_PACKET_LENGTH]; |
| 1694 | |
| 1695 | buf[0] = 'O'; |
| 1696 | if (len > (MAX_PACKET_LENGTH/2) - 1) |
| 1697 | len = (MAX_PACKET_LENGTH/2) - 1; |
| 1698 | memtohex(buf + 1, (uint8_t *)msg, len); |
| 1699 | put_packet(s, buf); |
| 1700 | } |
| 1701 | |
| 1702 | static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1703 | { |
| 1704 | const char *p = (const char *)buf; |
| 1705 | int max_sz; |
| 1706 | |
| 1707 | max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2; |
| 1708 | for (;;) { |
| 1709 | if (len <= max_sz) { |
| 1710 | gdb_monitor_output(gdbserver_state, p, len); |
| 1711 | break; |
| 1712 | } |
| 1713 | gdb_monitor_output(gdbserver_state, p, max_sz); |
| 1714 | p += max_sz; |
| 1715 | len -= max_sz; |
| 1716 | } |
| 1717 | return len; |
| 1718 | } |
| 1719 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1720 | #ifndef _WIN32 |
| 1721 | static void gdb_sigterm_handler(int signal) |
| 1722 | { |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 1723 | if (runstate_is_running()) { |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1724 | vm_stop(RUN_STATE_PAUSED); |
Jan Kiszka | e07bbac | 2011-02-09 16:29:40 +0100 | [diff] [blame] | 1725 | } |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1726 | } |
| 1727 | #endif |
| 1728 | |
| 1729 | int gdbserver_start(const char *device) |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1730 | { |
| 1731 | GDBState *s; |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1732 | char gdbstub_device_name[128]; |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1733 | CharDriverState *chr = NULL; |
| 1734 | CharDriverState *mon_chr; |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1735 | ChardevCommon common = { 0 }; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1736 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1737 | if (!device) |
| 1738 | return -1; |
| 1739 | if (strcmp(device, "none") != 0) { |
| 1740 | if (strstart(device, "tcp:", NULL)) { |
| 1741 | /* enforce required TCP attributes */ |
| 1742 | snprintf(gdbstub_device_name, sizeof(gdbstub_device_name), |
| 1743 | "%s,nowait,nodelay,server", device); |
| 1744 | device = gdbstub_device_name; |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1745 | } |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1746 | #ifndef _WIN32 |
| 1747 | else if (strcmp(device, "stdio") == 0) { |
| 1748 | struct sigaction act; |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 1749 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1750 | memset(&act, 0, sizeof(act)); |
| 1751 | act.sa_handler = gdb_sigterm_handler; |
| 1752 | sigaction(SIGINT, &act, NULL); |
| 1753 | } |
| 1754 | #endif |
Anthony Liguori | 27143a4 | 2011-08-15 11:17:36 -0500 | [diff] [blame] | 1755 | chr = qemu_chr_new("gdb", device, NULL); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1756 | if (!chr) |
| 1757 | return -1; |
| 1758 | |
Hans de Goede | 456d606 | 2013-03-27 20:29:40 +0100 | [diff] [blame] | 1759 | qemu_chr_fe_claim_no_fail(chr); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1760 | qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive, |
| 1761 | gdb_chr_event, NULL); |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1764 | s = gdbserver_state; |
| 1765 | if (!s) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1766 | s = g_malloc0(sizeof(GDBState)); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1767 | gdbserver_state = s; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1768 | |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1769 | qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL); |
| 1770 | |
| 1771 | /* Initialize a monitor terminal for gdb */ |
Daniel P. Berrange | d0d7708 | 2016-01-11 12:44:41 +0000 | [diff] [blame] | 1772 | mon_chr = qemu_chr_alloc(&common, &error_abort); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1773 | mon_chr->chr_write = gdb_monitor_write; |
| 1774 | monitor_init(mon_chr, 0); |
| 1775 | } else { |
| 1776 | if (s->chr) |
Anthony Liguori | 70f24fb | 2011-08-15 11:17:38 -0500 | [diff] [blame] | 1777 | qemu_chr_delete(s->chr); |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1778 | mon_chr = s->mon_chr; |
| 1779 | memset(s, 0, sizeof(GDBState)); |
| 1780 | } |
Andreas Färber | 2e0f2cf | 2013-06-27 19:19:39 +0200 | [diff] [blame] | 1781 | s->c_cpu = first_cpu; |
| 1782 | s->g_cpu = first_cpu; |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1783 | s->chr = chr; |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1784 | s->state = chr ? RS_IDLE : RS_INACTIVE; |
| 1785 | s->mon_chr = mon_chr; |
Meador Inge | cdb432b | 2012-03-15 17:49:45 +0000 | [diff] [blame] | 1786 | s->current_syscall_cb = NULL; |
aliguori | 8a34a0f | 2009-03-05 23:01:55 +0000 | [diff] [blame] | 1787 | |
pbrook | 4046d91 | 2007-01-28 01:53:16 +0000 | [diff] [blame] | 1788 | return 0; |
| 1789 | } |
| 1790 | #endif |