2012-01-17 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 /* 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)
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 return;
1625 }
1626
1627 /* Thread-local storage support. */
1628 if (the_target->get_tls_address != NULL
1629 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
1630 {
1631 char *p = own_buf + 12;
1632 CORE_ADDR parts[2], address = 0;
1633 int i, err;
1634 ptid_t ptid = null_ptid;
1635
1636 require_running (own_buf);
1637
1638 for (i = 0; i < 3; i++)
1639 {
1640 char *p2;
1641 int len;
1642
1643 if (p == NULL)
1644 break;
1645
1646 p2 = strchr (p, ',');
1647 if (p2)
1648 {
1649 len = p2 - p;
1650 p2++;
1651 }
1652 else
1653 {
1654 len = strlen (p);
1655 p2 = NULL;
1656 }
1657
1658 if (i == 0)
1659 ptid = read_ptid (p, NULL);
1660 else
1661 decode_address (&parts[i - 1], p, len);
1662 p = p2;
1663 }
1664
1665 if (p != NULL || i < 3)
1666 err = 1;
1667 else
1668 {
1669 struct thread_info *thread = find_thread_ptid (ptid);
1670
1671 if (thread == NULL)
1672 err = 2;
1673 else
1674 err = the_target->get_tls_address (thread, parts[0], parts[1],
1675 &address);
1676 }
1677
1678 if (err == 0)
1679 {
1680 strcpy (own_buf, paddress(address));
1681 return;
1682 }
1683 else if (err > 0)
1684 {
1685 write_enn (own_buf);
1686 return;
1687 }
1688
1689 /* Otherwise, pretend we do not understand this packet. */
1690 }
1691
1692 /* Windows OS Thread Information Block address support. */
1693 if (the_target->get_tib_address != NULL
1694 && strncmp ("qGetTIBAddr:", own_buf, 12) == 0)
1695 {
1696 char *annex;
1697 int n;
1698 CORE_ADDR tlb;
1699 ptid_t ptid = read_ptid (own_buf + 12, &annex);
1700
1701 n = (*the_target->get_tib_address) (ptid, &tlb);
1702 if (n == 1)
1703 {
1704 strcpy (own_buf, paddress(tlb));
1705 return;
1706 }
1707 else if (n == 0)
1708 {
1709 write_enn (own_buf);
1710 return;
1711 }
1712 return;
1713 }
1714
1715 /* Handle "monitor" commands. */
1716 if (strncmp ("qRcmd,", own_buf, 6) == 0)
1717 {
1718 char *mon = malloc (PBUFSIZ);
1719 int len = strlen (own_buf + 6);
1720
1721 if (mon == NULL)
1722 {
1723 write_enn (own_buf);
1724 return;
1725 }
1726
1727 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
1728 {
1729 write_enn (own_buf);
1730 free (mon);
1731 return;
1732 }
1733 mon[len / 2] = '\0';
1734
1735 write_ok (own_buf);
1736
1737 if (the_target->handle_monitor_command == NULL
1738 || (*the_target->handle_monitor_command) (mon) == 0)
1739 /* Default processing. */
1740 handle_monitor_command (mon);
1741
1742 free (mon);
1743 return;
1744 }
1745
1746 if (strncmp ("qSearch:memory:", own_buf,
1747 sizeof ("qSearch:memory:") - 1) == 0)
1748 {
1749 require_running (own_buf);
1750 handle_search_memory (own_buf, packet_len);
1751 return;
1752 }
1753
1754 if (strcmp (own_buf, "qAttached") == 0
1755 || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1756 {
1757 struct process_info *process;
1758
1759 if (own_buf[sizeof ("qAttached") - 1])
1760 {
1761 int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16);
1762 process = (struct process_info *)
1763 find_inferior_id (&all_processes, pid_to_ptid (pid));
1764 }
1765 else
1766 {
1767 require_running (own_buf);
1768 process = current_process ();
1769 }
1770
1771 if (process == NULL)
1772 {
1773 write_enn (own_buf);
1774 return;
1775 }
1776
1777 strcpy (own_buf, process->attached ? "1" : "0");
1778 return;
1779 }
1780
1781 if (strncmp ("qCRC:", own_buf, 5) == 0)
1782 {
1783 /* CRC check (compare-section). */
1784 char *comma;
1785 CORE_ADDR base;
1786 int len;
1787 unsigned long long crc;
1788
1789 require_running (own_buf);
1790 base = strtoul (own_buf + 5, &comma, 16);
1791 if (*comma++ != ',')
1792 {
1793 write_enn (own_buf);
1794 return;
1795 }
1796 len = strtoul (comma, NULL, 16);
1797 crc = crc32 (base, len, 0xffffffff);
1798 /* Check for memory failure. */
1799 if (crc == (unsigned long long) -1)
1800 {
1801 write_enn (own_buf);
1802 return;
1803 }
1804 sprintf (own_buf, "C%lx", (unsigned long) crc);
1805 return;
1806 }
1807
1808 if (handle_qxfer (own_buf, packet_len, new_packet_len_p))
1809 return;
1810
1811 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf))
1812 return;
1813
1814 /* Otherwise we didn't know what packet it was. Say we didn't
1815 understand it. */
1816 own_buf[0] = 0;
1817 }
1818
1819 static void gdb_wants_all_threads_stopped (void);
1820
1821 /* Parse vCont packets. */
1822 void
1823 handle_v_cont (char *own_buf)
1824 {
1825 char *p, *q;
1826 int n = 0, i = 0;
1827 struct thread_resume *resume_info;
1828 struct thread_resume default_action = {{0}};
1829
1830 /* Count the number of semicolons in the packet. There should be one
1831 for every action. */
1832 p = &own_buf[5];
1833 while (p)
1834 {
1835 n++;
1836 p++;
1837 p = strchr (p, ';');
1838 }
1839
1840 resume_info = malloc (n * sizeof (resume_info[0]));
1841 if (resume_info == NULL)
1842 goto err;
1843
1844 p = &own_buf[5];
1845 while (*p)
1846 {
1847 p++;
1848
1849 if (p[0] == 's' || p[0] == 'S')
1850 resume_info[i].kind = resume_step;
1851 else if (p[0] == 'c' || p[0] == 'C')
1852 resume_info[i].kind = resume_continue;
1853 else if (p[0] == 't')
1854 resume_info[i].kind = resume_stop;
1855 else
1856 goto err;
1857
1858 if (p[0] == 'S' || p[0] == 'C')
1859 {
1860 int sig;
1861 sig = strtol (p + 1, &q, 16);
1862 if (p == q)
1863 goto err;
1864 p = q;
1865
1866 if (!target_signal_to_host_p (sig))
1867 goto err;
1868 resume_info[i].sig = target_signal_to_host (sig);
1869 }
1870 else
1871 {
1872 resume_info[i].sig = 0;
1873 p = p + 1;
1874 }
1875
1876 if (p[0] == 0)
1877 {
1878 resume_info[i].thread = minus_one_ptid;
1879 default_action = resume_info[i];
1880
1881 /* Note: we don't increment i here, we'll overwrite this entry
1882 the next time through. */
1883 }
1884 else if (p[0] == ':')
1885 {
1886 ptid_t ptid = read_ptid (p + 1, &q);
1887
1888 if (p == q)
1889 goto err;
1890 p = q;
1891 if (p[0] != ';' && p[0] != 0)
1892 goto err;
1893
1894 resume_info[i].thread = ptid;
1895
1896 i++;
1897 }
1898 }
1899
1900 if (i < n)
1901 resume_info[i] = default_action;
1902
1903 /* Still used in occasional places in the backend. */
1904 if (n == 1
1905 && !ptid_equal (resume_info[0].thread, minus_one_ptid)
1906 && resume_info[0].kind != resume_stop)
1907 cont_thread = resume_info[0].thread;
1908 else
1909 cont_thread = minus_one_ptid;
1910 set_desired_inferior (0);
1911
1912 if (!non_stop)
1913 enable_async_io ();
1914
1915 (*the_target->resume) (resume_info, n);
1916
1917 free (resume_info);
1918
1919 if (non_stop)
1920 write_ok (own_buf);
1921 else
1922 {
1923 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
1924
1925 if (last_status.kind != TARGET_WAITKIND_EXITED
1926 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
1927 current_inferior->last_status = last_status;
1928
1929 /* From the client's perspective, all-stop mode always stops all
1930 threads implicitly (and the target backend has already done
1931 so by now). Tag all threads as "want-stopped", so we don't
1932 resume them implicitly without the client telling us to. */
1933 gdb_wants_all_threads_stopped ();
1934 prepare_resume_reply (own_buf, last_ptid, &last_status);
1935 disable_async_io ();
1936
1937 if (last_status.kind == TARGET_WAITKIND_EXITED
1938 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
1939 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid)));
1940 }
1941 return;
1942
1943 err:
1944 write_enn (own_buf);
1945 free (resume_info);
1946 return;
1947 }
1948
1949 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1950 int
1951 handle_v_attach (char *own_buf)
1952 {
1953 int pid;
1954
1955 pid = strtol (own_buf + 8, NULL, 16);
1956 if (pid != 0 && attach_inferior (pid) == 0)
1957 {
1958 /* Don't report shared library events after attaching, even if
1959 some libraries are preloaded. GDB will always poll the
1960 library list. Avoids the "stopped by shared library event"
1961 notice on the GDB side. */
1962 dlls_changed = 0;
1963
1964 if (non_stop)
1965 {
1966 /* In non-stop, we don't send a resume reply. Stop events
1967 will follow up using the normal notification
1968 mechanism. */
1969 write_ok (own_buf);
1970 }
1971 else
1972 prepare_resume_reply (own_buf, last_ptid, &last_status);
1973
1974 return 1;
1975 }
1976 else
1977 {
1978 write_enn (own_buf);
1979 return 0;
1980 }
1981 }
1982
1983 /* Run a new program. Return 1 if successful, 0 if failure. */
1984 static int
1985 handle_v_run (char *own_buf)
1986 {
1987 char *p, *next_p, **new_argv;
1988 int i, new_argc;
1989
1990 new_argc = 0;
1991 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
1992 {
1993 p++;
1994 new_argc++;
1995 }
1996
1997 new_argv = calloc (new_argc + 2, sizeof (char *));
1998 if (new_argv == NULL)
1999 {
2000 write_enn (own_buf);
2001 return 0;
2002 }
2003
2004 i = 0;
2005 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
2006 {
2007 next_p = strchr (p, ';');
2008 if (next_p == NULL)
2009 next_p = p + strlen (p);
2010
2011 if (i == 0 && p == next_p)
2012 new_argv[i] = NULL;
2013 else
2014 {
2015 /* FIXME: Fail request if out of memory instead of dying. */
2016 new_argv[i] = xmalloc (1 + (next_p - p) / 2);
2017 unhexify (new_argv[i], p, (next_p - p) / 2);
2018 new_argv[i][(next_p - p) / 2] = '\0';
2019 }
2020
2021 if (*next_p)
2022 next_p++;
2023 i++;
2024 }
2025 new_argv[i] = NULL;
2026
2027 if (new_argv[0] == NULL)
2028 {
2029 /* GDB didn't specify a program to run. Use the program from the
2030 last run with the new argument list. */
2031
2032 if (program_argv == NULL)
2033 {
2034 write_enn (own_buf);
2035 freeargv (new_argv);
2036 return 0;
2037 }
2038
2039 new_argv[0] = strdup (program_argv[0]);
2040 if (new_argv[0] == NULL)
2041 {
2042 write_enn (own_buf);
2043 freeargv (new_argv);
2044 return 0;
2045 }
2046 }
2047
2048 /* Free the old argv and install the new one. */
2049 freeargv (program_argv);
2050 program_argv = new_argv;
2051
2052 start_inferior (program_argv);
2053 if (last_status.kind == TARGET_WAITKIND_STOPPED)
2054 {
2055 prepare_resume_reply (own_buf, last_ptid, &last_status);
2056
2057 /* In non-stop, sending a resume reply doesn't set the general
2058 thread, but GDB assumes a vRun sets it (this is so GDB can
2059 query which is the main thread of the new inferior. */
2060 if (non_stop)
2061 general_thread = last_ptid;
2062
2063 return 1;
2064 }
2065 else
2066 {
2067 write_enn (own_buf);
2068 return 0;
2069 }
2070 }
2071
2072 /* Kill process. Return 1 if successful, 0 if failure. */
2073 int
2074 handle_v_kill (char *own_buf)
2075 {
2076 int pid;
2077 char *p = &own_buf[6];
2078 if (multi_process)
2079 pid = strtol (p, NULL, 16);
2080 else
2081 pid = signal_pid;
2082 if (pid != 0 && kill_inferior (pid) == 0)
2083 {
2084 last_status.kind = TARGET_WAITKIND_SIGNALLED;
2085 last_status.value.sig = TARGET_SIGNAL_KILL;
2086 last_ptid = pid_to_ptid (pid);
2087 discard_queued_stop_replies (pid);
2088 write_ok (own_buf);
2089 return 1;
2090 }
2091 else
2092 {
2093 write_enn (own_buf);
2094 return 0;
2095 }
2096 }
2097
2098 /* Handle a 'vStopped' packet. */
2099 static void
2100 handle_v_stopped (char *own_buf)
2101 {
2102 /* If we're waiting for GDB to acknowledge a pending stop reply,
2103 consider that done. */
2104 if (notif_queue)
2105 {
2106 struct vstop_notif *head;
2107
2108 if (remote_debug)
2109 fprintf (stderr, "vStopped: acking %s\n",
2110 target_pid_to_str (notif_queue->ptid));
2111
2112 head = notif_queue;
2113 notif_queue = notif_queue->next;
2114 free (head);
2115 }
2116
2117 /* Push another stop reply, or if there are no more left, an OK. */
2118 send_next_stop_reply (own_buf);
2119 }
2120
2121 /* Handle all of the extended 'v' packets. */
2122 void
2123 handle_v_requests (char *own_buf, int packet_len, int *new_packet_len)
2124 {
2125 if (!disable_packet_vCont)
2126 {
2127 if (strncmp (own_buf, "vCont;", 6) == 0)
2128 {
2129 require_running (own_buf);
2130 handle_v_cont (own_buf);
2131 return;
2132 }
2133
2134 if (strncmp (own_buf, "vCont?", 6) == 0)
2135 {
2136 strcpy (own_buf, "vCont;c;C;s;S;t");
2137 return;
2138 }
2139 }
2140
2141 if (strncmp (own_buf, "vFile:", 6) == 0
2142 && handle_vFile (own_buf, packet_len, new_packet_len))
2143 return;
2144
2145 if (strncmp (own_buf, "vAttach;", 8) == 0)
2146 {
2147 if (!multi_process && target_running ())
2148 {
2149 fprintf (stderr, "Already debugging a process\n");
2150 write_enn (own_buf);
2151 return;
2152 }
2153 handle_v_attach (own_buf);
2154 return;
2155 }
2156
2157 if (strncmp (own_buf, "vRun;", 5) == 0)
2158 {
2159 if (!multi_process && target_running ())
2160 {
2161 fprintf (stderr, "Already debugging a process\n");
2162 write_enn (own_buf);
2163 return;
2164 }
2165 handle_v_run (own_buf);
2166 return;
2167 }
2168
2169 if (strncmp (own_buf, "vKill;", 6) == 0)
2170 {
2171 if (!target_running ())
2172 {
2173 fprintf (stderr, "No process to kill\n");
2174 write_enn (own_buf);
2175 return;
2176 }
2177 handle_v_kill (own_buf);
2178 return;
2179 }
2180
2181 if (strncmp (own_buf, "vStopped", 8) == 0)
2182 {
2183 handle_v_stopped (own_buf);
2184 return;
2185 }
2186
2187 /* Otherwise we didn't know what packet it was. Say we didn't
2188 understand it. */
2189 own_buf[0] = 0;
2190 return;
2191 }
2192
2193 /* Resume inferior and wait for another event. In non-stop mode,
2194 don't really wait here, but return immediatelly to the event
2195 loop. */
2196 static void
2197 myresume (char *own_buf, int step, int sig)
2198 {
2199 struct thread_resume resume_info[2];
2200 int n = 0;
2201 int valid_cont_thread;
2202
2203 set_desired_inferior (0);
2204
2205 valid_cont_thread = (!ptid_equal (cont_thread, null_ptid)
2206 && !ptid_equal (cont_thread, minus_one_ptid));
2207
2208 if (step || sig || valid_cont_thread)
2209 {
2210 resume_info[0].thread
2211 = ((struct inferior_list_entry *) current_inferior)->id;
2212 if (step)
2213 resume_info[0].kind = resume_step;
2214 else
2215 resume_info[0].kind = resume_continue;
2216 resume_info[0].sig = sig;
2217 n++;
2218 }
2219
2220 if (!valid_cont_thread)
2221 {
2222 resume_info[n].thread = minus_one_ptid;
2223 resume_info[n].kind = resume_continue;
2224 resume_info[n].sig = 0;
2225 n++;
2226 }
2227
2228 if (!non_stop)
2229 enable_async_io ();
2230
2231 (*the_target->resume) (resume_info, n);
2232
2233 if (non_stop)
2234 write_ok (own_buf);
2235 else
2236 {
2237 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
2238
2239 if (last_status.kind != TARGET_WAITKIND_EXITED
2240 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
2241 {
2242 current_inferior->last_resume_kind = resume_stop;
2243 current_inferior->last_status = last_status;
2244 }
2245
2246 prepare_resume_reply (own_buf, last_ptid, &last_status);
2247 disable_async_io ();
2248
2249 if (last_status.kind == TARGET_WAITKIND_EXITED
2250 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2251 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid)));
2252 }
2253 }
2254
2255 /* Callback for for_each_inferior. Make a new stop reply for each
2256 stopped thread. */
2257
2258 static int
2259 queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg)
2260 {
2261 struct thread_info *thread = (struct thread_info *) entry;
2262
2263 /* For now, assume targets that don't have this callback also don't
2264 manage the thread's last_status field. */
2265 if (the_target->thread_stopped == NULL)
2266 {
2267 /* Pass the last stop reply back to GDB, but don't notify
2268 yet. */
2269 queue_stop_reply (entry->id, &thread->last_status);
2270 }
2271 else
2272 {
2273 if (thread_stopped (thread))
2274 {
2275 if (debug_threads)
2276 fprintf (stderr,
2277 "Reporting thread %s as already stopped with %s\n",
2278 target_pid_to_str (entry->id),
2279 target_waitstatus_to_string (&thread->last_status));
2280
2281 gdb_assert (thread->last_status.kind != TARGET_WAITKIND_IGNORE);
2282
2283 /* Pass the last stop reply back to GDB, but don't notify
2284 yet. */
2285 queue_stop_reply (entry->id, &thread->last_status);
2286 }
2287 }
2288
2289 return 0;
2290 }
2291
2292 /* Set this inferior threads's state as "want-stopped". We won't
2293 resume this thread until the client gives us another action for
2294 it. */
2295
2296 static void
2297 gdb_wants_thread_stopped (struct inferior_list_entry *entry)
2298 {
2299 struct thread_info *thread = (struct thread_info *) entry;
2300
2301 thread->last_resume_kind = resume_stop;
2302
2303 if (thread->last_status.kind == TARGET_WAITKIND_IGNORE)
2304 {
2305 /* Most threads are stopped implicitly (all-stop); tag that with
2306 signal 0. */
2307 thread->last_status.kind = TARGET_WAITKIND_STOPPED;
2308 thread->last_status.value.sig = TARGET_SIGNAL_0;
2309 }
2310 }
2311
2312 /* Set all threads' states as "want-stopped". */
2313
2314 static void
2315 gdb_wants_all_threads_stopped (void)
2316 {
2317 for_each_inferior (&all_threads, gdb_wants_thread_stopped);
2318 }
2319
2320 /* Clear the gdb_detached flag of every process. */
2321
2322 static void
2323 gdb_reattached_process (struct inferior_list_entry *entry)
2324 {
2325 struct process_info *process = (struct process_info *) entry;
2326
2327 process->gdb_detached = 0;
2328 }
2329
2330 /* Status handler for the '?' packet. */
2331
2332 static void
2333 handle_status (char *own_buf)
2334 {
2335 /* GDB is connected, don't forward events to the target anymore. */
2336 for_each_inferior (&all_processes, gdb_reattached_process);
2337
2338 /* In non-stop mode, we must send a stop reply for each stopped
2339 thread. In all-stop mode, just send one for the first stopped
2340 thread we find. */
2341
2342 if (non_stop)
2343 {
2344 discard_queued_stop_replies (-1);
2345 find_inferior (&all_threads, queue_stop_reply_callback, NULL);
2346
2347 /* The first is sent immediatly. OK is sent if there is no
2348 stopped thread, which is the same handling of the vStopped
2349 packet (by design). */
2350 send_next_stop_reply (own_buf);
2351 }
2352 else
2353 {
2354 pause_all (0);
2355 stabilize_threads ();
2356 gdb_wants_all_threads_stopped ();
2357
2358 if (all_threads.head)
2359 {
2360 struct target_waitstatus status;
2361
2362 status.kind = TARGET_WAITKIND_STOPPED;
2363 status.value.sig = TARGET_SIGNAL_TRAP;
2364 prepare_resume_reply (own_buf,
2365 all_threads.head->id, &status);
2366 }
2367 else
2368 strcpy (own_buf, "W00");
2369 }
2370 }
2371
2372 static void
2373 gdbserver_version (void)
2374 {
2375 printf ("GNU gdbserver %s%s\n"
2376 "Copyright (C) 2012 Free Software Foundation, Inc.\n"
2377 "gdbserver is free software, covered by the "
2378 "GNU General Public License.\n"
2379 "This gdbserver was configured as \"%s\"\n",
2380 PKGVERSION, version, host_name);
2381 }
2382
2383 static void
2384 gdbserver_usage (FILE *stream)
2385 {
2386 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2387 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2388 "\tgdbserver [OPTIONS] --multi COMM\n"
2389 "\n"
2390 "COMM may either be a tty device (for serial debugging), or \n"
2391 "HOST:PORT to listen for a TCP connection.\n"
2392 "\n"
2393 "Options:\n"
2394 " --debug Enable general debugging output.\n"
2395 " --remote-debug Enable remote protocol debugging output.\n"
2396 " --version Display version information and exit.\n"
2397 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2398 " --once Exit after the first connection has "
2399 "closed.\n");
2400 if (REPORT_BUGS_TO[0] && stream == stdout)
2401 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
2402 }
2403
2404 static void
2405 gdbserver_show_disableable (FILE *stream)
2406 {
2407 fprintf (stream, "Disableable packets:\n"
2408 " vCont \tAll vCont packets\n"
2409 " qC \tQuerying the current thread\n"
2410 " qfThreadInfo\tThread listing\n"
2411 " Tthread \tPassing the thread specifier in the "
2412 "T stop reply packet\n"
2413 " threads \tAll of the above\n");
2414 }
2415
2416
2417 #undef require_running
2418 #define require_running(BUF) \
2419 if (!target_running ()) \
2420 { \
2421 write_enn (BUF); \
2422 break; \
2423 }
2424
2425 static int
2426 first_thread_of (struct inferior_list_entry *entry, void *args)
2427 {
2428 int pid = * (int *) args;
2429
2430 if (ptid_get_pid (entry->id) == pid)
2431 return 1;
2432
2433 return 0;
2434 }
2435
2436 static void
2437 kill_inferior_callback (struct inferior_list_entry *entry)
2438 {
2439 struct process_info *process = (struct process_info *) entry;
2440 int pid = ptid_get_pid (process->head.id);
2441
2442 kill_inferior (pid);
2443 discard_queued_stop_replies (pid);
2444 }
2445
2446 /* Callback for for_each_inferior to detach or kill the inferior,
2447 depending on whether we attached to it or not.
2448 We inform the user whether we're detaching or killing the process
2449 as this is only called when gdbserver is about to exit. */
2450
2451 static void
2452 detach_or_kill_inferior_callback (struct inferior_list_entry *entry)
2453 {
2454 struct process_info *process = (struct process_info *) entry;
2455 int pid = ptid_get_pid (process->head.id);
2456
2457 if (process->attached)
2458 detach_inferior (pid);
2459 else
2460 kill_inferior (pid);
2461
2462 discard_queued_stop_replies (pid);
2463 }
2464
2465 /* for_each_inferior callback for detach_or_kill_for_exit to print
2466 the pids of started inferiors. */
2467
2468 static void
2469 print_started_pid (struct inferior_list_entry *entry)
2470 {
2471 struct process_info *process = (struct process_info *) entry;
2472
2473 if (! process->attached)
2474 {
2475 int pid = ptid_get_pid (process->head.id);
2476 fprintf (stderr, " %d", pid);
2477 }
2478 }
2479
2480 /* for_each_inferior callback for detach_or_kill_for_exit to print
2481 the pids of attached inferiors. */
2482
2483 static void
2484 print_attached_pid (struct inferior_list_entry *entry)
2485 {
2486 struct process_info *process = (struct process_info *) entry;
2487
2488 if (process->attached)
2489 {
2490 int pid = ptid_get_pid (process->head.id);
2491 fprintf (stderr, " %d", pid);
2492 }
2493 }
2494
2495 /* Call this when exiting gdbserver with possible inferiors that need
2496 to be killed or detached from. */
2497
2498 static void
2499 detach_or_kill_for_exit (void)
2500 {
2501 /* First print a list of the inferiors we will be killing/detaching.
2502 This is to assist the user, for example, in case the inferior unexpectedly
2503 dies after we exit: did we screw up or did the inferior exit on its own?
2504 Having this info will save some head-scratching. */
2505
2506 if (have_started_inferiors_p ())
2507 {
2508 fprintf (stderr, "Killing process(es):");
2509 for_each_inferior (&all_processes, print_started_pid);
2510 fprintf (stderr, "\n");
2511 }
2512 if (have_attached_inferiors_p ())
2513 {
2514 fprintf (stderr, "Detaching process(es):");
2515 for_each_inferior (&all_processes, print_attached_pid);
2516 fprintf (stderr, "\n");
2517 }
2518
2519 /* Now we can kill or detach the inferiors. */
2520
2521 for_each_inferior (&all_processes, detach_or_kill_inferior_callback);
2522 }
2523
2524 int
2525 main (int argc, char *argv[])
2526 {
2527 int bad_attach;
2528 int pid;
2529 char *arg_end, *port;
2530 char **next_arg = &argv[1];
2531 int multi_mode = 0;
2532 int attach = 0;
2533 int was_running;
2534
2535 while (*next_arg != NULL && **next_arg == '-')
2536 {
2537 if (strcmp (*next_arg, "--version") == 0)
2538 {
2539 gdbserver_version ();
2540 exit (0);
2541 }
2542 else if (strcmp (*next_arg, "--help") == 0)
2543 {
2544 gdbserver_usage (stdout);
2545 exit (0);
2546 }
2547 else if (strcmp (*next_arg, "--attach") == 0)
2548 attach = 1;
2549 else if (strcmp (*next_arg, "--multi") == 0)
2550 multi_mode = 1;
2551 else if (strcmp (*next_arg, "--wrapper") == 0)
2552 {
2553 next_arg++;
2554
2555 wrapper_argv = next_arg;
2556 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
2557 next_arg++;
2558
2559 if (next_arg == wrapper_argv || *next_arg == NULL)
2560 {
2561 gdbserver_usage (stderr);
2562 exit (1);
2563 }
2564
2565 /* Consume the "--". */
2566 *next_arg = NULL;
2567 }
2568 else if (strcmp (*next_arg, "--debug") == 0)
2569 debug_threads = 1;
2570 else if (strcmp (*next_arg, "--remote-debug") == 0)
2571 remote_debug = 1;
2572 else if (strcmp (*next_arg, "--disable-packet") == 0)
2573 {
2574 gdbserver_show_disableable (stdout);
2575 exit (0);
2576 }
2577 else if (strncmp (*next_arg,
2578 "--disable-packet=",
2579 sizeof ("--disable-packet=") - 1) == 0)
2580 {
2581 char *packets, *tok;
2582
2583 packets = *next_arg += sizeof ("--disable-packet=") - 1;
2584 for (tok = strtok (packets, ",");
2585 tok != NULL;
2586 tok = strtok (NULL, ","))
2587 {
2588 if (strcmp ("vCont", tok) == 0)
2589 disable_packet_vCont = 1;
2590 else if (strcmp ("Tthread", tok) == 0)
2591 disable_packet_Tthread = 1;
2592 else if (strcmp ("qC", tok) == 0)
2593 disable_packet_qC = 1;
2594 else if (strcmp ("qfThreadInfo", tok) == 0)
2595 disable_packet_qfThreadInfo = 1;
2596 else if (strcmp ("threads", tok) == 0)
2597 {
2598 disable_packet_vCont = 1;
2599 disable_packet_Tthread = 1;
2600 disable_packet_qC = 1;
2601 disable_packet_qfThreadInfo = 1;
2602 }
2603 else
2604 {
2605 fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
2606 tok);
2607 gdbserver_show_disableable (stderr);
2608 exit (1);
2609 }
2610 }
2611 }
2612 else if (strcmp (*next_arg, "-") == 0)
2613 {
2614 /* "-" specifies a stdio connection and is a form of port
2615 specification. */
2616 *next_arg = STDIO_CONNECTION_NAME;
2617 break;
2618 }
2619 else if (strcmp (*next_arg, "--disable-randomization") == 0)
2620 disable_randomization = 1;
2621 else if (strcmp (*next_arg, "--no-disable-randomization") == 0)
2622 disable_randomization = 0;
2623 else if (strcmp (*next_arg, "--once") == 0)
2624 run_once = 1;
2625 else
2626 {
2627 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
2628 exit (1);
2629 }
2630
2631 next_arg++;
2632 continue;
2633 }
2634
2635 if (setjmp (toplevel))
2636 {
2637 fprintf (stderr, "Exiting\n");
2638 exit (1);
2639 }
2640
2641 port = *next_arg;
2642 next_arg++;
2643 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
2644 {
2645 gdbserver_usage (stderr);
2646 exit (1);
2647 }
2648
2649 /* We need to know whether the remote connection is stdio before
2650 starting the inferior. Inferiors created in this scenario have
2651 stdin,stdout redirected. So do this here before we call
2652 start_inferior. */
2653 remote_prepare (port);
2654
2655 bad_attach = 0;
2656 pid = 0;
2657
2658 /* --attach used to come after PORT, so allow it there for
2659 compatibility. */
2660 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
2661 {
2662 attach = 1;
2663 next_arg++;
2664 }
2665
2666 if (attach
2667 && (*next_arg == NULL
2668 || (*next_arg)[0] == '\0'
2669 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
2670 || *arg_end != '\0'
2671 || next_arg[1] != NULL))
2672 bad_attach = 1;
2673
2674 if (bad_attach)
2675 {
2676 gdbserver_usage (stderr);
2677 exit (1);
2678 }
2679
2680 initialize_async_io ();
2681 initialize_low ();
2682 if (target_supports_tracepoints ())
2683 initialize_tracepoint ();
2684
2685 own_buf = xmalloc (PBUFSIZ + 1);
2686 mem_buf = xmalloc (PBUFSIZ);
2687
2688 if (pid == 0 && *next_arg != NULL)
2689 {
2690 int i, n;
2691
2692 n = argc - (next_arg - argv);
2693 program_argv = xmalloc (sizeof (char *) * (n + 1));
2694 for (i = 0; i < n; i++)
2695 program_argv[i] = xstrdup (next_arg[i]);
2696 program_argv[i] = NULL;
2697
2698 /* Wait till we are at first instruction in program. */
2699 start_inferior (program_argv);
2700
2701 /* We are now (hopefully) stopped at the first instruction of
2702 the target process. This assumes that the target process was
2703 successfully created. */
2704 }
2705 else if (pid != 0)
2706 {
2707 if (attach_inferior (pid) == -1)
2708 error ("Attaching not supported on this target");
2709
2710 /* Otherwise succeeded. */
2711 }
2712 else
2713 {
2714 last_status.kind = TARGET_WAITKIND_EXITED;
2715 last_status.value.integer = 0;
2716 last_ptid = minus_one_ptid;
2717 }
2718
2719 /* Don't report shared library events on the initial connection,
2720 even if some libraries are preloaded. Avoids the "stopped by
2721 shared library event" notice on gdb side. */
2722 dlls_changed = 0;
2723
2724 if (setjmp (toplevel))
2725 {
2726 /* If something fails and longjmps while detaching or killing
2727 inferiors, we'd end up here again, stuck in an infinite loop
2728 trap. Be sure that if that happens, we exit immediately
2729 instead. */
2730 if (setjmp (toplevel))
2731 {
2732 fprintf (stderr, "Detach or kill failed. Exiting\n");
2733 exit (1);
2734 }
2735
2736 detach_or_kill_for_exit ();
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 detach_or_kill_for_exit ();
2783 exit (0);
2784 }
2785
2786 fprintf (stderr,
2787 "Remote side has terminated connection. "
2788 "GDBserver will reopen the connection.\n");
2789
2790 if (tracing)
2791 {
2792 if (disconnected_tracing)
2793 {
2794 /* Try to enable non-stop/async mode, so we we can both
2795 wait for an async socket accept, and handle async
2796 target events simultaneously. There's also no point
2797 either in having the target always stop all threads,
2798 when we're going to pass signals down without
2799 informing GDB. */
2800 if (!non_stop)
2801 {
2802 if (start_non_stop (1))
2803 non_stop = 1;
2804
2805 /* Detaching implicitly resumes all threads; simply
2806 disconnecting does not. */
2807 }
2808 }
2809 else
2810 {
2811 fprintf (stderr,
2812 "Disconnected tracing disabled; stopping trace run.\n");
2813 stop_tracing ();
2814 }
2815 }
2816 }
2817 }
2818
2819 /* Event loop callback that handles a serial event. The first byte in
2820 the serial buffer gets us here. We expect characters to arrive at
2821 a brisk pace, so we read the rest of the packet with a blocking
2822 getpkt call. */
2823
2824 static int
2825 process_serial_event (void)
2826 {
2827 char ch;
2828 int i = 0;
2829 int signal;
2830 unsigned int len;
2831 int res;
2832 CORE_ADDR mem_addr;
2833 int pid;
2834 unsigned char sig;
2835 int packet_len;
2836 int new_packet_len = -1;
2837
2838 /* Used to decide when gdbserver should exit in
2839 multi-mode/remote. */
2840 static int have_ran = 0;
2841
2842 if (!have_ran)
2843 have_ran = target_running ();
2844
2845 disable_async_io ();
2846
2847 response_needed = 0;
2848 packet_len = getpkt (own_buf);
2849 if (packet_len <= 0)
2850 {
2851 remote_close ();
2852 /* Force an event loop break. */
2853 return -1;
2854 }
2855 response_needed = 1;
2856
2857 i = 0;
2858 ch = own_buf[i++];
2859 switch (ch)
2860 {
2861 case 'q':
2862 handle_query (own_buf, packet_len, &new_packet_len);
2863 break;
2864 case 'Q':
2865 handle_general_set (own_buf);
2866 break;
2867 case 'D':
2868 require_running (own_buf);
2869
2870 if (multi_process)
2871 {
2872 i++; /* skip ';' */
2873 pid = strtol (&own_buf[i], NULL, 16);
2874 }
2875 else
2876 pid =
2877 ptid_get_pid (((struct inferior_list_entry *) current_inferior)->id);
2878
2879 if (tracing && disconnected_tracing)
2880 {
2881 struct thread_resume resume_info;
2882 struct process_info *process = find_process_pid (pid);
2883
2884 if (process == NULL)
2885 {
2886 write_enn (own_buf);
2887 break;
2888 }
2889
2890 fprintf (stderr,
2891 "Disconnected tracing in effect, "
2892 "leaving gdbserver attached to the process\n");
2893
2894 /* Make sure we're in non-stop/async mode, so we we can both
2895 wait for an async socket accept, and handle async target
2896 events simultaneously. There's also no point either in
2897 having the target stop all threads, when we're going to
2898 pass signals down without informing GDB. */
2899 if (!non_stop)
2900 {
2901 if (debug_threads)
2902 fprintf (stderr, "Forcing non-stop mode\n");
2903
2904 non_stop = 1;
2905 start_non_stop (1);
2906 }
2907
2908 process->gdb_detached = 1;
2909
2910 /* Detaching implicitly resumes all threads. */
2911 resume_info.thread = minus_one_ptid;
2912 resume_info.kind = resume_continue;
2913 resume_info.sig = 0;
2914 (*the_target->resume) (&resume_info, 1);
2915
2916 write_ok (own_buf);
2917 break; /* from switch/case */
2918 }
2919
2920 fprintf (stderr, "Detaching from process %d\n", pid);
2921 stop_tracing ();
2922 if (detach_inferior (pid) != 0)
2923 write_enn (own_buf);
2924 else
2925 {
2926 discard_queued_stop_replies (pid);
2927 write_ok (own_buf);
2928
2929 if (extended_protocol)
2930 {
2931 /* Treat this like a normal program exit. */
2932 last_status.kind = TARGET_WAITKIND_EXITED;
2933 last_status.value.integer = 0;
2934 last_ptid = pid_to_ptid (pid);
2935
2936 current_inferior = NULL;
2937 }
2938 else
2939 {
2940 putpkt (own_buf);
2941 remote_close ();
2942
2943 /* If we are attached, then we can exit. Otherwise, we
2944 need to hang around doing nothing, until the child is
2945 gone. */
2946 join_inferior (pid);
2947 exit (0);
2948 }
2949 }
2950 break;
2951 case '!':
2952 extended_protocol = 1;
2953 write_ok (own_buf);
2954 break;
2955 case '?':
2956 handle_status (own_buf);
2957 break;
2958 case 'H':
2959 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
2960 {
2961 ptid_t gdb_id, thread_id;
2962 int pid;
2963
2964 require_running (own_buf);
2965
2966 gdb_id = read_ptid (&own_buf[2], NULL);
2967
2968 pid = ptid_get_pid (gdb_id);
2969
2970 if (ptid_equal (gdb_id, null_ptid)
2971 || ptid_equal (gdb_id, minus_one_ptid))
2972 thread_id = null_ptid;
2973 else if (pid != 0
2974 && ptid_equal (pid_to_ptid (pid),
2975 gdb_id))
2976 {
2977 struct thread_info *thread =
2978 (struct thread_info *) find_inferior (&all_threads,
2979 first_thread_of,
2980 &pid);
2981 if (!thread)
2982 {
2983 write_enn (own_buf);
2984 break;
2985 }
2986
2987 thread_id = ((struct inferior_list_entry *)thread)->id;
2988 }
2989 else
2990 {
2991 thread_id = gdb_id_to_thread_id (gdb_id);
2992 if (ptid_equal (thread_id, null_ptid))
2993 {
2994 write_enn (own_buf);
2995 break;
2996 }
2997 }
2998
2999 if (own_buf[1] == 'g')
3000 {
3001 if (ptid_equal (thread_id, null_ptid))
3002 {
3003 /* GDB is telling us to choose any thread. Check if
3004 the currently selected thread is still valid. If
3005 it is not, select the first available. */
3006 struct thread_info *thread =
3007 (struct thread_info *) find_inferior_id (&all_threads,
3008 general_thread);
3009 if (thread == NULL)
3010 thread_id = all_threads.head->id;
3011 }
3012
3013 general_thread = thread_id;
3014 set_desired_inferior (1);
3015 }
3016 else if (own_buf[1] == 'c')
3017 cont_thread = thread_id;
3018
3019 write_ok (own_buf);
3020 }
3021 else
3022 {
3023 /* Silently ignore it so that gdb can extend the protocol
3024 without compatibility headaches. */
3025 own_buf[0] = '\0';
3026 }
3027 break;
3028 case 'g':
3029 require_running (own_buf);
3030 if (current_traceframe >= 0)
3031 {
3032 struct regcache *regcache = new_register_cache ();
3033
3034 if (fetch_traceframe_registers (current_traceframe,
3035 regcache, -1) == 0)
3036 registers_to_string (regcache, own_buf);
3037 else
3038 write_enn (own_buf);
3039 free_register_cache (regcache);
3040 }
3041 else
3042 {
3043 struct regcache *regcache;
3044
3045 set_desired_inferior (1);
3046 regcache = get_thread_regcache (current_inferior, 1);
3047 registers_to_string (regcache, own_buf);
3048 }
3049 break;
3050 case 'G':
3051 require_running (own_buf);
3052 if (current_traceframe >= 0)
3053 write_enn (own_buf);
3054 else
3055 {
3056 struct regcache *regcache;
3057
3058 set_desired_inferior (1);
3059 regcache = get_thread_regcache (current_inferior, 1);
3060 registers_from_string (regcache, &own_buf[1]);
3061 write_ok (own_buf);
3062 }
3063 break;
3064 case 'm':
3065 require_running (own_buf);
3066 decode_m_packet (&own_buf[1], &mem_addr, &len);
3067 res = gdb_read_memory (mem_addr, mem_buf, len);
3068 if (res < 0)
3069 write_enn (own_buf);
3070 else
3071 convert_int_to_ascii (mem_buf, own_buf, res);
3072 break;
3073 case 'M':
3074 require_running (own_buf);
3075 decode_M_packet (&own_buf[1], &mem_addr, &len, &mem_buf);
3076 if (gdb_write_memory (mem_addr, mem_buf, len) == 0)
3077 write_ok (own_buf);
3078 else
3079 write_enn (own_buf);
3080 break;
3081 case 'X':
3082 require_running (own_buf);
3083 if (decode_X_packet (&own_buf[1], packet_len - 1,
3084 &mem_addr, &len, &mem_buf) < 0
3085 || gdb_write_memory (mem_addr, mem_buf, len) != 0)
3086 write_enn (own_buf);
3087 else
3088 write_ok (own_buf);
3089 break;
3090 case 'C':
3091 require_running (own_buf);
3092 convert_ascii_to_int (own_buf + 1, &sig, 1);
3093 if (target_signal_to_host_p (sig))
3094 signal = target_signal_to_host (sig);
3095 else
3096 signal = 0;
3097 myresume (own_buf, 0, signal);
3098 break;
3099 case 'S':
3100 require_running (own_buf);
3101 convert_ascii_to_int (own_buf + 1, &sig, 1);
3102 if (target_signal_to_host_p (sig))
3103 signal = target_signal_to_host (sig);
3104 else
3105 signal = 0;
3106 myresume (own_buf, 1, signal);
3107 break;
3108 case 'c':
3109 require_running (own_buf);
3110 signal = 0;
3111 myresume (own_buf, 0, signal);
3112 break;
3113 case 's':
3114 require_running (own_buf);
3115 signal = 0;
3116 myresume (own_buf, 1, signal);
3117 break;
3118 case 'Z': /* insert_ ... */
3119 /* Fallthrough. */
3120 case 'z': /* remove_ ... */
3121 {
3122 char *lenptr;
3123 char *dataptr;
3124 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
3125 int len = strtol (lenptr + 1, &dataptr, 16);
3126 char type = own_buf[1];
3127 int res;
3128 const int insert = ch == 'Z';
3129
3130 /* Default to unrecognized/unsupported. */
3131 res = 1;
3132 switch (type)
3133 {
3134 case '0': /* software-breakpoint */
3135 case '1': /* hardware-breakpoint */
3136 case '2': /* write watchpoint */
3137 case '3': /* read watchpoint */
3138 case '4': /* access watchpoint */
3139 require_running (own_buf);
3140 if (insert && the_target->insert_point != NULL)
3141 res = (*the_target->insert_point) (type, addr, len);
3142 else if (!insert && the_target->remove_point != NULL)
3143 res = (*the_target->remove_point) (type, addr, len);
3144 break;
3145 default:
3146 break;
3147 }
3148
3149 if (res == 0)
3150 write_ok (own_buf);
3151 else if (res == 1)
3152 /* Unsupported. */
3153 own_buf[0] = '\0';
3154 else
3155 write_enn (own_buf);
3156 break;
3157 }
3158 case 'k':
3159 response_needed = 0;
3160 if (!target_running ())
3161 /* The packet we received doesn't make sense - but we can't
3162 reply to it, either. */
3163 return 0;
3164
3165 fprintf (stderr, "Killing all inferiors\n");
3166 for_each_inferior (&all_processes, kill_inferior_callback);
3167
3168 /* When using the extended protocol, we wait with no program
3169 running. The traditional protocol will exit instead. */
3170 if (extended_protocol)
3171 {
3172 last_status.kind = TARGET_WAITKIND_EXITED;
3173 last_status.value.sig = TARGET_SIGNAL_KILL;
3174 return 0;
3175 }
3176 else
3177 exit (0);
3178
3179 case 'T':
3180 {
3181 ptid_t gdb_id, thread_id;
3182
3183 require_running (own_buf);
3184
3185 gdb_id = read_ptid (&own_buf[1], NULL);
3186 thread_id = gdb_id_to_thread_id (gdb_id);
3187 if (ptid_equal (thread_id, null_ptid))
3188 {
3189 write_enn (own_buf);
3190 break;
3191 }
3192
3193 if (mythread_alive (thread_id))
3194 write_ok (own_buf);
3195 else
3196 write_enn (own_buf);
3197 }
3198 break;
3199 case 'R':
3200 response_needed = 0;
3201
3202 /* Restarting the inferior is only supported in the extended
3203 protocol. */
3204 if (extended_protocol)
3205 {
3206 if (target_running ())
3207 for_each_inferior (&all_processes,
3208 kill_inferior_callback);
3209 fprintf (stderr, "GDBserver restarting\n");
3210
3211 /* Wait till we are at 1st instruction in prog. */
3212 if (program_argv != NULL)
3213 start_inferior (program_argv);
3214 else
3215 {
3216 last_status.kind = TARGET_WAITKIND_EXITED;
3217 last_status.value.sig = TARGET_SIGNAL_KILL;
3218 }
3219 return 0;
3220 }
3221 else
3222 {
3223 /* It is a request we don't understand. Respond with an
3224 empty packet so that gdb knows that we don't support this
3225 request. */
3226 own_buf[0] = '\0';
3227 break;
3228 }
3229 case 'v':
3230 /* Extended (long) request. */
3231 handle_v_requests (own_buf, packet_len, &new_packet_len);
3232 break;
3233
3234 default:
3235 /* It is a request we don't understand. Respond with an empty
3236 packet so that gdb knows that we don't support this
3237 request. */
3238 own_buf[0] = '\0';
3239 break;
3240 }
3241
3242 if (new_packet_len != -1)
3243 putpkt_binary (own_buf, new_packet_len);
3244 else
3245 putpkt (own_buf);
3246
3247 response_needed = 0;
3248
3249 if (!extended_protocol && have_ran && !target_running ())
3250 {
3251 /* In non-stop, defer exiting until GDB had a chance to query
3252 the whole vStopped list (until it gets an OK). */
3253 if (!notif_queue)
3254 {
3255 fprintf (stderr, "GDBserver exiting\n");
3256 remote_close ();
3257 exit (0);
3258 }
3259 }
3260
3261 if (exit_requested)
3262 return -1;
3263
3264 return 0;
3265 }
3266
3267 /* Event-loop callback for serial events. */
3268
3269 int
3270 handle_serial_event (int err, gdb_client_data client_data)
3271 {
3272 if (debug_threads)
3273 fprintf (stderr, "handling possible serial event\n");
3274
3275 /* Really handle it. */
3276 if (process_serial_event () < 0)
3277 return -1;
3278
3279 /* Be sure to not change the selected inferior behind GDB's back.
3280 Important in the non-stop mode asynchronous protocol. */
3281 set_desired_inferior (1);
3282
3283 return 0;
3284 }
3285
3286 /* Event-loop callback for target events. */
3287
3288 int
3289 handle_target_event (int err, gdb_client_data client_data)
3290 {
3291 if (debug_threads)
3292 fprintf (stderr, "handling possible target event\n");
3293
3294 last_ptid = mywait (minus_one_ptid, &last_status,
3295 TARGET_WNOHANG, 1);
3296
3297 if (last_status.kind != TARGET_WAITKIND_IGNORE)
3298 {
3299 int pid = ptid_get_pid (last_ptid);
3300 struct process_info *process = find_process_pid (pid);
3301 int forward_event = !gdb_connected () || process->gdb_detached;
3302
3303 if (last_status.kind == TARGET_WAITKIND_EXITED
3304 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
3305 {
3306 mark_breakpoints_out (process);
3307 mourn_inferior (process);
3308 }
3309 else
3310 {
3311 /* We're reporting this thread as stopped. Update its
3312 "want-stopped" state to what the client wants, until it
3313 gets a new resume action. */
3314 current_inferior->last_resume_kind = resume_stop;
3315 current_inferior->last_status = last_status;
3316 }
3317
3318 if (forward_event)
3319 {
3320 if (!target_running ())
3321 {
3322 /* The last process exited. We're done. */
3323 exit (0);
3324 }
3325
3326 if (last_status.kind == TARGET_WAITKIND_STOPPED)
3327 {
3328 /* A thread stopped with a signal, but gdb isn't
3329 connected to handle it. Pass it down to the
3330 inferior, as if it wasn't being traced. */
3331 struct thread_resume resume_info;
3332
3333 if (debug_threads)
3334 fprintf (stderr,
3335 "GDB not connected; forwarding event %d for [%s]\n",
3336 (int) last_status.kind,
3337 target_pid_to_str (last_ptid));
3338
3339 resume_info.thread = last_ptid;
3340 resume_info.kind = resume_continue;
3341 resume_info.sig = target_signal_to_host (last_status.value.sig);
3342 (*the_target->resume) (&resume_info, 1);
3343 }
3344 else if (debug_threads)
3345 fprintf (stderr, "GDB not connected; ignoring event %d for [%s]\n",
3346 (int) last_status.kind,
3347 target_pid_to_str (last_ptid));
3348 }
3349 else
3350 {
3351 /* Something interesting. Tell GDB about it. */
3352 push_event (last_ptid, &last_status);
3353 }
3354 }
3355
3356 /* Be sure to not change the selected inferior behind GDB's back.
3357 Important in the non-stop mode asynchronous protocol. */
3358 set_desired_inferior (1);
3359
3360 return 0;
3361 }
This page took 0.117568 seconds and 4 git commands to generate.