4 * Copyright (c) 2003-2005 Fabrice Bellard
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
21 #include "qemu-common.h"
22 #ifdef CONFIG_USER_ONLY
33 #include "qemu-char.h"
39 #define MAX_PACKET_LENGTH 4096
41 #include "qemu_socket.h"
48 GDB_SIGNAL_UNKNOWN
= 143
51 #ifdef CONFIG_USER_ONLY
53 /* Map target signal numbers to GDB protocol signal numbers and vice
54 * versa. For user emulation's currently supported systems, we can
55 * assume most signals are defined.
58 static int gdb_signal_table
[] = {
218 /* In system mode we only need SIGINT and SIGTRAP; other signals
219 are not yet supported. */
226 static int gdb_signal_table
[] = {
236 #ifdef CONFIG_USER_ONLY
237 static int target_signal_to_gdb (int sig
)
240 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
241 if (gdb_signal_table
[i
] == sig
)
243 return GDB_SIGNAL_UNKNOWN
;
247 static int gdb_signal_to_target (int sig
)
249 if (sig
< ARRAY_SIZE (gdb_signal_table
))
250 return gdb_signal_table
[sig
];
257 typedef struct GDBRegisterState
{
263 struct GDBRegisterState
*next
;
273 typedef struct GDBState
{
274 CPUState
*c_cpu
; /* current CPU for step/continue ops */
275 CPUState
*g_cpu
; /* current CPU for other ops */
276 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
277 enum RSState state
; /* parsing state */
278 char line_buf
[MAX_PACKET_LENGTH
];
281 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
284 #ifdef CONFIG_USER_ONLY
288 CharDriverState
*chr
;
292 /* By default use no IRQs and no timers while single stepping so as to
293 * make single stepping like an ICE HW step.
295 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
297 static GDBState
*gdbserver_state
;
299 /* This is an ugly hack to cope with both new and old gdb.
300 If gdb sends qXfer:features:read then assume we're talking to a newish
301 gdb that understands target descriptions. */
302 static int gdb_has_xml
;
304 #ifdef CONFIG_USER_ONLY
305 /* XXX: This is not thread safe. Do we care? */
306 static int gdbserver_fd
= -1;
308 static int get_char(GDBState
*s
)
314 ret
= recv(s
->fd
, &ch
, 1, 0);
316 if (errno
== ECONNRESET
)
318 if (errno
!= EINTR
&& errno
!= EAGAIN
)
320 } else if (ret
== 0) {
332 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
340 /* If gdb is connected when the first semihosting syscall occurs then use
341 remote gdb syscalls. Otherwise use native file IO. */
342 int use_gdb_syscalls(void)
344 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
345 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
348 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
351 /* Resume execution. */
352 static inline void gdb_continue(GDBState
*s
)
354 #ifdef CONFIG_USER_ONLY
355 s
->running_state
= 1;
361 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
363 #ifdef CONFIG_USER_ONLY
367 ret
= send(s
->fd
, buf
, len
, 0);
369 if (errno
!= EINTR
&& errno
!= EAGAIN
)
377 qemu_chr_write(s
->chr
, buf
, len
);
381 static inline int fromhex(int v
)
383 if (v
>= '0' && v
<= '9')
385 else if (v
>= 'A' && v
<= 'F')
387 else if (v
>= 'a' && v
<= 'f')
393 static inline int tohex(int v
)
401 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
406 for(i
= 0; i
< len
; i
++) {
408 *q
++ = tohex(c
>> 4);
409 *q
++ = tohex(c
& 0xf);
414 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
418 for(i
= 0; i
< len
; i
++) {
419 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
424 /* return -1 if error, 0 if OK */
425 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
)
436 for(i
= 0; i
< len
; i
++) {
440 *(p
++) = tohex((csum
>> 4) & 0xf);
441 *(p
++) = tohex((csum
) & 0xf);
443 s
->last_packet_len
= p
- s
->last_packet
;
444 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
446 #ifdef CONFIG_USER_ONLY
459 /* return -1 if error, 0 if OK */
460 static int put_packet(GDBState
*s
, const char *buf
)
463 printf("reply='%s'\n", buf
);
466 return put_packet_binary(s
, buf
, strlen(buf
));
469 /* The GDB remote protocol transfers values in target byte order. This means
470 we can use the raw memory access routines to access the value buffer.
471 Conveniently, these also handle the case where the buffer is mis-aligned.
473 #define GET_REG8(val) do { \
474 stb_p(mem_buf, val); \
477 #define GET_REG16(val) do { \
478 stw_p(mem_buf, val); \
481 #define GET_REG32(val) do { \
482 stl_p(mem_buf, val); \
485 #define GET_REG64(val) do { \
486 stq_p(mem_buf, val); \
490 #if TARGET_LONG_BITS == 64
491 #define GET_REGL(val) GET_REG64(val)
492 #define ldtul_p(addr) ldq_p(addr)
494 #define GET_REGL(val) GET_REG32(val)
495 #define ldtul_p(addr) ldl_p(addr)
498 #if defined(TARGET_I386)
501 static const int gpr_map
[16] = {
502 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
503 8, 9, 10, 11, 12, 13, 14, 15
506 static const int gpr_map
[8] = {0, 1, 2, 3, 4, 5, 6, 7};
509 #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
511 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
513 if (n
< CPU_NB_REGS
) {
514 GET_REGL(env
->regs
[gpr_map
[n
]]);
515 } else if (n
>= CPU_NB_REGS
+ 8 && n
< CPU_NB_REGS
+ 16) {
516 /* FIXME: byteswap float values. */
517 #ifdef USE_X86LDOUBLE
518 memcpy(mem_buf
, &env
->fpregs
[n
- (CPU_NB_REGS
+ 8)], 10);
520 memset(mem_buf
, 0, 10);
523 } else if (n
>= CPU_NB_REGS
+ 24) {
524 n
-= CPU_NB_REGS
+ 24;
525 if (n
< CPU_NB_REGS
) {
526 stq_p(mem_buf
, env
->xmm_regs
[n
].XMM_Q(0));
527 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].XMM_Q(1));
529 } else if (n
== CPU_NB_REGS
) {
530 GET_REG32(env
->mxcsr
);
535 case 0: GET_REGL(env
->eip
);
536 case 1: GET_REG32(env
->eflags
);
537 case 2: GET_REG32(env
->segs
[R_CS
].selector
);
538 case 3: GET_REG32(env
->segs
[R_SS
].selector
);
539 case 4: GET_REG32(env
->segs
[R_DS
].selector
);
540 case 5: GET_REG32(env
->segs
[R_ES
].selector
);
541 case 6: GET_REG32(env
->segs
[R_FS
].selector
);
542 case 7: GET_REG32(env
->segs
[R_GS
].selector
);
543 /* 8...15 x87 regs. */
544 case 16: GET_REG32(env
->fpuc
);
545 case 17: GET_REG32((env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11);
546 case 18: GET_REG32(0); /* ftag */
547 case 19: GET_REG32(0); /* fiseg */
548 case 20: GET_REG32(0); /* fioff */
549 case 21: GET_REG32(0); /* foseg */
550 case 22: GET_REG32(0); /* fooff */
551 case 23: GET_REG32(0); /* fop */
558 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int i
)
562 if (i
< CPU_NB_REGS
) {
563 env
->regs
[gpr_map
[i
]] = ldtul_p(mem_buf
);
564 return sizeof(target_ulong
);
565 } else if (i
>= CPU_NB_REGS
+ 8 && i
< CPU_NB_REGS
+ 16) {
566 i
-= CPU_NB_REGS
+ 8;
567 #ifdef USE_X86LDOUBLE
568 memcpy(&env
->fpregs
[i
], mem_buf
, 10);
571 } else if (i
>= CPU_NB_REGS
+ 24) {
572 i
-= CPU_NB_REGS
+ 24;
573 if (i
< CPU_NB_REGS
) {
574 env
->xmm_regs
[i
].XMM_Q(0) = ldq_p(mem_buf
);
575 env
->xmm_regs
[i
].XMM_Q(1) = ldq_p(mem_buf
+ 8);
577 } else if (i
== CPU_NB_REGS
) {
578 env
->mxcsr
= ldl_p(mem_buf
);
584 case 0: env
->eip
= ldtul_p(mem_buf
); return sizeof(target_ulong
);
585 case 1: env
->eflags
= ldl_p(mem_buf
); return 4;
586 #if defined(CONFIG_USER_ONLY)
587 #define LOAD_SEG(index, sreg)\
588 tmp = ldl_p(mem_buf);\
589 if (tmp != env->segs[sreg].selector)\
590 cpu_x86_load_seg(env, sreg, tmp);
592 /* FIXME: Honor segment registers. Needs to avoid raising an exception
593 when the selector is invalid. */
594 #define LOAD_SEG(index, sreg) do {} while(0)
596 case 2: LOAD_SEG(10, R_CS
); return 4;
597 case 3: LOAD_SEG(11, R_SS
); return 4;
598 case 4: LOAD_SEG(12, R_DS
); return 4;
599 case 5: LOAD_SEG(13, R_ES
); return 4;
600 case 6: LOAD_SEG(14, R_FS
); return 4;
601 case 7: LOAD_SEG(15, R_GS
); return 4;
602 /* 8...15 x87 regs. */
603 case 16: env
->fpuc
= ldl_p(mem_buf
); return 4;
605 tmp
= ldl_p(mem_buf
);
606 env
->fpstt
= (tmp
>> 11) & 7;
607 env
->fpus
= tmp
& ~0x3800;
609 case 18: /* ftag */ return 4;
610 case 19: /* fiseg */ return 4;
611 case 20: /* fioff */ return 4;
612 case 21: /* foseg */ return 4;
613 case 22: /* fooff */ return 4;
614 case 23: /* fop */ return 4;
618 /* Unrecognised register. */
622 #elif defined (TARGET_PPC)
624 #define NUM_CORE_REGS 71
626 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
630 GET_REGL(env
->gpr
[n
]);
633 stfq_p(mem_buf
, env
->fpr
[n
-32]);
637 case 64: GET_REGL(env
->nip
);
638 case 65: GET_REGL(env
->msr
);
643 for (i
= 0; i
< 8; i
++)
644 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
647 case 67: GET_REGL(env
->lr
);
648 case 68: GET_REGL(env
->ctr
);
649 case 69: GET_REGL(env
->xer
);
650 case 70: GET_REG32(0); /* fpscr */
656 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
660 env
->gpr
[n
] = ldtul_p(mem_buf
);
661 return sizeof(target_ulong
);
664 env
->fpr
[n
-32] = ldfq_p(mem_buf
);
669 env
->nip
= ldtul_p(mem_buf
);
670 return sizeof(target_ulong
);
672 ppc_store_msr(env
, ldtul_p(mem_buf
));
673 return sizeof(target_ulong
);
676 uint32_t cr
= ldl_p(mem_buf
);
678 for (i
= 0; i
< 8; i
++)
679 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
683 env
->lr
= ldtul_p(mem_buf
);
684 return sizeof(target_ulong
);
686 env
->ctr
= ldtul_p(mem_buf
);
687 return sizeof(target_ulong
);
689 env
->xer
= ldtul_p(mem_buf
);
690 return sizeof(target_ulong
);
699 #elif defined (TARGET_SPARC)
701 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
702 #define NUM_CORE_REGS 86
704 #define NUM_CORE_REGS 72
708 #define GET_REGA(val) GET_REG32(val)
710 #define GET_REGA(val) GET_REGL(val)
713 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
717 GET_REGA(env
->gregs
[n
]);
720 /* register window */
721 GET_REGA(env
->regwptr
[n
- 8]);
723 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
726 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
728 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
730 case 64: GET_REGA(env
->y
);
731 case 65: GET_REGA(GET_PSR(env
));
732 case 66: GET_REGA(env
->wim
);
733 case 67: GET_REGA(env
->tbr
);
734 case 68: GET_REGA(env
->pc
);
735 case 69: GET_REGA(env
->npc
);
736 case 70: GET_REGA(env
->fsr
);
737 case 71: GET_REGA(0); /* csr */
738 default: GET_REGA(0);
743 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
746 /* f32-f62 (double width, even numbers only) */
749 val
= (uint64_t)*((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) << 32;
750 val
|= *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]);
754 case 80: GET_REGL(env
->pc
);
755 case 81: GET_REGL(env
->npc
);
756 case 82: GET_REGL(((uint64_t)GET_CCR(env
) << 32) |
757 ((env
->asi
& 0xff) << 24) |
758 ((env
->pstate
& 0xfff) << 8) |
760 case 83: GET_REGL(env
->fsr
);
761 case 84: GET_REGL(env
->fprs
);
762 case 85: GET_REGL(env
->y
);
768 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
770 #if defined(TARGET_ABI32)
773 tmp
= ldl_p(mem_buf
);
777 tmp
= ldtul_p(mem_buf
);
784 /* register window */
785 env
->regwptr
[n
- 8] = tmp
;
787 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
790 *((uint32_t *)&env
->fpr
[n
- 32]) = tmp
;
792 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
794 case 64: env
->y
= tmp
; break;
795 case 65: PUT_PSR(env
, tmp
); break;
796 case 66: env
->wim
= tmp
; break;
797 case 67: env
->tbr
= tmp
; break;
798 case 68: env
->pc
= tmp
; break;
799 case 69: env
->npc
= tmp
; break;
800 case 70: env
->fsr
= tmp
; break;
808 env
->fpr
[n
] = ldfl_p(mem_buf
);
811 /* f32-f62 (double width, even numbers only) */
812 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) = tmp
>> 32;
813 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]) = tmp
;
816 case 80: env
->pc
= tmp
; break;
817 case 81: env
->npc
= tmp
; break;
819 PUT_CCR(env
, tmp
>> 32);
820 env
->asi
= (tmp
>> 24) & 0xff;
821 env
->pstate
= (tmp
>> 8) & 0xfff;
822 PUT_CWP64(env
, tmp
& 0xff);
824 case 83: env
->fsr
= tmp
; break;
825 case 84: env
->fprs
= tmp
; break;
826 case 85: env
->y
= tmp
; break;
833 #elif defined (TARGET_ARM)
835 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
836 whatever the target description contains. Due to a historical mishap
837 the FPA registers appear in between core integer regs and the CPSR.
838 We hack round this by giving the FPA regs zero size when talking to a
840 #define NUM_CORE_REGS 26
841 #define GDB_CORE_XML "arm-core.xml"
843 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
846 /* Core integer register. */
847 GET_REG32(env
->regs
[n
]);
853 memset(mem_buf
, 0, 12);
858 /* FPA status register. */
864 GET_REG32(cpsr_read(env
));
866 /* Unknown register. */
870 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
874 tmp
= ldl_p(mem_buf
);
876 /* Mask out low bit of PC to workaround gdb bugs. This will probably
877 cause problems if we ever implement the Jazelle DBX extensions. */
882 /* Core integer register. */
886 if (n
< 24) { /* 16-23 */
887 /* FPA registers (ignored). */
894 /* FPA status register (ignored). */
900 cpsr_write (env
, tmp
, 0xffffffff);
903 /* Unknown register. */
907 #elif defined (TARGET_M68K)
909 #define NUM_CORE_REGS 18
911 #define GDB_CORE_XML "cf-core.xml"
913 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
917 GET_REG32(env
->dregs
[n
]);
920 GET_REG32(env
->aregs
[n
- 8]);
923 case 16: GET_REG32(env
->sr
);
924 case 17: GET_REG32(env
->pc
);
927 /* FP registers not included here because they vary between
928 ColdFire and m68k. Use XML bits for these. */
932 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
936 tmp
= ldl_p(mem_buf
);
943 env
->aregs
[n
- 8] = tmp
;
946 case 16: env
->sr
= tmp
; break;
947 case 17: env
->pc
= tmp
; break;
953 #elif defined (TARGET_MIPS)
955 #define NUM_CORE_REGS 73
957 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
960 GET_REGL(env
->active_tc
.gpr
[n
]);
962 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
963 if (n
>= 38 && n
< 70) {
964 if (env
->CP0_Status
& (1 << CP0St_FR
))
965 GET_REGL(env
->active_fpu
.fpr
[n
- 38].d
);
967 GET_REGL(env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
]);
970 case 70: GET_REGL((int32_t)env
->active_fpu
.fcr31
);
971 case 71: GET_REGL((int32_t)env
->active_fpu
.fcr0
);
975 case 32: GET_REGL((int32_t)env
->CP0_Status
);
976 case 33: GET_REGL(env
->active_tc
.LO
[0]);
977 case 34: GET_REGL(env
->active_tc
.HI
[0]);
978 case 35: GET_REGL(env
->CP0_BadVAddr
);
979 case 36: GET_REGL((int32_t)env
->CP0_Cause
);
980 case 37: GET_REGL(env
->active_tc
.PC
);
981 case 72: GET_REGL(0); /* fp */
982 case 89: GET_REGL((int32_t)env
->CP0_PRid
);
984 if (n
>= 73 && n
<= 88) {
985 /* 16 embedded regs. */
992 /* convert MIPS rounding mode in FCR31 to IEEE library */
993 static unsigned int ieee_rm
[] =
995 float_round_nearest_even
,
1000 #define RESTORE_ROUNDING_MODE \
1001 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
1003 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1007 tmp
= ldtul_p(mem_buf
);
1010 env
->active_tc
.gpr
[n
] = tmp
;
1011 return sizeof(target_ulong
);
1013 if (env
->CP0_Config1
& (1 << CP0C1_FP
)
1014 && n
>= 38 && n
< 73) {
1016 if (env
->CP0_Status
& (1 << CP0St_FR
))
1017 env
->active_fpu
.fpr
[n
- 38].d
= tmp
;
1019 env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
] = tmp
;
1023 env
->active_fpu
.fcr31
= tmp
& 0xFF83FFFF;
1024 /* set rounding mode */
1025 RESTORE_ROUNDING_MODE
;
1026 #ifndef CONFIG_SOFTFLOAT
1027 /* no floating point exception for native float */
1028 SET_FP_ENABLE(env
->active_fpu
.fcr31
, 0);
1031 case 71: env
->active_fpu
.fcr0
= tmp
; break;
1033 return sizeof(target_ulong
);
1036 case 32: env
->CP0_Status
= tmp
; break;
1037 case 33: env
->active_tc
.LO
[0] = tmp
; break;
1038 case 34: env
->active_tc
.HI
[0] = tmp
; break;
1039 case 35: env
->CP0_BadVAddr
= tmp
; break;
1040 case 36: env
->CP0_Cause
= tmp
; break;
1041 case 37: env
->active_tc
.PC
= tmp
; break;
1042 case 72: /* fp, ignored */ break;
1046 /* Other registers are readonly. Ignore writes. */
1050 return sizeof(target_ulong
);
1052 #elif defined (TARGET_SH4)
1054 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1055 /* FIXME: We should use XML for this. */
1057 #define NUM_CORE_REGS 59
1059 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1062 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1063 GET_REGL(env
->gregs
[n
+ 16]);
1065 GET_REGL(env
->gregs
[n
]);
1067 } else if (n
< 16) {
1068 GET_REGL(env
->gregs
[n
- 8]);
1069 } else if (n
>= 25 && n
< 41) {
1070 GET_REGL(env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
1071 } else if (n
>= 43 && n
< 51) {
1072 GET_REGL(env
->gregs
[n
- 43]);
1073 } else if (n
>= 51 && n
< 59) {
1074 GET_REGL(env
->gregs
[n
- (51 - 16)]);
1077 case 16: GET_REGL(env
->pc
);
1078 case 17: GET_REGL(env
->pr
);
1079 case 18: GET_REGL(env
->gbr
);
1080 case 19: GET_REGL(env
->vbr
);
1081 case 20: GET_REGL(env
->mach
);
1082 case 21: GET_REGL(env
->macl
);
1083 case 22: GET_REGL(env
->sr
);
1084 case 23: GET_REGL(env
->fpul
);
1085 case 24: GET_REGL(env
->fpscr
);
1086 case 41: GET_REGL(env
->ssr
);
1087 case 42: GET_REGL(env
->spc
);
1093 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1097 tmp
= ldl_p(mem_buf
);
1100 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1101 env
->gregs
[n
+ 16] = tmp
;
1103 env
->gregs
[n
] = tmp
;
1106 } else if (n
< 16) {
1107 env
->gregs
[n
- 8] = tmp
;
1109 } else if (n
>= 25 && n
< 41) {
1110 env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)] = tmp
;
1111 } else if (n
>= 43 && n
< 51) {
1112 env
->gregs
[n
- 43] = tmp
;
1114 } else if (n
>= 51 && n
< 59) {
1115 env
->gregs
[n
- (51 - 16)] = tmp
;
1119 case 16: env
->pc
= tmp
;
1120 case 17: env
->pr
= tmp
;
1121 case 18: env
->gbr
= tmp
;
1122 case 19: env
->vbr
= tmp
;
1123 case 20: env
->mach
= tmp
;
1124 case 21: env
->macl
= tmp
;
1125 case 22: env
->sr
= tmp
;
1126 case 23: env
->fpul
= tmp
;
1127 case 24: env
->fpscr
= tmp
;
1128 case 41: env
->ssr
= tmp
;
1129 case 42: env
->spc
= tmp
;
1135 #elif defined (TARGET_CRIS)
1137 #define NUM_CORE_REGS 49
1139 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1143 srs
= env
->pregs
[PR_SRS
];
1145 GET_REG32(env
->regs
[n
]);
1148 if (n
>= 21 && n
< 32) {
1149 GET_REG32(env
->pregs
[n
- 16]);
1151 if (n
>= 33 && n
< 49) {
1152 GET_REG32(env
->sregs
[srs
][n
- 33]);
1155 case 16: GET_REG8(env
->pregs
[0]);
1156 case 17: GET_REG8(env
->pregs
[1]);
1157 case 18: GET_REG32(env
->pregs
[2]);
1158 case 19: GET_REG8(srs
);
1159 case 20: GET_REG16(env
->pregs
[4]);
1160 case 32: GET_REG32(env
->pc
);
1166 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1173 tmp
= ldl_p(mem_buf
);
1179 if (n
>= 21 && n
< 32) {
1180 env
->pregs
[n
- 16] = tmp
;
1183 /* FIXME: Should support function regs be writable? */
1187 case 18: env
->pregs
[PR_PID
] = tmp
; break;
1190 case 32: env
->pc
= tmp
; break;
1195 #elif defined (TARGET_ALPHA)
1197 #define NUM_CORE_REGS 65
1199 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1202 GET_REGL(env
->ir
[n
]);
1210 val
=*((uint64_t *)&env
->fir
[n
-32]);
1214 GET_REGL(env
->fpcr
);
1226 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1229 tmp
= ldtul_p(mem_buf
);
1235 if (n
> 31 && n
< 63) {
1236 env
->fir
[n
- 32] = ldfl_p(mem_buf
);
1247 #define NUM_CORE_REGS 0
1249 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1254 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1261 static int num_g_regs
= NUM_CORE_REGS
;
1264 /* Encode data using the encoding for 'x' packets. */
1265 static int memtox(char *buf
, const char *mem
, int len
)
1273 case '#': case '$': case '*': case '}':
1285 static const char *get_feature_xml(const char *p
, const char **newp
)
1287 extern const char *const xml_builtin
[][2];
1291 static char target_xml
[1024];
1294 while (p
[len
] && p
[len
] != ':')
1299 if (strncmp(p
, "target.xml", len
) == 0) {
1300 /* Generate the XML description for this CPU. */
1301 if (!target_xml
[0]) {
1302 GDBRegisterState
*r
;
1304 snprintf(target_xml
, sizeof(target_xml
),
1305 "<?xml version=\"1.0\"?>"
1306 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1308 "<xi:include href=\"%s\"/>",
1311 for (r
= first_cpu
->gdb_regs
; r
; r
= r
->next
) {
1312 strcat(target_xml
, "<xi:include href=\"");
1313 strcat(target_xml
, r
->xml
);
1314 strcat(target_xml
, "\"/>");
1316 strcat(target_xml
, "</target>");
1320 for (i
= 0; ; i
++) {
1321 name
= xml_builtin
[i
][0];
1322 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
1325 return name
? xml_builtin
[i
][1] : NULL
;
1329 static int gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1331 GDBRegisterState
*r
;
1333 if (reg
< NUM_CORE_REGS
)
1334 return cpu_gdb_read_register(env
, mem_buf
, reg
);
1336 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1337 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1338 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
1344 static int gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1346 GDBRegisterState
*r
;
1348 if (reg
< NUM_CORE_REGS
)
1349 return cpu_gdb_write_register(env
, mem_buf
, reg
);
1351 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1352 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1353 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
1359 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
1360 specifies the first register number and these registers are included in
1361 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1362 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1365 void gdb_register_coprocessor(CPUState
* env
,
1366 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
1367 int num_regs
, const char *xml
, int g_pos
)
1369 GDBRegisterState
*s
;
1370 GDBRegisterState
**p
;
1371 static int last_reg
= NUM_CORE_REGS
;
1373 s
= (GDBRegisterState
*)qemu_mallocz(sizeof(GDBRegisterState
));
1374 s
->base_reg
= last_reg
;
1375 s
->num_regs
= num_regs
;
1376 s
->get_reg
= get_reg
;
1377 s
->set_reg
= set_reg
;
1381 /* Check for duplicates. */
1382 if (strcmp((*p
)->xml
, xml
) == 0)
1386 /* Add to end of list. */
1387 last_reg
+= num_regs
;
1390 if (g_pos
!= s
->base_reg
) {
1391 fprintf(stderr
, "Error: Bad gdb register numbering for '%s'\n"
1392 "Expected %d got %d\n", xml
, g_pos
, s
->base_reg
);
1394 num_g_regs
= last_reg
;
1399 #ifndef CONFIG_USER_ONLY
1400 static const int xlat_gdb_type
[] = {
1401 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
1402 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
1403 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
1407 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
1413 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1416 case GDB_BREAKPOINT_SW
:
1417 case GDB_BREAKPOINT_HW
:
1418 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1419 err
= cpu_breakpoint_insert(env
, addr
, BP_GDB
, NULL
);
1424 #ifndef CONFIG_USER_ONLY
1425 case GDB_WATCHPOINT_WRITE
:
1426 case GDB_WATCHPOINT_READ
:
1427 case GDB_WATCHPOINT_ACCESS
:
1428 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1429 err
= cpu_watchpoint_insert(env
, addr
, len
, xlat_gdb_type
[type
],
1441 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1447 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1450 case GDB_BREAKPOINT_SW
:
1451 case GDB_BREAKPOINT_HW
:
1452 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1453 err
= cpu_breakpoint_remove(env
, addr
, BP_GDB
);
1458 #ifndef CONFIG_USER_ONLY
1459 case GDB_WATCHPOINT_WRITE
:
1460 case GDB_WATCHPOINT_READ
:
1461 case GDB_WATCHPOINT_ACCESS
:
1462 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1463 err
= cpu_watchpoint_remove(env
, addr
, len
, xlat_gdb_type
[type
]);
1474 static void gdb_breakpoint_remove_all(void)
1478 if (kvm_enabled()) {
1479 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1483 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1484 cpu_breakpoint_remove_all(env
, BP_GDB
);
1485 #ifndef CONFIG_USER_ONLY
1486 cpu_watchpoint_remove_all(env
, BP_GDB
);
1491 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1495 int ch
, reg_size
, type
, res
, thread
;
1496 char buf
[MAX_PACKET_LENGTH
];
1497 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1499 target_ulong addr
, len
;
1502 printf("command='%s'\n", line_buf
);
1508 /* TODO: Make this return the correct value for user-mode. */
1509 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
1510 s
->c_cpu
->cpu_index
+1);
1512 /* Remove all the breakpoints when this query is issued,
1513 * because gdb is doing and initial connect and the state
1514 * should be cleaned up.
1516 gdb_breakpoint_remove_all();
1520 addr
= strtoull(p
, (char **)&p
, 16);
1521 #if defined(TARGET_I386)
1522 s
->c_cpu
->eip
= addr
;
1523 kvm_load_registers(s
->c_cpu
);
1524 #elif defined (TARGET_PPC)
1525 s
->c_cpu
->nip
= addr
;
1526 kvm_load_registers(s
->c_cpu
);
1527 #elif defined (TARGET_SPARC)
1528 s
->c_cpu
->pc
= addr
;
1529 s
->c_cpu
->npc
= addr
+ 4;
1530 #elif defined (TARGET_ARM)
1531 s
->c_cpu
->regs
[15] = addr
;
1532 #elif defined (TARGET_SH4)
1533 s
->c_cpu
->pc
= addr
;
1534 #elif defined (TARGET_MIPS)
1535 s
->c_cpu
->active_tc
.PC
= addr
;
1536 #elif defined (TARGET_CRIS)
1537 s
->c_cpu
->pc
= addr
;
1538 #elif defined (TARGET_ALPHA)
1539 s
->c_cpu
->pc
= addr
;
1546 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1547 if (s
->signal
== -1)
1552 /* Kill the target */
1553 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1557 gdb_breakpoint_remove_all();
1559 put_packet(s
, "OK");
1563 addr
= strtoull(p
, (char **)&p
, 16);
1564 #if defined(TARGET_I386)
1565 s
->c_cpu
->eip
= addr
;
1566 kvm_load_registers(s
->c_cpu
);
1567 #elif defined (TARGET_PPC)
1568 s
->c_cpu
->nip
= addr
;
1569 kvm_load_registers(s
->c_cpu
);
1570 #elif defined (TARGET_SPARC)
1571 s
->c_cpu
->pc
= addr
;
1572 s
->c_cpu
->npc
= addr
+ 4;
1573 #elif defined (TARGET_ARM)
1574 s
->c_cpu
->regs
[15] = addr
;
1575 #elif defined (TARGET_SH4)
1576 s
->c_cpu
->pc
= addr
;
1577 #elif defined (TARGET_MIPS)
1578 s
->c_cpu
->active_tc
.PC
= addr
;
1579 #elif defined (TARGET_CRIS)
1580 s
->c_cpu
->pc
= addr
;
1581 #elif defined (TARGET_ALPHA)
1582 s
->c_cpu
->pc
= addr
;
1585 cpu_single_step(s
->c_cpu
, sstep_flags
);
1593 ret
= strtoull(p
, (char **)&p
, 16);
1596 err
= strtoull(p
, (char **)&p
, 16);
1603 if (gdb_current_syscall_cb
)
1604 gdb_current_syscall_cb(s
->c_cpu
, ret
, err
);
1606 put_packet(s
, "T02");
1613 kvm_save_registers(s
->g_cpu
);
1615 for (addr
= 0; addr
< num_g_regs
; addr
++) {
1616 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1619 memtohex(buf
, mem_buf
, len
);
1623 registers
= mem_buf
;
1624 len
= strlen(p
) / 2;
1625 hextomem((uint8_t *)registers
, p
, len
);
1626 for (addr
= 0; addr
< num_g_regs
&& len
> 0; addr
++) {
1627 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1629 registers
+= reg_size
;
1631 kvm_load_registers(s
->g_cpu
);
1632 put_packet(s
, "OK");
1635 addr
= strtoull(p
, (char **)&p
, 16);
1638 len
= strtoull(p
, NULL
, 16);
1639 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 0) != 0) {
1640 put_packet (s
, "E14");
1642 memtohex(buf
, mem_buf
, len
);
1647 addr
= strtoull(p
, (char **)&p
, 16);
1650 len
= strtoull(p
, (char **)&p
, 16);
1653 hextomem(mem_buf
, p
, len
);
1654 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 1) != 0)
1655 put_packet(s
, "E14");
1657 put_packet(s
, "OK");
1660 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1661 This works, but can be very slow. Anything new enough to
1662 understand XML also knows how to use this properly. */
1664 goto unknown_command
;
1665 addr
= strtoull(p
, (char **)&p
, 16);
1666 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1668 memtohex(buf
, mem_buf
, reg_size
);
1671 put_packet(s
, "E14");
1676 goto unknown_command
;
1677 addr
= strtoull(p
, (char **)&p
, 16);
1680 reg_size
= strlen(p
) / 2;
1681 hextomem(mem_buf
, p
, reg_size
);
1682 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1683 put_packet(s
, "OK");
1687 type
= strtoul(p
, (char **)&p
, 16);
1690 addr
= strtoull(p
, (char **)&p
, 16);
1693 len
= strtoull(p
, (char **)&p
, 16);
1695 res
= gdb_breakpoint_insert(addr
, len
, type
);
1697 res
= gdb_breakpoint_remove(addr
, len
, type
);
1699 put_packet(s
, "OK");
1700 else if (res
== -ENOSYS
)
1703 put_packet(s
, "E22");
1707 thread
= strtoull(p
, (char **)&p
, 16);
1708 if (thread
== -1 || thread
== 0) {
1709 put_packet(s
, "OK");
1712 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1713 if (env
->cpu_index
+ 1 == thread
)
1716 put_packet(s
, "E22");
1722 put_packet(s
, "OK");
1726 put_packet(s
, "OK");
1729 put_packet(s
, "E22");
1734 thread
= strtoull(p
, (char **)&p
, 16);
1735 #ifndef CONFIG_USER_ONLY
1736 if (thread
> 0 && thread
< smp_cpus
+ 1)
1740 put_packet(s
, "OK");
1742 put_packet(s
, "E22");
1746 /* parse any 'q' packets here */
1747 if (!strcmp(p
,"qemu.sstepbits")) {
1748 /* Query Breakpoint bit definitions */
1749 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1755 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1756 /* Display or change the sstep_flags */
1759 /* Display current setting */
1760 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1765 type
= strtoul(p
, (char **)&p
, 16);
1767 put_packet(s
, "OK");
1769 } else if (strcmp(p
,"C") == 0) {
1770 /* "Current thread" remains vague in the spec, so always return
1771 * the first CPU (gdb returns the first thread). */
1772 put_packet(s
, "QC1");
1774 } else if (strcmp(p
,"fThreadInfo") == 0) {
1775 s
->query_cpu
= first_cpu
;
1776 goto report_cpuinfo
;
1777 } else if (strcmp(p
,"sThreadInfo") == 0) {
1780 snprintf(buf
, sizeof(buf
), "m%x", s
->query_cpu
->cpu_index
+1);
1782 s
->query_cpu
= s
->query_cpu
->next_cpu
;
1786 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1787 thread
= strtoull(p
+16, (char **)&p
, 16);
1788 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1789 if (env
->cpu_index
+ 1 == thread
) {
1790 kvm_save_registers(env
);
1791 len
= snprintf((char *)mem_buf
, sizeof(mem_buf
),
1792 "CPU#%d [%s]", env
->cpu_index
,
1793 env
->halted
? "halted " : "running");
1794 memtohex(buf
, mem_buf
, len
);
1800 #ifdef CONFIG_LINUX_USER
1801 else if (strncmp(p
, "Offsets", 7) == 0) {
1802 TaskState
*ts
= s
->c_cpu
->opaque
;
1804 snprintf(buf
, sizeof(buf
),
1805 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1806 ";Bss=" TARGET_ABI_FMT_lx
,
1807 ts
->info
->code_offset
,
1808 ts
->info
->data_offset
,
1809 ts
->info
->data_offset
);
1814 if (strncmp(p
, "Supported", 9) == 0) {
1815 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1817 strcat(buf
, ";qXfer:features:read+");
1823 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1825 target_ulong total_len
;
1829 xml
= get_feature_xml(p
, &p
);
1831 snprintf(buf
, sizeof(buf
), "E00");
1838 addr
= strtoul(p
, (char **)&p
, 16);
1841 len
= strtoul(p
, (char **)&p
, 16);
1843 total_len
= strlen(xml
);
1844 if (addr
> total_len
) {
1845 snprintf(buf
, sizeof(buf
), "E00");
1849 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
1850 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1851 if (len
< total_len
- addr
) {
1853 len
= memtox(buf
+ 1, xml
+ addr
, len
);
1856 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
1858 put_packet_binary(s
, buf
, len
+ 1);
1862 /* Unrecognised 'q' command. */
1863 goto unknown_command
;
1867 /* put empty packet */
1875 void gdb_set_stop_cpu(CPUState
*env
)
1877 gdbserver_state
->c_cpu
= env
;
1878 gdbserver_state
->g_cpu
= env
;
1881 #ifndef CONFIG_USER_ONLY
1882 static void gdb_vm_stopped(void *opaque
, int reason
)
1884 GDBState
*s
= gdbserver_state
;
1885 CPUState
*env
= s
->c_cpu
;
1890 if (s
->state
== RS_SYSCALL
)
1893 /* disable single step if it was enable */
1894 cpu_single_step(env
, 0);
1896 if (reason
== EXCP_DEBUG
) {
1897 if (env
->watchpoint_hit
) {
1898 switch (env
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
1909 snprintf(buf
, sizeof(buf
),
1910 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
1911 GDB_SIGNAL_TRAP
, env
->cpu_index
+1, type
,
1912 env
->watchpoint_hit
->vaddr
);
1914 env
->watchpoint_hit
= NULL
;
1918 ret
= GDB_SIGNAL_TRAP
;
1919 } else if (reason
== EXCP_INTERRUPT
) {
1920 ret
= GDB_SIGNAL_INT
;
1924 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, env
->cpu_index
+1);
1929 /* Send a gdb syscall request.
1930 This accepts limited printf-style format specifiers, specifically:
1931 %x - target_ulong argument printed in hex.
1932 %lx - 64-bit argument printed in hex.
1933 %s - string pointer (target_ulong) and length (int) pair. */
1934 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
1943 s
= gdbserver_state
;
1946 gdb_current_syscall_cb
= cb
;
1947 s
->state
= RS_SYSCALL
;
1948 #ifndef CONFIG_USER_ONLY
1949 vm_stop(EXCP_DEBUG
);
1960 addr
= va_arg(va
, target_ulong
);
1961 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
, addr
);
1964 if (*(fmt
++) != 'x')
1966 i64
= va_arg(va
, uint64_t);
1967 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, "%" PRIx64
, i64
);
1970 addr
= va_arg(va
, target_ulong
);
1971 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
"/%x",
1972 addr
, va_arg(va
, int));
1976 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1987 #ifdef CONFIG_USER_ONLY
1988 gdb_handlesig(s
->c_cpu
, 0);
1990 cpu_interrupt(s
->c_cpu
, CPU_INTERRUPT_EXIT
);
1994 static void gdb_read_byte(GDBState
*s
, int ch
)
1999 #ifndef CONFIG_USER_ONLY
2000 if (s
->last_packet_len
) {
2001 /* Waiting for a response to the last packet. If we see the start
2002 of a new command then abandon the previous response. */
2005 printf("Got NACK, retransmitting\n");
2007 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2011 printf("Got ACK\n");
2013 printf("Got '%c' when expecting ACK/NACK\n", ch
);
2015 if (ch
== '+' || ch
== '$')
2016 s
->last_packet_len
= 0;
2021 /* when the CPU is running, we cannot do anything except stop
2022 it when receiving a char */
2023 vm_stop(EXCP_INTERRUPT
);
2030 s
->line_buf_index
= 0;
2031 s
->state
= RS_GETLINE
;
2036 s
->state
= RS_CHKSUM1
;
2037 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2040 s
->line_buf
[s
->line_buf_index
++] = ch
;
2044 s
->line_buf
[s
->line_buf_index
] = '\0';
2045 s
->line_csum
= fromhex(ch
) << 4;
2046 s
->state
= RS_CHKSUM2
;
2049 s
->line_csum
|= fromhex(ch
);
2051 for(i
= 0; i
< s
->line_buf_index
; i
++) {
2052 csum
+= s
->line_buf
[i
];
2054 if (s
->line_csum
!= (csum
& 0xff)) {
2056 put_buffer(s
, &reply
, 1);
2060 put_buffer(s
, &reply
, 1);
2061 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2070 #ifdef CONFIG_USER_ONLY
2076 s
= gdbserver_state
;
2078 if (gdbserver_fd
< 0 || s
->fd
< 0)
2085 gdb_handlesig (CPUState
*env
, int sig
)
2091 s
= gdbserver_state
;
2092 if (gdbserver_fd
< 0 || s
->fd
< 0)
2095 /* disable single step if it was enabled */
2096 cpu_single_step(env
, 0);
2101 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb (sig
));
2104 /* put_packet() might have detected that the peer terminated the
2111 s
->running_state
= 0;
2112 while (s
->running_state
== 0) {
2113 n
= read (s
->fd
, buf
, 256);
2118 for (i
= 0; i
< n
; i
++)
2119 gdb_read_byte (s
, buf
[i
]);
2121 else if (n
== 0 || errno
!= EAGAIN
)
2123 /* XXX: Connection closed. Should probably wait for annother
2124 connection before continuing. */
2133 /* Tell the remote gdb that the process has exited. */
2134 void gdb_exit(CPUState
*env
, int code
)
2139 s
= gdbserver_state
;
2140 if (gdbserver_fd
< 0 || s
->fd
< 0)
2143 snprintf(buf
, sizeof(buf
), "W%02x", code
);
2147 /* Tell the remote gdb that the process has exited due to SIG. */
2148 void gdb_signalled(CPUState
*env
, int sig
)
2153 s
= gdbserver_state
;
2154 if (gdbserver_fd
< 0 || s
->fd
< 0)
2157 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb (sig
));
2161 static void gdb_accept(void)
2164 struct sockaddr_in sockaddr
;
2169 len
= sizeof(sockaddr
);
2170 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2171 if (fd
< 0 && errno
!= EINTR
) {
2174 } else if (fd
>= 0) {
2179 /* set short latency */
2181 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2183 s
= qemu_mallocz(sizeof(GDBState
));
2190 memset (s
, 0, sizeof (GDBState
));
2191 s
->c_cpu
= first_cpu
;
2192 s
->g_cpu
= first_cpu
;
2196 gdbserver_state
= s
;
2198 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2201 static int gdbserver_open(int port
)
2203 struct sockaddr_in sockaddr
;
2206 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2212 /* allow fast reuse */
2214 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
2216 sockaddr
.sin_family
= AF_INET
;
2217 sockaddr
.sin_port
= htons(port
);
2218 sockaddr
.sin_addr
.s_addr
= 0;
2219 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2224 ret
= listen(fd
, 0);
2232 int gdbserver_start(int port
)
2234 gdbserver_fd
= gdbserver_open(port
);
2235 if (gdbserver_fd
< 0)
2237 /* accept connections */
2242 /* Disable gdb stub for child processes. */
2243 void gdbserver_fork(CPUState
*env
)
2245 GDBState
*s
= gdbserver_state
;
2246 if (gdbserver_fd
< 0 || s
->fd
< 0)
2250 cpu_breakpoint_remove_all(env
, BP_GDB
);
2251 cpu_watchpoint_remove_all(env
, BP_GDB
);
2254 static int gdb_chr_can_receive(void *opaque
)
2256 /* We can handle an arbitrarily large amount of data.
2257 Pick the maximum packet size, which is as good as anything. */
2258 return MAX_PACKET_LENGTH
;
2261 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2265 for (i
= 0; i
< size
; i
++) {
2266 gdb_read_byte(gdbserver_state
, buf
[i
]);
2270 static void gdb_chr_event(void *opaque
, int event
)
2273 case CHR_EVENT_RESET
:
2274 vm_stop(EXCP_INTERRUPT
);
2282 int gdbserver_start(const char *port
)
2285 char gdbstub_port_name
[128];
2288 CharDriverState
*chr
;
2290 if (!port
|| !*port
)
2293 port_num
= strtol(port
, &p
, 10);
2295 /* A numeric value is interpreted as a port number. */
2296 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
2297 "tcp::%d,nowait,nodelay,server", port_num
);
2298 port
= gdbstub_port_name
;
2301 chr
= qemu_chr_open("gdb", port
);
2305 s
= qemu_mallocz(sizeof(GDBState
));
2309 s
->c_cpu
= first_cpu
;
2310 s
->g_cpu
= first_cpu
;
2312 gdbserver_state
= s
;
2313 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2314 gdb_chr_event
, NULL
);
2315 qemu_add_vm_stop_handler(gdb_vm_stopped
, NULL
);