blob: d3ad71f32bfa5b21e997fd681e2c3ee3b3197b5b [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001#!/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.
jeffhao5d1ac922011-09-29 17:41:15 -07005
6msg() {
7 if [ "$QUIET" = "n" ]; then
8 echo "$@"
9 fi
10}
11
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +010012ARCHITECTURES_32="(arm|x86|mips|none)"
13ARCHITECTURES_64="(arm64|x86_64|none)"
14ARCHITECTURES_PATTERN="${ARCHITECTURES_32}"
Alex Lighta59dd802014-07-02 16:28:08 -070015RELOCATE="y"
Ian Rogers4c1bf1a2012-12-11 10:44:28 -080016GDB="n"
Brian Carlstromfa42b442013-06-17 12:53:45 -070017DEBUGGER="n"
jeffhao0dff3f42012-11-20 15:13:43 -080018INTERPRETER="n"
jeffhao5d1ac922011-09-29 17:41:15 -070019VERIFY="y"
20OPTIMIZE="y"
Elliott Hughes58bcc402012-02-14 14:10:10 -080021ZYGOTE=""
jeffhao5d1ac922011-09-29 17:41:15 -070022QUIET="n"
jeffhao5d1ac922011-09-29 17:41:15 -070023DEV_MODE="n"
Elliott Hughes7c046102011-10-19 18:16:03 -070024INVOKE_WITH=""
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000025FLAGS=""
Alex Lightfc0082b2014-07-29 18:31:18 -070026TARGET_SUFFIX="32"
27GDB_TARGET_SUFFIX=""
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +010028COMPILE_FLAGS=""
Alex Light03a112d2014-08-25 13:25:56 -070029FALSE_BIN="/system/bin/false"
30PATCHOAT=""
31DEX2OAT=""
32HAVE_IMAGE="y"
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +010033PREBUILD="y"
jeffhao5d1ac922011-09-29 17:41:15 -070034
35while true; do
36 if [ "x$1" = "x--quiet" ]; then
37 QUIET="y"
38 shift
Brian Carlstromdc959ea2013-10-28 00:44:49 -070039 elif [ "x$1" = "x--lib" ]; then
Brian Carlstromfa42b442013-06-17 12:53:45 -070040 shift
Brian Carlstromdc959ea2013-10-28 00:44:49 -070041 if [ "x$1" = "x" ]; then
42 echo "$0 missing argument to --lib" 1>&2
43 exit 1
44 fi
Brian Carlstromfa42b442013-06-17 12:53:45 -070045 LIB="$1"
Brian Carlstromdc959ea2013-10-28 00:44:49 -070046 shift
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000047 elif [ "x$1" = "x-Xcompiler-option" ]; then
48 shift
49 option="$1"
50 FLAGS="${FLAGS} -Xcompiler-option $option"
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +010051 COMPILE_FLAGS="${COMPILE_FLAGS} $option"
Alex Light03a112d2014-08-25 13:25:56 -070052 shift
Mathieu Chartier769a5ad2014-05-18 15:30:10 -070053 elif [ "x$1" = "x--runtime-option" ]; then
54 shift
55 option="$1"
56 FLAGS="${FLAGS} $option"
57 shift
Brian Carlstromdc959ea2013-10-28 00:44:49 -070058 elif [ "x$1" = "x--boot" ]; then
59 shift
60 BOOT_OPT="$1"
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +010061 BUILD_BOOT_OPT="--boot-image=${1#-Ximage:}"
62 shift
63 elif [ "x$1" = "x--no-dex2oat" ]; then
64 DEX2OAT="-Xcompiler:${FALSE_BIN}"
65 shift
66 elif [ "x$1" = "x--no-patchoat" ]; then
67 PATCHOAT="-Xpatchoat:${FALSE_BIN}"
68 shift
69 elif [ "x$1" = "x--relocate" ]; then
70 RELOCATE="y"
71 shift
72 elif [ "x$1" = "x--no-relocate" ]; then
73 RELOCATE="n"
74 shift
75 elif [ "x$1" = "x--prebuild" ]; then
76 PREBUILD="y"
77 shift
78 elif [ "x$1" = "x--no-prebuild" ]; then
79 PREBUILD="n"
80 shift
81 elif [ "x$1" = "x--no-image" ]; then
82 HAVE_IMAGE="n"
Elliott Hughes7c046102011-10-19 18:16:03 -070083 shift
jeffhao5d1ac922011-09-29 17:41:15 -070084 elif [ "x$1" = "x--debug" ]; then
Brian Carlstromfa42b442013-06-17 12:53:45 -070085 DEBUGGER="y"
jeffhao5d1ac922011-09-29 17:41:15 -070086 shift
Ian Rogers4c1bf1a2012-12-11 10:44:28 -080087 elif [ "x$1" = "x--gdb" ]; then
88 GDB="y"
89 DEV_MODE="y"
90 shift
jeffhao5d1ac922011-09-29 17:41:15 -070091 elif [ "x$1" = "x--zygote" ]; then
Elliott Hughes58bcc402012-02-14 14:10:10 -080092 ZYGOTE="--zygote"
jeffhao5d1ac922011-09-29 17:41:15 -070093 msg "Spawning from zygote"
94 shift
95 elif [ "x$1" = "x--dev" ]; then
96 DEV_MODE="y"
97 shift
jeffhao0dff3f42012-11-20 15:13:43 -080098 elif [ "x$1" = "x--interpreter" ]; then
99 INTERPRETER="y"
100 shift
Elliott Hughes7c046102011-10-19 18:16:03 -0700101 elif [ "x$1" = "x--invoke-with" ]; then
102 shift
Brian Carlstromdc959ea2013-10-28 00:44:49 -0700103 if [ "x$1" = "x" ]; then
104 echo "$0 missing argument to --invoke-with" 1>&2
105 exit 1
106 fi
Ian Rogers0e033672013-04-19 10:22:46 -0700107 if [ "x$INVOKE_WITH" = "x" ]; then
108 INVOKE_WITH="$1"
109 else
110 INVOKE_WITH="$INVOKE_WITH $1"
111 fi
Elliott Hughes7c046102011-10-19 18:16:03 -0700112 shift
jeffhao5d1ac922011-09-29 17:41:15 -0700113 elif [ "x$1" = "x--no-verify" ]; then
114 VERIFY="n"
115 shift
116 elif [ "x$1" = "x--no-optimize" ]; then
117 OPTIMIZE="n"
118 shift
jeffhao5d1ac922011-09-29 17:41:15 -0700119 elif [ "x$1" = "x--" ]; then
120 shift
121 break
Andreas Gampeafbaa1a2014-03-25 18:09:32 -0700122 elif [ "x$1" = "x--64" ]; then
123 TARGET_SUFFIX="64"
Alex Lightfc0082b2014-07-29 18:31:18 -0700124 GDB_TARGET_SUFFIX="64"
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +0100125 ARCHITECTURES_PATTERN="${ARCHITECTURES_64}"
Andreas Gampeafbaa1a2014-03-25 18:09:32 -0700126 shift
jeffhao5d1ac922011-09-29 17:41:15 -0700127 elif expr "x$1" : "x--" >/dev/null 2>&1; then
Elliott Hughes7c046102011-10-19 18:16:03 -0700128 echo "unknown $0 option: $1" 1>&2
jeffhao5d1ac922011-09-29 17:41:15 -0700129 exit 1
130 else
131 break
132 fi
133done
134
Elliott Hughes58bcc402012-02-14 14:10:10 -0800135if [ "$ZYGOTE" = "" ]; then
jeffhao5d1ac922011-09-29 17:41:15 -0700136 if [ "$OPTIMIZE" = "y" ]; then
137 if [ "$VERIFY" = "y" ]; then
138 DEX_OPTIMIZE="-Xdexopt:verified"
139 else
140 DEX_OPTIMIZE="-Xdexopt:all"
141 fi
142 msg "Performing optimizations"
143 else
144 DEX_OPTIMIZE="-Xdexopt:none"
145 msg "Skipping optimizations"
146 fi
147
148 if [ "$VERIFY" = "y" ]; then
149 DEX_VERIFY=""
150 msg "Performing verification"
151 else
152 DEX_VERIFY="-Xverify:none"
153 msg "Skipping verification"
154 fi
155fi
156
157msg "------------------------------"
158
Alex Light03a112d2014-08-25 13:25:56 -0700159if [ "$HAVE_IMAGE" = "n" ]; then
Alex Light1ef4ce82014-08-27 11:13:47 -0700160 BOOT_OPT="-Ximage:/system/non-existant/core.art"
Alex Light03a112d2014-08-25 13:25:56 -0700161fi
162
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +0100163ARCH=$(adb shell ls -F /data/dalvik-cache | grep -Ewo "${ARCHITECTURES_PATTERN}")
164if [ x"$ARCH" = "x" ]; then
165 echo "Unable to determine architecture"
166 exit 1
167fi
168
jeffhao5d1ac922011-09-29 17:41:15 -0700169if [ "$QUIET" = "n" ]; then
Brian Carlstrom4ec9b1f2012-06-17 22:27:43 -0700170 adb shell rm -r $DEX_LOCATION
Brian Carlstrom105215d2012-06-14 12:50:44 -0700171 adb shell mkdir -p $DEX_LOCATION
172 adb push $TEST_NAME.jar $DEX_LOCATION
173 adb push $TEST_NAME-ex.jar $DEX_LOCATION
jeffhao5d1ac922011-09-29 17:41:15 -0700174else
Brian Carlstrom4ec9b1f2012-06-17 22:27:43 -0700175 adb shell rm -r $DEX_LOCATION >/dev/null 2>&1
Brian Carlstrom105215d2012-06-14 12:50:44 -0700176 adb shell mkdir -p $DEX_LOCATION >/dev/null 2>&1
177 adb push $TEST_NAME.jar $DEX_LOCATION >/dev/null 2>&1
178 adb push $TEST_NAME-ex.jar $DEX_LOCATION >/dev/null 2>&1
jeffhao5d1ac922011-09-29 17:41:15 -0700179fi
180
Brian Carlstromfa42b442013-06-17 12:53:45 -0700181if [ "$DEBUGGER" = "y" ]; then
Elliott Hughes72395bf2012-04-24 13:45:26 -0700182 # Use this instead for ddms and connect by running 'ddms':
Brian Carlstromfa42b442013-06-17 12:53:45 -0700183 # DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_android_adb,server=y,suspend=y"
Elliott Hughes72395bf2012-04-24 13:45:26 -0700184 # TODO: add a separate --ddms option?
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700185
Elliott Hughes72395bf2012-04-24 13:45:26 -0700186 PORT=12345
187 msg "Waiting for jdb to connect:"
188 msg " adb forward tcp:$PORT tcp:$PORT"
189 msg " jdb -attach localhost:$PORT"
Brian Carlstromfa42b442013-06-17 12:53:45 -0700190 DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=$PORT,server=y,suspend=y"
jeffhao5d1ac922011-09-29 17:41:15 -0700191fi
192
Ian Rogers4c1bf1a2012-12-11 10:44:28 -0800193if [ "$GDB" = "y" ]; then
Alex Lightfc0082b2014-07-29 18:31:18 -0700194 gdb="gdbserver$GDB_TARGET_SUFFIX :5039"
Ian Rogersee043fc2014-03-11 11:30:20 -0700195 gdbargs="$exe"
Ian Rogers4c1bf1a2012-12-11 10:44:28 -0800196fi
197
jeffhao0dff3f42012-11-20 15:13:43 -0800198if [ "$INTERPRETER" = "y" ]; then
199 INT_OPTS="-Xint"
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +0100200 COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=interpret-only"
jeffhao0dff3f42012-11-20 15:13:43 -0800201fi
202
Mathieu Chartierdbe6f462012-09-25 16:54:50 -0700203JNI_OPTS="-Xjnigreflimit:512 -Xcheck:jni"
Elliott Hughes72395bf2012-04-24 13:45:26 -0700204
Alex Lighta59dd802014-07-02 16:28:08 -0700205if [ "$RELOCATE" = "y" ]; then
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +0100206 RELOCATE_OPT="-Xrelocate"
207 BUILD_RELOCATE_OPT="--runtime-arg -Xnorelocate"
208 COMPILE_FLAGS="${COMPILE_FLAGS} --include-patch-information"
209 FLAGS="${FLAGS} -Xcompiler-option --include-patch-information"
Alex Lighta59dd802014-07-02 16:28:08 -0700210else
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +0100211 RELOCATE_OPT="-Xnorelocate"
212 BUILD_RELOCATE_OPT="--runtime-arg -Xnorelocate"
Alex Lighta59dd802014-07-02 16:28:08 -0700213fi
214
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +0100215# Create a script with the command. The command can get longer than the longest
216# allowed adb command and there is no way to get the exit status from a adb shell
217# command.
218cmdline="cd $DEX_LOCATION && \
219 export ANDROID_DATA=$DEX_LOCATION && \
220 export DEX_LOCATION=$DEX_LOCATION && "
221if [ "$PREBUILD" = "y" ]; then
222 cmdline+="mkdir -p $DEX_LOCATION/dalvik-cache/$ARCH/ && \
223 $INVOKE_WITH /system/bin/dex2oatd \
224 $COMPILE_FLAGS \
225 $BUILD_BOOT_OPT \
226 $BUILD_RELOCATE_OPT \
227 --runtime-arg -classpath --runtime-arg $DEX_LOCATION/$TEST_NAME.jar \
228 --dex-file=$DEX_LOCATION/$TEST_NAME.jar \
229 --oat-file=$DEX_LOCATION/dalvik-cache/$ARCH/$(echo $DEX_LOCATION/$TEST_NAME.jar/classes.dex | cut -d/ -f 2- | sed "s:/:@:g") \
230 --instruction-set=$ARCH && "
231fi
232
233cmdline+="$INVOKE_WITH $gdb /system/bin/dalvikvm$TARGET_SUFFIX \
234 $FLAGS \
235 $gdbargs \
236 -XXlib:$LIB \
237 $PATCHOAT \
238 $DEX2OAT \
239 $ZYGOTE \
240 $JNI_OPTS \
241 $RELOCATE_OPT \
242 $INT_OPTS \
243 $DEBUGGER_OPTS \
244 $BOOT_OPT \
245 -cp $DEX_LOCATION/$TEST_NAME.jar Main"
246
247cmdfile=$(tempfile -p "cmd-" -s "-$TEST_NAME")
248echo "$cmdline" > $cmdfile
249
Elliott Hughes58bcc402012-02-14 14:10:10 -0800250if [ "$DEV_MODE" = "y" ]; then
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +0100251 echo $cmdline
jeffhao5d1ac922011-09-29 17:41:15 -0700252fi
Elliott Hughes58bcc402012-02-14 14:10:10 -0800253
Nicolas Geoffray5fd18ba2014-10-03 12:08:38 +0100254if [ "$QUIET" = "n" ]; then
255 adb push $cmdfile $DEX_LOCATION/cmdline.sh
256else
257 adb push $cmdfile $DEX_LOCATION/cmdline.sh > /dev/null 2>&1
258fi
259
260adb shell sh $DEX_LOCATION/cmdline.sh
261
262rm -f $cmdfile