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