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