Cherry-pick 7.10 changes to 7.11.

Cherry-picked commits:
    e24220e gdb: disable python when python is not available.
    d73c546 use snprintf instead of strlcpy for portablitly
    c64829b Fallback to open if multifs_open fails.
    2177073 Fix gdbserver process interruption.
    b711aa8 Add Unix domain socket support to gdbserver 7.9.1

Change-Id: I2cba7ae5c05503afb81f732cbd78440ebd2c45e8
diff --git a/gdb-7.11/gdb/defs.h b/gdb-7.11/gdb/defs.h
index f6ffeac..8d8e734 100644
--- a/gdb-7.11/gdb/defs.h
+++ b/gdb-7.11/gdb/defs.h
@@ -690,4 +690,8 @@
 
 #include "utils.h"
 
+#ifdef HAVE_PYTHON
+extern int python_available (void);
+#endif
+
 #endif /* #ifndef DEFS_H */
diff --git a/gdb-7.11/gdb/gdbserver/hostio.c b/gdb-7.11/gdb/gdbserver/hostio.c
index 242206e..ae0b303 100644
--- a/gdb-7.11/gdb/gdbserver/hostio.c
+++ b/gdb-7.11/gdb/gdbserver/hostio.c
@@ -297,7 +297,7 @@
 {
   char filename[HOSTIO_PATH_MAX];
   char *p;
-  int fileio_flags, fileio_mode, flags, fd;
+  int fileio_flags, fileio_mode, flags, fd = -1;
   mode_t mode;
   struct fd_list *new_fd;
 
@@ -321,7 +321,11 @@
   if (hostio_fs_pid != 0 && the_target->multifs_open != NULL)
     fd = the_target->multifs_open (hostio_fs_pid, filename,
 				   flags, mode);
-  else
+
+  /* HACK: multifs_open will fail for android applications, because run-as does
+     not switch to the same mount namespace as the running application. Retry
+     with regular open if this happens.  */
+  if (fd == -1)
     fd = open (filename, flags, mode);
 
   if (fd == -1)
diff --git a/gdb-7.11/gdb/gdbserver/linux-low.c b/gdb-7.11/gdb/gdbserver/linux-low.c
index 8b025bd..4c46e94 100644
--- a/gdb-7.11/gdb/gdbserver/linux-low.c
+++ b/gdb-7.11/gdb/gdbserver/linux-low.c
@@ -5783,10 +5783,7 @@
 linux_request_interrupt (void)
 {
   extern unsigned long signal_pid;
-
-  /* Send a SIGINT to the process group.  This acts just like the user
-     typed a ^C on the controlling terminal.  */
-  kill (-signal_pid, SIGINT);
+  kill (signal_pid, SIGINT);
 }
 
 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
diff --git a/gdb-7.11/gdb/gdbserver/remote-utils.c b/gdb-7.11/gdb/gdbserver/remote-utils.c
index e751473..4ae6553 100644
--- a/gdb-7.11/gdb/gdbserver/remote-utils.c
+++ b/gdb-7.11/gdb/gdbserver/remote-utils.c
@@ -283,11 +283,63 @@
 /* Open a connection to a remote debugger.
    NAME is the filename used for communication.  */
 
+// ANDROID BEGIN.
+#ifndef USE_WIN32API
+#include <sys/un.h>
+#endif
+// ANDROID END.
+
 void
 remote_open (char *name)
 {
   char *port_str;
 
+  // ANDROID BEGIN.
+  // The Android NDK uses Unix domain sockets because applications
+  // aren't allowed to bind to localhost TCP sockets, and developers
+  // debugging on production devices can't get root.
+  // Typical ndk-gdb usage is "gdbserver +debug-socket --attach 123".
+  if (name[0] == '+')
+    {
+#ifdef USE_WIN32API
+      error ("Only <host>:<port> is supported on this platform.");
+#else
+      struct sockaddr_un sockaddr;
+      socklen_t sockaddrlen;
+
+      listen_desc = socket (AF_UNIX, SOCK_STREAM, 0);
+      if (listen_desc == -1)
+        perror_with_name ("Can't create Unix domain socket");
+
+      /* Skip the initial '+'. */
+      name++;
+
+      memset (&sockaddr, 0, sizeof sockaddr);
+      sockaddr.sun_family = AF_UNIX;
+      snprintf(sockaddr.sun_path, sizeof (sockaddr.sun_path), "%s", name);
+      sockaddrlen = sizeof (sockaddr.sun_family) +
+          strlen (sockaddr.sun_path) + 1;
+
+      unlink (sockaddr.sun_path);
+
+      if (bind (listen_desc, (struct sockaddr *) &sockaddr, sockaddrlen)
+          || listen (listen_desc, 1))
+        perror_with_name ("Can't bind Unix domain socket");
+
+      fprintf (stderr, "Listening on Unix domain socket '%s'\n",
+               sockaddr.sun_path);
+      fflush (stderr);
+
+      /* Register the event loop handler.  */
+      add_file_handler (listen_desc, handle_accept_event, NULL);
+
+      transport_is_reliable = 1;
+#endif
+
+      return;
+    }
+  // ANDROID END.
+
   port_str = strchr (name, ':');
 #ifdef USE_WIN32API
   if (port_str == NULL)
diff --git a/gdb-7.11/gdb/python/py-auto-load.c b/gdb-7.11/gdb/python/py-auto-load.c
index 08ba58b..7e98e16 100644
--- a/gdb-7.11/gdb/python/py-auto-load.c
+++ b/gdb-7.11/gdb/python/py-auto-load.c
@@ -46,6 +46,8 @@
 int
 gdbpy_auto_load_enabled (const struct extension_language_defn *extlang)
 {
+  if (!python_available ())
+    return 0;
   return auto_load_python_scripts;
 }
 
diff --git a/gdb-7.11/gdb/python/py-prettyprint.c b/gdb-7.11/gdb/python/py-prettyprint.c
index e97769b..2836399 100644
--- a/gdb-7.11/gdb/python/py-prettyprint.c
+++ b/gdb-7.11/gdb/python/py-prettyprint.c
@@ -722,7 +722,7 @@
   if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
     return EXT_LANG_RC_NOP;
 
-  if (!gdb_python_initialized)
+  if (!python_available () || !gdb_python_initialized)
     return EXT_LANG_RC_NOP;
 
   cleanups = ensure_python_env (gdbarch, language);
diff --git a/gdb-7.11/gdb/python/python.c b/gdb-7.11/gdb/python/python.c
index 7202105..ef7431b 100644
--- a/gdb-7.11/gdb/python/python.c
+++ b/gdb-7.11/gdb/python/python.c
@@ -33,6 +33,13 @@
 #include "python.h"
 #include "extension-priv.h"
 #include "cli/cli-utils.h"
+#include "gdb_wait.h"
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <sys/types.h>
 #include <ctype.h>
 #include "location.h"
 
@@ -1625,6 +1632,44 @@
 /* Provide a prototype to silence -Wmissing-prototypes.  */
 extern initialize_file_ftype _initialize_python;
 
+#ifdef HAVE_PYTHON
+/* Check whether python is available at runtime. */
+
+int
+python_available(void)
+{
+#ifndef HAVE_WORKING_FORK
+  return 1;
+#endif
+
+  static int python_status = -1;
+  int child_status = 0;
+
+  if (python_status != -1)
+    return python_status;
+
+  pid_t pid = fork ();
+
+  if (pid < 0)
+    perror_with_name (("fork"));
+
+  if (pid == 0)
+    {
+      freopen ("/dev/null", "w", stderr);
+      Py_Initialize ();
+      _exit(0);
+    }
+
+  wait (&child_status);
+  if (WIFEXITED (child_status) && WEXITSTATUS (child_status) == 0)
+    python_status = 1;
+  else
+    python_status = 0;
+
+  return python_status;
+}
+#endif
+
 void
 _initialize_python (void)
 {
@@ -1748,6 +1793,9 @@
 #endif
 #endif
 
+  if (!python_available ())
+    return;
+
   Py_Initialize ();
   PyEval_InitThreads ();
 
@@ -1886,6 +1934,9 @@
   PyObject *sys_path;
   struct cleanup *cleanup;
 
+  if (!python_available())
+    return;
+
   cleanup = ensure_python_env (get_current_arch (), current_language);
 
   /* Add the initial data-directory to sys.path.  */
diff --git a/gdb-7.11/gdb/varobj.c b/gdb-7.11/gdb/varobj.c
index 6f56cba..d14c9eb 100644
--- a/gdb-7.11/gdb/varobj.c
+++ b/gdb-7.11/gdb/varobj.c
@@ -566,7 +566,7 @@
 #if HAVE_PYTHON
   struct cleanup *back_to;
 
-  if (!gdb_python_initialized)
+  if (!python_available () || !gdb_python_initialized)
     return NULL;
 
   back_to = varobj_ensure_python_env (var);
@@ -700,7 +700,7 @@
   PyObject *printer = var->dynamic->pretty_printer;
   int result;
 
-  if (!gdb_python_initialized)
+  if (!python_available () || !gdb_python_initialized)
     return 0;
 
   back_to = varobj_ensure_python_env (var);
@@ -1211,7 +1211,7 @@
 #if HAVE_PYTHON
   /* If the constructor is None, then we want the raw value.  If VAR
      does not have a value, just skip this.  */
-  if (!gdb_python_initialized)
+  if (!python_available () || !gdb_python_initialized)
     return;
 
   if (var->dynamic->constructor != Py_None && var->value != NULL)
@@ -1497,7 +1497,7 @@
   PyObject *mainmod, *globals, *constructor;
   struct cleanup *back_to;
 
-  if (!gdb_python_initialized)
+  if (!python_available () || !gdb_python_initialized)
     return;
 
   back_to = varobj_ensure_python_env (var);
@@ -2466,7 +2466,7 @@
 
   gdbarch = get_type_arch (value_type (value));
 #if HAVE_PYTHON
-  if (gdb_python_initialized)
+  if (python_available () && gdb_python_initialized)
     {
       PyObject *value_formatter =  var->dynamic->pretty_printer;