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