Add support for gtest options to `tools/run-gtests.sh`.

Have `tools/run-gtests.sh` accept gtest options after `--` on the
command line, and pass them verbatim to each executed gtest
binary.

Also document the use of gtest options with `tools/run-gtests.sh`
in `test/README.chroot.md`.

Test: art/tools/run-gtests.sh -- --gtest_list_tests
Test: art/tools/run-gtests.sh \
        /apex/com.android.art/bin/art/arm64/art_cmdline_tests \
        -- --gtest_list_tests
Test: Render `README.chroot.md` as HTML and check the output manually
Bug: 34729697
Change-Id: If41181666eb52750444b1b40dfab0629f802b7b5
diff --git a/test/README.chroot.md b/test/README.chroot.md
index a4fee55..5d03c78 100644
--- a/test/README.chroot.md
+++ b/test/README.chroot.md
@@ -72,7 +72,8 @@
     ```bash
     art/tools/buildbot-cleanup-device.sh
     ```
-5. Setup the device (including setting up mount points and files in the chroot directory):
+5. Setup the device (including setting up mount points and files in the chroot
+   directory):
     ```bash
     art/tools/buildbot-setup-device.sh
     ```
@@ -85,14 +86,45 @@
     ```bash
     art/tools/run-gtests.sh -j4
     ```
-    * Note: This currently fails on test
-    `test-art-target-gtest-image_space_test{32,64}` when using the full AOSP
+    * Specific tests to run can be passed on the command line, specified by
+      their absolute paths beginning with `/apex/`:
+        ```bash
+        art/tools/run-gtests.sh \
+          /apex/com.android.art/bin/art/arm64/art_cmdline_tests \
+          /apex/com.android.art/bin/art/arm64/art_dexdump_tests
+        ```
+    * Gtest options can be passed to each gtest by passing them after `--`; see
+      the following examples.
+        * To print the list of all test cases of a given gtest, use option
+          `--gtest_list_tests`:
+            ```bash
+            art/tools/run-gtests.sh \
+              /apex/com.android.art/bin/art/arm64/art_cmdline_tests \
+              -- --gtest_list_tests
+            ```
+        * To filter the test cases to execute, use option `--gtest_filter`:
+            ```bash
+            art/tools/run-gtests.sh \
+              /apex/com.android.art/bin/art/arm64/art_cmdline_tests \
+              -- --gtest_filter="*TestJdwp*"
+            ```
+        * To see all the options supported by a gtest, use option `--help`:
+            ```bash
+            art/tools/run-gtests.sh \
+              /apex/com.android.art/bin/art/arm64/art_cmdline_tests \
+              -- --help
+            ```
+    * Note: Some test cases of `art_runtime_tests` defined in
+    `art/runtime/gc/space/image_space_test.cc` may fail when using the full AOSP
     tree (b/119815008).
         * Workaround: Run `m clean-oat-host` before the build step
         (`art/tools/buildbot-build.sh --target`) above.
-    * Note: The `-j` option is not honored yet (b/129930445).
-    * Specific tests to run can be passed on the command line, specified by
-    their absolute paths beginning with `/apex/`.
+    * Note: The `-j` option of script `art/tools/run-gtests.sh` is not honored
+      yet (b/129930445). However, gtests themselves support parallel execution,
+      which can be specified via the gtest option `-j`:
+        ```bash
+        art/tools/run-gtests.sh -- -j4
+        ```
 8. Run ART run-tests:
     * On a 64-bit target:
         ```bash
diff --git a/tools/run-gtests.sh b/tools/run-gtests.sh
index 5a4ab3a..21064c1 100755
--- a/tools/run-gtests.sh
+++ b/tools/run-gtests.sh
@@ -14,12 +14,18 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+set -e
+
 if [[ $1 = -h ]]; then
   cat <<EOF
+Usage: $0 [<gtest>...] [--] [<gtest-option>...]
+
 Script to run gtests located in the ART (Testing) APEX.
 
 If called with arguments, only those tests are run, as specified by their
 absolute paths (starting with /apex). All gtests are run otherwise.
+
+Options after \`--\` are passed verbatim to each gtest binary.
 EOF
   exit
 fi
@@ -40,27 +46,38 @@
   shift
 fi
 
-if [ $# -gt 0 ]; then
-  tests="$@"
-else
+tests=()
+
+while [[ $# -gt 0 ]]; do
+  if [[ "$1" == "--" ]]; then
+    shift
+    break
+  fi
+  tests+=("$1")
+  shift
+done
+
+options="$@"
+
+if [[ ${#tests[@]} -eq 0 ]]; then
   # Search for executables under the `bin/art` directory of the ART APEX.
-  tests=$("$adb" shell chroot "$ART_TEST_CHROOT" \
+  readarray -t tests <<<$("$adb" shell chroot "$ART_TEST_CHROOT" \
     find "$android_art_root/bin/art" -type f -perm /ugo+x | sort)
 fi
 
 failing_tests=()
 
-for t in $tests; do
+for t in ${tests[@]}; do
   echo "$t"
   "$adb" shell chroot "$ART_TEST_CHROOT" \
     env ANDROID_ART_ROOT="$android_art_root" \
         ANDROID_I18N_ROOT="$android_i18n_root" \
         ANDROID_TZDATA_ROOT="$android_tzdata_root" \
-        $t \
+        $t $options \
     || failing_tests+=("$t")
 done
 
-if [ -n "$failing_tests" ]; then
+if [[ -n "$failing_tests" ]]; then
   for t in "${failing_tests[@]}"; do
     echo "Failed test: $t"
   done