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 30 while true; do 31 if [ "x$1" = "x--quiet" ]; then 32 QUIET="y" 33 shift 34 elif [ "x$1" = "x--lib" ]; then 35 shift 36 if [ "x$1" = "x" ]; then 37 echo "$0 missing argument to --lib" 1>&2 38 exit 1 39 fi 40 LIB="$1" 41 shift 42 elif [ "x$1" = "x-Xcompiler-option" ]; then 43 shift 44 option="$1" 45 FLAGS="${FLAGS} -Xcompiler-option $option" 46 COMPILE_FLAGS="${COMPILE_FLAGS} $option" 47 shift 48 elif [ "x$1" = "x--runtime-option" ]; then 49 shift 50 option="$1" 51 FLAGS="${FLAGS} $option" 52 shift 53 elif [ "x$1" = "x--boot" ]; then 54 shift 55 BOOT_OPT="$1" 56 BUILD_BOOT_OPT="--boot-image=${1#-Ximage:}" 57 shift 58 elif [ "x$1" = "x--relocate" ]; then 59 RELOCATE="y" 60 shift 61 elif [ "x$1" = "x--no-relocate" ]; then 62 RELOCATE="n" 63 shift 64 elif [ "x$1" = "x--debug" ]; then 65 DEBUGGER="y" 66 shift 67 elif [ "x$1" = "x--gdb" ]; then 68 GDB="y" 69 DEV_MODE="y" 70 shift 71 elif [ "x$1" = "x--zygote" ]; then 72 ZYGOTE="--zygote" 73 msg "Spawning from zygote" 74 shift 75 elif [ "x$1" = "x--dev" ]; then 76 DEV_MODE="y" 77 shift 78 elif [ "x$1" = "x--interpreter" ]; then 79 INTERPRETER="y" 80 shift 81 elif [ "x$1" = "x--invoke-with" ]; then 82 shift 83 if [ "x$1" = "x" ]; then 84 echo "$0 missing argument to --invoke-with" 1>&2 85 exit 1 86 fi 87 if [ "x$INVOKE_WITH" = "x" ]; then 88 INVOKE_WITH="$1" 89 else 90 INVOKE_WITH="$INVOKE_WITH $1" 91 fi 92 shift 93 elif [ "x$1" = "x--no-verify" ]; then 94 VERIFY="n" 95 shift 96 elif [ "x$1" = "x--no-optimize" ]; then 97 OPTIMIZE="n" 98 shift 99 elif [ "x$1" = "x--" ]; then 100 shift 101 break 102 elif [ "x$1" = "x--64" ]; then 103 TARGET_SUFFIX="64" 104 GDB_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 113 done 114 115 if [ "$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 135 fi 136 137 msg "------------------------------" 138 139 ARCH=$(adb shell ls -F /data/dalvik-cache | grep -Ewo "${ARCHITECTURES_PATTERN}") 140 if [ x"$ARCH" = "x" ]; then 141 echo "Unable to determine architecture" 142 exit 1 143 fi 144 145 if [ "$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 150 else 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 155 fi 156 157 if [ "$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" 167 fi 168 169 if [ "$GDB" = "y" ]; then 170 gdb="gdbserver$GDB_TARGET_SUFFIX :5039" 171 gdbargs="$exe" 172 fi 173 174 if [ "$INTERPRETER" = "y" ]; then 175 INT_OPTS="-Xint" 176 COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=interpret-only" 177 fi 178 179 JNI_OPTS="-Xjnigreflimit:512 -Xcheck:jni" 180 181 if [ "$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" 186 else 187 RELOCATE_OPT="-Xnorelocate" 188 BUILD_RELOCATE_OPT="--runtime-arg -Xnorelocate" 189 fi 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. 194 cmdline="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 $@" 198 cmdfile=$(tempfile -p "cmd-" -s "-$TEST_NAME") 199 echo "$cmdline" > $cmdfile 200 201 if [ "$DEV_MODE" = "y" ]; then 202 echo $cmdline 203 fi 204 205 if [ "$QUIET" = "n" ]; then 206 adb push $cmdfile $DEX_LOCATION/cmdline.sh 207 else 208 adb push $cmdfile $DEX_LOCATION/cmdline.sh > /dev/null 2>&1 209 fi 210 211 adb shell sh $DEX_LOCATION/cmdline.sh 212 213 rm -f $cmdfile 214