diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index c6e618a..85de646 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1178,7 +1178,6 @@ SFILES = \
 	reverse.c \
 	rust-exp.y \
 	rust-lang.c \
-	selftest.c \
 	selftest-arch.c \
 	sentinel-frame.c \
 	ser-base.c \
@@ -1244,6 +1243,7 @@ SFILES = \
 	common/ptid.c \
 	common/rsp-low.c \
 	common/run-time-clock.c \
+	common/selftest.c \
 	common/signals.c \
 	common/signals-state-save-restore.c \
 	common/vec.c \
diff --git a/gdb/NEWS b/gdb/NEWS
index 9cd1df1..8fca378 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,14 +3,19 @@
 
 *** Changes since GDB 8.0
 
-* On Unix systems, GDBserver now does globbing expansion and variable
-  substitution in inferior command line arguments.
-
-  This is done by starting inferiors using a shell, like GDB does.
-  See "set startup-with-shell" in the user manual for how to disable
-  this from GDB when using "target extended-remote".  When using
-  "target remote", you can disable the startup with shell by using the
-  new "--no-startup-with-shell" GDBserver command line option.
+* New features in the GDB remote stub, GDBserver
+
+  ** New "--selftest" command line option runs some GDBserver self
+     tests.  These self tests are disabled in release.
+
+  ** On Unix systems, GDBserver now does globbing expansion and variable
+     substitution in inferior command line arguments.
+
+     This is done by starting inferiors using a shell, like GDB does.
+     See "set startup-with-shell" in the user manual for how to disable
+     this from GDB when using "target extended-remote".  When using
+     "target remote", you can disable the startup with shell by using the
+     new "--no-startup-with-shell" GDBserver command line option.
 
 * New remote packets
 
diff --git a/gdb/selftest.c b/gdb/common/selftest.c
similarity index 92%
rename from gdb/selftest.c
rename to gdb/common/selftest.c
index eb7728b..0fb8f2a 100644
--- a/gdb/selftest.c
+++ b/gdb/common/selftest.c
@@ -16,7 +16,9 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-#include "defs.h"
+#include "common-defs.h"
+#include "common-exceptions.h"
+#include "common-debug.h"
 #include "selftest.h"
 #include <vector>
 
@@ -55,9 +57,7 @@ run_tests (void)
 	}
       END_CATCH
 
-      /* Clear GDB internal state.  */
-      registers_changed ();
-      reinit_frame_cache ();
+      reset ();
     }
 
   debug_printf ("Ran %lu unit tests, %d failed\n",
diff --git a/gdb/selftest.h b/gdb/common/selftest.h
similarity index 95%
rename from gdb/selftest.h
rename to gdb/common/selftest.h
index 8a01a5d..e211c34 100644
--- a/gdb/selftest.h
+++ b/gdb/common/selftest.h
@@ -36,6 +36,9 @@ extern void register_test (self_test_function *function);
 
 extern void run_tests (void);
 
+/* Reset GDB or GDBserver's internal state.  */
+extern void reset ();
+
 }
 
 /* Check that VALUE is true, and, if not, throw an exception.  */
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index d098eba..be03da0 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -20253,6 +20253,15 @@ environment:
 $ gdbserver --wrapper env LD_PRELOAD=libtest.so -- :2222 ./testprog
 @end smallexample
 
+@cindex @option{--selftest}
+The @option{--wrapper} option runs the self tests in @code{gdbserver}:
+
+@smallexample
+$ gdbserver --selftest
+Ran 2 unit tests, 0 failed
+@end smallexample
+
+These tests are disabled in release.
 @subsection Connecting to @code{gdbserver}
 
 The basic procedure for connecting to the remote target is:
diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in
index 4e0080e..6cd0959 100644
--- a/gdb/gdbserver/Makefile.in
+++ b/gdb/gdbserver/Makefile.in
@@ -258,6 +258,7 @@ OBS = \
 	regcache.o \
 	remote-utils.o \
 	rsp-low.o \
+	selftest.o \
 	server.o \
 	signals.o \
 	signals-state-save-restore.o \
diff --git a/gdb/gdbserver/config.in b/gdb/gdbserver/config.in
index 34a7443..5dacbac 100644
--- a/gdb/gdbserver/config.in
+++ b/gdb/gdbserver/config.in
@@ -8,6 +8,9 @@
 /* Define to 1 if using `alloca.c'. */
 #undef C_ALLOCA
 
+/* Define if self-testing features should be enabled */
+#undef GDB_SELF_TEST
+
 /* Define to 1 if you have `alloca', as a function or macro. */
 #undef HAVE_ALLOCA
 
diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure
index 35aeabc..30aa95b 100755
--- a/gdb/gdbserver/configure
+++ b/gdb/gdbserver/configure
@@ -5813,6 +5813,12 @@ fi
   fi
 
 
+if $development; then
+
+$as_echo "#define GDB_SELF_TEST 1" >>confdefs.h
+
+fi
+
  case ${build_alias} in
   "") build_noncanonical=${build} ;;
   *) build_noncanonical=${build_alias} ;;
diff --git a/gdb/gdbserver/configure.ac b/gdb/gdbserver/configure.ac
index 4ea7913..36e21c5 100644
--- a/gdb/gdbserver/configure.ac
+++ b/gdb/gdbserver/configure.ac
@@ -56,6 +56,11 @@ else
 fi
 GDB_AC_LIBMCHECK(${libmcheck_default})
 
+if $development; then
+  AC_DEFINE(GDB_SELF_TEST, 1,
+            [Define if self-testing features should be enabled])
+fi
+
 ACX_NONCANONICAL_TARGET
 ACX_NONCANONICAL_HOST
 
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 3838351..346a3c2 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -40,6 +40,8 @@
 #include "job-control.h"
 #include "environ.h"
 
+#include "common/selftest.h"
+
 /* The environment to pass to the inferior when creating it.  */
 
 static gdb_environ our_environ;
@@ -3357,6 +3359,7 @@ gdbserver_usage (FILE *stream)
 	   "                          Options:\n"
 	   "                            vCont, Tthread, qC, qfThreadInfo and \n"
 	   "                            threads (disable all threading packets).\n"
+	   "  --selftest            Run self tests.\n"
 	   "\n"
 	   "For more information, consult the GDB manual (available as on-line \n"
 	   "info or a printed manual).\n");
@@ -3521,6 +3524,7 @@ captured_main (int argc, char *argv[])
   volatile int multi_mode = 0;
   volatile int attach = 0;
   int was_running;
+  bool selftest = false;
 
   while (*next_arg != NULL && **next_arg == '-')
     {
@@ -3639,6 +3643,8 @@ captured_main (int argc, char *argv[])
 	startup_with_shell = false;
       else if (strcmp (*next_arg, "--once") == 0)
 	run_once = 1;
+      else if (strcmp (*next_arg, "--selftest") == 0)
+	selftest = true;
       else
 	{
 	  fprintf (stderr, "Unknown argument: %s\n", *next_arg);
@@ -3654,7 +3660,8 @@ captured_main (int argc, char *argv[])
       port = *next_arg;
       next_arg++;
     }
-  if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
+  if ((port == NULL || (!attach && !multi_mode && *next_arg == NULL))
+       && !selftest)
     {
       gdbserver_usage (stderr);
       exit (1);
@@ -3670,7 +3677,8 @@ captured_main (int argc, char *argv[])
      starting the inferior.  Inferiors created in this scenario have
      stdin,stdout redirected.  So do this here before we call
      start_inferior.  */
-  remote_prepare (port);
+  if (port != NULL)
+    remote_prepare (port);
 
   bad_attach = 0;
   pid = 0;
@@ -3711,6 +3719,12 @@ captured_main (int argc, char *argv[])
   own_buf = (char *) xmalloc (PBUFSIZ + 1);
   mem_buf = (unsigned char *) xmalloc (PBUFSIZ);
 
+  if (selftest)
+    {
+      selftests::run_tests ();
+      throw_quit ("Quit");
+    }
+
   if (pid == 0 && *next_arg != NULL)
     {
       int i, n;
@@ -4507,3 +4521,14 @@ handle_target_event (int err, gdb_client_data client_data)
 
   return 0;
 }
+
+#if GDB_SELF_TEST
+namespace selftests
+{
+
+void
+reset ()
+{}
+
+} // namespace selftests
+#endif /* GDB_SELF_TEST */
diff --git a/gdb/selftest-arch.c b/gdb/selftest-arch.c
index dfcbe27..9a19f76 100644
--- a/gdb/selftest-arch.c
+++ b/gdb/selftest-arch.c
@@ -33,6 +33,14 @@ register_test_foreach_arch (self_test_foreach_arch_function *function)
   gdbarch_tests.push_back (function);
 }
 
+void
+reset ()
+{
+  /* Clear GDB internal state.  */
+  registers_changed ();
+  reinit_frame_cache ();
+}
+
 static void
 tests_with_arch ()
 {
@@ -82,9 +90,7 @@ tests_with_arch ()
 	    }
 	  END_CATCH
 
-	  /* Clear GDB internal state.  */
-	  registers_changed ();
-	  reinit_frame_cache ();
+	  reset ();
 	}
     }
 
