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