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