1#!/bin/sh
2#
3# Run the code in test.jar on the device. The jar should contain a top-level
4# class named Main to run.
5
6msg() {
7    if [ "$QUIET" = "n" ]; then
8        echo "$@"
9    fi
10}
11
12RELOCATE="y"
13GDB="n"
14DEBUGGER="n"
15INTERPRETER="n"
16VERIFY="y"
17OPTIMIZE="y"
18ZYGOTE=""
19QUIET="n"
20DEV_MODE="n"
21INVOKE_WITH=""
22FLAGS=""
23TARGET_SUFFIX="32"
24GDB_TARGET_SUFFIX=""
25SECONDARY_DEX=""
26DEX_VERIFY=""
27
28while true; do
29    if [ "x$1" = "x--quiet" ]; then
30        QUIET="y"
31        shift
32    elif [ "x$1" = "x--lib" ]; then
33        shift
34        if [ "x$1" = "x" ]; then
35            echo "$0 missing argument to --lib" 1>&2
36            exit 1
37        fi
38        LIB="$1"
39        shift
40    elif [ "x$1" = "x-Xcompiler-option" ]; then
41        shift
42        option="$1"
43        FLAGS="${FLAGS} -Xcompiler-option $option"
44        shift
45    elif [ "x$1" = "x--runtime-option" ]; then
46        shift
47        option="$1"
48        FLAGS="${FLAGS} $option"
49        shift
50    elif [ "x$1" = "x--boot" ]; then
51        shift
52        BOOT_OPT="$1"
53        shift
54    elif [ "x$1" = "x--debug" ]; then
55        DEBUGGER="y"
56        shift
57    elif [ "x$1" = "x--gdb" ]; then
58        GDB="y"
59        DEV_MODE="y"
60        shift
61    elif [ "x$1" = "x--zygote" ]; then
62        ZYGOTE="--zygote"
63        msg "Spawning from zygote"
64        shift
65    elif [ "x$1" = "x--dev" ]; then
66        DEV_MODE="y"
67        shift
68    elif [ "x$1" = "x--relocate" ]; then
69        RELOCATE="y"
70        shift
71    elif [ "x$1" = "x--no-relocate" ]; then
72        RELOCATE="n"
73        shift
74    elif [ "x$1" = "x--interpreter" ]; then
75        INTERPRETER="y"
76        shift
77    elif [ "x$1" = "x--invoke-with" ]; then
78        shift
79        if [ "x$1" = "x" ]; then
80            echo "$0 missing argument to --invoke-with" 1>&2
81            exit 1
82        fi
83        if [ "x$INVOKE_WITH" = "x" ]; then
84            INVOKE_WITH="$1"
85        else
86            INVOKE_WITH="$INVOKE_WITH $1"
87        fi
88        shift
89    elif [ "x$1" = "x--no-verify" ]; then
90        VERIFY="n"
91        shift
92    elif [ "x$1" = "x--no-optimize" ]; then
93        OPTIMIZE="n"
94        shift
95    elif [ "x$1" = "x--" ]; then
96        shift
97        break
98    elif [ "x$1" = "x--64" ]; then
99        TARGET_SUFFIX="64"
100        GDB_TARGET_SUFFIX="64"
101        shift
102    elif [ "x$1" = "x--secondary" ]; then
103        SECONDARY_DEX=":$DEX_LOCATION/$TEST_NAME-ex.jar"
104        shift
105    elif expr "x$1" : "x--" >/dev/null 2>&1; then
106        echo "unknown $0 option: $1" 1>&2
107        exit 1
108    else
109        break
110    fi
111done
112
113if [ "$ZYGOTE" = "" ]; then
114    if [ "$OPTIMIZE" = "y" ]; then
115        if [ "$VERIFY" = "y" ]; then
116            DEX_OPTIMIZE="-Xdexopt:verified"
117        else
118            DEX_OPTIMIZE="-Xdexopt:all"
119        fi
120        msg "Performing optimizations"
121    else
122        DEX_OPTIMIZE="-Xdexopt:none"
123        msg "Skipping optimizations"
124    fi
125
126    if [ "$VERIFY" = "y" ]; then
127        msg "Performing verification"
128    else
129        DEX_VERIFY="-Xverify:none"
130        msg "Skipping verification"
131    fi
132fi
133
134msg "------------------------------"
135
136if [ "$QUIET" = "n" ]; then
137  adb shell rm -r $DEX_LOCATION
138  adb shell mkdir -p $DEX_LOCATION
139  adb push $TEST_NAME.jar $DEX_LOCATION
140  adb push $TEST_NAME-ex.jar $DEX_LOCATION
141else
142  adb shell rm -r $DEX_LOCATION >/dev/null 2>&1
143  adb shell mkdir -p $DEX_LOCATION >/dev/null 2>&1
144  adb push $TEST_NAME.jar $DEX_LOCATION >/dev/null 2>&1
145  adb push $TEST_NAME-ex.jar $DEX_LOCATION >/dev/null 2>&1
146fi
147
148if [ "$DEBUGGER" = "y" ]; then
149  # Use this instead for ddms and connect by running 'ddms':
150  # DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_android_adb,server=y,suspend=y"
151  # TODO: add a separate --ddms option?
152
153  PORT=12345
154  msg "Waiting for jdb to connect:"
155  msg "    adb forward tcp:$PORT tcp:$PORT"
156  msg "    jdb -attach localhost:$PORT"
157  DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=$PORT,server=y,suspend=y"
158fi
159
160if [ "$GDB" = "y" ]; then
161    gdb="gdbserver$GDB_TARGET_SUFFIX :5039"
162    gdbargs="$exe"
163fi
164
165if [ "$INTERPRETER" = "y" ]; then
166    INT_OPTS="-Xint"
167    if [ "$VERIFY" = "y" ] ; then
168      COMPILER_FLAGS="${COMPILER_FLAGS} --compiler-filter=interpret-only"
169    else
170      COMPILER_FLAGS="${COMPILER_FLAGS} --compiler-filter=verify-none"
171      DEX_VERIFY="${DEX_VERIFY} -Xverify:none"
172    fi
173fi
174
175JNI_OPTS="-Xjnigreflimit:512 -Xcheck:jni"
176
177if [ "$RELOCATE" = "y" ]; then
178  RELOCATE_OPT="-Xrelocate"
179  FLAGS="${FLAGS} -Xcompiler-option --include-patch-information"
180else
181  RELOCATE_OPT="-Xnorelocate"
182fi
183
184cmdline="cd $DEX_LOCATION && export ANDROID_DATA=$DEX_LOCATION && export DEX_LOCATION=$DEX_LOCATION && \
185    $INVOKE_WITH $gdb /system/bin/dalvikvm$TARGET_SUFFIX $FLAGS $DEX_VERIFY $gdbargs -XXlib:$LIB $ZYGOTE $JNI_OPTS $RELOCATE_OPT $INT_OPTS $DEBUGGER_OPTS $BOOT_OPT -cp $DEX_LOCATION/$TEST_NAME.jar$SECONDARY_DEX Main"
186if [ "$DEV_MODE" = "y" ]; then
187  echo $cmdline "$@"
188fi
189
190adb shell $cmdline "$@"
191