Add support for using 'art' tools with gdbserver

Some tools (like run-jdwp-tests) invoke the 'art' tool automatically.
This makes it difficult to debug the runtime when running code
controlled by those tools. To allow this I added a --gdbserver flag to
both the 'art' tool and the 'run-jdwp-tests.sh' script.

To use:
    % ./art/tools/run-jdwp-tests.sh --debug \
                                    <...> \
                                    --gdbserver localhost:9999
or
    % art --gdbserver localhost:9999 <...>

In another terminal:
    % gdb /path/to/dalvikvm # the art command will print the
                            # appropriate path
    (gdb) target remote localhost:9999

Test: Manual
Change-Id: Ice9a9f7622eee66261d10663c83bf06aef95f399
diff --git a/tools/art b/tools/art
index 15993dd..456508f 100644
--- a/tools/art
+++ b/tools/art
@@ -81,6 +81,8 @@
   -d                       Use the debug ART library (libartd.so).
   --debug                  Equivalent to -d.
   --gdb                    Launch the Android Runtime in gdb.
+  --gdbserver <comms>      Launch the Android Runtime in gdbserver using the
+                           supplied communication channel.
   --help                   Display usage message.
   --invoke-with <program>  Launch the Android Runtime in <program>.
   --perf                   Launch the Android Runtime with perf recording.
@@ -271,6 +273,10 @@
     # Expect that debug mode wants all checks.
     EXTRA_OPTIONS+=(-XX:SlowDebug=true)
     ;;
+  --gdbserver)
+    LAUNCH_WRAPPER="gdbserver $2"
+    shift
+    ;;
   --gdb)
     LIBART="libartd.so"
     LAUNCH_WRAPPER="gdb --args"
diff --git a/tools/run-jdwp-tests.sh b/tools/run-jdwp-tests.sh
index d0e35ac..cee75df 100755
--- a/tools/run-jdwp-tests.sh
+++ b/tools/run-jdwp-tests.sh
@@ -43,12 +43,14 @@
 image_compiler_option=""
 plugin=""
 debug="no"
+explicit_debug="no"
 verbose="no"
 image="-Ximage:/data/art-test/core.art"
 with_jdwp_path=""
 agent_wrapper=""
 vm_args=""
 # By default, we run the whole JDWP test suite.
+has_specific_test="no"
 test="org.apache.harmony.jpda.tests.share.AllTests"
 mode="target"
 # Use JIT compiling by default.
@@ -61,6 +63,9 @@
 # continuous testing. This value can be adjusted to fit the configuration of the host machine(s).
 jdwp_test_timeout=10000
 
+gdb_target=
+has_gdb="no"
+
 while true; do
   if [[ "$1" == "--mode=host" ]]; then
     mode="host"
@@ -107,7 +112,14 @@
     # Remove the --no-jit from the arguments.
     args=${args/$1}
     shift
+  elif [[ $1 == "--no-debug" ]]; then
+    explicit_debug="yes"
+    debug="no"
+    # Remove the --no-debug from the arguments.
+    args=${args/$1}
+    shift
   elif [[ $1 == "--debug" ]]; then
+    explicit_debug="yes"
     debug="yes"
     # Remove the --debug from the arguments.
     args=${args/$1}
@@ -117,10 +129,20 @@
     # Remove the --verbose from the arguments.
     args=${args/$1}
     shift
+  elif [[ $1 == "--gdbserver" ]]; then
+    # Remove the --gdbserver from the arguments.
+    args=${args/$1}
+    has_gdb="yes"
+    shift
+    gdb_target=$1
+    # Remove the target from the arguments.
+    args=${args/$1}
+    shift
   elif [[ $1 == "--test" ]]; then
     # Remove the --test from the arguments.
     args=${args/$1}
     shift
+    has_specific_test="yes"
     test=$1
     # Remove the test from the arguments.
     args=${args/$1}
@@ -147,6 +169,12 @@
   fi
 done
 
+if [[ $has_gdb = "yes" ]]; then
+  if [[ $explicit_debug = "no" ]]; then
+    debug="yes"
+  fi
+fi
+
 if [[ $mode == "ri" ]]; then
   using_jack="false"
   if [[ "x$with_jdwp_path" != "x" ]]; then
@@ -156,11 +184,25 @@
   if [[ "x$image" != "x" ]]; then
     echo "Cannot use -Ximage: with --mode=jvm"
     exit 1
+  elif [[ $has_gdb = "yes" ]]; then
+    echo "Cannot use --gdbserver with --mode=jvm"
+    exit 1
   elif [[ $debug == "yes" ]]; then
     echo "Cannot use --debug with --mode=jvm"
     exit 1
   fi
 else
+  if [[ $has_gdb = "yes" ]]; then
+    if [[ $mode == "target" ]]; then
+      echo "Cannot use --gdbserver with --mode=target"
+      exit 1
+    else
+      art_debugee="$art_debugee --gdbserver $gdb_target"
+      # The tests absolutely require some timeout. We set a ~2 week timeout since we can kill the
+      # test with gdb if it goes on too long.
+      jdwp_test_timeout="1000000000"
+    fi
+  fi
   if [[ "x$with_jdwp_path" != "x" ]]; then
     vm_args="${vm_args} --vm-arg -Djpda.settings.debuggeeAgentArgument=-agentpath:${agent_wrapper}"
     vm_args="${vm_args} --vm-arg -Djpda.settings.debuggeeAgentName=${with_jdwp_path}"