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