blob: de6b8feeed2ac55b85ff8a16a1f55324eb54b0e0 [file] [log] [blame]
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001#
David Srbeckybfa76b32022-06-20 21:30:56 +01002# Copyright (C) 2022 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +010015
David Srbeckybfa76b32022-06-20 21:30:56 +010016import sys, os, shutil, shlex, re, subprocess, glob
David Srbeckye4e701b2022-10-24 11:00:31 +000017from argparse import ArgumentParser, BooleanOptionalAction, Namespace
David Srbeckybfa76b32022-06-20 21:30:56 +010018from os import path
David Srbecky35a48ce2022-11-09 12:19:29 +000019from os.path import isfile, isdir
David Srbeckybfa76b32022-06-20 21:30:56 +010020from typing import List
21from subprocess import DEVNULL, PIPE, STDOUT
David Srbeckyd7674c22022-10-03 18:22:13 +010022from tempfile import NamedTemporaryFile
David Srbeckybfa76b32022-06-20 21:30:56 +010023
David Srbeckybfa76b32022-06-20 21:30:56 +010024
David Srbeckye4e701b2022-10-24 11:00:31 +000025def parse_args(argv):
David Srbecky94d21412022-10-10 16:02:51 +010026 argp, opt_bool = ArgumentParser(), BooleanOptionalAction
27 argp.add_argument("--64", dest="is64", action="store_true")
28 argp.add_argument("--O", action="store_true")
29 argp.add_argument("--Xcompiler-option", default=[], action="append")
30 argp.add_argument("--add-libdir-argument", action="store_true")
31 argp.add_argument("--android-art-root", default="/apex/com.android.art")
32 argp.add_argument("--android-i18n-root", default="/apex/com.android.i18n")
33 argp.add_argument("--android-root", default="/system")
34 argp.add_argument("--android-runtime-option", default=[], action="append")
35 argp.add_argument("--android-tzdata-root", default="/apex/com.android.tzdata")
36 argp.add_argument("--app-image", default=True, action=opt_bool)
David Srbecky94d21412022-10-10 16:02:51 +010037 argp.add_argument("--baseline", action="store_true")
38 argp.add_argument("--bionic", action="store_true")
39 argp.add_argument("--boot", default="")
40 argp.add_argument("--chroot", default="")
41 argp.add_argument("--compact-dex-level")
42 argp.add_argument("--compiler-only-option", default=[], action="append")
43 argp.add_argument("--create-runner", action="store_true")
44 argp.add_argument("--debug", action="store_true")
45 argp.add_argument("--debug-agent")
46 argp.add_argument("--debug-wrap-agent", action="store_true")
47 argp.add_argument("--dex2oat-dm", action="store_true")
48 argp.add_argument(
49 "--dex2oat-rt-timeout", type=int,
50 default=360) # The *hard* timeout. 6 min.
51 argp.add_argument(
52 "--dex2oat-timeout", type=int, default=300) # The "soft" timeout. 5 min.
53 argp.add_argument("--dry-run", action="store_true")
54 argp.add_argument("--experimental", default=[], action="append")
55 argp.add_argument("--external-log-tags", action="store_true")
56 argp.add_argument("--gc-stress", action="store_true")
57 argp.add_argument("--gdb", action="store_true")
58 argp.add_argument("--gdb-arg", default=[], action="append")
59 argp.add_argument("--gdb-dex2oat", action="store_true")
60 argp.add_argument("--gdb-dex2oat-args", default=[], action="append")
61 argp.add_argument("--gdbserver", action="store_true")
62 argp.add_argument("--gdbserver-bin")
63 argp.add_argument("--gdbserver-port", default=":5039")
64 argp.add_argument("--host", action="store_true")
65 argp.add_argument("--image", default=True, action=opt_bool)
66 argp.add_argument("--instruction-set-features", default="")
67 argp.add_argument("--interpreter", action="store_true")
68 argp.add_argument("--invoke-with", default=[], action="append")
69 argp.add_argument("--jit", action="store_true")
70 argp.add_argument("--jvm", action="store_true")
71 argp.add_argument("--jvmti", action="store_true")
72 argp.add_argument("--jvmti-field-stress", action="store_true")
73 argp.add_argument("--jvmti-redefine-stress", action="store_true")
74 argp.add_argument("--jvmti-step-stress", action="store_true")
75 argp.add_argument("--jvmti-trace-stress", action="store_true")
76 argp.add_argument("--lib", default="")
77 argp.add_argument("--optimize", default=True, action=opt_bool)
78 argp.add_argument("--prebuild", default=True, action=opt_bool)
79 argp.add_argument("--profile", action="store_true")
80 argp.add_argument("--random-profile", action="store_true")
81 argp.add_argument("--relocate", default=False, action=opt_bool)
82 argp.add_argument("--runtime-dm", action="store_true")
83 argp.add_argument("--runtime-extracted-zipapex", default="")
84 argp.add_argument("--runtime-option", default=[], action="append")
85 argp.add_argument("--runtime-zipapex", default="")
86 argp.add_argument("--secondary", action="store_true")
87 argp.add_argument("--secondary-app-image", default=True, action=opt_bool)
88 argp.add_argument("--secondary-class-loader-context", default="")
89 argp.add_argument("--secondary-compilation", default=True, action=opt_bool)
90 argp.add_argument("--simpleperf", action="store_true")
91 argp.add_argument("--sync", action="store_true")
92 argp.add_argument("--testlib", default=[], action="append")
93 argp.add_argument("--timeout", default=0, type=int)
94 argp.add_argument("--vdex", action="store_true")
95 argp.add_argument("--vdex-arg", default=[], action="append")
David Srbeckye4e701b2022-10-24 11:00:31 +000096 argp.add_argument("--vdex-filter", default="")
David Srbecky94d21412022-10-10 16:02:51 +010097 argp.add_argument("--verbose", action="store_true")
98 argp.add_argument("--verify", default=True, action=opt_bool)
99 argp.add_argument("--verify-soft-fail", action="store_true")
100 argp.add_argument("--with-agent", default=[], action="append")
101 argp.add_argument("--zygote", action="store_true")
David Srbeckye4e701b2022-10-24 11:00:31 +0000102 argp.add_argument("--test_args", default=[], action="append")
103 argp.add_argument("--stdout_file", default="")
104 argp.add_argument("--stderr_file", default="")
105 argp.add_argument("--main", default="Main")
106 argp.add_argument("--expected_exit_code", default=0)
David Srbecky94d21412022-10-10 16:02:51 +0100107
David Srbecky94d21412022-10-10 16:02:51 +0100108 # Python parser requires the format --key=--value, since without the equals symbol
109 # it looks like the required value has been omitted and there is just another flag.
110 # For example, '--args --foo --host --64' will become '--arg=--foo --host --64'
111 # because otherwise the --args is missing its value and --foo is unknown argument.
112 for i, arg in reversed(list(enumerate(argv))):
113 if arg in [
114 "--args", "--runtime-option", "--android-runtime-option",
115 "-Xcompiler-option", "--compiler-only-option"
116 ]:
117 argv[i] += "=" + argv.pop(i + 1)
David Srbeckye4e701b2022-10-24 11:00:31 +0000118
119 # Accept single-dash arguments as if they were double-dash arguments.
120 # For exmpample, '-Xcompiler-option' becomes '--Xcompiler-option'
121 # became single-dash can be used only with single-letter arguments.
David Srbecky94d21412022-10-10 16:02:51 +0100122 for i, arg in list(enumerate(argv)):
123 if arg.startswith("-") and not arg.startswith("--"):
124 argv[i] = "-" + arg
125 if arg == "--":
126 break
David Srbecky85786e72022-09-21 23:54:01 +0100127
David Srbeckye4e701b2022-10-24 11:00:31 +0000128 return argp.parse_args(argv)
129
130
David Srbeckye0cbf8e2022-11-03 10:42:21 +0000131# Note: This must start with the CORE_IMG_JARS in Android.common_path.mk
132# because that's what we use for compiling the boot.art image.
133# It may contain additional modules from TEST_CORE_JARS.
134bpath_modules = ("core-oj core-libart okhttp bouncycastle apache-xml core-icu4j"
135 " conscrypt")
136
137
138# Helper function to construct paths for apex modules (for both -Xbootclasspath and
139# -Xbootclasspath-location).
140def get_apex_bootclasspath_impl(bpath_prefix: str):
141 bpath_separator = ""
142 bpath = ""
143 bpath_jar = ""
144 for bpath_module in bpath_modules.split(" "):
145 apex_module = "com.android.art"
146 if bpath_module == "conscrypt":
147 apex_module = "com.android.conscrypt"
148 if bpath_module == "core-icu4j":
149 apex_module = "com.android.i18n"
150 bpath_jar = f"/apex/{apex_module}/javalib/{bpath_module}.jar"
151 bpath += f"{bpath_separator}{bpath_prefix}{bpath_jar}"
152 bpath_separator = ":"
153 return bpath
154
155
156# Gets a -Xbootclasspath paths with the apex modules.
157def get_apex_bootclasspath(host: bool):
158 bpath_prefix = ""
159
160 if host:
161 bpath_prefix = os.environ["ANDROID_HOST_OUT"]
162
163 return get_apex_bootclasspath_impl(bpath_prefix)
164
165
166# Gets a -Xbootclasspath-location paths with the apex modules.
167def get_apex_bootclasspath_locations(host: bool):
168 bpath_location_prefix = ""
169
170 if host:
171 ANDROID_BUILD_TOP=os.environ["ANDROID_BUILD_TOP"]
172 ANDROID_HOST_OUT=os.environ["ANDROID_HOST_OUT"]
173 if ANDROID_HOST_OUT[0:len(ANDROID_BUILD_TOP)+1] == f"{ANDROID_BUILD_TOP}/":
174 bpath_location_prefix=ANDROID_HOST_OUT[len(ANDROID_BUILD_TOP)+1:]
175 else:
176 print(f"ANDROID_BUILD_TOP/ is not a prefix of ANDROID_HOST_OUT"\
177 "\nANDROID_BUILD_TOP={ANDROID_BUILD_TOP}"\
178 "\nANDROID_HOST_OUT={ANDROID_HOST_OUT}")
179 sys.exit(1)
180
181 return get_apex_bootclasspath_impl(bpath_location_prefix)
182
183
David Srbeckye4e701b2022-10-24 11:00:31 +0000184def default_run(ctx, args, **kwargs):
185 # Clone the args so we can modify them without affecting args in the caller.
186 args = Namespace(**vars(args))
187
188 # Overwrite args based on the named parameters.
189 # E.g. the caller can do `default_run(args, jvmti=True)` to modify args.jvmti.
190 for name, new_value in kwargs.items():
191 old_value = getattr(args, name)
192 assert isinstance(new_value, old_value.__class__), name + " should have type " + str(old_value.__class__)
193 if isinstance(old_value, list):
194 setattr(args, name, old_value + new_value) # Lists get merged.
195 else:
196 setattr(args, name, new_value)
197
198 # Script debugging: Record executed commands into the given directory.
199 # This is useful to ensure that changes to the script don't change behaviour.
200 # (the commands are appended so the directory needs to be cleared before run)
201 ART_TEST_CMD_DIR = os.environ.get("ART_TEST_CMD_DIR")
202
203 # Script debugging: Record executed commands, but don't actually run the main test.
204 # This makes it possible the extract the test commands without waiting for days.
205 # This will make tests fail since there is no stdout. Use with large -j value.
206 ART_TEST_DRY_RUN = os.environ.get("ART_TEST_DRY_RUN")
207
208 def run(cmdline: str,
209 env={},
David Srbecky35a48ce2022-11-09 12:19:29 +0000210 stdout_file=None,
211 stderr_file=None,
David Srbeckye4e701b2022-10-24 11:00:31 +0000212 check=True,
David Srbeckydb121732022-10-31 18:40:26 +0000213 parse_exit_code_from_stdout=False,
David Srbeckye4e701b2022-10-24 11:00:31 +0000214 expected_exit_code=0,
215 save_cmd=True) -> subprocess.CompletedProcess:
216 env.setdefault("PATH", PATH) # Ensure that PATH is always set.
217 env = {k: v for k, v in env.items() if v != None} # Filter "None" entries.
218 if ART_TEST_CMD_DIR and save_cmd and cmdline != "true":
219 tmp = os.environ["DEX_LOCATION"]
220 with open(
221 os.path.join(ART_TEST_CMD_DIR, os.environ["FULL_TEST_NAME"]),
222 "a") as f:
223 # Replace DEX_LOCATION (which is randomly generated temporary directory),
224 # with a deterministic placeholder so that we can do a diff from run to run.
225 f.write("\n".join(
226 k + ":" + v.replace(tmp, "<tmp>") for k, v in env.items()) + "\n\n")
227 f.write(re.sub(" +", "\n", cmdline).replace(tmp, "<tmp>") + "\n\n")
228 if ART_TEST_DRY_RUN and ("dalvikvm" in cmdline or
229 "adb shell chroot" in cmdline):
230 cmdline = "true" # We still need to run some command, so run the no-op "true" binary instead.
231 proc = subprocess.run([cmdline],
232 shell=True,
233 env=env,
234 encoding="utf8",
235 capture_output=True)
236
David Srbeckydb121732022-10-31 18:40:26 +0000237 # ADB forwards exit code from the executed command, but if ADB process itself crashes,
238 # it will also return non-zero exit code, and we can not distinguish those two cases.
239 # As a work-around, we wrap the executed command so that it always returns 0 exit code
240 # and we also make it print the actual exit code as the last line of its stdout.
241 if parse_exit_code_from_stdout:
242 assert proc.returncode == 0, f"ADB crashed (out:'{proc.stdout}' err:'{proc.stderr}')"
243 found = re.search("exit_code=([0-9]+)$", proc.stdout)
244 assert found, "Expected exit code as the last line of stdout"
245 proc.stdout = proc.stdout[:found.start(0)] # Remove the exit code from stdout.
246 proc.returncode = int(found.group(1)) # Use it as if it was the process exit code.
247
David Srbecky35a48ce2022-11-09 12:19:29 +0000248 # Save copy of the output on disk.
249 if stdout_file:
250 with open(stdout_file, "a") as f:
251 f.write(proc.stdout)
252 if stderr_file:
253 with open(stderr_file, "a") as f:
254 f.write(proc.stderr)
255
David Srbeckye4e701b2022-10-24 11:00:31 +0000256 # Check the exit code.
257 if (check and proc.returncode != expected_exit_code) or VERBOSE:
258 print("$ " + cmdline)
259 print(proc.stdout or "", file=sys.stdout, end="", flush=True)
260 print(proc.stderr or "", file=sys.stderr, end="", flush=True)
261 if (check and proc.returncode != expected_exit_code):
262 suffix = ""
263 if proc.returncode == 124:
264 suffix = " (TIME OUT)"
265 elif expected_exit_code != 0:
266 suffix = " (expected {})".format(expected_exit_code)
267 raise Exception("Command returned exit code {}{}".format(proc.returncode, suffix))
268
269 return proc
270
271 class Adb():
272
273 def __init__(self):
274 self.env = {
275 "ADB_VENDOR_KEYS": os.environ.get("ADB_VENDOR_KEYS"),
276 "ANDROID_SERIAL": os.environ.get("ANDROID_SERIAL"),
277 "PATH": os.environ.get("PATH"),
278 }
279
280 def root(self) -> None:
281 run("adb root", self.env)
282
283 def wait_for_device(self) -> None:
284 run("adb wait-for-device", self.env)
285
286 def shell(self, cmdline: str, **kwargs) -> subprocess.CompletedProcess:
David Srbeckydb121732022-10-31 18:40:26 +0000287 return run(f"adb shell '{cmdline}; echo exit_code=$?'", self.env,
288 parse_exit_code_from_stdout=True, **kwargs)
David Srbeckye4e701b2022-10-24 11:00:31 +0000289
290 def push(self, src: str, dst: str, **kwargs) -> None:
291 run(f"adb push {src} {dst}", self.env, **kwargs)
292
293 adb = Adb()
294
295 local_path = os.path.dirname(__file__)
296
297 # Check that stdout is connected to a terminal and that we have at least 1 color.
298 # This ensures that if the stdout is not connected to a terminal and instead
299 # the stdout will be used for a log, it will not append the color characters.
300 bold_red = ""
301 if sys.stdout.isatty():
302 if int(subprocess.run(["tput", "colors"], capture_output=True).stdout) >= 1:
303 bold_red = subprocess.run(["tput", "bold"],
304 text=True,
305 capture_output=True).stdout.strip()
306 bold_red += subprocess.run(["tput", "setaf", "1"],
307 text=True,
308 capture_output=True).stdout.strip()
309
310 ANDROID_BUILD_TOP = os.environ.get("ANDROID_BUILD_TOP")
311 ANDROID_DATA = os.environ.get("ANDROID_DATA")
312 ANDROID_HOST_OUT = os.environ["ANDROID_HOST_OUT"]
313 ANDROID_LOG_TAGS = os.environ.get("ANDROID_LOG_TAGS", "")
314 ART_TIME_OUT_MULTIPLIER = int(os.environ.get("ART_TIME_OUT_MULTIPLIER", 1))
315 DEX2OAT = os.environ.get("DEX2OAT", "")
316 DEX_LOCATION = os.environ["DEX_LOCATION"]
317 JAVA = os.environ.get("JAVA")
318 OUT_DIR = os.environ.get("OUT_DIR")
319 PATH = os.environ.get("PATH", "")
320 SANITIZE_HOST = os.environ.get("SANITIZE_HOST", "")
321 TEST_NAME = os.environ["TEST_NAME"]
322 USE_EXRACTED_ZIPAPEX = os.environ.get("USE_EXRACTED_ZIPAPEX", "")
323
324 assert ANDROID_BUILD_TOP, "Did you forget to run `lunch`?"
325
326 def msg(msg: str):
327 if VERBOSE:
328 print(msg)
David Srbecky85786e72022-09-21 23:54:01 +0100329
David Srbecky94d21412022-10-10 16:02:51 +0100330 ANDROID_ROOT = args.android_root
331 ANDROID_ART_ROOT = args.android_art_root
332 ANDROID_I18N_ROOT = args.android_i18n_root
333 ANDROID_TZDATA_ROOT = args.android_tzdata_root
334 ARCHITECTURES_32 = "(arm|x86|none)"
335 ARCHITECTURES_64 = "(arm64|x86_64|none)"
336 ARCHITECTURES_PATTERN = ARCHITECTURES_32
337 GET_DEVICE_ISA_BITNESS_FLAG = "--32"
338 BOOT_IMAGE = args.boot
339 CHROOT = args.chroot
340 COMPILE_FLAGS = ""
341 DALVIKVM = "dalvikvm32"
342 DEBUGGER = "n"
343 WITH_AGENT = args.with_agent
344 DEBUGGER_AGENT = args.debug_agent
345 WRAP_DEBUGGER_AGENT = args.debug_wrap_agent
346 VERBOSE = args.verbose
347 DEX2OAT_NDEBUG_BINARY = "dex2oat32"
348 DEX2OAT_DEBUG_BINARY = "dex2oatd32"
349 EXPERIMENTAL = args.experimental
350 FALSE_BIN = "false"
351 FLAGS = ""
352 ANDROID_FLAGS = ""
353 GDB = ""
354 GDB_ARGS = ""
355 GDB_DEX2OAT = ""
356 GDB_DEX2OAT_ARGS = ""
357 GDBSERVER_DEVICE = "gdbserver"
358 GDBSERVER_HOST = "gdbserver"
359 HAVE_IMAGE = args.image
360 HOST = args.host
361 BIONIC = args.bionic
362 CREATE_ANDROID_ROOT = False
363 USE_ZIPAPEX = (args.runtime_zipapex != "")
364 ZIPAPEX_LOC = args.runtime_zipapex
365 USE_EXTRACTED_ZIPAPEX = (args.runtime_extracted_zipapex != "")
366 EXTRACTED_ZIPAPEX_LOC = args.runtime_extracted_zipapex
367 INTERPRETER = args.interpreter
368 JIT = args.jit
369 INVOKE_WITH = " ".join(args.invoke_with)
370 USE_JVMTI = args.jvmti
371 IS_JVMTI_TEST = False
372 ADD_LIBDIR_ARGUMENTS = args.add_libdir_argument
373 SUFFIX64 = ""
374 ISA = "x86"
375 LIBRARY_DIRECTORY = "lib"
376 TEST_DIRECTORY = "nativetest"
David Srbeckye4e701b2022-10-24 11:00:31 +0000377 MAIN = args.main
David Srbecky94d21412022-10-10 16:02:51 +0100378 OPTIMIZE = args.optimize
379 PREBUILD = args.prebuild
380 RELOCATE = args.relocate
381 SECONDARY_DEX = ""
382 TIME_OUT = "n" # "n" (disabled), "timeout" (use timeout), "gdb" (use gdb)
383 TIMEOUT_DUMPER = "signal_dumper"
384 # Values in seconds.
385 TIME_OUT_EXTRA = 0
386 TIME_OUT_VALUE = args.timeout
387 USE_GDB = args.gdb
388 USE_GDBSERVER = args.gdbserver
389 GDBSERVER_PORT = args.gdbserver_port
390 USE_GDB_DEX2OAT = args.gdb_dex2oat
391 USE_JVM = args.jvm
392 VERIFY = "y" if args.verify else "n" # y=yes,n=no,s=softfail
393 ZYGOTE = ""
394 DEX_VERIFY = ""
395 INSTRUCTION_SET_FEATURES = args.instruction_set_features
396 ARGS = ""
397 VDEX_ARGS = ""
398 EXTERNAL_LOG_TAGS = args.external_log_tags # respect external ANDROID_LOG_TAGS.
399 DRY_RUN = args.dry_run
400 TEST_VDEX = args.vdex
401 TEST_DEX2OAT_DM = args.dex2oat_dm
402 TEST_RUNTIME_DM = args.runtime_dm
403 TEST_IS_NDEBUG = args.O
404 APP_IMAGE = args.app_image
405 SECONDARY_APP_IMAGE = args.secondary_app_image
406 SECONDARY_CLASS_LOADER_CONTEXT = args.secondary_class_loader_context
407 SECONDARY_COMPILATION = args.secondary_compilation
408 JVMTI_STRESS = False
409 JVMTI_REDEFINE_STRESS = args.jvmti_redefine_stress
410 JVMTI_STEP_STRESS = args.jvmti_step_stress
411 JVMTI_FIELD_STRESS = args.jvmti_field_stress
412 JVMTI_TRACE_STRESS = args.jvmti_trace_stress
413 PROFILE = args.profile
414 RANDOM_PROFILE = args.random_profile
415 DEX2OAT_TIMEOUT = args.dex2oat_timeout
416 DEX2OAT_RT_TIMEOUT = args.dex2oat_rt_timeout
417 CREATE_RUNNER = args.create_runner
418 INT_OPTS = ""
419 SIMPLEPERF = args.simpleperf
420 DEBUGGER_OPTS = ""
421 JVM_VERIFY_ARG = ""
422 LIB = args.lib
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100423
David Srbecky94d21412022-10-10 16:02:51 +0100424 # if True, run 'sync' before dalvikvm to make sure all files from
425 # build step (e.g. dex2oat) were finished writing.
426 SYNC_BEFORE_RUN = args.sync
Igor Murashkin271a0f82017-02-14 21:14:17 +0000427
David Srbecky94d21412022-10-10 16:02:51 +0100428 # When running a debug build, we want to run with all checks.
429 ANDROID_FLAGS += " -XX:SlowDebug=true"
430 # The same for dex2oatd, both prebuild and runtime-driven.
431 ANDROID_FLAGS += (" -Xcompiler-option --runtime-arg -Xcompiler-option "
432 "-XX:SlowDebug=true")
433 COMPILER_FLAGS = " --runtime-arg -XX:SlowDebug=true"
Andreas Gampe1c5b42f2017-06-15 18:20:45 -0700434
David Srbecky94d21412022-10-10 16:02:51 +0100435 # Let the compiler and runtime know that we are running tests.
436 COMPILE_FLAGS += " --compile-art-test"
437 ANDROID_FLAGS += " -Xcompiler-option --compile-art-test"
David Srbecky4fa07a52020-03-31 20:52:09 +0100438
David Srbecky94d21412022-10-10 16:02:51 +0100439 if USE_JVMTI:
440 IS_JVMTI_TEST = True
David Srbecky85786e72022-09-21 23:54:01 +0100441 # Secondary images block some tested behavior.
David Srbecky94d21412022-10-10 16:02:51 +0100442 SECONDARY_APP_IMAGE = False
443 if args.gc_stress:
David Srbecky85786e72022-09-21 23:54:01 +0100444 # Give an extra 20 mins if we are gc-stress.
David Srbecky94d21412022-10-10 16:02:51 +0100445 TIME_OUT_EXTRA += 1200
446 for arg in args.testlib:
447 ARGS += f" {arg}"
David Srbeckye4e701b2022-10-24 11:00:31 +0000448 for arg in args.test_args:
David Srbecky94d21412022-10-10 16:02:51 +0100449 ARGS += f" {arg}"
450 for arg in args.compiler_only_option:
451 COMPILE_FLAGS += f" {arg}"
452 for arg in args.Xcompiler_option:
453 FLAGS += f" -Xcompiler-option {arg}"
454 COMPILE_FLAGS += f" {arg}"
455 if args.secondary:
456 SECONDARY_DEX = f":{DEX_LOCATION}/{TEST_NAME}-ex.jar"
David Srbecky85786e72022-09-21 23:54:01 +0100457 # Enable cfg-append to make sure we get the dump for both dex files.
458 # (otherwise the runtime compilation of the secondary dex will overwrite
459 # the dump of the first one).
David Srbecky94d21412022-10-10 16:02:51 +0100460 FLAGS += " -Xcompiler-option --dump-cfg-append"
461 COMPILE_FLAGS += " --dump-cfg-append"
462 for arg in args.android_runtime_option:
463 ANDROID_FLAGS += f" {arg}"
464 for arg in args.runtime_option:
465 FLAGS += f" {arg}"
David Srbecky85786e72022-09-21 23:54:01 +0100466 if arg == "-Xmethod-trace":
David Srbecky94d21412022-10-10 16:02:51 +0100467 # Method tracing can slow some tests down a lot.
468 TIME_OUT_EXTRA += 1200
469 if args.compact_dex_level:
470 arg = args.compact_dex_level
471 COMPILE_FLAGS += f" --compact-dex-level={arg}"
472 if JVMTI_REDEFINE_STRESS:
David Srbecky85786e72022-09-21 23:54:01 +0100473 # APP_IMAGE doesn't really work with jvmti redefine stress
David Srbecky94d21412022-10-10 16:02:51 +0100474 SECONDARY_APP_IMAGE = False
475 JVMTI_STRESS = True
476 if JVMTI_REDEFINE_STRESS or JVMTI_STEP_STRESS or JVMTI_FIELD_STRESS or JVMTI_TRACE_STRESS:
477 USE_JVMTI = True
478 JVMTI_STRESS = True
479 if HOST:
480 ANDROID_ROOT = ANDROID_HOST_OUT
481 ANDROID_ART_ROOT = f"{ANDROID_HOST_OUT}/com.android.art"
482 ANDROID_I18N_ROOT = f"{ANDROID_HOST_OUT}/com.android.i18n"
483 ANDROID_TZDATA_ROOT = f"{ANDROID_HOST_OUT}/com.android.tzdata"
David Srbecky85786e72022-09-21 23:54:01 +0100484 # On host, we default to using the symlink, as the PREFER_32BIT
485 # configuration is the only configuration building a 32bit version of
486 # dex2oat.
David Srbecky94d21412022-10-10 16:02:51 +0100487 DEX2OAT_DEBUG_BINARY = "dex2oatd"
488 DEX2OAT_NDEBUG_BINARY = "dex2oat"
489 if BIONIC:
David Srbecky85786e72022-09-21 23:54:01 +0100490 # We need to create an ANDROID_ROOT because currently we cannot create
491 # the frameworks/libcore with linux_bionic so we need to use the normal
492 # host ones which are in a different location.
David Srbecky94d21412022-10-10 16:02:51 +0100493 CREATE_ANDROID_ROOT = True
494 if USE_ZIPAPEX:
David Srbecky85786e72022-09-21 23:54:01 +0100495 # TODO (b/119942078): Currently apex does not support
496 # symlink_preferred_arch so we will not have a dex2oatd to execute and
497 # need to manually provide
498 # dex2oatd64.
David Srbecky94d21412022-10-10 16:02:51 +0100499 DEX2OAT_DEBUG_BINARY = "dex2oatd64"
500 if WITH_AGENT:
501 USE_JVMTI = True
502 if DEBUGGER_AGENT:
503 DEBUGGER = "agent"
504 USE_JVMTI = True
505 TIME_OUT = "n"
506 if args.debug:
507 USE_JVMTI = True
508 DEBUGGER = "y"
509 TIME_OUT = "n"
510 if args.gdbserver_bin:
David Srbecky85786e72022-09-21 23:54:01 +0100511 arg = args.gdbserver_bin
David Srbecky94d21412022-10-10 16:02:51 +0100512 GDBSERVER_HOST = arg
513 GDBSERVER_DEVICE = arg
514 if args.gdbserver or args.gdb or USE_GDB_DEX2OAT:
515 VERBOSE = True
516 TIME_OUT = "n"
517 for arg in args.gdb_arg:
518 GDB_ARGS += f" {arg}"
519 if args.gdb_dex2oat_args:
David Srbecky85786e72022-09-21 23:54:01 +0100520 for arg in arg.split(";"):
David Srbecky94d21412022-10-10 16:02:51 +0100521 GDB_DEX2OAT_ARGS += f"{arg} "
522 if args.zygote:
523 ZYGOTE = "-Xzygote"
David Srbecky85786e72022-09-21 23:54:01 +0100524 msg("Spawning from zygote")
David Srbecky94d21412022-10-10 16:02:51 +0100525 if args.baseline:
526 FLAGS += " -Xcompiler-option --baseline"
527 COMPILE_FLAGS += " --baseline"
528 if args.verify_soft_fail:
529 VERIFY = "s"
530 if args.is64:
531 SUFFIX64 = "64"
532 ISA = "x86_64"
533 GDBSERVER_DEVICE = "gdbserver64"
534 DALVIKVM = "dalvikvm64"
535 LIBRARY_DIRECTORY = "lib64"
536 TEST_DIRECTORY = "nativetest64"
537 ARCHITECTURES_PATTERN = ARCHITECTURES_64
538 GET_DEVICE_ISA_BITNESS_FLAG = "--64"
539 DEX2OAT_NDEBUG_BINARY = "dex2oat64"
540 DEX2OAT_DEBUG_BINARY = "dex2oatd64"
541 if args.vdex_filter:
542 option = args.vdex_filter
543 VDEX_ARGS += f" --compiler-filter={option}"
544 if args.vdex_arg:
David Srbecky85786e72022-09-21 23:54:01 +0100545 arg = args.vdex_arg
David Srbecky94d21412022-10-10 16:02:51 +0100546 VDEX_ARGS += f" {arg}"
David Srbecky4c93c252022-09-08 13:17:44 +0100547
Roland Levillainb59f5fb2020-02-19 16:22:48 +0000548# HACK: Force the use of `signal_dumper` on host.
David Srbecky94d21412022-10-10 16:02:51 +0100549 if HOST:
550 TIME_OUT = "timeout"
Roland Levillain505e56b2019-11-20 08:49:24 +0000551
Andreas Gampe6ad30a22019-09-12 15:12:36 -0700552# If you change this, update the timeout in testrunner.py as well.
David Srbecky94d21412022-10-10 16:02:51 +0100553 if not TIME_OUT_VALUE:
554 # 10 minutes is the default.
555 TIME_OUT_VALUE = 600
Andreas Gampe6ad30a22019-09-12 15:12:36 -0700556
David Srbecky94d21412022-10-10 16:02:51 +0100557 # For sanitized builds use a larger base.
558 # TODO: Consider sanitized target builds?
559 if SANITIZE_HOST != "":
560 TIME_OUT_VALUE = 1500 # 25 minutes.
Andreas Gampe6ad30a22019-09-12 15:12:36 -0700561
David Srbecky94d21412022-10-10 16:02:51 +0100562 TIME_OUT_VALUE += TIME_OUT_EXTRA
Andreas Gampe6ad30a22019-09-12 15:12:36 -0700563
Andreas Gampe29bfb0c2019-09-12 15:17:43 -0700564# Escape hatch for slow hosts or devices. Accept an environment variable as a timeout factor.
David Srbecky94d21412022-10-10 16:02:51 +0100565 if ART_TIME_OUT_MULTIPLIER:
566 TIME_OUT_VALUE *= ART_TIME_OUT_MULTIPLIER
Andreas Gampe29bfb0c2019-09-12 15:17:43 -0700567
Roland Levillain76cfe612017-10-30 13:14:28 +0000568# The DEX_LOCATION with the chroot prefix, if any.
David Srbecky94d21412022-10-10 16:02:51 +0100569 CHROOT_DEX_LOCATION = f"{CHROOT}{DEX_LOCATION}"
Roland Levillain76cfe612017-10-30 13:14:28 +0000570
David Srbecky94d21412022-10-10 16:02:51 +0100571 # If running on device, determine the ISA of the device.
572 if not HOST and not USE_JVM:
573 ISA = run(
574 f"{ANDROID_BUILD_TOP}/art/test/utils/get-device-isa {GET_DEVICE_ISA_BITNESS_FLAG}",
575 adb.env,
576 save_cmd=False).stdout.strip()
Roland Levillain72f67742019-03-06 15:48:08 +0000577
David Srbecky94d21412022-10-10 16:02:51 +0100578 if not USE_JVM:
579 FLAGS += f" {ANDROID_FLAGS}"
Alex Lightbaac7e42018-06-08 15:30:11 +0000580 # we don't want to be trying to get adbconnections since the plugin might
581 # not have been built.
David Srbecky94d21412022-10-10 16:02:51 +0100582 FLAGS += " -XjdwpProvider:none"
David Srbeckybfa76b32022-06-20 21:30:56 +0100583 for feature in EXPERIMENTAL:
David Srbecky94d21412022-10-10 16:02:51 +0100584 FLAGS += f" -Xexperimental:{feature} -Xcompiler-option --runtime-arg -Xcompiler-option -Xexperimental:{feature}"
585 COMPILE_FLAGS = f"{COMPILE_FLAGS} --runtime-arg -Xexperimental:{feature}"
Alex Light8a0e0332015-10-26 10:11:58 -0700586
David Srbecky94d21412022-10-10 16:02:51 +0100587 if CREATE_ANDROID_ROOT:
588 ANDROID_ROOT = f"{DEX_LOCATION}/android-root"
Alex Light680cbf22018-10-31 11:00:19 -0700589
David Srbecky94d21412022-10-10 16:02:51 +0100590 if ZYGOTE == "":
David Srbeckyc6d2a792022-08-18 14:15:43 +0100591 if OPTIMIZE:
David Srbecky94d21412022-10-10 16:02:51 +0100592 if VERIFY == "y":
593 DEX_OPTIMIZE = "-Xdexopt:verified"
594 else:
595 DEX_OPTIMIZE = "-Xdexopt:all"
596 msg("Performing optimizations")
David Srbeckybfa76b32022-06-20 21:30:56 +0100597 else:
David Srbecky94d21412022-10-10 16:02:51 +0100598 DEX_OPTIMIZE = "-Xdexopt:none"
599 msg("Skipping optimizations")
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100600
David Srbeckybfa76b32022-06-20 21:30:56 +0100601 if VERIFY == "y":
David Srbecky94d21412022-10-10 16:02:51 +0100602 JVM_VERIFY_ARG = "-Xverify:all"
603 msg("Performing verification")
David Srbeckybfa76b32022-06-20 21:30:56 +0100604 elif VERIFY == "s":
David Srbecky94d21412022-10-10 16:02:51 +0100605 JVM_VERIFY_ARG = "Xverify:all"
606 DEX_VERIFY = "-Xverify:softfail"
607 msg("Forcing verification to be soft fail")
608 else: # VERIFY == "n"
609 DEX_VERIFY = "-Xverify:none"
610 JVM_VERIFY_ARG = "-Xverify:none"
611 msg("Skipping verification")
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100612
David Srbecky94d21412022-10-10 16:02:51 +0100613 msg("------------------------------")
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100614
David Srbecky94d21412022-10-10 16:02:51 +0100615 if DEBUGGER == "y":
616 # Use this instead for ddms and connect by running 'ddms':
617 # DEBUGGER_OPTS="-XjdwpOptions=server=y,suspend=y -XjdwpProvider:adbconnection"
618 # TODO: add a separate --ddms option?
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100619
David Srbecky94d21412022-10-10 16:02:51 +0100620 PORT = 12345
621 msg("Waiting for jdb to connect:")
622 if not HOST:
623 msg(f" adb forward tcp:{PORT} tcp:{PORT}")
624 msg(f" jdb -attach localhost:{PORT}")
625 if not USE_JVM:
626 # Use the default libjdwp agent. Use --debug-agent to use a custom one.
627 DEBUGGER_OPTS = f"-agentpath:libjdwp.so=transport=dt_socket,address={PORT},server=y,suspend=y -XjdwpProvider:internal"
628 else:
629 DEBUGGER_OPTS = f"-agentlib:jdwp=transport=dt_socket,address={PORT},server=y,suspend=y"
630 elif DEBUGGER == "agent":
631 PORT = 12345
632 # TODO Support ddms connection and support target.
David Srbeckye4e701b2022-10-24 11:00:31 +0000633 assert HOST, "--debug-agent not supported yet for target!"
David Srbecky94d21412022-10-10 16:02:51 +0100634 AGENTPATH = DEBUGGER_AGENT
635 if WRAP_DEBUGGER_AGENT:
636 WRAPPROPS = f"{ANDROID_ROOT}/{LIBRARY_DIRECTORY}/libwrapagentpropertiesd.so"
637 if TEST_IS_NDEBUG:
638 WRAPPROPS = f"{ANDROID_ROOT}/{LIBRARY_DIRECTORY}/libwrapagentproperties.so"
639 AGENTPATH = f"{WRAPPROPS}={ANDROID_BUILD_TOP}/art/tools/libjdwp-compat.props,{AGENTPATH}"
640 msg(f"Connect to localhost:{PORT}")
641 DEBUGGER_OPTS = f"-agentpath:{AGENTPATH}=transport=dt_socket,address={PORT},server=y,suspend=y"
Alex Lightc281ba52017-10-11 11:35:55 -0700642
David Srbecky94d21412022-10-10 16:02:51 +0100643 for agent in WITH_AGENT:
644 FLAGS += f" -agentpath:{agent}"
Alex Light0e151e72017-10-25 10:50:35 -0700645
David Srbecky94d21412022-10-10 16:02:51 +0100646 if USE_JVMTI:
647 if not USE_JVM:
648 plugin = "libopenjdkjvmtid.so"
649 if TEST_IS_NDEBUG:
650 plugin = "libopenjdkjvmti.so"
651 # We used to add flags here that made the runtime debuggable but that is not
652 # needed anymore since the plugin can do it for us now.
653 FLAGS += f" -Xplugin:{plugin}"
Mythri Allef0d8b442021-11-03 17:28:24 +0000654
David Srbecky94d21412022-10-10 16:02:51 +0100655 # For jvmti tests, set the threshold of compilation to 1, so we jit early to
656 # provide better test coverage for jvmti + jit. This means we won't run
657 # the default --jit configuration but it is not too important test scenario for
658 # jvmti tests. This is art specific flag, so don't use it with jvm.
659 FLAGS += " -Xjitthreshold:1"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100660
Alex Light280e6c32020-03-03 13:52:07 -0800661# Add the libdir to the argv passed to the main function.
David Srbecky94d21412022-10-10 16:02:51 +0100662 if ADD_LIBDIR_ARGUMENTS:
663 if HOST:
664 ARGS += f" {ANDROID_HOST_OUT}/{TEST_DIRECTORY}/"
665 else:
666 ARGS += f" /data/{TEST_DIRECTORY}/art/{ISA}/"
David Srbeckyc6d2a792022-08-18 14:15:43 +0100667 if IS_JVMTI_TEST:
David Srbecky94d21412022-10-10 16:02:51 +0100668 agent = "libtiagentd.so"
669 lib = "tiagentd"
670 if TEST_IS_NDEBUG:
671 agent = "libtiagent.so"
672 lib = "tiagent"
Nicolas Geoffray288a4a22014-10-07 11:02:40 +0100673
David Srbecky94d21412022-10-10 16:02:51 +0100674 ARGS += f" {lib}"
675 if USE_JVM:
676 FLAGS += f" -agentpath:{ANDROID_HOST_OUT}/nativetest64/{agent}={TEST_NAME},jvm"
677 else:
678 FLAGS += f" -agentpath:{agent}={TEST_NAME},art"
Artem Serov55e509f2022-01-26 11:59:22 +0000679
David Srbecky94d21412022-10-10 16:02:51 +0100680 if JVMTI_STRESS:
681 agent = "libtistressd.so"
682 if TEST_IS_NDEBUG:
683 agent = "libtistress.so"
684
685 # Just give it a default start so we can always add ',' to it.
686 agent_args = "jvmti-stress"
687 if JVMTI_REDEFINE_STRESS:
688 # We really cannot do this on RI so don't both passing it in that case.
689 if not USE_JVM:
690 agent_args = f"{agent_args},redefine"
691 if JVMTI_FIELD_STRESS:
692 agent_args = f"{agent_args},field"
693 if JVMTI_STEP_STRESS:
694 agent_args = f"{agent_args},step"
695 if JVMTI_TRACE_STRESS:
696 agent_args = f"{agent_args},trace"
697 # In the future add onto this;
698 if USE_JVM:
699 FLAGS += f" -agentpath:{ANDROID_HOST_OUT}/nativetest64/{agent}={agent_args}"
700 else:
701 FLAGS += f" -agentpath:{agent}={agent_args}"
702
703 if USE_JVM:
704 env = {
705 "ANDROID_I18N_ROOT": ANDROID_I18N_ROOT,
706 "DEX_LOCATION": DEX_LOCATION,
707 "JAVA_HOME": os.environ["JAVA_HOME"],
708 "LANG":
709 "en_US.UTF-8", # Needed to enable unicode and make the output is deterministic.
710 "LD_LIBRARY_PATH": f"{ANDROID_HOST_OUT}/lib64",
711 }
712 # Some jvmti tests are flaky without -Xint on the RI.
713 if IS_JVMTI_TEST:
714 FLAGS += " -Xint"
715 # Xmx is necessary since we don't pass down the ART flags to JVM.
716 # We pass the classes2 path whether it's used (src-multidex) or not.
David Srbeckye4e701b2022-10-24 11:00:31 +0000717 cmdline = f"{JAVA} {DEBUGGER_OPTS} {JVM_VERIFY_ARG} -Xmx256m -classpath classes:classes2 {FLAGS} {MAIN} {ARGS}"
David Srbecky94d21412022-10-10 16:02:51 +0100718 if CREATE_RUNNER:
719 with open("runit.sh", "w") as f:
720 f.write("#!/bin/bash")
721 print(f"export LD_LIBRARY_PATH=\"{LD_LIBRARY_PATH}\"")
722 f.write(cmdline)
723 os.chmod("runit.sh", 0o777)
724 pwd = os.getcwd()
725 print(f"Runnable test script written to {pwd}/runit.sh")
David Srbeckye4e701b2022-10-24 11:00:31 +0000726 return
David Srbecky94d21412022-10-10 16:02:51 +0100727 else:
David Srbecky35a48ce2022-11-09 12:19:29 +0000728 run(cmdline,
729 env,
730 stdout_file=args.stdout_file,
731 stderr_file=args.stderr_file,
732 expected_exit_code=args.expected_exit_code)
David Srbeckye4e701b2022-10-24 11:00:31 +0000733 return
David Srbecky94d21412022-10-10 16:02:51 +0100734
735 b_path = get_apex_bootclasspath(HOST)
736 b_path_locations = get_apex_bootclasspath_locations(HOST)
737
738 BCPEX = ""
739 if isfile(f"{TEST_NAME}-bcpex.jar"):
740 BCPEX = f":{DEX_LOCATION}/{TEST_NAME}-bcpex.jar"
Vladimir Marko239c9412022-01-05 16:09:09 +0000741
Vladimir Marko7a85e702018-12-03 18:47:23 +0000742# Pass down the bootclasspath
David Srbecky94d21412022-10-10 16:02:51 +0100743 FLAGS += f" -Xbootclasspath:{b_path}{BCPEX}"
744 FLAGS += f" -Xbootclasspath-locations:{b_path_locations}{BCPEX}"
745 COMPILE_FLAGS += f" --runtime-arg -Xbootclasspath:{b_path}"
746 COMPILE_FLAGS += f" --runtime-arg -Xbootclasspath-locations:{b_path_locations}"
Nicolas Geoffray288a4a22014-10-07 11:02:40 +0100747
David Srbecky94d21412022-10-10 16:02:51 +0100748 if not HAVE_IMAGE:
Andreas Gampe1078d152017-11-07 18:00:54 -0800749 # Disable image dex2oat - this will forbid the runtime to patch or compile an image.
David Srbecky94d21412022-10-10 16:02:51 +0100750 FLAGS += " -Xnoimage-dex2oat"
Andreas Gampe1078d152017-11-07 18:00:54 -0800751
752 # We'll abuse a second flag here to test different behavior. If --relocate, use the
753 # existing image - relocation will fail as patching is disallowed. If --no-relocate,
754 # pass a non-existent image - compilation will fail as dex2oat is disallowed.
David Srbeckyc6d2a792022-08-18 14:15:43 +0100755 if not RELOCATE:
David Srbecky94d21412022-10-10 16:02:51 +0100756 BOOT_IMAGE = "/system/non-existent/boot.art"
Nicolas Geoffray48eb8392022-02-24 16:20:18 +0000757 # App images cannot be generated without a boot image.
David Srbecky94d21412022-10-10 16:02:51 +0100758 APP_IMAGE = False
759 DALVIKVM_BOOT_OPT = f"-Ximage:{BOOT_IMAGE}"
Nicolas Geoffray288a4a22014-10-07 11:02:40 +0100760
David Srbecky94d21412022-10-10 16:02:51 +0100761 if USE_GDB_DEX2OAT:
David Srbeckye4e701b2022-10-24 11:00:31 +0000762 assert HOST, "The --gdb-dex2oat option is not yet implemented for target."
Dmitrii Ishcheikine60131a2022-10-07 11:45:44 +0000763
David Srbecky94d21412022-10-10 16:02:51 +0100764 if USE_GDB:
David Srbeckye4e701b2022-10-24 11:00:31 +0000765 assert not USE_GDBSERVER, "Cannot pass both --gdb and --gdbserver at the same time!"
766 if not HOST:
David Srbecky94d21412022-10-10 16:02:51 +0100767 # We might not have any hostname resolution if we are using a chroot.
768 GDB = f"{GDBSERVER_DEVICE} --no-startup-with-shell 127.0.0.1{GDBSERVER_PORT}"
769 else:
770 if run("uname").stdout.strip() == "Darwin":
771 GDB = "lldb"
772 GDB_ARGS += f" -- {DALVIKVM}"
773 DALVIKVM = ""
774 else:
775 GDB = "gdb"
776 GDB_ARGS += f" --args {DALVIKVM}"
777 # Enable for Emacs "M-x gdb" support. TODO: allow extra gdb arguments on command line.
778 # gdbargs=f"--annotate=3 {gdbargs}"
779 elif USE_GDBSERVER:
780 if not HOST:
781 # We might not have any hostname resolution if we are using a chroot.
782 GDB = f"{GDBSERVER_DEVICE} --no-startup-with-shell 127.0.0.1{GDBSERVER_PORT}"
783 else:
784 GDB = f"{GDBSERVER_HOST} {GDBSERVER_PORT}"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100785
David Srbeckye4e701b2022-10-24 11:00:31 +0000786 assert shutil.which(GDBSERVER_HOST), f"{GDBSERVER_HOST} is not available"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800787
David Srbecky94d21412022-10-10 16:02:51 +0100788 if INTERPRETER:
789 INT_OPTS += " -Xint"
Nicolas Geoffrayc76fbf02021-04-06 08:59:17 +0100790
David Srbecky94d21412022-10-10 16:02:51 +0100791 if JIT:
792 INT_OPTS += " -Xusejit:true"
793 else:
794 INT_OPTS += " -Xusejit:false"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100795
David Srbecky94d21412022-10-10 16:02:51 +0100796 if INTERPRETER or JIT:
797 if VERIFY == "y":
798 INT_OPTS += " -Xcompiler-option --compiler-filter=verify"
799 COMPILE_FLAGS += " --compiler-filter=verify"
800 elif VERIFY == "s":
801 INT_OPTS += " -Xcompiler-option --compiler-filter=extract"
802 COMPILE_FLAGS += " --compiler-filter=extract"
803 DEX_VERIFY = f"{DEX_VERIFY} -Xverify:softfail"
804 else: # VERIFY == "n"
805 INT_OPTS += " -Xcompiler-option --compiler-filter=assume-verified"
806 COMPILE_FLAGS += " --compiler-filter=assume-verified"
807 DEX_VERIFY = f"{DEX_VERIFY} -Xverify:none"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100808
David Srbecky94d21412022-10-10 16:02:51 +0100809 JNI_OPTS = "-Xjnigreflimit:512 -Xcheck:jni"
810
811 COMPILE_FLAGS += " --runtime-arg -Xnorelocate"
812 if RELOCATE:
813 FLAGS += " -Xrelocate"
814 else:
815 FLAGS += " -Xnorelocate"
816
817 if BIONIC:
818 # This is the location that soong drops linux_bionic builds. Despite being
819 # called linux_bionic-x86 the build is actually amd64 (x86_64) only.
David Srbeckye4e701b2022-10-24 11:00:31 +0000820 assert path.exists(f"{OUT_DIR}/soong/host/linux_bionic-x86"), (
821 "linux_bionic-x86 target doesn't seem to have been built!")
David Srbecky94d21412022-10-10 16:02:51 +0100822 # Set TIMEOUT_DUMPER manually so it works even with apex's
823 TIMEOUT_DUMPER = f"{OUT_DIR}/soong/host/linux_bionic-x86/bin/signal_dumper"
Alex Light680cbf22018-10-31 11:00:19 -0700824
Wojciech Staszkiewicze6636532016-09-23 10:59:55 -0700825# Prevent test from silently falling back to interpreter in no-prebuild mode. This happens
826# when DEX_LOCATION path is too long, because vdex/odex filename is constructed by taking
827# full path to dex, stripping leading '/', appending '@classes.vdex' and changing every
828# remaining '/' into '@'.
David Srbecky94d21412022-10-10 16:02:51 +0100829 if HOST:
830 max_filename_size = int(
831 run(f"getconf NAME_MAX {DEX_LOCATION}", save_cmd=False).stdout)
832 else:
833 # There is no getconf on device, fallback to standard value.
834 # See NAME_MAX in kernel <linux/limits.h>
835 max_filename_size = 255
Wojciech Staszkiewicze6636532016-09-23 10:59:55 -0700836# Compute VDEX_NAME.
David Srbecky94d21412022-10-10 16:02:51 +0100837 DEX_LOCATION_STRIPPED = DEX_LOCATION.lstrip("/")
838 VDEX_NAME = f"{DEX_LOCATION_STRIPPED}@{TEST_NAME}.jar@classes.vdex".replace(
839 "/", "@")
David Srbeckye4e701b2022-10-24 11:00:31 +0000840 assert len(VDEX_NAME) <= max_filename_size, "Dex location path too long"
Wojciech Staszkiewicze6636532016-09-23 10:59:55 -0700841
David Srbecky94d21412022-10-10 16:02:51 +0100842 if HOST:
843 # On host, run binaries (`dex2oat(d)`, `dalvikvm`, `profman`) from the `bin`
844 # directory under the "Android Root" (usually `out/host/linux-x86`).
845 #
846 # TODO(b/130295968): Adjust this if/when ART host artifacts are installed
847 # under the ART root (usually `out/host/linux-x86/com.android.art`).
848 ANDROID_ART_BIN_DIR = f"{ANDROID_ROOT}/bin"
849 else:
850 # On target, run binaries (`dex2oat(d)`, `dalvikvm`, `profman`) from the ART
851 # APEX's `bin` directory. This means the linker will observe the ART APEX
852 # linker configuration file (`/apex/com.android.art/etc/ld.config.txt`) for
853 # these binaries.
854 ANDROID_ART_BIN_DIR = f"{ANDROID_ART_ROOT}/bin"
Alex Light20802ca2018-12-05 15:36:03 -0800855
David Srbecky94d21412022-10-10 16:02:51 +0100856 profman_cmdline = "true"
857 dex2oat_cmdline = "true"
858 vdex_cmdline = "true"
859 dm_cmdline = "true"
860 mkdir_locations = f"{DEX_LOCATION}/dalvik-cache/{ISA}"
861 strip_cmdline = "true"
862 sync_cmdline = "true"
863 linkroot_cmdline = "true"
864 linkroot_overlay_cmdline = "true"
865 setupapex_cmdline = "true"
866 installapex_cmdline = "true"
867 installapex_test_cmdline = "true"
Alex Light680cbf22018-10-31 11:00:19 -0700868
David Srbecky94d21412022-10-10 16:02:51 +0100869 def linkdirs(host_out: str, root: str):
870 dirs = list(filter(os.path.isdir, glob.glob(os.path.join(host_out, "*"))))
871 # Also create a link for the boot image.
872 dirs.append(f"{ANDROID_HOST_OUT}/apex/art_boot_images")
873 return " && ".join(f"ln -sf {dir} {root}" for dir in dirs)
Alex Light680cbf22018-10-31 11:00:19 -0700874
David Srbecky94d21412022-10-10 16:02:51 +0100875 if CREATE_ANDROID_ROOT:
876 mkdir_locations += f" {ANDROID_ROOT}"
877 linkroot_cmdline = linkdirs(ANDROID_HOST_OUT, ANDROID_ROOT)
878 if BIONIC:
879 # TODO Make this overlay more generic.
880 linkroot_overlay_cmdline = linkdirs(
881 f"{OUT_DIR}/soong/host/linux_bionic-x86", ANDROID_ROOT)
882 # Replace the boot image to a location expected by the runtime.
883 DALVIKVM_BOOT_OPT = f"-Ximage:{ANDROID_ROOT}/art_boot_images/javalib/boot.art"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100884
David Srbecky94d21412022-10-10 16:02:51 +0100885 if USE_ZIPAPEX:
886 # TODO Currently this only works for linux_bionic zipapexes because those are
887 # stripped and so small enough that the ulimit doesn't kill us.
888 mkdir_locations += f" {DEX_LOCATION}/zipapex"
889 setupapex_cmdline = f"unzip -o -u {ZIPAPEX_LOC} apex_payload.zip -d {DEX_LOCATION}"
890 installapex_cmdline = f"unzip -o -u {DEX_LOCATION}/apex_payload.zip -d {DEX_LOCATION}/zipapex"
891 ANDROID_ART_BIN_DIR = f"{DEX_LOCATION}/zipapex/bin"
892 elif USE_EXTRACTED_ZIPAPEX:
893 # Just symlink the zipapex binaries
894 ANDROID_ART_BIN_DIR = f"{DEX_LOCATION}/zipapex/bin"
895 # Force since some tests manually run this file twice.
896 # If the {RUN} is executed multiple times we don't need to recreate the link
897 installapex_test_cmdline = f"test -L {DEX_LOCATION}/zipapex"
898 installapex_cmdline = f"ln -s -f --verbose {EXTRACTED_ZIPAPEX_LOC} {DEX_LOCATION}/zipapex"
Alex Light20802ca2018-12-05 15:36:03 -0800899
Jeff Hao002b9312017-03-27 16:23:08 -0700900# PROFILE takes precedence over RANDOM_PROFILE, since PROFILE tests require a
901# specific profile to run properly.
David Srbeckyc6d2a792022-08-18 14:15:43 +0100902 if PROFILE or RANDOM_PROFILE:
David Srbecky94d21412022-10-10 16:02:51 +0100903 profman_cmdline = f"{ANDROID_ART_BIN_DIR}/profman \
904 --apk={DEX_LOCATION}/{TEST_NAME}.jar \
905 --dex-location={DEX_LOCATION}/{TEST_NAME}.jar"
906
907 if isfile(f"{TEST_NAME}-ex.jar") and SECONDARY_COMPILATION:
908 profman_cmdline = f"{profman_cmdline} \
909 --apk={DEX_LOCATION}/{TEST_NAME}-ex.jar \
910 --dex-location={DEX_LOCATION}/{TEST_NAME}-ex.jar"
911
912 COMPILE_FLAGS = f"{COMPILE_FLAGS} --profile-file={DEX_LOCATION}/{TEST_NAME}.prof"
913 FLAGS = f"{FLAGS} -Xcompiler-option --profile-file={DEX_LOCATION}/{TEST_NAME}.prof"
914 if PROFILE:
915 profman_cmdline = f"{profman_cmdline} --create-profile-from={DEX_LOCATION}/profile \
916 --reference-profile-file={DEX_LOCATION}/{TEST_NAME}.prof"
917
David Srbeckybfa76b32022-06-20 21:30:56 +0100918 else:
David Srbecky94d21412022-10-10 16:02:51 +0100919 profman_cmdline = f"{profman_cmdline} --generate-test-profile={DEX_LOCATION}/{TEST_NAME}.prof \
920 --generate-test-profile-seed=0"
921
922 def get_prebuilt_lldb_path():
923 CLANG_BASE = "prebuilts/clang/host"
924 CLANG_VERSION = run(
925 f"{ANDROID_BUILD_TOP}/build/soong/scripts/get_clang_version.py"
926 ).stdout.strip()
927 uname = run("uname -s").stdout.strip()
928 if uname == "Darwin":
929 PREBUILT_NAME = "darwin-x86"
930 elif uname == "Linux":
931 PREBUILT_NAME = "linux-x86"
932 else:
933 print(
934 "Unknown host $(uname -s). Unsupported for debugging dex2oat with LLDB.",
935 file=sys.stderr)
936 return
937 CLANG_PREBUILT_HOST_PATH = f"{ANDROID_BUILD_TOP}/{CLANG_BASE}/{PREBUILT_NAME}/{CLANG_VERSION}"
938 # If the clang prebuilt directory exists and the reported clang version
939 # string does not, then it is likely that the clang version reported by the
940 # get_clang_version.py script does not match the expected directory name.
David Srbeckye4e701b2022-10-24 11:00:31 +0000941 if isdir(f"{ANDROID_BUILD_TOP}/{CLANG_BASE}/{PREBUILT_NAME}"):
942 assert isdir(CLANG_PREBUILT_HOST_PATH), (
943 "The prebuilt clang directory exists, but the specific "
944 "clang\nversion reported by get_clang_version.py does not exist in "
945 "that path.\nPlease make sure that the reported clang version "
946 "resides in the\nprebuilt clang directory!")
David Srbecky94d21412022-10-10 16:02:51 +0100947
948 # The lldb-server binary is a dependency of lldb.
949 os.environ[
950 "LLDB_DEBUGSERVER_PATH"] = f"{CLANG_PREBUILT_HOST_PATH}/runtimes_ndk_cxx/x86_64/lldb-server"
951
952 # Set the current terminfo directory to TERMINFO so that LLDB can read the
953 # termcap database.
954 terminfo = re.search("/.*/terminfo/", run("infocmp", save_cmd=False).stdout)
955 if terminfo:
956 os.environ["TERMINFO"] = terminfo[0]
957
958 return f"{CLANG_PREBUILT_HOST_PATH}/bin/lldb.sh"
959
960 def write_dex2oat_cmdlines(name: str):
David Srbeckye4e701b2022-10-24 11:00:31 +0000961 nonlocal dex2oat_cmdline, dm_cmdline, vdex_cmdline
David Srbecky94d21412022-10-10 16:02:51 +0100962
963 class_loader_context = ""
964 enable_app_image = False
965 if APP_IMAGE:
966 enable_app_image = True
967
968 # If the name ends in -ex then this is a secondary dex file
969 if name.endswith("-ex"):
970 # Lazily realize the default value in case DEX_LOCATION/TEST_NAME change
David Srbeckye4e701b2022-10-24 11:00:31 +0000971 nonlocal SECONDARY_CLASS_LOADER_CONTEXT
David Srbecky94d21412022-10-10 16:02:51 +0100972 if SECONDARY_CLASS_LOADER_CONTEXT == "":
973 if SECONDARY_DEX == "":
974 # Tests without `--secondary` load the "-ex" jar in a separate PathClassLoader
975 # that is a child of the main PathClassLoader. If the class loader is constructed
976 # in any other way, the test needs to specify the secondary CLC explicitly.
977 SECONDARY_CLASS_LOADER_CONTEXT = f"PCL[];PCL[{DEX_LOCATION}/{TEST_NAME}.jar]"
978 else:
979 # Tests with `--secondary` load the `-ex` jar a part of the main PathClassLoader.
980 SECONDARY_CLASS_LOADER_CONTEXT = f"PCL[{DEX_LOCATION}/{TEST_NAME}.jar]"
981 class_loader_context = f"'--class-loader-context={SECONDARY_CLASS_LOADER_CONTEXT}'"
982 enable_app_image = enable_app_image and SECONDARY_APP_IMAGE
983
984 app_image = ""
985 if enable_app_image:
986 app_image = f"--app-image-file={DEX_LOCATION}/oat/{ISA}/{name}.art --resolve-startup-const-strings=true"
987
David Srbeckye4e701b2022-10-24 11:00:31 +0000988 nonlocal GDB_DEX2OAT, GDB_DEX2OAT_ARGS
David Srbecky94d21412022-10-10 16:02:51 +0100989 if USE_GDB_DEX2OAT:
990 prebuilt_lldb_path = get_prebuilt_lldb_path()
991 GDB_DEX2OAT = f"{prebuilt_lldb_path} -f"
992 GDB_DEX2OAT_ARGS += " -- "
993
994 dex2oat_binary = DEX2OAT_DEBUG_BINARY
995 if TEST_IS_NDEBUG:
996 dex2oat_binary = DEX2OAT_NDEBUG_BINARY
997 dex2oat_cmdline = f"{INVOKE_WITH} {GDB_DEX2OAT} \
998 {ANDROID_ART_BIN_DIR}/{dex2oat_binary} \
999 {GDB_DEX2OAT_ARGS} \
1000 {COMPILE_FLAGS} \
1001 --boot-image={BOOT_IMAGE} \
1002 --dex-file={DEX_LOCATION}/{name}.jar \
1003 --oat-file={DEX_LOCATION}/oat/{ISA}/{name}.odex \
1004 {app_image} \
1005 --generate-mini-debug-info \
1006 --instruction-set={ISA} \
1007 {class_loader_context}"
1008
1009 if INSTRUCTION_SET_FEATURES != "":
1010 dex2oat_cmdline += f" --instruction-set-features={INSTRUCTION_SET_FEATURES}"
1011
1012 # Add in a timeout. This is important for testing the compilation/verification time of
1013 # pathological cases. We do not append a timeout when debugging dex2oat because we
1014 # do not want it to exit while debugging.
1015 # Note: as we don't know how decent targets are (e.g., emulator), only do this on the host for
1016 # now. We should try to improve this.
1017 # The current value is rather arbitrary. run-tests should compile quickly.
1018 # Watchdog timeout is in milliseconds so add 3 '0's to the dex2oat timeout.
1019 if HOST and not USE_GDB_DEX2OAT:
1020 # Use SIGRTMIN+2 to try to dump threads.
1021 # Use -k 1m to SIGKILL it a minute later if it hasn't ended.
1022 dex2oat_cmdline = f"timeout -k {DEX2OAT_TIMEOUT}s -s SIGRTMIN+2 {DEX2OAT_RT_TIMEOUT}s {dex2oat_cmdline} --watchdog-timeout={DEX2OAT_TIMEOUT}000"
1023 if PROFILE or RANDOM_PROFILE:
1024 vdex_cmdline = f"{dex2oat_cmdline} {VDEX_ARGS} --input-vdex={DEX_LOCATION}/oat/{ISA}/{name}.vdex --output-vdex={DEX_LOCATION}/oat/{ISA}/{name}.vdex"
1025 elif TEST_VDEX:
1026 if VDEX_ARGS == "":
1027 # If no arguments need to be passed, just delete the odex file so that the runtime only picks up the vdex file.
1028 vdex_cmdline = f"rm {DEX_LOCATION}/oat/{ISA}/{name}.odex"
1029 else:
1030 vdex_cmdline = f"{dex2oat_cmdline} {VDEX_ARGS} --compact-dex-level=none --input-vdex={DEX_LOCATION}/oat/{ISA}/{name}.vdex"
1031 elif TEST_DEX2OAT_DM:
1032 vdex_cmdline = f"{dex2oat_cmdline} {VDEX_ARGS} --dump-timings --dm-file={DEX_LOCATION}/oat/{ISA}/{name}.dm"
1033 dex2oat_cmdline = f"{dex2oat_cmdline} --copy-dex-files=false --output-vdex={DEX_LOCATION}/oat/{ISA}/primary.vdex"
1034 dm_cmdline = f"zip -qj {DEX_LOCATION}/oat/{ISA}/{name}.dm {DEX_LOCATION}/oat/{ISA}/primary.vdex"
1035 elif TEST_RUNTIME_DM:
1036 dex2oat_cmdline = f"{dex2oat_cmdline} --copy-dex-files=false --output-vdex={DEX_LOCATION}/oat/{ISA}/primary.vdex"
1037 dm_cmdline = f"zip -qj {DEX_LOCATION}/{name}.dm {DEX_LOCATION}/oat/{ISA}/primary.vdex"
Andreas Gampe038a1982020-03-11 23:06:42 +00001038
1039# Enable mini-debug-info for JIT (if JIT is used).
Andreas Gampe038a1982020-03-11 23:06:42 +00001040
David Srbecky94d21412022-10-10 16:02:51 +01001041 FLAGS += " -Xcompiler-option --generate-mini-debug-info"
Andreas Gampe038a1982020-03-11 23:06:42 +00001042
David Srbecky94d21412022-10-10 16:02:51 +01001043 if PREBUILD:
1044 mkdir_locations += f" {DEX_LOCATION}/oat/{ISA}"
Andreas Gampe038a1982020-03-11 23:06:42 +00001045
David Srbecky94d21412022-10-10 16:02:51 +01001046 # "Primary".
1047 write_dex2oat_cmdlines(TEST_NAME)
1048 dex2oat_cmdline = re.sub(" +", " ", dex2oat_cmdline)
1049 dm_cmdline = re.sub(" +", " ", dm_cmdline)
1050 vdex_cmdline = re.sub(" +", " ", vdex_cmdline)
Andreas Gampe038a1982020-03-11 23:06:42 +00001051
David Srbecky94d21412022-10-10 16:02:51 +01001052 # Enable mini-debug-info for JIT (if JIT is used).
1053 FLAGS += " -Xcompiler-option --generate-mini-debug-info"
Andreas Gampe038a1982020-03-11 23:06:42 +00001054
David Srbecky94d21412022-10-10 16:02:51 +01001055 if isfile(f"{TEST_NAME}-ex.jar") and SECONDARY_COMPILATION:
1056 # "Secondary" for test coverage.
Andreas Gampe038a1982020-03-11 23:06:42 +00001057
David Srbecky94d21412022-10-10 16:02:51 +01001058 # Store primary values.
1059 base_dex2oat_cmdline = dex2oat_cmdline
1060 base_dm_cmdline = dm_cmdline
1061 base_vdex_cmdline = vdex_cmdline
Andreas Gampe038a1982020-03-11 23:06:42 +00001062
David Srbecky94d21412022-10-10 16:02:51 +01001063 write_dex2oat_cmdlines(f"{TEST_NAME}-ex")
1064 dex2oat_cmdline = re.sub(" +", " ", dex2oat_cmdline)
1065 dm_cmdline = re.sub(" +", " ", dm_cmdline)
1066 vdex_cmdline = re.sub(" +", " ", vdex_cmdline)
Andreas Gampe4d2ef332015-08-05 09:24:45 -07001067
David Srbecky94d21412022-10-10 16:02:51 +01001068 # Concatenate.
1069 dex2oat_cmdline = f"{base_dex2oat_cmdline} && {dex2oat_cmdline}"
1070 dm_cmdline = base_dm_cmdline # Only use primary dm.
1071 vdex_cmdline = f"{base_vdex_cmdline} && {vdex_cmdline}"
Igor Murashkin271a0f82017-02-14 21:14:17 +00001072
David Srbecky94d21412022-10-10 16:02:51 +01001073 if SYNC_BEFORE_RUN:
1074 sync_cmdline = "sync"
1075
1076 DALVIKVM_ISA_FEATURES_ARGS = ""
1077 if INSTRUCTION_SET_FEATURES != "":
1078 DALVIKVM_ISA_FEATURES_ARGS = f"-Xcompiler-option --instruction-set-features={INSTRUCTION_SET_FEATURES}"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001079
Nicolas Geoffrayc5256042015-12-26 19:41:37 +00001080# java.io.tmpdir can only be set at launch time.
David Srbecky94d21412022-10-10 16:02:51 +01001081 TMP_DIR_OPTION = ""
1082 if not HOST:
1083 TMP_DIR_OPTION = "-Djava.io.tmpdir=/data/local/tmp"
Nicolas Geoffrayc5256042015-12-26 19:41:37 +00001084
Alex Lightb8012102019-11-13 01:41:04 +00001085# The build servers have an ancient version of bash so we cannot use @Q.
David Srbecky94d21412022-10-10 16:02:51 +01001086 QUOTED_DALVIKVM_BOOT_OPT = shlex.quote(DALVIKVM_BOOT_OPT)
Alex Lightb8012102019-11-13 01:41:04 +00001087
David Srbecky94d21412022-10-10 16:02:51 +01001088 DALVIKVM_CLASSPATH = f"{DEX_LOCATION}/{TEST_NAME}.jar"
1089 if isfile(f"{TEST_NAME}-aotex.jar"):
1090 DALVIKVM_CLASSPATH = f"{DALVIKVM_CLASSPATH}:{DEX_LOCATION}/{TEST_NAME}-aotex.jar"
1091 DALVIKVM_CLASSPATH = f"{DALVIKVM_CLASSPATH}{SECONDARY_DEX}"
Vladimir Marko239c9412022-01-05 16:09:09 +00001092
David Srbecky94d21412022-10-10 16:02:51 +01001093 # We set DumpNativeStackOnSigQuit to false to avoid stressing libunwind.
1094 # b/27185632
1095 # b/24664297
Ulya Trafimovich7f7f6442021-11-05 15:26:13 +00001096
David Srbecky94d21412022-10-10 16:02:51 +01001097 dalvikvm_cmdline = f"{INVOKE_WITH} {GDB} {ANDROID_ART_BIN_DIR}/{DALVIKVM} \
David Srbecky35a48ce2022-11-09 12:19:29 +00001098 {GDB_ARGS} \
1099 {FLAGS} \
1100 {DEX_VERIFY} \
1101 -XXlib:{LIB} \
1102 {DEX2OAT} \
1103 {DALVIKVM_ISA_FEATURES_ARGS} \
1104 {ZYGOTE} \
1105 {JNI_OPTS} \
1106 {INT_OPTS} \
1107 {DEBUGGER_OPTS} \
1108 {QUOTED_DALVIKVM_BOOT_OPT} \
1109 {TMP_DIR_OPTION} \
1110 -XX:DumpNativeStackOnSigQuit:false \
1111 -cp {DALVIKVM_CLASSPATH} {MAIN} {ARGS}"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001112
David Srbecky94d21412022-10-10 16:02:51 +01001113 if SIMPLEPERF:
1114 dalvikvm_cmdline = f"simpleperf record {dalvikvm_cmdline} && simpleperf report"
Ulya Trafimovich7f7f6442021-11-05 15:26:13 +00001115
David Srbecky94d21412022-10-10 16:02:51 +01001116 def sanitize_dex2oat_cmdline(cmdline: str) -> str:
1117 args = []
1118 for arg in cmdline.split(" "):
1119 if arg == "--class-loader-context=&":
1120 arg = "--class-loader-context=\&"
1121 args.append(arg)
1122 return " ".join(args)
Andreas Gampe038a1982020-03-11 23:06:42 +00001123
David Srbeckye4e701b2022-10-24 11:00:31 +00001124
Andreas Gampe3ad5d5e2015-02-03 18:26:55 -08001125# Remove whitespace.
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001126
David Srbecky94d21412022-10-10 16:02:51 +01001127 dex2oat_cmdline = sanitize_dex2oat_cmdline(dex2oat_cmdline)
1128 dalvikvm_cmdline = re.sub(" +", " ", dalvikvm_cmdline)
1129 dm_cmdline = re.sub(" +", " ", dm_cmdline)
1130 vdex_cmdline = sanitize_dex2oat_cmdline(vdex_cmdline)
1131 profman_cmdline = re.sub(" +", " ", profman_cmdline)
Andreas Gampeb31a8e72017-05-16 10:30:24 -07001132
David Srbecky94d21412022-10-10 16:02:51 +01001133 # Use an empty ASAN_OPTIONS to enable defaults.
1134 # Note: this is required as envsetup right now exports detect_leaks=0.
1135 RUN_TEST_ASAN_OPTIONS = ""
Andreas Gampe58408392017-05-16 10:45:46 -07001136
David Srbecky94d21412022-10-10 16:02:51 +01001137 # Multiple shutdown leaks. b/38341789
1138 if RUN_TEST_ASAN_OPTIONS != "":
1139 RUN_TEST_ASAN_OPTIONS = f"{RUN_TEST_ASAN_OPTIONS}:"
1140 RUN_TEST_ASAN_OPTIONS = f"{RUN_TEST_ASAN_OPTIONS}detect_leaks=0"
1141
1142 # For running, we must turn off logging when dex2oat is missing. Otherwise we use
1143 # the same defaults as for prebuilt: everything when --dev, otherwise errors and above only.
1144 if not EXTERNAL_LOG_TAGS:
1145 if VERBOSE:
1146 ANDROID_LOG_TAGS = "*:d"
1147 elif not HAVE_IMAGE:
Nicolas Geoffrayf39f04c2018-03-05 15:42:11 +00001148 # All tests would log the error of missing image. Be silent here and only log fatal
1149 # events.
David Srbecky94d21412022-10-10 16:02:51 +01001150 ANDROID_LOG_TAGS = "*:s"
1151 else:
Nicolas Geoffrayf39f04c2018-03-05 15:42:11 +00001152 # We are interested in LOG(ERROR) output.
David Srbecky94d21412022-10-10 16:02:51 +01001153 ANDROID_LOG_TAGS = "*:e"
Nicolas Geoffrayf39f04c2018-03-05 15:42:11 +00001154
David Srbecky94d21412022-10-10 16:02:51 +01001155 if not HOST:
David Srbeckybfa76b32022-06-20 21:30:56 +01001156 adb.root()
1157 adb.wait_for_device()
David Srbeckye60b4a72022-09-27 14:53:06 +01001158 adb.shell(f"rm -rf {CHROOT_DEX_LOCATION}")
1159 adb.shell(f"mkdir -p {CHROOT_DEX_LOCATION}")
1160 adb.push(f"{TEST_NAME}.jar", CHROOT_DEX_LOCATION)
1161 adb.push(f"{TEST_NAME}-ex.jar", CHROOT_DEX_LOCATION, check=False)
1162 adb.push(f"{TEST_NAME}-aotex.jar", CHROOT_DEX_LOCATION, check=False)
1163 adb.push(f"{TEST_NAME}-bcpex.jar", CHROOT_DEX_LOCATION, check=False)
1164 if PROFILE or RANDOM_PROFILE:
1165 adb.push("profile", CHROOT_DEX_LOCATION, check=False)
1166 # Copy resource folder
1167 if isdir("res"):
1168 adb.push("res", CHROOT_DEX_LOCATION)
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001169
Roland Levillain72f67742019-03-06 15:48:08 +00001170 # Populate LD_LIBRARY_PATH.
David Srbecky94d21412022-10-10 16:02:51 +01001171 LD_LIBRARY_PATH = ""
David Srbeckybfa76b32022-06-20 21:30:56 +01001172 if ANDROID_ROOT != "/system":
Nicolas Geoffrayeb441dd2014-10-31 15:34:50 +00001173 # Current default installation is dalvikvm 64bits and dex2oat 32bits,
1174 # so we can only use LD_LIBRARY_PATH when testing on a local
1175 # installation.
David Srbecky94d21412022-10-10 16:02:51 +01001176 LD_LIBRARY_PATH = f"{ANDROID_ROOT}/{LIBRARY_DIRECTORY}"
Martin Stjernholm0d0f8df2021-04-28 16:47:01 +01001177
1178 # This adds libarttest(d).so to the default linker namespace when dalvikvm
1179 # is run from /apex/com.android.art/bin. Since that namespace is essentially
1180 # an alias for the com_android_art namespace, that gives libarttest(d).so
1181 # full access to the internal ART libraries.
David Srbecky94d21412022-10-10 16:02:51 +01001182 LD_LIBRARY_PATH = f"/data/{TEST_DIRECTORY}/com.android.art/lib{SUFFIX64}:{LD_LIBRARY_PATH}"
1183 dlib = ("" if TEST_IS_NDEBUG else "d")
1184 art_test_internal_libraries = [
1185 f"libartagent{dlib}.so",
1186 f"libarttest{dlib}.so",
1187 f"libtiagent{dlib}.so",
1188 f"libtistress{dlib}.so",
David Srbeckybfa76b32022-06-20 21:30:56 +01001189 ]
David Srbecky94d21412022-10-10 16:02:51 +01001190 NATIVELOADER_DEFAULT_NAMESPACE_LIBS = ":".join(art_test_internal_libraries)
1191 dlib = ""
1192 art_test_internal_libraries = []
Martin Stjernholm0d0f8df2021-04-28 16:47:01 +01001193
Roland Levillain72f67742019-03-06 15:48:08 +00001194 # Needed to access the test's Odex files.
David Srbecky94d21412022-10-10 16:02:51 +01001195 LD_LIBRARY_PATH = f"{DEX_LOCATION}/oat/{ISA}:{LD_LIBRARY_PATH}"
Roland Levillain72f67742019-03-06 15:48:08 +00001196 # Needed to access the test's native libraries (see e.g. 674-hiddenapi,
David Srbeckybfa76b32022-06-20 21:30:56 +01001197 # which generates `libhiddenapitest_*.so` libraries in `{DEX_LOCATION}`).
David Srbecky94d21412022-10-10 16:02:51 +01001198 LD_LIBRARY_PATH = f"{DEX_LOCATION}:{LD_LIBRARY_PATH}"
Nicolas Geoffrayeb441dd2014-10-31 15:34:50 +00001199
Roland Levillain72f67742019-03-06 15:48:08 +00001200 # Prepend directories to the path on device.
David Srbecky94d21412022-10-10 16:02:51 +01001201 PREPEND_TARGET_PATH = ANDROID_ART_BIN_DIR
David Srbeckybfa76b32022-06-20 21:30:56 +01001202 if ANDROID_ROOT != "/system":
David Srbecky94d21412022-10-10 16:02:51 +01001203 PREPEND_TARGET_PATH = f"{PREPEND_TARGET_PATH}:{ANDROID_ROOT}/bin"
Roland Levillain72f67742019-03-06 15:48:08 +00001204
David Srbecky94d21412022-10-10 16:02:51 +01001205 timeout_dumper_cmd = ""
Andreas Gampea6138082019-09-11 11:08:23 -07001206
1207 # Check whether signal_dumper is available.
David Srbeckybfa76b32022-06-20 21:30:56 +01001208 if TIMEOUT_DUMPER == "signal_dumper":
Andreas Gampe5d775eb2019-09-13 09:54:55 -07001209 # Chroot? Use as prefix for tests.
David Srbecky94d21412022-10-10 16:02:51 +01001210 TIMEOUT_DUMPER_PATH_PREFIX = ""
David Srbeckybfa76b32022-06-20 21:30:56 +01001211 if CHROOT:
David Srbecky94d21412022-10-10 16:02:51 +01001212 TIMEOUT_DUMPER_PATH_PREFIX = f"{CHROOT}/"
Andreas Gampe5d775eb2019-09-13 09:54:55 -07001213
Roland Levillain52b4dc92019-09-12 11:56:52 +01001214 # Testing APEX?
David Srbecky94d21412022-10-10 16:02:51 +01001215 if adb.shell(
1216 f"test -x {TIMEOUT_DUMPER_PATH_PREFIX}/apex/com.android.art/bin/signal_dumper",
1217 check=False,
1218 save_cmd=False).returncode:
1219 TIMEOUT_DUMPER = "/apex/com.android.art/bin/signal_dumper"
Andreas Gampe5d775eb2019-09-13 09:54:55 -07001220 # Is it in /system/bin?
David Srbecky94d21412022-10-10 16:02:51 +01001221 elif adb.shell(
1222 f"test -x {TIMEOUT_DUMPER_PATH_PREFIX}/system/bin/signal_dumper",
1223 check=False,
1224 save_cmd=False).returncode:
1225 TIMEOUT_DUMPER = "/system/bin/signal_dumper"
David Srbeckybfa76b32022-06-20 21:30:56 +01001226 else:
David Srbecky94d21412022-10-10 16:02:51 +01001227 TIMEOUT_DUMPER = ""
David Srbeckybfa76b32022-06-20 21:30:56 +01001228 else:
David Srbecky94d21412022-10-10 16:02:51 +01001229 TIMEOUT_DUMPER = ""
Andreas Gampea6138082019-09-11 11:08:23 -07001230
David Srbeckybfa76b32022-06-20 21:30:56 +01001231 if TIMEOUT_DUMPER:
Andreas Gampe975d70b2019-09-11 11:00:18 -07001232 # Use "-l" to dump to logcat. That is convenience for the build bot crash symbolization.
Andreas Gampe38a28182019-09-13 11:36:11 -07001233 # Use exit code 124 for toybox timeout (b/141007616).
David Srbecky94d21412022-10-10 16:02:51 +01001234 timeout_dumper_cmd = f"{TIMEOUT_DUMPER} -l -s 15 -e 124"
Andreas Gampe975d70b2019-09-11 11:00:18 -07001235
David Srbecky94d21412022-10-10 16:02:51 +01001236 timeout_prefix = ""
David Srbeckybfa76b32022-06-20 21:30:56 +01001237 if TIME_OUT == "timeout":
Andreas Gampe975d70b2019-09-11 11:00:18 -07001238 # Add timeout command if time out is desired.
1239 #
1240 # Note: We first send SIGTERM (the timeout default, signal 15) to the signal dumper, which
1241 # will induce a full thread dump before killing the process. To ensure any issues in
1242 # dumping do not lead to a deadlock, we also use the "-k" option to definitely kill the
1243 # child.
1244 # Note: Using "--foreground" to not propagate the signal to children, i.e., the runtime.
David Srbecky94d21412022-10-10 16:02:51 +01001245 timeout_prefix = f"timeout --foreground -k 120s {TIME_OUT_VALUE}s {timeout_dumper_cmd} {cmdline}"
Andreas Gampe975d70b2019-09-11 11:00:18 -07001246
David Srbeckyd7674c22022-10-03 18:22:13 +01001247 env = {
1248 "ASAN_OPTIONS": RUN_TEST_ASAN_OPTIONS,
1249 "ANDROID_DATA": DEX_LOCATION,
1250 "DEX_LOCATION": DEX_LOCATION,
1251 "ANDROID_ROOT": ANDROID_ROOT,
1252 "ANDROID_I18N_ROOT": ANDROID_I18N_ROOT,
1253 "ANDROID_ART_ROOT": ANDROID_ART_ROOT,
1254 "ANDROID_TZDATA_ROOT": ANDROID_TZDATA_ROOT,
1255 "ANDROID_LOG_TAGS": ANDROID_LOG_TAGS,
1256 "LD_LIBRARY_PATH": LD_LIBRARY_PATH,
1257 "NATIVELOADER_DEFAULT_NAMESPACE_LIBS": NATIVELOADER_DEFAULT_NAMESPACE_LIBS,
1258 "PATH": f"{PREPEND_TARGET_PATH}:$PATH",
David Srbecky94d21412022-10-10 16:02:51 +01001259 } # pyformat: disable
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001260
David Srbeckye4e701b2022-10-24 11:00:31 +00001261 def run_cmd(cmdline: str, env={}, **kwargs) -> subprocess.CompletedProcess:
1262 if cmdline == "true" or DRY_RUN:
1263 return run('true') # Noop command which just executes the linux 'true' binary.
David Srbeckyd7674c22022-10-03 18:22:13 +01001264 cmdline = (f"cd {DEX_LOCATION} && " +
1265 "".join(f"export {k}={v} && " for k, v in env.items()) +
1266 cmdline)
1267 # Create a script with the command. The command can get longer than the longest
1268 # allowed adb command and there is no way to get the exit status from a adb shell command.
David Srbecky94d21412022-10-10 16:02:51 +01001269 with NamedTemporaryFile(mode="w") as cmdfile:
David Srbeckyd7674c22022-10-03 18:22:13 +01001270 cmdfile.write(cmdline)
1271 cmdfile.flush()
David Srbecky94d21412022-10-10 16:02:51 +01001272 adb.push(
1273 cmdfile.name, f"{CHROOT_DEX_LOCATION}/cmdline.sh", save_cmd=False)
David Srbeckyd7674c22022-10-03 18:22:13 +01001274 run('echo cmdline.sh "' + cmdline.replace('"', '\\"') + '"')
David Srbecky35a48ce2022-11-09 12:19:29 +00001275 chroot_prefix = f"chroot {CHROOT} " if CHROOT else ""
David Srbeckye4e701b2022-10-24 11:00:31 +00001276 return adb.shell(f"{chroot_prefix} sh {DEX_LOCATION}/cmdline.sh", **kwargs)
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001277
David Srbeckyd7674c22022-10-03 18:22:13 +01001278 if VERBOSE and (USE_GDB or USE_GDBSERVER):
1279 print(f"Forward {GDBSERVER_PORT} to local port and connect GDB")
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001280
David Srbeckyd7674c22022-10-03 18:22:13 +01001281 run_cmd(f"rm -rf {DEX_LOCATION}/dalvik-cache/")
1282 run_cmd(f"mkdir -p {mkdir_locations}")
1283 run_cmd(f"{profman_cmdline}", env)
1284 run_cmd(f"{dex2oat_cmdline}", env)
1285 run_cmd(f"{dm_cmdline}", env)
1286 run_cmd(f"{vdex_cmdline}", env)
1287 run_cmd(f"{strip_cmdline}")
1288 run_cmd(f"{sync_cmdline}")
David Srbecky35a48ce2022-11-09 12:19:29 +00001289 run_cmd(f"{timeout_prefix} {dalvikvm_cmdline}",
1290 env,
1291 stdout_file=args.stdout_file,
1292 stderr_file=args.stderr_file,
1293 expected_exit_code=args.expected_exit_code)
David Srbecky94d21412022-10-10 16:02:51 +01001294 else:
Calin Juravle24bd3f92017-05-11 00:36:53 -07001295 # Host run.
David Srbeckyc6d2a792022-08-18 14:15:43 +01001296 if USE_ZIPAPEX or USE_EXRACTED_ZIPAPEX:
Alex Light20802ca2018-12-05 15:36:03 -08001297 # Put the zipapex files in front of the ld-library-path
David Srbeckyd7674c22022-10-03 18:22:13 +01001298 LD_LIBRARY_PATH = f"{ANDROID_DATA}/zipapex/{LIBRARY_DIRECTORY}:{ANDROID_ROOT}/{TEST_DIRECTORY}"
David Srbeckybfa76b32022-06-20 21:30:56 +01001299 else:
David Srbeckyd7674c22022-10-03 18:22:13 +01001300 LD_LIBRARY_PATH = f"{ANDROID_ROOT}/{LIBRARY_DIRECTORY}:{ANDROID_ROOT}/{TEST_DIRECTORY}"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001301
David Srbeckyd7674c22022-10-03 18:22:13 +01001302 env = {
David Srbecky94d21412022-10-10 16:02:51 +01001303 "ANDROID_PRINTF_LOG": "brief",
1304 "ASAN_OPTIONS": RUN_TEST_ASAN_OPTIONS,
1305 "ANDROID_DATA": DEX_LOCATION,
1306 "DEX_LOCATION": DEX_LOCATION,
1307 "ANDROID_ROOT": ANDROID_ROOT,
1308 "ANDROID_I18N_ROOT": ANDROID_I18N_ROOT,
1309 "ANDROID_ART_ROOT": ANDROID_ART_ROOT,
1310 "ANDROID_TZDATA_ROOT": ANDROID_TZDATA_ROOT,
1311 "ANDROID_LOG_TAGS": ANDROID_LOG_TAGS,
1312 "LD_LIBRARY_PATH": LD_LIBRARY_PATH,
1313 "PATH": f"{PATH}:{ANDROID_ART_BIN_DIR}",
1314 # Temporarily disable address space layout randomization (ASLR).
1315 # This is needed on the host so that the linker loads core.oat at the necessary address.
1316 "LD_USE_LOAD_BIAS": "1",
David Srbeckyd7674c22022-10-03 18:22:13 +01001317 }
David Srbeckyc9ede382015-06-20 06:03:53 +01001318
David Srbecky94d21412022-10-10 16:02:51 +01001319 cmdline = dalvikvm_cmdline
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001320
David Srbeckybfa76b32022-06-20 21:30:56 +01001321 if TIME_OUT == "gdb":
1322 if run("uname").stdout.strip() == "Darwin":
Hiroshi Yamauchi6ffb9cc2015-08-31 15:14:17 -07001323 # Fall back to timeout on Mac.
David Srbecky94d21412022-10-10 16:02:51 +01001324 TIME_OUT = "timeout"
David Srbeckybfa76b32022-06-20 21:30:56 +01001325 elif ISA == "x86":
Hiroshi Yamauchic823eff2015-09-01 16:21:35 -07001326 # prctl call may fail in 32-bit on an older (3.2) 64-bit Linux kernel. Fall back to timeout.
David Srbecky94d21412022-10-10 16:02:51 +01001327 TIME_OUT = "timeout"
David Srbeckybfa76b32022-06-20 21:30:56 +01001328 else:
Hiroshi Yamauchi6ffb9cc2015-08-31 15:14:17 -07001329 # Check if gdb is available.
David Srbeckye60b4a72022-09-27 14:53:06 +01001330 proc = run('gdb --eval-command="quit"', check=False, save_cmd=False)
David Srbeckybfa76b32022-06-20 21:30:56 +01001331 if proc.returncode != 0:
Hiroshi Yamauchi6ffb9cc2015-08-31 15:14:17 -07001332 # gdb isn't available. Fall back to timeout.
David Srbecky94d21412022-10-10 16:02:51 +01001333 TIME_OUT = "timeout"
Hiroshi Yamauchi6ffb9cc2015-08-31 15:14:17 -07001334
David Srbeckybfa76b32022-06-20 21:30:56 +01001335 if TIME_OUT == "timeout":
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001336 # Add timeout command if time out is desired.
Andreas Gampe038bb222015-01-13 19:48:14 -08001337 #
Andreas Gampe0df2aba2019-06-10 16:53:55 -07001338 # Note: We first send SIGTERM (the timeout default, signal 15) to the signal dumper, which
1339 # will induce a full thread dump before killing the process. To ensure any issues in
1340 # dumping do not lead to a deadlock, we also use the "-k" option to definitely kill the
1341 # child.
Andreas Gampe4bdcf5d2018-12-14 10:48:53 -08001342 # Note: Using "--foreground" to not propagate the signal to children, i.e., the runtime.
David Srbecky94d21412022-10-10 16:02:51 +01001343 cmdline = f"timeout --foreground -k 120s {TIME_OUT_VALUE}s {TIMEOUT_DUMPER} -s 15 {cmdline}"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001344
David Srbeckybfa76b32022-06-20 21:30:56 +01001345 os.chdir(ANDROID_BUILD_TOP)
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001346
Calin Juravle24bd3f92017-05-11 00:36:53 -07001347 # Make sure we delete any existing compiler artifacts.
1348 # This enables tests to call the RUN script multiple times in a row
1349 # without worrying about interference.
David Srbeckybfa76b32022-06-20 21:30:56 +01001350 shutil.rmtree(f"{DEX_LOCATION}/oat", ignore_errors=True)
1351 shutil.rmtree(f"{DEX_LOCATION}/dalvik-cache/", ignore_errors=True)
Calin Juravle24bd3f92017-05-11 00:36:53 -07001352
David Srbeckybfa76b32022-06-20 21:30:56 +01001353 run(f"mkdir -p {mkdir_locations}", save_cmd=False)
David Srbeckye60b4a72022-09-27 14:53:06 +01001354 run(setupapex_cmdline)
1355 if run(installapex_test_cmdline, check=False).returncode != 0:
1356 run(installapex_cmdline)
1357 run(linkroot_cmdline)
1358 run(linkroot_overlay_cmdline)
David Srbeckyd7674c22022-10-03 18:22:13 +01001359 run(profman_cmdline, env)
1360 run(dex2oat_cmdline, env)
1361 run(dm_cmdline, env)
1362 run(vdex_cmdline, env)
David Srbeckye60b4a72022-09-27 14:53:06 +01001363 run(strip_cmdline)
1364 run(sync_cmdline)
Andreas Gampea8780822015-03-13 19:51:09 -07001365
David Srbeckyc6d2a792022-08-18 14:15:43 +01001366 if CREATE_RUNNER:
David Srbeckybfa76b32022-06-20 21:30:56 +01001367 with open(f"{DEX_LOCATION}/runit.sh", "w") as f:
1368 f.write("#!/bin/bash")
David Srbecky94d21412022-10-10 16:02:51 +01001369 for var in ("ANDROID_PRINTF_LOG ANDROID_DATA ANDROID_ROOT "
1370 "ANDROID_I18N_ROOT ANDROID_TZDATA_ROOT ANDROID_ART_ROOT "
1371 "LD_LIBRARY_PATH DYLD_LIBRARY_PATH PATH LD_USE_LOAD_BIAS"
1372 ).split(" "):
David Srbeckybfa76b32022-06-20 21:30:56 +01001373 value = os.environ.get(var, "")
1374 f.write(f'export {var}="{value}"')
David Srbeckye60b4a72022-09-27 14:53:06 +01001375 if VERBOSE:
David Srbeckybfa76b32022-06-20 21:30:56 +01001376 f.write(cmdline)
1377 else:
1378 f.writelines([
David Srbecky94d21412022-10-10 16:02:51 +01001379 "STDERR=$(mktemp)",
1380 "STDOUT=$(mktemp)",
1381 cmdline + " >${STDOUT} 2>${STDERR}",
1382 "if diff ${STDOUT} {ANDROID_DATA}/expected-stdout.txt; then",
1383 " rm -f ${STDOUT} ${STDERR}",
1384 " exit 0",
1385 "elif diff ${STDERR} {ANDROID_DATA}/expected-stderr.txt; then",
1386 " rm -f ${STDOUT} ${STDERR}",
1387 " exit 0",
1388 "else",
1389 " echo STDOUT:",
1390 " cat ${STDOUT}",
1391 " echo STDERR:",
1392 " cat ${STDERR}",
1393 " rm -f ${STDOUT} ${STDERR}",
1394 " exit 1",
1395 "fi",
David Srbeckybfa76b32022-06-20 21:30:56 +01001396 ])
1397 os.chmod("{DEX_LOCATION}/runit.sh", 0o777)
1398 print(f"Runnable test script written to {DEX_LOCATION}/runit.sh")
David Srbeckyc6d2a792022-08-18 14:15:43 +01001399 if DRY_RUN:
David Srbeckye4e701b2022-10-24 11:00:31 +00001400 return
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001401
David Srbeckyc6d2a792022-08-18 14:15:43 +01001402 if USE_GDB:
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001403 # When running under gdb, we cannot do piping and grepping...
Dmitrii Ishcheikine60131a2022-10-07 11:45:44 +00001404 env["TERM"] = os.environ.get("TERM", "")
David Srbeckyd828fc12022-10-12 14:45:04 +01001405 subprocess.run(cmdline, env=env, shell=True)
David Srbeckyc6d2a792022-08-18 14:15:43 +01001406 elif USE_GDBSERVER:
Dmitrii Ishcheikine60131a2022-10-07 11:45:44 +00001407 print(f"Connect to {GDBSERVER_PORT}")
Alex Lighte4b4a182019-02-12 14:19:49 -08001408 # When running under gdb, we cannot do piping and grepping...
David Srbeckyd828fc12022-10-12 14:45:04 +01001409 subprocess.run(cmdline, env=env, shell=True)
David Srbeckybfa76b32022-06-20 21:30:56 +01001410 else:
1411 if TIME_OUT != "gdb":
David Srbeckye4e701b2022-10-24 11:00:31 +00001412 run(cmdline,
1413 env,
David Srbecky35a48ce2022-11-09 12:19:29 +00001414 stdout_file=args.stdout_file,
1415 stderr_file=args.stderr_file,
David Srbeckye4e701b2022-10-24 11:00:31 +00001416 expected_exit_code=args.expected_exit_code)
David Srbeckybfa76b32022-06-20 21:30:56 +01001417 else:
Hiroshi Yamauchi6ffb9cc2015-08-31 15:14:17 -07001418 # With a thread dump that uses gdb if a timeout.
David Srbeckyd828fc12022-10-12 14:45:04 +01001419 proc = run(cmdline, check=False)
David Srbeckybfa76b32022-06-20 21:30:56 +01001420 # TODO: Spawn a watcher process.
1421 raise Exception("Not implemented")
1422 # ( sleep {TIME_OUT_VALUE} && \
1423 # echo "##### Thread dump using gdb on test timeout" && \
1424 # ( gdb -q -p {pid} --eval-command="info thread" --eval-command="thread apply all bt" \
1425 # --eval-command="call exit(124)" --eval-command=quit || \
1426 # kill {pid} )) 2> /dev/null & watcher=$!
David Srbecky94d21412022-10-10 16:02:51 +01001427 test_exit_status = proc.returncode
David Srbeckybfa76b32022-06-20 21:30:56 +01001428 # pkill -P {watcher} 2> /dev/null # kill the sleep which will in turn end the watcher as well
David Srbeckye4e701b2022-10-24 11:00:31 +00001429 if proc.returncode == 124 and TIME_OUT == "timeout":
1430 print("\e[91mTEST TIMED OUT!\e[0m", file=sys.stderr)
1431 assert proc.returncode == args.expected_exit_code, f"exit code: {proc.returncode}"