Home | History | Annotate | Download | only in tools
      1 # Copyright (C) 2011 The Android Open Source Project
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 
     15 # This script is used on host and device. It uses a common subset
     16 # shell dialect that should work on the host (e.g. bash), and
     17 # Android (e.g. mksh).
     18 
     19 function follow_links() {
     20   if [ z"$BASH_SOURCE" != z ]; then
     21     file="$BASH_SOURCE"
     22   else
     23     file="$0"
     24   fi
     25   while [ -h "$file" ]; do
     26     # On Mac OS, readlink -f doesn't work.
     27     file="$(readlink "$file")"
     28   done
     29   echo "$file"
     30 }
     31 
     32 function find_libdir() {
     33   # Use realpath instead of readlink because Android does not have a readlink.
     34   if [ "$(realpath "$ANDROID_ROOT/bin/$DALVIKVM")" = "$(realpath "$ANDROID_ROOT/bin/dalvikvm64")" ]; then
     35     echo "lib64"
     36   else
     37     echo "lib"
     38   fi
     39 }
     40 
     41 invoke_with=
     42 DALVIKVM=dalvikvm
     43 LIBART=libart.so
     44 
     45 while true; do
     46   if [ "$1" = "--invoke-with" ]; then
     47     shift
     48     invoke_with="$invoke_with $1"
     49     shift
     50   elif [ "$1" = "-d" ]; then
     51     LIBART="libartd.so"
     52     shift
     53   elif [ "$1" = "--32" ]; then
     54     DALVIKVM=dalvikvm32
     55     shift
     56   elif [ "$1" = "--64" ]; then
     57     DALVIKVM=dalvikvm64
     58     shift
     59   elif [ "$1" = "--perf" ]; then
     60     PERF="record"
     61     shift
     62   elif [ "$1" = "--perf-report" ]; then
     63     PERF="report"
     64     shift
     65   elif expr "$1" : "--" >/dev/null 2>&1; then
     66     echo "unknown option: $1" 1>&2
     67     exit 1
     68   else
     69     break
     70   fi
     71 done
     72 
     73 PROG_NAME="$(follow_links)"
     74 PROG_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
     75 ANDROID_ROOT=$PROG_DIR/..
     76 LIBDIR=$(find_libdir)
     77 LD_LIBRARY_PATH=$ANDROID_ROOT/$LIBDIR
     78 
     79 DELETE_ANDROID_DATA=false
     80 # If ANDROID_DATA is the system ANDROID_DATA or is not set, use our own,
     81 # and ensure we delete it at the end.
     82 if [ "$ANDROID_DATA" = "/data" ] || [ "$ANDROID_DATA" = "" ]; then
     83   ANDROID_DATA=$PWD/android-data$$
     84   mkdir -p $ANDROID_DATA/dalvik-cache/{arm,arm64,x86,x86_64}
     85   DELETE_ANDROID_DATA=true
     86 fi
     87 
     88 if [ z"$PERF" != z ]; then
     89   invoke_with="perf record -o $ANDROID_DATA/perf.data -e cycles:u $invoke_with"
     90 fi
     91 
     92 ANDROID_DATA=$ANDROID_DATA \
     93   ANDROID_ROOT=$ANDROID_ROOT \
     94   LD_LIBRARY_PATH=$LD_LIBRARY_PATH \
     95   PATH=$ANDROID_ROOT/bin:$PATH \
     96   $invoke_with $ANDROID_ROOT/bin/$DALVIKVM $lib \
     97     -XXlib:$LIBART \
     98     -Xnorelocate \
     99     -Ximage:$ANDROID_ROOT/framework/core.art \
    100     -Xcompiler-option --generate-debug-info \
    101     "$@"
    102 
    103 EXIT_STATUS=$?
    104 
    105 if [ z"$PERF" != z ]; then
    106   if [ z"$PERF" = zreport ]; then
    107     perf report -i $ANDROID_DATA/perf.data
    108   fi
    109   echo "Perf data saved in: $ANDROID_DATA/perf.data"
    110 else
    111   if [ "$DELETE_ANDROID_DATA" = "true" ]; then
    112     rm -rf $ANDROID_DATA
    113   fi
    114 fi
    115 
    116 exit $EXIT_STATUS
    117