Switch --gdb-dex2oat to GDB

This also fixes the issue with the "--" part erroneously being added
to GDB_DEX2OAT_ARGS twice for the secondary compilation - via
renaming GDB_DEX2OAT_ARGS -> GDB_DEX2OAT_EXTRA_ARGS and introducing
GDB_DEX2OAT_ARGS local variable with value constructed independently
of previous write_dex2oat_cmdlines invocations.

To avoid ambiguity on whether the extra arguments are for gdb or
for dex2oat, move them to an earlier position on the command line.
Also, fix handling of dash symbols in the extra arguments.

Test: art/test.py -v -j56 --host --64 --optimizing --ndebug \
      --ndebuggable --gdb-dex2oat -t 071-dexfile-map-clean \
      --run-test
Test: art/test.py -v -j56 --host --64 --optimizing \
      --ndebuggable --gdb-dex2oat -t 071-dexfile-map-clean \
      --run-test
Test: art/test.py -v -j56 --host --64 --optimizing \
      --ndebuggable --gdb-dex2oat --gdb-dex2oat-args='--version' \
      -t 071-dexfile-map-clean --run-test
Test: art/test.py -v -j56 --host --64 --optimizing \
      --ndebuggable --gdb-dex2oat --gdb-dex2oat-args='--tui' \
      -t 071-dexfile-map-clean --run-test
Test: art/test.py -v -j56 --host --64 --optimizing \
      --ndebuggable --gdb-dex2oat --gdb-dex2oat-args='-ex=run;--tui' \
      -t 071-dexfile-map-clean --run-test
Change-Id: I598be3c3009947fea2fd6f45fa3a94a54a3c3d9e
diff --git a/test/default_run.py b/test/default_run.py
index e3e5d21..6e334d0 100755
--- a/test/default_run.py
+++ b/test/default_run.py
@@ -65,7 +65,7 @@
   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")
+  argp.add_argument("--gdb-dex2oat-args")
   argp.add_argument("--gdbserver", action="store_true")
   argp.add_argument("--gdbserver-bin")
   argp.add_argument("--gdbserver-port", default=":5039")
@@ -265,8 +265,7 @@
   ANDROID_FLAGS = ""
   GDB = ""
   GDB_ARGS = ""
-  GDB_DEX2OAT = ""
-  GDB_DEX2OAT_ARGS = ""
+  GDB_DEX2OAT_EXTRA_ARGS = ""
   GDBSERVER_DEVICE = "gdbserver"
   GDBSERVER_HOST = "gdbserver"
   HAVE_IMAGE = args.image
@@ -428,8 +427,8 @@
   for arg in args.gdb_arg:
     GDB_ARGS += f" {arg}"
   if args.gdb_dex2oat_args:
-    for arg in arg.split(";"):
-      GDB_DEX2OAT_ARGS += f"{arg} "
+    for arg in args.gdb_dex2oat_args.split(";"):
+      GDB_DEX2OAT_EXTRA_ARGS += f'"{arg}" '
   if args.zygote:
     ZYGOTE = "-Xzygote"
     print("Spawning from zygote")
@@ -788,44 +787,6 @@
       profman_cmdline = f"{profman_cmdline} --generate-test-profile={DEX_LOCATION}/{TEST_NAME}.prof \
           --generate-test-profile-seed=0"
 
-  def get_prebuilt_lldb_path():
-    CLANG_BASE = "prebuilts/clang/host"
-    CLANG_VERSION = check_output(
-        f"{ANDROID_BUILD_TOP}/build/soong/scripts/get_clang_version.py"
-    ).strip()
-    uname = check_output("uname -s", shell=True).strip()
-    if uname == "Darwin":
-      PREBUILT_NAME = "darwin-x86"
-    elif uname == "Linux":
-      PREBUILT_NAME = "linux-x86"
-    else:
-      print(
-          "Unknown host $(uname -s). Unsupported for debugging dex2oat with LLDB.",
-          file=sys.stderr)
-      return
-    CLANG_PREBUILT_HOST_PATH = f"{ANDROID_BUILD_TOP}/{CLANG_BASE}/{PREBUILT_NAME}/{CLANG_VERSION}"
-    # If the clang prebuilt directory exists and the reported clang version
-    # string does not, then it is likely that the clang version reported by the
-    # get_clang_version.py script does not match the expected directory name.
-    if isdir(f"{ANDROID_BUILD_TOP}/{CLANG_BASE}/{PREBUILT_NAME}"):
-      assert isdir(CLANG_PREBUILT_HOST_PATH), (
-          "The prebuilt clang directory exists, but the specific "
-          "clang\nversion reported by get_clang_version.py does not exist in "
-          "that path.\nPlease make sure that the reported clang version "
-          "resides in the\nprebuilt clang directory!")
-
-    # The lldb-server binary is a dependency of lldb.
-    os.environ[
-        "LLDB_DEBUGSERVER_PATH"] = f"{CLANG_PREBUILT_HOST_PATH}/runtimes_ndk_cxx/x86_64/lldb-server"
-
-    # Set the current terminfo directory to TERMINFO so that LLDB can read the
-    # termcap database.
-    terminfo = re.search("/.*/terminfo/", check_output("infocmp"))
-    if terminfo:
-      os.environ["TERMINFO"] = terminfo[0]
-
-    return f"{CLANG_PREBUILT_HOST_PATH}/bin/lldb.sh"
-
   def write_dex2oat_cmdlines(name: str):
     nonlocal dex2oat_cmdline, dm_cmdline, vdex_cmdline
 
@@ -854,18 +815,17 @@
     if enable_app_image:
       app_image = f"--app-image-file={DEX_LOCATION}/oat/{ISA}/{name}.art --resolve-startup-const-strings=true"
 
-    nonlocal GDB_DEX2OAT, GDB_DEX2OAT_ARGS
-    if USE_GDB_DEX2OAT:
-      prebuilt_lldb_path = get_prebuilt_lldb_path()
-      GDB_DEX2OAT = f"{prebuilt_lldb_path} -f"
-      GDB_DEX2OAT_ARGS += " -- "
-
     dex2oat_binary = DEX2OAT_DEBUG_BINARY
     if TEST_IS_NDEBUG:
       dex2oat_binary = DEX2OAT_NDEBUG_BINARY
-    dex2oat_cmdline = f"{INVOKE_WITH} {GDB_DEX2OAT} \
-                        {ANDROID_ART_BIN_DIR}/{dex2oat_binary} \
-                        {GDB_DEX2OAT_ARGS} \
+
+    dex2oat_cmdline = f"{INVOKE_WITH} "
+
+    if USE_GDB_DEX2OAT:
+      nonlocal GDB_DEX2OAT_EXTRA_ARGS
+      dex2oat_cmdline += f"gdb {GDB_DEX2OAT_EXTRA_ARGS} --args "
+
+    dex2oat_cmdline += f"'{ANDROID_ART_BIN_DIR}/{dex2oat_binary}' \
                         {COMPILE_FLAGS} \
                         --boot-image={BOOT_IMAGE} \
                         --dex-file={DEX_LOCATION}/{name}.jar \
diff --git a/test/run-test b/test/run-test
index f2be1d8..e699c7b 100755
--- a/test/run-test
+++ b/test/run-test
@@ -353,7 +353,7 @@
     elif arg == "--gdb-dex2oat-args":
       shift()
       gdb_dex2oat_args = arg
-      run_args += ['--gdb-dex2oat-args "{gdb_dex2oat_args}"']
+      run_args += [f'--gdb-dex2oat-args="{gdb_dex2oat_args}"']
       shift()
     elif arg == "--debug":
       run_args += ["--debug"]
@@ -801,14 +801,14 @@
         "    --with-agent <agent>  Run the test with the given agent loaded with -agentpath:\n"
         "    --debuggable          Whether to compile Java code for a debugger.\n"
         "    --gdb                 Run under gdb; incompatible with some tests.\n"
-        "    --gdb-dex2oat         Run dex2oat under the prebuilt lldb.\n"
+        "    --gdb-dex2oat         Run dex2oat under the prebuilt gdb.\n"
         "    --gdbserver           Start gdbserver (defaults to port :5039).\n"
         "    --gdbserver-port <port>\n"
         "                          Start gdbserver with the given COMM (see man gdbserver).\n"
         "    --gdbserver-bin <binary>\n"
         "                          Use the given binary as gdbserver.\n"
         "    --gdb-arg             Pass an option to gdb or gdbserver.\n"
-        "    --gdb-dex2oat-args    Pass options separated by ';' to lldb for dex2oat.\n"
+        "    --gdb-dex2oat-args    Pass options separated by ';' to gdb for dex2oat.\n"
         "    --simpleperf          Wraps the dalvikvm invocation in 'simpleperf record ...\n"
         "                          ... simpleperf report' and dumps stats to stdout.\n"
         "    --temp-path [path]    Location where to execute the tests.\n"
diff --git a/test/testrunner/testrunner.py b/test/testrunner/testrunner.py
index e730f5b..b0b6cc0 100755
--- a/test/testrunner/testrunner.py
+++ b/test/testrunner/testrunner.py
@@ -431,7 +431,7 @@
   if gdb_dex2oat:
     options_all += ' --gdb-dex2oat'
     if gdb_dex2oat_args:
-      options_all += ' --gdb-dex2oat-args ' + gdb_dex2oat_args
+      options_all += f' --gdb-dex2oat-args "{gdb_dex2oat_args}"'
 
   options_all += ' ' + ' '.join(run_test_option)