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