Update
[gdb.git] / gdb / gdbserver / server.c
blob51dfbcce08c8671b748af5cee11d5ee4d27abc37
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");
273 #define require_running(BUF) \
274 if (!target_running ()) \
276 write_enn (BUF); \
277 return; \
280 /* Handle all of the extended 'q' packets. */
281 void
282 handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
284 static struct inferior_list_entry *thread_ptr;
286 /* Reply the current thread id. */
287 if (strcmp ("qC", own_buf) == 0)
289 require_running (own_buf);
290 thread_ptr = all_threads.head;
291 sprintf (own_buf, "QC%x",
292 thread_to_gdb_id ((struct thread_info *)thread_ptr));
293 return;
296 if (strcmp ("qSymbol::", own_buf) == 0)
298 if (target_running () && the_target->look_up_symbols != NULL)
299 (*the_target->look_up_symbols) ();
301 strcpy (own_buf, "OK");
302 return;
305 if (strcmp ("qfThreadInfo", own_buf) == 0)
307 require_running (own_buf);
308 thread_ptr = all_threads.head;
309 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
310 thread_ptr = thread_ptr->next;
311 return;
314 if (strcmp ("qsThreadInfo", own_buf) == 0)
316 require_running (own_buf);
317 if (thread_ptr != NULL)
319 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
320 thread_ptr = thread_ptr->next;
321 return;
323 else
325 sprintf (own_buf, "l");
326 return;
330 if (the_target->read_offsets != NULL
331 && strcmp ("qOffsets", own_buf) == 0)
333 CORE_ADDR text, data;
335 require_running (own_buf);
336 if (the_target->read_offsets (&text, &data))
337 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
338 (long)text, (long)data, (long)data);
339 else
340 write_enn (own_buf);
342 return;
345 if (the_target->qxfer_spu != NULL
346 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
348 char *annex;
349 int n;
350 unsigned int len;
351 CORE_ADDR ofs;
352 unsigned char *spu_buf;
354 require_running (own_buf);
355 strcpy (own_buf, "E00");
356 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
357 return;
358 if (len > PBUFSIZ - 2)
359 len = PBUFSIZ - 2;
360 spu_buf = malloc (len + 1);
361 if (!spu_buf)
362 return;
364 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
365 if (n < 0)
366 write_enn (own_buf);
367 else if (n > len)
368 *new_packet_len_p = write_qxfer_response
369 (own_buf, spu_buf, len, 1);
370 else
371 *new_packet_len_p = write_qxfer_response
372 (own_buf, spu_buf, n, 0);
374 free (spu_buf);
375 return;
378 if (the_target->qxfer_spu != NULL
379 && strncmp ("qXfer:spu:write:", own_buf, 16) == 0)
381 char *annex;
382 int n;
383 unsigned int len;
384 CORE_ADDR ofs;
385 unsigned char *spu_buf;
387 require_running (own_buf);
388 strcpy (own_buf, "E00");
389 spu_buf = malloc (packet_len - 15);
390 if (!spu_buf)
391 return;
392 if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
393 &ofs, &len, spu_buf) < 0)
395 free (spu_buf);
396 return;
399 n = (*the_target->qxfer_spu)
400 (annex, NULL, (unsigned const char *)spu_buf, ofs, len);
401 if (n < 0)
402 write_enn (own_buf);
403 else
404 sprintf (own_buf, "%x", n);
406 free (spu_buf);
407 return;
410 if (the_target->read_auxv != NULL
411 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
413 unsigned char *data;
414 int n;
415 CORE_ADDR ofs;
416 unsigned int len;
417 char *annex;
419 require_running (own_buf);
421 /* Reject any annex; grab the offset and length. */
422 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
423 || annex[0] != '\0')
425 strcpy (own_buf, "E00");
426 return;
429 /* Read one extra byte, as an indicator of whether there is
430 more. */
431 if (len > PBUFSIZ - 2)
432 len = PBUFSIZ - 2;
433 data = malloc (len + 1);
434 n = (*the_target->read_auxv) (ofs, data, len + 1);
435 if (n < 0)
436 write_enn (own_buf);
437 else if (n > len)
438 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
439 else
440 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
442 free (data);
444 return;
447 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
449 CORE_ADDR ofs;
450 unsigned int len, total_len;
451 const char *document;
452 char *annex;
454 require_running (own_buf);
456 /* Check for support. */
457 document = get_features_xml ("target.xml");
458 if (document == NULL)
460 own_buf[0] = '\0';
461 return;
464 /* Grab the annex, offset, and length. */
465 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
467 strcpy (own_buf, "E00");
468 return;
471 /* Now grab the correct annex. */
472 document = get_features_xml (annex);
473 if (document == NULL)
475 strcpy (own_buf, "E00");
476 return;
479 total_len = strlen (document);
480 if (len > PBUFSIZ - 2)
481 len = PBUFSIZ - 2;
483 if (ofs > total_len)
484 write_enn (own_buf);
485 else if (len < total_len - ofs)
486 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
487 len, 1);
488 else
489 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
490 total_len - ofs, 0);
492 return;
495 if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
497 CORE_ADDR ofs;
498 unsigned int len, total_len;
499 char *document, *p;
500 struct inferior_list_entry *dll_ptr;
501 char *annex;
503 require_running (own_buf);
505 /* Reject any annex; grab the offset and length. */
506 if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
507 || annex[0] != '\0')
509 strcpy (own_buf, "E00");
510 return;
513 /* Over-estimate the necessary memory. Assume that every character
514 in the library name must be escaped. */
515 total_len = 64;
516 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
517 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
519 document = malloc (total_len);
520 strcpy (document, "<library-list>\n");
521 p = document + strlen (document);
523 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
525 struct dll_info *dll = (struct dll_info *) dll_ptr;
526 char *name;
528 strcpy (p, " <library name=\"");
529 p = p + strlen (p);
530 name = xml_escape_text (dll->name);
531 strcpy (p, name);
532 free (name);
533 p = p + strlen (p);
534 strcpy (p, "\"><segment address=\"");
535 p = p + strlen (p);
536 sprintf (p, "0x%lx", (long) dll->base_addr);
537 p = p + strlen (p);
538 strcpy (p, "\"/></library>\n");
539 p = p + strlen (p);
542 strcpy (p, "</library-list>\n");
544 total_len = strlen (document);
545 if (len > PBUFSIZ - 2)
546 len = PBUFSIZ - 2;
548 if (ofs > total_len)
549 write_enn (own_buf);
550 else if (len < total_len - ofs)
551 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
552 len, 1);
553 else
554 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
555 total_len - ofs, 0);
557 free (document);
558 return;
561 /* Protocol features query. */
562 if (strncmp ("qSupported", own_buf, 10) == 0
563 && (own_buf[10] == ':' || own_buf[10] == '\0'))
565 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
567 /* We do not have any hook to indicate whether the target backend
568 supports qXfer:libraries:read, so always report it. */
569 strcat (own_buf, ";qXfer:libraries:read+");
571 if (the_target->read_auxv != NULL)
572 strcat (own_buf, ";qXfer:auxv:read+");
574 if (the_target->qxfer_spu != NULL)
575 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
577 if (get_features_xml ("target.xml") != NULL)
578 strcat (own_buf, ";qXfer:features:read+");
580 return;
583 /* Thread-local storage support. */
584 if (the_target->get_tls_address != NULL
585 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
587 char *p = own_buf + 12;
588 CORE_ADDR parts[3], address = 0;
589 int i, err;
591 require_running (own_buf);
593 for (i = 0; i < 3; i++)
595 char *p2;
596 int len;
598 if (p == NULL)
599 break;
601 p2 = strchr (p, ',');
602 if (p2)
604 len = p2 - p;
605 p2++;
607 else
609 len = strlen (p);
610 p2 = NULL;
613 decode_address (&parts[i], p, len);
614 p = p2;
617 if (p != NULL || i < 3)
618 err = 1;
619 else
621 struct thread_info *thread = gdb_id_to_thread (parts[0]);
623 if (thread == NULL)
624 err = 2;
625 else
626 err = the_target->get_tls_address (thread, parts[1], parts[2],
627 &address);
630 if (err == 0)
632 sprintf (own_buf, "%llx", address);
633 return;
635 else if (err > 0)
637 write_enn (own_buf);
638 return;
641 /* Otherwise, pretend we do not understand this packet. */
644 /* Handle "monitor" commands. */
645 if (strncmp ("qRcmd,", own_buf, 6) == 0)
647 char *mon = malloc (PBUFSIZ);
648 int len = strlen (own_buf + 6);
650 if ((len % 1) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
652 write_enn (own_buf);
653 free (mon);
654 return;
656 mon[len / 2] = '\0';
658 write_ok (own_buf);
660 if (strcmp (mon, "set debug 1") == 0)
662 debug_threads = 1;
663 monitor_output ("Debug output enabled.\n");
665 else if (strcmp (mon, "set debug 0") == 0)
667 debug_threads = 0;
668 monitor_output ("Debug output disabled.\n");
670 else if (strcmp (mon, "set remote-debug 1") == 0)
672 remote_debug = 1;
673 monitor_output ("Protocol debug output enabled.\n");
675 else if (strcmp (mon, "set remote-debug 0") == 0)
677 remote_debug = 0;
678 monitor_output ("Protocol debug output disabled.\n");
680 else if (strcmp (mon, "help") == 0)
681 monitor_show_help ();
682 else if (strcmp (mon, "exit") == 0)
683 exit_requested = 1;
684 else
686 monitor_output ("Unknown monitor command.\n\n");
687 monitor_show_help ();
688 write_enn (own_buf);
691 free (mon);
692 return;
695 /* Otherwise we didn't know what packet it was. Say we didn't
696 understand it. */
697 own_buf[0] = 0;
700 /* Parse vCont packets. */
701 void
702 handle_v_cont (char *own_buf, char *status, int *signal)
704 char *p, *q;
705 int n = 0, i = 0;
706 struct thread_resume *resume_info, default_action;
708 /* Count the number of semicolons in the packet. There should be one
709 for every action. */
710 p = &own_buf[5];
711 while (p)
713 n++;
714 p++;
715 p = strchr (p, ';');
717 /* Allocate room for one extra action, for the default remain-stopped
718 behavior; if no default action is in the list, we'll need the extra
719 slot. */
720 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
722 default_action.thread = -1;
723 default_action.leave_stopped = 1;
724 default_action.step = 0;
725 default_action.sig = 0;
727 p = &own_buf[5];
728 i = 0;
729 while (*p)
731 p++;
733 resume_info[i].leave_stopped = 0;
735 if (p[0] == 's' || p[0] == 'S')
736 resume_info[i].step = 1;
737 else if (p[0] == 'c' || p[0] == 'C')
738 resume_info[i].step = 0;
739 else
740 goto err;
742 if (p[0] == 'S' || p[0] == 'C')
744 int sig;
745 sig = strtol (p + 1, &q, 16);
746 if (p == q)
747 goto err;
748 p = q;
750 if (!target_signal_to_host_p (sig))
751 goto err;
752 resume_info[i].sig = target_signal_to_host (sig);
754 else
756 resume_info[i].sig = 0;
757 p = p + 1;
760 if (p[0] == 0)
762 resume_info[i].thread = -1;
763 default_action = resume_info[i];
765 /* Note: we don't increment i here, we'll overwrite this entry
766 the next time through. */
768 else if (p[0] == ':')
770 unsigned int gdb_id = strtoul (p + 1, &q, 16);
771 unsigned long thread_id;
773 if (p == q)
774 goto err;
775 p = q;
776 if (p[0] != ';' && p[0] != 0)
777 goto err;
779 thread_id = gdb_id_to_thread_id (gdb_id);
780 if (thread_id)
781 resume_info[i].thread = thread_id;
782 else
783 goto err;
785 i++;
789 resume_info[i] = default_action;
791 /* Still used in occasional places in the backend. */
792 if (n == 1 && resume_info[0].thread != -1)
793 cont_thread = resume_info[0].thread;
794 else
795 cont_thread = -1;
796 set_desired_inferior (0);
798 enable_async_io ();
799 (*the_target->resume) (resume_info);
801 free (resume_info);
803 *signal = mywait (status, 1);
804 prepare_resume_reply (own_buf, *status, *signal);
805 disable_async_io ();
806 return;
808 err:
809 write_enn (own_buf);
810 free (resume_info);
811 return;
814 /* Attach to a new program. Return 1 if successful, 0 if failure. */
816 handle_v_attach (char *own_buf, char *status, int *signal)
818 int pid;
820 pid = strtol (own_buf + 8, NULL, 16);
821 if (pid != 0 && attach_inferior (pid, status, signal) == 0)
823 prepare_resume_reply (own_buf, *status, *signal);
824 return 1;
826 else
828 write_enn (own_buf);
829 return 0;
833 /* Run a new program. Return 1 if successful, 0 if failure. */
834 static int
835 handle_v_run (char *own_buf, char *status, int *signal)
837 char *p, **pp, *next_p, **new_argv;
838 int i, new_argc;
840 new_argc = 0;
841 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
843 p++;
844 new_argc++;
847 new_argv = malloc ((new_argc + 2) * sizeof (char *));
848 i = 0;
849 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
851 next_p = strchr (p, ';');
852 if (next_p == NULL)
853 next_p = p + strlen (p);
855 if (i == 0 && p == next_p)
856 new_argv[i] = NULL;
857 else
859 new_argv[i] = malloc (1 + (next_p - p) / 2);
860 unhexify (new_argv[i], p, (next_p - p) / 2);
861 new_argv[i][(next_p - p) / 2] = '\0';
864 if (*next_p)
865 next_p++;
866 i++;
868 new_argv[i] = NULL;
870 if (new_argv[0] == NULL)
872 if (program_argv == NULL)
874 write_enn (own_buf);
875 return 0;
878 new_argv[0] = strdup (program_argv[0]);
881 /* Free the old argv. */
882 if (program_argv)
884 for (pp = program_argv; *pp != NULL; pp++)
885 free (*pp);
886 free (program_argv);
888 program_argv = new_argv;
890 *signal = start_inferior (program_argv, status);
891 if (*status == 'T')
893 prepare_resume_reply (own_buf, *status, *signal);
894 return 1;
896 else
898 write_enn (own_buf);
899 return 0;
903 /* Handle all of the extended 'v' packets. */
904 void
905 handle_v_requests (char *own_buf, char *status, int *signal,
906 int packet_len, int *new_packet_len)
908 if (strncmp (own_buf, "vCont;", 6) == 0)
910 require_running (own_buf);
911 handle_v_cont (own_buf, status, signal);
912 return;
915 if (strncmp (own_buf, "vCont?", 6) == 0)
917 strcpy (own_buf, "vCont;c;C;s;S");
918 return;
921 if (strncmp (own_buf, "vFile:", 6) == 0
922 && handle_vFile (own_buf, packet_len, new_packet_len))
923 return;
925 if (strncmp (own_buf, "vAttach;", 8) == 0)
927 if (target_running ())
929 fprintf (stderr, "Killing inferior\n");
930 kill_inferior ();
932 handle_v_attach (own_buf, status, signal);
933 return;
936 if (strncmp (own_buf, "vRun;", 5) == 0)
938 if (target_running ())
940 fprintf (stderr, "Killing inferior\n");
941 kill_inferior ();
943 handle_v_run (own_buf, status, signal);
944 return;
947 /* Otherwise we didn't know what packet it was. Say we didn't
948 understand it. */
949 own_buf[0] = 0;
950 return;
953 void
954 myresume (char *own_buf, int step, int *signalp, char *statusp)
956 struct thread_resume resume_info[2];
957 int n = 0;
958 int sig = *signalp;
960 set_desired_inferior (0);
962 if (step || sig || (cont_thread != 0 && cont_thread != -1))
964 resume_info[0].thread
965 = ((struct inferior_list_entry *) current_inferior)->id;
966 resume_info[0].step = step;
967 resume_info[0].sig = sig;
968 resume_info[0].leave_stopped = 0;
969 n++;
971 resume_info[n].thread = -1;
972 resume_info[n].step = 0;
973 resume_info[n].sig = 0;
974 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
976 enable_async_io ();
977 (*the_target->resume) (resume_info);
978 *signalp = mywait (statusp, 1);
979 prepare_resume_reply (own_buf, *statusp, *signalp);
980 disable_async_io ();
983 static void
984 gdbserver_version (void)
986 printf ("GNU gdbserver %s\n"
987 "Copyright (C) 2007 Free Software Foundation, Inc.\n"
988 "gdbserver is free software, covered by the GNU General Public License.\n"
989 "This gdbserver was configured as \"%s\"\n",
990 version, host_name);
993 static void
994 gdbserver_usage (void)
996 printf ("Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
997 "\tgdbserver [OPTIONS] --attach COMM PID\n"
998 "\tgdbserver [OPTIONS] --multi COMM\n"
999 "\n"
1000 "COMM may either be a tty device (for serial debugging), or \n"
1001 "HOST:PORT to listen for a TCP connection.\n"
1002 "\n"
1003 "Options:\n"
1004 " --debug\t\tEnable debugging output.\n");
1007 #undef require_running
1008 #define require_running(BUF) \
1009 if (!target_running ()) \
1011 write_enn (BUF); \
1012 break; \
1016 main (int argc, char *argv[])
1018 char ch, status, *own_buf;
1019 unsigned char *mem_buf;
1020 int i = 0;
1021 int signal;
1022 unsigned int len;
1023 CORE_ADDR mem_addr;
1024 int bad_attach;
1025 int pid;
1026 char *arg_end, *port;
1027 char **next_arg = &argv[1];
1028 int multi_mode = 0;
1029 int attach = 0;
1030 int was_running;
1032 while (*next_arg != NULL && **next_arg == '-')
1034 if (strcmp (*next_arg, "--version") == 0)
1036 gdbserver_version ();
1037 exit (0);
1039 else if (strcmp (*next_arg, "--help") == 0)
1041 gdbserver_usage ();
1042 exit (0);
1044 else if (strcmp (*next_arg, "--attach") == 0)
1045 attach = 1;
1046 else if (strcmp (*next_arg, "--multi") == 0)
1047 multi_mode = 1;
1048 else if (strcmp (*next_arg, "--debug") == 0)
1049 debug_threads = 1;
1050 else
1052 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
1053 exit (1);
1056 next_arg++;
1057 continue;
1060 if (setjmp (toplevel))
1062 fprintf (stderr, "Exiting\n");
1063 exit (1);
1066 port = *next_arg;
1067 next_arg++;
1068 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
1070 gdbserver_usage ();
1071 exit (1);
1074 bad_attach = 0;
1075 pid = 0;
1077 /* --attach used to come after PORT, so allow it there for
1078 compatibility. */
1079 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
1081 attach = 1;
1082 next_arg++;
1085 if (attach
1086 && (*next_arg == NULL
1087 || (*next_arg)[0] == '\0'
1088 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
1089 || *arg_end != '\0'
1090 || next_arg[1] != NULL))
1091 bad_attach = 1;
1093 if (bad_attach)
1095 gdbserver_usage ();
1096 exit (1);
1099 initialize_async_io ();
1100 initialize_low ();
1102 own_buf = malloc (PBUFSIZ + 1);
1103 mem_buf = malloc (PBUFSIZ);
1105 if (pid == 0 && *next_arg != NULL)
1107 int i, n;
1109 n = argc - (next_arg - argv);
1110 program_argv = malloc (sizeof (char *) * (n + 1));
1111 for (i = 0; i < n; i++)
1112 program_argv[i] = strdup (next_arg[i]);
1113 program_argv[i] = NULL;
1115 /* Wait till we are at first instruction in program. */
1116 signal = start_inferior (program_argv, &status);
1118 /* We are now (hopefully) stopped at the first instruction of
1119 the target process. This assumes that the target process was
1120 successfully created. */
1122 else if (pid != 0)
1124 if (attach_inferior (pid, &status, &signal) == -1)
1125 error ("Attaching not supported on this target");
1127 /* Otherwise succeeded. */
1129 else
1131 status = 'W';
1132 signal = 0;
1135 /* Don't report shared library events on the initial connection,
1136 even if some libraries are preloaded. Avoids the "stopped by
1137 shared library event" notice on gdb side. */
1138 dlls_changed = 0;
1140 if (setjmp (toplevel))
1142 fprintf (stderr, "Killing inferior\n");
1143 kill_inferior ();
1144 exit (1);
1147 if (status == 'W' || status == 'X')
1148 was_running = 0;
1149 else
1150 was_running = 1;
1152 if (!was_running && !multi_mode)
1154 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
1155 exit (1);
1158 while (1)
1160 remote_open (port);
1162 restart:
1163 if (setjmp (toplevel) != 0)
1165 /* An error occurred. */
1166 if (response_needed)
1168 write_enn (own_buf);
1169 putpkt (own_buf);
1173 disable_async_io ();
1174 while (!exit_requested)
1176 unsigned char sig;
1177 int packet_len;
1178 int new_packet_len = -1;
1180 response_needed = 0;
1181 packet_len = getpkt (own_buf);
1182 if (packet_len <= 0)
1183 break;
1184 response_needed = 1;
1186 i = 0;
1187 ch = own_buf[i++];
1188 switch (ch)
1190 case 'q':
1191 handle_query (own_buf, packet_len, &new_packet_len);
1192 break;
1193 case 'Q':
1194 handle_general_set (own_buf);
1195 break;
1196 case 'D':
1197 require_running (own_buf);
1198 fprintf (stderr, "Detaching from inferior\n");
1199 if (detach_inferior () != 0)
1200 write_enn (own_buf);
1201 else
1203 write_ok (own_buf);
1205 if (extended_protocol)
1207 /* Treat this like a normal program exit. */
1208 signal = 0;
1209 status = 'W';
1211 else
1213 putpkt (own_buf);
1214 remote_close ();
1216 /* If we are attached, then we can exit. Otherwise, we
1217 need to hang around doing nothing, until the child
1218 is gone. */
1219 if (!attached)
1220 join_inferior ();
1222 exit (0);
1225 break;
1226 case '!':
1227 extended_protocol = 1;
1228 write_ok (own_buf);
1229 break;
1230 case '?':
1231 prepare_resume_reply (own_buf, status, signal);
1232 break;
1233 case 'H':
1234 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
1236 unsigned long gdb_id, thread_id;
1238 require_running (own_buf);
1239 gdb_id = strtoul (&own_buf[2], NULL, 16);
1240 if (gdb_id == 0 || gdb_id == -1)
1241 thread_id = gdb_id;
1242 else
1244 thread_id = gdb_id_to_thread_id (gdb_id);
1245 if (thread_id == 0)
1247 write_enn (own_buf);
1248 break;
1252 if (own_buf[1] == 'g')
1254 general_thread = thread_id;
1255 set_desired_inferior (1);
1257 else if (own_buf[1] == 'c')
1258 cont_thread = thread_id;
1259 else if (own_buf[1] == 's')
1260 step_thread = thread_id;
1262 write_ok (own_buf);
1264 else
1266 /* Silently ignore it so that gdb can extend the protocol
1267 without compatibility headaches. */
1268 own_buf[0] = '\0';
1270 break;
1271 case 'g':
1272 require_running (own_buf);
1273 set_desired_inferior (1);
1274 registers_to_string (own_buf);
1275 break;
1276 case 'G':
1277 require_running (own_buf);
1278 set_desired_inferior (1);
1279 registers_from_string (&own_buf[1]);
1280 write_ok (own_buf);
1281 break;
1282 case 'm':
1283 require_running (own_buf);
1284 decode_m_packet (&own_buf[1], &mem_addr, &len);
1285 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
1286 convert_int_to_ascii (mem_buf, own_buf, len);
1287 else
1288 write_enn (own_buf);
1289 break;
1290 case 'M':
1291 require_running (own_buf);
1292 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
1293 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
1294 write_ok (own_buf);
1295 else
1296 write_enn (own_buf);
1297 break;
1298 case 'X':
1299 require_running (own_buf);
1300 if (decode_X_packet (&own_buf[1], packet_len - 1,
1301 &mem_addr, &len, mem_buf) < 0
1302 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
1303 write_enn (own_buf);
1304 else
1305 write_ok (own_buf);
1306 break;
1307 case 'C':
1308 require_running (own_buf);
1309 convert_ascii_to_int (own_buf + 1, &sig, 1);
1310 if (target_signal_to_host_p (sig))
1311 signal = target_signal_to_host (sig);
1312 else
1313 signal = 0;
1314 myresume (own_buf, 0, &signal, &status);
1315 break;
1316 case 'S':
1317 require_running (own_buf);
1318 convert_ascii_to_int (own_buf + 1, &sig, 1);
1319 if (target_signal_to_host_p (sig))
1320 signal = target_signal_to_host (sig);
1321 else
1322 signal = 0;
1323 myresume (own_buf, 1, &signal, &status);
1324 break;
1325 case 'c':
1326 require_running (own_buf);
1327 signal = 0;
1328 myresume (own_buf, 0, &signal, &status);
1329 break;
1330 case 's':
1331 require_running (own_buf);
1332 signal = 0;
1333 myresume (own_buf, 1, &signal, &status);
1334 break;
1335 case 'Z':
1337 char *lenptr;
1338 char *dataptr;
1339 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1340 int len = strtol (lenptr + 1, &dataptr, 16);
1341 char type = own_buf[1];
1343 if (the_target->insert_watchpoint == NULL
1344 || (type < '2' || type > '4'))
1346 /* No watchpoint support or not a watchpoint command;
1347 unrecognized either way. */
1348 own_buf[0] = '\0';
1350 else
1352 int res;
1354 require_running (own_buf);
1355 res = (*the_target->insert_watchpoint) (type, addr, len);
1356 if (res == 0)
1357 write_ok (own_buf);
1358 else if (res == 1)
1359 /* Unsupported. */
1360 own_buf[0] = '\0';
1361 else
1362 write_enn (own_buf);
1364 break;
1366 case 'z':
1368 char *lenptr;
1369 char *dataptr;
1370 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1371 int len = strtol (lenptr + 1, &dataptr, 16);
1372 char type = own_buf[1];
1374 if (the_target->remove_watchpoint == NULL
1375 || (type < '2' || type > '4'))
1377 /* No watchpoint support or not a watchpoint command;
1378 unrecognized either way. */
1379 own_buf[0] = '\0';
1381 else
1383 int res;
1385 require_running (own_buf);
1386 res = (*the_target->remove_watchpoint) (type, addr, len);
1387 if (res == 0)
1388 write_ok (own_buf);
1389 else if (res == 1)
1390 /* Unsupported. */
1391 own_buf[0] = '\0';
1392 else
1393 write_enn (own_buf);
1395 break;
1397 case 'k':
1398 response_needed = 0;
1399 if (!target_running ())
1400 /* The packet we received doesn't make sense - but we
1401 can't reply to it, either. */
1402 goto restart;
1404 fprintf (stderr, "Killing inferior\n");
1405 kill_inferior ();
1407 /* When using the extended protocol, we wait with no
1408 program running. The traditional protocol will exit
1409 instead. */
1410 if (extended_protocol)
1412 status = 'X';
1413 signal = TARGET_SIGNAL_KILL;
1414 was_running = 0;
1415 goto restart;
1417 else
1419 exit (0);
1420 break;
1422 case 'T':
1424 unsigned long gdb_id, thread_id;
1426 require_running (own_buf);
1427 gdb_id = strtoul (&own_buf[1], NULL, 16);
1428 thread_id = gdb_id_to_thread_id (gdb_id);
1429 if (thread_id == 0)
1431 write_enn (own_buf);
1432 break;
1435 if (mythread_alive (thread_id))
1436 write_ok (own_buf);
1437 else
1438 write_enn (own_buf);
1440 break;
1441 case 'R':
1442 response_needed = 0;
1444 /* Restarting the inferior is only supported in the
1445 extended protocol. */
1446 if (extended_protocol)
1448 if (target_running ())
1449 kill_inferior ();
1450 fprintf (stderr, "GDBserver restarting\n");
1452 /* Wait till we are at 1st instruction in prog. */
1453 if (program_argv != NULL)
1454 signal = start_inferior (program_argv, &status);
1455 else
1457 status = 'X';
1458 signal = TARGET_SIGNAL_KILL;
1460 goto restart;
1462 else
1464 /* It is a request we don't understand. Respond with an
1465 empty packet so that gdb knows that we don't support this
1466 request. */
1467 own_buf[0] = '\0';
1468 break;
1470 case 'v':
1471 /* Extended (long) request. */
1472 handle_v_requests (own_buf, &status, &signal,
1473 packet_len, &new_packet_len);
1474 break;
1476 default:
1477 /* It is a request we don't understand. Respond with an
1478 empty packet so that gdb knows that we don't support this
1479 request. */
1480 own_buf[0] = '\0';
1481 break;
1484 if (new_packet_len != -1)
1485 putpkt_binary (own_buf, new_packet_len);
1486 else
1487 putpkt (own_buf);
1489 response_needed = 0;
1491 if (was_running && (status == 'W' || status == 'X'))
1493 was_running = 0;
1495 if (status == 'W')
1496 fprintf (stderr,
1497 "\nChild exited with status %d\n", signal);
1498 if (status == 'X')
1499 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
1500 target_signal_to_host (signal),
1501 target_signal_to_name (signal));
1503 if (extended_protocol)
1504 goto restart;
1505 else
1507 fprintf (stderr, "GDBserver exiting\n");
1508 exit (0);
1512 if (status != 'W' && status != 'X')
1513 was_running = 1;
1516 /* If an exit was requested (using the "monitor exit" command),
1517 terminate now. The only other way to get here is for
1518 getpkt to fail; close the connection and reopen it at the
1519 top of the loop. */
1521 if (exit_requested)
1523 remote_close ();
1524 if (attached && target_running ())
1525 detach_inferior ();
1526 else if (target_running ())
1527 kill_inferior ();
1528 exit (0);
1530 else
1532 fprintf (stderr, "Remote side has terminated connection. "
1533 "GDBserver will reopen the connection.\n");
1534 remote_close ();