root / gdbstub.c @ 2f937732
History | View | Annotate | Download (54 kB)
1 |
/*
|
---|---|
2 |
* gdb server stub
|
3 |
*
|
4 |
* Copyright (c) 2003-2005 Fabrice Bellard
|
5 |
*
|
6 |
* This library is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU Lesser General Public
|
8 |
* License as published by the Free Software Foundation; either
|
9 |
* version 2 of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This library is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
* Lesser General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU Lesser General Public
|
17 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
18 |
*/
|
19 |
#include "config.h" |
20 |
#include "qemu-common.h" |
21 |
#ifdef CONFIG_USER_ONLY
|
22 |
#include <stdlib.h> |
23 |
#include <stdio.h> |
24 |
#include <stdarg.h> |
25 |
#include <string.h> |
26 |
#include <errno.h> |
27 |
#include <unistd.h> |
28 |
#include <fcntl.h> |
29 |
|
30 |
#include "qemu.h" |
31 |
#else
|
32 |
#include "monitor/monitor.h" |
33 |
#include "sysemu/char.h" |
34 |
#include "sysemu/sysemu.h" |
35 |
#include "exec/gdbstub.h" |
36 |
#endif
|
37 |
|
38 |
#define MAX_PACKET_LENGTH 4096 |
39 |
|
40 |
#include "cpu.h" |
41 |
#include "qemu/sockets.h" |
42 |
#include "sysemu/kvm.h" |
43 |
#include "qemu/bitops.h" |
44 |
|
45 |
static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr, |
46 |
uint8_t *buf, int len, bool is_write) |
47 |
{ |
48 |
CPUClass *cc = CPU_GET_CLASS(cpu); |
49 |
|
50 |
if (cc->memory_rw_debug) {
|
51 |
return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
|
52 |
} |
53 |
return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
|
54 |
} |
55 |
|
56 |
enum {
|
57 |
GDB_SIGNAL_0 = 0,
|
58 |
GDB_SIGNAL_INT = 2,
|
59 |
GDB_SIGNAL_QUIT = 3,
|
60 |
GDB_SIGNAL_TRAP = 5,
|
61 |
GDB_SIGNAL_ABRT = 6,
|
62 |
GDB_SIGNAL_ALRM = 14,
|
63 |
GDB_SIGNAL_IO = 23,
|
64 |
GDB_SIGNAL_XCPU = 24,
|
65 |
GDB_SIGNAL_UNKNOWN = 143
|
66 |
}; |
67 |
|
68 |
#ifdef CONFIG_USER_ONLY
|
69 |
|
70 |
/* Map target signal numbers to GDB protocol signal numbers and vice
|
71 |
* versa. For user emulation's currently supported systems, we can
|
72 |
* assume most signals are defined.
|
73 |
*/
|
74 |
|
75 |
static int gdb_signal_table[] = { |
76 |
0,
|
77 |
TARGET_SIGHUP, |
78 |
TARGET_SIGINT, |
79 |
TARGET_SIGQUIT, |
80 |
TARGET_SIGILL, |
81 |
TARGET_SIGTRAP, |
82 |
TARGET_SIGABRT, |
83 |
-1, /* SIGEMT */ |
84 |
TARGET_SIGFPE, |
85 |
TARGET_SIGKILL, |
86 |
TARGET_SIGBUS, |
87 |
TARGET_SIGSEGV, |
88 |
TARGET_SIGSYS, |
89 |
TARGET_SIGPIPE, |
90 |
TARGET_SIGALRM, |
91 |
TARGET_SIGTERM, |
92 |
TARGET_SIGURG, |
93 |
TARGET_SIGSTOP, |
94 |
TARGET_SIGTSTP, |
95 |
TARGET_SIGCONT, |
96 |
TARGET_SIGCHLD, |
97 |
TARGET_SIGTTIN, |
98 |
TARGET_SIGTTOU, |
99 |
TARGET_SIGIO, |
100 |
TARGET_SIGXCPU, |
101 |
TARGET_SIGXFSZ, |
102 |
TARGET_SIGVTALRM, |
103 |
TARGET_SIGPROF, |
104 |
TARGET_SIGWINCH, |
105 |
-1, /* SIGLOST */ |
106 |
TARGET_SIGUSR1, |
107 |
TARGET_SIGUSR2, |
108 |
#ifdef TARGET_SIGPWR
|
109 |
TARGET_SIGPWR, |
110 |
#else
|
111 |
-1,
|
112 |
#endif
|
113 |
-1, /* SIGPOLL */ |
114 |
-1,
|
115 |
-1,
|
116 |
-1,
|
117 |
-1,
|
118 |
-1,
|
119 |
-1,
|
120 |
-1,
|
121 |
-1,
|
122 |
-1,
|
123 |
-1,
|
124 |
-1,
|
125 |
#ifdef __SIGRTMIN
|
126 |
__SIGRTMIN + 1,
|
127 |
__SIGRTMIN + 2,
|
128 |
__SIGRTMIN + 3,
|
129 |
__SIGRTMIN + 4,
|
130 |
__SIGRTMIN + 5,
|
131 |
__SIGRTMIN + 6,
|
132 |
__SIGRTMIN + 7,
|
133 |
__SIGRTMIN + 8,
|
134 |
__SIGRTMIN + 9,
|
135 |
__SIGRTMIN + 10,
|
136 |
__SIGRTMIN + 11,
|
137 |
__SIGRTMIN + 12,
|
138 |
__SIGRTMIN + 13,
|
139 |
__SIGRTMIN + 14,
|
140 |
__SIGRTMIN + 15,
|
141 |
__SIGRTMIN + 16,
|
142 |
__SIGRTMIN + 17,
|
143 |
__SIGRTMIN + 18,
|
144 |
__SIGRTMIN + 19,
|
145 |
__SIGRTMIN + 20,
|
146 |
__SIGRTMIN + 21,
|
147 |
__SIGRTMIN + 22,
|
148 |
__SIGRTMIN + 23,
|
149 |
__SIGRTMIN + 24,
|
150 |
__SIGRTMIN + 25,
|
151 |
__SIGRTMIN + 26,
|
152 |
__SIGRTMIN + 27,
|
153 |
__SIGRTMIN + 28,
|
154 |
__SIGRTMIN + 29,
|
155 |
__SIGRTMIN + 30,
|
156 |
__SIGRTMIN + 31,
|
157 |
-1, /* SIGCANCEL */ |
158 |
__SIGRTMIN, |
159 |
__SIGRTMIN + 32,
|
160 |
__SIGRTMIN + 33,
|
161 |
__SIGRTMIN + 34,
|
162 |
__SIGRTMIN + 35,
|
163 |
__SIGRTMIN + 36,
|
164 |
__SIGRTMIN + 37,
|
165 |
__SIGRTMIN + 38,
|
166 |
__SIGRTMIN + 39,
|
167 |
__SIGRTMIN + 40,
|
168 |
__SIGRTMIN + 41,
|
169 |
__SIGRTMIN + 42,
|
170 |
__SIGRTMIN + 43,
|
171 |
__SIGRTMIN + 44,
|
172 |
__SIGRTMIN + 45,
|
173 |
__SIGRTMIN + 46,
|
174 |
__SIGRTMIN + 47,
|
175 |
__SIGRTMIN + 48,
|
176 |
__SIGRTMIN + 49,
|
177 |
__SIGRTMIN + 50,
|
178 |
__SIGRTMIN + 51,
|
179 |
__SIGRTMIN + 52,
|
180 |
__SIGRTMIN + 53,
|
181 |
__SIGRTMIN + 54,
|
182 |
__SIGRTMIN + 55,
|
183 |
__SIGRTMIN + 56,
|
184 |
__SIGRTMIN + 57,
|
185 |
__SIGRTMIN + 58,
|
186 |
__SIGRTMIN + 59,
|
187 |
__SIGRTMIN + 60,
|
188 |
__SIGRTMIN + 61,
|
189 |
__SIGRTMIN + 62,
|
190 |
__SIGRTMIN + 63,
|
191 |
__SIGRTMIN + 64,
|
192 |
__SIGRTMIN + 65,
|
193 |
__SIGRTMIN + 66,
|
194 |
__SIGRTMIN + 67,
|
195 |
__SIGRTMIN + 68,
|
196 |
__SIGRTMIN + 69,
|
197 |
__SIGRTMIN + 70,
|
198 |
__SIGRTMIN + 71,
|
199 |
__SIGRTMIN + 72,
|
200 |
__SIGRTMIN + 73,
|
201 |
__SIGRTMIN + 74,
|
202 |
__SIGRTMIN + 75,
|
203 |
__SIGRTMIN + 76,
|
204 |
__SIGRTMIN + 77,
|
205 |
__SIGRTMIN + 78,
|
206 |
__SIGRTMIN + 79,
|
207 |
__SIGRTMIN + 80,
|
208 |
__SIGRTMIN + 81,
|
209 |
__SIGRTMIN + 82,
|
210 |
__SIGRTMIN + 83,
|
211 |
__SIGRTMIN + 84,
|
212 |
__SIGRTMIN + 85,
|
213 |
__SIGRTMIN + 86,
|
214 |
__SIGRTMIN + 87,
|
215 |
__SIGRTMIN + 88,
|
216 |
__SIGRTMIN + 89,
|
217 |
__SIGRTMIN + 90,
|
218 |
__SIGRTMIN + 91,
|
219 |
__SIGRTMIN + 92,
|
220 |
__SIGRTMIN + 93,
|
221 |
__SIGRTMIN + 94,
|
222 |
__SIGRTMIN + 95,
|
223 |
-1, /* SIGINFO */ |
224 |
-1, /* UNKNOWN */ |
225 |
-1, /* DEFAULT */ |
226 |
-1,
|
227 |
-1,
|
228 |
-1,
|
229 |
-1,
|
230 |
-1,
|
231 |
-1
|
232 |
#endif
|
233 |
}; |
234 |
#else
|
235 |
/* In system mode we only need SIGINT and SIGTRAP; other signals
|
236 |
are not yet supported. */
|
237 |
|
238 |
enum {
|
239 |
TARGET_SIGINT = 2,
|
240 |
TARGET_SIGTRAP = 5
|
241 |
}; |
242 |
|
243 |
static int gdb_signal_table[] = { |
244 |
-1,
|
245 |
-1,
|
246 |
TARGET_SIGINT, |
247 |
-1,
|
248 |
-1,
|
249 |
TARGET_SIGTRAP |
250 |
}; |
251 |
#endif
|
252 |
|
253 |
#ifdef CONFIG_USER_ONLY
|
254 |
static int target_signal_to_gdb (int sig) |
255 |
{ |
256 |
int i;
|
257 |
for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++) |
258 |
if (gdb_signal_table[i] == sig)
|
259 |
return i;
|
260 |
return GDB_SIGNAL_UNKNOWN;
|
261 |
} |
262 |
#endif
|
263 |
|
264 |
static int gdb_signal_to_target (int sig) |
265 |
{ |
266 |
if (sig < ARRAY_SIZE (gdb_signal_table))
|
267 |
return gdb_signal_table[sig];
|
268 |
else
|
269 |
return -1; |
270 |
} |
271 |
|
272 |
//#define DEBUG_GDB
|
273 |
|
274 |
typedef struct GDBRegisterState { |
275 |
int base_reg;
|
276 |
int num_regs;
|
277 |
gdb_reg_cb get_reg; |
278 |
gdb_reg_cb set_reg; |
279 |
const char *xml; |
280 |
struct GDBRegisterState *next;
|
281 |
} GDBRegisterState; |
282 |
|
283 |
enum RSState {
|
284 |
RS_INACTIVE, |
285 |
RS_IDLE, |
286 |
RS_GETLINE, |
287 |
RS_CHKSUM1, |
288 |
RS_CHKSUM2, |
289 |
}; |
290 |
typedef struct GDBState { |
291 |
CPUState *c_cpu; /* current CPU for step/continue ops */
|
292 |
CPUState *g_cpu; /* current CPU for other ops */
|
293 |
CPUState *query_cpu; /* for q{f|s}ThreadInfo */
|
294 |
enum RSState state; /* parsing state */ |
295 |
char line_buf[MAX_PACKET_LENGTH];
|
296 |
int line_buf_index;
|
297 |
int line_csum;
|
298 |
uint8_t last_packet[MAX_PACKET_LENGTH + 4];
|
299 |
int last_packet_len;
|
300 |
int signal;
|
301 |
#ifdef CONFIG_USER_ONLY
|
302 |
int fd;
|
303 |
int running_state;
|
304 |
#else
|
305 |
CharDriverState *chr; |
306 |
CharDriverState *mon_chr; |
307 |
#endif
|
308 |
char syscall_buf[256]; |
309 |
gdb_syscall_complete_cb current_syscall_cb; |
310 |
} GDBState; |
311 |
|
312 |
/* By default use no IRQs and no timers while single stepping so as to
|
313 |
* make single stepping like an ICE HW step.
|
314 |
*/
|
315 |
static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER; |
316 |
|
317 |
static GDBState *gdbserver_state;
|
318 |
|
319 |
/* This is an ugly hack to cope with both new and old gdb.
|
320 |
If gdb sends qXfer:features:read then assume we're talking to a newish
|
321 |
gdb that understands target descriptions. */
|
322 |
static int gdb_has_xml; |
323 |
|
324 |
#ifdef CONFIG_USER_ONLY
|
325 |
/* XXX: This is not thread safe. Do we care? */
|
326 |
static int gdbserver_fd = -1; |
327 |
|
328 |
static int get_char(GDBState *s) |
329 |
{ |
330 |
uint8_t ch; |
331 |
int ret;
|
332 |
|
333 |
for(;;) {
|
334 |
ret = qemu_recv(s->fd, &ch, 1, 0); |
335 |
if (ret < 0) { |
336 |
if (errno == ECONNRESET)
|
337 |
s->fd = -1;
|
338 |
if (errno != EINTR && errno != EAGAIN)
|
339 |
return -1; |
340 |
} else if (ret == 0) { |
341 |
close(s->fd); |
342 |
s->fd = -1;
|
343 |
return -1; |
344 |
} else {
|
345 |
break;
|
346 |
} |
347 |
} |
348 |
return ch;
|
349 |
} |
350 |
#endif
|
351 |
|
352 |
static enum { |
353 |
GDB_SYS_UNKNOWN, |
354 |
GDB_SYS_ENABLED, |
355 |
GDB_SYS_DISABLED, |
356 |
} gdb_syscall_mode; |
357 |
|
358 |
/* If gdb is connected when the first semihosting syscall occurs then use
|
359 |
remote gdb syscalls. Otherwise use native file IO. */
|
360 |
int use_gdb_syscalls(void) |
361 |
{ |
362 |
if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
|
363 |
gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED |
364 |
: GDB_SYS_DISABLED); |
365 |
} |
366 |
return gdb_syscall_mode == GDB_SYS_ENABLED;
|
367 |
} |
368 |
|
369 |
/* Resume execution. */
|
370 |
static inline void gdb_continue(GDBState *s) |
371 |
{ |
372 |
#ifdef CONFIG_USER_ONLY
|
373 |
s->running_state = 1;
|
374 |
#else
|
375 |
if (runstate_check(RUN_STATE_GUEST_PANICKED)) {
|
376 |
runstate_set(RUN_STATE_DEBUG); |
377 |
} |
378 |
if (!runstate_needs_reset()) {
|
379 |
vm_start(); |
380 |
} |
381 |
#endif
|
382 |
} |
383 |
|
384 |
static void put_buffer(GDBState *s, const uint8_t *buf, int len) |
385 |
{ |
386 |
#ifdef CONFIG_USER_ONLY
|
387 |
int ret;
|
388 |
|
389 |
while (len > 0) { |
390 |
ret = send(s->fd, buf, len, 0);
|
391 |
if (ret < 0) { |
392 |
if (errno != EINTR && errno != EAGAIN)
|
393 |
return;
|
394 |
} else {
|
395 |
buf += ret; |
396 |
len -= ret; |
397 |
} |
398 |
} |
399 |
#else
|
400 |
qemu_chr_fe_write(s->chr, buf, len); |
401 |
#endif
|
402 |
} |
403 |
|
404 |
static inline int fromhex(int v) |
405 |
{ |
406 |
if (v >= '0' && v <= '9') |
407 |
return v - '0'; |
408 |
else if (v >= 'A' && v <= 'F') |
409 |
return v - 'A' + 10; |
410 |
else if (v >= 'a' && v <= 'f') |
411 |
return v - 'a' + 10; |
412 |
else
|
413 |
return 0; |
414 |
} |
415 |
|
416 |
static inline int tohex(int v) |
417 |
{ |
418 |
if (v < 10) |
419 |
return v + '0'; |
420 |
else
|
421 |
return v - 10 + 'a'; |
422 |
} |
423 |
|
424 |
static void memtohex(char *buf, const uint8_t *mem, int len) |
425 |
{ |
426 |
int i, c;
|
427 |
char *q;
|
428 |
q = buf; |
429 |
for(i = 0; i < len; i++) { |
430 |
c = mem[i]; |
431 |
*q++ = tohex(c >> 4);
|
432 |
*q++ = tohex(c & 0xf);
|
433 |
} |
434 |
*q = '\0';
|
435 |
} |
436 |
|
437 |
static void hextomem(uint8_t *mem, const char *buf, int len) |
438 |
{ |
439 |
int i;
|
440 |
|
441 |
for(i = 0; i < len; i++) { |
442 |
mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]); |
443 |
buf += 2;
|
444 |
} |
445 |
} |
446 |
|
447 |
/* return -1 if error, 0 if OK */
|
448 |
static int put_packet_binary(GDBState *s, const char *buf, int len) |
449 |
{ |
450 |
int csum, i;
|
451 |
uint8_t *p; |
452 |
|
453 |
for(;;) {
|
454 |
p = s->last_packet; |
455 |
*(p++) = '$';
|
456 |
memcpy(p, buf, len); |
457 |
p += len; |
458 |
csum = 0;
|
459 |
for(i = 0; i < len; i++) { |
460 |
csum += buf[i]; |
461 |
} |
462 |
*(p++) = '#';
|
463 |
*(p++) = tohex((csum >> 4) & 0xf); |
464 |
*(p++) = tohex((csum) & 0xf);
|
465 |
|
466 |
s->last_packet_len = p - s->last_packet; |
467 |
put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
468 |
|
469 |
#ifdef CONFIG_USER_ONLY
|
470 |
i = get_char(s); |
471 |
if (i < 0) |
472 |
return -1; |
473 |
if (i == '+') |
474 |
break;
|
475 |
#else
|
476 |
break;
|
477 |
#endif
|
478 |
} |
479 |
return 0; |
480 |
} |
481 |
|
482 |
/* return -1 if error, 0 if OK */
|
483 |
static int put_packet(GDBState *s, const char *buf) |
484 |
{ |
485 |
#ifdef DEBUG_GDB
|
486 |
printf("reply='%s'\n", buf);
|
487 |
#endif
|
488 |
|
489 |
return put_packet_binary(s, buf, strlen(buf));
|
490 |
} |
491 |
|
492 |
/* The GDB remote protocol transfers values in target byte order. This means
|
493 |
we can use the raw memory access routines to access the value buffer.
|
494 |
Conveniently, these also handle the case where the buffer is mis-aligned.
|
495 |
*/
|
496 |
#define GET_REG8(val) do { \ |
497 |
stb_p(mem_buf, val); \ |
498 |
return 1; \ |
499 |
} while(0) |
500 |
#define GET_REG16(val) do { \ |
501 |
stw_p(mem_buf, val); \ |
502 |
return 2; \ |
503 |
} while(0) |
504 |
#define GET_REG32(val) do { \ |
505 |
stl_p(mem_buf, val); \ |
506 |
return 4; \ |
507 |
} while(0) |
508 |
#define GET_REG64(val) do { \ |
509 |
stq_p(mem_buf, val); \ |
510 |
return 8; \ |
511 |
} while(0) |
512 |
|
513 |
#if TARGET_LONG_BITS == 64 |
514 |
#define GET_REGL(val) GET_REG64(val)
|
515 |
#define ldtul_p(addr) ldq_p(addr)
|
516 |
#else
|
517 |
#define GET_REGL(val) GET_REG32(val)
|
518 |
#define ldtul_p(addr) ldl_p(addr)
|
519 |
#endif
|
520 |
|
521 |
#if defined(TARGET_I386)
|
522 |
|
523 |
#include "target-i386/gdbstub.c" |
524 |
|
525 |
#elif defined (TARGET_PPC)
|
526 |
|
527 |
#if defined (TARGET_PPC64)
|
528 |
#define GDB_CORE_XML "power64-core.xml" |
529 |
#else
|
530 |
#define GDB_CORE_XML "power-core.xml" |
531 |
#endif
|
532 |
|
533 |
#include "target-ppc/gdbstub.c" |
534 |
|
535 |
#elif defined (TARGET_SPARC)
|
536 |
|
537 |
#include "target-sparc/gdbstub.c" |
538 |
|
539 |
#elif defined (TARGET_ARM)
|
540 |
|
541 |
#define GDB_CORE_XML "arm-core.xml" |
542 |
|
543 |
#include "target-arm/gdbstub.c" |
544 |
|
545 |
#elif defined (TARGET_M68K)
|
546 |
|
547 |
#define GDB_CORE_XML "cf-core.xml" |
548 |
|
549 |
#include "target-m68k/gdbstub.c" |
550 |
|
551 |
#elif defined (TARGET_MIPS)
|
552 |
|
553 |
#include "target-mips/gdbstub.c" |
554 |
|
555 |
#elif defined(TARGET_OPENRISC)
|
556 |
|
557 |
#include "target-openrisc/gdbstub.c" |
558 |
|
559 |
#elif defined (TARGET_SH4)
|
560 |
|
561 |
#include "target-sh4/gdbstub.c" |
562 |
|
563 |
#elif defined (TARGET_MICROBLAZE)
|
564 |
|
565 |
static int cpu_gdb_read_register(CPUMBState *env, uint8_t *mem_buf, int n) |
566 |
{ |
567 |
if (n < 32) { |
568 |
GET_REG32(env->regs[n]); |
569 |
} else {
|
570 |
GET_REG32(env->sregs[n - 32]);
|
571 |
} |
572 |
return 0; |
573 |
} |
574 |
|
575 |
static int cpu_gdb_write_register(CPUMBState *env, uint8_t *mem_buf, int n) |
576 |
{ |
577 |
MicroBlazeCPU *cpu = mb_env_get_cpu(env); |
578 |
CPUClass *cc = CPU_GET_CLASS(cpu); |
579 |
uint32_t tmp; |
580 |
|
581 |
if (n > cc->gdb_num_core_regs) {
|
582 |
return 0; |
583 |
} |
584 |
|
585 |
tmp = ldl_p(mem_buf); |
586 |
|
587 |
if (n < 32) { |
588 |
env->regs[n] = tmp; |
589 |
} else {
|
590 |
env->sregs[n - 32] = tmp;
|
591 |
} |
592 |
return 4; |
593 |
} |
594 |
#elif defined (TARGET_CRIS)
|
595 |
|
596 |
static int |
597 |
read_register_crisv10(CPUCRISState *env, uint8_t *mem_buf, int n)
|
598 |
{ |
599 |
if (n < 15) { |
600 |
GET_REG32(env->regs[n]); |
601 |
} |
602 |
|
603 |
if (n == 15) { |
604 |
GET_REG32(env->pc); |
605 |
} |
606 |
|
607 |
if (n < 32) { |
608 |
switch (n) {
|
609 |
case 16: |
610 |
GET_REG8(env->pregs[n - 16]);
|
611 |
case 17: |
612 |
GET_REG8(env->pregs[n - 16]);
|
613 |
case 20: |
614 |
case 21: |
615 |
GET_REG16(env->pregs[n - 16]);
|
616 |
default:
|
617 |
if (n >= 23) { |
618 |
GET_REG32(env->pregs[n - 16]);
|
619 |
} |
620 |
break;
|
621 |
} |
622 |
} |
623 |
return 0; |
624 |
} |
625 |
|
626 |
static int cpu_gdb_read_register(CPUCRISState *env, uint8_t *mem_buf, int n) |
627 |
{ |
628 |
uint8_t srs; |
629 |
|
630 |
if (env->pregs[PR_VR] < 32) { |
631 |
return read_register_crisv10(env, mem_buf, n);
|
632 |
} |
633 |
|
634 |
srs = env->pregs[PR_SRS]; |
635 |
if (n < 16) { |
636 |
GET_REG32(env->regs[n]); |
637 |
} |
638 |
|
639 |
if (n >= 21 && n < 32) { |
640 |
GET_REG32(env->pregs[n - 16]);
|
641 |
} |
642 |
if (n >= 33 && n < 49) { |
643 |
GET_REG32(env->sregs[srs][n - 33]);
|
644 |
} |
645 |
switch (n) {
|
646 |
case 16: |
647 |
GET_REG8(env->pregs[0]);
|
648 |
case 17: |
649 |
GET_REG8(env->pregs[1]);
|
650 |
case 18: |
651 |
GET_REG32(env->pregs[2]);
|
652 |
case 19: |
653 |
GET_REG8(srs); |
654 |
case 20: |
655 |
GET_REG16(env->pregs[4]);
|
656 |
case 32: |
657 |
GET_REG32(env->pc); |
658 |
} |
659 |
|
660 |
return 0; |
661 |
} |
662 |
|
663 |
static int cpu_gdb_write_register(CPUCRISState *env, uint8_t *mem_buf, int n) |
664 |
{ |
665 |
uint32_t tmp; |
666 |
|
667 |
if (n > 49) { |
668 |
return 0; |
669 |
} |
670 |
|
671 |
tmp = ldl_p(mem_buf); |
672 |
|
673 |
if (n < 16) { |
674 |
env->regs[n] = tmp; |
675 |
} |
676 |
|
677 |
if (n >= 21 && n < 32) { |
678 |
env->pregs[n - 16] = tmp;
|
679 |
} |
680 |
|
681 |
/* FIXME: Should support function regs be writable? */
|
682 |
switch (n) {
|
683 |
case 16: |
684 |
return 1; |
685 |
case 17: |
686 |
return 1; |
687 |
case 18: |
688 |
env->pregs[PR_PID] = tmp; |
689 |
break;
|
690 |
case 19: |
691 |
return 1; |
692 |
case 20: |
693 |
return 2; |
694 |
case 32: |
695 |
env->pc = tmp; |
696 |
break;
|
697 |
} |
698 |
|
699 |
return 4; |
700 |
} |
701 |
#elif defined (TARGET_ALPHA)
|
702 |
|
703 |
static int cpu_gdb_read_register(CPUAlphaState *env, uint8_t *mem_buf, int n) |
704 |
{ |
705 |
uint64_t val; |
706 |
CPU_DoubleU d; |
707 |
|
708 |
switch (n) {
|
709 |
case 0 ... 30: |
710 |
val = env->ir[n]; |
711 |
break;
|
712 |
case 32 ... 62: |
713 |
d.d = env->fir[n - 32];
|
714 |
val = d.ll; |
715 |
break;
|
716 |
case 63: |
717 |
val = cpu_alpha_load_fpcr(env); |
718 |
break;
|
719 |
case 64: |
720 |
val = env->pc; |
721 |
break;
|
722 |
case 66: |
723 |
val = env->unique; |
724 |
break;
|
725 |
case 31: |
726 |
case 65: |
727 |
/* 31 really is the zero register; 65 is unassigned in the
|
728 |
gdb protocol, but is still required to occupy 8 bytes. */
|
729 |
val = 0;
|
730 |
break;
|
731 |
default:
|
732 |
return 0; |
733 |
} |
734 |
GET_REGL(val); |
735 |
} |
736 |
|
737 |
static int cpu_gdb_write_register(CPUAlphaState *env, uint8_t *mem_buf, int n) |
738 |
{ |
739 |
target_ulong tmp = ldtul_p(mem_buf); |
740 |
CPU_DoubleU d; |
741 |
|
742 |
switch (n) {
|
743 |
case 0 ... 30: |
744 |
env->ir[n] = tmp; |
745 |
break;
|
746 |
case 32 ... 62: |
747 |
d.ll = tmp; |
748 |
env->fir[n - 32] = d.d;
|
749 |
break;
|
750 |
case 63: |
751 |
cpu_alpha_store_fpcr(env, tmp); |
752 |
break;
|
753 |
case 64: |
754 |
env->pc = tmp; |
755 |
break;
|
756 |
case 66: |
757 |
env->unique = tmp; |
758 |
break;
|
759 |
case 31: |
760 |
case 65: |
761 |
/* 31 really is the zero register; 65 is unassigned in the
|
762 |
gdb protocol, but is still required to occupy 8 bytes. */
|
763 |
break;
|
764 |
default:
|
765 |
return 0; |
766 |
} |
767 |
return 8; |
768 |
} |
769 |
#elif defined (TARGET_S390X)
|
770 |
|
771 |
static int cpu_gdb_read_register(CPUS390XState *env, uint8_t *mem_buf, int n) |
772 |
{ |
773 |
uint64_t val; |
774 |
int cc_op;
|
775 |
|
776 |
switch (n) {
|
777 |
case S390_PSWM_REGNUM:
|
778 |
cc_op = calc_cc(env, env->cc_op, env->cc_src, env->cc_dst, env->cc_vr); |
779 |
val = deposit64(env->psw.mask, 44, 2, cc_op); |
780 |
GET_REGL(val); |
781 |
case S390_PSWA_REGNUM:
|
782 |
GET_REGL(env->psw.addr); |
783 |
case S390_R0_REGNUM ... S390_R15_REGNUM:
|
784 |
GET_REGL(env->regs[n-S390_R0_REGNUM]); |
785 |
case S390_A0_REGNUM ... S390_A15_REGNUM:
|
786 |
GET_REG32(env->aregs[n-S390_A0_REGNUM]); |
787 |
case S390_FPC_REGNUM:
|
788 |
GET_REG32(env->fpc); |
789 |
case S390_F0_REGNUM ... S390_F15_REGNUM:
|
790 |
GET_REG64(env->fregs[n-S390_F0_REGNUM].ll); |
791 |
} |
792 |
|
793 |
return 0; |
794 |
} |
795 |
|
796 |
static int cpu_gdb_write_register(CPUS390XState *env, uint8_t *mem_buf, int n) |
797 |
{ |
798 |
target_ulong tmpl; |
799 |
uint32_t tmp32; |
800 |
int r = 8; |
801 |
tmpl = ldtul_p(mem_buf); |
802 |
tmp32 = ldl_p(mem_buf); |
803 |
|
804 |
switch (n) {
|
805 |
case S390_PSWM_REGNUM:
|
806 |
env->psw.mask = tmpl; |
807 |
env->cc_op = extract64(tmpl, 44, 2); |
808 |
break;
|
809 |
case S390_PSWA_REGNUM:
|
810 |
env->psw.addr = tmpl; |
811 |
break;
|
812 |
case S390_R0_REGNUM ... S390_R15_REGNUM:
|
813 |
env->regs[n-S390_R0_REGNUM] = tmpl; |
814 |
break;
|
815 |
case S390_A0_REGNUM ... S390_A15_REGNUM:
|
816 |
env->aregs[n-S390_A0_REGNUM] = tmp32; |
817 |
r = 4;
|
818 |
break;
|
819 |
case S390_FPC_REGNUM:
|
820 |
env->fpc = tmp32; |
821 |
r = 4;
|
822 |
break;
|
823 |
case S390_F0_REGNUM ... S390_F15_REGNUM:
|
824 |
env->fregs[n-S390_F0_REGNUM].ll = tmpl; |
825 |
break;
|
826 |
default:
|
827 |
return 0; |
828 |
} |
829 |
return r;
|
830 |
} |
831 |
#elif defined (TARGET_LM32)
|
832 |
|
833 |
#include "hw/lm32/lm32_pic.h" |
834 |
|
835 |
static int cpu_gdb_read_register(CPULM32State *env, uint8_t *mem_buf, int n) |
836 |
{ |
837 |
if (n < 32) { |
838 |
GET_REG32(env->regs[n]); |
839 |
} else {
|
840 |
switch (n) {
|
841 |
case 32: |
842 |
GET_REG32(env->pc); |
843 |
/* FIXME: put in right exception ID */
|
844 |
case 33: |
845 |
GET_REG32(0);
|
846 |
case 34: |
847 |
GET_REG32(env->eba); |
848 |
case 35: |
849 |
GET_REG32(env->deba); |
850 |
case 36: |
851 |
GET_REG32(env->ie); |
852 |
case 37: |
853 |
GET_REG32(lm32_pic_get_im(env->pic_state)); |
854 |
case 38: |
855 |
GET_REG32(lm32_pic_get_ip(env->pic_state)); |
856 |
} |
857 |
} |
858 |
return 0; |
859 |
} |
860 |
|
861 |
static int cpu_gdb_write_register(CPULM32State *env, uint8_t *mem_buf, int n) |
862 |
{ |
863 |
LM32CPU *cpu = lm32_env_get_cpu(env); |
864 |
CPUClass *cc = CPU_GET_CLASS(cpu); |
865 |
uint32_t tmp; |
866 |
|
867 |
if (n > cc->gdb_num_core_regs) {
|
868 |
return 0; |
869 |
} |
870 |
|
871 |
tmp = ldl_p(mem_buf); |
872 |
|
873 |
if (n < 32) { |
874 |
env->regs[n] = tmp; |
875 |
} else {
|
876 |
switch (n) {
|
877 |
case 32: |
878 |
env->pc = tmp; |
879 |
break;
|
880 |
case 34: |
881 |
env->eba = tmp; |
882 |
break;
|
883 |
case 35: |
884 |
env->deba = tmp; |
885 |
break;
|
886 |
case 36: |
887 |
env->ie = tmp; |
888 |
break;
|
889 |
case 37: |
890 |
lm32_pic_set_im(env->pic_state, tmp); |
891 |
break;
|
892 |
case 38: |
893 |
lm32_pic_set_ip(env->pic_state, tmp); |
894 |
break;
|
895 |
} |
896 |
} |
897 |
return 4; |
898 |
} |
899 |
#elif defined(TARGET_XTENSA)
|
900 |
|
901 |
static int cpu_gdb_read_register(CPUXtensaState *env, uint8_t *mem_buf, int n) |
902 |
{ |
903 |
const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
|
904 |
|
905 |
if (n < 0 || n >= env->config->gdb_regmap.num_regs) { |
906 |
return 0; |
907 |
} |
908 |
|
909 |
switch (reg->type) {
|
910 |
case 9: /*pc*/ |
911 |
GET_REG32(env->pc); |
912 |
|
913 |
case 1: /*ar*/ |
914 |
xtensa_sync_phys_from_window(env); |
915 |
GET_REG32(env->phys_regs[(reg->targno & 0xff) % env->config->nareg]);
|
916 |
|
917 |
case 2: /*SR*/ |
918 |
GET_REG32(env->sregs[reg->targno & 0xff]);
|
919 |
|
920 |
case 3: /*UR*/ |
921 |
GET_REG32(env->uregs[reg->targno & 0xff]);
|
922 |
|
923 |
case 4: /*f*/ |
924 |
GET_REG32(float32_val(env->fregs[reg->targno & 0x0f]));
|
925 |
|
926 |
case 8: /*a*/ |
927 |
GET_REG32(env->regs[reg->targno & 0x0f]);
|
928 |
|
929 |
default:
|
930 |
qemu_log("%s from reg %d of unsupported type %d\n",
|
931 |
__func__, n, reg->type); |
932 |
return 0; |
933 |
} |
934 |
} |
935 |
|
936 |
static int cpu_gdb_write_register(CPUXtensaState *env, uint8_t *mem_buf, int n) |
937 |
{ |
938 |
uint32_t tmp; |
939 |
const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
|
940 |
|
941 |
if (n < 0 || n >= env->config->gdb_regmap.num_regs) { |
942 |
return 0; |
943 |
} |
944 |
|
945 |
tmp = ldl_p(mem_buf); |
946 |
|
947 |
switch (reg->type) {
|
948 |
case 9: /*pc*/ |
949 |
env->pc = tmp; |
950 |
break;
|
951 |
|
952 |
case 1: /*ar*/ |
953 |
env->phys_regs[(reg->targno & 0xff) % env->config->nareg] = tmp;
|
954 |
xtensa_sync_window_from_phys(env); |
955 |
break;
|
956 |
|
957 |
case 2: /*SR*/ |
958 |
env->sregs[reg->targno & 0xff] = tmp;
|
959 |
break;
|
960 |
|
961 |
case 3: /*UR*/ |
962 |
env->uregs[reg->targno & 0xff] = tmp;
|
963 |
break;
|
964 |
|
965 |
case 4: /*f*/ |
966 |
env->fregs[reg->targno & 0x0f] = make_float32(tmp);
|
967 |
break;
|
968 |
|
969 |
case 8: /*a*/ |
970 |
env->regs[reg->targno & 0x0f] = tmp;
|
971 |
break;
|
972 |
|
973 |
default:
|
974 |
qemu_log("%s to reg %d of unsupported type %d\n",
|
975 |
__func__, n, reg->type); |
976 |
return 0; |
977 |
} |
978 |
|
979 |
return 4; |
980 |
} |
981 |
#else
|
982 |
|
983 |
static int cpu_gdb_read_register(CPUArchState *env, uint8_t *mem_buf, int n) |
984 |
{ |
985 |
return 0; |
986 |
} |
987 |
|
988 |
static int cpu_gdb_write_register(CPUArchState *env, uint8_t *mem_buf, int n) |
989 |
{ |
990 |
return 0; |
991 |
} |
992 |
|
993 |
#endif
|
994 |
|
995 |
#ifdef GDB_CORE_XML
|
996 |
/* Encode data using the encoding for 'x' packets. */
|
997 |
static int memtox(char *buf, const char *mem, int len) |
998 |
{ |
999 |
char *p = buf;
|
1000 |
char c;
|
1001 |
|
1002 |
while (len--) {
|
1003 |
c = *(mem++); |
1004 |
switch (c) {
|
1005 |
case '#': case '$': case '*': case '}': |
1006 |
*(p++) = '}';
|
1007 |
*(p++) = c ^ 0x20;
|
1008 |
break;
|
1009 |
default:
|
1010 |
*(p++) = c; |
1011 |
break;
|
1012 |
} |
1013 |
} |
1014 |
return p - buf;
|
1015 |
} |
1016 |
|
1017 |
static const char *get_feature_xml(const char *p, const char **newp) |
1018 |
{ |
1019 |
size_t len; |
1020 |
int i;
|
1021 |
const char *name; |
1022 |
static char target_xml[1024]; |
1023 |
|
1024 |
len = 0;
|
1025 |
while (p[len] && p[len] != ':') |
1026 |
len++; |
1027 |
*newp = p + len; |
1028 |
|
1029 |
name = NULL;
|
1030 |
if (strncmp(p, "target.xml", len) == 0) { |
1031 |
/* Generate the XML description for this CPU. */
|
1032 |
if (!target_xml[0]) { |
1033 |
GDBRegisterState *r; |
1034 |
CPUState *cpu = first_cpu; |
1035 |
|
1036 |
snprintf(target_xml, sizeof(target_xml),
|
1037 |
"<?xml version=\"1.0\"?>"
|
1038 |
"<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
|
1039 |
"<target>"
|
1040 |
"<xi:include href=\"%s\"/>",
|
1041 |
GDB_CORE_XML); |
1042 |
|
1043 |
for (r = cpu->gdb_regs; r; r = r->next) {
|
1044 |
pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\""); |
1045 |
pstrcat(target_xml, sizeof(target_xml), r->xml);
|
1046 |
pstrcat(target_xml, sizeof(target_xml), "\"/>"); |
1047 |
} |
1048 |
pstrcat(target_xml, sizeof(target_xml), "</target>"); |
1049 |
} |
1050 |
return target_xml;
|
1051 |
} |
1052 |
for (i = 0; ; i++) { |
1053 |
name = xml_builtin[i][0];
|
1054 |
if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len)) |
1055 |
break;
|
1056 |
} |
1057 |
return name ? xml_builtin[i][1] : NULL; |
1058 |
} |
1059 |
#endif
|
1060 |
|
1061 |
static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
1062 |
{ |
1063 |
CPUClass *cc = CPU_GET_CLASS(cpu); |
1064 |
CPUArchState *env = cpu->env_ptr; |
1065 |
GDBRegisterState *r; |
1066 |
|
1067 |
if (reg < cc->gdb_num_core_regs) {
|
1068 |
return cpu_gdb_read_register(env, mem_buf, reg);
|
1069 |
} |
1070 |
|
1071 |
for (r = cpu->gdb_regs; r; r = r->next) {
|
1072 |
if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
|
1073 |
return r->get_reg(env, mem_buf, reg - r->base_reg);
|
1074 |
} |
1075 |
} |
1076 |
return 0; |
1077 |
} |
1078 |
|
1079 |
static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
1080 |
{ |
1081 |
CPUClass *cc = CPU_GET_CLASS(cpu); |
1082 |
CPUArchState *env = cpu->env_ptr; |
1083 |
GDBRegisterState *r; |
1084 |
|
1085 |
if (reg < cc->gdb_num_core_regs) {
|
1086 |
return cpu_gdb_write_register(env, mem_buf, reg);
|
1087 |
} |
1088 |
|
1089 |
for (r = cpu->gdb_regs; r; r = r->next) {
|
1090 |
if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
|
1091 |
return r->set_reg(env, mem_buf, reg - r->base_reg);
|
1092 |
} |
1093 |
} |
1094 |
return 0; |
1095 |
} |
1096 |
|
1097 |
/* Register a supplemental set of CPU registers. If g_pos is nonzero it
|
1098 |
specifies the first register number and these registers are included in
|
1099 |
a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
|
1100 |
gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
|
1101 |
*/
|
1102 |
|
1103 |
void gdb_register_coprocessor(CPUState *cpu,
|
1104 |
gdb_reg_cb get_reg, gdb_reg_cb set_reg, |
1105 |
int num_regs, const char *xml, int g_pos) |
1106 |
{ |
1107 |
GDBRegisterState *s; |
1108 |
GDBRegisterState **p; |
1109 |
|
1110 |
p = &cpu->gdb_regs; |
1111 |
while (*p) {
|
1112 |
/* Check for duplicates. */
|
1113 |
if (strcmp((*p)->xml, xml) == 0) |
1114 |
return;
|
1115 |
p = &(*p)->next; |
1116 |
} |
1117 |
|
1118 |
s = g_new0(GDBRegisterState, 1);
|
1119 |
s->base_reg = cpu->gdb_num_regs; |
1120 |
s->num_regs = num_regs; |
1121 |
s->get_reg = get_reg; |
1122 |
s->set_reg = set_reg; |
1123 |
s->xml = xml; |
1124 |
|
1125 |
/* Add to end of list. */
|
1126 |
cpu->gdb_num_regs += num_regs; |
1127 |
*p = s; |
1128 |
if (g_pos) {
|
1129 |
if (g_pos != s->base_reg) {
|
1130 |
fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
|
1131 |
"Expected %d got %d\n", xml, g_pos, s->base_reg);
|
1132 |
} |
1133 |
} |
1134 |
} |
1135 |
|
1136 |
#ifndef CONFIG_USER_ONLY
|
1137 |
static const int xlat_gdb_type[] = { |
1138 |
[GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE, |
1139 |
[GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ, |
1140 |
[GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS, |
1141 |
}; |
1142 |
#endif
|
1143 |
|
1144 |
static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type) |
1145 |
{ |
1146 |
CPUState *cpu; |
1147 |
CPUArchState *env; |
1148 |
int err = 0; |
1149 |
|
1150 |
if (kvm_enabled()) {
|
1151 |
return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
|
1152 |
} |
1153 |
|
1154 |
switch (type) {
|
1155 |
case GDB_BREAKPOINT_SW:
|
1156 |
case GDB_BREAKPOINT_HW:
|
1157 |
for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
1158 |
env = cpu->env_ptr; |
1159 |
err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
|
1160 |
if (err)
|
1161 |
break;
|
1162 |
} |
1163 |
return err;
|
1164 |
#ifndef CONFIG_USER_ONLY
|
1165 |
case GDB_WATCHPOINT_WRITE:
|
1166 |
case GDB_WATCHPOINT_READ:
|
1167 |
case GDB_WATCHPOINT_ACCESS:
|
1168 |
for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
1169 |
env = cpu->env_ptr; |
1170 |
err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type], |
1171 |
NULL);
|
1172 |
if (err)
|
1173 |
break;
|
1174 |
} |
1175 |
return err;
|
1176 |
#endif
|
1177 |
default:
|
1178 |
return -ENOSYS;
|
1179 |
} |
1180 |
} |
1181 |
|
1182 |
static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type) |
1183 |
{ |
1184 |
CPUState *cpu; |
1185 |
CPUArchState *env; |
1186 |
int err = 0; |
1187 |
|
1188 |
if (kvm_enabled()) {
|
1189 |
return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
|
1190 |
} |
1191 |
|
1192 |
switch (type) {
|
1193 |
case GDB_BREAKPOINT_SW:
|
1194 |
case GDB_BREAKPOINT_HW:
|
1195 |
for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
1196 |
env = cpu->env_ptr; |
1197 |
err = cpu_breakpoint_remove(env, addr, BP_GDB); |
1198 |
if (err)
|
1199 |
break;
|
1200 |
} |
1201 |
return err;
|
1202 |
#ifndef CONFIG_USER_ONLY
|
1203 |
case GDB_WATCHPOINT_WRITE:
|
1204 |
case GDB_WATCHPOINT_READ:
|
1205 |
case GDB_WATCHPOINT_ACCESS:
|
1206 |
for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
1207 |
env = cpu->env_ptr; |
1208 |
err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]); |
1209 |
if (err)
|
1210 |
break;
|
1211 |
} |
1212 |
return err;
|
1213 |
#endif
|
1214 |
default:
|
1215 |
return -ENOSYS;
|
1216 |
} |
1217 |
} |
1218 |
|
1219 |
static void gdb_breakpoint_remove_all(void) |
1220 |
{ |
1221 |
CPUState *cpu; |
1222 |
CPUArchState *env; |
1223 |
|
1224 |
if (kvm_enabled()) {
|
1225 |
kvm_remove_all_breakpoints(gdbserver_state->c_cpu); |
1226 |
return;
|
1227 |
} |
1228 |
|
1229 |
for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
1230 |
env = cpu->env_ptr; |
1231 |
cpu_breakpoint_remove_all(env, BP_GDB); |
1232 |
#ifndef CONFIG_USER_ONLY
|
1233 |
cpu_watchpoint_remove_all(env, BP_GDB); |
1234 |
#endif
|
1235 |
} |
1236 |
} |
1237 |
|
1238 |
static void gdb_set_cpu_pc(GDBState *s, target_ulong pc) |
1239 |
{ |
1240 |
CPUState *cpu = s->c_cpu; |
1241 |
CPUClass *cc = CPU_GET_CLASS(cpu); |
1242 |
|
1243 |
cpu_synchronize_state(cpu); |
1244 |
if (cc->set_pc) {
|
1245 |
cc->set_pc(cpu, pc); |
1246 |
} |
1247 |
} |
1248 |
|
1249 |
static CPUState *find_cpu(uint32_t thread_id)
|
1250 |
{ |
1251 |
CPUState *cpu; |
1252 |
|
1253 |
for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
1254 |
if (cpu_index(cpu) == thread_id) {
|
1255 |
return cpu;
|
1256 |
} |
1257 |
} |
1258 |
|
1259 |
return NULL; |
1260 |
} |
1261 |
|
1262 |
static int gdb_handle_packet(GDBState *s, const char *line_buf) |
1263 |
{ |
1264 |
CPUState *cpu; |
1265 |
const char *p; |
1266 |
uint32_t thread; |
1267 |
int ch, reg_size, type, res;
|
1268 |
char buf[MAX_PACKET_LENGTH];
|
1269 |
uint8_t mem_buf[MAX_PACKET_LENGTH]; |
1270 |
uint8_t *registers; |
1271 |
target_ulong addr, len; |
1272 |
|
1273 |
#ifdef DEBUG_GDB
|
1274 |
printf("command='%s'\n", line_buf);
|
1275 |
#endif
|
1276 |
p = line_buf; |
1277 |
ch = *p++; |
1278 |
switch(ch) {
|
1279 |
case '?': |
1280 |
/* TODO: Make this return the correct value for user-mode. */
|
1281 |
snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP, |
1282 |
cpu_index(s->c_cpu)); |
1283 |
put_packet(s, buf); |
1284 |
/* Remove all the breakpoints when this query is issued,
|
1285 |
* because gdb is doing and initial connect and the state
|
1286 |
* should be cleaned up.
|
1287 |
*/
|
1288 |
gdb_breakpoint_remove_all(); |
1289 |
break;
|
1290 |
case 'c': |
1291 |
if (*p != '\0') { |
1292 |
addr = strtoull(p, (char **)&p, 16); |
1293 |
gdb_set_cpu_pc(s, addr); |
1294 |
} |
1295 |
s->signal = 0;
|
1296 |
gdb_continue(s); |
1297 |
return RS_IDLE;
|
1298 |
case 'C': |
1299 |
s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16)); |
1300 |
if (s->signal == -1) |
1301 |
s->signal = 0;
|
1302 |
gdb_continue(s); |
1303 |
return RS_IDLE;
|
1304 |
case 'v': |
1305 |
if (strncmp(p, "Cont", 4) == 0) { |
1306 |
int res_signal, res_thread;
|
1307 |
|
1308 |
p += 4;
|
1309 |
if (*p == '?') { |
1310 |
put_packet(s, "vCont;c;C;s;S");
|
1311 |
break;
|
1312 |
} |
1313 |
res = 0;
|
1314 |
res_signal = 0;
|
1315 |
res_thread = 0;
|
1316 |
while (*p) {
|
1317 |
int action, signal;
|
1318 |
|
1319 |
if (*p++ != ';') { |
1320 |
res = 0;
|
1321 |
break;
|
1322 |
} |
1323 |
action = *p++; |
1324 |
signal = 0;
|
1325 |
if (action == 'C' || action == 'S') { |
1326 |
signal = strtoul(p, (char **)&p, 16); |
1327 |
} else if (action != 'c' && action != 's') { |
1328 |
res = 0;
|
1329 |
break;
|
1330 |
} |
1331 |
thread = 0;
|
1332 |
if (*p == ':') { |
1333 |
thread = strtoull(p+1, (char **)&p, 16); |
1334 |
} |
1335 |
action = tolower(action); |
1336 |
if (res == 0 || (res == 'c' && action == 's')) { |
1337 |
res = action; |
1338 |
res_signal = signal; |
1339 |
res_thread = thread; |
1340 |
} |
1341 |
} |
1342 |
if (res) {
|
1343 |
if (res_thread != -1 && res_thread != 0) { |
1344 |
cpu = find_cpu(res_thread); |
1345 |
if (cpu == NULL) { |
1346 |
put_packet(s, "E22");
|
1347 |
break;
|
1348 |
} |
1349 |
s->c_cpu = cpu; |
1350 |
} |
1351 |
if (res == 's') { |
1352 |
cpu_single_step(s->c_cpu, sstep_flags); |
1353 |
} |
1354 |
s->signal = res_signal; |
1355 |
gdb_continue(s); |
1356 |
return RS_IDLE;
|
1357 |
} |
1358 |
break;
|
1359 |
} else {
|
1360 |
goto unknown_command;
|
1361 |
} |
1362 |
case 'k': |
1363 |
#ifdef CONFIG_USER_ONLY
|
1364 |
/* Kill the target */
|
1365 |
fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
|
1366 |
exit(0);
|
1367 |
#endif
|
1368 |
case 'D': |
1369 |
/* Detach packet */
|
1370 |
gdb_breakpoint_remove_all(); |
1371 |
gdb_syscall_mode = GDB_SYS_DISABLED; |
1372 |
gdb_continue(s); |
1373 |
put_packet(s, "OK");
|
1374 |
break;
|
1375 |
case 's': |
1376 |
if (*p != '\0') { |
1377 |
addr = strtoull(p, (char **)&p, 16); |
1378 |
gdb_set_cpu_pc(s, addr); |
1379 |
} |
1380 |
cpu_single_step(s->c_cpu, sstep_flags); |
1381 |
gdb_continue(s); |
1382 |
return RS_IDLE;
|
1383 |
case 'F': |
1384 |
{ |
1385 |
target_ulong ret; |
1386 |
target_ulong err; |
1387 |
|
1388 |
ret = strtoull(p, (char **)&p, 16); |
1389 |
if (*p == ',') { |
1390 |
p++; |
1391 |
err = strtoull(p, (char **)&p, 16); |
1392 |
} else {
|
1393 |
err = 0;
|
1394 |
} |
1395 |
if (*p == ',') |
1396 |
p++; |
1397 |
type = *p; |
1398 |
if (s->current_syscall_cb) {
|
1399 |
s->current_syscall_cb(s->c_cpu, ret, err); |
1400 |
s->current_syscall_cb = NULL;
|
1401 |
} |
1402 |
if (type == 'C') { |
1403 |
put_packet(s, "T02");
|
1404 |
} else {
|
1405 |
gdb_continue(s); |
1406 |
} |
1407 |
} |
1408 |
break;
|
1409 |
case 'g': |
1410 |
cpu_synchronize_state(s->g_cpu); |
1411 |
len = 0;
|
1412 |
for (addr = 0; addr < s->g_cpu->gdb_num_regs; addr++) { |
1413 |
reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr); |
1414 |
len += reg_size; |
1415 |
} |
1416 |
memtohex(buf, mem_buf, len); |
1417 |
put_packet(s, buf); |
1418 |
break;
|
1419 |
case 'G': |
1420 |
cpu_synchronize_state(s->g_cpu); |
1421 |
registers = mem_buf; |
1422 |
len = strlen(p) / 2;
|
1423 |
hextomem((uint8_t *)registers, p, len); |
1424 |
for (addr = 0; addr < s->g_cpu->gdb_num_regs && len > 0; addr++) { |
1425 |
reg_size = gdb_write_register(s->g_cpu, registers, addr); |
1426 |
len -= reg_size; |
1427 |
registers += reg_size; |
1428 |
} |
1429 |
put_packet(s, "OK");
|
1430 |
break;
|
1431 |
case 'm': |
1432 |
addr = strtoull(p, (char **)&p, 16); |
1433 |
if (*p == ',') |
1434 |
p++; |
1435 |
len = strtoull(p, NULL, 16); |
1436 |
if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) { |
1437 |
put_packet (s, "E14");
|
1438 |
} else {
|
1439 |
memtohex(buf, mem_buf, len); |
1440 |
put_packet(s, buf); |
1441 |
} |
1442 |
break;
|
1443 |
case 'M': |
1444 |
addr = strtoull(p, (char **)&p, 16); |
1445 |
if (*p == ',') |
1446 |
p++; |
1447 |
len = strtoull(p, (char **)&p, 16); |
1448 |
if (*p == ':') |
1449 |
p++; |
1450 |
hextomem(mem_buf, p, len); |
1451 |
if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
|
1452 |
true) != 0) { |
1453 |
put_packet(s, "E14");
|
1454 |
} else {
|
1455 |
put_packet(s, "OK");
|
1456 |
} |
1457 |
break;
|
1458 |
case 'p': |
1459 |
/* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
|
1460 |
This works, but can be very slow. Anything new enough to
|
1461 |
understand XML also knows how to use this properly. */
|
1462 |
if (!gdb_has_xml)
|
1463 |
goto unknown_command;
|
1464 |
addr = strtoull(p, (char **)&p, 16); |
1465 |
reg_size = gdb_read_register(s->g_cpu, mem_buf, addr); |
1466 |
if (reg_size) {
|
1467 |
memtohex(buf, mem_buf, reg_size); |
1468 |
put_packet(s, buf); |
1469 |
} else {
|
1470 |
put_packet(s, "E14");
|
1471 |
} |
1472 |
break;
|
1473 |
case 'P': |
1474 |
if (!gdb_has_xml)
|
1475 |
goto unknown_command;
|
1476 |
addr = strtoull(p, (char **)&p, 16); |
1477 |
if (*p == '=') |
1478 |
p++; |
1479 |
reg_size = strlen(p) / 2;
|
1480 |
hextomem(mem_buf, p, reg_size); |
1481 |
gdb_write_register(s->g_cpu, mem_buf, addr); |
1482 |
put_packet(s, "OK");
|
1483 |
break;
|
1484 |
case 'Z': |
1485 |
case 'z': |
1486 |
type = strtoul(p, (char **)&p, 16); |
1487 |
if (*p == ',') |
1488 |
p++; |
1489 |
addr = strtoull(p, (char **)&p, 16); |
1490 |
if (*p == ',') |
1491 |
p++; |
1492 |
len = strtoull(p, (char **)&p, 16); |
1493 |
if (ch == 'Z') |
1494 |
res = gdb_breakpoint_insert(addr, len, type); |
1495 |
else
|
1496 |
res = gdb_breakpoint_remove(addr, len, type); |
1497 |
if (res >= 0) |
1498 |
put_packet(s, "OK");
|
1499 |
else if (res == -ENOSYS) |
1500 |
put_packet(s, "");
|
1501 |
else
|
1502 |
put_packet(s, "E22");
|
1503 |
break;
|
1504 |
case 'H': |
1505 |
type = *p++; |
1506 |
thread = strtoull(p, (char **)&p, 16); |
1507 |
if (thread == -1 || thread == 0) { |
1508 |
put_packet(s, "OK");
|
1509 |
break;
|
1510 |
} |
1511 |
cpu = find_cpu(thread); |
1512 |
if (cpu == NULL) { |
1513 |
put_packet(s, "E22");
|
1514 |
break;
|
1515 |
} |
1516 |
switch (type) {
|
1517 |
case 'c': |
1518 |
s->c_cpu = cpu; |
1519 |
put_packet(s, "OK");
|
1520 |
break;
|
1521 |
case 'g': |
1522 |
s->g_cpu = cpu; |
1523 |
put_packet(s, "OK");
|
1524 |
break;
|
1525 |
default:
|
1526 |
put_packet(s, "E22");
|
1527 |
break;
|
1528 |
} |
1529 |
break;
|
1530 |
case 'T': |
1531 |
thread = strtoull(p, (char **)&p, 16); |
1532 |
cpu = find_cpu(thread); |
1533 |
|
1534 |
if (cpu != NULL) { |
1535 |
put_packet(s, "OK");
|
1536 |
} else {
|
1537 |
put_packet(s, "E22");
|
1538 |
} |
1539 |
break;
|
1540 |
case 'q': |
1541 |
case 'Q': |
1542 |
/* parse any 'q' packets here */
|
1543 |
if (!strcmp(p,"qemu.sstepbits")) { |
1544 |
/* Query Breakpoint bit definitions */
|
1545 |
snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", |
1546 |
SSTEP_ENABLE, |
1547 |
SSTEP_NOIRQ, |
1548 |
SSTEP_NOTIMER); |
1549 |
put_packet(s, buf); |
1550 |
break;
|
1551 |
} else if (strncmp(p,"qemu.sstep",10) == 0) { |
1552 |
/* Display or change the sstep_flags */
|
1553 |
p += 10;
|
1554 |
if (*p != '=') { |
1555 |
/* Display current setting */
|
1556 |
snprintf(buf, sizeof(buf), "0x%x", sstep_flags); |
1557 |
put_packet(s, buf); |
1558 |
break;
|
1559 |
} |
1560 |
p++; |
1561 |
type = strtoul(p, (char **)&p, 16); |
1562 |
sstep_flags = type; |
1563 |
put_packet(s, "OK");
|
1564 |
break;
|
1565 |
} else if (strcmp(p,"C") == 0) { |
1566 |
/* "Current thread" remains vague in the spec, so always return
|
1567 |
* the first CPU (gdb returns the first thread). */
|
1568 |
put_packet(s, "QC1");
|
1569 |
break;
|
1570 |
} else if (strcmp(p,"fThreadInfo") == 0) { |
1571 |
s->query_cpu = first_cpu; |
1572 |
goto report_cpuinfo;
|
1573 |
} else if (strcmp(p,"sThreadInfo") == 0) { |
1574 |
report_cpuinfo:
|
1575 |
if (s->query_cpu) {
|
1576 |
snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu)); |
1577 |
put_packet(s, buf); |
1578 |
s->query_cpu = s->query_cpu->next_cpu; |
1579 |
} else
|
1580 |
put_packet(s, "l");
|
1581 |
break;
|
1582 |
} else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) { |
1583 |
thread = strtoull(p+16, (char **)&p, 16); |
1584 |
cpu = find_cpu(thread); |
1585 |
if (cpu != NULL) { |
1586 |
cpu_synchronize_state(cpu); |
1587 |
len = snprintf((char *)mem_buf, sizeof(mem_buf), |
1588 |
"CPU#%d [%s]", cpu->cpu_index,
|
1589 |
cpu->halted ? "halted " : "running"); |
1590 |
memtohex(buf, mem_buf, len); |
1591 |
put_packet(s, buf); |
1592 |
} |
1593 |
break;
|
1594 |
} |
1595 |
#ifdef CONFIG_USER_ONLY
|
1596 |
else if (strncmp(p, "Offsets", 7) == 0) { |
1597 |
CPUArchState *env = s->c_cpu->env_ptr; |
1598 |
TaskState *ts = env->opaque; |
1599 |
|
1600 |
snprintf(buf, sizeof(buf),
|
1601 |
"Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx |
1602 |
";Bss=" TARGET_ABI_FMT_lx,
|
1603 |
ts->info->code_offset, |
1604 |
ts->info->data_offset, |
1605 |
ts->info->data_offset); |
1606 |
put_packet(s, buf); |
1607 |
break;
|
1608 |
} |
1609 |
#else /* !CONFIG_USER_ONLY */ |
1610 |
else if (strncmp(p, "Rcmd,", 5) == 0) { |
1611 |
int len = strlen(p + 5); |
1612 |
|
1613 |
if ((len % 2) != 0) { |
1614 |
put_packet(s, "E01");
|
1615 |
break;
|
1616 |
} |
1617 |
hextomem(mem_buf, p + 5, len);
|
1618 |
len = len / 2;
|
1619 |
mem_buf[len++] = 0;
|
1620 |
qemu_chr_be_write(s->mon_chr, mem_buf, len); |
1621 |
put_packet(s, "OK");
|
1622 |
break;
|
1623 |
} |
1624 |
#endif /* !CONFIG_USER_ONLY */ |
1625 |
if (strncmp(p, "Supported", 9) == 0) { |
1626 |
snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH); |
1627 |
#ifdef GDB_CORE_XML
|
1628 |
pstrcat(buf, sizeof(buf), ";qXfer:features:read+"); |
1629 |
#endif
|
1630 |
put_packet(s, buf); |
1631 |
break;
|
1632 |
} |
1633 |
#ifdef GDB_CORE_XML
|
1634 |
if (strncmp(p, "Xfer:features:read:", 19) == 0) { |
1635 |
const char *xml; |
1636 |
target_ulong total_len; |
1637 |
|
1638 |
gdb_has_xml = 1;
|
1639 |
p += 19;
|
1640 |
xml = get_feature_xml(p, &p); |
1641 |
if (!xml) {
|
1642 |
snprintf(buf, sizeof(buf), "E00"); |
1643 |
put_packet(s, buf); |
1644 |
break;
|
1645 |
} |
1646 |
|
1647 |
if (*p == ':') |
1648 |
p++; |
1649 |
addr = strtoul(p, (char **)&p, 16); |
1650 |
if (*p == ',') |
1651 |
p++; |
1652 |
len = strtoul(p, (char **)&p, 16); |
1653 |
|
1654 |
total_len = strlen(xml); |
1655 |
if (addr > total_len) {
|
1656 |
snprintf(buf, sizeof(buf), "E00"); |
1657 |
put_packet(s, buf); |
1658 |
break;
|
1659 |
} |
1660 |
if (len > (MAX_PACKET_LENGTH - 5) / 2) |
1661 |
len = (MAX_PACKET_LENGTH - 5) / 2; |
1662 |
if (len < total_len - addr) {
|
1663 |
buf[0] = 'm'; |
1664 |
len = memtox(buf + 1, xml + addr, len);
|
1665 |
} else {
|
1666 |
buf[0] = 'l'; |
1667 |
len = memtox(buf + 1, xml + addr, total_len - addr);
|
1668 |
} |
1669 |
put_packet_binary(s, buf, len + 1);
|
1670 |
break;
|
1671 |
} |
1672 |
#endif
|
1673 |
/* Unrecognised 'q' command. */
|
1674 |
goto unknown_command;
|
1675 |
|
1676 |
default:
|
1677 |
unknown_command:
|
1678 |
/* put empty packet */
|
1679 |
buf[0] = '\0'; |
1680 |
put_packet(s, buf); |
1681 |
break;
|
1682 |
} |
1683 |
return RS_IDLE;
|
1684 |
} |
1685 |
|
1686 |
void gdb_set_stop_cpu(CPUState *cpu)
|
1687 |
{ |
1688 |
gdbserver_state->c_cpu = cpu; |
1689 |
gdbserver_state->g_cpu = cpu; |
1690 |
} |
1691 |
|
1692 |
#ifndef CONFIG_USER_ONLY
|
1693 |
static void gdb_vm_state_change(void *opaque, int running, RunState state) |
1694 |
{ |
1695 |
GDBState *s = gdbserver_state; |
1696 |
CPUArchState *env = s->c_cpu->env_ptr; |
1697 |
CPUState *cpu = s->c_cpu; |
1698 |
char buf[256]; |
1699 |
const char *type; |
1700 |
int ret;
|
1701 |
|
1702 |
if (running || s->state == RS_INACTIVE) {
|
1703 |
return;
|
1704 |
} |
1705 |
/* Is there a GDB syscall waiting to be sent? */
|
1706 |
if (s->current_syscall_cb) {
|
1707 |
put_packet(s, s->syscall_buf); |
1708 |
return;
|
1709 |
} |
1710 |
switch (state) {
|
1711 |
case RUN_STATE_DEBUG:
|
1712 |
if (env->watchpoint_hit) {
|
1713 |
switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
|
1714 |
case BP_MEM_READ:
|
1715 |
type = "r";
|
1716 |
break;
|
1717 |
case BP_MEM_ACCESS:
|
1718 |
type = "a";
|
1719 |
break;
|
1720 |
default:
|
1721 |
type = "";
|
1722 |
break;
|
1723 |
} |
1724 |
snprintf(buf, sizeof(buf),
|
1725 |
"T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";", |
1726 |
GDB_SIGNAL_TRAP, cpu_index(cpu), type, |
1727 |
env->watchpoint_hit->vaddr); |
1728 |
env->watchpoint_hit = NULL;
|
1729 |
goto send_packet;
|
1730 |
} |
1731 |
tb_flush(env); |
1732 |
ret = GDB_SIGNAL_TRAP; |
1733 |
break;
|
1734 |
case RUN_STATE_PAUSED:
|
1735 |
ret = GDB_SIGNAL_INT; |
1736 |
break;
|
1737 |
case RUN_STATE_SHUTDOWN:
|
1738 |
ret = GDB_SIGNAL_QUIT; |
1739 |
break;
|
1740 |
case RUN_STATE_IO_ERROR:
|
1741 |
ret = GDB_SIGNAL_IO; |
1742 |
break;
|
1743 |
case RUN_STATE_WATCHDOG:
|
1744 |
ret = GDB_SIGNAL_ALRM; |
1745 |
break;
|
1746 |
case RUN_STATE_INTERNAL_ERROR:
|
1747 |
ret = GDB_SIGNAL_ABRT; |
1748 |
break;
|
1749 |
case RUN_STATE_SAVE_VM:
|
1750 |
case RUN_STATE_RESTORE_VM:
|
1751 |
return;
|
1752 |
case RUN_STATE_FINISH_MIGRATE:
|
1753 |
ret = GDB_SIGNAL_XCPU; |
1754 |
break;
|
1755 |
default:
|
1756 |
ret = GDB_SIGNAL_UNKNOWN; |
1757 |
break;
|
1758 |
} |
1759 |
snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu)); |
1760 |
|
1761 |
send_packet:
|
1762 |
put_packet(s, buf); |
1763 |
|
1764 |
/* disable single step if it was enabled */
|
1765 |
cpu_single_step(cpu, 0);
|
1766 |
} |
1767 |
#endif
|
1768 |
|
1769 |
/* Send a gdb syscall request.
|
1770 |
This accepts limited printf-style format specifiers, specifically:
|
1771 |
%x - target_ulong argument printed in hex.
|
1772 |
%lx - 64-bit argument printed in hex.
|
1773 |
%s - string pointer (target_ulong) and length (int) pair. */
|
1774 |
void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...) |
1775 |
{ |
1776 |
va_list va; |
1777 |
char *p;
|
1778 |
char *p_end;
|
1779 |
target_ulong addr; |
1780 |
uint64_t i64; |
1781 |
GDBState *s; |
1782 |
|
1783 |
s = gdbserver_state; |
1784 |
if (!s)
|
1785 |
return;
|
1786 |
s->current_syscall_cb = cb; |
1787 |
#ifndef CONFIG_USER_ONLY
|
1788 |
vm_stop(RUN_STATE_DEBUG); |
1789 |
#endif
|
1790 |
va_start(va, fmt); |
1791 |
p = s->syscall_buf; |
1792 |
p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
|
1793 |
*(p++) = 'F';
|
1794 |
while (*fmt) {
|
1795 |
if (*fmt == '%') { |
1796 |
fmt++; |
1797 |
switch (*fmt++) {
|
1798 |
case 'x': |
1799 |
addr = va_arg(va, target_ulong); |
1800 |
p += snprintf(p, p_end - p, TARGET_FMT_lx, addr); |
1801 |
break;
|
1802 |
case 'l': |
1803 |
if (*(fmt++) != 'x') |
1804 |
goto bad_format;
|
1805 |
i64 = va_arg(va, uint64_t); |
1806 |
p += snprintf(p, p_end - p, "%" PRIx64, i64);
|
1807 |
break;
|
1808 |
case 's': |
1809 |
addr = va_arg(va, target_ulong); |
1810 |
p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
|
1811 |
addr, va_arg(va, int));
|
1812 |
break;
|
1813 |
default:
|
1814 |
bad_format:
|
1815 |
fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
|
1816 |
fmt - 1);
|
1817 |
break;
|
1818 |
} |
1819 |
} else {
|
1820 |
*(p++) = *(fmt++); |
1821 |
} |
1822 |
} |
1823 |
*p = 0;
|
1824 |
va_end(va); |
1825 |
#ifdef CONFIG_USER_ONLY
|
1826 |
put_packet(s, s->syscall_buf); |
1827 |
gdb_handlesig(s->c_cpu, 0);
|
1828 |
#else
|
1829 |
/* In this case wait to send the syscall packet until notification that
|
1830 |
the CPU has stopped. This must be done because if the packet is sent
|
1831 |
now the reply from the syscall request could be received while the CPU
|
1832 |
is still in the running state, which can cause packets to be dropped
|
1833 |
and state transition 'T' packets to be sent while the syscall is still
|
1834 |
being processed. */
|
1835 |
cpu_exit(s->c_cpu); |
1836 |
#endif
|
1837 |
} |
1838 |
|
1839 |
static void gdb_read_byte(GDBState *s, int ch) |
1840 |
{ |
1841 |
int i, csum;
|
1842 |
uint8_t reply; |
1843 |
|
1844 |
#ifndef CONFIG_USER_ONLY
|
1845 |
if (s->last_packet_len) {
|
1846 |
/* Waiting for a response to the last packet. If we see the start
|
1847 |
of a new command then abandon the previous response. */
|
1848 |
if (ch == '-') { |
1849 |
#ifdef DEBUG_GDB
|
1850 |
printf("Got NACK, retransmitting\n");
|
1851 |
#endif
|
1852 |
put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); |
1853 |
} |
1854 |
#ifdef DEBUG_GDB
|
1855 |
else if (ch == '+') |
1856 |
printf("Got ACK\n");
|
1857 |
else
|
1858 |
printf("Got '%c' when expecting ACK/NACK\n", ch);
|
1859 |
#endif
|
1860 |
if (ch == '+' || ch == '$') |
1861 |
s->last_packet_len = 0;
|
1862 |
if (ch != '$') |
1863 |
return;
|
1864 |
} |
1865 |
if (runstate_is_running()) {
|
1866 |
/* when the CPU is running, we cannot do anything except stop
|
1867 |
it when receiving a char */
|
1868 |
vm_stop(RUN_STATE_PAUSED); |
1869 |
} else
|
1870 |
#endif
|
1871 |
{ |
1872 |
switch(s->state) {
|
1873 |
case RS_IDLE:
|
1874 |
if (ch == '$') { |
1875 |
s->line_buf_index = 0;
|
1876 |
s->state = RS_GETLINE; |
1877 |
} |
1878 |
break;
|
1879 |
case RS_GETLINE:
|
1880 |
if (ch == '#') { |
1881 |
s->state = RS_CHKSUM1; |
1882 |
} else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { |
1883 |
s->state = RS_IDLE; |
1884 |
} else {
|
1885 |
s->line_buf[s->line_buf_index++] = ch; |
1886 |
} |
1887 |
break;
|
1888 |
case RS_CHKSUM1:
|
1889 |
s->line_buf[s->line_buf_index] = '\0';
|
1890 |
s->line_csum = fromhex(ch) << 4;
|
1891 |
s->state = RS_CHKSUM2; |
1892 |
break;
|
1893 |
case RS_CHKSUM2:
|
1894 |
s->line_csum |= fromhex(ch); |
1895 |
csum = 0;
|
1896 |
for(i = 0; i < s->line_buf_index; i++) { |
1897 |
csum += s->line_buf[i]; |
1898 |
} |
1899 |
if (s->line_csum != (csum & 0xff)) { |
1900 |
reply = '-';
|
1901 |
put_buffer(s, &reply, 1);
|
1902 |
s->state = RS_IDLE; |
1903 |
} else {
|
1904 |
reply = '+';
|
1905 |
put_buffer(s, &reply, 1);
|
1906 |
s->state = gdb_handle_packet(s, s->line_buf); |
1907 |
} |
1908 |
break;
|
1909 |
default:
|
1910 |
abort(); |
1911 |
} |
1912 |
} |
1913 |
} |
1914 |
|
1915 |
/* Tell the remote gdb that the process has exited. */
|
1916 |
void gdb_exit(CPUArchState *env, int code) |
1917 |
{ |
1918 |
GDBState *s; |
1919 |
char buf[4]; |
1920 |
|
1921 |
s = gdbserver_state; |
1922 |
if (!s) {
|
1923 |
return;
|
1924 |
} |
1925 |
#ifdef CONFIG_USER_ONLY
|
1926 |
if (gdbserver_fd < 0 || s->fd < 0) { |
1927 |
return;
|
1928 |
} |
1929 |
#endif
|
1930 |
|
1931 |
snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code); |
1932 |
put_packet(s, buf); |
1933 |
|
1934 |
#ifndef CONFIG_USER_ONLY
|
1935 |
if (s->chr) {
|
1936 |
qemu_chr_delete(s->chr); |
1937 |
} |
1938 |
#endif
|
1939 |
} |
1940 |
|
1941 |
#ifdef CONFIG_USER_ONLY
|
1942 |
int
|
1943 |
gdb_queuesig (void)
|
1944 |
{ |
1945 |
GDBState *s; |
1946 |
|
1947 |
s = gdbserver_state; |
1948 |
|
1949 |
if (gdbserver_fd < 0 || s->fd < 0) |
1950 |
return 0; |
1951 |
else
|
1952 |
return 1; |
1953 |
} |
1954 |
|
1955 |
int
|
1956 |
gdb_handlesig(CPUState *cpu, int sig)
|
1957 |
{ |
1958 |
CPUArchState *env = cpu->env_ptr; |
1959 |
GDBState *s; |
1960 |
char buf[256]; |
1961 |
int n;
|
1962 |
|
1963 |
s = gdbserver_state; |
1964 |
if (gdbserver_fd < 0 || s->fd < 0) { |
1965 |
return sig;
|
1966 |
} |
1967 |
|
1968 |
/* disable single step if it was enabled */
|
1969 |
cpu_single_step(cpu, 0);
|
1970 |
tb_flush(env); |
1971 |
|
1972 |
if (sig != 0) { |
1973 |
snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig)); |
1974 |
put_packet(s, buf); |
1975 |
} |
1976 |
/* put_packet() might have detected that the peer terminated the
|
1977 |
connection. */
|
1978 |
if (s->fd < 0) { |
1979 |
return sig;
|
1980 |
} |
1981 |
|
1982 |
sig = 0;
|
1983 |
s->state = RS_IDLE; |
1984 |
s->running_state = 0;
|
1985 |
while (s->running_state == 0) { |
1986 |
n = read(s->fd, buf, 256);
|
1987 |
if (n > 0) { |
1988 |
int i;
|
1989 |
|
1990 |
for (i = 0; i < n; i++) { |
1991 |
gdb_read_byte(s, buf[i]); |
1992 |
} |
1993 |
} else if (n == 0 || errno != EAGAIN) { |
1994 |
/* XXX: Connection closed. Should probably wait for another
|
1995 |
connection before continuing. */
|
1996 |
return sig;
|
1997 |
} |
1998 |
} |
1999 |
sig = s->signal; |
2000 |
s->signal = 0;
|
2001 |
return sig;
|
2002 |
} |
2003 |
|
2004 |
/* Tell the remote gdb that the process has exited due to SIG. */
|
2005 |
void gdb_signalled(CPUArchState *env, int sig) |
2006 |
{ |
2007 |
GDBState *s; |
2008 |
char buf[4]; |
2009 |
|
2010 |
s = gdbserver_state; |
2011 |
if (gdbserver_fd < 0 || s->fd < 0) { |
2012 |
return;
|
2013 |
} |
2014 |
|
2015 |
snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig)); |
2016 |
put_packet(s, buf); |
2017 |
} |
2018 |
|
2019 |
static void gdb_accept(void) |
2020 |
{ |
2021 |
GDBState *s; |
2022 |
struct sockaddr_in sockaddr;
|
2023 |
socklen_t len; |
2024 |
int fd;
|
2025 |
|
2026 |
for(;;) {
|
2027 |
len = sizeof(sockaddr);
|
2028 |
fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
|
2029 |
if (fd < 0 && errno != EINTR) { |
2030 |
perror("accept");
|
2031 |
return;
|
2032 |
} else if (fd >= 0) { |
2033 |
#ifndef _WIN32
|
2034 |
fcntl(fd, F_SETFD, FD_CLOEXEC); |
2035 |
#endif
|
2036 |
break;
|
2037 |
} |
2038 |
} |
2039 |
|
2040 |
/* set short latency */
|
2041 |
socket_set_nodelay(fd); |
2042 |
|
2043 |
s = g_malloc0(sizeof(GDBState));
|
2044 |
s->c_cpu = first_cpu; |
2045 |
s->g_cpu = first_cpu; |
2046 |
s->fd = fd; |
2047 |
gdb_has_xml = 0;
|
2048 |
|
2049 |
gdbserver_state = s; |
2050 |
|
2051 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
2052 |
} |
2053 |
|
2054 |
static int gdbserver_open(int port) |
2055 |
{ |
2056 |
struct sockaddr_in sockaddr;
|
2057 |
int fd, val, ret;
|
2058 |
|
2059 |
fd = socket(PF_INET, SOCK_STREAM, 0);
|
2060 |
if (fd < 0) { |
2061 |
perror("socket");
|
2062 |
return -1; |
2063 |
} |
2064 |
#ifndef _WIN32
|
2065 |
fcntl(fd, F_SETFD, FD_CLOEXEC); |
2066 |
#endif
|
2067 |
|
2068 |
/* allow fast reuse */
|
2069 |
val = 1;
|
2070 |
qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
|
2071 |
|
2072 |
sockaddr.sin_family = AF_INET; |
2073 |
sockaddr.sin_port = htons(port); |
2074 |
sockaddr.sin_addr.s_addr = 0;
|
2075 |
ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
2076 |
if (ret < 0) { |
2077 |
perror("bind");
|
2078 |
close(fd); |
2079 |
return -1; |
2080 |
} |
2081 |
ret = listen(fd, 0);
|
2082 |
if (ret < 0) { |
2083 |
perror("listen");
|
2084 |
close(fd); |
2085 |
return -1; |
2086 |
} |
2087 |
return fd;
|
2088 |
} |
2089 |
|
2090 |
int gdbserver_start(int port) |
2091 |
{ |
2092 |
gdbserver_fd = gdbserver_open(port); |
2093 |
if (gdbserver_fd < 0) |
2094 |
return -1; |
2095 |
/* accept connections */
|
2096 |
gdb_accept(); |
2097 |
return 0; |
2098 |
} |
2099 |
|
2100 |
/* Disable gdb stub for child processes. */
|
2101 |
void gdbserver_fork(CPUArchState *env)
|
2102 |
{ |
2103 |
GDBState *s = gdbserver_state; |
2104 |
if (gdbserver_fd < 0 || s->fd < 0) |
2105 |
return;
|
2106 |
close(s->fd); |
2107 |
s->fd = -1;
|
2108 |
cpu_breakpoint_remove_all(env, BP_GDB); |
2109 |
cpu_watchpoint_remove_all(env, BP_GDB); |
2110 |
} |
2111 |
#else
|
2112 |
static int gdb_chr_can_receive(void *opaque) |
2113 |
{ |
2114 |
/* We can handle an arbitrarily large amount of data.
|
2115 |
Pick the maximum packet size, which is as good as anything. */
|
2116 |
return MAX_PACKET_LENGTH;
|
2117 |
} |
2118 |
|
2119 |
static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size) |
2120 |
{ |
2121 |
int i;
|
2122 |
|
2123 |
for (i = 0; i < size; i++) { |
2124 |
gdb_read_byte(gdbserver_state, buf[i]); |
2125 |
} |
2126 |
} |
2127 |
|
2128 |
static void gdb_chr_event(void *opaque, int event) |
2129 |
{ |
2130 |
switch (event) {
|
2131 |
case CHR_EVENT_OPENED:
|
2132 |
vm_stop(RUN_STATE_PAUSED); |
2133 |
gdb_has_xml = 0;
|
2134 |
break;
|
2135 |
default:
|
2136 |
break;
|
2137 |
} |
2138 |
} |
2139 |
|
2140 |
static void gdb_monitor_output(GDBState *s, const char *msg, int len) |
2141 |
{ |
2142 |
char buf[MAX_PACKET_LENGTH];
|
2143 |
|
2144 |
buf[0] = 'O'; |
2145 |
if (len > (MAX_PACKET_LENGTH/2) - 1) |
2146 |
len = (MAX_PACKET_LENGTH/2) - 1; |
2147 |
memtohex(buf + 1, (uint8_t *)msg, len);
|
2148 |
put_packet(s, buf); |
2149 |
} |
2150 |
|
2151 |
static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len) |
2152 |
{ |
2153 |
const char *p = (const char *)buf; |
2154 |
int max_sz;
|
2155 |
|
2156 |
max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2; |
2157 |
for (;;) {
|
2158 |
if (len <= max_sz) {
|
2159 |
gdb_monitor_output(gdbserver_state, p, len); |
2160 |
break;
|
2161 |
} |
2162 |
gdb_monitor_output(gdbserver_state, p, max_sz); |
2163 |
p += max_sz; |
2164 |
len -= max_sz; |
2165 |
} |
2166 |
return len;
|
2167 |
} |
2168 |
|
2169 |
#ifndef _WIN32
|
2170 |
static void gdb_sigterm_handler(int signal) |
2171 |
{ |
2172 |
if (runstate_is_running()) {
|
2173 |
vm_stop(RUN_STATE_PAUSED); |
2174 |
} |
2175 |
} |
2176 |
#endif
|
2177 |
|
2178 |
int gdbserver_start(const char *device) |
2179 |
{ |
2180 |
GDBState *s; |
2181 |
char gdbstub_device_name[128]; |
2182 |
CharDriverState *chr = NULL;
|
2183 |
CharDriverState *mon_chr; |
2184 |
|
2185 |
if (!device)
|
2186 |
return -1; |
2187 |
if (strcmp(device, "none") != 0) { |
2188 |
if (strstart(device, "tcp:", NULL)) { |
2189 |
/* enforce required TCP attributes */
|
2190 |
snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
|
2191 |
"%s,nowait,nodelay,server", device);
|
2192 |
device = gdbstub_device_name; |
2193 |
} |
2194 |
#ifndef _WIN32
|
2195 |
else if (strcmp(device, "stdio") == 0) { |
2196 |
struct sigaction act;
|
2197 |
|
2198 |
memset(&act, 0, sizeof(act)); |
2199 |
act.sa_handler = gdb_sigterm_handler; |
2200 |
sigaction(SIGINT, &act, NULL);
|
2201 |
} |
2202 |
#endif
|
2203 |
chr = qemu_chr_new("gdb", device, NULL); |
2204 |
if (!chr)
|
2205 |
return -1; |
2206 |
|
2207 |
qemu_chr_fe_claim_no_fail(chr); |
2208 |
qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive, |
2209 |
gdb_chr_event, NULL);
|
2210 |
} |
2211 |
|
2212 |
s = gdbserver_state; |
2213 |
if (!s) {
|
2214 |
s = g_malloc0(sizeof(GDBState));
|
2215 |
gdbserver_state = s; |
2216 |
|
2217 |
qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
|
2218 |
|
2219 |
/* Initialize a monitor terminal for gdb */
|
2220 |
mon_chr = g_malloc0(sizeof(*mon_chr));
|
2221 |
mon_chr->chr_write = gdb_monitor_write; |
2222 |
monitor_init(mon_chr, 0);
|
2223 |
} else {
|
2224 |
if (s->chr)
|
2225 |
qemu_chr_delete(s->chr); |
2226 |
mon_chr = s->mon_chr; |
2227 |
memset(s, 0, sizeof(GDBState)); |
2228 |
} |
2229 |
s->c_cpu = first_cpu; |
2230 |
s->g_cpu = first_cpu; |
2231 |
s->chr = chr; |
2232 |
s->state = chr ? RS_IDLE : RS_INACTIVE; |
2233 |
s->mon_chr = mon_chr; |
2234 |
s->current_syscall_cb = NULL;
|
2235 |
|
2236 |
return 0; |
2237 |
} |
2238 |
#endif
|