Fix display resize bug.
[qemu/mini2440.git] / gdbstub.c
blob5fdf800bdbc6bbb1e86b4bd018684994b7d9a6ff
1 /*
2 * gdb server stub
3 *
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
20 #ifdef CONFIG_USER_ONLY
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
28 #include "qemu.h"
29 #else
30 #include "vl.h"
31 #endif
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
36 #include <signal.h>
38 //#define DEBUG_GDB
40 enum RSState {
41 RS_IDLE,
42 RS_GETLINE,
43 RS_CHKSUM1,
44 RS_CHKSUM2,
46 /* XXX: This is not thread safe. Do we care? */
47 static int gdbserver_fd = -1;
49 typedef struct GDBState {
50 CPUState *env; /* current CPU */
51 enum RSState state; /* parsing state */
52 int fd;
53 char line_buf[4096];
54 int line_buf_index;
55 int line_csum;
56 #ifdef CONFIG_USER_ONLY
57 int running_state;
58 #endif
59 } GDBState;
61 #ifdef CONFIG_USER_ONLY
62 /* XXX: remove this hack. */
63 static GDBState gdbserver_state;
64 #endif
66 static int get_char(GDBState *s)
68 uint8_t ch;
69 int ret;
71 for(;;) {
72 ret = read(s->fd, &ch, 1);
73 if (ret < 0) {
74 if (errno != EINTR && errno != EAGAIN)
75 return -1;
76 } else if (ret == 0) {
77 return -1;
78 } else {
79 break;
82 return ch;
85 static void put_buffer(GDBState *s, const uint8_t *buf, int len)
87 int ret;
89 while (len > 0) {
90 ret = write(s->fd, buf, len);
91 if (ret < 0) {
92 if (errno != EINTR && errno != EAGAIN)
93 return;
94 } else {
95 buf += ret;
96 len -= ret;
101 static inline int fromhex(int v)
103 if (v >= '0' && v <= '9')
104 return v - '0';
105 else if (v >= 'A' && v <= 'F')
106 return v - 'A' + 10;
107 else if (v >= 'a' && v <= 'f')
108 return v - 'a' + 10;
109 else
110 return 0;
113 static inline int tohex(int v)
115 if (v < 10)
116 return v + '0';
117 else
118 return v - 10 + 'a';
121 static void memtohex(char *buf, const uint8_t *mem, int len)
123 int i, c;
124 char *q;
125 q = buf;
126 for(i = 0; i < len; i++) {
127 c = mem[i];
128 *q++ = tohex(c >> 4);
129 *q++ = tohex(c & 0xf);
131 *q = '\0';
134 static void hextomem(uint8_t *mem, const char *buf, int len)
136 int i;
138 for(i = 0; i < len; i++) {
139 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
140 buf += 2;
144 /* return -1 if error, 0 if OK */
145 static int put_packet(GDBState *s, char *buf)
147 char buf1[3];
148 int len, csum, ch, i;
150 #ifdef DEBUG_GDB
151 printf("reply='%s'\n", buf);
152 #endif
154 for(;;) {
155 buf1[0] = '$';
156 put_buffer(s, buf1, 1);
157 len = strlen(buf);
158 put_buffer(s, buf, len);
159 csum = 0;
160 for(i = 0; i < len; i++) {
161 csum += buf[i];
163 buf1[0] = '#';
164 buf1[1] = tohex((csum >> 4) & 0xf);
165 buf1[2] = tohex((csum) & 0xf);
167 put_buffer(s, buf1, 3);
169 ch = get_char(s);
170 if (ch < 0)
171 return -1;
172 if (ch == '+')
173 break;
175 return 0;
178 #if defined(TARGET_I386)
180 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
182 uint32_t *registers = (uint32_t *)mem_buf;
183 int i, fpus;
185 for(i = 0; i < 8; i++) {
186 registers[i] = env->regs[i];
188 registers[8] = env->eip;
189 registers[9] = env->eflags;
190 registers[10] = env->segs[R_CS].selector;
191 registers[11] = env->segs[R_SS].selector;
192 registers[12] = env->segs[R_DS].selector;
193 registers[13] = env->segs[R_ES].selector;
194 registers[14] = env->segs[R_FS].selector;
195 registers[15] = env->segs[R_GS].selector;
196 /* XXX: convert floats */
197 for(i = 0; i < 8; i++) {
198 memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10);
200 registers[36] = env->fpuc;
201 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
202 registers[37] = fpus;
203 registers[38] = 0; /* XXX: convert tags */
204 registers[39] = 0; /* fiseg */
205 registers[40] = 0; /* fioff */
206 registers[41] = 0; /* foseg */
207 registers[42] = 0; /* fooff */
208 registers[43] = 0; /* fop */
210 for(i = 0; i < 16; i++)
211 tswapls(&registers[i]);
212 for(i = 36; i < 44; i++)
213 tswapls(&registers[i]);
214 return 44 * 4;
217 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
219 uint32_t *registers = (uint32_t *)mem_buf;
220 int i;
222 for(i = 0; i < 8; i++) {
223 env->regs[i] = tswapl(registers[i]);
225 env->eip = tswapl(registers[8]);
226 env->eflags = tswapl(registers[9]);
227 #if defined(CONFIG_USER_ONLY)
228 #define LOAD_SEG(index, sreg)\
229 if (tswapl(registers[index]) != env->segs[sreg].selector)\
230 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
231 LOAD_SEG(10, R_CS);
232 LOAD_SEG(11, R_SS);
233 LOAD_SEG(12, R_DS);
234 LOAD_SEG(13, R_ES);
235 LOAD_SEG(14, R_FS);
236 LOAD_SEG(15, R_GS);
237 #endif
240 #elif defined (TARGET_PPC)
241 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
243 uint32_t *registers = (uint32_t *)mem_buf, tmp;
244 int i;
246 /* fill in gprs */
247 for(i = 0; i < 32; i++) {
248 registers[i] = tswapl(env->gpr[i]);
250 /* fill in fprs */
251 for (i = 0; i < 32; i++) {
252 registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
253 registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1));
255 /* nip, msr, ccr, lnk, ctr, xer, mq */
256 registers[96] = tswapl(env->nip);
257 registers[97] = tswapl(do_load_msr(env));
258 tmp = 0;
259 for (i = 0; i < 8; i++)
260 tmp |= env->crf[i] << (32 - ((i + 1) * 4));
261 registers[98] = tswapl(tmp);
262 registers[99] = tswapl(env->lr);
263 registers[100] = tswapl(env->ctr);
264 registers[101] = tswapl(do_load_xer(env));
265 registers[102] = 0;
267 return 103 * 4;
270 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
272 uint32_t *registers = (uint32_t *)mem_buf;
273 int i;
275 /* fill in gprs */
276 for (i = 0; i < 32; i++) {
277 env->gpr[i] = tswapl(registers[i]);
279 /* fill in fprs */
280 for (i = 0; i < 32; i++) {
281 *((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]);
282 *((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]);
284 /* nip, msr, ccr, lnk, ctr, xer, mq */
285 env->nip = tswapl(registers[96]);
286 do_store_msr(env, tswapl(registers[97]));
287 registers[98] = tswapl(registers[98]);
288 for (i = 0; i < 8; i++)
289 env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF;
290 env->lr = tswapl(registers[99]);
291 env->ctr = tswapl(registers[100]);
292 do_store_xer(env, tswapl(registers[101]));
294 #elif defined (TARGET_SPARC)
295 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
297 target_ulong *registers = (target_ulong *)mem_buf;
298 int i;
300 /* fill in g0..g7 */
301 for(i = 0; i < 8; i++) {
302 registers[i] = tswapl(env->gregs[i]);
304 /* fill in register window */
305 for(i = 0; i < 24; i++) {
306 registers[i + 8] = tswapl(env->regwptr[i]);
308 /* fill in fprs */
309 for (i = 0; i < 32; i++) {
310 registers[i + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
312 #ifndef TARGET_SPARC64
313 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
314 registers[64] = tswapl(env->y);
316 target_ulong tmp;
318 tmp = GET_PSR(env);
319 registers[65] = tswapl(tmp);
321 registers[66] = tswapl(env->wim);
322 registers[67] = tswapl(env->tbr);
323 registers[68] = tswapl(env->pc);
324 registers[69] = tswapl(env->npc);
325 registers[70] = tswapl(env->fsr);
326 registers[71] = 0; /* csr */
327 registers[72] = 0;
328 return 73 * sizeof(target_ulong);
329 #else
330 for (i = 0; i < 32; i += 2) {
331 registers[i/2 + 64] = tswapl(*((uint64_t *)&env->fpr[i]));
333 registers[81] = tswapl(env->pc);
334 registers[82] = tswapl(env->npc);
335 registers[83] = tswapl(env->tstate[env->tl]);
336 registers[84] = tswapl(env->fsr);
337 registers[85] = tswapl(env->fprs);
338 registers[86] = tswapl(env->y);
339 return 87 * sizeof(target_ulong);
340 #endif
343 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
345 target_ulong *registers = (target_ulong *)mem_buf;
346 int i;
348 /* fill in g0..g7 */
349 for(i = 0; i < 7; i++) {
350 env->gregs[i] = tswapl(registers[i]);
352 /* fill in register window */
353 for(i = 0; i < 24; i++) {
354 env->regwptr[i] = tswapl(registers[i + 8]);
356 /* fill in fprs */
357 for (i = 0; i < 32; i++) {
358 *((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]);
360 #ifndef TARGET_SPARC64
361 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
362 env->y = tswapl(registers[64]);
363 PUT_PSR(env, tswapl(registers[65]));
364 env->wim = tswapl(registers[66]);
365 env->tbr = tswapl(registers[67]);
366 env->pc = tswapl(registers[68]);
367 env->npc = tswapl(registers[69]);
368 env->fsr = tswapl(registers[70]);
369 #else
370 for (i = 0; i < 32; i += 2) {
371 uint64_t tmp;
372 tmp = tswapl(registers[i/2 + 64]) << 32;
373 tmp |= tswapl(registers[i/2 + 64 + 1]);
374 *((uint64_t *)&env->fpr[i]) = tmp;
376 env->pc = tswapl(registers[81]);
377 env->npc = tswapl(registers[82]);
378 env->tstate[env->tl] = tswapl(registers[83]);
379 env->fsr = tswapl(registers[84]);
380 env->fprs = tswapl(registers[85]);
381 env->y = tswapl(registers[86]);
382 #endif
384 #elif defined (TARGET_ARM)
385 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
387 int i;
388 uint8_t *ptr;
390 ptr = mem_buf;
391 /* 16 core integer registers (4 bytes each). */
392 for (i = 0; i < 16; i++)
394 *(uint32_t *)ptr = tswapl(env->regs[i]);
395 ptr += 4;
397 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
398 Not yet implemented. */
399 memset (ptr, 0, 8 * 12 + 4);
400 ptr += 8 * 12 + 4;
401 /* CPSR (4 bytes). */
402 *(uint32_t *)ptr = tswapl (cpsr_read(env));
403 ptr += 4;
405 return ptr - mem_buf;
408 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
410 int i;
411 uint8_t *ptr;
413 ptr = mem_buf;
414 /* Core integer registers. */
415 for (i = 0; i < 16; i++)
417 env->regs[i] = tswapl(*(uint32_t *)ptr);
418 ptr += 4;
420 /* Ignore FPA regs and scr. */
421 ptr += 8 * 12 + 4;
422 cpsr_write (env, tswapl(*(uint32_t *)ptr), 0xffffffff);
424 #elif defined (TARGET_MIPS)
425 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
427 int i;
428 uint8_t *ptr;
430 ptr = mem_buf;
431 for (i = 0; i < 32; i++)
433 *(uint32_t *)ptr = tswapl(env->gpr[i]);
434 ptr += 4;
437 *(uint32_t *)ptr = tswapl(env->CP0_Status);
438 ptr += 4;
440 *(uint32_t *)ptr = tswapl(env->LO);
441 ptr += 4;
443 *(uint32_t *)ptr = tswapl(env->HI);
444 ptr += 4;
446 *(uint32_t *)ptr = tswapl(env->CP0_BadVAddr);
447 ptr += 4;
449 *(uint32_t *)ptr = tswapl(env->CP0_Cause);
450 ptr += 4;
452 *(uint32_t *)ptr = tswapl(env->PC);
453 ptr += 4;
455 /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
457 return ptr - mem_buf;
460 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
462 int i;
463 uint8_t *ptr;
465 ptr = mem_buf;
466 for (i = 0; i < 32; i++)
468 env->gpr[i] = tswapl(*(uint32_t *)ptr);
469 ptr += 4;
472 env->CP0_Status = tswapl(*(uint32_t *)ptr);
473 ptr += 4;
475 env->LO = tswapl(*(uint32_t *)ptr);
476 ptr += 4;
478 env->HI = tswapl(*(uint32_t *)ptr);
479 ptr += 4;
481 env->CP0_BadVAddr = tswapl(*(uint32_t *)ptr);
482 ptr += 4;
484 env->CP0_Cause = tswapl(*(uint32_t *)ptr);
485 ptr += 4;
487 env->PC = tswapl(*(uint32_t *)ptr);
488 ptr += 4;
490 #else
491 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
493 return 0;
496 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
500 #endif
502 static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf)
504 const char *p;
505 int ch, reg_size, type;
506 char buf[4096];
507 uint8_t mem_buf[2000];
508 uint32_t *registers;
509 uint32_t addr, len;
511 #ifdef DEBUG_GDB
512 printf("command='%s'\n", line_buf);
513 #endif
514 p = line_buf;
515 ch = *p++;
516 switch(ch) {
517 case '?':
518 /* TODO: Make this return the correct value for user-mode. */
519 snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
520 put_packet(s, buf);
521 break;
522 case 'c':
523 if (*p != '\0') {
524 addr = strtoul(p, (char **)&p, 16);
525 #if defined(TARGET_I386)
526 env->eip = addr;
527 #elif defined (TARGET_PPC)
528 env->nip = addr;
529 #elif defined (TARGET_SPARC)
530 env->pc = addr;
531 env->npc = addr + 4;
532 #elif defined (TARGET_ARM)
533 env->regs[15] = addr;
534 #endif
536 #ifdef CONFIG_USER_ONLY
537 s->running_state = 1;
538 #else
539 vm_start();
540 #endif
541 return RS_IDLE;
542 case 's':
543 if (*p != '\0') {
544 addr = strtoul(p, (char **)&p, 16);
545 #if defined(TARGET_I386)
546 env->eip = addr;
547 #elif defined (TARGET_PPC)
548 env->nip = addr;
549 #elif defined (TARGET_SPARC)
550 env->pc = addr;
551 env->npc = addr + 4;
552 #elif defined (TARGET_ARM)
553 env->regs[15] = addr;
554 #endif
556 cpu_single_step(env, 1);
557 #ifdef CONFIG_USER_ONLY
558 s->running_state = 1;
559 #else
560 vm_start();
561 #endif
562 return RS_IDLE;
563 case 'g':
564 reg_size = cpu_gdb_read_registers(env, mem_buf);
565 memtohex(buf, mem_buf, reg_size);
566 put_packet(s, buf);
567 break;
568 case 'G':
569 registers = (void *)mem_buf;
570 len = strlen(p) / 2;
571 hextomem((uint8_t *)registers, p, len);
572 cpu_gdb_write_registers(env, mem_buf, len);
573 put_packet(s, "OK");
574 break;
575 case 'm':
576 addr = strtoul(p, (char **)&p, 16);
577 if (*p == ',')
578 p++;
579 len = strtoul(p, NULL, 16);
580 if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) {
581 put_packet (s, "E14");
582 } else {
583 memtohex(buf, mem_buf, len);
584 put_packet(s, buf);
586 break;
587 case 'M':
588 addr = strtoul(p, (char **)&p, 16);
589 if (*p == ',')
590 p++;
591 len = strtoul(p, (char **)&p, 16);
592 if (*p == ':')
593 p++;
594 hextomem(mem_buf, p, len);
595 if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0)
596 put_packet(s, "E14");
597 else
598 put_packet(s, "OK");
599 break;
600 case 'Z':
601 type = strtoul(p, (char **)&p, 16);
602 if (*p == ',')
603 p++;
604 addr = strtoul(p, (char **)&p, 16);
605 if (*p == ',')
606 p++;
607 len = strtoul(p, (char **)&p, 16);
608 if (type == 0 || type == 1) {
609 if (cpu_breakpoint_insert(env, addr) < 0)
610 goto breakpoint_error;
611 put_packet(s, "OK");
612 } else {
613 breakpoint_error:
614 put_packet(s, "E22");
616 break;
617 case 'z':
618 type = strtoul(p, (char **)&p, 16);
619 if (*p == ',')
620 p++;
621 addr = strtoul(p, (char **)&p, 16);
622 if (*p == ',')
623 p++;
624 len = strtoul(p, (char **)&p, 16);
625 if (type == 0 || type == 1) {
626 cpu_breakpoint_remove(env, addr);
627 put_packet(s, "OK");
628 } else {
629 goto breakpoint_error;
631 break;
632 default:
633 // unknown_command:
634 /* put empty packet */
635 buf[0] = '\0';
636 put_packet(s, buf);
637 break;
639 return RS_IDLE;
642 extern void tb_flush(CPUState *env);
644 #ifndef CONFIG_USER_ONLY
645 static void gdb_vm_stopped(void *opaque, int reason)
647 GDBState *s = opaque;
648 char buf[256];
649 int ret;
651 /* disable single step if it was enable */
652 cpu_single_step(s->env, 0);
654 if (reason == EXCP_DEBUG) {
655 tb_flush(s->env);
656 ret = SIGTRAP;
658 else
659 ret = 0;
660 snprintf(buf, sizeof(buf), "S%02x", ret);
661 put_packet(s, buf);
663 #endif
665 static void gdb_read_byte(GDBState *s, int ch)
667 CPUState *env = s->env;
668 int i, csum;
669 char reply[1];
671 #ifndef CONFIG_USER_ONLY
672 if (vm_running) {
673 /* when the CPU is running, we cannot do anything except stop
674 it when receiving a char */
675 vm_stop(EXCP_INTERRUPT);
676 } else
677 #endif
679 switch(s->state) {
680 case RS_IDLE:
681 if (ch == '$') {
682 s->line_buf_index = 0;
683 s->state = RS_GETLINE;
685 break;
686 case RS_GETLINE:
687 if (ch == '#') {
688 s->state = RS_CHKSUM1;
689 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
690 s->state = RS_IDLE;
691 } else {
692 s->line_buf[s->line_buf_index++] = ch;
694 break;
695 case RS_CHKSUM1:
696 s->line_buf[s->line_buf_index] = '\0';
697 s->line_csum = fromhex(ch) << 4;
698 s->state = RS_CHKSUM2;
699 break;
700 case RS_CHKSUM2:
701 s->line_csum |= fromhex(ch);
702 csum = 0;
703 for(i = 0; i < s->line_buf_index; i++) {
704 csum += s->line_buf[i];
706 if (s->line_csum != (csum & 0xff)) {
707 reply[0] = '-';
708 put_buffer(s, reply, 1);
709 s->state = RS_IDLE;
710 } else {
711 reply[0] = '+';
712 put_buffer(s, reply, 1);
713 s->state = gdb_handle_packet(s, env, s->line_buf);
715 break;
720 #ifdef CONFIG_USER_ONLY
722 gdb_handlesig (CPUState *env, int sig)
724 GDBState *s;
725 char buf[256];
726 int n;
728 if (gdbserver_fd < 0)
729 return sig;
731 s = &gdbserver_state;
733 /* disable single step if it was enabled */
734 cpu_single_step(env, 0);
735 tb_flush(env);
737 if (sig != 0)
739 snprintf(buf, sizeof(buf), "S%02x", sig);
740 put_packet(s, buf);
743 sig = 0;
744 s->state = RS_IDLE;
745 s->running_state = 0;
746 while (s->running_state == 0) {
747 n = read (s->fd, buf, 256);
748 if (n > 0)
750 int i;
752 for (i = 0; i < n; i++)
753 gdb_read_byte (s, buf[i]);
755 else if (n == 0 || errno != EAGAIN)
757 /* XXX: Connection closed. Should probably wait for annother
758 connection before continuing. */
759 return sig;
762 return sig;
765 /* Tell the remote gdb that the process has exited. */
766 void gdb_exit(CPUState *env, int code)
768 GDBState *s;
769 char buf[4];
771 if (gdbserver_fd < 0)
772 return;
774 s = &gdbserver_state;
776 snprintf(buf, sizeof(buf), "W%02x", code);
777 put_packet(s, buf);
780 #else
781 static void gdb_read(void *opaque)
783 GDBState *s = opaque;
784 int i, size;
785 uint8_t buf[4096];
787 size = read(s->fd, buf, sizeof(buf));
788 if (size < 0)
789 return;
790 if (size == 0) {
791 /* end of connection */
792 qemu_del_vm_stop_handler(gdb_vm_stopped, s);
793 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
794 qemu_free(s);
795 vm_start();
796 } else {
797 for(i = 0; i < size; i++)
798 gdb_read_byte(s, buf[i]);
802 #endif
804 static void gdb_accept(void *opaque)
806 GDBState *s;
807 struct sockaddr_in sockaddr;
808 socklen_t len;
809 int val, fd;
811 for(;;) {
812 len = sizeof(sockaddr);
813 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
814 if (fd < 0 && errno != EINTR) {
815 perror("accept");
816 return;
817 } else if (fd >= 0) {
818 break;
822 /* set short latency */
823 val = 1;
824 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
826 #ifdef CONFIG_USER_ONLY
827 s = &gdbserver_state;
828 memset (s, 0, sizeof (GDBState));
829 #else
830 s = qemu_mallocz(sizeof(GDBState));
831 if (!s) {
832 close(fd);
833 return;
835 #endif
836 s->env = first_cpu; /* XXX: allow to change CPU */
837 s->fd = fd;
839 fcntl(fd, F_SETFL, O_NONBLOCK);
841 #ifndef CONFIG_USER_ONLY
842 /* stop the VM */
843 vm_stop(EXCP_INTERRUPT);
845 /* start handling I/O */
846 qemu_set_fd_handler(s->fd, gdb_read, NULL, s);
847 /* when the VM is stopped, the following callback is called */
848 qemu_add_vm_stop_handler(gdb_vm_stopped, s);
849 #endif
852 static int gdbserver_open(int port)
854 struct sockaddr_in sockaddr;
855 int fd, val, ret;
857 fd = socket(PF_INET, SOCK_STREAM, 0);
858 if (fd < 0) {
859 perror("socket");
860 return -1;
863 /* allow fast reuse */
864 val = 1;
865 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
867 sockaddr.sin_family = AF_INET;
868 sockaddr.sin_port = htons(port);
869 sockaddr.sin_addr.s_addr = 0;
870 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
871 if (ret < 0) {
872 perror("bind");
873 return -1;
875 ret = listen(fd, 0);
876 if (ret < 0) {
877 perror("listen");
878 return -1;
880 #ifndef CONFIG_USER_ONLY
881 fcntl(fd, F_SETFL, O_NONBLOCK);
882 #endif
883 return fd;
886 int gdbserver_start(int port)
888 gdbserver_fd = gdbserver_open(port);
889 if (gdbserver_fd < 0)
890 return -1;
891 /* accept connections */
892 #ifdef CONFIG_USER_ONLY
893 gdb_accept (NULL);
894 #else
895 qemu_set_fd_handler(gdbserver_fd, gdb_accept, NULL, NULL);
896 #endif
897 return 0;