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