ia64/xen-unstable

view tools/ioemu/gdbstub.c @ 15841:c5f735271e22

[IA64] Foreign p2m: Fix vti domain builder.

It should set arch_domain::convmem_end.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Alex Williamson <alex.williamson@hp.com>
date Thu Sep 06 13:48:43 2007 -0600 (2007-09-06)
parents 00618037d37d
children
line source
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 #include "config.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>
30 #include "qemu.h"
31 #else
32 #include "vl.h"
33 #endif
35 #include "qemu_socket.h"
36 #ifdef _WIN32
37 /* XXX: these constants may be independent of the host ones even for Unix */
38 #ifndef SIGTRAP
39 #define SIGTRAP 5
40 #endif
41 #ifndef SIGINT
42 #define SIGINT 2
43 #endif
44 #else
45 #include <signal.h>
46 #endif
48 //#define DEBUG_GDB
50 enum RSState {
51 RS_IDLE,
52 RS_GETLINE,
53 RS_CHKSUM1,
54 RS_CHKSUM2,
55 RS_SYSCALL,
56 };
57 typedef struct GDBState {
58 CPUState *env; /* current CPU */
59 enum RSState state; /* parsing state */
60 char line_buf[4096];
61 int line_buf_index;
62 int line_csum;
63 char last_packet[4100];
64 int last_packet_len;
65 #ifdef CONFIG_USER_ONLY
66 int fd;
67 int running_state;
68 #else
69 CharDriverState *chr;
70 #endif
71 } GDBState;
73 #ifdef CONFIG_USER_ONLY
74 /* XXX: This is not thread safe. Do we care? */
75 static int gdbserver_fd = -1;
77 /* XXX: remove this hack. */
78 static GDBState gdbserver_state;
80 static int get_char(GDBState *s)
81 {
82 uint8_t ch;
83 int ret;
85 for(;;) {
86 ret = recv(s->fd, &ch, 1, 0);
87 if (ret < 0) {
88 if (errno != EINTR && errno != EAGAIN)
89 return -1;
90 } else if (ret == 0) {
91 return -1;
92 } else {
93 break;
94 }
95 }
96 return ch;
97 }
98 #endif
100 /* GDB stub state for use by semihosting syscalls. */
101 static GDBState *gdb_syscall_state;
102 static gdb_syscall_complete_cb gdb_current_syscall_cb;
104 enum {
105 GDB_SYS_UNKNOWN,
106 GDB_SYS_ENABLED,
107 GDB_SYS_DISABLED,
108 } gdb_syscall_mode;
110 /* If gdb is connected when the first semihosting syscall occurs then use
111 remote gdb syscalls. Otherwise use native file IO. */
112 int use_gdb_syscalls(void)
113 {
114 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
115 gdb_syscall_mode = (gdb_syscall_state ? GDB_SYS_ENABLED
116 : GDB_SYS_DISABLED);
117 }
118 return gdb_syscall_mode == GDB_SYS_ENABLED;
119 }
121 static void put_buffer(GDBState *s, const uint8_t *buf, int len)
122 {
123 #ifdef CONFIG_USER_ONLY
124 int ret;
126 while (len > 0) {
127 ret = send(s->fd, buf, len, 0);
128 if (ret < 0) {
129 if (errno != EINTR && errno != EAGAIN)
130 return;
131 } else {
132 buf += ret;
133 len -= ret;
134 }
135 }
136 #else
137 qemu_chr_write(s->chr, buf, len);
138 #endif
139 }
141 static inline int fromhex(int v)
142 {
143 if (v >= '0' && v <= '9')
144 return v - '0';
145 else if (v >= 'A' && v <= 'F')
146 return v - 'A' + 10;
147 else if (v >= 'a' && v <= 'f')
148 return v - 'a' + 10;
149 else
150 return 0;
151 }
153 static inline int tohex(int v)
154 {
155 if (v < 10)
156 return v + '0';
157 else
158 return v - 10 + 'a';
159 }
161 static void memtohex(char *buf, const uint8_t *mem, int len)
162 {
163 int i, c;
164 char *q;
165 q = buf;
166 for(i = 0; i < len; i++) {
167 c = mem[i];
168 *q++ = tohex(c >> 4);
169 *q++ = tohex(c & 0xf);
170 }
171 *q = '\0';
172 }
174 static void hextomem(uint8_t *mem, const char *buf, int len)
175 {
176 int i;
178 for(i = 0; i < len; i++) {
179 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
180 buf += 2;
181 }
182 }
184 /* return -1 if error, 0 if OK */
185 static int put_packet(GDBState *s, char *buf)
186 {
187 int len, csum, i;
188 char *p;
190 #ifdef DEBUG_GDB
191 printf("reply='%s'\n", buf);
192 #endif
194 for(;;) {
195 p = s->last_packet;
196 *(p++) = '$';
197 len = strlen(buf);
198 memcpy(p, buf, len);
199 p += len;
200 csum = 0;
201 for(i = 0; i < len; i++) {
202 csum += buf[i];
203 }
204 *(p++) = '#';
205 *(p++) = tohex((csum >> 4) & 0xf);
206 *(p++) = tohex((csum) & 0xf);
208 s->last_packet_len = p - s->last_packet;
209 put_buffer(s, s->last_packet, s->last_packet_len);
211 #ifdef CONFIG_USER_ONLY
212 i = get_char(s);
213 if (i < 0)
214 return -1;
215 if (i == '+')
216 break;
217 #else
218 break;
219 #endif
220 }
221 return 0;
222 }
224 #if defined(TARGET_I386)
226 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
227 {
228 uint32_t *registers = (uint32_t *)mem_buf;
229 int i, fpus;
231 for(i = 0; i < 8; i++) {
232 registers[i] = env->regs[i];
233 }
234 registers[8] = env->eip;
235 registers[9] = env->eflags;
236 registers[10] = env->segs[R_CS].selector;
237 registers[11] = env->segs[R_SS].selector;
238 registers[12] = env->segs[R_DS].selector;
239 registers[13] = env->segs[R_ES].selector;
240 registers[14] = env->segs[R_FS].selector;
241 registers[15] = env->segs[R_GS].selector;
242 /* XXX: convert floats */
243 for(i = 0; i < 8; i++) {
244 memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10);
245 }
246 registers[36] = env->fpuc;
247 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
248 registers[37] = fpus;
249 registers[38] = 0; /* XXX: convert tags */
250 registers[39] = 0; /* fiseg */
251 registers[40] = 0; /* fioff */
252 registers[41] = 0; /* foseg */
253 registers[42] = 0; /* fooff */
254 registers[43] = 0; /* fop */
256 for(i = 0; i < 16; i++)
257 tswapls(&registers[i]);
258 for(i = 36; i < 44; i++)
259 tswapls(&registers[i]);
260 return 44 * 4;
261 }
263 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
264 {
265 uint32_t *registers = (uint32_t *)mem_buf;
266 int i;
268 for(i = 0; i < 8; i++) {
269 env->regs[i] = tswapl(registers[i]);
270 }
271 env->eip = tswapl(registers[8]);
272 env->eflags = tswapl(registers[9]);
273 #if defined(CONFIG_USER_ONLY)
274 #define LOAD_SEG(index, sreg)\
275 if (tswapl(registers[index]) != env->segs[sreg].selector)\
276 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
277 LOAD_SEG(10, R_CS);
278 LOAD_SEG(11, R_SS);
279 LOAD_SEG(12, R_DS);
280 LOAD_SEG(13, R_ES);
281 LOAD_SEG(14, R_FS);
282 LOAD_SEG(15, R_GS);
283 #endif
284 }
286 #elif defined (TARGET_PPC)
287 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
288 {
289 uint32_t *registers = (uint32_t *)mem_buf, tmp;
290 int i;
292 /* fill in gprs */
293 for(i = 0; i < 32; i++) {
294 registers[i] = tswapl(env->gpr[i]);
295 }
296 /* fill in fprs */
297 for (i = 0; i < 32; i++) {
298 registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
299 registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1));
300 }
301 /* nip, msr, ccr, lnk, ctr, xer, mq */
302 registers[96] = tswapl(env->nip);
303 registers[97] = tswapl(do_load_msr(env));
304 tmp = 0;
305 for (i = 0; i < 8; i++)
306 tmp |= env->crf[i] << (32 - ((i + 1) * 4));
307 registers[98] = tswapl(tmp);
308 registers[99] = tswapl(env->lr);
309 registers[100] = tswapl(env->ctr);
310 registers[101] = tswapl(do_load_xer(env));
311 registers[102] = 0;
313 return 103 * 4;
314 }
316 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
317 {
318 uint32_t *registers = (uint32_t *)mem_buf;
319 int i;
321 /* fill in gprs */
322 for (i = 0; i < 32; i++) {
323 env->gpr[i] = tswapl(registers[i]);
324 }
325 /* fill in fprs */
326 for (i = 0; i < 32; i++) {
327 *((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]);
328 *((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]);
329 }
330 /* nip, msr, ccr, lnk, ctr, xer, mq */
331 env->nip = tswapl(registers[96]);
332 do_store_msr(env, tswapl(registers[97]));
333 registers[98] = tswapl(registers[98]);
334 for (i = 0; i < 8; i++)
335 env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF;
336 env->lr = tswapl(registers[99]);
337 env->ctr = tswapl(registers[100]);
338 do_store_xer(env, tswapl(registers[101]));
339 }
340 #elif defined (TARGET_SPARC)
341 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
342 {
343 target_ulong *registers = (target_ulong *)mem_buf;
344 int i;
346 /* fill in g0..g7 */
347 for(i = 0; i < 8; i++) {
348 registers[i] = tswapl(env->gregs[i]);
349 }
350 /* fill in register window */
351 for(i = 0; i < 24; i++) {
352 registers[i + 8] = tswapl(env->regwptr[i]);
353 }
354 #ifndef TARGET_SPARC64
355 /* fill in fprs */
356 for (i = 0; i < 32; i++) {
357 registers[i + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
358 }
359 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
360 registers[64] = tswapl(env->y);
361 {
362 target_ulong tmp;
364 tmp = GET_PSR(env);
365 registers[65] = tswapl(tmp);
366 }
367 registers[66] = tswapl(env->wim);
368 registers[67] = tswapl(env->tbr);
369 registers[68] = tswapl(env->pc);
370 registers[69] = tswapl(env->npc);
371 registers[70] = tswapl(env->fsr);
372 registers[71] = 0; /* csr */
373 registers[72] = 0;
374 return 73 * sizeof(target_ulong);
375 #else
376 /* fill in fprs */
377 for (i = 0; i < 64; i += 2) {
378 uint64_t tmp;
380 tmp = (uint64_t)tswap32(*((uint32_t *)&env->fpr[i])) << 32;
381 tmp |= tswap32(*((uint32_t *)&env->fpr[i + 1]));
382 registers[i/2 + 32] = tmp;
383 }
384 registers[64] = tswapl(env->pc);
385 registers[65] = tswapl(env->npc);
386 registers[66] = tswapl(env->tstate[env->tl]);
387 registers[67] = tswapl(env->fsr);
388 registers[68] = tswapl(env->fprs);
389 registers[69] = tswapl(env->y);
390 return 70 * sizeof(target_ulong);
391 #endif
392 }
394 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
395 {
396 target_ulong *registers = (target_ulong *)mem_buf;
397 int i;
399 /* fill in g0..g7 */
400 for(i = 0; i < 7; i++) {
401 env->gregs[i] = tswapl(registers[i]);
402 }
403 /* fill in register window */
404 for(i = 0; i < 24; i++) {
405 env->regwptr[i] = tswapl(registers[i + 8]);
406 }
407 #ifndef TARGET_SPARC64
408 /* fill in fprs */
409 for (i = 0; i < 32; i++) {
410 *((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]);
411 }
412 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
413 env->y = tswapl(registers[64]);
414 PUT_PSR(env, tswapl(registers[65]));
415 env->wim = tswapl(registers[66]);
416 env->tbr = tswapl(registers[67]);
417 env->pc = tswapl(registers[68]);
418 env->npc = tswapl(registers[69]);
419 env->fsr = tswapl(registers[70]);
420 #else
421 for (i = 0; i < 64; i += 2) {
422 *((uint32_t *)&env->fpr[i]) = tswap32(registers[i/2 + 32] >> 32);
423 *((uint32_t *)&env->fpr[i + 1]) = tswap32(registers[i/2 + 32] & 0xffffffff);
424 }
425 env->pc = tswapl(registers[64]);
426 env->npc = tswapl(registers[65]);
427 env->tstate[env->tl] = tswapl(registers[66]);
428 env->fsr = tswapl(registers[67]);
429 env->fprs = tswapl(registers[68]);
430 env->y = tswapl(registers[69]);
431 #endif
432 }
433 #elif defined (TARGET_ARM)
434 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
435 {
436 int i;
437 uint8_t *ptr;
439 ptr = mem_buf;
440 /* 16 core integer registers (4 bytes each). */
441 for (i = 0; i < 16; i++)
442 {
443 *(uint32_t *)ptr = tswapl(env->regs[i]);
444 ptr += 4;
445 }
446 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
447 Not yet implemented. */
448 memset (ptr, 0, 8 * 12 + 4);
449 ptr += 8 * 12 + 4;
450 /* CPSR (4 bytes). */
451 *(uint32_t *)ptr = tswapl (cpsr_read(env));
452 ptr += 4;
454 return ptr - mem_buf;
455 }
457 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
458 {
459 int i;
460 uint8_t *ptr;
462 ptr = mem_buf;
463 /* Core integer registers. */
464 for (i = 0; i < 16; i++)
465 {
466 env->regs[i] = tswapl(*(uint32_t *)ptr);
467 ptr += 4;
468 }
469 /* Ignore FPA regs and scr. */
470 ptr += 8 * 12 + 4;
471 cpsr_write (env, tswapl(*(uint32_t *)ptr), 0xffffffff);
472 }
473 #elif defined (TARGET_M68K)
474 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
475 {
476 int i;
477 uint8_t *ptr;
478 CPU_DoubleU u;
480 ptr = mem_buf;
481 /* D0-D7 */
482 for (i = 0; i < 8; i++) {
483 *(uint32_t *)ptr = tswapl(env->dregs[i]);
484 ptr += 4;
485 }
486 /* A0-A7 */
487 for (i = 0; i < 8; i++) {
488 *(uint32_t *)ptr = tswapl(env->aregs[i]);
489 ptr += 4;
490 }
491 *(uint32_t *)ptr = tswapl(env->sr);
492 ptr += 4;
493 *(uint32_t *)ptr = tswapl(env->pc);
494 ptr += 4;
495 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
496 ColdFire has 8-bit double precision registers. */
497 for (i = 0; i < 8; i++) {
498 u.d = env->fregs[i];
499 *(uint32_t *)ptr = tswap32(u.l.upper);
500 *(uint32_t *)ptr = tswap32(u.l.lower);
501 }
502 /* FP control regs (not implemented). */
503 memset (ptr, 0, 3 * 4);
504 ptr += 3 * 4;
506 return ptr - mem_buf;
507 }
509 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
510 {
511 int i;
512 uint8_t *ptr;
513 CPU_DoubleU u;
515 ptr = mem_buf;
516 /* D0-D7 */
517 for (i = 0; i < 8; i++) {
518 env->dregs[i] = tswapl(*(uint32_t *)ptr);
519 ptr += 4;
520 }
521 /* A0-A7 */
522 for (i = 0; i < 8; i++) {
523 env->aregs[i] = tswapl(*(uint32_t *)ptr);
524 ptr += 4;
525 }
526 env->sr = tswapl(*(uint32_t *)ptr);
527 ptr += 4;
528 env->pc = tswapl(*(uint32_t *)ptr);
529 ptr += 4;
530 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
531 ColdFire has 8-bit double precision registers. */
532 for (i = 0; i < 8; i++) {
533 u.l.upper = tswap32(*(uint32_t *)ptr);
534 u.l.lower = tswap32(*(uint32_t *)ptr);
535 env->fregs[i] = u.d;
536 }
537 /* FP control regs (not implemented). */
538 ptr += 3 * 4;
539 }
540 #elif defined (TARGET_MIPS)
541 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
542 {
543 int i;
544 uint8_t *ptr;
546 ptr = mem_buf;
547 for (i = 0; i < 32; i++)
548 {
549 *(uint32_t *)ptr = tswapl(env->gpr[i]);
550 ptr += 4;
551 }
553 *(uint32_t *)ptr = tswapl(env->CP0_Status);
554 ptr += 4;
556 *(uint32_t *)ptr = tswapl(env->LO);
557 ptr += 4;
559 *(uint32_t *)ptr = tswapl(env->HI);
560 ptr += 4;
562 *(uint32_t *)ptr = tswapl(env->CP0_BadVAddr);
563 ptr += 4;
565 *(uint32_t *)ptr = tswapl(env->CP0_Cause);
566 ptr += 4;
568 *(uint32_t *)ptr = tswapl(env->PC);
569 ptr += 4;
571 #ifdef MIPS_USES_FPU
572 for (i = 0; i < 32; i++)
573 {
574 *(uint32_t *)ptr = tswapl(FPR_W (env, i));
575 ptr += 4;
576 }
578 *(uint32_t *)ptr = tswapl(env->fcr31);
579 ptr += 4;
581 *(uint32_t *)ptr = tswapl(env->fcr0);
582 ptr += 4;
583 #endif
585 /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
586 /* what's 'fp' mean here? */
588 return ptr - mem_buf;
589 }
591 /* convert MIPS rounding mode in FCR31 to IEEE library */
592 static unsigned int ieee_rm[] =
593 {
594 float_round_nearest_even,
595 float_round_to_zero,
596 float_round_up,
597 float_round_down
598 };
599 #define RESTORE_ROUNDING_MODE \
600 set_float_rounding_mode(ieee_rm[env->fcr31 & 3], &env->fp_status)
602 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
603 {
604 int i;
605 uint8_t *ptr;
607 ptr = mem_buf;
608 for (i = 0; i < 32; i++)
609 {
610 env->gpr[i] = tswapl(*(uint32_t *)ptr);
611 ptr += 4;
612 }
614 env->CP0_Status = tswapl(*(uint32_t *)ptr);
615 ptr += 4;
617 env->LO = tswapl(*(uint32_t *)ptr);
618 ptr += 4;
620 env->HI = tswapl(*(uint32_t *)ptr);
621 ptr += 4;
623 env->CP0_BadVAddr = tswapl(*(uint32_t *)ptr);
624 ptr += 4;
626 env->CP0_Cause = tswapl(*(uint32_t *)ptr);
627 ptr += 4;
629 env->PC = tswapl(*(uint32_t *)ptr);
630 ptr += 4;
632 #ifdef MIPS_USES_FPU
633 for (i = 0; i < 32; i++)
634 {
635 FPR_W (env, i) = tswapl(*(uint32_t *)ptr);
636 ptr += 4;
637 }
639 env->fcr31 = tswapl(*(uint32_t *)ptr) & 0x0183FFFF;
640 ptr += 4;
642 env->fcr0 = tswapl(*(uint32_t *)ptr);
643 ptr += 4;
645 /* set rounding mode */
646 RESTORE_ROUNDING_MODE;
648 #ifndef CONFIG_SOFTFLOAT
649 /* no floating point exception for native float */
650 SET_FP_ENABLE(env->fcr31, 0);
651 #endif
652 #endif
653 }
654 #elif defined (TARGET_SH4)
655 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
656 {
657 uint32_t *ptr = (uint32_t *)mem_buf;
658 int i;
660 #define SAVE(x) *ptr++=tswapl(x)
661 if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
662 for (i = 0; i < 8; i++) SAVE(env->gregs[i + 16]);
663 } else {
664 for (i = 0; i < 8; i++) SAVE(env->gregs[i]);
665 }
666 for (i = 8; i < 16; i++) SAVE(env->gregs[i]);
667 SAVE (env->pc);
668 SAVE (env->pr);
669 SAVE (env->gbr);
670 SAVE (env->vbr);
671 SAVE (env->mach);
672 SAVE (env->macl);
673 SAVE (env->sr);
674 SAVE (0); /* TICKS */
675 SAVE (0); /* STALLS */
676 SAVE (0); /* CYCLES */
677 SAVE (0); /* INSTS */
678 SAVE (0); /* PLR */
680 return ((uint8_t *)ptr - mem_buf);
681 }
683 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
684 {
685 uint32_t *ptr = (uint32_t *)mem_buf;
686 int i;
688 #define LOAD(x) (x)=*ptr++;
689 if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
690 for (i = 0; i < 8; i++) LOAD(env->gregs[i + 16]);
691 } else {
692 for (i = 0; i < 8; i++) LOAD(env->gregs[i]);
693 }
694 for (i = 8; i < 16; i++) LOAD(env->gregs[i]);
695 LOAD (env->pc);
696 LOAD (env->pr);
697 LOAD (env->gbr);
698 LOAD (env->vbr);
699 LOAD (env->mach);
700 LOAD (env->macl);
701 LOAD (env->sr);
702 }
703 #else
704 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
705 {
706 return 0;
707 }
709 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
710 {
711 }
713 #endif
715 static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf)
716 {
717 const char *p;
718 int ch, reg_size, type;
719 char buf[4096];
720 uint8_t mem_buf[2000];
721 uint32_t *registers;
722 target_ulong addr, len;
724 #ifdef DEBUG_GDB
725 printf("command='%s'\n", line_buf);
726 #endif
727 p = line_buf;
728 ch = *p++;
729 switch(ch) {
730 case '?':
731 /* TODO: Make this return the correct value for user-mode. */
732 snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
733 put_packet(s, buf);
734 break;
735 case 'c':
736 if (*p != '\0') {
737 addr = strtoull(p, (char **)&p, 16);
738 #if defined(TARGET_I386)
739 env->eip = addr;
740 #elif defined (TARGET_PPC)
741 env->nip = addr;
742 #elif defined (TARGET_SPARC)
743 env->pc = addr;
744 env->npc = addr + 4;
745 #elif defined (TARGET_ARM)
746 env->regs[15] = addr;
747 #elif defined (TARGET_SH4)
748 env->pc = addr;
749 #endif
750 }
751 #ifdef CONFIG_USER_ONLY
752 s->running_state = 1;
753 #else
754 vm_start();
755 #endif
756 return RS_IDLE;
757 case 's':
758 if (*p != '\0') {
759 addr = strtoul(p, (char **)&p, 16);
760 #if defined(TARGET_I386)
761 env->eip = addr;
762 #elif defined (TARGET_PPC)
763 env->nip = addr;
764 #elif defined (TARGET_SPARC)
765 env->pc = addr;
766 env->npc = addr + 4;
767 #elif defined (TARGET_ARM)
768 env->regs[15] = addr;
769 #elif defined (TARGET_SH4)
770 env->pc = addr;
771 #endif
772 }
773 cpu_single_step(env, 1);
774 #ifdef CONFIG_USER_ONLY
775 s->running_state = 1;
776 #else
777 vm_start();
778 #endif
779 return RS_IDLE;
780 case 'F':
781 {
782 target_ulong ret;
783 target_ulong err;
785 ret = strtoull(p, (char **)&p, 16);
786 if (*p == ',') {
787 p++;
788 err = strtoull(p, (char **)&p, 16);
789 } else {
790 err = 0;
791 }
792 if (*p == ',')
793 p++;
794 type = *p;
795 if (gdb_current_syscall_cb)
796 gdb_current_syscall_cb(s->env, ret, err);
797 if (type == 'C') {
798 put_packet(s, "T02");
799 } else {
800 #ifdef CONFIG_USER_ONLY
801 s->running_state = 1;
802 #else
803 vm_start();
804 #endif
805 }
806 }
807 break;
808 case 'g':
809 reg_size = cpu_gdb_read_registers(env, mem_buf);
810 memtohex(buf, mem_buf, reg_size);
811 put_packet(s, buf);
812 break;
813 case 'G':
814 registers = (void *)mem_buf;
815 len = strlen(p) / 2;
816 hextomem((uint8_t *)registers, p, len);
817 cpu_gdb_write_registers(env, mem_buf, len);
818 put_packet(s, "OK");
819 break;
820 case 'm':
821 addr = strtoull(p, (char **)&p, 16);
822 if (*p == ',')
823 p++;
824 len = strtoull(p, NULL, 16);
825 if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) {
826 put_packet (s, "E14");
827 } else {
828 memtohex(buf, mem_buf, len);
829 put_packet(s, buf);
830 }
831 break;
832 case 'M':
833 addr = strtoull(p, (char **)&p, 16);
834 if (*p == ',')
835 p++;
836 len = strtoull(p, (char **)&p, 16);
837 if (*p == ':')
838 p++;
839 hextomem(mem_buf, p, len);
840 if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0)
841 put_packet(s, "E14");
842 else
843 put_packet(s, "OK");
844 break;
845 case 'Z':
846 type = strtoul(p, (char **)&p, 16);
847 if (*p == ',')
848 p++;
849 addr = strtoull(p, (char **)&p, 16);
850 if (*p == ',')
851 p++;
852 len = strtoull(p, (char **)&p, 16);
853 if (type == 0 || type == 1) {
854 if (cpu_breakpoint_insert(env, addr) < 0)
855 goto breakpoint_error;
856 put_packet(s, "OK");
857 } else {
858 breakpoint_error:
859 put_packet(s, "E22");
860 }
861 break;
862 case 'z':
863 type = strtoul(p, (char **)&p, 16);
864 if (*p == ',')
865 p++;
866 addr = strtoull(p, (char **)&p, 16);
867 if (*p == ',')
868 p++;
869 len = strtoull(p, (char **)&p, 16);
870 if (type == 0 || type == 1) {
871 cpu_breakpoint_remove(env, addr);
872 put_packet(s, "OK");
873 } else {
874 goto breakpoint_error;
875 }
876 break;
877 #ifdef CONFIG_LINUX_USER
878 case 'q':
879 if (strncmp(p, "Offsets", 7) == 0) {
880 TaskState *ts = env->opaque;
882 sprintf(buf, "Text=%x;Data=%x;Bss=%x", ts->info->code_offset,
883 ts->info->data_offset, ts->info->data_offset);
884 put_packet(s, buf);
885 break;
886 }
887 /* Fall through. */
888 #endif
889 default:
890 // unknown_command:
891 /* put empty packet */
892 buf[0] = '\0';
893 put_packet(s, buf);
894 break;
895 }
896 return RS_IDLE;
897 }
899 extern void tb_flush(CPUState *env);
901 #ifndef CONFIG_USER_ONLY
902 static void gdb_vm_stopped(void *opaque, int reason)
903 {
904 GDBState *s = opaque;
905 char buf[256];
906 int ret;
908 if (s->state == RS_SYSCALL)
909 return;
911 /* disable single step if it was enable */
912 cpu_single_step(s->env, 0);
914 if (reason == EXCP_DEBUG) {
915 tb_flush(s->env);
916 ret = SIGTRAP;
917 } else if (reason == EXCP_INTERRUPT) {
918 ret = SIGINT;
919 } else {
920 ret = 0;
921 }
922 snprintf(buf, sizeof(buf), "S%02x", ret);
923 put_packet(s, buf);
924 }
925 #endif
927 /* Send a gdb syscall request.
928 This accepts limited printf-style format specifiers, specifically:
929 %x - target_ulong argument printed in hex.
930 %s - string pointer (target_ulong) and length (int) pair. */
931 void gdb_do_syscall(gdb_syscall_complete_cb cb, char *fmt, ...)
932 {
933 va_list va;
934 char buf[256];
935 char *p;
936 target_ulong addr;
937 GDBState *s;
939 s = gdb_syscall_state;
940 if (!s)
941 return;
942 gdb_current_syscall_cb = cb;
943 s->state = RS_SYSCALL;
944 #ifndef CONFIG_USER_ONLY
945 vm_stop(EXCP_DEBUG);
946 #endif
947 s->state = RS_IDLE;
948 va_start(va, fmt);
949 p = buf;
950 *(p++) = 'F';
951 while (*fmt) {
952 if (*fmt == '%') {
953 fmt++;
954 switch (*fmt++) {
955 case 'x':
956 addr = va_arg(va, target_ulong);
957 p += sprintf(p, TARGET_FMT_lx, addr);
958 break;
959 case 's':
960 addr = va_arg(va, target_ulong);
961 p += sprintf(p, TARGET_FMT_lx "/%x", addr, va_arg(va, int));
962 break;
963 default:
964 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
965 fmt - 1);
966 break;
967 }
968 } else {
969 *(p++) = *(fmt++);
970 }
971 }
972 va_end(va);
973 put_packet(s, buf);
974 #ifdef CONFIG_USER_ONLY
975 gdb_handlesig(s->env, 0);
976 #else
977 cpu_interrupt(s->env, CPU_INTERRUPT_EXIT);
978 #endif
979 }
981 static void gdb_read_byte(GDBState *s, int ch)
982 {
983 CPUState *env = s->env;
984 int i, csum;
985 char reply[1];
987 #ifndef CONFIG_USER_ONLY
988 if (s->last_packet_len) {
989 /* Waiting for a response to the last packet. If we see the start
990 of a new command then abandon the previous response. */
991 if (ch == '-') {
992 #ifdef DEBUG_GDB
993 printf("Got NACK, retransmitting\n");
994 #endif
995 put_buffer(s, s->last_packet, s->last_packet_len);
996 }
997 #ifdef DEBUG_GDB
998 else if (ch == '+')
999 printf("Got ACK\n");
1000 else
1001 printf("Got '%c' when expecting ACK/NACK\n", ch);
1002 #endif
1003 if (ch == '+' || ch == '$')
1004 s->last_packet_len = 0;
1005 if (ch != '$')
1006 return;
1008 if (vm_running) {
1009 /* when the CPU is running, we cannot do anything except stop
1010 it when receiving a char */
1011 vm_stop(EXCP_INTERRUPT);
1012 } else
1013 #endif
1015 switch(s->state) {
1016 case RS_IDLE:
1017 if (ch == '$') {
1018 s->line_buf_index = 0;
1019 s->state = RS_GETLINE;
1021 break;
1022 case RS_GETLINE:
1023 if (ch == '#') {
1024 s->state = RS_CHKSUM1;
1025 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1026 s->state = RS_IDLE;
1027 } else {
1028 s->line_buf[s->line_buf_index++] = ch;
1030 break;
1031 case RS_CHKSUM1:
1032 s->line_buf[s->line_buf_index] = '\0';
1033 s->line_csum = fromhex(ch) << 4;
1034 s->state = RS_CHKSUM2;
1035 break;
1036 case RS_CHKSUM2:
1037 s->line_csum |= fromhex(ch);
1038 csum = 0;
1039 for(i = 0; i < s->line_buf_index; i++) {
1040 csum += s->line_buf[i];
1042 if (s->line_csum != (csum & 0xff)) {
1043 reply[0] = '-';
1044 put_buffer(s, reply, 1);
1045 s->state = RS_IDLE;
1046 } else {
1047 reply[0] = '+';
1048 put_buffer(s, reply, 1);
1049 s->state = gdb_handle_packet(s, env, s->line_buf);
1051 break;
1052 default:
1053 abort();
1058 #ifdef CONFIG_USER_ONLY
1059 int
1060 gdb_handlesig (CPUState *env, int sig)
1062 GDBState *s;
1063 char buf[256];
1064 int n;
1066 if (gdbserver_fd < 0)
1067 return sig;
1069 s = &gdbserver_state;
1071 /* disable single step if it was enabled */
1072 cpu_single_step(env, 0);
1073 tb_flush(env);
1075 if (sig != 0)
1077 snprintf(buf, sizeof(buf), "S%02x", sig);
1078 put_packet(s, buf);
1081 sig = 0;
1082 s->state = RS_IDLE;
1083 s->running_state = 0;
1084 while (s->running_state == 0) {
1085 n = read (s->fd, buf, 256);
1086 if (n > 0)
1088 int i;
1090 for (i = 0; i < n; i++)
1091 gdb_read_byte (s, buf[i]);
1093 else if (n == 0 || errno != EAGAIN)
1095 /* XXX: Connection closed. Should probably wait for annother
1096 connection before continuing. */
1097 return sig;
1100 return sig;
1103 /* Tell the remote gdb that the process has exited. */
1104 void gdb_exit(CPUState *env, int code)
1106 GDBState *s;
1107 char buf[4];
1109 if (gdbserver_fd < 0)
1110 return;
1112 s = &gdbserver_state;
1114 snprintf(buf, sizeof(buf), "W%02x", code);
1115 put_packet(s, buf);
1119 static void gdb_accept(void *opaque)
1121 GDBState *s;
1122 struct sockaddr_in sockaddr;
1123 socklen_t len;
1124 int val, fd;
1126 for(;;) {
1127 len = sizeof(sockaddr);
1128 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1129 if (fd < 0 && errno != EINTR) {
1130 perror("accept");
1131 return;
1132 } else if (fd >= 0) {
1133 break;
1137 /* set short latency */
1138 val = 1;
1139 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
1141 s = &gdbserver_state;
1142 memset (s, 0, sizeof (GDBState));
1143 s->env = first_cpu; /* XXX: allow to change CPU */
1144 s->fd = fd;
1146 gdb_syscall_state = s;
1148 fcntl(fd, F_SETFL, O_NONBLOCK);
1151 static int gdbserver_open(int port)
1153 struct sockaddr_in sockaddr;
1154 int fd, val, ret;
1156 fd = socket(PF_INET, SOCK_STREAM, 0);
1157 if (fd < 0) {
1158 perror("socket");
1159 return -1;
1162 /* allow fast reuse */
1163 val = 1;
1164 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
1166 sockaddr.sin_family = AF_INET;
1167 sockaddr.sin_port = htons(port);
1168 sockaddr.sin_addr.s_addr = 0;
1169 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1170 if (ret < 0) {
1171 perror("bind");
1172 return -1;
1174 ret = listen(fd, 0);
1175 if (ret < 0) {
1176 perror("listen");
1177 return -1;
1179 return fd;
1182 int gdbserver_start(int port)
1184 gdbserver_fd = gdbserver_open(port);
1185 if (gdbserver_fd < 0)
1186 return -1;
1187 /* accept connections */
1188 gdb_accept (NULL);
1189 return 0;
1191 #else
1192 static int gdb_chr_can_recieve(void *opaque)
1194 return 1;
1197 static void gdb_chr_recieve(void *opaque, const uint8_t *buf, int size)
1199 GDBState *s = opaque;
1200 int i;
1202 for (i = 0; i < size; i++) {
1203 gdb_read_byte(s, buf[i]);
1207 static void gdb_chr_event(void *opaque, int event)
1209 switch (event) {
1210 case CHR_EVENT_RESET:
1211 vm_stop(EXCP_INTERRUPT);
1212 gdb_syscall_state = opaque;
1213 break;
1214 default:
1215 break;
1219 int gdbserver_start(CharDriverState *chr)
1221 GDBState *s;
1223 if (!chr)
1224 return -1;
1226 s = qemu_mallocz(sizeof(GDBState));
1227 if (!s) {
1228 return -1;
1230 s->env = first_cpu; /* XXX: allow to change CPU */
1231 s->chr = chr;
1232 qemu_chr_add_handlers(chr, gdb_chr_can_recieve, gdb_chr_recieve,
1233 gdb_chr_event, s);
1234 qemu_add_vm_stop_handler(gdb_vm_stopped, s);
1235 return 0;
1238 int gdbserver_start_port(int port)
1240 CharDriverState *chr;
1241 char gdbstub_port_name[128];
1243 snprintf(gdbstub_port_name, sizeof(gdbstub_port_name),
1244 "tcp::%d,nowait,nodelay,server", port);
1245 chr = qemu_chr_open(gdbstub_port_name);
1246 if (!chr)
1247 return -EIO;
1248 return gdbserver_start(chr);
1251 #endif