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, see <http://www.gnu.org/licenses/>.
20 #include "qemu-common.h"
21 #ifdef CONFIG_USER_ONLY
33 #include "qemu-char.h"
39 #define MAX_PACKET_LENGTH 4096
42 #include "qemu_socket.h"
50 GDB_SIGNAL_UNKNOWN
= 143
53 #ifdef CONFIG_USER_ONLY
55 /* Map target signal numbers to GDB protocol signal numbers and vice
56 * versa. For user emulation's currently supported systems, we can
57 * assume most signals are defined.
60 static int gdb_signal_table
[] = {
220 /* In system mode we only need SIGINT and SIGTRAP; other signals
221 are not yet supported. */
228 static int gdb_signal_table
[] = {
238 #ifdef CONFIG_USER_ONLY
239 static int target_signal_to_gdb (int sig
)
242 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
243 if (gdb_signal_table
[i
] == sig
)
245 return GDB_SIGNAL_UNKNOWN
;
249 static int gdb_signal_to_target (int sig
)
251 if (sig
< ARRAY_SIZE (gdb_signal_table
))
252 return gdb_signal_table
[sig
];
259 typedef struct GDBRegisterState
{
265 struct GDBRegisterState
*next
;
276 typedef struct GDBState
{
277 CPUState
*c_cpu
; /* current CPU for step/continue ops */
278 CPUState
*g_cpu
; /* current CPU for other ops */
279 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
280 enum RSState state
; /* parsing state */
281 char line_buf
[MAX_PACKET_LENGTH
];
284 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
287 #ifdef CONFIG_USER_ONLY
291 CharDriverState
*chr
;
292 CharDriverState
*mon_chr
;
296 /* By default use no IRQs and no timers while single stepping so as to
297 * make single stepping like an ICE HW step.
299 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
301 static GDBState
*gdbserver_state
;
303 /* This is an ugly hack to cope with both new and old gdb.
304 If gdb sends qXfer:features:read then assume we're talking to a newish
305 gdb that understands target descriptions. */
306 static int gdb_has_xml
;
308 #ifdef CONFIG_USER_ONLY
309 /* XXX: This is not thread safe. Do we care? */
310 static int gdbserver_fd
= -1;
312 static int get_char(GDBState
*s
)
318 ret
= recv(s
->fd
, &ch
, 1, 0);
320 if (errno
== ECONNRESET
)
322 if (errno
!= EINTR
&& errno
!= EAGAIN
)
324 } else if (ret
== 0) {
336 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
344 /* If gdb is connected when the first semihosting syscall occurs then use
345 remote gdb syscalls. Otherwise use native file IO. */
346 int use_gdb_syscalls(void)
348 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
349 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
352 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
355 /* Resume execution. */
356 static inline void gdb_continue(GDBState
*s
)
358 #ifdef CONFIG_USER_ONLY
359 s
->running_state
= 1;
365 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
367 #ifdef CONFIG_USER_ONLY
371 ret
= send(s
->fd
, buf
, len
, 0);
373 if (errno
!= EINTR
&& errno
!= EAGAIN
)
381 qemu_chr_write(s
->chr
, buf
, len
);
385 static inline int fromhex(int v
)
387 if (v
>= '0' && v
<= '9')
389 else if (v
>= 'A' && v
<= 'F')
391 else if (v
>= 'a' && v
<= 'f')
397 static inline int tohex(int v
)
405 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
410 for(i
= 0; i
< len
; i
++) {
412 *q
++ = tohex(c
>> 4);
413 *q
++ = tohex(c
& 0xf);
418 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
422 for(i
= 0; i
< len
; i
++) {
423 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
428 /* return -1 if error, 0 if OK */
429 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
)
440 for(i
= 0; i
< len
; i
++) {
444 *(p
++) = tohex((csum
>> 4) & 0xf);
445 *(p
++) = tohex((csum
) & 0xf);
447 s
->last_packet_len
= p
- s
->last_packet
;
448 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
450 #ifdef CONFIG_USER_ONLY
463 /* return -1 if error, 0 if OK */
464 static int put_packet(GDBState
*s
, const char *buf
)
467 printf("reply='%s'\n", buf
);
470 return put_packet_binary(s
, buf
, strlen(buf
));
473 /* The GDB remote protocol transfers values in target byte order. This means
474 we can use the raw memory access routines to access the value buffer.
475 Conveniently, these also handle the case where the buffer is mis-aligned.
477 #define GET_REG8(val) do { \
478 stb_p(mem_buf, val); \
481 #define GET_REG16(val) do { \
482 stw_p(mem_buf, val); \
485 #define GET_REG32(val) do { \
486 stl_p(mem_buf, val); \
489 #define GET_REG64(val) do { \
490 stq_p(mem_buf, val); \
494 #if TARGET_LONG_BITS == 64
495 #define GET_REGL(val) GET_REG64(val)
496 #define ldtul_p(addr) ldq_p(addr)
498 #define GET_REGL(val) GET_REG32(val)
499 #define ldtul_p(addr) ldl_p(addr)
502 #if defined(TARGET_I386)
505 static const int gpr_map
[16] = {
506 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
507 8, 9, 10, 11, 12, 13, 14, 15
510 #define gpr_map gpr_map32
512 static const int gpr_map32
[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
514 #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
516 #define IDX_IP_REG CPU_NB_REGS
517 #define IDX_FLAGS_REG (IDX_IP_REG + 1)
518 #define IDX_SEG_REGS (IDX_FLAGS_REG + 1)
519 #define IDX_FP_REGS (IDX_SEG_REGS + 6)
520 #define IDX_XMM_REGS (IDX_FP_REGS + 16)
521 #define IDX_MXCSR_REG (IDX_XMM_REGS + CPU_NB_REGS)
523 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
525 if (n
< CPU_NB_REGS
) {
526 if (TARGET_LONG_BITS
== 64 && env
->hflags
& HF_CS64_MASK
) {
527 GET_REG64(env
->regs
[gpr_map
[n
]]);
528 } else if (n
< CPU_NB_REGS32
) {
529 GET_REG32(env
->regs
[gpr_map32
[n
]]);
531 } else if (n
>= IDX_FP_REGS
&& n
< IDX_FP_REGS
+ 8) {
532 #ifdef USE_X86LDOUBLE
533 /* FIXME: byteswap float values - after fixing fpregs layout. */
534 memcpy(mem_buf
, &env
->fpregs
[n
- IDX_FP_REGS
], 10);
536 memset(mem_buf
, 0, 10);
539 } else if (n
>= IDX_XMM_REGS
&& n
< IDX_XMM_REGS
+ CPU_NB_REGS
) {
541 if (n
< CPU_NB_REGS32
||
542 (TARGET_LONG_BITS
== 64 && env
->hflags
& HF_CS64_MASK
)) {
543 stq_p(mem_buf
, env
->xmm_regs
[n
].XMM_Q(0));
544 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].XMM_Q(1));
550 if (TARGET_LONG_BITS
== 64 && env
->hflags
& HF_CS64_MASK
) {
555 case IDX_FLAGS_REG
: GET_REG32(env
->eflags
);
557 case IDX_SEG_REGS
: GET_REG32(env
->segs
[R_CS
].selector
);
558 case IDX_SEG_REGS
+ 1: GET_REG32(env
->segs
[R_SS
].selector
);
559 case IDX_SEG_REGS
+ 2: GET_REG32(env
->segs
[R_DS
].selector
);
560 case IDX_SEG_REGS
+ 3: GET_REG32(env
->segs
[R_ES
].selector
);
561 case IDX_SEG_REGS
+ 4: GET_REG32(env
->segs
[R_FS
].selector
);
562 case IDX_SEG_REGS
+ 5: GET_REG32(env
->segs
[R_GS
].selector
);
564 case IDX_FP_REGS
+ 8: GET_REG32(env
->fpuc
);
565 case IDX_FP_REGS
+ 9: GET_REG32((env
->fpus
& ~0x3800) |
566 (env
->fpstt
& 0x7) << 11);
567 case IDX_FP_REGS
+ 10: GET_REG32(0); /* ftag */
568 case IDX_FP_REGS
+ 11: GET_REG32(0); /* fiseg */
569 case IDX_FP_REGS
+ 12: GET_REG32(0); /* fioff */
570 case IDX_FP_REGS
+ 13: GET_REG32(0); /* foseg */
571 case IDX_FP_REGS
+ 14: GET_REG32(0); /* fooff */
572 case IDX_FP_REGS
+ 15: GET_REG32(0); /* fop */
574 case IDX_MXCSR_REG
: GET_REG32(env
->mxcsr
);
580 static int cpu_x86_gdb_load_seg(CPUState
*env
, int sreg
, uint8_t *mem_buf
)
582 uint16_t selector
= ldl_p(mem_buf
);
584 if (selector
!= env
->segs
[sreg
].selector
) {
585 #if defined(CONFIG_USER_ONLY)
586 cpu_x86_load_seg(env
, sreg
, selector
);
588 unsigned int limit
, flags
;
591 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
)) {
592 base
= selector
<< 4;
596 if (!cpu_x86_get_descr_debug(env
, selector
, &base
, &limit
, &flags
))
599 cpu_x86_load_seg_cache(env
, sreg
, selector
, base
, limit
, flags
);
605 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
609 if (n
< CPU_NB_REGS
) {
610 if (TARGET_LONG_BITS
== 64 && env
->hflags
& HF_CS64_MASK
) {
611 env
->regs
[gpr_map
[n
]] = ldtul_p(mem_buf
);
612 return sizeof(target_ulong
);
613 } else if (n
< CPU_NB_REGS32
) {
615 env
->regs
[n
] &= ~0xffffffffUL
;
616 env
->regs
[n
] |= (uint32_t)ldl_p(mem_buf
);
619 } else if (n
>= IDX_FP_REGS
&& n
< IDX_FP_REGS
+ 8) {
620 #ifdef USE_X86LDOUBLE
621 /* FIXME: byteswap float values - after fixing fpregs layout. */
622 memcpy(&env
->fpregs
[n
- IDX_FP_REGS
], mem_buf
, 10);
625 } else if (n
>= IDX_XMM_REGS
&& n
< IDX_XMM_REGS
+ CPU_NB_REGS
) {
627 if (n
< CPU_NB_REGS32
||
628 (TARGET_LONG_BITS
== 64 && env
->hflags
& HF_CS64_MASK
)) {
629 env
->xmm_regs
[n
].XMM_Q(0) = ldq_p(mem_buf
);
630 env
->xmm_regs
[n
].XMM_Q(1) = ldq_p(mem_buf
+ 8);
636 if (TARGET_LONG_BITS
== 64 && env
->hflags
& HF_CS64_MASK
) {
637 env
->eip
= ldq_p(mem_buf
);
640 env
->eip
&= ~0xffffffffUL
;
641 env
->eip
|= (uint32_t)ldl_p(mem_buf
);
645 env
->eflags
= ldl_p(mem_buf
);
648 case IDX_SEG_REGS
: return cpu_x86_gdb_load_seg(env
, R_CS
, mem_buf
);
649 case IDX_SEG_REGS
+ 1: return cpu_x86_gdb_load_seg(env
, R_SS
, mem_buf
);
650 case IDX_SEG_REGS
+ 2: return cpu_x86_gdb_load_seg(env
, R_DS
, mem_buf
);
651 case IDX_SEG_REGS
+ 3: return cpu_x86_gdb_load_seg(env
, R_ES
, mem_buf
);
652 case IDX_SEG_REGS
+ 4: return cpu_x86_gdb_load_seg(env
, R_FS
, mem_buf
);
653 case IDX_SEG_REGS
+ 5: return cpu_x86_gdb_load_seg(env
, R_GS
, mem_buf
);
655 case IDX_FP_REGS
+ 8:
656 env
->fpuc
= ldl_p(mem_buf
);
658 case IDX_FP_REGS
+ 9:
659 tmp
= ldl_p(mem_buf
);
660 env
->fpstt
= (tmp
>> 11) & 7;
661 env
->fpus
= tmp
& ~0x3800;
663 case IDX_FP_REGS
+ 10: /* ftag */ return 4;
664 case IDX_FP_REGS
+ 11: /* fiseg */ return 4;
665 case IDX_FP_REGS
+ 12: /* fioff */ return 4;
666 case IDX_FP_REGS
+ 13: /* foseg */ return 4;
667 case IDX_FP_REGS
+ 14: /* fooff */ return 4;
668 case IDX_FP_REGS
+ 15: /* fop */ return 4;
671 env
->mxcsr
= ldl_p(mem_buf
);
675 /* Unrecognised register. */
679 #elif defined (TARGET_PPC)
681 /* Old gdb always expects FP registers. Newer (xml-aware) gdb only
682 expects whatever the target description contains. Due to a
683 historical mishap the FP registers appear in between core integer
684 regs and PC, MSR, CR, and so forth. We hack round this by giving the
685 FP regs zero size when talking to a newer gdb. */
686 #define NUM_CORE_REGS 71
687 #if defined (TARGET_PPC64)
688 #define GDB_CORE_XML "power64-core.xml"
690 #define GDB_CORE_XML "power-core.xml"
693 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
697 GET_REGL(env
->gpr
[n
]);
702 stfq_p(mem_buf
, env
->fpr
[n
-32]);
706 case 64: GET_REGL(env
->nip
);
707 case 65: GET_REGL(env
->msr
);
712 for (i
= 0; i
< 8; i
++)
713 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
716 case 67: GET_REGL(env
->lr
);
717 case 68: GET_REGL(env
->ctr
);
718 case 69: GET_REGL(env
->xer
);
723 GET_REG32(0); /* fpscr */
730 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
734 env
->gpr
[n
] = ldtul_p(mem_buf
);
735 return sizeof(target_ulong
);
740 env
->fpr
[n
-32] = ldfq_p(mem_buf
);
745 env
->nip
= ldtul_p(mem_buf
);
746 return sizeof(target_ulong
);
748 ppc_store_msr(env
, ldtul_p(mem_buf
));
749 return sizeof(target_ulong
);
752 uint32_t cr
= ldl_p(mem_buf
);
754 for (i
= 0; i
< 8; i
++)
755 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
759 env
->lr
= ldtul_p(mem_buf
);
760 return sizeof(target_ulong
);
762 env
->ctr
= ldtul_p(mem_buf
);
763 return sizeof(target_ulong
);
765 env
->xer
= ldtul_p(mem_buf
);
766 return sizeof(target_ulong
);
777 #elif defined (TARGET_SPARC)
779 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
780 #define NUM_CORE_REGS 86
782 #define NUM_CORE_REGS 72
786 #define GET_REGA(val) GET_REG32(val)
788 #define GET_REGA(val) GET_REGL(val)
791 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
795 GET_REGA(env
->gregs
[n
]);
798 /* register window */
799 GET_REGA(env
->regwptr
[n
- 8]);
801 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
804 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
806 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
808 case 64: GET_REGA(env
->y
);
809 case 65: GET_REGA(cpu_get_psr(env
));
810 case 66: GET_REGA(env
->wim
);
811 case 67: GET_REGA(env
->tbr
);
812 case 68: GET_REGA(env
->pc
);
813 case 69: GET_REGA(env
->npc
);
814 case 70: GET_REGA(env
->fsr
);
815 case 71: GET_REGA(0); /* csr */
816 default: GET_REGA(0);
821 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
824 /* f32-f62 (double width, even numbers only) */
827 val
= (uint64_t)*((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) << 32;
828 val
|= *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]);
832 case 80: GET_REGL(env
->pc
);
833 case 81: GET_REGL(env
->npc
);
834 case 82: GET_REGL((cpu_get_ccr(env
) << 32) |
835 ((env
->asi
& 0xff) << 24) |
836 ((env
->pstate
& 0xfff) << 8) |
838 case 83: GET_REGL(env
->fsr
);
839 case 84: GET_REGL(env
->fprs
);
840 case 85: GET_REGL(env
->y
);
846 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
848 #if defined(TARGET_ABI32)
851 tmp
= ldl_p(mem_buf
);
855 tmp
= ldtul_p(mem_buf
);
862 /* register window */
863 env
->regwptr
[n
- 8] = tmp
;
865 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
868 *((uint32_t *)&env
->fpr
[n
- 32]) = tmp
;
870 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
872 case 64: env
->y
= tmp
; break;
873 case 65: cpu_put_psr(env
, tmp
); break;
874 case 66: env
->wim
= tmp
; break;
875 case 67: env
->tbr
= tmp
; break;
876 case 68: env
->pc
= tmp
; break;
877 case 69: env
->npc
= tmp
; break;
878 case 70: env
->fsr
= tmp
; break;
886 env
->fpr
[n
] = ldfl_p(mem_buf
);
889 /* f32-f62 (double width, even numbers only) */
890 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) = tmp
>> 32;
891 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]) = tmp
;
894 case 80: env
->pc
= tmp
; break;
895 case 81: env
->npc
= tmp
; break;
897 cpu_put_ccr(env
, tmp
>> 32);
898 env
->asi
= (tmp
>> 24) & 0xff;
899 env
->pstate
= (tmp
>> 8) & 0xfff;
900 cpu_put_cwp64(env
, tmp
& 0xff);
902 case 83: env
->fsr
= tmp
; break;
903 case 84: env
->fprs
= tmp
; break;
904 case 85: env
->y
= tmp
; break;
911 #elif defined (TARGET_ARM)
913 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
914 whatever the target description contains. Due to a historical mishap
915 the FPA registers appear in between core integer regs and the CPSR.
916 We hack round this by giving the FPA regs zero size when talking to a
918 #define NUM_CORE_REGS 26
919 #define GDB_CORE_XML "arm-core.xml"
921 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
924 /* Core integer register. */
925 GET_REG32(env
->regs
[n
]);
931 memset(mem_buf
, 0, 12);
936 /* FPA status register. */
942 GET_REG32(cpsr_read(env
));
944 /* Unknown register. */
948 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
952 tmp
= ldl_p(mem_buf
);
954 /* Mask out low bit of PC to workaround gdb bugs. This will probably
955 cause problems if we ever implement the Jazelle DBX extensions. */
960 /* Core integer register. */
964 if (n
< 24) { /* 16-23 */
965 /* FPA registers (ignored). */
972 /* FPA status register (ignored). */
978 cpsr_write (env
, tmp
, 0xffffffff);
981 /* Unknown register. */
985 #elif defined (TARGET_M68K)
987 #define NUM_CORE_REGS 18
989 #define GDB_CORE_XML "cf-core.xml"
991 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
995 GET_REG32(env
->dregs
[n
]);
998 GET_REG32(env
->aregs
[n
- 8]);
1001 case 16: GET_REG32(env
->sr
);
1002 case 17: GET_REG32(env
->pc
);
1005 /* FP registers not included here because they vary between
1006 ColdFire and m68k. Use XML bits for these. */
1010 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1014 tmp
= ldl_p(mem_buf
);
1018 env
->dregs
[n
] = tmp
;
1019 } else if (n
< 16) {
1021 env
->aregs
[n
- 8] = tmp
;
1024 case 16: env
->sr
= tmp
; break;
1025 case 17: env
->pc
= tmp
; break;
1031 #elif defined (TARGET_MIPS)
1033 #define NUM_CORE_REGS 73
1035 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1038 GET_REGL(env
->active_tc
.gpr
[n
]);
1040 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
1041 if (n
>= 38 && n
< 70) {
1042 if (env
->CP0_Status
& (1 << CP0St_FR
))
1043 GET_REGL(env
->active_fpu
.fpr
[n
- 38].d
);
1045 GET_REGL(env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
]);
1048 case 70: GET_REGL((int32_t)env
->active_fpu
.fcr31
);
1049 case 71: GET_REGL((int32_t)env
->active_fpu
.fcr0
);
1053 case 32: GET_REGL((int32_t)env
->CP0_Status
);
1054 case 33: GET_REGL(env
->active_tc
.LO
[0]);
1055 case 34: GET_REGL(env
->active_tc
.HI
[0]);
1056 case 35: GET_REGL(env
->CP0_BadVAddr
);
1057 case 36: GET_REGL((int32_t)env
->CP0_Cause
);
1058 case 37: GET_REGL(env
->active_tc
.PC
| !!(env
->hflags
& MIPS_HFLAG_M16
));
1059 case 72: GET_REGL(0); /* fp */
1060 case 89: GET_REGL((int32_t)env
->CP0_PRid
);
1062 if (n
>= 73 && n
<= 88) {
1063 /* 16 embedded regs. */
1070 /* convert MIPS rounding mode in FCR31 to IEEE library */
1071 static unsigned int ieee_rm
[] =
1073 float_round_nearest_even
,
1074 float_round_to_zero
,
1078 #define RESTORE_ROUNDING_MODE \
1079 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
1081 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1085 tmp
= ldtul_p(mem_buf
);
1088 env
->active_tc
.gpr
[n
] = tmp
;
1089 return sizeof(target_ulong
);
1091 if (env
->CP0_Config1
& (1 << CP0C1_FP
)
1092 && n
>= 38 && n
< 73) {
1094 if (env
->CP0_Status
& (1 << CP0St_FR
))
1095 env
->active_fpu
.fpr
[n
- 38].d
= tmp
;
1097 env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
] = tmp
;
1101 env
->active_fpu
.fcr31
= tmp
& 0xFF83FFFF;
1102 /* set rounding mode */
1103 RESTORE_ROUNDING_MODE
;
1104 #ifndef CONFIG_SOFTFLOAT
1105 /* no floating point exception for native float */
1106 SET_FP_ENABLE(env
->active_fpu
.fcr31
, 0);
1109 case 71: env
->active_fpu
.fcr0
= tmp
; break;
1111 return sizeof(target_ulong
);
1114 case 32: env
->CP0_Status
= tmp
; break;
1115 case 33: env
->active_tc
.LO
[0] = tmp
; break;
1116 case 34: env
->active_tc
.HI
[0] = tmp
; break;
1117 case 35: env
->CP0_BadVAddr
= tmp
; break;
1118 case 36: env
->CP0_Cause
= tmp
; break;
1120 env
->active_tc
.PC
= tmp
& ~(target_ulong
)1;
1122 env
->hflags
|= MIPS_HFLAG_M16
;
1124 env
->hflags
&= ~(MIPS_HFLAG_M16
);
1127 case 72: /* fp, ignored */ break;
1131 /* Other registers are readonly. Ignore writes. */
1135 return sizeof(target_ulong
);
1137 #elif defined (TARGET_SH4)
1139 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1140 /* FIXME: We should use XML for this. */
1142 #define NUM_CORE_REGS 59
1144 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1147 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1148 GET_REGL(env
->gregs
[n
+ 16]);
1150 GET_REGL(env
->gregs
[n
]);
1152 } else if (n
< 16) {
1153 GET_REGL(env
->gregs
[n
]);
1154 } else if (n
>= 25 && n
< 41) {
1155 GET_REGL(env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
1156 } else if (n
>= 43 && n
< 51) {
1157 GET_REGL(env
->gregs
[n
- 43]);
1158 } else if (n
>= 51 && n
< 59) {
1159 GET_REGL(env
->gregs
[n
- (51 - 16)]);
1162 case 16: GET_REGL(env
->pc
);
1163 case 17: GET_REGL(env
->pr
);
1164 case 18: GET_REGL(env
->gbr
);
1165 case 19: GET_REGL(env
->vbr
);
1166 case 20: GET_REGL(env
->mach
);
1167 case 21: GET_REGL(env
->macl
);
1168 case 22: GET_REGL(env
->sr
);
1169 case 23: GET_REGL(env
->fpul
);
1170 case 24: GET_REGL(env
->fpscr
);
1171 case 41: GET_REGL(env
->ssr
);
1172 case 42: GET_REGL(env
->spc
);
1178 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1182 tmp
= ldl_p(mem_buf
);
1185 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1186 env
->gregs
[n
+ 16] = tmp
;
1188 env
->gregs
[n
] = tmp
;
1191 } else if (n
< 16) {
1192 env
->gregs
[n
] = tmp
;
1194 } else if (n
>= 25 && n
< 41) {
1195 env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)] = tmp
;
1197 } else if (n
>= 43 && n
< 51) {
1198 env
->gregs
[n
- 43] = tmp
;
1200 } else if (n
>= 51 && n
< 59) {
1201 env
->gregs
[n
- (51 - 16)] = tmp
;
1205 case 16: env
->pc
= tmp
; break;
1206 case 17: env
->pr
= tmp
; break;
1207 case 18: env
->gbr
= tmp
; break;
1208 case 19: env
->vbr
= tmp
; break;
1209 case 20: env
->mach
= tmp
; break;
1210 case 21: env
->macl
= tmp
; break;
1211 case 22: env
->sr
= tmp
; break;
1212 case 23: env
->fpul
= tmp
; break;
1213 case 24: env
->fpscr
= tmp
; break;
1214 case 41: env
->ssr
= tmp
; break;
1215 case 42: env
->spc
= tmp
; break;
1221 #elif defined (TARGET_MICROBLAZE)
1223 #define NUM_CORE_REGS (32 + 5)
1225 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1228 GET_REG32(env
->regs
[n
]);
1230 GET_REG32(env
->sregs
[n
- 32]);
1235 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1239 if (n
> NUM_CORE_REGS
)
1242 tmp
= ldl_p(mem_buf
);
1247 env
->sregs
[n
- 32] = tmp
;
1251 #elif defined (TARGET_CRIS)
1253 #define NUM_CORE_REGS 49
1256 read_register_crisv10(CPUState
*env
, uint8_t *mem_buf
, int n
)
1259 GET_REG32(env
->regs
[n
]);
1269 GET_REG8(env
->pregs
[n
- 16]);
1272 GET_REG8(env
->pregs
[n
- 16]);
1276 GET_REG16(env
->pregs
[n
- 16]);
1280 GET_REG32(env
->pregs
[n
- 16]);
1288 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1292 if (env
->pregs
[PR_VR
] < 32)
1293 return read_register_crisv10(env
, mem_buf
, n
);
1295 srs
= env
->pregs
[PR_SRS
];
1297 GET_REG32(env
->regs
[n
]);
1300 if (n
>= 21 && n
< 32) {
1301 GET_REG32(env
->pregs
[n
- 16]);
1303 if (n
>= 33 && n
< 49) {
1304 GET_REG32(env
->sregs
[srs
][n
- 33]);
1307 case 16: GET_REG8(env
->pregs
[0]);
1308 case 17: GET_REG8(env
->pregs
[1]);
1309 case 18: GET_REG32(env
->pregs
[2]);
1310 case 19: GET_REG8(srs
);
1311 case 20: GET_REG16(env
->pregs
[4]);
1312 case 32: GET_REG32(env
->pc
);
1318 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1325 tmp
= ldl_p(mem_buf
);
1331 if (n
>= 21 && n
< 32) {
1332 env
->pregs
[n
- 16] = tmp
;
1335 /* FIXME: Should support function regs be writable? */
1339 case 18: env
->pregs
[PR_PID
] = tmp
; break;
1342 case 32: env
->pc
= tmp
; break;
1347 #elif defined (TARGET_ALPHA)
1349 #define NUM_CORE_REGS 67
1351 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1361 d
.d
= env
->fir
[n
- 32];
1365 val
= cpu_alpha_load_fpcr(env
);
1375 /* 31 really is the zero register; 65 is unassigned in the
1376 gdb protocol, but is still required to occupy 8 bytes. */
1385 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1387 target_ulong tmp
= ldtul_p(mem_buf
);
1396 env
->fir
[n
- 32] = d
.d
;
1399 cpu_alpha_store_fpcr(env
, tmp
);
1409 /* 31 really is the zero register; 65 is unassigned in the
1410 gdb protocol, but is still required to occupy 8 bytes. */
1417 #elif defined (TARGET_S390X)
1419 #define NUM_CORE_REGS S390_NUM_TOTAL_REGS
1421 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1424 case S390_PSWM_REGNUM
: GET_REGL(env
->psw
.mask
); break;
1425 case S390_PSWA_REGNUM
: GET_REGL(env
->psw
.addr
); break;
1426 case S390_R0_REGNUM
... S390_R15_REGNUM
:
1427 GET_REGL(env
->regs
[n
-S390_R0_REGNUM
]); break;
1428 case S390_A0_REGNUM
... S390_A15_REGNUM
:
1429 GET_REG32(env
->aregs
[n
-S390_A0_REGNUM
]); break;
1430 case S390_FPC_REGNUM
: GET_REG32(env
->fpc
); break;
1431 case S390_F0_REGNUM
... S390_F15_REGNUM
:
1434 case S390_PC_REGNUM
: GET_REGL(env
->psw
.addr
); break;
1435 case S390_CC_REGNUM
: GET_REG32(env
->cc
); break;
1441 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1446 tmpl
= ldtul_p(mem_buf
);
1447 tmp32
= ldl_p(mem_buf
);
1450 case S390_PSWM_REGNUM
: env
->psw
.mask
= tmpl
; break;
1451 case S390_PSWA_REGNUM
: env
->psw
.addr
= tmpl
; break;
1452 case S390_R0_REGNUM
... S390_R15_REGNUM
:
1453 env
->regs
[n
-S390_R0_REGNUM
] = tmpl
; break;
1454 case S390_A0_REGNUM
... S390_A15_REGNUM
:
1455 env
->aregs
[n
-S390_A0_REGNUM
] = tmp32
; r
=4; break;
1456 case S390_FPC_REGNUM
: env
->fpc
= tmp32
; r
=4; break;
1457 case S390_F0_REGNUM
... S390_F15_REGNUM
:
1460 case S390_PC_REGNUM
: env
->psw
.addr
= tmpl
; break;
1461 case S390_CC_REGNUM
: env
->cc
= tmp32
; r
=4; break;
1468 #define NUM_CORE_REGS 0
1470 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1475 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1482 static int num_g_regs
= NUM_CORE_REGS
;
1485 /* Encode data using the encoding for 'x' packets. */
1486 static int memtox(char *buf
, const char *mem
, int len
)
1494 case '#': case '$': case '*': case '}':
1506 static const char *get_feature_xml(const char *p
, const char **newp
)
1511 static char target_xml
[1024];
1514 while (p
[len
] && p
[len
] != ':')
1519 if (strncmp(p
, "target.xml", len
) == 0) {
1520 /* Generate the XML description for this CPU. */
1521 if (!target_xml
[0]) {
1522 GDBRegisterState
*r
;
1524 snprintf(target_xml
, sizeof(target_xml
),
1525 "<?xml version=\"1.0\"?>"
1526 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1528 "<xi:include href=\"%s\"/>",
1531 for (r
= first_cpu
->gdb_regs
; r
; r
= r
->next
) {
1532 pstrcat(target_xml
, sizeof(target_xml
), "<xi:include href=\"");
1533 pstrcat(target_xml
, sizeof(target_xml
), r
->xml
);
1534 pstrcat(target_xml
, sizeof(target_xml
), "\"/>");
1536 pstrcat(target_xml
, sizeof(target_xml
), "</target>");
1540 for (i
= 0; ; i
++) {
1541 name
= xml_builtin
[i
][0];
1542 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
1545 return name
? xml_builtin
[i
][1] : NULL
;
1549 static int gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1551 GDBRegisterState
*r
;
1553 if (reg
< NUM_CORE_REGS
)
1554 return cpu_gdb_read_register(env
, mem_buf
, reg
);
1556 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1557 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1558 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
1564 static int gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1566 GDBRegisterState
*r
;
1568 if (reg
< NUM_CORE_REGS
)
1569 return cpu_gdb_write_register(env
, mem_buf
, reg
);
1571 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1572 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1573 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
1579 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
1580 specifies the first register number and these registers are included in
1581 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1582 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1585 void gdb_register_coprocessor(CPUState
* env
,
1586 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
1587 int num_regs
, const char *xml
, int g_pos
)
1589 GDBRegisterState
*s
;
1590 GDBRegisterState
**p
;
1591 static int last_reg
= NUM_CORE_REGS
;
1593 s
= (GDBRegisterState
*)qemu_mallocz(sizeof(GDBRegisterState
));
1594 s
->base_reg
= last_reg
;
1595 s
->num_regs
= num_regs
;
1596 s
->get_reg
= get_reg
;
1597 s
->set_reg
= set_reg
;
1601 /* Check for duplicates. */
1602 if (strcmp((*p
)->xml
, xml
) == 0)
1606 /* Add to end of list. */
1607 last_reg
+= num_regs
;
1610 if (g_pos
!= s
->base_reg
) {
1611 fprintf(stderr
, "Error: Bad gdb register numbering for '%s'\n"
1612 "Expected %d got %d\n", xml
, g_pos
, s
->base_reg
);
1614 num_g_regs
= last_reg
;
1619 #ifndef CONFIG_USER_ONLY
1620 static const int xlat_gdb_type
[] = {
1621 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
1622 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
1623 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
1627 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
1633 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1636 case GDB_BREAKPOINT_SW
:
1637 case GDB_BREAKPOINT_HW
:
1638 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1639 err
= cpu_breakpoint_insert(env
, addr
, BP_GDB
, NULL
);
1644 #ifndef CONFIG_USER_ONLY
1645 case GDB_WATCHPOINT_WRITE
:
1646 case GDB_WATCHPOINT_READ
:
1647 case GDB_WATCHPOINT_ACCESS
:
1648 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1649 err
= cpu_watchpoint_insert(env
, addr
, len
, xlat_gdb_type
[type
],
1661 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1667 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1670 case GDB_BREAKPOINT_SW
:
1671 case GDB_BREAKPOINT_HW
:
1672 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1673 err
= cpu_breakpoint_remove(env
, addr
, BP_GDB
);
1678 #ifndef CONFIG_USER_ONLY
1679 case GDB_WATCHPOINT_WRITE
:
1680 case GDB_WATCHPOINT_READ
:
1681 case GDB_WATCHPOINT_ACCESS
:
1682 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1683 err
= cpu_watchpoint_remove(env
, addr
, len
, xlat_gdb_type
[type
]);
1694 static void gdb_breakpoint_remove_all(void)
1698 if (kvm_enabled()) {
1699 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1703 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1704 cpu_breakpoint_remove_all(env
, BP_GDB
);
1705 #ifndef CONFIG_USER_ONLY
1706 cpu_watchpoint_remove_all(env
, BP_GDB
);
1711 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
1713 #if defined(TARGET_I386)
1714 cpu_synchronize_state(s
->c_cpu
);
1716 #elif defined (TARGET_PPC)
1718 #elif defined (TARGET_SPARC)
1720 s
->c_cpu
->npc
= pc
+ 4;
1721 #elif defined (TARGET_ARM)
1722 s
->c_cpu
->regs
[15] = pc
;
1723 #elif defined (TARGET_SH4)
1725 #elif defined (TARGET_MIPS)
1726 s
->c_cpu
->active_tc
.PC
= pc
& ~(target_ulong
)1;
1728 s
->c_cpu
->hflags
|= MIPS_HFLAG_M16
;
1730 s
->c_cpu
->hflags
&= ~(MIPS_HFLAG_M16
);
1732 #elif defined (TARGET_MICROBLAZE)
1733 s
->c_cpu
->sregs
[SR_PC
] = pc
;
1734 #elif defined (TARGET_CRIS)
1736 #elif defined (TARGET_ALPHA)
1738 #elif defined (TARGET_S390X)
1739 cpu_synchronize_state(s
->c_cpu
);
1740 s
->c_cpu
->psw
.addr
= pc
;
1744 static inline int gdb_id(CPUState
*env
)
1746 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_USE_NPTL)
1747 return env
->host_tid
;
1749 return env
->cpu_index
+ 1;
1753 static CPUState
*find_cpu(uint32_t thread_id
)
1757 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1758 if (gdb_id(env
) == thread_id
) {
1766 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1771 int ch
, reg_size
, type
, res
;
1772 char buf
[MAX_PACKET_LENGTH
];
1773 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1775 target_ulong addr
, len
;
1778 printf("command='%s'\n", line_buf
);
1784 /* TODO: Make this return the correct value for user-mode. */
1785 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
1788 /* Remove all the breakpoints when this query is issued,
1789 * because gdb is doing and initial connect and the state
1790 * should be cleaned up.
1792 gdb_breakpoint_remove_all();
1796 addr
= strtoull(p
, (char **)&p
, 16);
1797 gdb_set_cpu_pc(s
, addr
);
1803 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1804 if (s
->signal
== -1)
1809 if (strncmp(p
, "Cont", 4) == 0) {
1810 int res_signal
, res_thread
;
1814 put_packet(s
, "vCont;c;C;s;S");
1829 if (action
== 'C' || action
== 'S') {
1830 signal
= strtoul(p
, (char **)&p
, 16);
1831 } else if (action
!= 'c' && action
!= 's') {
1837 thread
= strtoull(p
+1, (char **)&p
, 16);
1839 action
= tolower(action
);
1840 if (res
== 0 || (res
== 'c' && action
== 's')) {
1842 res_signal
= signal
;
1843 res_thread
= thread
;
1847 if (res_thread
!= -1 && res_thread
!= 0) {
1848 env
= find_cpu(res_thread
);
1850 put_packet(s
, "E22");
1856 cpu_single_step(s
->c_cpu
, sstep_flags
);
1858 s
->signal
= res_signal
;
1864 goto unknown_command
;
1867 /* Kill the target */
1868 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1872 gdb_breakpoint_remove_all();
1873 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1875 put_packet(s
, "OK");
1879 addr
= strtoull(p
, (char **)&p
, 16);
1880 gdb_set_cpu_pc(s
, addr
);
1882 cpu_single_step(s
->c_cpu
, sstep_flags
);
1890 ret
= strtoull(p
, (char **)&p
, 16);
1893 err
= strtoull(p
, (char **)&p
, 16);
1900 if (gdb_current_syscall_cb
)
1901 gdb_current_syscall_cb(s
->c_cpu
, ret
, err
);
1903 put_packet(s
, "T02");
1910 cpu_synchronize_state(s
->g_cpu
);
1912 for (addr
= 0; addr
< num_g_regs
; addr
++) {
1913 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1916 memtohex(buf
, mem_buf
, len
);
1920 cpu_synchronize_state(s
->g_cpu
);
1921 registers
= mem_buf
;
1922 len
= strlen(p
) / 2;
1923 hextomem((uint8_t *)registers
, p
, len
);
1924 for (addr
= 0; addr
< num_g_regs
&& len
> 0; addr
++) {
1925 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1927 registers
+= reg_size
;
1929 put_packet(s
, "OK");
1932 addr
= strtoull(p
, (char **)&p
, 16);
1935 len
= strtoull(p
, NULL
, 16);
1936 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 0) != 0) {
1937 put_packet (s
, "E14");
1939 memtohex(buf
, mem_buf
, len
);
1944 addr
= strtoull(p
, (char **)&p
, 16);
1947 len
= strtoull(p
, (char **)&p
, 16);
1950 hextomem(mem_buf
, p
, len
);
1951 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 1) != 0)
1952 put_packet(s
, "E14");
1954 put_packet(s
, "OK");
1957 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1958 This works, but can be very slow. Anything new enough to
1959 understand XML also knows how to use this properly. */
1961 goto unknown_command
;
1962 addr
= strtoull(p
, (char **)&p
, 16);
1963 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1965 memtohex(buf
, mem_buf
, reg_size
);
1968 put_packet(s
, "E14");
1973 goto unknown_command
;
1974 addr
= strtoull(p
, (char **)&p
, 16);
1977 reg_size
= strlen(p
) / 2;
1978 hextomem(mem_buf
, p
, reg_size
);
1979 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1980 put_packet(s
, "OK");
1984 type
= strtoul(p
, (char **)&p
, 16);
1987 addr
= strtoull(p
, (char **)&p
, 16);
1990 len
= strtoull(p
, (char **)&p
, 16);
1992 res
= gdb_breakpoint_insert(addr
, len
, type
);
1994 res
= gdb_breakpoint_remove(addr
, len
, type
);
1996 put_packet(s
, "OK");
1997 else if (res
== -ENOSYS
)
2000 put_packet(s
, "E22");
2004 thread
= strtoull(p
, (char **)&p
, 16);
2005 if (thread
== -1 || thread
== 0) {
2006 put_packet(s
, "OK");
2009 env
= find_cpu(thread
);
2011 put_packet(s
, "E22");
2017 put_packet(s
, "OK");
2021 put_packet(s
, "OK");
2024 put_packet(s
, "E22");
2029 thread
= strtoull(p
, (char **)&p
, 16);
2030 env
= find_cpu(thread
);
2033 put_packet(s
, "OK");
2035 put_packet(s
, "E22");
2040 /* parse any 'q' packets here */
2041 if (!strcmp(p
,"qemu.sstepbits")) {
2042 /* Query Breakpoint bit definitions */
2043 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
2049 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
2050 /* Display or change the sstep_flags */
2053 /* Display current setting */
2054 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
2059 type
= strtoul(p
, (char **)&p
, 16);
2061 put_packet(s
, "OK");
2063 } else if (strcmp(p
,"C") == 0) {
2064 /* "Current thread" remains vague in the spec, so always return
2065 * the first CPU (gdb returns the first thread). */
2066 put_packet(s
, "QC1");
2068 } else if (strcmp(p
,"fThreadInfo") == 0) {
2069 s
->query_cpu
= first_cpu
;
2070 goto report_cpuinfo
;
2071 } else if (strcmp(p
,"sThreadInfo") == 0) {
2074 snprintf(buf
, sizeof(buf
), "m%x", gdb_id(s
->query_cpu
));
2076 s
->query_cpu
= s
->query_cpu
->next_cpu
;
2080 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
2081 thread
= strtoull(p
+16, (char **)&p
, 16);
2082 env
= find_cpu(thread
);
2084 cpu_synchronize_state(env
);
2085 len
= snprintf((char *)mem_buf
, sizeof(mem_buf
),
2086 "CPU#%d [%s]", env
->cpu_index
,
2087 env
->halted
? "halted " : "running");
2088 memtohex(buf
, mem_buf
, len
);
2093 #ifdef CONFIG_USER_ONLY
2094 else if (strncmp(p
, "Offsets", 7) == 0) {
2095 TaskState
*ts
= s
->c_cpu
->opaque
;
2097 snprintf(buf
, sizeof(buf
),
2098 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
2099 ";Bss=" TARGET_ABI_FMT_lx
,
2100 ts
->info
->code_offset
,
2101 ts
->info
->data_offset
,
2102 ts
->info
->data_offset
);
2106 #else /* !CONFIG_USER_ONLY */
2107 else if (strncmp(p
, "Rcmd,", 5) == 0) {
2108 int len
= strlen(p
+ 5);
2110 if ((len
% 2) != 0) {
2111 put_packet(s
, "E01");
2114 hextomem(mem_buf
, p
+ 5, len
);
2117 qemu_chr_read(s
->mon_chr
, mem_buf
, len
);
2118 put_packet(s
, "OK");
2121 #endif /* !CONFIG_USER_ONLY */
2122 if (strncmp(p
, "Supported", 9) == 0) {
2123 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
2125 pstrcat(buf
, sizeof(buf
), ";qXfer:features:read+");
2131 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
2133 target_ulong total_len
;
2137 xml
= get_feature_xml(p
, &p
);
2139 snprintf(buf
, sizeof(buf
), "E00");
2146 addr
= strtoul(p
, (char **)&p
, 16);
2149 len
= strtoul(p
, (char **)&p
, 16);
2151 total_len
= strlen(xml
);
2152 if (addr
> total_len
) {
2153 snprintf(buf
, sizeof(buf
), "E00");
2157 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
2158 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2159 if (len
< total_len
- addr
) {
2161 len
= memtox(buf
+ 1, xml
+ addr
, len
);
2164 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
2166 put_packet_binary(s
, buf
, len
+ 1);
2170 /* Unrecognised 'q' command. */
2171 goto unknown_command
;
2175 /* put empty packet */
2183 void gdb_set_stop_cpu(CPUState
*env
)
2185 gdbserver_state
->c_cpu
= env
;
2186 gdbserver_state
->g_cpu
= env
;
2189 #ifndef CONFIG_USER_ONLY
2190 static void gdb_vm_state_change(void *opaque
, int running
, int reason
)
2192 GDBState
*s
= gdbserver_state
;
2193 CPUState
*env
= s
->c_cpu
;
2198 if (running
|| (reason
!= EXCP_DEBUG
&& reason
!= EXCP_INTERRUPT
) ||
2199 s
->state
== RS_INACTIVE
|| s
->state
== RS_SYSCALL
)
2202 /* disable single step if it was enable */
2203 cpu_single_step(env
, 0);
2205 if (reason
== EXCP_DEBUG
) {
2206 if (env
->watchpoint_hit
) {
2207 switch (env
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2218 snprintf(buf
, sizeof(buf
),
2219 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
2220 GDB_SIGNAL_TRAP
, gdb_id(env
), type
,
2221 env
->watchpoint_hit
->vaddr
);
2223 env
->watchpoint_hit
= NULL
;
2227 ret
= GDB_SIGNAL_TRAP
;
2229 ret
= GDB_SIGNAL_INT
;
2231 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, gdb_id(env
));
2236 /* Send a gdb syscall request.
2237 This accepts limited printf-style format specifiers, specifically:
2238 %x - target_ulong argument printed in hex.
2239 %lx - 64-bit argument printed in hex.
2240 %s - string pointer (target_ulong) and length (int) pair. */
2241 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2250 s
= gdbserver_state
;
2253 gdb_current_syscall_cb
= cb
;
2254 s
->state
= RS_SYSCALL
;
2255 #ifndef CONFIG_USER_ONLY
2256 vm_stop(EXCP_DEBUG
);
2267 addr
= va_arg(va
, target_ulong
);
2268 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
, addr
);
2271 if (*(fmt
++) != 'x')
2273 i64
= va_arg(va
, uint64_t);
2274 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, "%" PRIx64
, i64
);
2277 addr
= va_arg(va
, target_ulong
);
2278 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
"/%x",
2279 addr
, va_arg(va
, int));
2283 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
2294 #ifdef CONFIG_USER_ONLY
2295 gdb_handlesig(s
->c_cpu
, 0);
2301 static void gdb_read_byte(GDBState
*s
, int ch
)
2306 #ifndef CONFIG_USER_ONLY
2307 if (s
->last_packet_len
) {
2308 /* Waiting for a response to the last packet. If we see the start
2309 of a new command then abandon the previous response. */
2312 printf("Got NACK, retransmitting\n");
2314 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2318 printf("Got ACK\n");
2320 printf("Got '%c' when expecting ACK/NACK\n", ch
);
2322 if (ch
== '+' || ch
== '$')
2323 s
->last_packet_len
= 0;
2328 /* when the CPU is running, we cannot do anything except stop
2329 it when receiving a char */
2330 vm_stop(EXCP_INTERRUPT
);
2337 s
->line_buf_index
= 0;
2338 s
->state
= RS_GETLINE
;
2343 s
->state
= RS_CHKSUM1
;
2344 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2347 s
->line_buf
[s
->line_buf_index
++] = ch
;
2351 s
->line_buf
[s
->line_buf_index
] = '\0';
2352 s
->line_csum
= fromhex(ch
) << 4;
2353 s
->state
= RS_CHKSUM2
;
2356 s
->line_csum
|= fromhex(ch
);
2358 for(i
= 0; i
< s
->line_buf_index
; i
++) {
2359 csum
+= s
->line_buf
[i
];
2361 if (s
->line_csum
!= (csum
& 0xff)) {
2363 put_buffer(s
, &reply
, 1);
2367 put_buffer(s
, &reply
, 1);
2368 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2377 /* Tell the remote gdb that the process has exited. */
2378 void gdb_exit(CPUState
*env
, int code
)
2383 s
= gdbserver_state
;
2387 #ifdef CONFIG_USER_ONLY
2388 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2393 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
2396 #ifndef CONFIG_USER_ONLY
2398 qemu_chr_close(s
->chr
);
2403 #ifdef CONFIG_USER_ONLY
2409 s
= gdbserver_state
;
2411 if (gdbserver_fd
< 0 || s
->fd
< 0)
2418 gdb_handlesig (CPUState
*env
, int sig
)
2424 s
= gdbserver_state
;
2425 if (gdbserver_fd
< 0 || s
->fd
< 0)
2428 /* disable single step if it was enabled */
2429 cpu_single_step(env
, 0);
2434 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb (sig
));
2437 /* put_packet() might have detected that the peer terminated the
2444 s
->running_state
= 0;
2445 while (s
->running_state
== 0) {
2446 n
= read (s
->fd
, buf
, 256);
2451 for (i
= 0; i
< n
; i
++)
2452 gdb_read_byte (s
, buf
[i
]);
2454 else if (n
== 0 || errno
!= EAGAIN
)
2456 /* XXX: Connection closed. Should probably wait for annother
2457 connection before continuing. */
2466 /* Tell the remote gdb that the process has exited due to SIG. */
2467 void gdb_signalled(CPUState
*env
, int sig
)
2472 s
= gdbserver_state
;
2473 if (gdbserver_fd
< 0 || s
->fd
< 0)
2476 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb (sig
));
2480 static void gdb_accept(void)
2483 struct sockaddr_in sockaddr
;
2488 len
= sizeof(sockaddr
);
2489 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2490 if (fd
< 0 && errno
!= EINTR
) {
2493 } else if (fd
>= 0) {
2495 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
2501 /* set short latency */
2503 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2505 s
= qemu_mallocz(sizeof(GDBState
));
2506 s
->c_cpu
= first_cpu
;
2507 s
->g_cpu
= first_cpu
;
2511 gdbserver_state
= s
;
2513 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2516 static int gdbserver_open(int port
)
2518 struct sockaddr_in sockaddr
;
2521 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2527 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
2530 /* allow fast reuse */
2532 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
2534 sockaddr
.sin_family
= AF_INET
;
2535 sockaddr
.sin_port
= htons(port
);
2536 sockaddr
.sin_addr
.s_addr
= 0;
2537 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2542 ret
= listen(fd
, 0);
2550 int gdbserver_start(int port
)
2552 gdbserver_fd
= gdbserver_open(port
);
2553 if (gdbserver_fd
< 0)
2555 /* accept connections */
2560 /* Disable gdb stub for child processes. */
2561 void gdbserver_fork(CPUState
*env
)
2563 GDBState
*s
= gdbserver_state
;
2564 if (gdbserver_fd
< 0 || s
->fd
< 0)
2568 cpu_breakpoint_remove_all(env
, BP_GDB
);
2569 cpu_watchpoint_remove_all(env
, BP_GDB
);
2572 static int gdb_chr_can_receive(void *opaque
)
2574 /* We can handle an arbitrarily large amount of data.
2575 Pick the maximum packet size, which is as good as anything. */
2576 return MAX_PACKET_LENGTH
;
2579 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2583 for (i
= 0; i
< size
; i
++) {
2584 gdb_read_byte(gdbserver_state
, buf
[i
]);
2588 static void gdb_chr_event(void *opaque
, int event
)
2591 case CHR_EVENT_OPENED
:
2592 vm_stop(EXCP_INTERRUPT
);
2600 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
2602 char buf
[MAX_PACKET_LENGTH
];
2605 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
2606 len
= (MAX_PACKET_LENGTH
/2) - 1;
2607 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
2611 static int gdb_monitor_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2613 const char *p
= (const char *)buf
;
2616 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
2618 if (len
<= max_sz
) {
2619 gdb_monitor_output(gdbserver_state
, p
, len
);
2622 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
2630 static void gdb_sigterm_handler(int signal
)
2633 vm_stop(EXCP_INTERRUPT
);
2637 int gdbserver_start(const char *device
)
2640 char gdbstub_device_name
[128];
2641 CharDriverState
*chr
= NULL
;
2642 CharDriverState
*mon_chr
;
2646 if (strcmp(device
, "none") != 0) {
2647 if (strstart(device
, "tcp:", NULL
)) {
2648 /* enforce required TCP attributes */
2649 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
2650 "%s,nowait,nodelay,server", device
);
2651 device
= gdbstub_device_name
;
2654 else if (strcmp(device
, "stdio") == 0) {
2655 struct sigaction act
;
2657 memset(&act
, 0, sizeof(act
));
2658 act
.sa_handler
= gdb_sigterm_handler
;
2659 sigaction(SIGINT
, &act
, NULL
);
2662 chr
= qemu_chr_open("gdb", device
, NULL
);
2666 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2667 gdb_chr_event
, NULL
);
2670 s
= gdbserver_state
;
2672 s
= qemu_mallocz(sizeof(GDBState
));
2673 gdbserver_state
= s
;
2675 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
2677 /* Initialize a monitor terminal for gdb */
2678 mon_chr
= qemu_mallocz(sizeof(*mon_chr
));
2679 mon_chr
->chr_write
= gdb_monitor_write
;
2680 monitor_init(mon_chr
, 0);
2683 qemu_chr_close(s
->chr
);
2684 mon_chr
= s
->mon_chr
;
2685 memset(s
, 0, sizeof(GDBState
));
2687 s
->c_cpu
= first_cpu
;
2688 s
->g_cpu
= first_cpu
;
2690 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
2691 s
->mon_chr
= mon_chr
;