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