Update
[gdb.git] / gdb / gdbserver / server.c
blob79bde643bdc7b93d0113bc31f5ec83ef176a10ea
1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "server.h"
22 #if HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 #if HAVE_SIGNAL_H
26 #include <signal.h>
27 #endif
28 #if HAVE_SYS_WAIT_H
29 #include <sys/wait.h>
30 #endif
32 unsigned long cont_thread;
33 unsigned long general_thread;
34 unsigned long step_thread;
35 unsigned long thread_from_wait;
36 unsigned long old_thread_from_wait;
37 int server_waiting;
39 static int extended_protocol;
40 static int attached;
41 static int response_needed;
42 static int exit_requested;
44 static char **program_argv;
46 /* Enable miscellaneous debugging output. The name is historical - it
47 was originally used to debug LinuxThreads support. */
48 int debug_threads;
50 int pass_signals[TARGET_SIGNAL_LAST];
52 jmp_buf toplevel;
54 /* The PID of the originally created or attached inferior. Used to
55 send signals to the process when GDB sends us an asynchronous interrupt
56 (user hitting Control-C in the client), and to wait for the child to exit
57 when no longer debugging it. */
59 unsigned long signal_pid;
61 #ifdef SIGTTOU
62 /* A file descriptor for the controlling terminal. */
63 int terminal_fd;
65 /* TERMINAL_FD's original foreground group. */
66 pid_t old_foreground_pgrp;
68 /* Hand back terminal ownership to the original foreground group. */
70 static void
71 restore_old_foreground_pgrp (void)
73 tcsetpgrp (terminal_fd, old_foreground_pgrp);
75 #endif
77 static int
78 target_running (void)
80 return all_threads.head != NULL;
83 static int
84 start_inferior (char *argv[], char *statusptr)
86 attached = 0;
88 #ifdef SIGTTOU
89 signal (SIGTTOU, SIG_DFL);
90 signal (SIGTTIN, SIG_DFL);
91 #endif
93 signal_pid = create_inferior (argv[0], argv);
95 /* FIXME: we don't actually know at this point that the create
96 actually succeeded. We won't know that until we wait. */
97 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
98 signal_pid);
99 fflush (stderr);
101 #ifdef SIGTTOU
102 signal (SIGTTOU, SIG_IGN);
103 signal (SIGTTIN, SIG_IGN);
104 terminal_fd = fileno (stderr);
105 old_foreground_pgrp = tcgetpgrp (terminal_fd);
106 tcsetpgrp (terminal_fd, signal_pid);
107 atexit (restore_old_foreground_pgrp);
108 #endif
110 /* Wait till we are at 1st instruction in program, return signal
111 number (assuming success). */
112 return mywait (statusptr, 0);
115 static int
116 attach_inferior (int pid, char *statusptr, int *sigptr)
118 /* myattach should return -1 if attaching is unsupported,
119 0 if it succeeded, and call error() otherwise. */
121 if (myattach (pid) != 0)
122 return -1;
124 attached = 1;
126 fprintf (stderr, "Attached; pid = %d\n", pid);
127 fflush (stderr);
129 /* FIXME - It may be that we should get the SIGNAL_PID from the
130 attach function, so that it can be the main thread instead of
131 whichever we were told to attach to. */
132 signal_pid = pid;
134 *sigptr = mywait (statusptr, 0);
136 /* GDB knows to ignore the first SIGSTOP after attaching to a running
137 process using the "attach" command, but this is different; it's
138 just using "target remote". Pretend it's just starting up. */
139 if (*statusptr == 'T' && *sigptr == TARGET_SIGNAL_STOP)
140 *sigptr = TARGET_SIGNAL_TRAP;
142 return 0;
145 extern int remote_debug;
147 /* Decode a qXfer read request. Return 0 if everything looks OK,
148 or -1 otherwise. */
150 static int
151 decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
153 /* Extract and NUL-terminate the annex. */
154 *annex = buf;
155 while (*buf && *buf != ':')
156 buf++;
157 if (*buf == '\0')
158 return -1;
159 *buf++ = 0;
161 /* After the read marker and annex, qXfer looks like a
162 traditional 'm' packet. */
163 decode_m_packet (buf, ofs, len);
165 return 0;
168 /* Write the response to a successful qXfer read. Returns the
169 length of the (binary) data stored in BUF, corresponding
170 to as much of DATA/LEN as we could fit. IS_MORE controls
171 the first character of the response. */
172 static int
173 write_qxfer_response (char *buf, const void *data, int len, int is_more)
175 int out_len;
177 if (is_more)
178 buf[0] = 'm';
179 else
180 buf[0] = 'l';
182 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
183 PBUFSIZ - 2) + 1;
186 /* Handle all of the extended 'Q' packets. */
187 void
188 handle_general_set (char *own_buf)
190 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
192 int numsigs = (int) TARGET_SIGNAL_LAST, i;
193 const char *p = own_buf + strlen ("QPassSignals:");
194 CORE_ADDR cursig;
196 p = decode_address_to_semicolon (&cursig, p);
197 for (i = 0; i < numsigs; i++)
199 if (i == cursig)
201 pass_signals[i] = 1;
202 if (*p == '\0')
203 /* Keep looping, to clear the remaining signals. */
204 cursig = -1;
205 else
206 p = decode_address_to_semicolon (&cursig, p);
208 else
209 pass_signals[i] = 0;
211 strcpy (own_buf, "OK");
212 return;
215 /* Otherwise we didn't know what packet it was. Say we didn't
216 understand it. */
217 own_buf[0] = 0;
220 static const char *
221 get_features_xml (const char *annex)
223 static int features_supported = -1;
224 static char *document;
226 #ifdef USE_XML
227 extern const char *const xml_builtin[][2];
228 int i;
230 /* Look for the annex. */
231 for (i = 0; xml_builtin[i][0] != NULL; i++)
232 if (strcmp (annex, xml_builtin[i][0]) == 0)
233 break;
235 if (xml_builtin[i][0] != NULL)
236 return xml_builtin[i][1];
237 #endif
239 if (strcmp (annex, "target.xml") != 0)
240 return NULL;
242 if (features_supported == -1)
244 const char *arch = NULL;
245 if (the_target->arch_string != NULL)
246 arch = (*the_target->arch_string) ();
248 if (arch == NULL)
249 features_supported = 0;
250 else
252 features_supported = 1;
253 document = malloc (64 + strlen (arch));
254 snprintf (document, 64 + strlen (arch),
255 "<target><architecture>%s</architecture></target>",
256 arch);
260 return document;
263 void
264 monitor_show_help (void)
266 monitor_output ("The following monitor commands are supported:\n");
267 monitor_output (" set debug <0|1>\n");
268 monitor_output (" Enable general debugging messages\n");
269 monitor_output (" set remote-debug <0|1>\n");
270 monitor_output (" Enable remote protocol debugging messages\n");
271 monitor_output (" exit\n");
272 monitor_output (" Quit GDBserver\n");
275 #define require_running(BUF) \
276 if (!target_running ()) \
278 write_enn (BUF); \
279 return; \
282 /* Handle all of the extended 'q' packets. */
283 void
284 handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
286 static struct inferior_list_entry *thread_ptr;
288 /* Reply the current thread id. */
289 if (strcmp ("qC", own_buf) == 0)
291 require_running (own_buf);
292 thread_ptr = all_threads.head;
293 sprintf (own_buf, "QC%x",
294 thread_to_gdb_id ((struct thread_info *)thread_ptr));
295 return;
298 if (strcmp ("qSymbol::", own_buf) == 0)
300 if (target_running () && the_target->look_up_symbols != NULL)
301 (*the_target->look_up_symbols) ();
303 strcpy (own_buf, "OK");
304 return;
307 if (strcmp ("qfThreadInfo", own_buf) == 0)
309 require_running (own_buf);
310 thread_ptr = all_threads.head;
311 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
312 thread_ptr = thread_ptr->next;
313 return;
316 if (strcmp ("qsThreadInfo", own_buf) == 0)
318 require_running (own_buf);
319 if (thread_ptr != NULL)
321 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
322 thread_ptr = thread_ptr->next;
323 return;
325 else
327 sprintf (own_buf, "l");
328 return;
332 if (the_target->read_offsets != NULL
333 && strcmp ("qOffsets", own_buf) == 0)
335 CORE_ADDR text, data;
337 require_running (own_buf);
338 if (the_target->read_offsets (&text, &data))
339 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
340 (long)text, (long)data, (long)data);
341 else
342 write_enn (own_buf);
344 return;
347 if (the_target->qxfer_spu != NULL
348 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
350 char *annex;
351 int n;
352 unsigned int len;
353 CORE_ADDR ofs;
354 unsigned char *spu_buf;
356 require_running (own_buf);
357 strcpy (own_buf, "E00");
358 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
359 return;
360 if (len > PBUFSIZ - 2)
361 len = PBUFSIZ - 2;
362 spu_buf = malloc (len + 1);
363 if (!spu_buf)
364 return;
366 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
367 if (n < 0)
368 write_enn (own_buf);
369 else if (n > len)
370 *new_packet_len_p = write_qxfer_response
371 (own_buf, spu_buf, len, 1);
372 else
373 *new_packet_len_p = write_qxfer_response
374 (own_buf, spu_buf, n, 0);
376 free (spu_buf);
377 return;
380 if (the_target->qxfer_spu != NULL
381 && strncmp ("qXfer:spu:write:", own_buf, 16) == 0)
383 char *annex;
384 int n;
385 unsigned int len;
386 CORE_ADDR ofs;
387 unsigned char *spu_buf;
389 require_running (own_buf);
390 strcpy (own_buf, "E00");
391 spu_buf = malloc (packet_len - 15);
392 if (!spu_buf)
393 return;
394 if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
395 &ofs, &len, spu_buf) < 0)
397 free (spu_buf);
398 return;
401 n = (*the_target->qxfer_spu)
402 (annex, NULL, (unsigned const char *)spu_buf, ofs, len);
403 if (n < 0)
404 write_enn (own_buf);
405 else
406 sprintf (own_buf, "%x", n);
408 free (spu_buf);
409 return;
412 if (the_target->read_auxv != NULL
413 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
415 unsigned char *data;
416 int n;
417 CORE_ADDR ofs;
418 unsigned int len;
419 char *annex;
421 require_running (own_buf);
423 /* Reject any annex; grab the offset and length. */
424 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
425 || annex[0] != '\0')
427 strcpy (own_buf, "E00");
428 return;
431 /* Read one extra byte, as an indicator of whether there is
432 more. */
433 if (len > PBUFSIZ - 2)
434 len = PBUFSIZ - 2;
435 data = malloc (len + 1);
436 n = (*the_target->read_auxv) (ofs, data, len + 1);
437 if (n < 0)
438 write_enn (own_buf);
439 else if (n > len)
440 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
441 else
442 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
444 free (data);
446 return;
449 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
451 CORE_ADDR ofs;
452 unsigned int len, total_len;
453 const char *document;
454 char *annex;
456 require_running (own_buf);
458 /* Check for support. */
459 document = get_features_xml ("target.xml");
460 if (document == NULL)
462 own_buf[0] = '\0';
463 return;
466 /* Grab the annex, offset, and length. */
467 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
469 strcpy (own_buf, "E00");
470 return;
473 /* Now grab the correct annex. */
474 document = get_features_xml (annex);
475 if (document == NULL)
477 strcpy (own_buf, "E00");
478 return;
481 total_len = strlen (document);
482 if (len > PBUFSIZ - 2)
483 len = PBUFSIZ - 2;
485 if (ofs > total_len)
486 write_enn (own_buf);
487 else if (len < total_len - ofs)
488 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
489 len, 1);
490 else
491 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
492 total_len - ofs, 0);
494 return;
497 if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
499 CORE_ADDR ofs;
500 unsigned int len, total_len;
501 char *document, *p;
502 struct inferior_list_entry *dll_ptr;
503 char *annex;
505 require_running (own_buf);
507 /* Reject any annex; grab the offset and length. */
508 if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
509 || annex[0] != '\0')
511 strcpy (own_buf, "E00");
512 return;
515 /* Over-estimate the necessary memory. Assume that every character
516 in the library name must be escaped. */
517 total_len = 64;
518 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
519 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
521 document = malloc (total_len);
522 strcpy (document, "<library-list>\n");
523 p = document + strlen (document);
525 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
527 struct dll_info *dll = (struct dll_info *) dll_ptr;
528 char *name;
530 strcpy (p, " <library name=\"");
531 p = p + strlen (p);
532 name = xml_escape_text (dll->name);
533 strcpy (p, name);
534 free (name);
535 p = p + strlen (p);
536 strcpy (p, "\"><segment address=\"");
537 p = p + strlen (p);
538 sprintf (p, "0x%lx", (long) dll->base_addr);
539 p = p + strlen (p);
540 strcpy (p, "\"/></library>\n");
541 p = p + strlen (p);
544 strcpy (p, "</library-list>\n");
546 total_len = strlen (document);
547 if (len > PBUFSIZ - 2)
548 len = PBUFSIZ - 2;
550 if (ofs > total_len)
551 write_enn (own_buf);
552 else if (len < total_len - ofs)
553 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
554 len, 1);
555 else
556 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
557 total_len - ofs, 0);
559 free (document);
560 return;
563 /* Protocol features query. */
564 if (strncmp ("qSupported", own_buf, 10) == 0
565 && (own_buf[10] == ':' || own_buf[10] == '\0'))
567 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
569 /* We do not have any hook to indicate whether the target backend
570 supports qXfer:libraries:read, so always report it. */
571 strcat (own_buf, ";qXfer:libraries:read+");
573 if (the_target->read_auxv != NULL)
574 strcat (own_buf, ";qXfer:auxv:read+");
576 if (the_target->qxfer_spu != NULL)
577 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
579 if (get_features_xml ("target.xml") != NULL)
580 strcat (own_buf, ";qXfer:features:read+");
582 return;
585 /* Thread-local storage support. */
586 if (the_target->get_tls_address != NULL
587 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
589 char *p = own_buf + 12;
590 CORE_ADDR parts[3], address = 0;
591 int i, err;
593 require_running (own_buf);
595 for (i = 0; i < 3; i++)
597 char *p2;
598 int len;
600 if (p == NULL)
601 break;
603 p2 = strchr (p, ',');
604 if (p2)
606 len = p2 - p;
607 p2++;
609 else
611 len = strlen (p);
612 p2 = NULL;
615 decode_address (&parts[i], p, len);
616 p = p2;
619 if (p != NULL || i < 3)
620 err = 1;
621 else
623 struct thread_info *thread = gdb_id_to_thread (parts[0]);
625 if (thread == NULL)
626 err = 2;
627 else
628 err = the_target->get_tls_address (thread, parts[1], parts[2],
629 &address);
632 if (err == 0)
634 sprintf (own_buf, "%llx", address);
635 return;
637 else if (err > 0)
639 write_enn (own_buf);
640 return;
643 /* Otherwise, pretend we do not understand this packet. */
646 /* Handle "monitor" commands. */
647 if (strncmp ("qRcmd,", own_buf, 6) == 0)
649 char *mon = malloc (PBUFSIZ);
650 int len = strlen (own_buf + 6);
652 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
654 write_enn (own_buf);
655 free (mon);
656 return;
658 mon[len / 2] = '\0';
660 write_ok (own_buf);
662 if (strcmp (mon, "set debug 1") == 0)
664 debug_threads = 1;
665 monitor_output ("Debug output enabled.\n");
667 else if (strcmp (mon, "set debug 0") == 0)
669 debug_threads = 0;
670 monitor_output ("Debug output disabled.\n");
672 else if (strcmp (mon, "set remote-debug 1") == 0)
674 remote_debug = 1;
675 monitor_output ("Protocol debug output enabled.\n");
677 else if (strcmp (mon, "set remote-debug 0") == 0)
679 remote_debug = 0;
680 monitor_output ("Protocol debug output disabled.\n");
682 else if (strcmp (mon, "help") == 0)
683 monitor_show_help ();
684 else if (strcmp (mon, "exit") == 0)
685 exit_requested = 1;
686 else
688 monitor_output ("Unknown monitor command.\n\n");
689 monitor_show_help ();
690 write_enn (own_buf);
693 free (mon);
694 return;
697 /* Otherwise we didn't know what packet it was. Say we didn't
698 understand it. */
699 own_buf[0] = 0;
702 /* Parse vCont packets. */
703 void
704 handle_v_cont (char *own_buf, char *status, int *signal)
706 char *p, *q;
707 int n = 0, i = 0;
708 struct thread_resume *resume_info, default_action;
710 /* Count the number of semicolons in the packet. There should be one
711 for every action. */
712 p = &own_buf[5];
713 while (p)
715 n++;
716 p++;
717 p = strchr (p, ';');
719 /* Allocate room for one extra action, for the default remain-stopped
720 behavior; if no default action is in the list, we'll need the extra
721 slot. */
722 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
724 default_action.thread = -1;
725 default_action.leave_stopped = 1;
726 default_action.step = 0;
727 default_action.sig = 0;
729 p = &own_buf[5];
730 i = 0;
731 while (*p)
733 p++;
735 resume_info[i].leave_stopped = 0;
737 if (p[0] == 's' || p[0] == 'S')
738 resume_info[i].step = 1;
739 else if (p[0] == 'c' || p[0] == 'C')
740 resume_info[i].step = 0;
741 else
742 goto err;
744 if (p[0] == 'S' || p[0] == 'C')
746 int sig;
747 sig = strtol (p + 1, &q, 16);
748 if (p == q)
749 goto err;
750 p = q;
752 if (!target_signal_to_host_p (sig))
753 goto err;
754 resume_info[i].sig = target_signal_to_host (sig);
756 else
758 resume_info[i].sig = 0;
759 p = p + 1;
762 if (p[0] == 0)
764 resume_info[i].thread = -1;
765 default_action = resume_info[i];
767 /* Note: we don't increment i here, we'll overwrite this entry
768 the next time through. */
770 else if (p[0] == ':')
772 unsigned int gdb_id = strtoul (p + 1, &q, 16);
773 unsigned long thread_id;
775 if (p == q)
776 goto err;
777 p = q;
778 if (p[0] != ';' && p[0] != 0)
779 goto err;
781 thread_id = gdb_id_to_thread_id (gdb_id);
782 if (thread_id)
783 resume_info[i].thread = thread_id;
784 else
785 goto err;
787 i++;
791 resume_info[i] = default_action;
793 /* Still used in occasional places in the backend. */
794 if (n == 1 && resume_info[0].thread != -1)
795 cont_thread = resume_info[0].thread;
796 else
797 cont_thread = -1;
798 set_desired_inferior (0);
800 enable_async_io ();
801 (*the_target->resume) (resume_info);
803 free (resume_info);
805 *signal = mywait (status, 1);
806 prepare_resume_reply (own_buf, *status, *signal);
807 disable_async_io ();
808 return;
810 err:
811 write_enn (own_buf);
812 free (resume_info);
813 return;
816 /* Attach to a new program. Return 1 if successful, 0 if failure. */
818 handle_v_attach (char *own_buf, char *status, int *signal)
820 int pid;
822 pid = strtol (own_buf + 8, NULL, 16);
823 if (pid != 0 && attach_inferior (pid, status, signal) == 0)
825 prepare_resume_reply (own_buf, *status, *signal);
826 return 1;
828 else
830 write_enn (own_buf);
831 return 0;
835 /* Run a new program. Return 1 if successful, 0 if failure. */
836 static int
837 handle_v_run (char *own_buf, char *status, int *signal)
839 char *p, **pp, *next_p, **new_argv;
840 int i, new_argc;
842 new_argc = 0;
843 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
845 p++;
846 new_argc++;
849 new_argv = malloc ((new_argc + 2) * sizeof (char *));
850 i = 0;
851 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
853 next_p = strchr (p, ';');
854 if (next_p == NULL)
855 next_p = p + strlen (p);
857 if (i == 0 && p == next_p)
858 new_argv[i] = NULL;
859 else
861 new_argv[i] = malloc (1 + (next_p - p) / 2);
862 unhexify (new_argv[i], p, (next_p - p) / 2);
863 new_argv[i][(next_p - p) / 2] = '\0';
866 if (*next_p)
867 next_p++;
868 i++;
870 new_argv[i] = NULL;
872 if (new_argv[0] == NULL)
874 if (program_argv == NULL)
876 write_enn (own_buf);
877 return 0;
880 new_argv[0] = strdup (program_argv[0]);
883 /* Free the old argv. */
884 if (program_argv)
886 for (pp = program_argv; *pp != NULL; pp++)
887 free (*pp);
888 free (program_argv);
890 program_argv = new_argv;
892 *signal = start_inferior (program_argv, status);
893 if (*status == 'T')
895 prepare_resume_reply (own_buf, *status, *signal);
896 return 1;
898 else
900 write_enn (own_buf);
901 return 0;
905 /* Handle all of the extended 'v' packets. */
906 void
907 handle_v_requests (char *own_buf, char *status, int *signal,
908 int packet_len, int *new_packet_len)
910 if (strncmp (own_buf, "vCont;", 6) == 0)
912 require_running (own_buf);
913 handle_v_cont (own_buf, status, signal);
914 return;
917 if (strncmp (own_buf, "vCont?", 6) == 0)
919 strcpy (own_buf, "vCont;c;C;s;S");
920 return;
923 if (strncmp (own_buf, "vFile:", 6) == 0
924 && handle_vFile (own_buf, packet_len, new_packet_len))
925 return;
927 if (strncmp (own_buf, "vAttach;", 8) == 0)
929 if (target_running ())
931 fprintf (stderr, "Killing inferior\n");
932 kill_inferior ();
934 handle_v_attach (own_buf, status, signal);
935 return;
938 if (strncmp (own_buf, "vRun;", 5) == 0)
940 if (target_running ())
942 fprintf (stderr, "Killing inferior\n");
943 kill_inferior ();
945 handle_v_run (own_buf, status, signal);
946 return;
949 /* Otherwise we didn't know what packet it was. Say we didn't
950 understand it. */
951 own_buf[0] = 0;
952 return;
955 void
956 myresume (char *own_buf, int step, int *signalp, char *statusp)
958 struct thread_resume resume_info[2];
959 int n = 0;
960 int sig = *signalp;
962 set_desired_inferior (0);
964 if (step || sig || (cont_thread != 0 && cont_thread != -1))
966 resume_info[0].thread
967 = ((struct inferior_list_entry *) current_inferior)->id;
968 resume_info[0].step = step;
969 resume_info[0].sig = sig;
970 resume_info[0].leave_stopped = 0;
971 n++;
973 resume_info[n].thread = -1;
974 resume_info[n].step = 0;
975 resume_info[n].sig = 0;
976 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
978 enable_async_io ();
979 (*the_target->resume) (resume_info);
980 *signalp = mywait (statusp, 1);
981 prepare_resume_reply (own_buf, *statusp, *signalp);
982 disable_async_io ();
985 static void
986 gdbserver_version (void)
988 printf ("GNU gdbserver %s\n"
989 "Copyright (C) 2007 Free Software Foundation, Inc.\n"
990 "gdbserver is free software, covered by the GNU General Public License.\n"
991 "This gdbserver was configured as \"%s\"\n",
992 version, host_name);
995 static void
996 gdbserver_usage (void)
998 printf ("Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
999 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1000 "\tgdbserver [OPTIONS] --multi COMM\n"
1001 "\n"
1002 "COMM may either be a tty device (for serial debugging), or \n"
1003 "HOST:PORT to listen for a TCP connection.\n"
1004 "\n"
1005 "Options:\n"
1006 " --debug\t\tEnable debugging output.\n");
1009 #undef require_running
1010 #define require_running(BUF) \
1011 if (!target_running ()) \
1013 write_enn (BUF); \
1014 break; \
1018 main (int argc, char *argv[])
1020 char ch, status, *own_buf;
1021 unsigned char *mem_buf;
1022 int i = 0;
1023 int signal;
1024 unsigned int len;
1025 CORE_ADDR mem_addr;
1026 int bad_attach;
1027 int pid;
1028 char *arg_end, *port;
1029 char **next_arg = &argv[1];
1030 int multi_mode = 0;
1031 int attach = 0;
1032 int was_running;
1034 while (*next_arg != NULL && **next_arg == '-')
1036 if (strcmp (*next_arg, "--version") == 0)
1038 gdbserver_version ();
1039 exit (0);
1041 else if (strcmp (*next_arg, "--help") == 0)
1043 gdbserver_usage ();
1044 exit (0);
1046 else if (strcmp (*next_arg, "--attach") == 0)
1047 attach = 1;
1048 else if (strcmp (*next_arg, "--multi") == 0)
1049 multi_mode = 1;
1050 else if (strcmp (*next_arg, "--debug") == 0)
1051 debug_threads = 1;
1052 else
1054 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
1055 exit (1);
1058 next_arg++;
1059 continue;
1062 if (setjmp (toplevel))
1064 fprintf (stderr, "Exiting\n");
1065 exit (1);
1068 port = *next_arg;
1069 next_arg++;
1070 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
1072 gdbserver_usage ();
1073 exit (1);
1076 bad_attach = 0;
1077 pid = 0;
1079 /* --attach used to come after PORT, so allow it there for
1080 compatibility. */
1081 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
1083 attach = 1;
1084 next_arg++;
1087 if (attach
1088 && (*next_arg == NULL
1089 || (*next_arg)[0] == '\0'
1090 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
1091 || *arg_end != '\0'
1092 || next_arg[1] != NULL))
1093 bad_attach = 1;
1095 if (bad_attach)
1097 gdbserver_usage ();
1098 exit (1);
1101 initialize_async_io ();
1102 initialize_low ();
1104 own_buf = malloc (PBUFSIZ + 1);
1105 mem_buf = malloc (PBUFSIZ);
1107 if (pid == 0 && *next_arg != NULL)
1109 int i, n;
1111 n = argc - (next_arg - argv);
1112 program_argv = malloc (sizeof (char *) * (n + 1));
1113 for (i = 0; i < n; i++)
1114 program_argv[i] = strdup (next_arg[i]);
1115 program_argv[i] = NULL;
1117 /* Wait till we are at first instruction in program. */
1118 signal = start_inferior (program_argv, &status);
1120 /* We are now (hopefully) stopped at the first instruction of
1121 the target process. This assumes that the target process was
1122 successfully created. */
1124 else if (pid != 0)
1126 if (attach_inferior (pid, &status, &signal) == -1)
1127 error ("Attaching not supported on this target");
1129 /* Otherwise succeeded. */
1131 else
1133 status = 'W';
1134 signal = 0;
1137 /* Don't report shared library events on the initial connection,
1138 even if some libraries are preloaded. Avoids the "stopped by
1139 shared library event" notice on gdb side. */
1140 dlls_changed = 0;
1142 if (setjmp (toplevel))
1144 fprintf (stderr, "Killing inferior\n");
1145 kill_inferior ();
1146 exit (1);
1149 if (status == 'W' || status == 'X')
1150 was_running = 0;
1151 else
1152 was_running = 1;
1154 if (!was_running && !multi_mode)
1156 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
1157 exit (1);
1160 while (1)
1162 remote_open (port);
1164 restart:
1165 if (setjmp (toplevel) != 0)
1167 /* An error occurred. */
1168 if (response_needed)
1170 write_enn (own_buf);
1171 putpkt (own_buf);
1175 disable_async_io ();
1176 while (!exit_requested)
1178 unsigned char sig;
1179 int packet_len;
1180 int new_packet_len = -1;
1182 response_needed = 0;
1183 packet_len = getpkt (own_buf);
1184 if (packet_len <= 0)
1185 break;
1186 response_needed = 1;
1188 i = 0;
1189 ch = own_buf[i++];
1190 switch (ch)
1192 case 'q':
1193 handle_query (own_buf, packet_len, &new_packet_len);
1194 break;
1195 case 'Q':
1196 handle_general_set (own_buf);
1197 break;
1198 case 'D':
1199 require_running (own_buf);
1200 fprintf (stderr, "Detaching from inferior\n");
1201 if (detach_inferior () != 0)
1202 write_enn (own_buf);
1203 else
1205 write_ok (own_buf);
1207 if (extended_protocol)
1209 /* Treat this like a normal program exit. */
1210 signal = 0;
1211 status = 'W';
1213 else
1215 putpkt (own_buf);
1216 remote_close ();
1218 /* If we are attached, then we can exit. Otherwise, we
1219 need to hang around doing nothing, until the child
1220 is gone. */
1221 if (!attached)
1222 join_inferior ();
1224 exit (0);
1227 break;
1228 case '!':
1229 extended_protocol = 1;
1230 write_ok (own_buf);
1231 break;
1232 case '?':
1233 prepare_resume_reply (own_buf, status, signal);
1234 break;
1235 case 'H':
1236 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
1238 unsigned long gdb_id, thread_id;
1240 require_running (own_buf);
1241 gdb_id = strtoul (&own_buf[2], NULL, 16);
1242 if (gdb_id == 0 || gdb_id == -1)
1243 thread_id = gdb_id;
1244 else
1246 thread_id = gdb_id_to_thread_id (gdb_id);
1247 if (thread_id == 0)
1249 write_enn (own_buf);
1250 break;
1254 if (own_buf[1] == 'g')
1256 general_thread = thread_id;
1257 set_desired_inferior (1);
1259 else if (own_buf[1] == 'c')
1260 cont_thread = thread_id;
1261 else if (own_buf[1] == 's')
1262 step_thread = thread_id;
1264 write_ok (own_buf);
1266 else
1268 /* Silently ignore it so that gdb can extend the protocol
1269 without compatibility headaches. */
1270 own_buf[0] = '\0';
1272 break;
1273 case 'g':
1274 require_running (own_buf);
1275 set_desired_inferior (1);
1276 registers_to_string (own_buf);
1277 break;
1278 case 'G':
1279 require_running (own_buf);
1280 set_desired_inferior (1);
1281 registers_from_string (&own_buf[1]);
1282 write_ok (own_buf);
1283 break;
1284 case 'm':
1285 require_running (own_buf);
1286 decode_m_packet (&own_buf[1], &mem_addr, &len);
1287 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
1288 convert_int_to_ascii (mem_buf, own_buf, len);
1289 else
1290 write_enn (own_buf);
1291 break;
1292 case 'M':
1293 require_running (own_buf);
1294 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
1295 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
1296 write_ok (own_buf);
1297 else
1298 write_enn (own_buf);
1299 break;
1300 case 'X':
1301 require_running (own_buf);
1302 if (decode_X_packet (&own_buf[1], packet_len - 1,
1303 &mem_addr, &len, mem_buf) < 0
1304 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
1305 write_enn (own_buf);
1306 else
1307 write_ok (own_buf);
1308 break;
1309 case 'C':
1310 require_running (own_buf);
1311 convert_ascii_to_int (own_buf + 1, &sig, 1);
1312 if (target_signal_to_host_p (sig))
1313 signal = target_signal_to_host (sig);
1314 else
1315 signal = 0;
1316 myresume (own_buf, 0, &signal, &status);
1317 break;
1318 case 'S':
1319 require_running (own_buf);
1320 convert_ascii_to_int (own_buf + 1, &sig, 1);
1321 if (target_signal_to_host_p (sig))
1322 signal = target_signal_to_host (sig);
1323 else
1324 signal = 0;
1325 myresume (own_buf, 1, &signal, &status);
1326 break;
1327 case 'c':
1328 require_running (own_buf);
1329 signal = 0;
1330 myresume (own_buf, 0, &signal, &status);
1331 break;
1332 case 's':
1333 require_running (own_buf);
1334 signal = 0;
1335 myresume (own_buf, 1, &signal, &status);
1336 break;
1337 case 'Z':
1339 char *lenptr;
1340 char *dataptr;
1341 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1342 int len = strtol (lenptr + 1, &dataptr, 16);
1343 char type = own_buf[1];
1345 if (the_target->insert_watchpoint == NULL
1346 || (type < '2' || type > '4'))
1348 /* No watchpoint support or not a watchpoint command;
1349 unrecognized either way. */
1350 own_buf[0] = '\0';
1352 else
1354 int res;
1356 require_running (own_buf);
1357 res = (*the_target->insert_watchpoint) (type, addr, len);
1358 if (res == 0)
1359 write_ok (own_buf);
1360 else if (res == 1)
1361 /* Unsupported. */
1362 own_buf[0] = '\0';
1363 else
1364 write_enn (own_buf);
1366 break;
1368 case 'z':
1370 char *lenptr;
1371 char *dataptr;
1372 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1373 int len = strtol (lenptr + 1, &dataptr, 16);
1374 char type = own_buf[1];
1376 if (the_target->remove_watchpoint == NULL
1377 || (type < '2' || type > '4'))
1379 /* No watchpoint support or not a watchpoint command;
1380 unrecognized either way. */
1381 own_buf[0] = '\0';
1383 else
1385 int res;
1387 require_running (own_buf);
1388 res = (*the_target->remove_watchpoint) (type, addr, len);
1389 if (res == 0)
1390 write_ok (own_buf);
1391 else if (res == 1)
1392 /* Unsupported. */
1393 own_buf[0] = '\0';
1394 else
1395 write_enn (own_buf);
1397 break;
1399 case 'k':
1400 response_needed = 0;
1401 if (!target_running ())
1402 /* The packet we received doesn't make sense - but we
1403 can't reply to it, either. */
1404 goto restart;
1406 fprintf (stderr, "Killing inferior\n");
1407 kill_inferior ();
1409 /* When using the extended protocol, we wait with no
1410 program running. The traditional protocol will exit
1411 instead. */
1412 if (extended_protocol)
1414 status = 'X';
1415 signal = TARGET_SIGNAL_KILL;
1416 was_running = 0;
1417 goto restart;
1419 else
1421 exit (0);
1422 break;
1424 case 'T':
1426 unsigned long gdb_id, thread_id;
1428 require_running (own_buf);
1429 gdb_id = strtoul (&own_buf[1], NULL, 16);
1430 thread_id = gdb_id_to_thread_id (gdb_id);
1431 if (thread_id == 0)
1433 write_enn (own_buf);
1434 break;
1437 if (mythread_alive (thread_id))
1438 write_ok (own_buf);
1439 else
1440 write_enn (own_buf);
1442 break;
1443 case 'R':
1444 response_needed = 0;
1446 /* Restarting the inferior is only supported in the
1447 extended protocol. */
1448 if (extended_protocol)
1450 if (target_running ())
1451 kill_inferior ();
1452 fprintf (stderr, "GDBserver restarting\n");
1454 /* Wait till we are at 1st instruction in prog. */
1455 if (program_argv != NULL)
1456 signal = start_inferior (program_argv, &status);
1457 else
1459 status = 'X';
1460 signal = TARGET_SIGNAL_KILL;
1462 goto restart;
1464 else
1466 /* It is a request we don't understand. Respond with an
1467 empty packet so that gdb knows that we don't support this
1468 request. */
1469 own_buf[0] = '\0';
1470 break;
1472 case 'v':
1473 /* Extended (long) request. */
1474 handle_v_requests (own_buf, &status, &signal,
1475 packet_len, &new_packet_len);
1476 break;
1478 default:
1479 /* It is a request we don't understand. Respond with an
1480 empty packet so that gdb knows that we don't support this
1481 request. */
1482 own_buf[0] = '\0';
1483 break;
1486 if (new_packet_len != -1)
1487 putpkt_binary (own_buf, new_packet_len);
1488 else
1489 putpkt (own_buf);
1491 response_needed = 0;
1493 if (was_running && (status == 'W' || status == 'X'))
1495 was_running = 0;
1497 if (status == 'W')
1498 fprintf (stderr,
1499 "\nChild exited with status %d\n", signal);
1500 if (status == 'X')
1501 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
1502 target_signal_to_host (signal),
1503 target_signal_to_name (signal));
1505 if (extended_protocol)
1506 goto restart;
1507 else
1509 fprintf (stderr, "GDBserver exiting\n");
1510 exit (0);
1514 if (status != 'W' && status != 'X')
1515 was_running = 1;
1518 /* If an exit was requested (using the "monitor exit" command),
1519 terminate now. The only other way to get here is for
1520 getpkt to fail; close the connection and reopen it at the
1521 top of the loop. */
1523 if (exit_requested)
1525 remote_close ();
1526 if (attached && target_running ())
1527 detach_inferior ();
1528 else if (target_running ())
1529 kill_inferior ();
1530 exit (0);
1532 else
1534 fprintf (stderr, "Remote side has terminated connection. "
1535 "GDBserver will reopen the connection.\n");
1536 remote_close ();