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