Enable SH bFLT loader.
[qemu/mini2440.git] / gdbstub.c
blob37241ce589f74fae073adad627c9f92dec033ef0
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 #include "config.h"
21 #ifdef CONFIG_USER_ONLY
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <fcntl.h>
30 #include "qemu.h"
31 #else
32 #include "vl.h"
33 #endif
35 #include "qemu_socket.h"
36 #ifdef _WIN32
37 /* XXX: these constants may be independent of the host ones even for Unix */
38 #ifndef SIGTRAP
39 #define SIGTRAP 5
40 #endif
41 #ifndef SIGINT
42 #define SIGINT 2
43 #endif
44 #else
45 #include <signal.h>
46 #endif
48 //#define DEBUG_GDB
50 enum RSState {
51 RS_IDLE,
52 RS_GETLINE,
53 RS_CHKSUM1,
54 RS_CHKSUM2,
56 /* XXX: This is not thread safe. Do we care? */
57 static int gdbserver_fd = -1;
59 typedef struct GDBState {
60 CPUState *env; /* current CPU */
61 enum RSState state; /* parsing state */
62 int fd;
63 char line_buf[4096];
64 int line_buf_index;
65 int line_csum;
66 #ifdef CONFIG_USER_ONLY
67 int running_state;
68 #endif
69 } GDBState;
71 #ifdef CONFIG_USER_ONLY
72 /* XXX: remove this hack. */
73 static GDBState gdbserver_state;
74 #endif
76 static int get_char(GDBState *s)
78 uint8_t ch;
79 int ret;
81 for(;;) {
82 ret = recv(s->fd, &ch, 1, 0);
83 if (ret < 0) {
84 if (errno != EINTR && errno != EAGAIN)
85 return -1;
86 } else if (ret == 0) {
87 return -1;
88 } else {
89 break;
92 return ch;
95 static void put_buffer(GDBState *s, const uint8_t *buf, int len)
97 int ret;
99 while (len > 0) {
100 ret = send(s->fd, buf, len, 0);
101 if (ret < 0) {
102 if (errno != EINTR && errno != EAGAIN)
103 return;
104 } else {
105 buf += ret;
106 len -= ret;
111 static inline int fromhex(int v)
113 if (v >= '0' && v <= '9')
114 return v - '0';
115 else if (v >= 'A' && v <= 'F')
116 return v - 'A' + 10;
117 else if (v >= 'a' && v <= 'f')
118 return v - 'a' + 10;
119 else
120 return 0;
123 static inline int tohex(int v)
125 if (v < 10)
126 return v + '0';
127 else
128 return v - 10 + 'a';
131 static void memtohex(char *buf, const uint8_t *mem, int len)
133 int i, c;
134 char *q;
135 q = buf;
136 for(i = 0; i < len; i++) {
137 c = mem[i];
138 *q++ = tohex(c >> 4);
139 *q++ = tohex(c & 0xf);
141 *q = '\0';
144 static void hextomem(uint8_t *mem, const char *buf, int len)
146 int i;
148 for(i = 0; i < len; i++) {
149 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
150 buf += 2;
154 /* return -1 if error, 0 if OK */
155 static int put_packet(GDBState *s, char *buf)
157 char buf1[3];
158 int len, csum, ch, i;
160 #ifdef DEBUG_GDB
161 printf("reply='%s'\n", buf);
162 #endif
164 for(;;) {
165 buf1[0] = '$';
166 put_buffer(s, buf1, 1);
167 len = strlen(buf);
168 put_buffer(s, buf, len);
169 csum = 0;
170 for(i = 0; i < len; i++) {
171 csum += buf[i];
173 buf1[0] = '#';
174 buf1[1] = tohex((csum >> 4) & 0xf);
175 buf1[2] = tohex((csum) & 0xf);
177 put_buffer(s, buf1, 3);
179 ch = get_char(s);
180 if (ch < 0)
181 return -1;
182 if (ch == '+')
183 break;
185 return 0;
188 #if defined(TARGET_I386)
190 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
192 uint32_t *registers = (uint32_t *)mem_buf;
193 int i, fpus;
195 for(i = 0; i < 8; i++) {
196 registers[i] = env->regs[i];
198 registers[8] = env->eip;
199 registers[9] = env->eflags;
200 registers[10] = env->segs[R_CS].selector;
201 registers[11] = env->segs[R_SS].selector;
202 registers[12] = env->segs[R_DS].selector;
203 registers[13] = env->segs[R_ES].selector;
204 registers[14] = env->segs[R_FS].selector;
205 registers[15] = env->segs[R_GS].selector;
206 /* XXX: convert floats */
207 for(i = 0; i < 8; i++) {
208 memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10);
210 registers[36] = env->fpuc;
211 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
212 registers[37] = fpus;
213 registers[38] = 0; /* XXX: convert tags */
214 registers[39] = 0; /* fiseg */
215 registers[40] = 0; /* fioff */
216 registers[41] = 0; /* foseg */
217 registers[42] = 0; /* fooff */
218 registers[43] = 0; /* fop */
220 for(i = 0; i < 16; i++)
221 tswapls(&registers[i]);
222 for(i = 36; i < 44; i++)
223 tswapls(&registers[i]);
224 return 44 * 4;
227 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
229 uint32_t *registers = (uint32_t *)mem_buf;
230 int i;
232 for(i = 0; i < 8; i++) {
233 env->regs[i] = tswapl(registers[i]);
235 env->eip = tswapl(registers[8]);
236 env->eflags = tswapl(registers[9]);
237 #if defined(CONFIG_USER_ONLY)
238 #define LOAD_SEG(index, sreg)\
239 if (tswapl(registers[index]) != env->segs[sreg].selector)\
240 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
241 LOAD_SEG(10, R_CS);
242 LOAD_SEG(11, R_SS);
243 LOAD_SEG(12, R_DS);
244 LOAD_SEG(13, R_ES);
245 LOAD_SEG(14, R_FS);
246 LOAD_SEG(15, R_GS);
247 #endif
250 #elif defined (TARGET_PPC)
251 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
253 uint32_t *registers = (uint32_t *)mem_buf, tmp;
254 int i;
256 /* fill in gprs */
257 for(i = 0; i < 32; i++) {
258 registers[i] = tswapl(env->gpr[i]);
260 /* fill in fprs */
261 for (i = 0; i < 32; i++) {
262 registers[(i * 2) + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
263 registers[(i * 2) + 33] = tswapl(*((uint32_t *)&env->fpr[i] + 1));
265 /* nip, msr, ccr, lnk, ctr, xer, mq */
266 registers[96] = tswapl(env->nip);
267 registers[97] = tswapl(do_load_msr(env));
268 tmp = 0;
269 for (i = 0; i < 8; i++)
270 tmp |= env->crf[i] << (32 - ((i + 1) * 4));
271 registers[98] = tswapl(tmp);
272 registers[99] = tswapl(env->lr);
273 registers[100] = tswapl(env->ctr);
274 registers[101] = tswapl(do_load_xer(env));
275 registers[102] = 0;
277 return 103 * 4;
280 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
282 uint32_t *registers = (uint32_t *)mem_buf;
283 int i;
285 /* fill in gprs */
286 for (i = 0; i < 32; i++) {
287 env->gpr[i] = tswapl(registers[i]);
289 /* fill in fprs */
290 for (i = 0; i < 32; i++) {
291 *((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]);
292 *((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]);
294 /* nip, msr, ccr, lnk, ctr, xer, mq */
295 env->nip = tswapl(registers[96]);
296 do_store_msr(env, tswapl(registers[97]));
297 registers[98] = tswapl(registers[98]);
298 for (i = 0; i < 8; i++)
299 env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF;
300 env->lr = tswapl(registers[99]);
301 env->ctr = tswapl(registers[100]);
302 do_store_xer(env, tswapl(registers[101]));
304 #elif defined (TARGET_SPARC)
305 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
307 target_ulong *registers = (target_ulong *)mem_buf;
308 int i;
310 /* fill in g0..g7 */
311 for(i = 0; i < 8; i++) {
312 registers[i] = tswapl(env->gregs[i]);
314 /* fill in register window */
315 for(i = 0; i < 24; i++) {
316 registers[i + 8] = tswapl(env->regwptr[i]);
318 /* fill in fprs */
319 for (i = 0; i < 32; i++) {
320 registers[i + 32] = tswapl(*((uint32_t *)&env->fpr[i]));
322 #ifndef TARGET_SPARC64
323 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
324 registers[64] = tswapl(env->y);
326 target_ulong tmp;
328 tmp = GET_PSR(env);
329 registers[65] = tswapl(tmp);
331 registers[66] = tswapl(env->wim);
332 registers[67] = tswapl(env->tbr);
333 registers[68] = tswapl(env->pc);
334 registers[69] = tswapl(env->npc);
335 registers[70] = tswapl(env->fsr);
336 registers[71] = 0; /* csr */
337 registers[72] = 0;
338 return 73 * sizeof(target_ulong);
339 #else
340 for (i = 0; i < 32; i += 2) {
341 registers[i/2 + 64] = tswapl(*((uint64_t *)&env->fpr[i]));
343 registers[81] = tswapl(env->pc);
344 registers[82] = tswapl(env->npc);
345 registers[83] = tswapl(env->tstate[env->tl]);
346 registers[84] = tswapl(env->fsr);
347 registers[85] = tswapl(env->fprs);
348 registers[86] = tswapl(env->y);
349 return 87 * sizeof(target_ulong);
350 #endif
353 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
355 target_ulong *registers = (target_ulong *)mem_buf;
356 int i;
358 /* fill in g0..g7 */
359 for(i = 0; i < 7; i++) {
360 env->gregs[i] = tswapl(registers[i]);
362 /* fill in register window */
363 for(i = 0; i < 24; i++) {
364 env->regwptr[i] = tswapl(registers[i + 8]);
366 /* fill in fprs */
367 for (i = 0; i < 32; i++) {
368 *((uint32_t *)&env->fpr[i]) = tswapl(registers[i + 32]);
370 #ifndef TARGET_SPARC64
371 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
372 env->y = tswapl(registers[64]);
373 PUT_PSR(env, tswapl(registers[65]));
374 env->wim = tswapl(registers[66]);
375 env->tbr = tswapl(registers[67]);
376 env->pc = tswapl(registers[68]);
377 env->npc = tswapl(registers[69]);
378 env->fsr = tswapl(registers[70]);
379 #else
380 for (i = 0; i < 32; i += 2) {
381 uint64_t tmp;
382 tmp = tswapl(registers[i/2 + 64]) << 32;
383 tmp |= tswapl(registers[i/2 + 64 + 1]);
384 *((uint64_t *)&env->fpr[i]) = tmp;
386 env->pc = tswapl(registers[81]);
387 env->npc = tswapl(registers[82]);
388 env->tstate[env->tl] = tswapl(registers[83]);
389 env->fsr = tswapl(registers[84]);
390 env->fprs = tswapl(registers[85]);
391 env->y = tswapl(registers[86]);
392 #endif
394 #elif defined (TARGET_ARM)
395 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
397 int i;
398 uint8_t *ptr;
400 ptr = mem_buf;
401 /* 16 core integer registers (4 bytes each). */
402 for (i = 0; i < 16; i++)
404 *(uint32_t *)ptr = tswapl(env->regs[i]);
405 ptr += 4;
407 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
408 Not yet implemented. */
409 memset (ptr, 0, 8 * 12 + 4);
410 ptr += 8 * 12 + 4;
411 /* CPSR (4 bytes). */
412 *(uint32_t *)ptr = tswapl (cpsr_read(env));
413 ptr += 4;
415 return ptr - mem_buf;
418 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
420 int i;
421 uint8_t *ptr;
423 ptr = mem_buf;
424 /* Core integer registers. */
425 for (i = 0; i < 16; i++)
427 env->regs[i] = tswapl(*(uint32_t *)ptr);
428 ptr += 4;
430 /* Ignore FPA regs and scr. */
431 ptr += 8 * 12 + 4;
432 cpsr_write (env, tswapl(*(uint32_t *)ptr), 0xffffffff);
434 #elif defined (TARGET_MIPS)
435 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
437 int i;
438 uint8_t *ptr;
440 ptr = mem_buf;
441 for (i = 0; i < 32; i++)
443 *(uint32_t *)ptr = tswapl(env->gpr[i]);
444 ptr += 4;
447 *(uint32_t *)ptr = tswapl(env->CP0_Status);
448 ptr += 4;
450 *(uint32_t *)ptr = tswapl(env->LO);
451 ptr += 4;
453 *(uint32_t *)ptr = tswapl(env->HI);
454 ptr += 4;
456 *(uint32_t *)ptr = tswapl(env->CP0_BadVAddr);
457 ptr += 4;
459 *(uint32_t *)ptr = tswapl(env->CP0_Cause);
460 ptr += 4;
462 *(uint32_t *)ptr = tswapl(env->PC);
463 ptr += 4;
465 /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
467 return ptr - mem_buf;
470 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
472 int i;
473 uint8_t *ptr;
475 ptr = mem_buf;
476 for (i = 0; i < 32; i++)
478 env->gpr[i] = tswapl(*(uint32_t *)ptr);
479 ptr += 4;
482 env->CP0_Status = tswapl(*(uint32_t *)ptr);
483 ptr += 4;
485 env->LO = tswapl(*(uint32_t *)ptr);
486 ptr += 4;
488 env->HI = tswapl(*(uint32_t *)ptr);
489 ptr += 4;
491 env->CP0_BadVAddr = tswapl(*(uint32_t *)ptr);
492 ptr += 4;
494 env->CP0_Cause = tswapl(*(uint32_t *)ptr);
495 ptr += 4;
497 env->PC = tswapl(*(uint32_t *)ptr);
498 ptr += 4;
500 #elif defined (TARGET_SH4)
501 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
503 uint32_t *ptr = (uint32_t *)mem_buf;
504 int i;
506 #define SAVE(x) *ptr++=tswapl(x)
507 for (i = 0; i < 16; i++) SAVE(env->gregs[i]);
508 SAVE (env->pc);
509 SAVE (env->pr);
510 SAVE (env->gbr);
511 SAVE (env->vbr);
512 SAVE (env->mach);
513 SAVE (env->macl);
514 SAVE (env->sr);
515 SAVE (0); /* TICKS */
516 SAVE (0); /* STALLS */
517 SAVE (0); /* CYCLES */
518 SAVE (0); /* INSTS */
519 SAVE (0); /* PLR */
521 return ((uint8_t *)ptr - mem_buf);
524 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
526 uint32_t *ptr = (uint32_t *)mem_buf;
527 int i;
529 #define LOAD(x) (x)=*ptr++;
530 for (i = 0; i < 16; i++) LOAD(env->gregs[i]);
531 LOAD (env->pc);
532 LOAD (env->pr);
533 LOAD (env->gbr);
534 LOAD (env->vbr);
535 LOAD (env->mach);
536 LOAD (env->macl);
537 LOAD (env->sr);
539 #else
540 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
542 return 0;
545 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
549 #endif
551 static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf)
553 const char *p;
554 int ch, reg_size, type;
555 char buf[4096];
556 uint8_t mem_buf[2000];
557 uint32_t *registers;
558 uint32_t addr, len;
560 #ifdef DEBUG_GDB
561 printf("command='%s'\n", line_buf);
562 #endif
563 p = line_buf;
564 ch = *p++;
565 switch(ch) {
566 case '?':
567 /* TODO: Make this return the correct value for user-mode. */
568 snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
569 put_packet(s, buf);
570 break;
571 case 'c':
572 if (*p != '\0') {
573 addr = strtoul(p, (char **)&p, 16);
574 #if defined(TARGET_I386)
575 env->eip = addr;
576 #elif defined (TARGET_PPC)
577 env->nip = addr;
578 #elif defined (TARGET_SPARC)
579 env->pc = addr;
580 env->npc = addr + 4;
581 #elif defined (TARGET_ARM)
582 env->regs[15] = addr;
583 #elif defined (TARGET_SH4)
584 env->pc = addr;
585 #endif
587 #ifdef CONFIG_USER_ONLY
588 s->running_state = 1;
589 #else
590 vm_start();
591 #endif
592 return RS_IDLE;
593 case 's':
594 if (*p != '\0') {
595 addr = strtoul(p, (char **)&p, 16);
596 #if defined(TARGET_I386)
597 env->eip = addr;
598 #elif defined (TARGET_PPC)
599 env->nip = addr;
600 #elif defined (TARGET_SPARC)
601 env->pc = addr;
602 env->npc = addr + 4;
603 #elif defined (TARGET_ARM)
604 env->regs[15] = addr;
605 #elif defined (TARGET_SH4)
606 env->pc = addr;
607 #endif
609 cpu_single_step(env, 1);
610 #ifdef CONFIG_USER_ONLY
611 s->running_state = 1;
612 #else
613 vm_start();
614 #endif
615 return RS_IDLE;
616 case 'g':
617 reg_size = cpu_gdb_read_registers(env, mem_buf);
618 memtohex(buf, mem_buf, reg_size);
619 put_packet(s, buf);
620 break;
621 case 'G':
622 registers = (void *)mem_buf;
623 len = strlen(p) / 2;
624 hextomem((uint8_t *)registers, p, len);
625 cpu_gdb_write_registers(env, mem_buf, len);
626 put_packet(s, "OK");
627 break;
628 case 'm':
629 addr = strtoul(p, (char **)&p, 16);
630 if (*p == ',')
631 p++;
632 len = strtoul(p, NULL, 16);
633 if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0) {
634 put_packet (s, "E14");
635 } else {
636 memtohex(buf, mem_buf, len);
637 put_packet(s, buf);
639 break;
640 case 'M':
641 addr = strtoul(p, (char **)&p, 16);
642 if (*p == ',')
643 p++;
644 len = strtoul(p, (char **)&p, 16);
645 if (*p == ':')
646 p++;
647 hextomem(mem_buf, p, len);
648 if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0)
649 put_packet(s, "E14");
650 else
651 put_packet(s, "OK");
652 break;
653 case 'Z':
654 type = strtoul(p, (char **)&p, 16);
655 if (*p == ',')
656 p++;
657 addr = strtoul(p, (char **)&p, 16);
658 if (*p == ',')
659 p++;
660 len = strtoul(p, (char **)&p, 16);
661 if (type == 0 || type == 1) {
662 if (cpu_breakpoint_insert(env, addr) < 0)
663 goto breakpoint_error;
664 put_packet(s, "OK");
665 } else {
666 breakpoint_error:
667 put_packet(s, "E22");
669 break;
670 case 'z':
671 type = strtoul(p, (char **)&p, 16);
672 if (*p == ',')
673 p++;
674 addr = strtoul(p, (char **)&p, 16);
675 if (*p == ',')
676 p++;
677 len = strtoul(p, (char **)&p, 16);
678 if (type == 0 || type == 1) {
679 cpu_breakpoint_remove(env, addr);
680 put_packet(s, "OK");
681 } else {
682 goto breakpoint_error;
684 break;
685 #ifdef CONFIG_USER_ONLY
686 case 'q':
687 if (strncmp(p, "Offsets", 7) == 0) {
688 TaskState *ts = env->opaque;
690 sprintf(buf, "Text=%x;Data=%x;Bss=%x", ts->info->code_offset,
691 ts->info->data_offset, ts->info->data_offset);
692 put_packet(s, buf);
693 break;
695 /* Fall through. */
696 #endif
697 default:
698 // unknown_command:
699 /* put empty packet */
700 buf[0] = '\0';
701 put_packet(s, buf);
702 break;
704 return RS_IDLE;
707 extern void tb_flush(CPUState *env);
709 #ifndef CONFIG_USER_ONLY
710 static void gdb_vm_stopped(void *opaque, int reason)
712 GDBState *s = opaque;
713 char buf[256];
714 int ret;
716 /* disable single step if it was enable */
717 cpu_single_step(s->env, 0);
719 if (reason == EXCP_DEBUG) {
720 tb_flush(s->env);
721 ret = SIGTRAP;
722 } else if (reason == EXCP_INTERRUPT) {
723 ret = SIGINT;
724 } else {
725 ret = 0;
727 snprintf(buf, sizeof(buf), "S%02x", ret);
728 put_packet(s, buf);
730 #endif
732 static void gdb_read_byte(GDBState *s, int ch)
734 CPUState *env = s->env;
735 int i, csum;
736 char reply[1];
738 #ifndef CONFIG_USER_ONLY
739 if (vm_running) {
740 /* when the CPU is running, we cannot do anything except stop
741 it when receiving a char */
742 vm_stop(EXCP_INTERRUPT);
743 } else
744 #endif
746 switch(s->state) {
747 case RS_IDLE:
748 if (ch == '$') {
749 s->line_buf_index = 0;
750 s->state = RS_GETLINE;
752 break;
753 case RS_GETLINE:
754 if (ch == '#') {
755 s->state = RS_CHKSUM1;
756 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
757 s->state = RS_IDLE;
758 } else {
759 s->line_buf[s->line_buf_index++] = ch;
761 break;
762 case RS_CHKSUM1:
763 s->line_buf[s->line_buf_index] = '\0';
764 s->line_csum = fromhex(ch) << 4;
765 s->state = RS_CHKSUM2;
766 break;
767 case RS_CHKSUM2:
768 s->line_csum |= fromhex(ch);
769 csum = 0;
770 for(i = 0; i < s->line_buf_index; i++) {
771 csum += s->line_buf[i];
773 if (s->line_csum != (csum & 0xff)) {
774 reply[0] = '-';
775 put_buffer(s, reply, 1);
776 s->state = RS_IDLE;
777 } else {
778 reply[0] = '+';
779 put_buffer(s, reply, 1);
780 s->state = gdb_handle_packet(s, env, s->line_buf);
782 break;
787 #ifdef CONFIG_USER_ONLY
789 gdb_handlesig (CPUState *env, int sig)
791 GDBState *s;
792 char buf[256];
793 int n;
795 if (gdbserver_fd < 0)
796 return sig;
798 s = &gdbserver_state;
800 /* disable single step if it was enabled */
801 cpu_single_step(env, 0);
802 tb_flush(env);
804 if (sig != 0)
806 snprintf(buf, sizeof(buf), "S%02x", sig);
807 put_packet(s, buf);
810 sig = 0;
811 s->state = RS_IDLE;
812 s->running_state = 0;
813 while (s->running_state == 0) {
814 n = read (s->fd, buf, 256);
815 if (n > 0)
817 int i;
819 for (i = 0; i < n; i++)
820 gdb_read_byte (s, buf[i]);
822 else if (n == 0 || errno != EAGAIN)
824 /* XXX: Connection closed. Should probably wait for annother
825 connection before continuing. */
826 return sig;
829 return sig;
832 /* Tell the remote gdb that the process has exited. */
833 void gdb_exit(CPUState *env, int code)
835 GDBState *s;
836 char buf[4];
838 if (gdbserver_fd < 0)
839 return;
841 s = &gdbserver_state;
843 snprintf(buf, sizeof(buf), "W%02x", code);
844 put_packet(s, buf);
847 #else
848 static void gdb_read(void *opaque)
850 GDBState *s = opaque;
851 int i, size;
852 uint8_t buf[4096];
854 size = recv(s->fd, buf, sizeof(buf), 0);
855 if (size < 0)
856 return;
857 if (size == 0) {
858 /* end of connection */
859 qemu_del_vm_stop_handler(gdb_vm_stopped, s);
860 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
861 qemu_free(s);
862 vm_start();
863 } else {
864 for(i = 0; i < size; i++)
865 gdb_read_byte(s, buf[i]);
869 #endif
871 static void gdb_accept(void *opaque)
873 GDBState *s;
874 struct sockaddr_in sockaddr;
875 socklen_t len;
876 int val, fd;
878 for(;;) {
879 len = sizeof(sockaddr);
880 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
881 if (fd < 0 && errno != EINTR) {
882 perror("accept");
883 return;
884 } else if (fd >= 0) {
885 break;
889 /* set short latency */
890 val = 1;
891 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
893 #ifdef CONFIG_USER_ONLY
894 s = &gdbserver_state;
895 memset (s, 0, sizeof (GDBState));
896 #else
897 s = qemu_mallocz(sizeof(GDBState));
898 if (!s) {
899 close(fd);
900 return;
902 #endif
903 s->env = first_cpu; /* XXX: allow to change CPU */
904 s->fd = fd;
906 #ifdef CONFIG_USER_ONLY
907 fcntl(fd, F_SETFL, O_NONBLOCK);
908 #else
909 socket_set_nonblock(fd);
911 /* stop the VM */
912 vm_stop(EXCP_INTERRUPT);
914 /* start handling I/O */
915 qemu_set_fd_handler(s->fd, gdb_read, NULL, s);
916 /* when the VM is stopped, the following callback is called */
917 qemu_add_vm_stop_handler(gdb_vm_stopped, s);
918 #endif
921 static int gdbserver_open(int port)
923 struct sockaddr_in sockaddr;
924 int fd, val, ret;
926 fd = socket(PF_INET, SOCK_STREAM, 0);
927 if (fd < 0) {
928 perror("socket");
929 return -1;
932 /* allow fast reuse */
933 val = 1;
934 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
936 sockaddr.sin_family = AF_INET;
937 sockaddr.sin_port = htons(port);
938 sockaddr.sin_addr.s_addr = 0;
939 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
940 if (ret < 0) {
941 perror("bind");
942 return -1;
944 ret = listen(fd, 0);
945 if (ret < 0) {
946 perror("listen");
947 return -1;
949 #ifndef CONFIG_USER_ONLY
950 socket_set_nonblock(fd);
951 #endif
952 return fd;
955 int gdbserver_start(int port)
957 gdbserver_fd = gdbserver_open(port);
958 if (gdbserver_fd < 0)
959 return -1;
960 /* accept connections */
961 #ifdef CONFIG_USER_ONLY
962 gdb_accept (NULL);
963 #else
964 qemu_set_fd_handler(gdbserver_fd, gdb_accept, NULL, NULL);
965 #endif
966 return 0;