blob: 1c603d4fa78f2ec5262257555f77c23010719f22 [file] [log] [blame]
Elliott Hughes40ef99e2011-08-11 17:44:34 -07001# 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
Nicolas Geoffray70a998c2014-12-04 17:05:22 +000015# 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
Orion Hodson9763f2e2017-03-28 08:27:23 +010019# Globals
Orion Hodson9763f2e2017-03-28 08:27:23 +010020ART_BINARY=dalvikvm
21DELETE_ANDROID_DATA="no"
22LAUNCH_WRAPPER=
23LIBART=libart.so
24JIT_PROFILE="no"
25VERBOSE="no"
Nicolas Geoffrayc0c07852017-08-08 09:44:15 +010026CLEAN_OAT_FILES="yes"
Vladimir Markoafd44ea2017-07-14 13:52:02 +010027EXTRA_OPTIONS=()
Orion Hodson9763f2e2017-03-28 08:27:23 +010028
Calin Juravle64f45cb2017-03-16 19:58:26 -070029# Follow all sym links to get the program name.
30if [ z"$BASH_SOURCE" != z ]; then
31 PROG_NAME="$BASH_SOURCE"
32else
33 PROG_NAME="$0"
34fi
35while [ -h "$PROG_NAME" ]; do
36 # On Mac OS, readlink -f doesn't work.
37 PROG_NAME="$(readlink "$PROG_NAME")"
38done
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000039
Nicolas Geoffrayfc3c67a2014-07-02 14:57:53 +010040function find_libdir() {
Orion Hodson9763f2e2017-03-28 08:27:23 +010041 # Get the actual file, $1 is the ART_BINARY_PATH and may be a symbolic link.
Nicolas Geoffray70a998c2014-12-04 17:05:22 +000042 # Use realpath instead of readlink because Android does not have a readlink.
Orion Hodson9763f2e2017-03-28 08:27:23 +010043 if [[ "$(realpath "$1")" == *dalvikvm64 ]]; then
Nicolas Geoffrayfc3c67a2014-07-02 14:57:53 +010044 echo "lib64"
45 else
46 echo "lib"
47 fi
48}
49
Nicolas Geoffray49cda062017-04-21 13:08:25 +010050function replace_compiler_filter_with_quicken() {
51 ARGS_WITH_QUICKEN=("$@")
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +000052
53 found="false"
54 ((index=0))
55 while ((index <= $#)); do
Nicolas Geoffray49cda062017-04-21 13:08:25 +010056 what="${ARGS_WITH_QUICKEN[$index]}"
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +000057
58 case "$what" in
59 --compiler-filter=*)
Nicolas Geoffray49cda062017-04-21 13:08:25 +010060 ARGS_WITH_QUICKEN[$index]="--compiler-filter=quicken"
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +000061 found="true"
62 ;;
63 esac
64
65 ((index++))
66 shift
67 done
68 if [ "$found" != "true" ]; then
Nicolas Geoffray49cda062017-04-21 13:08:25 +010069 ARGS_WITH_QUICKEN=(-Xcompiler-option --compiler-filter=quicken "${ARGS_WITH_QUICKEN[@]}")
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +000070 fi
71}
72
Orion Hodson9763f2e2017-03-28 08:27:23 +010073function usage() {
74 cat 1>&2 <<EOF
75Usage: art [OPTIONS] [--] [ART_OPTIONS] CLASS
Nicolas Geoffrayf63a0a52014-09-02 15:24:25 +010076
Orion Hodson9763f2e2017-03-28 08:27:23 +010077Supported OPTIONS include:
78 --32 Use the 32-bit Android Runtime.
79 --64 Use the 64-bit Android Runtime.
80 --callgrind Launch the Android Runtime in callgrind.
81 -d Use the debug ART library (libartd.so).
82 --debug Equivalent to -d.
83 --gdb Launch the Android Runtime in gdb.
Alex Light84b92e02017-09-29 13:46:14 -070084 --gdbserver <comms> Launch the Android Runtime in gdbserver using the
85 supplied communication channel.
Orion Hodson9763f2e2017-03-28 08:27:23 +010086 --help Display usage message.
87 --invoke-with <program> Launch the Android Runtime in <program>.
88 --perf Launch the Android Runtime with perf recording.
89 --perf-report Launch the Android Runtime with perf recording with
90 report upon completion.
91 --profile Run with profiling, then run using profile data.
92 --verbose Run script verbosely.
Nicolas Geoffrayc0c07852017-08-08 09:44:15 +010093 --no-clean Don't cleanup oat directories.
Orion Hodson9763f2e2017-03-28 08:27:23 +010094
95The ART_OPTIONS are passed directly to the Android Runtime.
96
97Example:
98 art --32 -cp my_classes.dex MainClass
99
100Common errors:
101 1) Not having core.art available (see $ANDROID_BUILD_TOP/art/Android.mk).
102 eg m -j32 build-art-host
103 2) Not having boot.art available (see $ANDROID_BUILD_TOP/build/make/core/dex_preopt_libart_boot.mk)
104 eg m -j32 out/target/product/generic_x86_64/dex_bootjars/system/framework/x86_64/boot.art
105EOF
106}
107
108function clean_android_data() {
109 if [ "$DELETE_ANDROID_DATA" = "yes" ]; then
110 rm -rf $ANDROID_DATA
Nicolas Geoffrayf63a0a52014-09-02 15:24:25 +0100111 fi
Orion Hodson9763f2e2017-03-28 08:27:23 +0100112}
113
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100114# Given 'VAR1=VAL VAR2=VAL2 ... cmd arg1 arg2 ... argN' run the 'cmd' with the args
115# with the modified environment {VAR1=VAL,VAL2=,...}.
116#
117# Also prints the command to be run if verbose mode is enabled.
Orion Hodson9763f2e2017-03-28 08:27:23 +0100118function verbose_run() {
119 if [ "$VERBOSE" = "yes" ]; then
120 echo "$@"
121 fi
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100122
123 env "$@"
Orion Hodson9763f2e2017-03-28 08:27:23 +0100124}
125
Nicolas Geoffray162a5702017-08-04 09:28:32 +0000126# Parse a colon-separated list into an array (e.g. "foo.dex:bar.dex" -> (foo.dex bar.dex))
127PARSE_CLASSPATH_RESULT=() # Return value will be here due to shell limitations.
128parse_classpath() {
129 local cp="$1"
130 local oldifs=$IFS
131
132 local cp_array
133 cp_array=()
134
135 IFS=":"
136 for part in $cp; do
137 cp_array+=("$part")
138 done
139 IFS=$oldifs
140
141 PARSE_CLASSPATH_RESULT=("${cp_array[@]}")
142}
143
144# Sets 'PARSE_CLASSPATH_RESULT' to an array of class path dex files.
145# e.g. (-cp foo/classes.dex:bar/classes.dex) -> (foo/classes.dex bar/classes.dex)
146find_cp_in_args() {
147 local found="false"
148 local index=0
149 local what
150
151 while [[ $# -gt 0 ]]; do
152 case "$1" in
153 -cp|-classpath)
154 parse_classpath "$2"
155 # Sets 'PARSE_CLASSPATH_RESULT' to an array of class path dex files.
156 # Subsequent parses will overwrite the preceding.
157 shift
158 ;;
159 esac
160 shift
161 done
162}
163
164# Delete the 'oat' directories relative to the classpath's dex files.
165# e.g. (foo/classes.dex bar/classes.dex) would delete (foo/oat bar/oat) directories.
166cleanup_oat_directory() {
167 local classpath
168 classpath=("$@")
169
170 local dirpath
171
172 for path in "${classpath[@]}"; do
173 dirpath="$(dirname "$path")"
174 [[ -d "$dirpath" ]] && verbose_run rm -rf "$dirpath/oat"
175 done
176}
177
178# Parse -cp <CP>, -classpath <CP>, and $CLASSPATH to find the dex files.
179# Each dex file's directory will have an 'oat' file directory, delete it.
180# Input: Command line arguments to the art script.
181# e.g. -cp foo/classes.dex:bar/classes.dex would delete (foo/oat bar/oat) directories.
182cleanup_oat_directory_for_classpath() {
Nicolas Geoffrayc0c07852017-08-08 09:44:15 +0100183 if [ "$CLEAN_OAT_FILES" = "yes" ]; then
184 # First try: Use $CLASSPATH environment variable.
185 parse_classpath "$CLASSPATH"
186 # Second try: Look for latest -cp or -classpath arg which will take precedence.
187 find_cp_in_args "$@"
Nicolas Geoffray162a5702017-08-04 09:28:32 +0000188
Nicolas Geoffrayc0c07852017-08-08 09:44:15 +0100189 cleanup_oat_directory "${PARSE_CLASSPATH_RESULT[@]}"
190 fi
Nicolas Geoffray162a5702017-08-04 09:28:32 +0000191}
192
Igor Murashkind54ac262017-07-26 11:16:23 -0700193# Attempt to find $ANDROID_ROOT/framework/<isa>/core.art' without knowing what <isa> is.
194function check_if_boot_image_file_exists() {
195 local image_location_dir="$1"
196 local image_location_name="$2"
197
198 # Expand image_files to a list of existing image files on the disk.
199 # If no such files exist, it expands to single element 'dir/*/file' with a literal '*'.
200 local image_files
201 image_files=("$image_location_dir"/*/"$image_location_name") # avoid treating "*" as literal.
202
203 # Array always has at least 1 element. Test explicitly whether the file exists.
204 [[ -e "${image_files[0]}" ]]
205}
206
Igor Murashkin7fef4eb2017-07-14 15:45:47 -0700207# Automatically find the boot image location. It uses core.art by default.
208# On a real device, it might only have a boot.art, so use that instead when core.art does not exist.
209function detect_boot_image_location() {
210 local image_location_dir="$ANDROID_ROOT/framework"
211 local image_location_name="core.art"
212
Igor Murashkind54ac262017-07-26 11:16:23 -0700213 # If there are no existing core.art, try to find boot.art.
214 # If there is no boot.art then leave it as-is, assumes -Ximage is explicitly used.
215 # Otherwise let dalvikvm give the error message about an invalid image file.
216 if ! check_if_boot_image_file_exists "$image_location_dir" "core.art" && \
217 check_if_boot_image_file_exists "$image_location_dir" "boot.art"; then
Igor Murashkin7fef4eb2017-07-14 15:45:47 -0700218 image_location_name="boot.art"
219 fi
220
221 local image_location="$image_location_dir/$image_location_name"
222 echo "$image_location"
223}
224
Nicolas Geoffrayffda8b82017-10-06 13:48:08 +0100225# If android logging is not explicitly set, only print warnings and errors.
226if [ -z "$ANDROID_LOG_TAGS" ]; then
227 ANDROID_LOG_TAGS='*:w'
228fi
229
Nicolas Geoffray162a5702017-08-04 09:28:32 +0000230# Runs dalvikvm, returns its exit code.
231# (Oat directories are cleaned up in between runs)
Orion Hodson9763f2e2017-03-28 08:27:23 +0100232function run_art() {
Igor Murashkin7fef4eb2017-07-14 15:45:47 -0700233 local image_location="$(detect_boot_image_location)"
Nicolas Geoffray162a5702017-08-04 09:28:32 +0000234 local ret
Igor Murashkin7fef4eb2017-07-14 15:45:47 -0700235
Nicolas Geoffray162a5702017-08-04 09:28:32 +0000236 # First cleanup any left-over 'oat' files from the last time dalvikvm was run.
237 cleanup_oat_directory_for_classpath "$@"
238 # Run dalvikvm.
Nicolas Geoffrayffda8b82017-10-06 13:48:08 +0100239 verbose_run ANDROID_DATA="$ANDROID_DATA" \
240 ANDROID_ROOT="$ANDROID_ROOT" \
241 LD_LIBRARY_PATH="$LD_LIBRARY_PATH" \
242 PATH="$ANDROID_ROOT/bin:$PATH" \
243 LD_USE_LOAD_BIAS=1 \
244 ANDROID_LOG_TAGS="$ANDROID_LOG_TAGS" \
245 $LAUNCH_WRAPPER $ART_BINARY_PATH $lib \
246 -XXlib:"$LIBART" \
247 -Xnorelocate \
248 -Ximage:"$image_location" \
Orion Hodson9763f2e2017-03-28 08:27:23 +0100249 "$@"
Nicolas Geoffray162a5702017-08-04 09:28:32 +0000250 ret=$?
251
252 # Avoid polluting disk with 'oat' files after dalvikvm has finished.
253 cleanup_oat_directory_for_classpath "$@"
254
255 # Forward exit code of dalvikvm.
256 return $ret
Orion Hodson9763f2e2017-03-28 08:27:23 +0100257}
258
259while [[ "$1" = "-"* ]]; do
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100260 case "$1" in
Orion Hodson9763f2e2017-03-28 08:27:23 +0100261 --)
262 # No more arguments for this script.
263 shift
264 break
265 ;;
266 --32)
267 ART_BINARY=dalvikvm32
268 ;;
269 --64)
270 ART_BINARY=dalvikvm64
271 ;;
272 --callgrind)
273 LAUNCH_WRAPPER="valgrind --tool=callgrind"
274 ;;
275 -d)
276 ;& # Fallthrough
277 --debug)
278 LIBART="libartd.so"
Andreas Gampe1c5b42f2017-06-15 18:20:45 -0700279 # Expect that debug mode wants all checks.
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100280 EXTRA_OPTIONS+=(-XX:SlowDebug=true)
Orion Hodson9763f2e2017-03-28 08:27:23 +0100281 ;;
Alex Light84b92e02017-09-29 13:46:14 -0700282 --gdbserver)
283 LAUNCH_WRAPPER="gdbserver $2"
284 shift
285 ;;
Orion Hodson9763f2e2017-03-28 08:27:23 +0100286 --gdb)
287 LIBART="libartd.so"
288 LAUNCH_WRAPPER="gdb --args"
289 ;;
290 --help)
291 usage
292 exit 0
293 ;;
294 --invoke-with)
295 LAUNCH_WRAPPER=$2
296 shift
297 ;;
298 --perf)
299 PERF="record"
300 ;;
301 --perf-report)
302 PERF="report"
303 ;;
304 --profile)
305 JIT_PROFILE="yes"
306 ;;
307 --verbose)
308 VERBOSE="yes"
309 ;;
Nicolas Geoffrayc0c07852017-08-08 09:44:15 +0100310 --no-clean)
311 CLEAN_OAT_FILES="no"
312 ;;
Orion Hodson9763f2e2017-03-28 08:27:23 +0100313 --*)
314 echo "unknown option: $1" 1>&2
315 usage
316 exit 1
317 ;;
318 *)
319 break
320 ;;
321 esac
322 shift
Nicolas Geoffrayf63a0a52014-09-02 15:24:25 +0100323done
324
Orion Hodson9763f2e2017-03-28 08:27:23 +0100325if [ $# -eq 0 ]; then
326 usage
327 exit 1
328fi
329
Brian Carlstrom87bb26f2014-09-08 11:13:47 -0700330PROG_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
331ANDROID_ROOT=$PROG_DIR/..
Orion Hodson9763f2e2017-03-28 08:27:23 +0100332ART_BINARY_PATH=$ANDROID_ROOT/bin/$ART_BINARY
Brian Carlstrom87bb26f2014-09-08 11:13:47 -0700333
Orion Hodson9763f2e2017-03-28 08:27:23 +0100334if [ ! -x "$ART_BINARY_PATH" ]; then
335 cat 1>&2 <<EOF
336Android Runtime not found: $ART_BINARY_PATH
337This script should be in the same directory as the Android Runtime ($ART_BINARY).
338EOF
339 exit 1
340fi
341
342LIBDIR="$(find_libdir $ART_BINARY_PATH)"
343LD_LIBRARY_PATH=$ANDROID_ROOT/$LIBDIR
Orion Hodson9763f2e2017-03-28 08:27:23 +0100344
Nicolas Geoffray70a998c2014-12-04 17:05:22 +0000345# If ANDROID_DATA is the system ANDROID_DATA or is not set, use our own,
346# and ensure we delete it at the end.
347if [ "$ANDROID_DATA" = "/data" ] || [ "$ANDROID_DATA" = "" ]; then
Igor Murashkin7fef4eb2017-07-14 15:45:47 -0700348 if [[ $PWD != / ]]; then
349 ANDROID_DATA="$PWD/android-data$$"
350 else
351 # Use /data/local/tmp when running this from adb shell, since it starts out in /
352 # by default.
353 ANDROID_DATA="$ANDROID_DATA/local/tmp/android-data$$"
354 fi
Igor Murashkind54ac262017-07-26 11:16:23 -0700355 mkdir -p "$ANDROID_DATA"
Orion Hodson9763f2e2017-03-28 08:27:23 +0100356 DELETE_ANDROID_DATA="yes"
Nicolas Geoffray70a998c2014-12-04 17:05:22 +0000357fi
358
Orion Hodson9763f2e2017-03-28 08:27:23 +0100359if [ "$PERF" != "" ]; then
360 LAUNCH_WRAPPER="perf record -g -o $ANDROID_DATA/perf.data -e cycles:u $LAUNCH_WRAPPER"
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100361 EXTRA_OPTIONS+=(-Xcompiler-option --generate-debug-info)
Nicolas Geoffraye099a612014-12-12 13:52:00 +0000362fi
363
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000364if [ "$JIT_PROFILE" = "yes" ]; then
365 # Create the profile. The runtime expects profiles to be created before
366 # execution.
367 PROFILE_PATH="$ANDROID_DATA/primary.prof"
Igor Murashkind54ac262017-07-26 11:16:23 -0700368 touch "$PROFILE_PATH"
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000369
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100370 # Replace the compiler filter with quicken so that we
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000371 # can capture the profile.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100372 ARGS_WITH_QUICKEN=
373 replace_compiler_filter_with_quicken "$@"
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000374
375 run_art -Xjitsaveprofilinginfo \
376 -Xps-min-methods-to-save:1 \
377 -Xps-min-classes-to-save:1 \
378 -Xps-min-notification-before-wake:10 \
379 -Xps-profile-path:$PROFILE_PATH \
380 -Xusejit:true \
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100381 "${ARGS_WITH_QUICKEN[@]}" \
Igor Murashkin11942442017-07-20 11:08:34 -0700382 &> "$ANDROID_DATA/profile_gen.log"
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000383 EXIT_STATUS=$?
384
385 if [ $EXIT_STATUS != 0 ]; then
Igor Murashkind54ac262017-07-26 11:16:23 -0700386 echo "Profile run failed: " >&2
387 cat "$ANDROID_DATA/profile_gen.log" >&2
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000388 clean_android_data
389 exit $EXIT_STATUS
390 fi
391
Igor Murashkind54ac262017-07-26 11:16:23 -0700392 # Wipe dalvik-cache so that a subsequent run_art must regenerate it.
393 # Leave $ANDROID_DATA intact since it contains our profile file.
394 rm -rf "$ANDROID_DATA/dalvik-cache"
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000395
396 # Append arguments so next invocation of run_art uses the profile.
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100397 EXTRA_OPTIONS+=(-Xcompiler-option --profile-file="$PROFILE_PATH")
Nicolas Geoffray9d7baf42017-04-19 09:01:29 +0000398fi
399
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100400# Protect additional arguments in quotes to preserve whitespaces (used by
401# run-jdwp-test.sh when running on device), '$' (may be used as part of
402# classpath) and other special characters when evaluated.
403EXTRA_OPTIONS+=("$@")
Calin Juravle64f45cb2017-03-16 19:58:26 -0700404
Vladimir Markoafd44ea2017-07-14 13:52:02 +0100405run_art "${EXTRA_OPTIONS[@]}"
Orion Hodson9763f2e2017-03-28 08:27:23 +0100406EXIT_STATUS=$?
Calin Juravleaa980612014-10-20 15:58:57 +0100407
Orion Hodson9763f2e2017-03-28 08:27:23 +0100408if [ "$PERF" != "" ]; then
409 if [ "$PERF" = report ]; then
Calin Juravleaa980612014-10-20 15:58:57 +0100410 perf report -i $ANDROID_DATA/perf.data
411 fi
412 echo "Perf data saved in: $ANDROID_DATA/perf.data"
413else
Orion Hodson9763f2e2017-03-28 08:27:23 +0100414 # Perf output is placed under $ANDROID_DATA so not cleaned when perf options used.
415 clean_android_data
Calin Juravleaa980612014-10-20 15:58:57 +0100416fi
417
Nicolas Geoffray89c4e282014-03-24 09:33:30 +0000418exit $EXIT_STATUS