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