1 /* Main code for remote server for GDB.
2 Copyright (C) 1989-2013 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
20 #include "gdbthread.h"
31 #include "btrace-common.h"
33 /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of
34 `vCont'. Note the multi-process extensions made `vCont' a
35 requirement, so `Hc pPID.TID' is pretty much undefined. So
36 CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for
37 resuming all threads of the process (again, `Hc' isn't used for
38 multi-process), or a specific thread ptid_t.
40 We also set this when handling a single-thread `vCont' resume, as
41 some places in the backends check it to know when (and for which
42 thread) single-thread scheduler-locking is in effect. */
45 /* The thread set with an `Hg' packet. */
46 ptid_t general_thread;
50 static int extended_protocol;
51 static int response_needed;
52 static int exit_requested;
54 /* --once: Exit after the first connection has closed. */
60 /* Whether we should attempt to disable the operating system's address
61 space randomization feature before starting an inferior. */
62 int disable_randomization = 1;
64 static char **program_argv, **wrapper_argv;
66 /* Enable miscellaneous debugging output. The name is historical - it
67 was originally used to debug LinuxThreads support. */
70 /* Enable debugging of h/w breakpoint/watchpoint support. */
73 int pass_signals[GDB_SIGNAL_LAST];
74 int program_signals[GDB_SIGNAL_LAST];
75 int program_signals_p;
79 const char *gdbserver_xmltarget;
81 /* The PID of the originally created or attached inferior. Used to
82 send signals to the process when GDB sends us an asynchronous interrupt
83 (user hitting Control-C in the client), and to wait for the child to exit
84 when no longer debugging it. */
86 unsigned long signal_pid;
89 /* A file descriptor for the controlling terminal. */
92 /* TERMINAL_FD's original foreground group. */
93 pid_t old_foreground_pgrp;
95 /* Hand back terminal ownership to the original foreground group. */
98 restore_old_foreground_pgrp (void)
100 tcsetpgrp (terminal_fd, old_foreground_pgrp);
104 /* Set if you want to disable optional thread related packets support
105 in gdbserver, for the sake of testing GDB against stubs that don't
107 int disable_packet_vCont;
108 int disable_packet_Tthread;
109 int disable_packet_qC;
110 int disable_packet_qfThreadInfo;
112 /* Last status reported to GDB. */
113 static struct target_waitstatus last_status;
114 static ptid_t last_ptid;
116 static char *own_buf;
117 static unsigned char *mem_buf;
119 /* A sub-class of 'struct notif_event' for stop, holding information
120 relative to a single stop reply. We keep a queue of these to
121 push to GDB in non-stop mode. */
125 struct notif_event base;
127 /* Thread or process that got the event. */
131 struct target_waitstatus status;
134 DEFINE_QUEUE_P (notif_event_p);
136 /* Put a stop reply to the stop reply queue. */
139 queue_stop_reply (ptid_t ptid, struct target_waitstatus *status)
141 struct vstop_notif *new_notif = xmalloc (sizeof (*new_notif));
143 new_notif->ptid = ptid;
144 new_notif->status = *status;
146 notif_event_enque (¬if_stop, (struct notif_event *) new_notif);
150 remove_all_on_match_pid (QUEUE (notif_event_p) *q,
151 QUEUE_ITER (notif_event_p) *iter,
152 struct notif_event *event,
158 || ptid_get_pid (((struct vstop_notif *) event)->ptid) == *pid)
160 if (q->free_func != NULL)
161 q->free_func (event);
163 QUEUE_remove_elem (notif_event_p, q, iter);
169 /* Get rid of the currently pending stop replies for PID. If PID is
170 -1, then apply to all processes. */
173 discard_queued_stop_replies (int pid)
175 QUEUE_iterate (notif_event_p, notif_stop.queue,
176 remove_all_on_match_pid, &pid);
180 vstop_notif_reply (struct notif_event *event, char *own_buf)
182 struct vstop_notif *vstop = (struct vstop_notif *) event;
184 prepare_resume_reply (own_buf, vstop->ptid, &vstop->status);
187 struct notif_server notif_stop =
189 "vStopped", "Stop", NULL, vstop_notif_reply,
193 target_running (void)
195 return all_threads.head != NULL;
199 start_inferior (char **argv)
201 char **new_argv = argv;
203 if (wrapper_argv != NULL)
207 for (i = 0; wrapper_argv[i] != NULL; i++)
209 for (i = 0; argv[i] != NULL; i++)
211 new_argv = alloca (sizeof (char *) * count);
213 for (i = 0; wrapper_argv[i] != NULL; i++)
214 new_argv[count++] = wrapper_argv[i];
215 for (i = 0; argv[i] != NULL; i++)
216 new_argv[count++] = argv[i];
217 new_argv[count] = NULL;
223 for (i = 0; new_argv[i]; ++i)
224 fprintf (stderr, "new_argv[%d] = \"%s\"\n", i, new_argv[i]);
229 signal (SIGTTOU, SIG_DFL);
230 signal (SIGTTIN, SIG_DFL);
233 /* Clear this so the backend doesn't get confused, thinking
234 CONT_THREAD died, and it needs to resume all threads. */
235 cont_thread = null_ptid;
237 signal_pid = create_inferior (new_argv[0], new_argv);
239 /* FIXME: we don't actually know at this point that the create
240 actually succeeded. We won't know that until we wait. */
241 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
246 signal (SIGTTOU, SIG_IGN);
247 signal (SIGTTIN, SIG_IGN);
248 terminal_fd = fileno (stderr);
249 old_foreground_pgrp = tcgetpgrp (terminal_fd);
250 tcsetpgrp (terminal_fd, signal_pid);
251 atexit (restore_old_foreground_pgrp);
254 if (wrapper_argv != NULL)
256 struct thread_resume resume_info;
258 resume_info.thread = pid_to_ptid (signal_pid);
259 resume_info.kind = resume_continue;
262 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
264 if (last_status.kind != TARGET_WAITKIND_STOPPED)
269 (*the_target->resume) (&resume_info, 1);
271 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
272 if (last_status.kind != TARGET_WAITKIND_STOPPED)
275 current_inferior->last_resume_kind = resume_stop;
276 current_inferior->last_status = last_status;
278 while (last_status.value.sig != GDB_SIGNAL_TRAP);
283 /* Wait till we are at 1st instruction in program, return new pid
284 (assuming success). */
285 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
287 if (last_status.kind != TARGET_WAITKIND_EXITED
288 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
290 current_inferior->last_resume_kind = resume_stop;
291 current_inferior->last_status = last_status;
298 attach_inferior (int pid)
300 /* myattach should return -1 if attaching is unsupported,
301 0 if it succeeded, and call error() otherwise. */
303 if (myattach (pid) != 0)
306 fprintf (stderr, "Attached; pid = %d\n", pid);
309 /* FIXME - It may be that we should get the SIGNAL_PID from the
310 attach function, so that it can be the main thread instead of
311 whichever we were told to attach to. */
314 /* Clear this so the backend doesn't get confused, thinking
315 CONT_THREAD died, and it needs to resume all threads. */
316 cont_thread = null_ptid;
320 last_ptid = mywait (pid_to_ptid (pid), &last_status, 0, 0);
322 /* GDB knows to ignore the first SIGSTOP after attaching to a running
323 process using the "attach" command, but this is different; it's
324 just using "target remote". Pretend it's just starting up. */
325 if (last_status.kind == TARGET_WAITKIND_STOPPED
326 && last_status.value.sig == GDB_SIGNAL_STOP)
327 last_status.value.sig = GDB_SIGNAL_TRAP;
329 current_inferior->last_resume_kind = resume_stop;
330 current_inferior->last_status = last_status;
336 extern int remote_debug;
338 /* Decode a qXfer read request. Return 0 if everything looks OK,
342 decode_xfer_read (char *buf, CORE_ADDR *ofs, unsigned int *len)
344 /* After the read marker and annex, qXfer looks like a
345 traditional 'm' packet. */
346 decode_m_packet (buf, ofs, len);
352 decode_xfer (char *buf, char **object, char **rw, char **annex, char **offset)
354 /* Extract and NUL-terminate the object. */
356 while (*buf && *buf != ':')
362 /* Extract and NUL-terminate the read/write action. */
364 while (*buf && *buf != ':')
370 /* Extract and NUL-terminate the annex. */
372 while (*buf && *buf != ':')
382 /* Write the response to a successful qXfer read. Returns the
383 length of the (binary) data stored in BUF, corresponding
384 to as much of DATA/LEN as we could fit. IS_MORE controls
385 the first character of the response. */
387 write_qxfer_response (char *buf, const void *data, int len, int is_more)
396 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
400 /* Handle btrace enabling. */
403 handle_btrace_enable (struct thread_info *thread)
405 if (thread->btrace != NULL)
406 return "E.Btrace already enabled.";
408 thread->btrace = target_enable_btrace (thread->entry.id);
409 if (thread->btrace == NULL)
410 return "E.Could not enable btrace.";
415 /* Handle btrace disabling. */
418 handle_btrace_disable (struct thread_info *thread)
421 if (thread->btrace == NULL)
422 return "E.Branch tracing not enabled.";
424 if (target_disable_btrace (thread->btrace) != 0)
425 return "E.Could not disable branch tracing.";
427 thread->btrace = NULL;
431 /* Handle the "Qbtrace" packet. */
434 handle_btrace_general_set (char *own_buf)
436 struct thread_info *thread;
440 if (strncmp ("Qbtrace:", own_buf, strlen ("Qbtrace:")) != 0)
443 op = own_buf + strlen ("Qbtrace:");
445 if (!target_supports_btrace ())
447 strcpy (own_buf, "E.Target does not support branch tracing.");
451 if (ptid_equal (general_thread, null_ptid)
452 || ptid_equal (general_thread, minus_one_ptid))
454 strcpy (own_buf, "E.Must select a single thread.");
458 thread = find_thread_ptid (general_thread);
461 strcpy (own_buf, "E.No such thread.");
467 if (strcmp (op, "bts") == 0)
468 err = handle_btrace_enable (thread);
469 else if (strcmp (op, "off") == 0)
470 err = handle_btrace_disable (thread);
472 err = "E.Bad Qbtrace operation. Use bts or off.";
475 strcpy (own_buf, err);
482 /* Handle all of the extended 'Q' packets. */
485 handle_general_set (char *own_buf)
487 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
489 int numsigs = (int) GDB_SIGNAL_LAST, i;
490 const char *p = own_buf + strlen ("QPassSignals:");
493 p = decode_address_to_semicolon (&cursig, p);
494 for (i = 0; i < numsigs; i++)
500 /* Keep looping, to clear the remaining signals. */
503 p = decode_address_to_semicolon (&cursig, p);
508 strcpy (own_buf, "OK");
512 if (strncmp ("QProgramSignals:", own_buf, strlen ("QProgramSignals:")) == 0)
514 int numsigs = (int) GDB_SIGNAL_LAST, i;
515 const char *p = own_buf + strlen ("QProgramSignals:");
518 program_signals_p = 1;
520 p = decode_address_to_semicolon (&cursig, p);
521 for (i = 0; i < numsigs; i++)
525 program_signals[i] = 1;
527 /* Keep looping, to clear the remaining signals. */
530 p = decode_address_to_semicolon (&cursig, p);
533 program_signals[i] = 0;
535 strcpy (own_buf, "OK");
539 if (strcmp (own_buf, "QStartNoAckMode") == 0)
543 fprintf (stderr, "[noack mode enabled]\n");
552 if (strncmp (own_buf, "QNonStop:", 9) == 0)
554 char *mode = own_buf + 9;
558 if (strcmp (mode, "0") == 0)
560 else if (strcmp (mode, "1") == 0)
564 /* We don't know what this mode is, so complain to
566 fprintf (stderr, "Unknown non-stop mode requested: %s\n",
572 req_str = req ? "non-stop" : "all-stop";
573 if (start_non_stop (req) != 0)
575 fprintf (stderr, "Setting %s mode failed\n", req_str);
583 fprintf (stderr, "[%s mode enabled]\n", req_str);
589 if (strncmp ("QDisableRandomization:", own_buf,
590 strlen ("QDisableRandomization:")) == 0)
592 char *packet = own_buf + strlen ("QDisableRandomization:");
595 unpack_varlen_hex (packet, &setting);
596 disable_randomization = setting;
600 if (disable_randomization)
601 fprintf (stderr, "[address space randomization disabled]\n");
603 fprintf (stderr, "[address space randomization enabled]\n");
610 if (target_supports_tracepoints ()
611 && handle_tracepoint_general_set (own_buf))
614 if (strncmp ("QAgent:", own_buf, strlen ("QAgent:")) == 0)
616 char *mode = own_buf + strlen ("QAgent:");
619 if (strcmp (mode, "0") == 0)
621 else if (strcmp (mode, "1") == 0)
625 /* We don't know what this value is, so complain to GDB. */
626 sprintf (own_buf, "E.Unknown QAgent value");
630 /* Update the flag. */
633 fprintf (stderr, "[%s agent]\n", req ? "Enable" : "Disable");
638 if (handle_btrace_general_set (own_buf))
641 /* Otherwise we didn't know what packet it was. Say we didn't
647 get_features_xml (const char *annex)
649 /* gdbserver_xmltarget defines what to return when looking
650 for the "target.xml" file. Its contents can either be
651 verbatim XML code (prefixed with a '@') or else the name
652 of the actual XML file to be used in place of "target.xml".
654 This variable is set up from the auto-generated
655 init_registers_... routine for the current target. */
657 if (gdbserver_xmltarget
658 && strcmp (annex, "target.xml") == 0)
660 if (*gdbserver_xmltarget == '@')
661 return gdbserver_xmltarget + 1;
663 annex = gdbserver_xmltarget;
668 extern const char *const xml_builtin[][2];
671 /* Look for the annex. */
672 for (i = 0; xml_builtin[i][0] != NULL; i++)
673 if (strcmp (annex, xml_builtin[i][0]) == 0)
676 if (xml_builtin[i][0] != NULL)
677 return xml_builtin[i][1];
685 monitor_show_help (void)
687 monitor_output ("The following monitor commands are supported:\n");
688 monitor_output (" set debug <0|1>\n");
689 monitor_output (" Enable general debugging messages\n");
690 monitor_output (" set debug-hw-points <0|1>\n");
691 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
692 monitor_output (" set remote-debug <0|1>\n");
693 monitor_output (" Enable remote protocol debugging messages\n");
694 monitor_output (" exit\n");
695 monitor_output (" Quit GDBserver\n");
698 /* Read trace frame or inferior memory. Returns the number of bytes
699 actually read, zero when no further transfer is possible, and -1 on
700 error. Return of a positive value smaller than LEN does not
701 indicate there's no more to be read, only the end of the transfer.
702 E.g., when GDB reads memory from a traceframe, a first request may
703 be served from a memory block that does not cover the whole request
704 length. A following request gets the rest served from either
705 another block (of the same traceframe) or from the read-only
709 gdb_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
713 if (current_traceframe >= 0)
716 ULONGEST length = len;
718 if (traceframe_read_mem (current_traceframe,
719 memaddr, myaddr, len, &nbytes))
721 /* Data read from trace buffer, we're done. */
724 if (!in_readonly_region (memaddr, length))
726 /* Otherwise we have a valid readonly case, fall through. */
727 /* (assume no half-trace half-real blocks for now) */
730 res = prepare_to_access_memory ();
733 res = read_inferior_memory (memaddr, myaddr, len);
734 done_accessing_memory ();
736 return res == 0 ? len : -1;
742 /* Write trace frame or inferior memory. Actually, writing to trace
743 frames is forbidden. */
746 gdb_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
748 if (current_traceframe >= 0)
754 ret = prepare_to_access_memory ();
757 ret = write_inferior_memory (memaddr, myaddr, len);
758 done_accessing_memory ();
764 /* Subroutine of handle_search_memory to simplify it. */
767 handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len,
768 gdb_byte *pattern, unsigned pattern_len,
769 gdb_byte *search_buf,
770 unsigned chunk_size, unsigned search_buf_size,
771 CORE_ADDR *found_addrp)
773 /* Prime the search buffer. */
775 if (gdb_read_memory (start_addr, search_buf, search_buf_size)
778 warning ("Unable to access %ld bytes of target "
779 "memory at 0x%lx, halting search.",
780 (long) search_buf_size, (long) start_addr);
784 /* Perform the search.
786 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
787 When we've scanned N bytes we copy the trailing bytes to the start and
788 read in another N bytes. */
790 while (search_space_len >= pattern_len)
793 unsigned nr_search_bytes = (search_space_len < search_buf_size
797 found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len);
799 if (found_ptr != NULL)
801 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
802 *found_addrp = found_addr;
806 /* Not found in this chunk, skip to next chunk. */
808 /* Don't let search_space_len wrap here, it's unsigned. */
809 if (search_space_len >= chunk_size)
810 search_space_len -= chunk_size;
812 search_space_len = 0;
814 if (search_space_len >= pattern_len)
816 unsigned keep_len = search_buf_size - chunk_size;
817 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
820 /* Copy the trailing part of the previous iteration to the front
821 of the buffer for the next iteration. */
822 memcpy (search_buf, search_buf + chunk_size, keep_len);
824 nr_to_read = (search_space_len - keep_len < chunk_size
825 ? search_space_len - keep_len
828 if (gdb_read_memory (read_addr, search_buf + keep_len,
829 nr_to_read) != search_buf_size)
831 warning ("Unable to access %ld bytes of target memory "
832 "at 0x%lx, halting search.",
833 (long) nr_to_read, (long) read_addr);
837 start_addr += chunk_size;
846 /* Handle qSearch:memory packets. */
849 handle_search_memory (char *own_buf, int packet_len)
851 CORE_ADDR start_addr;
852 CORE_ADDR search_space_len;
854 unsigned int pattern_len;
855 /* NOTE: also defined in find.c testcase. */
856 #define SEARCH_CHUNK_SIZE 16000
857 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
858 /* Buffer to hold memory contents for searching. */
859 gdb_byte *search_buf;
860 unsigned search_buf_size;
862 CORE_ADDR found_addr;
863 int cmd_name_len = sizeof ("qSearch:memory:") - 1;
865 pattern = malloc (packet_len);
868 error ("Unable to allocate memory to perform the search");
869 strcpy (own_buf, "E00");
872 if (decode_search_memory_packet (own_buf + cmd_name_len,
873 packet_len - cmd_name_len,
874 &start_addr, &search_space_len,
875 pattern, &pattern_len) < 0)
878 error ("Error in parsing qSearch:memory packet");
879 strcpy (own_buf, "E00");
883 search_buf_size = chunk_size + pattern_len - 1;
885 /* No point in trying to allocate a buffer larger than the search space. */
886 if (search_space_len < search_buf_size)
887 search_buf_size = search_space_len;
889 search_buf = malloc (search_buf_size);
890 if (search_buf == NULL)
893 error ("Unable to allocate memory to perform the search");
894 strcpy (own_buf, "E00");
898 found = handle_search_memory_1 (start_addr, search_space_len,
899 pattern, pattern_len,
900 search_buf, chunk_size, search_buf_size,
904 sprintf (own_buf, "1,%lx", (long) found_addr);
906 strcpy (own_buf, "0");
908 strcpy (own_buf, "E00");
914 #define require_running(BUF) \
915 if (!target_running ()) \
921 /* Handle monitor commands not handled by target-specific handlers. */
924 handle_monitor_command (char *mon, char *own_buf)
926 if (strcmp (mon, "set debug 1") == 0)
929 monitor_output ("Debug output enabled.\n");
931 else if (strcmp (mon, "set debug 0") == 0)
934 monitor_output ("Debug output disabled.\n");
936 else if (strcmp (mon, "set debug-hw-points 1") == 0)
939 monitor_output ("H/W point debugging output enabled.\n");
941 else if (strcmp (mon, "set debug-hw-points 0") == 0)
944 monitor_output ("H/W point debugging output disabled.\n");
946 else if (strcmp (mon, "set remote-debug 1") == 0)
949 monitor_output ("Protocol debug output enabled.\n");
951 else if (strcmp (mon, "set remote-debug 0") == 0)
954 monitor_output ("Protocol debug output disabled.\n");
956 else if (strcmp (mon, "help") == 0)
957 monitor_show_help ();
958 else if (strcmp (mon, "exit") == 0)
962 monitor_output ("Unknown monitor command.\n\n");
963 monitor_show_help ();
968 /* Associates a callback with each supported qXfer'able object. */
972 /* The object this handler handles. */
975 /* Request that the target transfer up to LEN 8-bit bytes of the
976 target's OBJECT. The OFFSET, for a seekable object, specifies
977 the starting point. The ANNEX can be used to provide additional
978 data-specific information to the target.
980 Return the number of bytes actually transfered, zero when no
981 further transfer is possible, -1 on error, -2 when the transfer
982 is not supported, and -3 on a verbose error message that should
983 be preserved. Return of a positive value smaller than LEN does
984 not indicate the end of the object, only the end of the transfer.
986 One, and only one, of readbuf or writebuf must be non-NULL. */
987 int (*xfer) (const char *annex,
988 gdb_byte *readbuf, const gdb_byte *writebuf,
989 ULONGEST offset, LONGEST len);
992 /* Handle qXfer:auxv:read. */
995 handle_qxfer_auxv (const char *annex,
996 gdb_byte *readbuf, const gdb_byte *writebuf,
997 ULONGEST offset, LONGEST len)
999 if (the_target->read_auxv == NULL || writebuf != NULL)
1002 if (annex[0] != '\0' || !target_running ())
1005 return (*the_target->read_auxv) (offset, readbuf, len);
1008 /* Handle qXfer:features:read. */
1011 handle_qxfer_features (const char *annex,
1012 gdb_byte *readbuf, const gdb_byte *writebuf,
1013 ULONGEST offset, LONGEST len)
1015 const char *document;
1018 if (writebuf != NULL)
1021 if (!target_running ())
1024 /* Grab the correct annex. */
1025 document = get_features_xml (annex);
1026 if (document == NULL)
1029 total_len = strlen (document);
1031 if (offset > total_len)
1034 if (offset + len > total_len)
1035 len = total_len - offset;
1037 memcpy (readbuf, document + offset, len);
1041 /* Handle qXfer:libraries:read. */
1044 handle_qxfer_libraries (const char *annex,
1045 gdb_byte *readbuf, const gdb_byte *writebuf,
1046 ULONGEST offset, LONGEST len)
1048 unsigned int total_len;
1050 struct inferior_list_entry *dll_ptr;
1052 if (writebuf != NULL)
1055 if (annex[0] != '\0' || !target_running ())
1058 /* Over-estimate the necessary memory. Assume that every character
1059 in the library name must be escaped. */
1061 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
1062 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
1064 document = malloc (total_len);
1065 if (document == NULL)
1068 strcpy (document, "<library-list>\n");
1069 p = document + strlen (document);
1071 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
1073 struct dll_info *dll = (struct dll_info *) dll_ptr;
1076 strcpy (p, " <library name=\"");
1078 name = xml_escape_text (dll->name);
1082 strcpy (p, "\"><segment address=\"");
1084 sprintf (p, "0x%lx", (long) dll->base_addr);
1086 strcpy (p, "\"/></library>\n");
1090 strcpy (p, "</library-list>\n");
1092 total_len = strlen (document);
1094 if (offset > total_len)
1100 if (offset + len > total_len)
1101 len = total_len - offset;
1103 memcpy (readbuf, document + offset, len);
1108 /* Handle qXfer:libraries-svr4:read. */
1111 handle_qxfer_libraries_svr4 (const char *annex,
1112 gdb_byte *readbuf, const gdb_byte *writebuf,
1113 ULONGEST offset, LONGEST len)
1115 if (writebuf != NULL)
1118 if (!target_running () || the_target->qxfer_libraries_svr4 == NULL)
1121 return the_target->qxfer_libraries_svr4 (annex, readbuf, writebuf, offset, len);
1124 /* Handle qXfer:osadata:read. */
1127 handle_qxfer_osdata (const char *annex,
1128 gdb_byte *readbuf, const gdb_byte *writebuf,
1129 ULONGEST offset, LONGEST len)
1131 if (the_target->qxfer_osdata == NULL || writebuf != NULL)
1134 return (*the_target->qxfer_osdata) (annex, readbuf, NULL, offset, len);
1137 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1140 handle_qxfer_siginfo (const char *annex,
1141 gdb_byte *readbuf, const gdb_byte *writebuf,
1142 ULONGEST offset, LONGEST len)
1144 if (the_target->qxfer_siginfo == NULL)
1147 if (annex[0] != '\0' || !target_running ())
1150 return (*the_target->qxfer_siginfo) (annex, readbuf, writebuf, offset, len);
1153 /* Handle qXfer:spu:read and qXfer:spu:write. */
1156 handle_qxfer_spu (const char *annex,
1157 gdb_byte *readbuf, const gdb_byte *writebuf,
1158 ULONGEST offset, LONGEST len)
1160 if (the_target->qxfer_spu == NULL)
1163 if (!target_running ())
1166 return (*the_target->qxfer_spu) (annex, readbuf, writebuf, offset, len);
1169 /* Handle qXfer:statictrace:read. */
1172 handle_qxfer_statictrace (const char *annex,
1173 gdb_byte *readbuf, const gdb_byte *writebuf,
1174 ULONGEST offset, LONGEST len)
1178 if (writebuf != NULL)
1181 if (annex[0] != '\0' || !target_running () || current_traceframe == -1)
1184 if (traceframe_read_sdata (current_traceframe, offset,
1185 readbuf, len, &nbytes))
1190 /* Helper for handle_qxfer_threads. */
1193 handle_qxfer_threads_proper (struct buffer *buffer)
1195 struct inferior_list_entry *thread;
1197 buffer_grow_str (buffer, "<threads>\n");
1199 for (thread = all_threads.head; thread; thread = thread->next)
1201 ptid_t ptid = thread_to_gdb_id ((struct thread_info *)thread);
1203 int core = target_core_of_thread (ptid);
1206 write_ptid (ptid_s, ptid);
1210 sprintf (core_s, "%d", core);
1211 buffer_xml_printf (buffer, "<thread id=\"%s\" core=\"%s\"/>\n",
1216 buffer_xml_printf (buffer, "<thread id=\"%s\"/>\n",
1221 buffer_grow_str0 (buffer, "</threads>\n");
1224 /* Handle qXfer:threads:read. */
1227 handle_qxfer_threads (const char *annex,
1228 gdb_byte *readbuf, const gdb_byte *writebuf,
1229 ULONGEST offset, LONGEST len)
1231 static char *result = 0;
1232 static unsigned int result_length = 0;
1234 if (writebuf != NULL)
1237 if (!target_running () || annex[0] != '\0')
1242 struct buffer buffer;
1243 /* When asked for data at offset 0, generate everything and store into
1244 'result'. Successive reads will be served off 'result'. */
1248 buffer_init (&buffer);
1250 handle_qxfer_threads_proper (&buffer);
1252 result = buffer_finish (&buffer);
1253 result_length = strlen (result);
1254 buffer_free (&buffer);
1257 if (offset >= result_length)
1259 /* We're out of data. */
1266 if (len > result_length - offset)
1267 len = result_length - offset;
1269 memcpy (readbuf, result + offset, len);
1274 /* Handle qXfer:traceframe-info:read. */
1277 handle_qxfer_traceframe_info (const char *annex,
1278 gdb_byte *readbuf, const gdb_byte *writebuf,
1279 ULONGEST offset, LONGEST len)
1281 static char *result = 0;
1282 static unsigned int result_length = 0;
1284 if (writebuf != NULL)
1287 if (!target_running () || annex[0] != '\0' || current_traceframe == -1)
1292 struct buffer buffer;
1294 /* When asked for data at offset 0, generate everything and
1295 store into 'result'. Successive reads will be served off
1299 buffer_init (&buffer);
1301 traceframe_read_info (current_traceframe, &buffer);
1303 result = buffer_finish (&buffer);
1304 result_length = strlen (result);
1305 buffer_free (&buffer);
1308 if (offset >= result_length)
1310 /* We're out of data. */
1317 if (len > result_length - offset)
1318 len = result_length - offset;
1320 memcpy (readbuf, result + offset, len);
1324 /* Handle qXfer:fdpic:read. */
1327 handle_qxfer_fdpic (const char *annex, gdb_byte *readbuf,
1328 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1330 if (the_target->read_loadmap == NULL)
1333 if (!target_running ())
1336 return (*the_target->read_loadmap) (annex, offset, readbuf, len);
1339 /* Handle qXfer:btrace:read. */
1342 handle_qxfer_btrace (const char *annex,
1343 gdb_byte *readbuf, const gdb_byte *writebuf,
1344 ULONGEST offset, LONGEST len)
1346 static struct buffer cache;
1347 struct thread_info *thread;
1350 if (the_target->read_btrace == NULL || writebuf != NULL)
1353 if (!target_running ())
1356 if (ptid_equal (general_thread, null_ptid)
1357 || ptid_equal (general_thread, minus_one_ptid))
1359 strcpy (own_buf, "E.Must select a single thread.");
1363 thread = find_thread_ptid (general_thread);
1366 strcpy (own_buf, "E.No such thread.");
1370 if (thread->btrace == NULL)
1372 strcpy (own_buf, "E.Btrace not enabled.");
1376 if (strcmp (annex, "all") == 0)
1377 type = btrace_read_all;
1378 else if (strcmp (annex, "new") == 0)
1379 type = btrace_read_new;
1382 strcpy (own_buf, "E.Bad annex.");
1388 buffer_free (&cache);
1390 target_read_btrace (thread->btrace, &cache, type);
1392 else if (offset > cache.used_size)
1394 buffer_free (&cache);
1398 if (len > cache.used_size - offset)
1399 len = cache.used_size - offset;
1401 memcpy (readbuf, cache.buffer + offset, len);
1406 static const struct qxfer qxfer_packets[] =
1408 { "auxv", handle_qxfer_auxv },
1409 { "btrace", handle_qxfer_btrace },
1410 { "fdpic", handle_qxfer_fdpic},
1411 { "features", handle_qxfer_features },
1412 { "libraries", handle_qxfer_libraries },
1413 { "libraries-svr4", handle_qxfer_libraries_svr4 },
1414 { "osdata", handle_qxfer_osdata },
1415 { "siginfo", handle_qxfer_siginfo },
1416 { "spu", handle_qxfer_spu },
1417 { "statictrace", handle_qxfer_statictrace },
1418 { "threads", handle_qxfer_threads },
1419 { "traceframe-info", handle_qxfer_traceframe_info },
1423 handle_qxfer (char *own_buf, int packet_len, int *new_packet_len_p)
1431 if (strncmp (own_buf, "qXfer:", 6) != 0)
1434 /* Grab the object, r/w and annex. */
1435 if (decode_xfer (own_buf + 6, &object, &rw, &annex, &offset) < 0)
1437 write_enn (own_buf);
1442 i < sizeof (qxfer_packets) / sizeof (qxfer_packets[0]);
1445 const struct qxfer *q = &qxfer_packets[i];
1447 if (strcmp (object, q->object) == 0)
1449 if (strcmp (rw, "read") == 0)
1451 unsigned char *data;
1456 /* Grab the offset and length. */
1457 if (decode_xfer_read (offset, &ofs, &len) < 0)
1459 write_enn (own_buf);
1463 /* Read one extra byte, as an indicator of whether there is
1465 if (len > PBUFSIZ - 2)
1467 data = malloc (len + 1);
1470 write_enn (own_buf);
1473 n = (*q->xfer) (annex, data, NULL, ofs, len + 1);
1481 /* Preserve error message. */
1484 write_enn (own_buf);
1486 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1488 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1493 else if (strcmp (rw, "write") == 0)
1498 unsigned char *data;
1500 strcpy (own_buf, "E00");
1501 data = malloc (packet_len - (offset - own_buf));
1504 write_enn (own_buf);
1507 if (decode_xfer_write (offset, packet_len - (offset - own_buf),
1508 &ofs, &len, data) < 0)
1511 write_enn (own_buf);
1515 n = (*q->xfer) (annex, NULL, data, ofs, len);
1523 /* Preserve error message. */
1526 write_enn (own_buf);
1528 sprintf (own_buf, "%x", n);
1541 /* Table used by the crc32 function to calcuate the checksum. */
1543 static unsigned int crc32_table[256] =
1546 /* Compute 32 bit CRC from inferior memory.
1548 On success, return 32 bit CRC.
1549 On failure, return (unsigned long long) -1. */
1551 static unsigned long long
1552 crc32 (CORE_ADDR base, int len, unsigned int crc)
1554 if (!crc32_table[1])
1556 /* Initialize the CRC table and the decoding table. */
1560 for (i = 0; i < 256; i++)
1562 for (c = i << 24, j = 8; j > 0; --j)
1563 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
1570 unsigned char byte = 0;
1572 /* Return failure if memory read fails. */
1573 if (read_inferior_memory (base, &byte, 1) != 0)
1574 return (unsigned long long) -1;
1576 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255];
1579 return (unsigned long long) crc;
1582 /* Handle all of the extended 'q' packets. */
1585 handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
1587 static struct inferior_list_entry *thread_ptr;
1589 /* Reply the current thread id. */
1590 if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC)
1593 require_running (own_buf);
1595 if (!ptid_equal (general_thread, null_ptid)
1596 && !ptid_equal (general_thread, minus_one_ptid))
1597 gdb_id = general_thread;
1600 thread_ptr = all_threads.head;
1601 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
1604 sprintf (own_buf, "QC");
1606 write_ptid (own_buf, gdb_id);
1610 if (strcmp ("qSymbol::", own_buf) == 0)
1612 /* GDB is suggesting new symbols have been loaded. This may
1613 mean a new shared library has been detected as loaded, so
1614 take the opportunity to check if breakpoints we think are
1615 inserted, still are. Note that it isn't guaranteed that
1616 we'll see this when a shared library is loaded, and nor will
1617 we see this for unloads (although breakpoints in unloaded
1618 libraries shouldn't trigger), as GDB may not find symbols for
1619 the library at all. We also re-validate breakpoints when we
1620 see a second GDB breakpoint for the same address, and or when
1621 we access breakpoint shadows. */
1622 validate_breakpoints ();
1624 if (target_supports_tracepoints ())
1625 tracepoint_look_up_symbols ();
1627 if (target_running () && the_target->look_up_symbols != NULL)
1628 (*the_target->look_up_symbols) ();
1630 strcpy (own_buf, "OK");
1634 if (!disable_packet_qfThreadInfo)
1636 if (strcmp ("qfThreadInfo", own_buf) == 0)
1640 require_running (own_buf);
1641 thread_ptr = all_threads.head;
1644 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
1645 write_ptid (own_buf, gdb_id);
1646 thread_ptr = thread_ptr->next;
1650 if (strcmp ("qsThreadInfo", own_buf) == 0)
1654 require_running (own_buf);
1655 if (thread_ptr != NULL)
1658 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
1659 write_ptid (own_buf, gdb_id);
1660 thread_ptr = thread_ptr->next;
1665 sprintf (own_buf, "l");
1671 if (the_target->read_offsets != NULL
1672 && strcmp ("qOffsets", own_buf) == 0)
1674 CORE_ADDR text, data;
1676 require_running (own_buf);
1677 if (the_target->read_offsets (&text, &data))
1678 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
1679 (long)text, (long)data, (long)data);
1681 write_enn (own_buf);
1686 /* Protocol features query. */
1687 if (strncmp ("qSupported", own_buf, 10) == 0
1688 && (own_buf[10] == ':' || own_buf[10] == '\0'))
1690 char *p = &own_buf[10];
1691 int gdb_supports_qRelocInsn = 0;
1693 /* Start processing qSupported packet. */
1694 target_process_qsupported (NULL);
1696 /* Process each feature being provided by GDB. The first
1697 feature will follow a ':', and latter features will follow
1701 char **qsupported = NULL;
1705 /* Two passes, to avoid nested strtok calls in
1706 target_process_qsupported. */
1707 for (p = strtok (p + 1, ";");
1709 p = strtok (NULL, ";"))
1712 qsupported = xrealloc (qsupported, count * sizeof (char *));
1713 qsupported[count - 1] = xstrdup (p);
1716 for (i = 0; i < count; i++)
1719 if (strcmp (p, "multiprocess+") == 0)
1721 /* GDB supports and wants multi-process support if
1723 if (target_supports_multi_process ())
1726 else if (strcmp (p, "qRelocInsn+") == 0)
1728 /* GDB supports relocate instruction requests. */
1729 gdb_supports_qRelocInsn = 1;
1732 target_process_qsupported (p);
1741 "PacketSize=%x;QPassSignals+;QProgramSignals+",
1744 if (the_target->qxfer_libraries_svr4 != NULL)
1745 strcat (own_buf, ";qXfer:libraries-svr4:read+"
1746 ";augmented-libraries-svr4-read+");
1749 /* We do not have any hook to indicate whether the non-SVR4 target
1750 backend supports qXfer:libraries:read, so always report it. */
1751 strcat (own_buf, ";qXfer:libraries:read+");
1754 if (the_target->read_auxv != NULL)
1755 strcat (own_buf, ";qXfer:auxv:read+");
1757 if (the_target->qxfer_spu != NULL)
1758 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
1760 if (the_target->qxfer_siginfo != NULL)
1761 strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1763 if (the_target->read_loadmap != NULL)
1764 strcat (own_buf, ";qXfer:fdpic:read+");
1766 /* We always report qXfer:features:read, as targets may
1767 install XML files on a subsequent call to arch_setup.
1768 If we reported to GDB on startup that we don't support
1769 qXfer:feature:read at all, we will never be re-queried. */
1770 strcat (own_buf, ";qXfer:features:read+");
1772 if (transport_is_reliable)
1773 strcat (own_buf, ";QStartNoAckMode+");
1775 if (the_target->qxfer_osdata != NULL)
1776 strcat (own_buf, ";qXfer:osdata:read+");
1778 if (target_supports_multi_process ())
1779 strcat (own_buf, ";multiprocess+");
1781 if (target_supports_non_stop ())
1782 strcat (own_buf, ";QNonStop+");
1784 if (target_supports_disable_randomization ())
1785 strcat (own_buf, ";QDisableRandomization+");
1787 strcat (own_buf, ";qXfer:threads:read+");
1789 if (target_supports_tracepoints ())
1791 strcat (own_buf, ";ConditionalTracepoints+");
1792 strcat (own_buf, ";TraceStateVariables+");
1793 strcat (own_buf, ";TracepointSource+");
1794 strcat (own_buf, ";DisconnectedTracing+");
1795 if (gdb_supports_qRelocInsn && target_supports_fast_tracepoints ())
1796 strcat (own_buf, ";FastTracepoints+");
1797 strcat (own_buf, ";StaticTracepoints+");
1798 strcat (own_buf, ";InstallInTrace+");
1799 strcat (own_buf, ";qXfer:statictrace:read+");
1800 strcat (own_buf, ";qXfer:traceframe-info:read+");
1801 strcat (own_buf, ";EnableDisableTracepoints+");
1802 strcat (own_buf, ";QTBuffer:size+");
1803 strcat (own_buf, ";tracenz+");
1806 /* Support target-side breakpoint conditions and commands. */
1807 strcat (own_buf, ";ConditionalBreakpoints+");
1808 strcat (own_buf, ";BreakpointCommands+");
1810 if (target_supports_agent ())
1811 strcat (own_buf, ";QAgent+");
1813 if (target_supports_btrace ())
1815 strcat (own_buf, ";Qbtrace:bts+");
1816 strcat (own_buf, ";Qbtrace:off+");
1817 strcat (own_buf, ";qXfer:btrace:read+");
1823 /* Thread-local storage support. */
1824 if (the_target->get_tls_address != NULL
1825 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
1827 char *p = own_buf + 12;
1828 CORE_ADDR parts[2], address = 0;
1830 ptid_t ptid = null_ptid;
1832 require_running (own_buf);
1834 for (i = 0; i < 3; i++)
1842 p2 = strchr (p, ',');
1855 ptid = read_ptid (p, NULL);
1857 decode_address (&parts[i - 1], p, len);
1861 if (p != NULL || i < 3)
1865 struct thread_info *thread = find_thread_ptid (ptid);
1870 err = the_target->get_tls_address (thread, parts[0], parts[1],
1876 strcpy (own_buf, paddress(address));
1881 write_enn (own_buf);
1885 /* Otherwise, pretend we do not understand this packet. */
1888 /* Windows OS Thread Information Block address support. */
1889 if (the_target->get_tib_address != NULL
1890 && strncmp ("qGetTIBAddr:", own_buf, 12) == 0)
1895 ptid_t ptid = read_ptid (own_buf + 12, &annex);
1897 n = (*the_target->get_tib_address) (ptid, &tlb);
1900 strcpy (own_buf, paddress(tlb));
1905 write_enn (own_buf);
1911 /* Handle "monitor" commands. */
1912 if (strncmp ("qRcmd,", own_buf, 6) == 0)
1914 char *mon = malloc (PBUFSIZ);
1915 int len = strlen (own_buf + 6);
1919 write_enn (own_buf);
1923 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
1925 write_enn (own_buf);
1929 mon[len / 2] = '\0';
1933 if (the_target->handle_monitor_command == NULL
1934 || (*the_target->handle_monitor_command) (mon) == 0)
1935 /* Default processing. */
1936 handle_monitor_command (mon, own_buf);
1942 if (strncmp ("qSearch:memory:", own_buf,
1943 sizeof ("qSearch:memory:") - 1) == 0)
1945 require_running (own_buf);
1946 handle_search_memory (own_buf, packet_len);
1950 if (strcmp (own_buf, "qAttached") == 0
1951 || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1953 struct process_info *process;
1955 if (own_buf[sizeof ("qAttached") - 1])
1957 int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16);
1958 process = (struct process_info *)
1959 find_inferior_id (&all_processes, pid_to_ptid (pid));
1963 require_running (own_buf);
1964 process = current_process ();
1967 if (process == NULL)
1969 write_enn (own_buf);
1973 strcpy (own_buf, process->attached ? "1" : "0");
1977 if (strncmp ("qCRC:", own_buf, 5) == 0)
1979 /* CRC check (compare-section). */
1983 unsigned long long crc;
1985 require_running (own_buf);
1986 comma = unpack_varlen_hex (own_buf + 5, &base);
1987 if (*comma++ != ',')
1989 write_enn (own_buf);
1992 len = strtoul (comma, NULL, 16);
1993 crc = crc32 (base, len, 0xffffffff);
1994 /* Check for memory failure. */
1995 if (crc == (unsigned long long) -1)
1997 write_enn (own_buf);
2000 sprintf (own_buf, "C%lx", (unsigned long) crc);
2004 if (handle_qxfer (own_buf, packet_len, new_packet_len_p))
2007 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf))
2010 /* Otherwise we didn't know what packet it was. Say we didn't
2015 static void gdb_wants_all_threads_stopped (void);
2017 /* Parse vCont packets. */
2019 handle_v_cont (char *own_buf)
2023 struct thread_resume *resume_info;
2024 struct thread_resume default_action = {{0}};
2026 /* Count the number of semicolons in the packet. There should be one
2027 for every action. */
2033 p = strchr (p, ';');
2036 resume_info = malloc (n * sizeof (resume_info[0]));
2037 if (resume_info == NULL)
2045 memset (&resume_info[i], 0, sizeof resume_info[i]);
2047 if (p[0] == 's' || p[0] == 'S')
2048 resume_info[i].kind = resume_step;
2049 else if (p[0] == 'r')
2050 resume_info[i].kind = resume_step;
2051 else if (p[0] == 'c' || p[0] == 'C')
2052 resume_info[i].kind = resume_continue;
2053 else if (p[0] == 't')
2054 resume_info[i].kind = resume_stop;
2058 if (p[0] == 'S' || p[0] == 'C')
2061 sig = strtol (p + 1, &q, 16);
2066 if (!gdb_signal_to_host_p (sig))
2068 resume_info[i].sig = gdb_signal_to_host (sig);
2070 else if (p[0] == 'r')
2074 p = unpack_varlen_hex (p + 1, &addr);
2075 resume_info[i].step_range_start = addr;
2080 p = unpack_varlen_hex (p + 1, &addr);
2081 resume_info[i].step_range_end = addr;
2090 resume_info[i].thread = minus_one_ptid;
2091 default_action = resume_info[i];
2093 /* Note: we don't increment i here, we'll overwrite this entry
2094 the next time through. */
2096 else if (p[0] == ':')
2098 ptid_t ptid = read_ptid (p + 1, &q);
2103 if (p[0] != ';' && p[0] != 0)
2106 resume_info[i].thread = ptid;
2113 resume_info[i] = default_action;
2115 /* `cont_thread' is still used in occasional places in the backend,
2116 to implement single-thread scheduler-locking. Doesn't make sense
2117 to set it if we see a stop request, or a wildcard action (one
2118 with '-1' (all threads), or 'pPID.-1' (all threads of PID)). */
2120 && !(ptid_equal (resume_info[0].thread, minus_one_ptid)
2121 || ptid_get_lwp (resume_info[0].thread) == -1)
2122 && resume_info[0].kind != resume_stop)
2123 cont_thread = resume_info[0].thread;
2125 cont_thread = minus_one_ptid;
2126 set_desired_inferior (0);
2131 (*the_target->resume) (resume_info, n);
2139 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
2141 if (last_status.kind != TARGET_WAITKIND_EXITED
2142 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
2143 current_inferior->last_status = last_status;
2145 /* From the client's perspective, all-stop mode always stops all
2146 threads implicitly (and the target backend has already done
2147 so by now). Tag all threads as "want-stopped", so we don't
2148 resume them implicitly without the client telling us to. */
2149 gdb_wants_all_threads_stopped ();
2150 prepare_resume_reply (own_buf, last_ptid, &last_status);
2151 disable_async_io ();
2153 if (last_status.kind == TARGET_WAITKIND_EXITED
2154 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2155 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid)));
2160 write_enn (own_buf);
2165 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2167 handle_v_attach (char *own_buf)
2171 pid = strtol (own_buf + 8, NULL, 16);
2172 if (pid != 0 && attach_inferior (pid) == 0)
2174 /* Don't report shared library events after attaching, even if
2175 some libraries are preloaded. GDB will always poll the
2176 library list. Avoids the "stopped by shared library event"
2177 notice on the GDB side. */
2182 /* In non-stop, we don't send a resume reply. Stop events
2183 will follow up using the normal notification
2188 prepare_resume_reply (own_buf, last_ptid, &last_status);
2194 write_enn (own_buf);
2199 /* Run a new program. Return 1 if successful, 0 if failure. */
2201 handle_v_run (char *own_buf)
2203 char *p, *next_p, **new_argv;
2207 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
2213 new_argv = calloc (new_argc + 2, sizeof (char *));
2214 if (new_argv == NULL)
2216 write_enn (own_buf);
2221 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
2223 next_p = strchr (p, ';');
2225 next_p = p + strlen (p);
2227 if (i == 0 && p == next_p)
2231 /* FIXME: Fail request if out of memory instead of dying. */
2232 new_argv[i] = xmalloc (1 + (next_p - p) / 2);
2233 unhexify (new_argv[i], p, (next_p - p) / 2);
2234 new_argv[i][(next_p - p) / 2] = '\0';
2243 if (new_argv[0] == NULL)
2245 /* GDB didn't specify a program to run. Use the program from the
2246 last run with the new argument list. */
2248 if (program_argv == NULL)
2250 write_enn (own_buf);
2251 freeargv (new_argv);
2255 new_argv[0] = strdup (program_argv[0]);
2256 if (new_argv[0] == NULL)
2258 write_enn (own_buf);
2259 freeargv (new_argv);
2264 /* Free the old argv and install the new one. */
2265 freeargv (program_argv);
2266 program_argv = new_argv;
2268 start_inferior (program_argv);
2269 if (last_status.kind == TARGET_WAITKIND_STOPPED)
2271 prepare_resume_reply (own_buf, last_ptid, &last_status);
2273 /* In non-stop, sending a resume reply doesn't set the general
2274 thread, but GDB assumes a vRun sets it (this is so GDB can
2275 query which is the main thread of the new inferior. */
2277 general_thread = last_ptid;
2283 write_enn (own_buf);
2288 /* Kill process. Return 1 if successful, 0 if failure. */
2290 handle_v_kill (char *own_buf)
2293 char *p = &own_buf[6];
2295 pid = strtol (p, NULL, 16);
2298 if (pid != 0 && kill_inferior (pid) == 0)
2300 last_status.kind = TARGET_WAITKIND_SIGNALLED;
2301 last_status.value.sig = GDB_SIGNAL_KILL;
2302 last_ptid = pid_to_ptid (pid);
2303 discard_queued_stop_replies (pid);
2309 write_enn (own_buf);
2314 /* Handle all of the extended 'v' packets. */
2316 handle_v_requests (char *own_buf, int packet_len, int *new_packet_len)
2318 if (!disable_packet_vCont)
2320 if (strncmp (own_buf, "vCont;", 6) == 0)
2322 require_running (own_buf);
2323 handle_v_cont (own_buf);
2327 if (strncmp (own_buf, "vCont?", 6) == 0)
2329 strcpy (own_buf, "vCont;c;C;s;S;t");
2330 if (target_supports_range_stepping ())
2332 own_buf = own_buf + strlen (own_buf);
2333 strcpy (own_buf, ";r");
2339 if (strncmp (own_buf, "vFile:", 6) == 0
2340 && handle_vFile (own_buf, packet_len, new_packet_len))
2343 if (strncmp (own_buf, "vAttach;", 8) == 0)
2345 if ((!extended_protocol || !multi_process) && target_running ())
2347 fprintf (stderr, "Already debugging a process\n");
2348 write_enn (own_buf);
2351 handle_v_attach (own_buf);
2355 if (strncmp (own_buf, "vRun;", 5) == 0)
2357 if ((!extended_protocol || !multi_process) && target_running ())
2359 fprintf (stderr, "Already debugging a process\n");
2360 write_enn (own_buf);
2363 handle_v_run (own_buf);
2367 if (strncmp (own_buf, "vKill;", 6) == 0)
2369 if (!target_running ())
2371 fprintf (stderr, "No process to kill\n");
2372 write_enn (own_buf);
2375 handle_v_kill (own_buf);
2379 if (handle_notif_ack (own_buf, packet_len))
2382 /* Otherwise we didn't know what packet it was. Say we didn't
2388 /* Resume inferior and wait for another event. In non-stop mode,
2389 don't really wait here, but return immediatelly to the event
2392 myresume (char *own_buf, int step, int sig)
2394 struct thread_resume resume_info[2];
2396 int valid_cont_thread;
2398 set_desired_inferior (0);
2400 valid_cont_thread = (!ptid_equal (cont_thread, null_ptid)
2401 && !ptid_equal (cont_thread, minus_one_ptid));
2403 if (step || sig || valid_cont_thread)
2405 resume_info[0].thread = current_ptid;
2407 resume_info[0].kind = resume_step;
2409 resume_info[0].kind = resume_continue;
2410 resume_info[0].sig = sig;
2414 if (!valid_cont_thread)
2416 resume_info[n].thread = minus_one_ptid;
2417 resume_info[n].kind = resume_continue;
2418 resume_info[n].sig = 0;
2425 (*the_target->resume) (resume_info, n);
2431 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
2433 if (last_status.kind != TARGET_WAITKIND_EXITED
2434 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
2436 current_inferior->last_resume_kind = resume_stop;
2437 current_inferior->last_status = last_status;
2440 prepare_resume_reply (own_buf, last_ptid, &last_status);
2441 disable_async_io ();
2443 if (last_status.kind == TARGET_WAITKIND_EXITED
2444 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2445 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid)));
2449 /* Callback for for_each_inferior. Make a new stop reply for each
2453 queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg)
2455 struct thread_info *thread = (struct thread_info *) entry;
2457 /* For now, assume targets that don't have this callback also don't
2458 manage the thread's last_status field. */
2459 if (the_target->thread_stopped == NULL)
2461 struct vstop_notif *new_notif = xmalloc (sizeof (*new_notif));
2463 new_notif->ptid = entry->id;
2464 new_notif->status = thread->last_status;
2465 /* Pass the last stop reply back to GDB, but don't notify
2467 notif_event_enque (¬if_stop,
2468 (struct notif_event *) new_notif);
2472 if (thread_stopped (thread))
2476 "Reporting thread %s as already stopped with %s\n",
2477 target_pid_to_str (entry->id),
2478 target_waitstatus_to_string (&thread->last_status));
2480 gdb_assert (thread->last_status.kind != TARGET_WAITKIND_IGNORE);
2482 /* Pass the last stop reply back to GDB, but don't notify
2484 queue_stop_reply (entry->id, &thread->last_status);
2491 /* Set this inferior threads's state as "want-stopped". We won't
2492 resume this thread until the client gives us another action for
2496 gdb_wants_thread_stopped (struct inferior_list_entry *entry)
2498 struct thread_info *thread = (struct thread_info *) entry;
2500 thread->last_resume_kind = resume_stop;
2502 if (thread->last_status.kind == TARGET_WAITKIND_IGNORE)
2504 /* Most threads are stopped implicitly (all-stop); tag that with
2506 thread->last_status.kind = TARGET_WAITKIND_STOPPED;
2507 thread->last_status.value.sig = GDB_SIGNAL_0;
2511 /* Set all threads' states as "want-stopped". */
2514 gdb_wants_all_threads_stopped (void)
2516 for_each_inferior (&all_threads, gdb_wants_thread_stopped);
2519 /* Clear the gdb_detached flag of every process. */
2522 gdb_reattached_process (struct inferior_list_entry *entry)
2524 struct process_info *process = (struct process_info *) entry;
2526 process->gdb_detached = 0;
2529 /* Status handler for the '?' packet. */
2532 handle_status (char *own_buf)
2534 /* GDB is connected, don't forward events to the target anymore. */
2535 for_each_inferior (&all_processes, gdb_reattached_process);
2537 /* In non-stop mode, we must send a stop reply for each stopped
2538 thread. In all-stop mode, just send one for the first stopped
2543 discard_queued_stop_replies (-1);
2544 find_inferior (&all_threads, queue_stop_reply_callback, NULL);
2546 /* The first is sent immediatly. OK is sent if there is no
2547 stopped thread, which is the same handling of the vStopped
2548 packet (by design). */
2549 notif_write_event (¬if_stop, own_buf);
2554 stabilize_threads ();
2555 gdb_wants_all_threads_stopped ();
2557 if (all_threads.head)
2559 struct target_waitstatus status;
2561 status.kind = TARGET_WAITKIND_STOPPED;
2562 status.value.sig = GDB_SIGNAL_TRAP;
2563 prepare_resume_reply (own_buf,
2564 all_threads.head->id, &status);
2567 strcpy (own_buf, "W00");
2572 gdbserver_version (void)
2574 printf ("GNU gdbserver %s%s\n"
2575 "Copyright (C) 2013 Free Software Foundation, Inc.\n"
2576 "gdbserver is free software, covered by the "
2577 "GNU General Public License.\n"
2578 "This gdbserver was configured as \"%s\"\n",
2579 PKGVERSION, version, host_name);
2583 gdbserver_usage (FILE *stream)
2585 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2586 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2587 "\tgdbserver [OPTIONS] --multi COMM\n"
2589 "COMM may either be a tty device (for serial debugging), or \n"
2590 "HOST:PORT to listen for a TCP connection.\n"
2593 " --debug Enable general debugging output.\n"
2594 " --remote-debug Enable remote protocol debugging output.\n"
2595 " --version Display version information and exit.\n"
2596 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2597 " --once Exit after the first connection has "
2599 if (REPORT_BUGS_TO[0] && stream == stdout)
2600 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
2604 gdbserver_show_disableable (FILE *stream)
2606 fprintf (stream, "Disableable packets:\n"
2607 " vCont \tAll vCont packets\n"
2608 " qC \tQuerying the current thread\n"
2609 " qfThreadInfo\tThread listing\n"
2610 " Tthread \tPassing the thread specifier in the "
2611 "T stop reply packet\n"
2612 " threads \tAll of the above\n");
2616 #undef require_running
2617 #define require_running(BUF) \
2618 if (!target_running ()) \
2625 first_thread_of (struct inferior_list_entry *entry, void *args)
2627 int pid = * (int *) args;
2629 if (ptid_get_pid (entry->id) == pid)
2636 kill_inferior_callback (struct inferior_list_entry *entry)
2638 struct process_info *process = (struct process_info *) entry;
2639 int pid = ptid_get_pid (process->head.id);
2641 kill_inferior (pid);
2642 discard_queued_stop_replies (pid);
2645 /* Callback for for_each_inferior to detach or kill the inferior,
2646 depending on whether we attached to it or not.
2647 We inform the user whether we're detaching or killing the process
2648 as this is only called when gdbserver is about to exit. */
2651 detach_or_kill_inferior_callback (struct inferior_list_entry *entry)
2653 struct process_info *process = (struct process_info *) entry;
2654 int pid = ptid_get_pid (process->head.id);
2656 if (process->attached)
2657 detach_inferior (pid);
2659 kill_inferior (pid);
2661 discard_queued_stop_replies (pid);
2664 /* for_each_inferior callback for detach_or_kill_for_exit to print
2665 the pids of started inferiors. */
2668 print_started_pid (struct inferior_list_entry *entry)
2670 struct process_info *process = (struct process_info *) entry;
2672 if (! process->attached)
2674 int pid = ptid_get_pid (process->head.id);
2675 fprintf (stderr, " %d", pid);
2679 /* for_each_inferior callback for detach_or_kill_for_exit to print
2680 the pids of attached inferiors. */
2683 print_attached_pid (struct inferior_list_entry *entry)
2685 struct process_info *process = (struct process_info *) entry;
2687 if (process->attached)
2689 int pid = ptid_get_pid (process->head.id);
2690 fprintf (stderr, " %d", pid);
2694 /* Call this when exiting gdbserver with possible inferiors that need
2695 to be killed or detached from. */
2698 detach_or_kill_for_exit (void)
2700 /* First print a list of the inferiors we will be killing/detaching.
2701 This is to assist the user, for example, in case the inferior unexpectedly
2702 dies after we exit: did we screw up or did the inferior exit on its own?
2703 Having this info will save some head-scratching. */
2705 if (have_started_inferiors_p ())
2707 fprintf (stderr, "Killing process(es):");
2708 for_each_inferior (&all_processes, print_started_pid);
2709 fprintf (stderr, "\n");
2711 if (have_attached_inferiors_p ())
2713 fprintf (stderr, "Detaching process(es):");
2714 for_each_inferior (&all_processes, print_attached_pid);
2715 fprintf (stderr, "\n");
2718 /* Now we can kill or detach the inferiors. */
2720 for_each_inferior (&all_processes, detach_or_kill_inferior_callback);
2724 main (int argc, char *argv[])
2728 char *arg_end, *port;
2729 char **next_arg = &argv[1];
2730 volatile int multi_mode = 0;
2731 volatile int attach = 0;
2734 while (*next_arg != NULL && **next_arg == '-')
2736 if (strcmp (*next_arg, "--version") == 0)
2738 gdbserver_version ();
2741 else if (strcmp (*next_arg, "--help") == 0)
2743 gdbserver_usage (stdout);
2746 else if (strcmp (*next_arg, "--attach") == 0)
2748 else if (strcmp (*next_arg, "--multi") == 0)
2750 else if (strcmp (*next_arg, "--wrapper") == 0)
2754 wrapper_argv = next_arg;
2755 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
2758 if (next_arg == wrapper_argv || *next_arg == NULL)
2760 gdbserver_usage (stderr);
2764 /* Consume the "--". */
2767 else if (strcmp (*next_arg, "--debug") == 0)
2769 else if (strcmp (*next_arg, "--remote-debug") == 0)
2771 else if (strcmp (*next_arg, "--disable-packet") == 0)
2773 gdbserver_show_disableable (stdout);
2776 else if (strncmp (*next_arg,
2777 "--disable-packet=",
2778 sizeof ("--disable-packet=") - 1) == 0)
2780 char *packets, *tok;
2782 packets = *next_arg += sizeof ("--disable-packet=") - 1;
2783 for (tok = strtok (packets, ",");
2785 tok = strtok (NULL, ","))
2787 if (strcmp ("vCont", tok) == 0)
2788 disable_packet_vCont = 1;
2789 else if (strcmp ("Tthread", tok) == 0)
2790 disable_packet_Tthread = 1;
2791 else if (strcmp ("qC", tok) == 0)
2792 disable_packet_qC = 1;
2793 else if (strcmp ("qfThreadInfo", tok) == 0)
2794 disable_packet_qfThreadInfo = 1;
2795 else if (strcmp ("threads", tok) == 0)
2797 disable_packet_vCont = 1;
2798 disable_packet_Tthread = 1;
2799 disable_packet_qC = 1;
2800 disable_packet_qfThreadInfo = 1;
2804 fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
2806 gdbserver_show_disableable (stderr);
2811 else if (strcmp (*next_arg, "-") == 0)
2813 /* "-" specifies a stdio connection and is a form of port
2815 *next_arg = STDIO_CONNECTION_NAME;
2818 else if (strcmp (*next_arg, "--disable-randomization") == 0)
2819 disable_randomization = 1;
2820 else if (strcmp (*next_arg, "--no-disable-randomization") == 0)
2821 disable_randomization = 0;
2822 else if (strcmp (*next_arg, "--once") == 0)
2826 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
2834 if (setjmp (toplevel))
2836 fprintf (stderr, "Exiting\n");
2842 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
2844 gdbserver_usage (stderr);
2848 /* We need to know whether the remote connection is stdio before
2849 starting the inferior. Inferiors created in this scenario have
2850 stdin,stdout redirected. So do this here before we call
2852 remote_prepare (port);
2857 /* --attach used to come after PORT, so allow it there for
2859 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
2866 && (*next_arg == NULL
2867 || (*next_arg)[0] == '\0'
2868 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
2870 || next_arg[1] != NULL))
2875 gdbserver_usage (stderr);
2879 initialize_async_io ();
2881 initialize_event_loop ();
2882 if (target_supports_tracepoints ())
2883 initialize_tracepoint ();
2885 own_buf = xmalloc (PBUFSIZ + 1);
2886 mem_buf = xmalloc (PBUFSIZ);
2888 if (pid == 0 && *next_arg != NULL)
2892 n = argc - (next_arg - argv);
2893 program_argv = xmalloc (sizeof (char *) * (n + 1));
2894 for (i = 0; i < n; i++)
2895 program_argv[i] = xstrdup (next_arg[i]);
2896 program_argv[i] = NULL;
2898 /* Wait till we are at first instruction in program. */
2899 start_inferior (program_argv);
2901 /* We are now (hopefully) stopped at the first instruction of
2902 the target process. This assumes that the target process was
2903 successfully created. */
2907 if (attach_inferior (pid) == -1)
2908 error ("Attaching not supported on this target");
2910 /* Otherwise succeeded. */
2914 last_status.kind = TARGET_WAITKIND_EXITED;
2915 last_status.value.integer = 0;
2916 last_ptid = minus_one_ptid;
2919 initialize_notif ();
2921 /* Don't report shared library events on the initial connection,
2922 even if some libraries are preloaded. Avoids the "stopped by
2923 shared library event" notice on gdb side. */
2926 if (setjmp (toplevel))
2928 /* If something fails and longjmps while detaching or killing
2929 inferiors, we'd end up here again, stuck in an infinite loop
2930 trap. Be sure that if that happens, we exit immediately
2932 if (setjmp (toplevel) == 0)
2933 detach_or_kill_for_exit ();
2935 fprintf (stderr, "Detach or kill failed. Exiting\n");
2939 if (last_status.kind == TARGET_WAITKIND_EXITED
2940 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2945 if (!was_running && !multi_mode)
2947 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
2955 /* Be sure we're out of tfind mode. */
2956 current_traceframe = -1;
2960 if (setjmp (toplevel) != 0)
2962 /* An error occurred. */
2963 if (response_needed)
2965 write_enn (own_buf);
2970 /* Wait for events. This will return when all event sources are
2971 removed from the event loop. */
2972 start_event_loop ();
2974 /* If an exit was requested (using the "monitor exit" command),
2975 terminate now. The only other way to get here is for
2976 getpkt to fail; close the connection and reopen it at the
2979 if (exit_requested || run_once)
2981 /* If something fails and longjmps while detaching or
2982 killing inferiors, we'd end up here again, stuck in an
2983 infinite loop trap. Be sure that if that happens, we
2984 exit immediately instead. */
2985 if (setjmp (toplevel) == 0)
2987 detach_or_kill_for_exit ();
2992 fprintf (stderr, "Detach or kill failed. Exiting\n");
2998 "Remote side has terminated connection. "
2999 "GDBserver will reopen the connection.\n");
3003 if (disconnected_tracing)
3005 /* Try to enable non-stop/async mode, so we we can both
3006 wait for an async socket accept, and handle async
3007 target events simultaneously. There's also no point
3008 either in having the target always stop all threads,
3009 when we're going to pass signals down without
3013 if (start_non_stop (1))
3016 /* Detaching implicitly resumes all threads; simply
3017 disconnecting does not. */
3023 "Disconnected tracing disabled; stopping trace run.\n");
3030 /* Process options coming from Z packets for *point at address
3031 POINT_ADDR. PACKET is the packet buffer. *PACKET is updated
3032 to point to the first char after the last processed option. */
3035 process_point_options (CORE_ADDR point_addr, char **packet)
3037 char *dataptr = *packet;
3040 /* Check if data has the correct format. */
3041 if (*dataptr != ';')
3048 if (*dataptr == ';')
3051 if (*dataptr == 'X')
3053 /* Conditional expression. */
3055 fprintf (stderr, "Found breakpoint condition.\n");
3056 add_breakpoint_condition (point_addr, &dataptr);
3058 else if (strncmp (dataptr, "cmds:", strlen ("cmds:")) == 0)
3060 dataptr += strlen ("cmds:");
3062 fprintf (stderr, "Found breakpoint commands %s.\n", dataptr);
3063 persist = (*dataptr == '1');
3065 add_breakpoint_commands (point_addr, &dataptr, persist);
3069 fprintf (stderr, "Unknown token %c, ignoring.\n",
3071 /* Skip tokens until we find one that we recognize. */
3072 while (*dataptr && *dataptr != ';')
3079 /* Event loop callback that handles a serial event. The first byte in
3080 the serial buffer gets us here. We expect characters to arrive at
3081 a brisk pace, so we read the rest of the packet with a blocking
3085 process_serial_event (void)
3096 int new_packet_len = -1;
3098 /* Used to decide when gdbserver should exit in
3099 multi-mode/remote. */
3100 static int have_ran = 0;
3103 have_ran = target_running ();
3105 disable_async_io ();
3107 response_needed = 0;
3108 packet_len = getpkt (own_buf);
3109 if (packet_len <= 0)
3112 /* Force an event loop break. */
3115 response_needed = 1;
3122 handle_query (own_buf, packet_len, &new_packet_len);
3125 handle_general_set (own_buf);
3128 require_running (own_buf);
3133 pid = strtol (&own_buf[i], NULL, 16);
3136 pid = ptid_get_pid (current_ptid);
3138 if ((tracing && disconnected_tracing) || any_persistent_commands ())
3140 struct thread_resume resume_info;
3141 struct process_info *process = find_process_pid (pid);
3143 if (process == NULL)
3145 write_enn (own_buf);
3149 if (tracing && disconnected_tracing)
3151 "Disconnected tracing in effect, "
3152 "leaving gdbserver attached to the process\n");
3154 if (any_persistent_commands ())
3156 "Persistent commands are present, "
3157 "leaving gdbserver attached to the process\n");
3159 /* Make sure we're in non-stop/async mode, so we we can both
3160 wait for an async socket accept, and handle async target
3161 events simultaneously. There's also no point either in
3162 having the target stop all threads, when we're going to
3163 pass signals down without informing GDB. */
3167 fprintf (stderr, "Forcing non-stop mode\n");
3173 process->gdb_detached = 1;
3175 /* Detaching implicitly resumes all threads. */
3176 resume_info.thread = minus_one_ptid;
3177 resume_info.kind = resume_continue;
3178 resume_info.sig = 0;
3179 (*the_target->resume) (&resume_info, 1);
3182 break; /* from switch/case */
3185 fprintf (stderr, "Detaching from process %d\n", pid);
3187 if (detach_inferior (pid) != 0)
3188 write_enn (own_buf);
3191 discard_queued_stop_replies (pid);
3194 if (extended_protocol)
3196 /* Treat this like a normal program exit. */
3197 last_status.kind = TARGET_WAITKIND_EXITED;
3198 last_status.value.integer = 0;
3199 last_ptid = pid_to_ptid (pid);
3201 current_inferior = NULL;
3208 /* If we are attached, then we can exit. Otherwise, we
3209 need to hang around doing nothing, until the child is
3211 join_inferior (pid);
3217 extended_protocol = 1;
3221 handle_status (own_buf);
3224 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
3226 ptid_t gdb_id, thread_id;
3229 require_running (own_buf);
3231 gdb_id = read_ptid (&own_buf[2], NULL);
3233 pid = ptid_get_pid (gdb_id);
3235 if (ptid_equal (gdb_id, null_ptid)
3236 || ptid_equal (gdb_id, minus_one_ptid))
3237 thread_id = null_ptid;
3239 && ptid_equal (pid_to_ptid (pid),
3242 struct thread_info *thread =
3243 (struct thread_info *) find_inferior (&all_threads,
3248 write_enn (own_buf);
3252 thread_id = ((struct inferior_list_entry *)thread)->id;
3256 thread_id = gdb_id_to_thread_id (gdb_id);
3257 if (ptid_equal (thread_id, null_ptid))
3259 write_enn (own_buf);
3264 if (own_buf[1] == 'g')
3266 if (ptid_equal (thread_id, null_ptid))
3268 /* GDB is telling us to choose any thread. Check if
3269 the currently selected thread is still valid. If
3270 it is not, select the first available. */
3271 struct thread_info *thread =
3272 (struct thread_info *) find_inferior_id (&all_threads,
3275 thread_id = all_threads.head->id;
3278 general_thread = thread_id;
3279 set_desired_inferior (1);
3281 else if (own_buf[1] == 'c')
3282 cont_thread = thread_id;
3288 /* Silently ignore it so that gdb can extend the protocol
3289 without compatibility headaches. */
3294 require_running (own_buf);
3295 if (current_traceframe >= 0)
3297 struct regcache *regcache = new_register_cache ();
3299 if (fetch_traceframe_registers (current_traceframe,
3301 registers_to_string (regcache, own_buf);
3303 write_enn (own_buf);
3304 free_register_cache (regcache);
3308 struct regcache *regcache;
3310 set_desired_inferior (1);
3311 regcache = get_thread_regcache (current_inferior, 1);
3312 registers_to_string (regcache, own_buf);
3316 require_running (own_buf);
3317 if (current_traceframe >= 0)
3318 write_enn (own_buf);
3321 struct regcache *regcache;
3323 set_desired_inferior (1);
3324 regcache = get_thread_regcache (current_inferior, 1);
3325 registers_from_string (regcache, &own_buf[1]);
3330 require_running (own_buf);
3331 decode_m_packet (&own_buf[1], &mem_addr, &len);
3332 res = gdb_read_memory (mem_addr, mem_buf, len);
3334 write_enn (own_buf);
3336 convert_int_to_ascii (mem_buf, own_buf, res);
3339 require_running (own_buf);
3340 decode_M_packet (&own_buf[1], &mem_addr, &len, &mem_buf);
3341 if (gdb_write_memory (mem_addr, mem_buf, len) == 0)
3344 write_enn (own_buf);
3347 require_running (own_buf);
3348 if (decode_X_packet (&own_buf[1], packet_len - 1,
3349 &mem_addr, &len, &mem_buf) < 0
3350 || gdb_write_memory (mem_addr, mem_buf, len) != 0)
3351 write_enn (own_buf);
3356 require_running (own_buf);
3357 convert_ascii_to_int (own_buf + 1, &sig, 1);
3358 if (gdb_signal_to_host_p (sig))
3359 signal = gdb_signal_to_host (sig);
3362 myresume (own_buf, 0, signal);
3365 require_running (own_buf);
3366 convert_ascii_to_int (own_buf + 1, &sig, 1);
3367 if (gdb_signal_to_host_p (sig))
3368 signal = gdb_signal_to_host (sig);
3371 myresume (own_buf, 1, signal);
3374 require_running (own_buf);
3376 myresume (own_buf, 0, signal);
3379 require_running (own_buf);
3381 myresume (own_buf, 1, signal);
3383 case 'Z': /* insert_ ... */
3385 case 'z': /* remove_ ... */
3390 char type = own_buf[1];
3392 const int insert = ch == 'Z';
3393 char *p = &own_buf[3];
3395 p = unpack_varlen_hex (p, &addr);
3396 len = strtol (p + 1, &dataptr, 16);
3398 /* Default to unrecognized/unsupported. */
3402 case '0': /* software-breakpoint */
3403 case '1': /* hardware-breakpoint */
3404 case '2': /* write watchpoint */
3405 case '3': /* read watchpoint */
3406 case '4': /* access watchpoint */
3407 require_running (own_buf);
3408 if (insert && the_target->insert_point != NULL)
3410 /* Insert the breakpoint. If it is already inserted, nothing
3412 res = (*the_target->insert_point) (type, addr, len);
3414 /* GDB may have sent us a list of *point parameters to be
3415 evaluated on the target's side. Read such list here. If we
3416 already have a list of parameters, GDB is telling us to drop
3417 that list and use this one instead. */
3418 if (!res && (type == '0' || type == '1'))
3420 /* Remove previous conditions. */
3421 clear_gdb_breakpoint_conditions (addr);
3422 process_point_options (addr, &dataptr);
3425 else if (!insert && the_target->remove_point != NULL)
3426 res = (*the_target->remove_point) (type, addr, len);
3438 write_enn (own_buf);
3442 response_needed = 0;
3443 if (!target_running ())
3444 /* The packet we received doesn't make sense - but we can't
3445 reply to it, either. */
3448 fprintf (stderr, "Killing all inferiors\n");
3449 for_each_inferior (&all_processes, kill_inferior_callback);
3451 /* When using the extended protocol, we wait with no program
3452 running. The traditional protocol will exit instead. */
3453 if (extended_protocol)
3455 last_status.kind = TARGET_WAITKIND_EXITED;
3456 last_status.value.sig = GDB_SIGNAL_KILL;
3464 ptid_t gdb_id, thread_id;
3466 require_running (own_buf);
3468 gdb_id = read_ptid (&own_buf[1], NULL);
3469 thread_id = gdb_id_to_thread_id (gdb_id);
3470 if (ptid_equal (thread_id, null_ptid))
3472 write_enn (own_buf);
3476 if (mythread_alive (thread_id))
3479 write_enn (own_buf);
3483 response_needed = 0;
3485 /* Restarting the inferior is only supported in the extended
3487 if (extended_protocol)
3489 if (target_running ())
3490 for_each_inferior (&all_processes,
3491 kill_inferior_callback);
3492 fprintf (stderr, "GDBserver restarting\n");
3494 /* Wait till we are at 1st instruction in prog. */
3495 if (program_argv != NULL)
3496 start_inferior (program_argv);
3499 last_status.kind = TARGET_WAITKIND_EXITED;
3500 last_status.value.sig = GDB_SIGNAL_KILL;
3506 /* It is a request we don't understand. Respond with an
3507 empty packet so that gdb knows that we don't support this
3513 /* Extended (long) request. */
3514 handle_v_requests (own_buf, packet_len, &new_packet_len);
3518 /* It is a request we don't understand. Respond with an empty
3519 packet so that gdb knows that we don't support this
3525 if (new_packet_len != -1)
3526 putpkt_binary (own_buf, new_packet_len);
3530 response_needed = 0;
3532 if (!extended_protocol && have_ran && !target_running ())
3534 /* In non-stop, defer exiting until GDB had a chance to query
3535 the whole vStopped list (until it gets an OK). */
3536 if (QUEUE_is_empty (notif_event_p, notif_stop.queue))
3538 fprintf (stderr, "GDBserver exiting\n");
3550 /* Event-loop callback for serial events. */
3553 handle_serial_event (int err, gdb_client_data client_data)
3556 fprintf (stderr, "handling possible serial event\n");
3558 /* Really handle it. */
3559 if (process_serial_event () < 0)
3562 /* Be sure to not change the selected inferior behind GDB's back.
3563 Important in the non-stop mode asynchronous protocol. */
3564 set_desired_inferior (1);
3569 /* Event-loop callback for target events. */
3572 handle_target_event (int err, gdb_client_data client_data)
3575 fprintf (stderr, "handling possible target event\n");
3577 last_ptid = mywait (minus_one_ptid, &last_status,
3580 if (last_status.kind != TARGET_WAITKIND_IGNORE)
3582 int pid = ptid_get_pid (last_ptid);
3583 struct process_info *process = find_process_pid (pid);
3584 int forward_event = !gdb_connected () || process->gdb_detached;
3586 if (last_status.kind == TARGET_WAITKIND_EXITED
3587 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
3589 mark_breakpoints_out (process);
3590 mourn_inferior (process);
3594 /* We're reporting this thread as stopped. Update its
3595 "want-stopped" state to what the client wants, until it
3596 gets a new resume action. */
3597 current_inferior->last_resume_kind = resume_stop;
3598 current_inferior->last_status = last_status;
3603 if (!target_running ())
3605 /* The last process exited. We're done. */
3609 if (last_status.kind == TARGET_WAITKIND_STOPPED)
3611 /* A thread stopped with a signal, but gdb isn't
3612 connected to handle it. Pass it down to the
3613 inferior, as if it wasn't being traced. */
3614 struct thread_resume resume_info;
3618 "GDB not connected; forwarding event %d for [%s]\n",
3619 (int) last_status.kind,
3620 target_pid_to_str (last_ptid));
3622 resume_info.thread = last_ptid;
3623 resume_info.kind = resume_continue;
3624 resume_info.sig = gdb_signal_to_host (last_status.value.sig);
3625 (*the_target->resume) (&resume_info, 1);
3627 else if (debug_threads)
3628 fprintf (stderr, "GDB not connected; ignoring event %d for [%s]\n",
3629 (int) last_status.kind,
3630 target_pid_to_str (last_ptid));
3634 struct vstop_notif *vstop_notif
3635 = xmalloc (sizeof (struct vstop_notif));
3637 vstop_notif->status = last_status;
3638 vstop_notif->ptid = last_ptid;
3639 /* Push Stop notification. */
3640 notif_push (¬if_stop,
3641 (struct notif_event *) vstop_notif);
3645 /* Be sure to not change the selected inferior behind GDB's back.
3646 Important in the non-stop mode asynchronous protocol. */
3647 set_desired_inferior (1);