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