]>
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" |
856dfd8a | 27 | #include "qemu/ctype.h" |
f348b6d1 | 28 | #include "qemu/cutils.h" |
0b8fa32f | 29 | #include "qemu/module.h" |
842b42df | 30 | #include "trace.h" |
85b4fa0c | 31 | #include "exec/gdbstub.h" |
f348b6d1 | 32 | #ifdef CONFIG_USER_ONLY |
d96bf49b | 33 | #include "gdbstub/user.h" |
1fddef4b | 34 | #else |
8f468636 | 35 | #include "hw/cpu/cluster.h" |
5cc8767d | 36 | #include "hw/boards.h" |
1fddef4b | 37 | #endif |
67b915a5 | 38 | |
b3946626 | 39 | #include "sysemu/hw_accel.h" |
54d31236 | 40 | #include "sysemu/runstate.h" |
6b5fe137 | 41 | #include "semihosting/semihost.h" |
63c91552 | 42 | #include "exec/exec-all.h" |
5b5968c4 | 43 | #include "exec/replay-core.h" |
548c9609 AB |
44 | #include "exec/tb-flush.h" |
45 | #include "exec/hwaddr.h" | |
ca587a8e | 46 | |
ae7467b1 AB |
47 | #include "internals.h" |
48 | ||
a3919386 JK |
49 | #ifdef CONFIG_USER_ONLY |
50 | #define GDB_ATTACHED "0" | |
51 | #else | |
52 | #define GDB_ATTACHED "1" | |
53 | #endif | |
54 | ||
ab4752ec JD |
55 | #ifndef CONFIG_USER_ONLY |
56 | static int phy_memory_mode; | |
57 | #endif | |
58 | ||
f3659eee AF |
59 | static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr, |
60 | uint8_t *buf, int len, bool is_write) | |
44520db1 | 61 | { |
ab4752ec | 62 | CPUClass *cc; |
f3659eee | 63 | |
ab4752ec JD |
64 | #ifndef CONFIG_USER_ONLY |
65 | if (phy_memory_mode) { | |
66 | if (is_write) { | |
67 | cpu_physical_memory_write(addr, buf, len); | |
68 | } else { | |
69 | cpu_physical_memory_read(addr, buf, len); | |
70 | } | |
71 | return 0; | |
72 | } | |
73 | #endif | |
74 | ||
75 | cc = CPU_GET_CLASS(cpu); | |
f3659eee AF |
76 | if (cc->memory_rw_debug) { |
77 | return cc->memory_rw_debug(cpu, addr, buf, len, is_write); | |
78 | } | |
79 | return cpu_memory_rw_debug(cpu, addr, buf, len, is_write); | |
44520db1 | 80 | } |
ca587a8e | 81 | |
56aebc89 PB |
82 | typedef struct GDBRegisterState { |
83 | int base_reg; | |
84 | int num_regs; | |
a010bdbe AB |
85 | gdb_get_reg_cb get_reg; |
86 | gdb_set_reg_cb set_reg; | |
56aebc89 PB |
87 | const char *xml; |
88 | struct GDBRegisterState *next; | |
89 | } GDBRegisterState; | |
90 | ||
b6fa2ec2 | 91 | GDBState gdbserver_state; |
8d98c445 | 92 | |
36e067b2 | 93 | void gdb_init_gdbserver_state(void) |
8d98c445 AB |
94 | { |
95 | g_assert(!gdbserver_state.init); | |
96 | memset(&gdbserver_state, 0, sizeof(GDBState)); | |
97 | gdbserver_state.init = true; | |
308f9e88 | 98 | gdbserver_state.str_buf = g_string_new(NULL); |
4a25f1b9 | 99 | gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH); |
d116e813 | 100 | gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4); |
ecd39d62 ML |
101 | |
102 | /* | |
3b7a9388 AB |
103 | * What single-step modes are supported is accelerator dependent. |
104 | * By default try to use no IRQs and no timers while single | |
105 | * stepping so as to make single stepping like a typical ICE HW step. | |
ecd39d62 | 106 | */ |
3b7a9388 | 107 | gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags(); |
12bc5b4c ML |
108 | gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER; |
109 | gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags; | |
8d98c445 AB |
110 | } |
111 | ||
5b50e790 | 112 | bool gdb_has_xml; |
56aebc89 | 113 | |
ebf1b4cb PM |
114 | /* |
115 | * Return true if there is a GDB currently connected to the stub | |
116 | * and attached to a CPU | |
117 | */ | |
118 | static bool gdb_attached(void) | |
119 | { | |
120 | return gdbserver_state.init && gdbserver_state.c_cpu; | |
121 | } | |
122 | ||
654efcf3 | 123 | static enum { |
a2d1ebaf PB |
124 | GDB_SYS_UNKNOWN, |
125 | GDB_SYS_ENABLED, | |
126 | GDB_SYS_DISABLED, | |
127 | } gdb_syscall_mode; | |
128 | ||
a38bb079 | 129 | /* Decide if either remote gdb syscalls or native file IO should be used. */ |
a2d1ebaf PB |
130 | int use_gdb_syscalls(void) |
131 | { | |
cfe67cef LA |
132 | SemihostingTarget target = semihosting_get_target(); |
133 | if (target == SEMIHOSTING_TARGET_NATIVE) { | |
a38bb079 LI |
134 | /* -semihosting-config target=native */ |
135 | return false; | |
cfe67cef | 136 | } else if (target == SEMIHOSTING_TARGET_GDB) { |
a38bb079 LI |
137 | /* -semihosting-config target=gdb */ |
138 | return true; | |
139 | } | |
140 | ||
141 | /* -semihosting-config target=auto */ | |
142 | /* On the first call check if gdb is connected and remember. */ | |
a2d1ebaf | 143 | if (gdb_syscall_mode == GDB_SYS_UNKNOWN) { |
ebf1b4cb | 144 | gdb_syscall_mode = gdb_attached() ? GDB_SYS_ENABLED : GDB_SYS_DISABLED; |
a2d1ebaf PB |
145 | } |
146 | return gdb_syscall_mode == GDB_SYS_ENABLED; | |
147 | } | |
148 | ||
ed12f5b4 AB |
149 | static bool stub_can_reverse(void) |
150 | { | |
151 | #ifdef CONFIG_USER_ONLY | |
152 | return false; | |
153 | #else | |
154 | return replay_mode == REPLAY_MODE_PLAY; | |
155 | #endif | |
156 | } | |
157 | ||
9005774b | 158 | /* writes 2*len+1 bytes in buf */ |
36e067b2 | 159 | void gdb_memtohex(GString *buf, const uint8_t *mem, int len) |
b4608c04 FB |
160 | { |
161 | int i, c; | |
b4608c04 FB |
162 | for(i = 0; i < len; i++) { |
163 | c = mem[i]; | |
308f9e88 AB |
164 | g_string_append_c(buf, tohex(c >> 4)); |
165 | g_string_append_c(buf, tohex(c & 0xf)); | |
b4608c04 | 166 | } |
308f9e88 | 167 | g_string_append_c(buf, '\0'); |
b4608c04 FB |
168 | } |
169 | ||
36e067b2 | 170 | void gdb_hextomem(GByteArray *mem, const char *buf, int len) |
b4608c04 FB |
171 | { |
172 | int i; | |
173 | ||
174 | for(i = 0; i < len; i++) { | |
4a25f1b9 AB |
175 | guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]); |
176 | g_byte_array_append(mem, &byte, 1); | |
b4608c04 FB |
177 | buf += 2; |
178 | } | |
179 | } | |
180 | ||
5c9522b3 DG |
181 | static void hexdump(const char *buf, int len, |
182 | void (*trace_fn)(size_t ofs, char const *text)) | |
183 | { | |
184 | char line_buffer[3 * 16 + 4 + 16 + 1]; | |
185 | ||
186 | size_t i; | |
187 | for (i = 0; i < len || (i & 0xF); ++i) { | |
188 | size_t byte_ofs = i & 15; | |
189 | ||
190 | if (byte_ofs == 0) { | |
191 | memset(line_buffer, ' ', 3 * 16 + 4 + 16); | |
192 | line_buffer[3 * 16 + 4 + 16] = 0; | |
193 | } | |
194 | ||
195 | size_t col_group = (i >> 2) & 3; | |
196 | size_t hex_col = byte_ofs * 3 + col_group; | |
197 | size_t txt_col = 3 * 16 + 4 + byte_ofs; | |
198 | ||
199 | if (i < len) { | |
200 | char value = buf[i]; | |
201 | ||
202 | line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF); | |
203 | line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF); | |
204 | line_buffer[txt_col + 0] = (value >= ' ' && value < 127) | |
205 | ? value | |
206 | : '.'; | |
207 | } | |
208 | ||
209 | if (byte_ofs == 0xF) | |
210 | trace_fn(i & -16, line_buffer); | |
211 | } | |
212 | } | |
213 | ||
b4608c04 | 214 | /* return -1 if error, 0 if OK */ |
36e067b2 | 215 | int gdb_put_packet_binary(const char *buf, int len, bool dump) |
b4608c04 | 216 | { |
56aebc89 | 217 | int csum, i; |
d116e813 | 218 | uint8_t footer[3]; |
b4608c04 | 219 | |
5c9522b3 DG |
220 | if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) { |
221 | hexdump(buf, len, trace_gdbstub_io_binaryreply); | |
222 | } | |
223 | ||
b4608c04 | 224 | for(;;) { |
d116e813 DH |
225 | g_byte_array_set_size(gdbserver_state.last_packet, 0); |
226 | g_byte_array_append(gdbserver_state.last_packet, | |
227 | (const uint8_t *) "$", 1); | |
228 | g_byte_array_append(gdbserver_state.last_packet, | |
229 | (const uint8_t *) buf, len); | |
b4608c04 FB |
230 | csum = 0; |
231 | for(i = 0; i < len; i++) { | |
232 | csum += buf[i]; | |
233 | } | |
d116e813 DH |
234 | footer[0] = '#'; |
235 | footer[1] = tohex((csum >> 4) & 0xf); | |
236 | footer[2] = tohex((csum) & 0xf); | |
237 | g_byte_array_append(gdbserver_state.last_packet, footer, 3); | |
b4608c04 | 238 | |
36e067b2 | 239 | gdb_put_buffer(gdbserver_state.last_packet->data, |
d116e813 | 240 | gdbserver_state.last_packet->len); |
b4608c04 | 241 | |
4046d913 | 242 | #ifdef CONFIG_USER_ONLY |
d96bf49b | 243 | i = gdb_get_char(); |
4046d913 | 244 | if (i < 0) |
b4608c04 | 245 | return -1; |
4046d913 | 246 | if (i == '+') |
b4608c04 | 247 | break; |
4046d913 PB |
248 | #else |
249 | break; | |
250 | #endif | |
b4608c04 FB |
251 | } |
252 | return 0; | |
253 | } | |
254 | ||
56aebc89 | 255 | /* return -1 if error, 0 if OK */ |
36e067b2 | 256 | int gdb_put_packet(const char *buf) |
56aebc89 | 257 | { |
5c9522b3 | 258 | trace_gdbstub_io_reply(buf); |
79808573 | 259 | |
36e067b2 | 260 | return gdb_put_packet_binary(buf, strlen(buf), false); |
56aebc89 PB |
261 | } |
262 | ||
36e067b2 | 263 | void gdb_put_strbuf(void) |
308f9e88 | 264 | { |
36e067b2 | 265 | gdb_put_packet(gdbserver_state.str_buf->str); |
308f9e88 AB |
266 | } |
267 | ||
56aebc89 | 268 | /* Encode data using the encoding for 'x' packets. */ |
36e067b2 | 269 | void gdb_memtox(GString *buf, const char *mem, int len) |
56aebc89 | 270 | { |
56aebc89 PB |
271 | char c; |
272 | ||
273 | while (len--) { | |
274 | c = *(mem++); | |
275 | switch (c) { | |
276 | case '#': case '$': case '*': case '}': | |
308f9e88 AB |
277 | g_string_append_c(buf, '}'); |
278 | g_string_append_c(buf, c ^ 0x20); | |
56aebc89 PB |
279 | break; |
280 | default: | |
308f9e88 | 281 | g_string_append_c(buf, c); |
56aebc89 PB |
282 | break; |
283 | } | |
284 | } | |
56aebc89 | 285 | } |
f1ccf904 | 286 | |
a346af3e | 287 | static uint32_t gdb_get_cpu_pid(CPUState *cpu) |
1a227336 | 288 | { |
46f5abc0 PM |
289 | /* TODO: In user mode, we should use the task state PID */ |
290 | if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) { | |
1a227336 | 291 | /* Return the default process' PID */ |
a346af3e AB |
292 | int index = gdbserver_state.process_num - 1; |
293 | return gdbserver_state.processes[index].pid; | |
1a227336 | 294 | } |
46f5abc0 | 295 | return cpu->cluster_index + 1; |
1a227336 LM |
296 | } |
297 | ||
a346af3e | 298 | static GDBProcess *gdb_get_process(uint32_t pid) |
7d8c87da LM |
299 | { |
300 | int i; | |
301 | ||
302 | if (!pid) { | |
303 | /* 0 means any process, we take the first one */ | |
a346af3e | 304 | return &gdbserver_state.processes[0]; |
7d8c87da LM |
305 | } |
306 | ||
a346af3e AB |
307 | for (i = 0; i < gdbserver_state.process_num; i++) { |
308 | if (gdbserver_state.processes[i].pid == pid) { | |
309 | return &gdbserver_state.processes[i]; | |
7d8c87da LM |
310 | } |
311 | } | |
312 | ||
313 | return NULL; | |
314 | } | |
315 | ||
a346af3e | 316 | static GDBProcess *gdb_get_cpu_process(CPUState *cpu) |
7d8c87da | 317 | { |
a346af3e | 318 | return gdb_get_process(gdb_get_cpu_pid(cpu)); |
7d8c87da LM |
319 | } |
320 | ||
321 | static CPUState *find_cpu(uint32_t thread_id) | |
322 | { | |
323 | CPUState *cpu; | |
324 | ||
325 | CPU_FOREACH(cpu) { | |
36e067b2 | 326 | if (gdb_get_cpu_index(cpu) == thread_id) { |
7d8c87da LM |
327 | return cpu; |
328 | } | |
329 | } | |
330 | ||
331 | return NULL; | |
332 | } | |
333 | ||
a346af3e | 334 | static CPUState *get_first_cpu_in_process(GDBProcess *process) |
e40e5204 LM |
335 | { |
336 | CPUState *cpu; | |
337 | ||
338 | CPU_FOREACH(cpu) { | |
a346af3e | 339 | if (gdb_get_cpu_pid(cpu) == process->pid) { |
e40e5204 LM |
340 | return cpu; |
341 | } | |
342 | } | |
343 | ||
344 | return NULL; | |
345 | } | |
346 | ||
a346af3e | 347 | static CPUState *gdb_next_cpu_in_process(CPUState *cpu) |
e40e5204 | 348 | { |
a346af3e | 349 | uint32_t pid = gdb_get_cpu_pid(cpu); |
e40e5204 LM |
350 | cpu = CPU_NEXT(cpu); |
351 | ||
352 | while (cpu) { | |
a346af3e | 353 | if (gdb_get_cpu_pid(cpu) == pid) { |
e40e5204 LM |
354 | break; |
355 | } | |
356 | ||
357 | cpu = CPU_NEXT(cpu); | |
358 | } | |
359 | ||
360 | return cpu; | |
361 | } | |
362 | ||
e40e5204 | 363 | /* Return the cpu following @cpu, while ignoring unattached processes. */ |
a346af3e | 364 | static CPUState *gdb_next_attached_cpu(CPUState *cpu) |
e40e5204 LM |
365 | { |
366 | cpu = CPU_NEXT(cpu); | |
367 | ||
368 | while (cpu) { | |
a346af3e | 369 | if (gdb_get_cpu_process(cpu)->attached) { |
e40e5204 LM |
370 | break; |
371 | } | |
372 | ||
373 | cpu = CPU_NEXT(cpu); | |
374 | } | |
375 | ||
376 | return cpu; | |
377 | } | |
378 | ||
379 | /* Return the first attached cpu */ | |
36e067b2 | 380 | CPUState *gdb_first_attached_cpu(void) |
e40e5204 LM |
381 | { |
382 | CPUState *cpu = first_cpu; | |
a346af3e | 383 | GDBProcess *process = gdb_get_cpu_process(cpu); |
e40e5204 LM |
384 | |
385 | if (!process->attached) { | |
a346af3e | 386 | return gdb_next_attached_cpu(cpu); |
e40e5204 LM |
387 | } |
388 | ||
389 | return cpu; | |
390 | } | |
391 | ||
a346af3e | 392 | static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid) |
ab65eed3 LM |
393 | { |
394 | GDBProcess *process; | |
395 | CPUState *cpu; | |
396 | ||
397 | if (!pid && !tid) { | |
398 | /* 0 means any process/thread, we take the first attached one */ | |
a346af3e | 399 | return gdb_first_attached_cpu(); |
ab65eed3 LM |
400 | } else if (pid && !tid) { |
401 | /* any thread in a specific process */ | |
a346af3e | 402 | process = gdb_get_process(pid); |
ab65eed3 LM |
403 | |
404 | if (process == NULL) { | |
405 | return NULL; | |
406 | } | |
407 | ||
408 | if (!process->attached) { | |
409 | return NULL; | |
410 | } | |
411 | ||
a346af3e | 412 | return get_first_cpu_in_process(process); |
ab65eed3 LM |
413 | } else { |
414 | /* a specific thread */ | |
415 | cpu = find_cpu(tid); | |
416 | ||
417 | if (cpu == NULL) { | |
418 | return NULL; | |
419 | } | |
420 | ||
a346af3e | 421 | process = gdb_get_cpu_process(cpu); |
ab65eed3 LM |
422 | |
423 | if (pid && process->pid != pid) { | |
424 | return NULL; | |
425 | } | |
426 | ||
427 | if (!process->attached) { | |
428 | return NULL; | |
429 | } | |
430 | ||
431 | return cpu; | |
432 | } | |
433 | } | |
434 | ||
a346af3e AB |
435 | static const char *get_feature_xml(const char *p, const char **newp, |
436 | GDBProcess *process) | |
56aebc89 | 437 | { |
56aebc89 PB |
438 | size_t len; |
439 | int i; | |
440 | const char *name; | |
a346af3e | 441 | CPUState *cpu = get_first_cpu_in_process(process); |
c145eeae | 442 | CPUClass *cc = CPU_GET_CLASS(cpu); |
56aebc89 PB |
443 | |
444 | len = 0; | |
445 | while (p[len] && p[len] != ':') | |
446 | len++; | |
447 | *newp = p + len; | |
448 | ||
449 | name = NULL; | |
450 | if (strncmp(p, "target.xml", len) == 0) { | |
c145eeae LM |
451 | char *buf = process->target_xml; |
452 | const size_t buf_sz = sizeof(process->target_xml); | |
453 | ||
56aebc89 | 454 | /* Generate the XML description for this CPU. */ |
c145eeae | 455 | if (!buf[0]) { |
56aebc89 PB |
456 | GDBRegisterState *r; |
457 | ||
c145eeae | 458 | pstrcat(buf, buf_sz, |
b3820e6c DH |
459 | "<?xml version=\"1.0\"?>" |
460 | "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">" | |
461 | "<target>"); | |
462 | if (cc->gdb_arch_name) { | |
463 | gchar *arch = cc->gdb_arch_name(cpu); | |
c145eeae LM |
464 | pstrcat(buf, buf_sz, "<architecture>"); |
465 | pstrcat(buf, buf_sz, arch); | |
466 | pstrcat(buf, buf_sz, "</architecture>"); | |
b3820e6c DH |
467 | g_free(arch); |
468 | } | |
c145eeae LM |
469 | pstrcat(buf, buf_sz, "<xi:include href=\""); |
470 | pstrcat(buf, buf_sz, cc->gdb_core_xml_file); | |
471 | pstrcat(buf, buf_sz, "\"/>"); | |
eac8b355 | 472 | for (r = cpu->gdb_regs; r; r = r->next) { |
c145eeae LM |
473 | pstrcat(buf, buf_sz, "<xi:include href=\""); |
474 | pstrcat(buf, buf_sz, r->xml); | |
475 | pstrcat(buf, buf_sz, "\"/>"); | |
56aebc89 | 476 | } |
c145eeae | 477 | pstrcat(buf, buf_sz, "</target>"); |
56aebc89 | 478 | } |
c145eeae | 479 | return buf; |
56aebc89 | 480 | } |
200bf5b7 | 481 | if (cc->gdb_get_dynamic_xml) { |
200bf5b7 AB |
482 | char *xmlname = g_strndup(p, len); |
483 | const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname); | |
484 | ||
485 | g_free(xmlname); | |
486 | if (xml) { | |
487 | return xml; | |
488 | } | |
489 | } | |
56aebc89 PB |
490 | for (i = 0; ; i++) { |
491 | name = xml_builtin[i][0]; | |
492 | if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len)) | |
493 | break; | |
494 | } | |
495 | return name ? xml_builtin[i][1] : NULL; | |
496 | } | |
f1ccf904 | 497 | |
a010bdbe | 498 | static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) |
56aebc89 | 499 | { |
a0e372f0 | 500 | CPUClass *cc = CPU_GET_CLASS(cpu); |
385b9f0e | 501 | CPUArchState *env = cpu->env_ptr; |
56aebc89 | 502 | GDBRegisterState *r; |
f1ccf904 | 503 | |
a0e372f0 | 504 | if (reg < cc->gdb_num_core_regs) { |
a010bdbe | 505 | return cc->gdb_read_register(cpu, buf, reg); |
a0e372f0 | 506 | } |
f1ccf904 | 507 | |
eac8b355 | 508 | for (r = cpu->gdb_regs; r; r = r->next) { |
56aebc89 | 509 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
a010bdbe | 510 | return r->get_reg(env, buf, reg - r->base_reg); |
56aebc89 PB |
511 | } |
512 | } | |
513 | return 0; | |
f1ccf904 TS |
514 | } |
515 | ||
385b9f0e | 516 | static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
f1ccf904 | 517 | { |
a0e372f0 | 518 | CPUClass *cc = CPU_GET_CLASS(cpu); |
385b9f0e | 519 | CPUArchState *env = cpu->env_ptr; |
56aebc89 | 520 | GDBRegisterState *r; |
f1ccf904 | 521 | |
a0e372f0 | 522 | if (reg < cc->gdb_num_core_regs) { |
5b50e790 | 523 | return cc->gdb_write_register(cpu, mem_buf, reg); |
a0e372f0 | 524 | } |
56aebc89 | 525 | |
eac8b355 | 526 | for (r = cpu->gdb_regs; r; r = r->next) { |
56aebc89 PB |
527 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { |
528 | return r->set_reg(env, mem_buf, reg - r->base_reg); | |
529 | } | |
530 | } | |
6da41eaf FB |
531 | return 0; |
532 | } | |
533 | ||
56aebc89 PB |
534 | /* Register a supplemental set of CPU registers. If g_pos is nonzero it |
535 | specifies the first register number and these registers are included in | |
536 | a standard "g" packet. Direction is relative to gdb, i.e. get_reg is | |
537 | gdb reading a CPU register, and set_reg is gdb modifying a CPU register. | |
538 | */ | |
539 | ||
22169d41 | 540 | void gdb_register_coprocessor(CPUState *cpu, |
a010bdbe | 541 | gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg, |
22169d41 | 542 | int num_regs, const char *xml, int g_pos) |
6da41eaf | 543 | { |
56aebc89 PB |
544 | GDBRegisterState *s; |
545 | GDBRegisterState **p; | |
56aebc89 | 546 | |
eac8b355 | 547 | p = &cpu->gdb_regs; |
56aebc89 PB |
548 | while (*p) { |
549 | /* Check for duplicates. */ | |
550 | if (strcmp((*p)->xml, xml) == 0) | |
551 | return; | |
552 | p = &(*p)->next; | |
553 | } | |
9643c25f SW |
554 | |
555 | s = g_new0(GDBRegisterState, 1); | |
a0e372f0 | 556 | s->base_reg = cpu->gdb_num_regs; |
9643c25f SW |
557 | s->num_regs = num_regs; |
558 | s->get_reg = get_reg; | |
559 | s->set_reg = set_reg; | |
560 | s->xml = xml; | |
561 | ||
56aebc89 | 562 | /* Add to end of list. */ |
a0e372f0 | 563 | cpu->gdb_num_regs += num_regs; |
56aebc89 PB |
564 | *p = s; |
565 | if (g_pos) { | |
566 | if (g_pos != s->base_reg) { | |
7ae6c571 ZY |
567 | error_report("Error: Bad gdb register numbering for '%s', " |
568 | "expected %d got %d", xml, g_pos, s->base_reg); | |
35143f01 AF |
569 | } else { |
570 | cpu->gdb_num_g_regs = cpu->gdb_num_regs; | |
56aebc89 PB |
571 | } |
572 | } | |
6da41eaf FB |
573 | } |
574 | ||
a346af3e | 575 | static void gdb_process_breakpoint_remove_all(GDBProcess *p) |
546f3c67 | 576 | { |
a346af3e | 577 | CPUState *cpu = get_first_cpu_in_process(p); |
546f3c67 LM |
578 | |
579 | while (cpu) { | |
ae7467b1 | 580 | gdb_breakpoint_remove_all(cpu); |
a346af3e | 581 | cpu = gdb_next_cpu_in_process(cpu); |
546f3c67 LM |
582 | } |
583 | } | |
584 | ||
a1d1bb31 | 585 | |
a346af3e | 586 | static void gdb_set_cpu_pc(target_ulong pc) |
fab9d284 | 587 | { |
a346af3e | 588 | CPUState *cpu = gdbserver_state.c_cpu; |
f45748f1 AF |
589 | |
590 | cpu_synchronize_state(cpu); | |
4a2b24ed | 591 | cpu_set_pc(cpu, pc); |
fab9d284 AJ |
592 | } |
593 | ||
36e067b2 | 594 | void gdb_append_thread_id(CPUState *cpu, GString *buf) |
1a227336 | 595 | { |
a346af3e | 596 | if (gdbserver_state.multiprocess) { |
308f9e88 | 597 | g_string_append_printf(buf, "p%02x.%02x", |
36e067b2 | 598 | gdb_get_cpu_pid(cpu), gdb_get_cpu_index(cpu)); |
1a227336 | 599 | } else { |
36e067b2 | 600 | g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu)); |
1a227336 | 601 | } |
1a227336 LM |
602 | } |
603 | ||
7d8c87da LM |
604 | static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf, |
605 | uint32_t *pid, uint32_t *tid) | |
606 | { | |
607 | unsigned long p, t; | |
608 | int ret; | |
609 | ||
610 | if (*buf == 'p') { | |
611 | buf++; | |
612 | ret = qemu_strtoul(buf, &buf, 16, &p); | |
613 | ||
614 | if (ret) { | |
615 | return GDB_READ_THREAD_ERR; | |
616 | } | |
617 | ||
618 | /* Skip '.' */ | |
619 | buf++; | |
620 | } else { | |
621 | p = 1; | |
622 | } | |
623 | ||
624 | ret = qemu_strtoul(buf, &buf, 16, &t); | |
625 | ||
626 | if (ret) { | |
627 | return GDB_READ_THREAD_ERR; | |
628 | } | |
629 | ||
630 | *end_buf = buf; | |
631 | ||
632 | if (p == -1) { | |
633 | return GDB_ALL_PROCESSES; | |
634 | } | |
635 | ||
636 | if (pid) { | |
637 | *pid = p; | |
638 | } | |
639 | ||
640 | if (t == -1) { | |
641 | return GDB_ALL_THREADS; | |
642 | } | |
643 | ||
644 | if (tid) { | |
645 | *tid = t; | |
646 | } | |
647 | ||
648 | return GDB_ONE_THREAD; | |
649 | } | |
650 | ||
544177ad CI |
651 | /** |
652 | * gdb_handle_vcont - Parses and handles a vCont packet. | |
653 | * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is | |
654 | * a format error, 0 on success. | |
655 | */ | |
a346af3e | 656 | static int gdb_handle_vcont(const char *p) |
544177ad | 657 | { |
e40e5204 | 658 | int res, signal = 0; |
544177ad CI |
659 | char cur_action; |
660 | char *newstates; | |
661 | unsigned long tmp; | |
e40e5204 LM |
662 | uint32_t pid, tid; |
663 | GDBProcess *process; | |
544177ad | 664 | CPUState *cpu; |
c99ef792 | 665 | GDBThreadIdKind kind; |
544177ad CI |
666 | #ifdef CONFIG_USER_ONLY |
667 | int max_cpus = 1; /* global variable max_cpus exists only in system mode */ | |
668 | ||
669 | CPU_FOREACH(cpu) { | |
670 | max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus; | |
671 | } | |
5cc8767d LX |
672 | #else |
673 | MachineState *ms = MACHINE(qdev_get_machine()); | |
674 | unsigned int max_cpus = ms->smp.max_cpus; | |
544177ad CI |
675 | #endif |
676 | /* uninitialised CPUs stay 0 */ | |
677 | newstates = g_new0(char, max_cpus); | |
678 | ||
679 | /* mark valid CPUs with 1 */ | |
680 | CPU_FOREACH(cpu) { | |
681 | newstates[cpu->cpu_index] = 1; | |
682 | } | |
683 | ||
684 | /* | |
685 | * res keeps track of what error we are returning, with -ENOTSUP meaning | |
686 | * that the command is unknown or unsupported, thus returning an empty | |
687 | * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid, | |
688 | * or incorrect parameters passed. | |
689 | */ | |
690 | res = 0; | |
691 | while (*p) { | |
692 | if (*p++ != ';') { | |
693 | res = -ENOTSUP; | |
694 | goto out; | |
695 | } | |
696 | ||
697 | cur_action = *p++; | |
698 | if (cur_action == 'C' || cur_action == 'S') { | |
95a5befc | 699 | cur_action = qemu_tolower(cur_action); |
3ddd9036 | 700 | res = qemu_strtoul(p, &p, 16, &tmp); |
544177ad CI |
701 | if (res) { |
702 | goto out; | |
703 | } | |
704 | signal = gdb_signal_to_target(tmp); | |
705 | } else if (cur_action != 'c' && cur_action != 's') { | |
706 | /* unknown/invalid/unsupported command */ | |
707 | res = -ENOTSUP; | |
708 | goto out; | |
709 | } | |
e40e5204 | 710 | |
c99ef792 LM |
711 | if (*p == '\0' || *p == ';') { |
712 | /* | |
713 | * No thread specifier, action is on "all threads". The | |
714 | * specification is unclear regarding the process to act on. We | |
715 | * choose all processes. | |
716 | */ | |
717 | kind = GDB_ALL_PROCESSES; | |
718 | } else if (*p++ == ':') { | |
719 | kind = read_thread_id(p, &p, &pid, &tid); | |
720 | } else { | |
e40e5204 LM |
721 | res = -ENOTSUP; |
722 | goto out; | |
723 | } | |
724 | ||
c99ef792 | 725 | switch (kind) { |
e40e5204 LM |
726 | case GDB_READ_THREAD_ERR: |
727 | res = -EINVAL; | |
728 | goto out; | |
729 | ||
730 | case GDB_ALL_PROCESSES: | |
a346af3e | 731 | cpu = gdb_first_attached_cpu(); |
e40e5204 LM |
732 | while (cpu) { |
733 | if (newstates[cpu->cpu_index] == 1) { | |
734 | newstates[cpu->cpu_index] = cur_action; | |
544177ad | 735 | } |
e40e5204 | 736 | |
a346af3e | 737 | cpu = gdb_next_attached_cpu(cpu); |
544177ad | 738 | } |
e40e5204 LM |
739 | break; |
740 | ||
741 | case GDB_ALL_THREADS: | |
a346af3e | 742 | process = gdb_get_process(pid); |
e40e5204 LM |
743 | |
744 | if (!process->attached) { | |
745 | res = -EINVAL; | |
544177ad CI |
746 | goto out; |
747 | } | |
5a6a1ad1 | 748 | |
a346af3e | 749 | cpu = get_first_cpu_in_process(process); |
e40e5204 LM |
750 | while (cpu) { |
751 | if (newstates[cpu->cpu_index] == 1) { | |
752 | newstates[cpu->cpu_index] = cur_action; | |
753 | } | |
754 | ||
a346af3e | 755 | cpu = gdb_next_cpu_in_process(cpu); |
e40e5204 LM |
756 | } |
757 | break; | |
758 | ||
759 | case GDB_ONE_THREAD: | |
a346af3e | 760 | cpu = gdb_get_cpu(pid, tid); |
544177ad | 761 | |
544177ad | 762 | /* invalid CPU/thread specified */ |
5a6a1ad1 | 763 | if (!cpu) { |
544177ad CI |
764 | res = -EINVAL; |
765 | goto out; | |
766 | } | |
5a6a1ad1 | 767 | |
544177ad CI |
768 | /* only use if no previous match occourred */ |
769 | if (newstates[cpu->cpu_index] == 1) { | |
770 | newstates[cpu->cpu_index] = cur_action; | |
771 | } | |
e40e5204 | 772 | break; |
544177ad CI |
773 | } |
774 | } | |
a346af3e AB |
775 | gdbserver_state.signal = signal; |
776 | gdb_continue_partial(newstates); | |
544177ad CI |
777 | |
778 | out: | |
779 | g_free(newstates); | |
780 | ||
781 | return res; | |
782 | } | |
783 | ||
d14055dc JD |
784 | static const char *cmd_next_param(const char *param, const char delimiter) |
785 | { | |
786 | static const char all_delimiters[] = ",;:="; | |
787 | char curr_delimiters[2] = {0}; | |
788 | const char *delimiters; | |
789 | ||
790 | if (delimiter == '?') { | |
791 | delimiters = all_delimiters; | |
792 | } else if (delimiter == '0') { | |
793 | return strchr(param, '\0'); | |
794 | } else if (delimiter == '.' && *param) { | |
795 | return param + 1; | |
796 | } else { | |
797 | curr_delimiters[0] = delimiter; | |
798 | delimiters = curr_delimiters; | |
799 | } | |
800 | ||
801 | param += strcspn(param, delimiters); | |
802 | if (*param) { | |
803 | param++; | |
804 | } | |
805 | return param; | |
806 | } | |
807 | ||
808 | static int cmd_parse_params(const char *data, const char *schema, | |
26a16181 | 809 | GArray *params) |
d14055dc | 810 | { |
d14055dc JD |
811 | const char *curr_schema, *curr_data; |
812 | ||
26a16181 AB |
813 | g_assert(schema); |
814 | g_assert(params->len == 0); | |
d14055dc JD |
815 | |
816 | curr_schema = schema; | |
d14055dc JD |
817 | curr_data = data; |
818 | while (curr_schema[0] && curr_schema[1] && *curr_data) { | |
26a16181 AB |
819 | GdbCmdVariant this_param; |
820 | ||
d14055dc JD |
821 | switch (curr_schema[0]) { |
822 | case 'l': | |
823 | if (qemu_strtoul(curr_data, &curr_data, 16, | |
26a16181 | 824 | &this_param.val_ul)) { |
d14055dc JD |
825 | return -EINVAL; |
826 | } | |
d14055dc | 827 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
26a16181 | 828 | g_array_append_val(params, this_param); |
d14055dc JD |
829 | break; |
830 | case 'L': | |
831 | if (qemu_strtou64(curr_data, &curr_data, 16, | |
26a16181 | 832 | (uint64_t *)&this_param.val_ull)) { |
d14055dc JD |
833 | return -EINVAL; |
834 | } | |
d14055dc | 835 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
26a16181 | 836 | g_array_append_val(params, this_param); |
d14055dc JD |
837 | break; |
838 | case 's': | |
26a16181 | 839 | this_param.data = curr_data; |
d14055dc | 840 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
26a16181 | 841 | g_array_append_val(params, this_param); |
d14055dc JD |
842 | break; |
843 | case 'o': | |
26a16181 | 844 | this_param.opcode = *(uint8_t *)curr_data; |
d14055dc | 845 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
26a16181 | 846 | g_array_append_val(params, this_param); |
d14055dc JD |
847 | break; |
848 | case 't': | |
26a16181 | 849 | this_param.thread_id.kind = |
d14055dc | 850 | read_thread_id(curr_data, &curr_data, |
26a16181 AB |
851 | &this_param.thread_id.pid, |
852 | &this_param.thread_id.tid); | |
d14055dc | 853 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
26a16181 | 854 | g_array_append_val(params, this_param); |
d14055dc JD |
855 | break; |
856 | case '?': | |
857 | curr_data = cmd_next_param(curr_data, curr_schema[1]); | |
858 | break; | |
859 | default: | |
860 | return -EINVAL; | |
861 | } | |
862 | curr_schema += 2; | |
863 | } | |
864 | ||
d14055dc JD |
865 | return 0; |
866 | } | |
867 | ||
26a16181 | 868 | typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx); |
d14055dc JD |
869 | |
870 | /* | |
871 | * cmd_startswith -> cmd is compared using startswith | |
872 | * | |
873 | * | |
874 | * schema definitions: | |
875 | * Each schema parameter entry consists of 2 chars, | |
876 | * the first char represents the parameter type handling | |
877 | * the second char represents the delimiter for the next parameter | |
878 | * | |
879 | * Currently supported schema types: | |
880 | * 'l' -> unsigned long (stored in .val_ul) | |
881 | * 'L' -> unsigned long long (stored in .val_ull) | |
882 | * 's' -> string (stored in .data) | |
883 | * 'o' -> single char (stored in .opcode) | |
884 | * 't' -> thread id (stored in .thread_id) | |
885 | * '?' -> skip according to delimiter | |
886 | * | |
887 | * Currently supported delimiters: | |
888 | * '?' -> Stop at any delimiter (",;:=\0") | |
889 | * '0' -> Stop at "\0" | |
890 | * '.' -> Skip 1 char unless reached "\0" | |
891 | * Any other value is treated as the delimiter value itself | |
892 | */ | |
893 | typedef struct GdbCmdParseEntry { | |
894 | GdbCmdHandler handler; | |
895 | const char *cmd; | |
896 | bool cmd_startswith; | |
897 | const char *schema; | |
898 | } GdbCmdParseEntry; | |
899 | ||
900 | static inline int startswith(const char *string, const char *pattern) | |
901 | { | |
902 | return !strncmp(string, pattern, strlen(pattern)); | |
903 | } | |
904 | ||
a346af3e | 905 | static int process_string_cmd(void *user_ctx, const char *data, |
d14055dc JD |
906 | const GdbCmdParseEntry *cmds, int num_cmds) |
907 | { | |
26a16181 AB |
908 | int i; |
909 | g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant)); | |
d14055dc JD |
910 | |
911 | if (!cmds) { | |
912 | return -1; | |
913 | } | |
914 | ||
915 | for (i = 0; i < num_cmds; i++) { | |
916 | const GdbCmdParseEntry *cmd = &cmds[i]; | |
917 | g_assert(cmd->handler && cmd->cmd); | |
918 | ||
919 | if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) || | |
920 | (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) { | |
921 | continue; | |
922 | } | |
923 | ||
924 | if (cmd->schema) { | |
26a16181 AB |
925 | if (cmd_parse_params(&data[strlen(cmd->cmd)], |
926 | cmd->schema, params)) { | |
927 | return -1; | |
d14055dc | 928 | } |
d14055dc JD |
929 | } |
930 | ||
26a16181 | 931 | cmd->handler(params, user_ctx); |
d14055dc JD |
932 | return 0; |
933 | } | |
934 | ||
935 | return -1; | |
936 | } | |
937 | ||
a346af3e | 938 | static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd) |
3e2c1261 JD |
939 | { |
940 | if (!data) { | |
941 | return; | |
942 | } | |
943 | ||
308f9e88 | 944 | g_string_set_size(gdbserver_state.str_buf, 0); |
4a25f1b9 | 945 | g_byte_array_set_size(gdbserver_state.mem_buf, 0); |
308f9e88 | 946 | |
3e2c1261 JD |
947 | /* In case there was an error during the command parsing we must |
948 | * send a NULL packet to indicate the command is not supported */ | |
a346af3e | 949 | if (process_string_cmd(NULL, data, cmd, 1)) { |
36e067b2 | 950 | gdb_put_packet(""); |
3e2c1261 JD |
951 | } |
952 | } | |
953 | ||
26a16181 | 954 | static void handle_detach(GArray *params, void *user_ctx) |
3e2c1261 JD |
955 | { |
956 | GDBProcess *process; | |
3e2c1261 JD |
957 | uint32_t pid = 1; |
958 | ||
a346af3e | 959 | if (gdbserver_state.multiprocess) { |
26a16181 | 960 | if (!params->len) { |
36e067b2 | 961 | gdb_put_packet("E22"); |
3e2c1261 JD |
962 | return; |
963 | } | |
964 | ||
26a16181 | 965 | pid = get_param(params, 0)->val_ul; |
3e2c1261 JD |
966 | } |
967 | ||
a346af3e AB |
968 | process = gdb_get_process(pid); |
969 | gdb_process_breakpoint_remove_all(process); | |
3e2c1261 JD |
970 | process->attached = false; |
971 | ||
a346af3e AB |
972 | if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) { |
973 | gdbserver_state.c_cpu = gdb_first_attached_cpu(); | |
3e2c1261 JD |
974 | } |
975 | ||
a346af3e AB |
976 | if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) { |
977 | gdbserver_state.g_cpu = gdb_first_attached_cpu(); | |
3e2c1261 JD |
978 | } |
979 | ||
a346af3e | 980 | if (!gdbserver_state.c_cpu) { |
3e2c1261 JD |
981 | /* No more process attached */ |
982 | gdb_syscall_mode = GDB_SYS_DISABLED; | |
a346af3e | 983 | gdb_continue(); |
3e2c1261 | 984 | } |
36e067b2 | 985 | gdb_put_packet("OK"); |
3e2c1261 JD |
986 | } |
987 | ||
26a16181 | 988 | static void handle_thread_alive(GArray *params, void *user_ctx) |
44ffded0 JD |
989 | { |
990 | CPUState *cpu; | |
991 | ||
26a16181 | 992 | if (!params->len) { |
36e067b2 | 993 | gdb_put_packet("E22"); |
44ffded0 JD |
994 | return; |
995 | } | |
996 | ||
26a16181 | 997 | if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { |
36e067b2 | 998 | gdb_put_packet("E22"); |
44ffded0 JD |
999 | return; |
1000 | } | |
1001 | ||
26a16181 AB |
1002 | cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid, |
1003 | get_param(params, 0)->thread_id.tid); | |
44ffded0 | 1004 | if (!cpu) { |
36e067b2 | 1005 | gdb_put_packet("E22"); |
44ffded0 JD |
1006 | return; |
1007 | } | |
1008 | ||
36e067b2 | 1009 | gdb_put_packet("OK"); |
44ffded0 JD |
1010 | } |
1011 | ||
26a16181 | 1012 | static void handle_continue(GArray *params, void *user_ctx) |
4d6e3fe2 | 1013 | { |
26a16181 AB |
1014 | if (params->len) { |
1015 | gdb_set_cpu_pc(get_param(params, 0)->val_ull); | |
4d6e3fe2 JD |
1016 | } |
1017 | ||
a346af3e AB |
1018 | gdbserver_state.signal = 0; |
1019 | gdb_continue(); | |
4d6e3fe2 JD |
1020 | } |
1021 | ||
26a16181 | 1022 | static void handle_cont_with_sig(GArray *params, void *user_ctx) |
ccc47d5d JD |
1023 | { |
1024 | unsigned long signal = 0; | |
1025 | ||
1026 | /* | |
1027 | * Note: C sig;[addr] is currently unsupported and we simply | |
1028 | * omit the addr parameter | |
1029 | */ | |
26a16181 AB |
1030 | if (params->len) { |
1031 | signal = get_param(params, 0)->val_ul; | |
ccc47d5d JD |
1032 | } |
1033 | ||
a346af3e AB |
1034 | gdbserver_state.signal = gdb_signal_to_target(signal); |
1035 | if (gdbserver_state.signal == -1) { | |
1036 | gdbserver_state.signal = 0; | |
ccc47d5d | 1037 | } |
a346af3e | 1038 | gdb_continue(); |
ccc47d5d JD |
1039 | } |
1040 | ||
26a16181 | 1041 | static void handle_set_thread(GArray *params, void *user_ctx) |
3a9651d6 JD |
1042 | { |
1043 | CPUState *cpu; | |
1044 | ||
26a16181 | 1045 | if (params->len != 2) { |
36e067b2 | 1046 | gdb_put_packet("E22"); |
3a9651d6 JD |
1047 | return; |
1048 | } | |
1049 | ||
26a16181 | 1050 | if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) { |
36e067b2 | 1051 | gdb_put_packet("E22"); |
3a9651d6 JD |
1052 | return; |
1053 | } | |
1054 | ||
26a16181 | 1055 | if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) { |
36e067b2 | 1056 | gdb_put_packet("OK"); |
3a9651d6 JD |
1057 | return; |
1058 | } | |
1059 | ||
26a16181 AB |
1060 | cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid, |
1061 | get_param(params, 1)->thread_id.tid); | |
3a9651d6 | 1062 | if (!cpu) { |
36e067b2 | 1063 | gdb_put_packet("E22"); |
3a9651d6 JD |
1064 | return; |
1065 | } | |
1066 | ||
1067 | /* | |
1068 | * Note: This command is deprecated and modern gdb's will be using the | |
1069 | * vCont command instead. | |
1070 | */ | |
26a16181 | 1071 | switch (get_param(params, 0)->opcode) { |
3a9651d6 | 1072 | case 'c': |
a346af3e | 1073 | gdbserver_state.c_cpu = cpu; |
36e067b2 | 1074 | gdb_put_packet("OK"); |
3a9651d6 JD |
1075 | break; |
1076 | case 'g': | |
a346af3e | 1077 | gdbserver_state.g_cpu = cpu; |
36e067b2 | 1078 | gdb_put_packet("OK"); |
3a9651d6 JD |
1079 | break; |
1080 | default: | |
36e067b2 | 1081 | gdb_put_packet("E22"); |
3a9651d6 JD |
1082 | break; |
1083 | } | |
1084 | } | |
1085 | ||
26a16181 | 1086 | static void handle_insert_bp(GArray *params, void *user_ctx) |
77f6ce50 JD |
1087 | { |
1088 | int res; | |
1089 | ||
26a16181 | 1090 | if (params->len != 3) { |
36e067b2 | 1091 | gdb_put_packet("E22"); |
77f6ce50 JD |
1092 | return; |
1093 | } | |
1094 | ||
ae7467b1 AB |
1095 | res = gdb_breakpoint_insert(gdbserver_state.c_cpu, |
1096 | get_param(params, 0)->val_ul, | |
26a16181 AB |
1097 | get_param(params, 1)->val_ull, |
1098 | get_param(params, 2)->val_ull); | |
77f6ce50 | 1099 | if (res >= 0) { |
36e067b2 | 1100 | gdb_put_packet("OK"); |
77f6ce50 JD |
1101 | return; |
1102 | } else if (res == -ENOSYS) { | |
36e067b2 | 1103 | gdb_put_packet(""); |
77f6ce50 JD |
1104 | return; |
1105 | } | |
1106 | ||
36e067b2 | 1107 | gdb_put_packet("E22"); |
77f6ce50 JD |
1108 | } |
1109 | ||
26a16181 | 1110 | static void handle_remove_bp(GArray *params, void *user_ctx) |
77f6ce50 JD |
1111 | { |
1112 | int res; | |
1113 | ||
26a16181 | 1114 | if (params->len != 3) { |
36e067b2 | 1115 | gdb_put_packet("E22"); |
77f6ce50 JD |
1116 | return; |
1117 | } | |
1118 | ||
ae7467b1 AB |
1119 | res = gdb_breakpoint_remove(gdbserver_state.c_cpu, |
1120 | get_param(params, 0)->val_ul, | |
26a16181 AB |
1121 | get_param(params, 1)->val_ull, |
1122 | get_param(params, 2)->val_ull); | |
77f6ce50 | 1123 | if (res >= 0) { |
36e067b2 | 1124 | gdb_put_packet("OK"); |
77f6ce50 JD |
1125 | return; |
1126 | } else if (res == -ENOSYS) { | |
36e067b2 | 1127 | gdb_put_packet(""); |
77f6ce50 JD |
1128 | return; |
1129 | } | |
1130 | ||
36e067b2 | 1131 | gdb_put_packet("E22"); |
77f6ce50 JD |
1132 | } |
1133 | ||
94b2a62b AB |
1134 | /* |
1135 | * handle_set/get_reg | |
1136 | * | |
1137 | * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available. | |
1138 | * This works, but can be very slow. Anything new enough to understand | |
1139 | * XML also knows how to use this properly. However to use this we | |
1140 | * need to define a local XML file as well as be talking to a | |
1141 | * reasonably modern gdb. Responding with an empty packet will cause | |
1142 | * the remote gdb to fallback to older methods. | |
1143 | */ | |
1144 | ||
26a16181 | 1145 | static void handle_set_reg(GArray *params, void *user_ctx) |
62b3320b JD |
1146 | { |
1147 | int reg_size; | |
1148 | ||
1149 | if (!gdb_has_xml) { | |
36e067b2 | 1150 | gdb_put_packet(""); |
62b3320b JD |
1151 | return; |
1152 | } | |
1153 | ||
26a16181 | 1154 | if (params->len != 2) { |
36e067b2 | 1155 | gdb_put_packet("E22"); |
62b3320b JD |
1156 | return; |
1157 | } | |
1158 | ||
26a16181 | 1159 | reg_size = strlen(get_param(params, 1)->data) / 2; |
36e067b2 | 1160 | gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size); |
4a25f1b9 | 1161 | gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data, |
26a16181 | 1162 | get_param(params, 0)->val_ull); |
36e067b2 | 1163 | gdb_put_packet("OK"); |
62b3320b JD |
1164 | } |
1165 | ||
26a16181 | 1166 | static void handle_get_reg(GArray *params, void *user_ctx) |
5d0e57bd JD |
1167 | { |
1168 | int reg_size; | |
1169 | ||
5d0e57bd | 1170 | if (!gdb_has_xml) { |
36e067b2 | 1171 | gdb_put_packet(""); |
5d0e57bd JD |
1172 | return; |
1173 | } | |
1174 | ||
26a16181 | 1175 | if (!params->len) { |
36e067b2 | 1176 | gdb_put_packet("E14"); |
5d0e57bd JD |
1177 | return; |
1178 | } | |
1179 | ||
4a25f1b9 | 1180 | reg_size = gdb_read_register(gdbserver_state.g_cpu, |
a010bdbe | 1181 | gdbserver_state.mem_buf, |
26a16181 | 1182 | get_param(params, 0)->val_ull); |
5d0e57bd | 1183 | if (!reg_size) { |
36e067b2 | 1184 | gdb_put_packet("E14"); |
5d0e57bd | 1185 | return; |
4a25f1b9 AB |
1186 | } else { |
1187 | g_byte_array_set_size(gdbserver_state.mem_buf, reg_size); | |
5d0e57bd JD |
1188 | } |
1189 | ||
36e067b2 AB |
1190 | gdb_memtohex(gdbserver_state.str_buf, |
1191 | gdbserver_state.mem_buf->data, reg_size); | |
1192 | gdb_put_strbuf(); | |
5d0e57bd JD |
1193 | } |
1194 | ||
26a16181 | 1195 | static void handle_write_mem(GArray *params, void *user_ctx) |
cc0ecc78 | 1196 | { |
26a16181 | 1197 | if (params->len != 3) { |
36e067b2 | 1198 | gdb_put_packet("E22"); |
cc0ecc78 JD |
1199 | return; |
1200 | } | |
1201 | ||
36e067b2 | 1202 | /* gdb_hextomem() reads 2*len bytes */ |
26a16181 AB |
1203 | if (get_param(params, 1)->val_ull > |
1204 | strlen(get_param(params, 2)->data) / 2) { | |
36e067b2 | 1205 | gdb_put_packet("E22"); |
cc0ecc78 JD |
1206 | return; |
1207 | } | |
1208 | ||
36e067b2 | 1209 | gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data, |
26a16181 AB |
1210 | get_param(params, 1)->val_ull); |
1211 | if (target_memory_rw_debug(gdbserver_state.g_cpu, | |
1212 | get_param(params, 0)->val_ull, | |
4a25f1b9 AB |
1213 | gdbserver_state.mem_buf->data, |
1214 | gdbserver_state.mem_buf->len, true)) { | |
36e067b2 | 1215 | gdb_put_packet("E14"); |
cc0ecc78 JD |
1216 | return; |
1217 | } | |
1218 | ||
36e067b2 | 1219 | gdb_put_packet("OK"); |
cc0ecc78 JD |
1220 | } |
1221 | ||
26a16181 | 1222 | static void handle_read_mem(GArray *params, void *user_ctx) |
da92e236 | 1223 | { |
26a16181 | 1224 | if (params->len != 2) { |
36e067b2 | 1225 | gdb_put_packet("E22"); |
da92e236 JD |
1226 | return; |
1227 | } | |
1228 | ||
36e067b2 | 1229 | /* gdb_memtohex() doubles the required space */ |
26a16181 | 1230 | if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) { |
36e067b2 | 1231 | gdb_put_packet("E22"); |
da92e236 JD |
1232 | return; |
1233 | } | |
1234 | ||
26a16181 AB |
1235 | g_byte_array_set_size(gdbserver_state.mem_buf, |
1236 | get_param(params, 1)->val_ull); | |
4a25f1b9 | 1237 | |
26a16181 AB |
1238 | if (target_memory_rw_debug(gdbserver_state.g_cpu, |
1239 | get_param(params, 0)->val_ull, | |
4a25f1b9 AB |
1240 | gdbserver_state.mem_buf->data, |
1241 | gdbserver_state.mem_buf->len, false)) { | |
36e067b2 | 1242 | gdb_put_packet("E14"); |
da92e236 JD |
1243 | return; |
1244 | } | |
1245 | ||
36e067b2 | 1246 | gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, |
4a25f1b9 | 1247 | gdbserver_state.mem_buf->len); |
36e067b2 | 1248 | gdb_put_strbuf(); |
da92e236 JD |
1249 | } |
1250 | ||
26a16181 | 1251 | static void handle_write_all_regs(GArray *params, void *user_ctx) |
287ca120 JD |
1252 | { |
1253 | target_ulong addr, len; | |
1254 | uint8_t *registers; | |
1255 | int reg_size; | |
1256 | ||
26a16181 | 1257 | if (!params->len) { |
287ca120 JD |
1258 | return; |
1259 | } | |
1260 | ||
a346af3e | 1261 | cpu_synchronize_state(gdbserver_state.g_cpu); |
26a16181 | 1262 | len = strlen(get_param(params, 0)->data) / 2; |
36e067b2 | 1263 | gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len); |
4a25f1b9 | 1264 | registers = gdbserver_state.mem_buf->data; |
a346af3e | 1265 | for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0; |
287ca120 | 1266 | addr++) { |
a346af3e | 1267 | reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, addr); |
287ca120 JD |
1268 | len -= reg_size; |
1269 | registers += reg_size; | |
1270 | } | |
36e067b2 | 1271 | gdb_put_packet("OK"); |
287ca120 JD |
1272 | } |
1273 | ||
26a16181 | 1274 | static void handle_read_all_regs(GArray *params, void *user_ctx) |
397d1370 JD |
1275 | { |
1276 | target_ulong addr, len; | |
1277 | ||
a346af3e | 1278 | cpu_synchronize_state(gdbserver_state.g_cpu); |
a010bdbe | 1279 | g_byte_array_set_size(gdbserver_state.mem_buf, 0); |
397d1370 | 1280 | len = 0; |
a346af3e | 1281 | for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs; addr++) { |
4a25f1b9 | 1282 | len += gdb_read_register(gdbserver_state.g_cpu, |
a010bdbe | 1283 | gdbserver_state.mem_buf, |
397d1370 JD |
1284 | addr); |
1285 | } | |
a010bdbe | 1286 | g_assert(len == gdbserver_state.mem_buf->len); |
397d1370 | 1287 | |
36e067b2 AB |
1288 | gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len); |
1289 | gdb_put_strbuf(); | |
397d1370 JD |
1290 | } |
1291 | ||
26a16181 | 1292 | static void handle_file_io(GArray *params, void *user_ctx) |
4b20fab1 | 1293 | { |
26a16181 | 1294 | if (params->len >= 1 && gdbserver_state.current_syscall_cb) { |
64c8c6a9 RH |
1295 | uint64_t ret; |
1296 | int err; | |
4b20fab1 | 1297 | |
64c8c6a9 | 1298 | ret = get_param(params, 0)->val_ull; |
26a16181 | 1299 | if (params->len >= 2) { |
64c8c6a9 | 1300 | err = get_param(params, 1)->val_ull; |
c6ee9521 SL |
1301 | } else { |
1302 | err = 0; | |
1303 | } | |
c805e118 RH |
1304 | |
1305 | /* Convert GDB error numbers back to host error numbers. */ | |
1306 | #define E(X) case GDB_E##X: err = E##X; break | |
1307 | switch (err) { | |
1308 | case 0: | |
1309 | break; | |
1310 | E(PERM); | |
1311 | E(NOENT); | |
1312 | E(INTR); | |
1313 | E(BADF); | |
1314 | E(ACCES); | |
1315 | E(FAULT); | |
1316 | E(BUSY); | |
1317 | E(EXIST); | |
1318 | E(NODEV); | |
1319 | E(NOTDIR); | |
1320 | E(ISDIR); | |
1321 | E(INVAL); | |
1322 | E(NFILE); | |
1323 | E(MFILE); | |
1324 | E(FBIG); | |
1325 | E(NOSPC); | |
1326 | E(SPIPE); | |
1327 | E(ROFS); | |
1328 | E(NAMETOOLONG); | |
1329 | default: | |
1330 | err = EINVAL; | |
1331 | break; | |
1332 | } | |
1333 | #undef E | |
1334 | ||
a346af3e AB |
1335 | gdbserver_state.current_syscall_cb(gdbserver_state.c_cpu, ret, err); |
1336 | gdbserver_state.current_syscall_cb = NULL; | |
4b20fab1 JD |
1337 | } |
1338 | ||
26a16181 | 1339 | if (params->len >= 3 && get_param(params, 2)->opcode == (uint8_t)'C') { |
36e067b2 | 1340 | gdb_put_packet("T02"); |
4b20fab1 JD |
1341 | return; |
1342 | } | |
1343 | ||
a346af3e | 1344 | gdb_continue(); |
4b20fab1 JD |
1345 | } |
1346 | ||
26a16181 | 1347 | static void handle_step(GArray *params, void *user_ctx) |
933f80dd | 1348 | { |
26a16181 AB |
1349 | if (params->len) { |
1350 | gdb_set_cpu_pc((target_ulong)get_param(params, 0)->val_ull); | |
933f80dd JD |
1351 | } |
1352 | ||
ecd39d62 | 1353 | cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags); |
a346af3e | 1354 | gdb_continue(); |
933f80dd JD |
1355 | } |
1356 | ||
26a16181 | 1357 | static void handle_backward(GArray *params, void *user_ctx) |
fda8458b | 1358 | { |
ed12f5b4 | 1359 | if (!stub_can_reverse()) { |
36e067b2 | 1360 | gdb_put_packet("E22"); |
fda8458b | 1361 | } |
26a16181 AB |
1362 | if (params->len == 1) { |
1363 | switch (get_param(params, 0)->opcode) { | |
fda8458b PD |
1364 | case 's': |
1365 | if (replay_reverse_step()) { | |
1366 | gdb_continue(); | |
1367 | } else { | |
36e067b2 | 1368 | gdb_put_packet("E14"); |
fda8458b PD |
1369 | } |
1370 | return; | |
cda38259 PD |
1371 | case 'c': |
1372 | if (replay_reverse_continue()) { | |
1373 | gdb_continue(); | |
1374 | } else { | |
36e067b2 | 1375 | gdb_put_packet("E14"); |
cda38259 PD |
1376 | } |
1377 | return; | |
fda8458b PD |
1378 | } |
1379 | } | |
1380 | ||
1381 | /* Default invalid command */ | |
36e067b2 | 1382 | gdb_put_packet(""); |
fda8458b PD |
1383 | } |
1384 | ||
26a16181 | 1385 | static void handle_v_cont_query(GArray *params, void *user_ctx) |
8536ec02 | 1386 | { |
36e067b2 | 1387 | gdb_put_packet("vCont;c;C;s;S"); |
8536ec02 JD |
1388 | } |
1389 | ||
26a16181 | 1390 | static void handle_v_cont(GArray *params, void *user_ctx) |
8536ec02 JD |
1391 | { |
1392 | int res; | |
1393 | ||
26a16181 | 1394 | if (!params->len) { |
8536ec02 JD |
1395 | return; |
1396 | } | |
1397 | ||
26a16181 | 1398 | res = gdb_handle_vcont(get_param(params, 0)->data); |
8536ec02 | 1399 | if ((res == -EINVAL) || (res == -ERANGE)) { |
36e067b2 | 1400 | gdb_put_packet("E22"); |
8536ec02 | 1401 | } else if (res) { |
36e067b2 | 1402 | gdb_put_packet(""); |
8536ec02 JD |
1403 | } |
1404 | } | |
1405 | ||
26a16181 | 1406 | static void handle_v_attach(GArray *params, void *user_ctx) |
8536ec02 JD |
1407 | { |
1408 | GDBProcess *process; | |
1409 | CPUState *cpu; | |
8536ec02 | 1410 | |
308f9e88 | 1411 | g_string_assign(gdbserver_state.str_buf, "E22"); |
26a16181 | 1412 | if (!params->len) { |
8536ec02 JD |
1413 | goto cleanup; |
1414 | } | |
1415 | ||
26a16181 | 1416 | process = gdb_get_process(get_param(params, 0)->val_ul); |
8536ec02 JD |
1417 | if (!process) { |
1418 | goto cleanup; | |
1419 | } | |
1420 | ||
a346af3e | 1421 | cpu = get_first_cpu_in_process(process); |
8536ec02 JD |
1422 | if (!cpu) { |
1423 | goto cleanup; | |
1424 | } | |
1425 | ||
1426 | process->attached = true; | |
a346af3e AB |
1427 | gdbserver_state.g_cpu = cpu; |
1428 | gdbserver_state.c_cpu = cpu; | |
8536ec02 | 1429 | |
308f9e88 AB |
1430 | g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP); |
1431 | gdb_append_thread_id(cpu, gdbserver_state.str_buf); | |
1432 | g_string_append_c(gdbserver_state.str_buf, ';'); | |
8536ec02 | 1433 | cleanup: |
36e067b2 | 1434 | gdb_put_strbuf(); |
8536ec02 JD |
1435 | } |
1436 | ||
26a16181 | 1437 | static void handle_v_kill(GArray *params, void *user_ctx) |
8536ec02 JD |
1438 | { |
1439 | /* Kill the target */ | |
36e067b2 | 1440 | gdb_put_packet("OK"); |
8536ec02 | 1441 | error_report("QEMU: Terminated via GDBstub"); |
b9e10c6c | 1442 | gdb_exit(0); |
8536ec02 JD |
1443 | exit(0); |
1444 | } | |
1445 | ||
305bea06 | 1446 | static const GdbCmdParseEntry gdb_v_commands_table[] = { |
8536ec02 JD |
1447 | /* Order is important if has same prefix */ |
1448 | { | |
1449 | .handler = handle_v_cont_query, | |
1450 | .cmd = "Cont?", | |
1451 | .cmd_startswith = 1 | |
1452 | }, | |
1453 | { | |
1454 | .handler = handle_v_cont, | |
1455 | .cmd = "Cont", | |
1456 | .cmd_startswith = 1, | |
1457 | .schema = "s0" | |
1458 | }, | |
1459 | { | |
1460 | .handler = handle_v_attach, | |
1461 | .cmd = "Attach;", | |
1462 | .cmd_startswith = 1, | |
1463 | .schema = "l0" | |
1464 | }, | |
1465 | { | |
1466 | .handler = handle_v_kill, | |
1467 | .cmd = "Kill;", | |
1468 | .cmd_startswith = 1 | |
1469 | }, | |
1470 | }; | |
1471 | ||
26a16181 | 1472 | static void handle_v_commands(GArray *params, void *user_ctx) |
8536ec02 | 1473 | { |
26a16181 | 1474 | if (!params->len) { |
8536ec02 JD |
1475 | return; |
1476 | } | |
1477 | ||
26a16181 | 1478 | if (process_string_cmd(NULL, get_param(params, 0)->data, |
8536ec02 JD |
1479 | gdb_v_commands_table, |
1480 | ARRAY_SIZE(gdb_v_commands_table))) { | |
36e067b2 | 1481 | gdb_put_packet(""); |
8536ec02 JD |
1482 | } |
1483 | } | |
1484 | ||
26a16181 | 1485 | static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx) |
2704efad | 1486 | { |
ecd39d62 ML |
1487 | g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE); |
1488 | ||
1489 | if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) { | |
1490 | g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x", | |
1491 | SSTEP_NOIRQ); | |
1492 | } | |
1493 | ||
1494 | if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) { | |
1495 | g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x", | |
1496 | SSTEP_NOTIMER); | |
1497 | } | |
1498 | ||
36e067b2 | 1499 | gdb_put_strbuf(); |
2704efad JD |
1500 | } |
1501 | ||
26a16181 | 1502 | static void handle_set_qemu_sstep(GArray *params, void *user_ctx) |
2704efad | 1503 | { |
ecd39d62 ML |
1504 | int new_sstep_flags; |
1505 | ||
26a16181 | 1506 | if (!params->len) { |
2704efad JD |
1507 | return; |
1508 | } | |
1509 | ||
ecd39d62 ML |
1510 | new_sstep_flags = get_param(params, 0)->val_ul; |
1511 | ||
1512 | if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) { | |
36e067b2 | 1513 | gdb_put_packet("E22"); |
ecd39d62 ML |
1514 | return; |
1515 | } | |
1516 | ||
1517 | gdbserver_state.sstep_flags = new_sstep_flags; | |
36e067b2 | 1518 | gdb_put_packet("OK"); |
2704efad JD |
1519 | } |
1520 | ||
26a16181 | 1521 | static void handle_query_qemu_sstep(GArray *params, void *user_ctx) |
2704efad | 1522 | { |
ecd39d62 ML |
1523 | g_string_printf(gdbserver_state.str_buf, "0x%x", |
1524 | gdbserver_state.sstep_flags); | |
36e067b2 | 1525 | gdb_put_strbuf(); |
2704efad JD |
1526 | } |
1527 | ||
26a16181 | 1528 | static void handle_query_curr_tid(GArray *params, void *user_ctx) |
b4608c04 | 1529 | { |
2e0f2cfb | 1530 | CPUState *cpu; |
2704efad | 1531 | GDBProcess *process; |
2704efad JD |
1532 | |
1533 | /* | |
1534 | * "Current thread" remains vague in the spec, so always return | |
1535 | * the first thread of the current process (gdb returns the | |
1536 | * first thread). | |
1537 | */ | |
a346af3e AB |
1538 | process = gdb_get_cpu_process(gdbserver_state.g_cpu); |
1539 | cpu = get_first_cpu_in_process(process); | |
308f9e88 AB |
1540 | g_string_assign(gdbserver_state.str_buf, "QC"); |
1541 | gdb_append_thread_id(cpu, gdbserver_state.str_buf); | |
36e067b2 | 1542 | gdb_put_strbuf(); |
2704efad JD |
1543 | } |
1544 | ||
26a16181 | 1545 | static void handle_query_threads(GArray *params, void *user_ctx) |
2704efad | 1546 | { |
a346af3e | 1547 | if (!gdbserver_state.query_cpu) { |
36e067b2 | 1548 | gdb_put_packet("l"); |
2704efad JD |
1549 | return; |
1550 | } | |
1551 | ||
308f9e88 AB |
1552 | g_string_assign(gdbserver_state.str_buf, "m"); |
1553 | gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf); | |
36e067b2 | 1554 | gdb_put_strbuf(); |
a346af3e | 1555 | gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu); |
2704efad JD |
1556 | } |
1557 | ||
26a16181 | 1558 | static void handle_query_first_threads(GArray *params, void *user_ctx) |
2704efad | 1559 | { |
a346af3e | 1560 | gdbserver_state.query_cpu = gdb_first_attached_cpu(); |
26a16181 | 1561 | handle_query_threads(params, user_ctx); |
2704efad JD |
1562 | } |
1563 | ||
26a16181 | 1564 | static void handle_query_thread_extra(GArray *params, void *user_ctx) |
2704efad | 1565 | { |
308f9e88 | 1566 | g_autoptr(GString) rs = g_string_new(NULL); |
2704efad | 1567 | CPUState *cpu; |
2704efad | 1568 | |
26a16181 AB |
1569 | if (!params->len || |
1570 | get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { | |
36e067b2 | 1571 | gdb_put_packet("E22"); |
2704efad JD |
1572 | return; |
1573 | } | |
1574 | ||
26a16181 AB |
1575 | cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid, |
1576 | get_param(params, 0)->thread_id.tid); | |
2704efad JD |
1577 | if (!cpu) { |
1578 | return; | |
1579 | } | |
1580 | ||
1581 | cpu_synchronize_state(cpu); | |
1582 | ||
a346af3e | 1583 | if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) { |
2704efad JD |
1584 | /* Print the CPU model and name in multiprocess mode */ |
1585 | ObjectClass *oc = object_get_class(OBJECT(cpu)); | |
1586 | const char *cpu_model = object_class_get_name(oc); | |
7a309cc9 | 1587 | const char *cpu_name = |
076b2fad | 1588 | object_get_canonical_path_component(OBJECT(cpu)); |
308f9e88 AB |
1589 | g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name, |
1590 | cpu->halted ? "halted " : "running"); | |
2704efad | 1591 | } else { |
308f9e88 | 1592 | g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index, |
2704efad JD |
1593 | cpu->halted ? "halted " : "running"); |
1594 | } | |
308f9e88 | 1595 | trace_gdbstub_op_extra_info(rs->str); |
36e067b2 AB |
1596 | gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len); |
1597 | gdb_put_strbuf(); | |
2704efad JD |
1598 | } |
1599 | ||
26a16181 | 1600 | static void handle_query_supported(GArray *params, void *user_ctx) |
2704efad JD |
1601 | { |
1602 | CPUClass *cc; | |
1603 | ||
308f9e88 | 1604 | g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH); |
2704efad JD |
1605 | cc = CPU_GET_CLASS(first_cpu); |
1606 | if (cc->gdb_core_xml_file) { | |
308f9e88 | 1607 | g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+"); |
2704efad JD |
1608 | } |
1609 | ||
ed12f5b4 | 1610 | if (stub_can_reverse()) { |
cda38259 PD |
1611 | g_string_append(gdbserver_state.str_buf, |
1612 | ";ReverseStep+;ReverseContinue+"); | |
fda8458b PD |
1613 | } |
1614 | ||
51c623b0 LY |
1615 | #ifdef CONFIG_USER_ONLY |
1616 | if (gdbserver_state.c_cpu->opaque) { | |
1617 | g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+"); | |
1618 | } | |
1619 | #endif | |
1620 | ||
26a16181 AB |
1621 | if (params->len && |
1622 | strstr(get_param(params, 0)->data, "multiprocess+")) { | |
a346af3e | 1623 | gdbserver_state.multiprocess = true; |
2704efad JD |
1624 | } |
1625 | ||
3bc2609d | 1626 | g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+"); |
36e067b2 | 1627 | gdb_put_strbuf(); |
2704efad JD |
1628 | } |
1629 | ||
26a16181 | 1630 | static void handle_query_xfer_features(GArray *params, void *user_ctx) |
2704efad | 1631 | { |
c145eeae | 1632 | GDBProcess *process; |
5b24c641 | 1633 | CPUClass *cc; |
2704efad JD |
1634 | unsigned long len, total_len, addr; |
1635 | const char *xml; | |
b4608c04 | 1636 | const char *p; |
2704efad | 1637 | |
26a16181 | 1638 | if (params->len < 3) { |
36e067b2 | 1639 | gdb_put_packet("E22"); |
2704efad JD |
1640 | return; |
1641 | } | |
1642 | ||
a346af3e AB |
1643 | process = gdb_get_cpu_process(gdbserver_state.g_cpu); |
1644 | cc = CPU_GET_CLASS(gdbserver_state.g_cpu); | |
2704efad | 1645 | if (!cc->gdb_core_xml_file) { |
36e067b2 | 1646 | gdb_put_packet(""); |
2704efad JD |
1647 | return; |
1648 | } | |
1649 | ||
1650 | gdb_has_xml = true; | |
26a16181 | 1651 | p = get_param(params, 0)->data; |
a346af3e | 1652 | xml = get_feature_xml(p, &p, process); |
2704efad | 1653 | if (!xml) { |
36e067b2 | 1654 | gdb_put_packet("E00"); |
2704efad JD |
1655 | return; |
1656 | } | |
1657 | ||
26a16181 AB |
1658 | addr = get_param(params, 1)->val_ul; |
1659 | len = get_param(params, 2)->val_ul; | |
2704efad JD |
1660 | total_len = strlen(xml); |
1661 | if (addr > total_len) { | |
36e067b2 | 1662 | gdb_put_packet("E00"); |
2704efad JD |
1663 | return; |
1664 | } | |
1665 | ||
1666 | if (len > (MAX_PACKET_LENGTH - 5) / 2) { | |
1667 | len = (MAX_PACKET_LENGTH - 5) / 2; | |
1668 | } | |
1669 | ||
1670 | if (len < total_len - addr) { | |
308f9e88 | 1671 | g_string_assign(gdbserver_state.str_buf, "m"); |
36e067b2 | 1672 | gdb_memtox(gdbserver_state.str_buf, xml + addr, len); |
2704efad | 1673 | } else { |
308f9e88 | 1674 | g_string_assign(gdbserver_state.str_buf, "l"); |
36e067b2 | 1675 | gdb_memtox(gdbserver_state.str_buf, xml + addr, total_len - addr); |
2704efad JD |
1676 | } |
1677 | ||
36e067b2 | 1678 | gdb_put_packet_binary(gdbserver_state.str_buf->str, |
308f9e88 | 1679 | gdbserver_state.str_buf->len, true); |
2704efad JD |
1680 | } |
1681 | ||
26a16181 | 1682 | static void handle_query_attached(GArray *params, void *user_ctx) |
2704efad | 1683 | { |
36e067b2 | 1684 | gdb_put_packet(GDB_ATTACHED); |
2704efad JD |
1685 | } |
1686 | ||
26a16181 | 1687 | static void handle_query_qemu_supported(GArray *params, void *user_ctx) |
2704efad | 1688 | { |
308f9e88 | 1689 | g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep"); |
ab4752ec | 1690 | #ifndef CONFIG_USER_ONLY |
308f9e88 | 1691 | g_string_append(gdbserver_state.str_buf, ";PhyMemMode"); |
ab4752ec | 1692 | #endif |
36e067b2 | 1693 | gdb_put_strbuf(); |
ab4752ec JD |
1694 | } |
1695 | ||
1696 | #ifndef CONFIG_USER_ONLY | |
26a16181 | 1697 | static void handle_query_qemu_phy_mem_mode(GArray *params, |
ab4752ec JD |
1698 | void *user_ctx) |
1699 | { | |
308f9e88 | 1700 | g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode); |
36e067b2 | 1701 | gdb_put_strbuf(); |
ab4752ec JD |
1702 | } |
1703 | ||
26a16181 | 1704 | static void handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx) |
ab4752ec | 1705 | { |
26a16181 | 1706 | if (!params->len) { |
36e067b2 | 1707 | gdb_put_packet("E22"); |
ab4752ec JD |
1708 | return; |
1709 | } | |
1710 | ||
26a16181 | 1711 | if (!get_param(params, 0)->val_ul) { |
ab4752ec JD |
1712 | phy_memory_mode = 0; |
1713 | } else { | |
1714 | phy_memory_mode = 1; | |
1715 | } | |
36e067b2 | 1716 | gdb_put_packet("OK"); |
2704efad | 1717 | } |
ab4752ec | 1718 | #endif |
2704efad | 1719 | |
305bea06 | 1720 | static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = { |
2704efad JD |
1721 | /* Order is important if has same prefix */ |
1722 | { | |
1723 | .handler = handle_query_qemu_sstepbits, | |
1724 | .cmd = "qemu.sstepbits", | |
1725 | }, | |
1726 | { | |
1727 | .handler = handle_query_qemu_sstep, | |
1728 | .cmd = "qemu.sstep", | |
1729 | }, | |
1730 | { | |
1731 | .handler = handle_set_qemu_sstep, | |
1732 | .cmd = "qemu.sstep=", | |
1733 | .cmd_startswith = 1, | |
1734 | .schema = "l0" | |
1735 | }, | |
1736 | }; | |
1737 | ||
305bea06 | 1738 | static const GdbCmdParseEntry gdb_gen_query_table[] = { |
2704efad JD |
1739 | { |
1740 | .handler = handle_query_curr_tid, | |
1741 | .cmd = "C", | |
1742 | }, | |
1743 | { | |
1744 | .handler = handle_query_threads, | |
1745 | .cmd = "sThreadInfo", | |
1746 | }, | |
1747 | { | |
1748 | .handler = handle_query_first_threads, | |
1749 | .cmd = "fThreadInfo", | |
1750 | }, | |
1751 | { | |
1752 | .handler = handle_query_thread_extra, | |
1753 | .cmd = "ThreadExtraInfo,", | |
1754 | .cmd_startswith = 1, | |
1755 | .schema = "t0" | |
1756 | }, | |
1757 | #ifdef CONFIG_USER_ONLY | |
1758 | { | |
d96bf49b | 1759 | .handler = gdb_handle_query_offsets, |
2704efad JD |
1760 | .cmd = "Offsets", |
1761 | }, | |
1762 | #else | |
1763 | { | |
b6fa2ec2 | 1764 | .handler = gdb_handle_query_rcmd, |
2704efad JD |
1765 | .cmd = "Rcmd,", |
1766 | .cmd_startswith = 1, | |
1767 | .schema = "s0" | |
1768 | }, | |
1769 | #endif | |
1770 | { | |
1771 | .handler = handle_query_supported, | |
1772 | .cmd = "Supported:", | |
1773 | .cmd_startswith = 1, | |
1774 | .schema = "s0" | |
1775 | }, | |
1776 | { | |
1777 | .handler = handle_query_supported, | |
1778 | .cmd = "Supported", | |
1779 | .schema = "s0" | |
1780 | }, | |
1781 | { | |
1782 | .handler = handle_query_xfer_features, | |
1783 | .cmd = "Xfer:features:read:", | |
1784 | .cmd_startswith = 1, | |
1785 | .schema = "s:l,l0" | |
1786 | }, | |
51c623b0 LY |
1787 | #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER) |
1788 | { | |
d96bf49b | 1789 | .handler = gdb_handle_query_xfer_auxv, |
51c623b0 LY |
1790 | .cmd = "Xfer:auxv:read::", |
1791 | .cmd_startswith = 1, | |
1792 | .schema = "l,l0" | |
1793 | }, | |
1794 | #endif | |
2704efad JD |
1795 | { |
1796 | .handler = handle_query_attached, | |
1797 | .cmd = "Attached:", | |
1798 | .cmd_startswith = 1 | |
1799 | }, | |
1800 | { | |
1801 | .handler = handle_query_attached, | |
1802 | .cmd = "Attached", | |
1803 | }, | |
1804 | { | |
1805 | .handler = handle_query_qemu_supported, | |
1806 | .cmd = "qemu.Supported", | |
1807 | }, | |
ab4752ec JD |
1808 | #ifndef CONFIG_USER_ONLY |
1809 | { | |
1810 | .handler = handle_query_qemu_phy_mem_mode, | |
1811 | .cmd = "qemu.PhyMemMode", | |
1812 | }, | |
1813 | #endif | |
2704efad JD |
1814 | }; |
1815 | ||
305bea06 | 1816 | static const GdbCmdParseEntry gdb_gen_set_table[] = { |
2704efad JD |
1817 | /* Order is important if has same prefix */ |
1818 | { | |
1819 | .handler = handle_set_qemu_sstep, | |
1820 | .cmd = "qemu.sstep:", | |
1821 | .cmd_startswith = 1, | |
1822 | .schema = "l0" | |
1823 | }, | |
ab4752ec JD |
1824 | #ifndef CONFIG_USER_ONLY |
1825 | { | |
1826 | .handler = handle_set_qemu_phy_mem_mode, | |
1827 | .cmd = "qemu.PhyMemMode:", | |
1828 | .cmd_startswith = 1, | |
1829 | .schema = "l0" | |
1830 | }, | |
1831 | #endif | |
2704efad JD |
1832 | }; |
1833 | ||
26a16181 | 1834 | static void handle_gen_query(GArray *params, void *user_ctx) |
2704efad | 1835 | { |
26a16181 | 1836 | if (!params->len) { |
2704efad JD |
1837 | return; |
1838 | } | |
1839 | ||
26a16181 | 1840 | if (!process_string_cmd(NULL, get_param(params, 0)->data, |
2704efad JD |
1841 | gdb_gen_query_set_common_table, |
1842 | ARRAY_SIZE(gdb_gen_query_set_common_table))) { | |
1843 | return; | |
1844 | } | |
1845 | ||
26a16181 | 1846 | if (process_string_cmd(NULL, get_param(params, 0)->data, |
2704efad JD |
1847 | gdb_gen_query_table, |
1848 | ARRAY_SIZE(gdb_gen_query_table))) { | |
36e067b2 | 1849 | gdb_put_packet(""); |
2704efad JD |
1850 | } |
1851 | } | |
1852 | ||
26a16181 | 1853 | static void handle_gen_set(GArray *params, void *user_ctx) |
2704efad | 1854 | { |
26a16181 | 1855 | if (!params->len) { |
2704efad JD |
1856 | return; |
1857 | } | |
1858 | ||
26a16181 | 1859 | if (!process_string_cmd(NULL, get_param(params, 0)->data, |
2704efad JD |
1860 | gdb_gen_query_set_common_table, |
1861 | ARRAY_SIZE(gdb_gen_query_set_common_table))) { | |
1862 | return; | |
1863 | } | |
1864 | ||
26a16181 | 1865 | if (process_string_cmd(NULL, get_param(params, 0)->data, |
2704efad JD |
1866 | gdb_gen_set_table, |
1867 | ARRAY_SIZE(gdb_gen_set_table))) { | |
36e067b2 | 1868 | gdb_put_packet(""); |
2704efad JD |
1869 | } |
1870 | } | |
1871 | ||
26a16181 | 1872 | static void handle_target_halt(GArray *params, void *user_ctx) |
7009d579 | 1873 | { |
308f9e88 AB |
1874 | g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP); |
1875 | gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf); | |
1876 | g_string_append_c(gdbserver_state.str_buf, ';'); | |
36e067b2 | 1877 | gdb_put_strbuf(); |
7009d579 JD |
1878 | /* |
1879 | * Remove all the breakpoints when this query is issued, | |
1880 | * because gdb is doing an initial connect and the state | |
1881 | * should be cleaned up. | |
1882 | */ | |
ae7467b1 | 1883 | gdb_breakpoint_remove_all(gdbserver_state.c_cpu); |
7009d579 JD |
1884 | } |
1885 | ||
a346af3e | 1886 | static int gdb_handle_packet(const char *line_buf) |
2704efad | 1887 | { |
3e2c1261 | 1888 | const GdbCmdParseEntry *cmd_parser = NULL; |
3b46e624 | 1889 | |
5c9522b3 | 1890 | trace_gdbstub_io_command(line_buf); |
118e2268 | 1891 | |
3f1cbac7 | 1892 | switch (line_buf[0]) { |
53fd6554 | 1893 | case '!': |
36e067b2 | 1894 | gdb_put_packet("OK"); |
53fd6554 | 1895 | break; |
858693c6 | 1896 | case '?': |
7009d579 JD |
1897 | { |
1898 | static const GdbCmdParseEntry target_halted_cmd_desc = { | |
1899 | .handler = handle_target_halt, | |
1900 | .cmd = "?", | |
1901 | .cmd_startswith = 1 | |
1902 | }; | |
1903 | cmd_parser = &target_halted_cmd_desc; | |
1904 | } | |
858693c6 FB |
1905 | break; |
1906 | case 'c': | |
4d6e3fe2 JD |
1907 | { |
1908 | static const GdbCmdParseEntry continue_cmd_desc = { | |
1909 | .handler = handle_continue, | |
1910 | .cmd = "c", | |
1911 | .cmd_startswith = 1, | |
1912 | .schema = "L0" | |
1913 | }; | |
1914 | cmd_parser = &continue_cmd_desc; | |
858693c6 | 1915 | } |
4d6e3fe2 | 1916 | break; |
1f487ee9 | 1917 | case 'C': |
ccc47d5d JD |
1918 | { |
1919 | static const GdbCmdParseEntry cont_with_sig_cmd_desc = { | |
1920 | .handler = handle_cont_with_sig, | |
1921 | .cmd = "C", | |
1922 | .cmd_startswith = 1, | |
1923 | .schema = "l0" | |
1924 | }; | |
1925 | cmd_parser = &cont_with_sig_cmd_desc; | |
1926 | } | |
1927 | break; | |
dd32aa10 | 1928 | case 'v': |
8536ec02 JD |
1929 | { |
1930 | static const GdbCmdParseEntry v_cmd_desc = { | |
1931 | .handler = handle_v_commands, | |
1932 | .cmd = "v", | |
1933 | .cmd_startswith = 1, | |
1934 | .schema = "s0" | |
1935 | }; | |
1936 | cmd_parser = &v_cmd_desc; | |
dd32aa10 | 1937 | } |
8536ec02 | 1938 | break; |
7d03f82f EI |
1939 | case 'k': |
1940 | /* Kill the target */ | |
7ae6c571 | 1941 | error_report("QEMU: Terminated via GDBstub"); |
b9e10c6c | 1942 | gdb_exit(0); |
7d03f82f EI |
1943 | exit(0); |
1944 | case 'D': | |
3e2c1261 JD |
1945 | { |
1946 | static const GdbCmdParseEntry detach_cmd_desc = { | |
1947 | .handler = handle_detach, | |
1948 | .cmd = "D", | |
1949 | .cmd_startswith = 1, | |
1950 | .schema = "?.l0" | |
1951 | }; | |
1952 | cmd_parser = &detach_cmd_desc; | |
546f3c67 | 1953 | } |
7d03f82f | 1954 | break; |
858693c6 | 1955 | case 's': |
933f80dd JD |
1956 | { |
1957 | static const GdbCmdParseEntry step_cmd_desc = { | |
1958 | .handler = handle_step, | |
1959 | .cmd = "s", | |
1960 | .cmd_startswith = 1, | |
1961 | .schema = "L0" | |
1962 | }; | |
1963 | cmd_parser = &step_cmd_desc; | |
858693c6 | 1964 | } |
933f80dd | 1965 | break; |
fda8458b PD |
1966 | case 'b': |
1967 | { | |
1968 | static const GdbCmdParseEntry backward_cmd_desc = { | |
1969 | .handler = handle_backward, | |
1970 | .cmd = "b", | |
1971 | .cmd_startswith = 1, | |
1972 | .schema = "o0" | |
1973 | }; | |
1974 | cmd_parser = &backward_cmd_desc; | |
1975 | } | |
1976 | break; | |
a2d1ebaf PB |
1977 | case 'F': |
1978 | { | |
4b20fab1 JD |
1979 | static const GdbCmdParseEntry file_io_cmd_desc = { |
1980 | .handler = handle_file_io, | |
1981 | .cmd = "F", | |
1982 | .cmd_startswith = 1, | |
1983 | .schema = "L,L,o0" | |
1984 | }; | |
1985 | cmd_parser = &file_io_cmd_desc; | |
a2d1ebaf PB |
1986 | } |
1987 | break; | |
858693c6 | 1988 | case 'g': |
397d1370 JD |
1989 | { |
1990 | static const GdbCmdParseEntry read_all_regs_cmd_desc = { | |
1991 | .handler = handle_read_all_regs, | |
1992 | .cmd = "g", | |
1993 | .cmd_startswith = 1 | |
1994 | }; | |
1995 | cmd_parser = &read_all_regs_cmd_desc; | |
56aebc89 | 1996 | } |
858693c6 FB |
1997 | break; |
1998 | case 'G': | |
287ca120 JD |
1999 | { |
2000 | static const GdbCmdParseEntry write_all_regs_cmd_desc = { | |
2001 | .handler = handle_write_all_regs, | |
2002 | .cmd = "G", | |
2003 | .cmd_startswith = 1, | |
2004 | .schema = "s0" | |
2005 | }; | |
2006 | cmd_parser = &write_all_regs_cmd_desc; | |
56aebc89 | 2007 | } |
858693c6 FB |
2008 | break; |
2009 | case 'm': | |
da92e236 JD |
2010 | { |
2011 | static const GdbCmdParseEntry read_mem_cmd_desc = { | |
2012 | .handler = handle_read_mem, | |
2013 | .cmd = "m", | |
2014 | .cmd_startswith = 1, | |
2015 | .schema = "L,L0" | |
2016 | }; | |
2017 | cmd_parser = &read_mem_cmd_desc; | |
6f970bd9 | 2018 | } |
858693c6 FB |
2019 | break; |
2020 | case 'M': | |
cc0ecc78 JD |
2021 | { |
2022 | static const GdbCmdParseEntry write_mem_cmd_desc = { | |
2023 | .handler = handle_write_mem, | |
2024 | .cmd = "M", | |
2025 | .cmd_startswith = 1, | |
2026 | .schema = "L,L:s0" | |
2027 | }; | |
2028 | cmd_parser = &write_mem_cmd_desc; | |
44520db1 | 2029 | } |
858693c6 | 2030 | break; |
56aebc89 | 2031 | case 'p': |
5d0e57bd JD |
2032 | { |
2033 | static const GdbCmdParseEntry get_reg_cmd_desc = { | |
2034 | .handler = handle_get_reg, | |
2035 | .cmd = "p", | |
2036 | .cmd_startswith = 1, | |
2037 | .schema = "L0" | |
2038 | }; | |
2039 | cmd_parser = &get_reg_cmd_desc; | |
56aebc89 PB |
2040 | } |
2041 | break; | |
2042 | case 'P': | |
62b3320b JD |
2043 | { |
2044 | static const GdbCmdParseEntry set_reg_cmd_desc = { | |
2045 | .handler = handle_set_reg, | |
2046 | .cmd = "P", | |
2047 | .cmd_startswith = 1, | |
2048 | .schema = "L?s0" | |
2049 | }; | |
2050 | cmd_parser = &set_reg_cmd_desc; | |
2051 | } | |
56aebc89 | 2052 | break; |
858693c6 | 2053 | case 'Z': |
77f6ce50 JD |
2054 | { |
2055 | static const GdbCmdParseEntry insert_bp_cmd_desc = { | |
2056 | .handler = handle_insert_bp, | |
2057 | .cmd = "Z", | |
2058 | .cmd_startswith = 1, | |
2059 | .schema = "l?L?L0" | |
2060 | }; | |
2061 | cmd_parser = &insert_bp_cmd_desc; | |
2062 | } | |
2063 | break; | |
858693c6 | 2064 | case 'z': |
77f6ce50 JD |
2065 | { |
2066 | static const GdbCmdParseEntry remove_bp_cmd_desc = { | |
2067 | .handler = handle_remove_bp, | |
2068 | .cmd = "z", | |
2069 | .cmd_startswith = 1, | |
2070 | .schema = "l?L?L0" | |
2071 | }; | |
2072 | cmd_parser = &remove_bp_cmd_desc; | |
2073 | } | |
858693c6 | 2074 | break; |
880a7578 | 2075 | case 'H': |
3a9651d6 JD |
2076 | { |
2077 | static const GdbCmdParseEntry set_thread_cmd_desc = { | |
2078 | .handler = handle_set_thread, | |
2079 | .cmd = "H", | |
2080 | .cmd_startswith = 1, | |
2081 | .schema = "o.t0" | |
2082 | }; | |
2083 | cmd_parser = &set_thread_cmd_desc; | |
880a7578 AL |
2084 | } |
2085 | break; | |
2086 | case 'T': | |
44ffded0 JD |
2087 | { |
2088 | static const GdbCmdParseEntry thread_alive_cmd_desc = { | |
2089 | .handler = handle_thread_alive, | |
2090 | .cmd = "T", | |
2091 | .cmd_startswith = 1, | |
2092 | .schema = "t0" | |
2093 | }; | |
2094 | cmd_parser = &thread_alive_cmd_desc; | |
1e9fa730 | 2095 | } |
880a7578 | 2096 | break; |
978efd6a | 2097 | case 'q': |
2704efad JD |
2098 | { |
2099 | static const GdbCmdParseEntry gen_query_cmd_desc = { | |
2100 | .handler = handle_gen_query, | |
2101 | .cmd = "q", | |
2102 | .cmd_startswith = 1, | |
2103 | .schema = "s0" | |
2104 | }; | |
2105 | cmd_parser = &gen_query_cmd_desc; | |
56aebc89 | 2106 | } |
2704efad JD |
2107 | break; |
2108 | case 'Q': | |
2109 | { | |
2110 | static const GdbCmdParseEntry gen_set_cmd_desc = { | |
2111 | .handler = handle_gen_set, | |
2112 | .cmd = "Q", | |
2113 | .cmd_startswith = 1, | |
2114 | .schema = "s0" | |
2115 | }; | |
2116 | cmd_parser = &gen_set_cmd_desc; | |
a3919386 | 2117 | } |
2704efad | 2118 | break; |
858693c6 | 2119 | default: |
858693c6 | 2120 | /* put empty packet */ |
36e067b2 | 2121 | gdb_put_packet(""); |
858693c6 FB |
2122 | break; |
2123 | } | |
3e2c1261 | 2124 | |
2bdec398 | 2125 | if (cmd_parser) { |
a346af3e | 2126 | run_cmd_parser(line_buf, cmd_parser); |
2bdec398 | 2127 | } |
3e2c1261 | 2128 | |
858693c6 FB |
2129 | return RS_IDLE; |
2130 | } | |
2131 | ||
64f6b346 | 2132 | void gdb_set_stop_cpu(CPUState *cpu) |
880a7578 | 2133 | { |
a346af3e | 2134 | GDBProcess *p = gdb_get_cpu_process(cpu); |
160d858d LM |
2135 | |
2136 | if (!p->attached) { | |
2137 | /* | |
2138 | * Having a stop CPU corresponding to a process that is not attached | |
2139 | * confuses GDB. So we ignore the request. | |
2140 | */ | |
2141 | return; | |
2142 | } | |
2143 | ||
8d98c445 AB |
2144 | gdbserver_state.c_cpu = cpu; |
2145 | gdbserver_state.g_cpu = cpu; | |
880a7578 AL |
2146 | } |
2147 | ||
a2d1ebaf PB |
2148 | /* Send a gdb syscall request. |
2149 | This accepts limited printf-style format specifiers, specifically: | |
a87295e8 PB |
2150 | %x - target_ulong argument printed in hex. |
2151 | %lx - 64-bit argument printed in hex. | |
2152 | %s - string pointer (target_ulong) and length (int) pair. */ | |
19239b39 | 2153 | void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va) |
a2d1ebaf | 2154 | { |
a2d1ebaf | 2155 | char *p; |
cdb432b2 | 2156 | char *p_end; |
a2d1ebaf | 2157 | target_ulong addr; |
a87295e8 | 2158 | uint64_t i64; |
a2d1ebaf | 2159 | |
ebf1b4cb | 2160 | if (!gdb_attached()) { |
a2d1ebaf | 2161 | return; |
a346af3e | 2162 | } |
8d98c445 AB |
2163 | |
2164 | gdbserver_state.current_syscall_cb = cb; | |
a2d1ebaf | 2165 | #ifndef CONFIG_USER_ONLY |
0461d5a6 | 2166 | vm_stop(RUN_STATE_DEBUG); |
a2d1ebaf | 2167 | #endif |
8d98c445 AB |
2168 | p = &gdbserver_state.syscall_buf[0]; |
2169 | p_end = &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_buf)]; | |
a2d1ebaf PB |
2170 | *(p++) = 'F'; |
2171 | while (*fmt) { | |
2172 | if (*fmt == '%') { | |
2173 | fmt++; | |
2174 | switch (*fmt++) { | |
2175 | case 'x': | |
2176 | addr = va_arg(va, target_ulong); | |
cdb432b2 | 2177 | p += snprintf(p, p_end - p, TARGET_FMT_lx, addr); |
a2d1ebaf | 2178 | break; |
a87295e8 PB |
2179 | case 'l': |
2180 | if (*(fmt++) != 'x') | |
2181 | goto bad_format; | |
2182 | i64 = va_arg(va, uint64_t); | |
cdb432b2 | 2183 | p += snprintf(p, p_end - p, "%" PRIx64, i64); |
a87295e8 | 2184 | break; |
a2d1ebaf PB |
2185 | case 's': |
2186 | addr = va_arg(va, target_ulong); | |
cdb432b2 | 2187 | p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x", |
363a37d5 | 2188 | addr, va_arg(va, int)); |
a2d1ebaf PB |
2189 | break; |
2190 | default: | |
a87295e8 | 2191 | bad_format: |
7ae6c571 ZY |
2192 | error_report("gdbstub: Bad syscall format string '%s'", |
2193 | fmt - 1); | |
a2d1ebaf PB |
2194 | break; |
2195 | } | |
2196 | } else { | |
2197 | *(p++) = *(fmt++); | |
2198 | } | |
2199 | } | |
8a93e02a | 2200 | *p = 0; |
a2d1ebaf | 2201 | #ifdef CONFIG_USER_ONLY |
36e067b2 | 2202 | gdb_put_packet(gdbserver_state.syscall_buf); |
4f710866 PM |
2203 | /* Return control to gdb for it to process the syscall request. |
2204 | * Since the protocol requires that gdb hands control back to us | |
2205 | * using a "here are the results" F packet, we don't need to check | |
2206 | * gdb_handlesig's return value (which is the signal to deliver if | |
2207 | * execution was resumed via a continue packet). | |
2208 | */ | |
8d98c445 | 2209 | gdb_handlesig(gdbserver_state.c_cpu, 0); |
a2d1ebaf | 2210 | #else |
cdb432b2 MI |
2211 | /* In this case wait to send the syscall packet until notification that |
2212 | the CPU has stopped. This must be done because if the packet is sent | |
2213 | now the reply from the syscall request could be received while the CPU | |
2214 | is still in the running state, which can cause packets to be dropped | |
2215 | and state transition 'T' packets to be sent while the syscall is still | |
2216 | being processed. */ | |
8d98c445 | 2217 | qemu_cpu_kick(gdbserver_state.c_cpu); |
a2d1ebaf PB |
2218 | #endif |
2219 | } | |
2220 | ||
19239b39 PM |
2221 | void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...) |
2222 | { | |
2223 | va_list va; | |
2224 | ||
2225 | va_start(va, fmt); | |
2226 | gdb_do_syscallv(cb, fmt, va); | |
2227 | va_end(va); | |
2228 | } | |
2229 | ||
36e067b2 | 2230 | void gdb_read_byte(uint8_t ch) |
858693c6 | 2231 | { |
60fe76f3 | 2232 | uint8_t reply; |
858693c6 | 2233 | |
1fddef4b | 2234 | #ifndef CONFIG_USER_ONLY |
d116e813 | 2235 | if (gdbserver_state.last_packet->len) { |
4046d913 PB |
2236 | /* Waiting for a response to the last packet. If we see the start |
2237 | of a new command then abandon the previous response. */ | |
2238 | if (ch == '-') { | |
5c9522b3 | 2239 | trace_gdbstub_err_got_nack(); |
36e067b2 | 2240 | gdb_put_buffer(gdbserver_state.last_packet->data, |
d116e813 | 2241 | gdbserver_state.last_packet->len); |
118e2268 | 2242 | } else if (ch == '+') { |
5c9522b3 | 2243 | trace_gdbstub_io_got_ack(); |
118e2268 | 2244 | } else { |
33c846ef | 2245 | trace_gdbstub_io_got_unexpected(ch); |
4046d913 | 2246 | } |
118e2268 | 2247 | |
d116e813 DH |
2248 | if (ch == '+' || ch == '$') { |
2249 | g_byte_array_set_size(gdbserver_state.last_packet, 0); | |
2250 | } | |
4046d913 PB |
2251 | if (ch != '$') |
2252 | return; | |
2253 | } | |
1354869c | 2254 | if (runstate_is_running()) { |
858693c6 FB |
2255 | /* when the CPU is running, we cannot do anything except stop |
2256 | it when receiving a char */ | |
0461d5a6 | 2257 | vm_stop(RUN_STATE_PAUSED); |
5fafdf24 | 2258 | } else |
1fddef4b | 2259 | #endif |
41625033 | 2260 | { |
a346af3e | 2261 | switch(gdbserver_state.state) { |
858693c6 FB |
2262 | case RS_IDLE: |
2263 | if (ch == '$') { | |
4bf43122 | 2264 | /* start of command packet */ |
a346af3e AB |
2265 | gdbserver_state.line_buf_index = 0; |
2266 | gdbserver_state.line_sum = 0; | |
2267 | gdbserver_state.state = RS_GETLINE; | |
4bf43122 | 2268 | } else { |
33c846ef | 2269 | trace_gdbstub_err_garbage(ch); |
c33a346e | 2270 | } |
b4608c04 | 2271 | break; |
858693c6 | 2272 | case RS_GETLINE: |
4bf43122 DG |
2273 | if (ch == '}') { |
2274 | /* start escape sequence */ | |
a346af3e AB |
2275 | gdbserver_state.state = RS_GETLINE_ESC; |
2276 | gdbserver_state.line_sum += ch; | |
4bf43122 DG |
2277 | } else if (ch == '*') { |
2278 | /* start run length encoding sequence */ | |
a346af3e AB |
2279 | gdbserver_state.state = RS_GETLINE_RLE; |
2280 | gdbserver_state.line_sum += ch; | |
4bf43122 DG |
2281 | } else if (ch == '#') { |
2282 | /* end of command, start of checksum*/ | |
a346af3e AB |
2283 | gdbserver_state.state = RS_CHKSUM1; |
2284 | } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) { | |
5c9522b3 | 2285 | trace_gdbstub_err_overrun(); |
a346af3e | 2286 | gdbserver_state.state = RS_IDLE; |
4bf43122 DG |
2287 | } else { |
2288 | /* unescaped command character */ | |
a346af3e AB |
2289 | gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch; |
2290 | gdbserver_state.line_sum += ch; | |
4bf43122 DG |
2291 | } |
2292 | break; | |
2293 | case RS_GETLINE_ESC: | |
858693c6 | 2294 | if (ch == '#') { |
4bf43122 | 2295 | /* unexpected end of command in escape sequence */ |
a346af3e AB |
2296 | gdbserver_state.state = RS_CHKSUM1; |
2297 | } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) { | |
4bf43122 | 2298 | /* command buffer overrun */ |
5c9522b3 | 2299 | trace_gdbstub_err_overrun(); |
a346af3e | 2300 | gdbserver_state.state = RS_IDLE; |
4c3a88a2 | 2301 | } else { |
4bf43122 | 2302 | /* parse escaped character and leave escape state */ |
a346af3e AB |
2303 | gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20; |
2304 | gdbserver_state.line_sum += ch; | |
2305 | gdbserver_state.state = RS_GETLINE; | |
4bf43122 DG |
2306 | } |
2307 | break; | |
2308 | case RS_GETLINE_RLE: | |
046aba16 MA |
2309 | /* |
2310 | * Run-length encoding is explained in "Debugging with GDB / | |
2311 | * Appendix E GDB Remote Serial Protocol / Overview". | |
2312 | */ | |
2313 | if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) { | |
4bf43122 | 2314 | /* invalid RLE count encoding */ |
33c846ef | 2315 | trace_gdbstub_err_invalid_repeat(ch); |
a346af3e | 2316 | gdbserver_state.state = RS_GETLINE; |
4bf43122 DG |
2317 | } else { |
2318 | /* decode repeat length */ | |
33c846ef | 2319 | int repeat = ch - ' ' + 3; |
a346af3e | 2320 | if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) { |
4bf43122 | 2321 | /* that many repeats would overrun the command buffer */ |
5c9522b3 | 2322 | trace_gdbstub_err_overrun(); |
a346af3e AB |
2323 | gdbserver_state.state = RS_IDLE; |
2324 | } else if (gdbserver_state.line_buf_index < 1) { | |
4bf43122 | 2325 | /* got a repeat but we have nothing to repeat */ |
5c9522b3 | 2326 | trace_gdbstub_err_invalid_rle(); |
a346af3e | 2327 | gdbserver_state.state = RS_GETLINE; |
4bf43122 DG |
2328 | } else { |
2329 | /* repeat the last character */ | |
a346af3e AB |
2330 | memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index, |
2331 | gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat); | |
2332 | gdbserver_state.line_buf_index += repeat; | |
2333 | gdbserver_state.line_sum += ch; | |
2334 | gdbserver_state.state = RS_GETLINE; | |
4bf43122 | 2335 | } |
4c3a88a2 FB |
2336 | } |
2337 | break; | |
858693c6 | 2338 | case RS_CHKSUM1: |
4bf43122 DG |
2339 | /* get high hex digit of checksum */ |
2340 | if (!isxdigit(ch)) { | |
33c846ef | 2341 | trace_gdbstub_err_checksum_invalid(ch); |
a346af3e | 2342 | gdbserver_state.state = RS_GETLINE; |
4bf43122 DG |
2343 | break; |
2344 | } | |
a346af3e AB |
2345 | gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0'; |
2346 | gdbserver_state.line_csum = fromhex(ch) << 4; | |
2347 | gdbserver_state.state = RS_CHKSUM2; | |
858693c6 FB |
2348 | break; |
2349 | case RS_CHKSUM2: | |
4bf43122 DG |
2350 | /* get low hex digit of checksum */ |
2351 | if (!isxdigit(ch)) { | |
33c846ef | 2352 | trace_gdbstub_err_checksum_invalid(ch); |
a346af3e | 2353 | gdbserver_state.state = RS_GETLINE; |
4bf43122 | 2354 | break; |
858693c6 | 2355 | } |
a346af3e | 2356 | gdbserver_state.line_csum |= fromhex(ch); |
4bf43122 | 2357 | |
a346af3e AB |
2358 | if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) { |
2359 | trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum); | |
4bf43122 | 2360 | /* send NAK reply */ |
60fe76f3 | 2361 | reply = '-'; |
36e067b2 | 2362 | gdb_put_buffer(&reply, 1); |
a346af3e | 2363 | gdbserver_state.state = RS_IDLE; |
4c3a88a2 | 2364 | } else { |
4bf43122 | 2365 | /* send ACK reply */ |
60fe76f3 | 2366 | reply = '+'; |
36e067b2 | 2367 | gdb_put_buffer(&reply, 1); |
a346af3e | 2368 | gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf); |
4c3a88a2 FB |
2369 | } |
2370 | break; | |
a2d1ebaf PB |
2371 | default: |
2372 | abort(); | |
858693c6 FB |
2373 | } |
2374 | } | |
2375 | } | |
2376 | ||
8f468636 LM |
2377 | /* |
2378 | * Create the process that will contain all the "orphan" CPUs (that are not | |
2379 | * part of a CPU cluster). Note that if this process contains no CPUs, it won't | |
2380 | * be attachable and thus will be invisible to the user. | |
2381 | */ | |
36e067b2 | 2382 | void gdb_create_default_process(GDBState *s) |
8f468636 LM |
2383 | { |
2384 | GDBProcess *process; | |
2385 | int max_pid = 0; | |
2386 | ||
a346af3e | 2387 | if (gdbserver_state.process_num) { |
8f468636 LM |
2388 | max_pid = s->processes[s->process_num - 1].pid; |
2389 | } | |
2390 | ||
2391 | s->processes = g_renew(GDBProcess, s->processes, ++s->process_num); | |
2392 | process = &s->processes[s->process_num - 1]; | |
2393 | ||
2394 | /* We need an available PID slot for this process */ | |
2395 | assert(max_pid < UINT32_MAX); | |
2396 | ||
2397 | process->pid = max_pid + 1; | |
2398 | process->attached = false; | |
c145eeae | 2399 | process->target_xml[0] = '\0'; |
8f468636 LM |
2400 | } |
2401 |