Fix gdb support in run-test and run-test-jar

Test: ./art/test/run-test --gdb --64 --host 466-get-live-vreg
Test: ./art/test/run-test --gdbserver --64 --host 466-get-live-vreg
Change-Id: I5cb2cf615834456adbd662757a6c36c8bf00acc2
diff --git a/test/etc/run-test-jar b/test/etc/run-test-jar
index b79cbe5..2b72b66 100755
--- a/test/etc/run-test-jar
+++ b/test/etc/run-test-jar
@@ -82,9 +82,9 @@
 # the stdout will be used for a log, it will not append the color characters.
 bold_red=""
 if sys.stdout.isatty():
-  if int(run("tput colors", save_cmd=False).stdout) >= 1:
-    bold_red=run("tput bold", save_cmd=False).stdout.strip()
-    bold_red+=run("tput setaf 1", save_cmd=False).stdout.strip()
+  if int(subprocess.run(["tput", "colors"], capture_output=True).stdout) >= 1:
+    bold_red = subprocess.run(["tput", "bold"], text=True, capture_output=True).stdout.strip()
+    bold_red += subprocess.run(["tput", "setaf", "1"], text=True, capture_output=True).stdout.strip()
 
 def error_msg(msg: str):
   print(f"{bold_red}ERROR: {msg}")
@@ -140,7 +140,7 @@
 argp.add_argument("--experimental", default=[], action='append')
 argp.add_argument("--external-log-tags", action='store_true')
 argp.add_argument("--gc-stress", action='store_true')
-argp.add_argument("--gdb")
+argp.add_argument("--gdb", action='store_true')
 argp.add_argument("--gdb-arg", default=[], action='append')
 argp.add_argument("--gdb-dex2oat", action='store_true')
 argp.add_argument("--gdb-dex2oat-args", default=[], action='append')
@@ -663,6 +663,10 @@
   else:
     GDB=f"{GDBSERVER_HOST} {GDBSERVER_PORT}"
 
+    if shutil.which(GDBSERVER_HOST) is None:
+      error_msg(f"{GDBSERVER_HOST} is not available")
+      sys.exit(1)
+
 if INTERPRETER:
     INT_OPTS+=" -Xint"
 
@@ -1262,11 +1266,12 @@
 
     if USE_GDB:
       # When running under gdb, we cannot do piping and grepping...
-      subprocess.run(cmdline + test_args, env, shell=True)
+      env["TERM"] = os.environ.get("TERM", "")
+      subprocess.run(cmdline + test_args, env=env, shell=True)
     elif USE_GDBSERVER:
-      print("Connect to {GDBSERVER_PORT}")
+      print(f"Connect to {GDBSERVER_PORT}")
       # When running under gdb, we cannot do piping and grepping...
-      subprocess.run(cmdline + test_args, env, shell=True)
+      subprocess.run(cmdline + test_args, env=env, shell=True)
     else:
       if TIME_OUT != "gdb":
         proc = run(cmdline + test_args, env, check=False, quiet=False)
diff --git a/test/run-test b/test/run-test
index 63f28b1..02f84ef 100755
--- a/test/run-test
+++ b/test/run-test
@@ -29,11 +29,11 @@
   tmp_dir=f"{TMPDIR}/{test_dir}"
 checker=f"{progdir}/../tools/checker/checker.py"
 
-def run(cmdline: str, check=True) -> subprocess.CompletedProcess:
+def run(cmdline: str, check=True, capture_output=True) -> subprocess.CompletedProcess:
   proc = subprocess.run([cmdline],
                         shell=True,
                         encoding="utf8",
-                        capture_output=True)
+                        capture_output=capture_output)
   if (check and proc.returncode != 0) or (quiet == "no"):
     print("$ " + cmdline)
     print(proc.stdout or "", file=sys.stdout, end='', flush=True)
@@ -842,8 +842,13 @@
 good_run="yes"
 export("TEST_RUNTIME", runtime)
 if dev_mode == "yes":
+    gdb_mode = "--gdb" in run_args
+    gdbserver_mode = "--gdbserver" in run_args
+    capture_output = not (gdb_mode or gdbserver_mode)
+    if gdb_mode:
+        export("TERM", os.environ.get("TERM"))
     verbose(f"{test_dir}: running...")
-    run_exit=run(f"./{run_cmd} {joined_run_args} {joined_args}", check=False).returncode
+    run_exit=run(f"./{run_cmd} {joined_run_args} {joined_args}", check=False, capture_output=capture_output).returncode
 
     if run_exit == 0:
         if run_checker == "yes":