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