Make it easier to run libjdwp.so as debugger for run-test

Adds a --debug-agent argument to run test that allows one to specify
the path for a debugger JVMTI agent. Also adds a --debug-wrap-agent
option that makes the agent specified by --debug-agent use
libwrapagentproperties to ensure compatibility.

Currently this only supports host use.

Also refactored JVMTI plugin loading slightly since it was getting
to be required by a large number of different options.

Test: ./test/run-test \
         --debug-agent $ANDROID_JAVA_HOME/jre/lib/amd64/libjdwp.so \
         --debug-wrap-agent \
         --64 \
         --host \
         001-HelloWorld

Bug: 67752243

Change-Id: Ie0b38999826cc09651a6d100be6acccf79e706dc
diff --git a/test/etc/run-test-jar b/test/etc/run-test-jar
index d37e6bc..f529d67 100755
--- a/test/etc/run-test-jar
+++ b/test/etc/run-test-jar
@@ -16,6 +16,8 @@
 COMPILE_FLAGS=""
 DALVIKVM="dalvikvm32"
 DEBUGGER="n"
+DEBUGGER_AGENT=""
+WRAP_DEBUGGER_AGENT="n"
 DEV_MODE="n"
 DEX2OAT=""
 EXPERIMENTAL=""
@@ -51,6 +53,7 @@
 fi
 USE_GDB="n"
 USE_JVM="n"
+USE_JVMTI="n"
 VERIFY="y" # y=yes,n=no,s=softfail
 ZYGOTE=""
 DEX_VERIFY=""
@@ -108,6 +111,7 @@
         DEX2OAT_TIMEOUT="$1"
         shift
     elif [ "x$1" = "x--jvmti" ]; then
+        USE_JVMTI="y"
         IS_JVMTI_TEST="y"
         shift
     elif [ "x$1" = "x-O" ]; then
@@ -180,19 +184,23 @@
         shift
     elif [ "x$1" = "x--jvmti-redefine-stress" ]; then
         # APP_IMAGE doesn't really work with jvmti redefine stress
+        USE_JVMTI="y"
         APP_IMAGE="n"
         JVMTI_STRESS="y"
         JVMTI_REDEFINE_STRESS="y"
         shift
     elif [ "x$1" = "x--jvmti-step-stress" ]; then
+        USE_JVMTI="y"
         JVMTI_STRESS="y"
         JVMTI_STEP_STRESS="y"
         shift
     elif [ "x$1" = "x--jvmti-field-stress" ]; then
+        USE_JVMTI="y"
         JVMTI_STRESS="y"
         JVMTI_FIELD_STRESS="y"
         shift
     elif [ "x$1" = "x--jvmti-trace-stress" ]; then
+        USE_JVMTI="y"
         JVMTI_STRESS="y"
         JVMTI_TRACE_STRESS="y"
         shift
@@ -220,6 +228,16 @@
         FLAGS="${FLAGS} -Xcompiler-option --dump-cfg-append"
         COMPILE_FLAGS="${COMPILE_FLAGS} --dump-cfg-append"
         shift
+    elif [ "x$1" = "x--debug-wrap-agent" ]; then
+        WRAP_DEBUGGER_AGENT="y"
+        shift
+    elif [ "x$1" = "x--debug-agent" ]; then
+        shift
+        DEBUGGER="agent"
+        USE_JVMTI="y"
+        DEBUGGER_AGENT="$1"
+        TIME_OUT="n"
+        shift
     elif [ "x$1" = "x--debug" ]; then
         DEBUGGER="y"
         TIME_OUT="n"
@@ -405,15 +423,43 @@
   fi
   msg "    jdb -attach localhost:$PORT"
   DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=$PORT,server=y,suspend=y"
+elif [ "$DEBUGGER" = "agent" ]; then
+  PORT=12345
+  # TODO Support ddms connection and support target.
+  if [ "$HOST" = "n" ]; then
+    echo "--debug-agent not supported yet for target!"
+    exit 1
+  fi
+  AGENTPATH=${DEBUGGER_AGENT}
+  if [ "$WRAP_DEBUGGER_AGENT" = "y" ]; then
+    WRAPPROPS="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}/libwrapagentpropertiesd.so"
+    if [ "$TEST_IS_NDEBUG" = "y" ]; then
+      WRAPPROPS="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}/libwrapagentproperties.so"
+    fi
+    AGENTPATH="${WRAPPROPS}=${ANDROID_BUILD_TOP}/art/tools/libjdwp-compat.props,${AGENTPATH}"
+  fi
+  msg "Connect to localhost:$PORT"
+  DEBUGGER_OPTS="-agentpath:${AGENTPATH}=transport=dt_socket,address=$PORT,server=y,suspend=y"
+fi
+
+if [ "$USE_JVMTI" = "y" ]; then
+  if [ "$USE_JVM" = "n" ]; then
+    plugin=libopenjdkjvmtid.so
+    if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
+      plugin=libopenjdkjvmti.so
+    fi
+    FLAGS="${FLAGS} -Xplugin:${plugin}"
+    FLAGS="${FLAGS} -Xcompiler-option --debuggable"
+    # Always make the compilation be debuggable.
+    COMPILE_FLAGS="${COMPILE_FLAGS} --debuggable"
+  fi
 fi
 
 if [ "$IS_JVMTI_TEST" = "y" ]; then
-  plugin=libopenjdkjvmtid.so
   agent=libtiagentd.so
   lib=tiagentd
   if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
     agent=libtiagent.so
-    plugin=libopenjdkjvmti.so
     lib=tiagent
   fi
 
@@ -422,19 +468,13 @@
     FLAGS="${FLAGS} -agentpath:${ANDROID_HOST_OUT}/nativetest64/${agent}=${TEST_NAME},jvm"
   else
     FLAGS="${FLAGS} -agentpath:${agent}=${TEST_NAME},art"
-    FLAGS="${FLAGS} -Xplugin:${plugin}"
-    FLAGS="${FLAGS} -Xcompiler-option --debuggable"
-    # Always make the compilation be debuggable.
-    COMPILE_FLAGS="${COMPILE_FLAGS} --debuggable"
   fi
 fi
 
 if [[ "$JVMTI_STRESS" = "y" ]]; then
-  plugin=libopenjdkjvmtid.so
   agent=libtistressd.so
   if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
     agent=libtistress.so
-    plugin=libopenjdkjvmti.so
   fi
 
   # Just give it a default start so we can always add ',' to it.
@@ -459,12 +499,6 @@
     FLAGS="${FLAGS} -agentpath:${ANDROID_HOST_OUT}/nativetest64/${agent}=${agent_args}"
   else
     FLAGS="${FLAGS} -agentpath:${agent}=${agent_args}"
-    if [ "$IS_JVMTI_TEST" = "n" ]; then
-      FLAGS="${FLAGS} -Xplugin:${plugin}"
-      FLAGS="${FLAGS} -Xcompiler-option --debuggable"
-      # Always make the compilation be debuggable.
-      COMPILE_FLAGS="${COMPILE_FLAGS} --debuggable"
-    fi
   fi
 fi
 
diff --git a/test/run-test b/test/run-test
index d04cd05..09a70e5 100755
--- a/test/run-test
+++ b/test/run-test
@@ -288,6 +288,14 @@
     elif [ "x$1" = "x--debug" ]; then
         run_args="${run_args} --debug"
         shift
+    elif [ "x$1" = "x--debug-wrap-agent" ]; then
+        run_args="${run_args} --debug-wrap-agent"
+        shift
+    elif [ "x$1" = "x--debug-agent" ]; then
+        shift
+        option="$1"
+        run_args="${run_args} --debug-agent $1"
+        shift
     elif [ "x$1" = "x--gdb" ]; then
         run_args="${run_args} --gdb"
         dev_mode="yes"
@@ -647,7 +655,12 @@
         echo "    -Xcompiler-option     Pass an option to the compiler."
         echo "    --build-option        Pass an option to the build script."
         echo "    --runtime-option      Pass an option to the runtime."
-        echo "    --debug               Wait for a debugger to attach."
+        echo "    --debug               Wait for the default debugger to attach."
+        echo "    --debug-agent <agent-path>"
+        echo "                          Wait for the given debugger agent to attach. Currently"
+        echo "                          only supported on host."
+        echo "    --debug-wrap-agent    use libwrapagentproperties and tools/libjdwp-compat.props"
+        echo "                          to load the debugger agent specified by --debug-agent."
         echo "    --debuggable          Whether to compile Java code for a debugger."
         echo "    --gdb                 Run under gdb; incompatible with some tests."
         echo "    --gdb-arg             Pass an option to gdb."