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