• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3# Runner for an individual run-test.
4
5readonly local_path=$(dirname "$0")
6source "${local_path}/apex-bootclasspath-utils.sh"
7
8# Check how many colors the terminal can display.
9ncolors=$(tput colors 2>/dev/null)
10
11# Check that stdout is connected to a terminal and that we have at least 1 color.
12# This ensures that if the stdout is not connected to a terminal and instead
13# the stdout will be used for a log, it will not append the color characters.
14if [[ -t 1 && ${ncolors} && ${ncolors} -ge 1 ]]; then
15  bold_red="$(tput bold)$(tput setaf 1)"
16fi
17
18readonly bold_red
19
20error_msg() {
21  echo -e "${bold_red}ERROR: $@" 1>&2
22}
23
24if [[ -z "$ANDROID_BUILD_TOP" ]]; then
25  error_msg 'ANDROID_BUILD_TOP environment variable is empty; did you forget to run `lunch`?'
26  exit 1
27fi
28
29msg() {
30    if [ "$QUIET" = "n" ]; then
31        echo "$@"
32    fi
33}
34
35ANDROID_ROOT="/system"
36ANDROID_ART_ROOT="/apex/com.android.art"
37ANDROID_I18N_ROOT="/apex/com.android.i18n"
38ANDROID_TZDATA_ROOT="/apex/com.android.tzdata"
39ARCHITECTURES_32="(arm|x86|none)"
40ARCHITECTURES_64="(arm64|x86_64|none)"
41ARCHITECTURES_PATTERN="${ARCHITECTURES_32}"
42GET_DEVICE_ISA_BITNESS_FLAG="--32"
43BOOT_IMAGE=""
44CHROOT=
45COMPILE_FLAGS=""
46DALVIKVM="dalvikvm32"
47DEBUGGER="n"
48WITH_AGENT=()
49DEBUGGER_AGENT=""
50WRAP_DEBUGGER_AGENT="n"
51DEV_MODE="n"
52DEX2OAT_NDEBUG_BINARY="dex2oat32"
53DEX2OAT_DEBUG_BINARY="dex2oatd32"
54EXPERIMENTAL=""
55FALSE_BIN="false"
56FLAGS=""
57ANDROID_FLAGS=""
58GDB=""
59GDB_ARGS=""
60GDB_DEX2OAT=""
61GDB_DEX2OAT_ARGS=""
62GDBSERVER_DEVICE="gdbserver"
63GDBSERVER_HOST="gdbserver"
64HAVE_IMAGE="y"
65HOST="n"
66BIONIC="n"
67CREATE_ANDROID_ROOT="n"
68USE_ZIPAPEX="n"
69ZIPAPEX_LOC=""
70USE_EXTRACTED_ZIPAPEX="n"
71EXTRACTED_ZIPAPEX_LOC=""
72INTERPRETER="n"
73JIT="n"
74INVOKE_WITH=""
75IS_JVMTI_TEST="n"
76ADD_LIBDIR_ARGUMENTS="n"
77SUFFIX64=""
78ISA=x86
79LIBRARY_DIRECTORY="lib"
80TEST_DIRECTORY="nativetest"
81MAIN=""
82OPTIMIZE="y"
83PREBUILD="y"
84QUIET="n"
85RELOCATE="n"
86SECONDARY_DEX=""
87TIME_OUT="n"  # "n" (disabled), "timeout" (use timeout), "gdb" (use gdb)
88TIMEOUT_DUMPER=signal_dumper
89# Values in seconds.
90TIME_OUT_EXTRA=0
91TIME_OUT_VALUE=
92USE_GDB="n"
93USE_GDBSERVER="n"
94GDBSERVER_PORT=":5039"
95USE_GDB_DEX2OAT="n"
96USE_JVM="n"
97USE_JVMTI="n"
98VERIFY="y" # y=yes,n=no,s=softfail
99ZYGOTE=""
100DEX_VERIFY=""
101INSTRUCTION_SET_FEATURES=""
102ARGS=""
103VDEX_ARGS=""
104EXTERNAL_LOG_TAGS="n" # if y respect externally set ANDROID_LOG_TAGS.
105DRY_RUN="n" # if y prepare to run the test but don't run it.
106TEST_VDEX="n"
107TEST_DEX2OAT_DM="n"
108TEST_RUNTIME_DM="n"
109TEST_IS_NDEBUG="n"
110APP_IMAGE="y"
111SECONDARY_APP_IMAGE="y"
112SECONDARY_CLASS_LOADER_CONTEXT=""
113SECONDARY_COMPILATION="y"
114JVMTI_STRESS="n"
115JVMTI_STEP_STRESS="n"
116JVMTI_FIELD_STRESS="n"
117JVMTI_TRACE_STRESS="n"
118JVMTI_REDEFINE_STRESS="n"
119PROFILE="n"
120RANDOM_PROFILE="n"
121# The normal dex2oat timeout.
122DEX2OAT_TIMEOUT="300" # 5 mins
123# The *hard* timeout where we really start trying to kill the dex2oat.
124DEX2OAT_RT_TIMEOUT="360" # 6 mins
125CREATE_RUNNER="n"
126
127# if "y", run 'sync' before dalvikvm to make sure all files from
128# build step (e.g. dex2oat) were finished writing.
129SYNC_BEFORE_RUN="n"
130
131# When running a debug build, we want to run with all checks.
132ANDROID_FLAGS="${ANDROID_FLAGS} -XX:SlowDebug=true"
133# The same for dex2oatd, both prebuild and runtime-driven.
134ANDROID_FLAGS="${ANDROID_FLAGS} -Xcompiler-option --runtime-arg -Xcompiler-option -XX:SlowDebug=true"
135COMPILER_FLAGS="${COMPILER_FLAGS} --runtime-arg -XX:SlowDebug=true"
136
137# Let the compiler and runtime know that we are running tests.
138COMPILE_FLAGS="${COMPILE_FLAGS} --compile-art-test"
139ANDROID_FLAGS="${ANDROID_FLAGS} -Xcompiler-option --compile-art-test"
140
141while true; do
142    if [ "x$1" = "x--quiet" ]; then
143        QUIET="y"
144        shift
145    elif [ "x$1" = "x--dex2oat-rt-timeout" ]; then
146        shift
147        if [ "x$1" = "x" ]; then
148            error_msg "$0 missing argument to --dex2oat-rt-timeout"
149            exit 1
150        fi
151        DEX2OAT_RT_TIMEOUT="$1"
152        shift
153    elif [ "x$1" = "x--dex2oat-timeout" ]; then
154        shift
155        if [ "x$1" = "x" ]; then
156            error_msg "$0 missing argument to --dex2oat-timeout"
157            exit 1
158        fi
159        DEX2OAT_TIMEOUT="$1"
160        shift
161    elif [ "x$1" = "x--jvmti" ]; then
162        USE_JVMTI="y"
163        IS_JVMTI_TEST="y"
164        # Secondary images block some tested behavior.
165        SECONDARY_APP_IMAGE="n"
166        shift
167    elif [ "x$1" = "x--add-libdir-argument" ]; then
168        ADD_LIBDIR_ARGUMENTS="y"
169        shift
170    elif [ "x$1" = "x-O" ]; then
171        TEST_IS_NDEBUG="y"
172        shift
173    elif [ "x$1" = "x--lib" ]; then
174        shift
175        if [ "x$1" = "x" ]; then
176            error_msg "$0 missing argument to --lib"
177            exit 1
178        fi
179        LIB="$1"
180        shift
181    elif [ "x$1" = "x--gc-stress" ]; then
182        # Give an extra 20 mins if we are gc-stress.
183        TIME_OUT_EXTRA=$((${TIME_OUT_EXTRA} + 1200))
184        shift
185    elif [ "x$1" = "x--testlib" ]; then
186        shift
187        if [ "x$1" = "x" ]; then
188            error_msg "$0 missing argument to --testlib"
189            exit 1
190        fi
191        ARGS="${ARGS} $1"
192        shift
193    elif [ "x$1" = "x--args" ]; then
194        shift
195        if [ "x$1" = "x" ]; then
196            error_msg "$0 missing argument to --args"
197            exit 1
198        fi
199        ARGS="${ARGS} $1"
200        shift
201    elif [ "x$1" = "x--compiler-only-option" ]; then
202        shift
203        option="$1"
204        COMPILE_FLAGS="${COMPILE_FLAGS} $option"
205        shift
206    elif [ "x$1" = "x-Xcompiler-option" ]; then
207        shift
208        option="$1"
209        FLAGS="${FLAGS} -Xcompiler-option $option"
210        COMPILE_FLAGS="${COMPILE_FLAGS} $option"
211        shift
212    elif [ "x$1" = "x--create-runner" ]; then
213        CREATE_RUNNER="y"
214        shift
215    elif [ "x$1" = "x--android-runtime-option" ]; then
216        shift
217        option="$1"
218        ANDROID_FLAGS="${ANDROID_FLAGS} $option"
219        shift
220    elif [ "x$1" = "x--runtime-option" ]; then
221        shift
222        option="$1"
223        FLAGS="${FLAGS} $option"
224        if [ "x$option" = "x-Xmethod-trace" ]; then
225            # Method tracing can slow some tests down a lot.
226            TIME_OUT_EXTRA=$((${TIME_OUT_EXTRA} + 1200))
227        fi
228        shift
229    elif [ "x$1" = "x--boot" ]; then
230        shift
231        BOOT_IMAGE="$1"
232        shift
233    elif [ "x$1" = "x--relocate" ]; then
234        RELOCATE="y"
235        shift
236    elif [ "x$1" = "x--no-relocate" ]; then
237        RELOCATE="n"
238        shift
239    elif [ "x$1" = "x--prebuild" ]; then
240        PREBUILD="y"
241        shift
242    elif [ "x$1" = "x--compact-dex-level" ]; then
243        shift
244        COMPILE_FLAGS="${COMPILE_FLAGS} --compact-dex-level=$1"
245        shift
246    elif [ "x$1" = "x--jvmti-redefine-stress" ]; then
247        # APP_IMAGE doesn't really work with jvmti redefine stress
248        USE_JVMTI="y"
249        APP_IMAGE="n"
250        SECONDARY_APP_IMAGE="n"
251        JVMTI_STRESS="y"
252        JVMTI_REDEFINE_STRESS="y"
253        shift
254    elif [ "x$1" = "x--jvmti-step-stress" ]; then
255        USE_JVMTI="y"
256        JVMTI_STRESS="y"
257        JVMTI_STEP_STRESS="y"
258        shift
259    elif [ "x$1" = "x--jvmti-field-stress" ]; then
260        USE_JVMTI="y"
261        JVMTI_STRESS="y"
262        JVMTI_FIELD_STRESS="y"
263        shift
264    elif [ "x$1" = "x--jvmti-trace-stress" ]; then
265        USE_JVMTI="y"
266        JVMTI_STRESS="y"
267        JVMTI_TRACE_STRESS="y"
268        shift
269    elif [ "x$1" = "x--no-app-image" ]; then
270        APP_IMAGE="n"
271        shift
272    elif [ "x$1" = "x--no-secondary-app-image" ]; then
273        SECONDARY_APP_IMAGE="n"
274        shift
275    elif [ "x$1" = "x--secondary-class-loader-context" ]; then
276        shift
277        SECONDARY_CLASS_LOADER_CONTEXT="$1"
278        shift
279    elif [ "x$1" = "x--no-secondary-compilation" ]; then
280        SECONDARY_COMPILATION="n"
281        shift
282    elif [ "x$1" = "x--host" ]; then
283        HOST="y"
284        ANDROID_ROOT="${ANDROID_HOST_OUT}"
285        ANDROID_ART_ROOT="${ANDROID_HOST_OUT}/com.android.art"
286        ANDROID_I18N_ROOT="${ANDROID_HOST_OUT}/com.android.i18n"
287        ANDROID_TZDATA_ROOT="${ANDROID_HOST_OUT}/com.android.tzdata"
288        # On host, we default to using the symlink, as the PREFER_32BIT
289        # configuration is the only configuration building a 32bit version of
290        # dex2oat.
291        DEX2OAT_DEBUG_BINARY="dex2oatd"
292        DEX2OAT_NDEBUG_BINARY="dex2oat"
293        shift
294    elif [ "x$1" = "x--bionic" ]; then
295        BIONIC="y"
296        # We need to create an ANDROID_ROOT because currently we cannot create
297        # the frameworks/libcore with linux_bionic so we need to use the normal
298        # host ones which are in a different location.
299        CREATE_ANDROID_ROOT="y"
300        shift
301    elif [ "x$1" = "x--runtime-extracted-zipapex" ]; then
302        shift
303        USE_EXTRACTED_ZIPAPEX="y"
304        EXTRACTED_ZIPAPEX_LOC="$1"
305        shift
306    elif [ "x$1" = "x--runtime-zipapex" ]; then
307        shift
308        USE_ZIPAPEX="y"
309        ZIPAPEX_LOC="$1"
310        # TODO (b/119942078): Currently apex does not support
311        # symlink_preferred_arch so we will not have a dex2oatd to execute and
312        # need to manually provide
313        # dex2oatd64.
314        DEX2OAT_DEBUG_BINARY="dex2oatd64"
315        shift
316    elif [ "x$1" = "x--no-prebuild" ]; then
317        PREBUILD="n"
318        shift
319    elif [ "x$1" = "x--no-image" ]; then
320        HAVE_IMAGE="n"
321        shift
322    elif [ "x$1" = "x--secondary" ]; then
323        SECONDARY_DEX=":$DEX_LOCATION/$TEST_NAME-ex.jar"
324        # Enable cfg-append to make sure we get the dump for both dex files.
325        # (otherwise the runtime compilation of the secondary dex will overwrite
326        # the dump of the first one).
327        FLAGS="${FLAGS} -Xcompiler-option --dump-cfg-append"
328        COMPILE_FLAGS="${COMPILE_FLAGS} --dump-cfg-append"
329        shift
330    elif [ "x$1" = "x--with-agent" ]; then
331        shift
332        USE_JVMTI="y"
333        WITH_AGENT+=("$1")
334        shift
335    elif [ "x$1" = "x--debug-wrap-agent" ]; then
336        WRAP_DEBUGGER_AGENT="y"
337        shift
338    elif [ "x$1" = "x--debug-agent" ]; then
339        shift
340        DEBUGGER="agent"
341        USE_JVMTI="y"
342        DEBUGGER_AGENT="$1"
343        TIME_OUT="n"
344        shift
345    elif [ "x$1" = "x--debug" ]; then
346        USE_JVMTI="y"
347        DEBUGGER="y"
348        TIME_OUT="n"
349        shift
350    elif [ "x$1" = "x--gdbserver-port" ]; then
351        shift
352        GDBSERVER_PORT=$1
353        shift
354    elif [ "x$1" = "x--gdbserver-bin" ]; then
355        shift
356        GDBSERVER_HOST=$1
357        GDBSERVER_DEVICE=$1
358        shift
359    elif [ "x$1" = "x--gdbserver" ]; then
360        USE_GDBSERVER="y"
361        DEV_MODE="y"
362        TIME_OUT="n"
363        shift
364    elif [ "x$1" = "x--gdb" ]; then
365        USE_GDB="y"
366        DEV_MODE="y"
367        TIME_OUT="n"
368        shift
369    elif [ "x$1" = "x--gdb-arg" ]; then
370        shift
371        gdb_arg="$1"
372        GDB_ARGS="${GDB_ARGS} $gdb_arg"
373        shift
374    elif [ "x$1" = "x--gdb-dex2oat" ]; then
375        USE_GDB_DEX2OAT="y"
376        DEV_MODE="y"
377        TIME_OUT="n"
378        shift
379    elif [ "x$1" = "x--gdb-dex2oat-args" ]; then
380        shift
381        for arg in $(echo $1 | tr ";" " "); do
382          GDB_DEX2OAT_ARGS+="$arg "
383        done
384        shift
385    elif [ "x$1" = "x--zygote" ]; then
386        ZYGOTE="-Xzygote"
387        msg "Spawning from zygote"
388        shift
389    elif [ "x$1" = "x--dev" ]; then
390        DEV_MODE="y"
391        shift
392    elif [ "x$1" = "x--interpreter" ]; then
393        INTERPRETER="y"
394        shift
395    elif [ "x$1" = "x--jit" ]; then
396        JIT="y"
397        shift
398    elif [ "x$1" = "x--baseline" ]; then
399        FLAGS="${FLAGS} -Xcompiler-option --baseline"
400        COMPILE_FLAGS="${COMPILE_FLAGS} --baseline"
401        shift
402    elif [ "x$1" = "x--jvm" ]; then
403        USE_JVM="y"
404        shift
405    elif [ "x$1" = "x--invoke-with" ]; then
406        shift
407        if [ "x$1" = "x" ]; then
408            error_msg "$0 missing argument to --invoke-with"
409            exit 1
410        fi
411        if [ "x$INVOKE_WITH" = "x" ]; then
412            INVOKE_WITH="$1"
413        else
414            INVOKE_WITH="$INVOKE_WITH $1"
415        fi
416        shift
417    elif [ "x$1" = "x--no-verify" ]; then
418        VERIFY="n"
419        shift
420    elif [ "x$1" = "x--verify-soft-fail" ]; then
421        VERIFY="s"
422        shift
423    elif [ "x$1" = "x--no-optimize" ]; then
424        OPTIMIZE="n"
425        shift
426    elif [ "x$1" = "x--chroot" ]; then
427        shift
428        CHROOT="$1"
429        shift
430    elif [ "x$1" = "x--simpleperf" ]; then
431        SIMPLEPERF="yes"
432        shift
433    elif [ "x$1" = "x--android-root" ]; then
434        shift
435        ANDROID_ROOT="$1"
436        shift
437    elif [ "x$1" = "x--android-i18n-root" ]; then
438        shift
439        ANDROID_I18N_ROOT="$1"
440        shift
441    elif [ "x$1" = "x--android-art-root" ]; then
442        shift
443        ANDROID_ART_ROOT="$1"
444        shift
445    elif [ "x$1" = "x--android-tzdata-root" ]; then
446        shift
447        ANDROID_TZDATA_ROOT="$1"
448        shift
449    elif [ "x$1" = "x--instruction-set-features" ]; then
450        shift
451        INSTRUCTION_SET_FEATURES="$1"
452        shift
453    elif [ "x$1" = "x--timeout" ]; then
454        shift
455        TIME_OUT_VALUE="$1"
456        shift
457    elif [ "x$1" = "x--" ]; then
458        shift
459        break
460    elif [ "x$1" = "x--64" ]; then
461        SUFFIX64="64"
462        ISA="x86_64"
463        GDBSERVER_DEVICE="gdbserver64"
464        DALVIKVM="dalvikvm64"
465        LIBRARY_DIRECTORY="lib64"
466        TEST_DIRECTORY="nativetest64"
467        ARCHITECTURES_PATTERN="${ARCHITECTURES_64}"
468        GET_DEVICE_ISA_BITNESS_FLAG="--64"
469        DEX2OAT_NDEBUG_BINARY="dex2oat64"
470        DEX2OAT_DEBUG_BINARY="dex2oatd64"
471        shift
472    elif [ "x$1" = "x--experimental" ]; then
473        if [ "$#" -lt 2 ]; then
474            error_msg "missing --experimental option"
475            exit 1
476        fi
477        EXPERIMENTAL="$EXPERIMENTAL $2"
478        shift 2
479    elif [ "x$1" = "x--external-log-tags" ]; then
480        EXTERNAL_LOG_TAGS="y"
481        shift
482    elif [ "x$1" = "x--dry-run" ]; then
483        DRY_RUN="y"
484        shift
485    elif [ "x$1" = "x--vdex" ]; then
486        TEST_VDEX="y"
487        shift
488    elif [ "x$1" = "x--dex2oat-dm" ]; then
489        TEST_DEX2OAT_DM="y"
490        shift
491    elif [ "x$1" = "x--runtime-dm" ]; then
492        TEST_RUNTIME_DM="y"
493        shift
494    elif [ "x$1" = "x--vdex-filter" ]; then
495        shift
496        option="$1"
497        VDEX_ARGS="${VDEX_ARGS} --compiler-filter=$option"
498        shift
499    elif [ "x$1" = "x--vdex-arg" ]; then
500        shift
501        VDEX_ARGS="${VDEX_ARGS} $1"
502        shift
503    elif [ "x$1" = "x--sync" ]; then
504        SYNC_BEFORE_RUN="y"
505        shift
506    elif [ "x$1" = "x--profile" ]; then
507        PROFILE="y"
508        shift
509    elif [ "x$1" = "x--random-profile" ]; then
510        RANDOM_PROFILE="y"
511        shift
512    elif expr "x$1" : "x--" >/dev/null 2>&1; then
513        error_msg "unknown $0 option: $1"
514        exit 1
515    else
516        break
517    fi
518done
519
520# HACK: Force the use of `signal_dumper` on host.
521if [[ "$HOST" = "y" ]]; then
522  TIME_OUT="timeout"
523fi
524
525# If you change this, update the timeout in testrunner.py as well.
526if [ -z "$TIME_OUT_VALUE" ] ; then
527  # 10 minutes is the default.
528  TIME_OUT_VALUE=600
529
530  # For sanitized builds use a larger base.
531  # TODO: Consider sanitized target builds?
532  if [ "x$SANITIZE_HOST" != "x" ] ; then
533    TIME_OUT_VALUE=1500  # 25 minutes.
534  fi
535
536  TIME_OUT_VALUE=$((${TIME_OUT_VALUE} + ${TIME_OUT_EXTRA}))
537fi
538
539# Escape hatch for slow hosts or devices. Accept an environment variable as a timeout factor.
540if [ ! -z "$ART_TIME_OUT_MULTIPLIER" ] ; then
541  TIME_OUT_VALUE=$((${TIME_OUT_VALUE} * ${ART_TIME_OUT_MULTIPLIER}))
542fi
543
544# The DEX_LOCATION with the chroot prefix, if any.
545CHROOT_DEX_LOCATION="$CHROOT$DEX_LOCATION"
546
547# If running on device, determine the ISA of the device.
548if [ "$HOST" = "n" -a "$USE_JVM" = "n" ]; then
549  ISA=$("$ANDROID_BUILD_TOP/art/test/utils/get-device-isa" "$GET_DEVICE_ISA_BITNESS_FLAG")
550fi
551
552if [ "$USE_JVM" = "n" ]; then
553    FLAGS="${FLAGS} ${ANDROID_FLAGS}"
554    # we don't want to be trying to get adbconnections since the plugin might
555    # not have been built.
556    FLAGS="${FLAGS} -XjdwpProvider:none"
557    for feature in ${EXPERIMENTAL}; do
558        FLAGS="${FLAGS} -Xexperimental:${feature} -Xcompiler-option --runtime-arg -Xcompiler-option -Xexperimental:${feature}"
559        COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xexperimental:${feature}"
560    done
561fi
562
563if [ "$CREATE_ANDROID_ROOT" = "y" ]; then
564    ANDROID_ROOT=$DEX_LOCATION/android-root
565fi
566
567if [ "x$1" = "x" ] ; then
568  MAIN="Main"
569else
570  MAIN="$1"
571  shift
572fi
573
574if [ "$ZYGOTE" = "" ]; then
575    if [ "$OPTIMIZE" = "y" ]; then
576        if [ "$VERIFY" = "y" ]; then
577            DEX_OPTIMIZE="-Xdexopt:verified"
578        else
579            DEX_OPTIMIZE="-Xdexopt:all"
580        fi
581        msg "Performing optimizations"
582    else
583        DEX_OPTIMIZE="-Xdexopt:none"
584        msg "Skipping optimizations"
585    fi
586
587    if [ "$VERIFY" = "y" ]; then
588        JVM_VERIFY_ARG="-Xverify:all"
589        msg "Performing verification"
590    elif [ "$VERIFY" = "s" ]; then
591        JVM_VERIFY_ARG="Xverify:all"
592        DEX_VERIFY="-Xverify:softfail"
593        msg "Forcing verification to be soft fail"
594    else # VERIFY = "n"
595        DEX_VERIFY="-Xverify:none"
596        JVM_VERIFY_ARG="-Xverify:none"
597        msg "Skipping verification"
598    fi
599fi
600
601msg "------------------------------"
602
603if [ "$DEBUGGER" = "y" ]; then
604  # Use this instead for ddms and connect by running 'ddms':
605  # DEBUGGER_OPTS="-XjdwpOptions=server=y,suspend=y -XjdwpProvider:adbconnection"
606  # TODO: add a separate --ddms option?
607
608  PORT=12345
609  msg "Waiting for jdb to connect:"
610  if [ "$HOST" = "n" ]; then
611    msg "    adb forward tcp:$PORT tcp:$PORT"
612  fi
613  msg "    jdb -attach localhost:$PORT"
614  if [ "$USE_JVM" = "n" ]; then
615    # Use the default libjdwp agent. Use --debug-agent to use a custom one.
616    DEBUGGER_OPTS="-agentpath:libjdwp.so=transport=dt_socket,address=$PORT,server=y,suspend=y -XjdwpProvider:internal"
617  else
618    DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=$PORT,server=y,suspend=y"
619  fi
620elif [ "$DEBUGGER" = "agent" ]; then
621  PORT=12345
622  # TODO Support ddms connection and support target.
623  if [ "$HOST" = "n" ]; then
624    error_msg "--debug-agent not supported yet for target!"
625    exit 1
626  fi
627  AGENTPATH=${DEBUGGER_AGENT}
628  if [ "$WRAP_DEBUGGER_AGENT" = "y" ]; then
629    WRAPPROPS="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}/libwrapagentpropertiesd.so"
630    if [ "$TEST_IS_NDEBUG" = "y" ]; then
631      WRAPPROPS="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}/libwrapagentproperties.so"
632    fi
633    AGENTPATH="${WRAPPROPS}=${ANDROID_BUILD_TOP}/art/tools/libjdwp-compat.props,${AGENTPATH}"
634  fi
635  msg "Connect to localhost:$PORT"
636  DEBUGGER_OPTS="-agentpath:${AGENTPATH}=transport=dt_socket,address=$PORT,server=y,suspend=y"
637fi
638
639for agent in "${WITH_AGENT[@]}"; do
640  FLAGS="${FLAGS} -agentpath:${agent}"
641done
642
643if [ "$USE_JVMTI" = "y" ]; then
644  if [ "$USE_JVM" = "n" ]; then
645    plugin=libopenjdkjvmtid.so
646    if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
647      plugin=libopenjdkjvmti.so
648    fi
649    # We used to add flags here that made the runtime debuggable but that is not
650    # needed anymore since the plugin can do it for us now.
651    FLAGS="${FLAGS} -Xplugin:${plugin}"
652
653    # For jvmti tests, set the threshold of compilation to 1, so we jit early to
654    # provide better test coverage for jvmti + jit. This means we won't run
655    # the default --jit configuration but it is not too important test scenario for
656    # jvmti tests. This is art specific flag, so don't use it with jvm.
657    FLAGS="${FLAGS} -Xjitthreshold:1"
658  fi
659fi
660
661# Add the libdir to the argv passed to the main function.
662if [ "$ADD_LIBDIR_ARGUMENTS" = "y" ]; then
663  if [[ "$HOST" = "y" ]]; then
664    ARGS="${ARGS} ${ANDROID_HOST_OUT}/${TEST_DIRECTORY}/"
665  else
666    ARGS="${ARGS} /data/${TEST_DIRECTORY}/art/${ISA}/"
667  fi
668fi
669if [ "$IS_JVMTI_TEST" = "y" ]; then
670  agent=libtiagentd.so
671  lib=tiagentd
672  if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
673    agent=libtiagent.so
674    lib=tiagent
675  fi
676
677  ARGS="${ARGS} ${lib}"
678  if [[ "$USE_JVM" = "y" ]]; then
679    FLAGS="${FLAGS} -agentpath:${ANDROID_HOST_OUT}/nativetest64/${agent}=${TEST_NAME},jvm"
680  else
681    FLAGS="${FLAGS} -agentpath:${agent}=${TEST_NAME},art"
682  fi
683fi
684
685if [[ "$JVMTI_STRESS" = "y" ]]; then
686  agent=libtistressd.so
687  if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
688    agent=libtistress.so
689  fi
690
691  # Just give it a default start so we can always add ',' to it.
692  agent_args="jvmti-stress"
693  if [[ "$JVMTI_REDEFINE_STRESS" = "y" ]]; then
694    # We really cannot do this on RI so don't both passing it in that case.
695    if [[ "$USE_JVM" = "n" ]]; then
696      agent_args="${agent_args},redefine"
697    fi
698  fi
699  if [[ "$JVMTI_FIELD_STRESS" = "y" ]]; then
700    agent_args="${agent_args},field"
701  fi
702  if [[ "$JVMTI_STEP_STRESS" = "y" ]]; then
703    agent_args="${agent_args},step"
704  fi
705  if [[ "$JVMTI_TRACE_STRESS" = "y" ]]; then
706    agent_args="${agent_args},trace"
707  fi
708  # In the future add onto this;
709  if [[ "$USE_JVM" = "y" ]]; then
710    FLAGS="${FLAGS} -agentpath:${ANDROID_HOST_OUT}/nativetest64/${agent}=${agent_args}"
711  else
712    FLAGS="${FLAGS} -agentpath:${agent}=${agent_args}"
713  fi
714fi
715
716if [ "$USE_JVM" = "y" ]; then
717  export LD_LIBRARY_PATH=${ANDROID_HOST_OUT}/lib64
718  # Some jvmti tests are flaky without -Xint on the RI.
719  if [ "$IS_JVMTI_TEST" = "y" ]; then
720    FLAGS="${FLAGS} -Xint"
721  fi
722  # Xmx is necessary since we don't pass down the ART flags to JVM.
723  # We pass the classes2 path whether it's used (src-multidex) or not.
724  cmdline="${JAVA} ${DEBUGGER_OPTS} ${JVM_VERIFY_ARG} -Xmx256m -classpath classes:classes2 ${FLAGS} $MAIN $@ ${ARGS}"
725  if [ "$DEV_MODE" = "y" ]; then
726    echo $cmdline
727  fi
728  if [ "$CREATE_RUNNER" = "y" ]; then
729    echo "#!/bin/bash" > runit.sh
730    echo "export LD_LIBRARY_PATH=\"$LD_LIBRARY_PATH\""
731    echo $cmdline >> runit.sh
732    chmod u+x runit.sh
733    echo "Runnable test script written to $PWD/runit.sh"
734  else
735    $cmdline
736  fi
737  exit
738fi
739
740readonly b_path=$(get_apex_bootclasspath ${HOST})
741readonly b_path_locations=$(get_apex_bootclasspath_locations ${HOST})
742
743BCPEX=
744if [ -f "$TEST_NAME-bcpex.jar" ] ; then
745  BCPEX=":$DEX_LOCATION/$TEST_NAME-bcpex.jar"
746fi
747
748# Pass down the bootclasspath
749FLAGS="${FLAGS} -Xbootclasspath:${b_path}${BCPEX}"
750FLAGS="${FLAGS} -Xbootclasspath-locations:${b_path_locations}${BCPEX}"
751COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xbootclasspath:${b_path}"
752COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xbootclasspath-locations:${b_path_locations}"
753
754if [ "$HAVE_IMAGE" = "n" ]; then
755    # Disable image dex2oat - this will forbid the runtime to patch or compile an image.
756    FLAGS="${FLAGS} -Xnoimage-dex2oat"
757
758    # We'll abuse a second flag here to test different behavior. If --relocate, use the
759    # existing image - relocation will fail as patching is disallowed. If --no-relocate,
760    # pass a non-existent image - compilation will fail as dex2oat is disallowed.
761    if [ "${RELOCATE}" = "n" ] ; then
762      BOOT_IMAGE="/system/non-existent/boot.art"
763    fi
764    # App images cannot be generated without a boot image.
765    APP_IMAGE="n"
766fi
767DALVIKVM_BOOT_OPT="-Ximage:${BOOT_IMAGE}"
768
769if [ "$USE_GDB_DEX2OAT" = "y" ]; then
770  if [ "$HOST" = "n" ]; then
771    echo "The --gdb-dex2oat option is not yet implemented for target." >&2
772    exit 1
773  fi
774fi
775
776if [ "$USE_GDB" = "y" ]; then
777  if [ "$USE_GDBSERVER" = "y" ]; then
778    error_msg "Cannot pass both --gdb and --gdbserver at the same time!"
779    exit 1
780  elif [ "$HOST" = "n" ]; then
781    # We might not have any hostname resolution if we are using a chroot.
782    GDB="$GDBSERVER_DEVICE --no-startup-with-shell 127.0.0.1$GDBSERVER_PORT"
783  else
784    if [ `uname` = "Darwin" ]; then
785        GDB=lldb
786        GDB_ARGS="$GDB_ARGS -- $DALVIKVM"
787        DALVIKVM=
788    else
789        GDB=gdb
790        GDB_ARGS="$GDB_ARGS --args $DALVIKVM"
791        # Enable for Emacs "M-x gdb" support. TODO: allow extra gdb arguments on command line.
792        # gdbargs="--annotate=3 $gdbargs"
793    fi
794  fi
795elif [ "$USE_GDBSERVER" = "y" ]; then
796  if [ "$HOST" = "n" ]; then
797    # We might not have any hostname resolution if we are using a chroot.
798    GDB="$GDBSERVER_DEVICE --no-startup-with-shell 127.0.0.1$GDBSERVER_PORT"
799  else
800    GDB="$GDBSERVER_HOST $GDBSERVER_PORT"
801  fi
802fi
803
804if [ "$INTERPRETER" = "y" ]; then
805    INT_OPTS="${INT_OPTS} -Xint"
806fi
807
808if [ "$JIT" = "y" ]; then
809    INT_OPTS="${INT_OPTS} -Xusejit:true"
810else
811    INT_OPTS="${INT_OPTS} -Xusejit:false"
812fi
813
814if [ "$INTERPRETER" = "y" ] || [ "$JIT" = "y" ]; then
815  if [ "$VERIFY" = "y" ] ; then
816    INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=verify"
817    COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=verify"
818  elif [ "$VERIFY" = "s" ]; then
819    INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=extract"
820    COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=extract"
821    DEX_VERIFY="${DEX_VERIFY} -Xverify:softfail"
822  else # VERIFY = "n"
823    INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=assume-verified"
824    COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=assume-verified"
825    DEX_VERIFY="${DEX_VERIFY} -Xverify:none"
826  fi
827fi
828
829JNI_OPTS="-Xjnigreflimit:512 -Xcheck:jni"
830
831COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xnorelocate"
832if [ "$RELOCATE" = "y" ]; then
833    FLAGS="${FLAGS} -Xrelocate"
834else
835    FLAGS="$FLAGS -Xnorelocate"
836fi
837
838if [ "$BIONIC" = "y" ]; then
839  # This is the location that soong drops linux_bionic builds. Despite being
840  # called linux_bionic-x86 the build is actually amd64 (x86_64) only.
841  if [ ! -e "$OUT_DIR/soong/host/linux_bionic-x86" ]; then
842    error_msg "linux_bionic-x86 target doesn't seem to have been built!"
843    exit 1
844  fi
845  # Set TIMEOUT_DUMPER manually so it works even with apex's
846  TIMEOUT_DUMPER=$OUT_DIR/soong/host/linux_bionic-x86/bin/signal_dumper
847fi
848
849# Prevent test from silently falling back to interpreter in no-prebuild mode. This happens
850# when DEX_LOCATION path is too long, because vdex/odex filename is constructed by taking
851# full path to dex, stripping leading '/', appending '@classes.vdex' and changing every
852# remaining '/' into '@'.
853if [ "$HOST" = "y" ]; then
854  max_filename_size=$(getconf NAME_MAX $DEX_LOCATION)
855else
856  # There is no getconf on device, fallback to standard value.
857  # See NAME_MAX in kernel <linux/limits.h>
858  max_filename_size=255
859fi
860# Compute VDEX_NAME.
861DEX_LOCATION_STRIPPED="${DEX_LOCATION#/}"
862VDEX_NAME="${DEX_LOCATION_STRIPPED//\//@}@$TEST_NAME.jar@classes.vdex"
863if [ ${#VDEX_NAME} -gt $max_filename_size ]; then
864    echo "Dex location path too long:"
865    error_msg "$VDEX_NAME is ${#VDEX_NAME} character long, and the limit is $max_filename_size."
866    exit 1
867fi
868
869if [ "$HOST" = "y" ]; then
870  # On host, run binaries (`dex2oat(d)`, `dalvikvm`, `profman`) from the `bin`
871  # directory under the "Android Root" (usually `out/host/linux-x86`).
872  #
873  # TODO(b/130295968): Adjust this if/when ART host artifacts are installed
874  # under the ART root (usually `out/host/linux-x86/com.android.art`).
875  ANDROID_ART_BIN_DIR=$ANDROID_ROOT/bin
876else
877  # On target, run binaries (`dex2oat(d)`, `dalvikvm`, `profman`) from the ART
878  # APEX's `bin` directory. This means the linker will observe the ART APEX
879  # linker configuration file (`/apex/com.android.art/etc/ld.config.txt`) for
880  # these binaries.
881  ANDROID_ART_BIN_DIR=$ANDROID_ART_ROOT/bin
882fi
883
884profman_cmdline="true"
885dex2oat_cmdline="true"
886vdex_cmdline="true"
887dm_cmdline="true"
888mkdir_locations="${DEX_LOCATION}/dalvik-cache/$ISA"
889strip_cmdline="true"
890sync_cmdline="true"
891linkroot_cmdline="true"
892linkroot_overlay_cmdline="true"
893setupapex_cmdline="true"
894installapex_cmdline="true"
895installapex_test_cmdline="true"
896
897linkdirs() {
898  find "$1" -maxdepth 1 -mindepth 1 -type d | xargs -i ln -sf '{}' "$2"
899  # Also create a link for the boot image.
900  ln -sf $ANDROID_HOST_OUT/apex/art_boot_images "$2"
901}
902
903if [ "$CREATE_ANDROID_ROOT" = "y" ]; then
904  mkdir_locations="${mkdir_locations} ${ANDROID_ROOT}"
905  linkroot_cmdline="linkdirs ${ANDROID_HOST_OUT} ${ANDROID_ROOT}"
906  if [ "${BIONIC}" = "y" ]; then
907    # TODO Make this overlay more generic.
908    linkroot_overlay_cmdline="linkdirs $OUT_DIR/soong/host/linux_bionic-x86 ${ANDROID_ROOT}"
909  fi
910  # Replace the boot image to a location expected by the runtime.
911  DALVIKVM_BOOT_OPT="-Ximage:${ANDROID_ROOT}/art_boot_images/javalib/boot.art"
912fi
913
914if [ "$USE_ZIPAPEX" = "y" ]; then
915  # TODO Currently this only works for linux_bionic zipapexes because those are
916  # stripped and so small enough that the ulimit doesn't kill us.
917  mkdir_locations="${mkdir_locations} $DEX_LOCATION/zipapex"
918  zip_options="-qq"
919  if [ "$DEV_MODE" = "y" ]; then
920    zip_options=""
921  fi
922  setupapex_cmdline="unzip -o -u ${zip_options} ${ZIPAPEX_LOC} apex_payload.zip -d ${DEX_LOCATION}"
923  installapex_cmdline="unzip -o -u ${zip_options} ${DEX_LOCATION}/apex_payload.zip -d ${DEX_LOCATION}/zipapex"
924  ANDROID_ART_BIN_DIR=$DEX_LOCATION/zipapex/bin
925elif [ "$USE_EXTRACTED_ZIPAPEX" = "y" ]; then
926  # Just symlink the zipapex binaries
927  ANDROID_ART_BIN_DIR=$DEX_LOCATION/zipapex/bin
928  # Force since some tests manually run this file twice.
929  ln_options=""
930  if [ "$DEV_MODE" = "y" ]; then
931    ln_options="--verbose"
932  fi
933  # If the ${RUN} is executed multiple times we don't need to recreate the link
934  installapex_test_cmdline="test -L ${DEX_LOCATION}/zipapex"
935  installapex_cmdline="ln -s -f ${ln_options} ${EXTRACTED_ZIPAPEX_LOC} ${DEX_LOCATION}/zipapex"
936fi
937
938# PROFILE takes precedence over RANDOM_PROFILE, since PROFILE tests require a
939# specific profile to run properly.
940if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
941  profman_cmdline="$ANDROID_ART_BIN_DIR/profman  \
942    --apk=$DEX_LOCATION/$TEST_NAME.jar \
943    --dex-location=$DEX_LOCATION/$TEST_NAME.jar"
944  if [ -f "$TEST_NAME-ex.jar" ] && [ "$SECONDARY_COMPILATION" = "y" ] ; then
945    profman_cmdline="${profman_cmdline} \
946      --apk=$DEX_LOCATION/$TEST_NAME-ex.jar \
947      --dex-location=$DEX_LOCATION/$TEST_NAME-ex.jar"
948  fi
949  COMPILE_FLAGS="${COMPILE_FLAGS} --profile-file=$DEX_LOCATION/$TEST_NAME.prof"
950  FLAGS="${FLAGS} -Xcompiler-option --profile-file=$DEX_LOCATION/$TEST_NAME.prof"
951  if [ "$PROFILE" = "y" ]; then
952    profman_cmdline="${profman_cmdline} --create-profile-from=$DEX_LOCATION/profile \
953        --reference-profile-file=$DEX_LOCATION/$TEST_NAME.prof"
954  else
955    profman_cmdline="${profman_cmdline} --generate-test-profile=$DEX_LOCATION/$TEST_NAME.prof \
956        --generate-test-profile-seed=0"
957  fi
958fi
959
960function get_prebuilt_lldb_path {
961  local CLANG_BASE="prebuilts/clang/host"
962  local CLANG_VERSION="$("$ANDROID_BUILD_TOP/build/soong/scripts/get_clang_version.py")"
963  case "$(uname -s)" in
964    Darwin)
965      local PREBUILT_NAME="darwin-x86"
966      ;;
967    Linux)
968      local PREBUILT_NAME="linux-x86"
969      ;;
970    *)
971      >&2 echo "Unknown host $(uname -s). Unsupported for debugging dex2oat with LLDB."
972      return
973      ;;
974  esac
975  local CLANG_PREBUILT_HOST_PATH="$ANDROID_BUILD_TOP/$CLANG_BASE/$PREBUILT_NAME/$CLANG_VERSION"
976  # If the clang prebuilt directory exists and the reported clang version
977  # string does not, then it is likely that the clang version reported by the
978  # get_clang_version.py script does not match the expected directory name.
979  if [ -d "${ANDROID_BUILD_TOP}/${CLANG_BASE}/${PREBUILT_NAME}" ] && \
980     [ ! -d "${CLANG_PREBUILT_HOST_PATH}" ]; then
981    error_msg "The prebuilt clang directory exists, but the specific clang"\
982    "\nversion reported by get_clang_version.py does not exist in that path."\
983    "\nPlease make sure that the reported clang version resides in the"\
984    "\nprebuilt clang directory!"
985    exit 1
986  fi
987
988  # The lldb-server binary is a dependency of lldb.
989  export LLDB_DEBUGSERVER_PATH="${CLANG_PREBUILT_HOST_PATH}/runtimes_ndk_cxx/x86_64/lldb-server"
990
991  # Set the current terminfo directory to TERMINFO so that LLDB can read the
992  # termcap database.
993  local terminfo_regexp_path='\/.*\/*terminfo\/'
994  if [[ $(infocmp) =~ $terminfo_regexp_path ]] ; then
995    export TERMINFO="${BASH_REMATCH[0]}"
996  fi
997
998  prebuilt_lldb_path="$CLANG_PREBUILT_HOST_PATH/bin/lldb.sh"
999}
1000
1001function write_dex2oat_cmdlines {
1002  local name="$1"
1003
1004  local class_loader_context=""
1005  local enable_app_image=false
1006  if [ "$APP_IMAGE" = "y" ]; then
1007    enable_app_image=true
1008  fi
1009
1010  # If the name ends in -ex then this is a secondary dex file
1011  if [ "${name:${#name}-3}" = "-ex" ]; then
1012    # Lazily realize the default value in case DEX_LOCATION/TEST_NAME change
1013    if [ "x$SECONDARY_CLASS_LOADER_CONTEXT" = "x" ]; then
1014      if [ "x$SECONDARY_DEX" = "x" ]; then
1015        # Tests without `--secondary` load the "-ex" jar in a separate PathClassLoader
1016        # that is a child of the main PathClassLoader. If the class loader is constructed
1017        # in any other way, the test needs to specify the secondary CLC explicitly.
1018        SECONDARY_CLASS_LOADER_CONTEXT="PCL[];PCL[$DEX_LOCATION/$TEST_NAME.jar]"
1019      else
1020        # Tests with `--secondary` load the `-ex` jar a part of the main PathClassLoader.
1021        SECONDARY_CLASS_LOADER_CONTEXT="PCL[$DEX_LOCATION/$TEST_NAME.jar]"
1022      fi
1023    fi
1024    class_loader_context="'--class-loader-context=$SECONDARY_CLASS_LOADER_CONTEXT'"
1025    $enable_app_image && [ "$SECONDARY_APP_IMAGE" = "y" ] || enable_app_image=false
1026  fi
1027
1028  local app_image=""
1029  $enable_app_image && app_image="--app-image-file=$DEX_LOCATION/oat/$ISA/$name.art --resolve-startup-const-strings=true"
1030
1031  if [ "$USE_GDB_DEX2OAT" = "y" ]; then
1032    get_prebuilt_lldb_path
1033    GDB_DEX2OAT="$prebuilt_lldb_path -f"
1034    GDB_DEX2OAT_ARGS+=" -- "
1035  fi
1036
1037  local dex2oat_binary
1038  dex2oat_binary=${DEX2OAT_DEBUG_BINARY}
1039  if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
1040    dex2oat_binary=${DEX2OAT_NDEBUG_BINARY}
1041  fi
1042  dex2oat_cmdline="$INVOKE_WITH $GDB_DEX2OAT \
1043                      $ANDROID_ART_BIN_DIR/$dex2oat_binary \
1044                      $GDB_DEX2OAT_ARGS \
1045                      $COMPILE_FLAGS \
1046                      --boot-image=${BOOT_IMAGE} \
1047                      --dex-file=$DEX_LOCATION/$name.jar \
1048                      --oat-file=$DEX_LOCATION/oat/$ISA/$name.odex \
1049                      "$app_image" \
1050                      --generate-mini-debug-info \
1051                      --instruction-set=$ISA \
1052                      $class_loader_context"
1053  if [ "x$INSTRUCTION_SET_FEATURES" != "x" ] ; then
1054    dex2oat_cmdline="${dex2oat_cmdline} --instruction-set-features=${INSTRUCTION_SET_FEATURES}"
1055  fi
1056
1057  # Add in a timeout. This is important for testing the compilation/verification time of
1058  # pathological cases. We do not append a timeout when debugging dex2oat because we
1059  # do not want it to exit while debugging.
1060  # Note: as we don't know how decent targets are (e.g., emulator), only do this on the host for
1061  #       now. We should try to improve this.
1062  #       The current value is rather arbitrary. run-tests should compile quickly.
1063  # Watchdog timeout is in milliseconds so add 3 '0's to the dex2oat timeout.
1064  if [ "$HOST" != "n" ] && [ "$USE_GDB_DEX2OAT" != "y" ]; then
1065    # Use SIGRTMIN+2 to try to dump threads.
1066    # Use -k 1m to SIGKILL it a minute later if it hasn't ended.
1067    dex2oat_cmdline="timeout -k ${DEX2OAT_TIMEOUT}s -s SIGRTMIN+2 ${DEX2OAT_RT_TIMEOUT}s ${dex2oat_cmdline} --watchdog-timeout=${DEX2OAT_TIMEOUT}000"
1068  fi
1069  if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
1070    vdex_cmdline="${dex2oat_cmdline} ${VDEX_ARGS} --input-vdex=$DEX_LOCATION/oat/$ISA/$name.vdex --output-vdex=$DEX_LOCATION/oat/$ISA/$name.vdex"
1071  elif [ "$TEST_VDEX" = "y" ]; then
1072    if [ "$VDEX_ARGS" = "" ]; then
1073      # If no arguments need to be passed, just delete the odex file so that the runtime only picks up the vdex file.
1074      vdex_cmdline="rm $DEX_LOCATION/oat/$ISA/$name.odex"
1075    else
1076      vdex_cmdline="${dex2oat_cmdline} ${VDEX_ARGS} --input-vdex=$DEX_LOCATION/oat/$ISA/$name.vdex"
1077    fi
1078  elif [ "$TEST_DEX2OAT_DM" = "y" ]; then
1079    vdex_cmdline="${dex2oat_cmdline} ${VDEX_ARGS} --dump-timings --dm-file=$DEX_LOCATION/oat/$ISA/$name.dm"
1080    dex2oat_cmdline="${dex2oat_cmdline} --copy-dex-files=false --output-vdex=$DEX_LOCATION/oat/$ISA/primary.vdex"
1081    dm_cmdline="zip -qj $DEX_LOCATION/oat/$ISA/$name.dm $DEX_LOCATION/oat/$ISA/primary.vdex"
1082  elif [ "$TEST_RUNTIME_DM" = "y" ]; then
1083    dex2oat_cmdline="${dex2oat_cmdline} --copy-dex-files=false --output-vdex=$DEX_LOCATION/oat/$ISA/primary.vdex"
1084    dm_cmdline="zip -qj $DEX_LOCATION/$name.dm $DEX_LOCATION/oat/$ISA/primary.vdex"
1085  fi
1086}
1087
1088# Enable mini-debug-info for JIT (if JIT is used).
1089FLAGS="$FLAGS -Xcompiler-option --generate-mini-debug-info"
1090
1091if [ "$PREBUILD" = "y" ]; then
1092  mkdir_locations="${mkdir_locations} ${DEX_LOCATION}/oat/$ISA"
1093
1094  # "Primary".
1095  write_dex2oat_cmdlines "$TEST_NAME"
1096  dex2oat_cmdline=$(echo $dex2oat_cmdline)
1097  dm_cmdline=$(echo $dm_cmdline)
1098  vdex_cmdline=$(echo $vdex_cmdline)
1099
1100  # Enable mini-debug-info for JIT (if JIT is used).
1101  FLAGS="$FLAGS -Xcompiler-option --generate-mini-debug-info"
1102
1103  if [ -f "$TEST_NAME-ex.jar" ] && [ "$SECONDARY_COMPILATION" = "y" ] ; then
1104    # "Secondary" for test coverage.
1105
1106    # Store primary values.
1107    base_dex2oat_cmdline="$dex2oat_cmdline"
1108    base_dm_cmdline="$dm_cmdline"
1109    base_vdex_cmdline="$vdex_cmdline"
1110
1111    write_dex2oat_cmdlines "$TEST_NAME-ex"
1112    dex2oat_cmdline=$(echo $dex2oat_cmdline)
1113    dm_cmdline=$(echo $dm_cmdline)
1114    vdex_cmdline=$(echo $vdex_cmdline)
1115
1116    # Concatenate.
1117    dex2oat_cmdline="$base_dex2oat_cmdline && $dex2oat_cmdline"
1118    dm_cmdline="$base_dm_cmdline" # Only use primary dm.
1119    vdex_cmdline="$base_vdex_cmdline && $vdex_cmdline"
1120  fi
1121fi
1122
1123if [ "$SYNC_BEFORE_RUN" = "y" ]; then
1124  sync_cmdline="sync"
1125fi
1126
1127DALVIKVM_ISA_FEATURES_ARGS=""
1128if [ "x$INSTRUCTION_SET_FEATURES" != "x" ] ; then
1129  DALVIKVM_ISA_FEATURES_ARGS="-Xcompiler-option --instruction-set-features=${INSTRUCTION_SET_FEATURES}"
1130fi
1131
1132# java.io.tmpdir can only be set at launch time.
1133TMP_DIR_OPTION=""
1134if [ "$HOST" = "n" ]; then
1135  TMP_DIR_OPTION="-Djava.io.tmpdir=/data/local/tmp"
1136fi
1137
1138# The build servers have an ancient version of bash so we cannot use @Q.
1139if [ "$USE_GDBSERVER" == "y" ]; then
1140  printf -v QUOTED_DALVIKVM_BOOT_OPT "%q" "$DALVIKVM_BOOT_OPT"
1141else
1142  QUOTED_DALVIKVM_BOOT_OPT="$DALVIKVM_BOOT_OPT"
1143fi
1144
1145DALVIKVM_CLASSPATH=$DEX_LOCATION/$TEST_NAME.jar
1146if [ -f "$TEST_NAME-aotex.jar" ] ; then
1147  DALVIKVM_CLASSPATH=$DALVIKVM_CLASSPATH:$DEX_LOCATION/$TEST_NAME-aotex.jar
1148fi
1149DALVIKVM_CLASSPATH=$DALVIKVM_CLASSPATH$SECONDARY_DEX
1150
1151# We set DumpNativeStackOnSigQuit to false to avoid stressing libunwind.
1152# b/27185632
1153# b/24664297
1154
1155dalvikvm_cmdline="$INVOKE_WITH $GDB $ANDROID_ART_BIN_DIR/$DALVIKVM \
1156                  $GDB_ARGS \
1157                  $FLAGS \
1158                  $DEX_VERIFY \
1159                  -XXlib:$LIB \
1160                  $DEX2OAT \
1161                  $DALVIKVM_ISA_FEATURES_ARGS \
1162                  $ZYGOTE \
1163                  $JNI_OPTS \
1164                  $INT_OPTS \
1165                  $DEBUGGER_OPTS \
1166                  ${QUOTED_DALVIKVM_BOOT_OPT} \
1167                  $TMP_DIR_OPTION \
1168                  -XX:DumpNativeStackOnSigQuit:false \
1169                  -cp $DALVIKVM_CLASSPATH $MAIN $ARGS"
1170
1171if [ "x$SIMPLEPERF" == xyes ]; then
1172  dalvikvm_cmdline="simpleperf record ${dalvikvm_cmdline} && simpleperf report"
1173fi
1174
1175sanitize_dex2oat_cmdline() {
1176  local args=()
1177  for arg in "$@"; do
1178    if [ "$arg" = "--class-loader-context=&" ]; then
1179      arg="--class-loader-context=\&"
1180    fi
1181    args+=("$arg")
1182  done
1183  echo -n "${args[@]}"
1184}
1185
1186# Remove whitespace.
1187dex2oat_cmdline=$(sanitize_dex2oat_cmdline $(echo $dex2oat_cmdline))
1188dalvikvm_cmdline=$(echo $dalvikvm_cmdline)
1189dm_cmdline=$(echo $dm_cmdline)
1190vdex_cmdline=$(sanitize_dex2oat_cmdline $(echo $vdex_cmdline))
1191profman_cmdline=$(echo $profman_cmdline)
1192
1193# Use an empty ASAN_OPTIONS to enable defaults.
1194# Note: this is required as envsetup right now exports detect_leaks=0.
1195RUN_TEST_ASAN_OPTIONS=""
1196
1197# Multiple shutdown leaks. b/38341789
1198if [ "x$RUN_TEST_ASAN_OPTIONS" != "x" ] ; then
1199  RUN_TEST_ASAN_OPTIONS="${RUN_TEST_ASAN_OPTIONS}:"
1200fi
1201RUN_TEST_ASAN_OPTIONS="${RUN_TEST_ASAN_OPTIONS}detect_leaks=0"
1202
1203# For running, we must turn off logging when dex2oat is missing. Otherwise we use
1204# the same defaults as for prebuilt: everything when --dev, otherwise errors and above only.
1205if [ "$EXTERNAL_LOG_TAGS" = "n" ]; then
1206  if [ "$DEV_MODE" = "y" ]; then
1207      export ANDROID_LOG_TAGS='*:d'
1208  elif [ "$HAVE_IMAGE" = "n" ]; then
1209      # All tests would log the error of missing image. Be silent here and only log fatal
1210      # events.
1211      export ANDROID_LOG_TAGS='*:s'
1212  else
1213      # We are interested in LOG(ERROR) output.
1214      export ANDROID_LOG_TAGS='*:e'
1215  fi
1216fi
1217
1218if [ "$HOST" = "n" ]; then
1219    adb root > /dev/null
1220    adb wait-for-device
1221    if [ "$QUIET" = "n" ]; then
1222      adb shell rm -rf $CHROOT_DEX_LOCATION
1223      adb shell mkdir -p $CHROOT_DEX_LOCATION
1224      adb push $TEST_NAME.jar $CHROOT_DEX_LOCATION
1225      adb push $TEST_NAME-ex.jar $CHROOT_DEX_LOCATION
1226      adb push $TEST_NAME-aotex.jar $CHROOT_DEX_LOCATION
1227      adb push $TEST_NAME-bcpex.jar $CHROOT_DEX_LOCATION
1228      if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
1229        adb push profile $CHROOT_DEX_LOCATION
1230      fi
1231      # Copy resource folder
1232      if [ -d res ]; then
1233        adb push res $CHROOT_DEX_LOCATION
1234      fi
1235    else
1236      adb shell rm -rf $CHROOT_DEX_LOCATION >/dev/null 2>&1
1237      adb shell mkdir -p $CHROOT_DEX_LOCATION >/dev/null 2>&1
1238      adb push $TEST_NAME.jar $CHROOT_DEX_LOCATION >/dev/null 2>&1
1239      adb push $TEST_NAME-ex.jar $CHROOT_DEX_LOCATION >/dev/null 2>&1
1240      adb push $TEST_NAME-aotex.jar $CHROOT_DEX_LOCATION >/dev/null 2>&1
1241      adb push $TEST_NAME-bcpex.jar $CHROOT_DEX_LOCATION >/dev/null 2>&1
1242      if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
1243        adb push profile $CHROOT_DEX_LOCATION >/dev/null 2>&1
1244      fi
1245      # Copy resource folder
1246      if [ -d res ]; then
1247        adb push res $CHROOT_DEX_LOCATION >/dev/null 2>&1
1248      fi
1249    fi
1250
1251    # Populate LD_LIBRARY_PATH.
1252    LD_LIBRARY_PATH=
1253    if [ "$ANDROID_ROOT" != "/system" ]; then
1254      # Current default installation is dalvikvm 64bits and dex2oat 32bits,
1255      # so we can only use LD_LIBRARY_PATH when testing on a local
1256      # installation.
1257      LD_LIBRARY_PATH="$ANDROID_ROOT/$LIBRARY_DIRECTORY"
1258    fi
1259
1260    # This adds libarttest(d).so to the default linker namespace when dalvikvm
1261    # is run from /apex/com.android.art/bin. Since that namespace is essentially
1262    # an alias for the com_android_art namespace, that gives libarttest(d).so
1263    # full access to the internal ART libraries.
1264    LD_LIBRARY_PATH="/data/$TEST_DIRECTORY/com.android.art/lib${SUFFIX64}:$LD_LIBRARY_PATH"
1265    if [ "$TEST_IS_NDEBUG" = "y" ]; then dlib=""; else dlib="d"; fi
1266    art_test_internal_libraries=(
1267      libartagent${dlib}.so
1268      libarttest${dlib}.so
1269      libtiagent${dlib}.so
1270      libtistress${dlib}.so
1271    )
1272    art_test_internal_libraries="${art_test_internal_libraries[*]}"
1273    NATIVELOADER_DEFAULT_NAMESPACE_LIBS="${art_test_internal_libraries// /:}"
1274    dlib=
1275    art_test_internal_libraries=
1276
1277    # Needed to access the test's Odex files.
1278    LD_LIBRARY_PATH="$DEX_LOCATION/oat/$ISA:$LD_LIBRARY_PATH"
1279    # Needed to access the test's native libraries (see e.g. 674-hiddenapi,
1280    # which generates `libhiddenapitest_*.so` libraries in `$DEX_LOCATION`).
1281    LD_LIBRARY_PATH="$DEX_LOCATION:$LD_LIBRARY_PATH"
1282
1283    # Prepend directories to the path on device.
1284    PREPEND_TARGET_PATH=$ANDROID_ART_BIN_DIR
1285    if [ "$ANDROID_ROOT" != "/system" ]; then
1286      PREPEND_TARGET_PATH="$PREPEND_TARGET_PATH:$ANDROID_ROOT/bin"
1287    fi
1288
1289    timeout_dumper_cmd=
1290
1291    # Check whether signal_dumper is available.
1292    if [ "$TIMEOUT_DUMPER" = signal_dumper ] ; then
1293      # Chroot? Use as prefix for tests.
1294      TIMEOUT_DUMPER_PATH_PREFIX=
1295      if [ -n "$CHROOT" ]; then
1296        TIMEOUT_DUMPER_PATH_PREFIX="$CHROOT/"
1297      fi
1298
1299      # Testing APEX?
1300      if adb shell "test -x ${TIMEOUT_DUMPER_PATH_PREFIX}/apex/com.android.art/bin/signal_dumper" ; then
1301        TIMEOUT_DUMPER="/apex/com.android.art/bin/signal_dumper"
1302      # Is it in /system/bin?
1303      elif adb shell "test -x ${TIMEOUT_DUMPER_PATH_PREFIX}/system/bin/signal_dumper" ; then
1304        TIMEOUT_DUMPER="/system/bin/signal_dumper"
1305      else
1306        TIMEOUT_DUMPER=
1307      fi
1308    else
1309      TIMEOUT_DUMPER=
1310    fi
1311
1312    if [ ! -z "$TIMEOUT_DUMPER" ] ; then
1313      # Use "-l" to dump to logcat. That is convenience for the build bot crash symbolization.
1314      # Use exit code 124 for toybox timeout (b/141007616).
1315      timeout_dumper_cmd="${TIMEOUT_DUMPER} -l -s 15 -e 124"
1316    fi
1317
1318    timeout_prefix=
1319    if [ "$TIME_OUT" = "timeout" ]; then
1320      # Add timeout command if time out is desired.
1321      #
1322      # Note: We first send SIGTERM (the timeout default, signal 15) to the signal dumper, which
1323      #       will induce a full thread dump before killing the process. To ensure any issues in
1324      #       dumping do not lead to a deadlock, we also use the "-k" option to definitely kill the
1325      #       child.
1326      # Note: Using "--foreground" to not propagate the signal to children, i.e., the runtime.
1327      timeout_prefix="timeout --foreground -k 120s ${TIME_OUT_VALUE}s ${timeout_dumper_cmd} $cmdline"
1328    fi
1329
1330    # Create a script with the command. The command can get longer than the longest
1331    # allowed adb command and there is no way to get the exit status from a adb shell
1332    # command. Dalvik cache is cleaned before running to make subsequent executions
1333    # of the script follow the same runtime path.
1334    cmdline="cd $DEX_LOCATION && \
1335             export ASAN_OPTIONS=$RUN_TEST_ASAN_OPTIONS && \
1336             export ANDROID_DATA=$DEX_LOCATION && \
1337             export DEX_LOCATION=$DEX_LOCATION && \
1338             export ANDROID_ROOT=$ANDROID_ROOT && \
1339             export ANDROID_I18N_ROOT=$ANDROID_I18N_ROOT && \
1340             export ANDROID_ART_ROOT=$ANDROID_ART_ROOT && \
1341             export ANDROID_TZDATA_ROOT=$ANDROID_TZDATA_ROOT && \
1342             export ANDROID_LOG_TAGS=$ANDROID_LOG_TAGS && \
1343             rm -rf ${DEX_LOCATION}/dalvik-cache/ && \
1344             mkdir -p ${mkdir_locations} && \
1345             export LD_LIBRARY_PATH=$LD_LIBRARY_PATH && \
1346             export NATIVELOADER_DEFAULT_NAMESPACE_LIBS=$NATIVELOADER_DEFAULT_NAMESPACE_LIBS && \
1347             export PATH=$PREPEND_TARGET_PATH:\$PATH && \
1348             $profman_cmdline && \
1349             $dex2oat_cmdline && \
1350             $dm_cmdline && \
1351             $vdex_cmdline && \
1352             $strip_cmdline && \
1353             $sync_cmdline && \
1354             $timeout_prefix $dalvikvm_cmdline"
1355
1356    cmdfile=$(mktemp cmd-XXXX --suffix "-$TEST_NAME")
1357    echo "$cmdline" >> $cmdfile
1358
1359    if [ "$DEV_MODE" = "y" ]; then
1360      echo $cmdline
1361      if [ "$USE_GDB" = "y" ] || [ "$USE_GDBSERVER" = "y" ]; then
1362        echo "Forward ${GDBSERVER_PORT} to local port and connect GDB"
1363      fi
1364    fi
1365
1366    if [ "$QUIET" = "n" ]; then
1367      adb push $cmdfile $CHROOT_DEX_LOCATION/cmdline.sh
1368    else
1369      adb push $cmdfile $CHROOT_DEX_LOCATION/cmdline.sh >/dev/null 2>&1
1370    fi
1371
1372    exit_status=0
1373    if [ "$DRY_RUN" != "y" ]; then
1374      if [ -n "$CHROOT" ]; then
1375        adb shell chroot "$CHROOT" sh $DEX_LOCATION/cmdline.sh
1376      else
1377        adb shell sh $DEX_LOCATION/cmdline.sh
1378      fi
1379      exit_status=$?
1380    fi
1381
1382    rm -f $cmdfile
1383    exit $exit_status
1384else
1385    # Host run.
1386    export ANDROID_PRINTF_LOG=brief
1387
1388    export ANDROID_DATA="$DEX_LOCATION"
1389    export ANDROID_ROOT="${ANDROID_ROOT}"
1390    export ANDROID_I18N_ROOT="${ANDROID_I18N_ROOT}"
1391    export ANDROID_ART_ROOT="${ANDROID_ART_ROOT}"
1392    export ANDROID_TZDATA_ROOT="${ANDROID_TZDATA_ROOT}"
1393    if [ "$USE_ZIPAPEX" = "y" ] || [ "$USE_EXRACTED_ZIPAPEX" = "y" ]; then
1394      # Put the zipapex files in front of the ld-library-path
1395      export LD_LIBRARY_PATH="${ANDROID_DATA}/zipapex/${LIBRARY_DIRECTORY}:${ANDROID_ROOT}/${TEST_DIRECTORY}"
1396      export DYLD_LIBRARY_PATH="${ANDROID_DATA}/zipapex/${LIBRARY_DIRECTORY}:${ANDROID_ROOT}/${TEST_DIRECTORY}"
1397    else
1398      export LD_LIBRARY_PATH="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}:${ANDROID_ROOT}/${TEST_DIRECTORY}"
1399      export DYLD_LIBRARY_PATH="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}:${ANDROID_ROOT}/${TEST_DIRECTORY}"
1400    fi
1401    export PATH="$PATH:$ANDROID_ART_BIN_DIR"
1402
1403    # Temporarily disable address space layout randomization (ASLR).
1404    # This is needed on the host so that the linker loads core.oat at the necessary address.
1405    export LD_USE_LOAD_BIAS=1
1406
1407    cmdline="$dalvikvm_cmdline"
1408
1409    if [ "$TIME_OUT" = "gdb" ]; then
1410      if [ `uname` = "Darwin" ]; then
1411        # Fall back to timeout on Mac.
1412        TIME_OUT="timeout"
1413      elif [ "$ISA" = "x86" ]; then
1414        # prctl call may fail in 32-bit on an older (3.2) 64-bit Linux kernel. Fall back to timeout.
1415        TIME_OUT="timeout"
1416      else
1417        # Check if gdb is available.
1418        gdb --eval-command="quit" > /dev/null 2>&1
1419        if [ $? != 0 ]; then
1420          # gdb isn't available. Fall back to timeout.
1421          TIME_OUT="timeout"
1422        fi
1423      fi
1424    fi
1425
1426    if [ "$TIME_OUT" = "timeout" ]; then
1427      # Add timeout command if time out is desired.
1428      #
1429      # Note: We first send SIGTERM (the timeout default, signal 15) to the signal dumper, which
1430      #       will induce a full thread dump before killing the process. To ensure any issues in
1431      #       dumping do not lead to a deadlock, we also use the "-k" option to definitely kill the
1432      #       child.
1433      # Note: Using "--foreground" to not propagate the signal to children, i.e., the runtime.
1434      cmdline="timeout --foreground -k 120s ${TIME_OUT_VALUE}s ${TIMEOUT_DUMPER} -s 15 $cmdline"
1435    fi
1436
1437    if [ "$DEV_MODE" = "y" ]; then
1438      for var in ANDROID_PRINTF_LOG ANDROID_DATA ANDROID_ROOT ANDROID_I18N_ROOT ANDROID_TZDATA_ROOT ANDROID_ART_ROOT LD_LIBRARY_PATH DYLD_LIBRARY_PATH PATH LD_USE_LOAD_BIAS; do
1439        echo EXPORT $var=${!var}
1440      done
1441      echo "$(declare -f linkdirs)"
1442      echo "mkdir -p ${mkdir_locations} && $setupapex_cmdline && ( $installapex_test_cmdline || $installapex_cmdline ) && $linkroot_cmdline && $linkroot_overlay_cmdline && $profman_cmdline && $dex2oat_cmdline && $dm_cmdline && $vdex_cmdline && $strip_cmdline && $sync_cmdline && $cmdline"
1443    fi
1444
1445    cd $ANDROID_BUILD_TOP
1446
1447    # Make sure we delete any existing compiler artifacts.
1448    # This enables tests to call the RUN script multiple times in a row
1449    # without worrying about interference.
1450    rm -rf ${DEX_LOCATION}/oat
1451    rm -rf ${DEX_LOCATION}/dalvik-cache/
1452
1453    export ASAN_OPTIONS=$RUN_TEST_ASAN_OPTIONS
1454
1455    mkdir -p ${mkdir_locations} || exit 1
1456    $setupapex_cmdline || { echo "zipapex extraction failed." >&2 ; exit 2; }
1457    $installapex_test_cmdline || $installapex_cmdline || { echo "zipapex install failed. cmd was: ${installapex_test_cmdline} || ${installapex_cmdline}." >&2; find ${mkdir_locations} -type f >&2; exit 2; }
1458    $linkroot_cmdline || { echo "create symlink android-root failed." >&2 ; exit 2; }
1459    $linkroot_overlay_cmdline || { echo "overlay android-root failed." >&2 ; exit 2; }
1460    $profman_cmdline || { echo "Profman failed." >&2 ; exit 2; }
1461    eval "$dex2oat_cmdline" || { echo "Dex2oat failed." >&2 ; exit 2; }
1462    eval "$dm_cmdline" || { echo "Dex2oat failed." >&2 ; exit 2; }
1463    eval "$vdex_cmdline" || { echo "Dex2oat failed." >&2 ; exit 2; }
1464    $strip_cmdline || { echo "Strip failed." >&2 ; exit 3; }
1465    $sync_cmdline || { echo "Sync failed." >&2 ; exit 4; }
1466
1467    if [ "$CREATE_RUNNER" = "y" ]; then
1468      echo "#!/bin/bash" > ${DEX_LOCATION}/runit.sh
1469      for var in ANDROID_PRINTF_LOG ANDROID_DATA ANDROID_ROOT ANDROID_I18N_ROOT ANDROID_TZDATA_ROOT ANDROID_ART_ROOT LD_LIBRARY_PATH DYLD_LIBRARY_PATH PATH LD_USE_LOAD_BIAS; do
1470        echo export $var="${!var}" >> ${DEX_LOCATION}/runit.sh
1471      done
1472      if [ "$DEV_MODE" = "y" ]; then
1473        echo $cmdline >> ${DEX_LOCATION}/runit.sh
1474      else
1475        echo 'STDERR=$(mktemp)' >> ${DEX_LOCATION}/runit.sh
1476        echo 'STDOUT=$(mktemp)' >> ${DEX_LOCATION}/runit.sh
1477        echo $cmdline '>${STDOUT} 2>${STDERR}' >> ${DEX_LOCATION}/runit.sh
1478        echo 'if diff ${STDOUT} $ANDROID_DATA/expected-stdout.txt; then' \
1479          >> ${DEX_LOCATION}/runit.sh
1480        echo '  rm -f ${STDOUT} ${STDERR}' >> ${DEX_LOCATION}/runit.sh
1481        echo '  exit 0' >> ${DEX_LOCATION}/runit.sh
1482        echo 'elif diff ${STDERR} $ANDROID_DATA/expected-stderr.txt; then' \
1483          >> ${DEX_LOCATION}/runit.sh
1484        echo '  rm -f ${STDOUT} ${STDERR}' >> ${DEX_LOCATION}/runit.sh
1485        echo '  exit 0' >> ${DEX_LOCATION}/runit.sh
1486        echo 'else' >> ${DEX_LOCATION}/runit.sh
1487        echo '  echo  STDOUT:' >> ${DEX_LOCATION}/runit.sh
1488        echo '  cat ${STDOUT}' >> ${DEX_LOCATION}/runit.sh
1489        echo '  echo  STDERR:' >> ${DEX_LOCATION}/runit.sh
1490        echo '  cat ${STDERR}' >> ${DEX_LOCATION}/runit.sh
1491        echo '  rm -f ${STDOUT} ${STDERR}' >> ${DEX_LOCATION}/runit.sh
1492        echo '  exit 1' >> ${DEX_LOCATION}/runit.sh
1493        echo 'fi' >> ${DEX_LOCATION}/runit.sh
1494      fi
1495      chmod u+x $DEX_LOCATION/runit.sh
1496      echo "Runnable test script written to ${DEX_LOCATION}/runit.sh"
1497    fi
1498    if [ "$DRY_RUN" = "y" ]; then
1499      exit 0
1500    fi
1501
1502    if [ "$USE_GDB" = "y" ]; then
1503      # When running under gdb, we cannot do piping and grepping...
1504      $cmdline "$@"
1505    elif [ "$USE_GDBSERVER" = "y" ]; then
1506      echo "Connect to $GDBSERVER_PORT"
1507      # When running under gdb, we cannot do piping and grepping...
1508      $cmdline "$@"
1509    else
1510      if [ "$TIME_OUT" != "gdb" ]; then
1511        trap 'kill -INT -$pid' INT
1512        $cmdline "$@" & pid=$!
1513        wait $pid
1514        exit_value=$?
1515        # Add extra detail if time out is enabled.
1516        if [ $exit_value = 124 ] && [ "$TIME_OUT" = "timeout" ]; then
1517          echo -e "\e[91mTEST TIMED OUT!\e[0m" >&2
1518        fi
1519        exit $exit_value
1520      else
1521        # With a thread dump that uses gdb if a timeout.
1522        trap 'kill -INT -$pid' INT
1523        $cmdline "$@" & pid=$!
1524        # Spawn a watcher process.
1525        ( sleep $TIME_OUT_VALUE && \
1526          echo "##### Thread dump using gdb on test timeout" && \
1527          ( gdb -q -p $pid --eval-command="info thread" --eval-command="thread apply all bt" \
1528                           --eval-command="call exit(124)" --eval-command=quit || \
1529            kill $pid )) 2> /dev/null & watcher=$!
1530        wait $pid
1531        test_exit_status=$?
1532        pkill -P $watcher 2> /dev/null # kill the sleep which will in turn end the watcher as well
1533        if [ $test_exit_status = 0 ]; then
1534          # The test finished normally.
1535          exit 0
1536        else
1537          # The test failed or timed out.
1538          if [ $test_exit_status = 124 ]; then
1539            # The test timed out.
1540            echo -e "\e[91mTEST TIMED OUT!\e[0m" >&2
1541          fi
1542          exit $test_exit_status
1543        fi
1544      fi
1545    fi
1546fi
1547