]>
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 | */ |
856dfd8a | 19 | |
d38ea87a | 20 | #include "qemu/osdep.h" |
a8d25326 | 21 | #include "qemu-common.h" |
da34e65c | 22 | #include "qapi/error.h" |
508b4ecc | 23 | #include "qemu/error-report.h" |
856dfd8a | 24 | #include "qemu/ctype.h" |
f348b6d1 | 25 | #include "qemu/cutils.h" |
0b8fa32f | 26 | #include "qemu/module.h" |
5c9522b3 | 27 | #include "trace-root.h" |
f348b6d1 | 28 | #ifdef CONFIG_USER_ONLY |
1fddef4b FB |
29 | #include "qemu.h" |
30 | #else | |
83c9089e | 31 | #include "monitor/monitor.h" |
8228e353 | 32 | #include "chardev/char.h" |
4d43a603 | 33 | #include "chardev/char-fe.h" |
9c17d615 | 34 | #include "sysemu/sysemu.h" |
022c62cb | 35 | #include "exec/gdbstub.h" |
8f468636 | 36 | #include "hw/cpu/cluster.h" |
1fddef4b | 37 | #endif |
67b915a5 | 38 | |
56aebc89 PB |
39 | #define MAX_PACKET_LENGTH 4096 |
40 | ||
1de7afc9 | 41 | #include "qemu/sockets.h" |
b3946626 | 42 | #include "sysemu/hw_accel.h" |
9c17d615 | 43 | #include "sysemu/kvm.h" |
f1672e6f | 44 | #include "hw/semihosting/semihost.h" |
63c91552 | 45 | #include "exec/exec-all.h" |
ca587a8e | 46 | |
a3919386 JK |
47 | #ifdef CONFIG_USER_ONLY |
48 | #define GDB_ATTACHED "0" | |
49 | #else | |
50 | #define GDB_ATTACHED "1" | |
51 | #endif | |
52 | ||
f3659eee AF |
53 | static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr, |
54 | uint8_t *buf, int len, bool is_write) | |
44520db1 | 55 | { |
f3659eee AF |
56 | CPUClass *cc = CPU_GET_CLASS(cpu); |
57 | ||
58 | if (cc->memory_rw_debug) { | |
59 | return cc->memory_rw_debug(cpu, addr, buf, len, is_write); | |
60 | } | |
61 | return cpu_memory_rw_debug(cpu, addr, buf, len, is_write); | |
44520db1 | 62 | } |
ca587a8e | 63 | |
d2a6c857 AB |
64 | /* Return the GDB index for a given vCPU state. |
65 | * | |
66 | * For user mode this is simply the thread id. In system mode GDB | |
67 | * numbers CPUs from 1 as 0 is reserved as an "any cpu" index. | |
68 | */ | |
69 | static inline int cpu_gdb_index(CPUState *cpu) | |
70 | { | |
71 | #if defined(CONFIG_USER_ONLY) | |
bd88c780 AB |
72 | TaskState *ts = (TaskState *) cpu->opaque; |
73 | return ts->ts_tid; | |
d2a6c857 AB |
74 | #else |
75 | return cpu->cpu_index + 1; | |
76 | #endif | |
77 | } | |
78 | ||
ca587a8e AJ |
79 | enum { |
80 | GDB_SIGNAL_0 = 0, | |
81 | GDB_SIGNAL_INT = 2, | |
425189a8 | 82 | GDB_SIGNAL_QUIT = 3, |
ca587a8e | 83 | GDB_SIGNAL_TRAP = 5, |
425189a8 JK |
84 | GDB_SIGNAL_ABRT = 6, |
85 | GDB_SIGNAL_ALRM = 14, | |
86 | GDB_SIGNAL_IO = 23, | |
87 | GDB_SIGNAL_XCPU = 24, | |
ca587a8e AJ |
88 | GDB_SIGNAL_UNKNOWN = 143 |
89 | }; | |
90 | ||
91 | #ifdef CONFIG_USER_ONLY | |
92 | ||
93 | /* Map target signal numbers to GDB protocol signal numbers and vice | |
94 | * versa. For user emulation's currently supported systems, we can | |
95 | * assume most signals are defined. | |
96 | */ | |
97 | ||
98 | static int gdb_signal_table[] = { | |
99 | 0, | |
100 | TARGET_SIGHUP, | |
101 | TARGET_SIGINT, | |
102 | TARGET_SIGQUIT, | |
103 | TARGET_SIGILL, | |
104 | TARGET_SIGTRAP, | |
105 | TARGET_SIGABRT, | |
106 | -1, /* SIGEMT */ | |
107 | TARGET_SIGFPE, | |
108 | TARGET_SIGKILL, | |
109 | TARGET_SIGBUS, | |
110 | TARGET_SIGSEGV, | |
111 | TARGET_SIGSYS, | |
112 | TARGET_SIGPIPE, | |
113 | TARGET_SIGALRM, | |
114 | TARGET_SIGTERM, | |
115 | TARGET_SIGURG, | |
116 | TARGET_SIGSTOP, | |
117 | TARGET_SIGTSTP, | |
118 | TARGET_SIGCONT, | |
119 | TARGET_SIGCHLD, | |
120 | TARGET_SIGTTIN, | |
121 | TARGET_SIGTTOU, | |
122 | TARGET_SIGIO, | |
123 | TARGET_SIGXCPU, | |
124 | TARGET_SIGXFSZ, | |
125 | TARGET_SIGVTALRM, | |
126 | TARGET_SIGPROF, | |
127 | TARGET_SIGWINCH, | |
128 | -1, /* SIGLOST */ | |
129 | TARGET_SIGUSR1, | |
130 | TARGET_SIGUSR2, | |
c72d5bf8 | 131 | #ifdef TARGET_SIGPWR |
ca587a8e | 132 | TARGET_SIGPWR, |
c72d5bf8 BS |
133 | #else |
134 | -1, | |
135 | #endif | |
ca587a8e AJ |
136 | -1, /* SIGPOLL */ |
137 | -1, | |
138 | -1, | |
139 | -1, | |
140 | -1, | |
141 | -1, | |
142 | -1, | |
143 | -1, | |
144 | -1, | |
145 | -1, | |
146 | -1, | |
147 | -1, | |
c72d5bf8 | 148 | #ifdef __SIGRTMIN |
ca587a8e AJ |
149 | __SIGRTMIN + 1, |
150 | __SIGRTMIN + 2, | |
151 | __SIGRTMIN + 3, | |
152 | __SIGRTMIN + 4, | |
153 | __SIGRTMIN + 5, | |
154 | __SIGRTMIN + 6, | |
155 | __SIGRTMIN + 7, | |
156 | __SIGRTMIN + 8, | |
157 | __SIGRTMIN + 9, | |
158 | __SIGRTMIN + 10, | |
159 | __SIGRTMIN + 11, | |
160 | __SIGRTMIN + 12, | |
161 | __SIGRTMIN + 13, | |
162 | __SIGRTMIN + 14, | |
163 | __SIGRTMIN + 15, | |
164 | __SIGRTMIN + 16, | |
165 | __SIGRTMIN + 17, | |
166 | __SIGRTMIN + 18, | |
167 | __SIGRTMIN + 19, | |
168 | __SIGRTMIN + 20, | |
169 | __SIGRTMIN + 21, | |
170 | __SIGRTMIN + 22, | |
171 | __SIGRTMIN + 23, | |
172 | __SIGRTMIN + 24, | |
173 | __SIGRTMIN + 25, | |
174 | __SIGRTMIN + 26, | |
175 | __SIGRTMIN + 27, | |
176 | __SIGRTMIN + 28, | |
177 | __SIGRTMIN + 29, | |
178 | __SIGRTMIN + 30, | |
179 | __SIGRTMIN + 31, | |
180 | -1, /* SIGCANCEL */ | |
181 | __SIGRTMIN, | |
182 | __SIGRTMIN + 32, | |
183 | __SIGRTMIN + 33, | |
184 | __SIGRTMIN + 34, | |
185 | __SIGRTMIN + 35, | |
186 | __SIGRTMIN + 36, | |
187 | __SIGRTMIN + 37, | |
188 | __SIGRTMIN + 38, | |
189 | __SIGRTMIN + 39, | |
190 | __SIGRTMIN + 40, | |
191 | __SIGRTMIN + 41, | |
192 | __SIGRTMIN + 42, | |
193 | __SIGRTMIN + 43, | |
194 | __SIGRTMIN + 44, | |
195 | __SIGRTMIN + 45, | |
196 | __SIGRTMIN + 46, | |
197 | __SIGRTMIN + 47, | |
198 | __SIGRTMIN + 48, | |
199 | __SIGRTMIN + 49, | |
200 | __SIGRTMIN + 50, | |
201 | __SIGRTMIN + 51, | |
202 | __SIGRTMIN + 52, | |
203 | __SIGRTMIN + 53, | |
204 | __SIGRTMIN + 54, | |
205 | __SIGRTMIN + 55, | |
206 | __SIGRTMIN + 56, | |
207 | __SIGRTMIN + 57, | |
208 | __SIGRTMIN + 58, | |
209 | __SIGRTMIN + 59, | |
210 | __SIGRTMIN + 60, | |
211 | __SIGRTMIN + 61, | |
212 | __SIGRTMIN + 62, | |
213 | __SIGRTMIN + 63, | |
214 | __SIGRTMIN + 64, | |
215 | __SIGRTMIN + 65, | |
216 | __SIGRTMIN + 66, | |
217 | __SIGRTMIN + 67, | |
218 | __SIGRTMIN + 68, | |
219 | __SIGRTMIN + 69, | |
220 | __SIGRTMIN + 70, | |
221 | __SIGRTMIN + 71, | |
222 | __SIGRTMIN + 72, | |
223 | __SIGRTMIN + 73, | |
224 | __SIGRTMIN + 74, | |
225 | __SIGRTMIN + 75, | |
226 | __SIGRTMIN + 76, | |
227 | __SIGRTMIN + 77, | |
228 | __SIGRTMIN + 78, | |
229 | __SIGRTMIN + 79, | |
230 | __SIGRTMIN + 80, | |
231 | __SIGRTMIN + 81, | |
232 | __SIGRTMIN + 82, | |
233 | __SIGRTMIN + 83, | |
234 | __SIGRTMIN + 84, | |
235 | __SIGRTMIN + 85, | |
236 | __SIGRTMIN + 86, | |
237 | __SIGRTMIN + 87, | |
238 | __SIGRTMIN + 88, | |
239 | __SIGRTMIN + 89, | |
240 | __SIGRTMIN + 90, | |
241 | __SIGRTMIN + 91, | |
242 | __SIGRTMIN + 92, | |
243 | __SIGRTMIN + 93, | |
244 | __SIGRTMIN + 94, | |
245 | __SIGRTMIN + 95, | |
246 | -1, /* SIGINFO */ | |
247 | -1, /* UNKNOWN */ | |
248 | -1, /* DEFAULT */ | |
249 | -1, | |
250 | -1, | |
251 | -1, | |
252 | -1, | |
253 | -1, | |
254 | -1 | |
c72d5bf8 | 255 | #endif |
ca587a8e | 256 | }; |
8f447cc7 | 257 | #else |
ca587a8e AJ |
258 | /* In system mode we only need SIGINT and SIGTRAP; other signals |
259 | are not yet supported. */ | |
260 | ||
261 | enum { | |
262 | TARGET_SIGINT = 2, | |
263 | TARGET_SIGTRAP = 5 | |
264 | }; | |
265 | ||
266 | static int gdb_signal_table[] = { | |
267 | -1, | |
268 | -1, | |
269 | TARGET_SIGINT, | |
270 | -1, | |
271 | -1, | |
272 | TARGET_SIGTRAP | |
273 | }; | |
274 | #endif | |
275 | ||
276 | #ifdef CONFIG_USER_ONLY | |
277 | static int target_signal_to_gdb (int sig) | |
278 | { | |
279 | int i; | |
280 | for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++) | |
281 | if (gdb_signal_table[i] == sig) | |
282 | return i; | |
283 | return GDB_SIGNAL_UNKNOWN; | |
284 | } | |
8f447cc7 | 285 | #endif |
b4608c04 | 286 | |
ca587a8e AJ |
287 | static int gdb_signal_to_target (int sig) |
288 | { | |
289 | if (sig < ARRAY_SIZE (gdb_signal_table)) | |
290 | return gdb_signal_table[sig]; | |
291 | else | |
292 | return -1; | |
293 | } | |
294 | ||
56aebc89 PB |
295 | typedef struct GDBRegisterState { |
296 | int base_reg; | |
297 | int num_regs; | |
298 | gdb_reg_cb get_reg; | |
299 | gdb_reg_cb set_reg; | |
300 | const char *xml; | |
301 | struct GDBRegisterState *next; | |
302 | } GDBRegisterState; | |
303 | ||
8f468636 LM |
304 | typedef struct GDBProcess { |
305 | uint32_t pid; | |
306 | bool attached; | |
c145eeae LM |
307 | |
308 | char target_xml[1024]; | |
8f468636 LM |
309 | } GDBProcess; |
310 | ||
858693c6 | 311 | enum RSState { |
36556b20 | 312 | RS_INACTIVE, |
858693c6 FB |
313 | RS_IDLE, |
314 | RS_GETLINE, | |
4bf43122 DG |
315 | RS_GETLINE_ESC, |
316 | RS_GETLINE_RLE, | |
858693c6 FB |
317 | RS_CHKSUM1, |
318 | RS_CHKSUM2, | |
319 | }; | |
858693c6 | 320 | typedef struct GDBState { |
2e0f2cfb AF |
321 | CPUState *c_cpu; /* current CPU for step/continue ops */ |
322 | CPUState *g_cpu; /* current CPU for other ops */ | |
52f34623 | 323 | CPUState *query_cpu; /* for q{f|s}ThreadInfo */ |
41625033 | 324 | enum RSState state; /* parsing state */ |
56aebc89 | 325 | char line_buf[MAX_PACKET_LENGTH]; |
858693c6 | 326 | int line_buf_index; |
4bf43122 DG |
327 | int line_sum; /* running checksum */ |
328 | int line_csum; /* checksum at the end of the packet */ | |
56aebc89 | 329 | uint8_t last_packet[MAX_PACKET_LENGTH + 4]; |
4046d913 | 330 | int last_packet_len; |
1f487ee9 | 331 | int signal; |
41625033 | 332 | #ifdef CONFIG_USER_ONLY |
4046d913 | 333 | int fd; |
41625033 | 334 | int running_state; |
4046d913 | 335 | #else |
32a6ebec | 336 | CharBackend chr; |
0ec7b3e7 | 337 | Chardev *mon_chr; |
41625033 | 338 | #endif |
8f468636 LM |
339 | bool multiprocess; |
340 | GDBProcess *processes; | |
341 | int process_num; | |
cdb432b2 MI |
342 | char syscall_buf[256]; |
343 | gdb_syscall_complete_cb current_syscall_cb; | |
858693c6 | 344 | } GDBState; |
b4608c04 | 345 | |
60897d36 EI |
346 | /* By default use no IRQs and no timers while single stepping so as to |
347 | * make single stepping like an ICE HW step. | |
348 | */ | |
349 | static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER; | |
350 | ||
880a7578 AL |
351 | static GDBState *gdbserver_state; |
352 | ||
5b50e790 | 353 | bool gdb_has_xml; |
56aebc89 | 354 | |
1fddef4b | 355 | #ifdef CONFIG_USER_ONLY |
4046d913 PB |
356 | /* XXX: This is not thread safe. Do we care? */ |
357 | static int gdbserver_fd = -1; | |
358 | ||
858693c6 | 359 | static int get_char(GDBState *s) |
b4608c04 FB |
360 | { |
361 | uint8_t ch; | |
362 | int ret; | |
363 | ||
364 | for(;;) { | |
00aa0040 | 365 | ret = qemu_recv(s->fd, &ch, 1, 0); |
b4608c04 | 366 | if (ret < 0) { |
1f487ee9 EI |
367 | if (errno == ECONNRESET) |
368 | s->fd = -1; | |
5819e3e0 | 369 | if (errno != EINTR) |
b4608c04 FB |
370 | return -1; |
371 | } else if (ret == 0) { | |
1f487ee9 EI |
372 | close(s->fd); |
373 | s->fd = -1; | |
b4608c04 FB |
374 | return -1; |
375 | } else { | |
376 | break; | |
377 | } | |
378 | } | |
379 | return ch; | |
380 | } | |
4046d913 | 381 | #endif |
b4608c04 | 382 | |
654efcf3 | 383 | static enum { |
a2d1ebaf PB |
384 | GDB_SYS_UNKNOWN, |
385 | GDB_SYS_ENABLED, | |
386 | GDB_SYS_DISABLED, | |
387 | } gdb_syscall_mode; | |
388 | ||
a38bb079 | 389 | /* Decide if either remote gdb syscalls or native file IO should be used. */ |
a2d1ebaf PB |
390 | int use_gdb_syscalls(void) |
391 | { | |
cfe67cef LA |
392 | SemihostingTarget target = semihosting_get_target(); |
393 | if (target == SEMIHOSTING_TARGET_NATIVE) { | |
a38bb079 LI |
394 | /* -semihosting-config target=native */ |
395 | return false; | |
cfe67cef | 396 | } else if (target == SEMIHOSTING_TARGET_GDB) { |
a38bb079 LI |
397 | /* -semihosting-config target=gdb */ |
398 | return true; | |
399 | } | |
400 | ||
401 | /* -semihosting-config target=auto */ | |
402 | /* On the first call check if gdb is connected and remember. */ | |
a2d1ebaf | 403 | if (gdb_syscall_mode == GDB_SYS_UNKNOWN) { |
880a7578 AL |
404 | gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED |
405 | : GDB_SYS_DISABLED); | |
a2d1ebaf PB |
406 | } |
407 | return gdb_syscall_mode == GDB_SYS_ENABLED; | |
408 | } | |
409 | ||
ba70a624 EI |
410 | /* Resume execution. */ |
411 | static inline void gdb_continue(GDBState *s) | |
412 | { | |
5c9522b3 | 413 | |
ba70a624 EI |
414 | #ifdef CONFIG_USER_ONLY |
415 | s->running_state = 1; | |
5c9522b3 | 416 | trace_gdbstub_op_continue(); |
ba70a624 | 417 | #else |
26ac7a31 | 418 | if (!runstate_needs_reset()) { |
5c9522b3 | 419 | trace_gdbstub_op_continue(); |
87f25c12 PB |
420 | vm_start(); |
421 | } | |
ba70a624 EI |
422 | #endif |
423 | } | |
424 | ||
544177ad CI |
425 | /* |
426 | * Resume execution, per CPU actions. For user-mode emulation it's | |
427 | * equivalent to gdb_continue. | |
428 | */ | |
429 | static int gdb_continue_partial(GDBState *s, char *newstates) | |
430 | { | |
431 | CPUState *cpu; | |
432 | int res = 0; | |
433 | #ifdef CONFIG_USER_ONLY | |
434 | /* | |
435 | * This is not exactly accurate, but it's an improvement compared to the | |
436 | * previous situation, where only one CPU would be single-stepped. | |
437 | */ | |
438 | CPU_FOREACH(cpu) { | |
439 | if (newstates[cpu->cpu_index] == 's') { | |
5c9522b3 | 440 | trace_gdbstub_op_stepping(cpu->cpu_index); |
544177ad CI |
441 | cpu_single_step(cpu, sstep_flags); |
442 | } | |
443 | } | |
444 | s->running_state = 1; | |
445 | #else | |
446 | int flag = 0; | |
447 | ||
448 | if (!runstate_needs_reset()) { | |
449 | if (vm_prepare_start()) { | |
450 | return 0; | |
451 | } | |
452 | ||
453 | CPU_FOREACH(cpu) { | |
454 | switch (newstates[cpu->cpu_index]) { | |
455 | case 0: | |
456 | case 1: | |
457 | break; /* nothing to do here */ | |
458 | case 's': | |
5c9522b3 | 459 | trace_gdbstub_op_stepping(cpu->cpu_index); |
544177ad CI |
460 | cpu_single_step(cpu, sstep_flags); |
461 | cpu_resume(cpu); | |
462 | flag = 1; | |
463 | break; | |
464 | case 'c': | |
5c9522b3 | 465 | trace_gdbstub_op_continue_cpu(cpu->cpu_index); |
544177ad CI |
466 | cpu_resume(cpu); |
467 | flag = 1; | |
468 | break; | |
469 | default: | |
470 | res = -1; | |
471 | break; | |
472 | } | |
473 | } | |
474 | } | |
475 | if (flag) { | |
476 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); | |
477 | } | |
478 | #endif | |
479 | return res; | |
480 | } | |
481 | ||
858693c6 | 482 | static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
b4608c04 | 483 | { |
4046d913 | 484 | #ifdef CONFIG_USER_ONLY |
b4608c04 FB |
485 | int ret; |
486 | ||
487 | while (len > 0) { | |
8f447cc7 | 488 | ret = send(s->fd, buf, len, 0); |
b4608c04 | 489 | if (ret < 0) { |
5819e3e0 | 490 | if (errno != EINTR) |
b4608c04 FB |
491 | return; |
492 | } else { | |
493 | buf += ret; | |
494 | len -= ret; | |
495 | } | |
496 | } | |
4046d913 | 497 | #else |
6ab3fc32 DB |
498 | /* XXX this blocks entire thread. Rewrite to use |
499 | * qemu_chr_fe_write and background I/O callbacks */ | |
5345fdb4 | 500 | qemu_chr_fe_write_all(&s->chr, buf, len); |
4046d913 | 501 | #endif |
b4608c04 FB |
502 | } |
503 | ||
504 | static inline int fromhex(int v) | |
505 | { | |
506 | if (v >= '0' && v <= '9') | |
507 | return v - '0'; | |
508 | else if (v >= 'A' && v <= 'F') | |
509 | return v - 'A' + 10; | |
510 | else if (v >= 'a' && v <= 'f') | |
511 | return v - 'a' + 10; | |
512 | else | |
513 | return 0; | |
514 | } | |
515 | ||
516 | static inline int tohex(int v) | |
517 | { | |
518 | if (v < 10) | |
519 | return v + '0'; | |
520 | else | |
521 | return v - 10 + 'a'; | |
522 | } | |
523 | ||
9005774b | 524 | /* writes 2*len+1 bytes in buf */ |
b4608c04 FB |
525 | static void memtohex(char *buf, const uint8_t *mem, int len) |
526 | { | |
527 | int i, c; | |
528 | char *q; | |
529 | q = buf; | |
530 | for(i = 0; i < len; i++) { | |
531 | c = mem[i]; | |
532 | *q++ = tohex(c >> 4); | |
533 | *q++ = tohex(c & 0xf); | |
534 | } | |
535 | *q = '\0'; | |
536 | } | |
537 | ||
538 | static void hextomem(uint8_t *mem, const char *buf, int len) | |
539 | { | |
540 | int i; | |
541 | ||
542 | for(i = 0; i < len; i++) { | |
543 | mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); | |
544 | buf += 2; | |
545 | } | |
546 | } | |
547 | ||
5c9522b3 DG |
548 | static void hexdump(const char *buf, int len, |
549 | void (*trace_fn)(size_t ofs, char const *text)) | |
550 | { | |
551 | char line_buffer[3 * 16 + 4 + 16 + 1]; | |
552 | ||
553 | size_t i; | |
554 | for (i = 0; i < len || (i & 0xF); ++i) { | |
555 | size_t byte_ofs = i & 15; | |
556 | ||
557 | if (byte_ofs == 0) { | |
558 | memset(line_buffer, ' ', 3 * 16 + 4 + 16); | |
559 | line_buffer[3 * 16 + 4 + 16] = 0; | |
560 | } | |
561 | ||
562 | size_t col_group = (i >> 2) & 3; | |
563 | size_t hex_col = byte_ofs * 3 + col_group; | |
564 | size_t txt_col = 3 * 16 + 4 + byte_ofs; | |
565 | ||
566 | if (i < len) { | |
567 | char value = buf[i]; | |
568 | ||
569 | line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF); | |
570 | line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF); | |
571 | line_buffer[txt_col + 0] = (value >= ' ' && value < 127) | |
572 | ? value | |
573 | : '.'; | |
574 | } | |
575 | ||
576 | if (byte_ofs == 0xF) | |
577 | trace_fn(i & -16, line_buffer); | |
578 | } | |
579 | } | |
580 | ||
b4608c04 | 581 | /* return -1 if error, 0 if OK */ |
5c9522b3 | 582 | static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump) |
b4608c04 | 583 | { |
56aebc89 | 584 | int csum, i; |
60fe76f3 | 585 | uint8_t *p; |
b4608c04 | 586 | |
5c9522b3 DG |
587 | if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) { |
588 | hexdump(buf, len, trace_gdbstub_io_binaryreply); | |
589 | } | |
590 | ||
b4608c04 | 591 | for(;;) { |
4046d913 PB |
592 | p = s->last_packet; |
593 | *(p++) = '$'; | |
4046d913 PB |
594 | memcpy(p, buf, len); |
595 | p += len; | |
b4608c04 FB |
596 | csum = 0; |
597 | for(i = 0; i < len; i++) { | |
598 | csum += buf[i]; | |
599 | } | |
4046d913 PB |
600 | *(p++) = '#'; |
601 | *(p++) = tohex((csum >> 4) & 0xf); | |
602 | *(p++) = tohex((csum) & 0xf); | |
b4608c04 | 603 | |
4046d913 | 604 | s->last_packet_len = p - s->last_packet; |
ffe8ab83 | 605 | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
b4608c04 | 606 | |
4046d913 PB |
607 | #ifdef CONFIG_USER_ONLY |
608 | i = get_char(s); | |
609 | if (i < 0) | |
b4608c04 | 610 | return -1; |
4046d913 | 611 | if (i == '+') |
b4608c04 | 612 | break; |
4046d913 PB |
613 | #else |
614 | break; | |
615 | #endif | |
b4608c04 FB |
616 | } |
617 | return 0; | |
618 | } | |
619 | ||
56aebc89 PB |
620 | /* return -1 if error, 0 if OK */ |
621 | static int put_packet(GDBState *s, const char *buf) | |
622 | { | |
5c9522b3 | 623 | trace_gdbstub_io_reply(buf); |
79808573 | 624 | |
5c9522b3 | 625 | return put_packet_binary(s, buf, strlen(buf), false); |
56aebc89 PB |
626 | } |
627 | ||
56aebc89 PB |
628 | /* Encode data using the encoding for 'x' packets. */ |
629 | static int memtox(char *buf, const char *mem, int len) | |
630 | { | |
631 | char *p = buf; | |
632 | char c; | |
633 | ||
634 | while (len--) { | |
635 | c = *(mem++); | |
636 | switch (c) { | |
637 | case '#': case '$': case '*': case '}': | |
638 | *(p++) = '}'; | |
639 | *(p++) = c ^ 0x20; | |
640 | break; | |
641 | default: | |
642 | *(p++) = c; | |
643 | break; | |
644 | } | |
645 | } | |
646 | return p - buf; | |
647 | } | |
f1ccf904 | 648 | |
1a227336 LM |
649 | static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu) |
650 | { | |
46f5abc0 PM |
651 | /* TODO: In user mode, we should use the task state PID */ |
652 | if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) { | |
1a227336 | 653 | /* Return the default process' PID */ |
46f5abc0 | 654 | return s->processes[s->process_num - 1].pid; |
1a227336 | 655 | } |
46f5abc0 | 656 | return cpu->cluster_index + 1; |
1a227336 LM |
657 | } |
658 | ||
7d8c87da LM |
659 | static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid) |
660 | { | |
661 | int i; | |
662 | ||
663 | if (!pid) { | |
664 | /* 0 means any process, we take the first one */ | |
665 | return &s->processes[0]; | |
666 | } | |
667 | ||
668 | for (i = 0; i < s->process_num; i++) { | |
669 | if (s->processes[i].pid == pid) { | |
670 | return &s->processes[i]; | |
671 | } | |
672 | } | |
673 | ||
674 | return NULL; | |
675 | } | |
676 | ||
677 | static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu) | |
678 | { | |
679 | return gdb_get_process(s, gdb_get_cpu_pid(s, cpu)); | |
680 | } | |
681 | ||
682 | static CPUState *find_cpu(uint32_t thread_id) | |
683 | { | |
684 | CPUState *cpu; | |
685 | ||
686 | CPU_FOREACH(cpu) { | |
687 | if (cpu_gdb_index(cpu) == thread_id) { | |
688 | return cpu; | |
689 | } | |
690 | } | |
691 | ||
692 | return NULL; | |
693 | } | |
694 | ||
e40e5204 LM |
695 | static CPUState *get_first_cpu_in_process(const GDBState *s, |
696 | GDBProcess *process) | |
697 | { | |
698 | CPUState *cpu; | |
699 | ||
700 | CPU_FOREACH(cpu) { | |
701 | if (gdb_get_cpu_pid(s, cpu) == process->pid) { | |
702 | return cpu; | |
703 | } | |
704 | } | |
705 | ||
706 | return NULL; | |
707 | } | |
708 | ||
709 | static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu) | |
710 | { | |
711 | uint32_t pid = gdb_get_cpu_pid(s, cpu); | |
712 | cpu = CPU_NEXT(cpu); | |
713 | ||
714 | while (cpu) { | |
715 | if (gdb_get_cpu_pid(s, cpu) == pid) { | |
716 | break; | |
717 | } | |
718 | ||
719 | cpu = CPU_NEXT(cpu); | |
720 | } | |
721 | ||
722 | return cpu; | |
723 | } | |
724 | ||
e40e5204 LM |
725 | /* Return the cpu following @cpu, while ignoring unattached processes. */ |
726 | static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu) | |
727 | { | |
728 | cpu = CPU_NEXT(cpu); | |
729 | ||
730 | while (cpu) { | |
731 | if (gdb_get_cpu_process(s, cpu)->attached) { | |
732 | break; | |
733 | } | |
734 | ||
735 | cpu = CPU_NEXT(cpu); | |
736 | } | |
737 | ||
738 | return cpu; | |
739 | } | |
740 | ||
741 | /* Return the first attached cpu */ | |
742 | static CPUState *gdb_first_attached_cpu(const GDBState *s) | |
743 | { | |
744 | CPUState *cpu = first_cpu; | |
745 | GDBProcess *process = gdb_get_cpu_process(s, cpu); | |
746 | ||
747 | if (!process->attached) { | |
748 | return gdb_next_attached_cpu(s, cpu); | |
749 | } | |
750 | ||
751 | return cpu; | |
752 | } | |
753 | ||
ab65eed3 LM |
754 | static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid) |
755 | { | |
756 | GDBProcess *process; | |
757 | CPUState *cpu; | |
758 | ||
759 | if (!pid && !tid) { | |
760 | /* 0 means any process/thread, we take the first attached one */ | |
761 | return gdb_first_attached_cpu(s); | |
762 | } else if (pid && !tid) { | |
763 | /* any thread in a specific process */ | |
764 | process = gdb_get_process(s, pid); | |
765 | ||
766 | if (process == NULL) { | |
767 | return NULL; | |
768 | } | |
769 | ||
770 | if (!process->attached) { | |
771 | return NULL; | |
772 | } | |
773 | ||
774 | return get_first_cpu_in_process(s, process); | |
775 | } else { | |
776 | /* a specific thread */ | |
777 | cpu = find_cpu(tid); | |
778 | ||
779 | if (cpu == NULL) { | |
780 | return NULL; | |
781 | } | |
782 | ||
783 | process = gdb_get_cpu_process(s, cpu); | |
784 | ||
785 | if (pid && process->pid != pid) { | |
786 | return NULL; | |
787 | } | |
788 | ||
789 | if (!process->attached) { | |
790 | return NULL; | |
791 | } | |
792 | ||
793 | return cpu; | |
794 | } | |
795 | } | |
796 | ||
c145eeae LM |
797 | static const char *get_feature_xml(const GDBState *s, const char *p, |
798 | const char **newp, GDBProcess *process) | |
56aebc89 | 799 | { |
56aebc89 PB |
800 | size_t len; |
801 | int i; | |
802 | const char *name; | |
c145eeae LM |
803 | CPUState *cpu = get_first_cpu_in_process(s, process); |
804 | CPUClass *cc = CPU_GET_CLASS(cpu); | |
56aebc89 PB |
805 | |
806 | len = 0; | |
807 | while (p[len] && p[len] != ':') | |
808 | len++; | |
809 | *newp = p + len; | |
810 | ||
811 | name = NULL; | |
812 | if (strncmp(p, "target.xml", len) == 0) { | |
c145eeae LM |
813 | char *buf = process->target_xml; |
814 | const size_t buf_sz = sizeof(process->target_xml); | |
815 | ||
56aebc89 | 816 | /* Generate the XML description for this CPU. */ |
c145eeae | 817 | if (!buf[0]) { |
56aebc89 PB |
818 | GDBRegisterState *r; |
819 | ||
c145eeae | 820 | pstrcat(buf, buf_sz, |
b3820e6c DH |
821 | "<?xml version=\"1.0\"?>" |
822 | "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">" | |
823 | "<target>"); | |
824 | if (cc->gdb_arch_name) { | |
825 | gchar *arch = cc->gdb_arch_name(cpu); | |
c145eeae LM |
826 | pstrcat(buf, buf_sz, "<architecture>"); |
827 | pstrcat(buf, buf_sz, arch); | |
828 | pstrcat(buf, buf_sz, "</architecture>"); | |
b3820e6c DH |
829 | g_free(arch); |
830 | } | |
c145eeae LM |
831 | pstrcat(buf, buf_sz, "<xi:include href=\""); |
832 | pstrcat(buf, buf_sz, cc->gdb_core_xml_file); | |
833 | pstrcat(buf, buf_sz, "\"/>"); | |
eac8b355 | 834 | for (r = cpu->gdb_regs; r; r = r->next) { |
c145eeae LM |
835 | pstrcat(buf, buf_sz, "<xi:include href=\""); |
836 | pstrcat(buf, buf_sz, r->xml); | |
837 | pstrcat(buf, buf_sz, "\"/>"); | |
56aebc89 | 838 | } |
c145eeae | 839 | pstrcat(buf, buf_sz, "</target>"); |
56aebc89 | 840 | } |
c145eeae | 841 | return buf; |
56aebc89 | 842 | } |
200bf5b7 | 843 | if (cc->gdb_get_dynamic_xml) { |
200bf5b7 AB |
844 | char *xmlname = g_strndup(p, len); |
845 | const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname); | |
846 | ||
847 | g_free(xmlname); | |
848 | if (xml) { | |
849 | return xml; | |
850 | } | |
851 | } | |
56aebc89 PB |
852 | for (i = 0; ; i++) { |
853 | name = xml_builtin[i][0]; | |
854 | if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len)) | |
855 | break; | |
856 | } | |
857 | return name ? xml_builtin[i][1] : NULL; | |
858 | } | |
f1ccf904 | 859 | |
385b9f0e | 860 | static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
56aebc89 | 861 | { |
a0e372f0 | 862 | CPUClass *cc = CPU_GET_CLASS(cpu); |
385b9f0e | 863 | CPUArchState *env = cpu->env_ptr; |
56aebc89 | 864 | GDBRegisterState *r; |
f1ccf904 | 865 | |
a0e372f0 | 866 | if (reg < cc->gdb_num_core_regs) { |
5b50e790 | 867 | return cc->gdb_read_register(cpu, mem_buf, reg); |
a0e372f0 | 868 | } |
f1ccf904 | 869 | |
eac8b355 | 870 | for (r = cpu->gdb_regs; r; r = r->next) { |
56aebc89 PB |
871 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
872 | return r->get_reg(env, mem_buf, reg - r->base_reg); | |
873 | } | |
874 | } | |
875 | return 0; | |
f1ccf904 TS |
876 | } |
877 | ||
385b9f0e | 878 | static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
f1ccf904 | 879 | { |
a0e372f0 | 880 | CPUClass *cc = CPU_GET_CLASS(cpu); |
385b9f0e | 881 | CPUArchState *env = cpu->env_ptr; |
56aebc89 | 882 | GDBRegisterState *r; |
f1ccf904 | 883 | |
a0e372f0 | 884 | if (reg < cc->gdb_num_core_regs) { |
5b50e790 | 885 | return cc->gdb_write_register(cpu, mem_buf, reg); |
a0e372f0 | 886 | } |
56aebc89 | 887 | |
eac8b355 | 888 | for (r = cpu->gdb_regs; r; r = r->next) { |
56aebc89 PB |
889 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
890 | return r->set_reg(env, mem_buf, reg - r->base_reg); | |
891 | } | |
892 | } | |
6da41eaf FB |
893 | return 0; |
894 | } | |
895 | ||
56aebc89 PB |
896 | /* Register a supplemental set of CPU registers. If g_pos is nonzero it |
897 | specifies the first register number and these registers are included in | |
898 | a standard "g" packet. Direction is relative to gdb, i.e. get_reg is | |
899 | gdb reading a CPU register, and set_reg is gdb modifying a CPU register. | |
900 | */ | |
901 | ||
22169d41 AF |
902 | void gdb_register_coprocessor(CPUState *cpu, |
903 | gdb_reg_cb get_reg, gdb_reg_cb set_reg, | |
904 | int num_regs, const char *xml, int g_pos) | |
6da41eaf | 905 | { |
56aebc89 PB |
906 | GDBRegisterState *s; |
907 | GDBRegisterState **p; | |
56aebc89 | 908 | |
eac8b355 | 909 | p = &cpu->gdb_regs; |
56aebc89 PB |
910 | while (*p) { |
911 | /* Check for duplicates. */ | |
912 | if (strcmp((*p)->xml, xml) == 0) | |
913 | return; | |
914 | p = &(*p)->next; | |
915 | } | |
9643c25f SW |
916 | |
917 | s = g_new0(GDBRegisterState, 1); | |
a0e372f0 | 918 | s->base_reg = cpu->gdb_num_regs; |
9643c25f SW |
919 | s->num_regs = num_regs; |
920 | s->get_reg = get_reg; | |
921 | s->set_reg = set_reg; | |
922 | s->xml = xml; | |
923 | ||
56aebc89 | 924 | /* Add to end of list. */ |
a0e372f0 | 925 | cpu->gdb_num_regs += num_regs; |
56aebc89 PB |
926 | *p = s; |
927 | if (g_pos) { | |
928 | if (g_pos != s->base_reg) { | |
7ae6c571 ZY |
929 | error_report("Error: Bad gdb register numbering for '%s', " |
930 | "expected %d got %d", xml, g_pos, s->base_reg); | |
35143f01 AF |
931 | } else { |
932 | cpu->gdb_num_g_regs = cpu->gdb_num_regs; | |
56aebc89 PB |
933 | } |
934 | } | |
6da41eaf FB |
935 | } |
936 | ||
a1d1bb31 | 937 | #ifndef CONFIG_USER_ONLY |
2472b6c0 PM |
938 | /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */ |
939 | static inline int xlat_gdb_type(CPUState *cpu, int gdbtype) | |
940 | { | |
941 | static const int xlat[] = { | |
942 | [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE, | |
943 | [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ, | |
944 | [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS, | |
945 | }; | |
946 | ||
947 | CPUClass *cc = CPU_GET_CLASS(cpu); | |
948 | int cputype = xlat[gdbtype]; | |
949 | ||
950 | if (cc->gdb_stop_before_watchpoint) { | |
951 | cputype |= BP_STOP_BEFORE_ACCESS; | |
952 | } | |
953 | return cputype; | |
954 | } | |
a1d1bb31 AL |
955 | #endif |
956 | ||
77f6ce50 | 957 | static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len) |
a1d1bb31 | 958 | { |
182735ef | 959 | CPUState *cpu; |
880a7578 AL |
960 | int err = 0; |
961 | ||
62278814 | 962 | if (kvm_enabled()) { |
2e0f2cfb | 963 | return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type); |
62278814 | 964 | } |
e22a25c9 | 965 | |
a1d1bb31 AL |
966 | switch (type) { |
967 | case GDB_BREAKPOINT_SW: | |
968 | case GDB_BREAKPOINT_HW: | |
bdc44640 | 969 | CPU_FOREACH(cpu) { |
b3310ab3 AF |
970 | err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL); |
971 | if (err) { | |
880a7578 | 972 | break; |
b3310ab3 | 973 | } |
880a7578 AL |
974 | } |
975 | return err; | |
a1d1bb31 AL |
976 | #ifndef CONFIG_USER_ONLY |
977 | case GDB_WATCHPOINT_WRITE: | |
978 | case GDB_WATCHPOINT_READ: | |
979 | case GDB_WATCHPOINT_ACCESS: | |
bdc44640 | 980 | CPU_FOREACH(cpu) { |
2472b6c0 PM |
981 | err = cpu_watchpoint_insert(cpu, addr, len, |
982 | xlat_gdb_type(cpu, type), NULL); | |
983 | if (err) { | |
880a7578 | 984 | break; |
2472b6c0 | 985 | } |
880a7578 AL |
986 | } |
987 | return err; | |
a1d1bb31 AL |
988 | #endif |
989 | default: | |
990 | return -ENOSYS; | |
991 | } | |
992 | } | |
993 | ||
77f6ce50 | 994 | static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len) |
a1d1bb31 | 995 | { |
182735ef | 996 | CPUState *cpu; |
880a7578 AL |
997 | int err = 0; |
998 | ||
62278814 | 999 | if (kvm_enabled()) { |
2e0f2cfb | 1000 | return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type); |
62278814 | 1001 | } |
e22a25c9 | 1002 | |
a1d1bb31 AL |
1003 | switch (type) { |
1004 | case GDB_BREAKPOINT_SW: | |
1005 | case GDB_BREAKPOINT_HW: | |
bdc44640 | 1006 | CPU_FOREACH(cpu) { |
b3310ab3 AF |
1007 | err = cpu_breakpoint_remove(cpu, addr, BP_GDB); |
1008 | if (err) { | |
880a7578 | 1009 | break; |
b3310ab3 | 1010 | } |
880a7578 AL |
1011 | } |
1012 | return err; | |
a1d1bb31 AL |
1013 | #ifndef CONFIG_USER_ONLY |
1014 | case GDB_WATCHPOINT_WRITE: | |
1015 | case GDB_WATCHPOINT_READ: | |
1016 | case GDB_WATCHPOINT_ACCESS: | |
bdc44640 | 1017 | CPU_FOREACH(cpu) { |
2472b6c0 PM |
1018 | err = cpu_watchpoint_remove(cpu, addr, len, |
1019 | xlat_gdb_type(cpu, type)); | |
880a7578 AL |
1020 | if (err) |
1021 | break; | |
1022 | } | |
1023 | return err; | |
a1d1bb31 AL |
1024 | #endif |
1025 | default: | |
1026 | return -ENOSYS; | |
1027 | } | |
1028 | } | |
1029 | ||
546f3c67 LM |
1030 | static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu) |
1031 | { | |
1032 | cpu_breakpoint_remove_all(cpu, BP_GDB); | |
1033 | #ifndef CONFIG_USER_ONLY | |
1034 | cpu_watchpoint_remove_all(cpu, BP_GDB); | |
1035 | #endif | |
1036 | } | |
1037 | ||
1038 | static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p) | |
1039 | { | |
1040 | CPUState *cpu = get_first_cpu_in_process(s, p); | |
1041 | ||
1042 | while (cpu) { | |
1043 | gdb_cpu_breakpoint_remove_all(cpu); | |
1044 | cpu = gdb_next_cpu_in_process(s, cpu); | |
1045 | } | |
1046 | } | |
1047 | ||
880a7578 | 1048 | static void gdb_breakpoint_remove_all(void) |
a1d1bb31 | 1049 | { |
182735ef | 1050 | CPUState *cpu; |
880a7578 | 1051 | |
e22a25c9 | 1052 | if (kvm_enabled()) { |
2e0f2cfb | 1053 | kvm_remove_all_breakpoints(gdbserver_state->c_cpu); |
e22a25c9 AL |
1054 | return; |
1055 | } | |
1056 | ||
bdc44640 | 1057 | CPU_FOREACH(cpu) { |
546f3c67 | 1058 | gdb_cpu_breakpoint_remove_all(cpu); |
880a7578 | 1059 | } |
a1d1bb31 AL |
1060 | } |
1061 | ||
fab9d284 AJ |
1062 | static void gdb_set_cpu_pc(GDBState *s, target_ulong pc) |
1063 | { | |
2e0f2cfb | 1064 | CPUState *cpu = s->c_cpu; |
f45748f1 AF |
1065 | |
1066 | cpu_synchronize_state(cpu); | |
4a2b24ed | 1067 | cpu_set_pc(cpu, pc); |
fab9d284 AJ |
1068 | } |
1069 | ||
1a227336 LM |
1070 | static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu, |
1071 | char *buf, size_t buf_size) | |
1072 | { | |
1073 | if (s->multiprocess) { | |
1074 | snprintf(buf, buf_size, "p%02x.%02x", | |
1075 | gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu)); | |
1076 | } else { | |
1077 | snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu)); | |
1078 | } | |
1079 | ||
1080 | return buf; | |
1081 | } | |
1082 | ||
7d8c87da LM |
1083 | typedef enum GDBThreadIdKind { |
1084 | GDB_ONE_THREAD = 0, | |
1085 | GDB_ALL_THREADS, /* One process, all threads */ | |
1086 | GDB_ALL_PROCESSES, | |
1087 | GDB_READ_THREAD_ERR | |
1088 | } GDBThreadIdKind; | |
1089 | ||
1090 | static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf, | |
1091 | uint32_t *pid, uint32_t *tid) | |
1092 | { | |
1093 | unsigned long p, t; | |
1094 | int ret; | |
1095 | ||
1096 | if (*buf == 'p') { | |
1097 | buf++; | |
1098 | ret = qemu_strtoul(buf, &buf, 16, &p); | |
1099 | ||
1100 | if (ret) { | |
1101 | return GDB_READ_THREAD_ERR; | |
1102 | } | |
1103 | ||
1104 | /* Skip '.' */ | |
1105 | buf++; | |
1106 | } else { | |
1107 | p = 1; | |
1108 | } | |
1109 | ||
1110 | ret = qemu_strtoul(buf, &buf, 16, &t); | |
1111 | ||
1112 | if (ret) { | |
1113 | return GDB_READ_THREAD_ERR; | |
1114 | } | |
1115 | ||
1116 | *end_buf = buf; | |
1117 | ||
1118 | if (p == -1) { | |
1119 | return GDB_ALL_PROCESSES; | |
1120 | } | |
1121 | ||
1122 | if (pid) { | |
1123 | *pid = p; | |
1124 | } | |
1125 | ||
1126 | if (t == -1) { | |
1127 | return GDB_ALL_THREADS; | |
1128 | } | |
1129 | ||
1130 | if (tid) { | |
1131 | *tid = t; | |
1132 | } | |
1133 | ||
1134 | return GDB_ONE_THREAD; | |
1135 | } | |
1136 | ||
544177ad CI |
1137 | /** |
1138 | * gdb_handle_vcont - Parses and handles a vCont packet. | |
1139 | * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is | |
1140 | * a format error, 0 on success. | |
1141 | */ | |
1142 | static int gdb_handle_vcont(GDBState *s, const char *p) | |
1143 | { | |
e40e5204 | 1144 | int res, signal = 0; |
544177ad CI |
1145 | char cur_action; |
1146 | char *newstates; | |
1147 | unsigned long tmp; | |
e40e5204 LM |
1148 | uint32_t pid, tid; |
1149 | GDBProcess *process; | |
544177ad | 1150 | CPUState *cpu; |
c99ef792 | 1151 | GDBThreadIdKind kind; |
544177ad CI |
1152 | #ifdef CONFIG_USER_ONLY |
1153 | int max_cpus = 1; /* global variable max_cpus exists only in system mode */ | |
1154 | ||
1155 | CPU_FOREACH(cpu) { | |
1156 | max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus; | |
1157 | } | |
1158 | #endif | |
1159 | /* uninitialised CPUs stay 0 */ | |
1160 | newstates = g_new0(char, max_cpus); | |
1161 | ||
1162 | /* mark valid CPUs with 1 */ | |
1163 | CPU_FOREACH(cpu) { | |
1164 | newstates[cpu->cpu_index] = 1; | |
1165 | } | |
1166 | ||
1167 | /* | |
1168 | * res keeps track of what error we are returning, with -ENOTSUP meaning | |
1169 | * that the command is unknown or unsupported, thus returning an empty | |
1170 | * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid, | |
1171 | * or incorrect parameters passed. | |
1172 | */ | |
1173 | res = 0; | |
1174 | while (*p) { | |
1175 | if (*p++ != ';') { | |
1176 | res = -ENOTSUP; | |
1177 | goto out; | |
1178 | } | |
1179 | ||
1180 | cur_action = *p++; | |
1181 | if (cur_action == 'C' || cur_action == 'S') { | |
95a5befc | 1182 | cur_action = qemu_tolower(cur_action); |
544177ad CI |
1183 | res = qemu_strtoul(p + 1, &p, 16, &tmp); |
1184 | if (res) { | |
1185 | goto out; | |
1186 | } | |
1187 | signal = gdb_signal_to_target(tmp); | |
1188 | } else if (cur_action != 'c' && cur_action != 's') { | |
1189 | /* unknown/invalid/unsupported command */ | |
1190 | res = -ENOTSUP; | |
1191 | goto out; | |
1192 | } | |
e40e5204 | 1193 | |
c99ef792 LM |
1194 | if (*p == '\0' || *p == ';') { |
1195 | /* | |
1196 | * No thread specifier, action is on "all threads". The | |
1197 | * specification is unclear regarding the process to act on. We | |
1198 | * choose all processes. | |
1199 | */ | |
1200 | kind = GDB_ALL_PROCESSES; | |
1201 | } else if (*p++ == ':') { | |
1202 | kind = read_thread_id(p, &p, &pid, &tid); | |
1203 | } else { | |
e40e5204 LM |
1204 | res = -ENOTSUP; |
1205 | goto out; | |
1206 | } | |
1207 | ||
c99ef792 | 1208 | switch (kind) { |
e40e5204 LM |
1209 | case GDB_READ_THREAD_ERR: |
1210 | res = -EINVAL; | |
1211 | goto out; | |
1212 | ||
1213 | case GDB_ALL_PROCESSES: | |
1214 | cpu = gdb_first_attached_cpu(s); | |
1215 | while (cpu) { | |
1216 | if (newstates[cpu->cpu_index] == 1) { | |
1217 | newstates[cpu->cpu_index] = cur_action; | |
544177ad | 1218 | } |
e40e5204 LM |
1219 | |
1220 | cpu = gdb_next_attached_cpu(s, cpu); | |
544177ad | 1221 | } |
e40e5204 LM |
1222 | break; |
1223 | ||
1224 | case GDB_ALL_THREADS: | |
1225 | process = gdb_get_process(s, pid); | |
1226 | ||
1227 | if (!process->attached) { | |
1228 | res = -EINVAL; | |
544177ad CI |
1229 | goto out; |
1230 | } | |
5a6a1ad1 | 1231 | |
e40e5204 LM |
1232 | cpu = get_first_cpu_in_process(s, process); |
1233 | while (cpu) { | |
1234 | if (newstates[cpu->cpu_index] == 1) { | |
1235 | newstates[cpu->cpu_index] = cur_action; | |
1236 | } | |
1237 | ||
1238 | cpu = gdb_next_cpu_in_process(s, cpu); | |
1239 | } | |
1240 | break; | |
1241 | ||
1242 | case GDB_ONE_THREAD: | |
1243 | cpu = gdb_get_cpu(s, pid, tid); | |
544177ad | 1244 | |
544177ad | 1245 | /* invalid CPU/thread specified */ |
5a6a1ad1 | 1246 | if (!cpu) { |
544177ad CI |
1247 | res = -EINVAL; |
1248 | goto out; | |
1249 | } | |
5a6a1ad1 | 1250 | |
544177ad CI |
1251 | /* only use if no previous match occourred */ |
1252 | if (newstates[cpu->cpu_index] == 1) { | |
1253 | newstates[cpu->cpu_index] = cur_action; | |
1254 | } | |
e40e5204 | 1255 | break; |
544177ad CI |
1256 | } |
1257 | } | |
1258 | s->signal = signal; | |
1259 | gdb_continue_partial(s, newstates); | |
1260 | ||
1261 | out: | |
1262 | g_free(newstates); | |
1263 | ||
1264 | return res; | |
1265 | } | |
1266 | ||
d14055dc JD |
1267 | typedef union GdbCmdVariant { |
1268 | const char *data; | |
1269 | uint8_t opcode; | |
1270 | unsigned long val_ul; | |
1271 | unsigned long long val_ull; | |
1272 | struct { | |
1273 | GDBThreadIdKind kind; | |
1274 | uint32_t pid; | |
1275 | uint32_t tid; | |
1276 | } thread_id; | |
1277 | } GdbCmdVariant; | |
1278 | ||
1279 | static const char *cmd_next_param(const char *param, const char delimiter) | |
1280 | { | |
1281 | static const char all_delimiters[] = ",;:="; | |
1282 | char curr_delimiters[2] = {0}; | |
1283 | const char *delimiters; | |
1284 | ||
1285 | if (delimiter == '?') { | |
1286 | delimiters = all_delimiters; | |
1287 | } else if (delimiter == '0') { | |
1288 | return strchr(param, '\0'); | |
1289 | } else if (delimiter == '.' && *param) { | |
1290 | return param + 1; | |
1291 | } else { | |
1292 | curr_delimiters[0] = delimiter; | |
1293 | delimiters = curr_delimiters; | |
1294 | } | |
1295 | ||
1296 | param += strcspn(param, delimiters); | |
1297 | if (*param) { | |
1298 | param++; | |
1299 | } | |
1300 | return param; | |
1301 | } | |
1302 | ||
1303 | static int cmd_parse_params(const char *data, const char *schema, | |
1304 | GdbCmdVariant *params, int *num_params) | |
1305 | { | |
1306 | int curr_param; | |
1307 | const char *curr_schema, *curr_data; | |
1308 | ||
1309 | *num_params = 0; | |
1310 | ||
1311 | if (!schema) { | |
1312 | return 0; | |
1313 | } | |
1314 | ||
1315 | curr_schema = schema; | |
1316 | curr_param = 0; | |
1317 | curr_data = data; | |
1318 | while (curr_schema[0] && curr_schema[1] && *curr_data) { | |
1319 | switch (curr_schema[0]) { | |
1320 | case 'l': | |
1321 | if (qemu_strtoul(curr_data, &curr_data, 16, | |
1322 | ¶ms[curr_param].val_ul)) { | |
1323 | return -EINVAL; | |
1324 | } | |
1325 | curr_param++; | |
1326 | curr_data = cmd_next_param(curr_data, curr_schema[1]); | |
1327 | break; | |
1328 | case 'L': | |
1329 | if (qemu_strtou64(curr_data, &curr_data, 16, | |
1330 | (uint64_t *)¶ms[curr_param].val_ull)) { | |
1331 | return -EINVAL; | |
1332 | } | |
1333 | curr_param++; | |
1334 | curr_data = cmd_next_param(curr_data, curr_schema[1]); | |
1335 | break; | |
1336 | case 's': | |
1337 | params[curr_param].data = curr_data; | |
1338 | curr_param++; | |
1339 | curr_data = cmd_next_param(curr_data, curr_schema[1]); | |
1340 | break; | |
1341 | case 'o': | |
1342 | params[curr_param].opcode = *(uint8_t *)curr_data; | |
1343 | curr_param++; | |
1344 | curr_data = cmd_next_param(curr_data, curr_schema[1]); | |
1345 | break; | |
1346 | case 't': | |
1347 | params[curr_param].thread_id.kind = | |
1348 | read_thread_id(curr_data, &curr_data, | |
1349 | ¶ms[curr_param].thread_id.pid, | |
1350 | ¶ms[curr_param].thread_id.tid); | |
1351 | curr_param++; | |
1352 | curr_data = cmd_next_param(curr_data, curr_schema[1]); | |
1353 | break; | |
1354 | case '?': | |
1355 | curr_data = cmd_next_param(curr_data, curr_schema[1]); | |
1356 | break; | |
1357 | default: | |
1358 | return -EINVAL; | |
1359 | } | |
1360 | curr_schema += 2; | |
1361 | } | |
1362 | ||
1363 | *num_params = curr_param; | |
1364 | return 0; | |
1365 | } | |
1366 | ||
1367 | typedef struct GdbCmdContext { | |
1368 | GDBState *s; | |
1369 | GdbCmdVariant *params; | |
1370 | int num_params; | |
1371 | uint8_t mem_buf[MAX_PACKET_LENGTH]; | |
1372 | char str_buf[MAX_PACKET_LENGTH + 1]; | |
1373 | } GdbCmdContext; | |
1374 | ||
1375 | typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx); | |
1376 | ||
1377 | /* | |
1378 | * cmd_startswith -> cmd is compared using startswith | |
1379 | * | |
1380 | * | |
1381 | * schema definitions: | |
1382 | * Each schema parameter entry consists of 2 chars, | |
1383 | * the first char represents the parameter type handling | |
1384 | * the second char represents the delimiter for the next parameter | |
1385 | * | |
1386 | * Currently supported schema types: | |
1387 | * 'l' -> unsigned long (stored in .val_ul) | |
1388 | * 'L' -> unsigned long long (stored in .val_ull) | |
1389 | * 's' -> string (stored in .data) | |
1390 | * 'o' -> single char (stored in .opcode) | |
1391 | * 't' -> thread id (stored in .thread_id) | |
1392 | * '?' -> skip according to delimiter | |
1393 | * | |
1394 | * Currently supported delimiters: | |
1395 | * '?' -> Stop at any delimiter (",;:=\0") | |
1396 | * '0' -> Stop at "\0" | |
1397 | * '.' -> Skip 1 char unless reached "\0" | |
1398 | * Any other value is treated as the delimiter value itself | |
1399 | */ | |
1400 | typedef struct GdbCmdParseEntry { | |
1401 | GdbCmdHandler handler; | |
1402 | const char *cmd; | |
1403 | bool cmd_startswith; | |
1404 | const char *schema; | |
1405 | } GdbCmdParseEntry; | |
1406 | ||
1407 | static inline int startswith(const char *string, const char *pattern) | |
1408 | { | |
1409 | return !strncmp(string, pattern, strlen(pattern)); | |
1410 | } | |
1411 | ||
d14055dc JD |
1412 | static int process_string_cmd(GDBState *s, void *user_ctx, const char *data, |
1413 | const GdbCmdParseEntry *cmds, int num_cmds) | |
1414 | { | |
1415 | int i, schema_len, max_num_params = 0; | |
1416 | GdbCmdContext gdb_ctx; | |
1417 | ||
1418 | if (!cmds) { | |
1419 | return -1; | |
1420 | } | |
1421 | ||
1422 | for (i = 0; i < num_cmds; i++) { | |
1423 | const GdbCmdParseEntry *cmd = &cmds[i]; | |
1424 | g_assert(cmd->handler && cmd->cmd); | |
1425 | ||
1426 | if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) || | |
1427 | (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) { | |
1428 | continue; | |
1429 | } | |
1430 | ||
1431 | if (cmd->schema) { | |
1432 | schema_len = strlen(cmd->schema); | |
1433 | if (schema_len % 2) { | |
1434 | return -2; | |
1435 | } | |
1436 | ||
1437 | max_num_params = schema_len / 2; | |
1438 | } | |
1439 | ||
1440 | gdb_ctx.params = | |
1441 | (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params); | |
1442 | memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params); | |
1443 | ||
1444 | if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema, | |
1445 | gdb_ctx.params, &gdb_ctx.num_params)) { | |
1446 | return -1; | |
1447 | } | |
1448 | ||
1449 | gdb_ctx.s = s; | |
1450 | cmd->handler(&gdb_ctx, user_ctx); | |
1451 | return 0; | |
1452 | } | |
1453 | ||
1454 | return -1; | |
1455 | } | |
1456 | ||
3e2c1261 JD |
1457 | static void run_cmd_parser(GDBState *s, const char *data, |
1458 | const GdbCmdParseEntry *cmd) | |
1459 | { | |
1460 | if (!data) { | |
1461 | return; | |
1462 | } | |
1463 | ||
1464 | /* In case there was an error during the command parsing we must | |
1465 | * send a NULL packet to indicate the command is not supported */ | |
1466 | if (process_string_cmd(s, NULL, data, cmd, 1)) { | |
1467 | put_packet(s, ""); | |
1468 | } | |
1469 | } | |
1470 | ||
1471 | static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx) | |
1472 | { | |
1473 | GDBProcess *process; | |
1474 | GDBState *s = gdb_ctx->s; | |
1475 | uint32_t pid = 1; | |
1476 | ||
1477 | if (s->multiprocess) { | |
1478 | if (!gdb_ctx->num_params) { | |
1479 | put_packet(s, "E22"); | |
1480 | return; | |
1481 | } | |
1482 | ||
1483 | pid = gdb_ctx->params[0].val_ul; | |
1484 | } | |
1485 | ||
1486 | process = gdb_get_process(s, pid); | |
1487 | gdb_process_breakpoint_remove_all(s, process); | |
1488 | process->attached = false; | |
1489 | ||
1490 | if (pid == gdb_get_cpu_pid(s, s->c_cpu)) { | |
1491 | s->c_cpu = gdb_first_attached_cpu(s); | |
1492 | } | |
1493 | ||
1494 | if (pid == gdb_get_cpu_pid(s, s->g_cpu)) { | |
1495 | s->g_cpu = gdb_first_attached_cpu(s); | |
1496 | } | |
1497 | ||
1498 | if (!s->c_cpu) { | |
1499 | /* No more process attached */ | |
1500 | gdb_syscall_mode = GDB_SYS_DISABLED; | |
1501 | gdb_continue(s); | |
1502 | } | |
1503 | put_packet(s, "OK"); | |
1504 | } | |
1505 | ||
44ffded0 JD |
1506 | static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx) |
1507 | { | |
1508 | CPUState *cpu; | |
1509 | ||
1510 | if (!gdb_ctx->num_params) { | |
1511 | put_packet(gdb_ctx->s, "E22"); | |
1512 | return; | |
1513 | } | |
1514 | ||
1515 | if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) { | |
1516 | put_packet(gdb_ctx->s, "E22"); | |
1517 | return; | |
1518 | } | |
1519 | ||
1520 | cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid, | |
1521 | gdb_ctx->params[0].thread_id.tid); | |
1522 | if (!cpu) { | |
1523 | put_packet(gdb_ctx->s, "E22"); | |
1524 | return; | |
1525 | } | |
1526 | ||
1527 | put_packet(gdb_ctx->s, "OK"); | |
1528 | } | |
1529 | ||
4d6e3fe2 JD |
1530 | static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx) |
1531 | { | |
1532 | if (gdb_ctx->num_params) { | |
1533 | gdb_set_cpu_pc(gdb_ctx->s, gdb_ctx->params[0].val_ull); | |
1534 | } | |
1535 | ||
1536 | gdb_ctx->s->signal = 0; | |
1537 | gdb_continue(gdb_ctx->s); | |
1538 | } | |
1539 | ||
ccc47d5d JD |
1540 | static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx) |
1541 | { | |
1542 | unsigned long signal = 0; | |
1543 | ||
1544 | /* | |
1545 | * Note: C sig;[addr] is currently unsupported and we simply | |
1546 | * omit the addr parameter | |
1547 | */ | |
1548 | if (gdb_ctx->num_params) { | |
1549 | signal = gdb_ctx->params[0].val_ul; | |
1550 | } | |
1551 | ||
1552 | gdb_ctx->s->signal = gdb_signal_to_target(signal); | |
1553 | if (gdb_ctx->s->signal == -1) { | |
1554 | gdb_ctx->s->signal = 0; | |
1555 | } | |
1556 | gdb_continue(gdb_ctx->s); | |
1557 | } | |
1558 | ||
3a9651d6 JD |
1559 | static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx) |
1560 | { | |
1561 | CPUState *cpu; | |
1562 | ||
1563 | if (gdb_ctx->num_params != 2) { | |
1564 | put_packet(gdb_ctx->s, "E22"); | |
1565 | return; | |
1566 | } | |
1567 | ||
1568 | if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) { | |
1569 | put_packet(gdb_ctx->s, "E22"); | |
1570 | return; | |
1571 | } | |
1572 | ||
1573 | if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) { | |
1574 | put_packet(gdb_ctx->s, "OK"); | |
1575 | return; | |
1576 | } | |
1577 | ||
1578 | cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[1].thread_id.pid, | |
1579 | gdb_ctx->params[1].thread_id.tid); | |
1580 | if (!cpu) { | |
1581 | put_packet(gdb_ctx->s, "E22"); | |
1582 | return; | |
1583 | } | |
1584 | ||
1585 | /* | |
1586 | * Note: This command is deprecated and modern gdb's will be using the | |
1587 | * vCont command instead. | |
1588 | */ | |
1589 | switch (gdb_ctx->params[0].opcode) { | |
1590 | case 'c': | |
1591 | gdb_ctx->s->c_cpu = cpu; | |
1592 | put_packet(gdb_ctx->s, "OK"); | |
1593 | break; | |
1594 | case 'g': | |
1595 | gdb_ctx->s->g_cpu = cpu; | |
1596 | put_packet(gdb_ctx->s, "OK"); | |
1597 | break; | |
1598 | default: | |
1599 | put_packet(gdb_ctx->s, "E22"); | |
1600 | break; | |
1601 | } | |
1602 | } | |
1603 | ||
77f6ce50 JD |
1604 | static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx) |
1605 | { | |
1606 | int res; | |
1607 | ||
1608 | if (gdb_ctx->num_params != 3) { | |
1609 | put_packet(gdb_ctx->s, "E22"); | |
1610 | return; | |
1611 | } | |
1612 | ||
1613 | res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul, | |
1614 | gdb_ctx->params[1].val_ull, | |
1615 | gdb_ctx->params[2].val_ull); | |
1616 | if (res >= 0) { | |
1617 | put_packet(gdb_ctx->s, "OK"); | |
1618 | return; | |
1619 | } else if (res == -ENOSYS) { | |
1620 | put_packet(gdb_ctx->s, ""); | |
1621 | return; | |
1622 | } | |
1623 | ||
1624 | put_packet(gdb_ctx->s, "E22"); | |
1625 | } | |
1626 | ||
1627 | static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx) | |
1628 | { | |
1629 | int res; | |
1630 | ||
1631 | if (gdb_ctx->num_params != 3) { | |
1632 | put_packet(gdb_ctx->s, "E22"); | |
1633 | return; | |
1634 | } | |
1635 | ||
1636 | res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul, | |
1637 | gdb_ctx->params[1].val_ull, | |
1638 | gdb_ctx->params[2].val_ull); | |
1639 | if (res >= 0) { | |
1640 | put_packet(gdb_ctx->s, "OK"); | |
1641 | return; | |
1642 | } else if (res == -ENOSYS) { | |
1643 | put_packet(gdb_ctx->s, ""); | |
1644 | return; | |
1645 | } | |
1646 | ||
1647 | put_packet(gdb_ctx->s, "E22"); | |
1648 | } | |
1649 | ||
62b3320b JD |
1650 | static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx) |
1651 | { | |
1652 | int reg_size; | |
1653 | ||
1654 | if (!gdb_has_xml) { | |
1655 | put_packet(gdb_ctx->s, "E00"); | |
1656 | return; | |
1657 | } | |
1658 | ||
1659 | if (gdb_ctx->num_params != 2) { | |
1660 | put_packet(gdb_ctx->s, "E22"); | |
1661 | return; | |
1662 | } | |
1663 | ||
1664 | reg_size = strlen(gdb_ctx->params[1].data) / 2; | |
1665 | hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size); | |
1666 | gdb_write_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf, | |
1667 | gdb_ctx->params[0].val_ull); | |
1668 | put_packet(gdb_ctx->s, "OK"); | |
1669 | } | |
1670 | ||
5d0e57bd JD |
1671 | static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx) |
1672 | { | |
1673 | int reg_size; | |
1674 | ||
1675 | /* | |
1676 | * Older gdb are really dumb, and don't use 'g' if 'p' is avaialable. | |
1677 | * This works, but can be very slow. Anything new enough to | |
1678 | * understand XML also knows how to use this properly. | |
1679 | */ | |
1680 | if (!gdb_has_xml) { | |
1681 | put_packet(gdb_ctx->s, ""); | |
1682 | return; | |
1683 | } | |
1684 | ||
1685 | if (!gdb_ctx->num_params) { | |
1686 | put_packet(gdb_ctx->s, "E14"); | |
1687 | return; | |
1688 | } | |
1689 | ||
1690 | reg_size = gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf, | |
1691 | gdb_ctx->params[0].val_ull); | |
1692 | if (!reg_size) { | |
1693 | put_packet(gdb_ctx->s, "E14"); | |
1694 | return; | |
1695 | } | |
1696 | ||
1697 | memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, reg_size); | |
1698 | put_packet(gdb_ctx->s, gdb_ctx->str_buf); | |
1699 | } | |
1700 | ||
cc0ecc78 JD |
1701 | static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx) |
1702 | { | |
1703 | if (gdb_ctx->num_params != 3) { | |
1704 | put_packet(gdb_ctx->s, "E22"); | |
1705 | return; | |
1706 | } | |
1707 | ||
1708 | /* hextomem() reads 2*len bytes */ | |
1709 | if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) { | |
1710 | put_packet(gdb_ctx->s, "E22"); | |
1711 | return; | |
1712 | } | |
1713 | ||
1714 | hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data, | |
1715 | gdb_ctx->params[1].val_ull); | |
1716 | if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull, | |
1717 | gdb_ctx->mem_buf, | |
1718 | gdb_ctx->params[1].val_ull, true)) { | |
1719 | put_packet(gdb_ctx->s, "E14"); | |
1720 | return; | |
1721 | } | |
1722 | ||
1723 | put_packet(gdb_ctx->s, "OK"); | |
1724 | } | |
1725 | ||
da92e236 JD |
1726 | static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx) |
1727 | { | |
1728 | if (gdb_ctx->num_params != 2) { | |
1729 | put_packet(gdb_ctx->s, "E22"); | |
1730 | return; | |
1731 | } | |
1732 | ||
1733 | /* memtohex() doubles the required space */ | |
1734 | if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) { | |
1735 | put_packet(gdb_ctx->s, "E22"); | |
1736 | return; | |
1737 | } | |
1738 | ||
1739 | if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull, | |
1740 | gdb_ctx->mem_buf, | |
1741 | gdb_ctx->params[1].val_ull, false)) { | |
1742 | put_packet(gdb_ctx->s, "E14"); | |
1743 | return; | |
1744 | } | |
1745 | ||
1746 | memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1].val_ull); | |
1747 | put_packet(gdb_ctx->s, gdb_ctx->str_buf); | |
1748 | } | |
1749 | ||
287ca120 JD |
1750 | static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx) |
1751 | { | |
1752 | target_ulong addr, len; | |
1753 | uint8_t *registers; | |
1754 | int reg_size; | |
1755 | ||
1756 | if (!gdb_ctx->num_params) { | |
1757 | return; | |
1758 | } | |
1759 | ||
1760 | cpu_synchronize_state(gdb_ctx->s->g_cpu); | |
1761 | registers = gdb_ctx->mem_buf; | |
1762 | len = strlen(gdb_ctx->params[0].data) / 2; | |
1763 | hextomem(registers, gdb_ctx->params[0].data, len); | |
1764 | for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs && len > 0; | |
1765 | addr++) { | |
1766 | reg_size = gdb_write_register(gdb_ctx->s->g_cpu, registers, addr); | |
1767 | len -= reg_size; | |
1768 | registers += reg_size; | |
1769 | } | |
1770 | put_packet(gdb_ctx->s, "OK"); | |
1771 | } | |
1772 | ||
397d1370 JD |
1773 | static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx) |
1774 | { | |
1775 | target_ulong addr, len; | |
1776 | ||
1777 | cpu_synchronize_state(gdb_ctx->s->g_cpu); | |
1778 | len = 0; | |
1779 | for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs; addr++) { | |
1780 | len += gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf + len, | |
1781 | addr); | |
1782 | } | |
1783 | ||
1784 | memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len); | |
1785 | put_packet(gdb_ctx->s, gdb_ctx->str_buf); | |
1786 | } | |
1787 | ||
4b20fab1 JD |
1788 | static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx) |
1789 | { | |
1790 | if (gdb_ctx->num_params >= 2 && gdb_ctx->s->current_syscall_cb) { | |
1791 | target_ulong ret, err; | |
1792 | ||
1793 | ret = (target_ulong)gdb_ctx->params[0].val_ull; | |
1794 | err = (target_ulong)gdb_ctx->params[1].val_ull; | |
1795 | gdb_ctx->s->current_syscall_cb(gdb_ctx->s->c_cpu, ret, err); | |
1796 | gdb_ctx->s->current_syscall_cb = NULL; | |
1797 | } | |
1798 | ||
1799 | if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') { | |
1800 | put_packet(gdb_ctx->s, "T02"); | |
1801 | return; | |
1802 | } | |
1803 | ||
1804 | gdb_continue(gdb_ctx->s); | |
1805 | } | |
1806 | ||
933f80dd JD |
1807 | static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx) |
1808 | { | |
1809 | if (gdb_ctx->num_params) { | |
1810 | gdb_set_cpu_pc(gdb_ctx->s, (target_ulong)gdb_ctx->params[0].val_ull); | |
1811 | } | |
1812 | ||
1813 | cpu_single_step(gdb_ctx->s->c_cpu, sstep_flags); | |
1814 | gdb_continue(gdb_ctx->s); | |
1815 | } | |
1816 | ||
8536ec02 JD |
1817 | static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx) |
1818 | { | |
1819 | put_packet(gdb_ctx->s, "vCont;c;C;s;S"); | |
1820 | } | |
1821 | ||
1822 | static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx) | |
1823 | { | |
1824 | int res; | |
1825 | ||
1826 | if (!gdb_ctx->num_params) { | |
1827 | return; | |
1828 | } | |
1829 | ||
1830 | res = gdb_handle_vcont(gdb_ctx->s, gdb_ctx->params[0].data); | |
1831 | if ((res == -EINVAL) || (res == -ERANGE)) { | |
1832 | put_packet(gdb_ctx->s, "E22"); | |
1833 | } else if (res) { | |
1834 | put_packet(gdb_ctx->s, ""); | |
1835 | } | |
1836 | } | |
1837 | ||
1838 | static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx) | |
1839 | { | |
1840 | GDBProcess *process; | |
1841 | CPUState *cpu; | |
1842 | char thread_id[16]; | |
1843 | ||
1844 | pstrcpy(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "E22"); | |
1845 | if (!gdb_ctx->num_params) { | |
1846 | goto cleanup; | |
1847 | } | |
1848 | ||
1849 | process = gdb_get_process(gdb_ctx->s, gdb_ctx->params[0].val_ul); | |
1850 | if (!process) { | |
1851 | goto cleanup; | |
1852 | } | |
1853 | ||
1854 | cpu = get_first_cpu_in_process(gdb_ctx->s, process); | |
1855 | if (!cpu) { | |
1856 | goto cleanup; | |
1857 | } | |
1858 | ||
1859 | process->attached = true; | |
1860 | gdb_ctx->s->g_cpu = cpu; | |
1861 | gdb_ctx->s->c_cpu = cpu; | |
1862 | ||
1863 | gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id)); | |
1864 | snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;", | |
1865 | GDB_SIGNAL_TRAP, thread_id); | |
1866 | cleanup: | |
1867 | put_packet(gdb_ctx->s, gdb_ctx->str_buf); | |
1868 | } | |
1869 | ||
1870 | static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx) | |
1871 | { | |
1872 | /* Kill the target */ | |
1873 | put_packet(gdb_ctx->s, "OK"); | |
1874 | error_report("QEMU: Terminated via GDBstub"); | |
1875 | exit(0); | |
1876 | } | |
1877 | ||
1878 | static GdbCmdParseEntry gdb_v_commands_table[] = { | |
1879 | /* Order is important if has same prefix */ | |
1880 | { | |
1881 | .handler = handle_v_cont_query, | |
1882 | .cmd = "Cont?", | |
1883 | .cmd_startswith = 1 | |
1884 | }, | |
1885 | { | |
1886 | .handler = handle_v_cont, | |
1887 | .cmd = "Cont", | |
1888 | .cmd_startswith = 1, | |
1889 | .schema = "s0" | |
1890 | }, | |
1891 | { | |
1892 | .handler = handle_v_attach, | |
1893 | .cmd = "Attach;", | |
1894 | .cmd_startswith = 1, | |
1895 | .schema = "l0" | |
1896 | }, | |
1897 | { | |
1898 | .handler = handle_v_kill, | |
1899 | .cmd = "Kill;", | |
1900 | .cmd_startswith = 1 | |
1901 | }, | |
1902 | }; | |
1903 | ||
1904 | static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx) | |
1905 | { | |
1906 | if (!gdb_ctx->num_params) { | |
1907 | return; | |
1908 | } | |
1909 | ||
1910 | if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data, | |
1911 | gdb_v_commands_table, | |
1912 | ARRAY_SIZE(gdb_v_commands_table))) { | |
1913 | put_packet(gdb_ctx->s, ""); | |
1914 | } | |
1915 | } | |
1916 | ||
2704efad JD |
1917 | static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx) |
1918 | { | |
1919 | snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), | |
1920 | "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE, | |
1921 | SSTEP_NOIRQ, SSTEP_NOTIMER); | |
1922 | put_packet(gdb_ctx->s, gdb_ctx->str_buf); | |
1923 | } | |
1924 | ||
1925 | static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx) | |
1926 | { | |
1927 | if (!gdb_ctx->num_params) { | |
1928 | return; | |
1929 | } | |
1930 | ||
1931 | sstep_flags = gdb_ctx->params[0].val_ul; | |
1932 | put_packet(gdb_ctx->s, "OK"); | |
1933 | } | |
1934 | ||
1935 | static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx) | |
1936 | { | |
1937 | snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "0x%x", sstep_flags); | |
1938 | put_packet(gdb_ctx->s, gdb_ctx->str_buf); | |
1939 | } | |
1940 | ||
1941 | static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx) | |
b4608c04 | 1942 | { |
2e0f2cfb | 1943 | CPUState *cpu; |
2704efad JD |
1944 | GDBProcess *process; |
1945 | char thread_id[16]; | |
1946 | ||
1947 | /* | |
1948 | * "Current thread" remains vague in the spec, so always return | |
1949 | * the first thread of the current process (gdb returns the | |
1950 | * first thread). | |
1951 | */ | |
1952 | process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu); | |
1953 | cpu = get_first_cpu_in_process(gdb_ctx->s, process); | |
1954 | gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id)); | |
1955 | snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "QC%s", thread_id); | |
1956 | put_packet(gdb_ctx->s, gdb_ctx->str_buf); | |
1957 | } | |
1958 | ||
1959 | static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx) | |
1960 | { | |
1961 | char thread_id[16]; | |
1962 | ||
1963 | if (!gdb_ctx->s->query_cpu) { | |
1964 | put_packet(gdb_ctx->s, "l"); | |
1965 | return; | |
1966 | } | |
1967 | ||
1968 | gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->query_cpu, thread_id, | |
1969 | sizeof(thread_id)); | |
1970 | snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "m%s", thread_id); | |
1971 | put_packet(gdb_ctx->s, gdb_ctx->str_buf); | |
1972 | gdb_ctx->s->query_cpu = | |
1973 | gdb_next_attached_cpu(gdb_ctx->s, gdb_ctx->s->query_cpu); | |
1974 | } | |
1975 | ||
1976 | static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx) | |
1977 | { | |
1978 | gdb_ctx->s->query_cpu = gdb_first_attached_cpu(gdb_ctx->s); | |
1979 | handle_query_threads(gdb_ctx, user_ctx); | |
1980 | } | |
1981 | ||
1982 | static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx) | |
1983 | { | |
1984 | CPUState *cpu; | |
1985 | int len; | |
1986 | ||
1987 | if (!gdb_ctx->num_params || | |
1988 | gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) { | |
1989 | put_packet(gdb_ctx->s, "E22"); | |
1990 | return; | |
1991 | } | |
1992 | ||
1993 | cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid, | |
1994 | gdb_ctx->params[0].thread_id.tid); | |
1995 | if (!cpu) { | |
1996 | return; | |
1997 | } | |
1998 | ||
1999 | cpu_synchronize_state(cpu); | |
2000 | ||
2001 | if (gdb_ctx->s->multiprocess && (gdb_ctx->s->process_num > 1)) { | |
2002 | /* Print the CPU model and name in multiprocess mode */ | |
2003 | ObjectClass *oc = object_get_class(OBJECT(cpu)); | |
2004 | const char *cpu_model = object_class_get_name(oc); | |
2005 | char *cpu_name = object_get_canonical_path_component(OBJECT(cpu)); | |
2006 | len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2, | |
2007 | "%s %s [%s]", cpu_model, cpu_name, | |
2008 | cpu->halted ? "halted " : "running"); | |
2009 | g_free(cpu_name); | |
2010 | } else { | |
2011 | /* memtohex() doubles the required space */ | |
2012 | len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2, | |
2013 | "CPU#%d [%s]", cpu->cpu_index, | |
2014 | cpu->halted ? "halted " : "running"); | |
2015 | } | |
2016 | trace_gdbstub_op_extra_info((char *)gdb_ctx->mem_buf); | |
2017 | memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len); | |
2018 | put_packet(gdb_ctx->s, gdb_ctx->str_buf); | |
2019 | } | |
2020 | ||
2021 | #ifdef CONFIG_USER_ONLY | |
2022 | static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx) | |
2023 | { | |
2024 | TaskState *ts; | |
2025 | ||
2026 | ts = gdb_ctx->s->c_cpu->opaque; | |
2027 | snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), | |
2028 | "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx | |
2029 | ";Bss=" TARGET_ABI_FMT_lx, | |
2030 | ts->info->code_offset, | |
2031 | ts->info->data_offset, | |
2032 | ts->info->data_offset); | |
2033 | put_packet(gdb_ctx->s, gdb_ctx->str_buf); | |
2034 | } | |
2035 | #else | |
2036 | static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx) | |
2037 | { | |
2038 | int len; | |
2039 | ||
2040 | if (!gdb_ctx->num_params) { | |
2041 | put_packet(gdb_ctx->s, "E22"); | |
2042 | return; | |
2043 | } | |
2044 | ||
2045 | len = strlen(gdb_ctx->params[0].data); | |
2046 | if (len % 2) { | |
2047 | put_packet(gdb_ctx->s, "E01"); | |
2048 | return; | |
2049 | } | |
2050 | ||
2051 | len = len / 2; | |
2052 | hextomem(gdb_ctx->mem_buf, gdb_ctx->params[0].data, len); | |
2053 | gdb_ctx->mem_buf[len++] = 0; | |
2054 | qemu_chr_be_write(gdb_ctx->s->mon_chr, gdb_ctx->mem_buf, len); | |
2055 | put_packet(gdb_ctx->s, "OK"); | |
2056 | ||
2057 | } | |
2058 | #endif | |
2059 | ||
2060 | static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx) | |
2061 | { | |
2062 | CPUClass *cc; | |
2063 | ||
2064 | snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "PacketSize=%x", | |
2065 | MAX_PACKET_LENGTH); | |
2066 | cc = CPU_GET_CLASS(first_cpu); | |
2067 | if (cc->gdb_core_xml_file) { | |
2068 | pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), | |
2069 | ";qXfer:features:read+"); | |
2070 | } | |
2071 | ||
2072 | if (gdb_ctx->num_params && | |
2073 | strstr(gdb_ctx->params[0].data, "multiprocess+")) { | |
2074 | gdb_ctx->s->multiprocess = true; | |
2075 | } | |
2076 | ||
2077 | pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";multiprocess+"); | |
2078 | put_packet(gdb_ctx->s, gdb_ctx->str_buf); | |
2079 | } | |
2080 | ||
2081 | static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx) | |
2082 | { | |
c145eeae | 2083 | GDBProcess *process; |
5b24c641 | 2084 | CPUClass *cc; |
2704efad JD |
2085 | unsigned long len, total_len, addr; |
2086 | const char *xml; | |
b4608c04 | 2087 | const char *p; |
2704efad JD |
2088 | |
2089 | if (gdb_ctx->num_params < 3) { | |
2090 | put_packet(gdb_ctx->s, "E22"); | |
2091 | return; | |
2092 | } | |
2093 | ||
2094 | process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu); | |
2095 | cc = CPU_GET_CLASS(gdb_ctx->s->g_cpu); | |
2096 | if (!cc->gdb_core_xml_file) { | |
2097 | put_packet(gdb_ctx->s, ""); | |
2098 | return; | |
2099 | } | |
2100 | ||
2101 | gdb_has_xml = true; | |
2102 | p = gdb_ctx->params[0].data; | |
2103 | xml = get_feature_xml(gdb_ctx->s, p, &p, process); | |
2104 | if (!xml) { | |
2105 | put_packet(gdb_ctx->s, "E00"); | |
2106 | return; | |
2107 | } | |
2108 | ||
2109 | addr = gdb_ctx->params[1].val_ul; | |
2110 | len = gdb_ctx->params[2].val_ul; | |
2111 | total_len = strlen(xml); | |
2112 | if (addr > total_len) { | |
2113 | put_packet(gdb_ctx->s, "E00"); | |
2114 | return; | |
2115 | } | |
2116 | ||
2117 | if (len > (MAX_PACKET_LENGTH - 5) / 2) { | |
2118 | len = (MAX_PACKET_LENGTH - 5) / 2; | |
2119 | } | |
2120 | ||
2121 | if (len < total_len - addr) { | |
2122 | gdb_ctx->str_buf[0] = 'm'; | |
2123 | len = memtox(gdb_ctx->str_buf + 1, xml + addr, len); | |
2124 | } else { | |
2125 | gdb_ctx->str_buf[0] = 'l'; | |
2126 | len = memtox(gdb_ctx->str_buf + 1, xml + addr, total_len - addr); | |
2127 | } | |
2128 | ||
2129 | put_packet_binary(gdb_ctx->s, gdb_ctx->str_buf, len + 1, true); | |
2130 | } | |
2131 | ||
2132 | static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx) | |
2133 | { | |
2134 | put_packet(gdb_ctx->s, GDB_ATTACHED); | |
2135 | } | |
2136 | ||
2137 | static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx) | |
2138 | { | |
2139 | put_packet(gdb_ctx->s, "sstepbits;sstep"); | |
2140 | } | |
2141 | ||
2142 | static GdbCmdParseEntry gdb_gen_query_set_common_table[] = { | |
2143 | /* Order is important if has same prefix */ | |
2144 | { | |
2145 | .handler = handle_query_qemu_sstepbits, | |
2146 | .cmd = "qemu.sstepbits", | |
2147 | }, | |
2148 | { | |
2149 | .handler = handle_query_qemu_sstep, | |
2150 | .cmd = "qemu.sstep", | |
2151 | }, | |
2152 | { | |
2153 | .handler = handle_set_qemu_sstep, | |
2154 | .cmd = "qemu.sstep=", | |
2155 | .cmd_startswith = 1, | |
2156 | .schema = "l0" | |
2157 | }, | |
2158 | }; | |
2159 | ||
2160 | static GdbCmdParseEntry gdb_gen_query_table[] = { | |
2161 | { | |
2162 | .handler = handle_query_curr_tid, | |
2163 | .cmd = "C", | |
2164 | }, | |
2165 | { | |
2166 | .handler = handle_query_threads, | |
2167 | .cmd = "sThreadInfo", | |
2168 | }, | |
2169 | { | |
2170 | .handler = handle_query_first_threads, | |
2171 | .cmd = "fThreadInfo", | |
2172 | }, | |
2173 | { | |
2174 | .handler = handle_query_thread_extra, | |
2175 | .cmd = "ThreadExtraInfo,", | |
2176 | .cmd_startswith = 1, | |
2177 | .schema = "t0" | |
2178 | }, | |
2179 | #ifdef CONFIG_USER_ONLY | |
2180 | { | |
2181 | .handler = handle_query_offsets, | |
2182 | .cmd = "Offsets", | |
2183 | }, | |
2184 | #else | |
2185 | { | |
2186 | .handler = handle_query_rcmd, | |
2187 | .cmd = "Rcmd,", | |
2188 | .cmd_startswith = 1, | |
2189 | .schema = "s0" | |
2190 | }, | |
2191 | #endif | |
2192 | { | |
2193 | .handler = handle_query_supported, | |
2194 | .cmd = "Supported:", | |
2195 | .cmd_startswith = 1, | |
2196 | .schema = "s0" | |
2197 | }, | |
2198 | { | |
2199 | .handler = handle_query_supported, | |
2200 | .cmd = "Supported", | |
2201 | .schema = "s0" | |
2202 | }, | |
2203 | { | |
2204 | .handler = handle_query_xfer_features, | |
2205 | .cmd = "Xfer:features:read:", | |
2206 | .cmd_startswith = 1, | |
2207 | .schema = "s:l,l0" | |
2208 | }, | |
2209 | { | |
2210 | .handler = handle_query_attached, | |
2211 | .cmd = "Attached:", | |
2212 | .cmd_startswith = 1 | |
2213 | }, | |
2214 | { | |
2215 | .handler = handle_query_attached, | |
2216 | .cmd = "Attached", | |
2217 | }, | |
2218 | { | |
2219 | .handler = handle_query_qemu_supported, | |
2220 | .cmd = "qemu.Supported", | |
2221 | }, | |
2222 | }; | |
2223 | ||
2224 | static GdbCmdParseEntry gdb_gen_set_table[] = { | |
2225 | /* Order is important if has same prefix */ | |
2226 | { | |
2227 | .handler = handle_set_qemu_sstep, | |
2228 | .cmd = "qemu.sstep:", | |
2229 | .cmd_startswith = 1, | |
2230 | .schema = "l0" | |
2231 | }, | |
2232 | }; | |
2233 | ||
2234 | static void handle_gen_query(GdbCmdContext *gdb_ctx, void *user_ctx) | |
2235 | { | |
2236 | if (!gdb_ctx->num_params) { | |
2237 | return; | |
2238 | } | |
2239 | ||
2240 | if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data, | |
2241 | gdb_gen_query_set_common_table, | |
2242 | ARRAY_SIZE(gdb_gen_query_set_common_table))) { | |
2243 | return; | |
2244 | } | |
2245 | ||
2246 | if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data, | |
2247 | gdb_gen_query_table, | |
2248 | ARRAY_SIZE(gdb_gen_query_table))) { | |
2249 | put_packet(gdb_ctx->s, ""); | |
2250 | } | |
2251 | } | |
2252 | ||
2253 | static void handle_gen_set(GdbCmdContext *gdb_ctx, void *user_ctx) | |
2254 | { | |
2255 | if (!gdb_ctx->num_params) { | |
2256 | return; | |
2257 | } | |
2258 | ||
2259 | if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data, | |
2260 | gdb_gen_query_set_common_table, | |
2261 | ARRAY_SIZE(gdb_gen_query_set_common_table))) { | |
2262 | return; | |
2263 | } | |
2264 | ||
2265 | if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data, | |
2266 | gdb_gen_set_table, | |
2267 | ARRAY_SIZE(gdb_gen_set_table))) { | |
2268 | put_packet(gdb_ctx->s, ""); | |
2269 | } | |
2270 | } | |
2271 | ||
7009d579 JD |
2272 | static void handle_target_halt(GdbCmdContext *gdb_ctx, void *user_ctx) |
2273 | { | |
2274 | char thread_id[16]; | |
2275 | ||
2276 | gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->c_cpu, thread_id, | |
2277 | sizeof(thread_id)); | |
2278 | snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;", | |
2279 | GDB_SIGNAL_TRAP, thread_id); | |
2280 | put_packet(gdb_ctx->s, gdb_ctx->str_buf); | |
2281 | /* | |
2282 | * Remove all the breakpoints when this query is issued, | |
2283 | * because gdb is doing an initial connect and the state | |
2284 | * should be cleaned up. | |
2285 | */ | |
2286 | gdb_breakpoint_remove_all(); | |
2287 | } | |
2288 | ||
2704efad JD |
2289 | static int gdb_handle_packet(GDBState *s, const char *line_buf) |
2290 | { | |
2291 | const char *p; | |
2292 | int ch; | |
56aebc89 | 2293 | uint8_t mem_buf[MAX_PACKET_LENGTH]; |
9005774b | 2294 | char buf[sizeof(mem_buf) + 1 /* trailing NUL */]; |
3e2c1261 | 2295 | const GdbCmdParseEntry *cmd_parser = NULL; |
3b46e624 | 2296 | |
5c9522b3 | 2297 | trace_gdbstub_io_command(line_buf); |
118e2268 | 2298 | |
858693c6 FB |
2299 | p = line_buf; |
2300 | ch = *p++; | |
2301 | switch(ch) { | |
53fd6554 LM |
2302 | case '!': |
2303 | put_packet(s, "OK"); | |
2304 | break; | |
858693c6 | 2305 | case '?': |
7009d579 JD |
2306 | { |
2307 | static const GdbCmdParseEntry target_halted_cmd_desc = { | |
2308 | .handler = handle_target_halt, | |
2309 | .cmd = "?", | |
2310 | .cmd_startswith = 1 | |
2311 | }; | |
2312 | cmd_parser = &target_halted_cmd_desc; | |
2313 | } | |
858693c6 FB |
2314 | break; |
2315 | case 'c': | |
4d6e3fe2 JD |
2316 | { |
2317 | static const GdbCmdParseEntry continue_cmd_desc = { | |
2318 | .handler = handle_continue, | |
2319 | .cmd = "c", | |
2320 | .cmd_startswith = 1, | |
2321 | .schema = "L0" | |
2322 | }; | |
2323 | cmd_parser = &continue_cmd_desc; | |
858693c6 | 2324 | } |
4d6e3fe2 | 2325 | break; |
1f487ee9 | 2326 | case 'C': |
ccc47d5d JD |
2327 | { |
2328 | static const GdbCmdParseEntry cont_with_sig_cmd_desc = { | |
2329 | .handler = handle_cont_with_sig, | |
2330 | .cmd = "C", | |
2331 | .cmd_startswith = 1, | |
2332 | .schema = "l0" | |
2333 | }; | |
2334 | cmd_parser = &cont_with_sig_cmd_desc; | |
2335 | } | |
2336 | break; | |
dd32aa10 | 2337 | case 'v': |
8536ec02 JD |
2338 | { |
2339 | static const GdbCmdParseEntry v_cmd_desc = { | |
2340 | .handler = handle_v_commands, | |
2341 | .cmd = "v", | |
2342 | .cmd_startswith = 1, | |
2343 | .schema = "s0" | |
2344 | }; | |
2345 | cmd_parser = &v_cmd_desc; | |
dd32aa10 | 2346 | } |
8536ec02 | 2347 | break; |
7d03f82f EI |
2348 | case 'k': |
2349 | /* Kill the target */ | |
7ae6c571 | 2350 | error_report("QEMU: Terminated via GDBstub"); |
7d03f82f EI |
2351 | exit(0); |
2352 | case 'D': | |
3e2c1261 JD |
2353 | { |
2354 | static const GdbCmdParseEntry detach_cmd_desc = { | |
2355 | .handler = handle_detach, | |
2356 | .cmd = "D", | |
2357 | .cmd_startswith = 1, | |
2358 | .schema = "?.l0" | |
2359 | }; | |
2360 | cmd_parser = &detach_cmd_desc; | |
546f3c67 | 2361 | } |
7d03f82f | 2362 | break; |
858693c6 | 2363 | case 's': |
933f80dd JD |
2364 | { |
2365 | static const GdbCmdParseEntry step_cmd_desc = { | |
2366 | .handler = handle_step, | |
2367 | .cmd = "s", | |
2368 | .cmd_startswith = 1, | |
2369 | .schema = "L0" | |
2370 | }; | |
2371 | cmd_parser = &step_cmd_desc; | |
858693c6 | 2372 | } |
933f80dd | 2373 | break; |
a2d1ebaf PB |
2374 | case 'F': |
2375 | { | |
4b20fab1 JD |
2376 | static const GdbCmdParseEntry file_io_cmd_desc = { |
2377 | .handler = handle_file_io, | |
2378 | .cmd = "F", | |
2379 | .cmd_startswith = 1, | |
2380 | .schema = "L,L,o0" | |
2381 | }; | |
2382 | cmd_parser = &file_io_cmd_desc; | |
a2d1ebaf PB |
2383 | } |
2384 | break; | |
858693c6 | 2385 | case 'g': |
397d1370 JD |
2386 | { |
2387 | static const GdbCmdParseEntry read_all_regs_cmd_desc = { | |
2388 | .handler = handle_read_all_regs, | |
2389 | .cmd = "g", | |
2390 | .cmd_startswith = 1 | |
2391 | }; | |
2392 | cmd_parser = &read_all_regs_cmd_desc; | |
56aebc89 | 2393 | } |
858693c6 FB |
2394 | break; |
2395 | case 'G': | |
287ca120 JD |
2396 | { |
2397 | static const GdbCmdParseEntry write_all_regs_cmd_desc = { | |
2398 | .handler = handle_write_all_regs, | |
2399 | .cmd = "G", | |
2400 | .cmd_startswith = 1, | |
2401 | .schema = "s0" | |
2402 | }; | |
2403 | cmd_parser = &write_all_regs_cmd_desc; | |
56aebc89 | 2404 | } |
858693c6 FB |
2405 | break; |
2406 | case 'm': | |
da92e236 JD |
2407 | { |
2408 | static const GdbCmdParseEntry read_mem_cmd_desc = { | |
2409 | .handler = handle_read_mem, | |
2410 | .cmd = "m", | |
2411 | .cmd_startswith = 1, | |
2412 | .schema = "L,L0" | |
2413 | }; | |
2414 | cmd_parser = &read_mem_cmd_desc; | |
6f970bd9 | 2415 | } |
858693c6 FB |
2416 | break; |
2417 | case 'M': | |
cc0ecc78 JD |
2418 | { |
2419 | static const GdbCmdParseEntry write_mem_cmd_desc = { | |
2420 | .handler = handle_write_mem, | |
2421 | .cmd = "M", | |
2422 | .cmd_startswith = 1, | |
2423 | .schema = "L,L:s0" | |
2424 | }; | |
2425 | cmd_parser = &write_mem_cmd_desc; | |
44520db1 | 2426 | } |
858693c6 | 2427 | break; |
56aebc89 | 2428 | case 'p': |
5d0e57bd JD |
2429 | { |
2430 | static const GdbCmdParseEntry get_reg_cmd_desc = { | |
2431 | .handler = handle_get_reg, | |
2432 | .cmd = "p", | |
2433 | .cmd_startswith = 1, | |
2434 | .schema = "L0" | |
2435 | }; | |
2436 | cmd_parser = &get_reg_cmd_desc; | |
56aebc89 PB |
2437 | } |
2438 | break; | |
2439 | case 'P': | |
62b3320b JD |
2440 | { |
2441 | static const GdbCmdParseEntry set_reg_cmd_desc = { | |
2442 | .handler = handle_set_reg, | |
2443 | .cmd = "P", | |
2444 | .cmd_startswith = 1, | |
2445 | .schema = "L?s0" | |
2446 | }; | |
2447 | cmd_parser = &set_reg_cmd_desc; | |
2448 | } | |
56aebc89 | 2449 | break; |
858693c6 | 2450 | case 'Z': |
77f6ce50 JD |
2451 | { |
2452 | static const GdbCmdParseEntry insert_bp_cmd_desc = { | |
2453 | .handler = handle_insert_bp, | |
2454 | .cmd = "Z", | |
2455 | .cmd_startswith = 1, | |
2456 | .schema = "l?L?L0" | |
2457 | }; | |
2458 | cmd_parser = &insert_bp_cmd_desc; | |
2459 | } | |
2460 | break; | |
858693c6 | 2461 | case 'z': |
77f6ce50 JD |
2462 | { |
2463 | static const GdbCmdParseEntry remove_bp_cmd_desc = { | |
2464 | .handler = handle_remove_bp, | |
2465 | .cmd = "z", | |
2466 | .cmd_startswith = 1, | |
2467 | .schema = "l?L?L0" | |
2468 | }; | |
2469 | cmd_parser = &remove_bp_cmd_desc; | |
2470 | } | |
858693c6 | 2471 | break; |
880a7578 | 2472 | case 'H': |
3a9651d6 JD |
2473 | { |
2474 | static const GdbCmdParseEntry set_thread_cmd_desc = { | |
2475 | .handler = handle_set_thread, | |
2476 | .cmd = "H", | |
2477 | .cmd_startswith = 1, | |
2478 | .schema = "o.t0" | |
2479 | }; | |
2480 | cmd_parser = &set_thread_cmd_desc; | |
880a7578 AL |
2481 | } |
2482 | break; | |
2483 | case 'T': | |
44ffded0 JD |
2484 | { |
2485 | static const GdbCmdParseEntry thread_alive_cmd_desc = { | |
2486 | .handler = handle_thread_alive, | |
2487 | .cmd = "T", | |
2488 | .cmd_startswith = 1, | |
2489 | .schema = "t0" | |
2490 | }; | |
2491 | cmd_parser = &thread_alive_cmd_desc; | |
1e9fa730 | 2492 | } |
880a7578 | 2493 | break; |
978efd6a | 2494 | case 'q': |
2704efad JD |
2495 | { |
2496 | static const GdbCmdParseEntry gen_query_cmd_desc = { | |
2497 | .handler = handle_gen_query, | |
2498 | .cmd = "q", | |
2499 | .cmd_startswith = 1, | |
2500 | .schema = "s0" | |
2501 | }; | |
2502 | cmd_parser = &gen_query_cmd_desc; | |
56aebc89 | 2503 | } |
2704efad JD |
2504 | break; |
2505 | case 'Q': | |
2506 | { | |
2507 | static const GdbCmdParseEntry gen_set_cmd_desc = { | |
2508 | .handler = handle_gen_set, | |
2509 | .cmd = "Q", | |
2510 | .cmd_startswith = 1, | |
2511 | .schema = "s0" | |
2512 | }; | |
2513 | cmd_parser = &gen_set_cmd_desc; | |
a3919386 | 2514 | } |
2704efad | 2515 | break; |
858693c6 | 2516 | default: |
858693c6 FB |
2517 | /* put empty packet */ |
2518 | buf[0] = '\0'; | |
2519 | put_packet(s, buf); | |
2520 | break; | |
2521 | } | |
3e2c1261 JD |
2522 | |
2523 | run_cmd_parser(s, line_buf, cmd_parser); | |
2524 | ||
858693c6 FB |
2525 | return RS_IDLE; |
2526 | } | |
2527 | ||
64f6b346 | 2528 | void gdb_set_stop_cpu(CPUState *cpu) |
880a7578 | 2529 | { |
160d858d LM |
2530 | GDBProcess *p = gdb_get_cpu_process(gdbserver_state, cpu); |
2531 | ||
2532 | if (!p->attached) { | |
2533 | /* | |
2534 | * Having a stop CPU corresponding to a process that is not attached | |
2535 | * confuses GDB. So we ignore the request. | |
2536 | */ | |
2537 | return; | |
2538 | } | |
2539 | ||
2e0f2cfb AF |
2540 | gdbserver_state->c_cpu = cpu; |
2541 | gdbserver_state->g_cpu = cpu; | |
880a7578 AL |
2542 | } |
2543 | ||
1fddef4b | 2544 | #ifndef CONFIG_USER_ONLY |
1dfb4dd9 | 2545 | static void gdb_vm_state_change(void *opaque, int running, RunState state) |
858693c6 | 2546 | { |
880a7578 | 2547 | GDBState *s = gdbserver_state; |
2e0f2cfb | 2548 | CPUState *cpu = s->c_cpu; |
858693c6 | 2549 | char buf[256]; |
95567c27 | 2550 | char thread_id[16]; |
d6fc1b39 | 2551 | const char *type; |
858693c6 FB |
2552 | int ret; |
2553 | ||
cdb432b2 MI |
2554 | if (running || s->state == RS_INACTIVE) { |
2555 | return; | |
2556 | } | |
2557 | /* Is there a GDB syscall waiting to be sent? */ | |
2558 | if (s->current_syscall_cb) { | |
2559 | put_packet(s, s->syscall_buf); | |
a2d1ebaf | 2560 | return; |
e07bbac5 | 2561 | } |
95567c27 LM |
2562 | |
2563 | if (cpu == NULL) { | |
2564 | /* No process attached */ | |
2565 | return; | |
2566 | } | |
2567 | ||
2568 | gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)); | |
2569 | ||
1dfb4dd9 | 2570 | switch (state) { |
0461d5a6 | 2571 | case RUN_STATE_DEBUG: |
ff4700b0 AF |
2572 | if (cpu->watchpoint_hit) { |
2573 | switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) { | |
a1d1bb31 | 2574 | case BP_MEM_READ: |
d6fc1b39 AL |
2575 | type = "r"; |
2576 | break; | |
a1d1bb31 | 2577 | case BP_MEM_ACCESS: |
d6fc1b39 AL |
2578 | type = "a"; |
2579 | break; | |
2580 | default: | |
2581 | type = ""; | |
2582 | break; | |
2583 | } | |
5c9522b3 DG |
2584 | trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu), |
2585 | (target_ulong)cpu->watchpoint_hit->vaddr); | |
880a7578 | 2586 | snprintf(buf, sizeof(buf), |
95567c27 LM |
2587 | "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";", |
2588 | GDB_SIGNAL_TRAP, thread_id, type, | |
ff4700b0 AF |
2589 | (target_ulong)cpu->watchpoint_hit->vaddr); |
2590 | cpu->watchpoint_hit = NULL; | |
425189a8 | 2591 | goto send_packet; |
5c9522b3 DG |
2592 | } else { |
2593 | trace_gdbstub_hit_break(); | |
6658ffb8 | 2594 | } |
bbd77c18 | 2595 | tb_flush(cpu); |
ca587a8e | 2596 | ret = GDB_SIGNAL_TRAP; |
425189a8 | 2597 | break; |
0461d5a6 | 2598 | case RUN_STATE_PAUSED: |
5c9522b3 | 2599 | trace_gdbstub_hit_paused(); |
9781e040 | 2600 | ret = GDB_SIGNAL_INT; |
425189a8 | 2601 | break; |
0461d5a6 | 2602 | case RUN_STATE_SHUTDOWN: |
5c9522b3 | 2603 | trace_gdbstub_hit_shutdown(); |
425189a8 JK |
2604 | ret = GDB_SIGNAL_QUIT; |
2605 | break; | |
0461d5a6 | 2606 | case RUN_STATE_IO_ERROR: |
5c9522b3 | 2607 | trace_gdbstub_hit_io_error(); |
425189a8 JK |
2608 | ret = GDB_SIGNAL_IO; |
2609 | break; | |
0461d5a6 | 2610 | case RUN_STATE_WATCHDOG: |
5c9522b3 | 2611 | trace_gdbstub_hit_watchdog(); |
425189a8 JK |
2612 | ret = GDB_SIGNAL_ALRM; |
2613 | break; | |
0461d5a6 | 2614 | case RUN_STATE_INTERNAL_ERROR: |
5c9522b3 | 2615 | trace_gdbstub_hit_internal_error(); |
425189a8 JK |
2616 | ret = GDB_SIGNAL_ABRT; |
2617 | break; | |
0461d5a6 LC |
2618 | case RUN_STATE_SAVE_VM: |
2619 | case RUN_STATE_RESTORE_VM: | |
425189a8 | 2620 | return; |
0461d5a6 | 2621 | case RUN_STATE_FINISH_MIGRATE: |
425189a8 JK |
2622 | ret = GDB_SIGNAL_XCPU; |
2623 | break; | |
2624 | default: | |
5c9522b3 | 2625 | trace_gdbstub_hit_unknown(state); |
425189a8 JK |
2626 | ret = GDB_SIGNAL_UNKNOWN; |
2627 | break; | |
bbeb7b5c | 2628 | } |
226d007d | 2629 | gdb_set_stop_cpu(cpu); |
95567c27 | 2630 | snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id); |
425189a8 JK |
2631 | |
2632 | send_packet: | |
858693c6 | 2633 | put_packet(s, buf); |
425189a8 JK |
2634 | |
2635 | /* disable single step if it was enabled */ | |
3825b28f | 2636 | cpu_single_step(cpu, 0); |
858693c6 | 2637 | } |
1fddef4b | 2638 | #endif |
858693c6 | 2639 | |
a2d1ebaf PB |
2640 | /* Send a gdb syscall request. |
2641 | This accepts limited printf-style format specifiers, specifically: | |
a87295e8 PB |
2642 | %x - target_ulong argument printed in hex. |
2643 | %lx - 64-bit argument printed in hex. | |
2644 | %s - string pointer (target_ulong) and length (int) pair. */ | |
19239b39 | 2645 | void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va) |
a2d1ebaf | 2646 | { |
a2d1ebaf | 2647 | char *p; |
cdb432b2 | 2648 | char *p_end; |
a2d1ebaf | 2649 | target_ulong addr; |
a87295e8 | 2650 | uint64_t i64; |
a2d1ebaf PB |
2651 | GDBState *s; |
2652 | ||
880a7578 | 2653 | s = gdbserver_state; |
a2d1ebaf PB |
2654 | if (!s) |
2655 | return; | |
cdb432b2 | 2656 | s->current_syscall_cb = cb; |
a2d1ebaf | 2657 | #ifndef CONFIG_USER_ONLY |
0461d5a6 | 2658 | vm_stop(RUN_STATE_DEBUG); |
a2d1ebaf | 2659 | #endif |
cdb432b2 MI |
2660 | p = s->syscall_buf; |
2661 | p_end = &s->syscall_buf[sizeof(s->syscall_buf)]; | |
a2d1ebaf PB |
2662 | *(p++) = 'F'; |
2663 | while (*fmt) { | |
2664 | if (*fmt == '%') { | |
2665 | fmt++; | |
2666 | switch (*fmt++) { | |
2667 | case 'x': | |
2668 | addr = va_arg(va, target_ulong); | |
cdb432b2 | 2669 | p += snprintf(p, p_end - p, TARGET_FMT_lx, addr); |
a2d1ebaf | 2670 | break; |
a87295e8 PB |
2671 | case 'l': |
2672 | if (*(fmt++) != 'x') | |
2673 | goto bad_format; | |
2674 | i64 = va_arg(va, uint64_t); | |
cdb432b2 | 2675 | p += snprintf(p, p_end - p, "%" PRIx64, i64); |
a87295e8 | 2676 | break; |
a2d1ebaf PB |
2677 | case 's': |
2678 | addr = va_arg(va, target_ulong); | |
cdb432b2 | 2679 | p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x", |
363a37d5 | 2680 | addr, va_arg(va, int)); |
a2d1ebaf PB |
2681 | break; |
2682 | default: | |
a87295e8 | 2683 | bad_format: |
7ae6c571 ZY |
2684 | error_report("gdbstub: Bad syscall format string '%s'", |
2685 | fmt - 1); | |
a2d1ebaf PB |
2686 | break; |
2687 | } | |
2688 | } else { | |
2689 | *(p++) = *(fmt++); | |
2690 | } | |
2691 | } | |
8a93e02a | 2692 | *p = 0; |
a2d1ebaf | 2693 | #ifdef CONFIG_USER_ONLY |
cdb432b2 | 2694 | put_packet(s, s->syscall_buf); |
4f710866 PM |
2695 | /* Return control to gdb for it to process the syscall request. |
2696 | * Since the protocol requires that gdb hands control back to us | |
2697 | * using a "here are the results" F packet, we don't need to check | |
2698 | * gdb_handlesig's return value (which is the signal to deliver if | |
2699 | * execution was resumed via a continue packet). | |
2700 | */ | |
2e0f2cfb | 2701 | gdb_handlesig(s->c_cpu, 0); |
a2d1ebaf | 2702 | #else |
cdb432b2 MI |
2703 | /* In this case wait to send the syscall packet until notification that |
2704 | the CPU has stopped. This must be done because if the packet is sent | |
2705 | now the reply from the syscall request could be received while the CPU | |
2706 | is still in the running state, which can cause packets to be dropped | |
2707 | and state transition 'T' packets to be sent while the syscall is still | |
2708 | being processed. */ | |
9102deda | 2709 | qemu_cpu_kick(s->c_cpu); |
a2d1ebaf PB |
2710 | #endif |
2711 | } | |
2712 | ||
19239b39 PM |
2713 | void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...) |
2714 | { | |
2715 | va_list va; | |
2716 | ||
2717 | va_start(va, fmt); | |
2718 | gdb_do_syscallv(cb, fmt, va); | |
2719 | va_end(va); | |
2720 | } | |
2721 | ||
33c846ef | 2722 | static void gdb_read_byte(GDBState *s, uint8_t ch) |
858693c6 | 2723 | { |
60fe76f3 | 2724 | uint8_t reply; |
858693c6 | 2725 | |
1fddef4b | 2726 | #ifndef CONFIG_USER_ONLY |
4046d913 PB |
2727 | if (s->last_packet_len) { |
2728 | /* Waiting for a response to the last packet. If we see the start | |
2729 | of a new command then abandon the previous response. */ | |
2730 | if (ch == '-') { | |
5c9522b3 | 2731 | trace_gdbstub_err_got_nack(); |
ffe8ab83 | 2732 | put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
118e2268 | 2733 | } else if (ch == '+') { |
5c9522b3 | 2734 | trace_gdbstub_io_got_ack(); |
118e2268 | 2735 | } else { |
33c846ef | 2736 | trace_gdbstub_io_got_unexpected(ch); |
4046d913 | 2737 | } |
118e2268 | 2738 | |
4046d913 PB |
2739 | if (ch == '+' || ch == '$') |
2740 | s->last_packet_len = 0; | |
2741 | if (ch != '$') | |
2742 | return; | |
2743 | } | |
1354869c | 2744 | if (runstate_is_running()) { |
858693c6 FB |
2745 | /* when the CPU is running, we cannot do anything except stop |
2746 | it when receiving a char */ | |
0461d5a6 | 2747 | vm_stop(RUN_STATE_PAUSED); |
5fafdf24 | 2748 | } else |
1fddef4b | 2749 | #endif |
41625033 | 2750 | { |
858693c6 FB |
2751 | switch(s->state) { |
2752 | case RS_IDLE: | |
2753 | if (ch == '$') { | |
4bf43122 | 2754 | /* start of command packet */ |
858693c6 | 2755 | s->line_buf_index = 0; |
4bf43122 | 2756 | s->line_sum = 0; |
858693c6 | 2757 | s->state = RS_GETLINE; |
4bf43122 | 2758 | } else { |
33c846ef | 2759 | trace_gdbstub_err_garbage(ch); |
c33a346e | 2760 | } |
b4608c04 | 2761 | break; |
858693c6 | 2762 | case RS_GETLINE: |
4bf43122 DG |
2763 | if (ch == '}') { |
2764 | /* start escape sequence */ | |
2765 | s->state = RS_GETLINE_ESC; | |
2766 | s->line_sum += ch; | |
2767 | } else if (ch == '*') { | |
2768 | /* start run length encoding sequence */ | |
2769 | s->state = RS_GETLINE_RLE; | |
2770 | s->line_sum += ch; | |
2771 | } else if (ch == '#') { | |
2772 | /* end of command, start of checksum*/ | |
2773 | s->state = RS_CHKSUM1; | |
2774 | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { | |
5c9522b3 | 2775 | trace_gdbstub_err_overrun(); |
4bf43122 DG |
2776 | s->state = RS_IDLE; |
2777 | } else { | |
2778 | /* unescaped command character */ | |
2779 | s->line_buf[s->line_buf_index++] = ch; | |
2780 | s->line_sum += ch; | |
2781 | } | |
2782 | break; | |
2783 | case RS_GETLINE_ESC: | |
858693c6 | 2784 | if (ch == '#') { |
4bf43122 DG |
2785 | /* unexpected end of command in escape sequence */ |
2786 | s->state = RS_CHKSUM1; | |
858693c6 | 2787 | } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
4bf43122 | 2788 | /* command buffer overrun */ |
5c9522b3 | 2789 | trace_gdbstub_err_overrun(); |
858693c6 | 2790 | s->state = RS_IDLE; |
4c3a88a2 | 2791 | } else { |
4bf43122 DG |
2792 | /* parse escaped character and leave escape state */ |
2793 | s->line_buf[s->line_buf_index++] = ch ^ 0x20; | |
2794 | s->line_sum += ch; | |
2795 | s->state = RS_GETLINE; | |
2796 | } | |
2797 | break; | |
2798 | case RS_GETLINE_RLE: | |
046aba16 MA |
2799 | /* |
2800 | * Run-length encoding is explained in "Debugging with GDB / | |
2801 | * Appendix E GDB Remote Serial Protocol / Overview". | |
2802 | */ | |
2803 | if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) { | |
4bf43122 | 2804 | /* invalid RLE count encoding */ |
33c846ef | 2805 | trace_gdbstub_err_invalid_repeat(ch); |
4bf43122 DG |
2806 | s->state = RS_GETLINE; |
2807 | } else { | |
2808 | /* decode repeat length */ | |
33c846ef | 2809 | int repeat = ch - ' ' + 3; |
4bf43122 DG |
2810 | if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) { |
2811 | /* that many repeats would overrun the command buffer */ | |
5c9522b3 | 2812 | trace_gdbstub_err_overrun(); |
4bf43122 DG |
2813 | s->state = RS_IDLE; |
2814 | } else if (s->line_buf_index < 1) { | |
2815 | /* got a repeat but we have nothing to repeat */ | |
5c9522b3 | 2816 | trace_gdbstub_err_invalid_rle(); |
4bf43122 DG |
2817 | s->state = RS_GETLINE; |
2818 | } else { | |
2819 | /* repeat the last character */ | |
2820 | memset(s->line_buf + s->line_buf_index, | |
2821 | s->line_buf[s->line_buf_index - 1], repeat); | |
2822 | s->line_buf_index += repeat; | |
2823 | s->line_sum += ch; | |
2824 | s->state = RS_GETLINE; | |
2825 | } | |
4c3a88a2 FB |
2826 | } |
2827 | break; | |
858693c6 | 2828 | case RS_CHKSUM1: |
4bf43122 DG |
2829 | /* get high hex digit of checksum */ |
2830 | if (!isxdigit(ch)) { | |
33c846ef | 2831 | trace_gdbstub_err_checksum_invalid(ch); |
4bf43122 DG |
2832 | s->state = RS_GETLINE; |
2833 | break; | |
2834 | } | |
858693c6 FB |
2835 | s->line_buf[s->line_buf_index] = '\0'; |
2836 | s->line_csum = fromhex(ch) << 4; | |
2837 | s->state = RS_CHKSUM2; | |
2838 | break; | |
2839 | case RS_CHKSUM2: | |
4bf43122 DG |
2840 | /* get low hex digit of checksum */ |
2841 | if (!isxdigit(ch)) { | |
33c846ef | 2842 | trace_gdbstub_err_checksum_invalid(ch); |
4bf43122 DG |
2843 | s->state = RS_GETLINE; |
2844 | break; | |
858693c6 | 2845 | } |
4bf43122 DG |
2846 | s->line_csum |= fromhex(ch); |
2847 | ||
2848 | if (s->line_csum != (s->line_sum & 0xff)) { | |
5c9522b3 | 2849 | trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum); |
4bf43122 | 2850 | /* send NAK reply */ |
60fe76f3 TS |
2851 | reply = '-'; |
2852 | put_buffer(s, &reply, 1); | |
858693c6 | 2853 | s->state = RS_IDLE; |
4c3a88a2 | 2854 | } else { |
4bf43122 | 2855 | /* send ACK reply */ |
60fe76f3 TS |
2856 | reply = '+'; |
2857 | put_buffer(s, &reply, 1); | |
880a7578 | 2858 | s->state = gdb_handle_packet(s, s->line_buf); |
4c3a88a2 FB |
2859 | } |
2860 | break; | |
a2d1ebaf PB |
2861 | default: |
2862 | abort(); | |
858693c6 FB |
2863 | } |
2864 | } | |
2865 | } | |
2866 | ||
0e1c9c54 | 2867 | /* Tell the remote gdb that the process has exited. */ |
9349b4f9 | 2868 | void gdb_exit(CPUArchState *env, int code) |
0e1c9c54 PB |
2869 | { |
2870 | GDBState *s; | |
2871 | char buf[4]; | |
2872 | ||
2873 | s = gdbserver_state; | |
2874 | if (!s) { | |
2875 | return; | |
2876 | } | |
2877 | #ifdef CONFIG_USER_ONLY | |
2878 | if (gdbserver_fd < 0 || s->fd < 0) { | |
2879 | return; | |
2880 | } | |
2881 | #endif | |
2882 | ||
5c9522b3 DG |
2883 | trace_gdbstub_op_exiting((uint8_t)code); |
2884 | ||
0e1c9c54 PB |
2885 | snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code); |
2886 | put_packet(s, buf); | |
e2af15b2 FC |
2887 | |
2888 | #ifndef CONFIG_USER_ONLY | |
1ce2610c | 2889 | qemu_chr_fe_deinit(&s->chr, true); |
e2af15b2 | 2890 | #endif |
0e1c9c54 PB |
2891 | } |
2892 | ||
8f468636 LM |
2893 | /* |
2894 | * Create the process that will contain all the "orphan" CPUs (that are not | |
2895 | * part of a CPU cluster). Note that if this process contains no CPUs, it won't | |
2896 | * be attachable and thus will be invisible to the user. | |
2897 | */ | |
2898 | static void create_default_process(GDBState *s) | |
2899 | { | |
2900 | GDBProcess *process; | |
2901 | int max_pid = 0; | |
2902 | ||
2903 | if (s->process_num) { | |
2904 | max_pid = s->processes[s->process_num - 1].pid; | |
2905 | } | |
2906 | ||
2907 | s->processes = g_renew(GDBProcess, s->processes, ++s->process_num); | |
2908 | process = &s->processes[s->process_num - 1]; | |
2909 | ||
2910 | /* We need an available PID slot for this process */ | |
2911 | assert(max_pid < UINT32_MAX); | |
2912 | ||
2913 | process->pid = max_pid + 1; | |
2914 | process->attached = false; | |
c145eeae | 2915 | process->target_xml[0] = '\0'; |
8f468636 LM |
2916 | } |
2917 | ||
1fddef4b FB |
2918 | #ifdef CONFIG_USER_ONLY |
2919 | int | |
db6b81d4 | 2920 | gdb_handlesig(CPUState *cpu, int sig) |
1fddef4b | 2921 | { |
5ca666c7 AF |
2922 | GDBState *s; |
2923 | char buf[256]; | |
2924 | int n; | |
1fddef4b | 2925 | |
5ca666c7 AF |
2926 | s = gdbserver_state; |
2927 | if (gdbserver_fd < 0 || s->fd < 0) { | |
2928 | return sig; | |
2929 | } | |
1fddef4b | 2930 | |
5ca666c7 | 2931 | /* disable single step if it was enabled */ |
3825b28f | 2932 | cpu_single_step(cpu, 0); |
bbd77c18 | 2933 | tb_flush(cpu); |
1fddef4b | 2934 | |
5ca666c7 AF |
2935 | if (sig != 0) { |
2936 | snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig)); | |
2937 | put_packet(s, buf); | |
2938 | } | |
2939 | /* put_packet() might have detected that the peer terminated the | |
2940 | connection. */ | |
2941 | if (s->fd < 0) { | |
2942 | return sig; | |
2943 | } | |
1fddef4b | 2944 | |
5ca666c7 AF |
2945 | sig = 0; |
2946 | s->state = RS_IDLE; | |
2947 | s->running_state = 0; | |
2948 | while (s->running_state == 0) { | |
2949 | n = read(s->fd, buf, 256); | |
2950 | if (n > 0) { | |
2951 | int i; | |
2952 | ||
2953 | for (i = 0; i < n; i++) { | |
2954 | gdb_read_byte(s, buf[i]); | |
2955 | } | |
5819e3e0 | 2956 | } else { |
5ca666c7 AF |
2957 | /* XXX: Connection closed. Should probably wait for another |
2958 | connection before continuing. */ | |
5819e3e0 PW |
2959 | if (n == 0) { |
2960 | close(s->fd); | |
2961 | } | |
2962 | s->fd = -1; | |
5ca666c7 | 2963 | return sig; |
1fddef4b | 2964 | } |
5ca666c7 AF |
2965 | } |
2966 | sig = s->signal; | |
2967 | s->signal = 0; | |
2968 | return sig; | |
1fddef4b | 2969 | } |
e9009676 | 2970 | |
ca587a8e | 2971 | /* Tell the remote gdb that the process has exited due to SIG. */ |
9349b4f9 | 2972 | void gdb_signalled(CPUArchState *env, int sig) |
ca587a8e | 2973 | { |
5ca666c7 AF |
2974 | GDBState *s; |
2975 | char buf[4]; | |
ca587a8e | 2976 | |
5ca666c7 AF |
2977 | s = gdbserver_state; |
2978 | if (gdbserver_fd < 0 || s->fd < 0) { | |
2979 | return; | |
2980 | } | |
ca587a8e | 2981 | |
5ca666c7 AF |
2982 | snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig)); |
2983 | put_packet(s, buf); | |
ca587a8e | 2984 | } |
1fddef4b | 2985 | |
2f652224 | 2986 | static bool gdb_accept(void) |
858693c6 FB |
2987 | { |
2988 | GDBState *s; | |
2989 | struct sockaddr_in sockaddr; | |
2990 | socklen_t len; | |
bf1c852a | 2991 | int fd; |
858693c6 FB |
2992 | |
2993 | for(;;) { | |
2994 | len = sizeof(sockaddr); | |
2995 | fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len); | |
2996 | if (fd < 0 && errno != EINTR) { | |
2997 | perror("accept"); | |
2f652224 | 2998 | return false; |
858693c6 | 2999 | } else if (fd >= 0) { |
f5bdd781 | 3000 | qemu_set_cloexec(fd); |
b4608c04 FB |
3001 | break; |
3002 | } | |
3003 | } | |
858693c6 FB |
3004 | |
3005 | /* set short latency */ | |
2f652224 PM |
3006 | if (socket_set_nodelay(fd)) { |
3007 | perror("setsockopt"); | |
ead75d84 | 3008 | close(fd); |
2f652224 PM |
3009 | return false; |
3010 | } | |
3b46e624 | 3011 | |
7267c094 | 3012 | s = g_malloc0(sizeof(GDBState)); |
8f468636 | 3013 | create_default_process(s); |
970ed906 LM |
3014 | s->processes[0].attached = true; |
3015 | s->c_cpu = gdb_first_attached_cpu(s); | |
3016 | s->g_cpu = s->c_cpu; | |
858693c6 | 3017 | s->fd = fd; |
5b50e790 | 3018 | gdb_has_xml = false; |
858693c6 | 3019 | |
880a7578 | 3020 | gdbserver_state = s; |
2f652224 | 3021 | return true; |
858693c6 FB |
3022 | } |
3023 | ||
3024 | static int gdbserver_open(int port) | |
3025 | { | |
3026 | struct sockaddr_in sockaddr; | |
6669ca13 | 3027 | int fd, ret; |
858693c6 FB |
3028 | |
3029 | fd = socket(PF_INET, SOCK_STREAM, 0); | |
3030 | if (fd < 0) { | |
3031 | perror("socket"); | |
3032 | return -1; | |
3033 | } | |
f5bdd781 | 3034 | qemu_set_cloexec(fd); |
858693c6 | 3035 | |
6669ca13 | 3036 | socket_set_fast_reuse(fd); |
858693c6 FB |
3037 | |
3038 | sockaddr.sin_family = AF_INET; | |
3039 | sockaddr.sin_port = htons(port); | |
3040 | sockaddr.sin_addr.s_addr = 0; | |
3041 | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); | |
3042 | if (ret < 0) { | |
3043 | perror("bind"); | |
bb16172c | 3044 | close(fd); |
858693c6 FB |
3045 | return -1; |
3046 | } | |
96165b9e | 3047 | ret = listen(fd, 1); |
858693c6 FB |
3048 | if (ret < 0) { |
3049 | perror("listen"); | |
bb16172c | 3050 | close(fd); |
858693c6 FB |
3051 | return -1; |
3052 | } | |
858693c6 FB |
3053 | return fd; |
3054 | } | |
3055 | ||
3056 | int gdbserver_start(int port) | |
3057 | { | |
3058 | gdbserver_fd = gdbserver_open(port); | |
3059 | if (gdbserver_fd < 0) | |
3060 | return -1; | |
3061 | /* accept connections */ | |
2f652224 PM |
3062 | if (!gdb_accept()) { |
3063 | close(gdbserver_fd); | |
3064 | gdbserver_fd = -1; | |
3065 | return -1; | |
3066 | } | |
4046d913 PB |
3067 | return 0; |
3068 | } | |
2b1319c8 AJ |
3069 | |
3070 | /* Disable gdb stub for child processes. */ | |
f7ec7f7b | 3071 | void gdbserver_fork(CPUState *cpu) |
2b1319c8 AJ |
3072 | { |
3073 | GDBState *s = gdbserver_state; | |
75a34036 AF |
3074 | |
3075 | if (gdbserver_fd < 0 || s->fd < 0) { | |
3076 | return; | |
3077 | } | |
2b1319c8 AJ |
3078 | close(s->fd); |
3079 | s->fd = -1; | |
b3310ab3 | 3080 | cpu_breakpoint_remove_all(cpu, BP_GDB); |
75a34036 | 3081 | cpu_watchpoint_remove_all(cpu, BP_GDB); |
2b1319c8 | 3082 | } |
1fddef4b | 3083 | #else |
aa1f17c1 | 3084 | static int gdb_chr_can_receive(void *opaque) |
4046d913 | 3085 | { |
56aebc89 PB |
3086 | /* We can handle an arbitrarily large amount of data. |
3087 | Pick the maximum packet size, which is as good as anything. */ | |
3088 | return MAX_PACKET_LENGTH; | |
4046d913 PB |
3089 | } |
3090 | ||
aa1f17c1 | 3091 | static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size) |
4046d913 | 3092 | { |
4046d913 PB |
3093 | int i; |
3094 | ||
3095 | for (i = 0; i < size; i++) { | |
880a7578 | 3096 | gdb_read_byte(gdbserver_state, buf[i]); |
4046d913 PB |
3097 | } |
3098 | } | |
3099 | ||
3100 | static void gdb_chr_event(void *opaque, int event) | |
3101 | { | |
970ed906 LM |
3102 | int i; |
3103 | GDBState *s = (GDBState *) opaque; | |
3104 | ||
4046d913 | 3105 | switch (event) { |
b6b8df56 | 3106 | case CHR_EVENT_OPENED: |
970ed906 LM |
3107 | /* Start with first process attached, others detached */ |
3108 | for (i = 0; i < s->process_num; i++) { | |
3109 | s->processes[i].attached = !i; | |
3110 | } | |
3111 | ||
3112 | s->c_cpu = gdb_first_attached_cpu(s); | |
3113 | s->g_cpu = s->c_cpu; | |
3114 | ||
0461d5a6 | 3115 | vm_stop(RUN_STATE_PAUSED); |
5b50e790 | 3116 | gdb_has_xml = false; |
4046d913 PB |
3117 | break; |
3118 | default: | |
3119 | break; | |
3120 | } | |
3121 | } | |
3122 | ||
8a34a0fb AL |
3123 | static void gdb_monitor_output(GDBState *s, const char *msg, int len) |
3124 | { | |
3125 | char buf[MAX_PACKET_LENGTH]; | |
3126 | ||
3127 | buf[0] = 'O'; | |
3128 | if (len > (MAX_PACKET_LENGTH/2) - 1) | |
3129 | len = (MAX_PACKET_LENGTH/2) - 1; | |
3130 | memtohex(buf + 1, (uint8_t *)msg, len); | |
3131 | put_packet(s, buf); | |
3132 | } | |
3133 | ||
0ec7b3e7 | 3134 | static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len) |
8a34a0fb AL |
3135 | { |
3136 | const char *p = (const char *)buf; | |
3137 | int max_sz; | |
3138 | ||
3139 | max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2; | |
3140 | for (;;) { | |
3141 | if (len <= max_sz) { | |
3142 | gdb_monitor_output(gdbserver_state, p, len); | |
3143 | break; | |
3144 | } | |
3145 | gdb_monitor_output(gdbserver_state, p, max_sz); | |
3146 | p += max_sz; | |
3147 | len -= max_sz; | |
3148 | } | |
3149 | return len; | |
3150 | } | |
3151 | ||
59030a8c AL |
3152 | #ifndef _WIN32 |
3153 | static void gdb_sigterm_handler(int signal) | |
3154 | { | |
1354869c | 3155 | if (runstate_is_running()) { |
0461d5a6 | 3156 | vm_stop(RUN_STATE_PAUSED); |
e07bbac5 | 3157 | } |
59030a8c AL |
3158 | } |
3159 | #endif | |
3160 | ||
777357d7 MAL |
3161 | static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend, |
3162 | bool *be_opened, Error **errp) | |
3163 | { | |
3164 | *be_opened = false; | |
3165 | } | |
3166 | ||
3167 | static void char_gdb_class_init(ObjectClass *oc, void *data) | |
3168 | { | |
3169 | ChardevClass *cc = CHARDEV_CLASS(oc); | |
3170 | ||
3171 | cc->internal = true; | |
3172 | cc->open = gdb_monitor_open; | |
3173 | cc->chr_write = gdb_monitor_write; | |
3174 | } | |
3175 | ||
3176 | #define TYPE_CHARDEV_GDB "chardev-gdb" | |
3177 | ||
3178 | static const TypeInfo char_gdb_type_info = { | |
3179 | .name = TYPE_CHARDEV_GDB, | |
3180 | .parent = TYPE_CHARDEV, | |
3181 | .class_init = char_gdb_class_init, | |
3182 | }; | |
3183 | ||
8f468636 LM |
3184 | static int find_cpu_clusters(Object *child, void *opaque) |
3185 | { | |
3186 | if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) { | |
3187 | GDBState *s = (GDBState *) opaque; | |
3188 | CPUClusterState *cluster = CPU_CLUSTER(child); | |
3189 | GDBProcess *process; | |
3190 | ||
3191 | s->processes = g_renew(GDBProcess, s->processes, ++s->process_num); | |
3192 | ||
3193 | process = &s->processes[s->process_num - 1]; | |
3194 | ||
3195 | /* | |
3196 | * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at | |
3197 | * runtime, we enforce here that the machine does not use a cluster ID | |
3198 | * that would lead to PID 0. | |
3199 | */ | |
3200 | assert(cluster->cluster_id != UINT32_MAX); | |
3201 | process->pid = cluster->cluster_id + 1; | |
3202 | process->attached = false; | |
c145eeae | 3203 | process->target_xml[0] = '\0'; |
8f468636 LM |
3204 | |
3205 | return 0; | |
3206 | } | |
3207 | ||
3208 | return object_child_foreach(child, find_cpu_clusters, opaque); | |
3209 | } | |
3210 | ||
3211 | static int pid_order(const void *a, const void *b) | |
3212 | { | |
3213 | GDBProcess *pa = (GDBProcess *) a; | |
3214 | GDBProcess *pb = (GDBProcess *) b; | |
3215 | ||
3216 | if (pa->pid < pb->pid) { | |
3217 | return -1; | |
3218 | } else if (pa->pid > pb->pid) { | |
3219 | return 1; | |
3220 | } else { | |
3221 | return 0; | |
3222 | } | |
3223 | } | |
3224 | ||
3225 | static void create_processes(GDBState *s) | |
3226 | { | |
3227 | object_child_foreach(object_get_root(), find_cpu_clusters, s); | |
3228 | ||
3229 | if (s->processes) { | |
3230 | /* Sort by PID */ | |
3231 | qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order); | |
3232 | } | |
3233 | ||
3234 | create_default_process(s); | |
3235 | } | |
3236 | ||
3237 | static void cleanup_processes(GDBState *s) | |
3238 | { | |
3239 | g_free(s->processes); | |
3240 | s->process_num = 0; | |
3241 | s->processes = NULL; | |
3242 | } | |
3243 | ||
59030a8c | 3244 | int gdbserver_start(const char *device) |
4046d913 | 3245 | { |
5c9522b3 DG |
3246 | trace_gdbstub_op_start(device); |
3247 | ||
4046d913 | 3248 | GDBState *s; |
59030a8c | 3249 | char gdbstub_device_name[128]; |
0ec7b3e7 MAL |
3250 | Chardev *chr = NULL; |
3251 | Chardev *mon_chr; | |
cfc3475a | 3252 | |
508b4ecc ZY |
3253 | if (!first_cpu) { |
3254 | error_report("gdbstub: meaningless to attach gdb to a " | |
3255 | "machine without any CPU."); | |
3256 | return -1; | |
3257 | } | |
3258 | ||
59030a8c AL |
3259 | if (!device) |
3260 | return -1; | |
3261 | if (strcmp(device, "none") != 0) { | |
3262 | if (strstart(device, "tcp:", NULL)) { | |
3263 | /* enforce required TCP attributes */ | |
3264 | snprintf(gdbstub_device_name, sizeof(gdbstub_device_name), | |
3265 | "%s,nowait,nodelay,server", device); | |
3266 | device = gdbstub_device_name; | |
36556b20 | 3267 | } |
59030a8c AL |
3268 | #ifndef _WIN32 |
3269 | else if (strcmp(device, "stdio") == 0) { | |
3270 | struct sigaction act; | |
4046d913 | 3271 | |
59030a8c AL |
3272 | memset(&act, 0, sizeof(act)); |
3273 | act.sa_handler = gdb_sigterm_handler; | |
3274 | sigaction(SIGINT, &act, NULL); | |
3275 | } | |
3276 | #endif | |
95e30b2a MAL |
3277 | /* |
3278 | * FIXME: it's a bit weird to allow using a mux chardev here | |
3279 | * and implicitly setup a monitor. We may want to break this. | |
3280 | */ | |
4ad6f6cb | 3281 | chr = qemu_chr_new_noreplay("gdb", device, true, NULL); |
36556b20 AL |
3282 | if (!chr) |
3283 | return -1; | |
cfc3475a PB |
3284 | } |
3285 | ||
36556b20 AL |
3286 | s = gdbserver_state; |
3287 | if (!s) { | |
7267c094 | 3288 | s = g_malloc0(sizeof(GDBState)); |
36556b20 | 3289 | gdbserver_state = s; |
4046d913 | 3290 | |
36556b20 AL |
3291 | qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL); |
3292 | ||
3293 | /* Initialize a monitor terminal for gdb */ | |
777357d7 | 3294 | mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB, |
4ad6f6cb | 3295 | NULL, NULL, &error_abort); |
36556b20 AL |
3296 | monitor_init(mon_chr, 0); |
3297 | } else { | |
1ce2610c | 3298 | qemu_chr_fe_deinit(&s->chr, true); |
36556b20 | 3299 | mon_chr = s->mon_chr; |
8f468636 | 3300 | cleanup_processes(s); |
36556b20 | 3301 | memset(s, 0, sizeof(GDBState)); |
32a6ebec | 3302 | s->mon_chr = mon_chr; |
36556b20 | 3303 | } |
8f468636 LM |
3304 | |
3305 | create_processes(s); | |
3306 | ||
32a6ebec MAL |
3307 | if (chr) { |
3308 | qemu_chr_fe_init(&s->chr, chr, &error_abort); | |
5345fdb4 | 3309 | qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive, |
970ed906 | 3310 | gdb_chr_event, NULL, s, NULL, true); |
32a6ebec | 3311 | } |
36556b20 AL |
3312 | s->state = chr ? RS_IDLE : RS_INACTIVE; |
3313 | s->mon_chr = mon_chr; | |
cdb432b2 | 3314 | s->current_syscall_cb = NULL; |
8a34a0fb | 3315 | |
b4608c04 FB |
3316 | return 0; |
3317 | } | |
777357d7 | 3318 | |
1bb982b8 KF |
3319 | void gdbserver_cleanup(void) |
3320 | { | |
3321 | if (gdbserver_state) { | |
3322 | put_packet(gdbserver_state, "W00"); | |
3323 | } | |
3324 | } | |
3325 | ||
777357d7 MAL |
3326 | static void register_types(void) |
3327 | { | |
3328 | type_register_static(&char_gdb_type_info); | |
3329 | } | |
3330 | ||
3331 | type_init(register_types); | |
4046d913 | 3332 | #endif |