1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
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/>. */
36 ptid_t general_thread;
41 static int extended_protocol;
42 static int response_needed;
43 static int exit_requested;
48 static char **program_argv, **wrapper_argv;
50 /* Enable miscellaneous debugging output. The name is historical - it
51 was originally used to debug LinuxThreads support. */
54 /* Enable debugging of h/w breakpoint/watchpoint support. */
57 int pass_signals[TARGET_SIGNAL_LAST];
61 const char *gdbserver_xmltarget;
63 /* The PID of the originally created or attached inferior. Used to
64 send signals to the process when GDB sends us an asynchronous interrupt
65 (user hitting Control-C in the client), and to wait for the child to exit
66 when no longer debugging it. */
68 unsigned long signal_pid;
71 /* A file descriptor for the controlling terminal. */
74 /* TERMINAL_FD's original foreground group. */
75 pid_t old_foreground_pgrp;
77 /* Hand back terminal ownership to the original foreground group. */
80 restore_old_foreground_pgrp (void)
82 tcsetpgrp (terminal_fd, old_foreground_pgrp);
86 /* Set if you want to disable optional thread related packets support
87 in gdbserver, for the sake of testing GDB against stubs that don't
89 int disable_packet_vCont;
90 int disable_packet_Tthread;
91 int disable_packet_qC;
92 int disable_packet_qfThreadInfo;
94 /* Last status reported to GDB. */
95 static struct target_waitstatus last_status;
96 static ptid_t last_ptid;
99 static unsigned char *mem_buf;
101 /* Structure holding information relative to a single stop reply. We
102 keep a queue of these (really a singly-linked list) to push to GDB
106 /* Pointer to next in list. */
107 struct vstop_notif *next;
109 /* Thread or process that got the event. */
113 struct target_waitstatus status;
116 /* The pending stop replies list head. */
117 static struct vstop_notif *notif_queue = NULL;
119 /* Put a stop reply to the stop reply queue. */
122 queue_stop_reply (ptid_t ptid, struct target_waitstatus *status)
124 struct vstop_notif *new_notif;
126 new_notif = malloc (sizeof (*new_notif));
127 new_notif->next = NULL;
128 new_notif->ptid = ptid;
129 new_notif->status = *status;
133 struct vstop_notif *tail;
134 for (tail = notif_queue;
138 tail->next = new_notif;
141 notif_queue = new_notif;
146 struct vstop_notif *n;
148 for (n = notif_queue; n; n = n->next)
151 fprintf (stderr, "pending stop replies: %d\n", i);
155 /* Place an event in the stop reply queue, and push a notification if
156 we aren't sending one yet. */
159 push_event (ptid_t ptid, struct target_waitstatus *status)
161 queue_stop_reply (ptid, status);
163 /* If this is the first stop reply in the queue, then inform GDB
164 about it, by sending a Stop notification. */
165 if (notif_queue->next == NULL)
170 prepare_resume_reply (p,
171 notif_queue->ptid, ¬if_queue->status);
172 putpkt_notif (own_buf);
176 /* Get rid of the currently pending stop replies for PID. If PID is
177 -1, then apply to all processes. */
180 discard_queued_stop_replies (int pid)
182 struct vstop_notif *prev = NULL, *reply, *next;
184 for (reply = notif_queue; reply; reply = next)
189 || ptid_get_pid (reply->ptid) == pid)
191 if (reply == notif_queue)
194 prev->next = reply->next;
203 /* If there are more stop replies to push, push one now. */
206 send_next_stop_reply (char *own_buf)
209 prepare_resume_reply (own_buf,
211 ¬if_queue->status);
217 target_running (void)
219 return all_threads.head != NULL;
223 start_inferior (char **argv)
225 char **new_argv = argv;
227 if (wrapper_argv != NULL)
231 for (i = 0; wrapper_argv[i] != NULL; i++)
233 for (i = 0; argv[i] != NULL; i++)
235 new_argv = alloca (sizeof (char *) * count);
237 for (i = 0; wrapper_argv[i] != NULL; i++)
238 new_argv[count++] = wrapper_argv[i];
239 for (i = 0; argv[i] != NULL; i++)
240 new_argv[count++] = argv[i];
241 new_argv[count] = NULL;
247 for (i = 0; new_argv[i]; ++i)
248 fprintf (stderr, "new_argv[%d] = \"%s\"\n", i, new_argv[i]);
253 signal (SIGTTOU, SIG_DFL);
254 signal (SIGTTIN, SIG_DFL);
257 signal_pid = create_inferior (new_argv[0], new_argv);
259 /* FIXME: we don't actually know at this point that the create
260 actually succeeded. We won't know that until we wait. */
261 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
266 signal (SIGTTOU, SIG_IGN);
267 signal (SIGTTIN, SIG_IGN);
268 terminal_fd = fileno (stderr);
269 old_foreground_pgrp = tcgetpgrp (terminal_fd);
270 tcsetpgrp (terminal_fd, signal_pid);
271 atexit (restore_old_foreground_pgrp);
274 if (wrapper_argv != NULL)
276 struct thread_resume resume_info;
279 resume_info.thread = pid_to_ptid (signal_pid);
280 resume_info.kind = resume_continue;
283 ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
285 if (last_status.kind != TARGET_WAITKIND_STOPPED)
290 (*the_target->resume) (&resume_info, 1);
292 mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
293 if (last_status.kind != TARGET_WAITKIND_STOPPED)
296 while (last_status.value.sig != TARGET_SIGNAL_TRAP);
301 /* Wait till we are at 1st instruction in program, return new pid
302 (assuming success). */
303 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
309 attach_inferior (int pid)
311 /* myattach should return -1 if attaching is unsupported,
312 0 if it succeeded, and call error() otherwise. */
314 if (myattach (pid) != 0)
317 fprintf (stderr, "Attached; pid = %d\n", pid);
320 /* FIXME - It may be that we should get the SIGNAL_PID from the
321 attach function, so that it can be the main thread instead of
322 whichever we were told to attach to. */
327 last_ptid = mywait (pid_to_ptid (pid), &last_status, 0, 0);
329 /* GDB knows to ignore the first SIGSTOP after attaching to a running
330 process using the "attach" command, but this is different; it's
331 just using "target remote". Pretend it's just starting up. */
332 if (last_status.kind == TARGET_WAITKIND_STOPPED
333 && last_status.value.sig == TARGET_SIGNAL_STOP)
334 last_status.value.sig = TARGET_SIGNAL_TRAP;
340 extern int remote_debug;
342 /* Decode a qXfer read request. Return 0 if everything looks OK,
346 decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
348 /* Extract and NUL-terminate the annex. */
350 while (*buf && *buf != ':')
356 /* After the read marker and annex, qXfer looks like a
357 traditional 'm' packet. */
358 decode_m_packet (buf, ofs, len);
363 /* Write the response to a successful qXfer read. Returns the
364 length of the (binary) data stored in BUF, corresponding
365 to as much of DATA/LEN as we could fit. IS_MORE controls
366 the first character of the response. */
368 write_qxfer_response (char *buf, const void *data, int len, int is_more)
377 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
381 /* Handle all of the extended 'Q' packets. */
384 handle_general_set (char *own_buf)
386 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
388 int numsigs = (int) TARGET_SIGNAL_LAST, i;
389 const char *p = own_buf + strlen ("QPassSignals:");
392 p = decode_address_to_semicolon (&cursig, p);
393 for (i = 0; i < numsigs; i++)
399 /* Keep looping, to clear the remaining signals. */
402 p = decode_address_to_semicolon (&cursig, p);
407 strcpy (own_buf, "OK");
411 if (strcmp (own_buf, "QStartNoAckMode") == 0)
415 fprintf (stderr, "[noack mode enabled]\n");
424 if (strncmp (own_buf, "QNonStop:", 9) == 0)
426 char *mode = own_buf + 9;
430 if (strcmp (mode, "0") == 0)
432 else if (strcmp (mode, "1") == 0)
436 /* We don't know what this mode is, so complain to
438 fprintf (stderr, "Unknown non-stop mode requested: %s\n",
444 req_str = req ? "non-stop" : "all-stop";
445 if (start_non_stop (req) != 0)
447 fprintf (stderr, "Setting %s mode failed\n", req_str);
455 fprintf (stderr, "[%s mode enabled]\n", req_str);
461 if (target_supports_tracepoints ()
462 && handle_tracepoint_general_set (own_buf))
465 /* Otherwise we didn't know what packet it was. Say we didn't
471 get_features_xml (const char *annex)
473 /* gdbserver_xmltarget defines what to return when looking
474 for the "target.xml" file. Its contents can either be
475 verbatim XML code (prefixed with a '@') or else the name
476 of the actual XML file to be used in place of "target.xml".
478 This variable is set up from the auto-generated
479 init_registers_... routine for the current target. */
481 if (gdbserver_xmltarget
482 && strcmp (annex, "target.xml") == 0)
484 if (*gdbserver_xmltarget == '@')
485 return gdbserver_xmltarget + 1;
487 annex = gdbserver_xmltarget;
492 extern const char *const xml_builtin[][2];
495 /* Look for the annex. */
496 for (i = 0; xml_builtin[i][0] != NULL; i++)
497 if (strcmp (annex, xml_builtin[i][0]) == 0)
500 if (xml_builtin[i][0] != NULL)
501 return xml_builtin[i][1];
509 monitor_show_help (void)
511 monitor_output ("The following monitor commands are supported:\n");
512 monitor_output (" set debug <0|1>\n");
513 monitor_output (" Enable general debugging messages\n");
514 monitor_output (" set debug-hw-points <0|1>\n");
515 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
516 monitor_output (" set remote-debug <0|1>\n");
517 monitor_output (" Enable remote protocol debugging messages\n");
518 monitor_output (" exit\n");
519 monitor_output (" Quit GDBserver\n");
522 /* Read trace frame or inferior memory. */
525 read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
527 if (current_traceframe >= 0)
530 ULONGEST length = len;
532 if (traceframe_read_mem (current_traceframe,
533 memaddr, myaddr, len, &nbytes))
535 /* Data read from trace buffer, we're done. */
536 if (nbytes == length)
538 if (!in_readonly_region (memaddr, length))
540 /* Otherwise we have a valid readonly case, fall through. */
541 /* (assume no half-trace half-real blocks for now) */
544 return read_inferior_memory (memaddr, myaddr, len);
547 /* Write trace frame or inferior memory. Actually, writing to trace
548 frames is forbidden. */
551 write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
553 if (current_traceframe >= 0)
556 return write_inferior_memory (memaddr, myaddr, len);
559 /* Subroutine of handle_search_memory to simplify it. */
562 handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len,
563 gdb_byte *pattern, unsigned pattern_len,
564 gdb_byte *search_buf,
565 unsigned chunk_size, unsigned search_buf_size,
566 CORE_ADDR *found_addrp)
568 /* Prime the search buffer. */
570 if (read_memory (start_addr, search_buf, search_buf_size) != 0)
572 warning ("Unable to access target memory at 0x%lx, halting search.",
577 /* Perform the search.
579 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
580 When we've scanned N bytes we copy the trailing bytes to the start and
581 read in another N bytes. */
583 while (search_space_len >= pattern_len)
586 unsigned nr_search_bytes = (search_space_len < search_buf_size
590 found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len);
592 if (found_ptr != NULL)
594 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
595 *found_addrp = found_addr;
599 /* Not found in this chunk, skip to next chunk. */
601 /* Don't let search_space_len wrap here, it's unsigned. */
602 if (search_space_len >= chunk_size)
603 search_space_len -= chunk_size;
605 search_space_len = 0;
607 if (search_space_len >= pattern_len)
609 unsigned keep_len = search_buf_size - chunk_size;
610 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
613 /* Copy the trailing part of the previous iteration to the front
614 of the buffer for the next iteration. */
615 memcpy (search_buf, search_buf + chunk_size, keep_len);
617 nr_to_read = (search_space_len - keep_len < chunk_size
618 ? search_space_len - keep_len
621 if (read_memory (read_addr, search_buf + keep_len,
624 warning ("Unable to access target memory at 0x%lx, halting search.",
629 start_addr += chunk_size;
638 /* Handle qSearch:memory packets. */
641 handle_search_memory (char *own_buf, int packet_len)
643 CORE_ADDR start_addr;
644 CORE_ADDR search_space_len;
646 unsigned int pattern_len;
647 /* NOTE: also defined in find.c testcase. */
648 #define SEARCH_CHUNK_SIZE 16000
649 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
650 /* Buffer to hold memory contents for searching. */
651 gdb_byte *search_buf;
652 unsigned search_buf_size;
654 CORE_ADDR found_addr;
655 int cmd_name_len = sizeof ("qSearch:memory:") - 1;
657 pattern = malloc (packet_len);
660 error ("Unable to allocate memory to perform the search");
661 strcpy (own_buf, "E00");
664 if (decode_search_memory_packet (own_buf + cmd_name_len,
665 packet_len - cmd_name_len,
666 &start_addr, &search_space_len,
667 pattern, &pattern_len) < 0)
670 error ("Error in parsing qSearch:memory packet");
671 strcpy (own_buf, "E00");
675 search_buf_size = chunk_size + pattern_len - 1;
677 /* No point in trying to allocate a buffer larger than the search space. */
678 if (search_space_len < search_buf_size)
679 search_buf_size = search_space_len;
681 search_buf = malloc (search_buf_size);
682 if (search_buf == NULL)
685 error ("Unable to allocate memory to perform the search");
686 strcpy (own_buf, "E00");
690 found = handle_search_memory_1 (start_addr, search_space_len,
691 pattern, pattern_len,
692 search_buf, chunk_size, search_buf_size,
696 sprintf (own_buf, "1,%lx", (long) found_addr);
698 strcpy (own_buf, "0");
700 strcpy (own_buf, "E00");
706 #define require_running(BUF) \
707 if (!target_running ()) \
713 /* Handle monitor commands not handled by target-specific handlers. */
716 handle_monitor_command (char *mon)
718 if (strcmp (mon, "set debug 1") == 0)
721 monitor_output ("Debug output enabled.\n");
723 else if (strcmp (mon, "set debug 0") == 0)
726 monitor_output ("Debug output disabled.\n");
728 else if (strcmp (mon, "set debug-hw-points 1") == 0)
731 monitor_output ("H/W point debugging output enabled.\n");
733 else if (strcmp (mon, "set debug-hw-points 0") == 0)
736 monitor_output ("H/W point debugging output disabled.\n");
738 else if (strcmp (mon, "set remote-debug 1") == 0)
741 monitor_output ("Protocol debug output enabled.\n");
743 else if (strcmp (mon, "set remote-debug 0") == 0)
746 monitor_output ("Protocol debug output disabled.\n");
748 else if (strcmp (mon, "help") == 0)
749 monitor_show_help ();
750 else if (strcmp (mon, "exit") == 0)
754 monitor_output ("Unknown monitor command.\n\n");
755 monitor_show_help ();
761 handle_threads_qxfer_proper (struct buffer *buffer)
763 struct inferior_list_entry *thread;
765 buffer_grow_str (buffer, "<threads>\n");
767 for (thread = all_threads.head; thread; thread = thread->next)
769 ptid_t ptid = thread_to_gdb_id ((struct thread_info *)thread);
774 write_ptid (ptid_s, ptid);
776 if (the_target->core_of_thread)
777 core = (*the_target->core_of_thread) (ptid);
781 sprintf (core_s, "%d", core);
782 buffer_xml_printf (buffer, "<thread id=\"%s\" core=\"%s\"/>\n",
787 buffer_xml_printf (buffer, "<thread id=\"%s\"/>\n",
792 buffer_grow_str0 (buffer, "</threads>\n");
796 handle_threads_qxfer (const char *annex,
797 unsigned char *readbuf,
798 CORE_ADDR offset, int length)
800 static char *result = 0;
801 static unsigned int result_length = 0;
803 if (annex && strcmp (annex, "") != 0)
808 struct buffer buffer;
809 /* When asked for data at offset 0, generate everything and store into
810 'result'. Successive reads will be served off 'result'. */
814 buffer_init (&buffer);
816 handle_threads_qxfer_proper (&buffer);
818 result = buffer_finish (&buffer);
819 result_length = strlen (result);
820 buffer_free (&buffer);
823 if (offset >= result_length)
825 /* We're out of data. */
832 if (length > result_length - offset)
833 length = result_length - offset;
835 memcpy (readbuf, result + offset, length);
841 /* Table used by the crc32 function to calcuate the checksum. */
843 static unsigned int crc32_table[256] =
846 /* Compute 32 bit CRC from inferior memory.
848 On success, return 32 bit CRC.
849 On failure, return (unsigned long long) -1. */
851 static unsigned long long
852 crc32 (CORE_ADDR base, int len, unsigned int crc)
856 /* Initialize the CRC table and the decoding table. */
860 for (i = 0; i < 256; i++)
862 for (c = i << 24, j = 8; j > 0; --j)
863 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
870 unsigned char byte = 0;
872 /* Return failure if memory read fails. */
873 if (read_inferior_memory (base, &byte, 1) != 0)
874 return (unsigned long long) -1;
876 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255];
879 return (unsigned long long) crc;
882 /* Handle all of the extended 'q' packets. */
884 handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
886 static struct inferior_list_entry *thread_ptr;
888 /* Reply the current thread id. */
889 if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC)
892 require_running (own_buf);
894 if (!ptid_equal (general_thread, null_ptid)
895 && !ptid_equal (general_thread, minus_one_ptid))
896 gdb_id = general_thread;
899 thread_ptr = all_threads.head;
900 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
903 sprintf (own_buf, "QC");
905 own_buf = write_ptid (own_buf, gdb_id);
909 if (strcmp ("qSymbol::", own_buf) == 0)
911 /* GDB is suggesting new symbols have been loaded. This may
912 mean a new shared library has been detected as loaded, so
913 take the opportunity to check if breakpoints we think are
914 inserted, still are. Note that it isn't guaranteed that
915 we'll see this when a shared library is loaded, and nor will
916 we see this for unloads (although breakpoints in unloaded
917 libraries shouldn't trigger), as GDB may not find symbols for
918 the library at all. We also re-validate breakpoints when we
919 see a second GDB breakpoint for the same address, and or when
920 we access breakpoint shadows. */
921 validate_breakpoints ();
923 if (target_running () && the_target->look_up_symbols != NULL)
924 (*the_target->look_up_symbols) ();
926 strcpy (own_buf, "OK");
930 if (!disable_packet_qfThreadInfo)
932 if (strcmp ("qfThreadInfo", own_buf) == 0)
936 require_running (own_buf);
937 thread_ptr = all_threads.head;
940 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
941 write_ptid (own_buf, gdb_id);
942 thread_ptr = thread_ptr->next;
946 if (strcmp ("qsThreadInfo", own_buf) == 0)
950 require_running (own_buf);
951 if (thread_ptr != NULL)
954 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
955 write_ptid (own_buf, gdb_id);
956 thread_ptr = thread_ptr->next;
961 sprintf (own_buf, "l");
967 if (the_target->read_offsets != NULL
968 && strcmp ("qOffsets", own_buf) == 0)
970 CORE_ADDR text, data;
972 require_running (own_buf);
973 if (the_target->read_offsets (&text, &data))
974 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
975 (long)text, (long)data, (long)data);
982 if (the_target->qxfer_spu != NULL
983 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
989 unsigned char *spu_buf;
991 require_running (own_buf);
992 strcpy (own_buf, "E00");
993 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
995 if (len > PBUFSIZ - 2)
997 spu_buf = malloc (len + 1);
1001 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
1003 write_enn (own_buf);
1005 *new_packet_len_p = write_qxfer_response (own_buf, spu_buf, len, 1);
1007 *new_packet_len_p = write_qxfer_response (own_buf, spu_buf, n, 0);
1013 if (the_target->qxfer_spu != NULL
1014 && strncmp ("qXfer:spu:write:", own_buf, 16) == 0)
1020 unsigned char *spu_buf;
1022 require_running (own_buf);
1023 strcpy (own_buf, "E00");
1024 spu_buf = malloc (packet_len - 15);
1027 if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
1028 &ofs, &len, spu_buf) < 0)
1034 n = (*the_target->qxfer_spu)
1035 (annex, NULL, (unsigned const char *)spu_buf, ofs, len);
1037 write_enn (own_buf);
1039 sprintf (own_buf, "%x", n);
1045 if (the_target->read_auxv != NULL
1046 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
1048 unsigned char *data;
1054 require_running (own_buf);
1056 /* Reject any annex; grab the offset and length. */
1057 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
1058 || annex[0] != '\0')
1060 strcpy (own_buf, "E00");
1064 /* Read one extra byte, as an indicator of whether there is
1066 if (len > PBUFSIZ - 2)
1068 data = malloc (len + 1);
1071 write_enn (own_buf);
1074 n = (*the_target->read_auxv) (ofs, data, len + 1);
1076 write_enn (own_buf);
1078 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1080 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1087 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
1090 unsigned int len, total_len;
1091 const char *document;
1094 require_running (own_buf);
1096 /* Grab the annex, offset, and length. */
1097 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
1099 strcpy (own_buf, "E00");
1103 /* Now grab the correct annex. */
1104 document = get_features_xml (annex);
1105 if (document == NULL)
1107 strcpy (own_buf, "E00");
1111 total_len = strlen (document);
1112 if (len > PBUFSIZ - 2)
1115 if (ofs > total_len)
1116 write_enn (own_buf);
1117 else if (len < total_len - ofs)
1118 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1121 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1122 total_len - ofs, 0);
1127 if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
1130 unsigned int len, total_len;
1132 struct inferior_list_entry *dll_ptr;
1135 require_running (own_buf);
1137 /* Reject any annex; grab the offset and length. */
1138 if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
1139 || annex[0] != '\0')
1141 strcpy (own_buf, "E00");
1145 /* Over-estimate the necessary memory. Assume that every character
1146 in the library name must be escaped. */
1148 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
1149 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
1151 document = malloc (total_len);
1152 if (document == NULL)
1154 write_enn (own_buf);
1157 strcpy (document, "<library-list>\n");
1158 p = document + strlen (document);
1160 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
1162 struct dll_info *dll = (struct dll_info *) dll_ptr;
1165 strcpy (p, " <library name=\"");
1167 name = xml_escape_text (dll->name);
1171 strcpy (p, "\"><segment address=\"");
1173 sprintf (p, "0x%lx", (long) dll->base_addr);
1175 strcpy (p, "\"/></library>\n");
1179 strcpy (p, "</library-list>\n");
1181 total_len = strlen (document);
1182 if (len > PBUFSIZ - 2)
1185 if (ofs > total_len)
1186 write_enn (own_buf);
1187 else if (len < total_len - ofs)
1188 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1191 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1192 total_len - ofs, 0);
1198 if (the_target->qxfer_osdata != NULL
1199 && strncmp ("qXfer:osdata:read:", own_buf, 18) == 0)
1205 unsigned char *workbuf;
1207 strcpy (own_buf, "E00");
1208 if (decode_xfer_read (own_buf + 18, &annex, &ofs, &len) < 0)
1210 if (len > PBUFSIZ - 2)
1212 workbuf = malloc (len + 1);
1216 n = (*the_target->qxfer_osdata) (annex, workbuf, NULL, ofs, len + 1);
1218 write_enn (own_buf);
1220 *new_packet_len_p = write_qxfer_response (own_buf, workbuf, len, 1);
1222 *new_packet_len_p = write_qxfer_response (own_buf, workbuf, n, 0);
1228 if (the_target->qxfer_siginfo != NULL
1229 && strncmp ("qXfer:siginfo:read:", own_buf, 19) == 0)
1231 unsigned char *data;
1237 require_running (own_buf);
1239 /* Reject any annex; grab the offset and length. */
1240 if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0
1241 || annex[0] != '\0')
1243 strcpy (own_buf, "E00");
1247 /* Read one extra byte, as an indicator of whether there is
1249 if (len > PBUFSIZ - 2)
1251 data = malloc (len + 1);
1254 n = (*the_target->qxfer_siginfo) (annex, data, NULL, ofs, len + 1);
1256 write_enn (own_buf);
1258 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1260 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1266 if (the_target->qxfer_siginfo != NULL
1267 && strncmp ("qXfer:siginfo:write:", own_buf, 20) == 0)
1273 unsigned char *data;
1275 require_running (own_buf);
1277 strcpy (own_buf, "E00");
1278 data = malloc (packet_len - 19);
1281 if (decode_xfer_write (own_buf + 20, packet_len - 20, &annex,
1282 &ofs, &len, data) < 0)
1288 n = (*the_target->qxfer_siginfo)
1289 (annex, NULL, (unsigned const char *)data, ofs, len);
1291 write_enn (own_buf);
1293 sprintf (own_buf, "%x", n);
1299 if (strncmp ("qXfer:threads:read:", own_buf, 19) == 0)
1301 unsigned char *data;
1307 require_running (own_buf);
1309 /* Reject any annex; grab the offset and length. */
1310 if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0
1311 || annex[0] != '\0')
1313 strcpy (own_buf, "E00");
1317 /* Read one extra byte, as an indicator of whether there is
1319 if (len > PBUFSIZ - 2)
1321 data = malloc (len + 1);
1324 n = handle_threads_qxfer (annex, data, ofs, len + 1);
1326 write_enn (own_buf);
1328 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1330 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1336 /* Protocol features query. */
1337 if (strncmp ("qSupported", own_buf, 10) == 0
1338 && (own_buf[10] == ':' || own_buf[10] == '\0'))
1340 char *p = &own_buf[10];
1342 /* Start processing qSupported packet. */
1343 target_process_qsupported (NULL);
1345 /* Process each feature being provided by GDB. The first
1346 feature will follow a ':', and latter features will follow
1350 char **qsupported = NULL;
1354 /* Two passes, to avoid nested strtok calls in
1355 target_process_qsupported. */
1356 for (p = strtok (p + 1, ";");
1358 p = strtok (NULL, ";"))
1361 qsupported = xrealloc (qsupported, count * sizeof (char *));
1362 qsupported[count - 1] = xstrdup (p);
1365 for (i = 0; i < count; i++)
1368 if (strcmp (p, "multiprocess+") == 0)
1370 /* GDB supports and wants multi-process support if
1372 if (target_supports_multi_process ())
1376 target_process_qsupported (p);
1384 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
1386 /* We do not have any hook to indicate whether the target backend
1387 supports qXfer:libraries:read, so always report it. */
1388 strcat (own_buf, ";qXfer:libraries:read+");
1390 if (the_target->read_auxv != NULL)
1391 strcat (own_buf, ";qXfer:auxv:read+");
1393 if (the_target->qxfer_spu != NULL)
1394 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
1396 if (the_target->qxfer_siginfo != NULL)
1397 strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1399 /* We always report qXfer:features:read, as targets may
1400 install XML files on a subsequent call to arch_setup.
1401 If we reported to GDB on startup that we don't support
1402 qXfer:feature:read at all, we will never be re-queried. */
1403 strcat (own_buf, ";qXfer:features:read+");
1405 if (transport_is_reliable)
1406 strcat (own_buf, ";QStartNoAckMode+");
1408 if (the_target->qxfer_osdata != NULL)
1409 strcat (own_buf, ";qXfer:osdata:read+");
1411 if (target_supports_multi_process ())
1412 strcat (own_buf, ";multiprocess+");
1414 if (target_supports_non_stop ())
1415 strcat (own_buf, ";QNonStop+");
1417 strcat (own_buf, ";qXfer:threads:read+");
1419 if (target_supports_tracepoints ())
1421 strcat (own_buf, ";ConditionalTracepoints+");
1422 strcat (own_buf, ";TraceStateVariables+");
1423 strcat (own_buf, ";TracepointSource+");
1424 strcat (own_buf, ";DisconnectedTracing+");
1430 /* Thread-local storage support. */
1431 if (the_target->get_tls_address != NULL
1432 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
1434 char *p = own_buf + 12;
1435 CORE_ADDR parts[2], address = 0;
1437 ptid_t ptid = null_ptid;
1439 require_running (own_buf);
1441 for (i = 0; i < 3; i++)
1449 p2 = strchr (p, ',');
1462 ptid = read_ptid (p, NULL);
1464 decode_address (&parts[i - 1], p, len);
1468 if (p != NULL || i < 3)
1472 struct thread_info *thread = find_thread_ptid (ptid);
1477 err = the_target->get_tls_address (thread, parts[0], parts[1],
1483 sprintf (own_buf, "%llx", address);
1488 write_enn (own_buf);
1492 /* Otherwise, pretend we do not understand this packet. */
1495 /* Windows OS Thread Information Block address support. */
1496 if (the_target->get_tib_address != NULL
1497 && strncmp ("qGetTIBAddr:", own_buf, 12) == 0)
1502 ptid_t ptid = read_ptid (own_buf + 12, &annex);
1504 n = (*the_target->get_tib_address) (ptid, &tlb);
1507 sprintf (own_buf, "%llx", tlb);
1512 write_enn (own_buf);
1518 /* Handle "monitor" commands. */
1519 if (strncmp ("qRcmd,", own_buf, 6) == 0)
1521 char *mon = malloc (PBUFSIZ);
1522 int len = strlen (own_buf + 6);
1526 write_enn (own_buf);
1530 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
1532 write_enn (own_buf);
1536 mon[len / 2] = '\0';
1540 if (the_target->handle_monitor_command == NULL
1541 || (*the_target->handle_monitor_command) (mon) == 0)
1542 /* Default processing. */
1543 handle_monitor_command (mon);
1549 if (strncmp ("qSearch:memory:", own_buf, sizeof ("qSearch:memory:") - 1) == 0)
1551 require_running (own_buf);
1552 handle_search_memory (own_buf, packet_len);
1556 if (strcmp (own_buf, "qAttached") == 0
1557 || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1559 struct process_info *process;
1561 if (own_buf[sizeof ("qAttached") - 1])
1563 int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16);
1564 process = (struct process_info *)
1565 find_inferior_id (&all_processes, pid_to_ptid (pid));
1569 require_running (own_buf);
1570 process = current_process ();
1573 if (process == NULL)
1575 write_enn (own_buf);
1579 strcpy (own_buf, process->attached ? "1" : "0");
1583 if (strncmp ("qCRC:", own_buf, 5) == 0)
1585 /* CRC check (compare-section). */
1589 unsigned long long crc;
1591 require_running (own_buf);
1592 base = strtoul (own_buf + 5, &comma, 16);
1593 if (*comma++ != ',')
1595 write_enn (own_buf);
1598 len = strtoul (comma, NULL, 16);
1599 crc = crc32 (base, len, 0xffffffff);
1600 /* Check for memory failure. */
1601 if (crc == (unsigned long long) -1)
1603 write_enn (own_buf);
1606 sprintf (own_buf, "C%lx", (unsigned long) crc);
1610 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf))
1613 /* Otherwise we didn't know what packet it was. Say we didn't
1618 /* Parse vCont packets. */
1620 handle_v_cont (char *own_buf)
1624 struct thread_resume *resume_info;
1625 struct thread_resume default_action = {{0}};
1627 /* Count the number of semicolons in the packet. There should be one
1628 for every action. */
1634 p = strchr (p, ';');
1637 resume_info = malloc (n * sizeof (resume_info[0]));
1638 if (resume_info == NULL)
1646 if (p[0] == 's' || p[0] == 'S')
1647 resume_info[i].kind = resume_step;
1648 else if (p[0] == 'c' || p[0] == 'C')
1649 resume_info[i].kind = resume_continue;
1650 else if (p[0] == 't')
1651 resume_info[i].kind = resume_stop;
1655 if (p[0] == 'S' || p[0] == 'C')
1658 sig = strtol (p + 1, &q, 16);
1663 if (!target_signal_to_host_p (sig))
1665 resume_info[i].sig = target_signal_to_host (sig);
1669 resume_info[i].sig = 0;
1675 resume_info[i].thread = minus_one_ptid;
1676 default_action = resume_info[i];
1678 /* Note: we don't increment i here, we'll overwrite this entry
1679 the next time through. */
1681 else if (p[0] == ':')
1683 ptid_t ptid = read_ptid (p + 1, &q);
1688 if (p[0] != ';' && p[0] != 0)
1691 resume_info[i].thread = ptid;
1698 resume_info[i] = default_action;
1700 /* Still used in occasional places in the backend. */
1702 && !ptid_equal (resume_info[0].thread, minus_one_ptid)
1703 && resume_info[0].kind != resume_stop)
1704 cont_thread = resume_info[0].thread;
1706 cont_thread = minus_one_ptid;
1707 set_desired_inferior (0);
1712 (*the_target->resume) (resume_info, n);
1720 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
1721 prepare_resume_reply (own_buf, last_ptid, &last_status);
1722 disable_async_io ();
1727 write_enn (own_buf);
1732 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1734 handle_v_attach (char *own_buf)
1738 pid = strtol (own_buf + 8, NULL, 16);
1739 if (pid != 0 && attach_inferior (pid) == 0)
1741 /* Don't report shared library events after attaching, even if
1742 some libraries are preloaded. GDB will always poll the
1743 library list. Avoids the "stopped by shared library event"
1744 notice on the GDB side. */
1749 /* In non-stop, we don't send a resume reply. Stop events
1750 will follow up using the normal notification
1755 prepare_resume_reply (own_buf, last_ptid, &last_status);
1761 write_enn (own_buf);
1766 /* Run a new program. Return 1 if successful, 0 if failure. */
1768 handle_v_run (char *own_buf)
1770 char *p, *next_p, **new_argv;
1774 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
1780 new_argv = calloc (new_argc + 2, sizeof (char *));
1781 if (new_argv == NULL)
1783 write_enn (own_buf);
1788 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
1790 next_p = strchr (p, ';');
1792 next_p = p + strlen (p);
1794 if (i == 0 && p == next_p)
1798 /* FIXME: Fail request if out of memory instead of dying. */
1799 new_argv[i] = xmalloc (1 + (next_p - p) / 2);
1800 unhexify (new_argv[i], p, (next_p - p) / 2);
1801 new_argv[i][(next_p - p) / 2] = '\0';
1810 if (new_argv[0] == NULL)
1812 /* GDB didn't specify a program to run. Use the program from the
1813 last run with the new argument list. */
1815 if (program_argv == NULL)
1817 /* FIXME: new_argv memory leak */
1818 write_enn (own_buf);
1822 new_argv[0] = strdup (program_argv[0]);
1823 if (new_argv[0] == NULL)
1825 /* FIXME: new_argv memory leak */
1826 write_enn (own_buf);
1831 /* Free the old argv and install the new one. */
1832 freeargv (program_argv);
1833 program_argv = new_argv;
1835 start_inferior (program_argv);
1836 if (last_status.kind == TARGET_WAITKIND_STOPPED)
1838 prepare_resume_reply (own_buf, last_ptid, &last_status);
1840 /* In non-stop, sending a resume reply doesn't set the general
1841 thread, but GDB assumes a vRun sets it (this is so GDB can
1842 query which is the main thread of the new inferior. */
1844 general_thread = last_ptid;
1850 write_enn (own_buf);
1855 /* Kill process. Return 1 if successful, 0 if failure. */
1857 handle_v_kill (char *own_buf)
1860 char *p = &own_buf[6];
1862 pid = strtol (p, NULL, 16);
1865 if (pid != 0 && kill_inferior (pid) == 0)
1867 last_status.kind = TARGET_WAITKIND_SIGNALLED;
1868 last_status.value.sig = TARGET_SIGNAL_KILL;
1869 last_ptid = pid_to_ptid (pid);
1870 discard_queued_stop_replies (pid);
1876 write_enn (own_buf);
1881 /* Handle a 'vStopped' packet. */
1883 handle_v_stopped (char *own_buf)
1885 /* If we're waiting for GDB to acknowledge a pending stop reply,
1886 consider that done. */
1889 struct vstop_notif *head;
1892 fprintf (stderr, "vStopped: acking %s\n",
1893 target_pid_to_str (notif_queue->ptid));
1896 notif_queue = notif_queue->next;
1900 /* Push another stop reply, or if there are no more left, an OK. */
1901 send_next_stop_reply (own_buf);
1904 /* Handle all of the extended 'v' packets. */
1906 handle_v_requests (char *own_buf, int packet_len, int *new_packet_len)
1908 if (!disable_packet_vCont)
1910 if (strncmp (own_buf, "vCont;", 6) == 0)
1912 require_running (own_buf);
1913 handle_v_cont (own_buf);
1917 if (strncmp (own_buf, "vCont?", 6) == 0)
1919 strcpy (own_buf, "vCont;c;C;s;S;t");
1924 if (strncmp (own_buf, "vFile:", 6) == 0
1925 && handle_vFile (own_buf, packet_len, new_packet_len))
1928 if (strncmp (own_buf, "vAttach;", 8) == 0)
1930 if (!multi_process && target_running ())
1932 fprintf (stderr, "Already debugging a process\n");
1933 write_enn (own_buf);
1936 handle_v_attach (own_buf);
1940 if (strncmp (own_buf, "vRun;", 5) == 0)
1942 if (!multi_process && target_running ())
1944 fprintf (stderr, "Already debugging a process\n");
1945 write_enn (own_buf);
1948 handle_v_run (own_buf);
1952 if (strncmp (own_buf, "vKill;", 6) == 0)
1954 if (!target_running ())
1956 fprintf (stderr, "No process to kill\n");
1957 write_enn (own_buf);
1960 handle_v_kill (own_buf);
1964 if (strncmp (own_buf, "vStopped", 8) == 0)
1966 handle_v_stopped (own_buf);
1970 /* Otherwise we didn't know what packet it was. Say we didn't
1976 /* Resume inferior and wait for another event. In non-stop mode,
1977 don't really wait here, but return immediatelly to the event
1980 myresume (char *own_buf, int step, int sig)
1982 struct thread_resume resume_info[2];
1984 int valid_cont_thread;
1986 set_desired_inferior (0);
1988 valid_cont_thread = (!ptid_equal (cont_thread, null_ptid)
1989 && !ptid_equal (cont_thread, minus_one_ptid));
1991 if (step || sig || valid_cont_thread)
1993 resume_info[0].thread
1994 = ((struct inferior_list_entry *) current_inferior)->id;
1996 resume_info[0].kind = resume_step;
1998 resume_info[0].kind = resume_continue;
1999 resume_info[0].sig = sig;
2003 if (!valid_cont_thread)
2005 resume_info[n].thread = minus_one_ptid;
2006 resume_info[n].kind = resume_continue;
2007 resume_info[n].sig = 0;
2014 (*the_target->resume) (resume_info, n);
2020 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
2021 prepare_resume_reply (own_buf, last_ptid, &last_status);
2022 disable_async_io ();
2026 /* Callback for for_each_inferior. Make a new stop reply for each
2030 queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg)
2032 struct thread_info *thread = (struct thread_info *) entry;
2034 /* For now, assume targets that don't have this callback also don't
2035 manage the thread's last_status field. */
2036 if (the_target->thread_stopped == NULL)
2038 struct target_waitstatus status;
2040 status.kind = TARGET_WAITKIND_STOPPED;
2041 status.value.sig = TARGET_SIGNAL_TRAP;
2043 /* Pass the last stop reply back to GDB, but don't notify
2045 queue_stop_reply (entry->id, &thread->last_status);
2049 if (thread_stopped (thread))
2052 fprintf (stderr, "Reporting thread %s as already stopped with %s\n",
2053 target_pid_to_str (entry->id),
2054 target_waitstatus_to_string (&thread->last_status));
2056 /* Pass the last stop reply back to GDB, but don't notify
2058 queue_stop_reply (entry->id, &thread->last_status);
2065 /* Set this inferior LWP's state as "want-stopped". We won't resume
2066 this LWP until the client gives us another action for it. */
2069 gdb_wants_thread_stopped (struct inferior_list_entry *entry)
2071 struct thread_info *thread = (struct thread_info *) entry;
2073 thread->last_resume_kind = resume_stop;
2075 if (thread->last_status.kind == TARGET_WAITKIND_IGNORE)
2077 thread->last_status.kind = TARGET_WAITKIND_STOPPED;
2078 thread->last_status.value.sig = TARGET_SIGNAL_0;
2082 /* Set all threads' states as "want-stopped". */
2085 gdb_wants_all_threads_stopped (void)
2087 for_each_inferior (&all_threads, gdb_wants_thread_stopped);
2090 /* Clear the gdb_detached flag of every process. */
2093 gdb_reattached_process (struct inferior_list_entry *entry)
2095 struct process_info *process = (struct process_info *) entry;
2097 process->gdb_detached = 0;
2100 /* Status handler for the '?' packet. */
2103 handle_status (char *own_buf)
2105 /* GDB is connected, don't forward events to the target anymore. */
2106 for_each_inferior (&all_processes, gdb_reattached_process);
2108 /* In non-stop mode, we must send a stop reply for each stopped
2109 thread. In all-stop mode, just send one for the first stopped
2114 discard_queued_stop_replies (-1);
2115 find_inferior (&all_threads, queue_stop_reply_callback, NULL);
2117 /* The first is sent immediatly. OK is sent if there is no
2118 stopped thread, which is the same handling of the vStopped
2119 packet (by design). */
2120 send_next_stop_reply (own_buf);
2125 gdb_wants_all_threads_stopped ();
2127 if (all_threads.head)
2129 struct target_waitstatus status;
2131 status.kind = TARGET_WAITKIND_STOPPED;
2132 status.value.sig = TARGET_SIGNAL_TRAP;
2133 prepare_resume_reply (own_buf,
2134 all_threads.head->id, &status);
2137 strcpy (own_buf, "W00");
2142 gdbserver_version (void)
2144 printf ("GNU gdbserver %s%s\n"
2145 "Copyright (C) 2010 Free Software Foundation, Inc.\n"
2146 "gdbserver is free software, covered by the GNU General Public License.\n"
2147 "This gdbserver was configured as \"%s\"\n",
2148 PKGVERSION, version, host_name);
2152 gdbserver_usage (FILE *stream)
2154 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2155 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2156 "\tgdbserver [OPTIONS] --multi COMM\n"
2158 "COMM may either be a tty device (for serial debugging), or \n"
2159 "HOST:PORT to listen for a TCP connection.\n"
2162 " --debug Enable general debugging output.\n"
2163 " --remote-debug Enable remote protocol debugging output.\n"
2164 " --version Display version information and exit.\n"
2165 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
2166 if (REPORT_BUGS_TO[0] && stream == stdout)
2167 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
2171 gdbserver_show_disableable (FILE *stream)
2173 fprintf (stream, "Disableable packets:\n"
2174 " vCont \tAll vCont packets\n"
2175 " qC \tQuerying the current thread\n"
2176 " qfThreadInfo\tThread listing\n"
2177 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
2178 " threads \tAll of the above\n");
2182 #undef require_running
2183 #define require_running(BUF) \
2184 if (!target_running ()) \
2191 first_thread_of (struct inferior_list_entry *entry, void *args)
2193 int pid = * (int *) args;
2195 if (ptid_get_pid (entry->id) == pid)
2202 kill_inferior_callback (struct inferior_list_entry *entry)
2204 struct process_info *process = (struct process_info *) entry;
2205 int pid = ptid_get_pid (process->head.id);
2207 kill_inferior (pid);
2208 discard_queued_stop_replies (pid);
2211 /* Callback for for_each_inferior to detach or kill the inferior,
2212 depending on whether we attached to it or not.
2213 We inform the user whether we're detaching or killing the process
2214 as this is only called when gdbserver is about to exit. */
2217 detach_or_kill_inferior_callback (struct inferior_list_entry *entry)
2219 struct process_info *process = (struct process_info *) entry;
2220 int pid = ptid_get_pid (process->head.id);
2222 if (process->attached)
2223 detach_inferior (pid);
2225 kill_inferior (pid);
2227 discard_queued_stop_replies (pid);
2230 /* for_each_inferior callback for detach_or_kill_for_exit to print
2231 the pids of started inferiors. */
2234 print_started_pid (struct inferior_list_entry *entry)
2236 struct process_info *process = (struct process_info *) entry;
2238 if (! process->attached)
2240 int pid = ptid_get_pid (process->head.id);
2241 fprintf (stderr, " %d", pid);
2245 /* for_each_inferior callback for detach_or_kill_for_exit to print
2246 the pids of attached inferiors. */
2249 print_attached_pid (struct inferior_list_entry *entry)
2251 struct process_info *process = (struct process_info *) entry;
2253 if (process->attached)
2255 int pid = ptid_get_pid (process->head.id);
2256 fprintf (stderr, " %d", pid);
2260 /* Call this when exiting gdbserver with possible inferiors that need
2261 to be killed or detached from. */
2264 detach_or_kill_for_exit (void)
2266 /* First print a list of the inferiors we will be killing/detaching.
2267 This is to assist the user, for example, in case the inferior unexpectedly
2268 dies after we exit: did we screw up or did the inferior exit on its own?
2269 Having this info will save some head-scratching. */
2271 if (have_started_inferiors_p ())
2273 fprintf (stderr, "Killing process(es):");
2274 for_each_inferior (&all_processes, print_started_pid);
2275 fprintf (stderr, "\n");
2277 if (have_attached_inferiors_p ())
2279 fprintf (stderr, "Detaching process(es):");
2280 for_each_inferior (&all_processes, print_attached_pid);
2281 fprintf (stderr, "\n");
2284 /* Now we can kill or detach the inferiors. */
2286 for_each_inferior (&all_processes, detach_or_kill_inferior_callback);
2290 join_inferiors_callback (struct inferior_list_entry *entry)
2292 struct process_info *process = (struct process_info *) entry;
2294 /* If we are attached, then we can exit. Otherwise, we need to hang
2295 around doing nothing, until the child is gone. */
2296 if (!process->attached)
2297 join_inferior (ptid_get_pid (process->head.id));
2301 main (int argc, char *argv[])
2305 char *arg_end, *port;
2306 char **next_arg = &argv[1];
2311 while (*next_arg != NULL && **next_arg == '-')
2313 if (strcmp (*next_arg, "--version") == 0)
2315 gdbserver_version ();
2318 else if (strcmp (*next_arg, "--help") == 0)
2320 gdbserver_usage (stdout);
2323 else if (strcmp (*next_arg, "--attach") == 0)
2325 else if (strcmp (*next_arg, "--multi") == 0)
2327 else if (strcmp (*next_arg, "--wrapper") == 0)
2331 wrapper_argv = next_arg;
2332 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
2335 if (next_arg == wrapper_argv || *next_arg == NULL)
2337 gdbserver_usage (stderr);
2341 /* Consume the "--". */
2344 else if (strcmp (*next_arg, "--debug") == 0)
2346 else if (strcmp (*next_arg, "--remote-debug") == 0)
2348 else if (strcmp (*next_arg, "--disable-packet") == 0)
2350 gdbserver_show_disableable (stdout);
2353 else if (strncmp (*next_arg,
2354 "--disable-packet=",
2355 sizeof ("--disable-packet=") - 1) == 0)
2357 char *packets, *tok;
2359 packets = *next_arg += sizeof ("--disable-packet=") - 1;
2360 for (tok = strtok (packets, ",");
2362 tok = strtok (NULL, ","))
2364 if (strcmp ("vCont", tok) == 0)
2365 disable_packet_vCont = 1;
2366 else if (strcmp ("Tthread", tok) == 0)
2367 disable_packet_Tthread = 1;
2368 else if (strcmp ("qC", tok) == 0)
2369 disable_packet_qC = 1;
2370 else if (strcmp ("qfThreadInfo", tok) == 0)
2371 disable_packet_qfThreadInfo = 1;
2372 else if (strcmp ("threads", tok) == 0)
2374 disable_packet_vCont = 1;
2375 disable_packet_Tthread = 1;
2376 disable_packet_qC = 1;
2377 disable_packet_qfThreadInfo = 1;
2381 fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
2383 gdbserver_show_disableable (stderr);
2390 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
2398 if (setjmp (toplevel))
2400 fprintf (stderr, "Exiting\n");
2406 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
2408 gdbserver_usage (stderr);
2415 /* --attach used to come after PORT, so allow it there for
2417 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
2424 && (*next_arg == NULL
2425 || (*next_arg)[0] == '\0'
2426 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
2428 || next_arg[1] != NULL))
2433 gdbserver_usage (stderr);
2437 initialize_inferiors ();
2438 initialize_async_io ();
2440 if (target_supports_tracepoints ())
2441 initialize_tracepoint ();
2443 own_buf = xmalloc (PBUFSIZ + 1);
2444 mem_buf = xmalloc (PBUFSIZ);
2446 if (pid == 0 && *next_arg != NULL)
2450 n = argc - (next_arg - argv);
2451 program_argv = xmalloc (sizeof (char *) * (n + 1));
2452 for (i = 0; i < n; i++)
2453 program_argv[i] = xstrdup (next_arg[i]);
2454 program_argv[i] = NULL;
2456 /* Wait till we are at first instruction in program. */
2457 start_inferior (program_argv);
2459 /* We are now (hopefully) stopped at the first instruction of
2460 the target process. This assumes that the target process was
2461 successfully created. */
2465 if (attach_inferior (pid) == -1)
2466 error ("Attaching not supported on this target");
2468 /* Otherwise succeeded. */
2472 last_status.kind = TARGET_WAITKIND_EXITED;
2473 last_status.value.integer = 0;
2474 last_ptid = minus_one_ptid;
2477 /* Don't report shared library events on the initial connection,
2478 even if some libraries are preloaded. Avoids the "stopped by
2479 shared library event" notice on gdb side. */
2482 if (setjmp (toplevel))
2484 detach_or_kill_for_exit ();
2488 if (last_status.kind == TARGET_WAITKIND_EXITED
2489 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2494 if (!was_running && !multi_mode)
2496 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
2504 /* Be sure we're out of tfind mode. */
2505 current_traceframe = -1;
2509 if (setjmp (toplevel) != 0)
2511 /* An error occurred. */
2512 if (response_needed)
2514 write_enn (own_buf);
2519 /* Wait for events. This will return when all event sources are
2520 removed from the event loop. */
2521 start_event_loop ();
2523 /* If an exit was requested (using the "monitor exit" command),
2524 terminate now. The only other way to get here is for
2525 getpkt to fail; close the connection and reopen it at the
2530 detach_or_kill_for_exit ();
2535 "Remote side has terminated connection. "
2536 "GDBserver will reopen the connection.\n");
2540 if (disconnected_tracing)
2542 /* Try to enable non-stop/async mode, so we we can both
2543 wait for an async socket accept, and handle async
2544 target events simultaneously. There's also no point
2545 either in having the target always stop all threads,
2546 when we're going to pass signals down without
2550 if (start_non_stop (1))
2553 /* Detaching implicitly resumes all threads; simply
2554 disconnecting does not. */
2560 "Disconnected tracing disabled; stopping trace run.\n");
2567 /* Event loop callback that handles a serial event. The first byte in
2568 the serial buffer gets us here. We expect characters to arrive at
2569 a brisk pace, so we read the rest of the packet with a blocking
2573 process_serial_event (void)
2583 int new_packet_len = -1;
2585 /* Used to decide when gdbserver should exit in
2586 multi-mode/remote. */
2587 static int have_ran = 0;
2590 have_ran = target_running ();
2592 disable_async_io ();
2594 response_needed = 0;
2595 packet_len = getpkt (own_buf);
2596 if (packet_len <= 0)
2599 /* Force an event loop break. */
2602 response_needed = 1;
2609 handle_query (own_buf, packet_len, &new_packet_len);
2612 handle_general_set (own_buf);
2615 require_running (own_buf);
2620 pid = strtol (&own_buf[i], NULL, 16);
2624 ptid_get_pid (((struct inferior_list_entry *) current_inferior)->id);
2626 if (tracing && disconnected_tracing)
2628 struct thread_resume resume_info;
2629 struct process_info *process = find_process_pid (pid);
2631 if (process == NULL)
2633 write_enn (own_buf);
2638 "Disconnected tracing in effect, "
2639 "leaving gdbserver attached to the process\n");
2641 /* Make sure we're in non-stop/async mode, so we we can both
2642 wait for an async socket accept, and handle async target
2643 events simultaneously. There's also no point either in
2644 having the target stop all threads, when we're going to
2645 pass signals down without informing GDB. */
2649 fprintf (stderr, "Forcing non-stop mode\n");
2655 process->gdb_detached = 1;
2657 /* Detaching implicitly resumes all threads. */
2658 resume_info.thread = minus_one_ptid;
2659 resume_info.kind = resume_continue;
2660 resume_info.sig = 0;
2661 (*the_target->resume) (&resume_info, 1);
2664 break; /* from switch/case */
2667 fprintf (stderr, "Detaching from process %d\n", pid);
2669 if (detach_inferior (pid) != 0)
2670 write_enn (own_buf);
2673 discard_queued_stop_replies (pid);
2676 if (extended_protocol)
2678 /* Treat this like a normal program exit. */
2679 last_status.kind = TARGET_WAITKIND_EXITED;
2680 last_status.value.integer = 0;
2681 last_ptid = pid_to_ptid (pid);
2683 current_inferior = NULL;
2690 /* If we are attached, then we can exit. Otherwise, we
2691 need to hang around doing nothing, until the child is
2693 for_each_inferior (&all_processes,
2694 join_inferiors_callback);
2700 extended_protocol = 1;
2704 handle_status (own_buf);
2707 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
2709 ptid_t gdb_id, thread_id;
2712 require_running (own_buf);
2714 gdb_id = read_ptid (&own_buf[2], NULL);
2716 pid = ptid_get_pid (gdb_id);
2718 if (ptid_equal (gdb_id, null_ptid)
2719 || ptid_equal (gdb_id, minus_one_ptid))
2720 thread_id = null_ptid;
2722 && ptid_equal (pid_to_ptid (pid),
2725 struct thread_info *thread =
2726 (struct thread_info *) find_inferior (&all_threads,
2731 write_enn (own_buf);
2735 thread_id = ((struct inferior_list_entry *)thread)->id;
2739 thread_id = gdb_id_to_thread_id (gdb_id);
2740 if (ptid_equal (thread_id, null_ptid))
2742 write_enn (own_buf);
2747 if (own_buf[1] == 'g')
2749 if (ptid_equal (thread_id, null_ptid))
2751 /* GDB is telling us to choose any thread. Check if
2752 the currently selected thread is still valid. If
2753 it is not, select the first available. */
2754 struct thread_info *thread =
2755 (struct thread_info *) find_inferior_id (&all_threads,
2758 thread_id = all_threads.head->id;
2761 general_thread = thread_id;
2762 set_desired_inferior (1);
2764 else if (own_buf[1] == 'c')
2765 cont_thread = thread_id;
2766 else if (own_buf[1] == 's')
2767 step_thread = thread_id;
2773 /* Silently ignore it so that gdb can extend the protocol
2774 without compatibility headaches. */
2779 require_running (own_buf);
2780 if (current_traceframe >= 0)
2782 struct regcache *regcache = new_register_cache ();
2784 if (fetch_traceframe_registers (current_traceframe,
2786 registers_to_string (regcache, own_buf);
2788 write_enn (own_buf);
2789 free_register_cache (regcache);
2793 struct regcache *regcache;
2795 set_desired_inferior (1);
2796 regcache = get_thread_regcache (current_inferior, 1);
2797 registers_to_string (regcache, own_buf);
2801 require_running (own_buf);
2802 if (current_traceframe >= 0)
2803 write_enn (own_buf);
2806 struct regcache *regcache;
2808 set_desired_inferior (1);
2809 regcache = get_thread_regcache (current_inferior, 1);
2810 registers_from_string (regcache, &own_buf[1]);
2815 require_running (own_buf);
2816 decode_m_packet (&own_buf[1], &mem_addr, &len);
2817 if (read_memory (mem_addr, mem_buf, len) == 0)
2818 convert_int_to_ascii (mem_buf, own_buf, len);
2820 write_enn (own_buf);
2823 require_running (own_buf);
2824 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
2825 if (write_memory (mem_addr, mem_buf, len) == 0)
2828 write_enn (own_buf);
2831 require_running (own_buf);
2832 if (decode_X_packet (&own_buf[1], packet_len - 1,
2833 &mem_addr, &len, mem_buf) < 0
2834 || write_memory (mem_addr, mem_buf, len) != 0)
2835 write_enn (own_buf);
2840 require_running (own_buf);
2841 convert_ascii_to_int (own_buf + 1, &sig, 1);
2842 if (target_signal_to_host_p (sig))
2843 signal = target_signal_to_host (sig);
2846 myresume (own_buf, 0, signal);
2849 require_running (own_buf);
2850 convert_ascii_to_int (own_buf + 1, &sig, 1);
2851 if (target_signal_to_host_p (sig))
2852 signal = target_signal_to_host (sig);
2855 myresume (own_buf, 1, signal);
2858 require_running (own_buf);
2860 myresume (own_buf, 0, signal);
2863 require_running (own_buf);
2865 myresume (own_buf, 1, signal);
2867 case 'Z': /* insert_ ... */
2869 case 'z': /* remove_ ... */
2873 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
2874 int len = strtol (lenptr + 1, &dataptr, 16);
2875 char type = own_buf[1];
2877 const int insert = ch == 'Z';
2879 /* Default to unrecognized/unsupported. */
2883 case '0': /* software-breakpoint */
2884 case '1': /* hardware-breakpoint */
2885 case '2': /* write watchpoint */
2886 case '3': /* read watchpoint */
2887 case '4': /* access watchpoint */
2888 require_running (own_buf);
2889 if (insert && the_target->insert_point != NULL)
2890 res = (*the_target->insert_point) (type, addr, len);
2891 else if (!insert && the_target->remove_point != NULL)
2892 res = (*the_target->remove_point) (type, addr, len);
2904 write_enn (own_buf);
2908 response_needed = 0;
2909 if (!target_running ())
2910 /* The packet we received doesn't make sense - but we can't
2911 reply to it, either. */
2914 fprintf (stderr, "Killing all inferiors\n");
2915 for_each_inferior (&all_processes, kill_inferior_callback);
2917 /* When using the extended protocol, we wait with no program
2918 running. The traditional protocol will exit instead. */
2919 if (extended_protocol)
2921 last_status.kind = TARGET_WAITKIND_EXITED;
2922 last_status.value.sig = TARGET_SIGNAL_KILL;
2930 ptid_t gdb_id, thread_id;
2932 require_running (own_buf);
2934 gdb_id = read_ptid (&own_buf[1], NULL);
2935 thread_id = gdb_id_to_thread_id (gdb_id);
2936 if (ptid_equal (thread_id, null_ptid))
2938 write_enn (own_buf);
2942 if (mythread_alive (thread_id))
2945 write_enn (own_buf);
2949 response_needed = 0;
2951 /* Restarting the inferior is only supported in the extended
2953 if (extended_protocol)
2955 if (target_running ())
2956 for_each_inferior (&all_processes,
2957 kill_inferior_callback);
2958 fprintf (stderr, "GDBserver restarting\n");
2960 /* Wait till we are at 1st instruction in prog. */
2961 if (program_argv != NULL)
2962 start_inferior (program_argv);
2965 last_status.kind = TARGET_WAITKIND_EXITED;
2966 last_status.value.sig = TARGET_SIGNAL_KILL;
2972 /* It is a request we don't understand. Respond with an
2973 empty packet so that gdb knows that we don't support this
2979 /* Extended (long) request. */
2980 handle_v_requests (own_buf, packet_len, &new_packet_len);
2984 /* It is a request we don't understand. Respond with an empty
2985 packet so that gdb knows that we don't support this
2991 if (new_packet_len != -1)
2992 putpkt_binary (own_buf, new_packet_len);
2996 response_needed = 0;
2998 if (!extended_protocol && have_ran && !target_running ())
3000 /* In non-stop, defer exiting until GDB had a chance to query
3001 the whole vStopped list (until it gets an OK). */
3004 fprintf (stderr, "GDBserver exiting\n");
3016 /* Event-loop callback for serial events. */
3019 handle_serial_event (int err, gdb_client_data client_data)
3022 fprintf (stderr, "handling possible serial event\n");
3024 /* Really handle it. */
3025 if (process_serial_event () < 0)
3028 /* Be sure to not change the selected inferior behind GDB's back.
3029 Important in the non-stop mode asynchronous protocol. */
3030 set_desired_inferior (1);
3035 /* Event-loop callback for target events. */
3038 handle_target_event (int err, gdb_client_data client_data)
3041 fprintf (stderr, "handling possible target event\n");
3043 last_ptid = mywait (minus_one_ptid, &last_status,
3046 if (last_status.kind != TARGET_WAITKIND_IGNORE)
3048 int pid = ptid_get_pid (last_ptid);
3049 struct process_info *process = find_process_pid (pid);
3050 int forward_event = !gdb_connected () || process->gdb_detached;
3052 if (last_status.kind == TARGET_WAITKIND_EXITED
3053 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
3055 mark_breakpoints_out (process);
3056 mourn_inferior (process);
3061 if (!target_running ())
3063 /* The last process exited. We're done. */
3067 if (last_status.kind == TARGET_WAITKIND_STOPPED)
3069 /* A thread stopped with a signal, but gdb isn't
3070 connected to handle it. Pass it down to the
3071 inferior, as if it wasn't being traced. */
3072 struct thread_resume resume_info;
3076 "GDB not connected; forwarding event %d for [%s]\n",
3077 (int) last_status.kind,
3078 target_pid_to_str (last_ptid));
3080 resume_info.thread = last_ptid;
3081 resume_info.kind = resume_continue;
3082 resume_info.sig = last_status.value.sig;
3083 (*the_target->resume) (&resume_info, 1);
3085 else if (debug_threads)
3086 fprintf (stderr, "GDB not connected; ignoring event %d for [%s]\n",
3087 (int) last_status.kind,
3088 target_pid_to_str (last_ptid));
3092 /* Something interesting. Tell GDB about it. */
3093 push_event (last_ptid, &last_status);
3097 /* Be sure to not change the selected inferior behind GDB's back.
3098 Important in the non-stop mode asynchronous protocol. */
3099 set_desired_inferior (1);