Multi-target support
[deliverable/binutils-gdb.git] / gdb / gdbserver / remote-utils.c
index f968e554a520137ba64a5713fcfef6a7e58da603..b8a8c6576f974b57264d1f6f8c18de79a5b72cb8 100644 (file)
@@ -1,5 +1,5 @@
 /* Remote utility routines for the remote server for GDB.
-   Copyright (C) 1986-2018 Free Software Foundation, Inc.
+   Copyright (C) 1986-2020 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include "target.h"
 #include "gdbthread.h"
 #include "tdesc.h"
+#include "debug.h"
 #include "dll.h"
-#include "rsp-low.h"
-#include "gdbthread.h"
-#include "netstuff.h"
-#include "filestuff.h"
+#include "gdbsupport/rsp-low.h"
+#include "gdbsupport/netstuff.h"
+#include "gdbsupport/filestuff.h"
+#include "gdbsupport/gdb-sigmask.h"
 #include <ctype.h>
 #if HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
@@ -38,9 +39,6 @@
 #if HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #endif
-#if HAVE_SYS_UN_H
-#include <sys/un.h>
-#endif
 #if HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
@@ -59,7 +57,7 @@
 #if HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
-#include "gdb_sys_time.h"
+#include "gdbsupport/gdb_sys_time.h"
 #include <unistd.h>
 #if HAVE_ARPA_INET_H
 #include <arpa/inet.h>
@@ -67,8 +65,7 @@
 #include <sys/stat.h>
 
 #if USE_WIN32API
-#include <winsock2.h>
-#include <wspiapi.h>
+#include <ws2tcpip.h>
 #endif
 
 #if __QNX__
@@ -109,18 +106,11 @@ struct sym_cache
   struct sym_cache *next;
 };
 
-int remote_debug = 0;
-struct ui_file *gdb_stdlog;
-
 static int remote_is_stdio = 0;
 
 static gdb_fildes_t remote_desc = INVALID_DESCRIPTOR;
 static gdb_fildes_t listen_desc = INVALID_DESCRIPTOR;
 
-/* FIXME headerize? */
-extern int using_threads;
-extern int debug_threads;
-
 #ifdef USE_WIN32API
 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
@@ -196,36 +186,20 @@ handle_accept_event (int err, gdb_client_data client_data)
      descriptor open for add_file_handler to wait for a new connection.  */
   delete_file_handler (listen_desc);
 
-#if HAVE_SYS_UN_H
-  if (sockaddr.ss_family == AF_UNIX)
-    {
-      struct sockaddr_un su;
-      socklen_t socklen = sizeof (su);
+  /* Convert IP address to string.  */
+  char orig_host[GDB_NI_MAX_ADDR], orig_port[GDB_NI_MAX_PORT];
 
-      if (getsockname (listen_desc, (struct sockaddr *) &su, &socklen) != 0)
-       perror (_("Could not obtain remote address"));
+  int r = getnameinfo ((struct sockaddr *) &sockaddr, len,
+                      orig_host, sizeof (orig_host),
+                      orig_port, sizeof (orig_port),
+                      NI_NUMERICHOST | NI_NUMERICSERV);
 
-      fprintf (stderr, _("Remote debugging on local socket bound to %s\n"),
-              su.sun_path);
-    }
+  if (r != 0)
+    fprintf (stderr, _("Could not obtain remote address: %s\n"),
+            gai_strerror (r));
   else
-#endif
-    {
-      /* Convert IP address to string.  */
-      char orig_host[GDB_NI_MAX_ADDR], orig_port[GDB_NI_MAX_PORT];
-
-      int r = getnameinfo ((struct sockaddr *) &sockaddr, len,
-                          orig_host, sizeof (orig_host),
-                          orig_port, sizeof (orig_port),
-                          NI_NUMERICHOST | NI_NUMERICSERV);
-
-      if (r != 0)
-       fprintf (stderr, _("Could not obtain remote address: %s\n"),
-                gai_strerror (r));
-      else
-       fprintf (stderr, _("Remote debugging from host %s, port %s\n"),
-                orig_host, orig_port);
-    }
+    fprintf (stderr, _("Remote debugging from host %s, port %s\n"),
+            orig_host, orig_port);
 
   enable_async_notification (remote_desc);
 
@@ -269,9 +243,6 @@ remote_prepare (const char *name)
   struct addrinfo hint;
   struct addrinfo *ainfo;
 
-  struct sockaddr *addr;
-  socklen_t addrlen;
-
   memset (&hint, 0, sizeof (hint));
   /* Assume no prefix will be passed, therefore we should use
      AF_UNSPEC.  */
@@ -280,7 +251,7 @@ remote_prepare (const char *name)
   hint.ai_protocol = IPPROTO_TCP;
 
   parsed_connection_spec parsed
-    = parse_connection_spec (name, &hint);
+    = parse_connection_spec_without_prefix (name, &hint);
 
   if (parsed.port_str.empty ())
     {
@@ -298,92 +269,47 @@ remote_prepare (const char *name)
     }
 #endif
 
-#if HAVE_SYS_UN_H
-  struct sockaddr_un unix_addr;
-
-#ifndef UNIX_PATH_MAX
-#define UNIX_PATH_MAX sizeof (((struct sockaddr_un *) NULL)->sun_path)
-#endif
-
-  if (hint.ai_family == AF_LOCAL)
-    {
-      const char *sock_name = parsed.port_str.c_str ();
-      if (parsed.port_str.length () > UNIX_PATH_MAX - 1)
-       {
-         error
-           (_("%s is too long.  Socket names may be no longer than %s bytes."),
-            sock_name, pulongest (UNIX_PATH_MAX - 1));
-         return;
-       }
-      listen_desc = socket (AF_UNIX,  SOCK_STREAM,  0);
-      if (listen_desc < 0)
-       perror_with_name ("Can't open socket");
+  int r = getaddrinfo (parsed.host_str.c_str (), parsed.port_str.c_str (),
+                      &hint, &ainfo);
 
-      memset (&unix_addr, 0, sizeof (unix_addr));
-      unix_addr.sun_family = AF_UNIX;
-      strncpy (unix_addr.sun_path, sock_name, UNIX_PATH_MAX - 1);
+  if (r != 0)
+    error (_("%s: cannot resolve name: %s"), name, gai_strerror (r));
 
-      struct stat statbuf;
-      int stat_result = stat (sock_name, &statbuf);
+  scoped_free_addrinfo freeaddrinfo (ainfo);
 
-      if (stat_result == 0
-         && S_ISSOCK (statbuf.st_mode))
-       unlink (sock_name);
+  struct addrinfo *iter;
 
-      addr = (struct sockaddr *) &unix_addr;
-      addrlen = sizeof (unix_addr);
-    }
-  else
-#endif
+  for (iter = ainfo; iter != NULL; iter = iter->ai_next)
     {
-      struct addrinfo *iter;
-
-      int r = getaddrinfo (parsed.host_str.c_str (),
-                          parsed.port_str.c_str (),
-                          &hint, &ainfo);
-
-      if (r != 0)
-       error (_("%s: cannot resolve name: %s"), name, gai_strerror (r));
-
-      scoped_free_addrinfo freeaddrinfo (ainfo);
-
-      for (iter = ainfo; iter != NULL; iter = iter->ai_next)
-       {
-         listen_desc = gdb_socket_cloexec (iter->ai_family, iter->ai_socktype,
-                                           iter->ai_protocol);
-
-         if (listen_desc >= 0)
-           break;
-       }
+      listen_desc = gdb_socket_cloexec (iter->ai_family, iter->ai_socktype,
+                                       iter->ai_protocol);
 
-      if (iter == NULL)
-       perror_with_name ("Can't open socket");
+      if (listen_desc >= 0)
+       break;
+    }
 
-      /* Allow rapid reuse of this port. */
-      tmp = 1;
-      setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
-                 sizeof (tmp));
+  if (iter == NULL)
+    perror_with_name ("Can't open socket");
 
-      if (iter->ai_socktype == SOCK_DGRAM)
-       error (_("Only stream orientated protocols are currently supported."));
+  /* Allow rapid reuse of this port. */
+  tmp = 1;
+  setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
+             sizeof (tmp));
 
-      switch (iter->ai_family)
-       {
-       case AF_INET:
-         ((struct sockaddr_in *) iter->ai_addr)->sin_addr.s_addr = INADDR_ANY;
-         break;
-       case AF_INET6:
-         ((struct sockaddr_in6 *) iter->ai_addr)->sin6_addr = in6addr_any;
-         break;
-       default:
-         internal_error (__FILE__, __LINE__,
-                         _("Invalid 'ai_family' %d\n"), iter->ai_family);
-       }
-      addr = iter->ai_addr;
-      addrlen = iter->ai_addrlen;
+  switch (iter->ai_family)
+    {
+    case AF_INET:
+      ((struct sockaddr_in *) iter->ai_addr)->sin_addr.s_addr = INADDR_ANY;
+      break;
+    case AF_INET6:
+      ((struct sockaddr_in6 *) iter->ai_addr)->sin6_addr = in6addr_any;
+      break;
+    default:
+      internal_error (__FILE__, __LINE__,
+                     _("Invalid 'ai_family' %d\n"), iter->ai_family);
     }
 
-  if (bind (listen_desc, addr, addrlen) != 0)
+  if (bind (listen_desc, iter->ai_addr, iter->ai_addrlen) != 0)
     perror_with_name ("Can't bind address");
 
   if (listen (listen_desc, 1) != 0)
@@ -421,11 +347,11 @@ remote_open (const char *name)
     }
 #ifndef USE_WIN32API
   else if (port_str == NULL)
-     {
+    {
       struct stat statbuf;
 
       if (stat (name, &statbuf) == 0
-       && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
+         && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
        remote_desc = open (name, O_RDWR);
       else
        {
@@ -882,7 +808,7 @@ block_unblock_async_io (int block)
 
   sigemptyset (&sigio_set);
   sigaddset (&sigio_set, SIGIO);
-  sigprocmask (block ? SIG_BLOCK : SIG_UNBLOCK, &sigio_set, NULL);
+  gdb_sigmask (block ? SIG_BLOCK : SIG_UNBLOCK, &sigio_set, NULL);
 #endif
 }
 
@@ -1220,7 +1146,7 @@ prepare_resume_reply (char *buf, ptid_t ptid,
        struct regcache *regcache;
 
        if ((status->kind == TARGET_WAITKIND_FORKED && cs.report_fork_events)
-           || (status->kind == TARGET_WAITKIND_VFORKED
+           || (status->kind == TARGET_WAITKIND_VFORKED 
                && cs.report_vfork_events))
          {
            enum gdb_signal signal = GDB_SIGNAL_TRAP;
@@ -1232,7 +1158,7 @@ prepare_resume_reply (char *buf, ptid_t ptid,
            buf = write_ptid (buf, status->value.related_pid);
            strcat (buf, ";");
          }
-       else if (status->kind == TARGET_WAITKIND_VFORK_DONE
+       else if (status->kind == TARGET_WAITKIND_VFORK_DONE 
                 && cs.report_vfork_events)
          {
            enum gdb_signal signal = GDB_SIGNAL_TRAP;
@@ -1282,7 +1208,7 @@ prepare_resume_reply (char *buf, ptid_t ptid,
 
        saved_thread = current_thread;
 
-       switch_to_thread (ptid);
+       switch_to_thread (the_target, ptid);
 
        regp = current_target_desc ()->expedite_regs;
 
@@ -1630,7 +1556,7 @@ look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
 
   if (!startswith (cs.own_buf, "qSymbol:"))
     {
-      warning ("Malformed response to qSymbol, ignoring: %s\n", cs.own_buf);
+      warning ("Malformed response to qSymbol, ignoring: %s", cs.own_buf);
       return -1;
     }
 
@@ -1708,7 +1634,7 @@ relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
        {
          if (decode_X_packet (&cs.own_buf[1], len - 1, &mem_addr,
                               &mem_len, &mem_buf) < 0
-             || write_inferior_memory (mem_addr, mem_buf, mem_len) != 0)
+             || target_write_memory (mem_addr, mem_buf, mem_len) != 0)
            write_enn (cs.own_buf);
          else
            write_ok (cs.own_buf);
@@ -1716,7 +1642,7 @@ relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
       else
        {
          decode_M_packet (&cs.own_buf[1], &mem_addr, &mem_len, &mem_buf);
-         if (write_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
+         if (target_write_memory (mem_addr, mem_buf, mem_len) == 0)
            write_ok (cs.own_buf);
          else
            write_enn (cs.own_buf);
@@ -1731,14 +1657,14 @@ relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
 
   if (cs.own_buf[0] == 'E')
     {
-      warning ("An error occurred while relocating an instruction: %s\n",
+      warning ("An error occurred while relocating an instruction: %s",
               cs.own_buf);
       return -1;
     }
 
   if (!startswith (cs.own_buf, "qRelocInsn:"))
     {
-      warning ("Malformed response to qRelocInsn, ignoring: %s\n",
+      warning ("Malformed response to qRelocInsn, ignoring: %s",
               cs.own_buf);
       return -1;
     }
This page took 0.033938 seconds and 4 git commands to generate.