+ if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
+ {
+ CORE_ADDR ofs;
+ unsigned int len, total_len;
+ char *document, *p;
+ struct inferior_list_entry *dll_ptr;
+ char *annex;
+
+ require_running (own_buf);
+
+ /* Reject any annex; grab the offset and length. */
+ if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
+ || annex[0] != '\0')
+ {
+ strcpy (own_buf, "E00");
+ return;
+ }
+
+ /* Over-estimate the necessary memory. Assume that every character
+ in the library name must be escaped. */
+ total_len = 64;
+ for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
+ total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
+
+ document = malloc (total_len);
+ if (document == NULL)
+ {
+ write_enn (own_buf);
+ return;
+ }
+ strcpy (document, "<library-list>\n");
+ p = document + strlen (document);
+
+ for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
+ {
+ struct dll_info *dll = (struct dll_info *) dll_ptr;
+ char *name;
+
+ strcpy (p, " <library name=\"");
+ p = p + strlen (p);
+ name = xml_escape_text (dll->name);
+ strcpy (p, name);
+ free (name);
+ p = p + strlen (p);
+ strcpy (p, "\"><segment address=\"");
+ p = p + strlen (p);
+ sprintf (p, "0x%lx", (long) dll->base_addr);
+ p = p + strlen (p);
+ strcpy (p, "\"/></library>\n");
+ p = p + strlen (p);
+ }
+
+ strcpy (p, "</library-list>\n");
+
+ total_len = strlen (document);
+ if (len > PBUFSIZ - 2)
+ len = PBUFSIZ - 2;
+
+ if (ofs > total_len)
+ write_enn (own_buf);
+ else if (len < total_len - ofs)
+ *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
+ len, 1);
+ else
+ *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
+ total_len - ofs, 0);
+
+ free (document);
+ return;
+ }
+
+ if (the_target->qxfer_osdata != NULL
+ && strncmp ("qXfer:osdata:read:", own_buf, 18) == 0)
+ {
+ char *annex;
+ int n;
+ unsigned int len;
+ CORE_ADDR ofs;
+ unsigned char *workbuf;
+
+ strcpy (own_buf, "E00");
+ if (decode_xfer_read (own_buf + 18, &annex, &ofs, &len) < 0)
+ return;
+ if (len > PBUFSIZ - 2)
+ len = PBUFSIZ - 2;
+ workbuf = malloc (len + 1);
+ if (!workbuf)
+ return;
+
+ n = (*the_target->qxfer_osdata) (annex, workbuf, NULL, ofs, len + 1);
+ if (n < 0)
+ write_enn (own_buf);
+ else if (n > len)
+ *new_packet_len_p = write_qxfer_response (own_buf, workbuf, len, 1);
+ else
+ *new_packet_len_p = write_qxfer_response (own_buf, workbuf, n, 0);
+
+ free (workbuf);
+ return;
+ }
+
+ if (the_target->qxfer_siginfo != NULL
+ && strncmp ("qXfer:siginfo:read:", own_buf, 19) == 0)
+ {
+ unsigned char *data;
+ int n;
+ CORE_ADDR ofs;
+ unsigned int len;
+ char *annex;
+
+ require_running (own_buf);
+
+ /* Reject any annex; grab the offset and length. */
+ if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0
+ || annex[0] != '\0')
+ {
+ strcpy (own_buf, "E00");
+ return;
+ }
+
+ /* Read one extra byte, as an indicator of whether there is
+ more. */
+ if (len > PBUFSIZ - 2)
+ len = PBUFSIZ - 2;
+ data = malloc (len + 1);
+ if (!data)
+ return;
+ n = (*the_target->qxfer_siginfo) (annex, data, NULL, ofs, len + 1);
+ if (n < 0)
+ write_enn (own_buf);
+ else if (n > len)
+ *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
+ else
+ *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
+
+ free (data);
+ return;
+ }
+
+ if (the_target->qxfer_siginfo != NULL
+ && strncmp ("qXfer:siginfo:write:", own_buf, 20) == 0)
+ {
+ char *annex;
+ int n;
+ unsigned int len;
+ CORE_ADDR ofs;
+ unsigned char *data;
+
+ require_running (own_buf);
+
+ strcpy (own_buf, "E00");
+ data = malloc (packet_len - 19);
+ if (!data)
+ return;
+ if (decode_xfer_write (own_buf + 20, packet_len - 20, &annex,
+ &ofs, &len, data) < 0)
+ {
+ free (data);
+ return;
+ }
+
+ n = (*the_target->qxfer_siginfo)
+ (annex, NULL, (unsigned const char *)data, ofs, len);
+ if (n < 0)
+ write_enn (own_buf);
+ else
+ sprintf (own_buf, "%x", n);
+
+ free (data);
+ return;
+ }
+
+ if (strncmp ("qXfer:threads:read:", own_buf, 19) == 0)
+ {
+ unsigned char *data;
+ int n;
+ CORE_ADDR ofs;
+ unsigned int len;
+ char *annex;
+
+ require_running (own_buf);
+
+ /* Reject any annex; grab the offset and length. */
+ if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0
+ || annex[0] != '\0')
+ {
+ strcpy (own_buf, "E00");
+ return;
+ }
+
+ /* Read one extra byte, as an indicator of whether there is
+ more. */
+ if (len > PBUFSIZ - 2)
+ len = PBUFSIZ - 2;
+ data = malloc (len + 1);
+ if (!data)
+ return;
+ n = handle_threads_qxfer (annex, data, ofs, len + 1);
+ if (n < 0)
+ write_enn (own_buf);
+ else if (n > len)
+ *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
+ else
+ *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
+
+ free (data);
+ return;
+ }
+
+ if (strncmp ("qXfer:statictrace:read:", own_buf,
+ sizeof ("qXfer:statictrace:read:") -1) == 0)
+ {
+ unsigned char *data;
+ CORE_ADDR ofs;
+ unsigned int len;
+ char *annex;
+ ULONGEST nbytes;
+
+ require_running (own_buf);
+
+ if (current_traceframe == -1)
+ {
+ write_enn (own_buf);
+ return;
+ }
+
+ /* Reject any annex; grab the offset and length. */
+ if (decode_xfer_read (own_buf + sizeof ("qXfer:statictrace:read:") -1,
+ &annex, &ofs, &len) < 0
+ || annex[0] != '\0')
+ {
+ strcpy (own_buf, "E00");
+ return;
+ }
+
+ /* Read one extra byte, as an indicator of whether there is
+ more. */
+ if (len > PBUFSIZ - 2)
+ len = PBUFSIZ - 2;
+ data = malloc (len + 1);
+ if (!data)
+ return;
+
+ if (traceframe_read_sdata (current_traceframe, ofs,
+ data, len + 1, &nbytes))
+ write_enn (own_buf);
+ else if (nbytes > len)
+ *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
+ else
+ *new_packet_len_p = write_qxfer_response (own_buf, data, nbytes, 0);
+
+ free (data);
+ return;
+ }
+
+ /* Protocol features query. */
+ if (strncmp ("qSupported", own_buf, 10) == 0
+ && (own_buf[10] == ':' || own_buf[10] == '\0'))
+ {
+ char *p = &own_buf[10];
+ int gdb_supports_qRelocInsn = 0;
+
+ /* Start processing qSupported packet. */
+ target_process_qsupported (NULL);
+
+ /* Process each feature being provided by GDB. The first
+ feature will follow a ':', and latter features will follow
+ ';'. */
+ if (*p == ':')
+ {
+ char **qsupported = NULL;
+ int count = 0;
+ int i;
+
+ /* Two passes, to avoid nested strtok calls in
+ target_process_qsupported. */
+ for (p = strtok (p + 1, ";");
+ p != NULL;
+ p = strtok (NULL, ";"))
+ {
+ count++;
+ qsupported = xrealloc (qsupported, count * sizeof (char *));
+ qsupported[count - 1] = xstrdup (p);
+ }
+
+ for (i = 0; i < count; i++)
+ {
+ p = qsupported[i];
+ if (strcmp (p, "multiprocess+") == 0)
+ {
+ /* GDB supports and wants multi-process support if
+ possible. */
+ if (target_supports_multi_process ())
+ multi_process = 1;
+ }
+ else if (strcmp (p, "qRelocInsn+") == 0)
+ {
+ /* GDB supports relocate instruction requests. */
+ gdb_supports_qRelocInsn = 1;
+ }
+ else
+ target_process_qsupported (p);
+
+ free (p);
+ }
+
+ free (qsupported);
+ }
+
+ sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
+
+ /* We do not have any hook to indicate whether the target backend
+ supports qXfer:libraries:read, so always report it. */
+ strcat (own_buf, ";qXfer:libraries:read+");
+
+ if (the_target->read_auxv != NULL)
+ strcat (own_buf, ";qXfer:auxv:read+");
+
+ if (the_target->qxfer_spu != NULL)
+ strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
+
+ if (the_target->qxfer_siginfo != NULL)
+ strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
+
+ /* We always report qXfer:features:read, as targets may
+ install XML files on a subsequent call to arch_setup.
+ If we reported to GDB on startup that we don't support
+ qXfer:feature:read at all, we will never be re-queried. */
+ strcat (own_buf, ";qXfer:features:read+");
+
+ if (transport_is_reliable)
+ strcat (own_buf, ";QStartNoAckMode+");
+
+ if (the_target->qxfer_osdata != NULL)
+ strcat (own_buf, ";qXfer:osdata:read+");
+
+ if (target_supports_multi_process ())
+ strcat (own_buf, ";multiprocess+");
+
+ if (target_supports_non_stop ())
+ strcat (own_buf, ";QNonStop+");
+
+ strcat (own_buf, ";qXfer:threads:read+");
+
+ if (target_supports_tracepoints ())
+ {
+ strcat (own_buf, ";ConditionalTracepoints+");
+ strcat (own_buf, ";TraceStateVariables+");
+ strcat (own_buf, ";TracepointSource+");
+ strcat (own_buf, ";DisconnectedTracing+");
+ if (gdb_supports_qRelocInsn && target_supports_fast_tracepoints ())
+ strcat (own_buf, ";FastTracepoints+");
+ strcat (own_buf, ";StaticTracepoints+");
+ strcat (own_buf, ";qXfer:statictrace:read+");
+ }
+
+ return;
+ }
+
+ /* Thread-local storage support. */
+ if (the_target->get_tls_address != NULL
+ && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
+ {
+ char *p = own_buf + 12;
+ CORE_ADDR parts[2], address = 0;
+ int i, err;
+ ptid_t ptid = null_ptid;
+
+ require_running (own_buf);
+
+ for (i = 0; i < 3; i++)
+ {
+ char *p2;
+ int len;
+
+ if (p == NULL)
+ break;
+
+ p2 = strchr (p, ',');
+ if (p2)
+ {
+ len = p2 - p;
+ p2++;
+ }
+ else
+ {
+ len = strlen (p);
+ p2 = NULL;
+ }
+
+ if (i == 0)
+ ptid = read_ptid (p, NULL);
+ else
+ decode_address (&parts[i - 1], p, len);
+ p = p2;
+ }
+
+ if (p != NULL || i < 3)
+ err = 1;
+ else
+ {
+ struct thread_info *thread = find_thread_ptid (ptid);
+
+ if (thread == NULL)
+ err = 2;
+ else
+ err = the_target->get_tls_address (thread, parts[0], parts[1],
+ &address);
+ }
+
+ if (err == 0)
+ {
+ strcpy (own_buf, paddress(address));
+ return;
+ }
+ else if (err > 0)
+ {
+ write_enn (own_buf);
+ return;
+ }
+
+ /* Otherwise, pretend we do not understand this packet. */
+ }
+
+ /* Windows OS Thread Information Block address support. */
+ if (the_target->get_tib_address != NULL
+ && strncmp ("qGetTIBAddr:", own_buf, 12) == 0)
+ {
+ char *annex;
+ int n;
+ CORE_ADDR tlb;
+ ptid_t ptid = read_ptid (own_buf + 12, &annex);
+
+ n = (*the_target->get_tib_address) (ptid, &tlb);
+ if (n == 1)
+ {
+ strcpy (own_buf, paddress(tlb));
+ return;
+ }
+ else if (n == 0)
+ {
+ write_enn (own_buf);
+ return;
+ }
+ return;
+ }
+
+ /* Handle "monitor" commands. */
+ if (strncmp ("qRcmd,", own_buf, 6) == 0)
+ {
+ char *mon = malloc (PBUFSIZ);
+ int len = strlen (own_buf + 6);
+
+ if (mon == NULL)
+ {
+ write_enn (own_buf);
+ return;
+ }
+
+ if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
+ {
+ write_enn (own_buf);
+ free (mon);
+ return;
+ }
+ mon[len / 2] = '\0';
+
+ write_ok (own_buf);
+
+ if (the_target->handle_monitor_command == NULL
+ || (*the_target->handle_monitor_command) (mon) == 0)
+ /* Default processing. */
+ handle_monitor_command (mon);
+
+ free (mon);
+ return;
+ }
+
+ if (strncmp ("qSearch:memory:", own_buf, sizeof ("qSearch:memory:") - 1) == 0)
+ {
+ require_running (own_buf);
+ handle_search_memory (own_buf, packet_len);
+ return;
+ }
+
+ if (strcmp (own_buf, "qAttached") == 0
+ || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0)
+ {
+ struct process_info *process;
+
+ if (own_buf[sizeof ("qAttached") - 1])
+ {
+ int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16);
+ process = (struct process_info *)
+ find_inferior_id (&all_processes, pid_to_ptid (pid));
+ }
+ else
+ {
+ require_running (own_buf);
+ process = current_process ();
+ }
+
+ if (process == NULL)
+ {
+ write_enn (own_buf);
+ return;
+ }
+
+ strcpy (own_buf, process->attached ? "1" : "0");
+ return;
+ }
+
+ if (strncmp ("qCRC:", own_buf, 5) == 0)
+ {
+ /* CRC check (compare-section). */
+ char *comma;
+ CORE_ADDR base;
+ int len;
+ unsigned long long crc;
+
+ require_running (own_buf);
+ base = strtoul (own_buf + 5, &comma, 16);
+ if (*comma++ != ',')
+ {
+ write_enn (own_buf);
+ return;
+ }
+ len = strtoul (comma, NULL, 16);
+ crc = crc32 (base, len, 0xffffffff);
+ /* Check for memory failure. */
+ if (crc == (unsigned long long) -1)
+ {
+ write_enn (own_buf);
+ return;
+ }
+ sprintf (own_buf, "C%lx", (unsigned long) crc);
+ return;
+ }
+
+ if (target_supports_tracepoints () && handle_tracepoint_query (own_buf))
+ return;
+
+ /* Otherwise we didn't know what packet it was. Say we didn't
+ understand it. */
+ own_buf[0] = 0;
+}
+
+/* Parse vCont packets. */
+void
+handle_v_cont (char *own_buf)
+{
+ char *p, *q;
+ int n = 0, i = 0;
+ struct thread_resume *resume_info;
+ struct thread_resume default_action = {{0}};
+
+ /* Count the number of semicolons in the packet. There should be one
+ for every action. */
+ p = &own_buf[5];
+ while (p)
+ {
+ n++;
+ p++;
+ p = strchr (p, ';');
+ }
+
+ resume_info = malloc (n * sizeof (resume_info[0]));
+ if (resume_info == NULL)
+ goto err;
+
+ p = &own_buf[5];
+ while (*p)
+ {
+ p++;
+
+ if (p[0] == 's' || p[0] == 'S')
+ resume_info[i].kind = resume_step;
+ else if (p[0] == 'c' || p[0] == 'C')
+ resume_info[i].kind = resume_continue;
+ else if (p[0] == 't')
+ resume_info[i].kind = resume_stop;
+ else
+ goto err;
+
+ if (p[0] == 'S' || p[0] == 'C')
+ {
+ int sig;
+ sig = strtol (p + 1, &q, 16);
+ if (p == q)
+ goto err;
+ p = q;
+
+ if (!target_signal_to_host_p (sig))
+ goto err;
+ resume_info[i].sig = target_signal_to_host (sig);
+ }
+ else
+ {
+ resume_info[i].sig = 0;
+ p = p + 1;
+ }
+
+ if (p[0] == 0)
+ {
+ resume_info[i].thread = minus_one_ptid;
+ default_action = resume_info[i];
+
+ /* Note: we don't increment i here, we'll overwrite this entry
+ the next time through. */
+ }
+ else if (p[0] == ':')
+ {
+ ptid_t ptid = read_ptid (p + 1, &q);
+
+ if (p == q)
+ goto err;
+ p = q;
+ if (p[0] != ';' && p[0] != 0)
+ goto err;
+
+ resume_info[i].thread = ptid;
+
+ i++;
+ }
+ }
+
+ if (i < n)
+ resume_info[i] = default_action;
+
+ /* Still used in occasional places in the backend. */
+ if (n == 1
+ && !ptid_equal (resume_info[0].thread, minus_one_ptid)
+ && resume_info[0].kind != resume_stop)
+ cont_thread = resume_info[0].thread;
+ else
+ cont_thread = minus_one_ptid;
+ set_desired_inferior (0);
+
+ if (!non_stop)
+ enable_async_io ();
+
+ (*the_target->resume) (resume_info, n);
+
+ free (resume_info);
+
+ 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 ();
+
+ if (last_status.kind == TARGET_WAITKIND_EXITED
+ || last_status.kind == TARGET_WAITKIND_SIGNALLED)
+ mourn_inferior (find_process_pid (ptid_get_pid (last_ptid)));
+ }
+ return;
+
+err:
+ write_enn (own_buf);
+ free (resume_info);
+ return;
+}
+
+/* Attach to a new program. Return 1 if successful, 0 if failure. */
+int
+handle_v_attach (char *own_buf)
+{
+ int pid;
+
+ pid = strtol (own_buf + 8, NULL, 16);
+ if (pid != 0 && attach_inferior (pid) == 0)
+ {
+ /* Don't report shared library events after attaching, even if
+ some libraries are preloaded. GDB will always poll the
+ library list. Avoids the "stopped by shared library event"
+ notice on the GDB side. */
+ dlls_changed = 0;
+
+ if (non_stop)
+ {
+ /* In non-stop, we don't send a resume reply. Stop events
+ will follow up using the normal notification
+ mechanism. */
+ write_ok (own_buf);
+ }
+ else
+ prepare_resume_reply (own_buf, last_ptid, &last_status);
+
+ return 1;
+ }
+ else
+ {
+ write_enn (own_buf);
+ return 0;
+ }
+}
+
+/* Run a new program. Return 1 if successful, 0 if failure. */
+static int
+handle_v_run (char *own_buf)
+{
+ char *p, *next_p, **new_argv;
+ int i, new_argc;
+
+ new_argc = 0;
+ for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
+ {
+ p++;
+ new_argc++;
+ }
+
+ new_argv = calloc (new_argc + 2, sizeof (char *));
+ if (new_argv == NULL)
+ {
+ write_enn (own_buf);
+ return 0;
+ }
+
+ i = 0;
+ for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
+ {
+ next_p = strchr (p, ';');
+ if (next_p == NULL)
+ next_p = p + strlen (p);
+
+ if (i == 0 && p == next_p)
+ new_argv[i] = NULL;
+ else
+ {
+ /* FIXME: Fail request if out of memory instead of dying. */
+ new_argv[i] = xmalloc (1 + (next_p - p) / 2);
+ unhexify (new_argv[i], p, (next_p - p) / 2);
+ new_argv[i][(next_p - p) / 2] = '\0';
+ }
+
+ if (*next_p)
+ next_p++;
+ i++;
+ }
+ new_argv[i] = NULL;
+
+ if (new_argv[0] == NULL)
+ {
+ /* GDB didn't specify a program to run. Use the program from the
+ last run with the new argument list. */
+
+ if (program_argv == NULL)
+ {
+ /* FIXME: new_argv memory leak */
+ write_enn (own_buf);
+ return 0;
+ }
+
+ new_argv[0] = strdup (program_argv[0]);
+ if (new_argv[0] == NULL)
+ {
+ /* FIXME: new_argv memory leak */
+ write_enn (own_buf);
+ return 0;
+ }
+ }
+
+ /* Free the old argv and install the new one. */
+ freeargv (program_argv);
+ program_argv = new_argv;
+
+ start_inferior (program_argv);
+ if (last_status.kind == TARGET_WAITKIND_STOPPED)
+ {
+ prepare_resume_reply (own_buf, last_ptid, &last_status);
+
+ /* In non-stop, sending a resume reply doesn't set the general
+ thread, but GDB assumes a vRun sets it (this is so GDB can
+ query which is the main thread of the new inferior. */
+ if (non_stop)
+ general_thread = last_ptid;
+
+ return 1;
+ }
+ else
+ {
+ write_enn (own_buf);
+ return 0;
+ }
+}
+
+/* Kill process. Return 1 if successful, 0 if failure. */
+int
+handle_v_kill (char *own_buf)
+{
+ int pid;
+ char *p = &own_buf[6];
+ if (multi_process)
+ pid = strtol (p, NULL, 16);
+ else
+ pid = signal_pid;
+ if (pid != 0 && kill_inferior (pid) == 0)
+ {
+ last_status.kind = TARGET_WAITKIND_SIGNALLED;
+ last_status.value.sig = TARGET_SIGNAL_KILL;
+ last_ptid = pid_to_ptid (pid);
+ discard_queued_stop_replies (pid);
+ write_ok (own_buf);
+ return 1;
+ }
+ else
+ {
+ write_enn (own_buf);
+ return 0;
+ }
+}
+
+/* Handle a 'vStopped' packet. */
+static void
+handle_v_stopped (char *own_buf)
+{
+ /* If we're waiting for GDB to acknowledge a pending stop reply,
+ consider that done. */
+ if (notif_queue)
+ {
+ struct vstop_notif *head;
+
+ if (remote_debug)
+ fprintf (stderr, "vStopped: acking %s\n",
+ target_pid_to_str (notif_queue->ptid));
+
+ head = notif_queue;
+ notif_queue = notif_queue->next;
+ free (head);
+ }
+
+ /* Push another stop reply, or if there are no more left, an OK. */
+ send_next_stop_reply (own_buf);
+}
+
+/* Handle all of the extended 'v' packets. */
+void
+handle_v_requests (char *own_buf, int packet_len, int *new_packet_len)
+{
+ if (!disable_packet_vCont)
+ {
+ if (strncmp (own_buf, "vCont;", 6) == 0)
+ {
+ require_running (own_buf);
+ handle_v_cont (own_buf);
+ return;
+ }
+
+ if (strncmp (own_buf, "vCont?", 6) == 0)
+ {
+ strcpy (own_buf, "vCont;c;C;s;S;t");
+ return;
+ }
+ }
+
+ if (strncmp (own_buf, "vFile:", 6) == 0
+ && handle_vFile (own_buf, packet_len, new_packet_len))
+ return;
+
+ 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. */
+static 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 ();
+
+ if (last_status.kind == TARGET_WAITKIND_EXITED
+ || last_status.kind == TARGET_WAITKIND_SIGNALLED)
+ mourn_inferior (find_process_pid (ptid_get_pid (last_ptid)));
+ }
+}
+
+/* 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)
+{
+ struct thread_info *thread = (struct thread_info *) entry;
+
+ /* For now, assume targets that don't have this callback also don't
+ manage the thread's last_status field. */
+ if (the_target->thread_stopped == NULL)
+ {
+ 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
+ yet. */
+ queue_stop_reply (entry->id, &thread->last_status);
+ }
+ else
+ {
+ if (thread_stopped (thread))
+ {
+ if (debug_threads)
+ fprintf (stderr, "Reporting thread %s as already stopped with %s\n",
+ target_pid_to_str (entry->id),
+ target_waitstatus_to_string (&thread->last_status));
+
+ /* Pass the last stop reply back to GDB, but don't notify
+ yet. */
+ queue_stop_reply (entry->id, &thread->last_status);
+ }
+ }
+
+ return 0;
+}
+
+/* Set this inferior LWP's state as "want-stopped". We won't resume
+ this LWP until the client gives us another action for it. */
+
+static void
+gdb_wants_thread_stopped (struct inferior_list_entry *entry)
+{
+ struct thread_info *thread = (struct thread_info *) entry;
+
+ thread->last_resume_kind = resume_stop;
+
+ if (thread->last_status.kind == TARGET_WAITKIND_IGNORE)
+ {
+ thread->last_status.kind = TARGET_WAITKIND_STOPPED;
+ thread->last_status.value.sig = TARGET_SIGNAL_0;
+ }
+}
+
+/* Set all threads' states as "want-stopped". */
+
+static void
+gdb_wants_all_threads_stopped (void)
+{
+ for_each_inferior (&all_threads, gdb_wants_thread_stopped);
+}
+
+/* Clear the gdb_detached flag of every process. */
+
+static void
+gdb_reattached_process (struct inferior_list_entry *entry)
+{
+ struct process_info *process = (struct process_info *) entry;
+
+ process->gdb_detached = 0;
+}
+
+/* Status handler for the '?' packet. */
+
+static void
+handle_status (char *own_buf)
+{
+ /* GDB is connected, don't forward events to the target anymore. */
+ for_each_inferior (&all_processes, gdb_reattached_process);
+
+ /* 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)
+ {
+ discard_queued_stop_replies (-1);
+ find_inferior (&all_threads, queue_stop_reply_callback, NULL);
+
+ /* 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
+ {
+ pause_all (0);
+ stabilize_threads ();
+ gdb_wants_all_threads_stopped ();
+
+ if (all_threads.head)
+ {
+ struct target_waitstatus status;
+
+ status.kind = TARGET_WAITKIND_STOPPED;
+ status.value.sig = TARGET_SIGNAL_TRAP;
+ 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) 2010 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)
+ {
+ disable_packet_vCont = 1;
+ disable_packet_Tthread = 1;
+ disable_packet_qC = 1;
+ disable_packet_qfThreadInfo = 1;
+ }
+ else
+ {
+ fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
+ tok);
+ gdbserver_show_disableable (stderr);
+ exit (1);
+ }
+ }
+ }
+ else
+ {
+ fprintf (stderr, "Unknown argument: %s\n", *next_arg);
+ exit (1);
+ }
+
+ next_arg++;
+ continue;
+ }
+
+ if (setjmp (toplevel))
+ {
+ fprintf (stderr, "Exiting\n");
+ exit (1);
+ }
+
+ port = *next_arg;
+ next_arg++;
+ if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
+ {
+ gdbserver_usage (stderr);
+ exit (1);
+ }
+
+ bad_attach = 0;
+ pid = 0;
+
+ /* --attach used to come after PORT, so allow it there for
+ compatibility. */
+ if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
+ {
+ attach = 1;
+ next_arg++;
+ }
+
+ if (attach
+ && (*next_arg == NULL
+ || (*next_arg)[0] == '\0'
+ || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
+ || *arg_end != '\0'
+ || next_arg[1] != NULL))
+ bad_attach = 1;
+
+ if (bad_attach)
+ {
+ gdbserver_usage (stderr);
+ exit (1);
+ }
+
+ initialize_inferiors ();
+ initialize_async_io ();
+ initialize_low ();
+ if (target_supports_tracepoints ())
+ initialize_tracepoint ();
+
+ own_buf = xmalloc (PBUFSIZ + 1);
+ mem_buf = xmalloc (PBUFSIZ);
+
+ if (pid == 0 && *next_arg != NULL)
+ {
+ int i, n;
+
+ n = argc - (next_arg - argv);
+ program_argv = xmalloc (sizeof (char *) * (n + 1));
+ for (i = 0; i < n; i++)
+ program_argv[i] = xstrdup (next_arg[i]);
+ program_argv[i] = NULL;
+
+ /* Wait till we are at first instruction in program. */
+ start_inferior (program_argv);
+
+ /* We are now (hopefully) stopped at the first instruction of
+ the target process. This assumes that the target process was
+ successfully created. */
+ }
+ else if (pid != 0)
+ {
+ if (attach_inferior (pid) == -1)
+ error ("Attaching not supported on this target");
+
+ /* Otherwise succeeded. */
+ }
+ else
+ {
+ last_status.kind = TARGET_WAITKIND_EXITED;
+ last_status.value.integer = 0;
+ last_ptid = minus_one_ptid;
+ }
+
+ /* Don't report shared library events on the initial connection,
+ even if some libraries are preloaded. Avoids the "stopped by
+ shared library event" notice on gdb side. */
+ dlls_changed = 0;
+
+ if (setjmp (toplevel))