Merge host-run-test-jar and push-and-run-test-jar.

- Too many code duplication between the two files.
- Also fix --zygote and --gdb invocations.

Change-Id: I1f99320e63a7a0f19ad1ad2b12693901b8c6bb43
diff --git a/test/etc/host-run-test-jar b/test/etc/host-run-test-jar
deleted file mode 100755
index 6ee1dcd..0000000
--- a/test/etc/host-run-test-jar
+++ /dev/null
@@ -1,240 +0,0 @@
-#!/bin/bash
-#
-# Run the code in test.jar using the host-mode virtual machine. The jar should
-# contain a top-level class named Main to run.
-
-msg() {
-    if [ "$QUIET" = "n" ]; then
-        echo "$@"
-    fi
-}
-
-DEBUGGER="n"
-PREBUILD="n"
-GDB="n"
-ISA="x86"
-INTERPRETER="n"
-VERIFY="y"
-RELOCATE="y"
-OPTIMIZE="y"
-INVOKE_WITH=""
-DEV_MODE="n"
-QUIET="n"
-FLAGS=""
-COMPILER_FLAGS=""
-BUILD_BOOT_OPT=""
-PATCHOAT=""
-DEX2OAT=""
-FALSE_BIN="/bin/false"
-HAVE_IMAGE="y"
-TIME_OUT="y"
-TIME_OUT_VALUE=5m
-exe="${ANDROID_HOST_OUT}/bin/dalvikvm32"
-main="Main"
-
-while true; do
-    if [ "x$1" = "x--quiet" ]; then
-        QUIET="y"
-        shift
-    elif [ "x$1" = "x--prebuild" ]; then
-        PREBUILD="y"
-        shift
-    elif [ "x$1" = "x--no-prebuild" ]; then
-        PREBUILD="n"
-        shift
-    elif [ "x$1" = "x--no-dex2oat" ]; then
-        DEX2OAT="-Xcompiler:${FALSE_BIN}"
-        shift
-    elif [ "x$1" = "x--no-patchoat" ]; then
-        PATCHOAT="-Xpatchoat:${FALSE_BIN}"
-        shift
-    elif [ "x$1" = "x--lib" ]; then
-        shift
-        if [ "x$1" = "x" ]; then
-            echo "$0 missing argument to --lib" 1>&2
-            exit 1
-        fi
-        LIB="$1"
-        if [ `uname` = "Darwin" ]; then
-            LIB=${LIB/%so/dylib}
-        fi
-        shift
-    elif [ "x$1" = "x--no-image" ]; then
-        HAVE_IMAGE="n"
-        shift
-    elif [ "x$1" = "x--boot" ]; then
-        shift
-        option="$1"
-        BOOT_OPT="$option"
-        BUILD_BOOT_OPT="--boot-image=${option#-Ximage:}"
-        shift
-    elif [ "x$1" = "x--debug" ]; then
-        DEBUGGER="y"
-        TIME_OUT="n"
-        shift
-    elif [ "x$1" = "x--gdb" ]; then
-        GDB="y"
-        DEV_MODE="y"
-        TIME_OUT="n"
-        shift
-    elif [ "x$1" = "x--invoke-with" ]; then
-        shift
-        if [ "x$1" = "x" ]; then
-            echo "$0 missing argument to --invoke-with" 1>&2
-            exit 1
-        fi
-        if [ "x$INVOKE_WITH" = "x" ]; then
-            INVOKE_WITH="$1"
-        else
-            INVOKE_WITH="$INVOKE_WITH $1"
-        fi
-        shift
-    elif [ "x$1" = "x--dev" ]; then
-        DEV_MODE="y"
-        shift
-    elif [ "x$1" = "x--interpreter" ]; then
-        INTERPRETER="y"
-        shift
-    elif [ "x$1" = "x--64" ]; then
-        ISA="x86_64"
-        exe="${ANDROID_HOST_OUT}/bin/dalvikvm64"
-        shift
-    elif [ "x$1" = "x--no-verify" ]; then
-        VERIFY="n"
-        shift
-    elif [ "x$1" = "x--no-optimize" ]; then
-        OPTIMIZE="n"
-        shift
-    elif [ "x$1" = "x--no-relocate" ]; then
-        RELOCATE="n"
-        shift
-    elif [ "x$1" = "x--relocate" ]; then
-        RELOCATE="y"
-        shift
-    elif [ "x$1" = "x-Xcompiler-option" ]; then
-        shift
-        option="$1"
-        FLAGS="${FLAGS} -Xcompiler-option $option"
-        COMPILER_FLAGS="${COMPILER_FLAGS} $option"
-        shift
-    elif [ "x$1" = "x--runtime-option" ]; then
-        shift
-        option="$1"
-        FLAGS="${FLAGS} $option"
-        shift
-    elif [ "x$1" = "x--" ]; then
-        shift
-        break
-    elif expr "x$1" : "x--" >/dev/null 2>&1; then
-        echo "unknown $0 option: $1" 1>&2
-        exit 1
-    else
-        break
-    fi
-done
-
-if [ "x$1" = "x" ] ; then
-  main="Main"
-else
-  main="$1"
-fi
-
-msg "------------------------------"
-
-export ANDROID_PRINTF_LOG=brief
-if [ "$DEV_MODE" = "y" ]; then
-    export ANDROID_LOG_TAGS='*:d'
-else
-    export ANDROID_LOG_TAGS='*:s'
-fi
-export ANDROID_DATA="$DEX_LOCATION"
-export ANDROID_ROOT="${ANDROID_HOST_OUT}"
-export LD_LIBRARY_PATH="${ANDROID_ROOT}/lib"
-export DYLD_LIBRARY_PATH="${ANDROID_ROOT}/lib"
-
-if [ "$DEBUGGER" = "y" ]; then
-    PORT=8000
-    msg "Waiting for jdb to connect:"
-    msg "    jdb -attach localhost:$PORT"
-    DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=$PORT,server=y,suspend=y"
-fi
-
-if [ "$GDB" = "y" ]; then
-    if [ `uname` = "Darwin" ]; then
-        gdb=lldb
-        gdbargs="-- $exe"
-        exe=
-    else
-        gdb=gdb
-        gdbargs="--args $exe"
-        # Enable for Emacs "M-x gdb" support. TODO: allow extra gdb arguments on command line.
-        # gdbargs="--annotate=3 $gdbargs"
-    fi
-fi
-
-if [ "$INTERPRETER" = "y" ]; then
-    INT_OPTS="-Xint"
-    COMPILER_FLAGS="${COMPILER_FLAGS} --compiler-filter=interpret-only"
-fi
-
-if [ "$HAVE_IMAGE" = "n" ]; then
-    # Set image to a place were there isn't one.
-    BOOT_OPT="-Ximage:/system/non-existant/core.art"
-fi
-
-if [ "$RELOCATE" = "y" ]; then
-  FLAGS="${FLAGS} -Xrelocate"
-  COMPILER_FLAGS="${COMPILER_FLAGS} --runtime-arg -Xnorelocate --include-patch-information"
-  # Run test sets a fairly draconian ulimit that we will likely blow right over
-  # since we are relocating. Get the total size of the /system/framework directory
-  # in 512 byte blocks and set it as the ulimit. This should be more than enough
-  # room.
-  if [ ! `uname` = "Darwin" ]; then  # TODO: Darwin doesn't support "du -B..."
-    ulimit -S $(du -c -B512 ${ANDROID_ROOT}/framework | tail -1 | cut -f1) || exit 1
-  fi
-else
-  FLAGS="${FLAGS} -Xnorelocate"
-  COMPILER_FLAGS="${COMPILER_FLAGS} --runtime-arg -Xnorelocate --no-include-patch-information"
-fi
-
-mkdir_cmd="mkdir -p ${DEX_LOCATION}/dalvik-cache/$ISA"
-if [ "$PREBUILD" = "y" ]; then
-  prebuild_cmd="${ANDROID_HOST_OUT}/bin/dex2oatd $COMPILER_FLAGS --instruction-set=$ISA $BUILD_BOOT_OPT --dex-file=$DEX_LOCATION/$TEST_NAME.jar --oat-file=$DEX_LOCATION/dalvik-cache/$ISA/$(echo $DEX_LOCATION/$TEST_NAME.jar/classes.dex | cut -d/ -f 2- | sed "s:/:@:g")"
-else
-  prebuild_cmd="true"
-fi
-
-JNI_OPTS="-Xjnigreflimit:512 -Xcheck:jni"
-cmdline="$INVOKE_WITH $gdb $exe $gdbargs -XXlib:$LIB $PATCHOAT $DEX2OAT $JNI_OPTS $FLAGS $INT_OPTS $DEBUGGER_OPTS $BOOT_OPT -cp $DEX_LOCATION/$TEST_NAME.jar $main"
-if [ "$TIME_OUT" = "y" ]; then
-  # Add timeout command if time out is desired.
-  cmdline="timeout $TIME_OUT_VALUE $cmdline"
-fi
-if [ "$DEV_MODE" = "y" ]; then
-  if [ "$PREBUILD" = "y" ]; then
-    echo "$mkdir_cmd && $prebuild_cmd && $cmdline"
-  elif [ "$RELOCATE" = "y" ]; then
-    echo "$mkdir_cmd && $cmdline"
-  else
-    echo $cmdline
-  fi
-fi
-
-cd $ANDROID_BUILD_TOP
-
-$mkdir_cmd || exit 1
-$prebuild_cmd || exit 2
-
-if [ "$GDB" = "y" ]; then
-  # When running under gdb, we cannot do piping and grepping...
-  LD_PRELOAD=libsigchain.so $cmdline "$@"
-else
-  # If we are execing /bin/false we might not be on the same ISA as libsigchain.so
-  # ld.so will helpfully warn us of this. Unfortunately this messes up our error
-  # checking so we will just filter out the error with a grep.
-  LD_PRELOAD=libsigchain.so $cmdline "$@" 2>&1 | grep -v -E "^ERROR: ld\.so: object '.+\.so' from LD_PRELOAD cannot be preloaded.*: ignored\.$"
-  # Add extra detail if time out is enabled.
-  if [ ${PIPESTATUS[0]} = 124 ] && [ "$TIME_OUT" = "y" ]; then
-    echo -e "\e[91mTEST TIMED OUT!\e[0m" >&2
-  fi
-fi
diff --git a/test/etc/push-and-run-test-jar b/test/etc/push-and-run-test-jar
deleted file mode 100755
index d3ad71f..0000000
--- a/test/etc/push-and-run-test-jar
+++ /dev/null
@@ -1,262 +0,0 @@
-#!/bin/sh
-#
-# Run the code in test.jar on the device. The jar should contain a top-level
-# class named Main to run.
-
-msg() {
-    if [ "$QUIET" = "n" ]; then
-        echo "$@"
-    fi
-}
-
-ARCHITECTURES_32="(arm|x86|mips|none)"
-ARCHITECTURES_64="(arm64|x86_64|none)"
-ARCHITECTURES_PATTERN="${ARCHITECTURES_32}"
-RELOCATE="y"
-GDB="n"
-DEBUGGER="n"
-INTERPRETER="n"
-VERIFY="y"
-OPTIMIZE="y"
-ZYGOTE=""
-QUIET="n"
-DEV_MODE="n"
-INVOKE_WITH=""
-FLAGS=""
-TARGET_SUFFIX="32"
-GDB_TARGET_SUFFIX=""
-COMPILE_FLAGS=""
-FALSE_BIN="/system/bin/false"
-PATCHOAT=""
-DEX2OAT=""
-HAVE_IMAGE="y"
-PREBUILD="y"
-
-while true; do
-    if [ "x$1" = "x--quiet" ]; then
-        QUIET="y"
-        shift
-    elif [ "x$1" = "x--lib" ]; then
-        shift
-        if [ "x$1" = "x" ]; then
-            echo "$0 missing argument to --lib" 1>&2
-            exit 1
-        fi
-        LIB="$1"
-        shift
-    elif [ "x$1" = "x-Xcompiler-option" ]; then
-        shift
-        option="$1"
-        FLAGS="${FLAGS} -Xcompiler-option $option"
-        COMPILE_FLAGS="${COMPILE_FLAGS} $option"
-        shift
-    elif [ "x$1" = "x--runtime-option" ]; then
-        shift
-        option="$1"
-        FLAGS="${FLAGS} $option"
-        shift
-    elif [ "x$1" = "x--boot" ]; then
-        shift
-        BOOT_OPT="$1"
-        BUILD_BOOT_OPT="--boot-image=${1#-Ximage:}"
-        shift
-    elif [ "x$1" = "x--no-dex2oat" ]; then
-        DEX2OAT="-Xcompiler:${FALSE_BIN}"
-        shift
-    elif [ "x$1" = "x--no-patchoat" ]; then
-        PATCHOAT="-Xpatchoat:${FALSE_BIN}"
-        shift
-    elif [ "x$1" = "x--relocate" ]; then
-        RELOCATE="y"
-        shift
-    elif [ "x$1" = "x--no-relocate" ]; then
-        RELOCATE="n"
-        shift
-    elif [ "x$1" = "x--prebuild" ]; then
-        PREBUILD="y"
-        shift
-    elif [ "x$1" = "x--no-prebuild" ]; then
-        PREBUILD="n"
-        shift
-    elif [ "x$1" = "x--no-image" ]; then
-        HAVE_IMAGE="n"
-        shift
-    elif [ "x$1" = "x--debug" ]; then
-        DEBUGGER="y"
-        shift
-    elif [ "x$1" = "x--gdb" ]; then
-        GDB="y"
-        DEV_MODE="y"
-        shift
-    elif [ "x$1" = "x--zygote" ]; then
-        ZYGOTE="--zygote"
-        msg "Spawning from zygote"
-        shift
-    elif [ "x$1" = "x--dev" ]; then
-        DEV_MODE="y"
-        shift
-    elif [ "x$1" = "x--interpreter" ]; then
-        INTERPRETER="y"
-        shift
-    elif [ "x$1" = "x--invoke-with" ]; then
-        shift
-        if [ "x$1" = "x" ]; then
-            echo "$0 missing argument to --invoke-with" 1>&2
-            exit 1
-        fi
-        if [ "x$INVOKE_WITH" = "x" ]; then
-            INVOKE_WITH="$1"
-        else
-            INVOKE_WITH="$INVOKE_WITH $1"
-        fi
-        shift
-    elif [ "x$1" = "x--no-verify" ]; then
-        VERIFY="n"
-        shift
-    elif [ "x$1" = "x--no-optimize" ]; then
-        OPTIMIZE="n"
-        shift
-    elif [ "x$1" = "x--" ]; then
-        shift
-        break
-    elif [ "x$1" = "x--64" ]; then
-        TARGET_SUFFIX="64"
-        GDB_TARGET_SUFFIX="64"
-        ARCHITECTURES_PATTERN="${ARCHITECTURES_64}"
-        shift
-    elif expr "x$1" : "x--" >/dev/null 2>&1; then
-        echo "unknown $0 option: $1" 1>&2
-        exit 1
-    else
-        break
-    fi
-done
-
-if [ "$ZYGOTE" = "" ]; then
-    if [ "$OPTIMIZE" = "y" ]; then
-        if [ "$VERIFY" = "y" ]; then
-            DEX_OPTIMIZE="-Xdexopt:verified"
-        else
-            DEX_OPTIMIZE="-Xdexopt:all"
-        fi
-        msg "Performing optimizations"
-    else
-        DEX_OPTIMIZE="-Xdexopt:none"
-        msg "Skipping optimizations"
-    fi
-
-    if [ "$VERIFY" = "y" ]; then
-        DEX_VERIFY=""
-        msg "Performing verification"
-    else
-        DEX_VERIFY="-Xverify:none"
-        msg "Skipping verification"
-    fi
-fi
-
-msg "------------------------------"
-
-if [ "$HAVE_IMAGE" = "n" ]; then
-    BOOT_OPT="-Ximage:/system/non-existant/core.art"
-fi
-
-ARCH=$(adb shell ls -F /data/dalvik-cache | grep -Ewo "${ARCHITECTURES_PATTERN}")
-if [ x"$ARCH" = "x" ]; then
-  echo "Unable to determine architecture"
-  exit 1
-fi
-
-if [ "$QUIET" = "n" ]; then
-  adb shell rm -r $DEX_LOCATION
-  adb shell mkdir -p $DEX_LOCATION
-  adb push $TEST_NAME.jar $DEX_LOCATION
-  adb push $TEST_NAME-ex.jar $DEX_LOCATION
-else
-  adb shell rm -r $DEX_LOCATION >/dev/null 2>&1
-  adb shell mkdir -p $DEX_LOCATION >/dev/null 2>&1
-  adb push $TEST_NAME.jar $DEX_LOCATION >/dev/null 2>&1
-  adb push $TEST_NAME-ex.jar $DEX_LOCATION >/dev/null 2>&1
-fi
-
-if [ "$DEBUGGER" = "y" ]; then
-  # Use this instead for ddms and connect by running 'ddms':
-  # DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_android_adb,server=y,suspend=y"
-  # TODO: add a separate --ddms option?
-
-  PORT=12345
-  msg "Waiting for jdb to connect:"
-  msg "    adb forward tcp:$PORT tcp:$PORT"
-  msg "    jdb -attach localhost:$PORT"
-  DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=$PORT,server=y,suspend=y"
-fi
-
-if [ "$GDB" = "y" ]; then
-    gdb="gdbserver$GDB_TARGET_SUFFIX :5039"
-    gdbargs="$exe"
-fi
-
-if [ "$INTERPRETER" = "y" ]; then
-    INT_OPTS="-Xint"
-    COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=interpret-only"
-fi
-
-JNI_OPTS="-Xjnigreflimit:512 -Xcheck:jni"
-
-if [ "$RELOCATE" = "y" ]; then
-    RELOCATE_OPT="-Xrelocate"
-    BUILD_RELOCATE_OPT="--runtime-arg -Xnorelocate"
-    COMPILE_FLAGS="${COMPILE_FLAGS} --include-patch-information"
-    FLAGS="${FLAGS} -Xcompiler-option --include-patch-information"
-else
-    RELOCATE_OPT="-Xnorelocate"
-    BUILD_RELOCATE_OPT="--runtime-arg -Xnorelocate"
-fi
-
-# Create a script with the command. The command can get longer than the longest
-# allowed adb command and there is no way to get the exit status from a adb shell
-# command.
-cmdline="cd $DEX_LOCATION && \
-         export ANDROID_DATA=$DEX_LOCATION && \
-         export DEX_LOCATION=$DEX_LOCATION && "
-if [ "$PREBUILD" = "y" ]; then
-  cmdline+="mkdir -p $DEX_LOCATION/dalvik-cache/$ARCH/ && \
-            $INVOKE_WITH /system/bin/dex2oatd \
-                $COMPILE_FLAGS \
-                $BUILD_BOOT_OPT \
-                $BUILD_RELOCATE_OPT  \
-                --runtime-arg -classpath --runtime-arg $DEX_LOCATION/$TEST_NAME.jar \
-                --dex-file=$DEX_LOCATION/$TEST_NAME.jar \
-                --oat-file=$DEX_LOCATION/dalvik-cache/$ARCH/$(echo $DEX_LOCATION/$TEST_NAME.jar/classes.dex | cut -d/ -f 2- | sed "s:/:@:g") \
-                --instruction-set=$ARCH && "
-fi
-
-cmdline+="$INVOKE_WITH $gdb /system/bin/dalvikvm$TARGET_SUFFIX \
-            $FLAGS \
-            $gdbargs \
-            -XXlib:$LIB \
-            $PATCHOAT \
-            $DEX2OAT \
-            $ZYGOTE \
-            $JNI_OPTS \
-            $RELOCATE_OPT \
-            $INT_OPTS \
-            $DEBUGGER_OPTS \
-            $BOOT_OPT \
-            -cp $DEX_LOCATION/$TEST_NAME.jar Main"
-
-cmdfile=$(tempfile -p "cmd-" -s "-$TEST_NAME")
-echo "$cmdline" > $cmdfile
-
-if [ "$DEV_MODE" = "y" ]; then
-  echo $cmdline
-fi
-
-if [ "$QUIET" = "n" ]; then
-  adb push $cmdfile $DEX_LOCATION/cmdline.sh
-else
-  adb push $cmdfile $DEX_LOCATION/cmdline.sh > /dev/null 2>&1
-fi
-
-adb shell sh $DEX_LOCATION/cmdline.sh
-
-rm -f $cmdfile
diff --git a/test/etc/run-test-jar b/test/etc/run-test-jar
new file mode 100755
index 0000000..59d241a
--- /dev/null
+++ b/test/etc/run-test-jar
@@ -0,0 +1,355 @@
+#!/bin/bash
+#
+# Runner for an individual run-test.
+
+msg() {
+    if [ "$QUIET" = "n" ]; then
+        echo "$@"
+    fi
+}
+
+ARCHITECTURES_32="(arm|x86|mips|none)"
+ARCHITECTURES_64="(arm64|x86_64|none)"
+ARCHITECTURES_PATTERN="${ARCHITECTURES_32}"
+COMPILE_FLAGS=""
+DALVIKVM="dalvikvm32"
+DEBUGGER="n"
+DEV_MODE="n"
+DEX2OAT=""
+GDB_SERVER="gdbserver"
+FALSE_BIN="/system/bin/false"
+FLAGS=""
+GDB=""
+HAVE_IMAGE="y"
+HOST="n"
+INTERPRETER="n"
+INVOKE_WITH=""
+ISA=x86
+OPTIMIZE="y"
+PATCHOAT=""
+PREBUILD="y"
+QUIET="n"
+RELOCATE="y"
+USE_GDB="n"
+VERIFY="y"
+ZYGOTE=""
+MAIN=""
+
+while true; do
+    if [ "x$1" = "x--quiet" ]; then
+        QUIET="y"
+        shift
+    elif [ "x$1" = "x--lib" ]; then
+        shift
+        if [ "x$1" = "x" ]; then
+            echo "$0 missing argument to --lib" 1>&2
+            exit 1
+        fi
+        LIB="$1"
+        shift
+    elif [ "x$1" = "x-Xcompiler-option" ]; then
+        shift
+        option="$1"
+        FLAGS="${FLAGS} -Xcompiler-option $option"
+        COMPILE_FLAGS="${COMPILE_FLAGS} $option"
+        shift
+    elif [ "x$1" = "x--runtime-option" ]; then
+        shift
+        option="$1"
+        FLAGS="${FLAGS} $option"
+        shift
+    elif [ "x$1" = "x--boot" ]; then
+        shift
+        DALVIKVM_BOOT_OPT="$1"
+        DEX2OAT_BOOT_OPT="--boot-image=${1#-Ximage:}"
+        shift
+    elif [ "x$1" = "x--no-dex2oat" ]; then
+        DEX2OAT="-Xcompiler:${FALSE_BIN}"
+        shift
+    elif [ "x$1" = "x--no-patchoat" ]; then
+        PATCHOAT="-Xpatchoat:${FALSE_BIN}"
+        shift
+    elif [ "x$1" = "x--relocate" ]; then
+        RELOCATE="y"
+        shift
+    elif [ "x$1" = "x--no-relocate" ]; then
+        RELOCATE="n"
+        shift
+    elif [ "x$1" = "x--prebuild" ]; then
+        PREBUILD="y"
+        shift
+    elif [ "x$1" = "x--host" ]; then
+        HOST="y"
+        shift
+    elif [ "x$1" = "x--no-prebuild" ]; then
+        PREBUILD="n"
+        shift
+    elif [ "x$1" = "x--no-image" ]; then
+        HAVE_IMAGE="n"
+        shift
+    elif [ "x$1" = "x--debug" ]; then
+        DEBUGGER="y"
+        shift
+    elif [ "x$1" = "x--gdb" ]; then
+        USE_GDB="y"
+        DEV_MODE="y"
+        shift
+    elif [ "x$1" = "x--zygote" ]; then
+        ZYGOTE="-Xzygote"
+        msg "Spawning from zygote"
+        shift
+    elif [ "x$1" = "x--dev" ]; then
+        DEV_MODE="y"
+        shift
+    elif [ "x$1" = "x--interpreter" ]; then
+        INTERPRETER="y"
+        shift
+    elif [ "x$1" = "x--invoke-with" ]; then
+        shift
+        if [ "x$1" = "x" ]; then
+            echo "$0 missing argument to --invoke-with" 1>&2
+            exit 1
+        fi
+        if [ "x$INVOKE_WITH" = "x" ]; then
+            INVOKE_WITH="$1"
+        else
+            INVOKE_WITH="$INVOKE_WITH $1"
+        fi
+        shift
+    elif [ "x$1" = "x--no-verify" ]; then
+        VERIFY="n"
+        shift
+    elif [ "x$1" = "x--no-optimize" ]; then
+        OPTIMIZE="n"
+        shift
+    elif [ "x$1" = "x--" ]; then
+        shift
+        break
+    elif [ "x$1" = "x--64" ]; then
+        ISA="x86_64"
+        GDB_SERVER="gdbserver64"
+        DALVIKVM="dalvikvm64"
+        ARCHITECTURES_PATTERN="${ARCHITECTURES_64}"
+        shift
+    elif expr "x$1" : "x--" >/dev/null 2>&1; then
+        echo "unknown $0 option: $1" 1>&2
+        exit 1
+    else
+        break
+    fi
+done
+
+if [ "x$1" = "x" ] ; then
+  MAIN="Main"
+else
+  MAIN="$1"
+fi
+
+if [ "$ZYGOTE" = "" ]; then
+    if [ "$OPTIMIZE" = "y" ]; then
+        if [ "$VERIFY" = "y" ]; then
+            DEX_OPTIMIZE="-Xdexopt:verified"
+        else
+            DEX_OPTIMIZE="-Xdexopt:all"
+        fi
+        msg "Performing optimizations"
+    else
+        DEX_OPTIMIZE="-Xdexopt:none"
+        msg "Skipping optimizations"
+    fi
+
+    if [ "$VERIFY" = "y" ]; then
+        DEX_VERIFY=""
+        msg "Performing verification"
+    else
+        DEX_VERIFY="-Xverify:none"
+        msg "Skipping verification"
+    fi
+fi
+
+msg "------------------------------"
+
+if [ "$HAVE_IMAGE" = "n" ]; then
+    BOOT_OPT="-Ximage:/system/non-existant/core.art"
+fi
+
+if [ "$DEBUGGER" = "y" ]; then
+  # Use this instead for ddms and connect by running 'ddms':
+  # DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_android_adb,server=y,suspend=y"
+  # TODO: add a separate --ddms option?
+
+  PORT=12345
+  msg "Waiting for jdb to connect:"
+  if [ "$HOST" = "n" ]; then
+    msg "    adb forward tcp:$PORT tcp:$PORT"
+  fi
+  msg "    jdb -attach localhost:$PORT"
+  DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=$PORT,server=y,suspend=y"
+fi
+
+if [ "$USE_GDB" = "y" ]; then
+  if [ "$HOST" = "n" ]; then
+    GDB="$GDB_SERVER :5039"
+    GDB_ARGS="$DALVIKVM"
+  else
+    if [ `uname` = "Darwin" ]; then
+        GDB=lldb
+        GDB_ARGS="-- $DALVIKVM"
+        DALVIKVM=
+    else
+        GDB=gdb
+        GDB_ARGS="--args $DALVIKVM"
+        # Enable for Emacs "M-x gdb" support. TODO: allow extra gdb arguments on command line.
+        # gdbargs="--annotate=3 $gdbargs"
+    fi
+  fi
+fi
+
+if [ "$INTERPRETER" = "y" ]; then
+    INT_OPTS="-Xint"
+    COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=interpret-only"
+fi
+
+JNI_OPTS="-Xjnigreflimit:512 -Xcheck:jni"
+
+if [ "$RELOCATE" = "y" ]; then
+    COMPILE_FLAGS="${COMPILE_FLAGS} --include-patch-information --runtime-arg -Xnorelocate"
+    FLAGS="${FLAGS} -Xrelocate -Xcompiler-option --include-patch-information"
+    if [ "$HOST" = "y" ]; then
+        # Run test sets a fairly draconian ulimit that we will likely blow right over
+        # since we are relocating. Get the total size of the /system/framework directory
+        # in 512 byte blocks and set it as the ulimit. This should be more than enough
+        # room.
+        if [ ! `uname` = "Darwin" ]; then  # TODO: Darwin doesn't support "du -B..."
+          ulimit -S $(du -c -B512 ${ANDROID_HOST_OUT}/framework | tail -1 | cut -f1) || exit 1
+        fi
+    fi
+else
+    FLAGS="$FLAGS -Xnorelocate"
+    COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xnorelocate"
+fi
+
+if [ "$HOST" = "n" ]; then
+  ISA=$(adb shell ls -F /data/dalvik-cache | grep -Ewo "${ARCHITECTURES_PATTERN}")
+  if [ x"$ISA" = "x" ]; then
+    echo "Unable to determine architecture"
+    exit 1
+  fi
+fi
+
+dex2oat_cmdline="true"
+mkdir_cmdline="mkdir -p ${DEX_LOCATION}/dalvik-cache/$ISA"
+
+if [ "$PREBUILD" = "y" ]; then
+  dex2oat_cmdline="$INVOKE_WITH dex2oatd \
+                      $COMPILE_FLAGS \
+                      $DEX2OAT_BOOT_OPT \
+                      --dex-file=$DEX_LOCATION/$TEST_NAME.jar \
+                      --oat-file=$DEX_LOCATION/dalvik-cache/$ISA/$(echo $DEX_LOCATION/$TEST_NAME.jar/classes.dex | cut -d/ -f 2- | sed "s:/:@:g") \
+                      --instruction-set=$ISA"
+fi
+
+dalvikvm_cmdline="$INVOKE_WITH $GDB $DALVIKVM \
+                  $GDB_ARGS \
+                  $FLAGS \
+                  -XXlib:$LIB \
+                  $PATCHOAT \
+                  $DEX2OAT \
+                  $ZYGOTE \
+                  $JNI_OPTS \
+                  $INT_OPTS \
+                  $DEBUGGER_OPTS \
+                  $DALVIKVM_BOOT_OPT \
+                  -cp $DEX_LOCATION/$TEST_NAME.jar $MAIN"
+
+
+if [ "$HOST" = "n" ]; then
+    adb root > /dev/null
+    adb wait-for-device
+    if [ "$QUIET" = "n" ]; then
+      adb shell rm -r $DEX_LOCATION
+      adb shell mkdir -p $DEX_LOCATION
+      adb push $TEST_NAME.jar $DEX_LOCATION
+      adb push $TEST_NAME-ex.jar $DEX_LOCATION
+    else
+      adb shell rm -r $DEX_LOCATION >/dev/null 2>&1
+      adb shell mkdir -p $DEX_LOCATION >/dev/null 2>&1
+      adb push $TEST_NAME.jar $DEX_LOCATION >/dev/null 2>&1
+      adb push $TEST_NAME-ex.jar $DEX_LOCATION >/dev/null 2>&1
+    fi
+
+    # Create a script with the command. The command can get longer than the longest
+    # allowed adb command and there is no way to get the exit status from a adb shell
+    # command.
+    cmdline="cd $DEX_LOCATION && \
+             export ANDROID_DATA=$DEX_LOCATION && \
+             export DEX_LOCATION=$DEX_LOCATION && \
+             $mkdir_cmdline && \
+             $dex2oat_cmdline && \
+             $dalvikvm_cmdline"
+
+    cmdfile=$(tempfile -p "cmd-" -s "-$TEST_NAME")
+    echo "$cmdline" > $cmdfile
+
+    if [ "$DEV_MODE" = "y" ]; then
+      echo $cmdline
+    fi
+
+    if [ "$QUIET" = "n" ]; then
+      adb push $cmdfile $DEX_LOCATION/cmdline.sh
+    else
+      adb push $cmdfile $DEX_LOCATION/cmdline.sh > /dev/null 2>&1
+    fi
+
+    adb shell sh $DEX_LOCATION/cmdline.sh
+
+    rm -f $cmdfile
+else
+    export ANDROID_PRINTF_LOG=brief
+    if [ "$DEV_MODE" = "y" ]; then
+        export ANDROID_LOG_TAGS='*:d'
+    else
+        export ANDROID_LOG_TAGS='*:s'
+    fi
+    export ANDROID_DATA="$DEX_LOCATION"
+    export ANDROID_ROOT="${ANDROID_HOST_OUT}"
+    export LD_LIBRARY_PATH="${ANDROID_ROOT}/lib"
+    export DYLD_LIBRARY_PATH="${ANDROID_ROOT}/lib"
+    export PATH="$PATH:${ANDROID_ROOT}/bin"
+
+    cmdline="$dalvikvm_cmdline"
+
+    if [ "$TIME_OUT" = "y" ]; then
+      # Add timeout command if time out is desired.
+      cmdline="timeout $TIME_OUT_VALUE $cmdline"
+    fi
+
+    if [ "$DEV_MODE" = "y" ]; then
+      if [ "$PREBUILD" = "y" ]; then
+        echo "$mkdir_cmdline && $dex2oat_cmdline && $cmdline"
+      elif [ "$RELOCATE" = "y" ]; then
+        echo "$mkdir_cmdline && $cmdline"
+      else
+        echo $cmdline
+      fi
+    fi
+
+    cd $ANDROID_BUILD_TOP
+
+    $mkdir_cmdline || exit 1
+    $dex2oat_cmdline || exit 2
+
+    if [ "$USE_GDB" = "y" ]; then
+      # When running under gdb, we cannot do piping and grepping...
+      LD_PRELOAD=libsigchain.so $cmdline "$@"
+    else
+      # If we are execing /bin/false we might not be on the same ISA as libsigchain.so
+      # ld.so will helpfully warn us of this. Unfortunately this messes up our error
+      # checking so we will just filter out the error with a grep.
+      LD_PRELOAD=libsigchain.so $cmdline "$@" 2>&1 | grep -v -E "^ERROR: ld\.so: object '.+\.so' from LD_PRELOAD cannot be preloaded.*: ignored\.$"
+      # Add extra detail if time out is enabled.
+      if [ ${PIPESTATUS[0]} = 124 ] && [ "$TIME_OUT" = "y" ]; then
+        echo -e "\e[91mTEST TIMED OUT!\e[0m" >&2
+      fi
+    fi
+fi
diff --git a/test/run-test b/test/run-test
index f9f45fc..3b5df0d 100755
--- a/test/run-test
+++ b/test/run-test
@@ -41,7 +41,7 @@
 
 export JAVA="java"
 export JAVAC="javac -g"
-export RUN="${progdir}/etc/push-and-run-test-jar"
+export RUN="${progdir}/etc/run-test-jar"
 export DEX_LOCATION=/data/run-test/${test_dir}
 export NEED_DEX="true"
 
@@ -89,6 +89,7 @@
     if [ "x$1" = "x--host" ]; then
         target_mode="no"
         DEX_LOCATION=$tmp_dir
+        run_args="${run_args} --host"
         shift
     elif [ "x$1" = "x--jvm" ]; then
         target_mode="no"
@@ -264,8 +265,6 @@
             echo "--prebuild with --jvm is unsupported";
             exit 1;
         fi
-    else
-        RUN="${progdir}/etc/host-run-test-jar"
     fi
 fi
 
@@ -291,12 +290,12 @@
     fi
 elif [ "$runtime" = "art" ]; then
     if [ "$target_mode" = "no" ]; then
-	# ANDROID_BUILD_TOP and ANDROID_HOST_OUT are not set in a build environment.
+        # ANDROID_BUILD_TOP and ANDROID_HOST_OUT are not set in a build environment.
         if [ -z "$ANDROID_BUILD_TOP" ]; then
-	    export ANDROID_BUILD_TOP=$oldwd
+            export ANDROID_BUILD_TOP=$oldwd
         fi
         if [ -z "$ANDROID_HOST_OUT" ]; then
-	    export ANDROID_HOST_OUT=$ANDROID_BUILD_TOP/out/host/linux-x86
+            export ANDROID_HOST_OUT=$ANDROID_BUILD_TOP/out/host/linux-x86
         fi
         run_args="${run_args} --boot -Ximage:${ANDROID_HOST_OUT}/framework/core.art"
         run_args="${run_args} --runtime-option -Djava.library.path=${ANDROID_HOST_OUT}/lib${suffix64}"