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
34 #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
;
289 CharDriverState
*mon_chr
;
293 /* By default use no IRQs and no timers while single stepping so as to
294 * make single stepping like an ICE HW step.
296 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
298 static GDBState
*gdbserver_state
;
300 /* This is an ugly hack to cope with both new and old gdb.
301 If gdb sends qXfer:features:read then assume we're talking to a newish
302 gdb that understands target descriptions. */
303 static int gdb_has_xml
;
305 #ifdef CONFIG_USER_ONLY
306 /* XXX: This is not thread safe. Do we care? */
307 static int gdbserver_fd
= -1;
309 static int get_char(GDBState
*s
)
315 ret
= recv(s
->fd
, &ch
, 1, 0);
317 if (errno
== ECONNRESET
)
319 if (errno
!= EINTR
&& errno
!= EAGAIN
)
321 } else if (ret
== 0) {
333 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
341 /* If gdb is connected when the first semihosting syscall occurs then use
342 remote gdb syscalls. Otherwise use native file IO. */
343 int use_gdb_syscalls(void)
345 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
346 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
349 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
352 /* Resume execution. */
353 static inline void gdb_continue(GDBState
*s
)
355 #ifdef CONFIG_USER_ONLY
356 s
->running_state
= 1;
362 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
364 #ifdef CONFIG_USER_ONLY
368 ret
= send(s
->fd
, buf
, len
, 0);
370 if (errno
!= EINTR
&& errno
!= EAGAIN
)
378 qemu_chr_write(s
->chr
, buf
, len
);
382 static inline int fromhex(int v
)
384 if (v
>= '0' && v
<= '9')
386 else if (v
>= 'A' && v
<= 'F')
388 else if (v
>= 'a' && v
<= 'f')
394 static inline int tohex(int v
)
402 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
407 for(i
= 0; i
< len
; i
++) {
409 *q
++ = tohex(c
>> 4);
410 *q
++ = tohex(c
& 0xf);
415 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
419 for(i
= 0; i
< len
; i
++) {
420 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
425 /* return -1 if error, 0 if OK */
426 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
)
437 for(i
= 0; i
< len
; i
++) {
441 *(p
++) = tohex((csum
>> 4) & 0xf);
442 *(p
++) = tohex((csum
) & 0xf);
444 s
->last_packet_len
= p
- s
->last_packet
;
445 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
447 #ifdef CONFIG_USER_ONLY
460 /* return -1 if error, 0 if OK */
461 static int put_packet(GDBState
*s
, const char *buf
)
464 printf("reply='%s'\n", buf
);
467 return put_packet_binary(s
, buf
, strlen(buf
));
470 /* The GDB remote protocol transfers values in target byte order. This means
471 we can use the raw memory access routines to access the value buffer.
472 Conveniently, these also handle the case where the buffer is mis-aligned.
474 #define GET_REG8(val) do { \
475 stb_p(mem_buf, val); \
478 #define GET_REG16(val) do { \
479 stw_p(mem_buf, val); \
482 #define GET_REG32(val) do { \
483 stl_p(mem_buf, val); \
486 #define GET_REG64(val) do { \
487 stq_p(mem_buf, val); \
491 #if TARGET_LONG_BITS == 64
492 #define GET_REGL(val) GET_REG64(val)
493 #define ldtul_p(addr) ldq_p(addr)
495 #define GET_REGL(val) GET_REG32(val)
496 #define ldtul_p(addr) ldl_p(addr)
499 #if defined(TARGET_I386)
502 static const int gpr_map
[16] = {
503 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
504 8, 9, 10, 11, 12, 13, 14, 15
507 static const int gpr_map
[8] = {0, 1, 2, 3, 4, 5, 6, 7};
510 #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
512 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
514 if (n
< CPU_NB_REGS
) {
515 GET_REGL(env
->regs
[gpr_map
[n
]]);
516 } else if (n
>= CPU_NB_REGS
+ 8 && n
< CPU_NB_REGS
+ 16) {
517 /* FIXME: byteswap float values. */
518 #ifdef USE_X86LDOUBLE
519 memcpy(mem_buf
, &env
->fpregs
[n
- (CPU_NB_REGS
+ 8)], 10);
521 memset(mem_buf
, 0, 10);
524 } else if (n
>= CPU_NB_REGS
+ 24) {
525 n
-= CPU_NB_REGS
+ 24;
526 if (n
< CPU_NB_REGS
) {
527 stq_p(mem_buf
, env
->xmm_regs
[n
].XMM_Q(0));
528 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].XMM_Q(1));
530 } else if (n
== CPU_NB_REGS
) {
531 GET_REG32(env
->mxcsr
);
536 case 0: GET_REGL(env
->eip
);
537 case 1: GET_REG32(env
->eflags
);
538 case 2: GET_REG32(env
->segs
[R_CS
].selector
);
539 case 3: GET_REG32(env
->segs
[R_SS
].selector
);
540 case 4: GET_REG32(env
->segs
[R_DS
].selector
);
541 case 5: GET_REG32(env
->segs
[R_ES
].selector
);
542 case 6: GET_REG32(env
->segs
[R_FS
].selector
);
543 case 7: GET_REG32(env
->segs
[R_GS
].selector
);
544 /* 8...15 x87 regs. */
545 case 16: GET_REG32(env
->fpuc
);
546 case 17: GET_REG32((env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11);
547 case 18: GET_REG32(0); /* ftag */
548 case 19: GET_REG32(0); /* fiseg */
549 case 20: GET_REG32(0); /* fioff */
550 case 21: GET_REG32(0); /* foseg */
551 case 22: GET_REG32(0); /* fooff */
552 case 23: GET_REG32(0); /* fop */
559 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int i
)
563 if (i
< CPU_NB_REGS
) {
564 env
->regs
[gpr_map
[i
]] = ldtul_p(mem_buf
);
565 return sizeof(target_ulong
);
566 } else if (i
>= CPU_NB_REGS
+ 8 && i
< CPU_NB_REGS
+ 16) {
567 i
-= CPU_NB_REGS
+ 8;
568 #ifdef USE_X86LDOUBLE
569 memcpy(&env
->fpregs
[i
], mem_buf
, 10);
572 } else if (i
>= CPU_NB_REGS
+ 24) {
573 i
-= CPU_NB_REGS
+ 24;
574 if (i
< CPU_NB_REGS
) {
575 env
->xmm_regs
[i
].XMM_Q(0) = ldq_p(mem_buf
);
576 env
->xmm_regs
[i
].XMM_Q(1) = ldq_p(mem_buf
+ 8);
578 } else if (i
== CPU_NB_REGS
) {
579 env
->mxcsr
= ldl_p(mem_buf
);
585 case 0: env
->eip
= ldtul_p(mem_buf
); return sizeof(target_ulong
);
586 case 1: env
->eflags
= ldl_p(mem_buf
); return 4;
587 #if defined(CONFIG_USER_ONLY)
588 #define LOAD_SEG(index, sreg)\
589 tmp = ldl_p(mem_buf);\
590 if (tmp != env->segs[sreg].selector)\
591 cpu_x86_load_seg(env, sreg, tmp);
593 /* FIXME: Honor segment registers. Needs to avoid raising an exception
594 when the selector is invalid. */
595 #define LOAD_SEG(index, sreg) do {} while(0)
597 case 2: LOAD_SEG(10, R_CS
); return 4;
598 case 3: LOAD_SEG(11, R_SS
); return 4;
599 case 4: LOAD_SEG(12, R_DS
); return 4;
600 case 5: LOAD_SEG(13, R_ES
); return 4;
601 case 6: LOAD_SEG(14, R_FS
); return 4;
602 case 7: LOAD_SEG(15, R_GS
); return 4;
603 /* 8...15 x87 regs. */
604 case 16: env
->fpuc
= ldl_p(mem_buf
); return 4;
606 tmp
= ldl_p(mem_buf
);
607 env
->fpstt
= (tmp
>> 11) & 7;
608 env
->fpus
= tmp
& ~0x3800;
610 case 18: /* ftag */ return 4;
611 case 19: /* fiseg */ return 4;
612 case 20: /* fioff */ return 4;
613 case 21: /* foseg */ return 4;
614 case 22: /* fooff */ return 4;
615 case 23: /* fop */ return 4;
619 /* Unrecognised register. */
623 #elif defined (TARGET_PPC)
625 /* Old gdb always expects FP registers. Newer (xml-aware) gdb only
626 expects whatever the target description contains. Due to a
627 historical mishap the FP registers appear in between core integer
628 regs and PC, MSR, CR, and so forth. We hack round this by giving the
629 FP regs zero size when talking to a newer gdb. */
630 #define NUM_CORE_REGS 71
631 #if defined (TARGET_PPC64)
632 #define GDB_CORE_XML "power64-core.xml"
634 #define GDB_CORE_XML "power-core.xml"
637 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
641 GET_REGL(env
->gpr
[n
]);
646 stfq_p(mem_buf
, env
->fpr
[n
-32]);
650 case 64: GET_REGL(env
->nip
);
651 case 65: GET_REGL(env
->msr
);
656 for (i
= 0; i
< 8; i
++)
657 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
660 case 67: GET_REGL(env
->lr
);
661 case 68: GET_REGL(env
->ctr
);
662 case 69: GET_REGL(env
->xer
);
667 GET_REG32(0); /* fpscr */
674 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
678 env
->gpr
[n
] = ldtul_p(mem_buf
);
679 return sizeof(target_ulong
);
684 env
->fpr
[n
-32] = ldfq_p(mem_buf
);
689 env
->nip
= ldtul_p(mem_buf
);
690 return sizeof(target_ulong
);
692 ppc_store_msr(env
, ldtul_p(mem_buf
));
693 return sizeof(target_ulong
);
696 uint32_t cr
= ldl_p(mem_buf
);
698 for (i
= 0; i
< 8; i
++)
699 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
703 env
->lr
= ldtul_p(mem_buf
);
704 return sizeof(target_ulong
);
706 env
->ctr
= ldtul_p(mem_buf
);
707 return sizeof(target_ulong
);
709 env
->xer
= ldtul_p(mem_buf
);
710 return sizeof(target_ulong
);
721 #elif defined (TARGET_SPARC)
723 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
724 #define NUM_CORE_REGS 86
726 #define NUM_CORE_REGS 72
730 #define GET_REGA(val) GET_REG32(val)
732 #define GET_REGA(val) GET_REGL(val)
735 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
739 GET_REGA(env
->gregs
[n
]);
742 /* register window */
743 GET_REGA(env
->regwptr
[n
- 8]);
745 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
748 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
750 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
752 case 64: GET_REGA(env
->y
);
753 case 65: GET_REGA(GET_PSR(env
));
754 case 66: GET_REGA(env
->wim
);
755 case 67: GET_REGA(env
->tbr
);
756 case 68: GET_REGA(env
->pc
);
757 case 69: GET_REGA(env
->npc
);
758 case 70: GET_REGA(env
->fsr
);
759 case 71: GET_REGA(0); /* csr */
760 default: GET_REGA(0);
765 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
768 /* f32-f62 (double width, even numbers only) */
771 val
= (uint64_t)*((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) << 32;
772 val
|= *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]);
776 case 80: GET_REGL(env
->pc
);
777 case 81: GET_REGL(env
->npc
);
778 case 82: GET_REGL(((uint64_t)GET_CCR(env
) << 32) |
779 ((env
->asi
& 0xff) << 24) |
780 ((env
->pstate
& 0xfff) << 8) |
782 case 83: GET_REGL(env
->fsr
);
783 case 84: GET_REGL(env
->fprs
);
784 case 85: GET_REGL(env
->y
);
790 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
792 #if defined(TARGET_ABI32)
795 tmp
= ldl_p(mem_buf
);
799 tmp
= ldtul_p(mem_buf
);
806 /* register window */
807 env
->regwptr
[n
- 8] = tmp
;
809 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
812 *((uint32_t *)&env
->fpr
[n
- 32]) = tmp
;
814 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
816 case 64: env
->y
= tmp
; break;
817 case 65: PUT_PSR(env
, tmp
); break;
818 case 66: env
->wim
= tmp
; break;
819 case 67: env
->tbr
= tmp
; break;
820 case 68: env
->pc
= tmp
; break;
821 case 69: env
->npc
= tmp
; break;
822 case 70: env
->fsr
= tmp
; break;
830 env
->fpr
[n
] = ldfl_p(mem_buf
);
833 /* f32-f62 (double width, even numbers only) */
834 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) = tmp
>> 32;
835 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]) = tmp
;
838 case 80: env
->pc
= tmp
; break;
839 case 81: env
->npc
= tmp
; break;
841 PUT_CCR(env
, tmp
>> 32);
842 env
->asi
= (tmp
>> 24) & 0xff;
843 env
->pstate
= (tmp
>> 8) & 0xfff;
844 PUT_CWP64(env
, tmp
& 0xff);
846 case 83: env
->fsr
= tmp
; break;
847 case 84: env
->fprs
= tmp
; break;
848 case 85: env
->y
= tmp
; break;
855 #elif defined (TARGET_ARM)
857 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
858 whatever the target description contains. Due to a historical mishap
859 the FPA registers appear in between core integer regs and the CPSR.
860 We hack round this by giving the FPA regs zero size when talking to a
862 #define NUM_CORE_REGS 26
863 #define GDB_CORE_XML "arm-core.xml"
865 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
868 /* Core integer register. */
869 GET_REG32(env
->regs
[n
]);
875 memset(mem_buf
, 0, 12);
880 /* FPA status register. */
886 GET_REG32(cpsr_read(env
));
888 /* Unknown register. */
892 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
896 tmp
= ldl_p(mem_buf
);
898 /* Mask out low bit of PC to workaround gdb bugs. This will probably
899 cause problems if we ever implement the Jazelle DBX extensions. */
904 /* Core integer register. */
908 if (n
< 24) { /* 16-23 */
909 /* FPA registers (ignored). */
916 /* FPA status register (ignored). */
922 cpsr_write (env
, tmp
, 0xffffffff);
925 /* Unknown register. */
929 #elif defined (TARGET_M68K)
931 #define NUM_CORE_REGS 18
933 #define GDB_CORE_XML "cf-core.xml"
935 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
939 GET_REG32(env
->dregs
[n
]);
942 GET_REG32(env
->aregs
[n
- 8]);
945 case 16: GET_REG32(env
->sr
);
946 case 17: GET_REG32(env
->pc
);
949 /* FP registers not included here because they vary between
950 ColdFire and m68k. Use XML bits for these. */
954 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
958 tmp
= ldl_p(mem_buf
);
965 env
->aregs
[n
- 8] = tmp
;
968 case 16: env
->sr
= tmp
; break;
969 case 17: env
->pc
= tmp
; break;
975 #elif defined (TARGET_MIPS)
977 #define NUM_CORE_REGS 73
979 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
982 GET_REGL(env
->active_tc
.gpr
[n
]);
984 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
985 if (n
>= 38 && n
< 70) {
986 if (env
->CP0_Status
& (1 << CP0St_FR
))
987 GET_REGL(env
->active_fpu
.fpr
[n
- 38].d
);
989 GET_REGL(env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
]);
992 case 70: GET_REGL((int32_t)env
->active_fpu
.fcr31
);
993 case 71: GET_REGL((int32_t)env
->active_fpu
.fcr0
);
997 case 32: GET_REGL((int32_t)env
->CP0_Status
);
998 case 33: GET_REGL(env
->active_tc
.LO
[0]);
999 case 34: GET_REGL(env
->active_tc
.HI
[0]);
1000 case 35: GET_REGL(env
->CP0_BadVAddr
);
1001 case 36: GET_REGL((int32_t)env
->CP0_Cause
);
1002 case 37: GET_REGL(env
->active_tc
.PC
);
1003 case 72: GET_REGL(0); /* fp */
1004 case 89: GET_REGL((int32_t)env
->CP0_PRid
);
1006 if (n
>= 73 && n
<= 88) {
1007 /* 16 embedded regs. */
1014 /* convert MIPS rounding mode in FCR31 to IEEE library */
1015 static unsigned int ieee_rm
[] =
1017 float_round_nearest_even
,
1018 float_round_to_zero
,
1022 #define RESTORE_ROUNDING_MODE \
1023 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
1025 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1029 tmp
= ldtul_p(mem_buf
);
1032 env
->active_tc
.gpr
[n
] = tmp
;
1033 return sizeof(target_ulong
);
1035 if (env
->CP0_Config1
& (1 << CP0C1_FP
)
1036 && n
>= 38 && n
< 73) {
1038 if (env
->CP0_Status
& (1 << CP0St_FR
))
1039 env
->active_fpu
.fpr
[n
- 38].d
= tmp
;
1041 env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
] = tmp
;
1045 env
->active_fpu
.fcr31
= tmp
& 0xFF83FFFF;
1046 /* set rounding mode */
1047 RESTORE_ROUNDING_MODE
;
1048 #ifndef CONFIG_SOFTFLOAT
1049 /* no floating point exception for native float */
1050 SET_FP_ENABLE(env
->active_fpu
.fcr31
, 0);
1053 case 71: env
->active_fpu
.fcr0
= tmp
; break;
1055 return sizeof(target_ulong
);
1058 case 32: env
->CP0_Status
= tmp
; break;
1059 case 33: env
->active_tc
.LO
[0] = tmp
; break;
1060 case 34: env
->active_tc
.HI
[0] = tmp
; break;
1061 case 35: env
->CP0_BadVAddr
= tmp
; break;
1062 case 36: env
->CP0_Cause
= tmp
; break;
1063 case 37: env
->active_tc
.PC
= tmp
; break;
1064 case 72: /* fp, ignored */ break;
1068 /* Other registers are readonly. Ignore writes. */
1072 return sizeof(target_ulong
);
1074 #elif defined (TARGET_SH4)
1076 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1077 /* FIXME: We should use XML for this. */
1079 #define NUM_CORE_REGS 59
1081 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1084 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1085 GET_REGL(env
->gregs
[n
+ 16]);
1087 GET_REGL(env
->gregs
[n
]);
1089 } else if (n
< 16) {
1090 GET_REGL(env
->gregs
[n
- 8]);
1091 } else if (n
>= 25 && n
< 41) {
1092 GET_REGL(env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
1093 } else if (n
>= 43 && n
< 51) {
1094 GET_REGL(env
->gregs
[n
- 43]);
1095 } else if (n
>= 51 && n
< 59) {
1096 GET_REGL(env
->gregs
[n
- (51 - 16)]);
1099 case 16: GET_REGL(env
->pc
);
1100 case 17: GET_REGL(env
->pr
);
1101 case 18: GET_REGL(env
->gbr
);
1102 case 19: GET_REGL(env
->vbr
);
1103 case 20: GET_REGL(env
->mach
);
1104 case 21: GET_REGL(env
->macl
);
1105 case 22: GET_REGL(env
->sr
);
1106 case 23: GET_REGL(env
->fpul
);
1107 case 24: GET_REGL(env
->fpscr
);
1108 case 41: GET_REGL(env
->ssr
);
1109 case 42: GET_REGL(env
->spc
);
1115 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1119 tmp
= ldl_p(mem_buf
);
1122 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1123 env
->gregs
[n
+ 16] = tmp
;
1125 env
->gregs
[n
] = tmp
;
1128 } else if (n
< 16) {
1129 env
->gregs
[n
- 8] = tmp
;
1131 } else if (n
>= 25 && n
< 41) {
1132 env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)] = tmp
;
1133 } else if (n
>= 43 && n
< 51) {
1134 env
->gregs
[n
- 43] = tmp
;
1136 } else if (n
>= 51 && n
< 59) {
1137 env
->gregs
[n
- (51 - 16)] = tmp
;
1141 case 16: env
->pc
= tmp
;
1142 case 17: env
->pr
= tmp
;
1143 case 18: env
->gbr
= tmp
;
1144 case 19: env
->vbr
= tmp
;
1145 case 20: env
->mach
= tmp
;
1146 case 21: env
->macl
= tmp
;
1147 case 22: env
->sr
= tmp
;
1148 case 23: env
->fpul
= tmp
;
1149 case 24: env
->fpscr
= tmp
;
1150 case 41: env
->ssr
= tmp
;
1151 case 42: env
->spc
= tmp
;
1157 #elif defined (TARGET_CRIS)
1159 #define NUM_CORE_REGS 49
1161 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1165 srs
= env
->pregs
[PR_SRS
];
1167 GET_REG32(env
->regs
[n
]);
1170 if (n
>= 21 && n
< 32) {
1171 GET_REG32(env
->pregs
[n
- 16]);
1173 if (n
>= 33 && n
< 49) {
1174 GET_REG32(env
->sregs
[srs
][n
- 33]);
1177 case 16: GET_REG8(env
->pregs
[0]);
1178 case 17: GET_REG8(env
->pregs
[1]);
1179 case 18: GET_REG32(env
->pregs
[2]);
1180 case 19: GET_REG8(srs
);
1181 case 20: GET_REG16(env
->pregs
[4]);
1182 case 32: GET_REG32(env
->pc
);
1188 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1195 tmp
= ldl_p(mem_buf
);
1201 if (n
>= 21 && n
< 32) {
1202 env
->pregs
[n
- 16] = tmp
;
1205 /* FIXME: Should support function regs be writable? */
1209 case 18: env
->pregs
[PR_PID
] = tmp
; break;
1212 case 32: env
->pc
= tmp
; break;
1217 #elif defined (TARGET_ALPHA)
1219 #define NUM_CORE_REGS 65
1221 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1224 GET_REGL(env
->ir
[n
]);
1232 val
=*((uint64_t *)&env
->fir
[n
-32]);
1236 GET_REGL(env
->fpcr
);
1248 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1251 tmp
= ldtul_p(mem_buf
);
1257 if (n
> 31 && n
< 63) {
1258 env
->fir
[n
- 32] = ldfl_p(mem_buf
);
1269 #define NUM_CORE_REGS 0
1271 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1276 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1283 static int num_g_regs
= NUM_CORE_REGS
;
1286 /* Encode data using the encoding for 'x' packets. */
1287 static int memtox(char *buf
, const char *mem
, int len
)
1295 case '#': case '$': case '*': case '}':
1307 static const char *get_feature_xml(const char *p
, const char **newp
)
1309 extern const char *const xml_builtin
[][2];
1313 static char target_xml
[1024];
1316 while (p
[len
] && p
[len
] != ':')
1321 if (strncmp(p
, "target.xml", len
) == 0) {
1322 /* Generate the XML description for this CPU. */
1323 if (!target_xml
[0]) {
1324 GDBRegisterState
*r
;
1326 snprintf(target_xml
, sizeof(target_xml
),
1327 "<?xml version=\"1.0\"?>"
1328 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1330 "<xi:include href=\"%s\"/>",
1333 for (r
= first_cpu
->gdb_regs
; r
; r
= r
->next
) {
1334 strcat(target_xml
, "<xi:include href=\"");
1335 strcat(target_xml
, r
->xml
);
1336 strcat(target_xml
, "\"/>");
1338 strcat(target_xml
, "</target>");
1342 for (i
= 0; ; i
++) {
1343 name
= xml_builtin
[i
][0];
1344 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
1347 return name
? xml_builtin
[i
][1] : NULL
;
1351 static int gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1353 GDBRegisterState
*r
;
1355 if (reg
< NUM_CORE_REGS
)
1356 return cpu_gdb_read_register(env
, mem_buf
, reg
);
1358 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1359 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1360 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
1366 static int gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1368 GDBRegisterState
*r
;
1370 if (reg
< NUM_CORE_REGS
)
1371 return cpu_gdb_write_register(env
, mem_buf
, reg
);
1373 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1374 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1375 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
1381 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
1382 specifies the first register number and these registers are included in
1383 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1384 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1387 void gdb_register_coprocessor(CPUState
* env
,
1388 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
1389 int num_regs
, const char *xml
, int g_pos
)
1391 GDBRegisterState
*s
;
1392 GDBRegisterState
**p
;
1393 static int last_reg
= NUM_CORE_REGS
;
1395 s
= (GDBRegisterState
*)qemu_mallocz(sizeof(GDBRegisterState
));
1396 s
->base_reg
= last_reg
;
1397 s
->num_regs
= num_regs
;
1398 s
->get_reg
= get_reg
;
1399 s
->set_reg
= set_reg
;
1403 /* Check for duplicates. */
1404 if (strcmp((*p
)->xml
, xml
) == 0)
1408 /* Add to end of list. */
1409 last_reg
+= num_regs
;
1412 if (g_pos
!= s
->base_reg
) {
1413 fprintf(stderr
, "Error: Bad gdb register numbering for '%s'\n"
1414 "Expected %d got %d\n", xml
, g_pos
, s
->base_reg
);
1416 num_g_regs
= last_reg
;
1421 /* GDB breakpoint/watchpoint types */
1422 #define GDB_BREAKPOINT_SW 0
1423 #define GDB_BREAKPOINT_HW 1
1424 #define GDB_WATCHPOINT_WRITE 2
1425 #define GDB_WATCHPOINT_READ 3
1426 #define GDB_WATCHPOINT_ACCESS 4
1428 #ifndef CONFIG_USER_ONLY
1429 static const int xlat_gdb_type
[] = {
1430 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
1431 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
1432 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
1436 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
1442 case GDB_BREAKPOINT_SW
:
1443 case GDB_BREAKPOINT_HW
:
1444 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1445 err
= cpu_breakpoint_insert(env
, addr
, BP_GDB
, NULL
);
1450 #ifndef CONFIG_USER_ONLY
1451 case GDB_WATCHPOINT_WRITE
:
1452 case GDB_WATCHPOINT_READ
:
1453 case GDB_WATCHPOINT_ACCESS
:
1454 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1455 err
= cpu_watchpoint_insert(env
, addr
, len
, xlat_gdb_type
[type
],
1467 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1473 case GDB_BREAKPOINT_SW
:
1474 case GDB_BREAKPOINT_HW
:
1475 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1476 err
= cpu_breakpoint_remove(env
, addr
, BP_GDB
);
1481 #ifndef CONFIG_USER_ONLY
1482 case GDB_WATCHPOINT_WRITE
:
1483 case GDB_WATCHPOINT_READ
:
1484 case GDB_WATCHPOINT_ACCESS
:
1485 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1486 err
= cpu_watchpoint_remove(env
, addr
, len
, xlat_gdb_type
[type
]);
1497 static void gdb_breakpoint_remove_all(void)
1501 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1502 cpu_breakpoint_remove_all(env
, BP_GDB
);
1503 #ifndef CONFIG_USER_ONLY
1504 cpu_watchpoint_remove_all(env
, BP_GDB
);
1509 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1513 int ch
, reg_size
, type
, res
, thread
;
1514 char buf
[MAX_PACKET_LENGTH
];
1515 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1517 target_ulong addr
, len
;
1520 printf("command='%s'\n", line_buf
);
1526 /* TODO: Make this return the correct value for user-mode. */
1527 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
1528 s
->c_cpu
->cpu_index
+1);
1530 /* Remove all the breakpoints when this query is issued,
1531 * because gdb is doing and initial connect and the state
1532 * should be cleaned up.
1534 gdb_breakpoint_remove_all();
1538 addr
= strtoull(p
, (char **)&p
, 16);
1539 #if defined(TARGET_I386)
1540 s
->c_cpu
->eip
= addr
;
1541 #elif defined (TARGET_PPC)
1542 s
->c_cpu
->nip
= addr
;
1543 #elif defined (TARGET_SPARC)
1544 s
->c_cpu
->pc
= addr
;
1545 s
->c_cpu
->npc
= addr
+ 4;
1546 #elif defined (TARGET_ARM)
1547 s
->c_cpu
->regs
[15] = addr
;
1548 #elif defined (TARGET_SH4)
1549 s
->c_cpu
->pc
= addr
;
1550 #elif defined (TARGET_MIPS)
1551 s
->c_cpu
->active_tc
.PC
= addr
;
1552 #elif defined (TARGET_CRIS)
1553 s
->c_cpu
->pc
= addr
;
1554 #elif defined (TARGET_ALPHA)
1555 s
->c_cpu
->pc
= addr
;
1562 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1563 if (s
->signal
== -1)
1568 /* Kill the target */
1569 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1573 gdb_breakpoint_remove_all();
1575 put_packet(s
, "OK");
1579 addr
= strtoull(p
, (char **)&p
, 16);
1580 #if defined(TARGET_I386)
1581 s
->c_cpu
->eip
= addr
;
1582 #elif defined (TARGET_PPC)
1583 s
->c_cpu
->nip
= addr
;
1584 #elif defined (TARGET_SPARC)
1585 s
->c_cpu
->pc
= addr
;
1586 s
->c_cpu
->npc
= addr
+ 4;
1587 #elif defined (TARGET_ARM)
1588 s
->c_cpu
->regs
[15] = addr
;
1589 #elif defined (TARGET_SH4)
1590 s
->c_cpu
->pc
= addr
;
1591 #elif defined (TARGET_MIPS)
1592 s
->c_cpu
->active_tc
.PC
= addr
;
1593 #elif defined (TARGET_CRIS)
1594 s
->c_cpu
->pc
= addr
;
1595 #elif defined (TARGET_ALPHA)
1596 s
->c_cpu
->pc
= addr
;
1599 cpu_single_step(s
->c_cpu
, sstep_flags
);
1607 ret
= strtoull(p
, (char **)&p
, 16);
1610 err
= strtoull(p
, (char **)&p
, 16);
1617 if (gdb_current_syscall_cb
)
1618 gdb_current_syscall_cb(s
->c_cpu
, ret
, err
);
1620 put_packet(s
, "T02");
1628 for (addr
= 0; addr
< num_g_regs
; addr
++) {
1629 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1632 memtohex(buf
, mem_buf
, len
);
1636 registers
= mem_buf
;
1637 len
= strlen(p
) / 2;
1638 hextomem((uint8_t *)registers
, p
, len
);
1639 for (addr
= 0; addr
< num_g_regs
&& len
> 0; addr
++) {
1640 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1642 registers
+= reg_size
;
1644 put_packet(s
, "OK");
1647 addr
= strtoull(p
, (char **)&p
, 16);
1650 len
= strtoull(p
, NULL
, 16);
1651 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 0) != 0) {
1652 put_packet (s
, "E14");
1654 memtohex(buf
, mem_buf
, len
);
1659 addr
= strtoull(p
, (char **)&p
, 16);
1662 len
= strtoull(p
, (char **)&p
, 16);
1665 hextomem(mem_buf
, p
, len
);
1666 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 1) != 0)
1667 put_packet(s
, "E14");
1669 put_packet(s
, "OK");
1672 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1673 This works, but can be very slow. Anything new enough to
1674 understand XML also knows how to use this properly. */
1676 goto unknown_command
;
1677 addr
= strtoull(p
, (char **)&p
, 16);
1678 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1680 memtohex(buf
, mem_buf
, reg_size
);
1683 put_packet(s
, "E14");
1688 goto unknown_command
;
1689 addr
= strtoull(p
, (char **)&p
, 16);
1692 reg_size
= strlen(p
) / 2;
1693 hextomem(mem_buf
, p
, reg_size
);
1694 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1695 put_packet(s
, "OK");
1699 type
= strtoul(p
, (char **)&p
, 16);
1702 addr
= strtoull(p
, (char **)&p
, 16);
1705 len
= strtoull(p
, (char **)&p
, 16);
1707 res
= gdb_breakpoint_insert(addr
, len
, type
);
1709 res
= gdb_breakpoint_remove(addr
, len
, type
);
1711 put_packet(s
, "OK");
1712 else if (res
== -ENOSYS
)
1715 put_packet(s
, "E22");
1719 thread
= strtoull(p
, (char **)&p
, 16);
1720 if (thread
== -1 || thread
== 0) {
1721 put_packet(s
, "OK");
1724 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1725 if (env
->cpu_index
+ 1 == thread
)
1728 put_packet(s
, "E22");
1734 put_packet(s
, "OK");
1738 put_packet(s
, "OK");
1741 put_packet(s
, "E22");
1746 thread
= strtoull(p
, (char **)&p
, 16);
1747 #ifndef CONFIG_USER_ONLY
1748 if (thread
> 0 && thread
< smp_cpus
+ 1)
1752 put_packet(s
, "OK");
1754 put_packet(s
, "E22");
1758 /* parse any 'q' packets here */
1759 if (!strcmp(p
,"qemu.sstepbits")) {
1760 /* Query Breakpoint bit definitions */
1761 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1767 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1768 /* Display or change the sstep_flags */
1771 /* Display current setting */
1772 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1777 type
= strtoul(p
, (char **)&p
, 16);
1779 put_packet(s
, "OK");
1781 } else if (strcmp(p
,"C") == 0) {
1782 /* "Current thread" remains vague in the spec, so always return
1783 * the first CPU (gdb returns the first thread). */
1784 put_packet(s
, "QC1");
1786 } else if (strcmp(p
,"fThreadInfo") == 0) {
1787 s
->query_cpu
= first_cpu
;
1788 goto report_cpuinfo
;
1789 } else if (strcmp(p
,"sThreadInfo") == 0) {
1792 snprintf(buf
, sizeof(buf
), "m%x", s
->query_cpu
->cpu_index
+1);
1794 s
->query_cpu
= s
->query_cpu
->next_cpu
;
1798 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1799 thread
= strtoull(p
+16, (char **)&p
, 16);
1800 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1801 if (env
->cpu_index
+ 1 == thread
) {
1802 len
= snprintf((char *)mem_buf
, sizeof(mem_buf
),
1803 "CPU#%d [%s]", env
->cpu_index
,
1804 env
->halted
? "halted " : "running");
1805 memtohex(buf
, mem_buf
, len
);
1811 #ifdef CONFIG_USER_ONLY
1812 else if (strncmp(p
, "Offsets", 7) == 0) {
1813 TaskState
*ts
= s
->c_cpu
->opaque
;
1815 snprintf(buf
, sizeof(buf
),
1816 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1817 ";Bss=" TARGET_ABI_FMT_lx
,
1818 ts
->info
->code_offset
,
1819 ts
->info
->data_offset
,
1820 ts
->info
->data_offset
);
1824 #else /* !CONFIG_USER_ONLY */
1825 else if (strncmp(p
, "Rcmd,", 5) == 0) {
1826 int len
= strlen(p
+ 5);
1828 if ((len
% 2) != 0) {
1829 put_packet(s
, "E01");
1832 hextomem(mem_buf
, p
+ 5, len
);
1835 qemu_chr_read(s
->mon_chr
, mem_buf
, len
);
1836 put_packet(s
, "OK");
1839 #endif /* !CONFIG_USER_ONLY */
1840 if (strncmp(p
, "Supported", 9) == 0) {
1841 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1843 strcat(buf
, ";qXfer:features:read+");
1849 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1851 target_ulong total_len
;
1855 xml
= get_feature_xml(p
, &p
);
1857 snprintf(buf
, sizeof(buf
), "E00");
1864 addr
= strtoul(p
, (char **)&p
, 16);
1867 len
= strtoul(p
, (char **)&p
, 16);
1869 total_len
= strlen(xml
);
1870 if (addr
> total_len
) {
1871 snprintf(buf
, sizeof(buf
), "E00");
1875 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
1876 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1877 if (len
< total_len
- addr
) {
1879 len
= memtox(buf
+ 1, xml
+ addr
, len
);
1882 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
1884 put_packet_binary(s
, buf
, len
+ 1);
1888 /* Unrecognised 'q' command. */
1889 goto unknown_command
;
1893 /* put empty packet */
1901 void gdb_set_stop_cpu(CPUState
*env
)
1903 gdbserver_state
->c_cpu
= env
;
1904 gdbserver_state
->g_cpu
= env
;
1907 #ifndef CONFIG_USER_ONLY
1908 static void gdb_vm_state_change(void *opaque
, int running
, int reason
)
1910 GDBState
*s
= gdbserver_state
;
1911 CPUState
*env
= s
->c_cpu
;
1916 if (running
|| (reason
!= EXCP_DEBUG
&& reason
!= EXCP_INTERRUPT
) ||
1917 s
->state
== RS_SYSCALL
)
1920 /* disable single step if it was enable */
1921 cpu_single_step(env
, 0);
1923 if (reason
== EXCP_DEBUG
) {
1924 if (env
->watchpoint_hit
) {
1925 switch (env
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
1936 snprintf(buf
, sizeof(buf
),
1937 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
1938 GDB_SIGNAL_TRAP
, env
->cpu_index
+1, type
,
1939 env
->watchpoint_hit
->vaddr
);
1941 env
->watchpoint_hit
= NULL
;
1945 ret
= GDB_SIGNAL_TRAP
;
1947 ret
= GDB_SIGNAL_INT
;
1949 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, env
->cpu_index
+1);
1954 /* Send a gdb syscall request.
1955 This accepts limited printf-style format specifiers, specifically:
1956 %x - target_ulong argument printed in hex.
1957 %lx - 64-bit argument printed in hex.
1958 %s - string pointer (target_ulong) and length (int) pair. */
1959 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
1968 s
= gdbserver_state
;
1971 gdb_current_syscall_cb
= cb
;
1972 s
->state
= RS_SYSCALL
;
1973 #ifndef CONFIG_USER_ONLY
1974 vm_stop(EXCP_DEBUG
);
1985 addr
= va_arg(va
, target_ulong
);
1986 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
, addr
);
1989 if (*(fmt
++) != 'x')
1991 i64
= va_arg(va
, uint64_t);
1992 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, "%" PRIx64
, i64
);
1995 addr
= va_arg(va
, target_ulong
);
1996 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
"/%x",
1997 addr
, va_arg(va
, int));
2001 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
2012 #ifdef CONFIG_USER_ONLY
2013 gdb_handlesig(s
->c_cpu
, 0);
2019 static void gdb_read_byte(GDBState
*s
, int ch
)
2024 #ifndef CONFIG_USER_ONLY
2025 if (s
->last_packet_len
) {
2026 /* Waiting for a response to the last packet. If we see the start
2027 of a new command then abandon the previous response. */
2030 printf("Got NACK, retransmitting\n");
2032 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2036 printf("Got ACK\n");
2038 printf("Got '%c' when expecting ACK/NACK\n", ch
);
2040 if (ch
== '+' || ch
== '$')
2041 s
->last_packet_len
= 0;
2046 /* when the CPU is running, we cannot do anything except stop
2047 it when receiving a char */
2048 vm_stop(EXCP_INTERRUPT
);
2055 s
->line_buf_index
= 0;
2056 s
->state
= RS_GETLINE
;
2061 s
->state
= RS_CHKSUM1
;
2062 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2065 s
->line_buf
[s
->line_buf_index
++] = ch
;
2069 s
->line_buf
[s
->line_buf_index
] = '\0';
2070 s
->line_csum
= fromhex(ch
) << 4;
2071 s
->state
= RS_CHKSUM2
;
2074 s
->line_csum
|= fromhex(ch
);
2076 for(i
= 0; i
< s
->line_buf_index
; i
++) {
2077 csum
+= s
->line_buf
[i
];
2079 if (s
->line_csum
!= (csum
& 0xff)) {
2081 put_buffer(s
, &reply
, 1);
2085 put_buffer(s
, &reply
, 1);
2086 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2095 #ifdef CONFIG_USER_ONLY
2101 s
= gdbserver_state
;
2103 if (gdbserver_fd
< 0 || s
->fd
< 0)
2110 gdb_handlesig (CPUState
*env
, int sig
)
2116 s
= gdbserver_state
;
2117 if (gdbserver_fd
< 0 || s
->fd
< 0)
2120 /* disable single step if it was enabled */
2121 cpu_single_step(env
, 0);
2126 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb (sig
));
2129 /* put_packet() might have detected that the peer terminated the
2136 s
->running_state
= 0;
2137 while (s
->running_state
== 0) {
2138 n
= read (s
->fd
, buf
, 256);
2143 for (i
= 0; i
< n
; i
++)
2144 gdb_read_byte (s
, buf
[i
]);
2146 else if (n
== 0 || errno
!= EAGAIN
)
2148 /* XXX: Connection closed. Should probably wait for annother
2149 connection before continuing. */
2158 /* Tell the remote gdb that the process has exited. */
2159 void gdb_exit(CPUState
*env
, int code
)
2164 s
= gdbserver_state
;
2165 if (gdbserver_fd
< 0 || s
->fd
< 0)
2168 snprintf(buf
, sizeof(buf
), "W%02x", code
);
2172 /* Tell the remote gdb that the process has exited due to SIG. */
2173 void gdb_signalled(CPUState
*env
, int sig
)
2178 s
= gdbserver_state
;
2179 if (gdbserver_fd
< 0 || s
->fd
< 0)
2182 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb (sig
));
2186 static void gdb_accept(void)
2189 struct sockaddr_in sockaddr
;
2194 len
= sizeof(sockaddr
);
2195 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2196 if (fd
< 0 && errno
!= EINTR
) {
2199 } else if (fd
>= 0) {
2204 /* set short latency */
2206 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2208 s
= qemu_mallocz(sizeof(GDBState
));
2210 memset (s
, 0, sizeof (GDBState
));
2211 s
->c_cpu
= first_cpu
;
2212 s
->g_cpu
= first_cpu
;
2216 gdbserver_state
= s
;
2218 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2221 static int gdbserver_open(int port
)
2223 struct sockaddr_in sockaddr
;
2226 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2232 /* allow fast reuse */
2234 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
2236 sockaddr
.sin_family
= AF_INET
;
2237 sockaddr
.sin_port
= htons(port
);
2238 sockaddr
.sin_addr
.s_addr
= 0;
2239 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2244 ret
= listen(fd
, 0);
2252 int gdbserver_start(int port
)
2254 gdbserver_fd
= gdbserver_open(port
);
2255 if (gdbserver_fd
< 0)
2257 /* accept connections */
2262 /* Disable gdb stub for child processes. */
2263 void gdbserver_fork(CPUState
*env
)
2265 GDBState
*s
= gdbserver_state
;
2266 if (gdbserver_fd
< 0 || s
->fd
< 0)
2270 cpu_breakpoint_remove_all(env
, BP_GDB
);
2271 cpu_watchpoint_remove_all(env
, BP_GDB
);
2274 static int gdb_chr_can_receive(void *opaque
)
2276 /* We can handle an arbitrarily large amount of data.
2277 Pick the maximum packet size, which is as good as anything. */
2278 return MAX_PACKET_LENGTH
;
2281 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2285 for (i
= 0; i
< size
; i
++) {
2286 gdb_read_byte(gdbserver_state
, buf
[i
]);
2290 static void gdb_chr_event(void *opaque
, int event
)
2293 case CHR_EVENT_RESET
:
2294 vm_stop(EXCP_INTERRUPT
);
2302 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
2304 char buf
[MAX_PACKET_LENGTH
];
2307 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
2308 len
= (MAX_PACKET_LENGTH
/2) - 1;
2309 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
2313 static int gdb_monitor_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2315 const char *p
= (const char *)buf
;
2318 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
2320 if (len
<= max_sz
) {
2321 gdb_monitor_output(gdbserver_state
, p
, len
);
2324 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
2331 int gdbserver_start(const char *port
)
2334 char gdbstub_port_name
[128];
2337 CharDriverState
*chr
;
2339 if (!port
|| !*port
)
2342 port_num
= strtol(port
, &p
, 10);
2344 /* A numeric value is interpreted as a port number. */
2345 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
2346 "tcp::%d,nowait,nodelay,server", port_num
);
2347 port
= gdbstub_port_name
;
2350 chr
= qemu_chr_open("gdb", port
, NULL
);
2354 s
= qemu_mallocz(sizeof(GDBState
));
2355 s
->c_cpu
= first_cpu
;
2356 s
->g_cpu
= first_cpu
;
2358 gdbserver_state
= s
;
2359 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2360 gdb_chr_event
, NULL
);
2361 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
2363 /* Initialize a monitor terminal for gdb */
2364 s
->mon_chr
= qemu_mallocz(sizeof(*s
->mon_chr
));
2365 s
->mon_chr
->chr_write
= gdb_monitor_write
;
2366 monitor_init(s
->mon_chr
, 0);