lldbtest.py revision ff5c6d9c07c02c80b24a59c25d9f5edd518479c4
1"""
2LLDB module which provides the abstract base class of lldb test case.
3
4The concrete subclass can override lldbtest.TesBase 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
34import os, sys, traceback
35import os.path
36import re
37import signal
38from subprocess import *
39import StringIO
40import time
41import types
42import unittest2
43import lldb
44
45# See also dotest.parseOptionsAndInitTestdirs(), where the environment variables
46# LLDB_COMMAND_TRACE and LLDB_DO_CLEANUP are set from '-t' and '-r dir' options.
47
48# By default, traceAlways is False.
49if "LLDB_COMMAND_TRACE" in os.environ and os.environ["LLDB_COMMAND_TRACE"]=="YES":
50    traceAlways = True
51else:
52    traceAlways = False
53
54# By default, doCleanup is True.
55if "LLDB_DO_CLEANUP" in os.environ and os.environ["LLDB_DO_CLEANUP"]=="NO":
56    doCleanup = False
57else:
58    doCleanup = True
59
60
61#
62# Some commonly used assert messages.
63#
64
65COMMAND_FAILED_AS_EXPECTED = "Command has failed as expected"
66
67CURRENT_EXECUTABLE_SET = "Current executable set successfully"
68
69PROCESS_IS_VALID = "Process is valid"
70
71PROCESS_KILLED = "Process is killed successfully"
72
73PROCESS_EXITED = "Process exited successfully"
74
75PROCESS_STOPPED = "Process status should be stopped"
76
77RUN_SUCCEEDED = "Process is launched successfully"
78
79RUN_COMPLETED = "Process exited successfully"
80
81BACKTRACE_DISPLAYED_CORRECTLY = "Backtrace displayed correctly"
82
83BREAKPOINT_CREATED = "Breakpoint created successfully"
84
85BREAKPOINT_STATE_CORRECT = "Breakpoint state is correct"
86
87BREAKPOINT_PENDING_CREATED = "Pending breakpoint created successfully"
88
89BREAKPOINT_HIT_ONCE = "Breakpoint resolved with hit cout = 1"
90
91BREAKPOINT_HIT_TWICE = "Breakpoint resolved with hit cout = 2"
92
93BREAKPOINT_HIT_THRICE = "Breakpoint resolved with hit cout = 3"
94
95MISSING_EXPECTED_REGISTERS = "At least one expected register is unavailable."
96
97OBJECT_PRINTED_CORRECTLY = "Object printed correctly"
98
99SOURCE_DISPLAYED_CORRECTLY = "Source code displayed correctly"
100
101STEP_OUT_SUCCEEDED = "Thread step-out succeeded"
102
103STOPPED_DUE_TO_EXC_BAD_ACCESS = "Process should be stopped due to bad access exception"
104
105STOPPED_DUE_TO_ASSERT = "Process should be stopped due to an assertion"
106
107STOPPED_DUE_TO_BREAKPOINT = "Process should be stopped due to breakpoint"
108
109STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS = "%s, %s" % (
110    STOPPED_DUE_TO_BREAKPOINT, "instead, the actual stop reason is: '%s'")
111
112STOPPED_DUE_TO_BREAKPOINT_CONDITION = "Stopped due to breakpoint condition"
113
114STOPPED_DUE_TO_BREAKPOINT_IGNORE_COUNT = "Stopped due to breakpoint and ignore count"
115
116STOPPED_DUE_TO_SIGNAL = "Process state is stopped due to signal"
117
118STOPPED_DUE_TO_STEP_IN = "Process state is stopped due to step in"
119
120STOPPED_DUE_TO_WATCHPOINT = "Process should be stopped due to watchpoint"
121
122DATA_TYPES_DISPLAYED_CORRECTLY = "Data type(s) displayed correctly"
123
124VALID_BREAKPOINT = "Got a valid breakpoint"
125
126VALID_BREAKPOINT_LOCATION = "Got a valid breakpoint location"
127
128VALID_COMMAND_INTERPRETER = "Got a valid command interpreter"
129
130VALID_FILESPEC = "Got a valid filespec"
131
132VALID_MODULE = "Got a valid module"
133
134VALID_PROCESS = "Got a valid process"
135
136VALID_SYMBOL = "Got a valid symbol"
137
138VALID_TARGET = "Got a valid target"
139
140VALID_TYPE = "Got a valid type"
141
142VALID_VARIABLE = "Got a valid variable"
143
144VARIABLES_DISPLAYED_CORRECTLY = "Variable(s) displayed correctly"
145
146WATCHPOINT_CREATED = "Watchpoint created successfully"
147
148def CMD_MSG(str):
149    '''A generic "Command '%s' returns successfully" message generator.'''
150    return "Command '%s' returns successfully" % str
151
152def COMPLETION_MSG(str_before, str_after):
153    '''A generic message generator for the completion mechanism.'''
154    return "'%s' successfully completes to '%s'" % (str_before, str_after)
155
156def EXP_MSG(str, exe):
157    '''A generic "'%s' returns expected result" message generator if exe.
158    Otherwise, it generates "'%s' matches expected result" message.'''
159    return "'%s' %s expected result" % (str, 'returns' if exe else 'matches')
160
161def SETTING_MSG(setting):
162    '''A generic "Value of setting '%s' is correct" message generator.'''
163    return "Value of setting '%s' is correct" % setting
164
165def EnvArray():
166    """Returns an env variable array from the os.environ map object."""
167    return map(lambda k,v: k+"="+v, os.environ.keys(), os.environ.values())
168
169def line_number(filename, string_to_match):
170    """Helper function to return the line number of the first matched string."""
171    with open(filename, 'r') as f:
172        for i, line in enumerate(f):
173            if line.find(string_to_match) != -1:
174                # Found our match.
175                return i+1
176    raise Exception("Unable to find '%s' within file %s" % (string_to_match, filename))
177
178def pointer_size():
179    """Return the pointer size of the host system."""
180    import ctypes
181    a_pointer = ctypes.c_void_p(0xffff)
182    return 8 * ctypes.sizeof(a_pointer)
183
184def is_exe(fpath):
185    """Returns true if fpath is an executable."""
186    return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
187
188def which(program):
189    """Returns the full path to a program; None otherwise."""
190    fpath, fname = os.path.split(program)
191    if fpath:
192        if is_exe(program):
193            return program
194    else:
195        for path in os.environ["PATH"].split(os.pathsep):
196            exe_file = os.path.join(path, program)
197            if is_exe(exe_file):
198                return exe_file
199    return None
200
201class recording(StringIO.StringIO):
202    """
203    A nice little context manager for recording the debugger interactions into
204    our session object.  If trace flag is ON, it also emits the interactions
205    into the stderr.
206    """
207    def __init__(self, test, trace):
208        """Create a StringIO instance; record the session obj and trace flag."""
209        StringIO.StringIO.__init__(self)
210        # The test might not have undergone the 'setUp(self)' phase yet, so that
211        # the attribute 'session' might not even exist yet.
212        self.session = getattr(test, "session", None) if test else None
213        self.trace = trace
214
215    def __enter__(self):
216        """
217        Context management protocol on entry to the body of the with statement.
218        Just return the StringIO object.
219        """
220        return self
221
222    def __exit__(self, type, value, tb):
223        """
224        Context management protocol on exit from the body of the with statement.
225        If trace is ON, it emits the recordings into stderr.  Always add the
226        recordings to our session object.  And close the StringIO object, too.
227        """
228        if self.trace:
229            print >> sys.stderr, self.getvalue()
230        if self.session:
231            print >> self.session, self.getvalue()
232        self.close()
233
234# From 2.7's subprocess.check_output() convenience function.
235# Return a tuple (stdoutdata, stderrdata).
236def system(*popenargs, **kwargs):
237    r"""Run an os command with arguments and return its output as a byte string.
238
239    If the exit code was non-zero it raises a CalledProcessError.  The
240    CalledProcessError object will have the return code in the returncode
241    attribute and output in the output attribute.
242
243    The arguments are the same as for the Popen constructor.  Example:
244
245    >>> check_output(["ls", "-l", "/dev/null"])
246    'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'
247
248    The stdout argument is not allowed as it is used internally.
249    To capture standard error in the result, use stderr=STDOUT.
250
251    >>> check_output(["/bin/sh", "-c",
252    ...               "ls -l non_existent_file ; exit 0"],
253    ...              stderr=STDOUT)
254    'ls: non_existent_file: No such file or directory\n'
255    """
256
257    # Assign the sender object to variable 'test' and remove it from kwargs.
258    test = kwargs.pop('sender', None)
259
260    if 'stdout' in kwargs:
261        raise ValueError('stdout argument not allowed, it will be overridden.')
262    process = Popen(stdout=PIPE, stderr=PIPE, *popenargs, **kwargs)
263    pid = process.pid
264    output, error = process.communicate()
265    retcode = process.poll()
266
267    with recording(test, traceAlways) as sbuf:
268        if isinstance(popenargs, types.StringTypes):
269            args = [popenargs]
270        else:
271            args = list(popenargs)
272        print >> sbuf
273        print >> sbuf, "os command:", args
274        print >> sbuf, "with pid:", pid
275        print >> sbuf, "stdout:", output
276        print >> sbuf, "stderr:", error
277        print >> sbuf, "retcode:", retcode
278        print >> sbuf
279
280    if retcode:
281        cmd = kwargs.get("args")
282        if cmd is None:
283            cmd = popenargs[0]
284        raise CalledProcessError(retcode, cmd)
285    return (output, error)
286
287def getsource_if_available(obj):
288    """
289    Return the text of the source code for an object if available.  Otherwise,
290    a print representation is returned.
291    """
292    import inspect
293    try:
294        return inspect.getsource(obj)
295    except:
296        return repr(obj)
297
298def builder_module():
299    return __import__("builder_" + sys.platform)
300
301#
302# Decorators for categorizing test cases.
303#
304
305from functools import wraps
306def python_api_test(func):
307    """Decorate the item as a Python API only test."""
308    if isinstance(func, type) and issubclass(func, unittest2.TestCase):
309        raise Exception("@python_api_test can only be used to decorate a test method")
310    @wraps(func)
311    def wrapper(self, *args, **kwargs):
312        try:
313            if lldb.dont_do_python_api_test:
314                self.skipTest("python api tests")
315        except AttributeError:
316            pass
317        return func(self, *args, **kwargs)
318
319    # Mark this function as such to separate them from lldb command line tests.
320    wrapper.__python_api_test__ = True
321    return wrapper
322
323def benchmarks_test(func):
324    """Decorate the item as a benchmarks test."""
325    if isinstance(func, type) and issubclass(func, unittest2.TestCase):
326        raise Exception("@benchmarks_test can only be used to decorate a test method")
327    @wraps(func)
328    def wrapper(self, *args, **kwargs):
329        try:
330            if not lldb.just_do_benchmarks_test:
331                self.skipTest("benchmarks tests")
332        except AttributeError:
333            pass
334        return func(self, *args, **kwargs)
335
336    # Mark this function as such to separate them from the regular tests.
337    wrapper.__benchmarks_test__ = True
338    return wrapper
339
340def dsym_test(func):
341    """Decorate the item as a dsym test."""
342    if isinstance(func, type) and issubclass(func, unittest2.TestCase):
343        raise Exception("@dsym_test can only be used to decorate a test method")
344    @wraps(func)
345    def wrapper(self, *args, **kwargs):
346        try:
347            if lldb.dont_do_dsym_test:
348                self.skipTest("dsym tests")
349        except AttributeError:
350            pass
351        return func(self, *args, **kwargs)
352
353    # Mark this function as such to separate them from the regular tests.
354    wrapper.__dsym_test__ = True
355    return wrapper
356
357def dwarf_test(func):
358    """Decorate the item as a dwarf test."""
359    if isinstance(func, type) and issubclass(func, unittest2.TestCase):
360        raise Exception("@dwarf_test can only be used to decorate a test method")
361    @wraps(func)
362    def wrapper(self, *args, **kwargs):
363        try:
364            if lldb.dont_do_dwarf_test:
365                self.skipTest("dwarf tests")
366        except AttributeError:
367            pass
368        return func(self, *args, **kwargs)
369
370    # Mark this function as such to separate them from the regular tests.
371    wrapper.__dwarf_test__ = True
372    return wrapper
373
374def expectedFailureGcc(bugnumber=None, compiler_version=["=", None]):
375     if callable(bugnumber):
376        @wraps(bugnumber)
377        def expectedFailureGcc_easy_wrapper(*args, **kwargs):
378            from unittest2 import case
379            self = args[0]
380            test_compiler = self.getCompiler()
381            try:
382                bugnumber(*args, **kwargs)
383            except Exception:
384                if "gcc" in test_compiler and self.expectedCompilerVersion(compiler_version):
385                    raise case._ExpectedFailure(sys.exc_info(),None)
386                else:
387                    raise
388            if "gcc" in test_compiler:
389                raise case._UnexpectedSuccess(sys.exc_info(),None)
390        return expectedFailureGcc_easy_wrapper
391     else:
392        def expectedFailureGcc_impl(func):
393              @wraps(func)
394              def wrapper(*args, **kwargs):
395                from unittest2 import case
396                self = args[0]
397                test_compiler = self.getCompiler()
398                try:
399                    func(*args, **kwargs)
400                except Exception:
401                    if "gcc" in test_compiler and self.expectedCompilerVersion(compiler_version):
402                        raise case._ExpectedFailure(sys.exc_info(),bugnumber)
403                    else:
404                        raise
405                if "gcc" in test_compiler:
406                    raise case._UnexpectedSuccess(sys.exc_info(),bugnumber)
407              return wrapper
408        return expectedFailureGcc_impl
409
410def expectedFailureClang(bugnumber=None):
411     if callable(bugnumber):
412        @wraps(bugnumber)
413        def expectedFailureClang_easy_wrapper(*args, **kwargs):
414            from unittest2 import case
415            self = args[0]
416            test_compiler = self.getCompiler()
417            try:
418                bugnumber(*args, **kwargs)
419            except Exception:
420                if "clang" in test_compiler:
421                    raise case._ExpectedFailure(sys.exc_info(),None)
422                else:
423                    raise
424            if "clang" in test_compiler:
425                raise case._UnexpectedSuccess(sys.exc_info(),None)
426        return expectedFailureClang_easy_wrapper
427     else:
428        def expectedFailureClang_impl(func):
429              @wraps(func)
430              def wrapper(*args, **kwargs):
431                from unittest2 import case
432                self = args[0]
433                test_compiler = self.getCompiler()
434                try:
435                    func(*args, **kwargs)
436                except Exception:
437                    if "clang" in test_compiler:
438                        raise case._ExpectedFailure(sys.exc_info(),bugnumber)
439                    else:
440                        raise
441                if "clang" in test_compiler:
442                    raise case._UnexpectedSuccess(sys.exc_info(),bugnumber)
443              return wrapper
444        return expectedFailureClang_impl
445
446def expectedFailureIcc(bugnumber=None):
447     if callable(bugnumber):
448        @wraps(bugnumber)
449        def expectedFailureIcc_easy_wrapper(*args, **kwargs):
450            from unittest2 import case
451            self = args[0]
452            test_compiler = self.getCompiler()
453            try:
454                bugnumber(*args, **kwargs)
455            except Exception:
456                if "icc" in test_compiler:
457                    raise case._ExpectedFailure(sys.exc_info(),None)
458                else:
459                    raise
460            if "icc" in test_compiler:
461                raise case._UnexpectedSuccess(sys.exc_info(),None)
462        return expectedFailureIcc_easy_wrapper
463     else:
464        def expectedFailureIcc_impl(func):
465              @wraps(func)
466              def wrapper(*args, **kwargs):
467                from unittest2 import case
468                self = args[0]
469                test_compiler = self.getCompiler()
470                try:
471                    func(*args, **kwargs)
472                except Exception:
473                    if "icc" in test_compiler:
474                        raise case._ExpectedFailure(sys.exc_info(),bugnumber)
475                    else:
476                        raise
477                if "icc" in test_compiler:
478                    raise case._UnexpectedSuccess(sys.exc_info(),bugnumber)
479              return wrapper
480        return expectedFailureIcc_impl
481
482
483def expectedFailurei386(bugnumber=None):
484     if callable(bugnumber):
485        @wraps(bugnumber)
486        def expectedFailurei386_easy_wrapper(*args, **kwargs):
487            from unittest2 import case
488            self = args[0]
489            arch = self.getArchitecture()
490            try:
491                bugnumber(*args, **kwargs)
492            except Exception:
493                if "i386" in arch:
494                    raise case._ExpectedFailure(sys.exc_info(),None)
495                else:
496                    raise
497            if "i386" in arch:
498                raise case._UnexpectedSuccess(sys.exc_info(),None)
499        return expectedFailurei386_easy_wrapper
500     else:
501        def expectedFailurei386_impl(func):
502              @wraps(func)
503              def wrapper(*args, **kwargs):
504                from unittest2 import case
505                self = args[0]
506                arch = self.getArchitecture()
507                try:
508                    func(*args, **kwargs)
509                except Exception:
510                    if "i386" in arch:
511                        raise case._ExpectedFailure(sys.exc_info(),bugnumber)
512                    else:
513                        raise
514                if "i386" in arch:
515                    raise case._UnexpectedSuccess(sys.exc_info(),bugnumber)
516              return wrapper
517        return expectedFailurei386_impl
518
519def expectedFailureLinux(bugnumber=None, compilers=None):
520     if callable(bugnumber):
521        @wraps(bugnumber)
522        def expectedFailureLinux_easy_wrapper(*args, **kwargs):
523            from unittest2 import case
524            self = args[0]
525            platform = sys.platform
526            try:
527                bugnumber(*args, **kwargs)
528            except Exception:
529                if "linux" in platform and self.expectedCompiler(compilers):
530                    raise case._ExpectedFailure(sys.exc_info(),None)
531                else:
532                    raise
533            if "linux" in platform and self.expectedCompiler(compilers):
534                raise case._UnexpectedSuccess(sys.exc_info(),None)
535        return expectedFailureLinux_easy_wrapper
536     else:
537        def expectedFailureLinux_impl(func):
538              @wraps(func)
539              def wrapper(*args, **kwargs):
540                from unittest2 import case
541                self = args[0]
542                platform = sys.platform
543                try:
544                    func(*args, **kwargs)
545                except Exception:
546                    if "linux" in platform and self.expectedCompiler(compilers):
547                        raise case._ExpectedFailure(sys.exc_info(),bugnumber)
548                    else:
549                        raise
550                if "linux" in platform and self.expectedCompiler(compilers):
551                    raise case._UnexpectedSuccess(sys.exc_info(),bugnumber)
552              return wrapper
553        return expectedFailureLinux_impl
554
555def expectedFailureDarwin(bugnumber=None):
556     if callable(bugnumber):
557        @wraps(bugnumber)
558        def expectedFailureDarwin_easy_wrapper(*args, **kwargs):
559            from unittest2 import case
560            self = args[0]
561            platform = sys.platform
562            try:
563                bugnumber(*args, **kwargs)
564            except Exception:
565                if "darwin" in platform:
566                    raise case._ExpectedFailure(sys.exc_info(),None)
567                else:
568                    raise
569            if "darwin" in platform:
570                raise case._UnexpectedSuccess(sys.exc_info(),None)
571        return expectedFailureDarwin_easy_wrapper
572     else:
573        def expectedFailureDarwin_impl(func):
574              @wraps(func)
575              def wrapper(*args, **kwargs):
576                from unittest2 import case
577                self = args[0]
578                platform = sys.platform
579                try:
580                    func(*args, **kwargs)
581                except Exception:
582                    if "darwin" in platform:
583                        raise case._ExpectedFailure(sys.exc_info(),bugnumber)
584                    else:
585                        raise
586                if "darwin" in platform:
587                    raise case._UnexpectedSuccess(sys.exc_info(),bugnumber)
588              return wrapper
589        return expectedFailureDarwin_impl
590
591def skipIfFreeBSD(func):
592    """Decorate the item to skip tests that should be skipped on FreeBSD."""
593    if isinstance(func, type) and issubclass(func, unittest2.TestCase):
594        raise Exception("@skipIfFreeBSD can only be used to decorate a test method")
595    @wraps(func)
596    def wrapper(*args, **kwargs):
597        from unittest2 import case
598        self = args[0]
599        platform = sys.platform
600        if "freebsd" in platform:
601            self.skipTest("skip on FreeBSD")
602        else:
603            func(*args, **kwargs)
604    return wrapper
605
606def skipIfLinux(func):
607    """Decorate the item to skip tests that should be skipped on Linux."""
608    if isinstance(func, type) and issubclass(func, unittest2.TestCase):
609        raise Exception("@skipIfLinux can only be used to decorate a test method")
610    @wraps(func)
611    def wrapper(*args, **kwargs):
612        from unittest2 import case
613        self = args[0]
614        platform = sys.platform
615        if "linux" in platform:
616            self.skipTest("skip on linux")
617        else:
618            func(*args, **kwargs)
619    return wrapper
620
621def skipIfDarwin(func):
622    """Decorate the item to skip tests that should be skipped on Darwin."""
623    if isinstance(func, type) and issubclass(func, unittest2.TestCase):
624        raise Exception("@skipIfLinux can only be used to decorate a test method")
625    @wraps(func)
626    def wrapper(*args, **kwargs):
627        from unittest2 import case
628        self = args[0]
629        platform = sys.platform
630        if "darwin" in platform:
631            self.skipTest("skip on darwin")
632        else:
633            func(*args, **kwargs)
634    return wrapper
635
636
637def skipIfLinuxClang(func):
638    """Decorate the item to skip tests that should be skipped if building on
639       Linux with clang.
640    """
641    if isinstance(func, type) and issubclass(func, unittest2.TestCase):
642        raise Exception("@skipIfLinuxClang can only be used to decorate a test method")
643    @wraps(func)
644    def wrapper(*args, **kwargs):
645        from unittest2 import case
646        self = args[0]
647        compiler = self.getCompiler()
648        platform = sys.platform
649        if "clang" in compiler and "linux" in platform:
650            self.skipTest("skipping because Clang is used on Linux")
651        else:
652            func(*args, **kwargs)
653    return wrapper
654
655def skipIfGcc(func):
656    """Decorate the item to skip tests that should be skipped if building with gcc ."""
657    if isinstance(func, type) and issubclass(func, unittest2.TestCase):
658        raise Exception("@skipIfGcc can only be used to decorate a test method")
659    @wraps(func)
660    def wrapper(*args, **kwargs):
661        from unittest2 import case
662        self = args[0]
663        compiler = self.getCompiler()
664        if "gcc" in compiler:
665            self.skipTest("skipping because gcc is the test compiler")
666        else:
667            func(*args, **kwargs)
668    return wrapper
669
670def skipIfIcc(func):
671    """Decorate the item to skip tests that should be skipped if building with icc ."""
672    if isinstance(func, type) and issubclass(func, unittest2.TestCase):
673        raise Exception("@skipIfIcc can only be used to decorate a test method")
674    @wraps(func)
675    def wrapper(*args, **kwargs):
676        from unittest2 import case
677        self = args[0]
678        compiler = self.getCompiler()
679        if "icc" in compiler:
680            self.skipTest("skipping because icc is the test compiler")
681        else:
682            func(*args, **kwargs)
683    return wrapper
684
685def skipIfi386(func):
686    """Decorate the item to skip tests that should be skipped if building 32-bit."""
687    if isinstance(func, type) and issubclass(func, unittest2.TestCase):
688        raise Exception("@skipIfi386 can only be used to decorate a test method")
689    @wraps(func)
690    def wrapper(*args, **kwargs):
691        from unittest2 import case
692        self = args[0]
693        if "i386" == self.getArchitecture():
694            self.skipTest("skipping because i386 is not a supported architecture")
695        else:
696            func(*args, **kwargs)
697    return wrapper
698
699
700class Base(unittest2.TestCase):
701    """
702    Abstract base for performing lldb (see TestBase) or other generic tests (see
703    BenchBase for one example).  lldbtest.Base works with the test driver to
704    accomplish things.
705
706    """
707
708    # The concrete subclass should override this attribute.
709    mydir = None
710
711    # Keep track of the old current working directory.
712    oldcwd = None
713
714    def TraceOn(self):
715        """Returns True if we are in trace mode (tracing detailed test execution)."""
716        return traceAlways
717
718    @classmethod
719    def setUpClass(cls):
720        """
721        Python unittest framework class setup fixture.
722        Do current directory manipulation.
723        """
724
725        # Fail fast if 'mydir' attribute is not overridden.
726        if not cls.mydir or len(cls.mydir) == 0:
727            raise Exception("Subclasses must override the 'mydir' attribute.")
728
729        # Save old working directory.
730        cls.oldcwd = os.getcwd()
731
732        # Change current working directory if ${LLDB_TEST} is defined.
733        # See also dotest.py which sets up ${LLDB_TEST}.
734        if ("LLDB_TEST" in os.environ):
735            if traceAlways:
736                print >> sys.stderr, "Change dir to:", os.path.join(os.environ["LLDB_TEST"], cls.mydir)
737            os.chdir(os.path.join(os.environ["LLDB_TEST"], cls.mydir))
738
739    @classmethod
740    def tearDownClass(cls):
741        """
742        Python unittest framework class teardown fixture.
743        Do class-wide cleanup.
744        """
745
746        if doCleanup and not lldb.skip_build_and_cleanup:
747            # First, let's do the platform-specific cleanup.
748            module = builder_module()
749            if not module.cleanup():
750                raise Exception("Don't know how to do cleanup")
751
752            # Subclass might have specific cleanup function defined.
753            if getattr(cls, "classCleanup", None):
754                if traceAlways:
755                    print >> sys.stderr, "Call class-specific cleanup function for class:", cls
756                try:
757                    cls.classCleanup()
758                except:
759                    exc_type, exc_value, exc_tb = sys.exc_info()
760                    traceback.print_exception(exc_type, exc_value, exc_tb)
761
762        # Restore old working directory.
763        if traceAlways:
764            print >> sys.stderr, "Restore dir to:", cls.oldcwd
765        os.chdir(cls.oldcwd)
766
767    @classmethod
768    def skipLongRunningTest(cls):
769        """
770        By default, we skip long running test case.
771        This can be overridden by passing '-l' to the test driver (dotest.py).
772        """
773        if "LLDB_SKIP_LONG_RUNNING_TEST" in os.environ and "NO" == os.environ["LLDB_SKIP_LONG_RUNNING_TEST"]:
774            return False
775        else:
776            return True
777
778    def setUp(self):
779        """Fixture for unittest test case setup.
780
781        It works with the test driver to conditionally skip tests and does other
782        initializations."""
783        #import traceback
784        #traceback.print_stack()
785
786        if "LLDB_EXEC" in os.environ:
787            self.lldbExec = os.environ["LLDB_EXEC"]
788        else:
789            self.lldbExec = None
790        if "LLDB_HERE" in os.environ:
791            self.lldbHere = os.environ["LLDB_HERE"]
792        else:
793            self.lldbHere = None
794        # If we spawn an lldb process for test (via pexpect), do not load the
795        # init file unless told otherwise.
796        if "NO_LLDBINIT" in os.environ and "NO" == os.environ["NO_LLDBINIT"]:
797            self.lldbOption = ""
798        else:
799            self.lldbOption = "--no-lldbinit"
800
801        # Assign the test method name to self.testMethodName.
802        #
803        # For an example of the use of this attribute, look at test/types dir.
804        # There are a bunch of test cases under test/types and we don't want the
805        # module cacheing subsystem to be confused with executable name "a.out"
806        # used for all the test cases.
807        self.testMethodName = self._testMethodName
808
809        # Python API only test is decorated with @python_api_test,
810        # which also sets the "__python_api_test__" attribute of the
811        # function object to True.
812        try:
813            if lldb.just_do_python_api_test:
814                testMethod = getattr(self, self._testMethodName)
815                if getattr(testMethod, "__python_api_test__", False):
816                    pass
817                else:
818                    self.skipTest("non python api test")
819        except AttributeError:
820            pass
821
822        # Benchmarks test is decorated with @benchmarks_test,
823        # which also sets the "__benchmarks_test__" attribute of the
824        # function object to True.
825        try:
826            if lldb.just_do_benchmarks_test:
827                testMethod = getattr(self, self._testMethodName)
828                if getattr(testMethod, "__benchmarks_test__", False):
829                    pass
830                else:
831                    self.skipTest("non benchmarks test")
832        except AttributeError:
833            pass
834
835        # This is for the case of directly spawning 'lldb'/'gdb' and interacting
836        # with it using pexpect.
837        self.child = None
838        self.child_prompt = "(lldb) "
839        # If the child is interacting with the embedded script interpreter,
840        # there are two exits required during tear down, first to quit the
841        # embedded script interpreter and second to quit the lldb command
842        # interpreter.
843        self.child_in_script_interpreter = False
844
845        # These are for customized teardown cleanup.
846        self.dict = None
847        self.doTearDownCleanup = False
848        # And in rare cases where there are multiple teardown cleanups.
849        self.dicts = []
850        self.doTearDownCleanups = False
851
852        # List of spawned subproces.Popen objects
853        self.subprocesses = []
854
855        # List of forked process PIDs
856        self.forkedProcessPids = []
857
858        # Create a string buffer to record the session info, to be dumped into a
859        # test case specific file if test failure is encountered.
860        self.session = StringIO.StringIO()
861
862        # Optimistically set __errored__, __failed__, __expected__ to False
863        # initially.  If the test errored/failed, the session info
864        # (self.session) is then dumped into a session specific file for
865        # diagnosis.
866        self.__errored__    = False
867        self.__failed__     = False
868        self.__expected__   = False
869        # We are also interested in unexpected success.
870        self.__unexpected__ = False
871        # And skipped tests.
872        self.__skipped__ = False
873
874        # See addTearDownHook(self, hook) which allows the client to add a hook
875        # function to be run during tearDown() time.
876        self.hooks = []
877
878        # See HideStdout(self).
879        self.sys_stdout_hidden = False
880
881        # set environment variable names for finding shared libraries
882        if sys.platform.startswith("darwin"):
883            self.dylibPath = 'DYLD_LIBRARY_PATH'
884        elif sys.platform.startswith("linux") or sys.platform.startswith("freebsd"):
885            self.dylibPath = 'LD_LIBRARY_PATH'
886
887    def runHooks(self, child=None, child_prompt=None, use_cmd_api=False):
888        """Perform the run hooks to bring lldb debugger to the desired state.
889
890        By default, expect a pexpect spawned child and child prompt to be
891        supplied (use_cmd_api=False).  If use_cmd_api is true, ignore the child
892        and child prompt and use self.runCmd() to run the hooks one by one.
893
894        Note that child is a process spawned by pexpect.spawn().  If not, your
895        test case is mostly likely going to fail.
896
897        See also dotest.py where lldb.runHooks are processed/populated.
898        """
899        if not lldb.runHooks:
900            self.skipTest("No runhooks specified for lldb, skip the test")
901        if use_cmd_api:
902            for hook in lldb.runhooks:
903                self.runCmd(hook)
904        else:
905            if not child or not child_prompt:
906                self.fail("Both child and child_prompt need to be defined.")
907            for hook in lldb.runHooks:
908                child.sendline(hook)
909                child.expect_exact(child_prompt)
910
911    def setAsync(self, value):
912        """ Sets async mode to True/False and ensures it is reset after the testcase completes."""
913        old_async = self.dbg.GetAsync()
914        self.dbg.SetAsync(value)
915        self.addTearDownHook(lambda: self.dbg.SetAsync(old_async))
916
917    def cleanupSubprocesses(self):
918        # Ensure any subprocesses are cleaned up
919        for p in self.subprocesses:
920            if p.poll() == None:
921                p.terminate()
922            del p
923        del self.subprocesses[:]
924        # Ensure any forked processes are cleaned up
925        for pid in self.forkedProcessPids:
926            if os.path.exists("/proc/" + str(pid)):
927                os.kill(pid, signal.SIGTERM)
928
929    def spawnSubprocess(self, executable, args=[]):
930        """ Creates a subprocess.Popen object with the specified executable and arguments,
931            saves it in self.subprocesses, and returns the object.
932            NOTE: if using this function, ensure you also call:
933
934              self.addTearDownHook(self.cleanupSubprocesses)
935
936            otherwise the test suite will leak processes.
937        """
938
939        # Don't display the stdout if not in TraceOn() mode.
940        proc = Popen([executable] + args,
941                     stdout = open(os.devnull) if not self.TraceOn() else None,
942                     stdin = PIPE)
943        self.subprocesses.append(proc)
944        return proc
945
946    def forkSubprocess(self, executable, args=[]):
947        """ Fork a subprocess with its own group ID.
948            NOTE: if using this function, ensure you also call:
949
950              self.addTearDownHook(self.cleanupSubprocesses)
951
952            otherwise the test suite will leak processes.
953        """
954        child_pid = os.fork()
955        if child_pid == 0:
956            # If more I/O support is required, this can be beefed up.
957            fd = os.open(os.devnull, os.O_RDWR)
958            os.dup2(fd, 1)
959            os.dup2(fd, 2)
960            # This call causes the child to have its of group ID
961            os.setpgid(0,0)
962            os.execvp(executable, [executable] + args)
963        # Give the child time to get through the execvp() call
964        time.sleep(0.1)
965        self.forkedProcessPids.append(child_pid)
966        return child_pid
967
968    def HideStdout(self):
969        """Hide output to stdout from the user.
970
971        During test execution, there might be cases where we don't want to show the
972        standard output to the user.  For example,
973
974            self.runCmd(r'''sc print "\n\n\tHello!\n"''')
975
976        tests whether command abbreviation for 'script' works or not.  There is no
977        need to show the 'Hello' output to the user as long as the 'script' command
978        succeeds and we are not in TraceOn() mode (see the '-t' option).
979
980        In this case, the test method calls self.HideStdout(self) to redirect the
981        sys.stdout to a null device, and restores the sys.stdout upon teardown.
982
983        Note that you should only call this method at most once during a test case
984        execution.  Any subsequent call has no effect at all."""
985        if self.sys_stdout_hidden:
986            return
987
988        self.sys_stdout_hidden = True
989        old_stdout = sys.stdout
990        sys.stdout = open(os.devnull, 'w')
991        def restore_stdout():
992            sys.stdout = old_stdout
993        self.addTearDownHook(restore_stdout)
994
995    # =======================================================================
996    # Methods for customized teardown cleanups as well as execution of hooks.
997    # =======================================================================
998
999    def setTearDownCleanup(self, dictionary=None):
1000        """Register a cleanup action at tearDown() time with a dictinary"""
1001        self.dict = dictionary
1002        self.doTearDownCleanup = True
1003
1004    def addTearDownCleanup(self, dictionary):
1005        """Add a cleanup action at tearDown() time with a dictinary"""
1006        self.dicts.append(dictionary)
1007        self.doTearDownCleanups = True
1008
1009    def addTearDownHook(self, hook):
1010        """
1011        Add a function to be run during tearDown() time.
1012
1013        Hooks are executed in a first come first serve manner.
1014        """
1015        if callable(hook):
1016            with recording(self, traceAlways) as sbuf:
1017                print >> sbuf, "Adding tearDown hook:", getsource_if_available(hook)
1018            self.hooks.append(hook)
1019
1020    def tearDown(self):
1021        """Fixture for unittest test case teardown."""
1022        #import traceback
1023        #traceback.print_stack()
1024
1025        # This is for the case of directly spawning 'lldb' and interacting with it
1026        # using pexpect.
1027        import pexpect
1028        if self.child and self.child.isalive():
1029            with recording(self, traceAlways) as sbuf:
1030                print >> sbuf, "tearing down the child process...."
1031            try:
1032                if self.child_in_script_interpreter:
1033                    self.child.sendline('quit()')
1034                    self.child.expect_exact(self.child_prompt)
1035                self.child.sendline('settings set interpreter.prompt-on-quit false')
1036                self.child.sendline('quit')
1037                self.child.expect(pexpect.EOF)
1038            except ValueError, ExceptionPexpect:
1039                # child is already terminated
1040                pass
1041
1042            # Give it one final blow to make sure the child is terminated.
1043            self.child.close()
1044
1045        # Check and run any hook functions.
1046        for hook in reversed(self.hooks):
1047            with recording(self, traceAlways) as sbuf:
1048                print >> sbuf, "Executing tearDown hook:", getsource_if_available(hook)
1049            hook()
1050
1051        del self.hooks
1052
1053        # Perform registered teardown cleanup.
1054        if doCleanup and self.doTearDownCleanup:
1055            self.cleanup(dictionary=self.dict)
1056
1057        # In rare cases where there are multiple teardown cleanups added.
1058        if doCleanup and self.doTearDownCleanups:
1059            if self.dicts:
1060                for dict in reversed(self.dicts):
1061                    self.cleanup(dictionary=dict)
1062
1063        # Decide whether to dump the session info.
1064        self.dumpSessionInfo()
1065
1066    # =========================================================
1067    # Various callbacks to allow introspection of test progress
1068    # =========================================================
1069
1070    def markError(self):
1071        """Callback invoked when an error (unexpected exception) errored."""
1072        self.__errored__ = True
1073        with recording(self, False) as sbuf:
1074            # False because there's no need to write "ERROR" to the stderr twice.
1075            # Once by the Python unittest framework, and a second time by us.
1076            print >> sbuf, "ERROR"
1077
1078    def markFailure(self):
1079        """Callback invoked when a failure (test assertion failure) occurred."""
1080        self.__failed__ = True
1081        with recording(self, False) as sbuf:
1082            # False because there's no need to write "FAIL" to the stderr twice.
1083            # Once by the Python unittest framework, and a second time by us.
1084            print >> sbuf, "FAIL"
1085
1086    def markExpectedFailure(self,err,bugnumber):
1087        """Callback invoked when an expected failure/error occurred."""
1088        self.__expected__ = True
1089        with recording(self, False) as sbuf:
1090            # False because there's no need to write "expected failure" to the
1091            # stderr twice.
1092            # Once by the Python unittest framework, and a second time by us.
1093            if bugnumber == None:
1094                print >> sbuf, "expected failure"
1095            else:
1096                print >> sbuf, "expected failure (problem id:" + str(bugnumber) + ")"
1097
1098    def markSkippedTest(self):
1099        """Callback invoked when a test is skipped."""
1100        self.__skipped__ = True
1101        with recording(self, False) as sbuf:
1102            # False because there's no need to write "skipped test" to the
1103            # stderr twice.
1104            # Once by the Python unittest framework, and a second time by us.
1105            print >> sbuf, "skipped test"
1106
1107    def markUnexpectedSuccess(self, bugnumber):
1108        """Callback invoked when an unexpected success occurred."""
1109        self.__unexpected__ = True
1110        with recording(self, False) as sbuf:
1111            # False because there's no need to write "unexpected success" to the
1112            # stderr twice.
1113            # Once by the Python unittest framework, and a second time by us.
1114            if bugnumber == None:
1115                print >> sbuf, "unexpected success"
1116            else:
1117                print >> sbuf, "unexpected success (problem id:" + str(bugnumber) + ")"
1118
1119    def dumpSessionInfo(self):
1120        """
1121        Dump the debugger interactions leading to a test error/failure.  This
1122        allows for more convenient postmortem analysis.
1123
1124        See also LLDBTestResult (dotest.py) which is a singlton class derived
1125        from TextTestResult and overwrites addError, addFailure, and
1126        addExpectedFailure methods to allow us to to mark the test instance as
1127        such.
1128        """
1129
1130        # We are here because self.tearDown() detected that this test instance
1131        # either errored or failed.  The lldb.test_result singleton contains
1132        # two lists (erros and failures) which get populated by the unittest
1133        # framework.  Look over there for stack trace information.
1134        #
1135        # The lists contain 2-tuples of TestCase instances and strings holding
1136        # formatted tracebacks.
1137        #
1138        # See http://docs.python.org/library/unittest.html#unittest.TestResult.
1139        if self.__errored__:
1140            pairs = lldb.test_result.errors
1141            prefix = 'Error'
1142        elif self.__failed__:
1143            pairs = lldb.test_result.failures
1144            prefix = 'Failure'
1145        elif self.__expected__:
1146            pairs = lldb.test_result.expectedFailures
1147            prefix = 'ExpectedFailure'
1148        elif self.__skipped__:
1149            prefix = 'SkippedTest'
1150        elif self.__unexpected__:
1151            prefix = "UnexpectedSuccess"
1152        else:
1153            # Simply return, there's no session info to dump!
1154            return
1155
1156        if not self.__unexpected__ and not self.__skipped__:
1157            for test, traceback in pairs:
1158                if test is self:
1159                    print >> self.session, traceback
1160
1161        testMethod = getattr(self, self._testMethodName)
1162        if getattr(testMethod, "__benchmarks_test__", False):
1163            benchmarks = True
1164        else:
1165            benchmarks = False
1166
1167        # This records the compiler version used for the test.
1168        system([self.getCompiler(), "-v"], sender=self)
1169
1170        dname = os.path.join(os.environ["LLDB_TEST"],
1171                             os.environ["LLDB_SESSION_DIRNAME"])
1172        if not os.path.isdir(dname):
1173            os.mkdir(dname)
1174        fname = os.path.join(dname, "%s-%s-%s-%s.log" % (prefix, self.getArchitecture(), "_".join(self.getCompiler().split('/')), self.id()))
1175        with open(fname, "w") as f:
1176            import datetime
1177            print >> f, "Session info generated @", datetime.datetime.now().ctime()
1178            print >> f, self.session.getvalue()
1179            print >> f, "To rerun this test, issue the following command from the 'test' directory:\n"
1180            print >> f, "./dotest.py %s -v %s -f %s.%s" % (self.getRunOptions(),
1181                                                           ('+b' if benchmarks else '-t'),
1182                                                           self.__class__.__name__,
1183                                                           self._testMethodName)
1184
1185    # ====================================================
1186    # Config. methods supported through a plugin interface
1187    # (enables reading of the current test configuration)
1188    # ====================================================
1189
1190    def getArchitecture(self):
1191        """Returns the architecture in effect the test suite is running with."""
1192        module = builder_module()
1193        return module.getArchitecture()
1194
1195    def getCompiler(self):
1196        """Returns the compiler in effect the test suite is running with."""
1197        module = builder_module()
1198        return module.getCompiler()
1199
1200    def getCompilerVersion(self):
1201        """ Returns a string that represents the compiler version.
1202            Supports: llvm, clang.
1203        """
1204        from lldbutil import which
1205        version = 'unknown'
1206
1207        compiler = self.getCompiler()
1208        version_output = system([which(compiler), "-v"])[1]
1209        for line in version_output.split(os.linesep):
1210            m = re.search('version ([0-9\.]+)', line)
1211            if m:
1212                version = m.group(1)
1213        return version
1214
1215    def expectedCompilerVersion(self, compiler_version):
1216        """Returns True iff compiler_version[1] matches the current compiler version.
1217           Use compiler_version[0] to specify the operator used to determine if a match has occurred.
1218           Any operator other than the following defaults to an equality test:
1219             '>', '>=', "=>", '<', '<=', '=<', '!=', "!" or 'not'
1220        """
1221        if (compiler_version == None):
1222            return True
1223        operator = str(compiler_version[0])
1224        version = compiler_version[1]
1225
1226        if (version == None):
1227            return True
1228        if (operator == '>'):
1229            return self.getCompilerVersion() > version
1230        if (operator == '>=' or operator == '=>'):
1231            return self.getCompilerVersion() >= version
1232        if (operator == '<'):
1233            return self.getCompilerVersion() < version
1234        if (operator == '<=' or operator == '=<'):
1235            return self.getCompilerVersion() <= version
1236        if (operator == '!=' or operator == '!' or operator == 'not'):
1237            return str(version) not in str(self.getCompilerVersion())
1238        return str(version) in str(self.getCompilerVersion())
1239
1240    def expectedCompiler(self, compilers):
1241        """Returns True iff any element of compilers is a sub-string of the current compiler."""
1242        if (compilers == None):
1243            return True
1244
1245        for compiler in compilers:
1246            if compiler in self.getCompiler():
1247                return True
1248
1249        return False
1250
1251    def getRunOptions(self):
1252        """Command line option for -A and -C to run this test again, called from
1253        self.dumpSessionInfo()."""
1254        arch = self.getArchitecture()
1255        comp = self.getCompiler()
1256        if arch:
1257            option_str = "-A " + arch
1258        else:
1259            option_str = ""
1260        if comp:
1261            option_str += " -C " + comp
1262        return option_str
1263
1264    # ==================================================
1265    # Build methods supported through a plugin interface
1266    # ==================================================
1267
1268    def buildDriver(self, sources, exe_name):
1269        """ Platform-specific way to build a program that links with LLDB (via the liblldb.so
1270            or LLDB.framework).
1271        """
1272        if "gcc" in self.getCompiler() and "4.6" in self.getCompilerVersion():
1273          stdflag = "-std=c++0x"
1274        else:
1275          stdflag = "-std=c++11"
1276
1277        if sys.platform.startswith("darwin"):
1278            dsym = os.path.join(self.lib_dir, 'LLDB.framework', 'LLDB')
1279            d = {'CXX_SOURCES' : sources,
1280                 'EXE' : exe_name,
1281                 'CFLAGS_EXTRAS' : "%s -stdlib=libc++" % stdflag,
1282                 'FRAMEWORK_INCLUDES' : "-F%s" % self.lib_dir,
1283                 'LD_EXTRAS' : "%s -rpath %s" % (dsym, self.lib_dir),
1284                }
1285        elif sys.platform.startswith("linux") or os.environ.get('LLDB_BUILD_TYPE') == 'Makefile':
1286            d = {'CXX_SOURCES' : sources,
1287                 'EXE' : exe_name,
1288                 'CFLAGS_EXTRAS' : "%s -I%s" % (stdflag, os.path.join(os.environ["LLDB_SRC"], "include")),
1289                 'LD_EXTRAS' : "-L%s -llldb" % self.lib_dir}
1290        if self.TraceOn():
1291            print "Building LLDB Driver (%s) from sources %s" % (exe_name, sources)
1292
1293        self.buildDefault(dictionary=d)
1294
1295    def buildProgram(self, sources, exe_name):
1296        """ Platform specific way to build an executable from C/C++ sources. """
1297        d = {'CXX_SOURCES' : sources,
1298             'EXE' : exe_name}
1299        self.buildDefault(dictionary=d)
1300
1301    def buildDefault(self, architecture=None, compiler=None, dictionary=None, clean=True):
1302        """Platform specific way to build the default binaries."""
1303        if lldb.skip_build_and_cleanup:
1304            return
1305        module = builder_module()
1306        if not module.buildDefault(self, architecture, compiler, dictionary, clean):
1307            raise Exception("Don't know how to build default binary")
1308
1309    def buildDsym(self, architecture=None, compiler=None, dictionary=None, clean=True):
1310        """Platform specific way to build binaries with dsym info."""
1311        if lldb.skip_build_and_cleanup:
1312            return
1313        module = builder_module()
1314        if not module.buildDsym(self, architecture, compiler, dictionary, clean):
1315            raise Exception("Don't know how to build binary with dsym")
1316
1317    def buildDwarf(self, architecture=None, compiler=None, dictionary=None, clean=True):
1318        """Platform specific way to build binaries with dwarf maps."""
1319        if lldb.skip_build_and_cleanup:
1320            return
1321        module = builder_module()
1322        if not module.buildDwarf(self, architecture, compiler, dictionary, clean):
1323            raise Exception("Don't know how to build binary with dwarf")
1324
1325    def getBuildFlags(self, use_cpp11=True, use_pthreads=True):
1326        """ Returns a dictionary (which can be provided to build* functions above) which
1327            contains OS-specific build flags.
1328        """
1329        cflags = ""
1330        if use_cpp11:
1331            cflags += "-std="
1332            if "gcc" in self.getCompiler() and "4.6" in self.getCompilerVersion():
1333                cflags += "c++0x"
1334            else:
1335                cflags += "c++11"
1336        if sys.platform.startswith("darwin"):
1337            cflags += " -stdlib=libc++"
1338        elif "clang" in self.getCompiler():
1339            cflags += " -stdlib=libstdc++"
1340
1341        if use_pthreads:
1342            ldflags = "-lpthread"
1343
1344        return {'CFLAGS_EXTRAS' : cflags,
1345                'LD_EXTRAS' : ldflags,
1346               }
1347
1348    def cleanup(self, dictionary=None):
1349        """Platform specific way to do cleanup after build."""
1350        if lldb.skip_build_and_cleanup:
1351            return
1352        module = builder_module()
1353        if not module.cleanup(self, dictionary):
1354            raise Exception("Don't know how to do cleanup with dictionary: "+dictionary)
1355
1356    def getLLDBLibraryEnvVal(self):
1357        """ Returns the path that the OS-specific library search environment variable
1358            (self.dylibPath) should be set to in order for a program to find the LLDB
1359            library. If an environment variable named self.dylibPath is already set,
1360            the new path is appended to it and returned.
1361        """
1362        existing_library_path = os.environ[self.dylibPath] if self.dylibPath in os.environ else None
1363        if existing_library_path:
1364            return "%s:%s" % (existing_library_path, self.lib_dir)
1365        elif sys.platform.startswith("darwin"):
1366            return os.path.join(self.lib_dir, 'LLDB.framework')
1367        else:
1368            return self.lib_dir
1369
1370class TestBase(Base):
1371    """
1372    This abstract base class is meant to be subclassed.  It provides default
1373    implementations for setUpClass(), tearDownClass(), setUp(), and tearDown(),
1374    among other things.
1375
1376    Important things for test class writers:
1377
1378        - Overwrite the mydir class attribute, otherwise your test class won't
1379          run.  It specifies the relative directory to the top level 'test' so
1380          the test harness can change to the correct working directory before
1381          running your test.
1382
1383        - The setUp method sets up things to facilitate subsequent interactions
1384          with the debugger as part of the test.  These include:
1385              - populate the test method name
1386              - create/get a debugger set with synchronous mode (self.dbg)
1387              - get the command interpreter from with the debugger (self.ci)
1388              - create a result object for use with the command interpreter
1389                (self.res)
1390              - plus other stuffs
1391
1392        - The tearDown method tries to perform some necessary cleanup on behalf
1393          of the test to return the debugger to a good state for the next test.
1394          These include:
1395              - execute any tearDown hooks registered by the test method with
1396                TestBase.addTearDownHook(); examples can be found in
1397                settings/TestSettings.py
1398              - kill the inferior process associated with each target, if any,
1399                and, then delete the target from the debugger's target list
1400              - perform build cleanup before running the next test method in the
1401                same test class; examples of registering for this service can be
1402                found in types/TestIntegerTypes.py with the call:
1403                    - self.setTearDownCleanup(dictionary=d)
1404
1405        - Similarly setUpClass and tearDownClass perform classwise setup and
1406          teardown fixtures.  The tearDownClass method invokes a default build
1407          cleanup for the entire test class;  also, subclasses can implement the
1408          classmethod classCleanup(cls) to perform special class cleanup action.
1409
1410        - The instance methods runCmd and expect are used heavily by existing
1411          test cases to send a command to the command interpreter and to perform
1412          string/pattern matching on the output of such command execution.  The
1413          expect method also provides a mode to peform string/pattern matching
1414          without running a command.
1415
1416        - The build methods buildDefault, buildDsym, and buildDwarf are used to
1417          build the binaries used during a particular test scenario.  A plugin
1418          should be provided for the sys.platform running the test suite.  The
1419          Mac OS X implementation is located in plugins/darwin.py.
1420    """
1421
1422    # Maximum allowed attempts when launching the inferior process.
1423    # Can be overridden by the LLDB_MAX_LAUNCH_COUNT environment variable.
1424    maxLaunchCount = 3;
1425
1426    # Time to wait before the next launching attempt in second(s).
1427    # Can be overridden by the LLDB_TIME_WAIT_NEXT_LAUNCH environment variable.
1428    timeWaitNextLaunch = 1.0;
1429
1430    def doDelay(self):
1431        """See option -w of dotest.py."""
1432        if ("LLDB_WAIT_BETWEEN_TEST_CASES" in os.environ and
1433            os.environ["LLDB_WAIT_BETWEEN_TEST_CASES"] == 'YES'):
1434            waitTime = 1.0
1435            if "LLDB_TIME_WAIT_BETWEEN_TEST_CASES" in os.environ:
1436                waitTime = float(os.environ["LLDB_TIME_WAIT_BETWEEN_TEST_CASES"])
1437            time.sleep(waitTime)
1438
1439    # Returns the list of categories to which this test case belongs
1440    # by default, look for a ".categories" file, and read its contents
1441    # if no such file exists, traverse the hierarchy - we guarantee
1442    # a .categories to exist at the top level directory so we do not end up
1443    # looping endlessly - subclasses are free to define their own categories
1444    # in whatever way makes sense to them
1445    def getCategories(self):
1446        import inspect
1447        import os.path
1448        folder = inspect.getfile(self.__class__)
1449        folder = os.path.dirname(folder)
1450        while folder != '/':
1451                categories_file_name = os.path.join(folder,".categories")
1452                if os.path.exists(categories_file_name):
1453                        categories_file = open(categories_file_name,'r')
1454                        categories = categories_file.readline()
1455                        categories_file.close()
1456                        categories = str.replace(categories,'\n','')
1457                        categories = str.replace(categories,'\r','')
1458                        return categories.split(',')
1459                else:
1460                        folder = os.path.dirname(folder)
1461                        continue
1462
1463    def setUp(self):
1464        #import traceback
1465        #traceback.print_stack()
1466
1467        # Works with the test driver to conditionally skip tests via decorators.
1468        Base.setUp(self)
1469
1470        try:
1471            if lldb.blacklist:
1472                className = self.__class__.__name__
1473                classAndMethodName = "%s.%s" % (className, self._testMethodName)
1474                if className in lldb.blacklist:
1475                    self.skipTest(lldb.blacklist.get(className))
1476                elif classAndMethodName in lldb.blacklist:
1477                    self.skipTest(lldb.blacklist.get(classAndMethodName))
1478        except AttributeError:
1479            pass
1480
1481        # Insert some delay between successive test cases if specified.
1482        self.doDelay()
1483
1484        if "LLDB_MAX_LAUNCH_COUNT" in os.environ:
1485            self.maxLaunchCount = int(os.environ["LLDB_MAX_LAUNCH_COUNT"])
1486
1487        if "LLDB_TIME_WAIT_NEXT_LAUNCH" in os.environ:
1488            self.timeWaitNextLaunch = float(os.environ["LLDB_TIME_WAIT_NEXT_LAUNCH"])
1489
1490        # Create the debugger instance if necessary.
1491        try:
1492            self.dbg = lldb.DBG
1493        except AttributeError:
1494            self.dbg = lldb.SBDebugger.Create()
1495
1496        if not self.dbg:
1497            raise Exception('Invalid debugger instance')
1498
1499        # We want our debugger to be synchronous.
1500        self.dbg.SetAsync(False)
1501
1502        # Retrieve the associated command interpreter instance.
1503        self.ci = self.dbg.GetCommandInterpreter()
1504        if not self.ci:
1505            raise Exception('Could not get the command interpreter')
1506
1507        # And the result object.
1508        self.res = lldb.SBCommandReturnObject()
1509
1510        # Run global pre-flight code, if defined via the config file.
1511        if lldb.pre_flight:
1512            lldb.pre_flight(self)
1513
1514    # utility methods that tests can use to access the current objects
1515    def target(self):
1516        if not self.dbg:
1517            raise Exception('Invalid debugger instance')
1518        return self.dbg.GetSelectedTarget()
1519
1520    def process(self):
1521        if not self.dbg:
1522            raise Exception('Invalid debugger instance')
1523        return self.dbg.GetSelectedTarget().GetProcess()
1524
1525    def thread(self):
1526        if not self.dbg:
1527            raise Exception('Invalid debugger instance')
1528        return self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread()
1529
1530    def frame(self):
1531        if not self.dbg:
1532            raise Exception('Invalid debugger instance')
1533        return self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame()
1534
1535    def tearDown(self):
1536        #import traceback
1537        #traceback.print_stack()
1538
1539        Base.tearDown(self)
1540
1541        # Delete the target(s) from the debugger as a general cleanup step.
1542        # This includes terminating the process for each target, if any.
1543        # We'd like to reuse the debugger for our next test without incurring
1544        # the initialization overhead.
1545        targets = []
1546        for target in self.dbg:
1547            if target:
1548                targets.append(target)
1549                process = target.GetProcess()
1550                if process:
1551                    rc = self.invoke(process, "Kill")
1552                    self.assertTrue(rc.Success(), PROCESS_KILLED)
1553        for target in targets:
1554            self.dbg.DeleteTarget(target)
1555
1556        # Run global post-flight code, if defined via the config file.
1557        if lldb.post_flight:
1558            lldb.post_flight(self)
1559
1560        del self.dbg
1561
1562    def switch_to_thread_with_stop_reason(self, stop_reason):
1563        """
1564        Run the 'thread list' command, and select the thread with stop reason as
1565        'stop_reason'.  If no such thread exists, no select action is done.
1566        """
1567        from lldbutil import stop_reason_to_str
1568        self.runCmd('thread list')
1569        output = self.res.GetOutput()
1570        thread_line_pattern = re.compile("^[ *] thread #([0-9]+):.*stop reason = %s" %
1571                                         stop_reason_to_str(stop_reason))
1572        for line in output.splitlines():
1573            matched = thread_line_pattern.match(line)
1574            if matched:
1575                self.runCmd('thread select %s' % matched.group(1))
1576
1577    def runCmd(self, cmd, msg=None, check=True, trace=False, inHistory=False):
1578        """
1579        Ask the command interpreter to handle the command and then check its
1580        return status.
1581        """
1582        # Fail fast if 'cmd' is not meaningful.
1583        if not cmd or len(cmd) == 0:
1584            raise Exception("Bad 'cmd' parameter encountered")
1585
1586        trace = (True if traceAlways else trace)
1587
1588        running = (cmd.startswith("run") or cmd.startswith("process launch"))
1589
1590        for i in range(self.maxLaunchCount if running else 1):
1591            self.ci.HandleCommand(cmd, self.res, inHistory)
1592
1593            with recording(self, trace) as sbuf:
1594                print >> sbuf, "runCmd:", cmd
1595                if not check:
1596                    print >> sbuf, "check of return status not required"
1597                if self.res.Succeeded():
1598                    print >> sbuf, "output:", self.res.GetOutput()
1599                else:
1600                    print >> sbuf, "runCmd failed!"
1601                    print >> sbuf, self.res.GetError()
1602
1603            if self.res.Succeeded():
1604                break
1605            elif running:
1606                # For process launch, wait some time before possible next try.
1607                time.sleep(self.timeWaitNextLaunch)
1608                with recording(self, trace) as sbuf:
1609                    print >> sbuf, "Command '" + cmd + "' failed!"
1610
1611        if check:
1612            self.assertTrue(self.res.Succeeded(),
1613                            msg if msg else CMD_MSG(cmd))
1614
1615    def match (self, str, patterns, msg=None, trace=False, error=False, matching=True, exe=True):
1616        """run command in str, and match the result against regexp in patterns returning the match object for the first matching pattern
1617
1618        Otherwise, all the arguments have the same meanings as for the expect function"""
1619
1620        trace = (True if traceAlways else trace)
1621
1622        if exe:
1623            # First run the command.  If we are expecting error, set check=False.
1624            # Pass the assert message along since it provides more semantic info.
1625            self.runCmd(str, msg=msg, trace = (True if trace else False), check = not error)
1626
1627            # Then compare the output against expected strings.
1628            output = self.res.GetError() if error else self.res.GetOutput()
1629
1630            # If error is True, the API client expects the command to fail!
1631            if error:
1632                self.assertFalse(self.res.Succeeded(),
1633                                 "Command '" + str + "' is expected to fail!")
1634        else:
1635            # No execution required, just compare str against the golden input.
1636            output = str
1637            with recording(self, trace) as sbuf:
1638                print >> sbuf, "looking at:", output
1639
1640        # The heading says either "Expecting" or "Not expecting".
1641        heading = "Expecting" if matching else "Not expecting"
1642
1643        for pattern in patterns:
1644            # Match Objects always have a boolean value of True.
1645            match_object = re.search(pattern, output)
1646            matched = bool(match_object)
1647            with recording(self, trace) as sbuf:
1648                print >> sbuf, "%s pattern: %s" % (heading, pattern)
1649                print >> sbuf, "Matched" if matched else "Not matched"
1650            if matched:
1651                break
1652
1653        self.assertTrue(matched if matching else not matched,
1654                        msg if msg else EXP_MSG(str, exe))
1655
1656        return match_object
1657
1658    def expect(self, str, msg=None, patterns=None, startstr=None, endstr=None, substrs=None, trace=False, error=False, matching=True, exe=True, inHistory=False):
1659        """
1660        Similar to runCmd; with additional expect style output matching ability.
1661
1662        Ask the command interpreter to handle the command and then check its
1663        return status.  The 'msg' parameter specifies an informational assert
1664        message.  We expect the output from running the command to start with
1665        'startstr', matches the substrings contained in 'substrs', and regexp
1666        matches the patterns contained in 'patterns'.
1667
1668        If the keyword argument error is set to True, it signifies that the API
1669        client is expecting the command to fail.  In this case, the error stream
1670        from running the command is retrieved and compared against the golden
1671        input, instead.
1672
1673        If the keyword argument matching is set to False, it signifies that the API
1674        client is expecting the output of the command not to match the golden
1675        input.
1676
1677        Finally, the required argument 'str' represents the lldb command to be
1678        sent to the command interpreter.  In case the keyword argument 'exe' is
1679        set to False, the 'str' is treated as a string to be matched/not-matched
1680        against the golden input.
1681        """
1682        trace = (True if traceAlways else trace)
1683
1684        if exe:
1685            # First run the command.  If we are expecting error, set check=False.
1686            # Pass the assert message along since it provides more semantic info.
1687            self.runCmd(str, msg=msg, trace = (True if trace else False), check = not error, inHistory=inHistory)
1688
1689            # Then compare the output against expected strings.
1690            output = self.res.GetError() if error else self.res.GetOutput()
1691
1692            # If error is True, the API client expects the command to fail!
1693            if error:
1694                self.assertFalse(self.res.Succeeded(),
1695                                 "Command '" + str + "' is expected to fail!")
1696        else:
1697            # No execution required, just compare str against the golden input.
1698            if isinstance(str,lldb.SBCommandReturnObject):
1699                output = str.GetOutput()
1700            else:
1701                output = str
1702            with recording(self, trace) as sbuf:
1703                print >> sbuf, "looking at:", output
1704
1705        # The heading says either "Expecting" or "Not expecting".
1706        heading = "Expecting" if matching else "Not expecting"
1707
1708        # Start from the startstr, if specified.
1709        # If there's no startstr, set the initial state appropriately.
1710        matched = output.startswith(startstr) if startstr else (True if matching else False)
1711
1712        if startstr:
1713            with recording(self, trace) as sbuf:
1714                print >> sbuf, "%s start string: %s" % (heading, startstr)
1715                print >> sbuf, "Matched" if matched else "Not matched"
1716
1717        # Look for endstr, if specified.
1718        keepgoing = matched if matching else not matched
1719        if endstr:
1720            matched = output.endswith(endstr)
1721            with recording(self, trace) as sbuf:
1722                print >> sbuf, "%s end string: %s" % (heading, endstr)
1723                print >> sbuf, "Matched" if matched else "Not matched"
1724
1725        # Look for sub strings, if specified.
1726        keepgoing = matched if matching else not matched
1727        if substrs and keepgoing:
1728            for str in substrs:
1729                matched = output.find(str) != -1
1730                with recording(self, trace) as sbuf:
1731                    print >> sbuf, "%s sub string: %s" % (heading, str)
1732                    print >> sbuf, "Matched" if matched else "Not matched"
1733                keepgoing = matched if matching else not matched
1734                if not keepgoing:
1735                    break
1736
1737        # Search for regular expression patterns, if specified.
1738        keepgoing = matched if matching else not matched
1739        if patterns and keepgoing:
1740            for pattern in patterns:
1741                # Match Objects always have a boolean value of True.
1742                matched = bool(re.search(pattern, output))
1743                with recording(self, trace) as sbuf:
1744                    print >> sbuf, "%s pattern: %s" % (heading, pattern)
1745                    print >> sbuf, "Matched" if matched else "Not matched"
1746                keepgoing = matched if matching else not matched
1747                if not keepgoing:
1748                    break
1749
1750        self.assertTrue(matched if matching else not matched,
1751                        msg if msg else EXP_MSG(str, exe))
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 >> sbuf, str(method) + ":",  result
1764        return result
1765
1766    # =================================================
1767    # Misc. helper methods for debugging test execution
1768    # =================================================
1769
1770    def DebugSBValue(self, val):
1771        """Debug print a SBValue object, if traceAlways is True."""
1772        from lldbutil import value_type_to_str
1773
1774        if not traceAlways:
1775            return
1776
1777        err = sys.stderr
1778        err.write(val.GetName() + ":\n")
1779        err.write('\t' + "TypeName         -> " + val.GetTypeName()            + '\n')
1780        err.write('\t' + "ByteSize         -> " + str(val.GetByteSize())       + '\n')
1781        err.write('\t' + "NumChildren      -> " + str(val.GetNumChildren())    + '\n')
1782        err.write('\t' + "Value            -> " + str(val.GetValue())          + '\n')
1783        err.write('\t' + "ValueAsUnsigned  -> " + str(val.GetValueAsUnsigned())+ '\n')
1784        err.write('\t' + "ValueType        -> " + value_type_to_str(val.GetValueType()) + '\n')
1785        err.write('\t' + "Summary          -> " + str(val.GetSummary())        + '\n')
1786        err.write('\t' + "IsPointerType    -> " + str(val.TypeIsPointerType()) + '\n')
1787        err.write('\t' + "Location         -> " + val.GetLocation()            + '\n')
1788
1789    def DebugSBType(self, type):
1790        """Debug print a SBType object, if traceAlways is True."""
1791        if not traceAlways:
1792            return
1793
1794        err = sys.stderr
1795        err.write(type.GetName() + ":\n")
1796        err.write('\t' + "ByteSize        -> " + str(type.GetByteSize())     + '\n')
1797        err.write('\t' + "IsPointerType   -> " + str(type.IsPointerType())   + '\n')
1798        err.write('\t' + "IsReferenceType -> " + str(type.IsReferenceType()) + '\n')
1799
1800    def DebugPExpect(self, child):
1801        """Debug the spwaned pexpect object."""
1802        if not traceAlways:
1803            return
1804
1805        print child
1806
1807    @classmethod
1808    def RemoveTempFile(cls, file):
1809        if os.path.exists(file):
1810            os.remove(file)
1811