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