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