push-and-run-prebuilt-test-jar revision 1c83cbc4a817acbd7f9abb5b29a2d418a958e6a1
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
12ARCHITECTURES_32="(arm|x86|mips|none)"
13ARCHITECTURES_64="(arm64|x86_64|none)"
14ARCHITECTURES_PATTERN="${ARCHITECTURES_32}"
15RELOCATE="y"
16GDB="n"
17DEBUGGER="n"
18INTERPRETER="n"
19VERIFY="y"
20OPTIMIZE="y"
21ZYGOTE=""
22QUIET="n"
23DEV_MODE="n"
24INVOKE_WITH=""
25FLAGS=""
26TARGET_SUFFIX=""
27COMPILE_FLAGS=""
28
29echo "$@"
30
31while true; do
32    if [ "x$1" = "x--quiet" ]; then
33        QUIET="y"
34        shift
35    elif [ "x$1" = "x--lib" ]; then
36        shift
37        if [ "x$1" = "x" ]; then
38            echo "$0 missing argument to --lib" 1>&2
39            exit 1
40        fi
41        LIB="$1"
42        shift
43    elif [ "x$1" = "x-Xcompiler-option" ]; then
44        shift
45        option="$1"
46        FLAGS="${FLAGS} -Xcompiler-option $option"
47        COMPILE_FLAGS="${COMPILE_FLAGS} $option"
48        shift
49    elif [ "x$1" = "x--runtime-option" ]; then
50        shift
51        option="$1"
52        FLAGS="${FLAGS} $option"
53        shift
54    elif [ "x$1" = "x--boot" ]; then
55        shift
56        BOOT_OPT="$1"
57        BUILD_BOOT_OPT="--boot-image=${1#-Ximage:}"
58        shift
59    elif [ "x$1" = "x--relocate" ]; then
60        RELOCATE="y"
61        shift
62    elif [ "x$1" = "x--no-relocate" ]; then
63        RELOCATE="n"
64        shift
65    elif [ "x$1" = "x--debug" ]; then
66        DEBUGGER="y"
67        shift
68    elif [ "x$1" = "x--gdb" ]; then
69        GDB="y"
70        DEV_MODE="y"
71        shift
72    elif [ "x$1" = "x--zygote" ]; then
73        ZYGOTE="--zygote"
74        msg "Spawning from zygote"
75        shift
76    elif [ "x$1" = "x--dev" ]; then
77        DEV_MODE="y"
78        shift
79    elif [ "x$1" = "x--interpreter" ]; then
80        INTERPRETER="y"
81        shift
82    elif [ "x$1" = "x--invoke-with" ]; then
83        shift
84        if [ "x$1" = "x" ]; then
85            echo "$0 missing argument to --invoke-with" 1>&2
86            exit 1
87        fi
88        if [ "x$INVOKE_WITH" = "x" ]; then
89            INVOKE_WITH="$1"
90        else
91            INVOKE_WITH="$INVOKE_WITH $1"
92        fi
93        shift
94    elif [ "x$1" = "x--no-verify" ]; then
95        VERIFY="n"
96        shift
97    elif [ "x$1" = "x--no-optimize" ]; then
98        OPTIMIZE="n"
99        shift
100    elif [ "x$1" = "x--" ]; then
101        shift
102        break
103    elif [ "x$1" = "x--64" ]; then
104        TARGET_SUFFIX="64"
105        ARCHITECTURES_PATTERN="${ARCHITECTURES_64}"
106        shift
107    elif expr "x$1" : "x--" >/dev/null 2>&1; then
108        echo "unknown $0 option: $1" 1>&2
109        exit 1
110    else
111        break
112    fi
113done
114
115if [ "$ZYGOTE" = "" ]; then
116    if [ "$OPTIMIZE" = "y" ]; then
117        if [ "$VERIFY" = "y" ]; then
118            DEX_OPTIMIZE="-Xdexopt:verified"
119        else
120            DEX_OPTIMIZE="-Xdexopt:all"
121        fi
122        msg "Performing optimizations"
123    else
124        DEX_OPTIMIZE="-Xdexopt:none"
125        msg "Skipping optimizations"
126    fi
127
128    if [ "$VERIFY" = "y" ]; then
129        DEX_VERIFY=""
130        msg "Performing verification"
131    else
132        DEX_VERIFY="-Xverify:none"
133        msg "Skipping verification"
134    fi
135fi
136
137msg "------------------------------"
138
139ARCH=$(adb shell ls -F /data/dalvik-cache | grep -Ewo "${ARCHITECTURES_PATTERN}")
140if [ x"$ARCH" = "x" ]; then
141  echo "Unable to determine architecture"
142  exit 1
143fi
144
145if [ "$QUIET" = "n" ]; then
146  adb shell rm -r $DEX_LOCATION
147  adb shell mkdir -p $DEX_LOCATION
148  adb push $TEST_NAME.jar $DEX_LOCATION
149  adb push $TEST_NAME-ex.jar $DEX_LOCATION
150else
151  adb shell rm -r $DEX_LOCATION >/dev/null 2>&1
152  adb shell mkdir -p $DEX_LOCATION >/dev/null 2>&1
153  adb push $TEST_NAME.jar $DEX_LOCATION >/dev/null 2>&1
154  adb push $TEST_NAME-ex.jar $DEX_LOCATION >/dev/null 2>&1
155fi
156
157if [ "$DEBUGGER" = "y" ]; then
158  # Use this instead for ddms and connect by running 'ddms':
159  # DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_android_adb,server=y,suspend=y"
160  # TODO: add a separate --ddms option?
161
162  PORT=12345
163  msg "Waiting for jdb to connect:"
164  msg "    adb forward tcp:$PORT tcp:$PORT"
165  msg "    jdb -attach localhost:$PORT"
166  DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=$PORT,server=y,suspend=y"
167fi
168
169if [ "$GDB" = "y" ]; then
170    gdb="gdbserver$TARGET_SUFFIX :5039"
171    gdbargs="$exe"
172fi
173
174if [ "$INTERPRETER" = "y" ]; then
175    INT_OPTS="-Xint"
176    COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=interpret-only"
177fi
178
179JNI_OPTS="-Xjnigreflimit:512 -Xcheck:jni"
180
181if [ "$RELOCATE" = "y" ]; then
182    RELOCATE_OPT="-Xrelocate"
183    BUILD_RELOCATE_OPT="--runtime-arg -Xnorelocate"
184    COMPILE_FLAGS="${COMPILE_FLAGS} --include-patch-information"
185    FLAGS="${FLAGS} -Xcompiler-option --include-patch-information"
186else
187    RELOCATE_OPT="-Xnorelocate"
188    BUILD_RELOCATE_OPT="--runtime-arg -Xnorelocate"
189fi
190
191# This is due to the fact this cmdline can get longer than the longest allowed
192# adb command and there is no way to get the exit status from a adb shell
193# command.
194cmdline="cd $DEX_LOCATION && export ANDROID_DATA=$DEX_LOCATION && export DEX_LOCATION=$DEX_LOCATION && \
195    mkdir -p $DEX_LOCATION/dalvik-cache/$ARCH/ && \
196    $INVOKE_WITH /system/bin/dex2oatd $COMPILE_FLAGS $BUILD_BOOT_OPT $BUILD_RELOCATE_OPT  --runtime-arg -classpath --runtime-arg $DEX_LOCATION/$TEST_NAME.jar --dex-file=$DEX_LOCATION/$TEST_NAME.jar --oat-file=$DEX_LOCATION/dalvik-cache/$ARCH/$(echo $DEX_LOCATION/$TEST_NAME.jar/classes.dex | cut -d/ -f 2- | sed "s:/:@:g") --instruction-set=$ARCH && \
197    $INVOKE_WITH $gdb /system/bin/dalvikvm$TARGET_SUFFIX $FLAGS $gdbargs -XXlib:$LIB $ZYGOTE $JNI_OPTS $RELOCATE_OPT $INT_OPTS $DEBUGGER_OPTS $BOOT_OPT -cp $DEX_LOCATION/$TEST_NAME.jar Main $@"
198cmdfile=$(tempfile -p "cmd-" -s "-$TEST_NAME")
199echo "$cmdline" > $cmdfile
200
201if [ "$DEV_MODE" = "y" ]; then
202  echo $cmdline
203fi
204
205if [ "$QUIET" = "n" ]; then
206  adb push $cmdfile $DEX_LOCATION/cmdline.sh
207else
208  adb push $cmdfile $DEX_LOCATION/cmdline.sh > /dev/null 2>&1
209fi
210
211adb shell sh $DEX_LOCATION/cmdline.sh
212
213rm -f $cmdfile
214