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