* server.c (handle_query): Never return "unsupported" for
[deliverable/binutils-gdb.git] / gdb / gdbserver / server.c
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.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "server.h"
21
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
31
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;
38
39 static int extended_protocol;
40 static int attached;
41 static int response_needed;
42 static int exit_requested;
43
44 static char **program_argv, **wrapper_argv;
45
46 /* Enable miscellaneous debugging output. The name is historical - it
47 was originally used to debug LinuxThreads support. */
48 int debug_threads;
49
50 int pass_signals[TARGET_SIGNAL_LAST];
51
52 jmp_buf toplevel;
53
54 const char *gdbserver_xmltarget;
55
56 /* The PID of the originally created or attached inferior. Used to
57 send signals to the process when GDB sends us an asynchronous interrupt
58 (user hitting Control-C in the client), and to wait for the child to exit
59 when no longer debugging it. */
60
61 unsigned long signal_pid;
62
63 #ifdef SIGTTOU
64 /* A file descriptor for the controlling terminal. */
65 int terminal_fd;
66
67 /* TERMINAL_FD's original foreground group. */
68 pid_t old_foreground_pgrp;
69
70 /* Hand back terminal ownership to the original foreground group. */
71
72 static void
73 restore_old_foreground_pgrp (void)
74 {
75 tcsetpgrp (terminal_fd, old_foreground_pgrp);
76 }
77 #endif
78
79 static int
80 target_running (void)
81 {
82 return all_threads.head != NULL;
83 }
84
85 static int
86 start_inferior (char **argv, char *statusptr)
87 {
88 char **new_argv = argv;
89 attached = 0;
90
91 if (wrapper_argv != NULL)
92 {
93 int i, count = 1;
94
95 for (i = 0; wrapper_argv[i] != NULL; i++)
96 count++;
97 for (i = 0; argv[i] != NULL; i++)
98 count++;
99 new_argv = alloca (sizeof (char *) * count);
100 count = 0;
101 for (i = 0; wrapper_argv[i] != NULL; i++)
102 new_argv[count++] = wrapper_argv[i];
103 for (i = 0; argv[i] != NULL; i++)
104 new_argv[count++] = argv[i];
105 new_argv[count] = NULL;
106 }
107
108 #ifdef SIGTTOU
109 signal (SIGTTOU, SIG_DFL);
110 signal (SIGTTIN, SIG_DFL);
111 #endif
112
113 signal_pid = create_inferior (new_argv[0], new_argv);
114
115 /* FIXME: we don't actually know at this point that the create
116 actually succeeded. We won't know that until we wait. */
117 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
118 signal_pid);
119 fflush (stderr);
120
121 #ifdef SIGTTOU
122 signal (SIGTTOU, SIG_IGN);
123 signal (SIGTTIN, SIG_IGN);
124 terminal_fd = fileno (stderr);
125 old_foreground_pgrp = tcgetpgrp (terminal_fd);
126 tcsetpgrp (terminal_fd, signal_pid);
127 atexit (restore_old_foreground_pgrp);
128 #endif
129
130 if (wrapper_argv != NULL)
131 {
132 struct thread_resume resume_info;
133 int sig;
134
135 resume_info.thread = -1;
136 resume_info.step = 0;
137 resume_info.sig = 0;
138 resume_info.leave_stopped = 0;
139
140 sig = mywait (statusptr, 0);
141 if (*statusptr != 'T')
142 return sig;
143
144 do
145 {
146 (*the_target->resume) (&resume_info);
147
148 sig = mywait (statusptr, 0);
149 if (*statusptr != 'T')
150 return sig;
151 }
152 while (sig != TARGET_SIGNAL_TRAP);
153
154 return sig;
155 }
156
157 /* Wait till we are at 1st instruction in program, return signal
158 number (assuming success). */
159 return mywait (statusptr, 0);
160 }
161
162 static int
163 attach_inferior (int pid, char *statusptr, int *sigptr)
164 {
165 /* myattach should return -1 if attaching is unsupported,
166 0 if it succeeded, and call error() otherwise. */
167
168 if (myattach (pid) != 0)
169 return -1;
170
171 attached = 1;
172
173 fprintf (stderr, "Attached; pid = %d\n", pid);
174 fflush (stderr);
175
176 /* FIXME - It may be that we should get the SIGNAL_PID from the
177 attach function, so that it can be the main thread instead of
178 whichever we were told to attach to. */
179 signal_pid = pid;
180
181 *sigptr = mywait (statusptr, 0);
182
183 /* GDB knows to ignore the first SIGSTOP after attaching to a running
184 process using the "attach" command, but this is different; it's
185 just using "target remote". Pretend it's just starting up. */
186 if (*statusptr == 'T' && *sigptr == TARGET_SIGNAL_STOP)
187 *sigptr = TARGET_SIGNAL_TRAP;
188
189 return 0;
190 }
191
192 extern int remote_debug;
193
194 /* Decode a qXfer read request. Return 0 if everything looks OK,
195 or -1 otherwise. */
196
197 static int
198 decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
199 {
200 /* Extract and NUL-terminate the annex. */
201 *annex = buf;
202 while (*buf && *buf != ':')
203 buf++;
204 if (*buf == '\0')
205 return -1;
206 *buf++ = 0;
207
208 /* After the read marker and annex, qXfer looks like a
209 traditional 'm' packet. */
210 decode_m_packet (buf, ofs, len);
211
212 return 0;
213 }
214
215 /* Write the response to a successful qXfer read. Returns the
216 length of the (binary) data stored in BUF, corresponding
217 to as much of DATA/LEN as we could fit. IS_MORE controls
218 the first character of the response. */
219 static int
220 write_qxfer_response (char *buf, const void *data, int len, int is_more)
221 {
222 int out_len;
223
224 if (is_more)
225 buf[0] = 'm';
226 else
227 buf[0] = 'l';
228
229 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
230 PBUFSIZ - 2) + 1;
231 }
232
233 /* Handle all of the extended 'Q' packets. */
234 void
235 handle_general_set (char *own_buf)
236 {
237 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
238 {
239 int numsigs = (int) TARGET_SIGNAL_LAST, i;
240 const char *p = own_buf + strlen ("QPassSignals:");
241 CORE_ADDR cursig;
242
243 p = decode_address_to_semicolon (&cursig, p);
244 for (i = 0; i < numsigs; i++)
245 {
246 if (i == cursig)
247 {
248 pass_signals[i] = 1;
249 if (*p == '\0')
250 /* Keep looping, to clear the remaining signals. */
251 cursig = -1;
252 else
253 p = decode_address_to_semicolon (&cursig, p);
254 }
255 else
256 pass_signals[i] = 0;
257 }
258 strcpy (own_buf, "OK");
259 return;
260 }
261
262 /* Otherwise we didn't know what packet it was. Say we didn't
263 understand it. */
264 own_buf[0] = 0;
265 }
266
267 static const char *
268 get_features_xml (const char *annex)
269 {
270 /* gdbserver_xmltarget defines what to return when looking
271 for the "target.xml" file. Its contents can either be
272 verbatim XML code (prefixed with a '@') or else the name
273 of the actual XML file to be used in place of "target.xml".
274
275 This variable is set up from the auto-generated
276 init_registers_... routine for the current target. */
277
278 if (gdbserver_xmltarget
279 && strcmp (annex, "target.xml") == 0)
280 {
281 if (*gdbserver_xmltarget == '@')
282 return gdbserver_xmltarget + 1;
283 else
284 annex = gdbserver_xmltarget;
285 }
286
287 #ifdef USE_XML
288 {
289 extern const char *const xml_builtin[][2];
290 int i;
291
292 /* Look for the annex. */
293 for (i = 0; xml_builtin[i][0] != NULL; i++)
294 if (strcmp (annex, xml_builtin[i][0]) == 0)
295 break;
296
297 if (xml_builtin[i][0] != NULL)
298 return xml_builtin[i][1];
299 }
300 #endif
301
302 return NULL;
303 }
304
305 void
306 monitor_show_help (void)
307 {
308 monitor_output ("The following monitor commands are supported:\n");
309 monitor_output (" set debug <0|1>\n");
310 monitor_output (" Enable general debugging messages\n");
311 monitor_output (" set remote-debug <0|1>\n");
312 monitor_output (" Enable remote protocol debugging messages\n");
313 monitor_output (" exit\n");
314 monitor_output (" Quit GDBserver\n");
315 }
316
317 #define require_running(BUF) \
318 if (!target_running ()) \
319 { \
320 write_enn (BUF); \
321 return; \
322 }
323
324 /* Handle all of the extended 'q' packets. */
325 void
326 handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
327 {
328 static struct inferior_list_entry *thread_ptr;
329
330 /* Reply the current thread id. */
331 if (strcmp ("qC", own_buf) == 0)
332 {
333 require_running (own_buf);
334 thread_ptr = all_threads.head;
335 sprintf (own_buf, "QC%x",
336 thread_to_gdb_id ((struct thread_info *)thread_ptr));
337 return;
338 }
339
340 if (strcmp ("qSymbol::", own_buf) == 0)
341 {
342 if (target_running () && the_target->look_up_symbols != NULL)
343 (*the_target->look_up_symbols) ();
344
345 strcpy (own_buf, "OK");
346 return;
347 }
348
349 if (strcmp ("qfThreadInfo", own_buf) == 0)
350 {
351 require_running (own_buf);
352 thread_ptr = all_threads.head;
353 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
354 thread_ptr = thread_ptr->next;
355 return;
356 }
357
358 if (strcmp ("qsThreadInfo", own_buf) == 0)
359 {
360 require_running (own_buf);
361 if (thread_ptr != NULL)
362 {
363 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
364 thread_ptr = thread_ptr->next;
365 return;
366 }
367 else
368 {
369 sprintf (own_buf, "l");
370 return;
371 }
372 }
373
374 if (the_target->read_offsets != NULL
375 && strcmp ("qOffsets", own_buf) == 0)
376 {
377 CORE_ADDR text, data;
378
379 require_running (own_buf);
380 if (the_target->read_offsets (&text, &data))
381 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
382 (long)text, (long)data, (long)data);
383 else
384 write_enn (own_buf);
385
386 return;
387 }
388
389 if (the_target->qxfer_spu != NULL
390 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
391 {
392 char *annex;
393 int n;
394 unsigned int len;
395 CORE_ADDR ofs;
396 unsigned char *spu_buf;
397
398 require_running (own_buf);
399 strcpy (own_buf, "E00");
400 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
401 return;
402 if (len > PBUFSIZ - 2)
403 len = PBUFSIZ - 2;
404 spu_buf = malloc (len + 1);
405 if (!spu_buf)
406 return;
407
408 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
409 if (n < 0)
410 write_enn (own_buf);
411 else if (n > len)
412 *new_packet_len_p = write_qxfer_response
413 (own_buf, spu_buf, len, 1);
414 else
415 *new_packet_len_p = write_qxfer_response
416 (own_buf, spu_buf, n, 0);
417
418 free (spu_buf);
419 return;
420 }
421
422 if (the_target->qxfer_spu != NULL
423 && strncmp ("qXfer:spu:write:", own_buf, 16) == 0)
424 {
425 char *annex;
426 int n;
427 unsigned int len;
428 CORE_ADDR ofs;
429 unsigned char *spu_buf;
430
431 require_running (own_buf);
432 strcpy (own_buf, "E00");
433 spu_buf = malloc (packet_len - 15);
434 if (!spu_buf)
435 return;
436 if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
437 &ofs, &len, spu_buf) < 0)
438 {
439 free (spu_buf);
440 return;
441 }
442
443 n = (*the_target->qxfer_spu)
444 (annex, NULL, (unsigned const char *)spu_buf, ofs, len);
445 if (n < 0)
446 write_enn (own_buf);
447 else
448 sprintf (own_buf, "%x", n);
449
450 free (spu_buf);
451 return;
452 }
453
454 if (the_target->read_auxv != NULL
455 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
456 {
457 unsigned char *data;
458 int n;
459 CORE_ADDR ofs;
460 unsigned int len;
461 char *annex;
462
463 require_running (own_buf);
464
465 /* Reject any annex; grab the offset and length. */
466 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
467 || annex[0] != '\0')
468 {
469 strcpy (own_buf, "E00");
470 return;
471 }
472
473 /* Read one extra byte, as an indicator of whether there is
474 more. */
475 if (len > PBUFSIZ - 2)
476 len = PBUFSIZ - 2;
477 data = malloc (len + 1);
478 n = (*the_target->read_auxv) (ofs, data, len + 1);
479 if (n < 0)
480 write_enn (own_buf);
481 else if (n > len)
482 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
483 else
484 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
485
486 free (data);
487
488 return;
489 }
490
491 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
492 {
493 CORE_ADDR ofs;
494 unsigned int len, total_len;
495 const char *document;
496 char *annex;
497
498 require_running (own_buf);
499
500 /* Grab the annex, offset, and length. */
501 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
502 {
503 strcpy (own_buf, "E00");
504 return;
505 }
506
507 /* Now grab the correct annex. */
508 document = get_features_xml (annex);
509 if (document == NULL)
510 {
511 strcpy (own_buf, "E00");
512 return;
513 }
514
515 total_len = strlen (document);
516 if (len > PBUFSIZ - 2)
517 len = PBUFSIZ - 2;
518
519 if (ofs > total_len)
520 write_enn (own_buf);
521 else if (len < total_len - ofs)
522 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
523 len, 1);
524 else
525 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
526 total_len - ofs, 0);
527
528 return;
529 }
530
531 if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
532 {
533 CORE_ADDR ofs;
534 unsigned int len, total_len;
535 char *document, *p;
536 struct inferior_list_entry *dll_ptr;
537 char *annex;
538
539 require_running (own_buf);
540
541 /* Reject any annex; grab the offset and length. */
542 if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
543 || annex[0] != '\0')
544 {
545 strcpy (own_buf, "E00");
546 return;
547 }
548
549 /* Over-estimate the necessary memory. Assume that every character
550 in the library name must be escaped. */
551 total_len = 64;
552 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
553 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
554
555 document = malloc (total_len);
556 strcpy (document, "<library-list>\n");
557 p = document + strlen (document);
558
559 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
560 {
561 struct dll_info *dll = (struct dll_info *) dll_ptr;
562 char *name;
563
564 strcpy (p, " <library name=\"");
565 p = p + strlen (p);
566 name = xml_escape_text (dll->name);
567 strcpy (p, name);
568 free (name);
569 p = p + strlen (p);
570 strcpy (p, "\"><segment address=\"");
571 p = p + strlen (p);
572 sprintf (p, "0x%lx", (long) dll->base_addr);
573 p = p + strlen (p);
574 strcpy (p, "\"/></library>\n");
575 p = p + strlen (p);
576 }
577
578 strcpy (p, "</library-list>\n");
579
580 total_len = strlen (document);
581 if (len > PBUFSIZ - 2)
582 len = PBUFSIZ - 2;
583
584 if (ofs > total_len)
585 write_enn (own_buf);
586 else if (len < total_len - ofs)
587 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
588 len, 1);
589 else
590 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
591 total_len - ofs, 0);
592
593 free (document);
594 return;
595 }
596
597 /* Protocol features query. */
598 if (strncmp ("qSupported", own_buf, 10) == 0
599 && (own_buf[10] == ':' || own_buf[10] == '\0'))
600 {
601 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
602
603 /* We do not have any hook to indicate whether the target backend
604 supports qXfer:libraries:read, so always report it. */
605 strcat (own_buf, ";qXfer:libraries:read+");
606
607 if (the_target->read_auxv != NULL)
608 strcat (own_buf, ";qXfer:auxv:read+");
609
610 if (the_target->qxfer_spu != NULL)
611 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
612
613 /* We always report qXfer:features:read, as targets may
614 install XML files on a subsequent call to arch_setup.
615 If we reported to GDB on startup that we don't support
616 qXfer:feature:read at all, we will never be re-queried. */
617 strcat (own_buf, ";qXfer:features:read+");
618
619 return;
620 }
621
622 /* Thread-local storage support. */
623 if (the_target->get_tls_address != NULL
624 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
625 {
626 char *p = own_buf + 12;
627 CORE_ADDR parts[3], address = 0;
628 int i, err;
629
630 require_running (own_buf);
631
632 for (i = 0; i < 3; i++)
633 {
634 char *p2;
635 int len;
636
637 if (p == NULL)
638 break;
639
640 p2 = strchr (p, ',');
641 if (p2)
642 {
643 len = p2 - p;
644 p2++;
645 }
646 else
647 {
648 len = strlen (p);
649 p2 = NULL;
650 }
651
652 decode_address (&parts[i], p, len);
653 p = p2;
654 }
655
656 if (p != NULL || i < 3)
657 err = 1;
658 else
659 {
660 struct thread_info *thread = gdb_id_to_thread (parts[0]);
661
662 if (thread == NULL)
663 err = 2;
664 else
665 err = the_target->get_tls_address (thread, parts[1], parts[2],
666 &address);
667 }
668
669 if (err == 0)
670 {
671 sprintf (own_buf, "%llx", address);
672 return;
673 }
674 else if (err > 0)
675 {
676 write_enn (own_buf);
677 return;
678 }
679
680 /* Otherwise, pretend we do not understand this packet. */
681 }
682
683 /* Handle "monitor" commands. */
684 if (strncmp ("qRcmd,", own_buf, 6) == 0)
685 {
686 char *mon = malloc (PBUFSIZ);
687 int len = strlen (own_buf + 6);
688
689 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
690 {
691 write_enn (own_buf);
692 free (mon);
693 return;
694 }
695 mon[len / 2] = '\0';
696
697 write_ok (own_buf);
698
699 if (strcmp (mon, "set debug 1") == 0)
700 {
701 debug_threads = 1;
702 monitor_output ("Debug output enabled.\n");
703 }
704 else if (strcmp (mon, "set debug 0") == 0)
705 {
706 debug_threads = 0;
707 monitor_output ("Debug output disabled.\n");
708 }
709 else if (strcmp (mon, "set remote-debug 1") == 0)
710 {
711 remote_debug = 1;
712 monitor_output ("Protocol debug output enabled.\n");
713 }
714 else if (strcmp (mon, "set remote-debug 0") == 0)
715 {
716 remote_debug = 0;
717 monitor_output ("Protocol debug output disabled.\n");
718 }
719 else if (strcmp (mon, "help") == 0)
720 monitor_show_help ();
721 else if (strcmp (mon, "exit") == 0)
722 exit_requested = 1;
723 else
724 {
725 monitor_output ("Unknown monitor command.\n\n");
726 monitor_show_help ();
727 write_enn (own_buf);
728 }
729
730 free (mon);
731 return;
732 }
733
734 /* Otherwise we didn't know what packet it was. Say we didn't
735 understand it. */
736 own_buf[0] = 0;
737 }
738
739 /* Parse vCont packets. */
740 void
741 handle_v_cont (char *own_buf, char *status, int *signal)
742 {
743 char *p, *q;
744 int n = 0, i = 0;
745 struct thread_resume *resume_info, default_action;
746
747 /* Count the number of semicolons in the packet. There should be one
748 for every action. */
749 p = &own_buf[5];
750 while (p)
751 {
752 n++;
753 p++;
754 p = strchr (p, ';');
755 }
756 /* Allocate room for one extra action, for the default remain-stopped
757 behavior; if no default action is in the list, we'll need the extra
758 slot. */
759 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
760
761 default_action.thread = -1;
762 default_action.leave_stopped = 1;
763 default_action.step = 0;
764 default_action.sig = 0;
765
766 p = &own_buf[5];
767 i = 0;
768 while (*p)
769 {
770 p++;
771
772 resume_info[i].leave_stopped = 0;
773
774 if (p[0] == 's' || p[0] == 'S')
775 resume_info[i].step = 1;
776 else if (p[0] == 'c' || p[0] == 'C')
777 resume_info[i].step = 0;
778 else
779 goto err;
780
781 if (p[0] == 'S' || p[0] == 'C')
782 {
783 int sig;
784 sig = strtol (p + 1, &q, 16);
785 if (p == q)
786 goto err;
787 p = q;
788
789 if (!target_signal_to_host_p (sig))
790 goto err;
791 resume_info[i].sig = target_signal_to_host (sig);
792 }
793 else
794 {
795 resume_info[i].sig = 0;
796 p = p + 1;
797 }
798
799 if (p[0] == 0)
800 {
801 resume_info[i].thread = -1;
802 default_action = resume_info[i];
803
804 /* Note: we don't increment i here, we'll overwrite this entry
805 the next time through. */
806 }
807 else if (p[0] == ':')
808 {
809 unsigned int gdb_id = strtoul (p + 1, &q, 16);
810 unsigned long thread_id;
811
812 if (p == q)
813 goto err;
814 p = q;
815 if (p[0] != ';' && p[0] != 0)
816 goto err;
817
818 thread_id = gdb_id_to_thread_id (gdb_id);
819 if (thread_id)
820 resume_info[i].thread = thread_id;
821 else
822 goto err;
823
824 i++;
825 }
826 }
827
828 resume_info[i] = default_action;
829
830 /* Still used in occasional places in the backend. */
831 if (n == 1 && resume_info[0].thread != -1)
832 cont_thread = resume_info[0].thread;
833 else
834 cont_thread = -1;
835 set_desired_inferior (0);
836
837 enable_async_io ();
838 (*the_target->resume) (resume_info);
839
840 free (resume_info);
841
842 *signal = mywait (status, 1);
843 prepare_resume_reply (own_buf, *status, *signal);
844 disable_async_io ();
845 return;
846
847 err:
848 write_enn (own_buf);
849 free (resume_info);
850 return;
851 }
852
853 /* Attach to a new program. Return 1 if successful, 0 if failure. */
854 int
855 handle_v_attach (char *own_buf, char *status, int *signal)
856 {
857 int pid;
858
859 pid = strtol (own_buf + 8, NULL, 16);
860 if (pid != 0 && attach_inferior (pid, status, signal) == 0)
861 {
862 prepare_resume_reply (own_buf, *status, *signal);
863 return 1;
864 }
865 else
866 {
867 write_enn (own_buf);
868 return 0;
869 }
870 }
871
872 /* Run a new program. Return 1 if successful, 0 if failure. */
873 static int
874 handle_v_run (char *own_buf, char *status, int *signal)
875 {
876 char *p, **pp, *next_p, **new_argv;
877 int i, new_argc;
878
879 new_argc = 0;
880 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
881 {
882 p++;
883 new_argc++;
884 }
885
886 new_argv = malloc ((new_argc + 2) * sizeof (char *));
887 i = 0;
888 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
889 {
890 next_p = strchr (p, ';');
891 if (next_p == NULL)
892 next_p = p + strlen (p);
893
894 if (i == 0 && p == next_p)
895 new_argv[i] = NULL;
896 else
897 {
898 new_argv[i] = malloc (1 + (next_p - p) / 2);
899 unhexify (new_argv[i], p, (next_p - p) / 2);
900 new_argv[i][(next_p - p) / 2] = '\0';
901 }
902
903 if (*next_p)
904 next_p++;
905 i++;
906 }
907 new_argv[i] = NULL;
908
909 if (new_argv[0] == NULL)
910 {
911 if (program_argv == NULL)
912 {
913 write_enn (own_buf);
914 return 0;
915 }
916
917 new_argv[0] = strdup (program_argv[0]);
918 }
919
920 /* Free the old argv. */
921 if (program_argv)
922 {
923 for (pp = program_argv; *pp != NULL; pp++)
924 free (*pp);
925 free (program_argv);
926 }
927 program_argv = new_argv;
928
929 *signal = start_inferior (program_argv, status);
930 if (*status == 'T')
931 {
932 prepare_resume_reply (own_buf, *status, *signal);
933 return 1;
934 }
935 else
936 {
937 write_enn (own_buf);
938 return 0;
939 }
940 }
941
942 /* Handle all of the extended 'v' packets. */
943 void
944 handle_v_requests (char *own_buf, char *status, int *signal,
945 int packet_len, int *new_packet_len)
946 {
947 if (strncmp (own_buf, "vCont;", 6) == 0)
948 {
949 require_running (own_buf);
950 handle_v_cont (own_buf, status, signal);
951 return;
952 }
953
954 if (strncmp (own_buf, "vCont?", 6) == 0)
955 {
956 strcpy (own_buf, "vCont;c;C;s;S");
957 return;
958 }
959
960 if (strncmp (own_buf, "vFile:", 6) == 0
961 && handle_vFile (own_buf, packet_len, new_packet_len))
962 return;
963
964 if (strncmp (own_buf, "vAttach;", 8) == 0)
965 {
966 if (target_running ())
967 {
968 fprintf (stderr, "Already debugging a process\n");
969 write_enn (own_buf);
970 return;
971 }
972 handle_v_attach (own_buf, status, signal);
973 return;
974 }
975
976 if (strncmp (own_buf, "vRun;", 5) == 0)
977 {
978 if (target_running ())
979 {
980 fprintf (stderr, "Already debugging a process\n");
981 write_enn (own_buf);
982 return;
983 }
984 handle_v_run (own_buf, status, signal);
985 return;
986 }
987
988 /* Otherwise we didn't know what packet it was. Say we didn't
989 understand it. */
990 own_buf[0] = 0;
991 return;
992 }
993
994 void
995 myresume (char *own_buf, int step, int *signalp, char *statusp)
996 {
997 struct thread_resume resume_info[2];
998 int n = 0;
999 int sig = *signalp;
1000
1001 set_desired_inferior (0);
1002
1003 if (step || sig || (cont_thread != 0 && cont_thread != -1))
1004 {
1005 resume_info[0].thread
1006 = ((struct inferior_list_entry *) current_inferior)->id;
1007 resume_info[0].step = step;
1008 resume_info[0].sig = sig;
1009 resume_info[0].leave_stopped = 0;
1010 n++;
1011 }
1012 resume_info[n].thread = -1;
1013 resume_info[n].step = 0;
1014 resume_info[n].sig = 0;
1015 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
1016
1017 enable_async_io ();
1018 (*the_target->resume) (resume_info);
1019 *signalp = mywait (statusp, 1);
1020 prepare_resume_reply (own_buf, *statusp, *signalp);
1021 disable_async_io ();
1022 }
1023
1024 static void
1025 gdbserver_version (void)
1026 {
1027 printf ("GNU gdbserver %s\n"
1028 "Copyright (C) 2007 Free Software Foundation, Inc.\n"
1029 "gdbserver is free software, covered by the GNU General Public License.\n"
1030 "This gdbserver was configured as \"%s\"\n",
1031 version, host_name);
1032 }
1033
1034 static void
1035 gdbserver_usage (void)
1036 {
1037 printf ("Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
1038 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1039 "\tgdbserver [OPTIONS] --multi COMM\n"
1040 "\n"
1041 "COMM may either be a tty device (for serial debugging), or \n"
1042 "HOST:PORT to listen for a TCP connection.\n"
1043 "\n"
1044 "Options:\n"
1045 " --debug\t\tEnable debugging output.\n"
1046 " --wrapper WRAPPER --\tRun WRAPPER to start new programs.\n");
1047 }
1048
1049 #undef require_running
1050 #define require_running(BUF) \
1051 if (!target_running ()) \
1052 { \
1053 write_enn (BUF); \
1054 break; \
1055 }
1056
1057 int
1058 main (int argc, char *argv[])
1059 {
1060 char ch, status, *own_buf;
1061 unsigned char *mem_buf;
1062 int i = 0;
1063 int signal;
1064 unsigned int len;
1065 CORE_ADDR mem_addr;
1066 int bad_attach;
1067 int pid;
1068 char *arg_end, *port;
1069 char **next_arg = &argv[1];
1070 int multi_mode = 0;
1071 int attach = 0;
1072 int was_running;
1073
1074 while (*next_arg != NULL && **next_arg == '-')
1075 {
1076 if (strcmp (*next_arg, "--version") == 0)
1077 {
1078 gdbserver_version ();
1079 exit (0);
1080 }
1081 else if (strcmp (*next_arg, "--help") == 0)
1082 {
1083 gdbserver_usage ();
1084 exit (0);
1085 }
1086 else if (strcmp (*next_arg, "--attach") == 0)
1087 attach = 1;
1088 else if (strcmp (*next_arg, "--multi") == 0)
1089 multi_mode = 1;
1090 else if (strcmp (*next_arg, "--wrapper") == 0)
1091 {
1092 next_arg++;
1093
1094 wrapper_argv = next_arg;
1095 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
1096 next_arg++;
1097
1098 if (next_arg == wrapper_argv || *next_arg == NULL)
1099 {
1100 gdbserver_usage ();
1101 exit (1);
1102 }
1103
1104 /* Consume the "--". */
1105 *next_arg = NULL;
1106 }
1107 else if (strcmp (*next_arg, "--debug") == 0)
1108 debug_threads = 1;
1109 else
1110 {
1111 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
1112 exit (1);
1113 }
1114
1115 next_arg++;
1116 continue;
1117 }
1118
1119 if (setjmp (toplevel))
1120 {
1121 fprintf (stderr, "Exiting\n");
1122 exit (1);
1123 }
1124
1125 port = *next_arg;
1126 next_arg++;
1127 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
1128 {
1129 gdbserver_usage ();
1130 exit (1);
1131 }
1132
1133 bad_attach = 0;
1134 pid = 0;
1135
1136 /* --attach used to come after PORT, so allow it there for
1137 compatibility. */
1138 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
1139 {
1140 attach = 1;
1141 next_arg++;
1142 }
1143
1144 if (attach
1145 && (*next_arg == NULL
1146 || (*next_arg)[0] == '\0'
1147 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
1148 || *arg_end != '\0'
1149 || next_arg[1] != NULL))
1150 bad_attach = 1;
1151
1152 if (bad_attach)
1153 {
1154 gdbserver_usage ();
1155 exit (1);
1156 }
1157
1158 initialize_async_io ();
1159 initialize_low ();
1160
1161 own_buf = malloc (PBUFSIZ + 1);
1162 mem_buf = malloc (PBUFSIZ);
1163
1164 if (pid == 0 && *next_arg != NULL)
1165 {
1166 int i, n;
1167
1168 n = argc - (next_arg - argv);
1169 program_argv = malloc (sizeof (char *) * (n + 1));
1170 for (i = 0; i < n; i++)
1171 program_argv[i] = strdup (next_arg[i]);
1172 program_argv[i] = NULL;
1173
1174 /* Wait till we are at first instruction in program. */
1175 signal = start_inferior (program_argv, &status);
1176
1177 /* We are now (hopefully) stopped at the first instruction of
1178 the target process. This assumes that the target process was
1179 successfully created. */
1180 }
1181 else if (pid != 0)
1182 {
1183 if (attach_inferior (pid, &status, &signal) == -1)
1184 error ("Attaching not supported on this target");
1185
1186 /* Otherwise succeeded. */
1187 }
1188 else
1189 {
1190 status = 'W';
1191 signal = 0;
1192 }
1193
1194 /* Don't report shared library events on the initial connection,
1195 even if some libraries are preloaded. Avoids the "stopped by
1196 shared library event" notice on gdb side. */
1197 dlls_changed = 0;
1198
1199 if (setjmp (toplevel))
1200 {
1201 fprintf (stderr, "Killing inferior\n");
1202 kill_inferior ();
1203 exit (1);
1204 }
1205
1206 if (status == 'W' || status == 'X')
1207 was_running = 0;
1208 else
1209 was_running = 1;
1210
1211 if (!was_running && !multi_mode)
1212 {
1213 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
1214 exit (1);
1215 }
1216
1217 while (1)
1218 {
1219 remote_open (port);
1220
1221 restart:
1222 if (setjmp (toplevel) != 0)
1223 {
1224 /* An error occurred. */
1225 if (response_needed)
1226 {
1227 write_enn (own_buf);
1228 putpkt (own_buf);
1229 }
1230 }
1231
1232 disable_async_io ();
1233 while (!exit_requested)
1234 {
1235 unsigned char sig;
1236 int packet_len;
1237 int new_packet_len = -1;
1238
1239 response_needed = 0;
1240 packet_len = getpkt (own_buf);
1241 if (packet_len <= 0)
1242 break;
1243 response_needed = 1;
1244
1245 i = 0;
1246 ch = own_buf[i++];
1247 switch (ch)
1248 {
1249 case 'q':
1250 handle_query (own_buf, packet_len, &new_packet_len);
1251 break;
1252 case 'Q':
1253 handle_general_set (own_buf);
1254 break;
1255 case 'D':
1256 require_running (own_buf);
1257 fprintf (stderr, "Detaching from inferior\n");
1258 if (detach_inferior () != 0)
1259 write_enn (own_buf);
1260 else
1261 {
1262 write_ok (own_buf);
1263
1264 if (extended_protocol)
1265 {
1266 /* Treat this like a normal program exit. */
1267 signal = 0;
1268 status = 'W';
1269 }
1270 else
1271 {
1272 putpkt (own_buf);
1273 remote_close ();
1274
1275 /* If we are attached, then we can exit. Otherwise, we
1276 need to hang around doing nothing, until the child
1277 is gone. */
1278 if (!attached)
1279 join_inferior ();
1280
1281 exit (0);
1282 }
1283 }
1284 break;
1285 case '!':
1286 extended_protocol = 1;
1287 write_ok (own_buf);
1288 break;
1289 case '?':
1290 prepare_resume_reply (own_buf, status, signal);
1291 break;
1292 case 'H':
1293 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
1294 {
1295 unsigned long gdb_id, thread_id;
1296
1297 require_running (own_buf);
1298 gdb_id = strtoul (&own_buf[2], NULL, 16);
1299 if (gdb_id == 0 || gdb_id == -1)
1300 thread_id = gdb_id;
1301 else
1302 {
1303 thread_id = gdb_id_to_thread_id (gdb_id);
1304 if (thread_id == 0)
1305 {
1306 write_enn (own_buf);
1307 break;
1308 }
1309 }
1310
1311 if (own_buf[1] == 'g')
1312 {
1313 general_thread = thread_id;
1314 set_desired_inferior (1);
1315 }
1316 else if (own_buf[1] == 'c')
1317 cont_thread = thread_id;
1318 else if (own_buf[1] == 's')
1319 step_thread = thread_id;
1320
1321 write_ok (own_buf);
1322 }
1323 else
1324 {
1325 /* Silently ignore it so that gdb can extend the protocol
1326 without compatibility headaches. */
1327 own_buf[0] = '\0';
1328 }
1329 break;
1330 case 'g':
1331 require_running (own_buf);
1332 set_desired_inferior (1);
1333 registers_to_string (own_buf);
1334 break;
1335 case 'G':
1336 require_running (own_buf);
1337 set_desired_inferior (1);
1338 registers_from_string (&own_buf[1]);
1339 write_ok (own_buf);
1340 break;
1341 case 'm':
1342 require_running (own_buf);
1343 decode_m_packet (&own_buf[1], &mem_addr, &len);
1344 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
1345 convert_int_to_ascii (mem_buf, own_buf, len);
1346 else
1347 write_enn (own_buf);
1348 break;
1349 case 'M':
1350 require_running (own_buf);
1351 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
1352 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
1353 write_ok (own_buf);
1354 else
1355 write_enn (own_buf);
1356 break;
1357 case 'X':
1358 require_running (own_buf);
1359 if (decode_X_packet (&own_buf[1], packet_len - 1,
1360 &mem_addr, &len, mem_buf) < 0
1361 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
1362 write_enn (own_buf);
1363 else
1364 write_ok (own_buf);
1365 break;
1366 case 'C':
1367 require_running (own_buf);
1368 convert_ascii_to_int (own_buf + 1, &sig, 1);
1369 if (target_signal_to_host_p (sig))
1370 signal = target_signal_to_host (sig);
1371 else
1372 signal = 0;
1373 myresume (own_buf, 0, &signal, &status);
1374 break;
1375 case 'S':
1376 require_running (own_buf);
1377 convert_ascii_to_int (own_buf + 1, &sig, 1);
1378 if (target_signal_to_host_p (sig))
1379 signal = target_signal_to_host (sig);
1380 else
1381 signal = 0;
1382 myresume (own_buf, 1, &signal, &status);
1383 break;
1384 case 'c':
1385 require_running (own_buf);
1386 signal = 0;
1387 myresume (own_buf, 0, &signal, &status);
1388 break;
1389 case 's':
1390 require_running (own_buf);
1391 signal = 0;
1392 myresume (own_buf, 1, &signal, &status);
1393 break;
1394 case 'Z':
1395 {
1396 char *lenptr;
1397 char *dataptr;
1398 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1399 int len = strtol (lenptr + 1, &dataptr, 16);
1400 char type = own_buf[1];
1401
1402 if (the_target->insert_watchpoint == NULL
1403 || (type < '2' || type > '4'))
1404 {
1405 /* No watchpoint support or not a watchpoint command;
1406 unrecognized either way. */
1407 own_buf[0] = '\0';
1408 }
1409 else
1410 {
1411 int res;
1412
1413 require_running (own_buf);
1414 res = (*the_target->insert_watchpoint) (type, addr, len);
1415 if (res == 0)
1416 write_ok (own_buf);
1417 else if (res == 1)
1418 /* Unsupported. */
1419 own_buf[0] = '\0';
1420 else
1421 write_enn (own_buf);
1422 }
1423 break;
1424 }
1425 case 'z':
1426 {
1427 char *lenptr;
1428 char *dataptr;
1429 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1430 int len = strtol (lenptr + 1, &dataptr, 16);
1431 char type = own_buf[1];
1432
1433 if (the_target->remove_watchpoint == NULL
1434 || (type < '2' || type > '4'))
1435 {
1436 /* No watchpoint support or not a watchpoint command;
1437 unrecognized either way. */
1438 own_buf[0] = '\0';
1439 }
1440 else
1441 {
1442 int res;
1443
1444 require_running (own_buf);
1445 res = (*the_target->remove_watchpoint) (type, addr, len);
1446 if (res == 0)
1447 write_ok (own_buf);
1448 else if (res == 1)
1449 /* Unsupported. */
1450 own_buf[0] = '\0';
1451 else
1452 write_enn (own_buf);
1453 }
1454 break;
1455 }
1456 case 'k':
1457 response_needed = 0;
1458 if (!target_running ())
1459 /* The packet we received doesn't make sense - but we
1460 can't reply to it, either. */
1461 goto restart;
1462
1463 fprintf (stderr, "Killing inferior\n");
1464 kill_inferior ();
1465
1466 /* When using the extended protocol, we wait with no
1467 program running. The traditional protocol will exit
1468 instead. */
1469 if (extended_protocol)
1470 {
1471 status = 'X';
1472 signal = TARGET_SIGNAL_KILL;
1473 was_running = 0;
1474 goto restart;
1475 }
1476 else
1477 {
1478 exit (0);
1479 break;
1480 }
1481 case 'T':
1482 {
1483 unsigned long gdb_id, thread_id;
1484
1485 require_running (own_buf);
1486 gdb_id = strtoul (&own_buf[1], NULL, 16);
1487 thread_id = gdb_id_to_thread_id (gdb_id);
1488 if (thread_id == 0)
1489 {
1490 write_enn (own_buf);
1491 break;
1492 }
1493
1494 if (mythread_alive (thread_id))
1495 write_ok (own_buf);
1496 else
1497 write_enn (own_buf);
1498 }
1499 break;
1500 case 'R':
1501 response_needed = 0;
1502
1503 /* Restarting the inferior is only supported in the
1504 extended protocol. */
1505 if (extended_protocol)
1506 {
1507 if (target_running ())
1508 kill_inferior ();
1509 fprintf (stderr, "GDBserver restarting\n");
1510
1511 /* Wait till we are at 1st instruction in prog. */
1512 if (program_argv != NULL)
1513 signal = start_inferior (program_argv, &status);
1514 else
1515 {
1516 status = 'X';
1517 signal = TARGET_SIGNAL_KILL;
1518 }
1519 goto restart;
1520 }
1521 else
1522 {
1523 /* It is a request we don't understand. Respond with an
1524 empty packet so that gdb knows that we don't support this
1525 request. */
1526 own_buf[0] = '\0';
1527 break;
1528 }
1529 case 'v':
1530 /* Extended (long) request. */
1531 handle_v_requests (own_buf, &status, &signal,
1532 packet_len, &new_packet_len);
1533 break;
1534
1535 default:
1536 /* It is a request we don't understand. Respond with an
1537 empty packet so that gdb knows that we don't support this
1538 request. */
1539 own_buf[0] = '\0';
1540 break;
1541 }
1542
1543 if (new_packet_len != -1)
1544 putpkt_binary (own_buf, new_packet_len);
1545 else
1546 putpkt (own_buf);
1547
1548 response_needed = 0;
1549
1550 if (was_running && (status == 'W' || status == 'X'))
1551 {
1552 was_running = 0;
1553
1554 if (status == 'W')
1555 fprintf (stderr,
1556 "\nChild exited with status %d\n", signal);
1557 if (status == 'X')
1558 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
1559 target_signal_to_host (signal),
1560 target_signal_to_name (signal));
1561
1562 if (extended_protocol)
1563 goto restart;
1564 else
1565 {
1566 fprintf (stderr, "GDBserver exiting\n");
1567 exit (0);
1568 }
1569 }
1570
1571 if (status != 'W' && status != 'X')
1572 was_running = 1;
1573 }
1574
1575 /* If an exit was requested (using the "monitor exit" command),
1576 terminate now. The only other way to get here is for
1577 getpkt to fail; close the connection and reopen it at the
1578 top of the loop. */
1579
1580 if (exit_requested)
1581 {
1582 remote_close ();
1583 if (attached && target_running ())
1584 detach_inferior ();
1585 else if (target_running ())
1586 kill_inferior ();
1587 exit (0);
1588 }
1589 else
1590 {
1591 fprintf (stderr, "Remote side has terminated connection. "
1592 "GDBserver will reopen the connection.\n");
1593 remote_close ();
1594 }
1595 }
1596 }
This page took 0.062192 seconds and 5 git commands to generate.