1""" 2LLDB module which provides the abstract base class of lldb test case. 3 4The concrete subclass can override lldbtest.TestBase in order to inherit the 5common behavior for unitest.TestCase.setUp/tearDown implemented in this file. 6 7The subclass should override the attribute mydir in order for the python runtime 8to locate the individual test cases when running as part of a large test suite 9or when running each test case as a separate python invocation. 10 11./dotest.py provides a test driver which sets up the environment to run the 12entire of part of the test suite . Example: 13 14# Exercises the test suite in the types directory.... 15/Volumes/data/lldb/svn/ToT/test $ ./dotest.py -A x86_64 types 16... 17 18Session logs for test failures/errors/unexpected successes will go into directory '2012-05-16-13_35_42' 19Command invoked: python ./dotest.py -A x86_64 types 20compilers=['clang'] 21 22Configuration: arch=x86_64 compiler=clang 23---------------------------------------------------------------------- 24Collected 72 tests 25 26........................................................................ 27---------------------------------------------------------------------- 28Ran 72 tests in 135.468s 29 30OK 31$ 32""" 33 34from __future__ import absolute_import 35from __future__ import print_function 36 37# System modules 38import abc 39from distutils.version import LooseVersion 40from functools import wraps 41import gc 42import glob 43import io 44import os.path 45import re 46import shutil 47import signal 48from subprocess import * 49import sys 50import time 51import traceback 52import distutils.spawn 53 54# Third-party modules 55import unittest2 56from six import add_metaclass 57from six import StringIO as SixStringIO 58import six 59 60# LLDB modules 61import lldb 62from . import configuration 63from . import decorators 64from . import lldbplatformutil 65from . import lldbtest_config 66from . import lldbutil 67from . import test_categories 68from lldbsuite.support import encoded_file 69from lldbsuite.support import funcutils 70from lldbsuite.support import seven 71from lldbsuite.test.builders import get_builder 72from lldbsuite.test_event import build_exception 73 74# See also dotest.parseOptionsAndInitTestdirs(), where the environment variables 75# LLDB_COMMAND_TRACE is set from '-t' option. 76 77# By default, traceAlways is False. 78if "LLDB_COMMAND_TRACE" in os.environ and os.environ[ 79 "LLDB_COMMAND_TRACE"] == "YES": 80 traceAlways = True 81else: 82 traceAlways = False 83 84# By default, doCleanup is True. 85if "LLDB_DO_CLEANUP" in os.environ and os.environ["LLDB_DO_CLEANUP"] == "NO": 86 doCleanup = False 87else: 88 doCleanup = True 89 90 91# 92# Some commonly used assert messages. 93# 94 95COMMAND_FAILED_AS_EXPECTED = "Command has failed as expected" 96 97CURRENT_EXECUTABLE_SET = "Current executable set successfully" 98 99PROCESS_IS_VALID = "Process is valid" 100 101PROCESS_KILLED = "Process is killed successfully" 102 103PROCESS_EXITED = "Process exited successfully" 104 105PROCESS_STOPPED = "Process status should be stopped" 106 107RUN_SUCCEEDED = "Process is launched successfully" 108 109RUN_COMPLETED = "Process exited successfully" 110 111BACKTRACE_DISPLAYED_CORRECTLY = "Backtrace displayed correctly" 112 113BREAKPOINT_CREATED = "Breakpoint created successfully" 114 115BREAKPOINT_STATE_CORRECT = "Breakpoint state is correct" 116 117BREAKPOINT_PENDING_CREATED = "Pending breakpoint created successfully" 118 119BREAKPOINT_HIT_ONCE = "Breakpoint resolved with hit count = 1" 120 121BREAKPOINT_HIT_TWICE = "Breakpoint resolved with hit count = 2" 122 123BREAKPOINT_HIT_THRICE = "Breakpoint resolved with hit count = 3" 124 125MISSING_EXPECTED_REGISTERS = "At least one expected register is unavailable." 126 127OBJECT_PRINTED_CORRECTLY = "Object printed correctly" 128 129SOURCE_DISPLAYED_CORRECTLY = "Source code displayed correctly" 130 131STEP_IN_SUCCEEDED = "Thread step-in succeeded" 132 133STEP_OUT_SUCCEEDED = "Thread step-out succeeded" 134 135STOPPED_DUE_TO_EXC_BAD_ACCESS = "Process should be stopped due to bad access exception" 136 137STOPPED_DUE_TO_ASSERT = "Process should be stopped due to an assertion" 138 139STOPPED_DUE_TO_BREAKPOINT = "Process should be stopped due to breakpoint" 140 141STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS = "%s, %s" % ( 142 STOPPED_DUE_TO_BREAKPOINT, "instead, the actual stop reason is: '%s'") 143 144STOPPED_DUE_TO_BREAKPOINT_CONDITION = "Stopped due to breakpoint condition" 145 146STOPPED_DUE_TO_BREAKPOINT_IGNORE_COUNT = "Stopped due to breakpoint and ignore count" 147 148STOPPED_DUE_TO_BREAKPOINT_JITTED_CONDITION = "Stopped due to breakpoint jitted condition" 149 150STOPPED_DUE_TO_SIGNAL = "Process state is stopped due to signal" 151 152STOPPED_DUE_TO_STEP_IN = "Process state is stopped due to step in" 153 154STOPPED_DUE_TO_WATCHPOINT = "Process should be stopped due to watchpoint" 155 156DATA_TYPES_DISPLAYED_CORRECTLY = "Data type(s) displayed correctly" 157 158VALID_BREAKPOINT = "Got a valid breakpoint" 159 160VALID_BREAKPOINT_LOCATION = "Got a valid breakpoint location" 161 162VALID_COMMAND_INTERPRETER = "Got a valid command interpreter" 163 164VALID_FILESPEC = "Got a valid filespec" 165 166VALID_MODULE = "Got a valid module" 167 168VALID_PROCESS = "Got a valid process" 169 170VALID_SYMBOL = "Got a valid symbol" 171 172VALID_TARGET = "Got a valid target" 173 174VALID_PLATFORM = "Got a valid platform" 175 176VALID_TYPE = "Got a valid type" 177 178VALID_VARIABLE = "Got a valid variable" 179 180VARIABLES_DISPLAYED_CORRECTLY = "Variable(s) displayed correctly" 181 182WATCHPOINT_CREATED = "Watchpoint created successfully" 183 184 185def CMD_MSG(str): 186 '''A generic "Command '%s' did not return successfully" message generator.''' 187 return "Command '%s' did not return successfully" % str 188 189 190def COMPLETION_MSG(str_before, str_after, completions): 191 '''A generic assertion failed message generator for the completion mechanism.''' 192 return ("'%s' successfully completes to '%s', but completions were:\n%s" 193 % (str_before, str_after, "\n".join(completions))) 194 195 196def EXP_MSG(str, actual, exe): 197 '''A generic "'%s' returned unexpected result" message generator if exe. 198 Otherwise, it generates "'%s' does not match expected result" message.''' 199 200 return "'%s' %s result, got '%s'" % ( 201 str, 'returned unexpected' if exe else 'does not match expected', actual.strip()) 202 203 204def SETTING_MSG(setting): 205 '''A generic "Value of setting '%s' is not correct" message generator.''' 206 return "Value of setting '%s' is not correct" % setting 207 208 209def line_number(filename, string_to_match): 210 """Helper function to return the line number of the first matched string.""" 211 with io.open(filename, mode='r', encoding="utf-8") as f: 212 for i, line in enumerate(f): 213 if line.find(string_to_match) != -1: 214 # Found our match. 215 return i + 1 216 raise Exception( 217 "Unable to find '%s' within file %s" % 218 (string_to_match, filename)) 219 220def get_line(filename, line_number): 221 """Return the text of the line at the 1-based line number.""" 222 with io.open(filename, mode='r', encoding="utf-8") as f: 223 return f.readlines()[line_number - 1] 224 225def pointer_size(): 226 """Return the pointer size of the host system.""" 227 import ctypes 228 a_pointer = ctypes.c_void_p(0xffff) 229 return 8 * ctypes.sizeof(a_pointer) 230 231 232def is_exe(fpath): 233 """Returns true if fpath is an executable.""" 234 return os.path.isfile(fpath) and os.access(fpath, os.X_OK) 235 236 237def which(program): 238 """Returns the full path to a program; None otherwise.""" 239 fpath, fname = os.path.split(program) 240 if fpath: 241 if is_exe(program): 242 return program 243 else: 244 for path in os.environ["PATH"].split(os.pathsep): 245 exe_file = os.path.join(path, program) 246 if is_exe(exe_file): 247 return exe_file 248 return None 249 250class ValueCheck: 251 def __init__(self, name=None, value=None, type=None, summary=None, 252 children=None): 253 """ 254 :param name: The name that the SBValue should have. None if the summary 255 should not be checked. 256 :param summary: The summary that the SBValue should have. None if the 257 summary should not be checked. 258 :param value: The value that the SBValue should have. None if the value 259 should not be checked. 260 :param type: The type that the SBValue result should have. None if the 261 type should not be checked. 262 :param children: A list of ValueChecks that need to match the children 263 of this SBValue. None if children shouldn't be checked. 264 The order of checks is the order of the checks in the 265 list. The number of checks has to match the number of 266 children. 267 """ 268 self.expect_name = name 269 self.expect_value = value 270 self.expect_type = type 271 self.expect_summary = summary 272 self.children = children 273 274 def check_value(self, test_base, val, error_msg=None): 275 """ 276 Checks that the given value matches the currently set properties 277 of this ValueCheck. If a match failed, the given TestBase will 278 be used to emit an error. A custom error message can be specified 279 that will be used to describe failed check for this SBValue (but 280 not errors in the child values). 281 """ 282 283 this_error_msg = error_msg if error_msg else "" 284 this_error_msg += "\nChecking SBValue: " + str(val) 285 286 test_base.assertSuccess(val.GetError()) 287 288 if self.expect_name: 289 test_base.assertEqual(self.expect_name, val.GetName(), 290 this_error_msg) 291 if self.expect_value: 292 test_base.assertEqual(self.expect_value, val.GetValue(), 293 this_error_msg) 294 if self.expect_type: 295 test_base.assertEqual(self.expect_type, val.GetDisplayTypeName(), 296 this_error_msg) 297 if self.expect_summary: 298 test_base.assertEqual(self.expect_summary, val.GetSummary(), 299 this_error_msg) 300 if self.children is not None: 301 self.check_value_children(test_base, val, error_msg) 302 303 def check_value_children(self, test_base, val, error_msg=None): 304 """ 305 Checks that the children of a SBValue match a certain structure and 306 have certain properties. 307 308 :param test_base: The current test's TestBase object. 309 :param val: The SBValue to check. 310 """ 311 312 this_error_msg = error_msg if error_msg else "" 313 this_error_msg += "\nChecking SBValue: " + str(val) 314 315 test_base.assertEqual(len(self.children), val.GetNumChildren(), this_error_msg) 316 317 for i in range(0, val.GetNumChildren()): 318 expected_child = self.children[i] 319 actual_child = val.GetChildAtIndex(i) 320 child_error = "Checking child with index " + str(i) + ":\n" + error_msg 321 expected_child.check_value(test_base, actual_child, child_error) 322 323class recording(SixStringIO): 324 """ 325 A nice little context manager for recording the debugger interactions into 326 our session object. If trace flag is ON, it also emits the interactions 327 into the stderr. 328 """ 329 330 def __init__(self, test, trace): 331 """Create a SixStringIO instance; record the session obj and trace flag.""" 332 SixStringIO.__init__(self) 333 # The test might not have undergone the 'setUp(self)' phase yet, so that 334 # the attribute 'session' might not even exist yet. 335 self.session = getattr(test, "session", None) if test else None 336 self.trace = trace 337 338 def __enter__(self): 339 """ 340 Context management protocol on entry to the body of the with statement. 341 Just return the SixStringIO object. 342 """ 343 return self 344 345 def __exit__(self, type, value, tb): 346 """ 347 Context management protocol on exit from the body of the with statement. 348 If trace is ON, it emits the recordings into stderr. Always add the 349 recordings to our session object. And close the SixStringIO object, too. 350 """ 351 if self.trace: 352 print(self.getvalue(), file=sys.stderr) 353 if self.session: 354 print(self.getvalue(), file=self.session) 355 self.close() 356 357 358@add_metaclass(abc.ABCMeta) 359class _BaseProcess(object): 360 361 @abc.abstractproperty 362 def pid(self): 363 """Returns process PID if has been launched already.""" 364 365 @abc.abstractmethod 366 def launch(self, executable, args, extra_env): 367 """Launches new process with given executable and args.""" 368 369 @abc.abstractmethod 370 def terminate(self): 371 """Terminates previously launched process..""" 372 373 374class _LocalProcess(_BaseProcess): 375 376 def __init__(self, trace_on): 377 self._proc = None 378 self._trace_on = trace_on 379 self._delayafterterminate = 0.1 380 381 @property 382 def pid(self): 383 return self._proc.pid 384 385 def launch(self, executable, args, extra_env): 386 env=None 387 if extra_env: 388 env = dict(os.environ) 389 env.update([kv.split("=", 1) for kv in extra_env]) 390 391 self._proc = Popen( 392 [executable] + args, 393 stdout=open( 394 os.devnull) if not self._trace_on else None, 395 stdin=PIPE, 396 preexec_fn=lldbplatformutil.enable_attach, 397 env=env) 398 399 def terminate(self): 400 if self._proc.poll() is None: 401 # Terminate _proc like it does the pexpect 402 signals_to_try = [ 403 sig for sig in [ 404 'SIGHUP', 405 'SIGCONT', 406 'SIGINT'] if sig in dir(signal)] 407 for sig in signals_to_try: 408 try: 409 self._proc.send_signal(getattr(signal, sig)) 410 time.sleep(self._delayafterterminate) 411 if self._proc.poll() is not None: 412 return 413 except ValueError: 414 pass # Windows says SIGINT is not a valid signal to send 415 self._proc.terminate() 416 time.sleep(self._delayafterterminate) 417 if self._proc.poll() is not None: 418 return 419 self._proc.kill() 420 time.sleep(self._delayafterterminate) 421 422 def poll(self): 423 return self._proc.poll() 424 425 def wait(self, timeout=None): 426 return self._proc.wait(timeout) 427 428 429class _RemoteProcess(_BaseProcess): 430 431 def __init__(self, install_remote): 432 self._pid = None 433 self._install_remote = install_remote 434 435 @property 436 def pid(self): 437 return self._pid 438 439 def launch(self, executable, args, extra_env): 440 if self._install_remote: 441 src_path = executable 442 dst_path = lldbutil.join_remote_paths( 443 lldb.remote_platform.GetWorkingDirectory(), os.path.basename(executable)) 444 445 dst_file_spec = lldb.SBFileSpec(dst_path, False) 446 err = lldb.remote_platform.Install( 447 lldb.SBFileSpec(src_path, True), dst_file_spec) 448 if err.Fail(): 449 raise Exception( 450 "remote_platform.Install('%s', '%s') failed: %s" % 451 (src_path, dst_path, err)) 452 else: 453 dst_path = executable 454 dst_file_spec = lldb.SBFileSpec(executable, False) 455 456 launch_info = lldb.SBLaunchInfo(args) 457 launch_info.SetExecutableFile(dst_file_spec, True) 458 launch_info.SetWorkingDirectory( 459 lldb.remote_platform.GetWorkingDirectory()) 460 461 # Redirect stdout and stderr to /dev/null 462 launch_info.AddSuppressFileAction(1, False, True) 463 launch_info.AddSuppressFileAction(2, False, True) 464 465 if extra_env: 466 launch_info.SetEnvironmentEntries(extra_env, True) 467 468 err = lldb.remote_platform.Launch(launch_info) 469 if err.Fail(): 470 raise Exception( 471 "remote_platform.Launch('%s', '%s') failed: %s" % 472 (dst_path, args, err)) 473 self._pid = launch_info.GetProcessID() 474 475 def terminate(self): 476 lldb.remote_platform.Kill(self._pid) 477 478def getsource_if_available(obj): 479 """ 480 Return the text of the source code for an object if available. Otherwise, 481 a print representation is returned. 482 """ 483 import inspect 484 try: 485 return inspect.getsource(obj) 486 except: 487 return repr(obj) 488 489 490def builder_module(): 491 return get_builder(sys.platform) 492 493 494class Base(unittest2.TestCase): 495 """ 496 Abstract base for performing lldb (see TestBase) or other generic tests (see 497 BenchBase for one example). lldbtest.Base works with the test driver to 498 accomplish things. 499 500 """ 501 502 # The concrete subclass should override this attribute. 503 mydir = None 504 505 # Keep track of the old current working directory. 506 oldcwd = None 507 508 @staticmethod 509 def compute_mydir(test_file): 510 '''Subclasses should call this function to correctly calculate the 511 required "mydir" attribute as follows: 512 513 mydir = TestBase.compute_mydir(__file__) 514 ''' 515 # /abs/path/to/packages/group/subdir/mytest.py -> group/subdir 516 lldb_test_src = configuration.test_src_root 517 if not test_file.startswith(lldb_test_src): 518 raise Exception( 519 "Test file '%s' must reside within lldb_test_src " 520 "(which is '%s')." % (test_file, lldb_test_src)) 521 return os.path.dirname(os.path.relpath(test_file, start=lldb_test_src)) 522 523 def TraceOn(self): 524 """Returns True if we are in trace mode (tracing detailed test execution).""" 525 return traceAlways 526 527 def trace(self, *args,**kwargs): 528 with recording(self, self.TraceOn()) as sbuf: 529 print(*args, file=sbuf, **kwargs) 530 531 @classmethod 532 def setUpClass(cls): 533 """ 534 Python unittest framework class setup fixture. 535 Do current directory manipulation. 536 """ 537 # Fail fast if 'mydir' attribute is not overridden. 538 if not cls.mydir or len(cls.mydir) == 0: 539 raise Exception("Subclasses must override the 'mydir' attribute.") 540 541 # Save old working directory. 542 cls.oldcwd = os.getcwd() 543 544 full_dir = os.path.join(configuration.test_src_root, cls.mydir) 545 if traceAlways: 546 print("Change dir to:", full_dir, file=sys.stderr) 547 os.chdir(full_dir) 548 lldb.SBReproducer.SetWorkingDirectory(full_dir) 549 550 # Set platform context. 551 cls.platformContext = lldbplatformutil.createPlatformContext() 552 553 @classmethod 554 def tearDownClass(cls): 555 """ 556 Python unittest framework class teardown fixture. 557 Do class-wide cleanup. 558 """ 559 560 if doCleanup: 561 # First, let's do the platform-specific cleanup. 562 module = builder_module() 563 module.cleanup() 564 565 # Subclass might have specific cleanup function defined. 566 if getattr(cls, "classCleanup", None): 567 if traceAlways: 568 print( 569 "Call class-specific cleanup function for class:", 570 cls, 571 file=sys.stderr) 572 try: 573 cls.classCleanup() 574 except: 575 exc_type, exc_value, exc_tb = sys.exc_info() 576 traceback.print_exception(exc_type, exc_value, exc_tb) 577 578 # Restore old working directory. 579 if traceAlways: 580 print("Restore dir to:", cls.oldcwd, file=sys.stderr) 581 os.chdir(cls.oldcwd) 582 583 def enableLogChannelsForCurrentTest(self): 584 if len(lldbtest_config.channels) == 0: 585 return 586 587 # if debug channels are specified in lldbtest_config.channels, 588 # create a new set of log files for every test 589 log_basename = self.getLogBasenameForCurrentTest() 590 591 # confirm that the file is writeable 592 host_log_path = "{}-host.log".format(log_basename) 593 open(host_log_path, 'w').close() 594 self.log_files.append(host_log_path) 595 596 log_enable = "log enable -Tpn -f {} ".format(host_log_path) 597 for channel_with_categories in lldbtest_config.channels: 598 channel_then_categories = channel_with_categories.split(' ', 1) 599 channel = channel_then_categories[0] 600 if len(channel_then_categories) > 1: 601 categories = channel_then_categories[1] 602 else: 603 categories = "default" 604 605 if channel == "gdb-remote" and lldb.remote_platform is None: 606 # communicate gdb-remote categories to debugserver 607 os.environ["LLDB_DEBUGSERVER_LOG_FLAGS"] = categories 608 609 self.ci.HandleCommand( 610 log_enable + channel_with_categories, self.res) 611 if not self.res.Succeeded(): 612 raise Exception( 613 'log enable failed (check LLDB_LOG_OPTION env variable)') 614 615 # Communicate log path name to debugserver & lldb-server 616 # For remote debugging, these variables need to be set when starting the platform 617 # instance. 618 if lldb.remote_platform is None: 619 server_log_path = "{}-server.log".format(log_basename) 620 open(server_log_path, 'w').close() 621 self.log_files.append(server_log_path) 622 os.environ["LLDB_DEBUGSERVER_LOG_FILE"] = server_log_path 623 624 # Communicate channels to lldb-server 625 os.environ["LLDB_SERVER_LOG_CHANNELS"] = ":".join( 626 lldbtest_config.channels) 627 628 self.addTearDownHook(self.disableLogChannelsForCurrentTest) 629 630 def disableLogChannelsForCurrentTest(self): 631 # close all log files that we opened 632 for channel_and_categories in lldbtest_config.channels: 633 # channel format - <channel-name> [<category0> [<category1> ...]] 634 channel = channel_and_categories.split(' ', 1)[0] 635 self.ci.HandleCommand("log disable " + channel, self.res) 636 if not self.res.Succeeded(): 637 raise Exception( 638 'log disable failed (check LLDB_LOG_OPTION env variable)') 639 640 # Retrieve the server log (if any) from the remote system. It is assumed the server log 641 # is writing to the "server.log" file in the current test directory. This can be 642 # achieved by setting LLDB_DEBUGSERVER_LOG_FILE="server.log" when starting remote 643 # platform. 644 if lldb.remote_platform: 645 server_log_path = self.getLogBasenameForCurrentTest() + "-server.log" 646 if lldb.remote_platform.Get( 647 lldb.SBFileSpec("server.log"), 648 lldb.SBFileSpec(server_log_path)).Success(): 649 self.log_files.append(server_log_path) 650 651 def setPlatformWorkingDir(self): 652 if not lldb.remote_platform or not configuration.lldb_platform_working_dir: 653 return 654 655 components = self.mydir.split(os.path.sep) + [str(self.test_number), self.getBuildDirBasename()] 656 remote_test_dir = configuration.lldb_platform_working_dir 657 for c in components: 658 remote_test_dir = lldbutil.join_remote_paths(remote_test_dir, c) 659 error = lldb.remote_platform.MakeDirectory( 660 remote_test_dir, 448) # 448 = 0o700 661 if error.Fail(): 662 raise Exception("making remote directory '%s': %s" % ( 663 remote_test_dir, error)) 664 665 lldb.remote_platform.SetWorkingDirectory(remote_test_dir) 666 667 # This function removes all files from the current working directory while leaving 668 # the directories in place. The cleanup is required to reduce the disk space required 669 # by the test suite while leaving the directories untouched is neccessary because 670 # sub-directories might belong to an other test 671 def clean_working_directory(): 672 # TODO: Make it working on Windows when we need it for remote debugging support 673 # TODO: Replace the heuristic to remove the files with a logic what collects the 674 # list of files we have to remove during test runs. 675 shell_cmd = lldb.SBPlatformShellCommand( 676 "rm %s/*" % remote_test_dir) 677 lldb.remote_platform.Run(shell_cmd) 678 self.addTearDownHook(clean_working_directory) 679 680 def getSourceDir(self): 681 """Return the full path to the current test.""" 682 return os.path.join(configuration.test_src_root, self.mydir) 683 684 def getBuildDirBasename(self): 685 return self.__class__.__module__ + "." + self.testMethodName 686 687 def getBuildDir(self): 688 """Return the full path to the current test.""" 689 return os.path.join(configuration.test_build_dir, self.mydir, 690 self.getBuildDirBasename()) 691 692 def makeBuildDir(self): 693 """Create the test-specific working directory, deleting any previous 694 contents.""" 695 bdir = self.getBuildDir() 696 if os.path.isdir(bdir): 697 shutil.rmtree(bdir) 698 lldbutil.mkdir_p(bdir) 699 700 def getBuildArtifact(self, name="a.out"): 701 """Return absolute path to an artifact in the test's build directory.""" 702 return os.path.join(self.getBuildDir(), name) 703 704 def getSourcePath(self, name): 705 """Return absolute path to a file in the test's source directory.""" 706 return os.path.join(self.getSourceDir(), name) 707 708 @classmethod 709 def setUpCommands(cls): 710 commands = [ 711 # First of all, clear all settings to have clean state of global properties. 712 "settings clear -all", 713 714 # Disable Spotlight lookup. The testsuite creates 715 # different binaries with the same UUID, because they only 716 # differ in the debug info, which is not being hashed. 717 "settings set symbols.enable-external-lookup false", 718 719 # Inherit the TCC permissions from the inferior's parent. 720 "settings set target.inherit-tcc true", 721 722 # Kill rather than detach from the inferior if something goes wrong. 723 "settings set target.detach-on-error false", 724 725 # Disable fix-its by default so that incorrect expressions in tests don't 726 # pass just because Clang thinks it has a fix-it. 727 "settings set target.auto-apply-fixits false", 728 729 # Testsuite runs in parallel and the host can have also other load. 730 "settings set plugin.process.gdb-remote.packet-timeout 60", 731 732 'settings set symbols.clang-modules-cache-path "{}"'.format( 733 configuration.lldb_module_cache_dir), 734 "settings set use-color false", 735 ] 736 737 # Set any user-overridden settings. 738 for setting, value in configuration.settings: 739 commands.append('setting set %s %s'%(setting, value)) 740 741 # Make sure that a sanitizer LLDB's environment doesn't get passed on. 742 if cls.platformContext and cls.platformContext.shlib_environment_var in os.environ: 743 commands.append('settings set target.env-vars {}='.format( 744 cls.platformContext.shlib_environment_var)) 745 746 # Set environment variables for the inferior. 747 if lldbtest_config.inferior_env: 748 commands.append('settings set target.env-vars {}'.format( 749 lldbtest_config.inferior_env)) 750 return commands 751 752 def setUp(self): 753 """Fixture for unittest test case setup. 754 755 It works with the test driver to conditionally skip tests and does other 756 initializations.""" 757 #import traceback 758 # traceback.print_stack() 759 760 if "LIBCXX_PATH" in os.environ: 761 self.libcxxPath = os.environ["LIBCXX_PATH"] 762 else: 763 self.libcxxPath = None 764 765 if "LLDBVSCODE_EXEC" in os.environ: 766 self.lldbVSCodeExec = os.environ["LLDBVSCODE_EXEC"] 767 else: 768 self.lldbVSCodeExec = None 769 770 self.lldbOption = " ".join( 771 "-o '" + s + "'" for s in self.setUpCommands()) 772 773 # If we spawn an lldb process for test (via pexpect), do not load the 774 # init file unless told otherwise. 775 if os.environ.get("NO_LLDBINIT") != "NO": 776 self.lldbOption += " --no-lldbinit" 777 778 # Assign the test method name to self.testMethodName. 779 # 780 # For an example of the use of this attribute, look at test/types dir. 781 # There are a bunch of test cases under test/types and we don't want the 782 # module cacheing subsystem to be confused with executable name "a.out" 783 # used for all the test cases. 784 self.testMethodName = self._testMethodName 785 786 # This is for the case of directly spawning 'lldb'/'gdb' and interacting 787 # with it using pexpect. 788 self.child = None 789 self.child_prompt = "(lldb) " 790 # If the child is interacting with the embedded script interpreter, 791 # there are two exits required during tear down, first to quit the 792 # embedded script interpreter and second to quit the lldb command 793 # interpreter. 794 self.child_in_script_interpreter = False 795 796 # These are for customized teardown cleanup. 797 self.dict = None 798 self.doTearDownCleanup = False 799 # And in rare cases where there are multiple teardown cleanups. 800 self.dicts = [] 801 self.doTearDownCleanups = False 802 803 # List of spawned subproces.Popen objects 804 self.subprocesses = [] 805 806 # List of log files produced by the current test. 807 self.log_files = [] 808 809 # Create the build directory. 810 # The logs are stored in the build directory, so we have to create it 811 # before creating the first log file. 812 self.makeBuildDir() 813 814 session_file = self.getLogBasenameForCurrentTest()+".log" 815 self.log_files.append(session_file) 816 817 # Python 3 doesn't support unbuffered I/O in text mode. Open buffered. 818 self.session = encoded_file.open(session_file, "utf-8", mode="w") 819 820 # Optimistically set __errored__, __failed__, __expected__ to False 821 # initially. If the test errored/failed, the session info 822 # (self.session) is then dumped into a session specific file for 823 # diagnosis. 824 self.__cleanup_errored__ = False 825 self.__errored__ = False 826 self.__failed__ = False 827 self.__expected__ = False 828 # We are also interested in unexpected success. 829 self.__unexpected__ = False 830 # And skipped tests. 831 self.__skipped__ = False 832 833 # See addTearDownHook(self, hook) which allows the client to add a hook 834 # function to be run during tearDown() time. 835 self.hooks = [] 836 837 # See HideStdout(self). 838 self.sys_stdout_hidden = False 839 840 if self.platformContext: 841 # set environment variable names for finding shared libraries 842 self.dylibPath = self.platformContext.shlib_environment_var 843 844 # Create the debugger instance. 845 self.dbg = lldb.SBDebugger.Create() 846 # Copy selected platform from a global instance if it exists. 847 if lldb.selected_platform is not None: 848 self.dbg.SetSelectedPlatform(lldb.selected_platform) 849 850 if not self.dbg: 851 raise Exception('Invalid debugger instance') 852 853 # Retrieve the associated command interpreter instance. 854 self.ci = self.dbg.GetCommandInterpreter() 855 if not self.ci: 856 raise Exception('Could not get the command interpreter') 857 858 # And the result object. 859 self.res = lldb.SBCommandReturnObject() 860 861 self.setPlatformWorkingDir() 862 self.enableLogChannelsForCurrentTest() 863 864 self.lib_lldb = None 865 self.framework_dir = None 866 self.darwinWithFramework = False 867 868 if sys.platform.startswith("darwin") and configuration.lldb_framework_path: 869 framework = configuration.lldb_framework_path 870 lib = os.path.join(framework, 'LLDB') 871 if os.path.exists(lib): 872 self.framework_dir = os.path.dirname(framework) 873 self.lib_lldb = lib 874 self.darwinWithFramework = self.platformIsDarwin() 875 876 def setAsync(self, value): 877 """ Sets async mode to True/False and ensures it is reset after the testcase completes.""" 878 old_async = self.dbg.GetAsync() 879 self.dbg.SetAsync(value) 880 self.addTearDownHook(lambda: self.dbg.SetAsync(old_async)) 881 882 def cleanupSubprocesses(self): 883 # Terminate subprocesses in reverse order from how they were created. 884 for p in reversed(self.subprocesses): 885 p.terminate() 886 del p 887 del self.subprocesses[:] 888 889 def spawnSubprocess(self, executable, args=[], extra_env=None, install_remote=True): 890 """ Creates a subprocess.Popen object with the specified executable and arguments, 891 saves it in self.subprocesses, and returns the object. 892 """ 893 proc = _RemoteProcess( 894 install_remote) if lldb.remote_platform else _LocalProcess(self.TraceOn()) 895 proc.launch(executable, args, extra_env=extra_env) 896 self.subprocesses.append(proc) 897 return proc 898 899 def HideStdout(self): 900 """Hide output to stdout from the user. 901 902 During test execution, there might be cases where we don't want to show the 903 standard output to the user. For example, 904 905 self.runCmd(r'''sc print("\n\n\tHello!\n")''') 906 907 tests whether command abbreviation for 'script' works or not. There is no 908 need to show the 'Hello' output to the user as long as the 'script' command 909 succeeds and we are not in TraceOn() mode (see the '-t' option). 910 911 In this case, the test method calls self.HideStdout(self) to redirect the 912 sys.stdout to a null device, and restores the sys.stdout upon teardown. 913 914 Note that you should only call this method at most once during a test case 915 execution. Any subsequent call has no effect at all.""" 916 if self.sys_stdout_hidden: 917 return 918 919 self.sys_stdout_hidden = True 920 old_stdout = sys.stdout 921 sys.stdout = open(os.devnull, 'w') 922 923 def restore_stdout(): 924 sys.stdout = old_stdout 925 self.addTearDownHook(restore_stdout) 926 927 # ======================================================================= 928 # Methods for customized teardown cleanups as well as execution of hooks. 929 # ======================================================================= 930 931 def setTearDownCleanup(self, dictionary=None): 932 """Register a cleanup action at tearDown() time with a dictionary""" 933 self.dict = dictionary 934 self.doTearDownCleanup = True 935 936 def addTearDownCleanup(self, dictionary): 937 """Add a cleanup action at tearDown() time with a dictionary""" 938 self.dicts.append(dictionary) 939 self.doTearDownCleanups = True 940 941 def addTearDownHook(self, hook): 942 """ 943 Add a function to be run during tearDown() time. 944 945 Hooks are executed in a first come first serve manner. 946 """ 947 if six.callable(hook): 948 with recording(self, traceAlways) as sbuf: 949 print( 950 "Adding tearDown hook:", 951 getsource_if_available(hook), 952 file=sbuf) 953 self.hooks.append(hook) 954 955 return self 956 957 def deletePexpectChild(self): 958 # This is for the case of directly spawning 'lldb' and interacting with it 959 # using pexpect. 960 if self.child and self.child.isalive(): 961 import pexpect 962 with recording(self, traceAlways) as sbuf: 963 print("tearing down the child process....", file=sbuf) 964 try: 965 if self.child_in_script_interpreter: 966 self.child.sendline('quit()') 967 self.child.expect_exact(self.child_prompt) 968 self.child.sendline( 969 'settings set interpreter.prompt-on-quit false') 970 self.child.sendline('quit') 971 self.child.expect(pexpect.EOF) 972 except (ValueError, pexpect.ExceptionPexpect): 973 # child is already terminated 974 pass 975 except OSError as exception: 976 import errno 977 if exception.errno != errno.EIO: 978 # unexpected error 979 raise 980 # child is already terminated 981 finally: 982 # Give it one final blow to make sure the child is terminated. 983 self.child.close() 984 985 def tearDown(self): 986 """Fixture for unittest test case teardown.""" 987 self.deletePexpectChild() 988 989 # Check and run any hook functions. 990 for hook in reversed(self.hooks): 991 with recording(self, traceAlways) as sbuf: 992 print( 993 "Executing tearDown hook:", 994 getsource_if_available(hook), 995 file=sbuf) 996 if funcutils.requires_self(hook): 997 hook(self) 998 else: 999 hook() # try the plain call and hope it works 1000 1001 del self.hooks 1002 1003 # Perform registered teardown cleanup. 1004 if doCleanup and self.doTearDownCleanup: 1005 self.cleanup(dictionary=self.dict) 1006 1007 # In rare cases where there are multiple teardown cleanups added. 1008 if doCleanup and self.doTearDownCleanups: 1009 if self.dicts: 1010 for dict in reversed(self.dicts): 1011 self.cleanup(dictionary=dict) 1012 1013 # Remove subprocesses created by the test. 1014 self.cleanupSubprocesses() 1015 1016 # This must be the last statement, otherwise teardown hooks or other 1017 # lines might depend on this still being active. 1018 lldb.SBDebugger.Destroy(self.dbg) 1019 del self.dbg 1020 1021 # All modules should be orphaned now so that they can be cleared from 1022 # the shared module cache. 1023 lldb.SBModule.GarbageCollectAllocatedModules() 1024 1025 # Assert that the global module cache is empty. 1026 self.assertEqual(lldb.SBModule.GetNumberAllocatedModules(), 0) 1027 1028 1029 # ========================================================= 1030 # Various callbacks to allow introspection of test progress 1031 # ========================================================= 1032 1033 def markError(self): 1034 """Callback invoked when an error (unexpected exception) errored.""" 1035 self.__errored__ = True 1036 with recording(self, False) as sbuf: 1037 # False because there's no need to write "ERROR" to the stderr twice. 1038 # Once by the Python unittest framework, and a second time by us. 1039 print("ERROR", file=sbuf) 1040 1041 def markCleanupError(self): 1042 """Callback invoked when an error occurs while a test is cleaning up.""" 1043 self.__cleanup_errored__ = True 1044 with recording(self, False) as sbuf: 1045 # False because there's no need to write "CLEANUP_ERROR" to the stderr twice. 1046 # Once by the Python unittest framework, and a second time by us. 1047 print("CLEANUP_ERROR", file=sbuf) 1048 1049 def markFailure(self): 1050 """Callback invoked when a failure (test assertion failure) occurred.""" 1051 self.__failed__ = True 1052 with recording(self, False) as sbuf: 1053 # False because there's no need to write "FAIL" to the stderr twice. 1054 # Once by the Python unittest framework, and a second time by us. 1055 print("FAIL", file=sbuf) 1056 1057 def markExpectedFailure(self, err, bugnumber): 1058 """Callback invoked when an expected failure/error occurred.""" 1059 self.__expected__ = True 1060 with recording(self, False) as sbuf: 1061 # False because there's no need to write "expected failure" to the 1062 # stderr twice. 1063 # Once by the Python unittest framework, and a second time by us. 1064 if bugnumber is None: 1065 print("expected failure", file=sbuf) 1066 else: 1067 print( 1068 "expected failure (problem id:" + str(bugnumber) + ")", 1069 file=sbuf) 1070 1071 def markSkippedTest(self): 1072 """Callback invoked when a test is skipped.""" 1073 self.__skipped__ = True 1074 with recording(self, False) as sbuf: 1075 # False because there's no need to write "skipped test" to the 1076 # stderr twice. 1077 # Once by the Python unittest framework, and a second time by us. 1078 print("skipped test", file=sbuf) 1079 1080 def markUnexpectedSuccess(self, bugnumber): 1081 """Callback invoked when an unexpected success occurred.""" 1082 self.__unexpected__ = True 1083 with recording(self, False) as sbuf: 1084 # False because there's no need to write "unexpected success" to the 1085 # stderr twice. 1086 # Once by the Python unittest framework, and a second time by us. 1087 if bugnumber is None: 1088 print("unexpected success", file=sbuf) 1089 else: 1090 print( 1091 "unexpected success (problem id:" + str(bugnumber) + ")", 1092 file=sbuf) 1093 1094 def getRerunArgs(self): 1095 return " -f %s.%s" % (self.__class__.__name__, self._testMethodName) 1096 1097 def getLogBasenameForCurrentTest(self, prefix="Incomplete"): 1098 """ 1099 returns a partial path that can be used as the beginning of the name of multiple 1100 log files pertaining to this test 1101 """ 1102 return os.path.join(self.getBuildDir(), prefix) 1103 1104 def dumpSessionInfo(self): 1105 """ 1106 Dump the debugger interactions leading to a test error/failure. This 1107 allows for more convenient postmortem analysis. 1108 1109 See also LLDBTestResult (dotest.py) which is a singlton class derived 1110 from TextTestResult and overwrites addError, addFailure, and 1111 addExpectedFailure methods to allow us to to mark the test instance as 1112 such. 1113 """ 1114 1115 # We are here because self.tearDown() detected that this test instance 1116 # either errored or failed. The lldb.test_result singleton contains 1117 # two lists (errors and failures) which get populated by the unittest 1118 # framework. Look over there for stack trace information. 1119 # 1120 # The lists contain 2-tuples of TestCase instances and strings holding 1121 # formatted tracebacks. 1122 # 1123 # See http://docs.python.org/library/unittest.html#unittest.TestResult. 1124 1125 # output tracebacks into session 1126 pairs = [] 1127 if self.__errored__: 1128 pairs = configuration.test_result.errors 1129 prefix = 'Error' 1130 elif self.__cleanup_errored__: 1131 pairs = configuration.test_result.cleanup_errors 1132 prefix = 'CleanupError' 1133 elif self.__failed__: 1134 pairs = configuration.test_result.failures 1135 prefix = 'Failure' 1136 elif self.__expected__: 1137 pairs = configuration.test_result.expectedFailures 1138 prefix = 'ExpectedFailure' 1139 elif self.__skipped__: 1140 prefix = 'SkippedTest' 1141 elif self.__unexpected__: 1142 prefix = 'UnexpectedSuccess' 1143 else: 1144 prefix = 'Success' 1145 1146 if not self.__unexpected__ and not self.__skipped__: 1147 for test, traceback in pairs: 1148 if test is self: 1149 print(traceback, file=self.session) 1150 1151 import datetime 1152 print( 1153 "Session info generated @", 1154 datetime.datetime.now().ctime(), 1155 file=self.session) 1156 self.session.close() 1157 del self.session 1158 1159 # process the log files 1160 if prefix != 'Success' or lldbtest_config.log_success: 1161 # keep all log files, rename them to include prefix 1162 src_log_basename = self.getLogBasenameForCurrentTest() 1163 dst_log_basename = self.getLogBasenameForCurrentTest(prefix) 1164 for src in self.log_files: 1165 if os.path.isfile(src): 1166 dst = src.replace(src_log_basename, dst_log_basename) 1167 if os.name == "nt" and os.path.isfile(dst): 1168 # On Windows, renaming a -> b will throw an exception if 1169 # b exists. On non-Windows platforms it silently 1170 # replaces the destination. Ultimately this means that 1171 # atomic renames are not guaranteed to be possible on 1172 # Windows, but we need this to work anyway, so just 1173 # remove the destination first if it already exists. 1174 remove_file(dst) 1175 1176 lldbutil.mkdir_p(os.path.dirname(dst)) 1177 os.rename(src, dst) 1178 else: 1179 # success! (and we don't want log files) delete log files 1180 for log_file in self.log_files: 1181 if os.path.isfile(log_file): 1182 remove_file(log_file) 1183 1184 # ==================================================== 1185 # Config. methods supported through a plugin interface 1186 # (enables reading of the current test configuration) 1187 # ==================================================== 1188 1189 def isMIPS(self): 1190 """Returns true if the architecture is MIPS.""" 1191 arch = self.getArchitecture() 1192 if re.match("mips", arch): 1193 return True 1194 return False 1195 1196 def isPPC64le(self): 1197 """Returns true if the architecture is PPC64LE.""" 1198 arch = self.getArchitecture() 1199 if re.match("powerpc64le", arch): 1200 return True 1201 return False 1202 1203 def getCPUInfo(self): 1204 triple = self.dbg.GetSelectedPlatform().GetTriple() 1205 1206 # TODO other platforms, please implement this function 1207 if not re.match(".*-.*-linux", triple): 1208 return "" 1209 1210 # Need to do something different for non-Linux/Android targets 1211 cpuinfo_path = self.getBuildArtifact("cpuinfo") 1212 if configuration.lldb_platform_name: 1213 self.runCmd('platform get-file "/proc/cpuinfo" ' + cpuinfo_path) 1214 else: 1215 cpuinfo_path = "/proc/cpuinfo" 1216 1217 try: 1218 with open(cpuinfo_path, 'r') as f: 1219 cpuinfo = f.read() 1220 except: 1221 return "" 1222 1223 return cpuinfo 1224 1225 def isAArch64(self): 1226 """Returns true if the architecture is AArch64.""" 1227 arch = self.getArchitecture().lower() 1228 return arch in ["aarch64", "arm64", "arm64e"] 1229 1230 def isAArch64SVE(self): 1231 return self.isAArch64() and "sve" in self.getCPUInfo() 1232 1233 def isAArch64MTE(self): 1234 return self.isAArch64() and "mte" in self.getCPUInfo() 1235 1236 def isAArch64PAuth(self): 1237 return self.isAArch64() and "paca" in self.getCPUInfo() 1238 1239 def getArchitecture(self): 1240 """Returns the architecture in effect the test suite is running with.""" 1241 module = builder_module() 1242 arch = module.getArchitecture() 1243 if arch == 'amd64': 1244 arch = 'x86_64' 1245 if arch in ['armv7l', 'armv8l'] : 1246 arch = 'arm' 1247 return arch 1248 1249 def getLldbArchitecture(self): 1250 """Returns the architecture of the lldb binary.""" 1251 if not hasattr(self, 'lldbArchitecture'): 1252 1253 # spawn local process 1254 command = [ 1255 lldbtest_config.lldbExec, 1256 "-o", 1257 "file " + lldbtest_config.lldbExec, 1258 "-o", 1259 "quit" 1260 ] 1261 1262 output = check_output(command) 1263 str = output.decode("utf-8") 1264 1265 for line in str.splitlines(): 1266 m = re.search( 1267 "Current executable set to '.*' \\((.*)\\)\\.", line) 1268 if m: 1269 self.lldbArchitecture = m.group(1) 1270 break 1271 1272 return self.lldbArchitecture 1273 1274 def getCompiler(self): 1275 """Returns the compiler in effect the test suite is running with.""" 1276 module = builder_module() 1277 return module.getCompiler() 1278 1279 def getCompilerBinary(self): 1280 """Returns the compiler binary the test suite is running with.""" 1281 return self.getCompiler().split()[0] 1282 1283 def getCompilerVersion(self): 1284 """ Returns a string that represents the compiler version. 1285 Supports: llvm, clang. 1286 """ 1287 compiler = self.getCompilerBinary() 1288 version_output = check_output([compiler, "--version"], errors="replace") 1289 m = re.search('version ([0-9.]+)', version_output) 1290 if m: 1291 return m.group(1) 1292 return 'unknown' 1293 1294 def getDwarfVersion(self): 1295 """ Returns the dwarf version generated by clang or '0'. """ 1296 if configuration.dwarf_version: 1297 return str(configuration.dwarf_version) 1298 if 'clang' in self.getCompiler(): 1299 try: 1300 triple = builder_module().getTriple(self.getArchitecture()) 1301 target = ['-target', triple] if triple else [] 1302 driver_output = check_output( 1303 [self.getCompiler()] + target + '-g -c -x c - -o - -###'.split(), 1304 stderr=STDOUT) 1305 driver_output = driver_output.decode("utf-8") 1306 for line in driver_output.split(os.linesep): 1307 m = re.search('dwarf-version=([0-9])', line) 1308 if m: 1309 return m.group(1) 1310 except CalledProcessError: 1311 pass 1312 return '0' 1313 1314 def platformIsDarwin(self): 1315 """Returns true if the OS triple for the selected platform is any valid apple OS""" 1316 return lldbplatformutil.platformIsDarwin() 1317 1318 def hasDarwinFramework(self): 1319 return self.darwinWithFramework 1320 1321 def getPlatform(self): 1322 """Returns the target platform the test suite is running on.""" 1323 return lldbplatformutil.getPlatform() 1324 1325 def isIntelCompiler(self): 1326 """ Returns true if using an Intel (ICC) compiler, false otherwise. """ 1327 return any([x in self.getCompiler() for x in ["icc", "icpc", "icl"]]) 1328 1329 def expectedCompilerVersion(self, compiler_version): 1330 """Returns True iff compiler_version[1] matches the current compiler version. 1331 Use compiler_version[0] to specify the operator used to determine if a match has occurred. 1332 Any operator other than the following defaults to an equality test: 1333 '>', '>=', "=>", '<', '<=', '=<', '!=', "!" or 'not' 1334 1335 If the current compiler version cannot be determined, we assume it is close to the top 1336 of trunk, so any less-than or equal-to comparisons will return False, and any 1337 greater-than or not-equal-to comparisons will return True. 1338 """ 1339 if compiler_version is None: 1340 return True 1341 operator = str(compiler_version[0]) 1342 version = compiler_version[1] 1343 1344 if version is None: 1345 return True 1346 1347 test_compiler_version = self.getCompilerVersion() 1348 if test_compiler_version == 'unknown': 1349 # Assume the compiler version is at or near the top of trunk. 1350 return operator in ['>', '>=', '!', '!=', 'not'] 1351 1352 if operator == '>': 1353 return LooseVersion(test_compiler_version) > LooseVersion(version) 1354 if operator == '>=' or operator == '=>': 1355 return LooseVersion(test_compiler_version) >= LooseVersion(version) 1356 if operator == '<': 1357 return LooseVersion(test_compiler_version) < LooseVersion(version) 1358 if operator == '<=' or operator == '=<': 1359 return LooseVersion(test_compiler_version) <= LooseVersion(version) 1360 if operator == '!=' or operator == '!' or operator == 'not': 1361 return str(version) not in str(test_compiler_version) 1362 return str(version) in str(test_compiler_version) 1363 1364 def expectedCompiler(self, compilers): 1365 """Returns True iff any element of compilers is a sub-string of the current compiler.""" 1366 if (compilers is None): 1367 return True 1368 1369 for compiler in compilers: 1370 if compiler in self.getCompiler(): 1371 return True 1372 1373 return False 1374 1375 def expectedArch(self, archs): 1376 """Returns True iff any element of archs is a sub-string of the current architecture.""" 1377 if (archs is None): 1378 return True 1379 1380 for arch in archs: 1381 if arch in self.getArchitecture(): 1382 return True 1383 1384 return False 1385 1386 def getRunOptions(self): 1387 """Command line option for -A and -C to run this test again, called from 1388 self.dumpSessionInfo().""" 1389 arch = self.getArchitecture() 1390 comp = self.getCompiler() 1391 option_str = "" 1392 if arch: 1393 option_str = "-A " + arch 1394 if comp: 1395 option_str += " -C " + comp 1396 return option_str 1397 1398 def getDebugInfo(self): 1399 method = getattr(self, self.testMethodName) 1400 return getattr(method, "debug_info", None) 1401 1402 def build( 1403 self, 1404 debug_info=None, 1405 architecture=None, 1406 compiler=None, 1407 dictionary=None): 1408 """Platform specific way to build binaries.""" 1409 if not architecture and configuration.arch: 1410 architecture = configuration.arch 1411 1412 if debug_info is None: 1413 debug_info = self.getDebugInfo() 1414 1415 dictionary = lldbplatformutil.finalize_build_dictionary(dictionary) 1416 1417 testdir = self.mydir 1418 testname = self.getBuildDirBasename() 1419 1420 module = builder_module() 1421 command = builder_module().getBuildCommand(debug_info, architecture, 1422 compiler, dictionary, testdir, testname) 1423 if command is None: 1424 raise Exception("Don't know how to build binary") 1425 1426 self.runBuildCommand(command) 1427 1428 def runBuildCommand(self, command): 1429 self.trace(seven.join_for_shell(command)) 1430 try: 1431 output = check_output(command, stderr=STDOUT, errors="replace") 1432 except CalledProcessError as cpe: 1433 raise build_exception.BuildError(cpe) 1434 self.trace(output) 1435 1436 1437 # ================================================== 1438 # Build methods supported through a plugin interface 1439 # ================================================== 1440 1441 def getstdlibFlag(self): 1442 """ Returns the proper -stdlib flag, or empty if not required.""" 1443 if self.platformIsDarwin() or self.getPlatform() == "freebsd" or self.getPlatform() == "openbsd": 1444 stdlibflag = "-stdlib=libc++" 1445 else: # this includes NetBSD 1446 stdlibflag = "" 1447 return stdlibflag 1448 1449 def getstdFlag(self): 1450 """ Returns the proper stdflag. """ 1451 if "gcc" in self.getCompiler() and "4.6" in self.getCompilerVersion(): 1452 stdflag = "-std=c++0x" 1453 else: 1454 stdflag = "-std=c++11" 1455 return stdflag 1456 1457 def buildDriver(self, sources, exe_name): 1458 """ Platform-specific way to build a program that links with LLDB (via the liblldb.so 1459 or LLDB.framework). 1460 """ 1461 stdflag = self.getstdFlag() 1462 stdlibflag = self.getstdlibFlag() 1463 1464 lib_dir = configuration.lldb_libs_dir 1465 if self.hasDarwinFramework(): 1466 d = {'CXX_SOURCES': sources, 1467 'EXE': exe_name, 1468 'CFLAGS_EXTRAS': "%s %s" % (stdflag, stdlibflag), 1469 'FRAMEWORK_INCLUDES': "-F%s" % self.framework_dir, 1470 'LD_EXTRAS': "%s -Wl,-rpath,%s" % (self.lib_lldb, self.framework_dir), 1471 } 1472 elif sys.platform.startswith('win'): 1473 d = { 1474 'CXX_SOURCES': sources, 1475 'EXE': exe_name, 1476 'CFLAGS_EXTRAS': "%s %s -I%s" % (stdflag, 1477 stdlibflag, 1478 os.path.join( 1479 os.environ["LLDB_SRC"], 1480 "include")), 1481 'LD_EXTRAS': "-L%s -lliblldb" % lib_dir} 1482 else: 1483 d = { 1484 'CXX_SOURCES': sources, 1485 'EXE': exe_name, 1486 'CFLAGS_EXTRAS': "%s %s -I%s" % (stdflag, 1487 stdlibflag, 1488 os.path.join( 1489 os.environ["LLDB_SRC"], 1490 "include")), 1491 'LD_EXTRAS': "-L%s -llldb -Wl,-rpath,%s" % (lib_dir, lib_dir)} 1492 if self.TraceOn(): 1493 print( 1494 "Building LLDB Driver (%s) from sources %s" % 1495 (exe_name, sources)) 1496 1497 self.build(dictionary=d) 1498 1499 def buildLibrary(self, sources, lib_name): 1500 """Platform specific way to build a default library. """ 1501 1502 stdflag = self.getstdFlag() 1503 1504 lib_dir = configuration.lldb_libs_dir 1505 if self.hasDarwinFramework(): 1506 d = {'DYLIB_CXX_SOURCES': sources, 1507 'DYLIB_NAME': lib_name, 1508 'CFLAGS_EXTRAS': "%s -stdlib=libc++" % stdflag, 1509 'FRAMEWORK_INCLUDES': "-F%s" % self.framework_dir, 1510 'LD_EXTRAS': "%s -Wl,-rpath,%s -dynamiclib" % (self.lib_lldb, self.framework_dir), 1511 } 1512 elif self.getPlatform() == 'windows': 1513 d = { 1514 'DYLIB_CXX_SOURCES': sources, 1515 'DYLIB_NAME': lib_name, 1516 'CFLAGS_EXTRAS': "%s -I%s " % (stdflag, 1517 os.path.join( 1518 os.environ["LLDB_SRC"], 1519 "include")), 1520 'LD_EXTRAS': "-shared -l%s\liblldb.lib" % lib_dir} 1521 else: 1522 d = { 1523 'DYLIB_CXX_SOURCES': sources, 1524 'DYLIB_NAME': lib_name, 1525 'CFLAGS_EXTRAS': "%s -I%s -fPIC" % (stdflag, 1526 os.path.join( 1527 os.environ["LLDB_SRC"], 1528 "include")), 1529 'LD_EXTRAS': "-shared -L%s -llldb -Wl,-rpath,%s" % (lib_dir, lib_dir)} 1530 if self.TraceOn(): 1531 print( 1532 "Building LLDB Library (%s) from sources %s" % 1533 (lib_name, sources)) 1534 1535 self.build(dictionary=d) 1536 1537 def buildProgram(self, sources, exe_name): 1538 """ Platform specific way to build an executable from C/C++ sources. """ 1539 d = {'CXX_SOURCES': sources, 1540 'EXE': exe_name} 1541 self.build(dictionary=d) 1542 1543 def signBinary(self, binary_path): 1544 if sys.platform.startswith("darwin"): 1545 codesign_cmd = "codesign --force --sign \"%s\" %s" % ( 1546 lldbtest_config.codesign_identity, binary_path) 1547 call(codesign_cmd, shell=True) 1548 1549 def findBuiltClang(self): 1550 """Tries to find and use Clang from the build directory as the compiler (instead of the system compiler).""" 1551 paths_to_try = [ 1552 "llvm-build/Release+Asserts/x86_64/bin/clang", 1553 "llvm-build/Debug+Asserts/x86_64/bin/clang", 1554 "llvm-build/Release/x86_64/bin/clang", 1555 "llvm-build/Debug/x86_64/bin/clang", 1556 ] 1557 lldb_root_path = os.path.join( 1558 os.path.dirname(__file__), "..", "..", "..", "..") 1559 for p in paths_to_try: 1560 path = os.path.join(lldb_root_path, p) 1561 if os.path.exists(path): 1562 return path 1563 1564 # Tries to find clang at the same folder as the lldb 1565 lldb_dir = os.path.dirname(lldbtest_config.lldbExec) 1566 path = distutils.spawn.find_executable("clang", lldb_dir) 1567 if path is not None: 1568 return path 1569 1570 return os.environ["CC"] 1571 1572 1573 def yaml2obj(self, yaml_path, obj_path, max_size=None): 1574 """ 1575 Create an object file at the given path from a yaml file. 1576 1577 Throws subprocess.CalledProcessError if the object could not be created. 1578 """ 1579 yaml2obj_bin = configuration.get_yaml2obj_path() 1580 if not yaml2obj_bin: 1581 self.assertTrue(False, "No valid yaml2obj executable specified") 1582 command = [yaml2obj_bin, "-o=%s" % obj_path, yaml_path] 1583 if max_size is not None: 1584 command += ["--max-size=%d" % max_size] 1585 self.runBuildCommand(command) 1586 1587 def cleanup(self, dictionary=None): 1588 """Platform specific way to do cleanup after build.""" 1589 module = builder_module() 1590 if not module.cleanup(dictionary): 1591 raise Exception( 1592 "Don't know how to do cleanup with dictionary: " + 1593 dictionary) 1594 1595 def invoke(self, obj, name, trace=False): 1596 """Use reflection to call a method dynamically with no argument.""" 1597 trace = (True if traceAlways else trace) 1598 1599 method = getattr(obj, name) 1600 import inspect 1601 self.assertTrue(inspect.ismethod(method), 1602 name + "is a method name of object: " + str(obj)) 1603 result = method() 1604 with recording(self, trace) as sbuf: 1605 print(str(method) + ":", result, file=sbuf) 1606 return result 1607 1608 def getLLDBLibraryEnvVal(self): 1609 """ Returns the path that the OS-specific library search environment variable 1610 (self.dylibPath) should be set to in order for a program to find the LLDB 1611 library. If an environment variable named self.dylibPath is already set, 1612 the new path is appended to it and returned. 1613 """ 1614 existing_library_path = os.environ[ 1615 self.dylibPath] if self.dylibPath in os.environ else None 1616 if existing_library_path: 1617 return "%s:%s" % (existing_library_path, configuration.lldb_libs_dir) 1618 if sys.platform.startswith("darwin") and configuration.lldb_framework_path: 1619 return configuration.lldb_framework_path 1620 return configuration.lldb_libs_dir 1621 1622 def getLibcPlusPlusLibs(self): 1623 if self.getPlatform() in ('freebsd', 'linux', 'netbsd', 'openbsd'): 1624 return ['libc++.so.1'] 1625 else: 1626 return ['libc++.1.dylib', 'libc++abi.'] 1627 1628 def run_platform_command(self, cmd): 1629 platform = self.dbg.GetSelectedPlatform() 1630 shell_command = lldb.SBPlatformShellCommand(cmd) 1631 err = platform.Run(shell_command) 1632 return (err, shell_command.GetStatus(), shell_command.GetOutput()) 1633 1634# Metaclass for TestBase to change the list of test metods when a new TestCase is loaded. 1635# We change the test methods to create a new test method for each test for each debug info we are 1636# testing. The name of the new test method will be '<original-name>_<debug-info>' and with adding 1637# the new test method we remove the old method at the same time. This functionality can be 1638# supressed by at test case level setting the class attribute NO_DEBUG_INFO_TESTCASE or at test 1639# level by using the decorator @no_debug_info_test. 1640 1641 1642class LLDBTestCaseFactory(type): 1643 1644 def __new__(cls, name, bases, attrs): 1645 original_testcase = super( 1646 LLDBTestCaseFactory, cls).__new__( 1647 cls, name, bases, attrs) 1648 if original_testcase.NO_DEBUG_INFO_TESTCASE: 1649 return original_testcase 1650 1651 newattrs = {} 1652 for attrname, attrvalue in attrs.items(): 1653 if attrname.startswith("test") and not getattr( 1654 attrvalue, "__no_debug_info_test__", False): 1655 1656 # If any debug info categories were explicitly tagged, assume that list to be 1657 # authoritative. If none were specified, try with all debug 1658 # info formats. 1659 all_dbginfo_categories = set(test_categories.debug_info_categories) 1660 categories = set( 1661 getattr( 1662 attrvalue, 1663 "categories", 1664 [])) & all_dbginfo_categories 1665 if not categories: 1666 categories = all_dbginfo_categories 1667 1668 for cat in categories: 1669 @decorators.add_test_categories([cat]) 1670 @wraps(attrvalue) 1671 def test_method(self, attrvalue=attrvalue): 1672 return attrvalue(self) 1673 1674 method_name = attrname + "_" + cat 1675 test_method.__name__ = method_name 1676 test_method.debug_info = cat 1677 newattrs[method_name] = test_method 1678 1679 else: 1680 newattrs[attrname] = attrvalue 1681 return super( 1682 LLDBTestCaseFactory, 1683 cls).__new__( 1684 cls, 1685 name, 1686 bases, 1687 newattrs) 1688 1689# Setup the metaclass for this class to change the list of the test 1690# methods when a new class is loaded 1691 1692 1693@add_metaclass(LLDBTestCaseFactory) 1694class TestBase(Base): 1695 """ 1696 This abstract base class is meant to be subclassed. It provides default 1697 implementations for setUpClass(), tearDownClass(), setUp(), and tearDown(), 1698 among other things. 1699 1700 Important things for test class writers: 1701 1702 - Overwrite the mydir class attribute, otherwise your test class won't 1703 run. It specifies the relative directory to the top level 'test' so 1704 the test harness can change to the correct working directory before 1705 running your test. 1706 1707 - The setUp method sets up things to facilitate subsequent interactions 1708 with the debugger as part of the test. These include: 1709 - populate the test method name 1710 - create/get a debugger set with synchronous mode (self.dbg) 1711 - get the command interpreter from with the debugger (self.ci) 1712 - create a result object for use with the command interpreter 1713 (self.res) 1714 - plus other stuffs 1715 1716 - The tearDown method tries to perform some necessary cleanup on behalf 1717 of the test to return the debugger to a good state for the next test. 1718 These include: 1719 - execute any tearDown hooks registered by the test method with 1720 TestBase.addTearDownHook(); examples can be found in 1721 settings/TestSettings.py 1722 - kill the inferior process associated with each target, if any, 1723 and, then delete the target from the debugger's target list 1724 - perform build cleanup before running the next test method in the 1725 same test class; examples of registering for this service can be 1726 found in types/TestIntegerTypes.py with the call: 1727 - self.setTearDownCleanup(dictionary=d) 1728 1729 - Similarly setUpClass and tearDownClass perform classwise setup and 1730 teardown fixtures. The tearDownClass method invokes a default build 1731 cleanup for the entire test class; also, subclasses can implement the 1732 classmethod classCleanup(cls) to perform special class cleanup action. 1733 1734 - The instance methods runCmd and expect are used heavily by existing 1735 test cases to send a command to the command interpreter and to perform 1736 string/pattern matching on the output of such command execution. The 1737 expect method also provides a mode to peform string/pattern matching 1738 without running a command. 1739 1740 - The build method is used to build the binaries used during a 1741 particular test scenario. A plugin should be provided for the 1742 sys.platform running the test suite. The Mac OS X implementation is 1743 located in builders/darwin.py. 1744 """ 1745 1746 # Subclasses can set this to true (if they don't depend on debug info) to avoid running the 1747 # test multiple times with various debug info types. 1748 NO_DEBUG_INFO_TESTCASE = False 1749 1750 # Maximum allowed attempts when launching the inferior process. 1751 # Can be overridden by the LLDB_MAX_LAUNCH_COUNT environment variable. 1752 maxLaunchCount = 1 1753 1754 # Time to wait before the next launching attempt in second(s). 1755 # Can be overridden by the LLDB_TIME_WAIT_NEXT_LAUNCH environment variable. 1756 timeWaitNextLaunch = 1.0 1757 1758 def generateSource(self, source): 1759 template = source + '.template' 1760 temp = os.path.join(self.getSourceDir(), template) 1761 with open(temp, 'r') as f: 1762 content = f.read() 1763 1764 public_api_dir = os.path.join( 1765 os.environ["LLDB_SRC"], "include", "lldb", "API") 1766 1767 # Look under the include/lldb/API directory and add #include statements 1768 # for all the SB API headers. 1769 public_headers = os.listdir(public_api_dir) 1770 # For different platforms, the include statement can vary. 1771 if self.hasDarwinFramework(): 1772 include_stmt = "'#include <%s>' % os.path.join('LLDB', header)" 1773 else: 1774 include_stmt = "'#include <%s>' % os.path.join('" + public_api_dir + "', header)" 1775 list = [eval(include_stmt) for header in public_headers if ( 1776 header.startswith("SB") and header.endswith(".h"))] 1777 includes = '\n'.join(list) 1778 new_content = content.replace('%include_SB_APIs%', includes) 1779 new_content = new_content.replace('%SOURCE_DIR%', self.getSourceDir()) 1780 src = os.path.join(self.getBuildDir(), source) 1781 with open(src, 'w') as f: 1782 f.write(new_content) 1783 1784 self.addTearDownHook(lambda: os.remove(src)) 1785 1786 def setUp(self): 1787 # Works with the test driver to conditionally skip tests via 1788 # decorators. 1789 Base.setUp(self) 1790 1791 for s in self.setUpCommands(): 1792 self.runCmd(s) 1793 1794 if "LLDB_MAX_LAUNCH_COUNT" in os.environ: 1795 self.maxLaunchCount = int(os.environ["LLDB_MAX_LAUNCH_COUNT"]) 1796 1797 if "LLDB_TIME_WAIT_NEXT_LAUNCH" in os.environ: 1798 self.timeWaitNextLaunch = float( 1799 os.environ["LLDB_TIME_WAIT_NEXT_LAUNCH"]) 1800 1801 # We want our debugger to be synchronous. 1802 self.dbg.SetAsync(False) 1803 1804 # Retrieve the associated command interpreter instance. 1805 self.ci = self.dbg.GetCommandInterpreter() 1806 if not self.ci: 1807 raise Exception('Could not get the command interpreter') 1808 1809 # And the result object. 1810 self.res = lldb.SBCommandReturnObject() 1811 1812 def registerSharedLibrariesWithTarget(self, target, shlibs): 1813 '''If we are remotely running the test suite, register the shared libraries with the target so they get uploaded, otherwise do nothing 1814 1815 Any modules in the target that have their remote install file specification set will 1816 get uploaded to the remote host. This function registers the local copies of the 1817 shared libraries with the target and sets their remote install locations so they will 1818 be uploaded when the target is run. 1819 ''' 1820 if not shlibs or not self.platformContext: 1821 return None 1822 1823 shlib_environment_var = self.platformContext.shlib_environment_var 1824 shlib_prefix = self.platformContext.shlib_prefix 1825 shlib_extension = '.' + self.platformContext.shlib_extension 1826 1827 dirs = [] 1828 # Add any shared libraries to our target if remote so they get 1829 # uploaded into the working directory on the remote side 1830 for name in shlibs: 1831 # The path can be a full path to a shared library, or a make file name like "Foo" for 1832 # "libFoo.dylib" or "libFoo.so", or "Foo.so" for "Foo.so" or "libFoo.so", or just a 1833 # basename like "libFoo.so". So figure out which one it is and resolve the local copy 1834 # of the shared library accordingly 1835 if os.path.isfile(name): 1836 local_shlib_path = name # name is the full path to the local shared library 1837 else: 1838 # Check relative names 1839 local_shlib_path = os.path.join( 1840 self.getBuildDir(), shlib_prefix + name + shlib_extension) 1841 if not os.path.exists(local_shlib_path): 1842 local_shlib_path = os.path.join( 1843 self.getBuildDir(), name + shlib_extension) 1844 if not os.path.exists(local_shlib_path): 1845 local_shlib_path = os.path.join(self.getBuildDir(), name) 1846 1847 # Make sure we found the local shared library in the above code 1848 self.assertTrue(os.path.exists(local_shlib_path)) 1849 1850 1851 # Add the shared library to our target 1852 shlib_module = target.AddModule(local_shlib_path, None, None, None) 1853 if lldb.remote_platform: 1854 # We must set the remote install location if we want the shared library 1855 # to get uploaded to the remote target 1856 remote_shlib_path = lldbutil.append_to_process_working_directory(self, 1857 os.path.basename(local_shlib_path)) 1858 shlib_module.SetRemoteInstallFileSpec( 1859 lldb.SBFileSpec(remote_shlib_path, False)) 1860 dir_to_add = self.get_process_working_directory() 1861 else: 1862 dir_to_add = os.path.dirname(local_shlib_path) 1863 1864 if dir_to_add not in dirs: 1865 dirs.append(dir_to_add) 1866 1867 env_value = self.platformContext.shlib_path_separator.join(dirs) 1868 return ['%s=%s' % (shlib_environment_var, env_value)] 1869 1870 def registerSanitizerLibrariesWithTarget(self, target): 1871 runtimes = [] 1872 for m in target.module_iter(): 1873 libspec = m.GetFileSpec() 1874 if "clang_rt" in libspec.GetFilename(): 1875 runtimes.append(os.path.join(libspec.GetDirectory(), 1876 libspec.GetFilename())) 1877 return self.registerSharedLibrariesWithTarget(target, runtimes) 1878 1879 # utility methods that tests can use to access the current objects 1880 def target(self): 1881 if not self.dbg: 1882 raise Exception('Invalid debugger instance') 1883 return self.dbg.GetSelectedTarget() 1884 1885 def process(self): 1886 if not self.dbg: 1887 raise Exception('Invalid debugger instance') 1888 return self.dbg.GetSelectedTarget().GetProcess() 1889 1890 def thread(self): 1891 if not self.dbg: 1892 raise Exception('Invalid debugger instance') 1893 return self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread() 1894 1895 def frame(self): 1896 if not self.dbg: 1897 raise Exception('Invalid debugger instance') 1898 return self.dbg.GetSelectedTarget().GetProcess( 1899 ).GetSelectedThread().GetSelectedFrame() 1900 1901 def get_process_working_directory(self): 1902 '''Get the working directory that should be used when launching processes for local or remote processes.''' 1903 if lldb.remote_platform: 1904 # Remote tests set the platform working directory up in 1905 # TestBase.setUp() 1906 return lldb.remote_platform.GetWorkingDirectory() 1907 else: 1908 # local tests change directory into each test subdirectory 1909 return self.getBuildDir() 1910 1911 def tearDown(self): 1912 # Ensure all the references to SB objects have gone away so that we can 1913 # be sure that all test-specific resources have been freed before we 1914 # attempt to delete the targets. 1915 gc.collect() 1916 1917 # Delete the target(s) from the debugger as a general cleanup step. 1918 # This includes terminating the process for each target, if any. 1919 # We'd like to reuse the debugger for our next test without incurring 1920 # the initialization overhead. 1921 targets = [] 1922 for target in self.dbg: 1923 if target: 1924 targets.append(target) 1925 process = target.GetProcess() 1926 if process: 1927 rc = self.invoke(process, "Kill") 1928 assert rc.Success() 1929 for target in targets: 1930 self.dbg.DeleteTarget(target) 1931 1932 # Assert that all targets are deleted. 1933 self.assertEqual(self.dbg.GetNumTargets(), 0) 1934 1935 # Do this last, to make sure it's in reverse order from how we setup. 1936 Base.tearDown(self) 1937 1938 def switch_to_thread_with_stop_reason(self, stop_reason): 1939 """ 1940 Run the 'thread list' command, and select the thread with stop reason as 1941 'stop_reason'. If no such thread exists, no select action is done. 1942 """ 1943 from .lldbutil import stop_reason_to_str 1944 self.runCmd('thread list') 1945 output = self.res.GetOutput() 1946 thread_line_pattern = re.compile( 1947 "^[ *] thread #([0-9]+):.*stop reason = %s" % 1948 stop_reason_to_str(stop_reason)) 1949 for line in output.splitlines(): 1950 matched = thread_line_pattern.match(line) 1951 if matched: 1952 self.runCmd('thread select %s' % matched.group(1)) 1953 1954 def runCmd(self, cmd, msg=None, check=True, trace=False, inHistory=False): 1955 """ 1956 Ask the command interpreter to handle the command and then check its 1957 return status. 1958 """ 1959 # Fail fast if 'cmd' is not meaningful. 1960 if cmd is None: 1961 raise Exception("Bad 'cmd' parameter encountered") 1962 1963 trace = (True if traceAlways else trace) 1964 1965 if cmd.startswith("target create "): 1966 cmd = cmd.replace("target create ", "file ") 1967 1968 running = (cmd.startswith("run") or cmd.startswith("process launch")) 1969 1970 for i in range(self.maxLaunchCount if running else 1): 1971 self.ci.HandleCommand(cmd, self.res, inHistory) 1972 1973 with recording(self, trace) as sbuf: 1974 print("runCmd:", cmd, file=sbuf) 1975 if not check: 1976 print("check of return status not required", file=sbuf) 1977 if self.res.Succeeded(): 1978 print("output:", self.res.GetOutput(), file=sbuf) 1979 else: 1980 print("runCmd failed!", file=sbuf) 1981 print(self.res.GetError(), file=sbuf) 1982 1983 if self.res.Succeeded(): 1984 break 1985 elif running: 1986 # For process launch, wait some time before possible next try. 1987 time.sleep(self.timeWaitNextLaunch) 1988 with recording(self, trace) as sbuf: 1989 print("Command '" + cmd + "' failed!", file=sbuf) 1990 1991 if check: 1992 output = "" 1993 if self.res.GetOutput(): 1994 output += "\nCommand output:\n" + self.res.GetOutput() 1995 if self.res.GetError(): 1996 output += "\nError output:\n" + self.res.GetError() 1997 if msg: 1998 msg += output 1999 if cmd: 2000 cmd += output 2001 self.assertTrue(self.res.Succeeded(), 2002 msg if (msg) else CMD_MSG(cmd)) 2003 2004 def match( 2005 self, 2006 str, 2007 patterns, 2008 msg=None, 2009 trace=False, 2010 error=False, 2011 matching=True, 2012 exe=True): 2013 """run command in str, and match the result against regexp in patterns returning the match object for the first matching pattern 2014 2015 Otherwise, all the arguments have the same meanings as for the expect function""" 2016 2017 trace = (True if traceAlways else trace) 2018 2019 if exe: 2020 # First run the command. If we are expecting error, set check=False. 2021 # Pass the assert message along since it provides more semantic 2022 # info. 2023 self.runCmd( 2024 str, 2025 msg=msg, 2026 trace=( 2027 True if trace else False), 2028 check=not error) 2029 2030 # Then compare the output against expected strings. 2031 output = self.res.GetError() if error else self.res.GetOutput() 2032 2033 # If error is True, the API client expects the command to fail! 2034 if error: 2035 self.assertFalse(self.res.Succeeded(), 2036 "Command '" + str + "' is expected to fail!") 2037 else: 2038 # No execution required, just compare str against the golden input. 2039 output = str 2040 with recording(self, trace) as sbuf: 2041 print("looking at:", output, file=sbuf) 2042 2043 # The heading says either "Expecting" or "Not expecting". 2044 heading = "Expecting" if matching else "Not expecting" 2045 2046 for pattern in patterns: 2047 # Match Objects always have a boolean value of True. 2048 match_object = re.search(pattern, output) 2049 matched = bool(match_object) 2050 with recording(self, trace) as sbuf: 2051 print("%s pattern: %s" % (heading, pattern), file=sbuf) 2052 print("Matched" if matched else "Not matched", file=sbuf) 2053 if matched: 2054 break 2055 2056 self.assertTrue(matched if matching else not matched, 2057 msg if msg else EXP_MSG(str, output, exe)) 2058 2059 return match_object 2060 2061 def check_completion_with_desc(self, str_input, match_desc_pairs, enforce_order=False): 2062 """ 2063 Checks that when the given input is completed at the given list of 2064 completions and descriptions is returned. 2065 :param str_input: The input that should be completed. The completion happens at the end of the string. 2066 :param match_desc_pairs: A list of pairs that indicate what completions have to be in the list of 2067 completions returned by LLDB. The first element of the pair is the completion 2068 string that LLDB should generate and the second element the description. 2069 :param enforce_order: True iff the order in which the completions are returned by LLDB 2070 should match the order of the match_desc_pairs pairs. 2071 """ 2072 interp = self.dbg.GetCommandInterpreter() 2073 match_strings = lldb.SBStringList() 2074 description_strings = lldb.SBStringList() 2075 num_matches = interp.HandleCompletionWithDescriptions(str_input, len(str_input), 0, -1, match_strings, description_strings) 2076 self.assertEqual(len(description_strings), len(match_strings)) 2077 2078 # The index of the last matched description in description_strings or 2079 # -1 if no description has been matched yet. 2080 last_found_index = -1 2081 out_of_order_errors = "" 2082 missing_pairs = [] 2083 for pair in match_desc_pairs: 2084 found_pair = False 2085 for i in range(num_matches + 1): 2086 match_candidate = match_strings.GetStringAtIndex(i) 2087 description_candidate = description_strings.GetStringAtIndex(i) 2088 if match_candidate == pair[0] and description_candidate == pair[1]: 2089 found_pair = True 2090 if enforce_order and last_found_index > i: 2091 new_err = ("Found completion " + pair[0] + " at index " + 2092 str(i) + " in returned completion list but " + 2093 "should have been after completion " + 2094 match_strings.GetStringAtIndex(last_found_index) + 2095 " (index:" + str(last_found_index) + ")\n") 2096 out_of_order_errors += new_err 2097 last_found_index = i 2098 break 2099 if not found_pair: 2100 missing_pairs.append(pair) 2101 2102 error_msg = "" 2103 got_failure = False 2104 if len(missing_pairs): 2105 got_failure = True 2106 error_msg += "Missing pairs:\n" 2107 for pair in missing_pairs: 2108 error_msg += " [" + pair[0] + ":" + pair[1] + "]\n" 2109 if len(out_of_order_errors): 2110 got_failure = True 2111 error_msg += out_of_order_errors 2112 if got_failure: 2113 error_msg += "Got the following " + str(num_matches) + " completions back:\n" 2114 for i in range(num_matches + 1): 2115 match_candidate = match_strings.GetStringAtIndex(i) 2116 description_candidate = description_strings.GetStringAtIndex(i) 2117 error_msg += "[" + match_candidate + ":" + description_candidate + "] index " + str(i) + "\n" 2118 self.assertFalse(got_failure, error_msg) 2119 2120 def complete_exactly(self, str_input, patterns): 2121 self.complete_from_to(str_input, patterns, True) 2122 2123 def complete_from_to(self, str_input, patterns, turn_off_re_match=False): 2124 """Test that the completion mechanism completes str_input to patterns, 2125 where patterns could be a pattern-string or a list of pattern-strings""" 2126 # Patterns should not be None in order to proceed. 2127 self.assertFalse(patterns is None) 2128 # And should be either a string or list of strings. Check for list type 2129 # below, if not, make a list out of the singleton string. If patterns 2130 # is not a string or not a list of strings, there'll be runtime errors 2131 # later on. 2132 if not isinstance(patterns, list): 2133 patterns = [patterns] 2134 2135 interp = self.dbg.GetCommandInterpreter() 2136 match_strings = lldb.SBStringList() 2137 num_matches = interp.HandleCompletion(str_input, len(str_input), 0, -1, match_strings) 2138 common_match = match_strings.GetStringAtIndex(0) 2139 if num_matches == 0: 2140 compare_string = str_input 2141 else: 2142 if common_match != None and len(common_match) > 0: 2143 compare_string = str_input + common_match 2144 else: 2145 compare_string = "" 2146 for idx in range(1, num_matches+1): 2147 compare_string += match_strings.GetStringAtIndex(idx) + "\n" 2148 2149 for p in patterns: 2150 if turn_off_re_match: 2151 self.expect( 2152 compare_string, msg=COMPLETION_MSG( 2153 str_input, p, match_strings), exe=False, substrs=[p]) 2154 else: 2155 self.expect( 2156 compare_string, msg=COMPLETION_MSG( 2157 str_input, p, match_strings), exe=False, patterns=[p]) 2158 2159 def completions_match(self, command, completions): 2160 """Checks that the completions for the given command are equal to the 2161 given list of completions""" 2162 interp = self.dbg.GetCommandInterpreter() 2163 match_strings = lldb.SBStringList() 2164 interp.HandleCompletion(command, len(command), 0, -1, match_strings) 2165 # match_strings is a 1-indexed list, so we have to slice... 2166 self.assertItemsEqual(completions, list(match_strings)[1:], 2167 "List of returned completion is wrong") 2168 2169 def completions_contain(self, command, completions): 2170 """Checks that the completions for the given command contain the given 2171 list of completions.""" 2172 interp = self.dbg.GetCommandInterpreter() 2173 match_strings = lldb.SBStringList() 2174 interp.HandleCompletion(command, len(command), 0, -1, match_strings) 2175 for completion in completions: 2176 # match_strings is a 1-indexed list, so we have to slice... 2177 self.assertIn(completion, list(match_strings)[1:], 2178 "Couldn't find expected completion") 2179 2180 def filecheck( 2181 self, 2182 command, 2183 check_file, 2184 filecheck_options = '', 2185 expect_cmd_failure = False): 2186 # Run the command. 2187 self.runCmd( 2188 command, 2189 check=(not expect_cmd_failure), 2190 msg="FileCheck'ing result of `{0}`".format(command)) 2191 2192 self.assertTrue((not expect_cmd_failure) == self.res.Succeeded()) 2193 2194 # Get the error text if there was an error, and the regular text if not. 2195 output = self.res.GetOutput() if self.res.Succeeded() \ 2196 else self.res.GetError() 2197 2198 # Assemble the absolute path to the check file. As a convenience for 2199 # LLDB inline tests, assume that the check file is a relative path to 2200 # a file within the inline test directory. 2201 if check_file.endswith('.pyc'): 2202 check_file = check_file[:-1] 2203 check_file_abs = os.path.abspath(check_file) 2204 2205 # Run FileCheck. 2206 filecheck_bin = configuration.get_filecheck_path() 2207 if not filecheck_bin: 2208 self.assertTrue(False, "No valid FileCheck executable specified") 2209 filecheck_args = [filecheck_bin, check_file_abs] 2210 if filecheck_options: 2211 filecheck_args.append(filecheck_options) 2212 subproc = Popen(filecheck_args, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines = True) 2213 cmd_stdout, cmd_stderr = subproc.communicate(input=output) 2214 cmd_status = subproc.returncode 2215 2216 filecheck_cmd = " ".join(filecheck_args) 2217 filecheck_trace = """ 2218--- FileCheck trace (code={0}) --- 2219{1} 2220 2221FileCheck input: 2222{2} 2223 2224FileCheck output: 2225{3} 2226{4} 2227""".format(cmd_status, filecheck_cmd, output, cmd_stdout, cmd_stderr) 2228 2229 trace = cmd_status != 0 or traceAlways 2230 with recording(self, trace) as sbuf: 2231 print(filecheck_trace, file=sbuf) 2232 2233 self.assertTrue(cmd_status == 0) 2234 2235 def expect( 2236 self, 2237 str, 2238 msg=None, 2239 patterns=None, 2240 startstr=None, 2241 endstr=None, 2242 substrs=None, 2243 trace=False, 2244 error=False, 2245 ordered=True, 2246 matching=True, 2247 exe=True, 2248 inHistory=False): 2249 """ 2250 Similar to runCmd; with additional expect style output matching ability. 2251 2252 Ask the command interpreter to handle the command and then check its 2253 return status. The 'msg' parameter specifies an informational assert 2254 message. We expect the output from running the command to start with 2255 'startstr', matches the substrings contained in 'substrs', and regexp 2256 matches the patterns contained in 'patterns'. 2257 2258 When matching is true and ordered is true, which are both the default, 2259 the strings in the substrs array have to appear in the command output 2260 in the order in which they appear in the array. 2261 2262 If the keyword argument error is set to True, it signifies that the API 2263 client is expecting the command to fail. In this case, the error stream 2264 from running the command is retrieved and compared against the golden 2265 input, instead. 2266 2267 If the keyword argument matching is set to False, it signifies that the API 2268 client is expecting the output of the command not to match the golden 2269 input. 2270 2271 Finally, the required argument 'str' represents the lldb command to be 2272 sent to the command interpreter. In case the keyword argument 'exe' is 2273 set to False, the 'str' is treated as a string to be matched/not-matched 2274 against the golden input. 2275 """ 2276 # Catch cases where `expect` has been miscalled. Specifically, prevent 2277 # this easy to make mistake: 2278 # self.expect("lldb command", "some substr") 2279 # The `msg` parameter is used only when a failed match occurs. A failed 2280 # match can only occur when one of `patterns`, `startstr`, `endstr`, or 2281 # `substrs` has been given. Thus, if a `msg` is given, it's an error to 2282 # not also provide one of the matcher parameters. 2283 if msg and not (patterns or startstr or endstr or substrs or error): 2284 assert False, "expect() missing a matcher argument" 2285 2286 # Check `patterns` and `substrs` are not accidentally given as strings. 2287 assert not isinstance(patterns, six.string_types), \ 2288 "patterns must be a collection of strings" 2289 assert not isinstance(substrs, six.string_types), \ 2290 "substrs must be a collection of strings" 2291 2292 trace = (True if traceAlways else trace) 2293 2294 if exe: 2295 # First run the command. If we are expecting error, set check=False. 2296 # Pass the assert message along since it provides more semantic 2297 # info. 2298 self.runCmd( 2299 str, 2300 msg=msg, 2301 trace=( 2302 True if trace else False), 2303 check=not error, 2304 inHistory=inHistory) 2305 2306 # Then compare the output against expected strings. 2307 output = self.res.GetError() if error else self.res.GetOutput() 2308 2309 # If error is True, the API client expects the command to fail! 2310 if error: 2311 self.assertFalse(self.res.Succeeded(), 2312 "Command '" + str + "' is expected to fail!") 2313 else: 2314 # No execution required, just compare str against the golden input. 2315 if isinstance(str, lldb.SBCommandReturnObject): 2316 output = str.GetOutput() 2317 else: 2318 output = str 2319 with recording(self, trace) as sbuf: 2320 print("looking at:", output, file=sbuf) 2321 2322 expecting_str = "Expecting" if matching else "Not expecting" 2323 def found_str(matched): 2324 return "was found" if matched else "was not found" 2325 2326 # To be used as assert fail message and/or trace content 2327 log_lines = [ 2328 "{}:".format("Ran command" if exe else "Checking string"), 2329 "\"{}\"".format(str), 2330 # Space out command and output 2331 "", 2332 ] 2333 if exe: 2334 # Newline before output to make large strings more readable 2335 log_lines.append("Got output:\n{}".format(output)) 2336 2337 # Assume that we start matched if we want a match 2338 # Meaning if you have no conditions, matching or 2339 # not matching will always pass 2340 matched = matching 2341 2342 # We will stop checking on first failure 2343 if startstr: 2344 matched = output.startswith(startstr) 2345 log_lines.append("{} start string: \"{}\" ({})".format( 2346 expecting_str, startstr, found_str(matched))) 2347 2348 if endstr and matched == matching: 2349 matched = output.endswith(endstr) 2350 log_lines.append("{} end string: \"{}\" ({})".format( 2351 expecting_str, endstr, found_str(matched))) 2352 2353 if substrs and matched == matching: 2354 start = 0 2355 for substr in substrs: 2356 index = output[start:].find(substr) 2357 start = start + index if ordered and matching else 0 2358 matched = index != -1 2359 log_lines.append("{} sub string: \"{}\" ({})".format( 2360 expecting_str, substr, found_str(matched))) 2361 2362 if matched != matching: 2363 break 2364 2365 if patterns and matched == matching: 2366 for pattern in patterns: 2367 matched = re.search(pattern, output) 2368 2369 pattern_line = "{} regex pattern: \"{}\" ({}".format( 2370 expecting_str, pattern, found_str(matched)) 2371 if matched: 2372 pattern_line += ", matched \"{}\"".format( 2373 matched.group(0)) 2374 pattern_line += ")" 2375 log_lines.append(pattern_line) 2376 2377 # Convert to bool because match objects 2378 # are True-ish but != True itself 2379 matched = bool(matched) 2380 if matched != matching: 2381 break 2382 2383 # If a check failed, add any extra assert message 2384 if msg is not None and matched != matching: 2385 log_lines.append(msg) 2386 2387 log_msg = "\n".join(log_lines) 2388 with recording(self, trace) as sbuf: 2389 print(log_msg, file=sbuf) 2390 if matched != matching: 2391 self.fail(log_msg) 2392 2393 def expect_expr( 2394 self, 2395 expr, 2396 result_summary=None, 2397 result_value=None, 2398 result_type=None, 2399 result_children=None 2400 ): 2401 """ 2402 Evaluates the given expression and verifies the result. 2403 :param expr: The expression as a string. 2404 :param result_summary: The summary that the expression should have. None if the summary should not be checked. 2405 :param result_value: The value that the expression should have. None if the value should not be checked. 2406 :param result_type: The type that the expression result should have. None if the type should not be checked. 2407 :param result_children: The expected children of the expression result 2408 as a list of ValueChecks. None if the children shouldn't be checked. 2409 """ 2410 self.assertTrue(expr.strip() == expr, "Expression contains trailing/leading whitespace: '" + expr + "'") 2411 2412 frame = self.frame() 2413 options = lldb.SBExpressionOptions() 2414 2415 # Disable fix-its that tests don't pass by accident. 2416 options.SetAutoApplyFixIts(False) 2417 2418 # Set the usual default options for normal expressions. 2419 options.SetIgnoreBreakpoints(True) 2420 2421 if self.frame().IsValid(): 2422 options.SetLanguage(frame.GuessLanguage()) 2423 eval_result = self.frame().EvaluateExpression(expr, options) 2424 else: 2425 target = self.target() 2426 # If there is no selected target, run the expression in the dummy 2427 # target. 2428 if not target.IsValid(): 2429 target = self.dbg.GetDummyTarget() 2430 eval_result = target.EvaluateExpression(expr, options) 2431 2432 value_check = ValueCheck(type=result_type, value=result_value, 2433 summary=result_summary, children=result_children) 2434 value_check.check_value(self, eval_result, str(eval_result)) 2435 return eval_result 2436 2437 def expect_var_path( 2438 self, 2439 var_path, 2440 summary=None, 2441 value=None, 2442 type=None, 2443 children=None 2444 ): 2445 """ 2446 Evaluates the given variable path and verifies the result. 2447 See also 'frame variable' and SBFrame.GetValueForVariablePath. 2448 :param var_path: The variable path as a string. 2449 :param summary: The summary that the variable should have. None if the summary should not be checked. 2450 :param value: The value that the variable should have. None if the value should not be checked. 2451 :param type: The type that the variable result should have. None if the type should not be checked. 2452 :param children: The expected children of the variable as a list of ValueChecks. 2453 None if the children shouldn't be checked. 2454 """ 2455 self.assertTrue(var_path.strip() == var_path, 2456 "Expression contains trailing/leading whitespace: '" + var_path + "'") 2457 2458 frame = self.frame() 2459 eval_result = frame.GetValueForVariablePath(var_path) 2460 2461 value_check = ValueCheck(type=type, value=value, 2462 summary=summary, children=children) 2463 value_check.check_value(self, eval_result, str(eval_result)) 2464 return eval_result 2465 2466 """Assert that an lldb.SBError is in the "success" state.""" 2467 def assertSuccess(self, obj, msg=None): 2468 if not obj.Success(): 2469 error = obj.GetCString() 2470 self.fail(self._formatMessage(msg, 2471 "'{}' is not success".format(error))) 2472 2473 def createTestTarget(self, file_path=None, msg=None, 2474 load_dependent_modules=True): 2475 """ 2476 Creates a target from the file found at the given file path. 2477 Asserts that the resulting target is valid. 2478 :param file_path: The file path that should be used to create the target. 2479 The default argument opens the current default test 2480 executable in the current test directory. 2481 :param msg: A custom error message. 2482 """ 2483 if file_path is None: 2484 file_path = self.getBuildArtifact("a.out") 2485 error = lldb.SBError() 2486 triple = "" 2487 platform = "" 2488 target = self.dbg.CreateTarget(file_path, triple, platform, 2489 load_dependent_modules, error) 2490 if error.Fail(): 2491 err = "Couldn't create target for path '{}': {}".format(file_path, 2492 str(error)) 2493 self.fail(self._formatMessage(msg, err)) 2494 2495 self.assertTrue(target.IsValid(), "Got invalid target without error") 2496 return target 2497 2498 # ================================================= 2499 # Misc. helper methods for debugging test execution 2500 # ================================================= 2501 2502 def DebugSBValue(self, val): 2503 """Debug print a SBValue object, if traceAlways is True.""" 2504 from .lldbutil import value_type_to_str 2505 2506 if not traceAlways: 2507 return 2508 2509 err = sys.stderr 2510 err.write(val.GetName() + ":\n") 2511 err.write('\t' + "TypeName -> " + val.GetTypeName() + '\n') 2512 err.write('\t' + "ByteSize -> " + 2513 str(val.GetByteSize()) + '\n') 2514 err.write('\t' + "NumChildren -> " + 2515 str(val.GetNumChildren()) + '\n') 2516 err.write('\t' + "Value -> " + str(val.GetValue()) + '\n') 2517 err.write('\t' + "ValueAsUnsigned -> " + 2518 str(val.GetValueAsUnsigned()) + '\n') 2519 err.write( 2520 '\t' + 2521 "ValueType -> " + 2522 value_type_to_str( 2523 val.GetValueType()) + 2524 '\n') 2525 err.write('\t' + "Summary -> " + str(val.GetSummary()) + '\n') 2526 err.write('\t' + "IsPointerType -> " + 2527 str(val.TypeIsPointerType()) + '\n') 2528 err.write('\t' + "Location -> " + val.GetLocation() + '\n') 2529 2530 def DebugSBType(self, type): 2531 """Debug print a SBType object, if traceAlways is True.""" 2532 if not traceAlways: 2533 return 2534 2535 err = sys.stderr 2536 err.write(type.GetName() + ":\n") 2537 err.write('\t' + "ByteSize -> " + 2538 str(type.GetByteSize()) + '\n') 2539 err.write('\t' + "IsPointerType -> " + 2540 str(type.IsPointerType()) + '\n') 2541 err.write('\t' + "IsReferenceType -> " + 2542 str(type.IsReferenceType()) + '\n') 2543 2544 def DebugPExpect(self, child): 2545 """Debug the spwaned pexpect object.""" 2546 if not traceAlways: 2547 return 2548 2549 print(child) 2550 2551 @classmethod 2552 def RemoveTempFile(cls, file): 2553 if os.path.exists(file): 2554 remove_file(file) 2555 2556# On Windows, the first attempt to delete a recently-touched file can fail 2557# because of a race with antimalware scanners. This function will detect a 2558# failure and retry. 2559 2560 2561def remove_file(file, num_retries=1, sleep_duration=0.5): 2562 for i in range(num_retries + 1): 2563 try: 2564 os.remove(file) 2565 return True 2566 except: 2567 time.sleep(sleep_duration) 2568 continue 2569 return False 2570