Run-test: Support using --gdb and --gdbserver with --chroot

--gdb and --gdbserver were unusable with --chroot due to --chroot
changing how dnslookup happened on device and differing symbol
directory setup.

This CL does the basic setup so that gdb can connect to the remote
program and (provided it is configured correctly) be used to debug it.

In order to get symbols one must set both the `solib-search-path` and
`solib-absolute-prefix` appropriately. See GDB documentation for more
information.

Test: ./test/run-test --gdbserver --chroot /data/local/art-test-chroot --64 001-HelloWorld
Change-Id: I598cb7654f5b8b669e1c658e25fb941c3d046666
diff --git a/test/etc/run-test-jar b/test/etc/run-test-jar
index 97ec834..596f9b4 100755
--- a/test/etc/run-test-jar
+++ b/test/etc/run-test-jar
@@ -307,11 +307,6 @@
         USE_GDBSERVER="y"
         DEV_MODE="y"
         TIME_OUT="n"
-        HOST="y"
-        ANDROID_ROOT="${ANDROID_HOST_OUT}"
-        ANDROID_ART_ROOT="${ANDROID_HOST_OUT}/com.android.art"
-        ANDROID_I18N_ROOT="${ANDROID_HOST_OUT}/com.android.i18n"
-        ANDROID_TZDATA_ROOT="${ANDROID_HOST_OUT}/com.android.tzdata"
         shift
     elif [ "x$1" = "x--gdb" ]; then
         USE_GDB="y"
@@ -718,7 +713,8 @@
     echo "Cannot pass both --gdb and --gdbserver at the same time!" >&2
     exit 1
   elif [ "$HOST" = "n" ]; then
-    GDB="$GDBSERVER_DEVICE $GDBSERVER_PORT"
+    # We might not have any hostname resolution if we are using a chroot.
+    GDB="$GDBSERVER_DEVICE --no-startup-with-shell 127.0.0.1$GDBSERVER_PORT"
   else
     if [ `uname` = "Darwin" ]; then
         GDB=lldb
@@ -733,10 +729,11 @@
   fi
 elif [ "$USE_GDBSERVER" = "y" ]; then
   if [ "$HOST" = "n" ]; then
-    echo "Cannot use --gdbserver in non-host configs" >&2
-    exit 1
+    # We might not have any hostname resolution if we are using a chroot.
+    GDB="$GDBSERVER_DEVICE --no-startup-with-shell 127.0.0.1$GDBSERVER_PORT"
+  else
+    GDB="$GDBSERVER_HOST $GDBSERVER_PORT"
   fi
-  GDB="$GDBSERVER_HOST $GDBSERVER_PORT"
 fi
 
 if [ "$INTERPRETER" = "y" ]; then
@@ -1149,6 +1146,9 @@
 
     if [ "$DEV_MODE" = "y" ]; then
       echo $cmdline
+      if [ "$USE_GDB" = "y" ] || [ "$USE_GDBSERVER" = "y" ]; then
+        echo "Forward ${GDBSERVER_PORT} to local port and connect GDB"
+      fi
     fi
 
     if [ "$QUIET" = "n" ]; then
diff --git a/tools/buildbot-build.sh b/tools/buildbot-build.sh
index b7b82cc..14d3544 100755
--- a/tools/buildbot-build.sh
+++ b/tools/buildbot-build.sh
@@ -76,7 +76,7 @@
   fi
   make_command="build/soong/soong_ui.bash --make-mode $j_arg $extra_args $showcommands build-art-target-tests $common_targets"
   make_command+=" libjavacrypto-target libnetd_client-target toybox toolbox sh"
-  make_command+=" debuggerd su"
+  make_command+=" debuggerd su gdbserver"
   make_command+=" libstdc++ "
   make_command+=" ${ANDROID_PRODUCT_OUT#"${ANDROID_BUILD_TOP}/"}/system/etc/public.libraries.txt"
   if [[ -n "$ART_TEST_CHROOT" ]]; then
@@ -131,4 +131,9 @@
   link_command="mkdir -p $(dirname "$link_name") && ln -sf com.android.art.testing \"$link_name\""
   echo "Executing $link_command"
   bash -c "$link_command"
+  find $ANDROID_PRODUCT_OUT/symbols/apex/com.android.runtime/bin -type f | while read target; do
+    cmd="ln -sf $target  $ANDROID_PRODUCT_OUT/symbols/system/bin/$(basename $target)"
+    echo "Executing $cmd"
+    bash -c "$cmd"
+  done
 fi