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