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
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <netinet/tcp.h>
42 static int gdbserver_fd
;
44 typedef struct GDBState
{
52 static int get_char(GDBState
*s
)
58 ret
= read(s
->fd
, &ch
, 1);
60 if (errno
!= EINTR
&& errno
!= EAGAIN
)
62 } else if (ret
== 0) {
71 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
76 ret
= write(s
->fd
, buf
, len
);
78 if (errno
!= EINTR
&& errno
!= EAGAIN
)
87 static inline int fromhex(int v
)
89 if (v
>= '0' && v
<= '9')
91 else if (v
>= 'A' && v
<= 'F')
93 else if (v
>= 'a' && v
<= 'f')
99 static inline int tohex(int v
)
107 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
112 for(i
= 0; i
< len
; i
++) {
114 *q
++ = tohex(c
>> 4);
115 *q
++ = tohex(c
& 0xf);
120 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
124 for(i
= 0; i
< len
; i
++) {
125 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
130 /* return -1 if error, 0 if OK */
131 static int put_packet(GDBState
*s
, char *buf
)
134 int len
, csum
, ch
, i
;
137 printf("reply='%s'\n", buf
);
142 put_buffer(s
, buf1
, 1);
144 put_buffer(s
, buf
, len
);
146 for(i
= 0; i
< len
; i
++) {
150 buf1
[1] = tohex((csum
>> 4) & 0xf);
151 buf1
[2] = tohex((csum
) & 0xf);
153 put_buffer(s
, buf1
, 3);
164 #if defined(TARGET_I386)
166 static void to_le32(uint8_t *p
, int v
)
174 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
178 for(i
= 0; i
< 8; i
++) {
179 to_le32(mem_buf
+ i
* 4, env
->regs
[i
]);
181 to_le32(mem_buf
+ 8 * 4, env
->eip
);
182 to_le32(mem_buf
+ 9 * 4, env
->eflags
);
183 to_le32(mem_buf
+ 10 * 4, env
->segs
[R_CS
].selector
);
184 to_le32(mem_buf
+ 11 * 4, env
->segs
[R_SS
].selector
);
185 to_le32(mem_buf
+ 12 * 4, env
->segs
[R_DS
].selector
);
186 to_le32(mem_buf
+ 13 * 4, env
->segs
[R_ES
].selector
);
187 to_le32(mem_buf
+ 14 * 4, env
->segs
[R_FS
].selector
);
188 to_le32(mem_buf
+ 15 * 4, env
->segs
[R_GS
].selector
);
189 /* XXX: convert floats */
190 for(i
= 0; i
< 8; i
++) {
191 memcpy(mem_buf
+ 16 * 4 + i
* 10, &env
->fpregs
[i
], 10);
193 to_le32(mem_buf
+ 36 * 4, env
->fpuc
);
194 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
195 to_le32(mem_buf
+ 37 * 4, fpus
);
196 to_le32(mem_buf
+ 38 * 4, 0); /* XXX: convert tags */
197 to_le32(mem_buf
+ 39 * 4, 0); /* fiseg */
198 to_le32(mem_buf
+ 40 * 4, 0); /* fioff */
199 to_le32(mem_buf
+ 41 * 4, 0); /* foseg */
200 to_le32(mem_buf
+ 42 * 4, 0); /* fooff */
201 to_le32(mem_buf
+ 43 * 4, 0); /* fop */
205 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
207 uint32_t *registers
= (uint32_t *)mem_buf
;
210 for(i
= 0; i
< 8; i
++) {
211 env
->regs
[i
] = tswapl(registers
[i
]);
213 env
->eip
= registers
[8];
214 env
->eflags
= registers
[9];
215 #if defined(CONFIG_USER_ONLY)
216 #define LOAD_SEG(index, sreg)\
217 if (tswapl(registers[index]) != env->segs[sreg].selector)\
218 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
228 #elif defined (TARGET_PPC)
229 static void to_le32(uint8_t *p
, int v
)
237 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
243 for(i
= 0; i
< 8; i
++) {
244 to_le32(mem_buf
+ i
* 4, env
->gpr
[i
]);
247 for (i
= 0; i
< 32; i
++) {
248 to_le32(mem_buf
+ (i
* 2) + 32, *((uint32_t *)&env
->fpr
[i
]));
249 to_le32(mem_buf
+ (i
* 2) + 33, *((uint32_t *)&env
->fpr
[i
] + 1));
251 /* nip, msr, ccr, lnk, ctr, xer, mq */
252 to_le32(mem_buf
+ 96, tswapl(env
->nip
));
253 to_le32(mem_buf
+ 97, tswapl(_load_msr()));
254 to_le32(mem_buf
+ 98, 0);
256 for (i
= 0; i
< 8; i
++)
257 tmp
|= env
->crf
[i
] << (32 - (i
* 4));
258 to_le32(mem_buf
+ 98, tmp
);
259 to_le32(mem_buf
+ 99, tswapl(env
->lr
));
260 to_le32(mem_buf
+ 100, tswapl(env
->ctr
));
261 to_le32(mem_buf
+ 101, tswapl(_load_xer()));
262 to_le32(mem_buf
+ 102, 0);
267 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
269 uint32_t *registers
= (uint32_t *)mem_buf
;
273 for (i
= 0; i
< 32; i
++) {
274 env
->gpr
[i
] = tswapl(registers
[i
]);
277 for (i
= 0; i
< 32; i
++) {
278 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
279 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
281 /* nip, msr, ccr, lnk, ctr, xer, mq */
282 env
->nip
= tswapl(registers
[96]);
283 _store_msr(tswapl(registers
[97]));
284 registers
[98] = tswapl(registers
[98]);
285 for (i
= 0; i
< 8; i
++)
286 env
->crf
[i
] = (registers
[98] >> (32 - (i
* 4))) & 0xF;
287 env
->lr
= tswapl(registers
[99]);
288 env
->ctr
= tswapl(registers
[100]);
289 _store_xer(tswapl(registers
[101]));
293 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
298 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
304 /* port = 0 means default port */
305 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
307 CPUState
*env
= cpu_single_env
;
309 int ch
, reg_size
, type
;
311 uint8_t mem_buf
[2000];
316 printf("command='%s'\n", line_buf
);
322 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
327 addr
= strtoul(p
, (char **)&p
, 16);
328 #if defined(TARGET_I386)
330 #elif defined (TARGET_PPC)
338 addr
= strtoul(p
, (char **)&p
, 16);
339 #if defined(TARGET_I386)
341 #elif defined (TARGET_PPC)
345 cpu_single_step(env
, 1);
349 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
350 memtohex(buf
, mem_buf
, reg_size
);
354 registers
= (void *)mem_buf
;
356 hextomem((uint8_t *)registers
, p
, len
);
357 cpu_gdb_write_registers(env
, mem_buf
, len
);
361 addr
= strtoul(p
, (char **)&p
, 16);
364 len
= strtoul(p
, NULL
, 16);
365 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0)
366 memset(mem_buf
, 0, len
);
367 memtohex(buf
, mem_buf
, len
);
371 addr
= strtoul(p
, (char **)&p
, 16);
374 len
= strtoul(p
, (char **)&p
, 16);
377 hextomem(mem_buf
, p
, len
);
378 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
379 put_packet(s
, "ENN");
384 type
= strtoul(p
, (char **)&p
, 16);
387 addr
= strtoul(p
, (char **)&p
, 16);
390 len
= strtoul(p
, (char **)&p
, 16);
391 if (type
== 0 || type
== 1) {
392 if (cpu_breakpoint_insert(env
, addr
) < 0)
393 goto breakpoint_error
;
397 put_packet(s
, "ENN");
401 type
= strtoul(p
, (char **)&p
, 16);
404 addr
= strtoul(p
, (char **)&p
, 16);
407 len
= strtoul(p
, (char **)&p
, 16);
408 if (type
== 0 || type
== 1) {
409 cpu_breakpoint_remove(env
, addr
);
412 goto breakpoint_error
;
417 /* put empty packet */
425 static void gdb_vm_stopped(void *opaque
, int reason
)
427 GDBState
*s
= opaque
;
431 /* disable single step if it was enable */
432 cpu_single_step(cpu_single_env
, 0);
434 if (reason
== EXCP_DEBUG
)
438 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
442 static void gdb_read_byte(GDBState
*s
, int ch
)
448 /* when the CPU is running, we cannot do anything except stop
449 it when receiving a char */
450 vm_stop(EXCP_INTERRUPT
);
455 s
->line_buf_index
= 0;
456 s
->state
= RS_GETLINE
;
461 s
->state
= RS_CHKSUM1
;
462 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
465 s
->line_buf
[s
->line_buf_index
++] = ch
;
469 s
->line_buf
[s
->line_buf_index
] = '\0';
470 s
->line_csum
= fromhex(ch
) << 4;
471 s
->state
= RS_CHKSUM2
;
474 s
->line_csum
|= fromhex(ch
);
476 for(i
= 0; i
< s
->line_buf_index
; i
++) {
477 csum
+= s
->line_buf
[i
];
479 if (s
->line_csum
!= (csum
& 0xff)) {
481 put_buffer(s
, reply
, 1);
485 put_buffer(s
, reply
, 1);
486 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
493 static int gdb_can_read(void *opaque
)
498 static void gdb_read(void *opaque
, const uint8_t *buf
, int size
)
500 GDBState
*s
= opaque
;
503 /* end of connection */
504 qemu_del_vm_stop_handler(gdb_vm_stopped
, s
);
505 qemu_del_fd_read_handler(s
->fd
);
509 for(i
= 0; i
< size
; i
++)
510 gdb_read_byte(s
, buf
[i
]);
514 static void gdb_accept(void *opaque
, const uint8_t *buf
, int size
)
517 struct sockaddr_in sockaddr
;
522 len
= sizeof(sockaddr
);
523 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
524 if (fd
< 0 && errno
!= EINTR
) {
527 } else if (fd
>= 0) {
532 /* set short latency */
534 setsockopt(fd
, SOL_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
536 s
= qemu_mallocz(sizeof(GDBState
));
543 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
546 vm_stop(EXCP_INTERRUPT
);
548 /* start handling I/O */
549 qemu_add_fd_read_handler(s
->fd
, gdb_can_read
, gdb_read
, s
);
550 /* when the VM is stopped, the following callback is called */
551 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);
554 static int gdbserver_open(int port
)
556 struct sockaddr_in sockaddr
;
559 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
565 /* allow fast reuse */
567 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
569 sockaddr
.sin_family
= AF_INET
;
570 sockaddr
.sin_port
= htons(port
);
571 sockaddr
.sin_addr
.s_addr
= 0;
572 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
582 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
586 int gdbserver_start(int port
)
588 gdbserver_fd
= gdbserver_open(port
);
589 if (gdbserver_fd
< 0)
591 /* accept connections */
592 qemu_add_fd_read_handler(gdbserver_fd
, NULL
, gdb_accept
, NULL
);