4 * Copyright (c) 2003 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
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netinet/tcp.h>
36 static int gdbserver_fd
;
38 typedef struct GDBState
{
46 static int get_char(GDBState
*s
)
52 ret
= read(s
->fd
, &ch
, 1);
54 if (errno
!= EINTR
&& errno
!= EAGAIN
)
56 } else if (ret
== 0) {
65 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
70 ret
= write(s
->fd
, buf
, len
);
72 if (errno
!= EINTR
&& errno
!= EAGAIN
)
81 static inline int fromhex(int v
)
83 if (v
>= '0' && v
<= '9')
85 else if (v
>= 'A' && v
<= 'F')
87 else if (v
>= 'a' && v
<= 'f')
93 static inline int tohex(int v
)
101 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
106 for(i
= 0; i
< len
; i
++) {
108 *q
++ = tohex(c
>> 4);
109 *q
++ = tohex(c
& 0xf);
114 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
118 for(i
= 0; i
< len
; i
++) {
119 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
124 /* return -1 if error, 0 if OK */
125 static int put_packet(GDBState
*s
, char *buf
)
128 int len
, csum
, ch
, i
;
131 printf("reply='%s'\n", buf
);
136 put_buffer(s
, buf1
, 1);
138 put_buffer(s
, buf
, len
);
140 for(i
= 0; i
< len
; i
++) {
144 buf1
[1] = tohex((csum
>> 4) & 0xf);
145 buf1
[2] = tohex((csum
) & 0xf);
147 put_buffer(s
, buf1
, 3);
158 #if defined(TARGET_I386)
160 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
162 uint32_t *registers
= (uint32_t *)mem_buf
;
165 for(i
= 0; i
< 8; i
++) {
166 registers
[i
] = env
->regs
[i
];
168 registers
[8] = env
->eip
;
169 registers
[9] = env
->eflags
;
170 registers
[10] = env
->segs
[R_CS
].selector
;
171 registers
[11] = env
->segs
[R_SS
].selector
;
172 registers
[12] = env
->segs
[R_DS
].selector
;
173 registers
[13] = env
->segs
[R_ES
].selector
;
174 registers
[14] = env
->segs
[R_FS
].selector
;
175 registers
[15] = env
->segs
[R_GS
].selector
;
176 /* XXX: convert floats */
177 for(i
= 0; i
< 8; i
++) {
178 memcpy(mem_buf
+ 16 * 4 + i
* 10, &env
->fpregs
[i
], 10);
180 registers
[36] = env
->fpuc
;
181 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
182 registers
[37] = fpus
;
183 registers
[38] = 0; /* XXX: convert tags */
184 registers
[39] = 0; /* fiseg */
185 registers
[40] = 0; /* fioff */
186 registers
[41] = 0; /* foseg */
187 registers
[42] = 0; /* fooff */
188 registers
[43] = 0; /* fop */
190 for(i
= 0; i
< 16; i
++)
191 tswapls(®isters
[i
]);
192 for(i
= 36; i
< 44; i
++)
193 tswapls(®isters
[i
]);
197 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
199 uint32_t *registers
= (uint32_t *)mem_buf
;
202 for(i
= 0; i
< 8; i
++) {
203 env
->regs
[i
] = tswapl(registers
[i
]);
205 env
->eip
= tswapl(registers
[8]);
206 env
->eflags
= tswapl(registers
[9]);
207 #if defined(CONFIG_USER_ONLY)
208 #define LOAD_SEG(index, sreg)\
209 if (tswapl(registers[index]) != env->segs[sreg].selector)\
210 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
220 #elif defined (TARGET_PPC)
221 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
223 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
227 for(i
= 0; i
< 32; i
++) {
228 registers
[i
] = tswapl(env
->gpr
[i
]);
231 for (i
= 0; i
< 32; i
++) {
232 registers
[(i
* 2) + 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
233 registers
[(i
* 2) + 33] = tswapl(*((uint32_t *)&env
->fpr
[i
] + 1));
235 /* nip, msr, ccr, lnk, ctr, xer, mq */
236 registers
[96] = tswapl(env
->nip
);
237 registers
[97] = tswapl(_load_msr(env
));
239 for (i
= 0; i
< 8; i
++)
240 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
241 registers
[98] = tswapl(tmp
);
242 registers
[99] = tswapl(env
->lr
);
243 registers
[100] = tswapl(env
->ctr
);
244 registers
[101] = tswapl(_load_xer(env
));
250 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
252 uint32_t *registers
= (uint32_t *)mem_buf
;
256 for (i
= 0; i
< 32; i
++) {
257 env
->gpr
[i
] = tswapl(registers
[i
]);
260 for (i
= 0; i
< 32; i
++) {
261 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
262 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
264 /* nip, msr, ccr, lnk, ctr, xer, mq */
265 env
->nip
= tswapl(registers
[96]);
266 _store_msr(env
, tswapl(registers
[97]));
267 registers
[98] = tswapl(registers
[98]);
268 for (i
= 0; i
< 8; i
++)
269 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
270 env
->lr
= tswapl(registers
[99]);
271 env
->ctr
= tswapl(registers
[100]);
272 _store_xer(env
, tswapl(registers
[101]));
274 #elif defined (TARGET_SPARC)
275 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
277 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
281 for(i
= 0; i
< 7; i
++) {
282 registers
[i
] = tswapl(env
->gregs
[i
]);
284 /* fill in register window */
285 for(i
= 0; i
< 24; i
++) {
286 registers
[i
+ 8] = tswapl(env
->regwptr
[i
]);
289 for (i
= 0; i
< 32; i
++) {
290 registers
[i
+ 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
292 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
293 registers
[64] = tswapl(env
->y
);
295 registers
[65] = tswapl(tmp
);
296 registers
[66] = tswapl(env
->wim
);
297 registers
[67] = tswapl(env
->tbr
);
298 registers
[68] = tswapl(env
->pc
);
299 registers
[69] = tswapl(env
->npc
);
300 registers
[70] = tswapl(env
->fsr
);
301 registers
[71] = 0; /* csr */
307 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
309 uint32_t *registers
= (uint32_t *)mem_buf
;
313 for(i
= 0; i
< 7; i
++) {
314 env
->gregs
[i
] = tswapl(registers
[i
]);
316 /* fill in register window */
317 for(i
= 0; i
< 24; i
++) {
318 env
->regwptr
[i
] = tswapl(registers
[i
]);
321 for (i
= 0; i
< 32; i
++) {
322 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[i
+ 32]);
324 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
325 env
->y
= tswapl(registers
[64]);
326 PUT_PSR(env
, tswapl(registers
[65]));
327 env
->wim
= tswapl(registers
[66]);
328 env
->tbr
= tswapl(registers
[67]);
329 env
->pc
= tswapl(registers
[68]);
330 env
->npc
= tswapl(registers
[69]);
331 env
->fsr
= tswapl(registers
[70]);
335 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
340 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
346 /* port = 0 means default port */
347 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
349 CPUState
*env
= cpu_single_env
;
351 int ch
, reg_size
, type
;
353 uint8_t mem_buf
[2000];
358 printf("command='%s'\n", line_buf
);
364 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
369 addr
= strtoul(p
, (char **)&p
, 16);
370 #if defined(TARGET_I386)
372 #elif defined (TARGET_PPC)
374 #elif defined (TARGET_SPARC)
383 addr
= strtoul(p
, (char **)&p
, 16);
384 #if defined(TARGET_I386)
386 #elif defined (TARGET_PPC)
388 #elif defined (TARGET_SPARC)
393 cpu_single_step(env
, 1);
397 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
398 memtohex(buf
, mem_buf
, reg_size
);
402 registers
= (void *)mem_buf
;
404 hextomem((uint8_t *)registers
, p
, len
);
405 cpu_gdb_write_registers(env
, mem_buf
, len
);
409 addr
= strtoul(p
, (char **)&p
, 16);
412 len
= strtoul(p
, NULL
, 16);
413 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0)
414 memset(mem_buf
, 0, len
);
415 memtohex(buf
, mem_buf
, len
);
419 addr
= strtoul(p
, (char **)&p
, 16);
422 len
= strtoul(p
, (char **)&p
, 16);
425 hextomem(mem_buf
, p
, len
);
426 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
427 put_packet(s
, "ENN");
432 type
= strtoul(p
, (char **)&p
, 16);
435 addr
= strtoul(p
, (char **)&p
, 16);
438 len
= strtoul(p
, (char **)&p
, 16);
439 if (type
== 0 || type
== 1) {
440 if (cpu_breakpoint_insert(env
, addr
) < 0)
441 goto breakpoint_error
;
445 put_packet(s
, "ENN");
449 type
= strtoul(p
, (char **)&p
, 16);
452 addr
= strtoul(p
, (char **)&p
, 16);
455 len
= strtoul(p
, (char **)&p
, 16);
456 if (type
== 0 || type
== 1) {
457 cpu_breakpoint_remove(env
, addr
);
460 goto breakpoint_error
;
465 /* put empty packet */
473 extern void tb_flush(CPUState
*env
);
475 static void gdb_vm_stopped(void *opaque
, int reason
)
477 GDBState
*s
= opaque
;
481 /* disable single step if it was enable */
482 cpu_single_step(cpu_single_env
, 0);
484 if (reason
== EXCP_DEBUG
) {
485 tb_flush(cpu_single_env
);
490 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
494 static void gdb_read_byte(GDBState
*s
, int ch
)
500 /* when the CPU is running, we cannot do anything except stop
501 it when receiving a char */
502 vm_stop(EXCP_INTERRUPT
);
507 s
->line_buf_index
= 0;
508 s
->state
= RS_GETLINE
;
513 s
->state
= RS_CHKSUM1
;
514 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
517 s
->line_buf
[s
->line_buf_index
++] = ch
;
521 s
->line_buf
[s
->line_buf_index
] = '\0';
522 s
->line_csum
= fromhex(ch
) << 4;
523 s
->state
= RS_CHKSUM2
;
526 s
->line_csum
|= fromhex(ch
);
528 for(i
= 0; i
< s
->line_buf_index
; i
++) {
529 csum
+= s
->line_buf
[i
];
531 if (s
->line_csum
!= (csum
& 0xff)) {
533 put_buffer(s
, reply
, 1);
537 put_buffer(s
, reply
, 1);
538 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
545 static int gdb_can_read(void *opaque
)
550 static void gdb_read(void *opaque
, const uint8_t *buf
, int size
)
552 GDBState
*s
= opaque
;
555 /* end of connection */
556 qemu_del_vm_stop_handler(gdb_vm_stopped
, s
);
557 qemu_del_fd_read_handler(s
->fd
);
561 for(i
= 0; i
< size
; i
++)
562 gdb_read_byte(s
, buf
[i
]);
566 static void gdb_accept(void *opaque
, const uint8_t *buf
, int size
)
569 struct sockaddr_in sockaddr
;
574 len
= sizeof(sockaddr
);
575 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
576 if (fd
< 0 && errno
!= EINTR
) {
579 } else if (fd
>= 0) {
584 /* set short latency */
586 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
588 s
= qemu_mallocz(sizeof(GDBState
));
595 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
598 vm_stop(EXCP_INTERRUPT
);
600 /* start handling I/O */
601 qemu_add_fd_read_handler(s
->fd
, gdb_can_read
, gdb_read
, s
);
602 /* when the VM is stopped, the following callback is called */
603 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);
606 static int gdbserver_open(int port
)
608 struct sockaddr_in sockaddr
;
611 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
617 /* allow fast reuse */
619 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
621 sockaddr
.sin_family
= AF_INET
;
622 sockaddr
.sin_port
= htons(port
);
623 sockaddr
.sin_addr
.s_addr
= 0;
624 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
634 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
638 int gdbserver_start(int port
)
640 gdbserver_fd
= gdbserver_open(port
);
641 if (gdbserver_fd
< 0)
643 /* accept connections */
644 qemu_add_fd_read_handler(gdbserver_fd
, NULL
, gdb_accept
, NULL
);