2012-02-24 Luis Machado <lgustavo@codesourcery>
[binutils-gdb.git] / gdb / gdbserver / server.c
1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993-1995, 1997-2000, 2002-2012 Free Software
3 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 ptid_t cont_thread;
33 ptid_t general_thread;
34
35 int server_waiting;
36
37 static int extended_protocol;
38 static int response_needed;
39 static int exit_requested;
40
41 /* --once: Exit after the first connection has closed. */
42 int run_once;
43
44 int multi_process;
45 int non_stop;
46
47 /* Whether we should attempt to disable the operating system's address
48 space randomization feature before starting an inferior. */
49 int disable_randomization = 1;
50
51 static char **program_argv, **wrapper_argv;
52
53 /* Enable miscellaneous debugging output. The name is historical - it
54 was originally used to debug LinuxThreads support. */
55 int debug_threads;
56
57 /* Enable debugging of h/w breakpoint/watchpoint support. */
58 int debug_hw_points;
59
60 int pass_signals[TARGET_SIGNAL_LAST];
61
62 jmp_buf toplevel;
63
64 const char *gdbserver_xmltarget;
65
66 /* The PID of the originally created or attached inferior. Used to
67 send signals to the process when GDB sends us an asynchronous interrupt
68 (user hitting Control-C in the client), and to wait for the child to exit
69 when no longer debugging it. */
70
71 unsigned long signal_pid;
72
73 #ifdef SIGTTOU
74 /* A file descriptor for the controlling terminal. */
75 int terminal_fd;
76
77 /* TERMINAL_FD's original foreground group. */
78 pid_t old_foreground_pgrp;
79
80 /* Hand back terminal ownership to the original foreground group. */
81
82 static void
83 restore_old_foreground_pgrp (void)
84 {
85 tcsetpgrp (terminal_fd, old_foreground_pgrp);
86 }
87 #endif
88
89 /* Set if you want to disable optional thread related packets support
90 in gdbserver, for the sake of testing GDB against stubs that don't
91 support them. */
92 int disable_packet_vCont;
93 int disable_packet_Tthread;
94 int disable_packet_qC;
95 int disable_packet_qfThreadInfo;
96
97 /* Last status reported to GDB. */
98 static struct target_waitstatus last_status;
99 static ptid_t last_ptid;
100
101 static char *own_buf;
102 static unsigned char *mem_buf;
103
104 /* Structure holding information relative to a single stop reply. We
105 keep a queue of these (really a singly-linked list) to push to GDB
106 in non-stop mode. */
107 struct vstop_notif
108 {
109 /* Pointer to next in list. */
110 struct vstop_notif *next;
111
112 /* Thread or process that got the event. */
113 ptid_t ptid;
114
115 /* Event info. */
116 struct target_waitstatus status;
117 };
118
119 /* The pending stop replies list head. */
120 static struct vstop_notif *notif_queue = NULL;
121
122 /* Put a stop reply to the stop reply queue. */
123
124 static void
125 queue_stop_reply (ptid_t ptid, struct target_waitstatus *status)
126 {
127 struct vstop_notif *new_notif;
128
129 new_notif = xmalloc (sizeof (*new_notif));
130 new_notif->next = NULL;
131 new_notif->ptid = ptid;
132 new_notif->status = *status;
133
134 if (notif_queue)
135 {
136 struct vstop_notif *tail;
137 for (tail = notif_queue;
138 tail && tail->next;
139 tail = tail->next)
140 ;
141 tail->next = new_notif;
142 }
143 else
144 notif_queue = new_notif;
145
146 if (remote_debug)
147 {
148 int i = 0;
149 struct vstop_notif *n;
150
151 for (n = notif_queue; n; n = n->next)
152 i++;
153
154 fprintf (stderr, "pending stop replies: %d\n", i);
155 }
156 }
157
158 /* Place an event in the stop reply queue, and push a notification if
159 we aren't sending one yet. */
160
161 void
162 push_event (ptid_t ptid, struct target_waitstatus *status)
163 {
164 gdb_assert (status->kind != TARGET_WAITKIND_IGNORE);
165
166 queue_stop_reply (ptid, status);
167
168 /* If this is the first stop reply in the queue, then inform GDB
169 about it, by sending a Stop notification. */
170 if (notif_queue->next == NULL)
171 {
172 char *p = own_buf;
173 strcpy (p, "Stop:");
174 p += strlen (p);
175 prepare_resume_reply (p,
176 notif_queue->ptid, &notif_queue->status);
177 putpkt_notif (own_buf);
178 }
179 }
180
181 /* Get rid of the currently pending stop replies for PID. If PID is
182 -1, then apply to all processes. */
183
184 static void
185 discard_queued_stop_replies (int pid)
186 {
187 struct vstop_notif *prev = NULL, *reply, *next;
188
189 for (reply = notif_queue; reply; reply = next)
190 {
191 next = reply->next;
192
193 if (pid == -1
194 || ptid_get_pid (reply->ptid) == pid)
195 {
196 if (reply == notif_queue)
197 notif_queue = next;
198 else
199 prev->next = reply->next;
200
201 free (reply);
202 }
203 else
204 prev = reply;
205 }
206 }
207
208 /* If there are more stop replies to push, push one now. */
209
210 static void
211 send_next_stop_reply (char *own_buf)
212 {
213 if (notif_queue)
214 prepare_resume_reply (own_buf,
215 notif_queue->ptid,
216 &notif_queue->status);
217 else
218 write_ok (own_buf);
219 }
220
221 static int
222 target_running (void)
223 {
224 return all_threads.head != NULL;
225 }
226
227 static int
228 start_inferior (char **argv)
229 {
230 char **new_argv = argv;
231
232 if (wrapper_argv != NULL)
233 {
234 int i, count = 1;
235
236 for (i = 0; wrapper_argv[i] != NULL; i++)
237 count++;
238 for (i = 0; argv[i] != NULL; i++)
239 count++;
240 new_argv = alloca (sizeof (char *) * count);
241 count = 0;
242 for (i = 0; wrapper_argv[i] != NULL; i++)
243 new_argv[count++] = wrapper_argv[i];
244 for (i = 0; argv[i] != NULL; i++)
245 new_argv[count++] = argv[i];
246 new_argv[count] = NULL;
247 }
248
249 if (debug_threads)
250 {
251 int i;
252 for (i = 0; new_argv[i]; ++i)
253 fprintf (stderr, "new_argv[%d] = \"%s\"\n", i, new_argv[i]);
254 fflush (stderr);
255 }
256
257 #ifdef SIGTTOU
258 signal (SIGTTOU, SIG_DFL);
259 signal (SIGTTIN, SIG_DFL);
260 #endif
261
262 signal_pid = create_inferior (new_argv[0], new_argv);
263
264 /* FIXME: we don't actually know at this point that the create
265 actually succeeded. We won't know that until we wait. */
266 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
267 signal_pid);
268 fflush (stderr);
269
270 #ifdef SIGTTOU
271 signal (SIGTTOU, SIG_IGN);
272 signal (SIGTTIN, SIG_IGN);
273 terminal_fd = fileno (stderr);
274 old_foreground_pgrp = tcgetpgrp (terminal_fd);
275 tcsetpgrp (terminal_fd, signal_pid);
276 atexit (restore_old_foreground_pgrp);
277 #endif
278
279 if (wrapper_argv != NULL)
280 {
281 struct thread_resume resume_info;
282
283 resume_info.thread = pid_to_ptid (signal_pid);
284 resume_info.kind = resume_continue;
285 resume_info.sig = 0;
286
287 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
288
289 if (last_status.kind != TARGET_WAITKIND_STOPPED)
290 return signal_pid;
291
292 do
293 {
294 (*the_target->resume) (&resume_info, 1);
295
296 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
297 if (last_status.kind != TARGET_WAITKIND_STOPPED)
298 return signal_pid;
299
300 current_inferior->last_resume_kind = resume_stop;
301 current_inferior->last_status = last_status;
302 }
303 while (last_status.value.sig != TARGET_SIGNAL_TRAP);
304
305 current_inferior->last_resume_kind = resume_stop;
306 current_inferior->last_status = last_status;
307 return signal_pid;
308 }
309
310 /* Wait till we are at 1st instruction in program, return new pid
311 (assuming success). */
312 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
313
314 if (last_status.kind != TARGET_WAITKIND_EXITED
315 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
316 {
317 current_inferior->last_resume_kind = resume_stop;
318 current_inferior->last_status = last_status;
319 }
320
321 return signal_pid;
322 }
323
324 static int
325 attach_inferior (int pid)
326 {
327 /* myattach should return -1 if attaching is unsupported,
328 0 if it succeeded, and call error() otherwise. */
329
330 if (myattach (pid) != 0)
331 return -1;
332
333 fprintf (stderr, "Attached; pid = %d\n", pid);
334 fflush (stderr);
335
336 /* FIXME - It may be that we should get the SIGNAL_PID from the
337 attach function, so that it can be the main thread instead of
338 whichever we were told to attach to. */
339 signal_pid = pid;
340
341 /* Clear this so the backend doesn't get confused, thinking
342 CONT_THREAD died, and it needs to resume all threads. */
343 cont_thread = null_ptid;
344
345 if (!non_stop)
346 {
347 last_ptid = mywait (pid_to_ptid (pid), &last_status, 0, 0);
348
349 /* GDB knows to ignore the first SIGSTOP after attaching to a running
350 process using the "attach" command, but this is different; it's
351 just using "target remote". Pretend it's just starting up. */
352 if (last_status.kind == TARGET_WAITKIND_STOPPED
353 && last_status.value.sig == TARGET_SIGNAL_STOP)
354 last_status.value.sig = TARGET_SIGNAL_TRAP;
355
356 current_inferior->last_resume_kind = resume_stop;
357 current_inferior->last_status = last_status;
358 }
359
360 return 0;
361 }
362
363 extern int remote_debug;
364
365 /* Decode a qXfer read request. Return 0 if everything looks OK,
366 or -1 otherwise. */
367
368 static int
369 decode_xfer_read (char *buf, CORE_ADDR *ofs, unsigned int *len)
370 {
371 /* After the read marker and annex, qXfer looks like a
372 traditional 'm' packet. */
373 decode_m_packet (buf, ofs, len);
374
375 return 0;
376 }
377
378 static int
379 decode_xfer (char *buf, char **object, char **rw, char **annex, char **offset)
380 {
381 /* Extract and NUL-terminate the object. */
382 *object = buf;
383 while (*buf && *buf != ':')
384 buf++;
385 if (*buf == '\0')
386 return -1;
387 *buf++ = 0;
388
389 /* Extract and NUL-terminate the read/write action. */
390 *rw = buf;
391 while (*buf && *buf != ':')
392 buf++;
393 if (*buf == '\0')
394 return -1;
395 *buf++ = 0;
396
397 /* Extract and NUL-terminate the annex. */
398 *annex = buf;
399 while (*buf && *buf != ':')
400 buf++;
401 if (*buf == '\0')
402 return -1;
403 *buf++ = 0;
404
405 *offset = buf;
406 return 0;
407 }
408
409 /* Write the response to a successful qXfer read. Returns the
410 length of the (binary) data stored in BUF, corresponding
411 to as much of DATA/LEN as we could fit. IS_MORE controls
412 the first character of the response. */
413 static int
414 write_qxfer_response (char *buf, const void *data, int len, int is_more)
415 {
416 int out_len;
417
418 if (is_more)
419 buf[0] = 'm';
420 else
421 buf[0] = 'l';
422
423 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
424 PBUFSIZ - 2) + 1;
425 }
426
427 /* Handle all of the extended 'Q' packets. */
428
429 static void
430 handle_general_set (char *own_buf)
431 {
432 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
433 {
434 int numsigs = (int) TARGET_SIGNAL_LAST, i;
435 const char *p = own_buf + strlen ("QPassSignals:");
436 CORE_ADDR cursig;
437
438 p = decode_address_to_semicolon (&cursig, p);
439 for (i = 0; i < numsigs; i++)
440 {
441 if (i == cursig)
442 {
443 pass_signals[i] = 1;
444 if (*p == '\0')
445 /* Keep looping, to clear the remaining signals. */
446 cursig = -1;
447 else
448 p = decode_address_to_semicolon (&cursig, p);
449 }
450 else
451 pass_signals[i] = 0;
452 }
453 strcpy (own_buf, "OK");
454 return;
455 }
456
457 if (strcmp (own_buf, "QStartNoAckMode") == 0)
458 {
459 if (remote_debug)
460 {
461 fprintf (stderr, "[noack mode enabled]\n");
462 fflush (stderr);
463 }
464
465 noack_mode = 1;
466 write_ok (own_buf);
467 return;
468 }
469
470 if (strncmp (own_buf, "QNonStop:", 9) == 0)
471 {
472 char *mode = own_buf + 9;
473 int req = -1;
474 char *req_str;
475
476 if (strcmp (mode, "0") == 0)
477 req = 0;
478 else if (strcmp (mode, "1") == 0)
479 req = 1;
480 else
481 {
482 /* We don't know what this mode is, so complain to
483 GDB. */
484 fprintf (stderr, "Unknown non-stop mode requested: %s\n",
485 own_buf);
486 write_enn (own_buf);
487 return;
488 }
489
490 req_str = req ? "non-stop" : "all-stop";
491 if (start_non_stop (req) != 0)
492 {
493 fprintf (stderr, "Setting %s mode failed\n", req_str);
494 write_enn (own_buf);
495 return;
496 }
497
498 non_stop = req;
499
500 if (remote_debug)
501 fprintf (stderr, "[%s mode enabled]\n", req_str);
502
503 write_ok (own_buf);
504 return;
505 }
506
507 if (strncmp ("QDisableRandomization:", own_buf,
508 strlen ("QDisableRandomization:")) == 0)
509 {
510 char *packet = own_buf + strlen ("QDisableRandomization:");
511 ULONGEST setting;
512
513 unpack_varlen_hex (packet, &setting);
514 disable_randomization = setting;
515
516 if (remote_debug)
517 {
518 if (disable_randomization)
519 fprintf (stderr, "[address space randomization disabled]\n");
520 else
521 fprintf (stderr, "[address space randomization enabled]\n");
522 }
523
524 write_ok (own_buf);
525 return;
526 }
527
528 if (target_supports_tracepoints ()
529 && handle_tracepoint_general_set (own_buf))
530 return;
531
532 /* Otherwise we didn't know what packet it was. Say we didn't
533 understand it. */
534 own_buf[0] = 0;
535 }
536
537 static const char *
538 get_features_xml (const char *annex)
539 {
540 /* gdbserver_xmltarget defines what to return when looking
541 for the "target.xml" file. Its contents can either be
542 verbatim XML code (prefixed with a '@') or else the name
543 of the actual XML file to be used in place of "target.xml".
544
545 This variable is set up from the auto-generated
546 init_registers_... routine for the current target. */
547
548 if (gdbserver_xmltarget
549 && strcmp (annex, "target.xml") == 0)
550 {
551 if (*gdbserver_xmltarget == '@')
552 return gdbserver_xmltarget + 1;
553 else
554 annex = gdbserver_xmltarget;
555 }
556
557 #ifdef USE_XML
558 {
559 extern const char *const xml_builtin[][2];
560 int i;
561
562 /* Look for the annex. */
563 for (i = 0; xml_builtin[i][0] != NULL; i++)
564 if (strcmp (annex, xml_builtin[i][0]) == 0)
565 break;
566
567 if (xml_builtin[i][0] != NULL)
568 return xml_builtin[i][1];
569 }
570 #endif
571
572 return NULL;
573 }
574
575 void
576 monitor_show_help (void)
577 {
578 monitor_output ("The following monitor commands are supported:\n");
579 monitor_output (" set debug <0|1>\n");
580 monitor_output (" Enable general debugging messages\n");
581 monitor_output (" set debug-hw-points <0|1>\n");
582 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
583 monitor_output (" set remote-debug <0|1>\n");
584 monitor_output (" Enable remote protocol debugging messages\n");
585 monitor_output (" exit\n");
586 monitor_output (" Quit GDBserver\n");
587 }
588
589 /* Read trace frame or inferior memory. Returns the number of bytes
590 actually read, zero when no further transfer is possible, and -1 on
591 error. Return of a positive value smaller than LEN does not
592 indicate there's no more to be read, only the end of the transfer.
593 E.g., when GDB reads memory from a traceframe, a first request may
594 be served from a memory block that does not cover the whole request
595 length. A following request gets the rest served from either
596 another block (of the same traceframe) or from the read-only
597 regions. */
598
599 static int
600 gdb_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
601 {
602 int res;
603
604 if (current_traceframe >= 0)
605 {
606 ULONGEST nbytes;
607 ULONGEST length = len;
608
609 if (traceframe_read_mem (current_traceframe,
610 memaddr, myaddr, len, &nbytes))
611 return EIO;
612 /* Data read from trace buffer, we're done. */
613 if (nbytes > 0)
614 return nbytes;
615 if (!in_readonly_region (memaddr, length))
616 return -1;
617 /* Otherwise we have a valid readonly case, fall through. */
618 /* (assume no half-trace half-real blocks for now) */
619 }
620
621 res = prepare_to_access_memory ();
622 if (res == 0)
623 {
624 res = read_inferior_memory (memaddr, myaddr, len);
625 done_accessing_memory ();
626
627 return res == 0 ? len : -1;
628 }
629 else
630 return -1;
631 }
632
633 /* Write trace frame or inferior memory. Actually, writing to trace
634 frames is forbidden. */
635
636 static int
637 gdb_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
638 {
639 if (current_traceframe >= 0)
640 return EIO;
641 else
642 {
643 int ret;
644
645 ret = prepare_to_access_memory ();
646 if (ret == 0)
647 {
648 ret = write_inferior_memory (memaddr, myaddr, len);
649 done_accessing_memory ();
650 }
651 return ret;
652 }
653 }
654
655 /* Subroutine of handle_search_memory to simplify it. */
656
657 static int
658 handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len,
659 gdb_byte *pattern, unsigned pattern_len,
660 gdb_byte *search_buf,
661 unsigned chunk_size, unsigned search_buf_size,
662 CORE_ADDR *found_addrp)
663 {
664 /* Prime the search buffer. */
665
666 if (gdb_read_memory (start_addr, search_buf, search_buf_size)
667 != search_buf_size)
668 {
669 warning ("Unable to access target memory at 0x%lx, halting search.",
670 (long) start_addr);
671 return -1;
672 }
673
674 /* Perform the search.
675
676 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
677 When we've scanned N bytes we copy the trailing bytes to the start and
678 read in another N bytes. */
679
680 while (search_space_len >= pattern_len)
681 {
682 gdb_byte *found_ptr;
683 unsigned nr_search_bytes = (search_space_len < search_buf_size
684 ? search_space_len
685 : search_buf_size);
686
687 found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len);
688
689 if (found_ptr != NULL)
690 {
691 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
692 *found_addrp = found_addr;
693 return 1;
694 }
695
696 /* Not found in this chunk, skip to next chunk. */
697
698 /* Don't let search_space_len wrap here, it's unsigned. */
699 if (search_space_len >= chunk_size)
700 search_space_len -= chunk_size;
701 else
702 search_space_len = 0;
703
704 if (search_space_len >= pattern_len)
705 {
706 unsigned keep_len = search_buf_size - chunk_size;
707 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
708 int nr_to_read;
709
710 /* Copy the trailing part of the previous iteration to the front
711 of the buffer for the next iteration. */
712 memcpy (search_buf, search_buf + chunk_size, keep_len);
713
714 nr_to_read = (search_space_len - keep_len < chunk_size
715 ? search_space_len - keep_len
716 : chunk_size);
717
718 if (gdb_read_memory (read_addr, search_buf + keep_len,
719 nr_to_read) != search_buf_size)
720 {
721 warning ("Unable to access target memory "
722 "at 0x%lx, halting search.",
723 (long) read_addr);
724 return -1;
725 }
726
727 start_addr += chunk_size;
728 }
729 }
730
731 /* Not found. */
732
733 return 0;
734 }
735
736 /* Handle qSearch:memory packets. */
737
738 static void
739 handle_search_memory (char *own_buf, int packet_len)
740 {
741 CORE_ADDR start_addr;
742 CORE_ADDR search_space_len;
743 gdb_byte *pattern;
744 unsigned int pattern_len;
745 /* NOTE: also defined in find.c testcase. */
746 #define SEARCH_CHUNK_SIZE 16000
747 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
748 /* Buffer to hold memory contents for searching. */
749 gdb_byte *search_buf;
750 unsigned search_buf_size;
751 int found;
752 CORE_ADDR found_addr;
753 int cmd_name_len = sizeof ("qSearch:memory:") - 1;
754
755 pattern = malloc (packet_len);
756 if (pattern == NULL)
757 {
758 error ("Unable to allocate memory to perform the search");
759 strcpy (own_buf, "E00");
760 return;
761 }
762 if (decode_search_memory_packet (own_buf + cmd_name_len,
763 packet_len - cmd_name_len,
764 &start_addr, &search_space_len,
765 pattern, &pattern_len) < 0)
766 {
767 free (pattern);
768 error ("Error in parsing qSearch:memory packet");
769 strcpy (own_buf, "E00");
770 return;
771 }
772
773 search_buf_size = chunk_size + pattern_len - 1;
774
775 /* No point in trying to allocate a buffer larger than the search space. */
776 if (search_space_len < search_buf_size)
777 search_buf_size = search_space_len;
778
779 search_buf = malloc (search_buf_size);
780 if (search_buf == NULL)
781 {
782 free (pattern);
783 error ("Unable to allocate memory to perform the search");
784 strcpy (own_buf, "E00");
785 return;
786 }
787
788 found = handle_search_memory_1 (start_addr, search_space_len,
789 pattern, pattern_len,
790 search_buf, chunk_size, search_buf_size,
791 &found_addr);
792
793 if (found > 0)
794 sprintf (own_buf, "1,%lx", (long) found_addr);
795 else if (found == 0)
796 strcpy (own_buf, "0");
797 else
798 strcpy (own_buf, "E00");
799
800 free (search_buf);
801 free (pattern);
802 }
803
804 #define require_running(BUF) \
805 if (!target_running ()) \
806 { \
807 write_enn (BUF); \
808 return; \
809 }
810
811 /* Handle monitor commands not handled by target-specific handlers. */
812
813 static void
814 handle_monitor_command (char *mon, char *own_buf)
815 {
816 if (strcmp (mon, "set debug 1") == 0)
817 {
818 debug_threads = 1;
819 monitor_output ("Debug output enabled.\n");
820 }
821 else if (strcmp (mon, "set debug 0") == 0)
822 {
823 debug_threads = 0;
824 monitor_output ("Debug output disabled.\n");
825 }
826 else if (strcmp (mon, "set debug-hw-points 1") == 0)
827 {
828 debug_hw_points = 1;
829 monitor_output ("H/W point debugging output enabled.\n");
830 }
831 else if (strcmp (mon, "set debug-hw-points 0") == 0)
832 {
833 debug_hw_points = 0;
834 monitor_output ("H/W point debugging output disabled.\n");
835 }
836 else if (strcmp (mon, "set remote-debug 1") == 0)
837 {
838 remote_debug = 1;
839 monitor_output ("Protocol debug output enabled.\n");
840 }
841 else if (strcmp (mon, "set remote-debug 0") == 0)
842 {
843 remote_debug = 0;
844 monitor_output ("Protocol debug output disabled.\n");
845 }
846 else if (strcmp (mon, "help") == 0)
847 monitor_show_help ();
848 else if (strcmp (mon, "exit") == 0)
849 exit_requested = 1;
850 else
851 {
852 monitor_output ("Unknown monitor command.\n\n");
853 monitor_show_help ();
854 write_enn (own_buf);
855 }
856 }
857
858 /* Associates a callback with each supported qXfer'able object. */
859
860 struct qxfer
861 {
862 /* The object this handler handles. */
863 const char *object;
864
865 /* Request that the target transfer up to LEN 8-bit bytes of the
866 target's OBJECT. The OFFSET, for a seekable object, specifies
867 the starting point. The ANNEX can be used to provide additional
868 data-specific information to the target.
869
870 Return the number of bytes actually transfered, zero when no
871 further transfer is possible, -1 on error, and -2 when the
872 transfer is not supported. Return of a positive value smaller
873 than LEN does not indicate the end of the object, only the end of
874 the transfer.
875
876 One, and only one, of readbuf or writebuf must be non-NULL. */
877 int (*xfer) (const char *annex,
878 gdb_byte *readbuf, const gdb_byte *writebuf,
879 ULONGEST offset, LONGEST len);
880 };
881
882 /* Handle qXfer:auxv:read. */
883
884 static int
885 handle_qxfer_auxv (const char *annex,
886 gdb_byte *readbuf, const gdb_byte *writebuf,
887 ULONGEST offset, LONGEST len)
888 {
889 if (the_target->read_auxv == NULL || writebuf != NULL)
890 return -2;
891
892 if (annex[0] != '\0' || !target_running ())
893 return -1;
894
895 return (*the_target->read_auxv) (offset, readbuf, len);
896 }
897
898 /* Handle qXfer:features:read. */
899
900 static int
901 handle_qxfer_features (const char *annex,
902 gdb_byte *readbuf, const gdb_byte *writebuf,
903 ULONGEST offset, LONGEST len)
904 {
905 const char *document;
906 size_t total_len;
907
908 if (writebuf != NULL)
909 return -2;
910
911 if (!target_running ())
912 return -1;
913
914 /* Grab the correct annex. */
915 document = get_features_xml (annex);
916 if (document == NULL)
917 return -1;
918
919 total_len = strlen (document);
920
921 if (offset > total_len)
922 return -1;
923
924 if (offset + len > total_len)
925 len = total_len - offset;
926
927 memcpy (readbuf, document + offset, len);
928 return len;
929 }
930
931 /* Handle qXfer:libraries:read. */
932
933 static int
934 handle_qxfer_libraries (const char *annex,
935 gdb_byte *readbuf, const gdb_byte *writebuf,
936 ULONGEST offset, LONGEST len)
937 {
938 unsigned int total_len;
939 char *document, *p;
940 struct inferior_list_entry *dll_ptr;
941
942 if (writebuf != NULL)
943 return -2;
944
945 if (annex[0] != '\0' || !target_running ())
946 return -1;
947
948 /* Do not confuse this packet with qXfer:libraries-svr4:read. */
949 if (the_target->qxfer_libraries_svr4 != NULL)
950 return 0;
951
952 /* Over-estimate the necessary memory. Assume that every character
953 in the library name must be escaped. */
954 total_len = 64;
955 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
956 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
957
958 document = malloc (total_len);
959 if (document == NULL)
960 return -1;
961
962 strcpy (document, "<library-list>\n");
963 p = document + strlen (document);
964
965 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
966 {
967 struct dll_info *dll = (struct dll_info *) dll_ptr;
968 char *name;
969
970 strcpy (p, " <library name=\"");
971 p = p + strlen (p);
972 name = xml_escape_text (dll->name);
973 strcpy (p, name);
974 free (name);
975 p = p + strlen (p);
976 strcpy (p, "\"><segment address=\"");
977 p = p + strlen (p);
978 sprintf (p, "0x%lx", (long) dll->base_addr);
979 p = p + strlen (p);
980 strcpy (p, "\"/></library>\n");
981 p = p + strlen (p);
982 }
983
984 strcpy (p, "</library-list>\n");
985
986 total_len = strlen (document);
987
988 if (offset > total_len)
989 {
990 free (document);
991 return -1;
992 }
993
994 if (offset + len > total_len)
995 len = total_len - offset;
996
997 memcpy (readbuf, document + offset, len);
998 free (document);
999 return len;
1000 }
1001
1002 /* Handle qXfer:libraries-svr4:read. */
1003
1004 static int
1005 handle_qxfer_libraries_svr4 (const char *annex,
1006 gdb_byte *readbuf, const gdb_byte *writebuf,
1007 ULONGEST offset, LONGEST len)
1008 {
1009 if (writebuf != NULL)
1010 return -2;
1011
1012 if (annex[0] != '\0' || !target_running ()
1013 || the_target->qxfer_libraries_svr4 == NULL)
1014 return -1;
1015
1016 return the_target->qxfer_libraries_svr4 (annex, readbuf, writebuf, offset, len);
1017 }
1018
1019 /* Handle qXfer:osadata:read. */
1020
1021 static int
1022 handle_qxfer_osdata (const char *annex,
1023 gdb_byte *readbuf, const gdb_byte *writebuf,
1024 ULONGEST offset, LONGEST len)
1025 {
1026 if (the_target->qxfer_osdata == NULL || writebuf != NULL)
1027 return -2;
1028
1029 return (*the_target->qxfer_osdata) (annex, readbuf, NULL, offset, len);
1030 }
1031
1032 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1033
1034 static int
1035 handle_qxfer_siginfo (const char *annex,
1036 gdb_byte *readbuf, const gdb_byte *writebuf,
1037 ULONGEST offset, LONGEST len)
1038 {
1039 if (the_target->qxfer_siginfo == NULL)
1040 return -2;
1041
1042 if (annex[0] != '\0' || !target_running ())
1043 return -1;
1044
1045 return (*the_target->qxfer_siginfo) (annex, readbuf, writebuf, offset, len);
1046 }
1047
1048 /* Handle qXfer:spu:read and qXfer:spu:write. */
1049
1050 static int
1051 handle_qxfer_spu (const char *annex,
1052 gdb_byte *readbuf, const gdb_byte *writebuf,
1053 ULONGEST offset, LONGEST len)
1054 {
1055 if (the_target->qxfer_spu == NULL)
1056 return -2;
1057
1058 if (!target_running ())
1059 return -1;
1060
1061 return (*the_target->qxfer_spu) (annex, readbuf, writebuf, offset, len);
1062 }
1063
1064 /* Handle qXfer:statictrace:read. */
1065
1066 static int
1067 handle_qxfer_statictrace (const char *annex,
1068 gdb_byte *readbuf, const gdb_byte *writebuf,
1069 ULONGEST offset, LONGEST len)
1070 {
1071 ULONGEST nbytes;
1072
1073 if (writebuf != NULL)
1074 return -2;
1075
1076 if (annex[0] != '\0' || !target_running () || current_traceframe == -1)
1077 return -1;
1078
1079 if (traceframe_read_sdata (current_traceframe, offset,
1080 readbuf, len, &nbytes))
1081 return -1;
1082 return nbytes;
1083 }
1084
1085 /* Helper for handle_qxfer_threads. */
1086
1087 static void
1088 handle_qxfer_threads_proper (struct buffer *buffer)
1089 {
1090 struct inferior_list_entry *thread;
1091
1092 buffer_grow_str (buffer, "<threads>\n");
1093
1094 for (thread = all_threads.head; thread; thread = thread->next)
1095 {
1096 ptid_t ptid = thread_to_gdb_id ((struct thread_info *)thread);
1097 char ptid_s[100];
1098 int core = -1;
1099 char core_s[21];
1100
1101 write_ptid (ptid_s, ptid);
1102
1103 if (the_target->core_of_thread)
1104 core = (*the_target->core_of_thread) (ptid);
1105
1106 if (core != -1)
1107 {
1108 sprintf (core_s, "%d", core);
1109 buffer_xml_printf (buffer, "<thread id=\"%s\" core=\"%s\"/>\n",
1110 ptid_s, core_s);
1111 }
1112 else
1113 {
1114 buffer_xml_printf (buffer, "<thread id=\"%s\"/>\n",
1115 ptid_s);
1116 }
1117 }
1118
1119 buffer_grow_str0 (buffer, "</threads>\n");
1120 }
1121
1122 /* Handle qXfer:threads:read. */
1123
1124 static int
1125 handle_qxfer_threads (const char *annex,
1126 gdb_byte *readbuf, const gdb_byte *writebuf,
1127 ULONGEST offset, LONGEST len)
1128 {
1129 static char *result = 0;
1130 static unsigned int result_length = 0;
1131
1132 if (writebuf != NULL)
1133 return -2;
1134
1135 if (!target_running () || annex[0] != '\0')
1136 return -1;
1137
1138 if (offset == 0)
1139 {
1140 struct buffer buffer;
1141 /* When asked for data at offset 0, generate everything and store into
1142 'result'. Successive reads will be served off 'result'. */
1143 if (result)
1144 free (result);
1145
1146 buffer_init (&buffer);
1147
1148 handle_qxfer_threads_proper (&buffer);
1149
1150 result = buffer_finish (&buffer);
1151 result_length = strlen (result);
1152 buffer_free (&buffer);
1153 }
1154
1155 if (offset >= result_length)
1156 {
1157 /* We're out of data. */
1158 free (result);
1159 result = NULL;
1160 result_length = 0;
1161 return 0;
1162 }
1163
1164 if (len > result_length - offset)
1165 len = result_length - offset;
1166
1167 memcpy (readbuf, result + offset, len);
1168
1169 return len;
1170 }
1171
1172 /* Handle qXfer:traceframe-info:read. */
1173
1174 static int
1175 handle_qxfer_traceframe_info (const char *annex,
1176 gdb_byte *readbuf, const gdb_byte *writebuf,
1177 ULONGEST offset, LONGEST len)
1178 {
1179 static char *result = 0;
1180 static unsigned int result_length = 0;
1181
1182 if (writebuf != NULL)
1183 return -2;
1184
1185 if (!target_running () || annex[0] != '\0' || current_traceframe == -1)
1186 return -1;
1187
1188 if (offset == 0)
1189 {
1190 struct buffer buffer;
1191
1192 /* When asked for data at offset 0, generate everything and
1193 store into 'result'. Successive reads will be served off
1194 'result'. */
1195 free (result);
1196
1197 buffer_init (&buffer);
1198
1199 traceframe_read_info (current_traceframe, &buffer);
1200
1201 result = buffer_finish (&buffer);
1202 result_length = strlen (result);
1203 buffer_free (&buffer);
1204 }
1205
1206 if (offset >= result_length)
1207 {
1208 /* We're out of data. */
1209 free (result);
1210 result = NULL;
1211 result_length = 0;
1212 return 0;
1213 }
1214
1215 if (len > result_length - offset)
1216 len = result_length - offset;
1217
1218 memcpy (readbuf, result + offset, len);
1219 return len;
1220 }
1221
1222 /* Handle qXfer:fdpic:read. */
1223
1224 static int
1225 handle_qxfer_fdpic (const char *annex, gdb_byte *readbuf,
1226 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1227 {
1228 if (the_target->read_loadmap == NULL)
1229 return -2;
1230
1231 if (!target_running ())
1232 return -1;
1233
1234 return (*the_target->read_loadmap) (annex, offset, readbuf, len);
1235 }
1236
1237 static const struct qxfer qxfer_packets[] =
1238 {
1239 { "auxv", handle_qxfer_auxv },
1240 { "fdpic", handle_qxfer_fdpic},
1241 { "features", handle_qxfer_features },
1242 { "libraries", handle_qxfer_libraries },
1243 { "libraries-svr4", handle_qxfer_libraries_svr4 },
1244 { "osdata", handle_qxfer_osdata },
1245 { "siginfo", handle_qxfer_siginfo },
1246 { "spu", handle_qxfer_spu },
1247 { "statictrace", handle_qxfer_statictrace },
1248 { "threads", handle_qxfer_threads },
1249 { "traceframe-info", handle_qxfer_traceframe_info },
1250 };
1251
1252 static int
1253 handle_qxfer (char *own_buf, int packet_len, int *new_packet_len_p)
1254 {
1255 int i;
1256 char *object;
1257 char *rw;
1258 char *annex;
1259 char *offset;
1260
1261 if (strncmp (own_buf, "qXfer:", 6) != 0)
1262 return 0;
1263
1264 /* Grab the object, r/w and annex. */
1265 if (decode_xfer (own_buf + 6, &object, &rw, &annex, &offset) < 0)
1266 {
1267 write_enn (own_buf);
1268 return 1;
1269 }
1270
1271 for (i = 0;
1272 i < sizeof (qxfer_packets) / sizeof (qxfer_packets[0]);
1273 i++)
1274 {
1275 const struct qxfer *q = &qxfer_packets[i];
1276
1277 if (strcmp (object, q->object) == 0)
1278 {
1279 if (strcmp (rw, "read") == 0)
1280 {
1281 unsigned char *data;
1282 int n;
1283 CORE_ADDR ofs;
1284 unsigned int len;
1285
1286 /* Grab the offset and length. */
1287 if (decode_xfer_read (offset, &ofs, &len) < 0)
1288 {
1289 write_enn (own_buf);
1290 return 1;
1291 }
1292
1293 /* Read one extra byte, as an indicator of whether there is
1294 more. */
1295 if (len > PBUFSIZ - 2)
1296 len = PBUFSIZ - 2;
1297 data = malloc (len + 1);
1298 if (data == NULL)
1299 {
1300 write_enn (own_buf);
1301 return 1;
1302 }
1303 n = (*q->xfer) (annex, data, NULL, ofs, len + 1);
1304 if (n == -2)
1305 {
1306 free (data);
1307 return 0;
1308 }
1309 else if (n < 0)
1310 write_enn (own_buf);
1311 else if (n > len)
1312 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1313 else
1314 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1315
1316 free (data);
1317 return 1;
1318 }
1319 else if (strcmp (rw, "write") == 0)
1320 {
1321 int n;
1322 unsigned int len;
1323 CORE_ADDR ofs;
1324 unsigned char *data;
1325
1326 strcpy (own_buf, "E00");
1327 data = malloc (packet_len - (offset - own_buf));
1328 if (data == NULL)
1329 {
1330 write_enn (own_buf);
1331 return 1;
1332 }
1333 if (decode_xfer_write (offset, packet_len - (offset - own_buf),
1334 &ofs, &len, data) < 0)
1335 {
1336 free (data);
1337 write_enn (own_buf);
1338 return 1;
1339 }
1340
1341 n = (*q->xfer) (annex, NULL, data, ofs, len);
1342 if (n == -2)
1343 {
1344 free (data);
1345 return 0;
1346 }
1347 else if (n < 0)
1348 write_enn (own_buf);
1349 else
1350 sprintf (own_buf, "%x", n);
1351
1352 free (data);
1353 return 1;
1354 }
1355
1356 return 0;
1357 }
1358 }
1359
1360 return 0;
1361 }
1362
1363 /* Table used by the crc32 function to calcuate the checksum. */
1364
1365 static unsigned int crc32_table[256] =
1366 {0, 0};
1367
1368 /* Compute 32 bit CRC from inferior memory.
1369
1370 On success, return 32 bit CRC.
1371 On failure, return (unsigned long long) -1. */
1372
1373 static unsigned long long
1374 crc32 (CORE_ADDR base, int len, unsigned int crc)
1375 {
1376 if (!crc32_table[1])
1377 {
1378 /* Initialize the CRC table and the decoding table. */
1379 int i, j;
1380 unsigned int c;
1381
1382 for (i = 0; i < 256; i++)
1383 {
1384 for (c = i << 24, j = 8; j > 0; --j)
1385 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
1386 crc32_table[i] = c;
1387 }
1388 }
1389
1390 while (len--)
1391 {
1392 unsigned char byte = 0;
1393
1394 /* Return failure if memory read fails. */
1395 if (read_inferior_memory (base, &byte, 1) != 0)
1396 return (unsigned long long) -1;
1397
1398 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255];
1399 base++;
1400 }
1401 return (unsigned long long) crc;
1402 }
1403
1404 /* Handle all of the extended 'q' packets. */
1405
1406 void
1407 handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
1408 {
1409 static struct inferior_list_entry *thread_ptr;
1410
1411 /* Reply the current thread id. */
1412 if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC)
1413 {
1414 ptid_t gdb_id;
1415 require_running (own_buf);
1416
1417 if (!ptid_equal (general_thread, null_ptid)
1418 && !ptid_equal (general_thread, minus_one_ptid))
1419 gdb_id = general_thread;
1420 else
1421 {
1422 thread_ptr = all_threads.head;
1423 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
1424 }
1425
1426 sprintf (own_buf, "QC");
1427 own_buf += 2;
1428 write_ptid (own_buf, gdb_id);
1429 return;
1430 }
1431
1432 if (strcmp ("qSymbol::", own_buf) == 0)
1433 {
1434 /* GDB is suggesting new symbols have been loaded. This may
1435 mean a new shared library has been detected as loaded, so
1436 take the opportunity to check if breakpoints we think are
1437 inserted, still are. Note that it isn't guaranteed that
1438 we'll see this when a shared library is loaded, and nor will
1439 we see this for unloads (although breakpoints in unloaded
1440 libraries shouldn't trigger), as GDB may not find symbols for
1441 the library at all. We also re-validate breakpoints when we
1442 see a second GDB breakpoint for the same address, and or when
1443 we access breakpoint shadows. */
1444 validate_breakpoints ();
1445
1446 if (target_supports_tracepoints ())
1447 tracepoint_look_up_symbols ();
1448
1449 if (target_running () && the_target->look_up_symbols != NULL)
1450 (*the_target->look_up_symbols) ();
1451
1452 strcpy (own_buf, "OK");
1453 return;
1454 }
1455
1456 if (!disable_packet_qfThreadInfo)
1457 {
1458 if (strcmp ("qfThreadInfo", own_buf) == 0)
1459 {
1460 ptid_t gdb_id;
1461
1462 require_running (own_buf);
1463 thread_ptr = all_threads.head;
1464
1465 *own_buf++ = 'm';
1466 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
1467 write_ptid (own_buf, gdb_id);
1468 thread_ptr = thread_ptr->next;
1469 return;
1470 }
1471
1472 if (strcmp ("qsThreadInfo", own_buf) == 0)
1473 {
1474 ptid_t gdb_id;
1475
1476 require_running (own_buf);
1477 if (thread_ptr != NULL)
1478 {
1479 *own_buf++ = 'm';
1480 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
1481 write_ptid (own_buf, gdb_id);
1482 thread_ptr = thread_ptr->next;
1483 return;
1484 }
1485 else
1486 {
1487 sprintf (own_buf, "l");
1488 return;
1489 }
1490 }
1491 }
1492
1493 if (the_target->read_offsets != NULL
1494 && strcmp ("qOffsets", own_buf) == 0)
1495 {
1496 CORE_ADDR text, data;
1497
1498 require_running (own_buf);
1499 if (the_target->read_offsets (&text, &data))
1500 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
1501 (long)text, (long)data, (long)data);
1502 else
1503 write_enn (own_buf);
1504
1505 return;
1506 }
1507
1508 /* Protocol features query. */
1509 if (strncmp ("qSupported", own_buf, 10) == 0
1510 && (own_buf[10] == ':' || own_buf[10] == '\0'))
1511 {
1512 char *p = &own_buf[10];
1513 int gdb_supports_qRelocInsn = 0;
1514
1515 /* Start processing qSupported packet. */
1516 target_process_qsupported (NULL);
1517
1518 /* Process each feature being provided by GDB. The first
1519 feature will follow a ':', and latter features will follow
1520 ';'. */
1521 if (*p == ':')
1522 {
1523 char **qsupported = NULL;
1524 int count = 0;
1525 int i;
1526
1527 /* Two passes, to avoid nested strtok calls in
1528 target_process_qsupported. */
1529 for (p = strtok (p + 1, ";");
1530 p != NULL;
1531 p = strtok (NULL, ";"))
1532 {
1533 count++;
1534 qsupported = xrealloc (qsupported, count * sizeof (char *));
1535 qsupported[count - 1] = xstrdup (p);
1536 }
1537
1538 for (i = 0; i < count; i++)
1539 {
1540 p = qsupported[i];
1541 if (strcmp (p, "multiprocess+") == 0)
1542 {
1543 /* GDB supports and wants multi-process support if
1544 possible. */
1545 if (target_supports_multi_process ())
1546 multi_process = 1;
1547 }
1548 else if (strcmp (p, "qRelocInsn+") == 0)
1549 {
1550 /* GDB supports relocate instruction requests. */
1551 gdb_supports_qRelocInsn = 1;
1552 }
1553 else
1554 target_process_qsupported (p);
1555
1556 free (p);
1557 }
1558
1559 free (qsupported);
1560 }
1561
1562 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
1563
1564 if (the_target->qxfer_libraries_svr4 != NULL)
1565 strcat (own_buf, ";qXfer:libraries-svr4:read+");
1566 else
1567 {
1568 /* We do not have any hook to indicate whether the non-SVR4 target
1569 backend supports qXfer:libraries:read, so always report it. */
1570 strcat (own_buf, ";qXfer:libraries:read+");
1571 }
1572
1573 if (the_target->read_auxv != NULL)
1574 strcat (own_buf, ";qXfer:auxv:read+");
1575
1576 if (the_target->qxfer_spu != NULL)
1577 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
1578
1579 if (the_target->qxfer_siginfo != NULL)
1580 strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1581
1582 if (the_target->read_loadmap != NULL)
1583 strcat (own_buf, ";qXfer:fdpic:read+");
1584
1585 /* We always report qXfer:features:read, as targets may
1586 install XML files on a subsequent call to arch_setup.
1587 If we reported to GDB on startup that we don't support
1588 qXfer:feature:read at all, we will never be re-queried. */
1589 strcat (own_buf, ";qXfer:features:read+");
1590
1591 if (transport_is_reliable)
1592 strcat (own_buf, ";QStartNoAckMode+");
1593
1594 if (the_target->qxfer_osdata != NULL)
1595 strcat (own_buf, ";qXfer:osdata:read+");
1596
1597 if (target_supports_multi_process ())
1598 strcat (own_buf, ";multiprocess+");
1599
1600 if (target_supports_non_stop ())
1601 strcat (own_buf, ";QNonStop+");
1602
1603 if (target_supports_disable_randomization ())
1604 strcat (own_buf, ";QDisableRandomization+");
1605
1606 strcat (own_buf, ";qXfer:threads:read+");
1607
1608 if (target_supports_tracepoints ())
1609 {
1610 strcat (own_buf, ";ConditionalTracepoints+");
1611 strcat (own_buf, ";TraceStateVariables+");
1612 strcat (own_buf, ";TracepointSource+");
1613 strcat (own_buf, ";DisconnectedTracing+");
1614 if (gdb_supports_qRelocInsn && target_supports_fast_tracepoints ())
1615 strcat (own_buf, ";FastTracepoints+");
1616 strcat (own_buf, ";StaticTracepoints+");
1617 strcat (own_buf, ";InstallInTrace+");
1618 strcat (own_buf, ";qXfer:statictrace:read+");
1619 strcat (own_buf, ";qXfer:traceframe-info:read+");
1620 strcat (own_buf, ";EnableDisableTracepoints+");
1621 strcat (own_buf, ";tracenz+");
1622 }
1623
1624 /* Support target-side breakpoint conditions. */
1625 strcat (own_buf, ";ConditionalBreakpoints+");
1626
1627 return;
1628 }
1629
1630 /* Thread-local storage support. */
1631 if (the_target->get_tls_address != NULL
1632 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
1633 {
1634 char *p = own_buf + 12;
1635 CORE_ADDR parts[2], address = 0;
1636 int i, err;
1637 ptid_t ptid = null_ptid;
1638
1639 require_running (own_buf);
1640
1641 for (i = 0; i < 3; i++)
1642 {
1643 char *p2;
1644 int len;
1645
1646 if (p == NULL)
1647 break;
1648
1649 p2 = strchr (p, ',');
1650 if (p2)
1651 {
1652 len = p2 - p;
1653 p2++;
1654 }
1655 else
1656 {
1657 len = strlen (p);
1658 p2 = NULL;
1659 }
1660
1661 if (i == 0)
1662 ptid = read_ptid (p, NULL);
1663 else
1664 decode_address (&parts[i - 1], p, len);
1665 p = p2;
1666 }
1667
1668 if (p != NULL || i < 3)
1669 err = 1;
1670 else
1671 {
1672 struct thread_info *thread = find_thread_ptid (ptid);
1673
1674 if (thread == NULL)
1675 err = 2;
1676 else
1677 err = the_target->get_tls_address (thread, parts[0], parts[1],
1678 &address);
1679 }
1680
1681 if (err == 0)
1682 {
1683 strcpy (own_buf, paddress(address));
1684 return;
1685 }
1686 else if (err > 0)
1687 {
1688 write_enn (own_buf);
1689 return;
1690 }
1691
1692 /* Otherwise, pretend we do not understand this packet. */
1693 }
1694
1695 /* Windows OS Thread Information Block address support. */
1696 if (the_target->get_tib_address != NULL
1697 && strncmp ("qGetTIBAddr:", own_buf, 12) == 0)
1698 {
1699 char *annex;
1700 int n;
1701 CORE_ADDR tlb;
1702 ptid_t ptid = read_ptid (own_buf + 12, &annex);
1703
1704 n = (*the_target->get_tib_address) (ptid, &tlb);
1705 if (n == 1)
1706 {
1707 strcpy (own_buf, paddress(tlb));
1708 return;
1709 }
1710 else if (n == 0)
1711 {
1712 write_enn (own_buf);
1713 return;
1714 }
1715 return;
1716 }
1717
1718 /* Handle "monitor" commands. */
1719 if (strncmp ("qRcmd,", own_buf, 6) == 0)
1720 {
1721 char *mon = malloc (PBUFSIZ);
1722 int len = strlen (own_buf + 6);
1723
1724 if (mon == NULL)
1725 {
1726 write_enn (own_buf);
1727 return;
1728 }
1729
1730 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
1731 {
1732 write_enn (own_buf);
1733 free (mon);
1734 return;
1735 }
1736 mon[len / 2] = '\0';
1737
1738 write_ok (own_buf);
1739
1740 if (the_target->handle_monitor_command == NULL
1741 || (*the_target->handle_monitor_command) (mon) == 0)
1742 /* Default processing. */
1743 handle_monitor_command (mon, own_buf);
1744
1745 free (mon);
1746 return;
1747 }
1748
1749 if (strncmp ("qSearch:memory:", own_buf,
1750 sizeof ("qSearch:memory:") - 1) == 0)
1751 {
1752 require_running (own_buf);
1753 handle_search_memory (own_buf, packet_len);
1754 return;
1755 }
1756
1757 if (strcmp (own_buf, "qAttached") == 0
1758 || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1759 {
1760 struct process_info *process;
1761
1762 if (own_buf[sizeof ("qAttached") - 1])
1763 {
1764 int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16);
1765 process = (struct process_info *)
1766 find_inferior_id (&all_processes, pid_to_ptid (pid));
1767 }
1768 else
1769 {
1770 require_running (own_buf);
1771 process = current_process ();
1772 }
1773
1774 if (process == NULL)
1775 {
1776 write_enn (own_buf);
1777 return;
1778 }
1779
1780 strcpy (own_buf, process->attached ? "1" : "0");
1781 return;
1782 }
1783
1784 if (strncmp ("qCRC:", own_buf, 5) == 0)
1785 {
1786 /* CRC check (compare-section). */
1787 char *comma;
1788 CORE_ADDR base;
1789 int len;
1790 unsigned long long crc;
1791
1792 require_running (own_buf);
1793 base = strtoul (own_buf + 5, &comma, 16);
1794 if (*comma++ != ',')
1795 {
1796 write_enn (own_buf);
1797 return;
1798 }
1799 len = strtoul (comma, NULL, 16);
1800 crc = crc32 (base, len, 0xffffffff);
1801 /* Check for memory failure. */
1802 if (crc == (unsigned long long) -1)
1803 {
1804 write_enn (own_buf);
1805 return;
1806 }
1807 sprintf (own_buf, "C%lx", (unsigned long) crc);
1808 return;
1809 }
1810
1811 if (handle_qxfer (own_buf, packet_len, new_packet_len_p))
1812 return;
1813
1814 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf))
1815 return;
1816
1817 /* Otherwise we didn't know what packet it was. Say we didn't
1818 understand it. */
1819 own_buf[0] = 0;
1820 }
1821
1822 static void gdb_wants_all_threads_stopped (void);
1823
1824 /* Parse vCont packets. */
1825 void
1826 handle_v_cont (char *own_buf)
1827 {
1828 char *p, *q;
1829 int n = 0, i = 0;
1830 struct thread_resume *resume_info;
1831 struct thread_resume default_action = {{0}};
1832
1833 /* Count the number of semicolons in the packet. There should be one
1834 for every action. */
1835 p = &own_buf[5];
1836 while (p)
1837 {
1838 n++;
1839 p++;
1840 p = strchr (p, ';');
1841 }
1842
1843 resume_info = malloc (n * sizeof (resume_info[0]));
1844 if (resume_info == NULL)
1845 goto err;
1846
1847 p = &own_buf[5];
1848 while (*p)
1849 {
1850 p++;
1851
1852 if (p[0] == 's' || p[0] == 'S')
1853 resume_info[i].kind = resume_step;
1854 else if (p[0] == 'c' || p[0] == 'C')
1855 resume_info[i].kind = resume_continue;
1856 else if (p[0] == 't')
1857 resume_info[i].kind = resume_stop;
1858 else
1859 goto err;
1860
1861 if (p[0] == 'S' || p[0] == 'C')
1862 {
1863 int sig;
1864 sig = strtol (p + 1, &q, 16);
1865 if (p == q)
1866 goto err;
1867 p = q;
1868
1869 if (!target_signal_to_host_p (sig))
1870 goto err;
1871 resume_info[i].sig = target_signal_to_host (sig);
1872 }
1873 else
1874 {
1875 resume_info[i].sig = 0;
1876 p = p + 1;
1877 }
1878
1879 if (p[0] == 0)
1880 {
1881 resume_info[i].thread = minus_one_ptid;
1882 default_action = resume_info[i];
1883
1884 /* Note: we don't increment i here, we'll overwrite this entry
1885 the next time through. */
1886 }
1887 else if (p[0] == ':')
1888 {
1889 ptid_t ptid = read_ptid (p + 1, &q);
1890
1891 if (p == q)
1892 goto err;
1893 p = q;
1894 if (p[0] != ';' && p[0] != 0)
1895 goto err;
1896
1897 resume_info[i].thread = ptid;
1898
1899 i++;
1900 }
1901 }
1902
1903 if (i < n)
1904 resume_info[i] = default_action;
1905
1906 /* Still used in occasional places in the backend. */
1907 if (n == 1
1908 && !ptid_equal (resume_info[0].thread, minus_one_ptid)
1909 && resume_info[0].kind != resume_stop)
1910 cont_thread = resume_info[0].thread;
1911 else
1912 cont_thread = minus_one_ptid;
1913 set_desired_inferior (0);
1914
1915 if (!non_stop)
1916 enable_async_io ();
1917
1918 (*the_target->resume) (resume_info, n);
1919
1920 free (resume_info);
1921
1922 if (non_stop)
1923 write_ok (own_buf);
1924 else
1925 {
1926 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
1927
1928 if (last_status.kind != TARGET_WAITKIND_EXITED
1929 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
1930 current_inferior->last_status = last_status;
1931
1932 /* From the client's perspective, all-stop mode always stops all
1933 threads implicitly (and the target backend has already done
1934 so by now). Tag all threads as "want-stopped", so we don't
1935 resume them implicitly without the client telling us to. */
1936 gdb_wants_all_threads_stopped ();
1937 prepare_resume_reply (own_buf, last_ptid, &last_status);
1938 disable_async_io ();
1939
1940 if (last_status.kind == TARGET_WAITKIND_EXITED
1941 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
1942 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid)));
1943 }
1944 return;
1945
1946 err:
1947 write_enn (own_buf);
1948 free (resume_info);
1949 return;
1950 }
1951
1952 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1953 int
1954 handle_v_attach (char *own_buf)
1955 {
1956 int pid;
1957
1958 pid = strtol (own_buf + 8, NULL, 16);
1959 if (pid != 0 && attach_inferior (pid) == 0)
1960 {
1961 /* Don't report shared library events after attaching, even if
1962 some libraries are preloaded. GDB will always poll the
1963 library list. Avoids the "stopped by shared library event"
1964 notice on the GDB side. */
1965 dlls_changed = 0;
1966
1967 if (non_stop)
1968 {
1969 /* In non-stop, we don't send a resume reply. Stop events
1970 will follow up using the normal notification
1971 mechanism. */
1972 write_ok (own_buf);
1973 }
1974 else
1975 prepare_resume_reply (own_buf, last_ptid, &last_status);
1976
1977 return 1;
1978 }
1979 else
1980 {
1981 write_enn (own_buf);
1982 return 0;
1983 }
1984 }
1985
1986 /* Run a new program. Return 1 if successful, 0 if failure. */
1987 static int
1988 handle_v_run (char *own_buf)
1989 {
1990 char *p, *next_p, **new_argv;
1991 int i, new_argc;
1992
1993 new_argc = 0;
1994 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
1995 {
1996 p++;
1997 new_argc++;
1998 }
1999
2000 new_argv = calloc (new_argc + 2, sizeof (char *));
2001 if (new_argv == NULL)
2002 {
2003 write_enn (own_buf);
2004 return 0;
2005 }
2006
2007 i = 0;
2008 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
2009 {
2010 next_p = strchr (p, ';');
2011 if (next_p == NULL)
2012 next_p = p + strlen (p);
2013
2014 if (i == 0 && p == next_p)
2015 new_argv[i] = NULL;
2016 else
2017 {
2018 /* FIXME: Fail request if out of memory instead of dying. */
2019 new_argv[i] = xmalloc (1 + (next_p - p) / 2);
2020 unhexify (new_argv[i], p, (next_p - p) / 2);
2021 new_argv[i][(next_p - p) / 2] = '\0';
2022 }
2023
2024 if (*next_p)
2025 next_p++;
2026 i++;
2027 }
2028 new_argv[i] = NULL;
2029
2030 if (new_argv[0] == NULL)
2031 {
2032 /* GDB didn't specify a program to run. Use the program from the
2033 last run with the new argument list. */
2034
2035 if (program_argv == NULL)
2036 {
2037 write_enn (own_buf);
2038 freeargv (new_argv);
2039 return 0;
2040 }
2041
2042 new_argv[0] = strdup (program_argv[0]);
2043 if (new_argv[0] == NULL)
2044 {
2045 write_enn (own_buf);
2046 freeargv (new_argv);
2047 return 0;
2048 }
2049 }
2050
2051 /* Free the old argv and install the new one. */
2052 freeargv (program_argv);
2053 program_argv = new_argv;
2054
2055 start_inferior (program_argv);
2056 if (last_status.kind == TARGET_WAITKIND_STOPPED)
2057 {
2058 prepare_resume_reply (own_buf, last_ptid, &last_status);
2059
2060 /* In non-stop, sending a resume reply doesn't set the general
2061 thread, but GDB assumes a vRun sets it (this is so GDB can
2062 query which is the main thread of the new inferior. */
2063 if (non_stop)
2064 general_thread = last_ptid;
2065
2066 return 1;
2067 }
2068 else
2069 {
2070 write_enn (own_buf);
2071 return 0;
2072 }
2073 }
2074
2075 /* Kill process. Return 1 if successful, 0 if failure. */
2076 int
2077 handle_v_kill (char *own_buf)
2078 {
2079 int pid;
2080 char *p = &own_buf[6];
2081 if (multi_process)
2082 pid = strtol (p, NULL, 16);
2083 else
2084 pid = signal_pid;
2085 if (pid != 0 && kill_inferior (pid) == 0)
2086 {
2087 last_status.kind = TARGET_WAITKIND_SIGNALLED;
2088 last_status.value.sig = TARGET_SIGNAL_KILL;
2089 last_ptid = pid_to_ptid (pid);
2090 discard_queued_stop_replies (pid);
2091 write_ok (own_buf);
2092 return 1;
2093 }
2094 else
2095 {
2096 write_enn (own_buf);
2097 return 0;
2098 }
2099 }
2100
2101 /* Handle a 'vStopped' packet. */
2102 static void
2103 handle_v_stopped (char *own_buf)
2104 {
2105 /* If we're waiting for GDB to acknowledge a pending stop reply,
2106 consider that done. */
2107 if (notif_queue)
2108 {
2109 struct vstop_notif *head;
2110
2111 if (remote_debug)
2112 fprintf (stderr, "vStopped: acking %s\n",
2113 target_pid_to_str (notif_queue->ptid));
2114
2115 head = notif_queue;
2116 notif_queue = notif_queue->next;
2117 free (head);
2118 }
2119
2120 /* Push another stop reply, or if there are no more left, an OK. */
2121 send_next_stop_reply (own_buf);
2122 }
2123
2124 /* Handle all of the extended 'v' packets. */
2125 void
2126 handle_v_requests (char *own_buf, int packet_len, int *new_packet_len)
2127 {
2128 if (!disable_packet_vCont)
2129 {
2130 if (strncmp (own_buf, "vCont;", 6) == 0)
2131 {
2132 require_running (own_buf);
2133 handle_v_cont (own_buf);
2134 return;
2135 }
2136
2137 if (strncmp (own_buf, "vCont?", 6) == 0)
2138 {
2139 strcpy (own_buf, "vCont;c;C;s;S;t");
2140 return;
2141 }
2142 }
2143
2144 if (strncmp (own_buf, "vFile:", 6) == 0
2145 && handle_vFile (own_buf, packet_len, new_packet_len))
2146 return;
2147
2148 if (strncmp (own_buf, "vAttach;", 8) == 0)
2149 {
2150 if ((!extended_protocol || !multi_process) && target_running ())
2151 {
2152 fprintf (stderr, "Already debugging a process\n");
2153 write_enn (own_buf);
2154 return;
2155 }
2156 handle_v_attach (own_buf);
2157 return;
2158 }
2159
2160 if (strncmp (own_buf, "vRun;", 5) == 0)
2161 {
2162 if ((!extended_protocol || !multi_process) && target_running ())
2163 {
2164 fprintf (stderr, "Already debugging a process\n");
2165 write_enn (own_buf);
2166 return;
2167 }
2168 handle_v_run (own_buf);
2169 return;
2170 }
2171
2172 if (strncmp (own_buf, "vKill;", 6) == 0)
2173 {
2174 if (!target_running ())
2175 {
2176 fprintf (stderr, "No process to kill\n");
2177 write_enn (own_buf);
2178 return;
2179 }
2180 handle_v_kill (own_buf);
2181 return;
2182 }
2183
2184 if (strncmp (own_buf, "vStopped", 8) == 0)
2185 {
2186 handle_v_stopped (own_buf);
2187 return;
2188 }
2189
2190 /* Otherwise we didn't know what packet it was. Say we didn't
2191 understand it. */
2192 own_buf[0] = 0;
2193 return;
2194 }
2195
2196 /* Resume inferior and wait for another event. In non-stop mode,
2197 don't really wait here, but return immediatelly to the event
2198 loop. */
2199 static void
2200 myresume (char *own_buf, int step, int sig)
2201 {
2202 struct thread_resume resume_info[2];
2203 int n = 0;
2204 int valid_cont_thread;
2205
2206 set_desired_inferior (0);
2207
2208 valid_cont_thread = (!ptid_equal (cont_thread, null_ptid)
2209 && !ptid_equal (cont_thread, minus_one_ptid));
2210
2211 if (step || sig || valid_cont_thread)
2212 {
2213 resume_info[0].thread
2214 = ((struct inferior_list_entry *) current_inferior)->id;
2215 if (step)
2216 resume_info[0].kind = resume_step;
2217 else
2218 resume_info[0].kind = resume_continue;
2219 resume_info[0].sig = sig;
2220 n++;
2221 }
2222
2223 if (!valid_cont_thread)
2224 {
2225 resume_info[n].thread = minus_one_ptid;
2226 resume_info[n].kind = resume_continue;
2227 resume_info[n].sig = 0;
2228 n++;
2229 }
2230
2231 if (!non_stop)
2232 enable_async_io ();
2233
2234 (*the_target->resume) (resume_info, n);
2235
2236 if (non_stop)
2237 write_ok (own_buf);
2238 else
2239 {
2240 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
2241
2242 if (last_status.kind != TARGET_WAITKIND_EXITED
2243 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
2244 {
2245 current_inferior->last_resume_kind = resume_stop;
2246 current_inferior->last_status = last_status;
2247 }
2248
2249 prepare_resume_reply (own_buf, last_ptid, &last_status);
2250 disable_async_io ();
2251
2252 if (last_status.kind == TARGET_WAITKIND_EXITED
2253 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2254 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid)));
2255 }
2256 }
2257
2258 /* Callback for for_each_inferior. Make a new stop reply for each
2259 stopped thread. */
2260
2261 static int
2262 queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg)
2263 {
2264 struct thread_info *thread = (struct thread_info *) entry;
2265
2266 /* For now, assume targets that don't have this callback also don't
2267 manage the thread's last_status field. */
2268 if (the_target->thread_stopped == NULL)
2269 {
2270 /* Pass the last stop reply back to GDB, but don't notify
2271 yet. */
2272 queue_stop_reply (entry->id, &thread->last_status);
2273 }
2274 else
2275 {
2276 if (thread_stopped (thread))
2277 {
2278 if (debug_threads)
2279 fprintf (stderr,
2280 "Reporting thread %s as already stopped with %s\n",
2281 target_pid_to_str (entry->id),
2282 target_waitstatus_to_string (&thread->last_status));
2283
2284 gdb_assert (thread->last_status.kind != TARGET_WAITKIND_IGNORE);
2285
2286 /* Pass the last stop reply back to GDB, but don't notify
2287 yet. */
2288 queue_stop_reply (entry->id, &thread->last_status);
2289 }
2290 }
2291
2292 return 0;
2293 }
2294
2295 /* Set this inferior threads's state as "want-stopped". We won't
2296 resume this thread until the client gives us another action for
2297 it. */
2298
2299 static void
2300 gdb_wants_thread_stopped (struct inferior_list_entry *entry)
2301 {
2302 struct thread_info *thread = (struct thread_info *) entry;
2303
2304 thread->last_resume_kind = resume_stop;
2305
2306 if (thread->last_status.kind == TARGET_WAITKIND_IGNORE)
2307 {
2308 /* Most threads are stopped implicitly (all-stop); tag that with
2309 signal 0. */
2310 thread->last_status.kind = TARGET_WAITKIND_STOPPED;
2311 thread->last_status.value.sig = TARGET_SIGNAL_0;
2312 }
2313 }
2314
2315 /* Set all threads' states as "want-stopped". */
2316
2317 static void
2318 gdb_wants_all_threads_stopped (void)
2319 {
2320 for_each_inferior (&all_threads, gdb_wants_thread_stopped);
2321 }
2322
2323 /* Clear the gdb_detached flag of every process. */
2324
2325 static void
2326 gdb_reattached_process (struct inferior_list_entry *entry)
2327 {
2328 struct process_info *process = (struct process_info *) entry;
2329
2330 process->gdb_detached = 0;
2331 }
2332
2333 /* Status handler for the '?' packet. */
2334
2335 static void
2336 handle_status (char *own_buf)
2337 {
2338 /* GDB is connected, don't forward events to the target anymore. */
2339 for_each_inferior (&all_processes, gdb_reattached_process);
2340
2341 /* In non-stop mode, we must send a stop reply for each stopped
2342 thread. In all-stop mode, just send one for the first stopped
2343 thread we find. */
2344
2345 if (non_stop)
2346 {
2347 discard_queued_stop_replies (-1);
2348 find_inferior (&all_threads, queue_stop_reply_callback, NULL);
2349
2350 /* The first is sent immediatly. OK is sent if there is no
2351 stopped thread, which is the same handling of the vStopped
2352 packet (by design). */
2353 send_next_stop_reply (own_buf);
2354 }
2355 else
2356 {
2357 pause_all (0);
2358 stabilize_threads ();
2359 gdb_wants_all_threads_stopped ();
2360
2361 if (all_threads.head)
2362 {
2363 struct target_waitstatus status;
2364
2365 status.kind = TARGET_WAITKIND_STOPPED;
2366 status.value.sig = TARGET_SIGNAL_TRAP;
2367 prepare_resume_reply (own_buf,
2368 all_threads.head->id, &status);
2369 }
2370 else
2371 strcpy (own_buf, "W00");
2372 }
2373 }
2374
2375 static void
2376 gdbserver_version (void)
2377 {
2378 printf ("GNU gdbserver %s%s\n"
2379 "Copyright (C) 2012 Free Software Foundation, Inc.\n"
2380 "gdbserver is free software, covered by the "
2381 "GNU General Public License.\n"
2382 "This gdbserver was configured as \"%s\"\n",
2383 PKGVERSION, version, host_name);
2384 }
2385
2386 static void
2387 gdbserver_usage (FILE *stream)
2388 {
2389 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2390 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2391 "\tgdbserver [OPTIONS] --multi COMM\n"
2392 "\n"
2393 "COMM may either be a tty device (for serial debugging), or \n"
2394 "HOST:PORT to listen for a TCP connection.\n"
2395 "\n"
2396 "Options:\n"
2397 " --debug Enable general debugging output.\n"
2398 " --remote-debug Enable remote protocol debugging output.\n"
2399 " --version Display version information and exit.\n"
2400 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2401 " --once Exit after the first connection has "
2402 "closed.\n");
2403 if (REPORT_BUGS_TO[0] && stream == stdout)
2404 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
2405 }
2406
2407 static void
2408 gdbserver_show_disableable (FILE *stream)
2409 {
2410 fprintf (stream, "Disableable packets:\n"
2411 " vCont \tAll vCont packets\n"
2412 " qC \tQuerying the current thread\n"
2413 " qfThreadInfo\tThread listing\n"
2414 " Tthread \tPassing the thread specifier in the "
2415 "T stop reply packet\n"
2416 " threads \tAll of the above\n");
2417 }
2418
2419
2420 #undef require_running
2421 #define require_running(BUF) \
2422 if (!target_running ()) \
2423 { \
2424 write_enn (BUF); \
2425 break; \
2426 }
2427
2428 static int
2429 first_thread_of (struct inferior_list_entry *entry, void *args)
2430 {
2431 int pid = * (int *) args;
2432
2433 if (ptid_get_pid (entry->id) == pid)
2434 return 1;
2435
2436 return 0;
2437 }
2438
2439 static void
2440 kill_inferior_callback (struct inferior_list_entry *entry)
2441 {
2442 struct process_info *process = (struct process_info *) entry;
2443 int pid = ptid_get_pid (process->head.id);
2444
2445 kill_inferior (pid);
2446 discard_queued_stop_replies (pid);
2447 }
2448
2449 /* Callback for for_each_inferior to detach or kill the inferior,
2450 depending on whether we attached to it or not.
2451 We inform the user whether we're detaching or killing the process
2452 as this is only called when gdbserver is about to exit. */
2453
2454 static void
2455 detach_or_kill_inferior_callback (struct inferior_list_entry *entry)
2456 {
2457 struct process_info *process = (struct process_info *) entry;
2458 int pid = ptid_get_pid (process->head.id);
2459
2460 if (process->attached)
2461 detach_inferior (pid);
2462 else
2463 kill_inferior (pid);
2464
2465 discard_queued_stop_replies (pid);
2466 }
2467
2468 /* for_each_inferior callback for detach_or_kill_for_exit to print
2469 the pids of started inferiors. */
2470
2471 static void
2472 print_started_pid (struct inferior_list_entry *entry)
2473 {
2474 struct process_info *process = (struct process_info *) entry;
2475
2476 if (! process->attached)
2477 {
2478 int pid = ptid_get_pid (process->head.id);
2479 fprintf (stderr, " %d", pid);
2480 }
2481 }
2482
2483 /* for_each_inferior callback for detach_or_kill_for_exit to print
2484 the pids of attached inferiors. */
2485
2486 static void
2487 print_attached_pid (struct inferior_list_entry *entry)
2488 {
2489 struct process_info *process = (struct process_info *) entry;
2490
2491 if (process->attached)
2492 {
2493 int pid = ptid_get_pid (process->head.id);
2494 fprintf (stderr, " %d", pid);
2495 }
2496 }
2497
2498 /* Call this when exiting gdbserver with possible inferiors that need
2499 to be killed or detached from. */
2500
2501 static void
2502 detach_or_kill_for_exit (void)
2503 {
2504 /* First print a list of the inferiors we will be killing/detaching.
2505 This is to assist the user, for example, in case the inferior unexpectedly
2506 dies after we exit: did we screw up or did the inferior exit on its own?
2507 Having this info will save some head-scratching. */
2508
2509 if (have_started_inferiors_p ())
2510 {
2511 fprintf (stderr, "Killing process(es):");
2512 for_each_inferior (&all_processes, print_started_pid);
2513 fprintf (stderr, "\n");
2514 }
2515 if (have_attached_inferiors_p ())
2516 {
2517 fprintf (stderr, "Detaching process(es):");
2518 for_each_inferior (&all_processes, print_attached_pid);
2519 fprintf (stderr, "\n");
2520 }
2521
2522 /* Now we can kill or detach the inferiors. */
2523
2524 for_each_inferior (&all_processes, detach_or_kill_inferior_callback);
2525 }
2526
2527 int
2528 main (int argc, char *argv[])
2529 {
2530 int bad_attach;
2531 int pid;
2532 char *arg_end, *port;
2533 char **next_arg = &argv[1];
2534 int multi_mode = 0;
2535 int attach = 0;
2536 int was_running;
2537
2538 while (*next_arg != NULL && **next_arg == '-')
2539 {
2540 if (strcmp (*next_arg, "--version") == 0)
2541 {
2542 gdbserver_version ();
2543 exit (0);
2544 }
2545 else if (strcmp (*next_arg, "--help") == 0)
2546 {
2547 gdbserver_usage (stdout);
2548 exit (0);
2549 }
2550 else if (strcmp (*next_arg, "--attach") == 0)
2551 attach = 1;
2552 else if (strcmp (*next_arg, "--multi") == 0)
2553 multi_mode = 1;
2554 else if (strcmp (*next_arg, "--wrapper") == 0)
2555 {
2556 next_arg++;
2557
2558 wrapper_argv = next_arg;
2559 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
2560 next_arg++;
2561
2562 if (next_arg == wrapper_argv || *next_arg == NULL)
2563 {
2564 gdbserver_usage (stderr);
2565 exit (1);
2566 }
2567
2568 /* Consume the "--". */
2569 *next_arg = NULL;
2570 }
2571 else if (strcmp (*next_arg, "--debug") == 0)
2572 debug_threads = 1;
2573 else if (strcmp (*next_arg, "--remote-debug") == 0)
2574 remote_debug = 1;
2575 else if (strcmp (*next_arg, "--disable-packet") == 0)
2576 {
2577 gdbserver_show_disableable (stdout);
2578 exit (0);
2579 }
2580 else if (strncmp (*next_arg,
2581 "--disable-packet=",
2582 sizeof ("--disable-packet=") - 1) == 0)
2583 {
2584 char *packets, *tok;
2585
2586 packets = *next_arg += sizeof ("--disable-packet=") - 1;
2587 for (tok = strtok (packets, ",");
2588 tok != NULL;
2589 tok = strtok (NULL, ","))
2590 {
2591 if (strcmp ("vCont", tok) == 0)
2592 disable_packet_vCont = 1;
2593 else if (strcmp ("Tthread", tok) == 0)
2594 disable_packet_Tthread = 1;
2595 else if (strcmp ("qC", tok) == 0)
2596 disable_packet_qC = 1;
2597 else if (strcmp ("qfThreadInfo", tok) == 0)
2598 disable_packet_qfThreadInfo = 1;
2599 else if (strcmp ("threads", tok) == 0)
2600 {
2601 disable_packet_vCont = 1;
2602 disable_packet_Tthread = 1;
2603 disable_packet_qC = 1;
2604 disable_packet_qfThreadInfo = 1;
2605 }
2606 else
2607 {
2608 fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
2609 tok);
2610 gdbserver_show_disableable (stderr);
2611 exit (1);
2612 }
2613 }
2614 }
2615 else if (strcmp (*next_arg, "-") == 0)
2616 {
2617 /* "-" specifies a stdio connection and is a form of port
2618 specification. */
2619 *next_arg = STDIO_CONNECTION_NAME;
2620 break;
2621 }
2622 else if (strcmp (*next_arg, "--disable-randomization") == 0)
2623 disable_randomization = 1;
2624 else if (strcmp (*next_arg, "--no-disable-randomization") == 0)
2625 disable_randomization = 0;
2626 else if (strcmp (*next_arg, "--once") == 0)
2627 run_once = 1;
2628 else
2629 {
2630 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
2631 exit (1);
2632 }
2633
2634 next_arg++;
2635 continue;
2636 }
2637
2638 if (setjmp (toplevel))
2639 {
2640 fprintf (stderr, "Exiting\n");
2641 exit (1);
2642 }
2643
2644 port = *next_arg;
2645 next_arg++;
2646 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
2647 {
2648 gdbserver_usage (stderr);
2649 exit (1);
2650 }
2651
2652 /* We need to know whether the remote connection is stdio before
2653 starting the inferior. Inferiors created in this scenario have
2654 stdin,stdout redirected. So do this here before we call
2655 start_inferior. */
2656 remote_prepare (port);
2657
2658 bad_attach = 0;
2659 pid = 0;
2660
2661 /* --attach used to come after PORT, so allow it there for
2662 compatibility. */
2663 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
2664 {
2665 attach = 1;
2666 next_arg++;
2667 }
2668
2669 if (attach
2670 && (*next_arg == NULL
2671 || (*next_arg)[0] == '\0'
2672 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
2673 || *arg_end != '\0'
2674 || next_arg[1] != NULL))
2675 bad_attach = 1;
2676
2677 if (bad_attach)
2678 {
2679 gdbserver_usage (stderr);
2680 exit (1);
2681 }
2682
2683 initialize_async_io ();
2684 initialize_low ();
2685 if (target_supports_tracepoints ())
2686 initialize_tracepoint ();
2687
2688 own_buf = xmalloc (PBUFSIZ + 1);
2689 mem_buf = xmalloc (PBUFSIZ);
2690
2691 if (pid == 0 && *next_arg != NULL)
2692 {
2693 int i, n;
2694
2695 n = argc - (next_arg - argv);
2696 program_argv = xmalloc (sizeof (char *) * (n + 1));
2697 for (i = 0; i < n; i++)
2698 program_argv[i] = xstrdup (next_arg[i]);
2699 program_argv[i] = NULL;
2700
2701 /* Wait till we are at first instruction in program. */
2702 start_inferior (program_argv);
2703
2704 /* We are now (hopefully) stopped at the first instruction of
2705 the target process. This assumes that the target process was
2706 successfully created. */
2707 }
2708 else if (pid != 0)
2709 {
2710 if (attach_inferior (pid) == -1)
2711 error ("Attaching not supported on this target");
2712
2713 /* Otherwise succeeded. */
2714 }
2715 else
2716 {
2717 last_status.kind = TARGET_WAITKIND_EXITED;
2718 last_status.value.integer = 0;
2719 last_ptid = minus_one_ptid;
2720 }
2721
2722 /* Don't report shared library events on the initial connection,
2723 even if some libraries are preloaded. Avoids the "stopped by
2724 shared library event" notice on gdb side. */
2725 dlls_changed = 0;
2726
2727 if (setjmp (toplevel))
2728 {
2729 /* If something fails and longjmps while detaching or killing
2730 inferiors, we'd end up here again, stuck in an infinite loop
2731 trap. Be sure that if that happens, we exit immediately
2732 instead. */
2733 if (setjmp (toplevel) == 0)
2734 detach_or_kill_for_exit ();
2735 else
2736 fprintf (stderr, "Detach or kill failed. Exiting\n");
2737 exit (1);
2738 }
2739
2740 if (last_status.kind == TARGET_WAITKIND_EXITED
2741 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2742 was_running = 0;
2743 else
2744 was_running = 1;
2745
2746 if (!was_running && !multi_mode)
2747 {
2748 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
2749 exit (1);
2750 }
2751
2752 while (1)
2753 {
2754 noack_mode = 0;
2755 multi_process = 0;
2756 /* Be sure we're out of tfind mode. */
2757 current_traceframe = -1;
2758
2759 remote_open (port);
2760
2761 if (setjmp (toplevel) != 0)
2762 {
2763 /* An error occurred. */
2764 if (response_needed)
2765 {
2766 write_enn (own_buf);
2767 putpkt (own_buf);
2768 }
2769 }
2770
2771 /* Wait for events. This will return when all event sources are
2772 removed from the event loop. */
2773 start_event_loop ();
2774
2775 /* If an exit was requested (using the "monitor exit" command),
2776 terminate now. The only other way to get here is for
2777 getpkt to fail; close the connection and reopen it at the
2778 top of the loop. */
2779
2780 if (exit_requested || run_once)
2781 {
2782 /* If something fails and longjmps while detaching or
2783 killing inferiors, we'd end up here again, stuck in an
2784 infinite loop trap. Be sure that if that happens, we
2785 exit immediately instead. */
2786 if (setjmp (toplevel) == 0)
2787 {
2788 detach_or_kill_for_exit ();
2789 exit (0);
2790 }
2791 else
2792 {
2793 fprintf (stderr, "Detach or kill failed. Exiting\n");
2794 exit (1);
2795 }
2796 }
2797
2798 fprintf (stderr,
2799 "Remote side has terminated connection. "
2800 "GDBserver will reopen the connection.\n");
2801
2802 if (tracing)
2803 {
2804 if (disconnected_tracing)
2805 {
2806 /* Try to enable non-stop/async mode, so we we can both
2807 wait for an async socket accept, and handle async
2808 target events simultaneously. There's also no point
2809 either in having the target always stop all threads,
2810 when we're going to pass signals down without
2811 informing GDB. */
2812 if (!non_stop)
2813 {
2814 if (start_non_stop (1))
2815 non_stop = 1;
2816
2817 /* Detaching implicitly resumes all threads; simply
2818 disconnecting does not. */
2819 }
2820 }
2821 else
2822 {
2823 fprintf (stderr,
2824 "Disconnected tracing disabled; stopping trace run.\n");
2825 stop_tracing ();
2826 }
2827 }
2828 }
2829 }
2830
2831 /* Process options coming from Z packets for *point at address
2832 POINT_ADDR. PACKET is the packet buffer. *PACKET is updated
2833 to point to the first char after the last processed option. */
2834
2835 static void
2836 process_point_options (CORE_ADDR point_addr, char **packet)
2837 {
2838 char *dataptr = *packet;
2839
2840 /* Check if data has the correct format. */
2841 if (*dataptr != ';')
2842 return;
2843
2844 dataptr++;
2845
2846 while (*dataptr)
2847 {
2848 switch (*dataptr)
2849 {
2850 case 'X':
2851 /* Conditional expression. */
2852 fprintf (stderr, "Found breakpoint condition.\n");
2853 add_breakpoint_condition (point_addr, &dataptr);
2854 break;
2855 default:
2856 /* Unrecognized token, just skip it. */
2857 fprintf (stderr, "Unknown token %c, ignoring.\n",
2858 *dataptr);
2859 }
2860
2861 /* Skip tokens until we find one that we recognize. */
2862 while (*dataptr && *dataptr != 'X' && *dataptr != ';')
2863 dataptr++;
2864 }
2865 *packet = dataptr;
2866 }
2867
2868 /* Event loop callback that handles a serial event. The first byte in
2869 the serial buffer gets us here. We expect characters to arrive at
2870 a brisk pace, so we read the rest of the packet with a blocking
2871 getpkt call. */
2872
2873 static int
2874 process_serial_event (void)
2875 {
2876 char ch;
2877 int i = 0;
2878 int signal;
2879 unsigned int len;
2880 int res;
2881 CORE_ADDR mem_addr;
2882 int pid;
2883 unsigned char sig;
2884 int packet_len;
2885 int new_packet_len = -1;
2886
2887 /* Used to decide when gdbserver should exit in
2888 multi-mode/remote. */
2889 static int have_ran = 0;
2890
2891 if (!have_ran)
2892 have_ran = target_running ();
2893
2894 disable_async_io ();
2895
2896 response_needed = 0;
2897 packet_len = getpkt (own_buf);
2898 if (packet_len <= 0)
2899 {
2900 remote_close ();
2901 /* Force an event loop break. */
2902 return -1;
2903 }
2904 response_needed = 1;
2905
2906 i = 0;
2907 ch = own_buf[i++];
2908 switch (ch)
2909 {
2910 case 'q':
2911 handle_query (own_buf, packet_len, &new_packet_len);
2912 break;
2913 case 'Q':
2914 handle_general_set (own_buf);
2915 break;
2916 case 'D':
2917 require_running (own_buf);
2918
2919 if (multi_process)
2920 {
2921 i++; /* skip ';' */
2922 pid = strtol (&own_buf[i], NULL, 16);
2923 }
2924 else
2925 pid =
2926 ptid_get_pid (((struct inferior_list_entry *) current_inferior)->id);
2927
2928 if (tracing && disconnected_tracing)
2929 {
2930 struct thread_resume resume_info;
2931 struct process_info *process = find_process_pid (pid);
2932
2933 if (process == NULL)
2934 {
2935 write_enn (own_buf);
2936 break;
2937 }
2938
2939 fprintf (stderr,
2940 "Disconnected tracing in effect, "
2941 "leaving gdbserver attached to the process\n");
2942
2943 /* Make sure we're in non-stop/async mode, so we we can both
2944 wait for an async socket accept, and handle async target
2945 events simultaneously. There's also no point either in
2946 having the target stop all threads, when we're going to
2947 pass signals down without informing GDB. */
2948 if (!non_stop)
2949 {
2950 if (debug_threads)
2951 fprintf (stderr, "Forcing non-stop mode\n");
2952
2953 non_stop = 1;
2954 start_non_stop (1);
2955 }
2956
2957 process->gdb_detached = 1;
2958
2959 /* Detaching implicitly resumes all threads. */
2960 resume_info.thread = minus_one_ptid;
2961 resume_info.kind = resume_continue;
2962 resume_info.sig = 0;
2963 (*the_target->resume) (&resume_info, 1);
2964
2965 write_ok (own_buf);
2966 break; /* from switch/case */
2967 }
2968
2969 fprintf (stderr, "Detaching from process %d\n", pid);
2970 stop_tracing ();
2971 if (detach_inferior (pid) != 0)
2972 write_enn (own_buf);
2973 else
2974 {
2975 discard_queued_stop_replies (pid);
2976 write_ok (own_buf);
2977
2978 if (extended_protocol)
2979 {
2980 /* Treat this like a normal program exit. */
2981 last_status.kind = TARGET_WAITKIND_EXITED;
2982 last_status.value.integer = 0;
2983 last_ptid = pid_to_ptid (pid);
2984
2985 current_inferior = NULL;
2986 }
2987 else
2988 {
2989 putpkt (own_buf);
2990 remote_close ();
2991
2992 /* If we are attached, then we can exit. Otherwise, we
2993 need to hang around doing nothing, until the child is
2994 gone. */
2995 join_inferior (pid);
2996 exit (0);
2997 }
2998 }
2999 break;
3000 case '!':
3001 extended_protocol = 1;
3002 write_ok (own_buf);
3003 break;
3004 case '?':
3005 handle_status (own_buf);
3006 break;
3007 case 'H':
3008 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
3009 {
3010 ptid_t gdb_id, thread_id;
3011 int pid;
3012
3013 require_running (own_buf);
3014
3015 gdb_id = read_ptid (&own_buf[2], NULL);
3016
3017 pid = ptid_get_pid (gdb_id);
3018
3019 if (ptid_equal (gdb_id, null_ptid)
3020 || ptid_equal (gdb_id, minus_one_ptid))
3021 thread_id = null_ptid;
3022 else if (pid != 0
3023 && ptid_equal (pid_to_ptid (pid),
3024 gdb_id))
3025 {
3026 struct thread_info *thread =
3027 (struct thread_info *) find_inferior (&all_threads,
3028 first_thread_of,
3029 &pid);
3030 if (!thread)
3031 {
3032 write_enn (own_buf);
3033 break;
3034 }
3035
3036 thread_id = ((struct inferior_list_entry *)thread)->id;
3037 }
3038 else
3039 {
3040 thread_id = gdb_id_to_thread_id (gdb_id);
3041 if (ptid_equal (thread_id, null_ptid))
3042 {
3043 write_enn (own_buf);
3044 break;
3045 }
3046 }
3047
3048 if (own_buf[1] == 'g')
3049 {
3050 if (ptid_equal (thread_id, null_ptid))
3051 {
3052 /* GDB is telling us to choose any thread. Check if
3053 the currently selected thread is still valid. If
3054 it is not, select the first available. */
3055 struct thread_info *thread =
3056 (struct thread_info *) find_inferior_id (&all_threads,
3057 general_thread);
3058 if (thread == NULL)
3059 thread_id = all_threads.head->id;
3060 }
3061
3062 general_thread = thread_id;
3063 set_desired_inferior (1);
3064 }
3065 else if (own_buf[1] == 'c')
3066 cont_thread = thread_id;
3067
3068 write_ok (own_buf);
3069 }
3070 else
3071 {
3072 /* Silently ignore it so that gdb can extend the protocol
3073 without compatibility headaches. */
3074 own_buf[0] = '\0';
3075 }
3076 break;
3077 case 'g':
3078 require_running (own_buf);
3079 if (current_traceframe >= 0)
3080 {
3081 struct regcache *regcache = new_register_cache ();
3082
3083 if (fetch_traceframe_registers (current_traceframe,
3084 regcache, -1) == 0)
3085 registers_to_string (regcache, own_buf);
3086 else
3087 write_enn (own_buf);
3088 free_register_cache (regcache);
3089 }
3090 else
3091 {
3092 struct regcache *regcache;
3093
3094 set_desired_inferior (1);
3095 regcache = get_thread_regcache (current_inferior, 1);
3096 registers_to_string (regcache, own_buf);
3097 }
3098 break;
3099 case 'G':
3100 require_running (own_buf);
3101 if (current_traceframe >= 0)
3102 write_enn (own_buf);
3103 else
3104 {
3105 struct regcache *regcache;
3106
3107 set_desired_inferior (1);
3108 regcache = get_thread_regcache (current_inferior, 1);
3109 registers_from_string (regcache, &own_buf[1]);
3110 write_ok (own_buf);
3111 }
3112 break;
3113 case 'm':
3114 require_running (own_buf);
3115 decode_m_packet (&own_buf[1], &mem_addr, &len);
3116 res = gdb_read_memory (mem_addr, mem_buf, len);
3117 if (res < 0)
3118 write_enn (own_buf);
3119 else
3120 convert_int_to_ascii (mem_buf, own_buf, res);
3121 break;
3122 case 'M':
3123 require_running (own_buf);
3124 decode_M_packet (&own_buf[1], &mem_addr, &len, &mem_buf);
3125 if (gdb_write_memory (mem_addr, mem_buf, len) == 0)
3126 write_ok (own_buf);
3127 else
3128 write_enn (own_buf);
3129 break;
3130 case 'X':
3131 require_running (own_buf);
3132 if (decode_X_packet (&own_buf[1], packet_len - 1,
3133 &mem_addr, &len, &mem_buf) < 0
3134 || gdb_write_memory (mem_addr, mem_buf, len) != 0)
3135 write_enn (own_buf);
3136 else
3137 write_ok (own_buf);
3138 break;
3139 case 'C':
3140 require_running (own_buf);
3141 convert_ascii_to_int (own_buf + 1, &sig, 1);
3142 if (target_signal_to_host_p (sig))
3143 signal = target_signal_to_host (sig);
3144 else
3145 signal = 0;
3146 myresume (own_buf, 0, signal);
3147 break;
3148 case 'S':
3149 require_running (own_buf);
3150 convert_ascii_to_int (own_buf + 1, &sig, 1);
3151 if (target_signal_to_host_p (sig))
3152 signal = target_signal_to_host (sig);
3153 else
3154 signal = 0;
3155 myresume (own_buf, 1, signal);
3156 break;
3157 case 'c':
3158 require_running (own_buf);
3159 signal = 0;
3160 myresume (own_buf, 0, signal);
3161 break;
3162 case 's':
3163 require_running (own_buf);
3164 signal = 0;
3165 myresume (own_buf, 1, signal);
3166 break;
3167 case 'Z': /* insert_ ... */
3168 /* Fallthrough. */
3169 case 'z': /* remove_ ... */
3170 {
3171 char *lenptr;
3172 char *dataptr;
3173 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
3174 int len = strtol (lenptr + 1, &dataptr, 16);
3175 char type = own_buf[1];
3176 int res;
3177 const int insert = ch == 'Z';
3178
3179 /* Default to unrecognized/unsupported. */
3180 res = 1;
3181 switch (type)
3182 {
3183 case '0': /* software-breakpoint */
3184 case '1': /* hardware-breakpoint */
3185 case '2': /* write watchpoint */
3186 case '3': /* read watchpoint */
3187 case '4': /* access watchpoint */
3188 require_running (own_buf);
3189 if (insert && the_target->insert_point != NULL)
3190 {
3191 /* Insert the breakpoint. If it is already inserted, nothing
3192 will take place. */
3193 res = (*the_target->insert_point) (type, addr, len);
3194
3195 /* GDB may have sent us a list of *point parameters to be
3196 evaluated on the target's side. Read such list here. If we
3197 already have a list of parameters, GDB is telling us to drop
3198 that list and use this one instead. */
3199 if (!res && (type == '0' || type == '1'))
3200 {
3201 /* Remove previous conditions. */
3202 clear_gdb_breakpoint_conditions (addr);
3203 process_point_options (addr, &dataptr);
3204 }
3205 }
3206 else if (!insert && the_target->remove_point != NULL)
3207 res = (*the_target->remove_point) (type, addr, len);
3208 break;
3209 default:
3210 break;
3211 }
3212
3213 if (res == 0)
3214 write_ok (own_buf);
3215 else if (res == 1)
3216 /* Unsupported. */
3217 own_buf[0] = '\0';
3218 else
3219 write_enn (own_buf);
3220 break;
3221 }
3222 case 'k':
3223 response_needed = 0;
3224 if (!target_running ())
3225 /* The packet we received doesn't make sense - but we can't
3226 reply to it, either. */
3227 return 0;
3228
3229 fprintf (stderr, "Killing all inferiors\n");
3230 for_each_inferior (&all_processes, kill_inferior_callback);
3231
3232 /* When using the extended protocol, we wait with no program
3233 running. The traditional protocol will exit instead. */
3234 if (extended_protocol)
3235 {
3236 last_status.kind = TARGET_WAITKIND_EXITED;
3237 last_status.value.sig = TARGET_SIGNAL_KILL;
3238 return 0;
3239 }
3240 else
3241 exit (0);
3242
3243 case 'T':
3244 {
3245 ptid_t gdb_id, thread_id;
3246
3247 require_running (own_buf);
3248
3249 gdb_id = read_ptid (&own_buf[1], NULL);
3250 thread_id = gdb_id_to_thread_id (gdb_id);
3251 if (ptid_equal (thread_id, null_ptid))
3252 {
3253 write_enn (own_buf);
3254 break;
3255 }
3256
3257 if (mythread_alive (thread_id))
3258 write_ok (own_buf);
3259 else
3260 write_enn (own_buf);
3261 }
3262 break;
3263 case 'R':
3264 response_needed = 0;
3265
3266 /* Restarting the inferior is only supported in the extended
3267 protocol. */
3268 if (extended_protocol)
3269 {
3270 if (target_running ())
3271 for_each_inferior (&all_processes,
3272 kill_inferior_callback);
3273 fprintf (stderr, "GDBserver restarting\n");
3274
3275 /* Wait till we are at 1st instruction in prog. */
3276 if (program_argv != NULL)
3277 start_inferior (program_argv);
3278 else
3279 {
3280 last_status.kind = TARGET_WAITKIND_EXITED;
3281 last_status.value.sig = TARGET_SIGNAL_KILL;
3282 }
3283 return 0;
3284 }
3285 else
3286 {
3287 /* It is a request we don't understand. Respond with an
3288 empty packet so that gdb knows that we don't support this
3289 request. */
3290 own_buf[0] = '\0';
3291 break;
3292 }
3293 case 'v':
3294 /* Extended (long) request. */
3295 handle_v_requests (own_buf, packet_len, &new_packet_len);
3296 break;
3297
3298 default:
3299 /* It is a request we don't understand. Respond with an empty
3300 packet so that gdb knows that we don't support this
3301 request. */
3302 own_buf[0] = '\0';
3303 break;
3304 }
3305
3306 if (new_packet_len != -1)
3307 putpkt_binary (own_buf, new_packet_len);
3308 else
3309 putpkt (own_buf);
3310
3311 response_needed = 0;
3312
3313 if (!extended_protocol && have_ran && !target_running ())
3314 {
3315 /* In non-stop, defer exiting until GDB had a chance to query
3316 the whole vStopped list (until it gets an OK). */
3317 if (!notif_queue)
3318 {
3319 fprintf (stderr, "GDBserver exiting\n");
3320 remote_close ();
3321 exit (0);
3322 }
3323 }
3324
3325 if (exit_requested)
3326 return -1;
3327
3328 return 0;
3329 }
3330
3331 /* Event-loop callback for serial events. */
3332
3333 int
3334 handle_serial_event (int err, gdb_client_data client_data)
3335 {
3336 if (debug_threads)
3337 fprintf (stderr, "handling possible serial event\n");
3338
3339 /* Really handle it. */
3340 if (process_serial_event () < 0)
3341 return -1;
3342
3343 /* Be sure to not change the selected inferior behind GDB's back.
3344 Important in the non-stop mode asynchronous protocol. */
3345 set_desired_inferior (1);
3346
3347 return 0;
3348 }
3349
3350 /* Event-loop callback for target events. */
3351
3352 int
3353 handle_target_event (int err, gdb_client_data client_data)
3354 {
3355 if (debug_threads)
3356 fprintf (stderr, "handling possible target event\n");
3357
3358 last_ptid = mywait (minus_one_ptid, &last_status,
3359 TARGET_WNOHANG, 1);
3360
3361 if (last_status.kind != TARGET_WAITKIND_IGNORE)
3362 {
3363 int pid = ptid_get_pid (last_ptid);
3364 struct process_info *process = find_process_pid (pid);
3365 int forward_event = !gdb_connected () || process->gdb_detached;
3366
3367 if (last_status.kind == TARGET_WAITKIND_EXITED
3368 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
3369 {
3370 mark_breakpoints_out (process);
3371 mourn_inferior (process);
3372 }
3373 else
3374 {
3375 /* We're reporting this thread as stopped. Update its
3376 "want-stopped" state to what the client wants, until it
3377 gets a new resume action. */
3378 current_inferior->last_resume_kind = resume_stop;
3379 current_inferior->last_status = last_status;
3380 }
3381
3382 if (forward_event)
3383 {
3384 if (!target_running ())
3385 {
3386 /* The last process exited. We're done. */
3387 exit (0);
3388 }
3389
3390 if (last_status.kind == TARGET_WAITKIND_STOPPED)
3391 {
3392 /* A thread stopped with a signal, but gdb isn't
3393 connected to handle it. Pass it down to the
3394 inferior, as if it wasn't being traced. */
3395 struct thread_resume resume_info;
3396
3397 if (debug_threads)
3398 fprintf (stderr,
3399 "GDB not connected; forwarding event %d for [%s]\n",
3400 (int) last_status.kind,
3401 target_pid_to_str (last_ptid));
3402
3403 resume_info.thread = last_ptid;
3404 resume_info.kind = resume_continue;
3405 resume_info.sig = target_signal_to_host (last_status.value.sig);
3406 (*the_target->resume) (&resume_info, 1);
3407 }
3408 else if (debug_threads)
3409 fprintf (stderr, "GDB not connected; ignoring event %d for [%s]\n",
3410 (int) last_status.kind,
3411 target_pid_to_str (last_ptid));
3412 }
3413 else
3414 {
3415 /* Something interesting. Tell GDB about it. */
3416 push_event (last_ptid, &last_status);
3417 }
3418 }
3419
3420 /* Be sure to not change the selected inferior behind GDB's back.
3421 Important in the non-stop mode asynchronous protocol. */
3422 set_desired_inferior (1);
3423
3424 return 0;
3425 }