Non-stop mode support.
[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, 2009 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 #if HAVE_MALLOC_H
32 #include <malloc.h>
33 #endif
34
35 unsigned long cont_thread;
36 unsigned long general_thread;
37 unsigned long step_thread;
38
39 int server_waiting;
40
41 static int extended_protocol;
42 static int attached;
43 static int response_needed;
44 static int exit_requested;
45
46 int non_stop;
47
48 static char **program_argv, **wrapper_argv;
49
50 /* Enable miscellaneous debugging output. The name is historical - it
51 was originally used to debug LinuxThreads support. */
52 int debug_threads;
53
54 int pass_signals[TARGET_SIGNAL_LAST];
55
56 jmp_buf toplevel;
57
58 const char *gdbserver_xmltarget;
59
60 /* The PID of the originally created or attached inferior. Used to
61 send signals to the process when GDB sends us an asynchronous interrupt
62 (user hitting Control-C in the client), and to wait for the child to exit
63 when no longer debugging it. */
64
65 unsigned long signal_pid;
66
67 #ifdef SIGTTOU
68 /* A file descriptor for the controlling terminal. */
69 int terminal_fd;
70
71 /* TERMINAL_FD's original foreground group. */
72 pid_t old_foreground_pgrp;
73
74 /* Hand back terminal ownership to the original foreground group. */
75
76 static void
77 restore_old_foreground_pgrp (void)
78 {
79 tcsetpgrp (terminal_fd, old_foreground_pgrp);
80 }
81 #endif
82
83 /* Set if you want to disable optional thread related packets support
84 in gdbserver, for the sake of testing GDB against stubs that don't
85 support them. */
86 int disable_packet_vCont;
87 int disable_packet_Tthread;
88 int disable_packet_qC;
89 int disable_packet_qfThreadInfo;
90
91 /* Last status reported to GDB. */
92 static struct target_waitstatus last_status;
93 static unsigned long last_ptid;
94
95 static char *own_buf;
96 static unsigned char *mem_buf;
97
98 /* Structure holding information relative to a single stop reply. We
99 keep a queue of these (really a singly-linked list) to push to GDB
100 in non-stop mode. */
101 struct vstop_notif
102 {
103 /* Pointer to next in list. */
104 struct vstop_notif *next;
105
106 /* Thread or process that got the event. */
107 unsigned long ptid;
108
109 /* Event info. */
110 struct target_waitstatus status;
111 };
112
113 /* The pending stop replies list head. */
114 static struct vstop_notif *notif_queue = NULL;
115
116 /* Put a stop reply to the stop reply queue. */
117
118 static void
119 queue_stop_reply (unsigned long ptid, struct target_waitstatus *status)
120 {
121 struct vstop_notif *new_notif;
122
123 new_notif = malloc (sizeof (*new_notif));
124 new_notif->next = NULL;
125 new_notif->ptid = ptid;
126 new_notif->status = *status;
127
128 if (notif_queue)
129 {
130 struct vstop_notif *tail;
131 for (tail = notif_queue;
132 tail && tail->next;
133 tail = tail->next)
134 ;
135 tail->next = new_notif;
136 }
137 else
138 notif_queue = new_notif;
139
140 if (remote_debug)
141 {
142 int i = 0;
143 struct vstop_notif *n;
144
145 for (n = notif_queue; n; n = n->next)
146 i++;
147
148 fprintf (stderr, "pending stop replies: %d\n", i);
149 }
150 }
151
152 /* Place an event in the stop reply queue, and push a notification if
153 we aren't sending one yet. */
154
155 void
156 push_event (unsigned long ptid, struct target_waitstatus *status)
157 {
158 queue_stop_reply (ptid, status);
159
160 /* If this is the first stop reply in the queue, then inform GDB
161 about it, by sending a Stop notification. */
162 if (notif_queue->next == NULL)
163 {
164 char *p = own_buf;
165 strcpy (p, "Stop:");
166 p += strlen (p);
167 prepare_resume_reply (p,
168 notif_queue->ptid, &notif_queue->status);
169 putpkt_notif (own_buf);
170 }
171 }
172
173 /* Get rid of the currently pending stop replies. */
174
175 static void
176 discard_queued_stop_replies (void)
177 {
178 struct vstop_notif *next;
179
180 while (notif_queue)
181 {
182 next = notif_queue->next;
183 notif_queue = next;
184
185 free (next);
186 }
187 }
188
189 /* If there are more stop replies to push, push one now. */
190
191 static void
192 send_next_stop_reply (char *own_buf)
193 {
194 if (notif_queue)
195 prepare_resume_reply (own_buf,
196 notif_queue->ptid,
197 &notif_queue->status);
198 else
199 write_ok (own_buf);
200 }
201
202 static int
203 target_running (void)
204 {
205 return all_threads.head != NULL;
206 }
207
208 static int
209 start_inferior (char **argv)
210 {
211 char **new_argv = argv;
212 attached = 0;
213
214 if (wrapper_argv != NULL)
215 {
216 int i, count = 1;
217
218 for (i = 0; wrapper_argv[i] != NULL; i++)
219 count++;
220 for (i = 0; argv[i] != NULL; i++)
221 count++;
222 new_argv = alloca (sizeof (char *) * count);
223 count = 0;
224 for (i = 0; wrapper_argv[i] != NULL; i++)
225 new_argv[count++] = wrapper_argv[i];
226 for (i = 0; argv[i] != NULL; i++)
227 new_argv[count++] = argv[i];
228 new_argv[count] = NULL;
229 }
230
231 #ifdef SIGTTOU
232 signal (SIGTTOU, SIG_DFL);
233 signal (SIGTTIN, SIG_DFL);
234 #endif
235
236 signal_pid = create_inferior (new_argv[0], new_argv);
237
238 /* FIXME: we don't actually know at this point that the create
239 actually succeeded. We won't know that until we wait. */
240 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
241 signal_pid);
242 fflush (stderr);
243
244 #ifdef SIGTTOU
245 signal (SIGTTOU, SIG_IGN);
246 signal (SIGTTIN, SIG_IGN);
247 terminal_fd = fileno (stderr);
248 old_foreground_pgrp = tcgetpgrp (terminal_fd);
249 tcsetpgrp (terminal_fd, signal_pid);
250 atexit (restore_old_foreground_pgrp);
251 #endif
252
253 if (wrapper_argv != NULL)
254 {
255 struct thread_resume resume_info;
256 unsigned long ptid;
257
258 resume_info.thread = -1;
259 resume_info.kind = resume_continue;
260 resume_info.sig = 0;
261
262 ptid = mywait (&last_status, 0, 0);
263
264 if (last_status.kind != TARGET_WAITKIND_STOPPED)
265 return signal_pid;
266
267 do
268 {
269 (*the_target->resume) (&resume_info, 1);
270
271 mywait (&last_status, 0, 0);
272 if (last_status.kind != TARGET_WAITKIND_STOPPED)
273 return signal_pid;
274 }
275 while (last_status.value.sig != TARGET_SIGNAL_TRAP);
276
277 return signal_pid;
278 }
279
280 /* Wait till we are at 1st instruction in program, return new pid
281 (assuming success). */
282 last_ptid = mywait (&last_status, 0, 0);
283
284 return signal_pid;
285 }
286
287 static int
288 attach_inferior (int pid)
289 {
290 /* myattach should return -1 if attaching is unsupported,
291 0 if it succeeded, and call error() otherwise. */
292
293 if (myattach (pid) != 0)
294 return -1;
295
296 attached = 1;
297
298 fprintf (stderr, "Attached; pid = %d\n", pid);
299 fflush (stderr);
300
301 /* FIXME - It may be that we should get the SIGNAL_PID from the
302 attach function, so that it can be the main thread instead of
303 whichever we were told to attach to. */
304 signal_pid = pid;
305
306 if (!non_stop)
307 {
308 last_ptid = mywait (&last_status, 0, 0);
309
310 /* GDB knows to ignore the first SIGSTOP after attaching to a running
311 process using the "attach" command, but this is different; it's
312 just using "target remote". Pretend it's just starting up. */
313 if (last_status.kind == TARGET_WAITKIND_STOPPED
314 && last_status.value.sig == TARGET_SIGNAL_STOP)
315 last_status.value.sig = TARGET_SIGNAL_TRAP;
316 }
317
318 return 0;
319 }
320
321 extern int remote_debug;
322
323 /* Decode a qXfer read request. Return 0 if everything looks OK,
324 or -1 otherwise. */
325
326 static int
327 decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
328 {
329 /* Extract and NUL-terminate the annex. */
330 *annex = buf;
331 while (*buf && *buf != ':')
332 buf++;
333 if (*buf == '\0')
334 return -1;
335 *buf++ = 0;
336
337 /* After the read marker and annex, qXfer looks like a
338 traditional 'm' packet. */
339 decode_m_packet (buf, ofs, len);
340
341 return 0;
342 }
343
344 /* Write the response to a successful qXfer read. Returns the
345 length of the (binary) data stored in BUF, corresponding
346 to as much of DATA/LEN as we could fit. IS_MORE controls
347 the first character of the response. */
348 static int
349 write_qxfer_response (char *buf, const void *data, int len, int is_more)
350 {
351 int out_len;
352
353 if (is_more)
354 buf[0] = 'm';
355 else
356 buf[0] = 'l';
357
358 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
359 PBUFSIZ - 2) + 1;
360 }
361
362 /* Handle all of the extended 'Q' packets. */
363 void
364 handle_general_set (char *own_buf)
365 {
366 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
367 {
368 int numsigs = (int) TARGET_SIGNAL_LAST, i;
369 const char *p = own_buf + strlen ("QPassSignals:");
370 CORE_ADDR cursig;
371
372 p = decode_address_to_semicolon (&cursig, p);
373 for (i = 0; i < numsigs; i++)
374 {
375 if (i == cursig)
376 {
377 pass_signals[i] = 1;
378 if (*p == '\0')
379 /* Keep looping, to clear the remaining signals. */
380 cursig = -1;
381 else
382 p = decode_address_to_semicolon (&cursig, p);
383 }
384 else
385 pass_signals[i] = 0;
386 }
387 strcpy (own_buf, "OK");
388 return;
389 }
390
391 if (strcmp (own_buf, "QStartNoAckMode") == 0)
392 {
393 if (remote_debug)
394 {
395 fprintf (stderr, "[noack mode enabled]\n");
396 fflush (stderr);
397 }
398
399 noack_mode = 1;
400 write_ok (own_buf);
401 return;
402 }
403
404 if (strncmp (own_buf, "QNonStop:", 9) == 0)
405 {
406 char *mode = own_buf + 9;
407 int req = -1;
408 char *req_str;
409
410 if (strcmp (mode, "0") == 0)
411 req = 0;
412 else if (strcmp (mode, "1") == 0)
413 req = 1;
414 else
415 {
416 /* We don't know what this mode is, so complain to
417 GDB. */
418 fprintf (stderr, "Unknown non-stop mode requested: %s\n",
419 own_buf);
420 write_enn (own_buf);
421 return;
422 }
423
424 req_str = req ? "non-stop" : "all-stop";
425 if (start_non_stop (req) != 0)
426 {
427 fprintf (stderr, "Setting %s mode failed\n", req_str);
428 write_enn (own_buf);
429 return;
430 }
431
432 non_stop = req;
433
434 if (remote_debug)
435 fprintf (stderr, "[%s mode enabled]\n", req_str);
436
437 write_ok (own_buf);
438 return;
439 }
440
441 /* Otherwise we didn't know what packet it was. Say we didn't
442 understand it. */
443 own_buf[0] = 0;
444 }
445
446 static const char *
447 get_features_xml (const char *annex)
448 {
449 /* gdbserver_xmltarget defines what to return when looking
450 for the "target.xml" file. Its contents can either be
451 verbatim XML code (prefixed with a '@') or else the name
452 of the actual XML file to be used in place of "target.xml".
453
454 This variable is set up from the auto-generated
455 init_registers_... routine for the current target. */
456
457 if (gdbserver_xmltarget
458 && strcmp (annex, "target.xml") == 0)
459 {
460 if (*gdbserver_xmltarget == '@')
461 return gdbserver_xmltarget + 1;
462 else
463 annex = gdbserver_xmltarget;
464 }
465
466 #ifdef USE_XML
467 {
468 extern const char *const xml_builtin[][2];
469 int i;
470
471 /* Look for the annex. */
472 for (i = 0; xml_builtin[i][0] != NULL; i++)
473 if (strcmp (annex, xml_builtin[i][0]) == 0)
474 break;
475
476 if (xml_builtin[i][0] != NULL)
477 return xml_builtin[i][1];
478 }
479 #endif
480
481 return NULL;
482 }
483
484 void
485 monitor_show_help (void)
486 {
487 monitor_output ("The following monitor commands are supported:\n");
488 monitor_output (" set debug <0|1>\n");
489 monitor_output (" Enable general debugging messages\n");
490 monitor_output (" set remote-debug <0|1>\n");
491 monitor_output (" Enable remote protocol debugging messages\n");
492 monitor_output (" exit\n");
493 monitor_output (" Quit GDBserver\n");
494 }
495
496 /* Subroutine of handle_search_memory to simplify it. */
497
498 static int
499 handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len,
500 gdb_byte *pattern, unsigned pattern_len,
501 gdb_byte *search_buf,
502 unsigned chunk_size, unsigned search_buf_size,
503 CORE_ADDR *found_addrp)
504 {
505 /* Prime the search buffer. */
506
507 if (read_inferior_memory (start_addr, search_buf, search_buf_size) != 0)
508 {
509 warning ("Unable to access target memory at 0x%lx, halting search.",
510 (long) start_addr);
511 return -1;
512 }
513
514 /* Perform the search.
515
516 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
517 When we've scanned N bytes we copy the trailing bytes to the start and
518 read in another N bytes. */
519
520 while (search_space_len >= pattern_len)
521 {
522 gdb_byte *found_ptr;
523 unsigned nr_search_bytes = (search_space_len < search_buf_size
524 ? search_space_len
525 : search_buf_size);
526
527 found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len);
528
529 if (found_ptr != NULL)
530 {
531 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
532 *found_addrp = found_addr;
533 return 1;
534 }
535
536 /* Not found in this chunk, skip to next chunk. */
537
538 /* Don't let search_space_len wrap here, it's unsigned. */
539 if (search_space_len >= chunk_size)
540 search_space_len -= chunk_size;
541 else
542 search_space_len = 0;
543
544 if (search_space_len >= pattern_len)
545 {
546 unsigned keep_len = search_buf_size - chunk_size;
547 CORE_ADDR read_addr = start_addr + keep_len;
548 int nr_to_read;
549
550 /* Copy the trailing part of the previous iteration to the front
551 of the buffer for the next iteration. */
552 memcpy (search_buf, search_buf + chunk_size, keep_len);
553
554 nr_to_read = (search_space_len - keep_len < chunk_size
555 ? search_space_len - keep_len
556 : chunk_size);
557
558 if (read_inferior_memory (read_addr, search_buf + keep_len,
559 nr_to_read) != 0)
560 {
561 warning ("Unable to access target memory at 0x%lx, halting search.",
562 (long) read_addr);
563 return -1;
564 }
565
566 start_addr += chunk_size;
567 }
568 }
569
570 /* Not found. */
571
572 return 0;
573 }
574
575 /* Handle qSearch:memory packets. */
576
577 static void
578 handle_search_memory (char *own_buf, int packet_len)
579 {
580 CORE_ADDR start_addr;
581 CORE_ADDR search_space_len;
582 gdb_byte *pattern;
583 unsigned int pattern_len;
584 /* NOTE: also defined in find.c testcase. */
585 #define SEARCH_CHUNK_SIZE 16000
586 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
587 /* Buffer to hold memory contents for searching. */
588 gdb_byte *search_buf;
589 unsigned search_buf_size;
590 int found;
591 CORE_ADDR found_addr;
592 int cmd_name_len = sizeof ("qSearch:memory:") - 1;
593
594 pattern = malloc (packet_len);
595 if (pattern == NULL)
596 {
597 error ("Unable to allocate memory to perform the search");
598 strcpy (own_buf, "E00");
599 return;
600 }
601 if (decode_search_memory_packet (own_buf + cmd_name_len,
602 packet_len - cmd_name_len,
603 &start_addr, &search_space_len,
604 pattern, &pattern_len) < 0)
605 {
606 free (pattern);
607 error ("Error in parsing qSearch:memory packet");
608 strcpy (own_buf, "E00");
609 return;
610 }
611
612 search_buf_size = chunk_size + pattern_len - 1;
613
614 /* No point in trying to allocate a buffer larger than the search space. */
615 if (search_space_len < search_buf_size)
616 search_buf_size = search_space_len;
617
618 search_buf = malloc (search_buf_size);
619 if (search_buf == NULL)
620 {
621 free (pattern);
622 error ("Unable to allocate memory to perform the search");
623 strcpy (own_buf, "E00");
624 return;
625 }
626
627 found = handle_search_memory_1 (start_addr, search_space_len,
628 pattern, pattern_len,
629 search_buf, chunk_size, search_buf_size,
630 &found_addr);
631
632 if (found > 0)
633 sprintf (own_buf, "1,%lx", (long) found_addr);
634 else if (found == 0)
635 strcpy (own_buf, "0");
636 else
637 strcpy (own_buf, "E00");
638
639 free (search_buf);
640 free (pattern);
641 }
642
643 #define require_running(BUF) \
644 if (!target_running ()) \
645 { \
646 write_enn (BUF); \
647 return; \
648 }
649
650 /* Handle all of the extended 'q' packets. */
651 void
652 handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
653 {
654 static struct inferior_list_entry *thread_ptr;
655
656 /* Reply the current thread id. */
657 if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC)
658 {
659 unsigned long gdb_id;
660 require_running (own_buf);
661
662 if (general_thread != 0 && general_thread != -1)
663 gdb_id = general_thread;
664 else
665 {
666 thread_ptr = all_threads.head;
667 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
668 }
669
670 sprintf (own_buf, "QC%lx", gdb_id);
671 return;
672 }
673
674 if (strcmp ("qSymbol::", own_buf) == 0)
675 {
676 if (target_running () && the_target->look_up_symbols != NULL)
677 (*the_target->look_up_symbols) ();
678
679 strcpy (own_buf, "OK");
680 return;
681 }
682
683 if (!disable_packet_qfThreadInfo)
684 {
685 if (strcmp ("qfThreadInfo", own_buf) == 0)
686 {
687 require_running (own_buf);
688 thread_ptr = all_threads.head;
689 sprintf (own_buf, "m%x",
690 thread_to_gdb_id ((struct thread_info *)thread_ptr));
691 thread_ptr = thread_ptr->next;
692 return;
693 }
694
695 if (strcmp ("qsThreadInfo", own_buf) == 0)
696 {
697 require_running (own_buf);
698 if (thread_ptr != NULL)
699 {
700 sprintf (own_buf, "m%x",
701 thread_to_gdb_id ((struct thread_info *)thread_ptr));
702 thread_ptr = thread_ptr->next;
703 return;
704 }
705 else
706 {
707 sprintf (own_buf, "l");
708 return;
709 }
710 }
711 }
712
713 if (the_target->read_offsets != NULL
714 && strcmp ("qOffsets", own_buf) == 0)
715 {
716 CORE_ADDR text, data;
717
718 require_running (own_buf);
719 if (the_target->read_offsets (&text, &data))
720 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
721 (long)text, (long)data, (long)data);
722 else
723 write_enn (own_buf);
724
725 return;
726 }
727
728 if (the_target->qxfer_spu != NULL
729 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
730 {
731 char *annex;
732 int n;
733 unsigned int len;
734 CORE_ADDR ofs;
735 unsigned char *spu_buf;
736
737 require_running (own_buf);
738 strcpy (own_buf, "E00");
739 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
740 return;
741 if (len > PBUFSIZ - 2)
742 len = PBUFSIZ - 2;
743 spu_buf = malloc (len + 1);
744 if (!spu_buf)
745 return;
746
747 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
748 if (n < 0)
749 write_enn (own_buf);
750 else if (n > len)
751 *new_packet_len_p = write_qxfer_response (own_buf, spu_buf, len, 1);
752 else
753 *new_packet_len_p = write_qxfer_response (own_buf, spu_buf, n, 0);
754
755 free (spu_buf);
756 return;
757 }
758
759 if (the_target->qxfer_spu != NULL
760 && strncmp ("qXfer:spu:write:", own_buf, 16) == 0)
761 {
762 char *annex;
763 int n;
764 unsigned int len;
765 CORE_ADDR ofs;
766 unsigned char *spu_buf;
767
768 require_running (own_buf);
769 strcpy (own_buf, "E00");
770 spu_buf = malloc (packet_len - 15);
771 if (!spu_buf)
772 return;
773 if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
774 &ofs, &len, spu_buf) < 0)
775 {
776 free (spu_buf);
777 return;
778 }
779
780 n = (*the_target->qxfer_spu)
781 (annex, NULL, (unsigned const char *)spu_buf, ofs, len);
782 if (n < 0)
783 write_enn (own_buf);
784 else
785 sprintf (own_buf, "%x", n);
786
787 free (spu_buf);
788 return;
789 }
790
791 if (the_target->read_auxv != NULL
792 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
793 {
794 unsigned char *data;
795 int n;
796 CORE_ADDR ofs;
797 unsigned int len;
798 char *annex;
799
800 require_running (own_buf);
801
802 /* Reject any annex; grab the offset and length. */
803 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
804 || annex[0] != '\0')
805 {
806 strcpy (own_buf, "E00");
807 return;
808 }
809
810 /* Read one extra byte, as an indicator of whether there is
811 more. */
812 if (len > PBUFSIZ - 2)
813 len = PBUFSIZ - 2;
814 data = malloc (len + 1);
815 if (data == NULL)
816 {
817 write_enn (own_buf);
818 return;
819 }
820 n = (*the_target->read_auxv) (ofs, data, len + 1);
821 if (n < 0)
822 write_enn (own_buf);
823 else if (n > len)
824 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
825 else
826 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
827
828 free (data);
829
830 return;
831 }
832
833 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
834 {
835 CORE_ADDR ofs;
836 unsigned int len, total_len;
837 const char *document;
838 char *annex;
839
840 require_running (own_buf);
841
842 /* Grab the annex, offset, and length. */
843 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
844 {
845 strcpy (own_buf, "E00");
846 return;
847 }
848
849 /* Now grab the correct annex. */
850 document = get_features_xml (annex);
851 if (document == NULL)
852 {
853 strcpy (own_buf, "E00");
854 return;
855 }
856
857 total_len = strlen (document);
858 if (len > PBUFSIZ - 2)
859 len = PBUFSIZ - 2;
860
861 if (ofs > total_len)
862 write_enn (own_buf);
863 else if (len < total_len - ofs)
864 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
865 len, 1);
866 else
867 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
868 total_len - ofs, 0);
869
870 return;
871 }
872
873 if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
874 {
875 CORE_ADDR ofs;
876 unsigned int len, total_len;
877 char *document, *p;
878 struct inferior_list_entry *dll_ptr;
879 char *annex;
880
881 require_running (own_buf);
882
883 /* Reject any annex; grab the offset and length. */
884 if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
885 || annex[0] != '\0')
886 {
887 strcpy (own_buf, "E00");
888 return;
889 }
890
891 /* Over-estimate the necessary memory. Assume that every character
892 in the library name must be escaped. */
893 total_len = 64;
894 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
895 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
896
897 document = malloc (total_len);
898 if (document == NULL)
899 {
900 write_enn (own_buf);
901 return;
902 }
903 strcpy (document, "<library-list>\n");
904 p = document + strlen (document);
905
906 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
907 {
908 struct dll_info *dll = (struct dll_info *) dll_ptr;
909 char *name;
910
911 strcpy (p, " <library name=\"");
912 p = p + strlen (p);
913 name = xml_escape_text (dll->name);
914 strcpy (p, name);
915 free (name);
916 p = p + strlen (p);
917 strcpy (p, "\"><segment address=\"");
918 p = p + strlen (p);
919 sprintf (p, "0x%lx", (long) dll->base_addr);
920 p = p + strlen (p);
921 strcpy (p, "\"/></library>\n");
922 p = p + strlen (p);
923 }
924
925 strcpy (p, "</library-list>\n");
926
927 total_len = strlen (document);
928 if (len > PBUFSIZ - 2)
929 len = PBUFSIZ - 2;
930
931 if (ofs > total_len)
932 write_enn (own_buf);
933 else if (len < total_len - ofs)
934 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
935 len, 1);
936 else
937 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
938 total_len - ofs, 0);
939
940 free (document);
941 return;
942 }
943
944 if (the_target->qxfer_osdata != NULL
945 && strncmp ("qXfer:osdata:read:", own_buf, 18) == 0)
946 {
947 char *annex;
948 int n;
949 unsigned int len;
950 CORE_ADDR ofs;
951 unsigned char *workbuf;
952
953 strcpy (own_buf, "E00");
954 if (decode_xfer_read (own_buf + 18, &annex, &ofs, &len) < 0)
955 return;
956 if (len > PBUFSIZ - 2)
957 len = PBUFSIZ - 2;
958 workbuf = malloc (len + 1);
959 if (!workbuf)
960 return;
961
962 n = (*the_target->qxfer_osdata) (annex, workbuf, NULL, ofs, len + 1);
963 if (n < 0)
964 write_enn (own_buf);
965 else if (n > len)
966 *new_packet_len_p = write_qxfer_response (own_buf, workbuf, len, 1);
967 else
968 *new_packet_len_p = write_qxfer_response (own_buf, workbuf, n, 0);
969
970 free (workbuf);
971 return;
972 }
973
974 if (the_target->qxfer_siginfo != NULL
975 && strncmp ("qXfer:siginfo:read:", own_buf, 19) == 0)
976 {
977 unsigned char *data;
978 int n;
979 CORE_ADDR ofs;
980 unsigned int len;
981 char *annex;
982
983 require_running (own_buf);
984
985 /* Reject any annex; grab the offset and length. */
986 if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0
987 || annex[0] != '\0')
988 {
989 strcpy (own_buf, "E00");
990 return;
991 }
992
993 /* Read one extra byte, as an indicator of whether there is
994 more. */
995 if (len > PBUFSIZ - 2)
996 len = PBUFSIZ - 2;
997 data = malloc (len + 1);
998 if (!data)
999 return;
1000 n = (*the_target->qxfer_siginfo) (annex, data, NULL, ofs, len + 1);
1001 if (n < 0)
1002 write_enn (own_buf);
1003 else if (n > len)
1004 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1005 else
1006 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1007
1008 free (data);
1009 return;
1010 }
1011
1012 if (the_target->qxfer_siginfo != NULL
1013 && strncmp ("qXfer:siginfo:write:", own_buf, 20) == 0)
1014 {
1015 char *annex;
1016 int n;
1017 unsigned int len;
1018 CORE_ADDR ofs;
1019 unsigned char *data;
1020
1021 require_running (own_buf);
1022
1023 strcpy (own_buf, "E00");
1024 data = malloc (packet_len - 19);
1025 if (!data)
1026 return;
1027 if (decode_xfer_write (own_buf + 20, packet_len - 20, &annex,
1028 &ofs, &len, data) < 0)
1029 {
1030 free (data);
1031 return;
1032 }
1033
1034 n = (*the_target->qxfer_siginfo)
1035 (annex, NULL, (unsigned const char *)data, ofs, len);
1036 if (n < 0)
1037 write_enn (own_buf);
1038 else
1039 sprintf (own_buf, "%x", n);
1040
1041 free (data);
1042 return;
1043 }
1044
1045 /* Protocol features query. */
1046 if (strncmp ("qSupported", own_buf, 10) == 0
1047 && (own_buf[10] == ':' || own_buf[10] == '\0'))
1048 {
1049 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
1050
1051 /* We do not have any hook to indicate whether the target backend
1052 supports qXfer:libraries:read, so always report it. */
1053 strcat (own_buf, ";qXfer:libraries:read+");
1054
1055 if (the_target->read_auxv != NULL)
1056 strcat (own_buf, ";qXfer:auxv:read+");
1057
1058 if (the_target->qxfer_spu != NULL)
1059 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
1060
1061 if (the_target->qxfer_siginfo != NULL)
1062 strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1063
1064 /* We always report qXfer:features:read, as targets may
1065 install XML files on a subsequent call to arch_setup.
1066 If we reported to GDB on startup that we don't support
1067 qXfer:feature:read at all, we will never be re-queried. */
1068 strcat (own_buf, ";qXfer:features:read+");
1069
1070 if (transport_is_reliable)
1071 strcat (own_buf, ";QStartNoAckMode+");
1072
1073 if (the_target->qxfer_osdata != NULL)
1074 strcat (own_buf, ";qXfer:osdata:read+");
1075
1076 if (target_supports_non_stop ())
1077 strcat (own_buf, ";QNonStop+");
1078
1079 return;
1080 }
1081
1082 /* Thread-local storage support. */
1083 if (the_target->get_tls_address != NULL
1084 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
1085 {
1086 char *p = own_buf + 12;
1087 CORE_ADDR parts[2], address = 0;
1088 int i, err;
1089 unsigned long ptid = 0;
1090
1091 require_running (own_buf);
1092
1093 for (i = 0; i < 3; i++)
1094 {
1095 char *p2;
1096 int len;
1097
1098 if (p == NULL)
1099 break;
1100
1101 p2 = strchr (p, ',');
1102 if (p2)
1103 {
1104 len = p2 - p;
1105 p2++;
1106 }
1107 else
1108 {
1109 len = strlen (p);
1110 p2 = NULL;
1111 }
1112
1113 if (i == 0)
1114 ptid = strtoul (p, NULL, 16);
1115 else
1116 decode_address (&parts[i - 1], p, len);
1117 p = p2;
1118 }
1119
1120 if (p != NULL || i < 3)
1121 err = 1;
1122 else
1123 {
1124 struct thread_info *thread = gdb_id_to_thread (ptid);
1125
1126 if (thread == NULL)
1127 err = 2;
1128 else
1129 err = the_target->get_tls_address (thread, parts[0], parts[1],
1130 &address);
1131 }
1132
1133 if (err == 0)
1134 {
1135 sprintf (own_buf, "%llx", address);
1136 return;
1137 }
1138 else if (err > 0)
1139 {
1140 write_enn (own_buf);
1141 return;
1142 }
1143
1144 /* Otherwise, pretend we do not understand this packet. */
1145 }
1146
1147 /* Handle "monitor" commands. */
1148 if (strncmp ("qRcmd,", own_buf, 6) == 0)
1149 {
1150 char *mon = malloc (PBUFSIZ);
1151 int len = strlen (own_buf + 6);
1152
1153 if (mon == NULL)
1154 {
1155 write_enn (own_buf);
1156 return;
1157 }
1158
1159 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
1160 {
1161 write_enn (own_buf);
1162 free (mon);
1163 return;
1164 }
1165 mon[len / 2] = '\0';
1166
1167 write_ok (own_buf);
1168
1169 if (strcmp (mon, "set debug 1") == 0)
1170 {
1171 debug_threads = 1;
1172 monitor_output ("Debug output enabled.\n");
1173 }
1174 else if (strcmp (mon, "set debug 0") == 0)
1175 {
1176 debug_threads = 0;
1177 monitor_output ("Debug output disabled.\n");
1178 }
1179 else if (strcmp (mon, "set remote-debug 1") == 0)
1180 {
1181 remote_debug = 1;
1182 monitor_output ("Protocol debug output enabled.\n");
1183 }
1184 else if (strcmp (mon, "set remote-debug 0") == 0)
1185 {
1186 remote_debug = 0;
1187 monitor_output ("Protocol debug output disabled.\n");
1188 }
1189 else if (strcmp (mon, "help") == 0)
1190 monitor_show_help ();
1191 else if (strcmp (mon, "exit") == 0)
1192 exit_requested = 1;
1193 else
1194 {
1195 monitor_output ("Unknown monitor command.\n\n");
1196 monitor_show_help ();
1197 write_enn (own_buf);
1198 }
1199
1200 free (mon);
1201 return;
1202 }
1203
1204 if (strncmp ("qSearch:memory:", own_buf, sizeof ("qSearch:memory:") - 1) == 0)
1205 {
1206 require_running (own_buf);
1207 handle_search_memory (own_buf, packet_len);
1208 return;
1209 }
1210
1211 if (strcmp (own_buf, "qAttached") == 0)
1212 {
1213 require_running (own_buf);
1214 strcpy (own_buf, attached ? "1" : "0");
1215 return;
1216 }
1217
1218 /* Otherwise we didn't know what packet it was. Say we didn't
1219 understand it. */
1220 own_buf[0] = 0;
1221 }
1222
1223 /* Parse vCont packets. */
1224 void
1225 handle_v_cont (char *own_buf)
1226 {
1227 char *p, *q;
1228 int n = 0, i = 0;
1229 struct thread_resume *resume_info;
1230 struct thread_resume default_action = {0};
1231
1232 /* Count the number of semicolons in the packet. There should be one
1233 for every action. */
1234 p = &own_buf[5];
1235 while (p)
1236 {
1237 n++;
1238 p++;
1239 p = strchr (p, ';');
1240 }
1241
1242 resume_info = malloc (n * sizeof (resume_info[0]));
1243 if (resume_info == NULL)
1244 goto err;
1245
1246 p = &own_buf[5];
1247 while (*p)
1248 {
1249 p++;
1250
1251 if (p[0] == 's' || p[0] == 'S')
1252 resume_info[i].kind = resume_step;
1253 else if (p[0] == 'c' || p[0] == 'C')
1254 resume_info[i].kind = resume_continue;
1255 else if (p[0] == 't')
1256 resume_info[i].kind = resume_stop;
1257 else
1258 goto err;
1259
1260 if (p[0] == 'S' || p[0] == 'C')
1261 {
1262 int sig;
1263 sig = strtol (p + 1, &q, 16);
1264 if (p == q)
1265 goto err;
1266 p = q;
1267
1268 if (!target_signal_to_host_p (sig))
1269 goto err;
1270 resume_info[i].sig = target_signal_to_host (sig);
1271 }
1272 else
1273 {
1274 resume_info[i].sig = 0;
1275 p = p + 1;
1276 }
1277
1278 if (p[0] == 0)
1279 {
1280 resume_info[i].thread = -1;
1281 default_action = resume_info[i];
1282
1283 /* Note: we don't increment i here, we'll overwrite this entry
1284 the next time through. */
1285 }
1286 else if (p[0] == ':')
1287 {
1288 unsigned int gdb_id = strtoul (p + 1, &q, 16);
1289 unsigned long thread_id;
1290
1291 if (p == q)
1292 goto err;
1293 p = q;
1294 if (p[0] != ';' && p[0] != 0)
1295 goto err;
1296
1297 thread_id = gdb_id_to_thread_id (gdb_id);
1298 if (thread_id)
1299 resume_info[i].thread = thread_id;
1300 else
1301 goto err;
1302
1303 i++;
1304 }
1305 }
1306
1307 if (i < n)
1308 resume_info[i] = default_action;
1309
1310 /* Still used in occasional places in the backend. */
1311 if (n == 1
1312 && resume_info[0].thread != -1
1313 && resume_info[0].kind != resume_stop)
1314 cont_thread = resume_info[0].thread;
1315 else
1316 cont_thread = -1;
1317 set_desired_inferior (0);
1318
1319 if (!non_stop)
1320 enable_async_io ();
1321
1322 (*the_target->resume) (resume_info, n);
1323
1324 free (resume_info);
1325
1326 if (non_stop)
1327 write_ok (own_buf);
1328 else
1329 {
1330 last_ptid = mywait (&last_status, 0, 1);
1331 prepare_resume_reply (own_buf, last_ptid, &last_status);
1332 disable_async_io ();
1333 }
1334 return;
1335
1336 err:
1337 write_enn (own_buf);
1338 free (resume_info);
1339 return;
1340 }
1341
1342 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1343 int
1344 handle_v_attach (char *own_buf)
1345 {
1346 int pid;
1347
1348 pid = strtol (own_buf + 8, NULL, 16);
1349 if (pid != 0 && attach_inferior (pid) == 0)
1350 {
1351 /* Don't report shared library events after attaching, even if
1352 some libraries are preloaded. GDB will always poll the
1353 library list. Avoids the "stopped by shared library event"
1354 notice on the GDB side. */
1355 dlls_changed = 0;
1356
1357 if (non_stop)
1358 {
1359 /* In non-stop, we don't send a resume reply. Stop events
1360 will follow up using the normal notification
1361 mechanism. */
1362 write_ok (own_buf);
1363 }
1364 else
1365 prepare_resume_reply (own_buf, last_ptid, &last_status);
1366
1367 return 1;
1368 }
1369 else
1370 {
1371 write_enn (own_buf);
1372 return 0;
1373 }
1374 }
1375
1376 /* Run a new program. Return 1 if successful, 0 if failure. */
1377 static int
1378 handle_v_run (char *own_buf)
1379 {
1380 char *p, *next_p, **new_argv;
1381 int i, new_argc;
1382
1383 new_argc = 0;
1384 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
1385 {
1386 p++;
1387 new_argc++;
1388 }
1389
1390 new_argv = calloc (new_argc + 2, sizeof (char *));
1391 if (new_argv == NULL)
1392 {
1393 write_enn (own_buf);
1394 return 0;
1395 }
1396
1397 i = 0;
1398 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
1399 {
1400 next_p = strchr (p, ';');
1401 if (next_p == NULL)
1402 next_p = p + strlen (p);
1403
1404 if (i == 0 && p == next_p)
1405 new_argv[i] = NULL;
1406 else
1407 {
1408 /* FIXME: Fail request if out of memory instead of dying. */
1409 new_argv[i] = xmalloc (1 + (next_p - p) / 2);
1410 unhexify (new_argv[i], p, (next_p - p) / 2);
1411 new_argv[i][(next_p - p) / 2] = '\0';
1412 }
1413
1414 if (*next_p)
1415 next_p++;
1416 i++;
1417 }
1418 new_argv[i] = NULL;
1419
1420 if (new_argv[0] == NULL)
1421 {
1422 /* GDB didn't specify a program to run. Use the program from the
1423 last run with the new argument list. */
1424
1425 if (program_argv == NULL)
1426 {
1427 /* FIXME: new_argv memory leak */
1428 write_enn (own_buf);
1429 return 0;
1430 }
1431
1432 new_argv[0] = strdup (program_argv[0]);
1433 if (new_argv[0] == NULL)
1434 {
1435 /* FIXME: new_argv memory leak */
1436 write_enn (own_buf);
1437 return 0;
1438 }
1439 }
1440
1441 /* Free the old argv and install the new one. */
1442 freeargv (program_argv);
1443 program_argv = new_argv;
1444
1445 start_inferior (program_argv);
1446 if (last_status.kind == TARGET_WAITKIND_STOPPED)
1447 {
1448 prepare_resume_reply (own_buf, last_ptid, &last_status);
1449
1450 /* In non-stop, sending a resume reply doesn't set the general
1451 thread, but GDB assumes a vRun sets it (this is so GDB can
1452 query which is the main thread of the new inferior. */
1453 if (non_stop)
1454 general_thread = last_ptid;
1455
1456 return 1;
1457 }
1458 else
1459 {
1460 write_enn (own_buf);
1461 return 0;
1462 }
1463 }
1464
1465 /* Handle a 'vStopped' packet. */
1466 static void
1467 handle_v_stopped (char *own_buf)
1468 {
1469 /* If we're waiting for GDB to acknowledge a pending stop reply,
1470 consider that done. */
1471 if (notif_queue)
1472 {
1473 struct vstop_notif *head;
1474
1475 if (remote_debug)
1476 fprintf (stderr, "vStopped: acking %ld\n", notif_queue->ptid);
1477
1478 head = notif_queue;
1479 notif_queue = notif_queue->next;
1480 free (head);
1481 }
1482
1483 /* Push another stop reply, or if there are no more left, an OK. */
1484 send_next_stop_reply (own_buf);
1485 }
1486
1487 /* Handle all of the extended 'v' packets. */
1488 void
1489 handle_v_requests (char *own_buf, int packet_len, int *new_packet_len)
1490 {
1491 if (!disable_packet_vCont)
1492 {
1493 if (strncmp (own_buf, "vCont;", 6) == 0)
1494 {
1495 require_running (own_buf);
1496 handle_v_cont (own_buf);
1497 return;
1498 }
1499
1500 if (strncmp (own_buf, "vCont?", 6) == 0)
1501 {
1502 strcpy (own_buf, "vCont;c;C;s;S;t");
1503 return;
1504 }
1505 }
1506
1507 if (strncmp (own_buf, "vFile:", 6) == 0
1508 && handle_vFile (own_buf, packet_len, new_packet_len))
1509 return;
1510
1511 if (strncmp (own_buf, "vAttach;", 8) == 0)
1512 {
1513 if (target_running ())
1514 {
1515 fprintf (stderr, "Already debugging a process\n");
1516 write_enn (own_buf);
1517 return;
1518 }
1519 handle_v_attach (own_buf);
1520 return;
1521 }
1522
1523 if (strncmp (own_buf, "vRun;", 5) == 0)
1524 {
1525 if (target_running ())
1526 {
1527 fprintf (stderr, "Already debugging a process\n");
1528 write_enn (own_buf);
1529 return;
1530 }
1531 handle_v_run (own_buf);
1532 return;
1533 }
1534
1535 if (strncmp (own_buf, "vStopped", 8) == 0)
1536 {
1537 handle_v_stopped (own_buf);
1538 return;
1539 }
1540
1541 /* Otherwise we didn't know what packet it was. Say we didn't
1542 understand it. */
1543 own_buf[0] = 0;
1544 return;
1545 }
1546
1547 /* Resume inferior and wait for another event. In non-stop mode,
1548 don't really wait here, but return immediatelly to the event
1549 loop. */
1550 void
1551 myresume (char *own_buf, int step, int sig)
1552 {
1553 struct thread_resume resume_info[2];
1554 int n = 0;
1555 int valid_cont_thread;
1556
1557 set_desired_inferior (0);
1558
1559 valid_cont_thread = (cont_thread != 0 && cont_thread != -1);
1560
1561 if (step || sig || valid_cont_thread)
1562 {
1563 resume_info[0].thread
1564 = ((struct inferior_list_entry *) current_inferior)->id;
1565 if (step)
1566 resume_info[0].kind = resume_step;
1567 else
1568 resume_info[0].kind = resume_continue;
1569 resume_info[0].sig = sig;
1570 n++;
1571 }
1572
1573 if (!valid_cont_thread)
1574 {
1575 resume_info[n].thread = -1;
1576 resume_info[n].kind = resume_continue;
1577 resume_info[n].sig = 0;
1578 n++;
1579 }
1580
1581 if (!non_stop)
1582 enable_async_io ();
1583
1584 (*the_target->resume) (resume_info, n);
1585
1586 if (non_stop)
1587 write_ok (own_buf);
1588 else
1589 {
1590 last_ptid = mywait (&last_status, 0, 1);
1591 prepare_resume_reply (own_buf, last_ptid, &last_status);
1592 disable_async_io ();
1593 }
1594 }
1595
1596 /* Callback for for_each_inferior. Make a new stop reply for each
1597 stopped thread. */
1598
1599 static void
1600 queue_stop_reply_callback (struct inferior_list_entry *entry)
1601 {
1602 struct target_waitstatus status;
1603
1604 status.kind = TARGET_WAITKIND_STOPPED;
1605 status.value.sig = TARGET_SIGNAL_TRAP;
1606
1607 /* Pass the last stop reply back to GDB, but don't notify. */
1608 queue_stop_reply (entry->id, &status);
1609 }
1610
1611 /* Status handler for the '?' packet. */
1612
1613 static void
1614 handle_status (char *own_buf)
1615 {
1616 struct target_waitstatus status;
1617 status.kind = TARGET_WAITKIND_STOPPED;
1618 status.value.sig = TARGET_SIGNAL_TRAP;
1619
1620 /* In non-stop mode, we must send a stop reply for each stopped
1621 thread. In all-stop mode, just send one for the first stopped
1622 thread we find. */
1623
1624 if (non_stop)
1625 {
1626 discard_queued_stop_replies ();
1627 for_each_inferior (&all_threads, queue_stop_reply_callback);
1628
1629 /* The first is sent immediatly. OK is sent if there is no
1630 stopped thread, which is the same handling of the vStopped
1631 packet (by design). */
1632 send_next_stop_reply (own_buf);
1633 }
1634 else
1635 {
1636 if (all_threads.head)
1637 prepare_resume_reply (own_buf,
1638 all_threads.head->id, &status);
1639 else
1640 strcpy (own_buf, "W00");
1641 }
1642 }
1643
1644 static void
1645 gdbserver_version (void)
1646 {
1647 printf ("GNU gdbserver %s%s\n"
1648 "Copyright (C) 2009 Free Software Foundation, Inc.\n"
1649 "gdbserver is free software, covered by the GNU General Public License.\n"
1650 "This gdbserver was configured as \"%s\"\n",
1651 PKGVERSION, version, host_name);
1652 }
1653
1654 static void
1655 gdbserver_usage (FILE *stream)
1656 {
1657 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
1658 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1659 "\tgdbserver [OPTIONS] --multi COMM\n"
1660 "\n"
1661 "COMM may either be a tty device (for serial debugging), or \n"
1662 "HOST:PORT to listen for a TCP connection.\n"
1663 "\n"
1664 "Options:\n"
1665 " --debug Enable general debugging output.\n"
1666 " --remote-debug Enable remote protocol debugging output.\n"
1667 " --version Display version information and exit.\n"
1668 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
1669 if (REPORT_BUGS_TO[0] && stream == stdout)
1670 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
1671 }
1672
1673 static void
1674 gdbserver_show_disableable (FILE *stream)
1675 {
1676 fprintf (stream, "Disableable packets:\n"
1677 " vCont \tAll vCont packets\n"
1678 " qC \tQuerying the current thread\n"
1679 " qfThreadInfo\tThread listing\n"
1680 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
1681 " threads \tAll of the above\n");
1682 }
1683
1684
1685 #undef require_running
1686 #define require_running(BUF) \
1687 if (!target_running ()) \
1688 { \
1689 write_enn (BUF); \
1690 break; \
1691 }
1692
1693 int
1694 main (int argc, char *argv[])
1695 {
1696 int bad_attach;
1697 int pid;
1698 char *arg_end, *port;
1699 char **next_arg = &argv[1];
1700 int multi_mode = 0;
1701 int attach = 0;
1702 int was_running;
1703
1704 while (*next_arg != NULL && **next_arg == '-')
1705 {
1706 if (strcmp (*next_arg, "--version") == 0)
1707 {
1708 gdbserver_version ();
1709 exit (0);
1710 }
1711 else if (strcmp (*next_arg, "--help") == 0)
1712 {
1713 gdbserver_usage (stdout);
1714 exit (0);
1715 }
1716 else if (strcmp (*next_arg, "--attach") == 0)
1717 attach = 1;
1718 else if (strcmp (*next_arg, "--multi") == 0)
1719 multi_mode = 1;
1720 else if (strcmp (*next_arg, "--wrapper") == 0)
1721 {
1722 next_arg++;
1723
1724 wrapper_argv = next_arg;
1725 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
1726 next_arg++;
1727
1728 if (next_arg == wrapper_argv || *next_arg == NULL)
1729 {
1730 gdbserver_usage (stderr);
1731 exit (1);
1732 }
1733
1734 /* Consume the "--". */
1735 *next_arg = NULL;
1736 }
1737 else if (strcmp (*next_arg, "--debug") == 0)
1738 debug_threads = 1;
1739 else if (strcmp (*next_arg, "--remote-debug") == 0)
1740 remote_debug = 1;
1741 else if (strcmp (*next_arg, "--disable-packet") == 0)
1742 {
1743 gdbserver_show_disableable (stdout);
1744 exit (0);
1745 }
1746 else if (strncmp (*next_arg,
1747 "--disable-packet=",
1748 sizeof ("--disable-packet=") - 1) == 0)
1749 {
1750 char *packets, *tok;
1751
1752 packets = *next_arg += sizeof ("--disable-packet=") - 1;
1753 for (tok = strtok (packets, ",");
1754 tok != NULL;
1755 tok = strtok (NULL, ","))
1756 {
1757 if (strcmp ("vCont", tok) == 0)
1758 disable_packet_vCont = 1;
1759 else if (strcmp ("Tthread", tok) == 0)
1760 disable_packet_Tthread = 1;
1761 else if (strcmp ("qC", tok) == 0)
1762 disable_packet_qC = 1;
1763 else if (strcmp ("qfThreadInfo", tok) == 0)
1764 disable_packet_qfThreadInfo = 1;
1765 else if (strcmp ("threads", tok) == 0)
1766 {
1767 disable_packet_vCont = 1;
1768 disable_packet_Tthread = 1;
1769 disable_packet_qC = 1;
1770 disable_packet_qfThreadInfo = 1;
1771 }
1772 else
1773 {
1774 fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
1775 tok);
1776 gdbserver_show_disableable (stderr);
1777 exit (1);
1778 }
1779 }
1780 }
1781 else
1782 {
1783 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
1784 exit (1);
1785 }
1786
1787 next_arg++;
1788 continue;
1789 }
1790
1791 if (setjmp (toplevel))
1792 {
1793 fprintf (stderr, "Exiting\n");
1794 exit (1);
1795 }
1796
1797 port = *next_arg;
1798 next_arg++;
1799 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
1800 {
1801 gdbserver_usage (stderr);
1802 exit (1);
1803 }
1804
1805 bad_attach = 0;
1806 pid = 0;
1807
1808 /* --attach used to come after PORT, so allow it there for
1809 compatibility. */
1810 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
1811 {
1812 attach = 1;
1813 next_arg++;
1814 }
1815
1816 if (attach
1817 && (*next_arg == NULL
1818 || (*next_arg)[0] == '\0'
1819 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
1820 || *arg_end != '\0'
1821 || next_arg[1] != NULL))
1822 bad_attach = 1;
1823
1824 if (bad_attach)
1825 {
1826 gdbserver_usage (stderr);
1827 exit (1);
1828 }
1829
1830 initialize_async_io ();
1831 initialize_low ();
1832
1833 own_buf = xmalloc (PBUFSIZ + 1);
1834 mem_buf = xmalloc (PBUFSIZ);
1835
1836 if (pid == 0 && *next_arg != NULL)
1837 {
1838 int i, n;
1839
1840 n = argc - (next_arg - argv);
1841 program_argv = xmalloc (sizeof (char *) * (n + 1));
1842 for (i = 0; i < n; i++)
1843 program_argv[i] = xstrdup (next_arg[i]);
1844 program_argv[i] = NULL;
1845
1846 /* Wait till we are at first instruction in program. */
1847 start_inferior (program_argv);
1848
1849 /* We are now (hopefully) stopped at the first instruction of
1850 the target process. This assumes that the target process was
1851 successfully created. */
1852 }
1853 else if (pid != 0)
1854 {
1855 if (attach_inferior (pid) == -1)
1856 error ("Attaching not supported on this target");
1857
1858 /* Otherwise succeeded. */
1859 }
1860 else
1861 {
1862 last_status.kind = TARGET_WAITKIND_EXITED;
1863 last_status.value.integer = 0;
1864 last_ptid = -1;
1865 }
1866
1867 /* Don't report shared library events on the initial connection,
1868 even if some libraries are preloaded. Avoids the "stopped by
1869 shared library event" notice on gdb side. */
1870 dlls_changed = 0;
1871
1872 if (setjmp (toplevel))
1873 {
1874 fprintf (stderr, "Killing inferior\n");
1875 kill_inferior ();
1876 exit (1);
1877 }
1878
1879 if (last_status.kind == TARGET_WAITKIND_EXITED
1880 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
1881 was_running = 0;
1882 else
1883 was_running = 1;
1884
1885 if (!was_running && !multi_mode)
1886 {
1887 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
1888 exit (1);
1889 }
1890
1891 while (1)
1892 {
1893 noack_mode = 0;
1894 non_stop = 0;
1895
1896 remote_open (port);
1897
1898 if (setjmp (toplevel) != 0)
1899 {
1900 /* An error occurred. */
1901 if (response_needed)
1902 {
1903 write_enn (own_buf);
1904 putpkt (own_buf);
1905 }
1906 }
1907
1908 /* Wait for events. This will return when all event sources are
1909 removed from the event loop. */
1910 start_event_loop ();
1911
1912 /* If an exit was requested (using the "monitor exit" command),
1913 terminate now. The only other way to get here is for
1914 getpkt to fail; close the connection and reopen it at the
1915 top of the loop. */
1916
1917 if (exit_requested)
1918 {
1919 if (attached)
1920 detach_inferior ();
1921 else
1922 kill_inferior ();
1923 exit (0);
1924 }
1925 else
1926 fprintf (stderr, "Remote side has terminated connection. "
1927 "GDBserver will reopen the connection.\n");
1928 }
1929 }
1930
1931 /* Event loop callback that handles a serial event. The first byte in
1932 the serial buffer gets us here. We expect characters to arrive at
1933 a brisk pace, so we read the rest of the packet with a blocking
1934 getpkt call. */
1935
1936 static void
1937 process_serial_event (void)
1938 {
1939 char ch;
1940 int i = 0;
1941 int signal;
1942 unsigned int len;
1943 CORE_ADDR mem_addr;
1944 unsigned char sig;
1945 int packet_len;
1946 int new_packet_len = -1;
1947
1948 /* Used to decide when gdbserver should exit in
1949 multi-mode/remote. */
1950 static int have_ran = 0;
1951
1952 if (!have_ran)
1953 have_ran = target_running ();
1954
1955 disable_async_io ();
1956
1957 response_needed = 0;
1958 packet_len = getpkt (own_buf);
1959 if (packet_len <= 0)
1960 {
1961 target_async (0);
1962 remote_close ();
1963 return;
1964 }
1965 response_needed = 1;
1966
1967 i = 0;
1968 ch = own_buf[i++];
1969 switch (ch)
1970 {
1971 case 'q':
1972 handle_query (own_buf, packet_len, &new_packet_len);
1973 break;
1974 case 'Q':
1975 handle_general_set (own_buf);
1976 break;
1977 case 'D':
1978 require_running (own_buf);
1979 fprintf (stderr, "Detaching from inferior\n");
1980 if (detach_inferior () != 0)
1981 write_enn (own_buf);
1982 else
1983 {
1984 discard_queued_stop_replies ();
1985 write_ok (own_buf);
1986
1987 if (extended_protocol)
1988 {
1989 /* Treat this like a normal program exit. */
1990 last_status.kind = TARGET_WAITKIND_EXITED;
1991 last_status.value.integer = 0;
1992 last_ptid = signal_pid;
1993
1994 current_inferior = NULL;
1995 }
1996 else
1997 {
1998 putpkt (own_buf);
1999 remote_close ();
2000
2001 /* If we are attached, then we can exit. Otherwise, we
2002 need to hang around doing nothing, until the child is
2003 gone. */
2004 if (!attached)
2005 join_inferior ();
2006
2007 exit (0);
2008 }
2009 }
2010 break;
2011 case '!':
2012 extended_protocol = 1;
2013 write_ok (own_buf);
2014 break;
2015 case '?':
2016 handle_status (own_buf);
2017 break;
2018 case 'H':
2019 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
2020 {
2021 unsigned long gdb_id, thread_id;
2022
2023 require_running (own_buf);
2024 gdb_id = strtoul (&own_buf[2], NULL, 16);
2025 if (gdb_id == 0 || gdb_id == -1)
2026 thread_id = gdb_id;
2027 else
2028 {
2029 thread_id = gdb_id_to_thread_id (gdb_id);
2030 if (thread_id == 0)
2031 {
2032 write_enn (own_buf);
2033 break;
2034 }
2035 }
2036
2037 if (own_buf[1] == 'g')
2038 {
2039 if (thread_id == 0)
2040 {
2041 /* GDB is telling us to choose any thread. Check if
2042 the currently selected thread is still valid. If
2043 it is not, select the first available. */
2044 struct thread_info *thread =
2045 (struct thread_info *) find_inferior_id (&all_threads,
2046 general_thread);
2047 if (thread == NULL)
2048 thread_id = all_threads.head->id;
2049 }
2050
2051 general_thread = thread_id;
2052 set_desired_inferior (1);
2053 }
2054 else if (own_buf[1] == 'c')
2055 cont_thread = thread_id;
2056 else if (own_buf[1] == 's')
2057 step_thread = thread_id;
2058
2059 write_ok (own_buf);
2060 }
2061 else
2062 {
2063 /* Silently ignore it so that gdb can extend the protocol
2064 without compatibility headaches. */
2065 own_buf[0] = '\0';
2066 }
2067 break;
2068 case 'g':
2069 require_running (own_buf);
2070 set_desired_inferior (1);
2071 registers_to_string (own_buf);
2072 break;
2073 case 'G':
2074 require_running (own_buf);
2075 set_desired_inferior (1);
2076 registers_from_string (&own_buf[1]);
2077 write_ok (own_buf);
2078 break;
2079 case 'm':
2080 require_running (own_buf);
2081 decode_m_packet (&own_buf[1], &mem_addr, &len);
2082 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
2083 convert_int_to_ascii (mem_buf, own_buf, len);
2084 else
2085 write_enn (own_buf);
2086 break;
2087 case 'M':
2088 require_running (own_buf);
2089 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
2090 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
2091 write_ok (own_buf);
2092 else
2093 write_enn (own_buf);
2094 break;
2095 case 'X':
2096 require_running (own_buf);
2097 if (decode_X_packet (&own_buf[1], packet_len - 1,
2098 &mem_addr, &len, mem_buf) < 0
2099 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
2100 write_enn (own_buf);
2101 else
2102 write_ok (own_buf);
2103 break;
2104 case 'C':
2105 require_running (own_buf);
2106 convert_ascii_to_int (own_buf + 1, &sig, 1);
2107 if (target_signal_to_host_p (sig))
2108 signal = target_signal_to_host (sig);
2109 else
2110 signal = 0;
2111 myresume (own_buf, 0, signal);
2112 break;
2113 case 'S':
2114 require_running (own_buf);
2115 convert_ascii_to_int (own_buf + 1, &sig, 1);
2116 if (target_signal_to_host_p (sig))
2117 signal = target_signal_to_host (sig);
2118 else
2119 signal = 0;
2120 myresume (own_buf, 1, signal);
2121 break;
2122 case 'c':
2123 require_running (own_buf);
2124 signal = 0;
2125 myresume (own_buf, 0, signal);
2126 break;
2127 case 's':
2128 require_running (own_buf);
2129 signal = 0;
2130 myresume (own_buf, 1, signal);
2131 break;
2132 case 'Z':
2133 {
2134 char *lenptr;
2135 char *dataptr;
2136 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
2137 int len = strtol (lenptr + 1, &dataptr, 16);
2138 char type = own_buf[1];
2139
2140 if (the_target->insert_watchpoint == NULL
2141 || (type < '2' || type > '4'))
2142 {
2143 /* No watchpoint support or not a watchpoint command;
2144 unrecognized either way. */
2145 own_buf[0] = '\0';
2146 }
2147 else
2148 {
2149 int res;
2150
2151 require_running (own_buf);
2152 res = (*the_target->insert_watchpoint) (type, addr, len);
2153 if (res == 0)
2154 write_ok (own_buf);
2155 else if (res == 1)
2156 /* Unsupported. */
2157 own_buf[0] = '\0';
2158 else
2159 write_enn (own_buf);
2160 }
2161 break;
2162 }
2163 case 'z':
2164 {
2165 char *lenptr;
2166 char *dataptr;
2167 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
2168 int len = strtol (lenptr + 1, &dataptr, 16);
2169 char type = own_buf[1];
2170
2171 if (the_target->remove_watchpoint == NULL
2172 || (type < '2' || type > '4'))
2173 {
2174 /* No watchpoint support or not a watchpoint command;
2175 unrecognized either way. */
2176 own_buf[0] = '\0';
2177 }
2178 else
2179 {
2180 int res;
2181
2182 require_running (own_buf);
2183 res = (*the_target->remove_watchpoint) (type, addr, len);
2184 if (res == 0)
2185 write_ok (own_buf);
2186 else if (res == 1)
2187 /* Unsupported. */
2188 own_buf[0] = '\0';
2189 else
2190 write_enn (own_buf);
2191 }
2192 break;
2193 }
2194 case 'k':
2195 response_needed = 0;
2196 if (!target_running ())
2197 /* The packet we received doesn't make sense - but we
2198 can't reply to it, either. */
2199 return;
2200
2201 fprintf (stderr, "Killing inferior\n");
2202 kill_inferior ();
2203 discard_queued_stop_replies ();
2204
2205 /* When using the extended protocol, we wait with no program
2206 running. The traditional protocol will exit instead. */
2207 if (extended_protocol)
2208 {
2209 last_status.kind = TARGET_WAITKIND_EXITED;
2210 last_status.value.sig = TARGET_SIGNAL_KILL;
2211 return;
2212 }
2213 else
2214 {
2215 exit (0);
2216 break;
2217 }
2218 case 'T':
2219 {
2220 unsigned long gdb_id, thread_id;
2221
2222 require_running (own_buf);
2223 gdb_id = strtoul (&own_buf[1], NULL, 16);
2224 thread_id = gdb_id_to_thread_id (gdb_id);
2225 if (thread_id == 0)
2226 {
2227 write_enn (own_buf);
2228 break;
2229 }
2230
2231 if (mythread_alive (thread_id))
2232 write_ok (own_buf);
2233 else
2234 write_enn (own_buf);
2235 }
2236 break;
2237 case 'R':
2238 response_needed = 0;
2239
2240 /* Restarting the inferior is only supported in the extended
2241 protocol. */
2242 if (extended_protocol)
2243 {
2244 if (target_running ())
2245 {
2246 kill_inferior ();
2247 discard_queued_stop_replies ();
2248 }
2249 fprintf (stderr, "GDBserver restarting\n");
2250
2251 /* Wait till we are at 1st instruction in prog. */
2252 if (program_argv != NULL)
2253 start_inferior (program_argv);
2254 else
2255 {
2256 last_status.kind = TARGET_WAITKIND_EXITED;
2257 last_status.value.sig = TARGET_SIGNAL_KILL;
2258 }
2259 return;
2260 }
2261 else
2262 {
2263 /* It is a request we don't understand. Respond with an
2264 empty packet so that gdb knows that we don't support this
2265 request. */
2266 own_buf[0] = '\0';
2267 break;
2268 }
2269 case 'v':
2270 /* Extended (long) request. */
2271 handle_v_requests (own_buf, packet_len, &new_packet_len);
2272 break;
2273
2274 default:
2275 /* It is a request we don't understand. Respond with an empty
2276 packet so that gdb knows that we don't support this
2277 request. */
2278 own_buf[0] = '\0';
2279 break;
2280 }
2281
2282 if (new_packet_len != -1)
2283 putpkt_binary (own_buf, new_packet_len);
2284 else
2285 putpkt (own_buf);
2286
2287 response_needed = 0;
2288
2289 if (!extended_protocol && have_ran && !target_running ())
2290 {
2291 /* In non-stop, defer exiting until GDB had a chance to query
2292 the whole vStopped list (until it gets an OK). */
2293 if (!notif_queue)
2294 {
2295 fprintf (stderr, "GDBserver exiting\n");
2296 remote_close ();
2297 exit (0);
2298 }
2299 }
2300 }
2301
2302 /* Event-loop callback for serial events. */
2303
2304 void
2305 handle_serial_event (int err, gdb_client_data client_data)
2306 {
2307 if (debug_threads)
2308 fprintf (stderr, "handling possible serial event\n");
2309
2310 /* Really handle it. */
2311 process_serial_event ();
2312
2313 /* Be sure to not change the selected inferior behind GDB's back.
2314 Important in the non-stop mode asynchronous protocol. */
2315 set_desired_inferior (1);
2316 }
2317
2318 /* Event-loop callback for target events. */
2319
2320 void
2321 handle_target_event (int err, gdb_client_data client_data)
2322 {
2323 if (debug_threads)
2324 fprintf (stderr, "handling possible target event\n");
2325
2326 last_ptid = mywait (&last_status, TARGET_WNOHANG, 1);
2327
2328 if (last_status.kind != TARGET_WAITKIND_IGNORE)
2329 {
2330 /* Something interesting. Tell GDB about it. */
2331 push_event (last_ptid, &last_status);
2332 }
2333
2334 /* Be sure to not change the selected inferior behind GDB's back.
2335 Important in the non-stop mode asynchronous protocol. */
2336 set_desired_inferior (1);
2337 }
This page took 0.133696 seconds and 5 git commands to generate.