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"
49 GDB_SIGNAL_UNKNOWN
= 143
52 #ifdef CONFIG_USER_ONLY
54 /* Map target signal numbers to GDB protocol signal numbers and vice
55 * versa. For user emulation's currently supported systems, we can
56 * assume most signals are defined.
59 static int gdb_signal_table
[] = {
219 /* In system mode we only need SIGINT and SIGTRAP; other signals
220 are not yet supported. */
227 static int gdb_signal_table
[] = {
237 #ifdef CONFIG_USER_ONLY
238 static int target_signal_to_gdb (int sig
)
241 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
242 if (gdb_signal_table
[i
] == sig
)
244 return GDB_SIGNAL_UNKNOWN
;
248 static int gdb_signal_to_target (int sig
)
250 if (sig
< ARRAY_SIZE (gdb_signal_table
))
251 return gdb_signal_table
[sig
];
258 typedef struct GDBRegisterState
{
264 struct GDBRegisterState
*next
;
274 typedef struct GDBState
{
275 CPUState
*c_cpu
; /* current CPU for step/continue ops */
276 CPUState
*g_cpu
; /* current CPU for other ops */
277 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
278 enum RSState state
; /* parsing state */
279 char line_buf
[MAX_PACKET_LENGTH
];
282 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
285 #ifdef CONFIG_USER_ONLY
289 CharDriverState
*chr
;
290 CharDriverState
*mon_chr
;
294 /* By default use no IRQs and no timers while single stepping so as to
295 * make single stepping like an ICE HW step.
297 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
299 static GDBState
*gdbserver_state
;
301 /* This is an ugly hack to cope with both new and old gdb.
302 If gdb sends qXfer:features:read then assume we're talking to a newish
303 gdb that understands target descriptions. */
304 static int gdb_has_xml
;
306 #ifdef CONFIG_USER_ONLY
307 /* XXX: This is not thread safe. Do we care? */
308 static int gdbserver_fd
= -1;
310 static int get_char(GDBState
*s
)
316 ret
= recv(s
->fd
, &ch
, 1, 0);
318 if (errno
== ECONNRESET
)
320 if (errno
!= EINTR
&& errno
!= EAGAIN
)
322 } else if (ret
== 0) {
334 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
342 /* If gdb is connected when the first semihosting syscall occurs then use
343 remote gdb syscalls. Otherwise use native file IO. */
344 int use_gdb_syscalls(void)
346 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
347 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
350 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
353 /* Resume execution. */
354 static inline void gdb_continue(GDBState
*s
)
356 #ifdef CONFIG_USER_ONLY
357 s
->running_state
= 1;
363 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
365 #ifdef CONFIG_USER_ONLY
369 ret
= send(s
->fd
, buf
, len
, 0);
371 if (errno
!= EINTR
&& errno
!= EAGAIN
)
379 qemu_chr_write(s
->chr
, buf
, len
);
383 static inline int fromhex(int v
)
385 if (v
>= '0' && v
<= '9')
387 else if (v
>= 'A' && v
<= 'F')
389 else if (v
>= 'a' && v
<= 'f')
395 static inline int tohex(int v
)
403 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
408 for(i
= 0; i
< len
; i
++) {
410 *q
++ = tohex(c
>> 4);
411 *q
++ = tohex(c
& 0xf);
416 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
420 for(i
= 0; i
< len
; i
++) {
421 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
426 /* return -1 if error, 0 if OK */
427 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
)
438 for(i
= 0; i
< len
; i
++) {
442 *(p
++) = tohex((csum
>> 4) & 0xf);
443 *(p
++) = tohex((csum
) & 0xf);
445 s
->last_packet_len
= p
- s
->last_packet
;
446 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
448 #ifdef CONFIG_USER_ONLY
461 /* return -1 if error, 0 if OK */
462 static int put_packet(GDBState
*s
, const char *buf
)
465 printf("reply='%s'\n", buf
);
468 return put_packet_binary(s
, buf
, strlen(buf
));
471 /* The GDB remote protocol transfers values in target byte order. This means
472 we can use the raw memory access routines to access the value buffer.
473 Conveniently, these also handle the case where the buffer is mis-aligned.
475 #define GET_REG8(val) do { \
476 stb_p(mem_buf, val); \
479 #define GET_REG16(val) do { \
480 stw_p(mem_buf, val); \
483 #define GET_REG32(val) do { \
484 stl_p(mem_buf, val); \
487 #define GET_REG64(val) do { \
488 stq_p(mem_buf, val); \
492 #if TARGET_LONG_BITS == 64
493 #define GET_REGL(val) GET_REG64(val)
494 #define ldtul_p(addr) ldq_p(addr)
496 #define GET_REGL(val) GET_REG32(val)
497 #define ldtul_p(addr) ldl_p(addr)
500 #if defined(TARGET_I386)
503 static const int gpr_map
[16] = {
504 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
505 8, 9, 10, 11, 12, 13, 14, 15
508 static const int gpr_map
[8] = {0, 1, 2, 3, 4, 5, 6, 7};
511 #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
513 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
515 if (n
< CPU_NB_REGS
) {
516 GET_REGL(env
->regs
[gpr_map
[n
]]);
517 } else if (n
>= CPU_NB_REGS
+ 8 && n
< CPU_NB_REGS
+ 16) {
518 /* FIXME: byteswap float values. */
519 #ifdef USE_X86LDOUBLE
520 memcpy(mem_buf
, &env
->fpregs
[n
- (CPU_NB_REGS
+ 8)], 10);
522 memset(mem_buf
, 0, 10);
525 } else if (n
>= CPU_NB_REGS
+ 24) {
526 n
-= CPU_NB_REGS
+ 24;
527 if (n
< CPU_NB_REGS
) {
528 stq_p(mem_buf
, env
->xmm_regs
[n
].XMM_Q(0));
529 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].XMM_Q(1));
531 } else if (n
== CPU_NB_REGS
) {
532 GET_REG32(env
->mxcsr
);
537 case 0: GET_REGL(env
->eip
);
538 case 1: GET_REG32(env
->eflags
);
539 case 2: GET_REG32(env
->segs
[R_CS
].selector
);
540 case 3: GET_REG32(env
->segs
[R_SS
].selector
);
541 case 4: GET_REG32(env
->segs
[R_DS
].selector
);
542 case 5: GET_REG32(env
->segs
[R_ES
].selector
);
543 case 6: GET_REG32(env
->segs
[R_FS
].selector
);
544 case 7: GET_REG32(env
->segs
[R_GS
].selector
);
545 /* 8...15 x87 regs. */
546 case 16: GET_REG32(env
->fpuc
);
547 case 17: GET_REG32((env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11);
548 case 18: GET_REG32(0); /* ftag */
549 case 19: GET_REG32(0); /* fiseg */
550 case 20: GET_REG32(0); /* fioff */
551 case 21: GET_REG32(0); /* foseg */
552 case 22: GET_REG32(0); /* fooff */
553 case 23: GET_REG32(0); /* fop */
560 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int i
)
564 if (i
< CPU_NB_REGS
) {
565 env
->regs
[gpr_map
[i
]] = ldtul_p(mem_buf
);
566 return sizeof(target_ulong
);
567 } else if (i
>= CPU_NB_REGS
+ 8 && i
< CPU_NB_REGS
+ 16) {
568 i
-= CPU_NB_REGS
+ 8;
569 #ifdef USE_X86LDOUBLE
570 memcpy(&env
->fpregs
[i
], mem_buf
, 10);
573 } else if (i
>= CPU_NB_REGS
+ 24) {
574 i
-= CPU_NB_REGS
+ 24;
575 if (i
< CPU_NB_REGS
) {
576 env
->xmm_regs
[i
].XMM_Q(0) = ldq_p(mem_buf
);
577 env
->xmm_regs
[i
].XMM_Q(1) = ldq_p(mem_buf
+ 8);
579 } else if (i
== CPU_NB_REGS
) {
580 env
->mxcsr
= ldl_p(mem_buf
);
586 case 0: env
->eip
= ldtul_p(mem_buf
); return sizeof(target_ulong
);
587 case 1: env
->eflags
= ldl_p(mem_buf
); return 4;
588 #if defined(CONFIG_USER_ONLY)
589 #define LOAD_SEG(index, sreg)\
590 tmp = ldl_p(mem_buf);\
591 if (tmp != env->segs[sreg].selector)\
592 cpu_x86_load_seg(env, sreg, tmp);
594 /* FIXME: Honor segment registers. Needs to avoid raising an exception
595 when the selector is invalid. */
596 #define LOAD_SEG(index, sreg) do {} while(0)
598 case 2: LOAD_SEG(10, R_CS
); return 4;
599 case 3: LOAD_SEG(11, R_SS
); return 4;
600 case 4: LOAD_SEG(12, R_DS
); return 4;
601 case 5: LOAD_SEG(13, R_ES
); return 4;
602 case 6: LOAD_SEG(14, R_FS
); return 4;
603 case 7: LOAD_SEG(15, R_GS
); return 4;
604 /* 8...15 x87 regs. */
605 case 16: env
->fpuc
= ldl_p(mem_buf
); return 4;
607 tmp
= ldl_p(mem_buf
);
608 env
->fpstt
= (tmp
>> 11) & 7;
609 env
->fpus
= tmp
& ~0x3800;
611 case 18: /* ftag */ return 4;
612 case 19: /* fiseg */ return 4;
613 case 20: /* fioff */ return 4;
614 case 21: /* foseg */ return 4;
615 case 22: /* fooff */ return 4;
616 case 23: /* fop */ return 4;
620 /* Unrecognised register. */
624 #elif defined (TARGET_PPC)
626 /* Old gdb always expects FP registers. Newer (xml-aware) gdb only
627 expects whatever the target description contains. Due to a
628 historical mishap the FP registers appear in between core integer
629 regs and PC, MSR, CR, and so forth. We hack round this by giving the
630 FP regs zero size when talking to a newer gdb. */
631 #define NUM_CORE_REGS 71
632 #if defined (TARGET_PPC64)
633 #define GDB_CORE_XML "power64-core.xml"
635 #define GDB_CORE_XML "power-core.xml"
638 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
642 GET_REGL(env
->gpr
[n
]);
647 stfq_p(mem_buf
, env
->fpr
[n
-32]);
651 case 64: GET_REGL(env
->nip
);
652 case 65: GET_REGL(env
->msr
);
657 for (i
= 0; i
< 8; i
++)
658 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
661 case 67: GET_REGL(env
->lr
);
662 case 68: GET_REGL(env
->ctr
);
663 case 69: GET_REGL(env
->xer
);
668 GET_REG32(0); /* fpscr */
675 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
679 env
->gpr
[n
] = ldtul_p(mem_buf
);
680 return sizeof(target_ulong
);
685 env
->fpr
[n
-32] = ldfq_p(mem_buf
);
690 env
->nip
= ldtul_p(mem_buf
);
691 return sizeof(target_ulong
);
693 ppc_store_msr(env
, ldtul_p(mem_buf
));
694 return sizeof(target_ulong
);
697 uint32_t cr
= ldl_p(mem_buf
);
699 for (i
= 0; i
< 8; i
++)
700 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
704 env
->lr
= ldtul_p(mem_buf
);
705 return sizeof(target_ulong
);
707 env
->ctr
= ldtul_p(mem_buf
);
708 return sizeof(target_ulong
);
710 env
->xer
= ldtul_p(mem_buf
);
711 return sizeof(target_ulong
);
722 #elif defined (TARGET_SPARC)
724 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
725 #define NUM_CORE_REGS 86
727 #define NUM_CORE_REGS 72
731 #define GET_REGA(val) GET_REG32(val)
733 #define GET_REGA(val) GET_REGL(val)
736 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
740 GET_REGA(env
->gregs
[n
]);
743 /* register window */
744 GET_REGA(env
->regwptr
[n
- 8]);
746 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
749 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
751 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
753 case 64: GET_REGA(env
->y
);
754 case 65: GET_REGA(GET_PSR(env
));
755 case 66: GET_REGA(env
->wim
);
756 case 67: GET_REGA(env
->tbr
);
757 case 68: GET_REGA(env
->pc
);
758 case 69: GET_REGA(env
->npc
);
759 case 70: GET_REGA(env
->fsr
);
760 case 71: GET_REGA(0); /* csr */
761 default: GET_REGA(0);
766 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
769 /* f32-f62 (double width, even numbers only) */
772 val
= (uint64_t)*((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) << 32;
773 val
|= *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]);
777 case 80: GET_REGL(env
->pc
);
778 case 81: GET_REGL(env
->npc
);
779 case 82: GET_REGL(((uint64_t)GET_CCR(env
) << 32) |
780 ((env
->asi
& 0xff) << 24) |
781 ((env
->pstate
& 0xfff) << 8) |
783 case 83: GET_REGL(env
->fsr
);
784 case 84: GET_REGL(env
->fprs
);
785 case 85: GET_REGL(env
->y
);
791 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
793 #if defined(TARGET_ABI32)
796 tmp
= ldl_p(mem_buf
);
800 tmp
= ldtul_p(mem_buf
);
807 /* register window */
808 env
->regwptr
[n
- 8] = tmp
;
810 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
813 *((uint32_t *)&env
->fpr
[n
- 32]) = tmp
;
815 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
817 case 64: env
->y
= tmp
; break;
818 case 65: PUT_PSR(env
, tmp
); break;
819 case 66: env
->wim
= tmp
; break;
820 case 67: env
->tbr
= tmp
; break;
821 case 68: env
->pc
= tmp
; break;
822 case 69: env
->npc
= tmp
; break;
823 case 70: env
->fsr
= tmp
; break;
831 env
->fpr
[n
] = ldfl_p(mem_buf
);
834 /* f32-f62 (double width, even numbers only) */
835 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) = tmp
>> 32;
836 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]) = tmp
;
839 case 80: env
->pc
= tmp
; break;
840 case 81: env
->npc
= tmp
; break;
842 PUT_CCR(env
, tmp
>> 32);
843 env
->asi
= (tmp
>> 24) & 0xff;
844 env
->pstate
= (tmp
>> 8) & 0xfff;
845 PUT_CWP64(env
, tmp
& 0xff);
847 case 83: env
->fsr
= tmp
; break;
848 case 84: env
->fprs
= tmp
; break;
849 case 85: env
->y
= tmp
; break;
856 #elif defined (TARGET_ARM)
858 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
859 whatever the target description contains. Due to a historical mishap
860 the FPA registers appear in between core integer regs and the CPSR.
861 We hack round this by giving the FPA regs zero size when talking to a
863 #define NUM_CORE_REGS 26
864 #define GDB_CORE_XML "arm-core.xml"
866 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
869 /* Core integer register. */
870 GET_REG32(env
->regs
[n
]);
876 memset(mem_buf
, 0, 12);
881 /* FPA status register. */
887 GET_REG32(cpsr_read(env
));
889 /* Unknown register. */
893 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
897 tmp
= ldl_p(mem_buf
);
899 /* Mask out low bit of PC to workaround gdb bugs. This will probably
900 cause problems if we ever implement the Jazelle DBX extensions. */
905 /* Core integer register. */
909 if (n
< 24) { /* 16-23 */
910 /* FPA registers (ignored). */
917 /* FPA status register (ignored). */
923 cpsr_write (env
, tmp
, 0xffffffff);
926 /* Unknown register. */
930 #elif defined (TARGET_M68K)
932 #define NUM_CORE_REGS 18
934 #define GDB_CORE_XML "cf-core.xml"
936 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
940 GET_REG32(env
->dregs
[n
]);
943 GET_REG32(env
->aregs
[n
- 8]);
946 case 16: GET_REG32(env
->sr
);
947 case 17: GET_REG32(env
->pc
);
950 /* FP registers not included here because they vary between
951 ColdFire and m68k. Use XML bits for these. */
955 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
959 tmp
= ldl_p(mem_buf
);
966 env
->aregs
[n
- 8] = tmp
;
969 case 16: env
->sr
= tmp
; break;
970 case 17: env
->pc
= tmp
; break;
976 #elif defined (TARGET_MIPS)
978 #define NUM_CORE_REGS 73
980 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
983 GET_REGL(env
->active_tc
.gpr
[n
]);
985 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
986 if (n
>= 38 && n
< 70) {
987 if (env
->CP0_Status
& (1 << CP0St_FR
))
988 GET_REGL(env
->active_fpu
.fpr
[n
- 38].d
);
990 GET_REGL(env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
]);
993 case 70: GET_REGL((int32_t)env
->active_fpu
.fcr31
);
994 case 71: GET_REGL((int32_t)env
->active_fpu
.fcr0
);
998 case 32: GET_REGL((int32_t)env
->CP0_Status
);
999 case 33: GET_REGL(env
->active_tc
.LO
[0]);
1000 case 34: GET_REGL(env
->active_tc
.HI
[0]);
1001 case 35: GET_REGL(env
->CP0_BadVAddr
);
1002 case 36: GET_REGL((int32_t)env
->CP0_Cause
);
1003 case 37: GET_REGL(env
->active_tc
.PC
);
1004 case 72: GET_REGL(0); /* fp */
1005 case 89: GET_REGL((int32_t)env
->CP0_PRid
);
1007 if (n
>= 73 && n
<= 88) {
1008 /* 16 embedded regs. */
1015 /* convert MIPS rounding mode in FCR31 to IEEE library */
1016 static unsigned int ieee_rm
[] =
1018 float_round_nearest_even
,
1019 float_round_to_zero
,
1023 #define RESTORE_ROUNDING_MODE \
1024 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
1026 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1030 tmp
= ldtul_p(mem_buf
);
1033 env
->active_tc
.gpr
[n
] = tmp
;
1034 return sizeof(target_ulong
);
1036 if (env
->CP0_Config1
& (1 << CP0C1_FP
)
1037 && n
>= 38 && n
< 73) {
1039 if (env
->CP0_Status
& (1 << CP0St_FR
))
1040 env
->active_fpu
.fpr
[n
- 38].d
= tmp
;
1042 env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
] = tmp
;
1046 env
->active_fpu
.fcr31
= tmp
& 0xFF83FFFF;
1047 /* set rounding mode */
1048 RESTORE_ROUNDING_MODE
;
1049 #ifndef CONFIG_SOFTFLOAT
1050 /* no floating point exception for native float */
1051 SET_FP_ENABLE(env
->active_fpu
.fcr31
, 0);
1054 case 71: env
->active_fpu
.fcr0
= tmp
; break;
1056 return sizeof(target_ulong
);
1059 case 32: env
->CP0_Status
= tmp
; break;
1060 case 33: env
->active_tc
.LO
[0] = tmp
; break;
1061 case 34: env
->active_tc
.HI
[0] = tmp
; break;
1062 case 35: env
->CP0_BadVAddr
= tmp
; break;
1063 case 36: env
->CP0_Cause
= tmp
; break;
1064 case 37: env
->active_tc
.PC
= tmp
; break;
1065 case 72: /* fp, ignored */ break;
1069 /* Other registers are readonly. Ignore writes. */
1073 return sizeof(target_ulong
);
1075 #elif defined (TARGET_SH4)
1077 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1078 /* FIXME: We should use XML for this. */
1080 #define NUM_CORE_REGS 59
1082 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1085 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1086 GET_REGL(env
->gregs
[n
+ 16]);
1088 GET_REGL(env
->gregs
[n
]);
1090 } else if (n
< 16) {
1091 GET_REGL(env
->gregs
[n
- 8]);
1092 } else if (n
>= 25 && n
< 41) {
1093 GET_REGL(env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
1094 } else if (n
>= 43 && n
< 51) {
1095 GET_REGL(env
->gregs
[n
- 43]);
1096 } else if (n
>= 51 && n
< 59) {
1097 GET_REGL(env
->gregs
[n
- (51 - 16)]);
1100 case 16: GET_REGL(env
->pc
);
1101 case 17: GET_REGL(env
->pr
);
1102 case 18: GET_REGL(env
->gbr
);
1103 case 19: GET_REGL(env
->vbr
);
1104 case 20: GET_REGL(env
->mach
);
1105 case 21: GET_REGL(env
->macl
);
1106 case 22: GET_REGL(env
->sr
);
1107 case 23: GET_REGL(env
->fpul
);
1108 case 24: GET_REGL(env
->fpscr
);
1109 case 41: GET_REGL(env
->ssr
);
1110 case 42: GET_REGL(env
->spc
);
1116 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1120 tmp
= ldl_p(mem_buf
);
1123 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1124 env
->gregs
[n
+ 16] = tmp
;
1126 env
->gregs
[n
] = tmp
;
1129 } else if (n
< 16) {
1130 env
->gregs
[n
- 8] = tmp
;
1132 } else if (n
>= 25 && n
< 41) {
1133 env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)] = tmp
;
1134 } else if (n
>= 43 && n
< 51) {
1135 env
->gregs
[n
- 43] = tmp
;
1137 } else if (n
>= 51 && n
< 59) {
1138 env
->gregs
[n
- (51 - 16)] = tmp
;
1142 case 16: env
->pc
= tmp
;
1143 case 17: env
->pr
= tmp
;
1144 case 18: env
->gbr
= tmp
;
1145 case 19: env
->vbr
= tmp
;
1146 case 20: env
->mach
= tmp
;
1147 case 21: env
->macl
= tmp
;
1148 case 22: env
->sr
= tmp
;
1149 case 23: env
->fpul
= tmp
;
1150 case 24: env
->fpscr
= tmp
;
1151 case 41: env
->ssr
= tmp
;
1152 case 42: env
->spc
= tmp
;
1158 #elif defined (TARGET_CRIS)
1160 #define NUM_CORE_REGS 49
1162 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1166 srs
= env
->pregs
[PR_SRS
];
1168 GET_REG32(env
->regs
[n
]);
1171 if (n
>= 21 && n
< 32) {
1172 GET_REG32(env
->pregs
[n
- 16]);
1174 if (n
>= 33 && n
< 49) {
1175 GET_REG32(env
->sregs
[srs
][n
- 33]);
1178 case 16: GET_REG8(env
->pregs
[0]);
1179 case 17: GET_REG8(env
->pregs
[1]);
1180 case 18: GET_REG32(env
->pregs
[2]);
1181 case 19: GET_REG8(srs
);
1182 case 20: GET_REG16(env
->pregs
[4]);
1183 case 32: GET_REG32(env
->pc
);
1189 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1196 tmp
= ldl_p(mem_buf
);
1202 if (n
>= 21 && n
< 32) {
1203 env
->pregs
[n
- 16] = tmp
;
1206 /* FIXME: Should support function regs be writable? */
1210 case 18: env
->pregs
[PR_PID
] = tmp
; break;
1213 case 32: env
->pc
= tmp
; break;
1218 #elif defined (TARGET_ALPHA)
1220 #define NUM_CORE_REGS 65
1222 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1225 GET_REGL(env
->ir
[n
]);
1233 val
=*((uint64_t *)&env
->fir
[n
-32]);
1237 GET_REGL(env
->fpcr
);
1249 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1252 tmp
= ldtul_p(mem_buf
);
1258 if (n
> 31 && n
< 63) {
1259 env
->fir
[n
- 32] = ldfl_p(mem_buf
);
1270 #define NUM_CORE_REGS 0
1272 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1277 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1284 static int num_g_regs
= NUM_CORE_REGS
;
1287 /* Encode data using the encoding for 'x' packets. */
1288 static int memtox(char *buf
, const char *mem
, int len
)
1296 case '#': case '$': case '*': case '}':
1308 static const char *get_feature_xml(const char *p
, const char **newp
)
1310 extern const char *const xml_builtin
[][2];
1314 static char target_xml
[1024];
1317 while (p
[len
] && p
[len
] != ':')
1322 if (strncmp(p
, "target.xml", len
) == 0) {
1323 /* Generate the XML description for this CPU. */
1324 if (!target_xml
[0]) {
1325 GDBRegisterState
*r
;
1327 snprintf(target_xml
, sizeof(target_xml
),
1328 "<?xml version=\"1.0\"?>"
1329 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1331 "<xi:include href=\"%s\"/>",
1334 for (r
= first_cpu
->gdb_regs
; r
; r
= r
->next
) {
1335 strcat(target_xml
, "<xi:include href=\"");
1336 strcat(target_xml
, r
->xml
);
1337 strcat(target_xml
, "\"/>");
1339 strcat(target_xml
, "</target>");
1343 for (i
= 0; ; i
++) {
1344 name
= xml_builtin
[i
][0];
1345 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
1348 return name
? xml_builtin
[i
][1] : NULL
;
1352 static int gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1354 GDBRegisterState
*r
;
1356 if (reg
< NUM_CORE_REGS
)
1357 return cpu_gdb_read_register(env
, mem_buf
, reg
);
1359 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1360 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1361 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
1367 static int gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1369 GDBRegisterState
*r
;
1371 if (reg
< NUM_CORE_REGS
)
1372 return cpu_gdb_write_register(env
, mem_buf
, reg
);
1374 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1375 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1376 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
1382 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
1383 specifies the first register number and these registers are included in
1384 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1385 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1388 void gdb_register_coprocessor(CPUState
* env
,
1389 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
1390 int num_regs
, const char *xml
, int g_pos
)
1392 GDBRegisterState
*s
;
1393 GDBRegisterState
**p
;
1394 static int last_reg
= NUM_CORE_REGS
;
1396 s
= (GDBRegisterState
*)qemu_mallocz(sizeof(GDBRegisterState
));
1397 s
->base_reg
= last_reg
;
1398 s
->num_regs
= num_regs
;
1399 s
->get_reg
= get_reg
;
1400 s
->set_reg
= set_reg
;
1404 /* Check for duplicates. */
1405 if (strcmp((*p
)->xml
, xml
) == 0)
1409 /* Add to end of list. */
1410 last_reg
+= num_regs
;
1413 if (g_pos
!= s
->base_reg
) {
1414 fprintf(stderr
, "Error: Bad gdb register numbering for '%s'\n"
1415 "Expected %d got %d\n", xml
, g_pos
, s
->base_reg
);
1417 num_g_regs
= last_reg
;
1422 #ifndef CONFIG_USER_ONLY
1423 static const int xlat_gdb_type
[] = {
1424 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
1425 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
1426 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
1429 static void turn_64bit_address(target_ulong
*paddr
)
1431 #if defined(TARGET_MIPS64)
1432 if (!(*paddr
& 0xffffffff00000000LL
))
1434 /*The highest bits are zero. expend them*/
1435 if (*paddr
&0x80000000)
1436 *paddr
|= 0xffffffff00000000LL
;
1440 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
1445 turn_64bit_address(&addr
);
1447 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1450 case GDB_BREAKPOINT_SW
:
1451 case GDB_BREAKPOINT_HW
:
1452 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1453 err
= cpu_breakpoint_insert(env
, addr
, BP_GDB
, NULL
);
1458 #ifndef CONFIG_USER_ONLY
1459 case GDB_WATCHPOINT_WRITE
:
1460 case GDB_WATCHPOINT_READ
:
1461 case GDB_WATCHPOINT_ACCESS
:
1462 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1463 err
= cpu_watchpoint_insert(env
, addr
, len
, xlat_gdb_type
[type
],
1475 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1480 turn_64bit_address(&addr
);
1483 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1486 case GDB_BREAKPOINT_SW
:
1487 case GDB_BREAKPOINT_HW
:
1488 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1489 err
= cpu_breakpoint_remove(env
, addr
, BP_GDB
);
1494 #ifndef CONFIG_USER_ONLY
1495 case GDB_WATCHPOINT_WRITE
:
1496 case GDB_WATCHPOINT_READ
:
1497 case GDB_WATCHPOINT_ACCESS
:
1498 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1499 err
= cpu_watchpoint_remove(env
, addr
, len
, xlat_gdb_type
[type
]);
1510 static void gdb_breakpoint_remove_all(void)
1514 if (kvm_enabled()) {
1515 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1519 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1520 cpu_breakpoint_remove_all(env
, BP_GDB
);
1521 #ifndef CONFIG_USER_ONLY
1522 cpu_watchpoint_remove_all(env
, BP_GDB
);
1527 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1531 int ch
, reg_size
, type
, res
, thread
;
1532 char buf
[MAX_PACKET_LENGTH
];
1533 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1535 target_ulong addr
, len
;
1538 printf("command='%s'\n", line_buf
);
1544 /* TODO: Make this return the correct value for user-mode. */
1545 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
1546 s
->c_cpu
->cpu_index
+1);
1548 /* Remove all the breakpoints when this query is issued,
1549 * because gdb is doing and initial connect and the state
1550 * should be cleaned up.
1552 gdb_breakpoint_remove_all();
1556 addr
= strtoull(p
, (char **)&p
, 16);
1557 #if defined(TARGET_I386)
1558 s
->c_cpu
->eip
= addr
;
1559 cpu_synchronize_state(s
->c_cpu
, 1);
1560 #elif defined (TARGET_PPC)
1561 s
->c_cpu
->nip
= addr
;
1562 #elif defined (TARGET_SPARC)
1563 s
->c_cpu
->pc
= addr
;
1564 s
->c_cpu
->npc
= addr
+ 4;
1565 #elif defined (TARGET_ARM)
1566 s
->c_cpu
->regs
[15] = addr
;
1567 #elif defined (TARGET_SH4)
1568 s
->c_cpu
->pc
= addr
;
1569 #elif defined (TARGET_MIPS)
1570 s
->c_cpu
->active_tc
.PC
= addr
;
1571 #elif defined (TARGET_CRIS)
1572 s
->c_cpu
->pc
= addr
;
1573 #elif defined (TARGET_ALPHA)
1574 s
->c_cpu
->pc
= addr
;
1581 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1582 if (s
->signal
== -1)
1587 /* Kill the target */
1588 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1592 gdb_breakpoint_remove_all();
1594 put_packet(s
, "OK");
1598 addr
= strtoull(p
, (char **)&p
, 16);
1599 turn_64bit_address(&addr
);
1600 #if defined(TARGET_I386)
1601 s
->c_cpu
->eip
= addr
;
1602 cpu_synchronize_state(s
->c_cpu
, 1);
1603 #elif defined (TARGET_PPC)
1604 s
->c_cpu
->nip
= addr
;
1605 #elif defined (TARGET_SPARC)
1606 s
->c_cpu
->pc
= addr
;
1607 s
->c_cpu
->npc
= addr
+ 4;
1608 #elif defined (TARGET_ARM)
1609 s
->c_cpu
->regs
[15] = addr
;
1610 #elif defined (TARGET_SH4)
1611 s
->c_cpu
->pc
= addr
;
1612 #elif defined (TARGET_MIPS)
1613 s
->c_cpu
->active_tc
.PC
= addr
;
1614 #elif defined (TARGET_CRIS)
1615 s
->c_cpu
->pc
= addr
;
1616 #elif defined (TARGET_ALPHA)
1617 s
->c_cpu
->pc
= addr
;
1620 cpu_single_step(s
->c_cpu
, sstep_flags
);
1628 ret
= strtoull(p
, (char **)&p
, 16);
1631 err
= strtoull(p
, (char **)&p
, 16);
1638 if (gdb_current_syscall_cb
)
1639 gdb_current_syscall_cb(s
->c_cpu
, ret
, err
);
1641 put_packet(s
, "T02");
1648 cpu_synchronize_state(s
->g_cpu
, 0);
1650 for (addr
= 0; addr
< num_g_regs
; addr
++) {
1651 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1654 memtohex(buf
, mem_buf
, len
);
1658 registers
= mem_buf
;
1659 len
= strlen(p
) / 2;
1660 hextomem((uint8_t *)registers
, p
, len
);
1661 for (addr
= 0; addr
< num_g_regs
&& len
> 0; addr
++) {
1662 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1664 registers
+= reg_size
;
1666 cpu_synchronize_state(s
->g_cpu
, 1);
1667 put_packet(s
, "OK");
1670 addr
= strtoull(p
, (char **)&p
, 16);
1671 turn_64bit_address(&addr
);
1674 len
= strtoull(p
, NULL
, 16);
1675 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 0) != 0) {
1676 put_packet (s
, "E14");
1678 memtohex(buf
, mem_buf
, len
);
1683 addr
= strtoull(p
, (char **)&p
, 16);
1684 turn_64bit_address(&addr
);
1687 len
= strtoull(p
, (char **)&p
, 16);
1690 hextomem(mem_buf
, p
, len
);
1691 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 1) != 0)
1692 put_packet(s
, "E14");
1694 put_packet(s
, "OK");
1697 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1698 This works, but can be very slow. Anything new enough to
1699 understand XML also knows how to use this properly. */
1701 goto unknown_command
;
1702 addr
= strtoull(p
, (char **)&p
, 16);
1703 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1705 memtohex(buf
, mem_buf
, reg_size
);
1708 put_packet(s
, "E14");
1713 goto unknown_command
;
1714 addr
= strtoull(p
, (char **)&p
, 16);
1717 reg_size
= strlen(p
) / 2;
1718 hextomem(mem_buf
, p
, reg_size
);
1719 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1720 put_packet(s
, "OK");
1724 type
= strtoul(p
, (char **)&p
, 16);
1727 addr
= strtoull(p
, (char **)&p
, 16);
1730 len
= strtoull(p
, (char **)&p
, 16);
1732 res
= gdb_breakpoint_insert(addr
, len
, type
);
1734 res
= gdb_breakpoint_remove(addr
, len
, type
);
1736 put_packet(s
, "OK");
1737 else if (res
== -ENOSYS
)
1740 put_packet(s
, "E22");
1744 thread
= strtoull(p
, (char **)&p
, 16);
1745 if (thread
== -1 || thread
== 0) {
1746 put_packet(s
, "OK");
1749 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1750 if (env
->cpu_index
+ 1 == thread
)
1753 put_packet(s
, "E22");
1759 put_packet(s
, "OK");
1763 put_packet(s
, "OK");
1766 put_packet(s
, "E22");
1771 thread
= strtoull(p
, (char **)&p
, 16);
1772 #ifndef CONFIG_USER_ONLY
1773 if (thread
> 0 && thread
< smp_cpus
+ 1)
1777 put_packet(s
, "OK");
1779 put_packet(s
, "E22");
1783 /* parse any 'q' packets here */
1784 if (!strcmp(p
,"qemu.sstepbits")) {
1785 /* Query Breakpoint bit definitions */
1786 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1792 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1793 /* Display or change the sstep_flags */
1796 /* Display current setting */
1797 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1802 type
= strtoul(p
, (char **)&p
, 16);
1804 put_packet(s
, "OK");
1806 } else if (strcmp(p
,"C") == 0) {
1807 /* "Current thread" remains vague in the spec, so always return
1808 * the first CPU (gdb returns the first thread). */
1809 put_packet(s
, "QC1");
1811 } else if (strcmp(p
,"fThreadInfo") == 0) {
1812 s
->query_cpu
= first_cpu
;
1813 goto report_cpuinfo
;
1814 } else if (strcmp(p
,"sThreadInfo") == 0) {
1817 snprintf(buf
, sizeof(buf
), "m%x", s
->query_cpu
->cpu_index
+1);
1819 s
->query_cpu
= s
->query_cpu
->next_cpu
;
1823 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1824 thread
= strtoull(p
+16, (char **)&p
, 16);
1825 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1826 if (env
->cpu_index
+ 1 == thread
) {
1827 cpu_synchronize_state(env
, 0);
1828 len
= snprintf((char *)mem_buf
, sizeof(mem_buf
),
1829 "CPU#%d [%s]", env
->cpu_index
,
1830 env
->halted
? "halted " : "running");
1831 memtohex(buf
, mem_buf
, len
);
1837 #ifdef CONFIG_USER_ONLY
1838 else if (strncmp(p
, "Offsets", 7) == 0) {
1839 TaskState
*ts
= s
->c_cpu
->opaque
;
1841 snprintf(buf
, sizeof(buf
),
1842 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1843 ";Bss=" TARGET_ABI_FMT_lx
,
1844 ts
->info
->code_offset
,
1845 ts
->info
->data_offset
,
1846 ts
->info
->data_offset
);
1850 #else /* !CONFIG_USER_ONLY */
1851 else if (strncmp(p
, "Rcmd,", 5) == 0) {
1852 int len
= strlen(p
+ 5);
1854 if ((len
% 2) != 0) {
1855 put_packet(s
, "E01");
1858 hextomem(mem_buf
, p
+ 5, len
);
1861 qemu_chr_read(s
->mon_chr
, mem_buf
, len
);
1862 put_packet(s
, "OK");
1865 #endif /* !CONFIG_USER_ONLY */
1866 if (strncmp(p
, "Supported", 9) == 0) {
1867 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1869 strcat(buf
, ";qXfer:features:read+");
1875 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1877 target_ulong total_len
;
1881 xml
= get_feature_xml(p
, &p
);
1883 snprintf(buf
, sizeof(buf
), "E00");
1890 addr
= strtoul(p
, (char **)&p
, 16);
1893 len
= strtoul(p
, (char **)&p
, 16);
1895 total_len
= strlen(xml
);
1896 if (addr
> total_len
) {
1897 snprintf(buf
, sizeof(buf
), "E00");
1901 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
1902 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1903 if (len
< total_len
- addr
) {
1905 len
= memtox(buf
+ 1, xml
+ addr
, len
);
1908 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
1910 put_packet_binary(s
, buf
, len
+ 1);
1914 /* Unrecognised 'q' command. */
1915 goto unknown_command
;
1919 /* put empty packet */
1927 void gdb_set_stop_cpu(CPUState
*env
)
1929 gdbserver_state
->c_cpu
= env
;
1930 gdbserver_state
->g_cpu
= env
;
1933 #ifndef CONFIG_USER_ONLY
1934 static void gdb_vm_state_change(void *opaque
, int running
, int reason
)
1936 GDBState
*s
= gdbserver_state
;
1937 CPUState
*env
= s
->c_cpu
;
1942 if (running
|| (reason
!= EXCP_DEBUG
&& reason
!= EXCP_INTERRUPT
) ||
1943 s
->state
== RS_SYSCALL
)
1946 /* disable single step if it was enable */
1947 cpu_single_step(env
, 0);
1949 if (reason
== EXCP_DEBUG
) {
1950 if (env
->watchpoint_hit
) {
1951 switch (env
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
1962 snprintf(buf
, sizeof(buf
),
1963 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
1964 GDB_SIGNAL_TRAP
, env
->cpu_index
+1, type
,
1965 env
->watchpoint_hit
->vaddr
);
1967 env
->watchpoint_hit
= NULL
;
1971 ret
= GDB_SIGNAL_TRAP
;
1973 ret
= GDB_SIGNAL_INT
;
1975 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, env
->cpu_index
+1);
1980 /* Send a gdb syscall request.
1981 This accepts limited printf-style format specifiers, specifically:
1982 %x - target_ulong argument printed in hex.
1983 %lx - 64-bit argument printed in hex.
1984 %s - string pointer (target_ulong) and length (int) pair. */
1985 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
1994 s
= gdbserver_state
;
1997 gdb_current_syscall_cb
= cb
;
1998 s
->state
= RS_SYSCALL
;
1999 #ifndef CONFIG_USER_ONLY
2000 vm_stop(EXCP_DEBUG
);
2011 addr
= va_arg(va
, target_ulong
);
2012 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
, addr
);
2015 if (*(fmt
++) != 'x')
2017 i64
= va_arg(va
, uint64_t);
2018 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, "%" PRIx64
, i64
);
2021 addr
= va_arg(va
, target_ulong
);
2022 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
"/%x",
2023 addr
, va_arg(va
, int));
2027 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
2038 #ifdef CONFIG_USER_ONLY
2039 gdb_handlesig(s
->c_cpu
, 0);
2045 static void gdb_read_byte(GDBState
*s
, int ch
)
2050 #ifndef CONFIG_USER_ONLY
2051 if (s
->last_packet_len
) {
2052 /* Waiting for a response to the last packet. If we see the start
2053 of a new command then abandon the previous response. */
2056 printf("Got NACK, retransmitting\n");
2058 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2062 printf("Got ACK\n");
2064 printf("Got '%c' when expecting ACK/NACK\n", ch
);
2066 if (ch
== '+' || ch
== '$')
2067 s
->last_packet_len
= 0;
2072 /* when the CPU is running, we cannot do anything except stop
2073 it when receiving a char */
2074 vm_stop(EXCP_INTERRUPT
);
2081 s
->line_buf_index
= 0;
2082 s
->state
= RS_GETLINE
;
2087 s
->state
= RS_CHKSUM1
;
2088 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2091 s
->line_buf
[s
->line_buf_index
++] = ch
;
2095 s
->line_buf
[s
->line_buf_index
] = '\0';
2096 s
->line_csum
= fromhex(ch
) << 4;
2097 s
->state
= RS_CHKSUM2
;
2100 s
->line_csum
|= fromhex(ch
);
2102 for(i
= 0; i
< s
->line_buf_index
; i
++) {
2103 csum
+= s
->line_buf
[i
];
2105 if (s
->line_csum
!= (csum
& 0xff)) {
2107 put_buffer(s
, &reply
, 1);
2111 put_buffer(s
, &reply
, 1);
2112 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2121 #ifdef CONFIG_USER_ONLY
2127 s
= gdbserver_state
;
2129 if (gdbserver_fd
< 0 || s
->fd
< 0)
2136 gdb_handlesig (CPUState
*env
, int sig
)
2142 s
= gdbserver_state
;
2143 if (gdbserver_fd
< 0 || s
->fd
< 0)
2146 /* disable single step if it was enabled */
2147 cpu_single_step(env
, 0);
2152 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb (sig
));
2155 /* put_packet() might have detected that the peer terminated the
2162 s
->running_state
= 0;
2163 while (s
->running_state
== 0) {
2164 n
= read (s
->fd
, buf
, 256);
2169 for (i
= 0; i
< n
; i
++)
2170 gdb_read_byte (s
, buf
[i
]);
2172 else if (n
== 0 || errno
!= EAGAIN
)
2174 /* XXX: Connection closed. Should probably wait for annother
2175 connection before continuing. */
2184 /* Tell the remote gdb that the process has exited. */
2185 void gdb_exit(CPUState
*env
, int code
)
2190 s
= gdbserver_state
;
2191 if (gdbserver_fd
< 0 || s
->fd
< 0)
2194 snprintf(buf
, sizeof(buf
), "W%02x", code
);
2198 /* Tell the remote gdb that the process has exited due to SIG. */
2199 void gdb_signalled(CPUState
*env
, int sig
)
2204 s
= gdbserver_state
;
2205 if (gdbserver_fd
< 0 || s
->fd
< 0)
2208 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb (sig
));
2212 static void gdb_accept(void)
2215 struct sockaddr_in sockaddr
;
2220 len
= sizeof(sockaddr
);
2221 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2222 if (fd
< 0 && errno
!= EINTR
) {
2225 } else if (fd
>= 0) {
2230 /* set short latency */
2232 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2234 s
= qemu_mallocz(sizeof(GDBState
));
2236 memset (s
, 0, sizeof (GDBState
));
2237 s
->c_cpu
= first_cpu
;
2238 s
->g_cpu
= first_cpu
;
2242 gdbserver_state
= s
;
2244 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2247 static int gdbserver_open(int port
)
2249 struct sockaddr_in sockaddr
;
2252 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2258 /* allow fast reuse */
2260 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
2262 sockaddr
.sin_family
= AF_INET
;
2263 sockaddr
.sin_port
= htons(port
);
2264 sockaddr
.sin_addr
.s_addr
= 0;
2265 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2270 ret
= listen(fd
, 0);
2278 int gdbserver_start(int port
)
2280 gdbserver_fd
= gdbserver_open(port
);
2281 if (gdbserver_fd
< 0)
2283 /* accept connections */
2288 /* Disable gdb stub for child processes. */
2289 void gdbserver_fork(CPUState
*env
)
2291 GDBState
*s
= gdbserver_state
;
2292 if (gdbserver_fd
< 0 || s
->fd
< 0)
2296 cpu_breakpoint_remove_all(env
, BP_GDB
);
2297 cpu_watchpoint_remove_all(env
, BP_GDB
);
2300 static int gdb_chr_can_receive(void *opaque
)
2302 /* We can handle an arbitrarily large amount of data.
2303 Pick the maximum packet size, which is as good as anything. */
2304 return MAX_PACKET_LENGTH
;
2307 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2311 for (i
= 0; i
< size
; i
++) {
2312 gdb_read_byte(gdbserver_state
, buf
[i
]);
2316 static void gdb_chr_event(void *opaque
, int event
)
2319 case CHR_EVENT_RESET
:
2320 vm_stop(EXCP_INTERRUPT
);
2328 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
2330 char buf
[MAX_PACKET_LENGTH
];
2333 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
2334 len
= (MAX_PACKET_LENGTH
/2) - 1;
2335 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
2339 static int gdb_monitor_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2341 const char *p
= (const char *)buf
;
2344 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
2346 if (len
<= max_sz
) {
2347 gdb_monitor_output(gdbserver_state
, p
, len
);
2350 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
2357 int gdbserver_start(const char *port
)
2360 char gdbstub_port_name
[128];
2363 CharDriverState
*chr
;
2365 if (!port
|| !*port
)
2368 port_num
= strtol(port
, &p
, 10);
2370 /* A numeric value is interpreted as a port number. */
2371 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
2372 "tcp::%d,nowait,nodelay,server", port_num
);
2373 port
= gdbstub_port_name
;
2376 chr
= qemu_chr_open("gdb", port
, NULL
);
2380 s
= qemu_mallocz(sizeof(GDBState
));
2381 s
->c_cpu
= first_cpu
;
2382 s
->g_cpu
= first_cpu
;
2384 gdbserver_state
= s
;
2385 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2386 gdb_chr_event
, NULL
);
2387 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
2389 /* Initialize a monitor terminal for gdb */
2390 s
->mon_chr
= qemu_mallocz(sizeof(*s
->mon_chr
));
2391 s
->mon_chr
->chr_write
= gdb_monitor_write
;
2392 monitor_init(s
->mon_chr
, 0);