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