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"
38 #define MAX_PACKET_LENGTH 4096
40 #include "qemu_socket.h"
47 GDB_SIGNAL_UNKNOWN
= 143
50 #ifdef CONFIG_USER_ONLY
52 /* Map target signal numbers to GDB protocol signal numbers and vice
53 * versa. For user emulation's currently supported systems, we can
54 * assume most signals are defined.
57 static int gdb_signal_table
[] = {
217 /* In system mode we only need SIGINT and SIGTRAP; other signals
218 are not yet supported. */
225 static int gdb_signal_table
[] = {
235 #ifdef CONFIG_USER_ONLY
236 static int target_signal_to_gdb (int sig
)
239 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
240 if (gdb_signal_table
[i
] == sig
)
242 return GDB_SIGNAL_UNKNOWN
;
246 static int gdb_signal_to_target (int sig
)
248 if (sig
< ARRAY_SIZE (gdb_signal_table
))
249 return gdb_signal_table
[sig
];
256 typedef struct GDBRegisterState
{
262 struct GDBRegisterState
*next
;
272 typedef struct GDBState
{
273 CPUState
*c_cpu
; /* current CPU for step/continue ops */
274 CPUState
*g_cpu
; /* current CPU for other ops */
275 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
276 enum RSState state
; /* parsing state */
277 char line_buf
[MAX_PACKET_LENGTH
];
280 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
283 #ifdef CONFIG_USER_ONLY
287 CharDriverState
*chr
;
291 /* By default use no IRQs and no timers while single stepping so as to
292 * make single stepping like an ICE HW step.
294 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
296 static GDBState
*gdbserver_state
;
298 /* This is an ugly hack to cope with both new and old gdb.
299 If gdb sends qXfer:features:read then assume we're talking to a newish
300 gdb that understands target descriptions. */
301 static int gdb_has_xml
;
303 #ifdef CONFIG_USER_ONLY
304 /* XXX: This is not thread safe. Do we care? */
305 static int gdbserver_fd
= -1;
307 static int get_char(GDBState
*s
)
313 ret
= recv(s
->fd
, &ch
, 1, 0);
315 if (errno
== ECONNRESET
)
317 if (errno
!= EINTR
&& errno
!= EAGAIN
)
319 } else if (ret
== 0) {
331 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
339 /* If gdb is connected when the first semihosting syscall occurs then use
340 remote gdb syscalls. Otherwise use native file IO. */
341 int use_gdb_syscalls(void)
343 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
344 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
347 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
350 /* Resume execution. */
351 static inline void gdb_continue(GDBState
*s
)
353 #ifdef CONFIG_USER_ONLY
354 s
->running_state
= 1;
360 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
362 #ifdef CONFIG_USER_ONLY
366 ret
= send(s
->fd
, buf
, len
, 0);
368 if (errno
!= EINTR
&& errno
!= EAGAIN
)
376 qemu_chr_write(s
->chr
, buf
, len
);
380 static inline int fromhex(int v
)
382 if (v
>= '0' && v
<= '9')
384 else if (v
>= 'A' && v
<= 'F')
386 else if (v
>= 'a' && v
<= 'f')
392 static inline int tohex(int v
)
400 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
405 for(i
= 0; i
< len
; i
++) {
407 *q
++ = tohex(c
>> 4);
408 *q
++ = tohex(c
& 0xf);
413 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
417 for(i
= 0; i
< len
; i
++) {
418 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
423 /* return -1 if error, 0 if OK */
424 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
)
435 for(i
= 0; i
< len
; i
++) {
439 *(p
++) = tohex((csum
>> 4) & 0xf);
440 *(p
++) = tohex((csum
) & 0xf);
442 s
->last_packet_len
= p
- s
->last_packet
;
443 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
445 #ifdef CONFIG_USER_ONLY
458 /* return -1 if error, 0 if OK */
459 static int put_packet(GDBState
*s
, const char *buf
)
462 printf("reply='%s'\n", buf
);
465 return put_packet_binary(s
, buf
, strlen(buf
));
468 /* The GDB remote protocol transfers values in target byte order. This means
469 we can use the raw memory access routines to access the value buffer.
470 Conveniently, these also handle the case where the buffer is mis-aligned.
472 #define GET_REG8(val) do { \
473 stb_p(mem_buf, val); \
476 #define GET_REG16(val) do { \
477 stw_p(mem_buf, val); \
480 #define GET_REG32(val) do { \
481 stl_p(mem_buf, val); \
484 #define GET_REG64(val) do { \
485 stq_p(mem_buf, val); \
489 #if TARGET_LONG_BITS == 64
490 #define GET_REGL(val) GET_REG64(val)
491 #define ldtul_p(addr) ldq_p(addr)
493 #define GET_REGL(val) GET_REG32(val)
494 #define ldtul_p(addr) ldl_p(addr)
497 #if defined(TARGET_I386)
500 static const int gpr_map
[16] = {
501 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
502 8, 9, 10, 11, 12, 13, 14, 15
505 static const int gpr_map
[8] = {0, 1, 2, 3, 4, 5, 6, 7};
508 #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
510 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
512 if (n
< CPU_NB_REGS
) {
513 GET_REGL(env
->regs
[gpr_map
[n
]]);
514 } else if (n
>= CPU_NB_REGS
+ 8 && n
< CPU_NB_REGS
+ 16) {
515 /* FIXME: byteswap float values. */
516 #ifdef USE_X86LDOUBLE
517 memcpy(mem_buf
, &env
->fpregs
[n
- (CPU_NB_REGS
+ 8)], 10);
519 memset(mem_buf
, 0, 10);
522 } else if (n
>= CPU_NB_REGS
+ 24) {
523 n
-= CPU_NB_REGS
+ 24;
524 if (n
< CPU_NB_REGS
) {
525 stq_p(mem_buf
, env
->xmm_regs
[n
].XMM_Q(0));
526 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].XMM_Q(1));
528 } else if (n
== CPU_NB_REGS
) {
529 GET_REG32(env
->mxcsr
);
534 case 0: GET_REGL(env
->eip
);
535 case 1: GET_REG32(env
->eflags
);
536 case 2: GET_REG32(env
->segs
[R_CS
].selector
);
537 case 3: GET_REG32(env
->segs
[R_SS
].selector
);
538 case 4: GET_REG32(env
->segs
[R_DS
].selector
);
539 case 5: GET_REG32(env
->segs
[R_ES
].selector
);
540 case 6: GET_REG32(env
->segs
[R_FS
].selector
);
541 case 7: GET_REG32(env
->segs
[R_GS
].selector
);
542 /* 8...15 x87 regs. */
543 case 16: GET_REG32(env
->fpuc
);
544 case 17: GET_REG32((env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11);
545 case 18: GET_REG32(0); /* ftag */
546 case 19: GET_REG32(0); /* fiseg */
547 case 20: GET_REG32(0); /* fioff */
548 case 21: GET_REG32(0); /* foseg */
549 case 22: GET_REG32(0); /* fooff */
550 case 23: GET_REG32(0); /* fop */
557 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int i
)
561 if (i
< CPU_NB_REGS
) {
562 env
->regs
[gpr_map
[i
]] = ldtul_p(mem_buf
);
563 return sizeof(target_ulong
);
564 } else if (i
>= CPU_NB_REGS
+ 8 && i
< CPU_NB_REGS
+ 16) {
565 i
-= CPU_NB_REGS
+ 8;
566 #ifdef USE_X86LDOUBLE
567 memcpy(&env
->fpregs
[i
], mem_buf
, 10);
570 } else if (i
>= CPU_NB_REGS
+ 24) {
571 i
-= CPU_NB_REGS
+ 24;
572 if (i
< CPU_NB_REGS
) {
573 env
->xmm_regs
[i
].XMM_Q(0) = ldq_p(mem_buf
);
574 env
->xmm_regs
[i
].XMM_Q(1) = ldq_p(mem_buf
+ 8);
576 } else if (i
== CPU_NB_REGS
) {
577 env
->mxcsr
= ldl_p(mem_buf
);
583 case 0: env
->eip
= ldtul_p(mem_buf
); return sizeof(target_ulong
);
584 case 1: env
->eflags
= ldl_p(mem_buf
); return 4;
585 #if defined(CONFIG_USER_ONLY)
586 #define LOAD_SEG(index, sreg)\
587 tmp = ldl_p(mem_buf);\
588 if (tmp != env->segs[sreg].selector)\
589 cpu_x86_load_seg(env, sreg, tmp);
591 /* FIXME: Honor segment registers. Needs to avoid raising an exception
592 when the selector is invalid. */
593 #define LOAD_SEG(index, sreg) do {} while(0)
595 case 2: LOAD_SEG(10, R_CS
); return 4;
596 case 3: LOAD_SEG(11, R_SS
); return 4;
597 case 4: LOAD_SEG(12, R_DS
); return 4;
598 case 5: LOAD_SEG(13, R_ES
); return 4;
599 case 6: LOAD_SEG(14, R_FS
); return 4;
600 case 7: LOAD_SEG(15, R_GS
); return 4;
601 /* 8...15 x87 regs. */
602 case 16: env
->fpuc
= ldl_p(mem_buf
); return 4;
604 tmp
= ldl_p(mem_buf
);
605 env
->fpstt
= (tmp
>> 11) & 7;
606 env
->fpus
= tmp
& ~0x3800;
608 case 18: /* ftag */ return 4;
609 case 19: /* fiseg */ return 4;
610 case 20: /* fioff */ return 4;
611 case 21: /* foseg */ return 4;
612 case 22: /* fooff */ return 4;
613 case 23: /* fop */ return 4;
617 /* Unrecognised register. */
621 #elif defined (TARGET_PPC)
623 /* Old gdb always expects FP registers. Newer (xml-aware) gdb only
624 expects whatever the target description contains. Due to a
625 historical mishap the FP registers appear in between core integer
626 regs and PC, MSR, CR, and so forth. We hack round this by giving the
627 FP regs zero size when talking to a newer gdb. */
628 #define NUM_CORE_REGS 71
629 #if defined (TARGET_PPC64)
630 #define GDB_CORE_XML "power64-core.xml"
632 #define GDB_CORE_XML "power-core.xml"
635 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
639 GET_REGL(env
->gpr
[n
]);
644 stfq_p(mem_buf
, env
->fpr
[n
-32]);
648 case 64: GET_REGL(env
->nip
);
649 case 65: GET_REGL(env
->msr
);
654 for (i
= 0; i
< 8; i
++)
655 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
658 case 67: GET_REGL(env
->lr
);
659 case 68: GET_REGL(env
->ctr
);
660 case 69: GET_REGL(env
->xer
);
665 GET_REG32(0); /* fpscr */
672 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
676 env
->gpr
[n
] = ldtul_p(mem_buf
);
677 return sizeof(target_ulong
);
682 env
->fpr
[n
-32] = ldfq_p(mem_buf
);
687 env
->nip
= ldtul_p(mem_buf
);
688 return sizeof(target_ulong
);
690 ppc_store_msr(env
, ldtul_p(mem_buf
));
691 return sizeof(target_ulong
);
694 uint32_t cr
= ldl_p(mem_buf
);
696 for (i
= 0; i
< 8; i
++)
697 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
701 env
->lr
= ldtul_p(mem_buf
);
702 return sizeof(target_ulong
);
704 env
->ctr
= ldtul_p(mem_buf
);
705 return sizeof(target_ulong
);
707 env
->xer
= ldtul_p(mem_buf
);
708 return sizeof(target_ulong
);
719 #elif defined (TARGET_SPARC)
721 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
722 #define NUM_CORE_REGS 86
724 #define NUM_CORE_REGS 72
728 #define GET_REGA(val) GET_REG32(val)
730 #define GET_REGA(val) GET_REGL(val)
733 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
737 GET_REGA(env
->gregs
[n
]);
740 /* register window */
741 GET_REGA(env
->regwptr
[n
- 8]);
743 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
746 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
748 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
750 case 64: GET_REGA(env
->y
);
751 case 65: GET_REGA(GET_PSR(env
));
752 case 66: GET_REGA(env
->wim
);
753 case 67: GET_REGA(env
->tbr
);
754 case 68: GET_REGA(env
->pc
);
755 case 69: GET_REGA(env
->npc
);
756 case 70: GET_REGA(env
->fsr
);
757 case 71: GET_REGA(0); /* csr */
758 default: GET_REGA(0);
763 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
766 /* f32-f62 (double width, even numbers only) */
769 val
= (uint64_t)*((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) << 32;
770 val
|= *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]);
774 case 80: GET_REGL(env
->pc
);
775 case 81: GET_REGL(env
->npc
);
776 case 82: GET_REGL(((uint64_t)GET_CCR(env
) << 32) |
777 ((env
->asi
& 0xff) << 24) |
778 ((env
->pstate
& 0xfff) << 8) |
780 case 83: GET_REGL(env
->fsr
);
781 case 84: GET_REGL(env
->fprs
);
782 case 85: GET_REGL(env
->y
);
788 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
790 #if defined(TARGET_ABI32)
793 tmp
= ldl_p(mem_buf
);
797 tmp
= ldtul_p(mem_buf
);
804 /* register window */
805 env
->regwptr
[n
- 8] = tmp
;
807 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
810 *((uint32_t *)&env
->fpr
[n
- 32]) = tmp
;
812 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
814 case 64: env
->y
= tmp
; break;
815 case 65: PUT_PSR(env
, tmp
); break;
816 case 66: env
->wim
= tmp
; break;
817 case 67: env
->tbr
= tmp
; break;
818 case 68: env
->pc
= tmp
; break;
819 case 69: env
->npc
= tmp
; break;
820 case 70: env
->fsr
= tmp
; break;
828 env
->fpr
[n
] = ldfl_p(mem_buf
);
831 /* f32-f62 (double width, even numbers only) */
832 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) = tmp
>> 32;
833 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]) = tmp
;
836 case 80: env
->pc
= tmp
; break;
837 case 81: env
->npc
= tmp
; break;
839 PUT_CCR(env
, tmp
>> 32);
840 env
->asi
= (tmp
>> 24) & 0xff;
841 env
->pstate
= (tmp
>> 8) & 0xfff;
842 PUT_CWP64(env
, tmp
& 0xff);
844 case 83: env
->fsr
= tmp
; break;
845 case 84: env
->fprs
= tmp
; break;
846 case 85: env
->y
= tmp
; break;
853 #elif defined (TARGET_ARM)
855 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
856 whatever the target description contains. Due to a historical mishap
857 the FPA registers appear in between core integer regs and the CPSR.
858 We hack round this by giving the FPA regs zero size when talking to a
860 #define NUM_CORE_REGS 26
861 #define GDB_CORE_XML "arm-core.xml"
863 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
866 /* Core integer register. */
867 GET_REG32(env
->regs
[n
]);
873 memset(mem_buf
, 0, 12);
878 /* FPA status register. */
884 GET_REG32(cpsr_read(env
));
886 /* Unknown register. */
890 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
894 tmp
= ldl_p(mem_buf
);
896 /* Mask out low bit of PC to workaround gdb bugs. This will probably
897 cause problems if we ever implement the Jazelle DBX extensions. */
902 /* Core integer register. */
906 if (n
< 24) { /* 16-23 */
907 /* FPA registers (ignored). */
914 /* FPA status register (ignored). */
920 cpsr_write (env
, tmp
, 0xffffffff);
923 /* Unknown register. */
927 #elif defined (TARGET_M68K)
929 #define NUM_CORE_REGS 18
931 #define GDB_CORE_XML "cf-core.xml"
933 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
937 GET_REG32(env
->dregs
[n
]);
940 GET_REG32(env
->aregs
[n
- 8]);
943 case 16: GET_REG32(env
->sr
);
944 case 17: GET_REG32(env
->pc
);
947 /* FP registers not included here because they vary between
948 ColdFire and m68k. Use XML bits for these. */
952 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
956 tmp
= ldl_p(mem_buf
);
963 env
->aregs
[n
- 8] = tmp
;
966 case 16: env
->sr
= tmp
; break;
967 case 17: env
->pc
= tmp
; break;
973 #elif defined (TARGET_MIPS)
975 #define NUM_CORE_REGS 73
977 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
980 GET_REGL(env
->active_tc
.gpr
[n
]);
982 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
983 if (n
>= 38 && n
< 70) {
984 if (env
->CP0_Status
& (1 << CP0St_FR
))
985 GET_REGL(env
->active_fpu
.fpr
[n
- 38].d
);
987 GET_REGL(env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
]);
990 case 70: GET_REGL((int32_t)env
->active_fpu
.fcr31
);
991 case 71: GET_REGL((int32_t)env
->active_fpu
.fcr0
);
995 case 32: GET_REGL((int32_t)env
->CP0_Status
);
996 case 33: GET_REGL(env
->active_tc
.LO
[0]);
997 case 34: GET_REGL(env
->active_tc
.HI
[0]);
998 case 35: GET_REGL(env
->CP0_BadVAddr
);
999 case 36: GET_REGL((int32_t)env
->CP0_Cause
);
1000 case 37: GET_REGL(env
->active_tc
.PC
);
1001 case 72: GET_REGL(0); /* fp */
1002 case 89: GET_REGL((int32_t)env
->CP0_PRid
);
1004 if (n
>= 73 && n
<= 88) {
1005 /* 16 embedded regs. */
1012 /* convert MIPS rounding mode in FCR31 to IEEE library */
1013 static unsigned int ieee_rm
[] =
1015 float_round_nearest_even
,
1016 float_round_to_zero
,
1020 #define RESTORE_ROUNDING_MODE \
1021 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
1023 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1027 tmp
= ldtul_p(mem_buf
);
1030 env
->active_tc
.gpr
[n
] = tmp
;
1031 return sizeof(target_ulong
);
1033 if (env
->CP0_Config1
& (1 << CP0C1_FP
)
1034 && n
>= 38 && n
< 73) {
1036 if (env
->CP0_Status
& (1 << CP0St_FR
))
1037 env
->active_fpu
.fpr
[n
- 38].d
= tmp
;
1039 env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
] = tmp
;
1043 env
->active_fpu
.fcr31
= tmp
& 0xFF83FFFF;
1044 /* set rounding mode */
1045 RESTORE_ROUNDING_MODE
;
1046 #ifndef CONFIG_SOFTFLOAT
1047 /* no floating point exception for native float */
1048 SET_FP_ENABLE(env
->active_fpu
.fcr31
, 0);
1051 case 71: env
->active_fpu
.fcr0
= tmp
; break;
1053 return sizeof(target_ulong
);
1056 case 32: env
->CP0_Status
= tmp
; break;
1057 case 33: env
->active_tc
.LO
[0] = tmp
; break;
1058 case 34: env
->active_tc
.HI
[0] = tmp
; break;
1059 case 35: env
->CP0_BadVAddr
= tmp
; break;
1060 case 36: env
->CP0_Cause
= tmp
; break;
1061 case 37: env
->active_tc
.PC
= tmp
; break;
1062 case 72: /* fp, ignored */ break;
1066 /* Other registers are readonly. Ignore writes. */
1070 return sizeof(target_ulong
);
1072 #elif defined (TARGET_SH4)
1074 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1075 /* FIXME: We should use XML for this. */
1077 #define NUM_CORE_REGS 59
1079 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1082 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1083 GET_REGL(env
->gregs
[n
+ 16]);
1085 GET_REGL(env
->gregs
[n
]);
1087 } else if (n
< 16) {
1088 GET_REGL(env
->gregs
[n
- 8]);
1089 } else if (n
>= 25 && n
< 41) {
1090 GET_REGL(env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
1091 } else if (n
>= 43 && n
< 51) {
1092 GET_REGL(env
->gregs
[n
- 43]);
1093 } else if (n
>= 51 && n
< 59) {
1094 GET_REGL(env
->gregs
[n
- (51 - 16)]);
1097 case 16: GET_REGL(env
->pc
);
1098 case 17: GET_REGL(env
->pr
);
1099 case 18: GET_REGL(env
->gbr
);
1100 case 19: GET_REGL(env
->vbr
);
1101 case 20: GET_REGL(env
->mach
);
1102 case 21: GET_REGL(env
->macl
);
1103 case 22: GET_REGL(env
->sr
);
1104 case 23: GET_REGL(env
->fpul
);
1105 case 24: GET_REGL(env
->fpscr
);
1106 case 41: GET_REGL(env
->ssr
);
1107 case 42: GET_REGL(env
->spc
);
1113 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1117 tmp
= ldl_p(mem_buf
);
1120 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1121 env
->gregs
[n
+ 16] = tmp
;
1123 env
->gregs
[n
] = tmp
;
1126 } else if (n
< 16) {
1127 env
->gregs
[n
- 8] = tmp
;
1129 } else if (n
>= 25 && n
< 41) {
1130 env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)] = tmp
;
1131 } else if (n
>= 43 && n
< 51) {
1132 env
->gregs
[n
- 43] = tmp
;
1134 } else if (n
>= 51 && n
< 59) {
1135 env
->gregs
[n
- (51 - 16)] = tmp
;
1139 case 16: env
->pc
= tmp
;
1140 case 17: env
->pr
= tmp
;
1141 case 18: env
->gbr
= tmp
;
1142 case 19: env
->vbr
= tmp
;
1143 case 20: env
->mach
= tmp
;
1144 case 21: env
->macl
= tmp
;
1145 case 22: env
->sr
= tmp
;
1146 case 23: env
->fpul
= tmp
;
1147 case 24: env
->fpscr
= tmp
;
1148 case 41: env
->ssr
= tmp
;
1149 case 42: env
->spc
= tmp
;
1155 #elif defined (TARGET_CRIS)
1157 #define NUM_CORE_REGS 49
1159 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1163 srs
= env
->pregs
[PR_SRS
];
1165 GET_REG32(env
->regs
[n
]);
1168 if (n
>= 21 && n
< 32) {
1169 GET_REG32(env
->pregs
[n
- 16]);
1171 if (n
>= 33 && n
< 49) {
1172 GET_REG32(env
->sregs
[srs
][n
- 33]);
1175 case 16: GET_REG8(env
->pregs
[0]);
1176 case 17: GET_REG8(env
->pregs
[1]);
1177 case 18: GET_REG32(env
->pregs
[2]);
1178 case 19: GET_REG8(srs
);
1179 case 20: GET_REG16(env
->pregs
[4]);
1180 case 32: GET_REG32(env
->pc
);
1186 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1193 tmp
= ldl_p(mem_buf
);
1199 if (n
>= 21 && n
< 32) {
1200 env
->pregs
[n
- 16] = tmp
;
1203 /* FIXME: Should support function regs be writable? */
1207 case 18: env
->pregs
[PR_PID
] = tmp
; break;
1210 case 32: env
->pc
= tmp
; break;
1215 #elif defined (TARGET_ALPHA)
1217 #define NUM_CORE_REGS 65
1219 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1222 GET_REGL(env
->ir
[n
]);
1230 val
=*((uint64_t *)&env
->fir
[n
-32]);
1234 GET_REGL(env
->fpcr
);
1246 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1249 tmp
= ldtul_p(mem_buf
);
1255 if (n
> 31 && n
< 63) {
1256 env
->fir
[n
- 32] = ldfl_p(mem_buf
);
1267 #define NUM_CORE_REGS 0
1269 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1274 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1281 static int num_g_regs
= NUM_CORE_REGS
;
1284 /* Encode data using the encoding for 'x' packets. */
1285 static int memtox(char *buf
, const char *mem
, int len
)
1293 case '#': case '$': case '*': case '}':
1305 static const char *get_feature_xml(const char *p
, const char **newp
)
1307 extern const char *const xml_builtin
[][2];
1311 static char target_xml
[1024];
1314 while (p
[len
] && p
[len
] != ':')
1319 if (strncmp(p
, "target.xml", len
) == 0) {
1320 /* Generate the XML description for this CPU. */
1321 if (!target_xml
[0]) {
1322 GDBRegisterState
*r
;
1324 snprintf(target_xml
, sizeof(target_xml
),
1325 "<?xml version=\"1.0\"?>"
1326 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1328 "<xi:include href=\"%s\"/>",
1331 for (r
= first_cpu
->gdb_regs
; r
; r
= r
->next
) {
1332 strcat(target_xml
, "<xi:include href=\"");
1333 strcat(target_xml
, r
->xml
);
1334 strcat(target_xml
, "\"/>");
1336 strcat(target_xml
, "</target>");
1340 for (i
= 0; ; i
++) {
1341 name
= xml_builtin
[i
][0];
1342 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
1345 return name
? xml_builtin
[i
][1] : NULL
;
1349 static int gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1351 GDBRegisterState
*r
;
1353 if (reg
< NUM_CORE_REGS
)
1354 return cpu_gdb_read_register(env
, mem_buf
, reg
);
1356 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1357 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1358 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
1364 static int gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1366 GDBRegisterState
*r
;
1368 if (reg
< NUM_CORE_REGS
)
1369 return cpu_gdb_write_register(env
, mem_buf
, reg
);
1371 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1372 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1373 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
1379 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
1380 specifies the first register number and these registers are included in
1381 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1382 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1385 void gdb_register_coprocessor(CPUState
* env
,
1386 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
1387 int num_regs
, const char *xml
, int g_pos
)
1389 GDBRegisterState
*s
;
1390 GDBRegisterState
**p
;
1391 static int last_reg
= NUM_CORE_REGS
;
1393 s
= (GDBRegisterState
*)qemu_mallocz(sizeof(GDBRegisterState
));
1394 s
->base_reg
= last_reg
;
1395 s
->num_regs
= num_regs
;
1396 s
->get_reg
= get_reg
;
1397 s
->set_reg
= set_reg
;
1401 /* Check for duplicates. */
1402 if (strcmp((*p
)->xml
, xml
) == 0)
1406 /* Add to end of list. */
1407 last_reg
+= num_regs
;
1410 if (g_pos
!= s
->base_reg
) {
1411 fprintf(stderr
, "Error: Bad gdb register numbering for '%s'\n"
1412 "Expected %d got %d\n", xml
, g_pos
, s
->base_reg
);
1414 num_g_regs
= last_reg
;
1419 /* GDB breakpoint/watchpoint types */
1420 #define GDB_BREAKPOINT_SW 0
1421 #define GDB_BREAKPOINT_HW 1
1422 #define GDB_WATCHPOINT_WRITE 2
1423 #define GDB_WATCHPOINT_READ 3
1424 #define GDB_WATCHPOINT_ACCESS 4
1426 #ifndef CONFIG_USER_ONLY
1427 static const int xlat_gdb_type
[] = {
1428 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
1429 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
1430 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
1434 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
1440 case GDB_BREAKPOINT_SW
:
1441 case GDB_BREAKPOINT_HW
:
1442 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1443 err
= cpu_breakpoint_insert(env
, addr
, BP_GDB
, NULL
);
1448 #ifndef CONFIG_USER_ONLY
1449 case GDB_WATCHPOINT_WRITE
:
1450 case GDB_WATCHPOINT_READ
:
1451 case GDB_WATCHPOINT_ACCESS
:
1452 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1453 err
= cpu_watchpoint_insert(env
, addr
, len
, xlat_gdb_type
[type
],
1465 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1471 case GDB_BREAKPOINT_SW
:
1472 case GDB_BREAKPOINT_HW
:
1473 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1474 err
= cpu_breakpoint_remove(env
, addr
, BP_GDB
);
1479 #ifndef CONFIG_USER_ONLY
1480 case GDB_WATCHPOINT_WRITE
:
1481 case GDB_WATCHPOINT_READ
:
1482 case GDB_WATCHPOINT_ACCESS
:
1483 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1484 err
= cpu_watchpoint_remove(env
, addr
, len
, xlat_gdb_type
[type
]);
1495 static void gdb_breakpoint_remove_all(void)
1499 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1500 cpu_breakpoint_remove_all(env
, BP_GDB
);
1501 #ifndef CONFIG_USER_ONLY
1502 cpu_watchpoint_remove_all(env
, BP_GDB
);
1507 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1511 int ch
, reg_size
, type
, res
, thread
;
1512 char buf
[MAX_PACKET_LENGTH
];
1513 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1515 target_ulong addr
, len
;
1518 printf("command='%s'\n", line_buf
);
1524 /* TODO: Make this return the correct value for user-mode. */
1525 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
1526 s
->c_cpu
->cpu_index
+1);
1528 /* Remove all the breakpoints when this query is issued,
1529 * because gdb is doing and initial connect and the state
1530 * should be cleaned up.
1532 gdb_breakpoint_remove_all();
1536 addr
= strtoull(p
, (char **)&p
, 16);
1537 #if defined(TARGET_I386)
1538 s
->c_cpu
->eip
= addr
;
1539 #elif defined (TARGET_PPC)
1540 s
->c_cpu
->nip
= addr
;
1541 #elif defined (TARGET_SPARC)
1542 s
->c_cpu
->pc
= addr
;
1543 s
->c_cpu
->npc
= addr
+ 4;
1544 #elif defined (TARGET_ARM)
1545 s
->c_cpu
->regs
[15] = addr
;
1546 #elif defined (TARGET_SH4)
1547 s
->c_cpu
->pc
= addr
;
1548 #elif defined (TARGET_MIPS)
1549 s
->c_cpu
->active_tc
.PC
= addr
;
1550 #elif defined (TARGET_CRIS)
1551 s
->c_cpu
->pc
= addr
;
1552 #elif defined (TARGET_ALPHA)
1553 s
->c_cpu
->pc
= addr
;
1560 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1561 if (s
->signal
== -1)
1566 /* Kill the target */
1567 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1571 gdb_breakpoint_remove_all();
1573 put_packet(s
, "OK");
1577 addr
= strtoull(p
, (char **)&p
, 16);
1578 #if defined(TARGET_I386)
1579 s
->c_cpu
->eip
= addr
;
1580 #elif defined (TARGET_PPC)
1581 s
->c_cpu
->nip
= addr
;
1582 #elif defined (TARGET_SPARC)
1583 s
->c_cpu
->pc
= addr
;
1584 s
->c_cpu
->npc
= addr
+ 4;
1585 #elif defined (TARGET_ARM)
1586 s
->c_cpu
->regs
[15] = addr
;
1587 #elif defined (TARGET_SH4)
1588 s
->c_cpu
->pc
= addr
;
1589 #elif defined (TARGET_MIPS)
1590 s
->c_cpu
->active_tc
.PC
= addr
;
1591 #elif defined (TARGET_CRIS)
1592 s
->c_cpu
->pc
= addr
;
1593 #elif defined (TARGET_ALPHA)
1594 s
->c_cpu
->pc
= addr
;
1597 cpu_single_step(s
->c_cpu
, sstep_flags
);
1605 ret
= strtoull(p
, (char **)&p
, 16);
1608 err
= strtoull(p
, (char **)&p
, 16);
1615 if (gdb_current_syscall_cb
)
1616 gdb_current_syscall_cb(s
->c_cpu
, ret
, err
);
1618 put_packet(s
, "T02");
1626 for (addr
= 0; addr
< num_g_regs
; addr
++) {
1627 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1630 memtohex(buf
, mem_buf
, len
);
1634 registers
= mem_buf
;
1635 len
= strlen(p
) / 2;
1636 hextomem((uint8_t *)registers
, p
, len
);
1637 for (addr
= 0; addr
< num_g_regs
&& len
> 0; addr
++) {
1638 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1640 registers
+= reg_size
;
1642 put_packet(s
, "OK");
1645 addr
= strtoull(p
, (char **)&p
, 16);
1648 len
= strtoull(p
, NULL
, 16);
1649 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 0) != 0) {
1650 put_packet (s
, "E14");
1652 memtohex(buf
, mem_buf
, len
);
1657 addr
= strtoull(p
, (char **)&p
, 16);
1660 len
= strtoull(p
, (char **)&p
, 16);
1663 hextomem(mem_buf
, p
, len
);
1664 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 1) != 0)
1665 put_packet(s
, "E14");
1667 put_packet(s
, "OK");
1670 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1671 This works, but can be very slow. Anything new enough to
1672 understand XML also knows how to use this properly. */
1674 goto unknown_command
;
1675 addr
= strtoull(p
, (char **)&p
, 16);
1676 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1678 memtohex(buf
, mem_buf
, reg_size
);
1681 put_packet(s
, "E14");
1686 goto unknown_command
;
1687 addr
= strtoull(p
, (char **)&p
, 16);
1690 reg_size
= strlen(p
) / 2;
1691 hextomem(mem_buf
, p
, reg_size
);
1692 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1693 put_packet(s
, "OK");
1697 type
= strtoul(p
, (char **)&p
, 16);
1700 addr
= strtoull(p
, (char **)&p
, 16);
1703 len
= strtoull(p
, (char **)&p
, 16);
1705 res
= gdb_breakpoint_insert(addr
, len
, type
);
1707 res
= gdb_breakpoint_remove(addr
, len
, type
);
1709 put_packet(s
, "OK");
1710 else if (res
== -ENOSYS
)
1713 put_packet(s
, "E22");
1717 thread
= strtoull(p
, (char **)&p
, 16);
1718 if (thread
== -1 || thread
== 0) {
1719 put_packet(s
, "OK");
1722 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1723 if (env
->cpu_index
+ 1 == thread
)
1726 put_packet(s
, "E22");
1732 put_packet(s
, "OK");
1736 put_packet(s
, "OK");
1739 put_packet(s
, "E22");
1744 thread
= strtoull(p
, (char **)&p
, 16);
1745 #ifndef CONFIG_USER_ONLY
1746 if (thread
> 0 && thread
< smp_cpus
+ 1)
1750 put_packet(s
, "OK");
1752 put_packet(s
, "E22");
1756 /* parse any 'q' packets here */
1757 if (!strcmp(p
,"qemu.sstepbits")) {
1758 /* Query Breakpoint bit definitions */
1759 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1765 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1766 /* Display or change the sstep_flags */
1769 /* Display current setting */
1770 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1775 type
= strtoul(p
, (char **)&p
, 16);
1777 put_packet(s
, "OK");
1779 } else if (strcmp(p
,"C") == 0) {
1780 /* "Current thread" remains vague in the spec, so always return
1781 * the first CPU (gdb returns the first thread). */
1782 put_packet(s
, "QC1");
1784 } else if (strcmp(p
,"fThreadInfo") == 0) {
1785 s
->query_cpu
= first_cpu
;
1786 goto report_cpuinfo
;
1787 } else if (strcmp(p
,"sThreadInfo") == 0) {
1790 snprintf(buf
, sizeof(buf
), "m%x", s
->query_cpu
->cpu_index
+1);
1792 s
->query_cpu
= s
->query_cpu
->next_cpu
;
1796 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1797 thread
= strtoull(p
+16, (char **)&p
, 16);
1798 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1799 if (env
->cpu_index
+ 1 == thread
) {
1800 len
= snprintf((char *)mem_buf
, sizeof(mem_buf
),
1801 "CPU#%d [%s]", env
->cpu_index
,
1802 env
->halted
? "halted " : "running");
1803 memtohex(buf
, mem_buf
, len
);
1809 #ifdef CONFIG_LINUX_USER
1810 else if (strncmp(p
, "Offsets", 7) == 0) {
1811 TaskState
*ts
= s
->c_cpu
->opaque
;
1813 snprintf(buf
, sizeof(buf
),
1814 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1815 ";Bss=" TARGET_ABI_FMT_lx
,
1816 ts
->info
->code_offset
,
1817 ts
->info
->data_offset
,
1818 ts
->info
->data_offset
);
1823 if (strncmp(p
, "Supported", 9) == 0) {
1824 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1826 strcat(buf
, ";qXfer:features:read+");
1832 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1834 target_ulong total_len
;
1838 xml
= get_feature_xml(p
, &p
);
1840 snprintf(buf
, sizeof(buf
), "E00");
1847 addr
= strtoul(p
, (char **)&p
, 16);
1850 len
= strtoul(p
, (char **)&p
, 16);
1852 total_len
= strlen(xml
);
1853 if (addr
> total_len
) {
1854 snprintf(buf
, sizeof(buf
), "E00");
1858 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
1859 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1860 if (len
< total_len
- addr
) {
1862 len
= memtox(buf
+ 1, xml
+ addr
, len
);
1865 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
1867 put_packet_binary(s
, buf
, len
+ 1);
1871 /* Unrecognised 'q' command. */
1872 goto unknown_command
;
1876 /* put empty packet */
1884 void gdb_set_stop_cpu(CPUState
*env
)
1886 gdbserver_state
->c_cpu
= env
;
1887 gdbserver_state
->g_cpu
= env
;
1890 #ifndef CONFIG_USER_ONLY
1891 static void gdb_vm_state_change(void *opaque
, int running
, int reason
)
1893 GDBState
*s
= gdbserver_state
;
1894 CPUState
*env
= s
->c_cpu
;
1899 if (running
|| (reason
!= EXCP_DEBUG
&& reason
!= EXCP_INTERRUPT
) ||
1900 s
->state
== RS_SYSCALL
)
1903 /* disable single step if it was enable */
1904 cpu_single_step(env
, 0);
1906 if (reason
== EXCP_DEBUG
) {
1907 if (env
->watchpoint_hit
) {
1908 switch (env
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
1919 snprintf(buf
, sizeof(buf
),
1920 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
1921 GDB_SIGNAL_TRAP
, env
->cpu_index
+1, type
,
1922 env
->watchpoint_hit
->vaddr
);
1924 env
->watchpoint_hit
= NULL
;
1928 ret
= GDB_SIGNAL_TRAP
;
1930 ret
= GDB_SIGNAL_INT
;
1932 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, env
->cpu_index
+1);
1937 /* Send a gdb syscall request.
1938 This accepts limited printf-style format specifiers, specifically:
1939 %x - target_ulong argument printed in hex.
1940 %lx - 64-bit argument printed in hex.
1941 %s - string pointer (target_ulong) and length (int) pair. */
1942 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
1951 s
= gdbserver_state
;
1954 gdb_current_syscall_cb
= cb
;
1955 s
->state
= RS_SYSCALL
;
1956 #ifndef CONFIG_USER_ONLY
1957 vm_stop(EXCP_DEBUG
);
1968 addr
= va_arg(va
, target_ulong
);
1969 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
, addr
);
1972 if (*(fmt
++) != 'x')
1974 i64
= va_arg(va
, uint64_t);
1975 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, "%" PRIx64
, i64
);
1978 addr
= va_arg(va
, target_ulong
);
1979 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
"/%x",
1980 addr
, va_arg(va
, int));
1984 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1995 #ifdef CONFIG_USER_ONLY
1996 gdb_handlesig(s
->c_cpu
, 0);
1998 cpu_interrupt(s
->c_cpu
, CPU_INTERRUPT_EXIT
);
2002 static void gdb_read_byte(GDBState
*s
, int ch
)
2007 #ifndef CONFIG_USER_ONLY
2008 if (s
->last_packet_len
) {
2009 /* Waiting for a response to the last packet. If we see the start
2010 of a new command then abandon the previous response. */
2013 printf("Got NACK, retransmitting\n");
2015 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2019 printf("Got ACK\n");
2021 printf("Got '%c' when expecting ACK/NACK\n", ch
);
2023 if (ch
== '+' || ch
== '$')
2024 s
->last_packet_len
= 0;
2029 /* when the CPU is running, we cannot do anything except stop
2030 it when receiving a char */
2031 vm_stop(EXCP_INTERRUPT
);
2038 s
->line_buf_index
= 0;
2039 s
->state
= RS_GETLINE
;
2044 s
->state
= RS_CHKSUM1
;
2045 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2048 s
->line_buf
[s
->line_buf_index
++] = ch
;
2052 s
->line_buf
[s
->line_buf_index
] = '\0';
2053 s
->line_csum
= fromhex(ch
) << 4;
2054 s
->state
= RS_CHKSUM2
;
2057 s
->line_csum
|= fromhex(ch
);
2059 for(i
= 0; i
< s
->line_buf_index
; i
++) {
2060 csum
+= s
->line_buf
[i
];
2062 if (s
->line_csum
!= (csum
& 0xff)) {
2064 put_buffer(s
, &reply
, 1);
2068 put_buffer(s
, &reply
, 1);
2069 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2078 #ifdef CONFIG_USER_ONLY
2084 s
= gdbserver_state
;
2086 if (gdbserver_fd
< 0 || s
->fd
< 0)
2093 gdb_handlesig (CPUState
*env
, int sig
)
2099 s
= gdbserver_state
;
2100 if (gdbserver_fd
< 0 || s
->fd
< 0)
2103 /* disable single step if it was enabled */
2104 cpu_single_step(env
, 0);
2109 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb (sig
));
2112 /* put_packet() might have detected that the peer terminated the
2119 s
->running_state
= 0;
2120 while (s
->running_state
== 0) {
2121 n
= read (s
->fd
, buf
, 256);
2126 for (i
= 0; i
< n
; i
++)
2127 gdb_read_byte (s
, buf
[i
]);
2129 else if (n
== 0 || errno
!= EAGAIN
)
2131 /* XXX: Connection closed. Should probably wait for annother
2132 connection before continuing. */
2141 /* Tell the remote gdb that the process has exited. */
2142 void gdb_exit(CPUState
*env
, int code
)
2147 s
= gdbserver_state
;
2148 if (gdbserver_fd
< 0 || s
->fd
< 0)
2151 snprintf(buf
, sizeof(buf
), "W%02x", code
);
2155 /* Tell the remote gdb that the process has exited due to SIG. */
2156 void gdb_signalled(CPUState
*env
, int sig
)
2161 s
= gdbserver_state
;
2162 if (gdbserver_fd
< 0 || s
->fd
< 0)
2165 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb (sig
));
2169 static void gdb_accept(void)
2172 struct sockaddr_in sockaddr
;
2177 len
= sizeof(sockaddr
);
2178 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2179 if (fd
< 0 && errno
!= EINTR
) {
2182 } else if (fd
>= 0) {
2187 /* set short latency */
2189 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2191 s
= qemu_mallocz(sizeof(GDBState
));
2193 memset (s
, 0, sizeof (GDBState
));
2194 s
->c_cpu
= first_cpu
;
2195 s
->g_cpu
= first_cpu
;
2199 gdbserver_state
= s
;
2201 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2204 static int gdbserver_open(int port
)
2206 struct sockaddr_in sockaddr
;
2209 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2215 /* allow fast reuse */
2217 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
2219 sockaddr
.sin_family
= AF_INET
;
2220 sockaddr
.sin_port
= htons(port
);
2221 sockaddr
.sin_addr
.s_addr
= 0;
2222 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2227 ret
= listen(fd
, 0);
2235 int gdbserver_start(int port
)
2237 gdbserver_fd
= gdbserver_open(port
);
2238 if (gdbserver_fd
< 0)
2240 /* accept connections */
2245 /* Disable gdb stub for child processes. */
2246 void gdbserver_fork(CPUState
*env
)
2248 GDBState
*s
= gdbserver_state
;
2249 if (gdbserver_fd
< 0 || s
->fd
< 0)
2253 cpu_breakpoint_remove_all(env
, BP_GDB
);
2254 cpu_watchpoint_remove_all(env
, BP_GDB
);
2257 static int gdb_chr_can_receive(void *opaque
)
2259 /* We can handle an arbitrarily large amount of data.
2260 Pick the maximum packet size, which is as good as anything. */
2261 return MAX_PACKET_LENGTH
;
2264 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2268 for (i
= 0; i
< size
; i
++) {
2269 gdb_read_byte(gdbserver_state
, buf
[i
]);
2273 static void gdb_chr_event(void *opaque
, int event
)
2276 case CHR_EVENT_RESET
:
2277 vm_stop(EXCP_INTERRUPT
);
2285 int gdbserver_start(const char *port
)
2288 char gdbstub_port_name
[128];
2291 CharDriverState
*chr
;
2293 if (!port
|| !*port
)
2296 port_num
= strtol(port
, &p
, 10);
2298 /* A numeric value is interpreted as a port number. */
2299 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
2300 "tcp::%d,nowait,nodelay,server", port_num
);
2301 port
= gdbstub_port_name
;
2304 chr
= qemu_chr_open("gdb", port
, NULL
);
2308 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_change_state_handler(gdb_vm_state_change
, NULL
);