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