2 A simple testing framework for lldb using python's unit testing framework.
4 Tests for lldb are written as python scripts which take advantage of the script
5 bridging provided by LLDB.framework to interact with lldb core.
7 A specific naming pattern is followed by the .py script to be recognized as
8 a module which implements a test scenario, namely, Test*.py.
10 To specify the directories where "Test*.py" python test scripts are located,
11 you need to pass in a list of directory names. By default, the current
12 working directory is searched if nothing is specified on the command line.
18 for available options.
21 from __future__ import absolute_import
22 from __future__ import print_function
43 from . import configuration
44 from . import dotest_args
45 from . import lldbtest_config
46 from . import test_categories
47 from lldbsuite.test_event import formatter
48 from . import test_result
49 from lldbsuite.test_event.event_builder import EventBuilder
50 from ..support import seven
54 """Returns true if fpath is an executable."""
57 return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
61 """Returns the full path to a program; None otherwise."""
62 fpath, _ = os.path.split(program)
67 for path in os.environ["PATH"].split(os.pathsep):
68 exe_file = os.path.join(path, program)
76 if configuration.verbose > 0:
80 This is an example of using the -f option to pinpoint to a specific test class
81 and test method to be run:
83 $ ./dotest.py -f ClassTypesTestCase.test_with_dsym_and_run_command
84 ----------------------------------------------------------------------
87 test_with_dsym_and_run_command (TestClassTypes.ClassTypesTestCase)
88 Test 'frame variable this' when stopped on a class constructor. ... ok
90 ----------------------------------------------------------------------
95 And this is an example of using the -p option to run a single file (the filename
96 matches the pattern 'ObjC' and it happens to be 'TestObjCMethods.py'):
98 $ ./dotest.py -v -p ObjC
99 ----------------------------------------------------------------------
102 test_break_with_dsym (TestObjCMethods.FoundationTestCase)
103 Test setting objc breakpoints using '_regexp-break' and 'breakpoint set'. ... ok
104 test_break_with_dwarf (TestObjCMethods.FoundationTestCase)
105 Test setting objc breakpoints using '_regexp-break' and 'breakpoint set'. ... ok
106 test_data_type_and_expr_with_dsym (TestObjCMethods.FoundationTestCase)
107 Lookup objective-c data types and evaluate expressions. ... ok
108 test_data_type_and_expr_with_dwarf (TestObjCMethods.FoundationTestCase)
109 Lookup objective-c data types and evaluate expressions. ... ok
111 ----------------------------------------------------------------------
112 Ran 4 tests in 16.661s
116 Running of this script also sets up the LLDB_TEST environment variable so that
117 individual test cases can locate their supporting files correctly. The script
118 tries to set up Python's search paths for modules by looking at the build tree
119 relative to this script. See also the '-i' option in the following example.
121 Finally, this is an example of using the lldb.py module distributed/installed by
122 Xcode4 to run against the tests under the 'forward' directory, and with the '-w'
123 option to add some delay between two tests. It uses ARCH=x86_64 to specify that
124 as the architecture and CC=clang to specify the compiler used for the test run:
126 $ PYTHONPATH=/Xcode4/Library/PrivateFrameworks/LLDB.framework/Versions/A/Resources/Python ARCH=x86_64 CC=clang ./dotest.py -v -w -i forward
128 Session logs for test failures/errors will go into directory '2010-11-11-13_56_16'
129 ----------------------------------------------------------------------
132 test_with_dsym_and_run_command (TestForwardDeclaration.ForwardDeclarationTestCase)
133 Display *bar_ptr when stopped on a function with forward declaration of struct bar. ... ok
134 test_with_dwarf_and_run_command (TestForwardDeclaration.ForwardDeclarationTestCase)
135 Display *bar_ptr when stopped on a function with forward declaration of struct bar. ... ok
137 ----------------------------------------------------------------------
138 Ran 2 tests in 5.659s
142 The 'Session ...' verbiage is recently introduced (see also the '-s' option) to
143 notify the directory containing the session logs for test failures or errors.
144 In case there is any test failure/error, a similar message is appended at the
145 end of the stderr output for your convenience.
147 ENABLING LOGS FROM TESTS
151 Writing logs into different files per test case::
153 $ ./dotest.py --channel "lldb all"
155 $ ./dotest.py --channel "lldb all" --channel "gdb-remote packets"
157 These log files are written to:
159 <session-dir>/<test-id>-host.log (logs from lldb host process)
160 <session-dir>/<test-id>-server.log (logs from debugserver/lldb-server)
161 <session-dir>/<test-id>-<test-result>.log (console logs)
163 By default, logs from successful runs are deleted. Use the --log-success flag
164 to create reference logs for debugging.
166 $ ./dotest.py --log-success
172 def parseExclusion(exclusion_file):
173 """Parse an exclusion file, of the following format, where
174 'skip files', 'skip methods', 'xfail files', and 'xfail methods'
175 are the possible list heading values:
186 with open(exclusion_file) as f:
195 elif excl_type == 'skip':
196 if not configuration.skip_tests:
197 configuration.skip_tests = []
198 configuration.skip_tests.append(line)
199 elif excl_type == 'xfail':
200 if not configuration.xfail_tests:
201 configuration.xfail_tests = []
202 configuration.xfail_tests.append(line)
205 def parseOptionsAndInitTestdirs():
206 """Initialize the list of directories containing our unittest scripts.
208 '-h/--help as the first option prints out usage info and exit the program.
213 platform_system = platform.system()
214 platform_machine = platform.machine()
217 parser = dotest_args.create_parser()
218 args = parser.parse_args()
222 if args.unset_env_varnames:
223 for env_var in args.unset_env_varnames:
224 if env_var in os.environ:
225 # From Python Doc: When unsetenv() is supported, deletion of items in os.environ
226 # is automatically translated into a corresponding call to
228 del os.environ[env_var]
229 # os.unsetenv(env_var)
231 if args.set_env_vars:
232 for env_var in args.set_env_vars:
233 parts = env_var.split('=', 1)
235 os.environ[parts[0]] = ""
237 os.environ[parts[0]] = parts[1]
239 if args.set_inferior_env_vars:
240 lldbtest_config.inferior_env = ' '.join(args.set_inferior_env_vars)
246 configuration.compiler = os.path.realpath(args.compiler)
247 if not is_exe(configuration.compiler):
248 configuration.compiler = which(args.compiler)
249 if not is_exe(configuration.compiler):
251 '%s is not a valid compiler executable; aborting...',
255 # Use a compiler appropriate appropriate for the Apple SDK if one was
257 if platform_system == 'Darwin' and args.apple_sdk:
258 configuration.compiler = seven.get_command_output(
259 'xcrun -sdk "%s" -find clang 2> /dev/null' %
262 # 'clang' on ubuntu 14.04 is 3.4 so we try clang-3.5 first
263 candidateCompilers = ['clang-3.5', 'clang', 'gcc']
264 for candidate in candidateCompilers:
266 configuration.compiler = candidate
270 configuration.dsymutil = args.dsymutil
271 elif platform_system == 'Darwin':
272 configuration.dsymutil = seven.get_command_output(
273 'xcrun -find -toolchain default dsymutil')
276 # The lldb-dotest script produced by the CMake build passes in a path to a
277 # working FileCheck and yaml2obj binary. So does one specific Xcode
278 # project target. However, when invoking dotest.py directly, a valid
279 # --filecheck and --yaml2obj option needs to be given.
281 configuration.filecheck = os.path.abspath(args.filecheck)
284 configuration.yaml2obj = os.path.abspath(args.yaml2obj)
286 if not configuration.get_filecheck_path():
287 logging.warning('No valid FileCheck executable; some tests may fail...')
288 logging.warning('(Double-check the --filecheck argument to dotest.py)')
291 lldbtest_config.channels = args.channels
294 lldbtest_config.log_success = args.log_success
296 if args.out_of_tree_debugserver:
297 lldbtest_config.out_of_tree_debugserver = args.out_of_tree_debugserver
299 # Set SDKROOT if we are using an Apple SDK
300 if platform_system == 'Darwin' and args.apple_sdk:
301 configuration.sdkroot = seven.get_command_output(
302 'xcrun --sdk "%s" --show-sdk-path 2> /dev/null' %
306 configuration.arch = args.arch
308 configuration.arch = platform_machine
310 if args.categories_list:
311 configuration.categories_list = set(
312 test_categories.validate(
313 args.categories_list, False))
314 configuration.use_categories = True
316 configuration.categories_list = []
318 if args.skip_categories:
319 configuration.skip_categories += test_categories.validate(
320 args.skip_categories, False)
322 if args.xfail_categories:
323 configuration.xfail_categories += test_categories.validate(
324 args.xfail_categories, False)
327 os.environ['CFLAGS_EXTRAS'] = args.E
329 if args.dwarf_version:
330 configuration.dwarf_version = args.dwarf_version
331 # We cannot modify CFLAGS_EXTRAS because they're used in test cases
332 # that explicitly require no debug info.
333 os.environ['CFLAGS'] = '-gdwarf-{}'.format(configuration.dwarf_version)
336 for setting in args.settings:
337 if not len(setting) == 1 or not setting[0].count('='):
338 logging.error('"%s" is not a setting in the form "key=value"',
341 setting_list = setting[0].split('=', 1)
342 configuration.settings.append((setting_list[0], setting_list[1]))
346 "Suspending the process %d to wait for debugger to attach...\n" %
349 os.kill(os.getpid(), signal.SIGSTOP)
352 if any([x.startswith('-') for x in args.f]):
354 configuration.filters.extend(args.f)
357 configuration.lldb_framework_path = args.framework
360 # lldb executable is passed explicitly
361 lldbtest_config.lldbExec = os.path.realpath(args.executable)
362 if not is_exe(lldbtest_config.lldbExec):
363 lldbtest_config.lldbExec = which(args.executable)
364 if not is_exe(lldbtest_config.lldbExec):
366 '%s is not a valid executable to test; aborting...',
370 if args.server and args.out_of_tree_debugserver:
371 logging.warning('Both --server and --out-of-tree-debugserver are set')
373 if args.server and not args.out_of_tree_debugserver:
374 os.environ['LLDB_DEBUGSERVER_PATH'] = args.server
377 for excl_file in args.excluded:
378 parseExclusion(excl_file)
381 if args.p.startswith('-'):
383 configuration.regexp = args.p
386 configuration.sdir_name = args.s
388 timestamp_started = datetime.datetime.now().strftime("%Y-%m-%d-%H_%M_%S")
389 configuration.sdir_name = os.path.join(os.getcwd(), timestamp_started)
391 configuration.session_file_format = args.session_file_format
394 os.environ['LLDB_COMMAND_TRACE'] = 'YES'
397 configuration.verbose = 2
399 # argparse makes sure we have a number
401 configuration.count = args.sharp
403 if sys.platform.startswith('win32'):
404 os.environ['LLDB_DISABLE_CRASH_DIALOG'] = str(
405 args.disable_crash_dialog)
406 os.environ['LLDB_LAUNCH_INFERIORS_WITHOUT_CONSOLE'] = str(True)
411 if args.results_file:
412 configuration.results_filename = args.results_file
414 if args.results_formatter:
415 configuration.results_formatter_name = args.results_formatter
416 if args.results_formatter_options:
417 configuration.results_formatter_options = args.results_formatter_options
419 # Default to using the BasicResultsFormatter if no formatter is specified.
420 if configuration.results_formatter_name is None:
421 configuration.results_formatter_name = (
422 "lldbsuite.test_event.formatter.results_formatter.ResultsFormatter")
424 # Reproducer arguments
425 if args.capture_path and args.replay_path:
426 logging.error('Cannot specify both a capture and a replay path.')
429 if args.capture_path:
430 configuration.capture_path = args.capture_path
433 configuration.replay_path = args.replay_path
435 # rerun-related arguments
436 configuration.rerun_all_issues = args.rerun_all_issues
438 if args.lldb_platform_name:
439 configuration.lldb_platform_name = args.lldb_platform_name
440 if args.lldb_platform_url:
441 configuration.lldb_platform_url = args.lldb_platform_url
442 if args.lldb_platform_working_dir:
443 configuration.lldb_platform_working_dir = args.lldb_platform_working_dir
444 if args.test_build_dir:
445 configuration.test_build_dir = args.test_build_dir
446 if args.lldb_module_cache_dir:
447 configuration.lldb_module_cache_dir = args.lldb_module_cache_dir
449 configuration.lldb_module_cache_dir = os.path.join(
450 configuration.test_build_dir, 'module-cache-lldb')
451 if args.clang_module_cache_dir:
452 configuration.clang_module_cache_dir = args.clang_module_cache_dir
454 configuration.clang_module_cache_dir = os.path.join(
455 configuration.test_build_dir, 'module-cache-clang')
457 if args.lldb_libs_dir:
458 configuration.lldb_libs_dir = args.lldb_libs_dir
460 if args.enabled_plugins:
461 configuration.enabled_plugins = args.enabled_plugins
463 # Gather all the dirs passed on the command line.
464 if len(args.args) > 0:
465 configuration.testdirs = [os.path.realpath(os.path.abspath(x)) for x in args.args]
467 lldbtest_config.codesign_identity = args.codesign_identity
470 def setupTestResults():
471 """Sets up test results-related objects based on arg settings."""
472 # Setup the results formatter configuration.
473 formatter_config = formatter.FormatterConfig()
474 formatter_config.filename = configuration.results_filename
475 formatter_config.formatter_name = configuration.results_formatter_name
476 formatter_config.formatter_options = (
477 configuration.results_formatter_options)
479 # Create the results formatter.
480 formatter_spec = formatter.create_results_formatter(
482 if formatter_spec is not None and formatter_spec.formatter is not None:
483 configuration.results_formatter_object = formatter_spec.formatter
485 # Send an initialize message to the formatter.
486 initialize_event = EventBuilder.bare_event("initialize")
487 initialize_event["worker_count"] = 1
489 formatter_spec.formatter.handle_event(initialize_event)
491 # Make sure we clean up the formatter on shutdown.
492 if formatter_spec.cleanup_func is not None:
493 atexit.register(formatter_spec.cleanup_func)
498 Add LLDB.framework/Resources/Python to the search paths for modules.
499 As a side effect, we also discover the 'lldb' executable and export it here.
502 # Get the directory containing the current script.
503 if "DOTEST_PROFILE" in os.environ and "DOTEST_SCRIPT_DIR" in os.environ:
504 scriptPath = os.environ["DOTEST_SCRIPT_DIR"]
506 scriptPath = os.path.dirname(os.path.realpath(__file__))
507 if not scriptPath.endswith('test'):
508 print("This script expects to reside in lldb's test directory.")
511 os.environ["LLDB_TEST"] = scriptPath
512 os.environ["LLDB_TEST_SRC"] = lldbsuite.lldb_test_root
514 # Set up the root build directory.
515 if not configuration.test_build_dir:
516 raise Exception("test_build_dir is not set")
517 configuration.test_build_dir = os.path.abspath(configuration.test_build_dir)
519 # Set up the LLDB_SRC environment variable, so that the tests can locate
520 # the LLDB source code.
521 os.environ["LLDB_SRC"] = lldbsuite.lldb_root
523 pluginPath = os.path.join(scriptPath, 'plugins')
524 toolsLLDBVSCode = os.path.join(scriptPath, 'tools', 'lldb-vscode')
525 toolsLLDBServerPath = os.path.join(scriptPath, 'tools', 'lldb-server')
527 # Insert script dir, plugin dir and lldb-server dir to the sys.path.
528 sys.path.insert(0, pluginPath)
529 # Adding test/tools/lldb-vscode to the path makes it easy to
530 # "import lldb_vscode_testcase" from the VSCode tests
531 sys.path.insert(0, toolsLLDBVSCode)
532 # Adding test/tools/lldb-server to the path makes it easy
533 sys.path.insert(0, toolsLLDBServerPath)
534 # to "import lldbgdbserverutils" from the lldb-server tests
536 # This is the root of the lldb git/svn checkout
537 # When this changes over to a package instead of a standalone script, this
538 # will be `lldbsuite.lldb_root`
539 lldbRootDirectory = lldbsuite.lldb_root
541 # Some of the tests can invoke the 'lldb' command directly.
542 # We'll try to locate the appropriate executable right here.
544 # The lldb executable can be set from the command line
545 # if it's not set, we try to find it now
546 # first, we try the environment
547 if not lldbtest_config.lldbExec:
548 # First, you can define an environment variable LLDB_EXEC specifying the
549 # full pathname of the lldb executable.
550 if "LLDB_EXEC" in os.environ:
551 lldbtest_config.lldbExec = os.environ["LLDB_EXEC"]
553 if not lldbtest_config.lldbExec:
554 # Last, check the path
555 lldbtest_config.lldbExec = which('lldb')
557 if lldbtest_config.lldbExec and not is_exe(lldbtest_config.lldbExec):
559 "'{}' is not a path to a valid executable".format(
560 lldbtest_config.lldbExec))
561 lldbtest_config.lldbExec = None
563 if not lldbtest_config.lldbExec:
564 print("The 'lldb' executable cannot be located. Some of the tests may not be run as a result.")
567 # confusingly, this is the "bin" directory
568 lldbLibDir = os.path.dirname(lldbtest_config.lldbExec)
569 os.environ["LLDB_LIB_DIR"] = lldbLibDir
570 lldbImpLibDir = configuration.lldb_libs_dir
571 os.environ["LLDB_IMPLIB_DIR"] = lldbImpLibDir
572 print("LLDB library dir:", os.environ["LLDB_LIB_DIR"])
573 print("LLDB import library dir:", os.environ["LLDB_IMPLIB_DIR"])
574 os.system('%s -v' % lldbtest_config.lldbExec)
576 lldbDir = os.path.dirname(lldbtest_config.lldbExec)
578 lldbVSCodeExec = os.path.join(lldbDir, "lldb-vscode")
579 if is_exe(lldbVSCodeExec):
580 os.environ["LLDBVSCODE_EXEC"] = lldbVSCodeExec
582 if not configuration.shouldSkipBecauseOfCategories(["lldb-vscode"]):
584 "The 'lldb-vscode' executable cannot be located. The lldb-vscode tests can not be run as a result.")
585 configuration.skip_categories.append("lldb-vscode")
587 lldbPythonDir = None # The directory that contains 'lldb/__init__.py'
588 if not configuration.lldb_framework_path and os.path.exists(os.path.join(lldbLibDir, "LLDB.framework")):
589 configuration.lldb_framework_path = os.path.join(lldbLibDir, "LLDB.framework")
590 if configuration.lldb_framework_path:
591 lldbtest_config.lldb_framework_path = configuration.lldb_framework_path
592 candidatePath = os.path.join(
593 configuration.lldb_framework_path, 'Resources', 'Python')
594 if os.path.isfile(os.path.join(candidatePath, 'lldb/__init__.py')):
595 lldbPythonDir = candidatePath
596 if not lldbPythonDir:
598 'Resources/Python/lldb/__init__.py was not found in ' +
599 configuration.lldb_framework_path)
602 # If our lldb supports the -P option, use it to find the python path:
603 init_in_python_dir = os.path.join('lldb', '__init__.py')
605 lldb_dash_p_result = subprocess.check_output(
606 [lldbtest_config.lldbExec, "-P"], stderr=subprocess.STDOUT, universal_newlines=True)
608 if lldb_dash_p_result and not lldb_dash_p_result.startswith(
609 ("<", "lldb: invalid option:")) and not lldb_dash_p_result.startswith("Traceback"):
610 lines = lldb_dash_p_result.splitlines()
612 # Workaround for readline vs libedit issue on FreeBSD. If stdout
613 # is not a terminal Python executes
614 # rl_variable_bind ("enable-meta-key", "off");
615 # This produces a warning with FreeBSD's libedit because the
616 # enable-meta-key variable is unknown. Not an issue on Apple
617 # because cpython commit f0ab6f9f0603 added a #ifndef __APPLE__
618 # around the call. See http://bugs.python.org/issue19884 for more
619 # information. For now we just discard the warning output.
620 if len(lines) >= 1 and lines[0].startswith(
621 "bind: Invalid command"):
624 # Taking the last line because lldb outputs
625 # 'Cannot read termcap database;\nusing dumb terminal settings.\n'
627 if len(lines) >= 1 and os.path.isfile(
628 os.path.join(lines[-1], init_in_python_dir)):
629 lldbPythonDir = lines[-1]
630 if "freebsd" in sys.platform or "linux" in sys.platform:
631 os.environ['LLDB_LIB_DIR'] = os.path.join(
632 lldbPythonDir, '..', '..')
634 if not lldbPythonDir:
636 "Unable to load lldb extension module. Possible reasons for this include:")
637 print(" 1) LLDB was built with LLDB_ENABLE_PYTHON=0")
639 " 2) PYTHONPATH and PYTHONHOME are not set correctly. PYTHONHOME should refer to")
641 " the version of Python that LLDB built and linked against, and PYTHONPATH")
643 " should contain the Lib directory for the same python distro, as well as the")
644 print(" location of LLDB\'s site-packages folder.")
646 " 3) A different version of Python than that which was built against is exported in")
647 print(" the system\'s PATH environment variable, causing conflicts.")
649 " 4) The executable '%s' could not be found. Please check " %
650 lldbtest_config.lldbExec)
651 print(" that it exists and is executable.")
654 lldbPythonDir = os.path.normpath(lldbPythonDir)
655 # Some of the code that uses this path assumes it hasn't resolved the Versions... link.
656 # If the path we've constructed looks like that, then we'll strip out
657 # the Versions/A part.
658 (before, frameWithVersion, after) = lldbPythonDir.rpartition(
659 "LLDB.framework/Versions/A")
660 if frameWithVersion != "":
661 lldbPythonDir = before + "LLDB.framework" + after
663 lldbPythonDir = os.path.abspath(lldbPythonDir)
665 # If tests need to find LLDB_FRAMEWORK, now they can do it
666 os.environ["LLDB_FRAMEWORK"] = os.path.dirname(
667 os.path.dirname(lldbPythonDir))
669 # This is to locate the lldb.py module. Insert it right after
671 sys.path[1:1] = [lldbPythonDir]
674 def visit_file(dir, name):
675 # Try to match the regexp pattern, if specified.
676 if configuration.regexp:
677 if not re.search(configuration.regexp, name):
678 # We didn't match the regex, we're done.
681 if configuration.skip_tests:
682 for file_regexp in configuration.skip_tests:
683 if re.search(file_regexp, name):
686 # We found a match for our test. Add it to the suite.
688 # Update the sys.path first.
689 if not sys.path.count(dir):
690 sys.path.insert(0, dir)
691 base = os.path.splitext(name)[0]
693 # Thoroughly check the filterspec against the base module and admit
694 # the (base, filterspec) combination only when it makes sense.
696 def check(obj, parts):
699 parent, obj = obj, getattr(obj, part)
700 except AttributeError:
701 # The filterspec has failed.
705 module = __import__(base)
708 for filterspec in configuration.filters:
709 parts = filterspec.split('.')
710 if check(module, parts):
712 elif parts[0] == base and len(parts) > 1 and check(module, parts[1:]):
713 yield '.'.join(parts[1:])
715 for key,value in module.__dict__.items():
716 if check(value, parts):
717 yield key + '.' + filterspec
720 for filterspec in iter_filters():
722 print("adding filter spec %s to module %s" % (filterspec, repr(module)))
723 tests = unittest2.defaultTestLoader.loadTestsFromName(filterspec, module)
724 configuration.suite.addTests(tests)
726 # Forgo this module if the (base, filterspec) combo is invalid
727 if configuration.filters and not filtered:
731 # Add the entire file's worth of tests since we're not filtered.
732 # Also the fail-over case when the filterspec branch
733 # (base, filterspec) combo doesn't make sense.
734 configuration.suite.addTests(
735 unittest2.defaultTestLoader.loadTestsFromName(base))
738 def visit(prefix, dir, names):
739 """Visitor function for os.path.walk(path, visit, arg)."""
741 dir_components = set(dir.split(os.sep))
742 excluded_components = set(['.svn', '.git'])
743 if dir_components.intersection(excluded_components):
746 # Gather all the Python test file names that follow the Test*.py pattern.
747 python_test_files = [
750 if name.endswith('.py') and name.startswith(prefix)]
752 # Visit all the python test files.
753 for name in python_test_files:
755 # Ensure we error out if we have multiple tests with the same
757 # Future improvement: find all the places where we work with base
758 # names and convert to full paths. We have directory structure
759 # to disambiguate these, so we shouldn't need this constraint.
760 if name in configuration.all_tests:
761 raise Exception("Found multiple tests with the name %s" % name)
762 configuration.all_tests.add(name)
764 # Run the relevant tests in the python file.
765 visit_file(dir, name)
766 except Exception as ex:
767 # Convert this exception to a test event error for the file.
768 test_filename = os.path.abspath(os.path.join(dir, name))
769 if configuration.results_formatter_object is not None:
770 # Grab the backtrace for the exception.
772 backtrace = traceback.format_exc()
774 # Generate the test event.
775 configuration.results_formatter_object.handle_event(
776 EventBuilder.event_for_job_test_add_error(
777 test_filename, ex, backtrace))
781 # ======================================== #
783 # Execution of the test driver starts here #
785 # ======================================== #
788 def checkDsymForUUIDIsNotOn():
789 cmd = ["defaults", "read", "com.apple.DebugSymbols"]
790 process = subprocess.Popen(
792 stdout=subprocess.PIPE,
793 stderr=subprocess.STDOUT)
794 cmd_output = process.stdout.read()
795 output_str = cmd_output.decode("utf-8")
796 if "DBGFileMappedPaths = " in output_str:
797 print("%s =>" % ' '.join(cmd))
800 "Disable automatic lookup and caching of dSYMs before running the test suite!")
805 def exitTestSuite(exitCode=None):
806 # lldb.py does SBDebugger.Initialize().
807 # Call SBDebugger.Terminate() on exit.
809 lldb.SBDebugger.Terminate()
814 def getVersionForSDK(sdk):
816 full_path = seven.get_command_output('xcrun -sdk %s --show-sdk-path' % sdk)
817 basename = os.path.basename(full_path)
818 basename = os.path.splitext(basename)[0]
819 basename = str.lower(basename)
820 ver = basename.replace(sdk, '')
824 def setDefaultTripleForPlatform():
825 if configuration.lldb_platform_name == 'ios-simulator':
826 triple_str = 'x86_64-apple-ios%s' % (
827 getVersionForSDK('iphonesimulator'))
828 os.environ['TRIPLE'] = triple_str
829 return {'TRIPLE': triple_str}
834 # Add some intervention here to sanity check that the compiler requested is sane.
835 # If found not to be an executable program, we abort.
836 c = configuration.compiler
840 if not sys.platform.startswith("darwin"):
841 raise Exception(c + " is not a valid compiler")
843 pipe = subprocess.Popen(
844 ['xcrun', '-find', c], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
845 cmd_output = pipe.stdout.read()
846 if not cmd_output or "not found" in cmd_output:
847 raise Exception(c + " is not a valid compiler")
849 configuration.compiler = cmd_output.split('\n')[0]
850 print("'xcrun -find %s' returning %s" % (c, configuration.compiler))
852 def canRunLibcxxTests():
853 from lldbsuite.test import lldbplatformutil
855 platform = lldbplatformutil.getPlatform()
857 if lldbplatformutil.target_is_android() or lldbplatformutil.platformIsDarwin():
858 return True, "libc++ always present"
860 if platform == "linux":
861 if os.path.isdir("/usr/include/c++/v1"):
862 return True, "Headers found, let's hope they work"
863 with tempfile.NamedTemporaryFile() as f:
864 cmd = [configuration.compiler, "-xc++", "-stdlib=libc++", "-o", f.name, "-"]
865 p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
866 _, stderr = p.communicate("#include <algorithm>\nint main() {}")
868 return True, "Compiling with -stdlib=libc++ works"
869 return False, "Compiling with -stdlib=libc++ fails with the error: %s" % stderr
871 return False, "Don't know how to build with libc++ on %s" % platform
873 def checkLibcxxSupport():
874 result, reason = canRunLibcxxTests()
876 return # libc++ supported
877 if "libc++" in configuration.categories_list:
878 return # libc++ category explicitly requested, let it run.
879 print("Libc++ tests will not be run because: " + reason)
880 configuration.skip_categories.append("libc++")
882 def canRunLibstdcxxTests():
883 from lldbsuite.test import lldbplatformutil
885 platform = lldbplatformutil.getPlatform()
886 if lldbplatformutil.target_is_android():
888 if platform == "linux":
889 return True, "libstdcxx always present"
890 return False, "Don't know how to build with libstdcxx on %s" % platform
892 def checkLibstdcxxSupport():
893 result, reason = canRunLibstdcxxTests()
895 return # libstdcxx supported
896 if "libstdcxx" in configuration.categories_list:
897 return # libstdcxx category explicitly requested, let it run.
898 print("libstdcxx tests will not be run because: " + reason)
899 configuration.skip_categories.append("libstdcxx")
901 def canRunWatchpointTests():
902 from lldbsuite.test import lldbplatformutil
904 platform = lldbplatformutil.getPlatform()
905 if platform == "netbsd":
906 if os.geteuid() == 0:
907 return True, "root can always write dbregs"
909 output = subprocess.check_output(["/sbin/sysctl", "-n",
910 "security.models.extensions.user_set_dbregs"]).decode().strip()
912 return True, "security.models.extensions.user_set_dbregs enabled"
913 except subprocess.CalledProcessError:
915 return False, "security.models.extensions.user_set_dbregs disabled"
916 return True, "watchpoint support available"
918 def checkWatchpointSupport():
919 result, reason = canRunWatchpointTests()
921 return # watchpoints supported
922 if "watchpoint" in configuration.categories_list:
923 return # watchpoint category explicitly requested, let it run.
924 print("watchpoint tests will not be run because: " + reason)
925 configuration.skip_categories.append("watchpoint")
927 def checkDebugInfoSupport():
930 platform = lldb.selected_platform.GetTriple().split('-')[2]
931 compiler = configuration.compiler
933 for cat in test_categories.debug_info_categories:
934 if cat in configuration.categories_list:
935 continue # Category explicitly requested, let it run.
936 if test_categories.is_supported_on_platform(cat, platform, compiler):
938 configuration.skip_categories.append(cat)
941 print("Skipping following debug info categories:", skipped)
944 # On MacOS X, check to make sure that domain for com.apple.DebugSymbols defaults
945 # does not exist before proceeding to running the test suite.
946 if sys.platform.startswith("darwin"):
947 checkDsymForUUIDIsNotOn()
949 # Start the actions by first parsing the options while setting up the test
950 # directories, followed by setting up the search paths for lldb utilities;
951 # then, we walk the directory trees and collect the tests into our test suite.
953 parseOptionsAndInitTestdirs()
955 # Setup test results (test results formatter and output handling).
961 if configuration.capture_path or configuration.replay_path:
962 lldbconfig.INITIALIZE = False
965 if configuration.capture_path:
966 lldb.SBReproducer.Capture(configuration.capture_path)
967 lldb.SBReproducer.SetAutoGenerate(True)
968 elif configuration.replay_path:
969 lldb.SBReproducer.PassiveReplay(configuration.replay_path)
971 if not lldbconfig.INITIALIZE:
972 lldb.SBDebugger.Initialize()
974 # Use host platform by default.
975 lldb.selected_platform = lldb.SBPlatform.GetHostPlatform()
977 # Now we can also import lldbutil
978 from lldbsuite.test import lldbutil
980 if configuration.lldb_platform_name:
981 print("Setting up remote platform '%s'" %
982 (configuration.lldb_platform_name))
983 lldb.remote_platform = lldb.SBPlatform(
984 configuration.lldb_platform_name)
985 if not lldb.remote_platform.IsValid():
987 "error: unable to create the LLDB platform named '%s'." %
988 (configuration.lldb_platform_name))
990 if configuration.lldb_platform_url:
991 # We must connect to a remote platform if a LLDB platform URL was
994 "Connecting to remote platform '%s' at '%s'..." %
995 (configuration.lldb_platform_name, configuration.lldb_platform_url))
996 platform_connect_options = lldb.SBPlatformConnectOptions(
997 configuration.lldb_platform_url)
998 err = lldb.remote_platform.ConnectRemote(platform_connect_options)
1002 print("error: failed to connect to remote platform using URL '%s': %s" % (
1003 configuration.lldb_platform_url, err))
1006 configuration.lldb_platform_url = None
1008 platform_changes = setDefaultTripleForPlatform()
1010 for key in platform_changes:
1012 print("Environment variables setup for platform support:")
1014 print("%s = %s" % (key, platform_changes[key]))
1016 if configuration.lldb_platform_working_dir:
1017 print("Setting remote platform working directory to '%s'..." %
1018 (configuration.lldb_platform_working_dir))
1019 error = lldb.remote_platform.MakeDirectory(
1020 configuration.lldb_platform_working_dir, 448) # 448 = 0o700
1022 raise Exception("making remote directory '%s': %s" % (
1023 configuration.lldb_platform_working_dir, error))
1025 if not lldb.remote_platform.SetWorkingDirectory(
1026 configuration.lldb_platform_working_dir):
1027 raise Exception("failed to set working directory '%s'" % configuration.lldb_platform_working_dir)
1028 lldb.selected_platform = lldb.remote_platform
1030 lldb.remote_platform = None
1031 configuration.lldb_platform_working_dir = None
1032 configuration.lldb_platform_url = None
1034 # Set up the working directory.
1035 # Note that it's not dotest's job to clean this directory.
1036 lldbutil.mkdir_p(configuration.test_build_dir)
1038 target_platform = lldb.selected_platform.GetTriple().split('-')[2]
1040 checkLibcxxSupport()
1041 checkLibstdcxxSupport()
1042 checkWatchpointSupport()
1043 checkDebugInfoSupport()
1045 # Don't do debugserver tests on anything except OS X.
1046 configuration.dont_do_debugserver_test = (
1047 "linux" in target_platform or
1048 "freebsd" in target_platform or
1049 "netbsd" in target_platform or
1050 "windows" in target_platform)
1052 # Don't do lldb-server (llgs) tests on anything except Linux and Windows.
1053 configuration.dont_do_llgs_test = not (
1054 "linux" in target_platform or
1055 "netbsd" in target_platform or
1056 "windows" in target_platform)
1058 for testdir in configuration.testdirs:
1059 for (dirpath, dirnames, filenames) in os.walk(testdir):
1060 visit('Test', dirpath, filenames)
1063 # Now that we have loaded all the test cases, run the whole test suite.
1066 # Install the control-c handler.
1067 unittest2.signals.installHandler()
1069 lldbutil.mkdir_p(configuration.sdir_name)
1070 os.environ["LLDB_SESSION_DIRNAME"] = configuration.sdir_name
1073 "\nSession logs for test failures/errors/unexpected successes"
1074 " will go into directory '%s'\n" %
1075 configuration.sdir_name)
1078 # Invoke the default TextTestRunner to run the test suite
1082 if configuration.verbose:
1083 print("compiler=%s" % configuration.compiler)
1085 # Iterating over all possible architecture and compiler combinations.
1086 configString = "arch=%s compiler=%s" % (configuration.arch,
1087 configuration.compiler)
1089 # Output the configuration.
1090 if configuration.verbose:
1091 sys.stderr.write("\nConfiguration: " + configString + "\n")
1093 # First, write out the number of collected test cases.
1094 if configuration.verbose:
1095 sys.stderr.write(configuration.separator + "\n")
1097 "Collected %d test%s\n\n" %
1098 (configuration.suite.countTestCases(),
1099 configuration.suite.countTestCases() != 1 and "s" or ""))
1101 # Invoke the test runner.
1102 if configuration.count == 1:
1103 result = unittest2.TextTestRunner(
1105 verbosity=configuration.verbose,
1106 resultclass=test_result.LLDBTestResult).run(
1107 configuration.suite)
1109 # We are invoking the same test suite more than once. In this case,
1110 # mark __ignore_singleton__ flag as True so the signleton pattern is
1112 test_result.LLDBTestResult.__ignore_singleton__ = True
1113 for i in range(configuration.count):
1115 result = unittest2.TextTestRunner(
1117 verbosity=configuration.verbose,
1118 resultclass=test_result.LLDBTestResult).run(
1119 configuration.suite)
1121 configuration.failed = not result.wasSuccessful()
1123 if configuration.sdir_has_content and configuration.verbose:
1125 "Session logs for test failures/errors/unexpected successes"
1126 " can be found in directory '%s'\n" %
1127 configuration.sdir_name)
1129 if configuration.use_categories and len(
1130 configuration.failures_per_category) > 0:
1131 sys.stderr.write("Failures per category:\n")
1132 for category in configuration.failures_per_category:
1135 (category, configuration.failures_per_category[category]))
1138 exitTestSuite(configuration.failed)
1140 if __name__ == "__main__":
1143 " is for use as a module only. It should not be run as a standalone script.")