+ if (strncmp (own_buf, "vAttach;", 8) == 0)
+ {
+ if (!multi_process && target_running ())
+ {
+ fprintf (stderr, "Already debugging a process\n");
+ write_enn (own_buf);
+ return;
+ }
+ handle_v_attach (own_buf);
+ return;
+ }
+
+ if (strncmp (own_buf, "vRun;", 5) == 0)
+ {
+ if (!multi_process && target_running ())
+ {
+ fprintf (stderr, "Already debugging a process\n");
+ write_enn (own_buf);
+ return;
+ }
+ handle_v_run (own_buf);
+ return;
+ }
+
+ if (strncmp (own_buf, "vKill;", 6) == 0)
+ {
+ if (!target_running ())
+ {
+ fprintf (stderr, "No process to kill\n");
+ write_enn (own_buf);
+ return;
+ }
+ handle_v_kill (own_buf);
+ return;
+ }
+
+ if (strncmp (own_buf, "vStopped", 8) == 0)
+ {
+ handle_v_stopped (own_buf);
+ return;
+ }
+
+ /* Otherwise we didn't know what packet it was. Say we didn't
+ understand it. */
+ own_buf[0] = 0;
+ return;
+}
+
+/* Resume inferior and wait for another event. In non-stop mode,
+ don't really wait here, but return immediatelly to the event
+ loop. */
+void
+myresume (char *own_buf, int step, int sig)
+{
+ struct thread_resume resume_info[2];
+ int n = 0;
+ int valid_cont_thread;
+
+ set_desired_inferior (0);
+
+ valid_cont_thread = (!ptid_equal (cont_thread, null_ptid)
+ && !ptid_equal (cont_thread, minus_one_ptid));
+
+ if (step || sig || valid_cont_thread)
+ {
+ resume_info[0].thread
+ = ((struct inferior_list_entry *) current_inferior)->id;
+ if (step)
+ resume_info[0].kind = resume_step;
+ else
+ resume_info[0].kind = resume_continue;
+ resume_info[0].sig = sig;
+ n++;
+ }
+
+ if (!valid_cont_thread)
+ {
+ resume_info[n].thread = minus_one_ptid;
+ resume_info[n].kind = resume_continue;
+ resume_info[n].sig = 0;
+ n++;
+ }
+
+ if (!non_stop)
+ enable_async_io ();
+
+ (*the_target->resume) (resume_info, n);
+
+ if (non_stop)
+ write_ok (own_buf);
+ else
+ {
+ last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
+ prepare_resume_reply (own_buf, last_ptid, &last_status);
+ disable_async_io ();
+ }
+}
+
+/* Callback for for_each_inferior. Make a new stop reply for each
+ stopped thread. */
+
+static int
+queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg)
+{
+ int pid = * (int *) arg;
+
+ if (pid == -1
+ || ptid_get_pid (entry->id) == pid)
+ {
+ struct target_waitstatus status;
+
+ status.kind = TARGET_WAITKIND_STOPPED;
+ status.value.sig = TARGET_SIGNAL_TRAP;
+
+ /* Pass the last stop reply back to GDB, but don't notify. */
+ queue_stop_reply (entry->id, &status);
+ }
+
+ return 0;
+}
+
+/* Status handler for the '?' packet. */
+
+static void
+handle_status (char *own_buf)
+{
+ struct target_waitstatus status;
+ status.kind = TARGET_WAITKIND_STOPPED;
+ status.value.sig = TARGET_SIGNAL_TRAP;
+
+ /* In non-stop mode, we must send a stop reply for each stopped
+ thread. In all-stop mode, just send one for the first stopped
+ thread we find. */
+
+ if (non_stop)
+ {
+ int pid = -1;
+ discard_queued_stop_replies (pid);
+ find_inferior (&all_threads, queue_stop_reply_callback, &pid);
+
+ /* The first is sent immediatly. OK is sent if there is no
+ stopped thread, which is the same handling of the vStopped
+ packet (by design). */
+ send_next_stop_reply (own_buf);
+ }
+ else
+ {
+ if (all_threads.head)
+ prepare_resume_reply (own_buf,
+ all_threads.head->id, &status);
+ else
+ strcpy (own_buf, "W00");
+ }
+}
+
+static void
+gdbserver_version (void)
+{
+ printf ("GNU gdbserver %s%s\n"
+ "Copyright (C) 2009 Free Software Foundation, Inc.\n"
+ "gdbserver is free software, covered by the GNU General Public License.\n"
+ "This gdbserver was configured as \"%s\"\n",
+ PKGVERSION, version, host_name);
+}
+
+static void
+gdbserver_usage (FILE *stream)
+{
+ fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
+ "\tgdbserver [OPTIONS] --attach COMM PID\n"
+ "\tgdbserver [OPTIONS] --multi COMM\n"
+ "\n"
+ "COMM may either be a tty device (for serial debugging), or \n"
+ "HOST:PORT to listen for a TCP connection.\n"
+ "\n"
+ "Options:\n"
+ " --debug Enable general debugging output.\n"
+ " --remote-debug Enable remote protocol debugging output.\n"
+ " --version Display version information and exit.\n"
+ " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
+ if (REPORT_BUGS_TO[0] && stream == stdout)
+ fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
+}
+
+static void
+gdbserver_show_disableable (FILE *stream)
+{
+ fprintf (stream, "Disableable packets:\n"
+ " vCont \tAll vCont packets\n"
+ " qC \tQuerying the current thread\n"
+ " qfThreadInfo\tThread listing\n"
+ " Tthread \tPassing the thread specifier in the T stop reply packet\n"
+ " threads \tAll of the above\n");
+}
+
+
+#undef require_running
+#define require_running(BUF) \
+ if (!target_running ()) \
+ { \
+ write_enn (BUF); \
+ break; \
+ }
+
+static int
+first_thread_of (struct inferior_list_entry *entry, void *args)
+{
+ int pid = * (int *) args;
+
+ if (ptid_get_pid (entry->id) == pid)
+ return 1;
+
+ return 0;
+}
+
+static void
+kill_inferior_callback (struct inferior_list_entry *entry)
+{
+ struct process_info *process = (struct process_info *) entry;
+ int pid = ptid_get_pid (process->head.id);
+
+ kill_inferior (pid);
+ discard_queued_stop_replies (pid);
+}
+
+/* Callback for for_each_inferior to detach or kill the inferior,
+ depending on whether we attached to it or not.
+ We inform the user whether we're detaching or killing the process
+ as this is only called when gdbserver is about to exit. */
+
+static void
+detach_or_kill_inferior_callback (struct inferior_list_entry *entry)
+{
+ struct process_info *process = (struct process_info *) entry;
+ int pid = ptid_get_pid (process->head.id);
+
+ if (process->attached)
+ detach_inferior (pid);
+ else
+ kill_inferior (pid);
+
+ discard_queued_stop_replies (pid);
+}
+
+/* for_each_inferior callback for detach_or_kill_for_exit to print
+ the pids of started inferiors. */
+
+static void
+print_started_pid (struct inferior_list_entry *entry)
+{
+ struct process_info *process = (struct process_info *) entry;
+
+ if (! process->attached)
+ {
+ int pid = ptid_get_pid (process->head.id);
+ fprintf (stderr, " %d", pid);
+ }
+}
+
+/* for_each_inferior callback for detach_or_kill_for_exit to print
+ the pids of attached inferiors. */
+
+static void
+print_attached_pid (struct inferior_list_entry *entry)
+{
+ struct process_info *process = (struct process_info *) entry;
+
+ if (process->attached)
+ {
+ int pid = ptid_get_pid (process->head.id);
+ fprintf (stderr, " %d", pid);
+ }
+}
+
+/* Call this when exiting gdbserver with possible inferiors that need
+ to be killed or detached from. */
+
+static void
+detach_or_kill_for_exit (void)
+{
+ /* First print a list of the inferiors we will be killing/detaching.
+ This is to assist the user, for example, in case the inferior unexpectedly
+ dies after we exit: did we screw up or did the inferior exit on its own?
+ Having this info will save some head-scratching. */
+
+ if (have_started_inferiors_p ())
+ {
+ fprintf (stderr, "Killing process(es):");
+ for_each_inferior (&all_processes, print_started_pid);
+ fprintf (stderr, "\n");
+ }
+ if (have_attached_inferiors_p ())
+ {
+ fprintf (stderr, "Detaching process(es):");
+ for_each_inferior (&all_processes, print_attached_pid);
+ fprintf (stderr, "\n");
+ }
+
+ /* Now we can kill or detach the inferiors. */
+
+ for_each_inferior (&all_processes, detach_or_kill_inferior_callback);
+}
+
+static void
+join_inferiors_callback (struct inferior_list_entry *entry)
+{
+ struct process_info *process = (struct process_info *) entry;
+
+ /* If we are attached, then we can exit. Otherwise, we need to hang
+ around doing nothing, until the child is gone. */
+ if (!process->attached)
+ join_inferior (ptid_get_pid (process->head.id));
+}
+
+int
+main (int argc, char *argv[])
+{
+ int bad_attach;
+ int pid;
+ char *arg_end, *port;
+ char **next_arg = &argv[1];
+ int multi_mode = 0;
+ int attach = 0;
+ int was_running;
+
+ while (*next_arg != NULL && **next_arg == '-')
+ {
+ if (strcmp (*next_arg, "--version") == 0)
+ {
+ gdbserver_version ();
+ exit (0);
+ }
+ else if (strcmp (*next_arg, "--help") == 0)
+ {
+ gdbserver_usage (stdout);
+ exit (0);
+ }
+ else if (strcmp (*next_arg, "--attach") == 0)
+ attach = 1;
+ else if (strcmp (*next_arg, "--multi") == 0)
+ multi_mode = 1;
+ else if (strcmp (*next_arg, "--wrapper") == 0)
+ {
+ next_arg++;
+
+ wrapper_argv = next_arg;
+ while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
+ next_arg++;
+
+ if (next_arg == wrapper_argv || *next_arg == NULL)
+ {
+ gdbserver_usage (stderr);
+ exit (1);
+ }
+
+ /* Consume the "--". */
+ *next_arg = NULL;
+ }
+ else if (strcmp (*next_arg, "--debug") == 0)
+ debug_threads = 1;
+ else if (strcmp (*next_arg, "--remote-debug") == 0)
+ remote_debug = 1;
+ else if (strcmp (*next_arg, "--disable-packet") == 0)
+ {
+ gdbserver_show_disableable (stdout);
+ exit (0);
+ }
+ else if (strncmp (*next_arg,
+ "--disable-packet=",
+ sizeof ("--disable-packet=") - 1) == 0)
+ {
+ char *packets, *tok;
+
+ packets = *next_arg += sizeof ("--disable-packet=") - 1;
+ for (tok = strtok (packets, ",");
+ tok != NULL;
+ tok = strtok (NULL, ","))
+ {
+ if (strcmp ("vCont", tok) == 0)
+ disable_packet_vCont = 1;
+ else if (strcmp ("Tthread", tok) == 0)
+ disable_packet_Tthread = 1;
+ else if (strcmp ("qC", tok) == 0)
+ disable_packet_qC = 1;
+ else if (strcmp ("qfThreadInfo", tok) == 0)
+ disable_packet_qfThreadInfo = 1;
+ else if (strcmp ("threads", tok) == 0)