1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993-1995, 1997-2000, 2002-2012 Free Software
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
33 ptid_t general_thread;
37 static int extended_protocol;
38 static int response_needed;
39 static int exit_requested;
41 /* --once: Exit after the first connection has closed. */
47 /* Whether we should attempt to disable the operating system's address
48 space randomization feature before starting an inferior. */
49 int disable_randomization = 1;
51 static char **program_argv, **wrapper_argv;
53 /* Enable miscellaneous debugging output. The name is historical - it
54 was originally used to debug LinuxThreads support. */
57 /* Enable debugging of h/w breakpoint/watchpoint support. */
60 int pass_signals[TARGET_SIGNAL_LAST];
64 const char *gdbserver_xmltarget;
66 /* The PID of the originally created or attached inferior. Used to
67 send signals to the process when GDB sends us an asynchronous interrupt
68 (user hitting Control-C in the client), and to wait for the child to exit
69 when no longer debugging it. */
71 unsigned long signal_pid;
74 /* A file descriptor for the controlling terminal. */
77 /* TERMINAL_FD's original foreground group. */
78 pid_t old_foreground_pgrp;
80 /* Hand back terminal ownership to the original foreground group. */
83 restore_old_foreground_pgrp (void)
85 tcsetpgrp (terminal_fd, old_foreground_pgrp);
89 /* Set if you want to disable optional thread related packets support
90 in gdbserver, for the sake of testing GDB against stubs that don't
92 int disable_packet_vCont;
93 int disable_packet_Tthread;
94 int disable_packet_qC;
95 int disable_packet_qfThreadInfo;
97 /* Last status reported to GDB. */
98 static struct target_waitstatus last_status;
99 static ptid_t last_ptid;
101 static char *own_buf;
102 static unsigned char *mem_buf;
104 /* Structure holding information relative to a single stop reply. We
105 keep a queue of these (really a singly-linked list) to push to GDB
109 /* Pointer to next in list. */
110 struct vstop_notif *next;
112 /* Thread or process that got the event. */
116 struct target_waitstatus status;
119 /* The pending stop replies list head. */
120 static struct vstop_notif *notif_queue = NULL;
122 /* Put a stop reply to the stop reply queue. */
125 queue_stop_reply (ptid_t ptid, struct target_waitstatus *status)
127 struct vstop_notif *new_notif;
129 new_notif = xmalloc (sizeof (*new_notif));
130 new_notif->next = NULL;
131 new_notif->ptid = ptid;
132 new_notif->status = *status;
136 struct vstop_notif *tail;
137 for (tail = notif_queue;
141 tail->next = new_notif;
144 notif_queue = new_notif;
149 struct vstop_notif *n;
151 for (n = notif_queue; n; n = n->next)
154 fprintf (stderr, "pending stop replies: %d\n", i);
158 /* Place an event in the stop reply queue, and push a notification if
159 we aren't sending one yet. */
162 push_event (ptid_t ptid, struct target_waitstatus *status)
164 gdb_assert (status->kind != TARGET_WAITKIND_IGNORE);
166 queue_stop_reply (ptid, status);
168 /* If this is the first stop reply in the queue, then inform GDB
169 about it, by sending a Stop notification. */
170 if (notif_queue->next == NULL)
175 prepare_resume_reply (p,
176 notif_queue->ptid, ¬if_queue->status);
177 putpkt_notif (own_buf);
181 /* Get rid of the currently pending stop replies for PID. If PID is
182 -1, then apply to all processes. */
185 discard_queued_stop_replies (int pid)
187 struct vstop_notif *prev = NULL, *reply, *next;
189 for (reply = notif_queue; reply; reply = next)
194 || ptid_get_pid (reply->ptid) == pid)
196 if (reply == notif_queue)
199 prev->next = reply->next;
208 /* If there are more stop replies to push, push one now. */
211 send_next_stop_reply (char *own_buf)
214 prepare_resume_reply (own_buf,
216 ¬if_queue->status);
222 target_running (void)
224 return all_threads.head != NULL;
228 start_inferior (char **argv)
230 char **new_argv = argv;
232 if (wrapper_argv != NULL)
236 for (i = 0; wrapper_argv[i] != NULL; i++)
238 for (i = 0; argv[i] != NULL; i++)
240 new_argv = alloca (sizeof (char *) * count);
242 for (i = 0; wrapper_argv[i] != NULL; i++)
243 new_argv[count++] = wrapper_argv[i];
244 for (i = 0; argv[i] != NULL; i++)
245 new_argv[count++] = argv[i];
246 new_argv[count] = NULL;
252 for (i = 0; new_argv[i]; ++i)
253 fprintf (stderr, "new_argv[%d] = \"%s\"\n", i, new_argv[i]);
258 signal (SIGTTOU, SIG_DFL);
259 signal (SIGTTIN, SIG_DFL);
262 signal_pid = create_inferior (new_argv[0], new_argv);
264 /* FIXME: we don't actually know at this point that the create
265 actually succeeded. We won't know that until we wait. */
266 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
271 signal (SIGTTOU, SIG_IGN);
272 signal (SIGTTIN, SIG_IGN);
273 terminal_fd = fileno (stderr);
274 old_foreground_pgrp = tcgetpgrp (terminal_fd);
275 tcsetpgrp (terminal_fd, signal_pid);
276 atexit (restore_old_foreground_pgrp);
279 if (wrapper_argv != NULL)
281 struct thread_resume resume_info;
283 resume_info.thread = pid_to_ptid (signal_pid);
284 resume_info.kind = resume_continue;
287 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
289 if (last_status.kind != TARGET_WAITKIND_STOPPED)
294 (*the_target->resume) (&resume_info, 1);
296 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
297 if (last_status.kind != TARGET_WAITKIND_STOPPED)
300 current_inferior->last_resume_kind = resume_stop;
301 current_inferior->last_status = last_status;
303 while (last_status.value.sig != TARGET_SIGNAL_TRAP);
305 current_inferior->last_resume_kind = resume_stop;
306 current_inferior->last_status = last_status;
310 /* Wait till we are at 1st instruction in program, return new pid
311 (assuming success). */
312 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
314 if (last_status.kind != TARGET_WAITKIND_EXITED
315 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
317 current_inferior->last_resume_kind = resume_stop;
318 current_inferior->last_status = last_status;
325 attach_inferior (int pid)
327 /* myattach should return -1 if attaching is unsupported,
328 0 if it succeeded, and call error() otherwise. */
330 if (myattach (pid) != 0)
333 fprintf (stderr, "Attached; pid = %d\n", pid);
336 /* FIXME - It may be that we should get the SIGNAL_PID from the
337 attach function, so that it can be the main thread instead of
338 whichever we were told to attach to. */
341 /* Clear this so the backend doesn't get confused, thinking
342 CONT_THREAD died, and it needs to resume all threads. */
343 cont_thread = null_ptid;
347 last_ptid = mywait (pid_to_ptid (pid), &last_status, 0, 0);
349 /* GDB knows to ignore the first SIGSTOP after attaching to a running
350 process using the "attach" command, but this is different; it's
351 just using "target remote". Pretend it's just starting up. */
352 if (last_status.kind == TARGET_WAITKIND_STOPPED
353 && last_status.value.sig == TARGET_SIGNAL_STOP)
354 last_status.value.sig = TARGET_SIGNAL_TRAP;
356 current_inferior->last_resume_kind = resume_stop;
357 current_inferior->last_status = last_status;
363 extern int remote_debug;
365 /* Decode a qXfer read request. Return 0 if everything looks OK,
369 decode_xfer_read (char *buf, CORE_ADDR *ofs, unsigned int *len)
371 /* After the read marker and annex, qXfer looks like a
372 traditional 'm' packet. */
373 decode_m_packet (buf, ofs, len);
379 decode_xfer (char *buf, char **object, char **rw, char **annex, char **offset)
381 /* Extract and NUL-terminate the object. */
383 while (*buf && *buf != ':')
389 /* Extract and NUL-terminate the read/write action. */
391 while (*buf && *buf != ':')
397 /* Extract and NUL-terminate the annex. */
399 while (*buf && *buf != ':')
409 /* Write the response to a successful qXfer read. Returns the
410 length of the (binary) data stored in BUF, corresponding
411 to as much of DATA/LEN as we could fit. IS_MORE controls
412 the first character of the response. */
414 write_qxfer_response (char *buf, const void *data, int len, int is_more)
423 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
427 /* Handle all of the extended 'Q' packets. */
430 handle_general_set (char *own_buf)
432 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
434 int numsigs = (int) TARGET_SIGNAL_LAST, i;
435 const char *p = own_buf + strlen ("QPassSignals:");
438 p = decode_address_to_semicolon (&cursig, p);
439 for (i = 0; i < numsigs; i++)
445 /* Keep looping, to clear the remaining signals. */
448 p = decode_address_to_semicolon (&cursig, p);
453 strcpy (own_buf, "OK");
457 if (strcmp (own_buf, "QStartNoAckMode") == 0)
461 fprintf (stderr, "[noack mode enabled]\n");
470 if (strncmp (own_buf, "QNonStop:", 9) == 0)
472 char *mode = own_buf + 9;
476 if (strcmp (mode, "0") == 0)
478 else if (strcmp (mode, "1") == 0)
482 /* We don't know what this mode is, so complain to
484 fprintf (stderr, "Unknown non-stop mode requested: %s\n",
490 req_str = req ? "non-stop" : "all-stop";
491 if (start_non_stop (req) != 0)
493 fprintf (stderr, "Setting %s mode failed\n", req_str);
501 fprintf (stderr, "[%s mode enabled]\n", req_str);
507 if (strncmp ("QDisableRandomization:", own_buf,
508 strlen ("QDisableRandomization:")) == 0)
510 char *packet = own_buf + strlen ("QDisableRandomization:");
513 unpack_varlen_hex (packet, &setting);
514 disable_randomization = setting;
518 if (disable_randomization)
519 fprintf (stderr, "[address space randomization disabled]\n");
521 fprintf (stderr, "[address space randomization enabled]\n");
528 if (target_supports_tracepoints ()
529 && handle_tracepoint_general_set (own_buf))
532 /* Otherwise we didn't know what packet it was. Say we didn't
538 get_features_xml (const char *annex)
540 /* gdbserver_xmltarget defines what to return when looking
541 for the "target.xml" file. Its contents can either be
542 verbatim XML code (prefixed with a '@') or else the name
543 of the actual XML file to be used in place of "target.xml".
545 This variable is set up from the auto-generated
546 init_registers_... routine for the current target. */
548 if (gdbserver_xmltarget
549 && strcmp (annex, "target.xml") == 0)
551 if (*gdbserver_xmltarget == '@')
552 return gdbserver_xmltarget + 1;
554 annex = gdbserver_xmltarget;
559 extern const char *const xml_builtin[][2];
562 /* Look for the annex. */
563 for (i = 0; xml_builtin[i][0] != NULL; i++)
564 if (strcmp (annex, xml_builtin[i][0]) == 0)
567 if (xml_builtin[i][0] != NULL)
568 return xml_builtin[i][1];
576 monitor_show_help (void)
578 monitor_output ("The following monitor commands are supported:\n");
579 monitor_output (" set debug <0|1>\n");
580 monitor_output (" Enable general debugging messages\n");
581 monitor_output (" set debug-hw-points <0|1>\n");
582 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
583 monitor_output (" set remote-debug <0|1>\n");
584 monitor_output (" Enable remote protocol debugging messages\n");
585 monitor_output (" exit\n");
586 monitor_output (" Quit GDBserver\n");
589 /* Read trace frame or inferior memory. Returns the number of bytes
590 actually read, zero when no further transfer is possible, and -1 on
591 error. Return of a positive value smaller than LEN does not
592 indicate there's no more to be read, only the end of the transfer.
593 E.g., when GDB reads memory from a traceframe, a first request may
594 be served from a memory block that does not cover the whole request
595 length. A following request gets the rest served from either
596 another block (of the same traceframe) or from the read-only
600 gdb_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
604 if (current_traceframe >= 0)
607 ULONGEST length = len;
609 if (traceframe_read_mem (current_traceframe,
610 memaddr, myaddr, len, &nbytes))
612 /* Data read from trace buffer, we're done. */
615 if (!in_readonly_region (memaddr, length))
617 /* Otherwise we have a valid readonly case, fall through. */
618 /* (assume no half-trace half-real blocks for now) */
621 res = prepare_to_access_memory ();
624 res = read_inferior_memory (memaddr, myaddr, len);
625 done_accessing_memory ();
627 return res == 0 ? len : -1;
633 /* Write trace frame or inferior memory. Actually, writing to trace
634 frames is forbidden. */
637 gdb_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
639 if (current_traceframe >= 0)
645 ret = prepare_to_access_memory ();
648 ret = write_inferior_memory (memaddr, myaddr, len);
649 done_accessing_memory ();
655 /* Subroutine of handle_search_memory to simplify it. */
658 handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len,
659 gdb_byte *pattern, unsigned pattern_len,
660 gdb_byte *search_buf,
661 unsigned chunk_size, unsigned search_buf_size,
662 CORE_ADDR *found_addrp)
664 /* Prime the search buffer. */
666 if (gdb_read_memory (start_addr, search_buf, search_buf_size)
669 warning ("Unable to access target memory at 0x%lx, halting search.",
674 /* Perform the search.
676 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
677 When we've scanned N bytes we copy the trailing bytes to the start and
678 read in another N bytes. */
680 while (search_space_len >= pattern_len)
683 unsigned nr_search_bytes = (search_space_len < search_buf_size
687 found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len);
689 if (found_ptr != NULL)
691 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
692 *found_addrp = found_addr;
696 /* Not found in this chunk, skip to next chunk. */
698 /* Don't let search_space_len wrap here, it's unsigned. */
699 if (search_space_len >= chunk_size)
700 search_space_len -= chunk_size;
702 search_space_len = 0;
704 if (search_space_len >= pattern_len)
706 unsigned keep_len = search_buf_size - chunk_size;
707 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
710 /* Copy the trailing part of the previous iteration to the front
711 of the buffer for the next iteration. */
712 memcpy (search_buf, search_buf + chunk_size, keep_len);
714 nr_to_read = (search_space_len - keep_len < chunk_size
715 ? search_space_len - keep_len
718 if (gdb_read_memory (read_addr, search_buf + keep_len,
719 nr_to_read) != search_buf_size)
721 warning ("Unable to access target memory "
722 "at 0x%lx, halting search.",
727 start_addr += chunk_size;
736 /* Handle qSearch:memory packets. */
739 handle_search_memory (char *own_buf, int packet_len)
741 CORE_ADDR start_addr;
742 CORE_ADDR search_space_len;
744 unsigned int pattern_len;
745 /* NOTE: also defined in find.c testcase. */
746 #define SEARCH_CHUNK_SIZE 16000
747 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
748 /* Buffer to hold memory contents for searching. */
749 gdb_byte *search_buf;
750 unsigned search_buf_size;
752 CORE_ADDR found_addr;
753 int cmd_name_len = sizeof ("qSearch:memory:") - 1;
755 pattern = malloc (packet_len);
758 error ("Unable to allocate memory to perform the search");
759 strcpy (own_buf, "E00");
762 if (decode_search_memory_packet (own_buf + cmd_name_len,
763 packet_len - cmd_name_len,
764 &start_addr, &search_space_len,
765 pattern, &pattern_len) < 0)
768 error ("Error in parsing qSearch:memory packet");
769 strcpy (own_buf, "E00");
773 search_buf_size = chunk_size + pattern_len - 1;
775 /* No point in trying to allocate a buffer larger than the search space. */
776 if (search_space_len < search_buf_size)
777 search_buf_size = search_space_len;
779 search_buf = malloc (search_buf_size);
780 if (search_buf == NULL)
783 error ("Unable to allocate memory to perform the search");
784 strcpy (own_buf, "E00");
788 found = handle_search_memory_1 (start_addr, search_space_len,
789 pattern, pattern_len,
790 search_buf, chunk_size, search_buf_size,
794 sprintf (own_buf, "1,%lx", (long) found_addr);
796 strcpy (own_buf, "0");
798 strcpy (own_buf, "E00");
804 #define require_running(BUF) \
805 if (!target_running ()) \
811 /* Handle monitor commands not handled by target-specific handlers. */
814 handle_monitor_command (char *mon, char *own_buf)
816 if (strcmp (mon, "set debug 1") == 0)
819 monitor_output ("Debug output enabled.\n");
821 else if (strcmp (mon, "set debug 0") == 0)
824 monitor_output ("Debug output disabled.\n");
826 else if (strcmp (mon, "set debug-hw-points 1") == 0)
829 monitor_output ("H/W point debugging output enabled.\n");
831 else if (strcmp (mon, "set debug-hw-points 0") == 0)
834 monitor_output ("H/W point debugging output disabled.\n");
836 else if (strcmp (mon, "set remote-debug 1") == 0)
839 monitor_output ("Protocol debug output enabled.\n");
841 else if (strcmp (mon, "set remote-debug 0") == 0)
844 monitor_output ("Protocol debug output disabled.\n");
846 else if (strcmp (mon, "help") == 0)
847 monitor_show_help ();
848 else if (strcmp (mon, "exit") == 0)
852 monitor_output ("Unknown monitor command.\n\n");
853 monitor_show_help ();
858 /* Associates a callback with each supported qXfer'able object. */
862 /* The object this handler handles. */
865 /* Request that the target transfer up to LEN 8-bit bytes of the
866 target's OBJECT. The OFFSET, for a seekable object, specifies
867 the starting point. The ANNEX can be used to provide additional
868 data-specific information to the target.
870 Return the number of bytes actually transfered, zero when no
871 further transfer is possible, -1 on error, and -2 when the
872 transfer is not supported. Return of a positive value smaller
873 than LEN does not indicate the end of the object, only the end of
876 One, and only one, of readbuf or writebuf must be non-NULL. */
877 int (*xfer) (const char *annex,
878 gdb_byte *readbuf, const gdb_byte *writebuf,
879 ULONGEST offset, LONGEST len);
882 /* Handle qXfer:auxv:read. */
885 handle_qxfer_auxv (const char *annex,
886 gdb_byte *readbuf, const gdb_byte *writebuf,
887 ULONGEST offset, LONGEST len)
889 if (the_target->read_auxv == NULL || writebuf != NULL)
892 if (annex[0] != '\0' || !target_running ())
895 return (*the_target->read_auxv) (offset, readbuf, len);
898 /* Handle qXfer:features:read. */
901 handle_qxfer_features (const char *annex,
902 gdb_byte *readbuf, const gdb_byte *writebuf,
903 ULONGEST offset, LONGEST len)
905 const char *document;
908 if (writebuf != NULL)
911 if (!target_running ())
914 /* Grab the correct annex. */
915 document = get_features_xml (annex);
916 if (document == NULL)
919 total_len = strlen (document);
921 if (offset > total_len)
924 if (offset + len > total_len)
925 len = total_len - offset;
927 memcpy (readbuf, document + offset, len);
931 /* Handle qXfer:libraries:read. */
934 handle_qxfer_libraries (const char *annex,
935 gdb_byte *readbuf, const gdb_byte *writebuf,
936 ULONGEST offset, LONGEST len)
938 unsigned int total_len;
940 struct inferior_list_entry *dll_ptr;
942 if (writebuf != NULL)
945 if (annex[0] != '\0' || !target_running ())
948 /* Do not confuse this packet with qXfer:libraries-svr4:read. */
949 if (the_target->qxfer_libraries_svr4 != NULL)
952 /* Over-estimate the necessary memory. Assume that every character
953 in the library name must be escaped. */
955 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
956 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
958 document = malloc (total_len);
959 if (document == NULL)
962 strcpy (document, "<library-list>\n");
963 p = document + strlen (document);
965 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
967 struct dll_info *dll = (struct dll_info *) dll_ptr;
970 strcpy (p, " <library name=\"");
972 name = xml_escape_text (dll->name);
976 strcpy (p, "\"><segment address=\"");
978 sprintf (p, "0x%lx", (long) dll->base_addr);
980 strcpy (p, "\"/></library>\n");
984 strcpy (p, "</library-list>\n");
986 total_len = strlen (document);
988 if (offset > total_len)
994 if (offset + len > total_len)
995 len = total_len - offset;
997 memcpy (readbuf, document + offset, len);
1002 /* Handle qXfer:libraries-svr4:read. */
1005 handle_qxfer_libraries_svr4 (const char *annex,
1006 gdb_byte *readbuf, const gdb_byte *writebuf,
1007 ULONGEST offset, LONGEST len)
1009 if (writebuf != NULL)
1012 if (annex[0] != '\0' || !target_running ()
1013 || the_target->qxfer_libraries_svr4 == NULL)
1016 return the_target->qxfer_libraries_svr4 (annex, readbuf, writebuf, offset, len);
1019 /* Handle qXfer:osadata:read. */
1022 handle_qxfer_osdata (const char *annex,
1023 gdb_byte *readbuf, const gdb_byte *writebuf,
1024 ULONGEST offset, LONGEST len)
1026 if (the_target->qxfer_osdata == NULL || writebuf != NULL)
1029 return (*the_target->qxfer_osdata) (annex, readbuf, NULL, offset, len);
1032 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1035 handle_qxfer_siginfo (const char *annex,
1036 gdb_byte *readbuf, const gdb_byte *writebuf,
1037 ULONGEST offset, LONGEST len)
1039 if (the_target->qxfer_siginfo == NULL)
1042 if (annex[0] != '\0' || !target_running ())
1045 return (*the_target->qxfer_siginfo) (annex, readbuf, writebuf, offset, len);
1048 /* Handle qXfer:spu:read and qXfer:spu:write. */
1051 handle_qxfer_spu (const char *annex,
1052 gdb_byte *readbuf, const gdb_byte *writebuf,
1053 ULONGEST offset, LONGEST len)
1055 if (the_target->qxfer_spu == NULL)
1058 if (!target_running ())
1061 return (*the_target->qxfer_spu) (annex, readbuf, writebuf, offset, len);
1064 /* Handle qXfer:statictrace:read. */
1067 handle_qxfer_statictrace (const char *annex,
1068 gdb_byte *readbuf, const gdb_byte *writebuf,
1069 ULONGEST offset, LONGEST len)
1073 if (writebuf != NULL)
1076 if (annex[0] != '\0' || !target_running () || current_traceframe == -1)
1079 if (traceframe_read_sdata (current_traceframe, offset,
1080 readbuf, len, &nbytes))
1085 /* Helper for handle_qxfer_threads. */
1088 handle_qxfer_threads_proper (struct buffer *buffer)
1090 struct inferior_list_entry *thread;
1092 buffer_grow_str (buffer, "<threads>\n");
1094 for (thread = all_threads.head; thread; thread = thread->next)
1096 ptid_t ptid = thread_to_gdb_id ((struct thread_info *)thread);
1101 write_ptid (ptid_s, ptid);
1103 if (the_target->core_of_thread)
1104 core = (*the_target->core_of_thread) (ptid);
1108 sprintf (core_s, "%d", core);
1109 buffer_xml_printf (buffer, "<thread id=\"%s\" core=\"%s\"/>\n",
1114 buffer_xml_printf (buffer, "<thread id=\"%s\"/>\n",
1119 buffer_grow_str0 (buffer, "</threads>\n");
1122 /* Handle qXfer:threads:read. */
1125 handle_qxfer_threads (const char *annex,
1126 gdb_byte *readbuf, const gdb_byte *writebuf,
1127 ULONGEST offset, LONGEST len)
1129 static char *result = 0;
1130 static unsigned int result_length = 0;
1132 if (writebuf != NULL)
1135 if (!target_running () || annex[0] != '\0')
1140 struct buffer buffer;
1141 /* When asked for data at offset 0, generate everything and store into
1142 'result'. Successive reads will be served off 'result'. */
1146 buffer_init (&buffer);
1148 handle_qxfer_threads_proper (&buffer);
1150 result = buffer_finish (&buffer);
1151 result_length = strlen (result);
1152 buffer_free (&buffer);
1155 if (offset >= result_length)
1157 /* We're out of data. */
1164 if (len > result_length - offset)
1165 len = result_length - offset;
1167 memcpy (readbuf, result + offset, len);
1172 /* Handle qXfer:traceframe-info:read. */
1175 handle_qxfer_traceframe_info (const char *annex,
1176 gdb_byte *readbuf, const gdb_byte *writebuf,
1177 ULONGEST offset, LONGEST len)
1179 static char *result = 0;
1180 static unsigned int result_length = 0;
1182 if (writebuf != NULL)
1185 if (!target_running () || annex[0] != '\0' || current_traceframe == -1)
1190 struct buffer buffer;
1192 /* When asked for data at offset 0, generate everything and
1193 store into 'result'. Successive reads will be served off
1197 buffer_init (&buffer);
1199 traceframe_read_info (current_traceframe, &buffer);
1201 result = buffer_finish (&buffer);
1202 result_length = strlen (result);
1203 buffer_free (&buffer);
1206 if (offset >= result_length)
1208 /* We're out of data. */
1215 if (len > result_length - offset)
1216 len = result_length - offset;
1218 memcpy (readbuf, result + offset, len);
1222 /* Handle qXfer:fdpic:read. */
1225 handle_qxfer_fdpic (const char *annex, gdb_byte *readbuf,
1226 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1228 if (the_target->read_loadmap == NULL)
1231 if (!target_running ())
1234 return (*the_target->read_loadmap) (annex, offset, readbuf, len);
1237 static const struct qxfer qxfer_packets[] =
1239 { "auxv", handle_qxfer_auxv },
1240 { "fdpic", handle_qxfer_fdpic},
1241 { "features", handle_qxfer_features },
1242 { "libraries", handle_qxfer_libraries },
1243 { "libraries-svr4", handle_qxfer_libraries_svr4 },
1244 { "osdata", handle_qxfer_osdata },
1245 { "siginfo", handle_qxfer_siginfo },
1246 { "spu", handle_qxfer_spu },
1247 { "statictrace", handle_qxfer_statictrace },
1248 { "threads", handle_qxfer_threads },
1249 { "traceframe-info", handle_qxfer_traceframe_info },
1253 handle_qxfer (char *own_buf, int packet_len, int *new_packet_len_p)
1261 if (strncmp (own_buf, "qXfer:", 6) != 0)
1264 /* Grab the object, r/w and annex. */
1265 if (decode_xfer (own_buf + 6, &object, &rw, &annex, &offset) < 0)
1267 write_enn (own_buf);
1272 i < sizeof (qxfer_packets) / sizeof (qxfer_packets[0]);
1275 const struct qxfer *q = &qxfer_packets[i];
1277 if (strcmp (object, q->object) == 0)
1279 if (strcmp (rw, "read") == 0)
1281 unsigned char *data;
1286 /* Grab the offset and length. */
1287 if (decode_xfer_read (offset, &ofs, &len) < 0)
1289 write_enn (own_buf);
1293 /* Read one extra byte, as an indicator of whether there is
1295 if (len > PBUFSIZ - 2)
1297 data = malloc (len + 1);
1300 write_enn (own_buf);
1303 n = (*q->xfer) (annex, data, NULL, ofs, len + 1);
1310 write_enn (own_buf);
1312 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1314 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1319 else if (strcmp (rw, "write") == 0)
1324 unsigned char *data;
1326 strcpy (own_buf, "E00");
1327 data = malloc (packet_len - (offset - own_buf));
1330 write_enn (own_buf);
1333 if (decode_xfer_write (offset, packet_len - (offset - own_buf),
1334 &ofs, &len, data) < 0)
1337 write_enn (own_buf);
1341 n = (*q->xfer) (annex, NULL, data, ofs, len);
1348 write_enn (own_buf);
1350 sprintf (own_buf, "%x", n);
1363 /* Table used by the crc32 function to calcuate the checksum. */
1365 static unsigned int crc32_table[256] =
1368 /* Compute 32 bit CRC from inferior memory.
1370 On success, return 32 bit CRC.
1371 On failure, return (unsigned long long) -1. */
1373 static unsigned long long
1374 crc32 (CORE_ADDR base, int len, unsigned int crc)
1376 if (!crc32_table[1])
1378 /* Initialize the CRC table and the decoding table. */
1382 for (i = 0; i < 256; i++)
1384 for (c = i << 24, j = 8; j > 0; --j)
1385 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
1392 unsigned char byte = 0;
1394 /* Return failure if memory read fails. */
1395 if (read_inferior_memory (base, &byte, 1) != 0)
1396 return (unsigned long long) -1;
1398 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255];
1401 return (unsigned long long) crc;
1404 /* Handle all of the extended 'q' packets. */
1407 handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
1409 static struct inferior_list_entry *thread_ptr;
1411 /* Reply the current thread id. */
1412 if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC)
1415 require_running (own_buf);
1417 if (!ptid_equal (general_thread, null_ptid)
1418 && !ptid_equal (general_thread, minus_one_ptid))
1419 gdb_id = general_thread;
1422 thread_ptr = all_threads.head;
1423 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
1426 sprintf (own_buf, "QC");
1428 write_ptid (own_buf, gdb_id);
1432 if (strcmp ("qSymbol::", own_buf) == 0)
1434 /* GDB is suggesting new symbols have been loaded. This may
1435 mean a new shared library has been detected as loaded, so
1436 take the opportunity to check if breakpoints we think are
1437 inserted, still are. Note that it isn't guaranteed that
1438 we'll see this when a shared library is loaded, and nor will
1439 we see this for unloads (although breakpoints in unloaded
1440 libraries shouldn't trigger), as GDB may not find symbols for
1441 the library at all. We also re-validate breakpoints when we
1442 see a second GDB breakpoint for the same address, and or when
1443 we access breakpoint shadows. */
1444 validate_breakpoints ();
1446 if (target_supports_tracepoints ())
1447 tracepoint_look_up_symbols ();
1449 if (target_running () && the_target->look_up_symbols != NULL)
1450 (*the_target->look_up_symbols) ();
1452 strcpy (own_buf, "OK");
1456 if (!disable_packet_qfThreadInfo)
1458 if (strcmp ("qfThreadInfo", own_buf) == 0)
1462 require_running (own_buf);
1463 thread_ptr = all_threads.head;
1466 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
1467 write_ptid (own_buf, gdb_id);
1468 thread_ptr = thread_ptr->next;
1472 if (strcmp ("qsThreadInfo", own_buf) == 0)
1476 require_running (own_buf);
1477 if (thread_ptr != NULL)
1480 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
1481 write_ptid (own_buf, gdb_id);
1482 thread_ptr = thread_ptr->next;
1487 sprintf (own_buf, "l");
1493 if (the_target->read_offsets != NULL
1494 && strcmp ("qOffsets", own_buf) == 0)
1496 CORE_ADDR text, data;
1498 require_running (own_buf);
1499 if (the_target->read_offsets (&text, &data))
1500 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
1501 (long)text, (long)data, (long)data);
1503 write_enn (own_buf);
1508 /* Protocol features query. */
1509 if (strncmp ("qSupported", own_buf, 10) == 0
1510 && (own_buf[10] == ':' || own_buf[10] == '\0'))
1512 char *p = &own_buf[10];
1513 int gdb_supports_qRelocInsn = 0;
1515 /* Start processing qSupported packet. */
1516 target_process_qsupported (NULL);
1518 /* Process each feature being provided by GDB. The first
1519 feature will follow a ':', and latter features will follow
1523 char **qsupported = NULL;
1527 /* Two passes, to avoid nested strtok calls in
1528 target_process_qsupported. */
1529 for (p = strtok (p + 1, ";");
1531 p = strtok (NULL, ";"))
1534 qsupported = xrealloc (qsupported, count * sizeof (char *));
1535 qsupported[count - 1] = xstrdup (p);
1538 for (i = 0; i < count; i++)
1541 if (strcmp (p, "multiprocess+") == 0)
1543 /* GDB supports and wants multi-process support if
1545 if (target_supports_multi_process ())
1548 else if (strcmp (p, "qRelocInsn+") == 0)
1550 /* GDB supports relocate instruction requests. */
1551 gdb_supports_qRelocInsn = 1;
1554 target_process_qsupported (p);
1562 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
1564 if (the_target->qxfer_libraries_svr4 != NULL)
1565 strcat (own_buf, ";qXfer:libraries-svr4:read+");
1568 /* We do not have any hook to indicate whether the non-SVR4 target
1569 backend supports qXfer:libraries:read, so always report it. */
1570 strcat (own_buf, ";qXfer:libraries:read+");
1573 if (the_target->read_auxv != NULL)
1574 strcat (own_buf, ";qXfer:auxv:read+");
1576 if (the_target->qxfer_spu != NULL)
1577 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
1579 if (the_target->qxfer_siginfo != NULL)
1580 strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1582 if (the_target->read_loadmap != NULL)
1583 strcat (own_buf, ";qXfer:fdpic:read+");
1585 /* We always report qXfer:features:read, as targets may
1586 install XML files on a subsequent call to arch_setup.
1587 If we reported to GDB on startup that we don't support
1588 qXfer:feature:read at all, we will never be re-queried. */
1589 strcat (own_buf, ";qXfer:features:read+");
1591 if (transport_is_reliable)
1592 strcat (own_buf, ";QStartNoAckMode+");
1594 if (the_target->qxfer_osdata != NULL)
1595 strcat (own_buf, ";qXfer:osdata:read+");
1597 if (target_supports_multi_process ())
1598 strcat (own_buf, ";multiprocess+");
1600 if (target_supports_non_stop ())
1601 strcat (own_buf, ";QNonStop+");
1603 if (target_supports_disable_randomization ())
1604 strcat (own_buf, ";QDisableRandomization+");
1606 strcat (own_buf, ";qXfer:threads:read+");
1608 if (target_supports_tracepoints ())
1610 strcat (own_buf, ";ConditionalTracepoints+");
1611 strcat (own_buf, ";TraceStateVariables+");
1612 strcat (own_buf, ";TracepointSource+");
1613 strcat (own_buf, ";DisconnectedTracing+");
1614 if (gdb_supports_qRelocInsn && target_supports_fast_tracepoints ())
1615 strcat (own_buf, ";FastTracepoints+");
1616 strcat (own_buf, ";StaticTracepoints+");
1617 strcat (own_buf, ";InstallInTrace+");
1618 strcat (own_buf, ";qXfer:statictrace:read+");
1619 strcat (own_buf, ";qXfer:traceframe-info:read+");
1620 strcat (own_buf, ";EnableDisableTracepoints+");
1621 strcat (own_buf, ";tracenz+");
1627 /* Thread-local storage support. */
1628 if (the_target->get_tls_address != NULL
1629 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
1631 char *p = own_buf + 12;
1632 CORE_ADDR parts[2], address = 0;
1634 ptid_t ptid = null_ptid;
1636 require_running (own_buf);
1638 for (i = 0; i < 3; i++)
1646 p2 = strchr (p, ',');
1659 ptid = read_ptid (p, NULL);
1661 decode_address (&parts[i - 1], p, len);
1665 if (p != NULL || i < 3)
1669 struct thread_info *thread = find_thread_ptid (ptid);
1674 err = the_target->get_tls_address (thread, parts[0], parts[1],
1680 strcpy (own_buf, paddress(address));
1685 write_enn (own_buf);
1689 /* Otherwise, pretend we do not understand this packet. */
1692 /* Windows OS Thread Information Block address support. */
1693 if (the_target->get_tib_address != NULL
1694 && strncmp ("qGetTIBAddr:", own_buf, 12) == 0)
1699 ptid_t ptid = read_ptid (own_buf + 12, &annex);
1701 n = (*the_target->get_tib_address) (ptid, &tlb);
1704 strcpy (own_buf, paddress(tlb));
1709 write_enn (own_buf);
1715 /* Handle "monitor" commands. */
1716 if (strncmp ("qRcmd,", own_buf, 6) == 0)
1718 char *mon = malloc (PBUFSIZ);
1719 int len = strlen (own_buf + 6);
1723 write_enn (own_buf);
1727 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
1729 write_enn (own_buf);
1733 mon[len / 2] = '\0';
1737 if (the_target->handle_monitor_command == NULL
1738 || (*the_target->handle_monitor_command) (mon) == 0)
1739 /* Default processing. */
1740 handle_monitor_command (mon, own_buf);
1746 if (strncmp ("qSearch:memory:", own_buf,
1747 sizeof ("qSearch:memory:") - 1) == 0)
1749 require_running (own_buf);
1750 handle_search_memory (own_buf, packet_len);
1754 if (strcmp (own_buf, "qAttached") == 0
1755 || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1757 struct process_info *process;
1759 if (own_buf[sizeof ("qAttached") - 1])
1761 int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16);
1762 process = (struct process_info *)
1763 find_inferior_id (&all_processes, pid_to_ptid (pid));
1767 require_running (own_buf);
1768 process = current_process ();
1771 if (process == NULL)
1773 write_enn (own_buf);
1777 strcpy (own_buf, process->attached ? "1" : "0");
1781 if (strncmp ("qCRC:", own_buf, 5) == 0)
1783 /* CRC check (compare-section). */
1787 unsigned long long crc;
1789 require_running (own_buf);
1790 base = strtoul (own_buf + 5, &comma, 16);
1791 if (*comma++ != ',')
1793 write_enn (own_buf);
1796 len = strtoul (comma, NULL, 16);
1797 crc = crc32 (base, len, 0xffffffff);
1798 /* Check for memory failure. */
1799 if (crc == (unsigned long long) -1)
1801 write_enn (own_buf);
1804 sprintf (own_buf, "C%lx", (unsigned long) crc);
1808 if (handle_qxfer (own_buf, packet_len, new_packet_len_p))
1811 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf))
1814 /* Otherwise we didn't know what packet it was. Say we didn't
1819 static void gdb_wants_all_threads_stopped (void);
1821 /* Parse vCont packets. */
1823 handle_v_cont (char *own_buf)
1827 struct thread_resume *resume_info;
1828 struct thread_resume default_action = {{0}};
1830 /* Count the number of semicolons in the packet. There should be one
1831 for every action. */
1837 p = strchr (p, ';');
1840 resume_info = malloc (n * sizeof (resume_info[0]));
1841 if (resume_info == NULL)
1849 if (p[0] == 's' || p[0] == 'S')
1850 resume_info[i].kind = resume_step;
1851 else if (p[0] == 'c' || p[0] == 'C')
1852 resume_info[i].kind = resume_continue;
1853 else if (p[0] == 't')
1854 resume_info[i].kind = resume_stop;
1858 if (p[0] == 'S' || p[0] == 'C')
1861 sig = strtol (p + 1, &q, 16);
1866 if (!target_signal_to_host_p (sig))
1868 resume_info[i].sig = target_signal_to_host (sig);
1872 resume_info[i].sig = 0;
1878 resume_info[i].thread = minus_one_ptid;
1879 default_action = resume_info[i];
1881 /* Note: we don't increment i here, we'll overwrite this entry
1882 the next time through. */
1884 else if (p[0] == ':')
1886 ptid_t ptid = read_ptid (p + 1, &q);
1891 if (p[0] != ';' && p[0] != 0)
1894 resume_info[i].thread = ptid;
1901 resume_info[i] = default_action;
1903 /* Still used in occasional places in the backend. */
1905 && !ptid_equal (resume_info[0].thread, minus_one_ptid)
1906 && resume_info[0].kind != resume_stop)
1907 cont_thread = resume_info[0].thread;
1909 cont_thread = minus_one_ptid;
1910 set_desired_inferior (0);
1915 (*the_target->resume) (resume_info, n);
1923 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
1925 if (last_status.kind != TARGET_WAITKIND_EXITED
1926 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
1927 current_inferior->last_status = last_status;
1929 /* From the client's perspective, all-stop mode always stops all
1930 threads implicitly (and the target backend has already done
1931 so by now). Tag all threads as "want-stopped", so we don't
1932 resume them implicitly without the client telling us to. */
1933 gdb_wants_all_threads_stopped ();
1934 prepare_resume_reply (own_buf, last_ptid, &last_status);
1935 disable_async_io ();
1937 if (last_status.kind == TARGET_WAITKIND_EXITED
1938 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
1939 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid)));
1944 write_enn (own_buf);
1949 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1951 handle_v_attach (char *own_buf)
1955 pid = strtol (own_buf + 8, NULL, 16);
1956 if (pid != 0 && attach_inferior (pid) == 0)
1958 /* Don't report shared library events after attaching, even if
1959 some libraries are preloaded. GDB will always poll the
1960 library list. Avoids the "stopped by shared library event"
1961 notice on the GDB side. */
1966 /* In non-stop, we don't send a resume reply. Stop events
1967 will follow up using the normal notification
1972 prepare_resume_reply (own_buf, last_ptid, &last_status);
1978 write_enn (own_buf);
1983 /* Run a new program. Return 1 if successful, 0 if failure. */
1985 handle_v_run (char *own_buf)
1987 char *p, *next_p, **new_argv;
1991 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
1997 new_argv = calloc (new_argc + 2, sizeof (char *));
1998 if (new_argv == NULL)
2000 write_enn (own_buf);
2005 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
2007 next_p = strchr (p, ';');
2009 next_p = p + strlen (p);
2011 if (i == 0 && p == next_p)
2015 /* FIXME: Fail request if out of memory instead of dying. */
2016 new_argv[i] = xmalloc (1 + (next_p - p) / 2);
2017 unhexify (new_argv[i], p, (next_p - p) / 2);
2018 new_argv[i][(next_p - p) / 2] = '\0';
2027 if (new_argv[0] == NULL)
2029 /* GDB didn't specify a program to run. Use the program from the
2030 last run with the new argument list. */
2032 if (program_argv == NULL)
2034 write_enn (own_buf);
2035 freeargv (new_argv);
2039 new_argv[0] = strdup (program_argv[0]);
2040 if (new_argv[0] == NULL)
2042 write_enn (own_buf);
2043 freeargv (new_argv);
2048 /* Free the old argv and install the new one. */
2049 freeargv (program_argv);
2050 program_argv = new_argv;
2052 start_inferior (program_argv);
2053 if (last_status.kind == TARGET_WAITKIND_STOPPED)
2055 prepare_resume_reply (own_buf, last_ptid, &last_status);
2057 /* In non-stop, sending a resume reply doesn't set the general
2058 thread, but GDB assumes a vRun sets it (this is so GDB can
2059 query which is the main thread of the new inferior. */
2061 general_thread = last_ptid;
2067 write_enn (own_buf);
2072 /* Kill process. Return 1 if successful, 0 if failure. */
2074 handle_v_kill (char *own_buf)
2077 char *p = &own_buf[6];
2079 pid = strtol (p, NULL, 16);
2082 if (pid != 0 && kill_inferior (pid) == 0)
2084 last_status.kind = TARGET_WAITKIND_SIGNALLED;
2085 last_status.value.sig = TARGET_SIGNAL_KILL;
2086 last_ptid = pid_to_ptid (pid);
2087 discard_queued_stop_replies (pid);
2093 write_enn (own_buf);
2098 /* Handle a 'vStopped' packet. */
2100 handle_v_stopped (char *own_buf)
2102 /* If we're waiting for GDB to acknowledge a pending stop reply,
2103 consider that done. */
2106 struct vstop_notif *head;
2109 fprintf (stderr, "vStopped: acking %s\n",
2110 target_pid_to_str (notif_queue->ptid));
2113 notif_queue = notif_queue->next;
2117 /* Push another stop reply, or if there are no more left, an OK. */
2118 send_next_stop_reply (own_buf);
2121 /* Handle all of the extended 'v' packets. */
2123 handle_v_requests (char *own_buf, int packet_len, int *new_packet_len)
2125 if (!disable_packet_vCont)
2127 if (strncmp (own_buf, "vCont;", 6) == 0)
2129 require_running (own_buf);
2130 handle_v_cont (own_buf);
2134 if (strncmp (own_buf, "vCont?", 6) == 0)
2136 strcpy (own_buf, "vCont;c;C;s;S;t");
2141 if (strncmp (own_buf, "vFile:", 6) == 0
2142 && handle_vFile (own_buf, packet_len, new_packet_len))
2145 if (strncmp (own_buf, "vAttach;", 8) == 0)
2147 if ((!extended_protocol || !multi_process) && target_running ())
2149 fprintf (stderr, "Already debugging a process\n");
2150 write_enn (own_buf);
2153 handle_v_attach (own_buf);
2157 if (strncmp (own_buf, "vRun;", 5) == 0)
2159 if ((!extended_protocol || !multi_process) && target_running ())
2161 fprintf (stderr, "Already debugging a process\n");
2162 write_enn (own_buf);
2165 handle_v_run (own_buf);
2169 if (strncmp (own_buf, "vKill;", 6) == 0)
2171 if (!target_running ())
2173 fprintf (stderr, "No process to kill\n");
2174 write_enn (own_buf);
2177 handle_v_kill (own_buf);
2181 if (strncmp (own_buf, "vStopped", 8) == 0)
2183 handle_v_stopped (own_buf);
2187 /* Otherwise we didn't know what packet it was. Say we didn't
2193 /* Resume inferior and wait for another event. In non-stop mode,
2194 don't really wait here, but return immediatelly to the event
2197 myresume (char *own_buf, int step, int sig)
2199 struct thread_resume resume_info[2];
2201 int valid_cont_thread;
2203 set_desired_inferior (0);
2205 valid_cont_thread = (!ptid_equal (cont_thread, null_ptid)
2206 && !ptid_equal (cont_thread, minus_one_ptid));
2208 if (step || sig || valid_cont_thread)
2210 resume_info[0].thread
2211 = ((struct inferior_list_entry *) current_inferior)->id;
2213 resume_info[0].kind = resume_step;
2215 resume_info[0].kind = resume_continue;
2216 resume_info[0].sig = sig;
2220 if (!valid_cont_thread)
2222 resume_info[n].thread = minus_one_ptid;
2223 resume_info[n].kind = resume_continue;
2224 resume_info[n].sig = 0;
2231 (*the_target->resume) (resume_info, n);
2237 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
2239 if (last_status.kind != TARGET_WAITKIND_EXITED
2240 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
2242 current_inferior->last_resume_kind = resume_stop;
2243 current_inferior->last_status = last_status;
2246 prepare_resume_reply (own_buf, last_ptid, &last_status);
2247 disable_async_io ();
2249 if (last_status.kind == TARGET_WAITKIND_EXITED
2250 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2251 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid)));
2255 /* Callback for for_each_inferior. Make a new stop reply for each
2259 queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg)
2261 struct thread_info *thread = (struct thread_info *) entry;
2263 /* For now, assume targets that don't have this callback also don't
2264 manage the thread's last_status field. */
2265 if (the_target->thread_stopped == NULL)
2267 /* Pass the last stop reply back to GDB, but don't notify
2269 queue_stop_reply (entry->id, &thread->last_status);
2273 if (thread_stopped (thread))
2277 "Reporting thread %s as already stopped with %s\n",
2278 target_pid_to_str (entry->id),
2279 target_waitstatus_to_string (&thread->last_status));
2281 gdb_assert (thread->last_status.kind != TARGET_WAITKIND_IGNORE);
2283 /* Pass the last stop reply back to GDB, but don't notify
2285 queue_stop_reply (entry->id, &thread->last_status);
2292 /* Set this inferior threads's state as "want-stopped". We won't
2293 resume this thread until the client gives us another action for
2297 gdb_wants_thread_stopped (struct inferior_list_entry *entry)
2299 struct thread_info *thread = (struct thread_info *) entry;
2301 thread->last_resume_kind = resume_stop;
2303 if (thread->last_status.kind == TARGET_WAITKIND_IGNORE)
2305 /* Most threads are stopped implicitly (all-stop); tag that with
2307 thread->last_status.kind = TARGET_WAITKIND_STOPPED;
2308 thread->last_status.value.sig = TARGET_SIGNAL_0;
2312 /* Set all threads' states as "want-stopped". */
2315 gdb_wants_all_threads_stopped (void)
2317 for_each_inferior (&all_threads, gdb_wants_thread_stopped);
2320 /* Clear the gdb_detached flag of every process. */
2323 gdb_reattached_process (struct inferior_list_entry *entry)
2325 struct process_info *process = (struct process_info *) entry;
2327 process->gdb_detached = 0;
2330 /* Status handler for the '?' packet. */
2333 handle_status (char *own_buf)
2335 /* GDB is connected, don't forward events to the target anymore. */
2336 for_each_inferior (&all_processes, gdb_reattached_process);
2338 /* In non-stop mode, we must send a stop reply for each stopped
2339 thread. In all-stop mode, just send one for the first stopped
2344 discard_queued_stop_replies (-1);
2345 find_inferior (&all_threads, queue_stop_reply_callback, NULL);
2347 /* The first is sent immediatly. OK is sent if there is no
2348 stopped thread, which is the same handling of the vStopped
2349 packet (by design). */
2350 send_next_stop_reply (own_buf);
2355 stabilize_threads ();
2356 gdb_wants_all_threads_stopped ();
2358 if (all_threads.head)
2360 struct target_waitstatus status;
2362 status.kind = TARGET_WAITKIND_STOPPED;
2363 status.value.sig = TARGET_SIGNAL_TRAP;
2364 prepare_resume_reply (own_buf,
2365 all_threads.head->id, &status);
2368 strcpy (own_buf, "W00");
2373 gdbserver_version (void)
2375 printf ("GNU gdbserver %s%s\n"
2376 "Copyright (C) 2012 Free Software Foundation, Inc.\n"
2377 "gdbserver is free software, covered by the "
2378 "GNU General Public License.\n"
2379 "This gdbserver was configured as \"%s\"\n",
2380 PKGVERSION, version, host_name);
2384 gdbserver_usage (FILE *stream)
2386 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2387 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2388 "\tgdbserver [OPTIONS] --multi COMM\n"
2390 "COMM may either be a tty device (for serial debugging), or \n"
2391 "HOST:PORT to listen for a TCP connection.\n"
2394 " --debug Enable general debugging output.\n"
2395 " --remote-debug Enable remote protocol debugging output.\n"
2396 " --version Display version information and exit.\n"
2397 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2398 " --once Exit after the first connection has "
2400 if (REPORT_BUGS_TO[0] && stream == stdout)
2401 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
2405 gdbserver_show_disableable (FILE *stream)
2407 fprintf (stream, "Disableable packets:\n"
2408 " vCont \tAll vCont packets\n"
2409 " qC \tQuerying the current thread\n"
2410 " qfThreadInfo\tThread listing\n"
2411 " Tthread \tPassing the thread specifier in the "
2412 "T stop reply packet\n"
2413 " threads \tAll of the above\n");
2417 #undef require_running
2418 #define require_running(BUF) \
2419 if (!target_running ()) \
2426 first_thread_of (struct inferior_list_entry *entry, void *args)
2428 int pid = * (int *) args;
2430 if (ptid_get_pid (entry->id) == pid)
2437 kill_inferior_callback (struct inferior_list_entry *entry)
2439 struct process_info *process = (struct process_info *) entry;
2440 int pid = ptid_get_pid (process->head.id);
2442 kill_inferior (pid);
2443 discard_queued_stop_replies (pid);
2446 /* Callback for for_each_inferior to detach or kill the inferior,
2447 depending on whether we attached to it or not.
2448 We inform the user whether we're detaching or killing the process
2449 as this is only called when gdbserver is about to exit. */
2452 detach_or_kill_inferior_callback (struct inferior_list_entry *entry)
2454 struct process_info *process = (struct process_info *) entry;
2455 int pid = ptid_get_pid (process->head.id);
2457 if (process->attached)
2458 detach_inferior (pid);
2460 kill_inferior (pid);
2462 discard_queued_stop_replies (pid);
2465 /* for_each_inferior callback for detach_or_kill_for_exit to print
2466 the pids of started inferiors. */
2469 print_started_pid (struct inferior_list_entry *entry)
2471 struct process_info *process = (struct process_info *) entry;
2473 if (! process->attached)
2475 int pid = ptid_get_pid (process->head.id);
2476 fprintf (stderr, " %d", pid);
2480 /* for_each_inferior callback for detach_or_kill_for_exit to print
2481 the pids of attached inferiors. */
2484 print_attached_pid (struct inferior_list_entry *entry)
2486 struct process_info *process = (struct process_info *) entry;
2488 if (process->attached)
2490 int pid = ptid_get_pid (process->head.id);
2491 fprintf (stderr, " %d", pid);
2495 /* Call this when exiting gdbserver with possible inferiors that need
2496 to be killed or detached from. */
2499 detach_or_kill_for_exit (void)
2501 /* First print a list of the inferiors we will be killing/detaching.
2502 This is to assist the user, for example, in case the inferior unexpectedly
2503 dies after we exit: did we screw up or did the inferior exit on its own?
2504 Having this info will save some head-scratching. */
2506 if (have_started_inferiors_p ())
2508 fprintf (stderr, "Killing process(es):");
2509 for_each_inferior (&all_processes, print_started_pid);
2510 fprintf (stderr, "\n");
2512 if (have_attached_inferiors_p ())
2514 fprintf (stderr, "Detaching process(es):");
2515 for_each_inferior (&all_processes, print_attached_pid);
2516 fprintf (stderr, "\n");
2519 /* Now we can kill or detach the inferiors. */
2521 for_each_inferior (&all_processes, detach_or_kill_inferior_callback);
2525 main (int argc, char *argv[])
2529 char *arg_end, *port;
2530 char **next_arg = &argv[1];
2535 while (*next_arg != NULL && **next_arg == '-')
2537 if (strcmp (*next_arg, "--version") == 0)
2539 gdbserver_version ();
2542 else if (strcmp (*next_arg, "--help") == 0)
2544 gdbserver_usage (stdout);
2547 else if (strcmp (*next_arg, "--attach") == 0)
2549 else if (strcmp (*next_arg, "--multi") == 0)
2551 else if (strcmp (*next_arg, "--wrapper") == 0)
2555 wrapper_argv = next_arg;
2556 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
2559 if (next_arg == wrapper_argv || *next_arg == NULL)
2561 gdbserver_usage (stderr);
2565 /* Consume the "--". */
2568 else if (strcmp (*next_arg, "--debug") == 0)
2570 else if (strcmp (*next_arg, "--remote-debug") == 0)
2572 else if (strcmp (*next_arg, "--disable-packet") == 0)
2574 gdbserver_show_disableable (stdout);
2577 else if (strncmp (*next_arg,
2578 "--disable-packet=",
2579 sizeof ("--disable-packet=") - 1) == 0)
2581 char *packets, *tok;
2583 packets = *next_arg += sizeof ("--disable-packet=") - 1;
2584 for (tok = strtok (packets, ",");
2586 tok = strtok (NULL, ","))
2588 if (strcmp ("vCont", tok) == 0)
2589 disable_packet_vCont = 1;
2590 else if (strcmp ("Tthread", tok) == 0)
2591 disable_packet_Tthread = 1;
2592 else if (strcmp ("qC", tok) == 0)
2593 disable_packet_qC = 1;
2594 else if (strcmp ("qfThreadInfo", tok) == 0)
2595 disable_packet_qfThreadInfo = 1;
2596 else if (strcmp ("threads", tok) == 0)
2598 disable_packet_vCont = 1;
2599 disable_packet_Tthread = 1;
2600 disable_packet_qC = 1;
2601 disable_packet_qfThreadInfo = 1;
2605 fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
2607 gdbserver_show_disableable (stderr);
2612 else if (strcmp (*next_arg, "-") == 0)
2614 /* "-" specifies a stdio connection and is a form of port
2616 *next_arg = STDIO_CONNECTION_NAME;
2619 else if (strcmp (*next_arg, "--disable-randomization") == 0)
2620 disable_randomization = 1;
2621 else if (strcmp (*next_arg, "--no-disable-randomization") == 0)
2622 disable_randomization = 0;
2623 else if (strcmp (*next_arg, "--once") == 0)
2627 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
2635 if (setjmp (toplevel))
2637 fprintf (stderr, "Exiting\n");
2643 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
2645 gdbserver_usage (stderr);
2649 /* We need to know whether the remote connection is stdio before
2650 starting the inferior. Inferiors created in this scenario have
2651 stdin,stdout redirected. So do this here before we call
2653 remote_prepare (port);
2658 /* --attach used to come after PORT, so allow it there for
2660 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
2667 && (*next_arg == NULL
2668 || (*next_arg)[0] == '\0'
2669 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
2671 || next_arg[1] != NULL))
2676 gdbserver_usage (stderr);
2680 initialize_async_io ();
2682 if (target_supports_tracepoints ())
2683 initialize_tracepoint ();
2685 own_buf = xmalloc (PBUFSIZ + 1);
2686 mem_buf = xmalloc (PBUFSIZ);
2688 if (pid == 0 && *next_arg != NULL)
2692 n = argc - (next_arg - argv);
2693 program_argv = xmalloc (sizeof (char *) * (n + 1));
2694 for (i = 0; i < n; i++)
2695 program_argv[i] = xstrdup (next_arg[i]);
2696 program_argv[i] = NULL;
2698 /* Wait till we are at first instruction in program. */
2699 start_inferior (program_argv);
2701 /* We are now (hopefully) stopped at the first instruction of
2702 the target process. This assumes that the target process was
2703 successfully created. */
2707 if (attach_inferior (pid) == -1)
2708 error ("Attaching not supported on this target");
2710 /* Otherwise succeeded. */
2714 last_status.kind = TARGET_WAITKIND_EXITED;
2715 last_status.value.integer = 0;
2716 last_ptid = minus_one_ptid;
2719 /* Don't report shared library events on the initial connection,
2720 even if some libraries are preloaded. Avoids the "stopped by
2721 shared library event" notice on gdb side. */
2724 if (setjmp (toplevel))
2726 /* If something fails and longjmps while detaching or killing
2727 inferiors, we'd end up here again, stuck in an infinite loop
2728 trap. Be sure that if that happens, we exit immediately
2730 if (setjmp (toplevel) == 0)
2731 detach_or_kill_for_exit ();
2733 fprintf (stderr, "Detach or kill failed. Exiting\n");
2737 if (last_status.kind == TARGET_WAITKIND_EXITED
2738 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2743 if (!was_running && !multi_mode)
2745 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
2753 /* Be sure we're out of tfind mode. */
2754 current_traceframe = -1;
2758 if (setjmp (toplevel) != 0)
2760 /* An error occurred. */
2761 if (response_needed)
2763 write_enn (own_buf);
2768 /* Wait for events. This will return when all event sources are
2769 removed from the event loop. */
2770 start_event_loop ();
2772 /* If an exit was requested (using the "monitor exit" command),
2773 terminate now. The only other way to get here is for
2774 getpkt to fail; close the connection and reopen it at the
2777 if (exit_requested || run_once)
2779 /* If something fails and longjmps while detaching or
2780 killing inferiors, we'd end up here again, stuck in an
2781 infinite loop trap. Be sure that if that happens, we
2782 exit immediately instead. */
2783 if (setjmp (toplevel) == 0)
2785 detach_or_kill_for_exit ();
2790 fprintf (stderr, "Detach or kill failed. Exiting\n");
2796 "Remote side has terminated connection. "
2797 "GDBserver will reopen the connection.\n");
2801 if (disconnected_tracing)
2803 /* Try to enable non-stop/async mode, so we we can both
2804 wait for an async socket accept, and handle async
2805 target events simultaneously. There's also no point
2806 either in having the target always stop all threads,
2807 when we're going to pass signals down without
2811 if (start_non_stop (1))
2814 /* Detaching implicitly resumes all threads; simply
2815 disconnecting does not. */
2821 "Disconnected tracing disabled; stopping trace run.\n");
2828 /* Event loop callback that handles a serial event. The first byte in
2829 the serial buffer gets us here. We expect characters to arrive at
2830 a brisk pace, so we read the rest of the packet with a blocking
2834 process_serial_event (void)
2845 int new_packet_len = -1;
2847 /* Used to decide when gdbserver should exit in
2848 multi-mode/remote. */
2849 static int have_ran = 0;
2852 have_ran = target_running ();
2854 disable_async_io ();
2856 response_needed = 0;
2857 packet_len = getpkt (own_buf);
2858 if (packet_len <= 0)
2861 /* Force an event loop break. */
2864 response_needed = 1;
2871 handle_query (own_buf, packet_len, &new_packet_len);
2874 handle_general_set (own_buf);
2877 require_running (own_buf);
2882 pid = strtol (&own_buf[i], NULL, 16);
2886 ptid_get_pid (((struct inferior_list_entry *) current_inferior)->id);
2888 if (tracing && disconnected_tracing)
2890 struct thread_resume resume_info;
2891 struct process_info *process = find_process_pid (pid);
2893 if (process == NULL)
2895 write_enn (own_buf);
2900 "Disconnected tracing in effect, "
2901 "leaving gdbserver attached to the process\n");
2903 /* Make sure we're in non-stop/async mode, so we we can both
2904 wait for an async socket accept, and handle async target
2905 events simultaneously. There's also no point either in
2906 having the target stop all threads, when we're going to
2907 pass signals down without informing GDB. */
2911 fprintf (stderr, "Forcing non-stop mode\n");
2917 process->gdb_detached = 1;
2919 /* Detaching implicitly resumes all threads. */
2920 resume_info.thread = minus_one_ptid;
2921 resume_info.kind = resume_continue;
2922 resume_info.sig = 0;
2923 (*the_target->resume) (&resume_info, 1);
2926 break; /* from switch/case */
2929 fprintf (stderr, "Detaching from process %d\n", pid);
2931 if (detach_inferior (pid) != 0)
2932 write_enn (own_buf);
2935 discard_queued_stop_replies (pid);
2938 if (extended_protocol)
2940 /* Treat this like a normal program exit. */
2941 last_status.kind = TARGET_WAITKIND_EXITED;
2942 last_status.value.integer = 0;
2943 last_ptid = pid_to_ptid (pid);
2945 current_inferior = NULL;
2952 /* If we are attached, then we can exit. Otherwise, we
2953 need to hang around doing nothing, until the child is
2955 join_inferior (pid);
2961 extended_protocol = 1;
2965 handle_status (own_buf);
2968 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
2970 ptid_t gdb_id, thread_id;
2973 require_running (own_buf);
2975 gdb_id = read_ptid (&own_buf[2], NULL);
2977 pid = ptid_get_pid (gdb_id);
2979 if (ptid_equal (gdb_id, null_ptid)
2980 || ptid_equal (gdb_id, minus_one_ptid))
2981 thread_id = null_ptid;
2983 && ptid_equal (pid_to_ptid (pid),
2986 struct thread_info *thread =
2987 (struct thread_info *) find_inferior (&all_threads,
2992 write_enn (own_buf);
2996 thread_id = ((struct inferior_list_entry *)thread)->id;
3000 thread_id = gdb_id_to_thread_id (gdb_id);
3001 if (ptid_equal (thread_id, null_ptid))
3003 write_enn (own_buf);
3008 if (own_buf[1] == 'g')
3010 if (ptid_equal (thread_id, null_ptid))
3012 /* GDB is telling us to choose any thread. Check if
3013 the currently selected thread is still valid. If
3014 it is not, select the first available. */
3015 struct thread_info *thread =
3016 (struct thread_info *) find_inferior_id (&all_threads,
3019 thread_id = all_threads.head->id;
3022 general_thread = thread_id;
3023 set_desired_inferior (1);
3025 else if (own_buf[1] == 'c')
3026 cont_thread = thread_id;
3032 /* Silently ignore it so that gdb can extend the protocol
3033 without compatibility headaches. */
3038 require_running (own_buf);
3039 if (current_traceframe >= 0)
3041 struct regcache *regcache = new_register_cache ();
3043 if (fetch_traceframe_registers (current_traceframe,
3045 registers_to_string (regcache, own_buf);
3047 write_enn (own_buf);
3048 free_register_cache (regcache);
3052 struct regcache *regcache;
3054 set_desired_inferior (1);
3055 regcache = get_thread_regcache (current_inferior, 1);
3056 registers_to_string (regcache, own_buf);
3060 require_running (own_buf);
3061 if (current_traceframe >= 0)
3062 write_enn (own_buf);
3065 struct regcache *regcache;
3067 set_desired_inferior (1);
3068 regcache = get_thread_regcache (current_inferior, 1);
3069 registers_from_string (regcache, &own_buf[1]);
3074 require_running (own_buf);
3075 decode_m_packet (&own_buf[1], &mem_addr, &len);
3076 res = gdb_read_memory (mem_addr, mem_buf, len);
3078 write_enn (own_buf);
3080 convert_int_to_ascii (mem_buf, own_buf, res);
3083 require_running (own_buf);
3084 decode_M_packet (&own_buf[1], &mem_addr, &len, &mem_buf);
3085 if (gdb_write_memory (mem_addr, mem_buf, len) == 0)
3088 write_enn (own_buf);
3091 require_running (own_buf);
3092 if (decode_X_packet (&own_buf[1], packet_len - 1,
3093 &mem_addr, &len, &mem_buf) < 0
3094 || gdb_write_memory (mem_addr, mem_buf, len) != 0)
3095 write_enn (own_buf);
3100 require_running (own_buf);
3101 convert_ascii_to_int (own_buf + 1, &sig, 1);
3102 if (target_signal_to_host_p (sig))
3103 signal = target_signal_to_host (sig);
3106 myresume (own_buf, 0, signal);
3109 require_running (own_buf);
3110 convert_ascii_to_int (own_buf + 1, &sig, 1);
3111 if (target_signal_to_host_p (sig))
3112 signal = target_signal_to_host (sig);
3115 myresume (own_buf, 1, signal);
3118 require_running (own_buf);
3120 myresume (own_buf, 0, signal);
3123 require_running (own_buf);
3125 myresume (own_buf, 1, signal);
3127 case 'Z': /* insert_ ... */
3129 case 'z': /* remove_ ... */
3133 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
3134 int len = strtol (lenptr + 1, &dataptr, 16);
3135 char type = own_buf[1];
3137 const int insert = ch == 'Z';
3139 /* Default to unrecognized/unsupported. */
3143 case '0': /* software-breakpoint */
3144 case '1': /* hardware-breakpoint */
3145 case '2': /* write watchpoint */
3146 case '3': /* read watchpoint */
3147 case '4': /* access watchpoint */
3148 require_running (own_buf);
3149 if (insert && the_target->insert_point != NULL)
3150 res = (*the_target->insert_point) (type, addr, len);
3151 else if (!insert && the_target->remove_point != NULL)
3152 res = (*the_target->remove_point) (type, addr, len);
3164 write_enn (own_buf);
3168 response_needed = 0;
3169 if (!target_running ())
3170 /* The packet we received doesn't make sense - but we can't
3171 reply to it, either. */
3174 fprintf (stderr, "Killing all inferiors\n");
3175 for_each_inferior (&all_processes, kill_inferior_callback);
3177 /* When using the extended protocol, we wait with no program
3178 running. The traditional protocol will exit instead. */
3179 if (extended_protocol)
3181 last_status.kind = TARGET_WAITKIND_EXITED;
3182 last_status.value.sig = TARGET_SIGNAL_KILL;
3190 ptid_t gdb_id, thread_id;
3192 require_running (own_buf);
3194 gdb_id = read_ptid (&own_buf[1], NULL);
3195 thread_id = gdb_id_to_thread_id (gdb_id);
3196 if (ptid_equal (thread_id, null_ptid))
3198 write_enn (own_buf);
3202 if (mythread_alive (thread_id))
3205 write_enn (own_buf);
3209 response_needed = 0;
3211 /* Restarting the inferior is only supported in the extended
3213 if (extended_protocol)
3215 if (target_running ())
3216 for_each_inferior (&all_processes,
3217 kill_inferior_callback);
3218 fprintf (stderr, "GDBserver restarting\n");
3220 /* Wait till we are at 1st instruction in prog. */
3221 if (program_argv != NULL)
3222 start_inferior (program_argv);
3225 last_status.kind = TARGET_WAITKIND_EXITED;
3226 last_status.value.sig = TARGET_SIGNAL_KILL;
3232 /* It is a request we don't understand. Respond with an
3233 empty packet so that gdb knows that we don't support this
3239 /* Extended (long) request. */
3240 handle_v_requests (own_buf, packet_len, &new_packet_len);
3244 /* It is a request we don't understand. Respond with an empty
3245 packet so that gdb knows that we don't support this
3251 if (new_packet_len != -1)
3252 putpkt_binary (own_buf, new_packet_len);
3256 response_needed = 0;
3258 if (!extended_protocol && have_ran && !target_running ())
3260 /* In non-stop, defer exiting until GDB had a chance to query
3261 the whole vStopped list (until it gets an OK). */
3264 fprintf (stderr, "GDBserver exiting\n");
3276 /* Event-loop callback for serial events. */
3279 handle_serial_event (int err, gdb_client_data client_data)
3282 fprintf (stderr, "handling possible serial event\n");
3284 /* Really handle it. */
3285 if (process_serial_event () < 0)
3288 /* Be sure to not change the selected inferior behind GDB's back.
3289 Important in the non-stop mode asynchronous protocol. */
3290 set_desired_inferior (1);
3295 /* Event-loop callback for target events. */
3298 handle_target_event (int err, gdb_client_data client_data)
3301 fprintf (stderr, "handling possible target event\n");
3303 last_ptid = mywait (minus_one_ptid, &last_status,
3306 if (last_status.kind != TARGET_WAITKIND_IGNORE)
3308 int pid = ptid_get_pid (last_ptid);
3309 struct process_info *process = find_process_pid (pid);
3310 int forward_event = !gdb_connected () || process->gdb_detached;
3312 if (last_status.kind == TARGET_WAITKIND_EXITED
3313 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
3315 mark_breakpoints_out (process);
3316 mourn_inferior (process);
3320 /* We're reporting this thread as stopped. Update its
3321 "want-stopped" state to what the client wants, until it
3322 gets a new resume action. */
3323 current_inferior->last_resume_kind = resume_stop;
3324 current_inferior->last_status = last_status;
3329 if (!target_running ())
3331 /* The last process exited. We're done. */
3335 if (last_status.kind == TARGET_WAITKIND_STOPPED)
3337 /* A thread stopped with a signal, but gdb isn't
3338 connected to handle it. Pass it down to the
3339 inferior, as if it wasn't being traced. */
3340 struct thread_resume resume_info;
3344 "GDB not connected; forwarding event %d for [%s]\n",
3345 (int) last_status.kind,
3346 target_pid_to_str (last_ptid));
3348 resume_info.thread = last_ptid;
3349 resume_info.kind = resume_continue;
3350 resume_info.sig = target_signal_to_host (last_status.value.sig);
3351 (*the_target->resume) (&resume_info, 1);
3353 else if (debug_threads)
3354 fprintf (stderr, "GDB not connected; ignoring event %d for [%s]\n",
3355 (int) last_status.kind,
3356 target_pid_to_str (last_ptid));
3360 /* Something interesting. Tell GDB about it. */
3361 push_event (last_ptid, &last_status);
3365 /* Be sure to not change the selected inferior behind GDB's back.
3366 Important in the non-stop mode asynchronous protocol. */
3367 set_desired_inferior (1);