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