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