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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "qemu-common.h"
22 #ifdef CONFIG_USER_ONLY
33 #include "qemu-char.h"
38 #define MAX_PACKET_LENGTH 4096
40 #include "qemu_socket.h"
47 GDB_SIGNAL_UNKNOWN
= 143
50 #ifdef CONFIG_USER_ONLY
52 /* Map target signal numbers to GDB protocol signal numbers and vice
53 * versa. For user emulation's currently supported systems, we can
54 * assume most signals are defined.
57 static int gdb_signal_table
[] = {
211 /* In system mode we only need SIGINT and SIGTRAP; other signals
212 are not yet supported. */
219 static int gdb_signal_table
[] = {
229 #ifdef CONFIG_USER_ONLY
230 static int target_signal_to_gdb (int sig
)
233 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
234 if (gdb_signal_table
[i
] == sig
)
236 return GDB_SIGNAL_UNKNOWN
;
240 static int gdb_signal_to_target (int sig
)
242 if (sig
< ARRAY_SIZE (gdb_signal_table
))
243 return gdb_signal_table
[sig
];
250 typedef struct GDBRegisterState
{
256 struct GDBRegisterState
*next
;
266 typedef struct GDBState
{
267 CPUState
*c_cpu
; /* current CPU for step/continue ops */
268 CPUState
*g_cpu
; /* current CPU for other ops */
269 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
270 enum RSState state
; /* parsing state */
271 char line_buf
[MAX_PACKET_LENGTH
];
274 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
277 #ifdef CONFIG_USER_ONLY
281 CharDriverState
*chr
;
285 /* By default use no IRQs and no timers while single stepping so as to
286 * make single stepping like an ICE HW step.
288 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
290 static GDBState
*gdbserver_state
;
292 /* This is an ugly hack to cope with both new and old gdb.
293 If gdb sends qXfer:features:read then assume we're talking to a newish
294 gdb that understands target descriptions. */
295 static int gdb_has_xml
;
297 #ifdef CONFIG_USER_ONLY
298 /* XXX: This is not thread safe. Do we care? */
299 static int gdbserver_fd
= -1;
301 static int get_char(GDBState
*s
)
307 ret
= recv(s
->fd
, &ch
, 1, 0);
309 if (errno
== ECONNRESET
)
311 if (errno
!= EINTR
&& errno
!= EAGAIN
)
313 } else if (ret
== 0) {
325 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
333 /* If gdb is connected when the first semihosting syscall occurs then use
334 remote gdb syscalls. Otherwise use native file IO. */
335 int use_gdb_syscalls(void)
337 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
338 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
341 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
344 /* Resume execution. */
345 static inline void gdb_continue(GDBState
*s
)
347 #ifdef CONFIG_USER_ONLY
348 s
->running_state
= 1;
354 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
356 #ifdef CONFIG_USER_ONLY
360 ret
= send(s
->fd
, buf
, len
, 0);
362 if (errno
!= EINTR
&& errno
!= EAGAIN
)
370 qemu_chr_write(s
->chr
, buf
, len
);
374 static inline int fromhex(int v
)
376 if (v
>= '0' && v
<= '9')
378 else if (v
>= 'A' && v
<= 'F')
380 else if (v
>= 'a' && v
<= 'f')
386 static inline int tohex(int v
)
394 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
399 for(i
= 0; i
< len
; i
++) {
401 *q
++ = tohex(c
>> 4);
402 *q
++ = tohex(c
& 0xf);
407 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
411 for(i
= 0; i
< len
; i
++) {
412 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
417 /* return -1 if error, 0 if OK */
418 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
)
429 for(i
= 0; i
< len
; i
++) {
433 *(p
++) = tohex((csum
>> 4) & 0xf);
434 *(p
++) = tohex((csum
) & 0xf);
436 s
->last_packet_len
= p
- s
->last_packet
;
437 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
439 #ifdef CONFIG_USER_ONLY
452 /* return -1 if error, 0 if OK */
453 static int put_packet(GDBState
*s
, const char *buf
)
456 printf("reply='%s'\n", buf
);
459 return put_packet_binary(s
, buf
, strlen(buf
));
462 /* The GDB remote protocol transfers values in target byte order. This means
463 we can use the raw memory access routines to access the value buffer.
464 Conveniently, these also handle the case where the buffer is mis-aligned.
466 #define GET_REG8(val) do { \
467 stb_p(mem_buf, val); \
470 #define GET_REG16(val) do { \
471 stw_p(mem_buf, val); \
474 #define GET_REG32(val) do { \
475 stl_p(mem_buf, val); \
478 #define GET_REG64(val) do { \
479 stq_p(mem_buf, val); \
483 #if TARGET_LONG_BITS == 64
484 #define GET_REGL(val) GET_REG64(val)
485 #define ldtul_p(addr) ldq_p(addr)
487 #define GET_REGL(val) GET_REG32(val)
488 #define ldtul_p(addr) ldl_p(addr)
491 #if defined(TARGET_I386)
494 static const int gpr_map
[16] = {
495 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
496 8, 9, 10, 11, 12, 13, 14, 15
499 static const int gpr_map
[8] = {0, 1, 2, 3, 4, 5, 6, 7};
502 #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
504 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
506 if (n
< CPU_NB_REGS
) {
507 GET_REGL(env
->regs
[gpr_map
[n
]]);
508 } else if (n
>= CPU_NB_REGS
+ 8 && n
< CPU_NB_REGS
+ 16) {
509 /* FIXME: byteswap float values. */
510 #ifdef USE_X86LDOUBLE
511 memcpy(mem_buf
, &env
->fpregs
[n
- (CPU_NB_REGS
+ 8)], 10);
513 memset(mem_buf
, 0, 10);
516 } else if (n
>= CPU_NB_REGS
+ 24) {
517 n
-= CPU_NB_REGS
+ 24;
518 if (n
< CPU_NB_REGS
) {
519 stq_p(mem_buf
, env
->xmm_regs
[n
].XMM_Q(0));
520 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].XMM_Q(1));
522 } else if (n
== CPU_NB_REGS
) {
523 GET_REG32(env
->mxcsr
);
528 case 0: GET_REGL(env
->eip
);
529 case 1: GET_REG32(env
->eflags
);
530 case 2: GET_REG32(env
->segs
[R_CS
].selector
);
531 case 3: GET_REG32(env
->segs
[R_SS
].selector
);
532 case 4: GET_REG32(env
->segs
[R_DS
].selector
);
533 case 5: GET_REG32(env
->segs
[R_ES
].selector
);
534 case 6: GET_REG32(env
->segs
[R_FS
].selector
);
535 case 7: GET_REG32(env
->segs
[R_GS
].selector
);
536 /* 8...15 x87 regs. */
537 case 16: GET_REG32(env
->fpuc
);
538 case 17: GET_REG32((env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11);
539 case 18: GET_REG32(0); /* ftag */
540 case 19: GET_REG32(0); /* fiseg */
541 case 20: GET_REG32(0); /* fioff */
542 case 21: GET_REG32(0); /* foseg */
543 case 22: GET_REG32(0); /* fooff */
544 case 23: GET_REG32(0); /* fop */
551 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int i
)
555 if (i
< CPU_NB_REGS
) {
556 env
->regs
[gpr_map
[i
]] = ldtul_p(mem_buf
);
557 return sizeof(target_ulong
);
558 } else if (i
>= CPU_NB_REGS
+ 8 && i
< CPU_NB_REGS
+ 16) {
559 i
-= CPU_NB_REGS
+ 8;
560 #ifdef USE_X86LDOUBLE
561 memcpy(&env
->fpregs
[i
], mem_buf
, 10);
564 } else if (i
>= CPU_NB_REGS
+ 24) {
565 i
-= CPU_NB_REGS
+ 24;
566 if (i
< CPU_NB_REGS
) {
567 env
->xmm_regs
[i
].XMM_Q(0) = ldq_p(mem_buf
);
568 env
->xmm_regs
[i
].XMM_Q(1) = ldq_p(mem_buf
+ 8);
570 } else if (i
== CPU_NB_REGS
) {
571 env
->mxcsr
= ldl_p(mem_buf
);
577 case 0: env
->eip
= ldtul_p(mem_buf
); return sizeof(target_ulong
);
578 case 1: env
->eflags
= ldl_p(mem_buf
); return 4;
579 #if defined(CONFIG_USER_ONLY)
580 #define LOAD_SEG(index, sreg)\
581 tmp = ldl_p(mem_buf);\
582 if (tmp != env->segs[sreg].selector)\
583 cpu_x86_load_seg(env, sreg, tmp);
585 /* FIXME: Honor segment registers. Needs to avoid raising an exception
586 when the selector is invalid. */
587 #define LOAD_SEG(index, sreg) do {} while(0)
589 case 2: LOAD_SEG(10, R_CS
); return 4;
590 case 3: LOAD_SEG(11, R_SS
); return 4;
591 case 4: LOAD_SEG(12, R_DS
); return 4;
592 case 5: LOAD_SEG(13, R_ES
); return 4;
593 case 6: LOAD_SEG(14, R_FS
); return 4;
594 case 7: LOAD_SEG(15, R_GS
); return 4;
595 /* 8...15 x87 regs. */
596 case 16: env
->fpuc
= ldl_p(mem_buf
); return 4;
598 tmp
= ldl_p(mem_buf
);
599 env
->fpstt
= (tmp
>> 11) & 7;
600 env
->fpus
= tmp
& ~0x3800;
602 case 18: /* ftag */ return 4;
603 case 19: /* fiseg */ return 4;
604 case 20: /* fioff */ return 4;
605 case 21: /* foseg */ return 4;
606 case 22: /* fooff */ return 4;
607 case 23: /* fop */ return 4;
611 /* Unrecognised register. */
615 #elif defined (TARGET_PPC)
617 #define NUM_CORE_REGS 71
619 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
623 GET_REGL(env
->gpr
[n
]);
626 stfq_p(mem_buf
, env
->fpr
[n
-32]);
630 case 64: GET_REGL(env
->nip
);
631 case 65: GET_REGL(env
->msr
);
636 for (i
= 0; i
< 8; i
++)
637 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
640 case 67: GET_REGL(env
->lr
);
641 case 68: GET_REGL(env
->ctr
);
642 case 69: GET_REGL(env
->xer
);
643 case 70: GET_REG32(0); /* fpscr */
649 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
653 env
->gpr
[n
] = ldtul_p(mem_buf
);
654 return sizeof(target_ulong
);
657 env
->fpr
[n
-32] = ldfq_p(mem_buf
);
662 env
->nip
= ldtul_p(mem_buf
);
663 return sizeof(target_ulong
);
665 ppc_store_msr(env
, ldtul_p(mem_buf
));
666 return sizeof(target_ulong
);
669 uint32_t cr
= ldl_p(mem_buf
);
671 for (i
= 0; i
< 8; i
++)
672 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
676 env
->lr
= ldtul_p(mem_buf
);
677 return sizeof(target_ulong
);
679 env
->ctr
= ldtul_p(mem_buf
);
680 return sizeof(target_ulong
);
682 env
->xer
= ldtul_p(mem_buf
);
683 return sizeof(target_ulong
);
692 #elif defined (TARGET_SPARC)
694 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
695 #define NUM_CORE_REGS 86
697 #define NUM_CORE_REGS 73
701 #define GET_REGA(val) GET_REG32(val)
703 #define GET_REGA(val) GET_REGL(val)
706 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
710 GET_REGA(env
->gregs
[n
]);
713 /* register window */
714 GET_REGA(env
->regwptr
[n
- 8]);
716 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
719 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
721 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
723 case 64: GET_REGA(env
->y
);
724 case 65: GET_REGA(GET_PSR(env
));
725 case 66: GET_REGA(env
->wim
);
726 case 67: GET_REGA(env
->tbr
);
727 case 68: GET_REGA(env
->pc
);
728 case 69: GET_REGA(env
->npc
);
729 case 70: GET_REGA(env
->fsr
);
730 case 71: GET_REGA(0); /* csr */
731 case 72: GET_REGA(0);
736 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
739 /* f32-f62 (double width, even numbers only) */
742 val
= (uint64_t)*((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) << 32;
743 val
|= *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]);
747 case 80: GET_REGL(env
->pc
);
748 case 81: GET_REGL(env
->npc
);
749 case 82: GET_REGL(((uint64_t)GET_CCR(env
) << 32) |
750 ((env
->asi
& 0xff) << 24) |
751 ((env
->pstate
& 0xfff) << 8) |
753 case 83: GET_REGL(env
->fsr
);
754 case 84: GET_REGL(env
->fprs
);
755 case 85: GET_REGL(env
->y
);
761 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
763 #if defined(TARGET_ABI32)
766 tmp
= ldl_p(mem_buf
);
770 tmp
= ldtul_p(mem_buf
);
777 /* register window */
778 env
->regwptr
[n
- 8] = tmp
;
780 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
783 *((uint32_t *)&env
->fpr
[n
- 32]) = tmp
;
785 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
787 case 64: env
->y
= tmp
; break;
788 case 65: PUT_PSR(env
, tmp
); break;
789 case 66: env
->wim
= tmp
; break;
790 case 67: env
->tbr
= tmp
; break;
791 case 68: env
->pc
= tmp
; break;
792 case 69: env
->npc
= tmp
; break;
793 case 70: env
->fsr
= tmp
; break;
801 env
->fpr
[n
] = ldfl_p(mem_buf
);
804 /* f32-f62 (double width, even numbers only) */
805 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) = tmp
>> 32;
806 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]) = tmp
;
809 case 80: env
->pc
= tmp
; break;
810 case 81: env
->npc
= tmp
; break;
812 PUT_CCR(env
, tmp
>> 32);
813 env
->asi
= (tmp
>> 24) & 0xff;
814 env
->pstate
= (tmp
>> 8) & 0xfff;
815 PUT_CWP64(env
, tmp
& 0xff);
817 case 83: env
->fsr
= tmp
; break;
818 case 84: env
->fprs
= tmp
; break;
819 case 85: env
->y
= tmp
; break;
826 #elif defined (TARGET_ARM)
828 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
829 whatever the target description contains. Due to a historical mishap
830 the FPA registers appear in between core integer regs and the CPSR.
831 We hack round this by giving the FPA regs zero size when talking to a
833 #define NUM_CORE_REGS 26
834 #define GDB_CORE_XML "arm-core.xml"
836 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
839 /* Core integer register. */
840 GET_REG32(env
->regs
[n
]);
846 memset(mem_buf
, 0, 12);
851 /* FPA status register. */
857 GET_REG32(cpsr_read(env
));
859 /* Unknown register. */
863 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
867 tmp
= ldl_p(mem_buf
);
869 /* Mask out low bit of PC to workaround gdb bugs. This will probably
870 cause problems if we ever implement the Jazelle DBX extensions. */
875 /* Core integer register. */
879 if (n
< 24) { /* 16-23 */
880 /* FPA registers (ignored). */
887 /* FPA status register (ignored). */
893 cpsr_write (env
, tmp
, 0xffffffff);
896 /* Unknown register. */
900 #elif defined (TARGET_M68K)
902 #define NUM_CORE_REGS 18
904 #define GDB_CORE_XML "cf-core.xml"
906 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
910 GET_REG32(env
->dregs
[n
]);
913 GET_REG32(env
->aregs
[n
- 8]);
916 case 16: GET_REG32(env
->sr
);
917 case 17: GET_REG32(env
->pc
);
920 /* FP registers not included here because they vary between
921 ColdFire and m68k. Use XML bits for these. */
925 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
929 tmp
= ldl_p(mem_buf
);
936 env
->aregs
[n
- 8] = tmp
;
939 case 16: env
->sr
= tmp
; break;
940 case 17: env
->pc
= tmp
; break;
946 #elif defined (TARGET_MIPS)
948 #define NUM_CORE_REGS 73
950 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
953 GET_REGL(env
->active_tc
.gpr
[n
]);
955 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
956 if (n
>= 38 && n
< 70) {
957 if (env
->CP0_Status
& (1 << CP0St_FR
))
958 GET_REGL(env
->active_fpu
.fpr
[n
- 38].d
);
960 GET_REGL(env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
]);
963 case 70: GET_REGL((int32_t)env
->active_fpu
.fcr31
);
964 case 71: GET_REGL((int32_t)env
->active_fpu
.fcr0
);
968 case 32: GET_REGL((int32_t)env
->CP0_Status
);
969 case 33: GET_REGL(env
->active_tc
.LO
[0]);
970 case 34: GET_REGL(env
->active_tc
.HI
[0]);
971 case 35: GET_REGL(env
->CP0_BadVAddr
);
972 case 36: GET_REGL((int32_t)env
->CP0_Cause
);
973 case 37: GET_REGL(env
->active_tc
.PC
);
974 case 72: GET_REGL(0); /* fp */
975 case 89: GET_REGL((int32_t)env
->CP0_PRid
);
977 if (n
>= 73 && n
<= 88) {
978 /* 16 embedded regs. */
985 /* convert MIPS rounding mode in FCR31 to IEEE library */
986 static unsigned int ieee_rm
[] =
988 float_round_nearest_even
,
993 #define RESTORE_ROUNDING_MODE \
994 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
996 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1000 tmp
= ldtul_p(mem_buf
);
1003 env
->active_tc
.gpr
[n
] = tmp
;
1004 return sizeof(target_ulong
);
1006 if (env
->CP0_Config1
& (1 << CP0C1_FP
)
1007 && n
>= 38 && n
< 73) {
1009 if (env
->CP0_Status
& (1 << CP0St_FR
))
1010 env
->active_fpu
.fpr
[n
- 38].d
= tmp
;
1012 env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
] = tmp
;
1016 env
->active_fpu
.fcr31
= tmp
& 0xFF83FFFF;
1017 /* set rounding mode */
1018 RESTORE_ROUNDING_MODE
;
1019 #ifndef CONFIG_SOFTFLOAT
1020 /* no floating point exception for native float */
1021 SET_FP_ENABLE(env
->active_fpu
.fcr31
, 0);
1024 case 71: env
->active_fpu
.fcr0
= tmp
; break;
1026 return sizeof(target_ulong
);
1029 case 32: env
->CP0_Status
= tmp
; break;
1030 case 33: env
->active_tc
.LO
[0] = tmp
; break;
1031 case 34: env
->active_tc
.HI
[0] = tmp
; break;
1032 case 35: env
->CP0_BadVAddr
= tmp
; break;
1033 case 36: env
->CP0_Cause
= tmp
; break;
1034 case 37: env
->active_tc
.PC
= tmp
; break;
1035 case 72: /* fp, ignored */ break;
1039 /* Other registers are readonly. Ignore writes. */
1043 return sizeof(target_ulong
);
1045 #elif defined (TARGET_SH4)
1047 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1048 /* FIXME: We should use XML for this. */
1050 #define NUM_CORE_REGS 59
1052 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1055 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1056 GET_REGL(env
->gregs
[n
+ 16]);
1058 GET_REGL(env
->gregs
[n
]);
1060 } else if (n
< 16) {
1061 GET_REGL(env
->gregs
[n
- 8]);
1062 } else if (n
>= 25 && n
< 41) {
1063 GET_REGL(env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
1064 } else if (n
>= 43 && n
< 51) {
1065 GET_REGL(env
->gregs
[n
- 43]);
1066 } else if (n
>= 51 && n
< 59) {
1067 GET_REGL(env
->gregs
[n
- (51 - 16)]);
1070 case 16: GET_REGL(env
->pc
);
1071 case 17: GET_REGL(env
->pr
);
1072 case 18: GET_REGL(env
->gbr
);
1073 case 19: GET_REGL(env
->vbr
);
1074 case 20: GET_REGL(env
->mach
);
1075 case 21: GET_REGL(env
->macl
);
1076 case 22: GET_REGL(env
->sr
);
1077 case 23: GET_REGL(env
->fpul
);
1078 case 24: GET_REGL(env
->fpscr
);
1079 case 41: GET_REGL(env
->ssr
);
1080 case 42: GET_REGL(env
->spc
);
1086 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1090 tmp
= ldl_p(mem_buf
);
1093 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1094 env
->gregs
[n
+ 16] = tmp
;
1096 env
->gregs
[n
] = tmp
;
1099 } else if (n
< 16) {
1100 env
->gregs
[n
- 8] = tmp
;
1102 } else if (n
>= 25 && n
< 41) {
1103 env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)] = tmp
;
1104 } else if (n
>= 43 && n
< 51) {
1105 env
->gregs
[n
- 43] = tmp
;
1107 } else if (n
>= 51 && n
< 59) {
1108 env
->gregs
[n
- (51 - 16)] = tmp
;
1112 case 16: env
->pc
= tmp
;
1113 case 17: env
->pr
= tmp
;
1114 case 18: env
->gbr
= tmp
;
1115 case 19: env
->vbr
= tmp
;
1116 case 20: env
->mach
= tmp
;
1117 case 21: env
->macl
= tmp
;
1118 case 22: env
->sr
= tmp
;
1119 case 23: env
->fpul
= tmp
;
1120 case 24: env
->fpscr
= tmp
;
1121 case 41: env
->ssr
= tmp
;
1122 case 42: env
->spc
= tmp
;
1128 #elif defined (TARGET_CRIS)
1130 #define NUM_CORE_REGS 49
1132 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1136 srs
= env
->pregs
[PR_SRS
];
1138 GET_REG32(env
->regs
[n
]);
1141 if (n
>= 21 && n
< 32) {
1142 GET_REG32(env
->pregs
[n
- 16]);
1144 if (n
>= 33 && n
< 49) {
1145 GET_REG32(env
->sregs
[srs
][n
- 33]);
1148 case 16: GET_REG8(env
->pregs
[0]);
1149 case 17: GET_REG8(env
->pregs
[1]);
1150 case 18: GET_REG32(env
->pregs
[2]);
1151 case 19: GET_REG8(srs
);
1152 case 20: GET_REG16(env
->pregs
[4]);
1153 case 32: GET_REG32(env
->pc
);
1159 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1166 tmp
= ldl_p(mem_buf
);
1172 if (n
>= 21 && n
< 32) {
1173 env
->pregs
[n
- 16] = tmp
;
1176 /* FIXME: Should support function regs be writable? */
1180 case 18: env
->pregs
[PR_PID
] = tmp
; break;
1183 case 32: env
->pc
= tmp
; break;
1188 #elif defined (TARGET_ALPHA)
1190 #define NUM_CORE_REGS 65
1192 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1195 GET_REGL(env
->ir
[n
]);
1203 val
=*((uint64_t *)&env
->fir
[n
-32]);
1207 GET_REGL(env
->fpcr
);
1219 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1222 tmp
= ldtul_p(mem_buf
);
1228 if (n
> 31 && n
< 63) {
1229 env
->fir
[n
- 32] = ldfl_p(mem_buf
);
1240 #define NUM_CORE_REGS 0
1242 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1247 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1254 static int num_g_regs
= NUM_CORE_REGS
;
1257 /* Encode data using the encoding for 'x' packets. */
1258 static int memtox(char *buf
, const char *mem
, int len
)
1266 case '#': case '$': case '*': case '}':
1278 static const char *get_feature_xml(const char *p
, const char **newp
)
1280 extern const char *const xml_builtin
[][2];
1284 static char target_xml
[1024];
1287 while (p
[len
] && p
[len
] != ':')
1292 if (strncmp(p
, "target.xml", len
) == 0) {
1293 /* Generate the XML description for this CPU. */
1294 if (!target_xml
[0]) {
1295 GDBRegisterState
*r
;
1297 snprintf(target_xml
, sizeof(target_xml
),
1298 "<?xml version=\"1.0\"?>"
1299 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1301 "<xi:include href=\"%s\"/>",
1304 for (r
= first_cpu
->gdb_regs
; r
; r
= r
->next
) {
1305 strcat(target_xml
, "<xi:include href=\"");
1306 strcat(target_xml
, r
->xml
);
1307 strcat(target_xml
, "\"/>");
1309 strcat(target_xml
, "</target>");
1313 for (i
= 0; ; i
++) {
1314 name
= xml_builtin
[i
][0];
1315 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
1318 return name
? xml_builtin
[i
][1] : NULL
;
1321 /* #elif defined(TARGET_Z80) */
1322 /* Z80 FIXME Z80 TODO Z80 */
1323 /* GDB doesn't define this yet */
1324 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
1326 uint32_t *registers
= (uint32_t *)mem_buf
;
1329 for(i
= 0; i
< 8; i
++) {
1330 registers
[i
] = env
->regs
[i
];
1332 registers
[8] = env
->pc
;
1333 registers
[9] = env
->imode
;
1338 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
1340 uint32_t *registers
= (uint32_t *)mem_buf
;
1343 for(i
= 0; i
< 8; i
++) {
1344 env
->regs
[i
] = tswapl(registers
[i
]);
1346 env
->pc
= tswapl(registers
[8]);
1347 env
->imode
= tswapl(registers
[9]);
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 /* GDB breakpoint/watchpoint types */
1423 #define GDB_BREAKPOINT_SW 0
1424 #define GDB_BREAKPOINT_HW 1
1425 #define GDB_WATCHPOINT_WRITE 2
1426 #define GDB_WATCHPOINT_READ 3
1427 #define GDB_WATCHPOINT_ACCESS 4
1429 #ifndef CONFIG_USER_ONLY
1430 static const int xlat_gdb_type
[] = {
1431 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
1432 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
1433 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
1437 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
1443 case GDB_BREAKPOINT_SW
:
1444 case GDB_BREAKPOINT_HW
:
1445 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1446 err
= cpu_breakpoint_insert(env
, addr
, BP_GDB
, NULL
);
1451 #ifndef CONFIG_USER_ONLY
1452 case GDB_WATCHPOINT_WRITE
:
1453 case GDB_WATCHPOINT_READ
:
1454 case GDB_WATCHPOINT_ACCESS
:
1455 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1456 err
= cpu_watchpoint_insert(env
, addr
, len
, xlat_gdb_type
[type
],
1468 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1474 case GDB_BREAKPOINT_SW
:
1475 case GDB_BREAKPOINT_HW
:
1476 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1477 err
= cpu_breakpoint_remove(env
, addr
, BP_GDB
);
1482 #ifndef CONFIG_USER_ONLY
1483 case GDB_WATCHPOINT_WRITE
:
1484 case GDB_WATCHPOINT_READ
:
1485 case GDB_WATCHPOINT_ACCESS
:
1486 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1487 err
= cpu_watchpoint_remove(env
, addr
, len
, xlat_gdb_type
[type
]);
1498 static void gdb_breakpoint_remove_all(void)
1502 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1503 cpu_breakpoint_remove_all(env
, BP_GDB
);
1504 #ifndef CONFIG_USER_ONLY
1505 cpu_watchpoint_remove_all(env
, BP_GDB
);
1510 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1514 int ch
, reg_size
, type
, res
, thread
;
1515 char buf
[MAX_PACKET_LENGTH
];
1516 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1518 target_ulong addr
, len
;
1521 printf("command='%s'\n", line_buf
);
1527 /* TODO: Make this return the correct value for user-mode. */
1528 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
1529 s
->c_cpu
->cpu_index
+1);
1531 /* Remove all the breakpoints when this query is issued,
1532 * because gdb is doing and initial connect and the state
1533 * should be cleaned up.
1535 gdb_breakpoint_remove_all();
1539 addr
= strtoull(p
, (char **)&p
, 16);
1540 #if defined(TARGET_I386)
1541 s
->c_cpu
->eip
= addr
;
1542 #elif defined (TARGET_PPC)
1543 s
->c_cpu
->nip
= addr
;
1544 #elif defined (TARGET_SPARC)
1545 s
->c_cpu
->pc
= addr
;
1546 s
->c_cpu
->npc
= addr
+ 4;
1547 #elif defined (TARGET_ARM)
1548 s
->c_cpu
->regs
[15] = addr
;
1549 #elif defined (TARGET_SH4)
1550 s
->c_cpu
->pc
= addr
;
1551 #elif defined (TARGET_MIPS)
1552 s
->c_cpu
->active_tc
.PC
= addr
;
1553 #elif defined (TARGET_CRIS)
1554 s
->c_cpu
->pc
= addr
;
1555 #elif defined (TARGET_ALPHA)
1556 s
->c_cpu
->pc
= addr
;
1563 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1564 if (s
->signal
== -1)
1569 /* Kill the target */
1570 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1574 gdb_breakpoint_remove_all();
1576 put_packet(s
, "OK");
1580 addr
= strtoull(p
, (char **)&p
, 16);
1581 #if defined(TARGET_I386)
1582 s
->c_cpu
->eip
= addr
;
1583 #elif defined (TARGET_PPC)
1584 s
->c_cpu
->nip
= addr
;
1585 #elif defined (TARGET_SPARC)
1586 s
->c_cpu
->pc
= addr
;
1587 s
->c_cpu
->npc
= addr
+ 4;
1588 #elif defined (TARGET_ARM)
1589 s
->c_cpu
->regs
[15] = addr
;
1590 #elif defined (TARGET_SH4)
1591 s
->c_cpu
->pc
= addr
;
1592 #elif defined (TARGET_MIPS)
1593 s
->c_cpu
->active_tc
.PC
= addr
;
1594 #elif defined (TARGET_CRIS)
1595 s
->c_cpu
->pc
= addr
;
1596 #elif defined (TARGET_ALPHA)
1597 s
->c_cpu
->pc
= addr
;
1600 cpu_single_step(s
->c_cpu
, sstep_flags
);
1608 ret
= strtoull(p
, (char **)&p
, 16);
1611 err
= strtoull(p
, (char **)&p
, 16);
1618 if (gdb_current_syscall_cb
)
1619 gdb_current_syscall_cb(s
->c_cpu
, ret
, err
);
1621 put_packet(s
, "T02");
1629 for (addr
= 0; addr
< num_g_regs
; addr
++) {
1630 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1633 memtohex(buf
, mem_buf
, len
);
1637 registers
= mem_buf
;
1638 len
= strlen(p
) / 2;
1639 hextomem((uint8_t *)registers
, p
, len
);
1640 for (addr
= 0; addr
< num_g_regs
&& len
> 0; addr
++) {
1641 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1643 registers
+= reg_size
;
1645 put_packet(s
, "OK");
1648 addr
= strtoull(p
, (char **)&p
, 16);
1651 len
= strtoull(p
, NULL
, 16);
1652 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 0) != 0) {
1653 put_packet (s
, "E14");
1655 memtohex(buf
, mem_buf
, len
);
1660 addr
= strtoull(p
, (char **)&p
, 16);
1663 len
= strtoull(p
, (char **)&p
, 16);
1666 hextomem(mem_buf
, p
, len
);
1667 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 1) != 0)
1668 put_packet(s
, "E14");
1670 put_packet(s
, "OK");
1673 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1674 This works, but can be very slow. Anything new enough to
1675 understand XML also knows how to use this properly. */
1677 goto unknown_command
;
1678 addr
= strtoull(p
, (char **)&p
, 16);
1679 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1681 memtohex(buf
, mem_buf
, reg_size
);
1684 put_packet(s
, "E14");
1689 goto unknown_command
;
1690 addr
= strtoull(p
, (char **)&p
, 16);
1693 reg_size
= strlen(p
) / 2;
1694 hextomem(mem_buf
, p
, reg_size
);
1695 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1696 put_packet(s
, "OK");
1700 type
= strtoul(p
, (char **)&p
, 16);
1703 addr
= strtoull(p
, (char **)&p
, 16);
1706 len
= strtoull(p
, (char **)&p
, 16);
1708 res
= gdb_breakpoint_insert(addr
, len
, type
);
1710 res
= gdb_breakpoint_remove(addr
, len
, type
);
1712 put_packet(s
, "OK");
1713 else if (res
== -ENOSYS
)
1716 put_packet(s
, "E22");
1720 thread
= strtoull(p
, (char **)&p
, 16);
1721 if (thread
== -1 || thread
== 0) {
1722 put_packet(s
, "OK");
1725 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1726 if (env
->cpu_index
+ 1 == thread
)
1729 put_packet(s
, "E22");
1735 put_packet(s
, "OK");
1739 put_packet(s
, "OK");
1742 put_packet(s
, "E22");
1747 thread
= strtoull(p
, (char **)&p
, 16);
1748 #ifndef CONFIG_USER_ONLY
1749 if (thread
> 0 && thread
< smp_cpus
+ 1)
1753 put_packet(s
, "OK");
1755 put_packet(s
, "E22");
1759 /* parse any 'q' packets here */
1760 if (!strcmp(p
,"qemu.sstepbits")) {
1761 /* Query Breakpoint bit definitions */
1762 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1768 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1769 /* Display or change the sstep_flags */
1772 /* Display current setting */
1773 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1778 type
= strtoul(p
, (char **)&p
, 16);
1780 put_packet(s
, "OK");
1782 } else if (strcmp(p
,"C") == 0) {
1783 /* "Current thread" remains vague in the spec, so always return
1784 * the first CPU (gdb returns the first thread). */
1785 put_packet(s
, "QC1");
1787 } else if (strcmp(p
,"fThreadInfo") == 0) {
1788 s
->query_cpu
= first_cpu
;
1789 goto report_cpuinfo
;
1790 } else if (strcmp(p
,"sThreadInfo") == 0) {
1793 snprintf(buf
, sizeof(buf
), "m%x", s
->query_cpu
->cpu_index
+1);
1795 s
->query_cpu
= s
->query_cpu
->next_cpu
;
1799 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1800 thread
= strtoull(p
+16, (char **)&p
, 16);
1801 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1802 if (env
->cpu_index
+ 1 == thread
) {
1803 len
= snprintf((char *)mem_buf
, sizeof(mem_buf
),
1804 "CPU#%d [%s]", env
->cpu_index
,
1805 env
->halted
? "halted " : "running");
1806 memtohex(buf
, mem_buf
, len
);
1812 #ifdef CONFIG_LINUX_USER
1813 else if (strncmp(p
, "Offsets", 7) == 0) {
1814 TaskState
*ts
= s
->c_cpu
->opaque
;
1816 snprintf(buf
, sizeof(buf
),
1817 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1818 ";Bss=" TARGET_ABI_FMT_lx
,
1819 ts
->info
->code_offset
,
1820 ts
->info
->data_offset
,
1821 ts
->info
->data_offset
);
1826 if (strncmp(p
, "Supported", 9) == 0) {
1827 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1829 strcat(buf
, ";qXfer:features:read+");
1835 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1837 target_ulong total_len
;
1841 xml
= get_feature_xml(p
, &p
);
1843 snprintf(buf
, sizeof(buf
), "E00");
1850 addr
= strtoul(p
, (char **)&p
, 16);
1853 len
= strtoul(p
, (char **)&p
, 16);
1855 total_len
= strlen(xml
);
1856 if (addr
> total_len
) {
1857 snprintf(buf
, sizeof(buf
), "E00");
1861 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
1862 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1863 if (len
< total_len
- addr
) {
1865 len
= memtox(buf
+ 1, xml
+ addr
, len
);
1868 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
1870 put_packet_binary(s
, buf
, len
+ 1);
1874 /* Unrecognised 'q' command. */
1875 goto unknown_command
;
1879 /* put empty packet */
1887 void gdb_set_stop_cpu(CPUState
*env
)
1889 gdbserver_state
->c_cpu
= env
;
1890 gdbserver_state
->g_cpu
= env
;
1893 #ifndef CONFIG_USER_ONLY
1894 static void gdb_vm_stopped(void *opaque
, int reason
)
1896 GDBState
*s
= gdbserver_state
;
1897 CPUState
*env
= s
->c_cpu
;
1902 if (s
->state
== RS_SYSCALL
)
1905 /* disable single step if it was enable */
1906 cpu_single_step(env
, 0);
1908 if (reason
== EXCP_DEBUG
) {
1909 if (env
->watchpoint_hit
) {
1910 switch (env
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
1921 snprintf(buf
, sizeof(buf
),
1922 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
1923 GDB_SIGNAL_TRAP
, env
->cpu_index
+1, type
,
1924 env
->watchpoint_hit
->vaddr
);
1926 env
->watchpoint_hit
= NULL
;
1930 ret
= GDB_SIGNAL_TRAP
;
1931 } else if (reason
== EXCP_INTERRUPT
) {
1932 ret
= GDB_SIGNAL_INT
;
1936 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, env
->cpu_index
+1);
1941 /* Send a gdb syscall request.
1942 This accepts limited printf-style format specifiers, specifically:
1943 %x - target_ulong argument printed in hex.
1944 %lx - 64-bit argument printed in hex.
1945 %s - string pointer (target_ulong) and length (int) pair. */
1946 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
1955 s
= gdbserver_state
;
1958 gdb_current_syscall_cb
= cb
;
1959 s
->state
= RS_SYSCALL
;
1960 #ifndef CONFIG_USER_ONLY
1961 vm_stop(EXCP_DEBUG
);
1972 addr
= va_arg(va
, target_ulong
);
1973 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
, addr
);
1976 if (*(fmt
++) != 'x')
1978 i64
= va_arg(va
, uint64_t);
1979 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, "%" PRIx64
, i64
);
1982 addr
= va_arg(va
, target_ulong
);
1983 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
"/%x",
1984 addr
, va_arg(va
, int));
1988 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1999 #ifdef CONFIG_USER_ONLY
2000 gdb_handlesig(s
->c_cpu
, 0);
2002 cpu_interrupt(s
->c_cpu
, CPU_INTERRUPT_EXIT
);
2006 static void gdb_read_byte(GDBState
*s
, int ch
)
2011 #ifndef CONFIG_USER_ONLY
2012 if (s
->last_packet_len
) {
2013 /* Waiting for a response to the last packet. If we see the start
2014 of a new command then abandon the previous response. */
2017 printf("Got NACK, retransmitting\n");
2019 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2023 printf("Got ACK\n");
2025 printf("Got '%c' when expecting ACK/NACK\n", ch
);
2027 if (ch
== '+' || ch
== '$')
2028 s
->last_packet_len
= 0;
2033 /* when the CPU is running, we cannot do anything except stop
2034 it when receiving a char */
2035 vm_stop(EXCP_INTERRUPT
);
2042 s
->line_buf_index
= 0;
2043 s
->state
= RS_GETLINE
;
2048 s
->state
= RS_CHKSUM1
;
2049 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2052 s
->line_buf
[s
->line_buf_index
++] = ch
;
2056 s
->line_buf
[s
->line_buf_index
] = '\0';
2057 s
->line_csum
= fromhex(ch
) << 4;
2058 s
->state
= RS_CHKSUM2
;
2061 s
->line_csum
|= fromhex(ch
);
2063 for(i
= 0; i
< s
->line_buf_index
; i
++) {
2064 csum
+= s
->line_buf
[i
];
2066 if (s
->line_csum
!= (csum
& 0xff)) {
2068 put_buffer(s
, &reply
, 1);
2072 put_buffer(s
, &reply
, 1);
2073 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2082 #ifdef CONFIG_USER_ONLY
2088 s
= gdbserver_state
;
2090 if (gdbserver_fd
< 0 || s
->fd
< 0)
2097 gdb_handlesig (CPUState
*env
, int sig
)
2103 s
= gdbserver_state
;
2104 if (gdbserver_fd
< 0 || s
->fd
< 0)
2107 /* disable single step if it was enabled */
2108 cpu_single_step(env
, 0);
2113 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb (sig
));
2116 /* put_packet() might have detected that the peer terminated the
2123 s
->running_state
= 0;
2124 while (s
->running_state
== 0) {
2125 n
= read (s
->fd
, buf
, 256);
2130 for (i
= 0; i
< n
; i
++)
2131 gdb_read_byte (s
, buf
[i
]);
2133 else if (n
== 0 || errno
!= EAGAIN
)
2135 /* XXX: Connection closed. Should probably wait for annother
2136 connection before continuing. */
2145 /* Tell the remote gdb that the process has exited. */
2146 void gdb_exit(CPUState
*env
, int code
)
2151 s
= gdbserver_state
;
2152 if (gdbserver_fd
< 0 || s
->fd
< 0)
2155 snprintf(buf
, sizeof(buf
), "W%02x", code
);
2159 /* Tell the remote gdb that the process has exited due to SIG. */
2160 void gdb_signalled(CPUState
*env
, int sig
)
2165 s
= gdbserver_state
;
2166 if (gdbserver_fd
< 0 || s
->fd
< 0)
2169 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb (sig
));
2173 static void gdb_accept(void)
2176 struct sockaddr_in sockaddr
;
2181 len
= sizeof(sockaddr
);
2182 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2183 if (fd
< 0 && errno
!= EINTR
) {
2186 } else if (fd
>= 0) {
2191 /* set short latency */
2193 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2195 s
= qemu_mallocz(sizeof(GDBState
));
2202 memset (s
, 0, sizeof (GDBState
));
2203 s
->c_cpu
= first_cpu
;
2204 s
->g_cpu
= first_cpu
;
2208 gdbserver_state
= s
;
2210 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2213 static int gdbserver_open(int port
)
2215 struct sockaddr_in sockaddr
;
2218 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2224 /* allow fast reuse */
2226 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
2228 sockaddr
.sin_family
= AF_INET
;
2229 sockaddr
.sin_port
= htons(port
);
2230 sockaddr
.sin_addr
.s_addr
= 0;
2231 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2236 ret
= listen(fd
, 0);
2244 int gdbserver_start(int port
)
2246 gdbserver_fd
= gdbserver_open(port
);
2247 if (gdbserver_fd
< 0)
2249 /* accept connections */
2254 /* Disable gdb stub for child processes. */
2255 void gdbserver_fork(CPUState
*env
)
2257 GDBState
*s
= gdbserver_state
;
2262 cpu_breakpoint_remove_all(env
, BP_GDB
);
2263 cpu_watchpoint_remove_all(env
, BP_GDB
);
2266 static int gdb_chr_can_receive(void *opaque
)
2268 /* We can handle an arbitrarily large amount of data.
2269 Pick the maximum packet size, which is as good as anything. */
2270 return MAX_PACKET_LENGTH
;
2273 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2277 for (i
= 0; i
< size
; i
++) {
2278 gdb_read_byte(gdbserver_state
, buf
[i
]);
2282 static void gdb_chr_event(void *opaque
, int event
)
2285 case CHR_EVENT_RESET
:
2286 vm_stop(EXCP_INTERRUPT
);
2294 int gdbserver_start(const char *port
)
2297 char gdbstub_port_name
[128];
2300 CharDriverState
*chr
;
2302 if (!port
|| !*port
)
2305 port_num
= strtol(port
, &p
, 10);
2307 /* A numeric value is interpreted as a port number. */
2308 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
2309 "tcp::%d,nowait,nodelay,server", port_num
);
2310 port
= gdbstub_port_name
;
2313 chr
= qemu_chr_open("gdb", port
);
2317 s
= qemu_mallocz(sizeof(GDBState
));
2321 s
->c_cpu
= first_cpu
;
2322 s
->g_cpu
= first_cpu
;
2324 gdbserver_state
= s
;
2325 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2326 gdb_chr_event
, NULL
);
2327 qemu_add_vm_stop_handler(gdb_vm_stopped
, NULL
);