Home | History | Annotate | Download | only in etc
      1 #!/bin/bash
      2 #
      3 # Runner for an individual run-test.
      4 
      5 msg() {
      6     if [ "$QUIET" = "n" ]; then
      7         echo "$@"
      8     fi
      9 }
     10 
     11 ANDROID_ROOT="/system"
     12 ARCHITECTURES_32="(arm|x86|mips|none)"
     13 ARCHITECTURES_64="(arm64|x86_64|mips64|none)"
     14 ARCHITECTURES_PATTERN="${ARCHITECTURES_32}"
     15 BOOT_IMAGE=""
     16 COMPILE_FLAGS=""
     17 DALVIKVM="dalvikvm32"
     18 DEBUGGER="n"
     19 DEV_MODE="n"
     20 DEX2OAT=""
     21 EXPERIMENTAL=""
     22 FALSE_BIN="false"
     23 FLAGS=""
     24 ANDROID_FLAGS=""
     25 GDB=""
     26 GDB_ARGS=""
     27 GDB_SERVER="gdbserver"
     28 HAVE_IMAGE="y"
     29 HOST="n"
     30 INTERPRETER="n"
     31 JIT="n"
     32 INVOKE_WITH=""
     33 IS_JVMTI_TEST="n"
     34 ISA=x86
     35 LIBRARY_DIRECTORY="lib"
     36 TEST_DIRECTORY="nativetest"
     37 MAIN=""
     38 OPTIMIZE="y"
     39 PATCHOAT=""
     40 PREBUILD="y"
     41 QUIET="n"
     42 RELOCATE="n"
     43 STRIP_DEX="n"
     44 SECONDARY_DEX=""
     45 TIME_OUT="gdb"  # "n" (disabled), "timeout" (use timeout), "gdb" (use gdb)
     46 # Value in seconds
     47 if [ "$ART_USE_READ_BARRIER" != "false" ]; then
     48   TIME_OUT_VALUE=2400  # 40 minutes.
     49 else
     50   TIME_OUT_VALUE=1200  # 20 minutes.
     51 fi
     52 USE_GDB="n"
     53 USE_JVM="n"
     54 VERIFY="y" # y=yes,n=no,s=softfail
     55 ZYGOTE=""
     56 DEX_VERIFY=""
     57 USE_DEX2OAT_AND_PATCHOAT="y"
     58 INSTRUCTION_SET_FEATURES=""
     59 ARGS=""
     60 EXTERNAL_LOG_TAGS="n" # if y respect externally set ANDROID_LOG_TAGS.
     61 DRY_RUN="n" # if y prepare to run the test but don't run it.
     62 TEST_VDEX="n"
     63 TEST_IS_NDEBUG="n"
     64 APP_IMAGE="y"
     65 JVMTI_STRESS="n"
     66 JVMTI_STEP_STRESS="n"
     67 JVMTI_FIELD_STRESS="n"
     68 JVMTI_TRACE_STRESS="n"
     69 JVMTI_REDEFINE_STRESS="n"
     70 VDEX_FILTER=""
     71 PROFILE="n"
     72 RANDOM_PROFILE="n"
     73 
     74 # if "y", set -Xstacktracedir and inform the test of its location. When
     75 # this is set, stack trace dumps (from signal 3) will be written to a file
     76 # under this directory instead of stdout.
     77 SET_STACK_TRACE_DUMP_DIR="n"
     78 
     79 # if "y", run 'sync' before dalvikvm to make sure all files from
     80 # build step (e.g. dex2oat) were finished writing.
     81 SYNC_BEFORE_RUN="n"
     82 
     83 # When running a debug build, we want to run with all checks.
     84 ANDROID_FLAGS="${ANDROID_FLAGS} -XX:SlowDebug=true"
     85 
     86 while true; do
     87     if [ "x$1" = "x--quiet" ]; then
     88         QUIET="y"
     89         shift
     90     elif [ "x$1" = "x--jvmti" ]; then
     91         IS_JVMTI_TEST="y"
     92         shift
     93     elif [ "x$1" = "x-O" ]; then
     94         TEST_IS_NDEBUG="y"
     95         shift
     96     elif [ "x$1" = "x--lib" ]; then
     97         shift
     98         if [ "x$1" = "x" ]; then
     99             echo "$0 missing argument to --lib" 1>&2
    100             exit 1
    101         fi
    102         LIB="$1"
    103         shift
    104     elif [ "x$1" = "x--gc-stress" ]; then
    105         # Give an extra 5 mins if we are gc-stress.
    106         TIME_OUT_VALUE=$((${TIME_OUT_VALUE} + 300))
    107         shift
    108     elif [ "x$1" = "x--testlib" ]; then
    109         shift
    110         if [ "x$1" = "x" ]; then
    111             echo "$0 missing argument to --testlib" 1>&2
    112             exit 1
    113         fi
    114         ARGS="${ARGS} $1"
    115         shift
    116     elif [ "x$1" = "x--args" ]; then
    117         shift
    118         if [ "x$1" = "x" ]; then
    119             echo "$0 missing argument to --args" 1>&2
    120             exit 1
    121         fi
    122         ARGS="${ARGS} $1"
    123         shift
    124     elif [ "x$1" = "x-Xcompiler-option" ]; then
    125         shift
    126         option="$1"
    127         FLAGS="${FLAGS} -Xcompiler-option $option"
    128         COMPILE_FLAGS="${COMPILE_FLAGS} $option"
    129         shift
    130     elif [ "x$1" = "x--android-runtime-option" ]; then
    131         shift
    132         option="$1"
    133         ANDROID_FLAGS="${ANDROID_FLAGS} $option"
    134         shift
    135     elif [ "x$1" = "x--runtime-option" ]; then
    136         shift
    137         option="$1"
    138         FLAGS="${FLAGS} $option"
    139         shift
    140     elif [ "x$1" = "x--boot" ]; then
    141         shift
    142         BOOT_IMAGE="$1"
    143         shift
    144     elif [ "x$1" = "x--no-dex2oat" ]; then
    145         DEX2OAT="-Xcompiler:${FALSE_BIN}"
    146         USE_DEX2OAT_AND_PATCHOAT="n"
    147         shift
    148     elif [ "x$1" = "x--no-patchoat" ]; then
    149         PATCHOAT="-Xpatchoat:${FALSE_BIN}"
    150         USE_DEX2OAT_AND_PATCHOAT="n"
    151         shift
    152     elif [ "x$1" = "x--relocate" ]; then
    153         RELOCATE="y"
    154         shift
    155     elif [ "x$1" = "x--no-relocate" ]; then
    156         RELOCATE="n"
    157         shift
    158     elif [ "x$1" = "x--prebuild" ]; then
    159         PREBUILD="y"
    160         shift
    161     elif [ "x$1" = "x--jvmti-redefine-stress" ]; then
    162         # APP_IMAGE doesn't really work with jvmti redefine stress
    163         APP_IMAGE="n"
    164         JVMTI_STRESS="y"
    165         JVMTI_REDEFINE_STRESS="y"
    166         shift
    167     elif [ "x$1" = "x--jvmti-step-stress" ]; then
    168         JVMTI_STRESS="y"
    169         JVMTI_STEP_STRESS="y"
    170         shift
    171     elif [ "x$1" = "x--jvmti-field-stress" ]; then
    172         JVMTI_STRESS="y"
    173         JVMTI_FIELD_STRESS="y"
    174         shift
    175     elif [ "x$1" = "x--jvmti-trace-stress" ]; then
    176         JVMTI_STRESS="y"
    177         JVMTI_TRACE_STRESS="y"
    178         shift
    179     elif [ "x$1" = "x--no-app-image" ]; then
    180         APP_IMAGE="n"
    181         shift
    182     elif [ "x$1" = "x--strip-dex" ]; then
    183         STRIP_DEX="y"
    184         shift
    185     elif [ "x$1" = "x--host" ]; then
    186         HOST="y"
    187         ANDROID_ROOT="$ANDROID_HOST_OUT"
    188         shift
    189     elif [ "x$1" = "x--no-prebuild" ]; then
    190         PREBUILD="n"
    191         shift
    192     elif [ "x$1" = "x--no-image" ]; then
    193         HAVE_IMAGE="n"
    194         shift
    195     elif [ "x$1" = "x--secondary" ]; then
    196         SECONDARY_DEX=":$DEX_LOCATION/$TEST_NAME-ex.jar"
    197         # Enable cfg-append to make sure we get the dump for both dex files.
    198         # (otherwise the runtime compilation of the secondary dex will overwrite
    199         # the dump of the first one).
    200         FLAGS="${FLAGS} -Xcompiler-option --dump-cfg-append"
    201         COMPILE_FLAGS="${COMPILE_FLAGS} --dump-cfg-append"
    202         shift
    203     elif [ "x$1" = "x--debug" ]; then
    204         DEBUGGER="y"
    205         TIME_OUT="n"
    206         shift
    207     elif [ "x$1" = "x--gdb" ]; then
    208         USE_GDB="y"
    209         DEV_MODE="y"
    210         TIME_OUT="n"
    211         shift
    212     elif [ "x$1" = "x--gdb-arg" ]; then
    213         shift
    214         gdb_arg="$1"
    215         GDB_ARGS="${GDB_ARGS} $gdb_arg"
    216         shift
    217     elif [ "x$1" = "x--zygote" ]; then
    218         ZYGOTE="-Xzygote"
    219         msg "Spawning from zygote"
    220         shift
    221     elif [ "x$1" = "x--dev" ]; then
    222         DEV_MODE="y"
    223         shift
    224     elif [ "x$1" = "x--interpreter" ]; then
    225         INTERPRETER="y"
    226         shift
    227     elif [ "x$1" = "x--jit" ]; then
    228         JIT="y"
    229         shift
    230     elif [ "x$1" = "x--jvm" ]; then
    231         USE_JVM="y"
    232         shift
    233     elif [ "x$1" = "x--invoke-with" ]; then
    234         shift
    235         if [ "x$1" = "x" ]; then
    236             echo "$0 missing argument to --invoke-with" 1>&2
    237             exit 1
    238         fi
    239         if [ "x$INVOKE_WITH" = "x" ]; then
    240             INVOKE_WITH="$1"
    241         else
    242             INVOKE_WITH="$INVOKE_WITH $1"
    243         fi
    244         shift
    245     elif [ "x$1" = "x--no-verify" ]; then
    246         VERIFY="n"
    247         shift
    248     elif [ "x$1" = "x--verify-soft-fail" ]; then
    249         VERIFY="s"
    250         shift
    251     elif [ "x$1" = "x--no-optimize" ]; then
    252         OPTIMIZE="n"
    253         shift
    254     elif [ "x$1" = "x--android-root" ]; then
    255         shift
    256         ANDROID_ROOT="$1"
    257         shift
    258     elif [ "x$1" = "x--instruction-set-features" ]; then
    259         shift
    260         INSTRUCTION_SET_FEATURES="$1"
    261         shift
    262     elif [ "x$1" = "x--timeout" ]; then
    263         shift
    264         TIME_OUT_VALUE="$1"
    265         shift
    266     elif [ "x$1" = "x--" ]; then
    267         shift
    268         break
    269     elif [ "x$1" = "x--64" ]; then
    270         ISA="x86_64"
    271         GDB_SERVER="gdbserver64"
    272         DALVIKVM="dalvikvm64"
    273         LIBRARY_DIRECTORY="lib64"
    274         TEST_DIRECTORY="nativetest64"
    275         ARCHITECTURES_PATTERN="${ARCHITECTURES_64}"
    276         shift
    277     elif [ "x$1" = "x--pic-test" ]; then
    278         FLAGS="${FLAGS} -Xcompiler-option --compile-pic"
    279         COMPILE_FLAGS="${COMPILE_FLAGS} --compile-pic"
    280         shift
    281     elif [ "x$1" = "x--experimental" ]; then
    282         if [ "$#" -lt 2 ]; then
    283             echo "missing --experimental option" 1>&2
    284             exit 1
    285         fi
    286         EXPERIMENTAL="$EXPERIMENTAL $2"
    287         shift 2
    288     elif [ "x$1" = "x--external-log-tags" ]; then
    289         EXTERNAL_LOG_TAGS="y"
    290         shift
    291     elif [ "x$1" = "x--dry-run" ]; then
    292         DRY_RUN="y"
    293         shift
    294     elif [ "x$1" = "x--vdex" ]; then
    295         TEST_VDEX="y"
    296         shift
    297     elif [ "x$1" = "x--vdex-filter" ]; then
    298         shift
    299         option="$1"
    300         VDEX_FILTER="--compiler-filter=$option"
    301         shift
    302     elif [ "x$1" = "x--sync" ]; then
    303         SYNC_BEFORE_RUN="y"
    304         shift
    305     elif [ "x$1" = "x--profile" ]; then
    306         PROFILE="y"
    307         shift
    308     elif [ "x$1" = "x--random-profile" ]; then
    309         RANDOM_PROFILE="y"
    310         shift
    311     elif [ "x$1" = "x--set-stack-trace-dump-dir" ]; then
    312         SET_STACK_TRACE_DUMP_DIR="y"
    313         shift
    314     elif expr "x$1" : "x--" >/dev/null 2>&1; then
    315         echo "unknown $0 option: $1" 1>&2
    316         exit 1
    317     else
    318         break
    319     fi
    320 done
    321 
    322 mkdir_locations=""
    323 
    324 if [ "$USE_JVM" = "n" ]; then
    325     FLAGS="${FLAGS} ${ANDROID_FLAGS}"
    326     for feature in ${EXPERIMENTAL}; do
    327         FLAGS="${FLAGS} -Xexperimental:${feature} -Xcompiler-option --runtime-arg -Xcompiler-option -Xexperimental:${feature}"
    328         COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xexperimental:${feature}"
    329     done
    330 
    331     if [ "$SET_STACK_TRACE_DUMP_DIR" = "y" ]; then
    332         # Note that DEX_LOCATION is used as a proxy for tmpdir throughout this
    333         # file (it will be under the test specific folder).
    334         mkdir_locations="${mkdir_locations} $DEX_LOCATION/stack_traces"
    335         FLAGS="${FLAGS} -Xstacktracedir:$DEX_LOCATION/stack_traces"
    336         ARGS="${ARGS} --stack-trace-dir $DEX_LOCATION/stack_traces"
    337     fi
    338 fi
    339 
    340 if [ "x$1" = "x" ] ; then
    341   MAIN="Main"
    342 else
    343   MAIN="$1"
    344   shift
    345 fi
    346 
    347 if [ "$ZYGOTE" = "" ]; then
    348     if [ "$OPTIMIZE" = "y" ]; then
    349         if [ "$VERIFY" = "y" ]; then
    350             DEX_OPTIMIZE="-Xdexopt:verified"
    351         else
    352             DEX_OPTIMIZE="-Xdexopt:all"
    353         fi
    354         msg "Performing optimizations"
    355     else
    356         DEX_OPTIMIZE="-Xdexopt:none"
    357         msg "Skipping optimizations"
    358     fi
    359 
    360     if [ "$VERIFY" = "y" ]; then
    361         JVM_VERIFY_ARG="-Xverify:all"
    362         msg "Performing verification"
    363     elif [ "$VERIFY" = "s" ]; then
    364         JVM_VERIFY_ARG="Xverify:all"
    365         DEX_VERIFY="-Xverify:softfail"
    366         msg "Forcing verification to be soft fail"
    367     else # VERIFY = "n"
    368         DEX_VERIFY="-Xverify:none"
    369         JVM_VERIFY_ARG="-Xverify:none"
    370         msg "Skipping verification"
    371     fi
    372 fi
    373 
    374 msg "------------------------------"
    375 
    376 if [ "$DEBUGGER" = "y" ]; then
    377   # Use this instead for ddms and connect by running 'ddms':
    378   # DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_android_adb,server=y,suspend=y"
    379   # TODO: add a separate --ddms option?
    380 
    381   PORT=12345
    382   msg "Waiting for jdb to connect:"
    383   if [ "$HOST" = "n" ]; then
    384     msg "    adb forward tcp:$PORT tcp:$PORT"
    385   fi
    386   msg "    jdb -attach localhost:$PORT"
    387   DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=$PORT,server=y,suspend=y"
    388 fi
    389 
    390 if [ "$IS_JVMTI_TEST" = "y" ]; then
    391   plugin=libopenjdkjvmtid.so
    392   agent=libtiagentd.so
    393   lib=tiagentd
    394   if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
    395     agent=libtiagent.so
    396     plugin=libopenjdkjvmti.so
    397     lib=tiagent
    398   fi
    399 
    400   ARGS="${ARGS} ${lib}"
    401   if [[ "$USE_JVM" = "y" ]]; then
    402     FLAGS="${FLAGS} -agentpath:${ANDROID_HOST_OUT}/nativetest64/${agent}=${TEST_NAME},jvm"
    403   else
    404     FLAGS="${FLAGS} -agentpath:${agent}=${TEST_NAME},art"
    405     FLAGS="${FLAGS} -Xplugin:${plugin}"
    406     FLAGS="${FLAGS} -Xcompiler-option --debuggable"
    407     # Always make the compilation be debuggable.
    408     COMPILE_FLAGS="${COMPILE_FLAGS} --debuggable"
    409   fi
    410 fi
    411 
    412 if [[ "$JVMTI_STRESS" = "y" ]]; then
    413   plugin=libopenjdkjvmtid.so
    414   agent=libtistressd.so
    415   if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
    416     agent=libtistress.so
    417     plugin=libopenjdkjvmti.so
    418   fi
    419 
    420   # Just give it a default start so we can always add ',' to it.
    421   agent_args="jvmti-stress"
    422   if [[ "$JVMTI_REDEFINE_STRESS" = "y" ]]; then
    423     # We really cannot do this on RI so don't both passing it in that case.
    424     if [[ "$USE_JVM" = "n" ]]; then
    425       file_1=$(mktemp --tmpdir=${DEX_LOCATION})
    426       file_2=$(mktemp --tmpdir=${DEX_LOCATION})
    427       # TODO Remove need for DEXTER_BINARY!
    428       agent_args="${agent_args},redefine,${DEXTER_BINARY},${file_1},${file_2}"
    429     fi
    430   fi
    431   if [[ "$JVMTI_FIELD_STRESS" = "y" ]]; then
    432     agent_args="${agent_args},field"
    433   fi
    434   if [[ "$JVMTI_STEP_STRESS" = "y" ]]; then
    435     agent_args="${agent_args},step"
    436   fi
    437   if [[ "$JVMTI_TRACE_STRESS" = "y" ]]; then
    438     agent_args="${agent_args},trace"
    439   fi
    440   # In the future add onto this;
    441   if [[ "$USE_JVM" = "y" ]]; then
    442     FLAGS="${FLAGS} -agentpath:${ANDROID_HOST_OUT}/nativetest64/${agent}=${agent_args}"
    443   else
    444     FLAGS="${FLAGS} -agentpath:${agent}=${agent_args}"
    445     if [ "$IS_JVMTI_TEST" = "n" ]; then
    446       FLAGS="${FLAGS} -Xplugin:${plugin}"
    447       FLAGS="${FLAGS} -Xcompiler-option --debuggable"
    448       # Always make the compilation be debuggable.
    449       COMPILE_FLAGS="${COMPILE_FLAGS} --debuggable"
    450     fi
    451   fi
    452 fi
    453 
    454 if [ "$USE_JVM" = "y" ]; then
    455   export LD_LIBRARY_PATH=${ANDROID_HOST_OUT}/lib64
    456   # Xmx is necessary since we don't pass down the ART flags to JVM.
    457   # We pass the classes2 path whether it's used (src-multidex) or not.
    458   cmdline="${JAVA} ${DEBUGGER_OPTS} ${JVM_VERIFY_ARG} -Xmx256m -classpath classes:classes2 ${FLAGS} $MAIN $@ ${ARGS}"
    459   if [ "$DEV_MODE" = "y" ]; then
    460     echo $cmdline
    461   fi
    462   $cmdline
    463   exit
    464 fi
    465 
    466 
    467 if [ "$HAVE_IMAGE" = "n" ]; then
    468     if [ "${HOST}" = "y" ]; then
    469         framework="${ANDROID_HOST_OUT}/framework"
    470         bpath_suffix="-hostdex"
    471     else
    472         framework="${ANDROID_ROOT}/framework"
    473         bpath_suffix="-testdex"
    474     fi
    475     bpath="${framework}/core-libart${bpath_suffix}.jar"
    476     bpath="${bpath}:${framework}/core-oj${bpath_suffix}.jar"
    477     bpath="${bpath}:${framework}/conscrypt${bpath_suffix}.jar"
    478     bpath="${bpath}:${framework}/okhttp${bpath_suffix}.jar"
    479     bpath="${bpath}:${framework}/bouncycastle${bpath_suffix}.jar"
    480     # Pass down the bootclasspath
    481     FLAGS="${FLAGS} -Xbootclasspath:${bpath}"
    482     # Add 5 minutes to give some time to generate the boot image.
    483     TIME_OUT_VALUE=$((${TIME_OUT_VALUE} + 300))
    484     DALVIKVM_BOOT_OPT="-Ximage:/system/non-existant/core.art"
    485 else
    486     DALVIKVM_BOOT_OPT="-Ximage:${BOOT_IMAGE}"
    487 fi
    488 
    489 
    490 if [ "$USE_GDB" = "y" ]; then
    491   if [ "$HOST" = "n" ]; then
    492     GDB="$GDB_SERVER :5039"
    493   else
    494     if [ `uname` = "Darwin" ]; then
    495         GDB=lldb
    496         GDB_ARGS="$GDB_ARGS -- $DALVIKVM"
    497         DALVIKVM=
    498     else
    499         GDB=gdb
    500         GDB_ARGS="$GDB_ARGS --args $DALVIKVM"
    501         # Enable for Emacs "M-x gdb" support. TODO: allow extra gdb arguments on command line.
    502         # gdbargs="--annotate=3 $gdbargs"
    503     fi
    504   fi
    505 fi
    506 
    507 if [ "$INTERPRETER" = "y" ]; then
    508     INT_OPTS="-Xint"
    509     if [ "$VERIFY" = "y" ] ; then
    510       INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=quicken"
    511       COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=quicken"
    512     elif [ "$VERIFY" = "s" ]; then
    513       INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=extract"
    514       COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=extract"
    515       DEX_VERIFY="${DEX_VERIFY} -Xverify:softfail"
    516     else # VERIFY = "n"
    517       INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=assume-verified"
    518       COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=assume-verified"
    519       DEX_VERIFY="${DEX_VERIFY} -Xverify:none"
    520     fi
    521 fi
    522 
    523 if [ "$JIT" = "y" ]; then
    524     INT_OPTS="-Xusejit:true"
    525     if [ "$VERIFY" = "y" ] ; then
    526       INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=quicken"
    527       COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=quicken"
    528     else
    529       INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=assume-verified"
    530       COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=assume-verified"
    531       DEX_VERIFY="${DEX_VERIFY} -Xverify:none"
    532     fi
    533 fi
    534 
    535 JNI_OPTS="-Xjnigreflimit:512 -Xcheck:jni"
    536 
    537 COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xnorelocate"
    538 if [ "$RELOCATE" = "y" ]; then
    539     FLAGS="${FLAGS} -Xrelocate"
    540 else
    541     FLAGS="$FLAGS -Xnorelocate"
    542 fi
    543 
    544 if [ "$HOST" = "n" ]; then
    545   # Need to be root to query /data/dalvik-cache
    546   adb root > /dev/null
    547   adb wait-for-device
    548   ISA=
    549   ISA_adb_invocation=
    550   ISA_outcome=
    551   # We iterate a few times to workaround an adb issue. b/32655576
    552   for i in {1..10}; do
    553     ISA_adb_invocation=$(adb shell ls -F /data/dalvik-cache)
    554     ISA_outcome=$?
    555     ISA=$(echo $ISA_adb_invocation | grep -Ewo "${ARCHITECTURES_PATTERN}")
    556     if [ x"$ISA" != "x" ]; then
    557       break;
    558     fi
    559   done
    560   if [ x"$ISA" = "x" ]; then
    561     echo "Unable to determine architecture"
    562     # Print a few things for helping diagnosing the problem.
    563     echo "adb invocation output: $ISA_adb_invocation"
    564     echo "adb invocation outcome: $ISA_outcome"
    565     echo $(adb shell ls -F /data/dalvik-cache)
    566     echo $(adb shell ls /data/dalvik-cache)
    567     echo ${ARCHITECTURES_PATTERN}
    568     echo $(adb shell ls -F /data/dalvik-cache | grep -Ewo "${ARCHITECTURES_PATTERN}")
    569     exit 1
    570   fi
    571 fi
    572 
    573 # Prevent test from silently falling back to interpreter in no-prebuild mode. This happens
    574 # when DEX_LOCATION path is too long, because vdex/odex filename is constructed by taking
    575 # full path to dex, stripping leading '/', appending '@classes.vdex' and changing every
    576 # remaining '/' into '@'.
    577 if [ "$HOST" = "y" ]; then
    578   max_filename_size=$(getconf NAME_MAX $DEX_LOCATION)
    579 else
    580   # There is no getconf on device, fallback to standard value. See NAME_MAX in kernel <linux/limits.h>
    581   max_filename_size=255
    582 fi
    583 # Compute VDEX_NAME.
    584 DEX_LOCATION_STRIPPED="${DEX_LOCATION#/}"
    585 VDEX_NAME="${DEX_LOCATION_STRIPPED//\//@}@$TEST_NAME.jar (at] classes.vdex"
    586 if [ ${#VDEX_NAME} -gt $max_filename_size ]; then
    587     echo "Dex location path too long:"
    588     echo "$VDEX_NAME is ${#VDEX_NAME} character long, and the limit is $max_filename_size."
    589     exit 1
    590 fi
    591 
    592 profman_cmdline="true"
    593 dex2oat_cmdline="true"
    594 vdex_cmdline="true"
    595 mkdir_locations="${mkdir_locations} ${DEX_LOCATION}/dalvik-cache/$ISA"
    596 strip_cmdline="true"
    597 sync_cmdline="true"
    598 
    599 # PROFILE takes precedence over RANDOM_PROFILE, since PROFILE tests require a
    600 # specific profile to run properly.
    601 if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
    602   profman_cmdline="${ANDROID_ROOT}/bin/profman  \
    603     --apk=$DEX_LOCATION/$TEST_NAME.jar \
    604     --dex-location=$DEX_LOCATION/$TEST_NAME.jar"
    605   if [ -f $DEX_LOCATION/$TEST_NAME-ex.jar ]; then
    606     profman_cmdline="${profman_cmdline} \
    607       --apk=$DEX_LOCATION/$TEST_NAME-ex.jar \
    608       --dex-location=$DEX_LOCATION/$TEST_NAME-ex.jar"
    609   fi
    610   COMPILE_FLAGS="${COMPILE_FLAGS} --profile-file=$DEX_LOCATION/$TEST_NAME.prof"
    611   FLAGS="${FLAGS} -Xcompiler-option --profile-file=$DEX_LOCATION/$TEST_NAME.prof"
    612   if [ "$PROFILE" = "y" ]; then
    613     profman_cmdline="${profman_cmdline} --create-profile-from=$DEX_LOCATION/profile \
    614         --reference-profile-file=$DEX_LOCATION/$TEST_NAME.prof"
    615   else
    616     profman_cmdline="${profman_cmdline} --generate-test-profile=$DEX_LOCATION/$TEST_NAME.prof \
    617         --generate-test-profile-seed=0"
    618   fi
    619 fi
    620 
    621 if [ "$PREBUILD" = "y" ]; then
    622   mkdir_locations="${mkdir_locations} ${DEX_LOCATION}/oat/$ISA"
    623   if [ "$APP_IMAGE" = "y" ]; then
    624     # Pick a base that will force the app image to get relocated.
    625     app_image="--base=0x4000 --app-image-file=$DEX_LOCATION/oat/$ISA/$TEST_NAME.art"
    626   fi
    627 
    628   dex2oat_binary=dex2oatd
    629   if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
    630     dex2oat_binary=dex2oat
    631   fi
    632   dex2oat_cmdline="$INVOKE_WITH $ANDROID_ROOT/bin/$dex2oat_binary \
    633                       $COMPILE_FLAGS \
    634                       --boot-image=${BOOT_IMAGE} \
    635                       --dex-file=$DEX_LOCATION/$TEST_NAME.jar \
    636                       --oat-file=$DEX_LOCATION/oat/$ISA/$TEST_NAME.odex \
    637                       ${app_image} \
    638                       --instruction-set=$ISA"
    639   if [ "x$INSTRUCTION_SET_FEATURES" != "x" ] ; then
    640     dex2oat_cmdline="${dex2oat_cmdline} --instruction-set-features=${INSTRUCTION_SET_FEATURES}"
    641   fi
    642 
    643   # Add in a timeout. This is important for testing the compilation/verification time of
    644   # pathological cases.
    645   # Note: as we don't know how decent targets are (e.g., emulator), only do this on the host for
    646   #       now. We should try to improve this.
    647   #       The current value is rather arbitrary. run-tests should compile quickly.
    648   if [ "$HOST" != "n" ]; then
    649     # Use SIGRTMIN+2 to try to dump threads.
    650     # Use -k 1m to SIGKILL it a minute later if it hasn't ended.
    651     dex2oat_cmdline="timeout -k 1m -s SIGRTMIN+2 90s ${dex2oat_cmdline} --watchdog-timeout=60000"
    652   fi
    653   if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
    654     vdex_cmdline="${dex2oat_cmdline} ${VDEX_FILTER} --input-vdex=$DEX_LOCATION/oat/$ISA/$TEST_NAME.vdex --output-vdex=$DEX_LOCATION/oat/$ISA/$TEST_NAME.vdex"
    655   elif [ "$TEST_VDEX" = "y" ]; then
    656     vdex_cmdline="${dex2oat_cmdline} ${VDEX_FILTER} --input-vdex=$DEX_LOCATION/oat/$ISA/$TEST_NAME.vdex"
    657   elif [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
    658     vdex_cmdline="${dex2oat_cmdline} --input-vdex=$DEX_LOCATION/oat/$ISA/$TEST_NAME.vdex --output-vdex=$DEX_LOCATION/oat/$ISA/$TEST_NAME.vdex"
    659   fi
    660 fi
    661 
    662 if [ "$STRIP_DEX" = "y" ]; then
    663   strip_cmdline="zip --quiet --delete $DEX_LOCATION/$TEST_NAME.jar classes.dex"
    664 fi
    665 
    666 if [ "$SYNC_BEFORE_RUN" = "y" ]; then
    667   sync_cmdline="sync"
    668 fi
    669 
    670 DALVIKVM_ISA_FEATURES_ARGS=""
    671 if [ "x$INSTRUCTION_SET_FEATURES" != "x" ] ; then
    672   DALVIKVM_ISA_FEATURES_ARGS="-Xcompiler-option --instruction-set-features=${INSTRUCTION_SET_FEATURES}"
    673 fi
    674 
    675 # java.io.tmpdir can only be set at launch time.
    676 TMP_DIR_OPTION=""
    677 if [ "$HOST" = "n" ]; then
    678   TMP_DIR_OPTION="-Djava.io.tmpdir=/data/local/tmp"
    679 fi
    680 
    681 # We set DumpNativeStackOnSigQuit to false to avoid stressing libunwind.
    682 # b/27185632
    683 # b/24664297
    684 dalvikvm_cmdline="$INVOKE_WITH $GDB $ANDROID_ROOT/bin/$DALVIKVM \
    685                   $GDB_ARGS \
    686                   $FLAGS \
    687                   $DEX_VERIFY \
    688                   -XXlib:$LIB \
    689                   $PATCHOAT \
    690                   $DEX2OAT \
    691                   $DALVIKVM_ISA_FEATURES_ARGS \
    692                   $ZYGOTE \
    693                   $JNI_OPTS \
    694                   $INT_OPTS \
    695                   $DEBUGGER_OPTS \
    696                   $DALVIKVM_BOOT_OPT \
    697                   $TMP_DIR_OPTION \
    698                   -XX:DumpNativeStackOnSigQuit:false \
    699                   -cp $DEX_LOCATION/$TEST_NAME.jar$SECONDARY_DEX $MAIN $ARGS"
    700 
    701 # Remove whitespace.
    702 dex2oat_cmdline=$(echo $dex2oat_cmdline)
    703 dalvikvm_cmdline=$(echo $dalvikvm_cmdline)
    704 vdex_cmdline=$(echo $vdex_cmdline)
    705 profman_cmdline=$(echo $profman_cmdline)
    706 
    707 # Use an empty ASAN_OPTIONS to enable defaults.
    708 # Note: this is required as envsetup right now exports detect_leaks=0.
    709 RUN_TEST_ASAN_OPTIONS=""
    710 
    711 # Multiple shutdown leaks. b/38341789
    712 if [ "x$RUN_TEST_ASAN_OPTIONS" != "x" ] ; then
    713   RUN_TEST_ASAN_OPTIONS="${RUN_TEST_ASAN_OPTIONS}:"
    714 fi
    715 RUN_TEST_ASAN_OPTIONS="${RUN_TEST_ASAN_OPTIONS}detect_leaks=0"
    716 
    717 if [ "$HOST" = "n" ]; then
    718     adb root > /dev/null
    719     adb wait-for-device
    720     if [ "$QUIET" = "n" ]; then
    721       adb shell rm -rf $DEX_LOCATION
    722       adb shell mkdir -p $DEX_LOCATION
    723       adb push $TEST_NAME.jar $DEX_LOCATION
    724       adb push $TEST_NAME-ex.jar $DEX_LOCATION
    725       if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
    726         adb push profile $DEX_LOCATION
    727       fi
    728     else
    729       adb shell rm -r $DEX_LOCATION >/dev/null 2>&1
    730       adb shell mkdir -p $DEX_LOCATION >/dev/null 2>&1
    731       adb push $TEST_NAME.jar $DEX_LOCATION >/dev/null 2>&1
    732       adb push $TEST_NAME-ex.jar $DEX_LOCATION >/dev/null 2>&1
    733       if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
    734         adb push profile $DEX_LOCATION >/dev/null 2>&1
    735       fi
    736 
    737     fi
    738 
    739     LD_LIBRARY_PATH=/data/$TEST_DIRECTORY/art/$ISA
    740     if [ "$ANDROID_ROOT" != "/system" ]; then
    741       # Current default installation is dalvikvm 64bits and dex2oat 32bits,
    742       # so we can only use LD_LIBRARY_PATH when testing on a local
    743       # installation.
    744       LD_LIBRARY_PATH=$ANDROID_ROOT/$LIBRARY_DIRECTORY:$LD_LIBRARY_PATH
    745     fi
    746 
    747     # System libraries needed by libarttestd.so
    748     PUBLIC_LIBS=libart.so:libartd.so:libc++.so:libbacktrace.so:libbase.so:libnativehelper.so
    749 
    750     # Create a script with the command. The command can get longer than the longest
    751     # allowed adb command and there is no way to get the exit status from a adb shell
    752     # command. Dalvik cache is cleaned before running to make subsequent executions
    753     # of the script follow the same runtime path.
    754     cmdline="cd $DEX_LOCATION && \
    755              export ASAN_OPTIONS=$RUN_TEST_ASAN_OPTIONS && \
    756              export ANDROID_DATA=$DEX_LOCATION && \
    757              export ANDROID_ADDITIONAL_PUBLIC_LIBRARIES=$PUBLIC_LIBS && \
    758              export DEX_LOCATION=$DEX_LOCATION && \
    759              export ANDROID_ROOT=$ANDROID_ROOT && \
    760              rm -rf ${DEX_LOCATION}/dalvik-cache/ && \
    761              mkdir -p ${mkdir_locations} && \
    762              export LD_LIBRARY_PATH=$LD_LIBRARY_PATH && \
    763              export PATH=$ANDROID_ROOT/bin:$PATH && \
    764              $profman_cmdline && \
    765              $dex2oat_cmdline && \
    766              $vdex_cmdline && \
    767              $strip_cmdline && \
    768              $sync_cmdline && \
    769              $dalvikvm_cmdline"
    770 
    771     cmdfile=$(tempfile -p "cmd-" -s "-$TEST_NAME")
    772     echo "$cmdline" > $cmdfile
    773 
    774     if [ "$DEV_MODE" = "y" ]; then
    775       echo $cmdline
    776     fi
    777 
    778     if [ "$QUIET" = "n" ]; then
    779       adb push $cmdfile $DEX_LOCATION/cmdline.sh
    780     else
    781       adb push $cmdfile $DEX_LOCATION/cmdline.sh > /dev/null 2>&1
    782     fi
    783 
    784     if [ "$DRY_RUN" != "y" ]; then
    785       adb shell sh $DEX_LOCATION/cmdline.sh
    786     fi
    787 
    788     rm -f $cmdfile
    789 else
    790     # Host run.
    791     export ANDROID_PRINTF_LOG=brief
    792 
    793     # By default, and for prebuild dex2oat, we are interested in errors being logged. In dev mode
    794     # we want debug messages.
    795     if [ "$EXTERNAL_LOG_TAGS" = "n" ]; then
    796       if [ "$DEV_MODE" = "y" ]; then
    797           export ANDROID_LOG_TAGS='*:d'
    798       else
    799           export ANDROID_LOG_TAGS='*:e'
    800       fi
    801     fi
    802 
    803     export ANDROID_DATA="$DEX_LOCATION"
    804     export ANDROID_ROOT="${ANDROID_ROOT}"
    805     export LD_LIBRARY_PATH="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}:${ANDROID_ROOT}/${TEST_DIRECTORY}"
    806     export DYLD_LIBRARY_PATH="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}:${ANDROID_ROOT}/${TEST_DIRECTORY}"
    807     export PATH="$PATH:${ANDROID_ROOT}/bin"
    808 
    809     # Temporarily disable address space layout randomization (ASLR).
    810     # This is needed on the host so that the linker loads core.oat at the necessary address.
    811     export LD_USE_LOAD_BIAS=1
    812 
    813     cmdline="$dalvikvm_cmdline"
    814 
    815     if [ "$TIME_OUT" = "gdb" ]; then
    816       if [ `uname` = "Darwin" ]; then
    817         # Fall back to timeout on Mac.
    818         TIME_OUT="timeout"
    819       elif [ "$ISA" = "x86" ]; then
    820         # prctl call may fail in 32-bit on an older (3.2) 64-bit Linux kernel. Fall back to timeout.
    821         TIME_OUT="timeout"
    822       else
    823         # Check if gdb is available.
    824         gdb --eval-command="quit" > /dev/null 2>&1
    825         if [ $? != 0 ]; then
    826           # gdb isn't available. Fall back to timeout.
    827           TIME_OUT="timeout"
    828         fi
    829       fi
    830     fi
    831 
    832     if [ "$TIME_OUT" = "timeout" ]; then
    833       # Add timeout command if time out is desired.
    834       #
    835       # Note: We first send SIGRTMIN+2 (usually 36) to ART, which will induce a full thread dump
    836       #       before abort. However, dumping threads might deadlock, so we also use the "-k"
    837       #       option to definitely kill the child.
    838       cmdline="timeout -k 120s -s SIGRTMIN+2 ${TIME_OUT_VALUE}s $cmdline"
    839     fi
    840 
    841     if [ "$DEV_MODE" = "y" ]; then
    842       echo "mkdir -p ${mkdir_locations} && $profman_cmdline && $dex2oat_cmdline && $vdex_cmdline && $strip_cmdline && $sync_cmdline && $cmdline"
    843     fi
    844 
    845     cd $ANDROID_BUILD_TOP
    846 
    847     # Make sure we delete any existing compiler artifacts.
    848     # This enables tests to call the RUN script multiple times in a row
    849     # without worrying about interference.
    850     rm -rf ${DEX_LOCATION}/oat
    851     rm -rf ${DEX_LOCATION}/dalvik-cache/
    852 
    853     export ASAN_OPTIONS=$RUN_TEST_ASAN_OPTIONS
    854 
    855     mkdir -p ${mkdir_locations} || exit 1
    856     $profman_cmdline || { echo "Profman failed." >&2 ; exit 2; }
    857     $dex2oat_cmdline || { echo "Dex2oat failed." >&2 ; exit 2; }
    858     $vdex_cmdline || { echo "Dex2oat failed." >&2 ; exit 2; }
    859     $strip_cmdline || { echo "Strip failed." >&2 ; exit 3; }
    860     $sync_cmdline || { echo "Sync failed." >&2 ; exit 4; }
    861 
    862     # For running, we must turn off logging when dex2oat or patchoat are missing. Otherwise we use
    863     # the same defaults as for prebuilt: everything when --dev, otherwise errors and above only.
    864     if [ "$EXTERNAL_LOG_TAGS" = "n" ]; then
    865       if [ "$DEV_MODE" = "y" ]; then
    866           export ANDROID_LOG_TAGS='*:d'
    867       elif [ "$USE_DEX2OAT_AND_PATCHOAT" = "n" ]; then
    868           # All tests would log the error of failing dex2oat/patchoat. Be silent here and only
    869           # log fatal events.
    870           export ANDROID_LOG_TAGS='*:s'
    871       else
    872           # We are interested in LOG(ERROR) output.
    873           export ANDROID_LOG_TAGS='*:e'
    874       fi
    875     fi
    876 
    877     if [ "$DRY_RUN" = "y" ]; then
    878       exit 0
    879     fi
    880 
    881     if [ "$USE_GDB" = "y" ]; then
    882       # When running under gdb, we cannot do piping and grepping...
    883       $cmdline "$@"
    884     else
    885       if [ "$TIME_OUT" != "gdb" ]; then
    886         trap 'kill -INT -$pid' INT
    887         $cmdline "$@" 2>&1 & pid=$!
    888         wait $pid
    889         # Add extra detail if time out is enabled.
    890         if [ ${PIPESTATUS[0]} = 124 ] && [ "$TIME_OUT" = "timeout" ]; then
    891           echo -e "\e[91mTEST TIMED OUT!\e[0m" >&2
    892         fi
    893       else
    894         # With a thread dump that uses gdb if a timeout.
    895         trap 'kill -INT -$pid' INT
    896         $cmdline "$@" 2>&1 & pid=$!
    897         # Spawn a watcher process.
    898         ( sleep $TIME_OUT_VALUE && \
    899           echo "##### Thread dump using gdb on test timeout" && \
    900           ( gdb -q -p $pid --eval-command="info thread" --eval-command="thread apply all bt" \
    901                            --eval-command="call exit(124)" --eval-command=quit || \
    902             kill $pid )) 2> /dev/null & watcher=$!
    903         wait $pid
    904         test_exit_status=$?
    905         pkill -P $watcher 2> /dev/null # kill the sleep which will in turn end the watcher as well
    906         if [ $test_exit_status = 0 ]; then
    907           # The test finished normally.
    908           exit 0
    909         else
    910           # The test failed or timed out.
    911           if [ $test_exit_status = 124 ]; then
    912             # The test timed out.
    913             echo -e "\e[91mTEST TIMED OUT!\e[0m" >&2
    914           fi
    915         fi
    916       fi
    917     fi
    918 fi
    919