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;
245 signal (SIGTTOU, SIG_DFL);
246 signal (SIGTTIN, SIG_DFL);
249 signal_pid = create_inferior (new_argv[0], new_argv);
251 /* FIXME: we don't actually know at this point that the create
252 actually succeeded. We won't know that until we wait. */
253 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
258 signal (SIGTTOU, SIG_IGN);
259 signal (SIGTTIN, SIG_IGN);
260 terminal_fd = fileno (stderr);
261 old_foreground_pgrp = tcgetpgrp (terminal_fd);
262 tcsetpgrp (terminal_fd, signal_pid);
263 atexit (restore_old_foreground_pgrp);
266 if (wrapper_argv != NULL)
268 struct thread_resume resume_info;
271 resume_info.thread = pid_to_ptid (signal_pid);
272 resume_info.kind = resume_continue;
275 ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
277 if (last_status.kind != TARGET_WAITKIND_STOPPED)
282 (*the_target->resume) (&resume_info, 1);
284 mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
285 if (last_status.kind != TARGET_WAITKIND_STOPPED)
288 while (last_status.value.sig != TARGET_SIGNAL_TRAP);
293 /* Wait till we are at 1st instruction in program, return new pid
294 (assuming success). */
295 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
301 attach_inferior (int pid)
303 /* myattach should return -1 if attaching is unsupported,
304 0 if it succeeded, and call error() otherwise. */
306 if (myattach (pid) != 0)
309 fprintf (stderr, "Attached; pid = %d\n", pid);
312 /* FIXME - It may be that we should get the SIGNAL_PID from the
313 attach function, so that it can be the main thread instead of
314 whichever we were told to attach to. */
319 last_ptid = mywait (pid_to_ptid (pid), &last_status, 0, 0);
321 /* GDB knows to ignore the first SIGSTOP after attaching to a running
322 process using the "attach" command, but this is different; it's
323 just using "target remote". Pretend it's just starting up. */
324 if (last_status.kind == TARGET_WAITKIND_STOPPED
325 && last_status.value.sig == TARGET_SIGNAL_STOP)
326 last_status.value.sig = TARGET_SIGNAL_TRAP;
332 extern int remote_debug;
334 /* Decode a qXfer read request. Return 0 if everything looks OK,
338 decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
340 /* Extract and NUL-terminate the annex. */
342 while (*buf && *buf != ':')
348 /* After the read marker and annex, qXfer looks like a
349 traditional 'm' packet. */
350 decode_m_packet (buf, ofs, len);
355 /* Write the response to a successful qXfer read. Returns the
356 length of the (binary) data stored in BUF, corresponding
357 to as much of DATA/LEN as we could fit. IS_MORE controls
358 the first character of the response. */
360 write_qxfer_response (char *buf, const void *data, int len, int is_more)
369 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
373 /* Handle all of the extended 'Q' packets. */
375 handle_general_set (char *own_buf)
377 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
379 int numsigs = (int) TARGET_SIGNAL_LAST, i;
380 const char *p = own_buf + strlen ("QPassSignals:");
383 p = decode_address_to_semicolon (&cursig, p);
384 for (i = 0; i < numsigs; i++)
390 /* Keep looping, to clear the remaining signals. */
393 p = decode_address_to_semicolon (&cursig, p);
398 strcpy (own_buf, "OK");
402 if (strcmp (own_buf, "QStartNoAckMode") == 0)
406 fprintf (stderr, "[noack mode enabled]\n");
415 if (strncmp (own_buf, "QNonStop:", 9) == 0)
417 char *mode = own_buf + 9;
421 if (strcmp (mode, "0") == 0)
423 else if (strcmp (mode, "1") == 0)
427 /* We don't know what this mode is, so complain to
429 fprintf (stderr, "Unknown non-stop mode requested: %s\n",
435 req_str = req ? "non-stop" : "all-stop";
436 if (start_non_stop (req) != 0)
438 fprintf (stderr, "Setting %s mode failed\n", req_str);
446 fprintf (stderr, "[%s mode enabled]\n", req_str);
452 if (target_supports_tracepoints ()
453 && handle_tracepoint_general_set (own_buf))
456 /* Otherwise we didn't know what packet it was. Say we didn't
462 get_features_xml (const char *annex)
464 /* gdbserver_xmltarget defines what to return when looking
465 for the "target.xml" file. Its contents can either be
466 verbatim XML code (prefixed with a '@') or else the name
467 of the actual XML file to be used in place of "target.xml".
469 This variable is set up from the auto-generated
470 init_registers_... routine for the current target. */
472 if (gdbserver_xmltarget
473 && strcmp (annex, "target.xml") == 0)
475 if (*gdbserver_xmltarget == '@')
476 return gdbserver_xmltarget + 1;
478 annex = gdbserver_xmltarget;
483 extern const char *const xml_builtin[][2];
486 /* Look for the annex. */
487 for (i = 0; xml_builtin[i][0] != NULL; i++)
488 if (strcmp (annex, xml_builtin[i][0]) == 0)
491 if (xml_builtin[i][0] != NULL)
492 return xml_builtin[i][1];
500 monitor_show_help (void)
502 monitor_output ("The following monitor commands are supported:\n");
503 monitor_output (" set debug <0|1>\n");
504 monitor_output (" Enable general debugging messages\n");
505 monitor_output (" set debug-hw-points <0|1>\n");
506 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
507 monitor_output (" set remote-debug <0|1>\n");
508 monitor_output (" Enable remote protocol debugging messages\n");
509 monitor_output (" exit\n");
510 monitor_output (" Quit GDBserver\n");
513 /* Read trace frame or inferior memory. */
516 read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
518 if (current_traceframe >= 0)
521 ULONGEST length = len;
523 if (traceframe_read_mem (current_traceframe,
524 memaddr, myaddr, len, &nbytes))
526 /* Data read from trace buffer, we're done. */
527 if (nbytes == length)
529 if (!in_readonly_region (memaddr, length))
531 /* Otherwise we have a valid readonly case, fall through. */
532 /* (assume no half-trace half-real blocks for now) */
535 return read_inferior_memory (memaddr, myaddr, len);
538 /* Write trace frame or inferior memory. Actually, writing to trace
539 frames is forbidden. */
542 write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
544 if (current_traceframe >= 0)
547 return write_inferior_memory (memaddr, myaddr, len);
550 /* Subroutine of handle_search_memory to simplify it. */
553 handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len,
554 gdb_byte *pattern, unsigned pattern_len,
555 gdb_byte *search_buf,
556 unsigned chunk_size, unsigned search_buf_size,
557 CORE_ADDR *found_addrp)
559 /* Prime the search buffer. */
561 if (read_memory (start_addr, search_buf, search_buf_size) != 0)
563 warning ("Unable to access target memory at 0x%lx, halting search.",
568 /* Perform the search.
570 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
571 When we've scanned N bytes we copy the trailing bytes to the start and
572 read in another N bytes. */
574 while (search_space_len >= pattern_len)
577 unsigned nr_search_bytes = (search_space_len < search_buf_size
581 found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len);
583 if (found_ptr != NULL)
585 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
586 *found_addrp = found_addr;
590 /* Not found in this chunk, skip to next chunk. */
592 /* Don't let search_space_len wrap here, it's unsigned. */
593 if (search_space_len >= chunk_size)
594 search_space_len -= chunk_size;
596 search_space_len = 0;
598 if (search_space_len >= pattern_len)
600 unsigned keep_len = search_buf_size - chunk_size;
601 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
604 /* Copy the trailing part of the previous iteration to the front
605 of the buffer for the next iteration. */
606 memcpy (search_buf, search_buf + chunk_size, keep_len);
608 nr_to_read = (search_space_len - keep_len < chunk_size
609 ? search_space_len - keep_len
612 if (read_memory (read_addr, search_buf + keep_len,
615 warning ("Unable to access target memory at 0x%lx, halting search.",
620 start_addr += chunk_size;
629 /* Handle qSearch:memory packets. */
632 handle_search_memory (char *own_buf, int packet_len)
634 CORE_ADDR start_addr;
635 CORE_ADDR search_space_len;
637 unsigned int pattern_len;
638 /* NOTE: also defined in find.c testcase. */
639 #define SEARCH_CHUNK_SIZE 16000
640 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
641 /* Buffer to hold memory contents for searching. */
642 gdb_byte *search_buf;
643 unsigned search_buf_size;
645 CORE_ADDR found_addr;
646 int cmd_name_len = sizeof ("qSearch:memory:") - 1;
648 pattern = malloc (packet_len);
651 error ("Unable to allocate memory to perform the search");
652 strcpy (own_buf, "E00");
655 if (decode_search_memory_packet (own_buf + cmd_name_len,
656 packet_len - cmd_name_len,
657 &start_addr, &search_space_len,
658 pattern, &pattern_len) < 0)
661 error ("Error in parsing qSearch:memory packet");
662 strcpy (own_buf, "E00");
666 search_buf_size = chunk_size + pattern_len - 1;
668 /* No point in trying to allocate a buffer larger than the search space. */
669 if (search_space_len < search_buf_size)
670 search_buf_size = search_space_len;
672 search_buf = malloc (search_buf_size);
673 if (search_buf == NULL)
676 error ("Unable to allocate memory to perform the search");
677 strcpy (own_buf, "E00");
681 found = handle_search_memory_1 (start_addr, search_space_len,
682 pattern, pattern_len,
683 search_buf, chunk_size, search_buf_size,
687 sprintf (own_buf, "1,%lx", (long) found_addr);
689 strcpy (own_buf, "0");
691 strcpy (own_buf, "E00");
697 #define require_running(BUF) \
698 if (!target_running ()) \
704 /* Handle monitor commands not handled by target-specific handlers. */
707 handle_monitor_command (char *mon)
709 if (strcmp (mon, "set debug 1") == 0)
712 monitor_output ("Debug output enabled.\n");
714 else if (strcmp (mon, "set debug 0") == 0)
717 monitor_output ("Debug output disabled.\n");
719 else if (strcmp (mon, "set debug-hw-points 1") == 0)
722 monitor_output ("H/W point debugging output enabled.\n");
724 else if (strcmp (mon, "set debug-hw-points 0") == 0)
727 monitor_output ("H/W point debugging output disabled.\n");
729 else if (strcmp (mon, "set remote-debug 1") == 0)
732 monitor_output ("Protocol debug output enabled.\n");
734 else if (strcmp (mon, "set remote-debug 0") == 0)
737 monitor_output ("Protocol debug output disabled.\n");
739 else if (strcmp (mon, "help") == 0)
740 monitor_show_help ();
741 else if (strcmp (mon, "exit") == 0)
745 monitor_output ("Unknown monitor command.\n\n");
746 monitor_show_help ();
752 handle_threads_qxfer_proper (struct buffer *buffer)
754 struct inferior_list_entry *thread;
756 buffer_grow_str (buffer, "<threads>\n");
758 for (thread = all_threads.head; thread; thread = thread->next)
760 ptid_t ptid = thread_to_gdb_id ((struct thread_info *)thread);
765 write_ptid (ptid_s, ptid);
767 if (the_target->core_of_thread)
768 core = (*the_target->core_of_thread) (ptid);
772 sprintf (core_s, "%d", core);
773 buffer_xml_printf (buffer, "<thread id=\"%s\" core=\"%s\"/>\n",
778 buffer_xml_printf (buffer, "<thread id=\"%s\"/>\n",
783 buffer_grow_str0 (buffer, "</threads>\n");
787 handle_threads_qxfer (const char *annex,
788 unsigned char *readbuf,
789 CORE_ADDR offset, int length)
791 static char *result = 0;
792 static unsigned int result_length = 0;
794 if (annex && strcmp (annex, "") != 0)
799 struct buffer buffer;
800 /* When asked for data at offset 0, generate everything and store into
801 'result'. Successive reads will be served off 'result'. */
805 buffer_init (&buffer);
807 handle_threads_qxfer_proper (&buffer);
809 result = buffer_finish (&buffer);
810 result_length = strlen (result);
811 buffer_free (&buffer);
814 if (offset >= result_length)
816 /* We're out of data. */
823 if (length > result_length - offset)
824 length = result_length - offset;
826 memcpy (readbuf, result + offset, length);
832 /* Table used by the crc32 function to calcuate the checksum. */
834 static unsigned int crc32_table[256] =
837 /* Compute 32 bit CRC from inferior memory.
839 On success, return 32 bit CRC.
840 On failure, return (unsigned long long) -1. */
842 static unsigned long long
843 crc32 (CORE_ADDR base, int len, unsigned int crc)
847 /* Initialize the CRC table and the decoding table. */
851 for (i = 0; i < 256; i++)
853 for (c = i << 24, j = 8; j > 0; --j)
854 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
861 unsigned char byte = 0;
863 /* Return failure if memory read fails. */
864 if (read_inferior_memory (base, &byte, 1) != 0)
865 return (unsigned long long) -1;
867 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255];
870 return (unsigned long long) crc;
873 /* Handle all of the extended 'q' packets. */
875 handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
877 static struct inferior_list_entry *thread_ptr;
879 /* Reply the current thread id. */
880 if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC)
883 require_running (own_buf);
885 if (!ptid_equal (general_thread, null_ptid)
886 && !ptid_equal (general_thread, minus_one_ptid))
887 gdb_id = general_thread;
890 thread_ptr = all_threads.head;
891 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
894 sprintf (own_buf, "QC");
896 own_buf = write_ptid (own_buf, gdb_id);
900 if (strcmp ("qSymbol::", own_buf) == 0)
902 /* GDB is suggesting new symbols have been loaded. This may
903 mean a new shared library has been detected as loaded, so
904 take the opportunity to check if breakpoints we think are
905 inserted, still are. Note that it isn't guaranteed that
906 we'll see this when a shared library is loaded, and nor will
907 we see this for unloads (although breakpoints in unloaded
908 libraries shouldn't trigger), as GDB may not find symbols for
909 the library at all. We also re-validate breakpoints when we
910 see a second GDB breakpoint for the same address, and or when
911 we access breakpoint shadows. */
912 validate_breakpoints ();
914 if (target_running () && the_target->look_up_symbols != NULL)
915 (*the_target->look_up_symbols) ();
917 strcpy (own_buf, "OK");
921 if (!disable_packet_qfThreadInfo)
923 if (strcmp ("qfThreadInfo", own_buf) == 0)
927 require_running (own_buf);
928 thread_ptr = all_threads.head;
931 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
932 write_ptid (own_buf, gdb_id);
933 thread_ptr = thread_ptr->next;
937 if (strcmp ("qsThreadInfo", own_buf) == 0)
941 require_running (own_buf);
942 if (thread_ptr != NULL)
945 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
946 write_ptid (own_buf, gdb_id);
947 thread_ptr = thread_ptr->next;
952 sprintf (own_buf, "l");
958 if (the_target->read_offsets != NULL
959 && strcmp ("qOffsets", own_buf) == 0)
961 CORE_ADDR text, data;
963 require_running (own_buf);
964 if (the_target->read_offsets (&text, &data))
965 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
966 (long)text, (long)data, (long)data);
973 if (the_target->qxfer_spu != NULL
974 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
980 unsigned char *spu_buf;
982 require_running (own_buf);
983 strcpy (own_buf, "E00");
984 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
986 if (len > PBUFSIZ - 2)
988 spu_buf = malloc (len + 1);
992 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
996 *new_packet_len_p = write_qxfer_response (own_buf, spu_buf, len, 1);
998 *new_packet_len_p = write_qxfer_response (own_buf, spu_buf, n, 0);
1004 if (the_target->qxfer_spu != NULL
1005 && strncmp ("qXfer:spu:write:", own_buf, 16) == 0)
1011 unsigned char *spu_buf;
1013 require_running (own_buf);
1014 strcpy (own_buf, "E00");
1015 spu_buf = malloc (packet_len - 15);
1018 if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
1019 &ofs, &len, spu_buf) < 0)
1025 n = (*the_target->qxfer_spu)
1026 (annex, NULL, (unsigned const char *)spu_buf, ofs, len);
1028 write_enn (own_buf);
1030 sprintf (own_buf, "%x", n);
1036 if (the_target->read_auxv != NULL
1037 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
1039 unsigned char *data;
1045 require_running (own_buf);
1047 /* Reject any annex; grab the offset and length. */
1048 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
1049 || annex[0] != '\0')
1051 strcpy (own_buf, "E00");
1055 /* Read one extra byte, as an indicator of whether there is
1057 if (len > PBUFSIZ - 2)
1059 data = malloc (len + 1);
1062 write_enn (own_buf);
1065 n = (*the_target->read_auxv) (ofs, data, len + 1);
1067 write_enn (own_buf);
1069 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1071 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1078 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
1081 unsigned int len, total_len;
1082 const char *document;
1085 require_running (own_buf);
1087 /* Grab the annex, offset, and length. */
1088 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
1090 strcpy (own_buf, "E00");
1094 /* Now grab the correct annex. */
1095 document = get_features_xml (annex);
1096 if (document == NULL)
1098 strcpy (own_buf, "E00");
1102 total_len = strlen (document);
1103 if (len > PBUFSIZ - 2)
1106 if (ofs > total_len)
1107 write_enn (own_buf);
1108 else if (len < total_len - ofs)
1109 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1112 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1113 total_len - ofs, 0);
1118 if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
1121 unsigned int len, total_len;
1123 struct inferior_list_entry *dll_ptr;
1126 require_running (own_buf);
1128 /* Reject any annex; grab the offset and length. */
1129 if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
1130 || annex[0] != '\0')
1132 strcpy (own_buf, "E00");
1136 /* Over-estimate the necessary memory. Assume that every character
1137 in the library name must be escaped. */
1139 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
1140 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
1142 document = malloc (total_len);
1143 if (document == NULL)
1145 write_enn (own_buf);
1148 strcpy (document, "<library-list>\n");
1149 p = document + strlen (document);
1151 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
1153 struct dll_info *dll = (struct dll_info *) dll_ptr;
1156 strcpy (p, " <library name=\"");
1158 name = xml_escape_text (dll->name);
1162 strcpy (p, "\"><segment address=\"");
1164 sprintf (p, "0x%lx", (long) dll->base_addr);
1166 strcpy (p, "\"/></library>\n");
1170 strcpy (p, "</library-list>\n");
1172 total_len = strlen (document);
1173 if (len > PBUFSIZ - 2)
1176 if (ofs > total_len)
1177 write_enn (own_buf);
1178 else if (len < total_len - ofs)
1179 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1182 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1183 total_len - ofs, 0);
1189 if (the_target->qxfer_osdata != NULL
1190 && strncmp ("qXfer:osdata:read:", own_buf, 18) == 0)
1196 unsigned char *workbuf;
1198 strcpy (own_buf, "E00");
1199 if (decode_xfer_read (own_buf + 18, &annex, &ofs, &len) < 0)
1201 if (len > PBUFSIZ - 2)
1203 workbuf = malloc (len + 1);
1207 n = (*the_target->qxfer_osdata) (annex, workbuf, NULL, ofs, len + 1);
1209 write_enn (own_buf);
1211 *new_packet_len_p = write_qxfer_response (own_buf, workbuf, len, 1);
1213 *new_packet_len_p = write_qxfer_response (own_buf, workbuf, n, 0);
1219 if (the_target->qxfer_siginfo != NULL
1220 && strncmp ("qXfer:siginfo:read:", own_buf, 19) == 0)
1222 unsigned char *data;
1228 require_running (own_buf);
1230 /* Reject any annex; grab the offset and length. */
1231 if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0
1232 || annex[0] != '\0')
1234 strcpy (own_buf, "E00");
1238 /* Read one extra byte, as an indicator of whether there is
1240 if (len > PBUFSIZ - 2)
1242 data = malloc (len + 1);
1245 n = (*the_target->qxfer_siginfo) (annex, data, NULL, ofs, len + 1);
1247 write_enn (own_buf);
1249 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1251 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1257 if (the_target->qxfer_siginfo != NULL
1258 && strncmp ("qXfer:siginfo:write:", own_buf, 20) == 0)
1264 unsigned char *data;
1266 require_running (own_buf);
1268 strcpy (own_buf, "E00");
1269 data = malloc (packet_len - 19);
1272 if (decode_xfer_write (own_buf + 20, packet_len - 20, &annex,
1273 &ofs, &len, data) < 0)
1279 n = (*the_target->qxfer_siginfo)
1280 (annex, NULL, (unsigned const char *)data, ofs, len);
1282 write_enn (own_buf);
1284 sprintf (own_buf, "%x", n);
1290 if (strncmp ("qXfer:threads:read:", own_buf, 19) == 0)
1292 unsigned char *data;
1298 require_running (own_buf);
1300 /* Reject any annex; grab the offset and length. */
1301 if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0
1302 || annex[0] != '\0')
1304 strcpy (own_buf, "E00");
1308 /* Read one extra byte, as an indicator of whether there is
1310 if (len > PBUFSIZ - 2)
1312 data = malloc (len + 1);
1315 n = handle_threads_qxfer (annex, data, ofs, len + 1);
1317 write_enn (own_buf);
1319 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1321 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1327 /* Protocol features query. */
1328 if (strncmp ("qSupported", own_buf, 10) == 0
1329 && (own_buf[10] == ':' || own_buf[10] == '\0'))
1331 char *p = &own_buf[10];
1333 /* Start processing qSupported packet. */
1334 target_process_qsupported (NULL);
1336 /* Process each feature being provided by GDB. The first
1337 feature will follow a ':', and latter features will follow
1340 for (p = strtok (p + 1, ";");
1342 p = strtok (NULL, ";"))
1344 if (strcmp (p, "multiprocess+") == 0)
1346 /* GDB supports and wants multi-process support if
1348 if (target_supports_multi_process ())
1352 target_process_qsupported (p);
1355 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
1357 /* We do not have any hook to indicate whether the target backend
1358 supports qXfer:libraries:read, so always report it. */
1359 strcat (own_buf, ";qXfer:libraries:read+");
1361 if (the_target->read_auxv != NULL)
1362 strcat (own_buf, ";qXfer:auxv:read+");
1364 if (the_target->qxfer_spu != NULL)
1365 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
1367 if (the_target->qxfer_siginfo != NULL)
1368 strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1370 /* We always report qXfer:features:read, as targets may
1371 install XML files on a subsequent call to arch_setup.
1372 If we reported to GDB on startup that we don't support
1373 qXfer:feature:read at all, we will never be re-queried. */
1374 strcat (own_buf, ";qXfer:features:read+");
1376 if (transport_is_reliable)
1377 strcat (own_buf, ";QStartNoAckMode+");
1379 if (the_target->qxfer_osdata != NULL)
1380 strcat (own_buf, ";qXfer:osdata:read+");
1382 if (target_supports_multi_process ())
1383 strcat (own_buf, ";multiprocess+");
1385 if (target_supports_non_stop ())
1386 strcat (own_buf, ";QNonStop+");
1388 strcat (own_buf, ";qXfer:threads:read+");
1390 if (target_supports_tracepoints ())
1392 strcat (own_buf, ";ConditionalTracepoints+");
1393 strcat (own_buf, ";TraceStateVariables+");
1394 strcat (own_buf, ";TracepointSource+");
1400 /* Thread-local storage support. */
1401 if (the_target->get_tls_address != NULL
1402 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
1404 char *p = own_buf + 12;
1405 CORE_ADDR parts[2], address = 0;
1407 ptid_t ptid = null_ptid;
1409 require_running (own_buf);
1411 for (i = 0; i < 3; i++)
1419 p2 = strchr (p, ',');
1432 ptid = read_ptid (p, NULL);
1434 decode_address (&parts[i - 1], p, len);
1438 if (p != NULL || i < 3)
1442 struct thread_info *thread = find_thread_ptid (ptid);
1447 err = the_target->get_tls_address (thread, parts[0], parts[1],
1453 sprintf (own_buf, "%llx", address);
1458 write_enn (own_buf);
1462 /* Otherwise, pretend we do not understand this packet. */
1465 /* Handle "monitor" commands. */
1466 if (strncmp ("qRcmd,", own_buf, 6) == 0)
1468 char *mon = malloc (PBUFSIZ);
1469 int len = strlen (own_buf + 6);
1473 write_enn (own_buf);
1477 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
1479 write_enn (own_buf);
1483 mon[len / 2] = '\0';
1487 if (the_target->handle_monitor_command == NULL
1488 || (*the_target->handle_monitor_command) (mon) == 0)
1489 /* Default processing. */
1490 handle_monitor_command (mon);
1496 if (strncmp ("qSearch:memory:", own_buf, sizeof ("qSearch:memory:") - 1) == 0)
1498 require_running (own_buf);
1499 handle_search_memory (own_buf, packet_len);
1503 if (strcmp (own_buf, "qAttached") == 0
1504 || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1506 struct process_info *process;
1508 if (own_buf[sizeof ("qAttached") - 1])
1510 int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16);
1511 process = (struct process_info *)
1512 find_inferior_id (&all_processes, pid_to_ptid (pid));
1516 require_running (own_buf);
1517 process = current_process ();
1520 if (process == NULL)
1522 write_enn (own_buf);
1526 strcpy (own_buf, process->attached ? "1" : "0");
1530 if (strncmp ("qCRC:", own_buf, 5) == 0)
1532 /* CRC check (compare-section). */
1536 unsigned long long crc;
1538 require_running (own_buf);
1539 base = strtoul (own_buf + 5, &comma, 16);
1540 if (*comma++ != ',')
1542 write_enn (own_buf);
1545 len = strtoul (comma, NULL, 16);
1546 crc = crc32 (base, len, 0xffffffff);
1547 /* Check for memory failure. */
1548 if (crc == (unsigned long long) -1)
1550 write_enn (own_buf);
1553 sprintf (own_buf, "C%lx", (unsigned long) crc);
1557 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf))
1560 /* Otherwise we didn't know what packet it was. Say we didn't
1565 /* Parse vCont packets. */
1567 handle_v_cont (char *own_buf)
1571 struct thread_resume *resume_info;
1572 struct thread_resume default_action = {{0}};
1574 /* Count the number of semicolons in the packet. There should be one
1575 for every action. */
1581 p = strchr (p, ';');
1584 resume_info = malloc (n * sizeof (resume_info[0]));
1585 if (resume_info == NULL)
1593 if (p[0] == 's' || p[0] == 'S')
1594 resume_info[i].kind = resume_step;
1595 else if (p[0] == 'c' || p[0] == 'C')
1596 resume_info[i].kind = resume_continue;
1597 else if (p[0] == 't')
1598 resume_info[i].kind = resume_stop;
1602 if (p[0] == 'S' || p[0] == 'C')
1605 sig = strtol (p + 1, &q, 16);
1610 if (!target_signal_to_host_p (sig))
1612 resume_info[i].sig = target_signal_to_host (sig);
1616 resume_info[i].sig = 0;
1622 resume_info[i].thread = minus_one_ptid;
1623 default_action = resume_info[i];
1625 /* Note: we don't increment i here, we'll overwrite this entry
1626 the next time through. */
1628 else if (p[0] == ':')
1630 ptid_t ptid = read_ptid (p + 1, &q);
1635 if (p[0] != ';' && p[0] != 0)
1638 resume_info[i].thread = ptid;
1645 resume_info[i] = default_action;
1647 /* Still used in occasional places in the backend. */
1649 && !ptid_equal (resume_info[0].thread, minus_one_ptid)
1650 && resume_info[0].kind != resume_stop)
1651 cont_thread = resume_info[0].thread;
1653 cont_thread = minus_one_ptid;
1654 set_desired_inferior (0);
1659 (*the_target->resume) (resume_info, n);
1667 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
1668 prepare_resume_reply (own_buf, last_ptid, &last_status);
1669 disable_async_io ();
1674 write_enn (own_buf);
1679 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1681 handle_v_attach (char *own_buf)
1685 pid = strtol (own_buf + 8, NULL, 16);
1686 if (pid != 0 && attach_inferior (pid) == 0)
1688 /* Don't report shared library events after attaching, even if
1689 some libraries are preloaded. GDB will always poll the
1690 library list. Avoids the "stopped by shared library event"
1691 notice on the GDB side. */
1696 /* In non-stop, we don't send a resume reply. Stop events
1697 will follow up using the normal notification
1702 prepare_resume_reply (own_buf, last_ptid, &last_status);
1708 write_enn (own_buf);
1713 /* Run a new program. Return 1 if successful, 0 if failure. */
1715 handle_v_run (char *own_buf)
1717 char *p, *next_p, **new_argv;
1721 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
1727 new_argv = calloc (new_argc + 2, sizeof (char *));
1728 if (new_argv == NULL)
1730 write_enn (own_buf);
1735 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
1737 next_p = strchr (p, ';');
1739 next_p = p + strlen (p);
1741 if (i == 0 && p == next_p)
1745 /* FIXME: Fail request if out of memory instead of dying. */
1746 new_argv[i] = xmalloc (1 + (next_p - p) / 2);
1747 unhexify (new_argv[i], p, (next_p - p) / 2);
1748 new_argv[i][(next_p - p) / 2] = '\0';
1757 if (new_argv[0] == NULL)
1759 /* GDB didn't specify a program to run. Use the program from the
1760 last run with the new argument list. */
1762 if (program_argv == NULL)
1764 /* FIXME: new_argv memory leak */
1765 write_enn (own_buf);
1769 new_argv[0] = strdup (program_argv[0]);
1770 if (new_argv[0] == NULL)
1772 /* FIXME: new_argv memory leak */
1773 write_enn (own_buf);
1778 /* Free the old argv and install the new one. */
1779 freeargv (program_argv);
1780 program_argv = new_argv;
1782 start_inferior (program_argv);
1783 if (last_status.kind == TARGET_WAITKIND_STOPPED)
1785 prepare_resume_reply (own_buf, last_ptid, &last_status);
1787 /* In non-stop, sending a resume reply doesn't set the general
1788 thread, but GDB assumes a vRun sets it (this is so GDB can
1789 query which is the main thread of the new inferior. */
1791 general_thread = last_ptid;
1797 write_enn (own_buf);
1802 /* Kill process. Return 1 if successful, 0 if failure. */
1804 handle_v_kill (char *own_buf)
1807 char *p = &own_buf[6];
1809 pid = strtol (p, NULL, 16);
1812 if (pid != 0 && kill_inferior (pid) == 0)
1814 last_status.kind = TARGET_WAITKIND_SIGNALLED;
1815 last_status.value.sig = TARGET_SIGNAL_KILL;
1816 last_ptid = pid_to_ptid (pid);
1817 discard_queued_stop_replies (pid);
1823 write_enn (own_buf);
1828 /* Handle a 'vStopped' packet. */
1830 handle_v_stopped (char *own_buf)
1832 /* If we're waiting for GDB to acknowledge a pending stop reply,
1833 consider that done. */
1836 struct vstop_notif *head;
1839 fprintf (stderr, "vStopped: acking %s\n",
1840 target_pid_to_str (notif_queue->ptid));
1843 notif_queue = notif_queue->next;
1847 /* Push another stop reply, or if there are no more left, an OK. */
1848 send_next_stop_reply (own_buf);
1851 /* Handle all of the extended 'v' packets. */
1853 handle_v_requests (char *own_buf, int packet_len, int *new_packet_len)
1855 if (!disable_packet_vCont)
1857 if (strncmp (own_buf, "vCont;", 6) == 0)
1859 require_running (own_buf);
1860 handle_v_cont (own_buf);
1864 if (strncmp (own_buf, "vCont?", 6) == 0)
1866 strcpy (own_buf, "vCont;c;C;s;S;t");
1871 if (strncmp (own_buf, "vFile:", 6) == 0
1872 && handle_vFile (own_buf, packet_len, new_packet_len))
1875 if (strncmp (own_buf, "vAttach;", 8) == 0)
1877 if (!multi_process && target_running ())
1879 fprintf (stderr, "Already debugging a process\n");
1880 write_enn (own_buf);
1883 handle_v_attach (own_buf);
1887 if (strncmp (own_buf, "vRun;", 5) == 0)
1889 if (!multi_process && target_running ())
1891 fprintf (stderr, "Already debugging a process\n");
1892 write_enn (own_buf);
1895 handle_v_run (own_buf);
1899 if (strncmp (own_buf, "vKill;", 6) == 0)
1901 if (!target_running ())
1903 fprintf (stderr, "No process to kill\n");
1904 write_enn (own_buf);
1907 handle_v_kill (own_buf);
1911 if (strncmp (own_buf, "vStopped", 8) == 0)
1913 handle_v_stopped (own_buf);
1917 /* Otherwise we didn't know what packet it was. Say we didn't
1923 /* Resume inferior and wait for another event. In non-stop mode,
1924 don't really wait here, but return immediatelly to the event
1927 myresume (char *own_buf, int step, int sig)
1929 struct thread_resume resume_info[2];
1931 int valid_cont_thread;
1933 set_desired_inferior (0);
1935 valid_cont_thread = (!ptid_equal (cont_thread, null_ptid)
1936 && !ptid_equal (cont_thread, minus_one_ptid));
1938 if (step || sig || valid_cont_thread)
1940 resume_info[0].thread
1941 = ((struct inferior_list_entry *) current_inferior)->id;
1943 resume_info[0].kind = resume_step;
1945 resume_info[0].kind = resume_continue;
1946 resume_info[0].sig = sig;
1950 if (!valid_cont_thread)
1952 resume_info[n].thread = minus_one_ptid;
1953 resume_info[n].kind = resume_continue;
1954 resume_info[n].sig = 0;
1961 (*the_target->resume) (resume_info, n);
1967 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
1968 prepare_resume_reply (own_buf, last_ptid, &last_status);
1969 disable_async_io ();
1973 /* Callback for for_each_inferior. Make a new stop reply for each
1977 queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg)
1979 int pid = * (int *) arg;
1982 || ptid_get_pid (entry->id) == pid)
1984 struct target_waitstatus status;
1986 status.kind = TARGET_WAITKIND_STOPPED;
1987 status.value.sig = TARGET_SIGNAL_TRAP;
1989 /* Pass the last stop reply back to GDB, but don't notify. */
1990 queue_stop_reply (entry->id, &status);
1996 /* Status handler for the '?' packet. */
1999 handle_status (char *own_buf)
2001 struct target_waitstatus status;
2002 status.kind = TARGET_WAITKIND_STOPPED;
2003 status.value.sig = TARGET_SIGNAL_TRAP;
2005 /* In non-stop mode, we must send a stop reply for each stopped
2006 thread. In all-stop mode, just send one for the first stopped
2012 discard_queued_stop_replies (pid);
2013 find_inferior (&all_threads, queue_stop_reply_callback, &pid);
2015 /* The first is sent immediatly. OK is sent if there is no
2016 stopped thread, which is the same handling of the vStopped
2017 packet (by design). */
2018 send_next_stop_reply (own_buf);
2022 if (all_threads.head)
2023 prepare_resume_reply (own_buf,
2024 all_threads.head->id, &status);
2026 strcpy (own_buf, "W00");
2031 gdbserver_version (void)
2033 printf ("GNU gdbserver %s%s\n"
2034 "Copyright (C) 2010 Free Software Foundation, Inc.\n"
2035 "gdbserver is free software, covered by the GNU General Public License.\n"
2036 "This gdbserver was configured as \"%s\"\n",
2037 PKGVERSION, version, host_name);
2041 gdbserver_usage (FILE *stream)
2043 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2044 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2045 "\tgdbserver [OPTIONS] --multi COMM\n"
2047 "COMM may either be a tty device (for serial debugging), or \n"
2048 "HOST:PORT to listen for a TCP connection.\n"
2051 " --debug Enable general debugging output.\n"
2052 " --remote-debug Enable remote protocol debugging output.\n"
2053 " --version Display version information and exit.\n"
2054 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
2055 if (REPORT_BUGS_TO[0] && stream == stdout)
2056 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
2060 gdbserver_show_disableable (FILE *stream)
2062 fprintf (stream, "Disableable packets:\n"
2063 " vCont \tAll vCont packets\n"
2064 " qC \tQuerying the current thread\n"
2065 " qfThreadInfo\tThread listing\n"
2066 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
2067 " threads \tAll of the above\n");
2071 #undef require_running
2072 #define require_running(BUF) \
2073 if (!target_running ()) \
2080 first_thread_of (struct inferior_list_entry *entry, void *args)
2082 int pid = * (int *) args;
2084 if (ptid_get_pid (entry->id) == pid)
2091 kill_inferior_callback (struct inferior_list_entry *entry)
2093 struct process_info *process = (struct process_info *) entry;
2094 int pid = ptid_get_pid (process->head.id);
2096 kill_inferior (pid);
2097 discard_queued_stop_replies (pid);
2100 /* Callback for for_each_inferior to detach or kill the inferior,
2101 depending on whether we attached to it or not.
2102 We inform the user whether we're detaching or killing the process
2103 as this is only called when gdbserver is about to exit. */
2106 detach_or_kill_inferior_callback (struct inferior_list_entry *entry)
2108 struct process_info *process = (struct process_info *) entry;
2109 int pid = ptid_get_pid (process->head.id);
2111 if (process->attached)
2112 detach_inferior (pid);
2114 kill_inferior (pid);
2116 discard_queued_stop_replies (pid);
2119 /* for_each_inferior callback for detach_or_kill_for_exit to print
2120 the pids of started inferiors. */
2123 print_started_pid (struct inferior_list_entry *entry)
2125 struct process_info *process = (struct process_info *) entry;
2127 if (! process->attached)
2129 int pid = ptid_get_pid (process->head.id);
2130 fprintf (stderr, " %d", pid);
2134 /* for_each_inferior callback for detach_or_kill_for_exit to print
2135 the pids of attached inferiors. */
2138 print_attached_pid (struct inferior_list_entry *entry)
2140 struct process_info *process = (struct process_info *) entry;
2142 if (process->attached)
2144 int pid = ptid_get_pid (process->head.id);
2145 fprintf (stderr, " %d", pid);
2149 /* Call this when exiting gdbserver with possible inferiors that need
2150 to be killed or detached from. */
2153 detach_or_kill_for_exit (void)
2155 /* First print a list of the inferiors we will be killing/detaching.
2156 This is to assist the user, for example, in case the inferior unexpectedly
2157 dies after we exit: did we screw up or did the inferior exit on its own?
2158 Having this info will save some head-scratching. */
2160 if (have_started_inferiors_p ())
2162 fprintf (stderr, "Killing process(es):");
2163 for_each_inferior (&all_processes, print_started_pid);
2164 fprintf (stderr, "\n");
2166 if (have_attached_inferiors_p ())
2168 fprintf (stderr, "Detaching process(es):");
2169 for_each_inferior (&all_processes, print_attached_pid);
2170 fprintf (stderr, "\n");
2173 /* Now we can kill or detach the inferiors. */
2175 for_each_inferior (&all_processes, detach_or_kill_inferior_callback);
2179 join_inferiors_callback (struct inferior_list_entry *entry)
2181 struct process_info *process = (struct process_info *) entry;
2183 /* If we are attached, then we can exit. Otherwise, we need to hang
2184 around doing nothing, until the child is gone. */
2185 if (!process->attached)
2186 join_inferior (ptid_get_pid (process->head.id));
2190 main (int argc, char *argv[])
2194 char *arg_end, *port;
2195 char **next_arg = &argv[1];
2200 while (*next_arg != NULL && **next_arg == '-')
2202 if (strcmp (*next_arg, "--version") == 0)
2204 gdbserver_version ();
2207 else if (strcmp (*next_arg, "--help") == 0)
2209 gdbserver_usage (stdout);
2212 else if (strcmp (*next_arg, "--attach") == 0)
2214 else if (strcmp (*next_arg, "--multi") == 0)
2216 else if (strcmp (*next_arg, "--wrapper") == 0)
2220 wrapper_argv = next_arg;
2221 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
2224 if (next_arg == wrapper_argv || *next_arg == NULL)
2226 gdbserver_usage (stderr);
2230 /* Consume the "--". */
2233 else if (strcmp (*next_arg, "--debug") == 0)
2235 else if (strcmp (*next_arg, "--remote-debug") == 0)
2237 else if (strcmp (*next_arg, "--disable-packet") == 0)
2239 gdbserver_show_disableable (stdout);
2242 else if (strncmp (*next_arg,
2243 "--disable-packet=",
2244 sizeof ("--disable-packet=") - 1) == 0)
2246 char *packets, *tok;
2248 packets = *next_arg += sizeof ("--disable-packet=") - 1;
2249 for (tok = strtok (packets, ",");
2251 tok = strtok (NULL, ","))
2253 if (strcmp ("vCont", tok) == 0)
2254 disable_packet_vCont = 1;
2255 else if (strcmp ("Tthread", tok) == 0)
2256 disable_packet_Tthread = 1;
2257 else if (strcmp ("qC", tok) == 0)
2258 disable_packet_qC = 1;
2259 else if (strcmp ("qfThreadInfo", tok) == 0)
2260 disable_packet_qfThreadInfo = 1;
2261 else if (strcmp ("threads", tok) == 0)
2263 disable_packet_vCont = 1;
2264 disable_packet_Tthread = 1;
2265 disable_packet_qC = 1;
2266 disable_packet_qfThreadInfo = 1;
2270 fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
2272 gdbserver_show_disableable (stderr);
2279 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
2287 if (setjmp (toplevel))
2289 fprintf (stderr, "Exiting\n");
2295 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
2297 gdbserver_usage (stderr);
2304 /* --attach used to come after PORT, so allow it there for
2306 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
2313 && (*next_arg == NULL
2314 || (*next_arg)[0] == '\0'
2315 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
2317 || next_arg[1] != NULL))
2322 gdbserver_usage (stderr);
2326 initialize_inferiors ();
2327 initialize_async_io ();
2329 if (target_supports_tracepoints ())
2330 initialize_tracepoint ();
2332 own_buf = xmalloc (PBUFSIZ + 1);
2333 mem_buf = xmalloc (PBUFSIZ);
2335 if (pid == 0 && *next_arg != NULL)
2339 n = argc - (next_arg - argv);
2340 program_argv = xmalloc (sizeof (char *) * (n + 1));
2341 for (i = 0; i < n; i++)
2342 program_argv[i] = xstrdup (next_arg[i]);
2343 program_argv[i] = NULL;
2345 /* Wait till we are at first instruction in program. */
2346 start_inferior (program_argv);
2348 /* We are now (hopefully) stopped at the first instruction of
2349 the target process. This assumes that the target process was
2350 successfully created. */
2354 if (attach_inferior (pid) == -1)
2355 error ("Attaching not supported on this target");
2357 /* Otherwise succeeded. */
2361 last_status.kind = TARGET_WAITKIND_EXITED;
2362 last_status.value.integer = 0;
2363 last_ptid = minus_one_ptid;
2366 /* Don't report shared library events on the initial connection,
2367 even if some libraries are preloaded. Avoids the "stopped by
2368 shared library event" notice on gdb side. */
2371 if (setjmp (toplevel))
2373 detach_or_kill_for_exit ();
2377 if (last_status.kind == TARGET_WAITKIND_EXITED
2378 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2383 if (!was_running && !multi_mode)
2385 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
2397 if (setjmp (toplevel) != 0)
2399 /* An error occurred. */
2400 if (response_needed)
2402 write_enn (own_buf);
2407 /* Wait for events. This will return when all event sources are
2408 removed from the event loop. */
2409 start_event_loop ();
2411 /* If an exit was requested (using the "monitor exit" command),
2412 terminate now. The only other way to get here is for
2413 getpkt to fail; close the connection and reopen it at the
2418 detach_or_kill_for_exit ();
2422 fprintf (stderr, "Remote side has terminated connection. "
2423 "GDBserver will reopen the connection.\n");
2427 /* Event loop callback that handles a serial event. The first byte in
2428 the serial buffer gets us here. We expect characters to arrive at
2429 a brisk pace, so we read the rest of the packet with a blocking
2433 process_serial_event (void)
2443 int new_packet_len = -1;
2445 /* Used to decide when gdbserver should exit in
2446 multi-mode/remote. */
2447 static int have_ran = 0;
2450 have_ran = target_running ();
2452 disable_async_io ();
2454 response_needed = 0;
2455 packet_len = getpkt (own_buf);
2456 if (packet_len <= 0)
2462 response_needed = 1;
2469 handle_query (own_buf, packet_len, &new_packet_len);
2472 handle_general_set (own_buf);
2475 require_running (own_buf);
2480 pid = strtol (&own_buf[i], NULL, 16);
2484 ptid_get_pid (((struct inferior_list_entry *) current_inferior)->id);
2486 fprintf (stderr, "Detaching from process %d\n", pid);
2487 if (detach_inferior (pid) != 0)
2488 write_enn (own_buf);
2491 discard_queued_stop_replies (pid);
2494 if (extended_protocol)
2496 /* Treat this like a normal program exit. */
2497 last_status.kind = TARGET_WAITKIND_EXITED;
2498 last_status.value.integer = 0;
2499 last_ptid = pid_to_ptid (pid);
2501 current_inferior = NULL;
2508 /* If we are attached, then we can exit. Otherwise, we
2509 need to hang around doing nothing, until the child is
2511 for_each_inferior (&all_processes,
2512 join_inferiors_callback);
2518 extended_protocol = 1;
2522 handle_status (own_buf);
2525 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
2527 ptid_t gdb_id, thread_id;
2530 require_running (own_buf);
2532 gdb_id = read_ptid (&own_buf[2], NULL);
2534 pid = ptid_get_pid (gdb_id);
2536 if (ptid_equal (gdb_id, null_ptid)
2537 || ptid_equal (gdb_id, minus_one_ptid))
2538 thread_id = null_ptid;
2540 && ptid_equal (pid_to_ptid (pid),
2543 struct thread_info *thread =
2544 (struct thread_info *) find_inferior (&all_threads,
2549 write_enn (own_buf);
2553 thread_id = ((struct inferior_list_entry *)thread)->id;
2557 thread_id = gdb_id_to_thread_id (gdb_id);
2558 if (ptid_equal (thread_id, null_ptid))
2560 write_enn (own_buf);
2565 if (own_buf[1] == 'g')
2567 if (ptid_equal (thread_id, null_ptid))
2569 /* GDB is telling us to choose any thread. Check if
2570 the currently selected thread is still valid. If
2571 it is not, select the first available. */
2572 struct thread_info *thread =
2573 (struct thread_info *) find_inferior_id (&all_threads,
2576 thread_id = all_threads.head->id;
2579 general_thread = thread_id;
2580 set_desired_inferior (1);
2582 else if (own_buf[1] == 'c')
2583 cont_thread = thread_id;
2584 else if (own_buf[1] == 's')
2585 step_thread = thread_id;
2591 /* Silently ignore it so that gdb can extend the protocol
2592 without compatibility headaches. */
2597 require_running (own_buf);
2598 if (current_traceframe >= 0)
2600 struct regcache *regcache = new_register_cache ();
2602 if (fetch_traceframe_registers (current_traceframe,
2604 registers_to_string (regcache, own_buf);
2606 write_enn (own_buf);
2607 free_register_cache (regcache);
2611 struct regcache *regcache;
2613 set_desired_inferior (1);
2614 regcache = get_thread_regcache (current_inferior, 1);
2615 registers_to_string (regcache, own_buf);
2619 require_running (own_buf);
2620 if (current_traceframe >= 0)
2621 write_enn (own_buf);
2624 struct regcache *regcache;
2626 set_desired_inferior (1);
2627 regcache = get_thread_regcache (current_inferior, 1);
2628 registers_from_string (regcache, &own_buf[1]);
2633 require_running (own_buf);
2634 decode_m_packet (&own_buf[1], &mem_addr, &len);
2635 if (read_memory (mem_addr, mem_buf, len) == 0)
2636 convert_int_to_ascii (mem_buf, own_buf, len);
2638 write_enn (own_buf);
2641 require_running (own_buf);
2642 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
2643 if (write_memory (mem_addr, mem_buf, len) == 0)
2646 write_enn (own_buf);
2649 require_running (own_buf);
2650 if (decode_X_packet (&own_buf[1], packet_len - 1,
2651 &mem_addr, &len, mem_buf) < 0
2652 || write_memory (mem_addr, mem_buf, len) != 0)
2653 write_enn (own_buf);
2658 require_running (own_buf);
2659 convert_ascii_to_int (own_buf + 1, &sig, 1);
2660 if (target_signal_to_host_p (sig))
2661 signal = target_signal_to_host (sig);
2664 myresume (own_buf, 0, signal);
2667 require_running (own_buf);
2668 convert_ascii_to_int (own_buf + 1, &sig, 1);
2669 if (target_signal_to_host_p (sig))
2670 signal = target_signal_to_host (sig);
2673 myresume (own_buf, 1, signal);
2676 require_running (own_buf);
2678 myresume (own_buf, 0, signal);
2681 require_running (own_buf);
2683 myresume (own_buf, 1, signal);
2685 case 'Z': /* insert_ ... */
2687 case 'z': /* remove_ ... */
2691 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
2692 int len = strtol (lenptr + 1, &dataptr, 16);
2693 char type = own_buf[1];
2695 const int insert = ch == 'Z';
2697 /* Default to unrecognized/unsupported. */
2701 case '0': /* software-breakpoint */
2702 case '1': /* hardware-breakpoint */
2703 case '2': /* write watchpoint */
2704 case '3': /* read watchpoint */
2705 case '4': /* access watchpoint */
2706 require_running (own_buf);
2707 if (insert && the_target->insert_point != NULL)
2708 res = (*the_target->insert_point) (type, addr, len);
2709 else if (!insert && the_target->remove_point != NULL)
2710 res = (*the_target->remove_point) (type, addr, len);
2722 write_enn (own_buf);
2726 response_needed = 0;
2727 if (!target_running ())
2728 /* The packet we received doesn't make sense - but we can't
2729 reply to it, either. */
2732 fprintf (stderr, "Killing all inferiors\n");
2733 for_each_inferior (&all_processes, kill_inferior_callback);
2735 /* When using the extended protocol, we wait with no program
2736 running. The traditional protocol will exit instead. */
2737 if (extended_protocol)
2739 last_status.kind = TARGET_WAITKIND_EXITED;
2740 last_status.value.sig = TARGET_SIGNAL_KILL;
2750 ptid_t gdb_id, thread_id;
2752 require_running (own_buf);
2754 gdb_id = read_ptid (&own_buf[1], NULL);
2755 thread_id = gdb_id_to_thread_id (gdb_id);
2756 if (ptid_equal (thread_id, null_ptid))
2758 write_enn (own_buf);
2762 if (mythread_alive (thread_id))
2765 write_enn (own_buf);
2769 response_needed = 0;
2771 /* Restarting the inferior is only supported in the extended
2773 if (extended_protocol)
2775 if (target_running ())
2776 for_each_inferior (&all_processes,
2777 kill_inferior_callback);
2778 fprintf (stderr, "GDBserver restarting\n");
2780 /* Wait till we are at 1st instruction in prog. */
2781 if (program_argv != NULL)
2782 start_inferior (program_argv);
2785 last_status.kind = TARGET_WAITKIND_EXITED;
2786 last_status.value.sig = TARGET_SIGNAL_KILL;
2792 /* It is a request we don't understand. Respond with an
2793 empty packet so that gdb knows that we don't support this
2799 /* Extended (long) request. */
2800 handle_v_requests (own_buf, packet_len, &new_packet_len);
2804 /* It is a request we don't understand. Respond with an empty
2805 packet so that gdb knows that we don't support this
2811 if (new_packet_len != -1)
2812 putpkt_binary (own_buf, new_packet_len);
2816 response_needed = 0;
2818 if (!extended_protocol && have_ran && !target_running ())
2820 /* In non-stop, defer exiting until GDB had a chance to query
2821 the whole vStopped list (until it gets an OK). */
2824 fprintf (stderr, "GDBserver exiting\n");
2831 /* Event-loop callback for serial events. */
2834 handle_serial_event (int err, gdb_client_data client_data)
2837 fprintf (stderr, "handling possible serial event\n");
2839 /* Really handle it. */
2840 process_serial_event ();
2842 /* Be sure to not change the selected inferior behind GDB's back.
2843 Important in the non-stop mode asynchronous protocol. */
2844 set_desired_inferior (1);
2847 /* Event-loop callback for target events. */
2850 handle_target_event (int err, gdb_client_data client_data)
2853 fprintf (stderr, "handling possible target event\n");
2855 last_ptid = mywait (minus_one_ptid, &last_status,
2858 if (last_status.kind != TARGET_WAITKIND_IGNORE)
2860 /* Something interesting. Tell GDB about it. */
2861 push_event (last_ptid, &last_status);
2864 /* Be sure to not change the selected inferior behind GDB's back.
2865 Important in the non-stop mode asynchronous protocol. */
2866 set_desired_inferior (1);