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