gdb/gdbserver/
[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 unsigned long thread_from_wait;
39 unsigned long old_thread_from_wait;
40 int server_waiting;
41
42 static int extended_protocol;
43 static int attached;
44 static int response_needed;
45 static int exit_requested;
46
47 static char **program_argv, **wrapper_argv;
48
49 /* Enable miscellaneous debugging output. The name is historical - it
50 was originally used to debug LinuxThreads support. */
51 int debug_threads;
52
53 int pass_signals[TARGET_SIGNAL_LAST];
54
55 jmp_buf toplevel;
56
57 const 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
64 unsigned long signal_pid;
65
66 #ifdef SIGTTOU
67 /* A file descriptor for the controlling terminal. */
68 int terminal_fd;
69
70 /* TERMINAL_FD's original foreground group. */
71 pid_t old_foreground_pgrp;
72
73 /* Hand back terminal ownership to the original foreground group. */
74
75 static void
76 restore_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. */
85 int disable_packet_vCont;
86 int disable_packet_Tthread;
87 int disable_packet_qC;
88 int disable_packet_qfThreadInfo;
89
90 static int
91 target_running (void)
92 {
93 return all_threads.head != NULL;
94 }
95
96 static int
97 start_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
173 static int
174 attach_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
203 extern int remote_debug;
204
205 /* Decode a qXfer read request. Return 0 if everything looks OK,
206 or -1 otherwise. */
207
208 static int
209 decode_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. */
230 static int
231 write_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. */
245 void
246 handle_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
291 static const char *
292 get_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
329 void
330 monitor_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
343 static int
344 handle_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
422 static void
423 handle_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. */
496 void
497 handle_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", thread_to_gdb_id ((struct thread_info *)thread_ptr));
527 thread_ptr = thread_ptr->next;
528 return;
529 }
530
531 if (strcmp ("qsThreadInfo", own_buf) == 0)
532 {
533 require_running (own_buf);
534 if (thread_ptr != NULL)
535 {
536 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
537 thread_ptr = thread_ptr->next;
538 return;
539 }
540 else
541 {
542 sprintf (own_buf, "l");
543 return;
544 }
545 }
546 }
547
548 if (the_target->read_offsets != NULL
549 && strcmp ("qOffsets", own_buf) == 0)
550 {
551 CORE_ADDR text, data;
552
553 require_running (own_buf);
554 if (the_target->read_offsets (&text, &data))
555 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
556 (long)text, (long)data, (long)data);
557 else
558 write_enn (own_buf);
559
560 return;
561 }
562
563 if (the_target->qxfer_spu != NULL
564 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
565 {
566 char *annex;
567 int n;
568 unsigned int len;
569 CORE_ADDR ofs;
570 unsigned char *spu_buf;
571
572 require_running (own_buf);
573 strcpy (own_buf, "E00");
574 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
575 return;
576 if (len > PBUFSIZ - 2)
577 len = PBUFSIZ - 2;
578 spu_buf = malloc (len + 1);
579 if (!spu_buf)
580 return;
581
582 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
583 if (n < 0)
584 write_enn (own_buf);
585 else if (n > len)
586 *new_packet_len_p = write_qxfer_response
587 (own_buf, spu_buf, len, 1);
588 else
589 *new_packet_len_p = write_qxfer_response
590 (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
804 (own_buf, workbuf, len, 1);
805 else
806 *new_packet_len_p = write_qxfer_response
807 (own_buf, workbuf, n, 0);
808
809 free (workbuf);
810 return;
811 }
812
813 /* Protocol features query. */
814 if (strncmp ("qSupported", own_buf, 10) == 0
815 && (own_buf[10] == ':' || own_buf[10] == '\0'))
816 {
817 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
818
819 /* We do not have any hook to indicate whether the target backend
820 supports qXfer:libraries:read, so always report it. */
821 strcat (own_buf, ";qXfer:libraries:read+");
822
823 if (the_target->read_auxv != NULL)
824 strcat (own_buf, ";qXfer:auxv:read+");
825
826 if (the_target->qxfer_spu != NULL)
827 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
828
829 /* We always report qXfer:features:read, as targets may
830 install XML files on a subsequent call to arch_setup.
831 If we reported to GDB on startup that we don't support
832 qXfer:feature:read at all, we will never be re-queried. */
833 strcat (own_buf, ";qXfer:features:read+");
834
835 if (transport_is_reliable)
836 strcat (own_buf, ";QStartNoAckMode+");
837
838 if (the_target->qxfer_osdata != NULL)
839 strcat (own_buf, ";qXfer:osdata:read+");
840
841 return;
842 }
843
844 /* Thread-local storage support. */
845 if (the_target->get_tls_address != NULL
846 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
847 {
848 char *p = own_buf + 12;
849 CORE_ADDR parts[3], address = 0;
850 int i, err;
851
852 require_running (own_buf);
853
854 for (i = 0; i < 3; i++)
855 {
856 char *p2;
857 int len;
858
859 if (p == NULL)
860 break;
861
862 p2 = strchr (p, ',');
863 if (p2)
864 {
865 len = p2 - p;
866 p2++;
867 }
868 else
869 {
870 len = strlen (p);
871 p2 = NULL;
872 }
873
874 decode_address (&parts[i], p, len);
875 p = p2;
876 }
877
878 if (p != NULL || i < 3)
879 err = 1;
880 else
881 {
882 struct thread_info *thread = gdb_id_to_thread (parts[0]);
883
884 if (thread == NULL)
885 err = 2;
886 else
887 err = the_target->get_tls_address (thread, parts[1], parts[2],
888 &address);
889 }
890
891 if (err == 0)
892 {
893 sprintf (own_buf, "%llx", address);
894 return;
895 }
896 else if (err > 0)
897 {
898 write_enn (own_buf);
899 return;
900 }
901
902 /* Otherwise, pretend we do not understand this packet. */
903 }
904
905 /* Handle "monitor" commands. */
906 if (strncmp ("qRcmd,", own_buf, 6) == 0)
907 {
908 char *mon = malloc (PBUFSIZ);
909 int len = strlen (own_buf + 6);
910
911 if (mon == NULL)
912 {
913 write_enn (own_buf);
914 return;
915 }
916
917 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
918 {
919 write_enn (own_buf);
920 free (mon);
921 return;
922 }
923 mon[len / 2] = '\0';
924
925 write_ok (own_buf);
926
927 if (strcmp (mon, "set debug 1") == 0)
928 {
929 debug_threads = 1;
930 monitor_output ("Debug output enabled.\n");
931 }
932 else if (strcmp (mon, "set debug 0") == 0)
933 {
934 debug_threads = 0;
935 monitor_output ("Debug output disabled.\n");
936 }
937 else if (strcmp (mon, "set remote-debug 1") == 0)
938 {
939 remote_debug = 1;
940 monitor_output ("Protocol debug output enabled.\n");
941 }
942 else if (strcmp (mon, "set remote-debug 0") == 0)
943 {
944 remote_debug = 0;
945 monitor_output ("Protocol debug output disabled.\n");
946 }
947 else if (strcmp (mon, "help") == 0)
948 monitor_show_help ();
949 else if (strcmp (mon, "exit") == 0)
950 exit_requested = 1;
951 else
952 {
953 monitor_output ("Unknown monitor command.\n\n");
954 monitor_show_help ();
955 write_enn (own_buf);
956 }
957
958 free (mon);
959 return;
960 }
961
962 if (strncmp ("qSearch:memory:", own_buf, sizeof ("qSearch:memory:") - 1) == 0)
963 {
964 require_running (own_buf);
965 handle_search_memory (own_buf, packet_len);
966 return;
967 }
968
969 /* Otherwise we didn't know what packet it was. Say we didn't
970 understand it. */
971 own_buf[0] = 0;
972 }
973
974 /* Parse vCont packets. */
975 void
976 handle_v_cont (char *own_buf, char *status, int *signal)
977 {
978 char *p, *q;
979 int n = 0, i = 0;
980 struct thread_resume *resume_info, default_action;
981
982 /* Count the number of semicolons in the packet. There should be one
983 for every action. */
984 p = &own_buf[5];
985 while (p)
986 {
987 n++;
988 p++;
989 p = strchr (p, ';');
990 }
991 /* Allocate room for one extra action, for the default remain-stopped
992 behavior; if no default action is in the list, we'll need the extra
993 slot. */
994 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
995 if (resume_info == NULL)
996 goto err;
997
998 default_action.thread = -1;
999 default_action.leave_stopped = 1;
1000 default_action.step = 0;
1001 default_action.sig = 0;
1002
1003 p = &own_buf[5];
1004 i = 0;
1005 while (*p)
1006 {
1007 p++;
1008
1009 resume_info[i].leave_stopped = 0;
1010
1011 if (p[0] == 's' || p[0] == 'S')
1012 resume_info[i].step = 1;
1013 else if (p[0] == 'c' || p[0] == 'C')
1014 resume_info[i].step = 0;
1015 else
1016 goto err;
1017
1018 if (p[0] == 'S' || p[0] == 'C')
1019 {
1020 int sig;
1021 sig = strtol (p + 1, &q, 16);
1022 if (p == q)
1023 goto err;
1024 p = q;
1025
1026 if (!target_signal_to_host_p (sig))
1027 goto err;
1028 resume_info[i].sig = target_signal_to_host (sig);
1029 }
1030 else
1031 {
1032 resume_info[i].sig = 0;
1033 p = p + 1;
1034 }
1035
1036 if (p[0] == 0)
1037 {
1038 resume_info[i].thread = -1;
1039 default_action = resume_info[i];
1040
1041 /* Note: we don't increment i here, we'll overwrite this entry
1042 the next time through. */
1043 }
1044 else if (p[0] == ':')
1045 {
1046 unsigned int gdb_id = strtoul (p + 1, &q, 16);
1047 unsigned long thread_id;
1048
1049 if (p == q)
1050 goto err;
1051 p = q;
1052 if (p[0] != ';' && p[0] != 0)
1053 goto err;
1054
1055 thread_id = gdb_id_to_thread_id (gdb_id);
1056 if (thread_id)
1057 resume_info[i].thread = thread_id;
1058 else
1059 goto err;
1060
1061 i++;
1062 }
1063 }
1064
1065 resume_info[i] = default_action;
1066
1067 /* Still used in occasional places in the backend. */
1068 if (n == 1 && resume_info[0].thread != -1)
1069 cont_thread = resume_info[0].thread;
1070 else
1071 cont_thread = -1;
1072 set_desired_inferior (0);
1073
1074 enable_async_io ();
1075 (*the_target->resume) (resume_info);
1076
1077 free (resume_info);
1078
1079 *signal = mywait (status, 1);
1080 prepare_resume_reply (own_buf, *status, *signal);
1081 disable_async_io ();
1082 return;
1083
1084 err:
1085 write_enn (own_buf);
1086 free (resume_info);
1087 return;
1088 }
1089
1090 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1091 int
1092 handle_v_attach (char *own_buf, char *status, int *signal)
1093 {
1094 int pid;
1095
1096 pid = strtol (own_buf + 8, NULL, 16);
1097 if (pid != 0 && attach_inferior (pid, status, signal) == 0)
1098 {
1099 /* Don't report shared library events after attaching, even if
1100 some libraries are preloaded. GDB will always poll the
1101 library list. Avoids the "stopped by shared library event"
1102 notice on the GDB side. */
1103 dlls_changed = 0;
1104 prepare_resume_reply (own_buf, *status, *signal);
1105 return 1;
1106 }
1107 else
1108 {
1109 write_enn (own_buf);
1110 return 0;
1111 }
1112 }
1113
1114 /* Run a new program. Return 1 if successful, 0 if failure. */
1115 static int
1116 handle_v_run (char *own_buf, char *status, int *signal)
1117 {
1118 char *p, *next_p, **new_argv;
1119 int i, new_argc;
1120
1121 new_argc = 0;
1122 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
1123 {
1124 p++;
1125 new_argc++;
1126 }
1127
1128 new_argv = calloc (new_argc + 2, sizeof (char *));
1129 if (new_argv == NULL)
1130 {
1131 write_enn (own_buf);
1132 return 0;
1133 }
1134
1135 i = 0;
1136 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
1137 {
1138 next_p = strchr (p, ';');
1139 if (next_p == NULL)
1140 next_p = p + strlen (p);
1141
1142 if (i == 0 && p == next_p)
1143 new_argv[i] = NULL;
1144 else
1145 {
1146 /* FIXME: Fail request if out of memory instead of dying. */
1147 new_argv[i] = xmalloc (1 + (next_p - p) / 2);
1148 unhexify (new_argv[i], p, (next_p - p) / 2);
1149 new_argv[i][(next_p - p) / 2] = '\0';
1150 }
1151
1152 if (*next_p)
1153 next_p++;
1154 i++;
1155 }
1156 new_argv[i] = NULL;
1157
1158 if (new_argv[0] == NULL)
1159 {
1160 /* GDB didn't specify a program to run. Use the program from the
1161 last run with the new argument list. */
1162
1163 if (program_argv == NULL)
1164 {
1165 /* FIXME: new_argv memory leak */
1166 write_enn (own_buf);
1167 return 0;
1168 }
1169
1170 new_argv[0] = strdup (program_argv[0]);
1171 if (new_argv[0] == NULL)
1172 {
1173 /* FIXME: new_argv memory leak */
1174 write_enn (own_buf);
1175 return 0;
1176 }
1177 }
1178
1179 /* Free the old argv and install the new one. */
1180 freeargv (program_argv);
1181 program_argv = new_argv;
1182
1183 *signal = start_inferior (program_argv, status);
1184 if (*status == 'T')
1185 {
1186 prepare_resume_reply (own_buf, *status, *signal);
1187 return 1;
1188 }
1189 else
1190 {
1191 write_enn (own_buf);
1192 return 0;
1193 }
1194 }
1195
1196 /* Handle all of the extended 'v' packets. */
1197 void
1198 handle_v_requests (char *own_buf, char *status, int *signal,
1199 int packet_len, int *new_packet_len)
1200 {
1201 if (!disable_packet_vCont)
1202 {
1203 if (strncmp (own_buf, "vCont;", 6) == 0)
1204 {
1205 require_running (own_buf);
1206 handle_v_cont (own_buf, status, signal);
1207 return;
1208 }
1209
1210 if (strncmp (own_buf, "vCont?", 6) == 0)
1211 {
1212 strcpy (own_buf, "vCont;c;C;s;S");
1213 return;
1214 }
1215 }
1216
1217 if (strncmp (own_buf, "vFile:", 6) == 0
1218 && handle_vFile (own_buf, packet_len, new_packet_len))
1219 return;
1220
1221 if (strncmp (own_buf, "vAttach;", 8) == 0)
1222 {
1223 if (target_running ())
1224 {
1225 fprintf (stderr, "Already debugging a process\n");
1226 write_enn (own_buf);
1227 return;
1228 }
1229 handle_v_attach (own_buf, status, signal);
1230 return;
1231 }
1232
1233 if (strncmp (own_buf, "vRun;", 5) == 0)
1234 {
1235 if (target_running ())
1236 {
1237 fprintf (stderr, "Already debugging a process\n");
1238 write_enn (own_buf);
1239 return;
1240 }
1241 handle_v_run (own_buf, status, signal);
1242 return;
1243 }
1244
1245 /* Otherwise we didn't know what packet it was. Say we didn't
1246 understand it. */
1247 own_buf[0] = 0;
1248 return;
1249 }
1250
1251 void
1252 myresume (char *own_buf, int step, int *signalp, char *statusp)
1253 {
1254 struct thread_resume resume_info[2];
1255 int n = 0;
1256 int sig = *signalp;
1257
1258 set_desired_inferior (0);
1259
1260 if (step || sig || (cont_thread != 0 && cont_thread != -1))
1261 {
1262 resume_info[0].thread
1263 = ((struct inferior_list_entry *) current_inferior)->id;
1264 resume_info[0].step = step;
1265 resume_info[0].sig = sig;
1266 resume_info[0].leave_stopped = 0;
1267 n++;
1268 }
1269 resume_info[n].thread = -1;
1270 resume_info[n].step = 0;
1271 resume_info[n].sig = 0;
1272 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
1273
1274 enable_async_io ();
1275 (*the_target->resume) (resume_info);
1276 *signalp = mywait (statusp, 1);
1277 prepare_resume_reply (own_buf, *statusp, *signalp);
1278 disable_async_io ();
1279 }
1280
1281 static void
1282 gdbserver_version (void)
1283 {
1284 printf ("GNU gdbserver %s%s\n"
1285 "Copyright (C) 2009 Free Software Foundation, Inc.\n"
1286 "gdbserver is free software, covered by the GNU General Public License.\n"
1287 "This gdbserver was configured as \"%s\"\n",
1288 PKGVERSION, version, host_name);
1289 }
1290
1291 static void
1292 gdbserver_usage (FILE *stream)
1293 {
1294 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
1295 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1296 "\tgdbserver [OPTIONS] --multi COMM\n"
1297 "\n"
1298 "COMM may either be a tty device (for serial debugging), or \n"
1299 "HOST:PORT to listen for a TCP connection.\n"
1300 "\n"
1301 "Options:\n"
1302 " --debug Enable general debugging output.\n"
1303 " --remote-debug Enable remote protocol debugging output.\n"
1304 " --version Display version information and exit.\n"
1305 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
1306 if (REPORT_BUGS_TO[0] && stream == stdout)
1307 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
1308 }
1309
1310 static void
1311 gdbserver_show_disableable (FILE *stream)
1312 {
1313 fprintf (stream, "Disableable packets:\n"
1314 " vCont \tAll vCont packets\n"
1315 " qC \tQuerying the current thread\n"
1316 " qfThreadInfo\tThread listing\n"
1317 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
1318 " threads \tAll of the above\n");
1319 }
1320
1321
1322 #undef require_running
1323 #define require_running(BUF) \
1324 if (!target_running ()) \
1325 { \
1326 write_enn (BUF); \
1327 break; \
1328 }
1329
1330 int
1331 main (int argc, char *argv[])
1332 {
1333 char ch, status, *own_buf;
1334 unsigned char *mem_buf;
1335 int i = 0;
1336 int signal;
1337 unsigned int len;
1338 CORE_ADDR mem_addr;
1339 int bad_attach;
1340 int pid;
1341 char *arg_end, *port;
1342 char **next_arg = &argv[1];
1343 int multi_mode = 0;
1344 int attach = 0;
1345 int was_running;
1346
1347 while (*next_arg != NULL && **next_arg == '-')
1348 {
1349 if (strcmp (*next_arg, "--version") == 0)
1350 {
1351 gdbserver_version ();
1352 exit (0);
1353 }
1354 else if (strcmp (*next_arg, "--help") == 0)
1355 {
1356 gdbserver_usage (stdout);
1357 exit (0);
1358 }
1359 else if (strcmp (*next_arg, "--attach") == 0)
1360 attach = 1;
1361 else if (strcmp (*next_arg, "--multi") == 0)
1362 multi_mode = 1;
1363 else if (strcmp (*next_arg, "--wrapper") == 0)
1364 {
1365 next_arg++;
1366
1367 wrapper_argv = next_arg;
1368 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
1369 next_arg++;
1370
1371 if (next_arg == wrapper_argv || *next_arg == NULL)
1372 {
1373 gdbserver_usage (stderr);
1374 exit (1);
1375 }
1376
1377 /* Consume the "--". */
1378 *next_arg = NULL;
1379 }
1380 else if (strcmp (*next_arg, "--debug") == 0)
1381 debug_threads = 1;
1382 else if (strcmp (*next_arg, "--remote-debug") == 0)
1383 remote_debug = 1;
1384 else if (strcmp (*next_arg, "--disable-packet") == 0)
1385 {
1386 gdbserver_show_disableable (stdout);
1387 exit (0);
1388 }
1389 else if (strncmp (*next_arg,
1390 "--disable-packet=",
1391 sizeof ("--disable-packet=") - 1) == 0)
1392 {
1393 char *packets, *tok;
1394
1395 packets = *next_arg += sizeof ("--disable-packet=") - 1;
1396 for (tok = strtok (packets, ",");
1397 tok != NULL;
1398 tok = strtok (NULL, ","))
1399 {
1400 if (strcmp ("vCont", tok) == 0)
1401 disable_packet_vCont = 1;
1402 else if (strcmp ("Tthread", tok) == 0)
1403 disable_packet_Tthread = 1;
1404 else if (strcmp ("qC", tok) == 0)
1405 disable_packet_qC = 1;
1406 else if (strcmp ("qfThreadInfo", tok) == 0)
1407 disable_packet_qfThreadInfo = 1;
1408 else if (strcmp ("threads", tok) == 0)
1409 {
1410 disable_packet_vCont = 1;
1411 disable_packet_Tthread = 1;
1412 disable_packet_qC = 1;
1413 disable_packet_qfThreadInfo = 1;
1414 }
1415 else
1416 {
1417 fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
1418 tok);
1419 gdbserver_show_disableable (stderr);
1420 exit (1);
1421 }
1422 }
1423 }
1424 else
1425 {
1426 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
1427 exit (1);
1428 }
1429
1430 next_arg++;
1431 continue;
1432 }
1433
1434 if (setjmp (toplevel))
1435 {
1436 fprintf (stderr, "Exiting\n");
1437 exit (1);
1438 }
1439
1440 port = *next_arg;
1441 next_arg++;
1442 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
1443 {
1444 gdbserver_usage (stderr);
1445 exit (1);
1446 }
1447
1448 bad_attach = 0;
1449 pid = 0;
1450
1451 /* --attach used to come after PORT, so allow it there for
1452 compatibility. */
1453 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
1454 {
1455 attach = 1;
1456 next_arg++;
1457 }
1458
1459 if (attach
1460 && (*next_arg == NULL
1461 || (*next_arg)[0] == '\0'
1462 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
1463 || *arg_end != '\0'
1464 || next_arg[1] != NULL))
1465 bad_attach = 1;
1466
1467 if (bad_attach)
1468 {
1469 gdbserver_usage (stderr);
1470 exit (1);
1471 }
1472
1473 initialize_async_io ();
1474 initialize_low ();
1475
1476 own_buf = xmalloc (PBUFSIZ + 1);
1477 mem_buf = xmalloc (PBUFSIZ);
1478
1479 if (pid == 0 && *next_arg != NULL)
1480 {
1481 int i, n;
1482
1483 n = argc - (next_arg - argv);
1484 program_argv = xmalloc (sizeof (char *) * (n + 1));
1485 for (i = 0; i < n; i++)
1486 program_argv[i] = xstrdup (next_arg[i]);
1487 program_argv[i] = NULL;
1488
1489 /* Wait till we are at first instruction in program. */
1490 signal = start_inferior (program_argv, &status);
1491
1492 /* We are now (hopefully) stopped at the first instruction of
1493 the target process. This assumes that the target process was
1494 successfully created. */
1495 }
1496 else if (pid != 0)
1497 {
1498 if (attach_inferior (pid, &status, &signal) == -1)
1499 error ("Attaching not supported on this target");
1500
1501 /* Otherwise succeeded. */
1502 }
1503 else
1504 {
1505 status = 'W';
1506 signal = 0;
1507 }
1508
1509 /* Don't report shared library events on the initial connection,
1510 even if some libraries are preloaded. Avoids the "stopped by
1511 shared library event" notice on gdb side. */
1512 dlls_changed = 0;
1513
1514 if (setjmp (toplevel))
1515 {
1516 fprintf (stderr, "Killing inferior\n");
1517 kill_inferior ();
1518 exit (1);
1519 }
1520
1521 if (status == 'W' || status == 'X')
1522 was_running = 0;
1523 else
1524 was_running = 1;
1525
1526 if (!was_running && !multi_mode)
1527 {
1528 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
1529 exit (1);
1530 }
1531
1532 while (1)
1533 {
1534 noack_mode = 0;
1535 remote_open (port);
1536
1537 restart:
1538 if (setjmp (toplevel) != 0)
1539 {
1540 /* An error occurred. */
1541 if (response_needed)
1542 {
1543 write_enn (own_buf);
1544 putpkt (own_buf);
1545 }
1546 }
1547
1548 disable_async_io ();
1549 while (!exit_requested)
1550 {
1551 unsigned char sig;
1552 int packet_len;
1553 int new_packet_len = -1;
1554
1555 response_needed = 0;
1556 packet_len = getpkt (own_buf);
1557 if (packet_len <= 0)
1558 break;
1559 response_needed = 1;
1560
1561 i = 0;
1562 ch = own_buf[i++];
1563 switch (ch)
1564 {
1565 case 'q':
1566 handle_query (own_buf, packet_len, &new_packet_len);
1567 break;
1568 case 'Q':
1569 handle_general_set (own_buf);
1570 break;
1571 case 'D':
1572 require_running (own_buf);
1573 fprintf (stderr, "Detaching from inferior\n");
1574 if (detach_inferior () != 0)
1575 write_enn (own_buf);
1576 else
1577 {
1578 write_ok (own_buf);
1579
1580 if (extended_protocol)
1581 {
1582 /* Treat this like a normal program exit. */
1583 signal = 0;
1584 status = 'W';
1585 }
1586 else
1587 {
1588 putpkt (own_buf);
1589 remote_close ();
1590
1591 /* If we are attached, then we can exit. Otherwise, we
1592 need to hang around doing nothing, until the child
1593 is gone. */
1594 if (!attached)
1595 join_inferior ();
1596
1597 exit (0);
1598 }
1599 }
1600 break;
1601 case '!':
1602 extended_protocol = 1;
1603 write_ok (own_buf);
1604 break;
1605 case '?':
1606 prepare_resume_reply (own_buf, status, signal);
1607 break;
1608 case 'H':
1609 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
1610 {
1611 unsigned long gdb_id, thread_id;
1612
1613 require_running (own_buf);
1614 gdb_id = strtoul (&own_buf[2], NULL, 16);
1615 if (gdb_id == 0 || gdb_id == -1)
1616 thread_id = gdb_id;
1617 else
1618 {
1619 thread_id = gdb_id_to_thread_id (gdb_id);
1620 if (thread_id == 0)
1621 {
1622 write_enn (own_buf);
1623 break;
1624 }
1625 }
1626
1627 if (own_buf[1] == 'g')
1628 {
1629 general_thread = thread_id;
1630 set_desired_inferior (1);
1631 }
1632 else if (own_buf[1] == 'c')
1633 cont_thread = thread_id;
1634 else if (own_buf[1] == 's')
1635 step_thread = thread_id;
1636
1637 write_ok (own_buf);
1638 }
1639 else
1640 {
1641 /* Silently ignore it so that gdb can extend the protocol
1642 without compatibility headaches. */
1643 own_buf[0] = '\0';
1644 }
1645 break;
1646 case 'g':
1647 require_running (own_buf);
1648 set_desired_inferior (1);
1649 registers_to_string (own_buf);
1650 break;
1651 case 'G':
1652 require_running (own_buf);
1653 set_desired_inferior (1);
1654 registers_from_string (&own_buf[1]);
1655 write_ok (own_buf);
1656 break;
1657 case 'm':
1658 require_running (own_buf);
1659 decode_m_packet (&own_buf[1], &mem_addr, &len);
1660 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
1661 convert_int_to_ascii (mem_buf, own_buf, len);
1662 else
1663 write_enn (own_buf);
1664 break;
1665 case 'M':
1666 require_running (own_buf);
1667 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
1668 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
1669 write_ok (own_buf);
1670 else
1671 write_enn (own_buf);
1672 break;
1673 case 'X':
1674 require_running (own_buf);
1675 if (decode_X_packet (&own_buf[1], packet_len - 1,
1676 &mem_addr, &len, mem_buf) < 0
1677 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
1678 write_enn (own_buf);
1679 else
1680 write_ok (own_buf);
1681 break;
1682 case 'C':
1683 require_running (own_buf);
1684 convert_ascii_to_int (own_buf + 1, &sig, 1);
1685 if (target_signal_to_host_p (sig))
1686 signal = target_signal_to_host (sig);
1687 else
1688 signal = 0;
1689 myresume (own_buf, 0, &signal, &status);
1690 break;
1691 case 'S':
1692 require_running (own_buf);
1693 convert_ascii_to_int (own_buf + 1, &sig, 1);
1694 if (target_signal_to_host_p (sig))
1695 signal = target_signal_to_host (sig);
1696 else
1697 signal = 0;
1698 myresume (own_buf, 1, &signal, &status);
1699 break;
1700 case 'c':
1701 require_running (own_buf);
1702 signal = 0;
1703 myresume (own_buf, 0, &signal, &status);
1704 break;
1705 case 's':
1706 require_running (own_buf);
1707 signal = 0;
1708 myresume (own_buf, 1, &signal, &status);
1709 break;
1710 case 'Z':
1711 {
1712 char *lenptr;
1713 char *dataptr;
1714 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1715 int len = strtol (lenptr + 1, &dataptr, 16);
1716 char type = own_buf[1];
1717
1718 if (the_target->insert_watchpoint == NULL
1719 || (type < '2' || type > '4'))
1720 {
1721 /* No watchpoint support or not a watchpoint command;
1722 unrecognized either way. */
1723 own_buf[0] = '\0';
1724 }
1725 else
1726 {
1727 int res;
1728
1729 require_running (own_buf);
1730 res = (*the_target->insert_watchpoint) (type, addr, len);
1731 if (res == 0)
1732 write_ok (own_buf);
1733 else if (res == 1)
1734 /* Unsupported. */
1735 own_buf[0] = '\0';
1736 else
1737 write_enn (own_buf);
1738 }
1739 break;
1740 }
1741 case 'z':
1742 {
1743 char *lenptr;
1744 char *dataptr;
1745 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1746 int len = strtol (lenptr + 1, &dataptr, 16);
1747 char type = own_buf[1];
1748
1749 if (the_target->remove_watchpoint == NULL
1750 || (type < '2' || type > '4'))
1751 {
1752 /* No watchpoint support or not a watchpoint command;
1753 unrecognized either way. */
1754 own_buf[0] = '\0';
1755 }
1756 else
1757 {
1758 int res;
1759
1760 require_running (own_buf);
1761 res = (*the_target->remove_watchpoint) (type, addr, len);
1762 if (res == 0)
1763 write_ok (own_buf);
1764 else if (res == 1)
1765 /* Unsupported. */
1766 own_buf[0] = '\0';
1767 else
1768 write_enn (own_buf);
1769 }
1770 break;
1771 }
1772 case 'k':
1773 response_needed = 0;
1774 if (!target_running ())
1775 /* The packet we received doesn't make sense - but we
1776 can't reply to it, either. */
1777 goto restart;
1778
1779 fprintf (stderr, "Killing inferior\n");
1780 kill_inferior ();
1781
1782 /* When using the extended protocol, we wait with no
1783 program running. The traditional protocol will exit
1784 instead. */
1785 if (extended_protocol)
1786 {
1787 status = 'X';
1788 signal = TARGET_SIGNAL_KILL;
1789 was_running = 0;
1790 goto restart;
1791 }
1792 else
1793 {
1794 exit (0);
1795 break;
1796 }
1797 case 'T':
1798 {
1799 unsigned long gdb_id, thread_id;
1800
1801 require_running (own_buf);
1802 gdb_id = strtoul (&own_buf[1], NULL, 16);
1803 thread_id = gdb_id_to_thread_id (gdb_id);
1804 if (thread_id == 0)
1805 {
1806 write_enn (own_buf);
1807 break;
1808 }
1809
1810 if (mythread_alive (thread_id))
1811 write_ok (own_buf);
1812 else
1813 write_enn (own_buf);
1814 }
1815 break;
1816 case 'R':
1817 response_needed = 0;
1818
1819 /* Restarting the inferior is only supported in the
1820 extended protocol. */
1821 if (extended_protocol)
1822 {
1823 if (target_running ())
1824 kill_inferior ();
1825 fprintf (stderr, "GDBserver restarting\n");
1826
1827 /* Wait till we are at 1st instruction in prog. */
1828 if (program_argv != NULL)
1829 signal = start_inferior (program_argv, &status);
1830 else
1831 {
1832 status = 'X';
1833 signal = TARGET_SIGNAL_KILL;
1834 }
1835 goto restart;
1836 }
1837 else
1838 {
1839 /* It is a request we don't understand. Respond with an
1840 empty packet so that gdb knows that we don't support this
1841 request. */
1842 own_buf[0] = '\0';
1843 break;
1844 }
1845 case 'v':
1846 /* Extended (long) request. */
1847 handle_v_requests (own_buf, &status, &signal,
1848 packet_len, &new_packet_len);
1849 break;
1850
1851 default:
1852 /* It is a request we don't understand. Respond with an
1853 empty packet so that gdb knows that we don't support this
1854 request. */
1855 own_buf[0] = '\0';
1856 break;
1857 }
1858
1859 if (new_packet_len != -1)
1860 putpkt_binary (own_buf, new_packet_len);
1861 else
1862 putpkt (own_buf);
1863
1864 response_needed = 0;
1865
1866 if (was_running && (status == 'W' || status == 'X'))
1867 {
1868 was_running = 0;
1869
1870 if (status == 'W')
1871 fprintf (stderr,
1872 "\nChild exited with status %d\n", signal);
1873 if (status == 'X')
1874 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
1875 target_signal_to_host (signal),
1876 target_signal_to_name (signal));
1877
1878 if (extended_protocol)
1879 goto restart;
1880 else
1881 {
1882 fprintf (stderr, "GDBserver exiting\n");
1883 exit (0);
1884 }
1885 }
1886
1887 if (status != 'W' && status != 'X')
1888 was_running = 1;
1889 }
1890
1891 /* If an exit was requested (using the "monitor exit" command),
1892 terminate now. The only other way to get here is for
1893 getpkt to fail; close the connection and reopen it at the
1894 top of the loop. */
1895
1896 if (exit_requested)
1897 {
1898 remote_close ();
1899 if (attached && target_running ())
1900 detach_inferior ();
1901 else if (target_running ())
1902 kill_inferior ();
1903 exit (0);
1904 }
1905 else
1906 {
1907 fprintf (stderr, "Remote side has terminated connection. "
1908 "GDBserver will reopen the connection.\n");
1909 remote_close ();
1910 }
1911 }
1912 }
This page took 0.098123 seconds and 5 git commands to generate.