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
[] = {
212 /* In system mode we only need SIGINT and SIGTRAP; other signals
213 are not yet supported. */
220 static int gdb_signal_table
[] = {
230 #ifdef CONFIG_USER_ONLY
231 static int target_signal_to_gdb (int sig
)
234 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
235 if (gdb_signal_table
[i
] == sig
)
237 return GDB_SIGNAL_UNKNOWN
;
241 static int gdb_signal_to_target (int sig
)
243 if (sig
< ARRAY_SIZE (gdb_signal_table
))
244 return gdb_signal_table
[sig
];
251 typedef struct GDBRegisterState
{
257 struct GDBRegisterState
*next
;
267 typedef struct GDBState
{
268 CPUState
*c_cpu
; /* current CPU for step/continue ops */
269 CPUState
*g_cpu
; /* current CPU for other ops */
270 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
271 enum RSState state
; /* parsing state */
272 char line_buf
[MAX_PACKET_LENGTH
];
275 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
278 #ifdef CONFIG_USER_ONLY
282 CharDriverState
*chr
;
286 /* By default use no IRQs and no timers while single stepping so as to
287 * make single stepping like an ICE HW step.
289 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
291 static GDBState
*gdbserver_state
;
293 /* This is an ugly hack to cope with both new and old gdb.
294 If gdb sends qXfer:features:read then assume we're talking to a newish
295 gdb that understands target descriptions. */
296 static int gdb_has_xml
;
298 #ifdef CONFIG_USER_ONLY
299 /* XXX: This is not thread safe. Do we care? */
300 static int gdbserver_fd
= -1;
302 static int get_char(GDBState
*s
)
308 ret
= recv(s
->fd
, &ch
, 1, 0);
310 if (errno
== ECONNRESET
)
312 if (errno
!= EINTR
&& errno
!= EAGAIN
)
314 } else if (ret
== 0) {
326 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
334 /* If gdb is connected when the first semihosting syscall occurs then use
335 remote gdb syscalls. Otherwise use native file IO. */
336 int use_gdb_syscalls(void)
338 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
339 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
342 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
345 /* Resume execution. */
346 static inline void gdb_continue(GDBState
*s
)
348 #ifdef CONFIG_USER_ONLY
349 s
->running_state
= 1;
355 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
357 #ifdef CONFIG_USER_ONLY
361 ret
= send(s
->fd
, buf
, len
, 0);
363 if (errno
!= EINTR
&& errno
!= EAGAIN
)
371 qemu_chr_write(s
->chr
, buf
, len
);
375 static inline int fromhex(int v
)
377 if (v
>= '0' && v
<= '9')
379 else if (v
>= 'A' && v
<= 'F')
381 else if (v
>= 'a' && v
<= 'f')
387 static inline int tohex(int v
)
395 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
400 for(i
= 0; i
< len
; i
++) {
402 *q
++ = tohex(c
>> 4);
403 *q
++ = tohex(c
& 0xf);
408 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
412 for(i
= 0; i
< len
; i
++) {
413 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
418 /* return -1 if error, 0 if OK */
419 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
)
430 for(i
= 0; i
< len
; i
++) {
434 *(p
++) = tohex((csum
>> 4) & 0xf);
435 *(p
++) = tohex((csum
) & 0xf);
437 s
->last_packet_len
= p
- s
->last_packet
;
438 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
440 #ifdef CONFIG_USER_ONLY
453 /* return -1 if error, 0 if OK */
454 static int put_packet(GDBState
*s
, const char *buf
)
457 printf("reply='%s'\n", buf
);
460 return put_packet_binary(s
, buf
, strlen(buf
));
463 /* The GDB remote protocol transfers values in target byte order. This means
464 we can use the raw memory access routines to access the value buffer.
465 Conveniently, these also handle the case where the buffer is mis-aligned.
467 #define GET_REG8(val) do { \
468 stb_p(mem_buf, val); \
471 #define GET_REG16(val) do { \
472 stw_p(mem_buf, val); \
475 #define GET_REG32(val) do { \
476 stl_p(mem_buf, val); \
479 #define GET_REG64(val) do { \
480 stq_p(mem_buf, val); \
484 #if TARGET_LONG_BITS == 64
485 #define GET_REGL(val) GET_REG64(val)
486 #define ldtul_p(addr) ldq_p(addr)
488 #define GET_REGL(val) GET_REG32(val)
489 #define ldtul_p(addr) ldl_p(addr)
492 #if defined(TARGET_I386)
495 static const int gpr_map
[16] = {
496 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
497 8, 9, 10, 11, 12, 13, 14, 15
500 static const int gpr_map
[8] = {0, 1, 2, 3, 4, 5, 6, 7};
503 #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
505 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
507 if (n
< CPU_NB_REGS
) {
508 GET_REGL(env
->regs
[gpr_map
[n
]]);
509 } else if (n
>= CPU_NB_REGS
+ 8 && n
< CPU_NB_REGS
+ 16) {
510 /* FIXME: byteswap float values. */
511 #ifdef USE_X86LDOUBLE
512 memcpy(mem_buf
, &env
->fpregs
[n
- (CPU_NB_REGS
+ 8)], 10);
514 memset(mem_buf
, 0, 10);
517 } else if (n
>= CPU_NB_REGS
+ 24) {
518 n
-= CPU_NB_REGS
+ 24;
519 if (n
< CPU_NB_REGS
) {
520 stq_p(mem_buf
, env
->xmm_regs
[n
].XMM_Q(0));
521 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].XMM_Q(1));
523 } else if (n
== CPU_NB_REGS
) {
524 GET_REG32(env
->mxcsr
);
529 case 0: GET_REGL(env
->eip
);
530 case 1: GET_REG32(env
->eflags
);
531 case 2: GET_REG32(env
->segs
[R_CS
].selector
);
532 case 3: GET_REG32(env
->segs
[R_SS
].selector
);
533 case 4: GET_REG32(env
->segs
[R_DS
].selector
);
534 case 5: GET_REG32(env
->segs
[R_ES
].selector
);
535 case 6: GET_REG32(env
->segs
[R_FS
].selector
);
536 case 7: GET_REG32(env
->segs
[R_GS
].selector
);
537 /* 8...15 x87 regs. */
538 case 16: GET_REG32(env
->fpuc
);
539 case 17: GET_REG32((env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11);
540 case 18: GET_REG32(0); /* ftag */
541 case 19: GET_REG32(0); /* fiseg */
542 case 20: GET_REG32(0); /* fioff */
543 case 21: GET_REG32(0); /* foseg */
544 case 22: GET_REG32(0); /* fooff */
545 case 23: GET_REG32(0); /* fop */
552 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int i
)
556 if (i
< CPU_NB_REGS
) {
557 env
->regs
[gpr_map
[i
]] = ldtul_p(mem_buf
);
558 return sizeof(target_ulong
);
559 } else if (i
>= CPU_NB_REGS
+ 8 && i
< CPU_NB_REGS
+ 16) {
560 i
-= CPU_NB_REGS
+ 8;
561 #ifdef USE_X86LDOUBLE
562 memcpy(&env
->fpregs
[i
], mem_buf
, 10);
565 } else if (i
>= CPU_NB_REGS
+ 24) {
566 i
-= CPU_NB_REGS
+ 24;
567 if (i
< CPU_NB_REGS
) {
568 env
->xmm_regs
[i
].XMM_Q(0) = ldq_p(mem_buf
);
569 env
->xmm_regs
[i
].XMM_Q(1) = ldq_p(mem_buf
+ 8);
571 } else if (i
== CPU_NB_REGS
) {
572 env
->mxcsr
= ldl_p(mem_buf
);
578 case 0: env
->eip
= ldtul_p(mem_buf
); return sizeof(target_ulong
);
579 case 1: env
->eflags
= ldl_p(mem_buf
); return 4;
580 #if defined(CONFIG_USER_ONLY)
581 #define LOAD_SEG(index, sreg)\
582 tmp = ldl_p(mem_buf);\
583 if (tmp != env->segs[sreg].selector)\
584 cpu_x86_load_seg(env, sreg, tmp);
586 /* FIXME: Honor segment registers. Needs to avoid raising an exception
587 when the selector is invalid. */
588 #define LOAD_SEG(index, sreg) do {} while(0)
590 case 2: LOAD_SEG(10, R_CS
); return 4;
591 case 3: LOAD_SEG(11, R_SS
); return 4;
592 case 4: LOAD_SEG(12, R_DS
); return 4;
593 case 5: LOAD_SEG(13, R_ES
); return 4;
594 case 6: LOAD_SEG(14, R_FS
); return 4;
595 case 7: LOAD_SEG(15, R_GS
); return 4;
596 /* 8...15 x87 regs. */
597 case 16: env
->fpuc
= ldl_p(mem_buf
); return 4;
599 tmp
= ldl_p(mem_buf
);
600 env
->fpstt
= (tmp
>> 11) & 7;
601 env
->fpus
= tmp
& ~0x3800;
603 case 18: /* ftag */ return 4;
604 case 19: /* fiseg */ return 4;
605 case 20: /* fioff */ return 4;
606 case 21: /* foseg */ return 4;
607 case 22: /* fooff */ return 4;
608 case 23: /* fop */ return 4;
612 /* Unrecognised register. */
616 #elif defined (TARGET_PPC)
618 #define NUM_CORE_REGS 71
620 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
624 GET_REGL(env
->gpr
[n
]);
627 stfq_p(mem_buf
, env
->fpr
[n
-32]);
631 case 64: GET_REGL(env
->nip
);
632 case 65: GET_REGL(env
->msr
);
637 for (i
= 0; i
< 8; i
++)
638 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
641 case 67: GET_REGL(env
->lr
);
642 case 68: GET_REGL(env
->ctr
);
643 case 69: GET_REGL(env
->xer
);
644 case 70: GET_REG32(0); /* fpscr */
650 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
654 env
->gpr
[n
] = ldtul_p(mem_buf
);
655 return sizeof(target_ulong
);
658 env
->fpr
[n
-32] = ldfq_p(mem_buf
);
663 env
->nip
= ldtul_p(mem_buf
);
664 return sizeof(target_ulong
);
666 ppc_store_msr(env
, ldtul_p(mem_buf
));
667 return sizeof(target_ulong
);
670 uint32_t cr
= ldl_p(mem_buf
);
672 for (i
= 0; i
< 8; i
++)
673 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
677 env
->lr
= ldtul_p(mem_buf
);
678 return sizeof(target_ulong
);
680 env
->ctr
= ldtul_p(mem_buf
);
681 return sizeof(target_ulong
);
683 env
->xer
= ldtul_p(mem_buf
);
684 return sizeof(target_ulong
);
693 #elif defined (TARGET_SPARC)
695 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
696 #define NUM_CORE_REGS 86
698 #define NUM_CORE_REGS 72
702 #define GET_REGA(val) GET_REG32(val)
704 #define GET_REGA(val) GET_REGL(val)
707 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
711 GET_REGA(env
->gregs
[n
]);
714 /* register window */
715 GET_REGA(env
->regwptr
[n
- 8]);
717 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
720 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
722 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
724 case 64: GET_REGA(env
->y
);
725 case 65: GET_REGA(GET_PSR(env
));
726 case 66: GET_REGA(env
->wim
);
727 case 67: GET_REGA(env
->tbr
);
728 case 68: GET_REGA(env
->pc
);
729 case 69: GET_REGA(env
->npc
);
730 case 70: GET_REGA(env
->fsr
);
731 case 71: GET_REGA(0); /* csr */
732 default: GET_REGA(0);
737 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
740 /* f32-f62 (double width, even numbers only) */
743 val
= (uint64_t)*((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) << 32;
744 val
|= *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]);
748 case 80: GET_REGL(env
->pc
);
749 case 81: GET_REGL(env
->npc
);
750 case 82: GET_REGL(((uint64_t)GET_CCR(env
) << 32) |
751 ((env
->asi
& 0xff) << 24) |
752 ((env
->pstate
& 0xfff) << 8) |
754 case 83: GET_REGL(env
->fsr
);
755 case 84: GET_REGL(env
->fprs
);
756 case 85: GET_REGL(env
->y
);
762 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
764 #if defined(TARGET_ABI32)
767 tmp
= ldl_p(mem_buf
);
771 tmp
= ldtul_p(mem_buf
);
778 /* register window */
779 env
->regwptr
[n
- 8] = tmp
;
781 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
784 *((uint32_t *)&env
->fpr
[n
- 32]) = tmp
;
786 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
788 case 64: env
->y
= tmp
; break;
789 case 65: PUT_PSR(env
, tmp
); break;
790 case 66: env
->wim
= tmp
; break;
791 case 67: env
->tbr
= tmp
; break;
792 case 68: env
->pc
= tmp
; break;
793 case 69: env
->npc
= tmp
; break;
794 case 70: env
->fsr
= tmp
; break;
802 env
->fpr
[n
] = ldfl_p(mem_buf
);
805 /* f32-f62 (double width, even numbers only) */
806 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) = tmp
>> 32;
807 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]) = tmp
;
810 case 80: env
->pc
= tmp
; break;
811 case 81: env
->npc
= tmp
; break;
813 PUT_CCR(env
, tmp
>> 32);
814 env
->asi
= (tmp
>> 24) & 0xff;
815 env
->pstate
= (tmp
>> 8) & 0xfff;
816 PUT_CWP64(env
, tmp
& 0xff);
818 case 83: env
->fsr
= tmp
; break;
819 case 84: env
->fprs
= tmp
; break;
820 case 85: env
->y
= tmp
; break;
827 #elif defined (TARGET_ARM)
829 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
830 whatever the target description contains. Due to a historical mishap
831 the FPA registers appear in between core integer regs and the CPSR.
832 We hack round this by giving the FPA regs zero size when talking to a
834 #define NUM_CORE_REGS 26
835 #define GDB_CORE_XML "arm-core.xml"
837 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
840 /* Core integer register. */
841 GET_REG32(env
->regs
[n
]);
847 memset(mem_buf
, 0, 12);
852 /* FPA status register. */
858 GET_REG32(cpsr_read(env
));
860 /* Unknown register. */
864 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
868 tmp
= ldl_p(mem_buf
);
870 /* Mask out low bit of PC to workaround gdb bugs. This will probably
871 cause problems if we ever implement the Jazelle DBX extensions. */
876 /* Core integer register. */
880 if (n
< 24) { /* 16-23 */
881 /* FPA registers (ignored). */
888 /* FPA status register (ignored). */
894 cpsr_write (env
, tmp
, 0xffffffff);
897 /* Unknown register. */
901 #elif defined (TARGET_M68K)
903 #define NUM_CORE_REGS 18
905 #define GDB_CORE_XML "cf-core.xml"
907 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
911 GET_REG32(env
->dregs
[n
]);
914 GET_REG32(env
->aregs
[n
- 8]);
917 case 16: GET_REG32(env
->sr
);
918 case 17: GET_REG32(env
->pc
);
921 /* FP registers not included here because they vary between
922 ColdFire and m68k. Use XML bits for these. */
926 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
930 tmp
= ldl_p(mem_buf
);
937 env
->aregs
[n
- 8] = tmp
;
940 case 16: env
->sr
= tmp
; break;
941 case 17: env
->pc
= tmp
; break;
947 #elif defined (TARGET_MIPS)
949 #define NUM_CORE_REGS 73
951 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
954 GET_REGL(env
->active_tc
.gpr
[n
]);
956 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
957 if (n
>= 38 && n
< 70) {
958 if (env
->CP0_Status
& (1 << CP0St_FR
))
959 GET_REGL(env
->active_fpu
.fpr
[n
- 38].d
);
961 GET_REGL(env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
]);
964 case 70: GET_REGL((int32_t)env
->active_fpu
.fcr31
);
965 case 71: GET_REGL((int32_t)env
->active_fpu
.fcr0
);
969 case 32: GET_REGL((int32_t)env
->CP0_Status
);
970 case 33: GET_REGL(env
->active_tc
.LO
[0]);
971 case 34: GET_REGL(env
->active_tc
.HI
[0]);
972 case 35: GET_REGL(env
->CP0_BadVAddr
);
973 case 36: GET_REGL((int32_t)env
->CP0_Cause
);
974 case 37: GET_REGL(env
->active_tc
.PC
);
975 case 72: GET_REGL(0); /* fp */
976 case 89: GET_REGL((int32_t)env
->CP0_PRid
);
978 if (n
>= 73 && n
<= 88) {
979 /* 16 embedded regs. */
986 /* convert MIPS rounding mode in FCR31 to IEEE library */
987 static unsigned int ieee_rm
[] =
989 float_round_nearest_even
,
994 #define RESTORE_ROUNDING_MODE \
995 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
997 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1001 tmp
= ldtul_p(mem_buf
);
1004 env
->active_tc
.gpr
[n
] = tmp
;
1005 return sizeof(target_ulong
);
1007 if (env
->CP0_Config1
& (1 << CP0C1_FP
)
1008 && n
>= 38 && n
< 73) {
1010 if (env
->CP0_Status
& (1 << CP0St_FR
))
1011 env
->active_fpu
.fpr
[n
- 38].d
= tmp
;
1013 env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
] = tmp
;
1017 env
->active_fpu
.fcr31
= tmp
& 0xFF83FFFF;
1018 /* set rounding mode */
1019 RESTORE_ROUNDING_MODE
;
1020 #ifndef CONFIG_SOFTFLOAT
1021 /* no floating point exception for native float */
1022 SET_FP_ENABLE(env
->active_fpu
.fcr31
, 0);
1025 case 71: env
->active_fpu
.fcr0
= tmp
; break;
1027 return sizeof(target_ulong
);
1030 case 32: env
->CP0_Status
= tmp
; break;
1031 case 33: env
->active_tc
.LO
[0] = tmp
; break;
1032 case 34: env
->active_tc
.HI
[0] = tmp
; break;
1033 case 35: env
->CP0_BadVAddr
= tmp
; break;
1034 case 36: env
->CP0_Cause
= tmp
; break;
1035 case 37: env
->active_tc
.PC
= tmp
; break;
1036 case 72: /* fp, ignored */ break;
1040 /* Other registers are readonly. Ignore writes. */
1044 return sizeof(target_ulong
);
1046 #elif defined (TARGET_SH4)
1048 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1049 /* FIXME: We should use XML for this. */
1051 #define NUM_CORE_REGS 59
1053 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1056 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1057 GET_REGL(env
->gregs
[n
+ 16]);
1059 GET_REGL(env
->gregs
[n
]);
1061 } else if (n
< 16) {
1062 GET_REGL(env
->gregs
[n
- 8]);
1063 } else if (n
>= 25 && n
< 41) {
1064 GET_REGL(env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
1065 } else if (n
>= 43 && n
< 51) {
1066 GET_REGL(env
->gregs
[n
- 43]);
1067 } else if (n
>= 51 && n
< 59) {
1068 GET_REGL(env
->gregs
[n
- (51 - 16)]);
1071 case 16: GET_REGL(env
->pc
);
1072 case 17: GET_REGL(env
->pr
);
1073 case 18: GET_REGL(env
->gbr
);
1074 case 19: GET_REGL(env
->vbr
);
1075 case 20: GET_REGL(env
->mach
);
1076 case 21: GET_REGL(env
->macl
);
1077 case 22: GET_REGL(env
->sr
);
1078 case 23: GET_REGL(env
->fpul
);
1079 case 24: GET_REGL(env
->fpscr
);
1080 case 41: GET_REGL(env
->ssr
);
1081 case 42: GET_REGL(env
->spc
);
1087 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1091 tmp
= ldl_p(mem_buf
);
1094 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1095 env
->gregs
[n
+ 16] = tmp
;
1097 env
->gregs
[n
] = tmp
;
1100 } else if (n
< 16) {
1101 env
->gregs
[n
- 8] = tmp
;
1103 } else if (n
>= 25 && n
< 41) {
1104 env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)] = tmp
;
1105 } else if (n
>= 43 && n
< 51) {
1106 env
->gregs
[n
- 43] = tmp
;
1108 } else if (n
>= 51 && n
< 59) {
1109 env
->gregs
[n
- (51 - 16)] = tmp
;
1113 case 16: env
->pc
= tmp
;
1114 case 17: env
->pr
= tmp
;
1115 case 18: env
->gbr
= tmp
;
1116 case 19: env
->vbr
= tmp
;
1117 case 20: env
->mach
= tmp
;
1118 case 21: env
->macl
= tmp
;
1119 case 22: env
->sr
= tmp
;
1120 case 23: env
->fpul
= tmp
;
1121 case 24: env
->fpscr
= tmp
;
1122 case 41: env
->ssr
= tmp
;
1123 case 42: env
->spc
= tmp
;
1129 #elif defined (TARGET_CRIS)
1131 #define NUM_CORE_REGS 49
1133 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1137 srs
= env
->pregs
[PR_SRS
];
1139 GET_REG32(env
->regs
[n
]);
1142 if (n
>= 21 && n
< 32) {
1143 GET_REG32(env
->pregs
[n
- 16]);
1145 if (n
>= 33 && n
< 49) {
1146 GET_REG32(env
->sregs
[srs
][n
- 33]);
1149 case 16: GET_REG8(env
->pregs
[0]);
1150 case 17: GET_REG8(env
->pregs
[1]);
1151 case 18: GET_REG32(env
->pregs
[2]);
1152 case 19: GET_REG8(srs
);
1153 case 20: GET_REG16(env
->pregs
[4]);
1154 case 32: GET_REG32(env
->pc
);
1160 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1167 tmp
= ldl_p(mem_buf
);
1173 if (n
>= 21 && n
< 32) {
1174 env
->pregs
[n
- 16] = tmp
;
1177 /* FIXME: Should support function regs be writable? */
1181 case 18: env
->pregs
[PR_PID
] = tmp
; break;
1184 case 32: env
->pc
= tmp
; break;
1189 #elif defined (TARGET_ALPHA)
1191 #define NUM_CORE_REGS 65
1193 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1196 GET_REGL(env
->ir
[n
]);
1204 val
=*((uint64_t *)&env
->fir
[n
-32]);
1208 GET_REGL(env
->fpcr
);
1220 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1223 tmp
= ldtul_p(mem_buf
);
1229 if (n
> 31 && n
< 63) {
1230 env
->fir
[n
- 32] = ldfl_p(mem_buf
);
1241 #define NUM_CORE_REGS 0
1243 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1248 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1255 static int num_g_regs
= NUM_CORE_REGS
;
1258 /* Encode data using the encoding for 'x' packets. */
1259 static int memtox(char *buf
, const char *mem
, int len
)
1267 case '#': case '$': case '*': case '}':
1279 static const char *get_feature_xml(const char *p
, const char **newp
)
1281 extern const char *const xml_builtin
[][2];
1285 static char target_xml
[1024];
1288 while (p
[len
] && p
[len
] != ':')
1293 if (strncmp(p
, "target.xml", len
) == 0) {
1294 /* Generate the XML description for this CPU. */
1295 if (!target_xml
[0]) {
1296 GDBRegisterState
*r
;
1298 snprintf(target_xml
, sizeof(target_xml
),
1299 "<?xml version=\"1.0\"?>"
1300 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1302 "<xi:include href=\"%s\"/>",
1305 for (r
= first_cpu
->gdb_regs
; r
; r
= r
->next
) {
1306 strcat(target_xml
, "<xi:include href=\"");
1307 strcat(target_xml
, r
->xml
);
1308 strcat(target_xml
, "\"/>");
1310 strcat(target_xml
, "</target>");
1314 for (i
= 0; ; i
++) {
1315 name
= xml_builtin
[i
][0];
1316 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
1319 return name
? xml_builtin
[i
][1] : NULL
;
1323 static int gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1325 GDBRegisterState
*r
;
1327 if (reg
< NUM_CORE_REGS
)
1328 return cpu_gdb_read_register(env
, mem_buf
, reg
);
1330 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1331 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1332 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
1338 static int gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1340 GDBRegisterState
*r
;
1342 if (reg
< NUM_CORE_REGS
)
1343 return cpu_gdb_write_register(env
, mem_buf
, reg
);
1345 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1346 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1347 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
1353 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
1354 specifies the first register number and these registers are included in
1355 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1356 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1359 void gdb_register_coprocessor(CPUState
* env
,
1360 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
1361 int num_regs
, const char *xml
, int g_pos
)
1363 GDBRegisterState
*s
;
1364 GDBRegisterState
**p
;
1365 static int last_reg
= NUM_CORE_REGS
;
1367 s
= (GDBRegisterState
*)qemu_mallocz(sizeof(GDBRegisterState
));
1368 s
->base_reg
= last_reg
;
1369 s
->num_regs
= num_regs
;
1370 s
->get_reg
= get_reg
;
1371 s
->set_reg
= set_reg
;
1375 /* Check for duplicates. */
1376 if (strcmp((*p
)->xml
, xml
) == 0)
1380 /* Add to end of list. */
1381 last_reg
+= num_regs
;
1384 if (g_pos
!= s
->base_reg
) {
1385 fprintf(stderr
, "Error: Bad gdb register numbering for '%s'\n"
1386 "Expected %d got %d\n", xml
, g_pos
, s
->base_reg
);
1388 num_g_regs
= last_reg
;
1393 #ifndef CONFIG_USER_ONLY
1394 static const int xlat_gdb_type
[] = {
1395 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
1396 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
1397 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
1401 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
1407 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1410 case GDB_BREAKPOINT_SW
:
1411 case GDB_BREAKPOINT_HW
:
1412 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1413 err
= cpu_breakpoint_insert(env
, addr
, BP_GDB
, NULL
);
1418 #ifndef CONFIG_USER_ONLY
1419 case GDB_WATCHPOINT_WRITE
:
1420 case GDB_WATCHPOINT_READ
:
1421 case GDB_WATCHPOINT_ACCESS
:
1422 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1423 err
= cpu_watchpoint_insert(env
, addr
, len
, xlat_gdb_type
[type
],
1435 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1441 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1444 case GDB_BREAKPOINT_SW
:
1445 case GDB_BREAKPOINT_HW
:
1446 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1447 err
= cpu_breakpoint_remove(env
, addr
, BP_GDB
);
1452 #ifndef CONFIG_USER_ONLY
1453 case GDB_WATCHPOINT_WRITE
:
1454 case GDB_WATCHPOINT_READ
:
1455 case GDB_WATCHPOINT_ACCESS
:
1456 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1457 err
= cpu_watchpoint_remove(env
, addr
, len
, xlat_gdb_type
[type
]);
1468 static void gdb_breakpoint_remove_all(void)
1472 if (kvm_enabled()) {
1473 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1477 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1478 cpu_breakpoint_remove_all(env
, BP_GDB
);
1479 #ifndef CONFIG_USER_ONLY
1480 cpu_watchpoint_remove_all(env
, BP_GDB
);
1485 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1489 int ch
, reg_size
, type
, res
, thread
;
1490 char buf
[MAX_PACKET_LENGTH
];
1491 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1493 target_ulong addr
, len
;
1496 printf("command='%s'\n", line_buf
);
1502 /* TODO: Make this return the correct value for user-mode. */
1503 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
1504 s
->c_cpu
->cpu_index
+1);
1506 /* Remove all the breakpoints when this query is issued,
1507 * because gdb is doing and initial connect and the state
1508 * should be cleaned up.
1510 gdb_breakpoint_remove_all();
1514 addr
= strtoull(p
, (char **)&p
, 16);
1515 #if defined(TARGET_I386)
1516 s
->c_cpu
->eip
= addr
;
1517 kvm_load_registers(s
->c_cpu
);
1518 #elif defined (TARGET_PPC)
1519 s
->c_cpu
->nip
= addr
;
1520 kvm_load_registers(s
->c_cpu
);
1521 #elif defined (TARGET_SPARC)
1522 s
->c_cpu
->pc
= addr
;
1523 s
->c_cpu
->npc
= addr
+ 4;
1524 #elif defined (TARGET_ARM)
1525 s
->c_cpu
->regs
[15] = addr
;
1526 #elif defined (TARGET_SH4)
1527 s
->c_cpu
->pc
= addr
;
1528 #elif defined (TARGET_MIPS)
1529 s
->c_cpu
->active_tc
.PC
= addr
;
1530 #elif defined (TARGET_CRIS)
1531 s
->c_cpu
->pc
= addr
;
1532 #elif defined (TARGET_ALPHA)
1533 s
->c_cpu
->pc
= addr
;
1540 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1541 if (s
->signal
== -1)
1546 /* Kill the target */
1547 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1551 gdb_breakpoint_remove_all();
1553 put_packet(s
, "OK");
1557 addr
= strtoull(p
, (char **)&p
, 16);
1558 #if defined(TARGET_I386)
1559 s
->c_cpu
->eip
= addr
;
1560 kvm_load_registers(s
->c_cpu
);
1561 #elif defined (TARGET_PPC)
1562 s
->c_cpu
->nip
= addr
;
1563 kvm_load_registers(s
->c_cpu
);
1564 #elif defined (TARGET_SPARC)
1565 s
->c_cpu
->pc
= addr
;
1566 s
->c_cpu
->npc
= addr
+ 4;
1567 #elif defined (TARGET_ARM)
1568 s
->c_cpu
->regs
[15] = addr
;
1569 #elif defined (TARGET_SH4)
1570 s
->c_cpu
->pc
= addr
;
1571 #elif defined (TARGET_MIPS)
1572 s
->c_cpu
->active_tc
.PC
= addr
;
1573 #elif defined (TARGET_CRIS)
1574 s
->c_cpu
->pc
= addr
;
1575 #elif defined (TARGET_ALPHA)
1576 s
->c_cpu
->pc
= addr
;
1579 cpu_single_step(s
->c_cpu
, sstep_flags
);
1587 ret
= strtoull(p
, (char **)&p
, 16);
1590 err
= strtoull(p
, (char **)&p
, 16);
1597 if (gdb_current_syscall_cb
)
1598 gdb_current_syscall_cb(s
->c_cpu
, ret
, err
);
1600 put_packet(s
, "T02");
1607 kvm_save_registers(s
->g_cpu
);
1609 for (addr
= 0; addr
< num_g_regs
; addr
++) {
1610 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1613 memtohex(buf
, mem_buf
, len
);
1617 registers
= mem_buf
;
1618 len
= strlen(p
) / 2;
1619 hextomem((uint8_t *)registers
, p
, len
);
1620 for (addr
= 0; addr
< num_g_regs
&& len
> 0; addr
++) {
1621 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1623 registers
+= reg_size
;
1625 kvm_load_registers(s
->g_cpu
);
1626 put_packet(s
, "OK");
1629 addr
= strtoull(p
, (char **)&p
, 16);
1632 len
= strtoull(p
, NULL
, 16);
1633 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 0) != 0) {
1634 put_packet (s
, "E14");
1636 memtohex(buf
, mem_buf
, len
);
1641 addr
= strtoull(p
, (char **)&p
, 16);
1644 len
= strtoull(p
, (char **)&p
, 16);
1647 hextomem(mem_buf
, p
, len
);
1648 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 1) != 0)
1649 put_packet(s
, "E14");
1651 put_packet(s
, "OK");
1654 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1655 This works, but can be very slow. Anything new enough to
1656 understand XML also knows how to use this properly. */
1658 goto unknown_command
;
1659 addr
= strtoull(p
, (char **)&p
, 16);
1660 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1662 memtohex(buf
, mem_buf
, reg_size
);
1665 put_packet(s
, "E14");
1670 goto unknown_command
;
1671 addr
= strtoull(p
, (char **)&p
, 16);
1674 reg_size
= strlen(p
) / 2;
1675 hextomem(mem_buf
, p
, reg_size
);
1676 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1677 put_packet(s
, "OK");
1681 type
= strtoul(p
, (char **)&p
, 16);
1684 addr
= strtoull(p
, (char **)&p
, 16);
1687 len
= strtoull(p
, (char **)&p
, 16);
1689 res
= gdb_breakpoint_insert(addr
, len
, type
);
1691 res
= gdb_breakpoint_remove(addr
, len
, type
);
1693 put_packet(s
, "OK");
1694 else if (res
== -ENOSYS
)
1697 put_packet(s
, "E22");
1701 thread
= strtoull(p
, (char **)&p
, 16);
1702 if (thread
== -1 || thread
== 0) {
1703 put_packet(s
, "OK");
1706 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1707 if (env
->cpu_index
+ 1 == thread
)
1710 put_packet(s
, "E22");
1716 put_packet(s
, "OK");
1720 put_packet(s
, "OK");
1723 put_packet(s
, "E22");
1728 thread
= strtoull(p
, (char **)&p
, 16);
1729 #ifndef CONFIG_USER_ONLY
1730 if (thread
> 0 && thread
< smp_cpus
+ 1)
1734 put_packet(s
, "OK");
1736 put_packet(s
, "E22");
1740 /* parse any 'q' packets here */
1741 if (!strcmp(p
,"qemu.sstepbits")) {
1742 /* Query Breakpoint bit definitions */
1743 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1749 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1750 /* Display or change the sstep_flags */
1753 /* Display current setting */
1754 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1759 type
= strtoul(p
, (char **)&p
, 16);
1761 put_packet(s
, "OK");
1763 } else if (strcmp(p
,"C") == 0) {
1764 /* "Current thread" remains vague in the spec, so always return
1765 * the first CPU (gdb returns the first thread). */
1766 put_packet(s
, "QC1");
1768 } else if (strcmp(p
,"fThreadInfo") == 0) {
1769 s
->query_cpu
= first_cpu
;
1770 goto report_cpuinfo
;
1771 } else if (strcmp(p
,"sThreadInfo") == 0) {
1774 snprintf(buf
, sizeof(buf
), "m%x", s
->query_cpu
->cpu_index
+1);
1776 s
->query_cpu
= s
->query_cpu
->next_cpu
;
1780 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1781 thread
= strtoull(p
+16, (char **)&p
, 16);
1782 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1783 if (env
->cpu_index
+ 1 == thread
) {
1784 kvm_save_registers(env
);
1785 len
= snprintf((char *)mem_buf
, sizeof(mem_buf
),
1786 "CPU#%d [%s]", env
->cpu_index
,
1787 env
->halted
? "halted " : "running");
1788 memtohex(buf
, mem_buf
, len
);
1794 #ifdef CONFIG_LINUX_USER
1795 else if (strncmp(p
, "Offsets", 7) == 0) {
1796 TaskState
*ts
= s
->c_cpu
->opaque
;
1798 snprintf(buf
, sizeof(buf
),
1799 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1800 ";Bss=" TARGET_ABI_FMT_lx
,
1801 ts
->info
->code_offset
,
1802 ts
->info
->data_offset
,
1803 ts
->info
->data_offset
);
1808 if (strncmp(p
, "Supported", 9) == 0) {
1809 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1811 strcat(buf
, ";qXfer:features:read+");
1817 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1819 target_ulong total_len
;
1823 xml
= get_feature_xml(p
, &p
);
1825 snprintf(buf
, sizeof(buf
), "E00");
1832 addr
= strtoul(p
, (char **)&p
, 16);
1835 len
= strtoul(p
, (char **)&p
, 16);
1837 total_len
= strlen(xml
);
1838 if (addr
> total_len
) {
1839 snprintf(buf
, sizeof(buf
), "E00");
1843 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
1844 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1845 if (len
< total_len
- addr
) {
1847 len
= memtox(buf
+ 1, xml
+ addr
, len
);
1850 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
1852 put_packet_binary(s
, buf
, len
+ 1);
1856 /* Unrecognised 'q' command. */
1857 goto unknown_command
;
1861 /* put empty packet */
1869 void gdb_set_stop_cpu(CPUState
*env
)
1871 gdbserver_state
->c_cpu
= env
;
1872 gdbserver_state
->g_cpu
= env
;
1875 #ifndef CONFIG_USER_ONLY
1876 static void gdb_vm_stopped(void *opaque
, int reason
)
1878 GDBState
*s
= gdbserver_state
;
1879 CPUState
*env
= s
->c_cpu
;
1884 if (s
->state
== RS_SYSCALL
)
1887 /* disable single step if it was enable */
1888 cpu_single_step(env
, 0);
1890 if (reason
== EXCP_DEBUG
) {
1891 if (env
->watchpoint_hit
) {
1892 switch (env
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
1903 snprintf(buf
, sizeof(buf
),
1904 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
1905 GDB_SIGNAL_TRAP
, env
->cpu_index
+1, type
,
1906 env
->watchpoint_hit
->vaddr
);
1908 env
->watchpoint_hit
= NULL
;
1912 ret
= GDB_SIGNAL_TRAP
;
1913 } else if (reason
== EXCP_INTERRUPT
) {
1914 ret
= GDB_SIGNAL_INT
;
1918 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, env
->cpu_index
+1);
1923 /* Send a gdb syscall request.
1924 This accepts limited printf-style format specifiers, specifically:
1925 %x - target_ulong argument printed in hex.
1926 %lx - 64-bit argument printed in hex.
1927 %s - string pointer (target_ulong) and length (int) pair. */
1928 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
1937 s
= gdbserver_state
;
1940 gdb_current_syscall_cb
= cb
;
1941 s
->state
= RS_SYSCALL
;
1942 #ifndef CONFIG_USER_ONLY
1943 vm_stop(EXCP_DEBUG
);
1954 addr
= va_arg(va
, target_ulong
);
1955 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
, addr
);
1958 if (*(fmt
++) != 'x')
1960 i64
= va_arg(va
, uint64_t);
1961 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, "%" PRIx64
, i64
);
1964 addr
= va_arg(va
, target_ulong
);
1965 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
"/%x",
1966 addr
, va_arg(va
, int));
1970 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1981 #ifdef CONFIG_USER_ONLY
1982 gdb_handlesig(s
->c_cpu
, 0);
1984 cpu_interrupt(s
->c_cpu
, CPU_INTERRUPT_EXIT
);
1988 static void gdb_read_byte(GDBState
*s
, int ch
)
1993 #ifndef CONFIG_USER_ONLY
1994 if (s
->last_packet_len
) {
1995 /* Waiting for a response to the last packet. If we see the start
1996 of a new command then abandon the previous response. */
1999 printf("Got NACK, retransmitting\n");
2001 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2005 printf("Got ACK\n");
2007 printf("Got '%c' when expecting ACK/NACK\n", ch
);
2009 if (ch
== '+' || ch
== '$')
2010 s
->last_packet_len
= 0;
2015 /* when the CPU is running, we cannot do anything except stop
2016 it when receiving a char */
2017 vm_stop(EXCP_INTERRUPT
);
2024 s
->line_buf_index
= 0;
2025 s
->state
= RS_GETLINE
;
2030 s
->state
= RS_CHKSUM1
;
2031 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2034 s
->line_buf
[s
->line_buf_index
++] = ch
;
2038 s
->line_buf
[s
->line_buf_index
] = '\0';
2039 s
->line_csum
= fromhex(ch
) << 4;
2040 s
->state
= RS_CHKSUM2
;
2043 s
->line_csum
|= fromhex(ch
);
2045 for(i
= 0; i
< s
->line_buf_index
; i
++) {
2046 csum
+= s
->line_buf
[i
];
2048 if (s
->line_csum
!= (csum
& 0xff)) {
2050 put_buffer(s
, &reply
, 1);
2054 put_buffer(s
, &reply
, 1);
2055 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2064 #ifdef CONFIG_USER_ONLY
2070 s
= gdbserver_state
;
2072 if (gdbserver_fd
< 0 || s
->fd
< 0)
2079 gdb_handlesig (CPUState
*env
, int sig
)
2085 s
= gdbserver_state
;
2086 if (gdbserver_fd
< 0 || s
->fd
< 0)
2089 /* disable single step if it was enabled */
2090 cpu_single_step(env
, 0);
2095 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb (sig
));
2098 /* put_packet() might have detected that the peer terminated the
2105 s
->running_state
= 0;
2106 while (s
->running_state
== 0) {
2107 n
= read (s
->fd
, buf
, 256);
2112 for (i
= 0; i
< n
; i
++)
2113 gdb_read_byte (s
, buf
[i
]);
2115 else if (n
== 0 || errno
!= EAGAIN
)
2117 /* XXX: Connection closed. Should probably wait for annother
2118 connection before continuing. */
2127 /* Tell the remote gdb that the process has exited. */
2128 void gdb_exit(CPUState
*env
, int code
)
2133 s
= gdbserver_state
;
2134 if (gdbserver_fd
< 0 || s
->fd
< 0)
2137 snprintf(buf
, sizeof(buf
), "W%02x", code
);
2141 /* Tell the remote gdb that the process has exited due to SIG. */
2142 void gdb_signalled(CPUState
*env
, int sig
)
2147 s
= gdbserver_state
;
2148 if (gdbserver_fd
< 0 || s
->fd
< 0)
2151 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb (sig
));
2155 static void gdb_accept(void)
2158 struct sockaddr_in sockaddr
;
2163 len
= sizeof(sockaddr
);
2164 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2165 if (fd
< 0 && errno
!= EINTR
) {
2168 } else if (fd
>= 0) {
2173 /* set short latency */
2175 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2177 s
= qemu_mallocz(sizeof(GDBState
));
2184 memset (s
, 0, sizeof (GDBState
));
2185 s
->c_cpu
= first_cpu
;
2186 s
->g_cpu
= first_cpu
;
2190 gdbserver_state
= s
;
2192 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2195 static int gdbserver_open(int port
)
2197 struct sockaddr_in sockaddr
;
2200 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2206 /* allow fast reuse */
2208 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
2210 sockaddr
.sin_family
= AF_INET
;
2211 sockaddr
.sin_port
= htons(port
);
2212 sockaddr
.sin_addr
.s_addr
= 0;
2213 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2218 ret
= listen(fd
, 0);
2226 int gdbserver_start(int port
)
2228 gdbserver_fd
= gdbserver_open(port
);
2229 if (gdbserver_fd
< 0)
2231 /* accept connections */
2236 /* Disable gdb stub for child processes. */
2237 void gdbserver_fork(CPUState
*env
)
2239 GDBState
*s
= gdbserver_state
;
2240 if (gdbserver_fd
< 0 || s
->fd
< 0)
2244 cpu_breakpoint_remove_all(env
, BP_GDB
);
2245 cpu_watchpoint_remove_all(env
, BP_GDB
);
2248 static int gdb_chr_can_receive(void *opaque
)
2250 /* We can handle an arbitrarily large amount of data.
2251 Pick the maximum packet size, which is as good as anything. */
2252 return MAX_PACKET_LENGTH
;
2255 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2259 for (i
= 0; i
< size
; i
++) {
2260 gdb_read_byte(gdbserver_state
, buf
[i
]);
2264 static void gdb_chr_event(void *opaque
, int event
)
2267 case CHR_EVENT_RESET
:
2268 vm_stop(EXCP_INTERRUPT
);
2276 int gdbserver_start(const char *port
)
2279 char gdbstub_port_name
[128];
2282 CharDriverState
*chr
;
2284 if (!port
|| !*port
)
2287 port_num
= strtol(port
, &p
, 10);
2289 /* A numeric value is interpreted as a port number. */
2290 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
2291 "tcp::%d,nowait,nodelay,server", port_num
);
2292 port
= gdbstub_port_name
;
2295 chr
= qemu_chr_open("gdb", port
);
2299 s
= qemu_mallocz(sizeof(GDBState
));
2303 s
->c_cpu
= first_cpu
;
2304 s
->g_cpu
= first_cpu
;
2306 gdbserver_state
= s
;
2307 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2308 gdb_chr_event
, NULL
);
2309 qemu_add_vm_stop_handler(gdb_vm_stopped
, NULL
);