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