Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1 | """ |
| 2 | LLDB module which provides the abstract base class of lldb test case. |
| 3 | |
| 4 | The concrete subclass can override lldbtest.TesBase in order to inherit the |
| 5 | common behavior for unitest.TestCase.setUp/tearDown implemented in this file. |
| 6 | |
| 7 | The subclass should override the attribute mydir in order for the python runtime |
| 8 | to locate the individual test cases when running as part of a large test suite |
| 9 | or 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 |
Johnny Chen | c98892e | 2012-05-16 20:41:28 +0000 | [diff] [blame] | 12 | entire of part of the test suite . Example: |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 13 | |
Johnny Chen | c98892e | 2012-05-16 20:41:28 +0000 | [diff] [blame] | 14 | # Exercises the test suite in the types directory.... |
| 15 | /Volumes/data/lldb/svn/ToT/test $ ./dotest.py -A x86_64 types |
Johnny Chen | 57b4738 | 2010-09-02 22:25:47 +0000 | [diff] [blame] | 16 | ... |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 17 | |
Johnny Chen | c98892e | 2012-05-16 20:41:28 +0000 | [diff] [blame] | 18 | Session logs for test failures/errors/unexpected successes will go into directory '2012-05-16-13_35_42' |
| 19 | Command invoked: python ./dotest.py -A x86_64 types |
| 20 | compilers=['clang'] |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 21 | |
Johnny Chen | c98892e | 2012-05-16 20:41:28 +0000 | [diff] [blame] | 22 | Configuration: arch=x86_64 compiler=clang |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 23 | ---------------------------------------------------------------------- |
Johnny Chen | c98892e | 2012-05-16 20:41:28 +0000 | [diff] [blame] | 24 | Collected 72 tests |
| 25 | |
| 26 | ........................................................................ |
| 27 | ---------------------------------------------------------------------- |
| 28 | Ran 72 tests in 135.468s |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 29 | |
| 30 | OK |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 31 | $ |
| 32 | """ |
| 33 | |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 34 | from __future__ import print_function |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 35 | from __future__ import absolute_import |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 36 | |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 37 | # System modules |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 38 | import abc |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 39 | import collections |
Zachary Turner | abdb839 | 2015-11-16 22:40:30 +0000 | [diff] [blame] | 40 | from distutils.version import LooseVersion |
Adrian McCarthy | 6ecdbc8 | 2015-10-15 22:39:55 +0000 | [diff] [blame] | 41 | import gc |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 42 | import glob |
Zachary Turner | ba10570 | 2015-11-16 23:58:20 +0000 | [diff] [blame] | 43 | import inspect |
Johnny Chen | 90312a8 | 2010-09-21 22:34:45 +0000 | [diff] [blame] | 44 | import os, sys, traceback |
Enrico Granata | 7e137e3 | 2012-10-24 18:14:21 +0000 | [diff] [blame] | 45 | import os.path |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 46 | import re |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 47 | import signal |
Johnny Chen | 8952a2d | 2010-08-30 21:35:00 +0000 | [diff] [blame] | 48 | from subprocess import * |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 49 | import time |
Johnny Chen | a33a93c | 2010-08-30 23:08:52 +0000 | [diff] [blame] | 50 | import types |
Zachary Turner | 43a01e4 | 2015-10-20 21:06:05 +0000 | [diff] [blame] | 51 | |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 52 | # Third-party modules |
| 53 | import unittest2 |
Zachary Turner | 43a01e4 | 2015-10-20 21:06:05 +0000 | [diff] [blame] | 54 | from six import add_metaclass |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 55 | from six import StringIO as SixStringIO |
| 56 | from six.moves.urllib import parse as urlparse |
Zachary Turner | cd236b8 | 2015-10-26 18:48:24 +0000 | [diff] [blame] | 57 | import six |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 58 | |
| 59 | # LLDB modules |
| 60 | import lldb |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 61 | from . import configuration |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 62 | from . import lldbtest_config |
| 63 | from . import lldbutil |
| 64 | from . import test_categories |
Siva Chandra | 8af9166 | 2015-06-05 00:22:49 +0000 | [diff] [blame] | 65 | |
Vince Harron | 85d1965 | 2015-05-21 19:09:29 +0000 | [diff] [blame] | 66 | # dosep.py starts lots and lots of dotest instances |
| 67 | # This option helps you find if two (or more) dotest instances are using the same |
| 68 | # directory at the same time |
| 69 | # Enable it to cause test failures and stderr messages if dotest instances try to run in |
| 70 | # the same directory simultaneously |
| 71 | # it is disabled by default because it litters the test directories with ".dirlock" files |
| 72 | debug_confirm_directory_exclusivity = False |
| 73 | |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 74 | # See also dotest.parseOptionsAndInitTestdirs(), where the environment variables |
Johnny Chen | d2047fa | 2011-01-19 18:18:47 +0000 | [diff] [blame] | 75 | # LLDB_COMMAND_TRACE and LLDB_DO_CLEANUP are set from '-t' and '-r dir' options. |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 76 | |
| 77 | # By default, traceAlways is False. |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 78 | if "LLDB_COMMAND_TRACE" in os.environ and os.environ["LLDB_COMMAND_TRACE"]=="YES": |
| 79 | traceAlways = True |
| 80 | else: |
| 81 | traceAlways = False |
| 82 | |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 83 | # By default, doCleanup is True. |
| 84 | if "LLDB_DO_CLEANUP" in os.environ and os.environ["LLDB_DO_CLEANUP"]=="NO": |
| 85 | doCleanup = False |
| 86 | else: |
| 87 | doCleanup = True |
| 88 | |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 89 | |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 90 | # |
| 91 | # Some commonly used assert messages. |
| 92 | # |
| 93 | |
Johnny Chen | aa90292 | 2010-09-17 22:45:27 +0000 | [diff] [blame] | 94 | COMMAND_FAILED_AS_EXPECTED = "Command has failed as expected" |
| 95 | |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 96 | CURRENT_EXECUTABLE_SET = "Current executable set successfully" |
| 97 | |
Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame] | 98 | PROCESS_IS_VALID = "Process is valid" |
| 99 | |
| 100 | PROCESS_KILLED = "Process is killed successfully" |
| 101 | |
Johnny Chen | d5f66fc | 2010-12-23 01:12:19 +0000 | [diff] [blame] | 102 | PROCESS_EXITED = "Process exited successfully" |
| 103 | |
| 104 | PROCESS_STOPPED = "Process status should be stopped" |
| 105 | |
Sean Callanan | 05834cd | 2015-07-01 23:56:30 +0000 | [diff] [blame] | 106 | RUN_SUCCEEDED = "Process is launched successfully" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 107 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 108 | RUN_COMPLETED = "Process exited successfully" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 109 | |
Johnny Chen | 67af43f | 2010-10-05 19:27:32 +0000 | [diff] [blame] | 110 | BACKTRACE_DISPLAYED_CORRECTLY = "Backtrace displayed correctly" |
| 111 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 112 | BREAKPOINT_CREATED = "Breakpoint created successfully" |
| 113 | |
Johnny Chen | f10af38 | 2010-12-04 00:07:24 +0000 | [diff] [blame] | 114 | BREAKPOINT_STATE_CORRECT = "Breakpoint state is correct" |
| 115 | |
Johnny Chen | e76896c | 2010-08-17 21:33:31 +0000 | [diff] [blame] | 116 | BREAKPOINT_PENDING_CREATED = "Pending breakpoint created successfully" |
| 117 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 118 | BREAKPOINT_HIT_ONCE = "Breakpoint resolved with hit cout = 1" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 119 | |
Johnny Chen | 703dbd0 | 2010-09-30 17:06:24 +0000 | [diff] [blame] | 120 | BREAKPOINT_HIT_TWICE = "Breakpoint resolved with hit cout = 2" |
| 121 | |
Johnny Chen | 164f1e1 | 2010-10-15 18:07:09 +0000 | [diff] [blame] | 122 | BREAKPOINT_HIT_THRICE = "Breakpoint resolved with hit cout = 3" |
| 123 | |
Greg Clayton | 5db6b79 | 2012-10-24 18:24:14 +0000 | [diff] [blame] | 124 | MISSING_EXPECTED_REGISTERS = "At least one expected register is unavailable." |
| 125 | |
Johnny Chen | 89109ed1 | 2011-06-27 20:05:23 +0000 | [diff] [blame] | 126 | OBJECT_PRINTED_CORRECTLY = "Object printed correctly" |
| 127 | |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 128 | SOURCE_DISPLAYED_CORRECTLY = "Source code displayed correctly" |
| 129 | |
Johnny Chen | c70b02a | 2010-09-22 23:00:20 +0000 | [diff] [blame] | 130 | STEP_OUT_SUCCEEDED = "Thread step-out succeeded" |
| 131 | |
Johnny Chen | 1691a16 | 2011-04-15 16:44:48 +0000 | [diff] [blame] | 132 | STOPPED_DUE_TO_EXC_BAD_ACCESS = "Process should be stopped due to bad access exception" |
| 133 | |
Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 134 | STOPPED_DUE_TO_ASSERT = "Process should be stopped due to an assertion" |
| 135 | |
Johnny Chen | 5d6c464 | 2010-11-10 23:46:38 +0000 | [diff] [blame] | 136 | STOPPED_DUE_TO_BREAKPOINT = "Process should be stopped due to breakpoint" |
Johnny Chen | de0338b | 2010-11-10 20:20:06 +0000 | [diff] [blame] | 137 | |
Johnny Chen | 5d6c464 | 2010-11-10 23:46:38 +0000 | [diff] [blame] | 138 | STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS = "%s, %s" % ( |
| 139 | STOPPED_DUE_TO_BREAKPOINT, "instead, the actual stop reason is: '%s'") |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 140 | |
Johnny Chen | 2e431ce | 2010-10-20 18:38:48 +0000 | [diff] [blame] | 141 | STOPPED_DUE_TO_BREAKPOINT_CONDITION = "Stopped due to breakpoint condition" |
| 142 | |
Johnny Chen | 0a3d1ca | 2010-12-13 21:49:58 +0000 | [diff] [blame] | 143 | STOPPED_DUE_TO_BREAKPOINT_IGNORE_COUNT = "Stopped due to breakpoint and ignore count" |
| 144 | |
Johnny Chen | c066ab4 | 2010-10-14 01:22:03 +0000 | [diff] [blame] | 145 | STOPPED_DUE_TO_SIGNAL = "Process state is stopped due to signal" |
| 146 | |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 147 | STOPPED_DUE_TO_STEP_IN = "Process state is stopped due to step in" |
| 148 | |
Johnny Chen | f68cc12 | 2011-09-15 21:09:59 +0000 | [diff] [blame] | 149 | STOPPED_DUE_TO_WATCHPOINT = "Process should be stopped due to watchpoint" |
| 150 | |
Johnny Chen | 3c884a0 | 2010-08-24 22:07:56 +0000 | [diff] [blame] | 151 | DATA_TYPES_DISPLAYED_CORRECTLY = "Data type(s) displayed correctly" |
| 152 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 153 | VALID_BREAKPOINT = "Got a valid breakpoint" |
| 154 | |
Johnny Chen | 5bfb8ee | 2010-10-22 18:10:25 +0000 | [diff] [blame] | 155 | VALID_BREAKPOINT_LOCATION = "Got a valid breakpoint location" |
| 156 | |
Johnny Chen | 7209d84f | 2011-05-06 23:26:12 +0000 | [diff] [blame] | 157 | VALID_COMMAND_INTERPRETER = "Got a valid command interpreter" |
| 158 | |
Johnny Chen | 5ee8819 | 2010-08-27 23:47:36 +0000 | [diff] [blame] | 159 | VALID_FILESPEC = "Got a valid filespec" |
| 160 | |
Johnny Chen | 025d1b8 | 2010-12-08 01:25:21 +0000 | [diff] [blame] | 161 | VALID_MODULE = "Got a valid module" |
| 162 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 163 | VALID_PROCESS = "Got a valid process" |
| 164 | |
Johnny Chen | 025d1b8 | 2010-12-08 01:25:21 +0000 | [diff] [blame] | 165 | VALID_SYMBOL = "Got a valid symbol" |
| 166 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 167 | VALID_TARGET = "Got a valid target" |
| 168 | |
Matthew Gardiner | c928de3 | 2014-10-22 07:22:56 +0000 | [diff] [blame] | 169 | VALID_PLATFORM = "Got a valid platform" |
| 170 | |
Johnny Chen | 15f247a | 2012-02-03 20:43:00 +0000 | [diff] [blame] | 171 | VALID_TYPE = "Got a valid type" |
| 172 | |
Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 173 | VALID_VARIABLE = "Got a valid variable" |
| 174 | |
Johnny Chen | 981463d | 2010-08-25 19:00:04 +0000 | [diff] [blame] | 175 | VARIABLES_DISPLAYED_CORRECTLY = "Variable(s) displayed correctly" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 176 | |
Johnny Chen | f68cc12 | 2011-09-15 21:09:59 +0000 | [diff] [blame] | 177 | WATCHPOINT_CREATED = "Watchpoint created successfully" |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 178 | |
Sean Callanan | 05834cd | 2015-07-01 23:56:30 +0000 | [diff] [blame] | 179 | def CMD_MSG(str): |
| 180 | '''A generic "Command '%s' returns successfully" message generator.''' |
| 181 | return "Command '%s' returns successfully" % str |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 182 | |
Johnny Chen | 3bc8ae4 | 2012-03-15 19:10:00 +0000 | [diff] [blame] | 183 | def COMPLETION_MSG(str_before, str_after): |
Johnny Chen | 98aceb0 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 184 | '''A generic message generator for the completion mechanism.''' |
| 185 | return "'%s' successfully completes to '%s'" % (str_before, str_after) |
| 186 | |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 187 | def EXP_MSG(str, exe): |
Johnny Chen | aacf92e | 2011-05-31 22:16:51 +0000 | [diff] [blame] | 188 | '''A generic "'%s' returns expected result" message generator if exe. |
| 189 | Otherwise, it generates "'%s' matches expected result" message.''' |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 190 | return "'%s' %s expected result" % (str, 'returns' if exe else 'matches') |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 191 | |
Johnny Chen | 3343f04 | 2010-10-19 19:11:38 +0000 | [diff] [blame] | 192 | def SETTING_MSG(setting): |
Johnny Chen | aacf92e | 2011-05-31 22:16:51 +0000 | [diff] [blame] | 193 | '''A generic "Value of setting '%s' is correct" message generator.''' |
Johnny Chen | 3343f04 | 2010-10-19 19:11:38 +0000 | [diff] [blame] | 194 | return "Value of setting '%s' is correct" % setting |
| 195 | |
Johnny Chen | 27c4123 | 2010-08-26 21:49:29 +0000 | [diff] [blame] | 196 | def EnvArray(): |
Johnny Chen | aacf92e | 2011-05-31 22:16:51 +0000 | [diff] [blame] | 197 | """Returns an env variable array from the os.environ map object.""" |
Zachary Turner | 606e1e3 | 2015-10-23 17:53:51 +0000 | [diff] [blame] | 198 | return list(map(lambda k,v: k+"="+v, list(os.environ.keys()), list(os.environ.values()))) |
Johnny Chen | 27c4123 | 2010-08-26 21:49:29 +0000 | [diff] [blame] | 199 | |
Johnny Chen | 47ceb03 | 2010-10-11 23:52:19 +0000 | [diff] [blame] | 200 | def line_number(filename, string_to_match): |
| 201 | """Helper function to return the line number of the first matched string.""" |
| 202 | with open(filename, 'r') as f: |
| 203 | for i, line in enumerate(f): |
| 204 | if line.find(string_to_match) != -1: |
| 205 | # Found our match. |
Johnny Chen | cd9b777 | 2010-10-12 00:09:25 +0000 | [diff] [blame] | 206 | return i+1 |
Johnny Chen | 1691a16 | 2011-04-15 16:44:48 +0000 | [diff] [blame] | 207 | raise Exception("Unable to find '%s' within file %s" % (string_to_match, filename)) |
Johnny Chen | 47ceb03 | 2010-10-11 23:52:19 +0000 | [diff] [blame] | 208 | |
Johnny Chen | 67af43f | 2010-10-05 19:27:32 +0000 | [diff] [blame] | 209 | def pointer_size(): |
| 210 | """Return the pointer size of the host system.""" |
| 211 | import ctypes |
| 212 | a_pointer = ctypes.c_void_p(0xffff) |
| 213 | return 8 * ctypes.sizeof(a_pointer) |
| 214 | |
Johnny Chen | 5781673 | 2012-02-09 02:01:59 +0000 | [diff] [blame] | 215 | def is_exe(fpath): |
| 216 | """Returns true if fpath is an executable.""" |
| 217 | return os.path.isfile(fpath) and os.access(fpath, os.X_OK) |
| 218 | |
| 219 | def which(program): |
| 220 | """Returns the full path to a program; None otherwise.""" |
| 221 | fpath, fname = os.path.split(program) |
| 222 | if fpath: |
| 223 | if is_exe(program): |
| 224 | return program |
| 225 | else: |
| 226 | for path in os.environ["PATH"].split(os.pathsep): |
| 227 | exe_file = os.path.join(path, program) |
| 228 | if is_exe(exe_file): |
| 229 | return exe_file |
| 230 | return None |
| 231 | |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 232 | class recording(SixStringIO): |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 233 | """ |
| 234 | A nice little context manager for recording the debugger interactions into |
| 235 | our session object. If trace flag is ON, it also emits the interactions |
| 236 | into the stderr. |
| 237 | """ |
| 238 | def __init__(self, test, trace): |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 239 | """Create a SixStringIO instance; record the session obj and trace flag.""" |
| 240 | SixStringIO.__init__(self) |
Johnny Chen | 0241f14 | 2011-08-16 22:06:17 +0000 | [diff] [blame] | 241 | # The test might not have undergone the 'setUp(self)' phase yet, so that |
| 242 | # the attribute 'session' might not even exist yet. |
Johnny Chen | bfcf37f | 2011-08-16 17:06:45 +0000 | [diff] [blame] | 243 | self.session = getattr(test, "session", None) if test else None |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 244 | self.trace = trace |
| 245 | |
| 246 | def __enter__(self): |
| 247 | """ |
| 248 | Context management protocol on entry to the body of the with statement. |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 249 | Just return the SixStringIO object. |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 250 | """ |
| 251 | return self |
| 252 | |
| 253 | def __exit__(self, type, value, tb): |
| 254 | """ |
| 255 | Context management protocol on exit from the body of the with statement. |
| 256 | If trace is ON, it emits the recordings into stderr. Always add the |
Zachary Turner | 814236d | 2015-10-21 17:48:52 +0000 | [diff] [blame] | 257 | recordings to our session object. And close the SixStringIO object, too. |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 258 | """ |
| 259 | if self.trace: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 260 | print(self.getvalue(), file=sys.stderr) |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 261 | if self.session: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 262 | print(self.getvalue(), file=self.session) |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 263 | self.close() |
| 264 | |
Zachary Turner | 43a01e4 | 2015-10-20 21:06:05 +0000 | [diff] [blame] | 265 | @add_metaclass(abc.ABCMeta) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 266 | class _BaseProcess(object): |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 267 | |
| 268 | @abc.abstractproperty |
| 269 | def pid(self): |
| 270 | """Returns process PID if has been launched already.""" |
| 271 | |
| 272 | @abc.abstractmethod |
| 273 | def launch(self, executable, args): |
| 274 | """Launches new process with given executable and args.""" |
| 275 | |
| 276 | @abc.abstractmethod |
| 277 | def terminate(self): |
| 278 | """Terminates previously launched process..""" |
| 279 | |
| 280 | class _LocalProcess(_BaseProcess): |
| 281 | |
| 282 | def __init__(self, trace_on): |
| 283 | self._proc = None |
| 284 | self._trace_on = trace_on |
Ilia K | 725abcb | 2015-04-15 13:35:49 +0000 | [diff] [blame] | 285 | self._delayafterterminate = 0.1 |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 286 | |
| 287 | @property |
| 288 | def pid(self): |
| 289 | return self._proc.pid |
| 290 | |
| 291 | def launch(self, executable, args): |
| 292 | self._proc = Popen([executable] + args, |
| 293 | stdout = open(os.devnull) if not self._trace_on else None, |
| 294 | stdin = PIPE) |
| 295 | |
| 296 | def terminate(self): |
| 297 | if self._proc.poll() == None: |
Ilia K | 725abcb | 2015-04-15 13:35:49 +0000 | [diff] [blame] | 298 | # Terminate _proc like it does the pexpect |
Adrian McCarthy | 137d7ba | 2015-07-07 14:47:34 +0000 | [diff] [blame] | 299 | signals_to_try = [sig for sig in ['SIGHUP', 'SIGCONT', 'SIGINT'] if sig in dir(signal)] |
| 300 | for sig in signals_to_try: |
| 301 | try: |
| 302 | self._proc.send_signal(getattr(signal, sig)) |
| 303 | time.sleep(self._delayafterterminate) |
| 304 | if self._proc.poll() != None: |
| 305 | return |
| 306 | except ValueError: |
| 307 | pass # Windows says SIGINT is not a valid signal to send |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 308 | self._proc.terminate() |
Ilia K | 725abcb | 2015-04-15 13:35:49 +0000 | [diff] [blame] | 309 | time.sleep(self._delayafterterminate) |
| 310 | if self._proc.poll() != None: |
| 311 | return |
| 312 | self._proc.kill() |
| 313 | time.sleep(self._delayafterterminate) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 314 | |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 315 | def poll(self): |
| 316 | return self._proc.poll() |
| 317 | |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 318 | class _RemoteProcess(_BaseProcess): |
| 319 | |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 320 | def __init__(self, install_remote): |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 321 | self._pid = None |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 322 | self._install_remote = install_remote |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 323 | |
| 324 | @property |
| 325 | def pid(self): |
| 326 | return self._pid |
| 327 | |
| 328 | def launch(self, executable, args): |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 329 | if self._install_remote: |
| 330 | src_path = executable |
Chaoren Lin | 5d76b1b | 2015-06-06 00:25:50 +0000 | [diff] [blame] | 331 | dst_path = lldbutil.append_to_process_working_directory(os.path.basename(executable)) |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 332 | |
| 333 | dst_file_spec = lldb.SBFileSpec(dst_path, False) |
| 334 | err = lldb.remote_platform.Install(lldb.SBFileSpec(src_path, True), dst_file_spec) |
| 335 | if err.Fail(): |
| 336 | raise Exception("remote_platform.Install('%s', '%s') failed: %s" % (src_path, dst_path, err)) |
| 337 | else: |
| 338 | dst_path = executable |
| 339 | dst_file_spec = lldb.SBFileSpec(executable, False) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 340 | |
| 341 | launch_info = lldb.SBLaunchInfo(args) |
| 342 | launch_info.SetExecutableFile(dst_file_spec, True) |
Chaoren Lin | 3e2bdb4 | 2015-05-11 17:53:39 +0000 | [diff] [blame] | 343 | launch_info.SetWorkingDirectory(lldb.remote_platform.GetWorkingDirectory()) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 344 | |
| 345 | # Redirect stdout and stderr to /dev/null |
| 346 | launch_info.AddSuppressFileAction(1, False, True) |
| 347 | launch_info.AddSuppressFileAction(2, False, True) |
| 348 | |
| 349 | err = lldb.remote_platform.Launch(launch_info) |
| 350 | if err.Fail(): |
| 351 | raise Exception("remote_platform.Launch('%s', '%s') failed: %s" % (dst_path, args, err)) |
| 352 | self._pid = launch_info.GetProcessID() |
| 353 | |
| 354 | def terminate(self): |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 355 | lldb.remote_platform.Kill(self._pid) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 356 | |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 357 | # From 2.7's subprocess.check_output() convenience function. |
Johnny Chen | ac77f3b | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 358 | # Return a tuple (stdoutdata, stderrdata). |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 359 | def system(commands, **kwargs): |
Johnny Chen | 8eb14a9 | 2011-11-16 22:44:28 +0000 | [diff] [blame] | 360 | r"""Run an os command with arguments and return its output as a byte string. |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 361 | |
| 362 | If the exit code was non-zero it raises a CalledProcessError. The |
| 363 | CalledProcessError object will have the return code in the returncode |
| 364 | attribute and output in the output attribute. |
| 365 | |
| 366 | The arguments are the same as for the Popen constructor. Example: |
| 367 | |
| 368 | >>> check_output(["ls", "-l", "/dev/null"]) |
| 369 | 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' |
| 370 | |
| 371 | The stdout argument is not allowed as it is used internally. |
| 372 | To capture standard error in the result, use stderr=STDOUT. |
| 373 | |
| 374 | >>> check_output(["/bin/sh", "-c", |
| 375 | ... "ls -l non_existent_file ; exit 0"], |
| 376 | ... stderr=STDOUT) |
| 377 | 'ls: non_existent_file: No such file or directory\n' |
| 378 | """ |
| 379 | |
| 380 | # Assign the sender object to variable 'test' and remove it from kwargs. |
| 381 | test = kwargs.pop('sender', None) |
| 382 | |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 383 | # [['make', 'clean', 'foo'], ['make', 'foo']] -> ['make clean foo', 'make foo'] |
| 384 | commandList = [' '.join(x) for x in commands] |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 385 | output = "" |
| 386 | error = "" |
| 387 | for shellCommand in commandList: |
| 388 | if 'stdout' in kwargs: |
| 389 | raise ValueError('stdout argument not allowed, it will be overridden.') |
| 390 | if 'shell' in kwargs and kwargs['shell']==False: |
| 391 | raise ValueError('shell=False not allowed') |
Zachary Turner | 48ef8d4c | 2015-11-18 18:40:16 +0000 | [diff] [blame] | 392 | process = Popen(shellCommand, stdout=PIPE, stderr=PIPE, shell=True, universal_newlines=True, **kwargs) |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 393 | pid = process.pid |
| 394 | this_output, this_error = process.communicate() |
| 395 | retcode = process.poll() |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 396 | |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 397 | # Enable trace on failure return while tracking down FreeBSD buildbot issues |
| 398 | trace = traceAlways |
| 399 | if not trace and retcode and sys.platform.startswith("freebsd"): |
| 400 | trace = True |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 401 | |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 402 | with recording(test, trace) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 403 | print(file=sbuf) |
| 404 | print("os command:", shellCommand, file=sbuf) |
| 405 | print("with pid:", pid, file=sbuf) |
| 406 | print("stdout:", this_output, file=sbuf) |
| 407 | print("stderr:", this_error, file=sbuf) |
| 408 | print("retcode:", retcode, file=sbuf) |
| 409 | print(file=sbuf) |
Ed Maste | 6e49633 | 2014-08-05 20:33:17 +0000 | [diff] [blame] | 410 | |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 411 | if retcode: |
| 412 | cmd = kwargs.get("args") |
| 413 | if cmd is None: |
| 414 | cmd = shellCommand |
| 415 | raise CalledProcessError(retcode, cmd) |
| 416 | output = output + this_output |
| 417 | error = error + this_error |
Johnny Chen | ac77f3b | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 418 | return (output, error) |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 419 | |
Johnny Chen | ab9c1dd | 2010-11-01 20:35:01 +0000 | [diff] [blame] | 420 | def getsource_if_available(obj): |
| 421 | """ |
| 422 | Return the text of the source code for an object if available. Otherwise, |
| 423 | a print representation is returned. |
| 424 | """ |
| 425 | import inspect |
| 426 | try: |
| 427 | return inspect.getsource(obj) |
| 428 | except: |
| 429 | return repr(obj) |
| 430 | |
Peter Collingbourne | 19f48d5 | 2011-06-20 19:06:20 +0000 | [diff] [blame] | 431 | def builder_module(): |
Ed Maste | 4d90f0f | 2013-07-25 13:24:34 +0000 | [diff] [blame] | 432 | if sys.platform.startswith("freebsd"): |
| 433 | return __import__("builder_freebsd") |
Kamil Rytarowski | 0b655da | 2015-12-05 18:46:56 +0000 | [diff] [blame] | 434 | if sys.platform.startswith("netbsd"): |
| 435 | return __import__("builder_netbsd") |
Peter Collingbourne | 19f48d5 | 2011-06-20 19:06:20 +0000 | [diff] [blame] | 436 | return __import__("builder_" + sys.platform) |
| 437 | |
Siva Chandra | 8af9166 | 2015-06-05 00:22:49 +0000 | [diff] [blame] | 438 | def run_adb_command(cmd, device_id): |
| 439 | device_id_args = [] |
| 440 | if device_id: |
| 441 | device_id_args = ["-s", device_id] |
| 442 | full_cmd = ["adb"] + device_id_args + cmd |
| 443 | p = Popen(full_cmd, stdout=PIPE, stderr=PIPE) |
| 444 | stdout, stderr = p.communicate() |
| 445 | return p.returncode, stdout, stderr |
| 446 | |
Chaoren Lin | e9bbabc | 2015-07-18 00:37:55 +0000 | [diff] [blame] | 447 | def append_android_envs(dictionary): |
| 448 | if dictionary is None: |
| 449 | dictionary = {} |
| 450 | dictionary["OS"] = "Android" |
| 451 | if android_device_api() >= 16: |
| 452 | dictionary["PIE"] = 1 |
| 453 | return dictionary |
| 454 | |
Chaoren Lin | 9070f53 | 2015-07-17 22:13:29 +0000 | [diff] [blame] | 455 | def target_is_android(): |
| 456 | if not hasattr(target_is_android, 'result'): |
| 457 | triple = lldb.DBG.GetSelectedPlatform().GetTriple() |
| 458 | match = re.match(".*-.*-.*-android", triple) |
| 459 | target_is_android.result = match is not None |
| 460 | return target_is_android.result |
| 461 | |
Siva Chandra | 8af9166 | 2015-06-05 00:22:49 +0000 | [diff] [blame] | 462 | def android_device_api(): |
Chaoren Lin | 9070f53 | 2015-07-17 22:13:29 +0000 | [diff] [blame] | 463 | if not hasattr(android_device_api, 'result'): |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 464 | assert configuration.lldb_platform_url is not None |
Chaoren Lin | 9070f53 | 2015-07-17 22:13:29 +0000 | [diff] [blame] | 465 | device_id = None |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 466 | parsed_url = urlparse.urlparse(configuration.lldb_platform_url) |
Ying Chen | ca922bb | 2015-11-18 19:03:20 +0000 | [diff] [blame] | 467 | host_name = parsed_url.netloc.split(":")[0] |
| 468 | if host_name != 'localhost': |
| 469 | device_id = host_name |
| 470 | if device_id.startswith('[') and device_id.endswith(']'): |
| 471 | device_id = device_id[1:-1] |
Chaoren Lin | 9070f53 | 2015-07-17 22:13:29 +0000 | [diff] [blame] | 472 | retcode, stdout, stderr = run_adb_command( |
| 473 | ["shell", "getprop", "ro.build.version.sdk"], device_id) |
| 474 | if retcode == 0: |
| 475 | android_device_api.result = int(stdout) |
| 476 | else: |
| 477 | raise LookupError( |
| 478 | ">>> Unable to determine the API level of the Android device.\n" |
| 479 | ">>> stdout:\n%s\n" |
| 480 | ">>> stderr:\n%s\n" % (stdout, stderr)) |
| 481 | return android_device_api.result |
Siva Chandra | 8af9166 | 2015-06-05 00:22:49 +0000 | [diff] [blame] | 482 | |
Zachary Turner | abdb839 | 2015-11-16 22:40:30 +0000 | [diff] [blame] | 483 | def check_expected_version(comparison, expected, actual): |
| 484 | def fn_leq(x,y): return x <= y |
| 485 | def fn_less(x,y): return x < y |
| 486 | def fn_geq(x,y): return x >= y |
| 487 | def fn_greater(x,y): return x > y |
| 488 | def fn_eq(x,y): return x == y |
| 489 | def fn_neq(x,y): return x != y |
| 490 | |
| 491 | op_lookup = { |
| 492 | "==": fn_eq, |
| 493 | "=": fn_eq, |
| 494 | "!=": fn_neq, |
| 495 | "<>": fn_neq, |
| 496 | ">": fn_greater, |
| 497 | "<": fn_less, |
| 498 | ">=": fn_geq, |
| 499 | "<=": fn_leq |
| 500 | } |
| 501 | expected_str = '.'.join([str(x) for x in expected]) |
| 502 | actual_str = '.'.join([str(x) for x in actual]) |
| 503 | |
| 504 | return op_lookup[comparison](LooseVersion(actual_str), LooseVersion(expected_str)) |
| 505 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 506 | # |
| 507 | # Decorators for categorizing test cases. |
| 508 | # |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 509 | from functools import wraps |
Pavel Labath | dc8b2d3 | 2015-10-26 09:28:32 +0000 | [diff] [blame] | 510 | def add_test_categories(cat): |
| 511 | """Decorate an item with test categories""" |
| 512 | cat = test_categories.validate(cat, True) |
| 513 | def impl(func): |
| 514 | func.getCategories = lambda test: cat |
| 515 | return func |
| 516 | return impl |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 517 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 518 | def benchmarks_test(func): |
| 519 | """Decorate the item as a benchmarks test.""" |
| 520 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 521 | raise Exception("@benchmarks_test can only be used to decorate a test method") |
| 522 | @wraps(func) |
| 523 | def wrapper(self, *args, **kwargs): |
Zachary Turner | aad25fb | 2015-12-08 18:36:05 +0000 | [diff] [blame] | 524 | self.skipTest("benchmarks test") |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 525 | return func(self, *args, **kwargs) |
| 526 | |
| 527 | # Mark this function as such to separate them from the regular tests. |
| 528 | wrapper.__benchmarks_test__ = True |
| 529 | return wrapper |
| 530 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 531 | def no_debug_info_test(func): |
| 532 | """Decorate the item as a test what don't use any debug info. If this annotation is specified |
| 533 | then the test runner won't generate a separate test for each debug info format. """ |
| 534 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 535 | raise Exception("@no_debug_info_test can only be used to decorate a test method") |
| 536 | @wraps(func) |
| 537 | def wrapper(self, *args, **kwargs): |
| 538 | return func(self, *args, **kwargs) |
| 539 | |
| 540 | # Mark this function as such to separate them from the regular tests. |
| 541 | wrapper.__no_debug_info_test__ = True |
| 542 | return wrapper |
| 543 | |
Johnny Chen | f1548d4 | 2012-04-06 00:56:05 +0000 | [diff] [blame] | 544 | def dsym_test(func): |
| 545 | """Decorate the item as a dsym test.""" |
| 546 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 547 | raise Exception("@dsym_test can only be used to decorate a test method") |
| 548 | @wraps(func) |
| 549 | def wrapper(self, *args, **kwargs): |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 550 | if configuration.dont_do_dsym_test: |
Pavel Labath | f882f6f | 2015-10-12 13:42:16 +0000 | [diff] [blame] | 551 | self.skipTest("dsym tests") |
Johnny Chen | f1548d4 | 2012-04-06 00:56:05 +0000 | [diff] [blame] | 552 | return func(self, *args, **kwargs) |
| 553 | |
| 554 | # Mark this function as such to separate them from the regular tests. |
| 555 | wrapper.__dsym_test__ = True |
| 556 | return wrapper |
| 557 | |
| 558 | def dwarf_test(func): |
| 559 | """Decorate the item as a dwarf test.""" |
| 560 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 561 | raise Exception("@dwarf_test can only be used to decorate a test method") |
| 562 | @wraps(func) |
| 563 | def wrapper(self, *args, **kwargs): |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 564 | if configuration.dont_do_dwarf_test: |
Pavel Labath | f882f6f | 2015-10-12 13:42:16 +0000 | [diff] [blame] | 565 | self.skipTest("dwarf tests") |
Johnny Chen | f1548d4 | 2012-04-06 00:56:05 +0000 | [diff] [blame] | 566 | return func(self, *args, **kwargs) |
| 567 | |
| 568 | # Mark this function as such to separate them from the regular tests. |
| 569 | wrapper.__dwarf_test__ = True |
| 570 | return wrapper |
| 571 | |
Tamas Berghammer | 4c0c7a7 | 2015-10-07 10:02:17 +0000 | [diff] [blame] | 572 | def dwo_test(func): |
| 573 | """Decorate the item as a dwo test.""" |
| 574 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 575 | raise Exception("@dwo_test can only be used to decorate a test method") |
| 576 | @wraps(func) |
| 577 | def wrapper(self, *args, **kwargs): |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 578 | if configuration.dont_do_dwo_test: |
Pavel Labath | f882f6f | 2015-10-12 13:42:16 +0000 | [diff] [blame] | 579 | self.skipTest("dwo tests") |
Tamas Berghammer | 4c0c7a7 | 2015-10-07 10:02:17 +0000 | [diff] [blame] | 580 | return func(self, *args, **kwargs) |
| 581 | |
| 582 | # Mark this function as such to separate them from the regular tests. |
| 583 | wrapper.__dwo_test__ = True |
| 584 | return wrapper |
| 585 | |
Todd Fiala | a41d48c | 2014-04-28 04:49:40 +0000 | [diff] [blame] | 586 | def debugserver_test(func): |
| 587 | """Decorate the item as a debugserver test.""" |
| 588 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 589 | raise Exception("@debugserver_test can only be used to decorate a test method") |
| 590 | @wraps(func) |
| 591 | def wrapper(self, *args, **kwargs): |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 592 | if configuration.dont_do_debugserver_test: |
Pavel Labath | f882f6f | 2015-10-12 13:42:16 +0000 | [diff] [blame] | 593 | self.skipTest("debugserver tests") |
Todd Fiala | a41d48c | 2014-04-28 04:49:40 +0000 | [diff] [blame] | 594 | return func(self, *args, **kwargs) |
| 595 | |
| 596 | # Mark this function as such to separate them from the regular tests. |
| 597 | wrapper.__debugserver_test__ = True |
| 598 | return wrapper |
| 599 | |
| 600 | def llgs_test(func): |
Robert Flack | 8cc4cf1 | 2015-03-06 14:36:33 +0000 | [diff] [blame] | 601 | """Decorate the item as a lldb-server test.""" |
Todd Fiala | a41d48c | 2014-04-28 04:49:40 +0000 | [diff] [blame] | 602 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 603 | raise Exception("@llgs_test can only be used to decorate a test method") |
| 604 | @wraps(func) |
| 605 | def wrapper(self, *args, **kwargs): |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 606 | if configuration.dont_do_llgs_test: |
Pavel Labath | f882f6f | 2015-10-12 13:42:16 +0000 | [diff] [blame] | 607 | self.skipTest("llgs tests") |
Todd Fiala | a41d48c | 2014-04-28 04:49:40 +0000 | [diff] [blame] | 608 | return func(self, *args, **kwargs) |
| 609 | |
| 610 | # Mark this function as such to separate them from the regular tests. |
| 611 | wrapper.__llgs_test__ = True |
| 612 | return wrapper |
| 613 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 614 | def not_remote_testsuite_ready(func): |
| 615 | """Decorate the item as a test which is not ready yet for remote testsuite.""" |
| 616 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 617 | raise Exception("@not_remote_testsuite_ready can only be used to decorate a test method") |
| 618 | @wraps(func) |
| 619 | def wrapper(self, *args, **kwargs): |
Zachary Turner | d865c6b | 2015-12-08 22:15:48 +0000 | [diff] [blame^] | 620 | if lldb.remote_platform: |
Pavel Labath | f882f6f | 2015-10-12 13:42:16 +0000 | [diff] [blame] | 621 | self.skipTest("not ready for remote testsuite") |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 622 | return func(self, *args, **kwargs) |
| 623 | |
| 624 | # Mark this function as such to separate them from the regular tests. |
| 625 | wrapper.__not_ready_for_remote_testsuite_test__ = True |
| 626 | return wrapper |
| 627 | |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 628 | def expectedFailure(expected_fn, bugnumber=None): |
| 629 | def expectedFailure_impl(func): |
| 630 | @wraps(func) |
| 631 | def wrapper(*args, **kwargs): |
Enrico Granata | 43f6213 | 2013-02-23 01:28:30 +0000 | [diff] [blame] | 632 | from unittest2 import case |
| 633 | self = args[0] |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 634 | if expected_fn(self): |
Zachary Turner | 5cb8e67 | 2015-11-06 18:14:42 +0000 | [diff] [blame] | 635 | xfail_func = unittest2.expectedFailure(func) |
| 636 | xfail_func(*args, **kwargs) |
| 637 | else: |
| 638 | func(*args, **kwargs) |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 639 | return wrapper |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 640 | # if bugnumber is not-callable(incluing None), that means decorator function is called with optional arguments |
| 641 | # return decorator in this case, so it will be used to decorating original method |
Zachary Turner | cd236b8 | 2015-10-26 18:48:24 +0000 | [diff] [blame] | 642 | if six.callable(bugnumber): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 643 | return expectedFailure_impl(bugnumber) |
| 644 | else: |
| 645 | return expectedFailure_impl |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 646 | |
Ying Chen | 0c35282 | 2015-11-16 23:41:02 +0000 | [diff] [blame] | 647 | # You can also pass not_in(list) to reverse the sense of the test for the arguments that |
| 648 | # are simple lists, namely oslist, compiler, and debug_info. |
| 649 | |
| 650 | def not_in (iterable): |
| 651 | return lambda x : x not in iterable |
| 652 | |
Siva Chandra | 7dcad31 | 2015-11-20 20:30:36 +0000 | [diff] [blame] | 653 | def check_list_or_lambda (list_or_lambda, value): |
| 654 | if six.callable(list_or_lambda): |
| 655 | return list_or_lambda(value) |
| 656 | else: |
| 657 | return list_or_lambda is None or value is None or value in list_or_lambda |
Ying Chen | 0c35282 | 2015-11-16 23:41:02 +0000 | [diff] [blame] | 658 | |
Ying Chen | 7091c2c | 2015-04-21 01:15:47 +0000 | [diff] [blame] | 659 | # provide a function to xfail on defined oslist, compiler version, and archs |
| 660 | # if none is specified for any argument, that argument won't be checked and thus means for all |
| 661 | # for example, |
| 662 | # @expectedFailureAll, xfail for all platform/compiler/arch, |
| 663 | # @expectedFailureAll(compiler='gcc'), xfail for gcc on all platform/architecture |
| 664 | # @expectedFailureAll(bugnumber, ["linux"], "gcc", ['>=', '4.9'], ['i386']), xfail for gcc>=4.9 on linux with i386 |
Zachary Turner | abdb839 | 2015-11-16 22:40:30 +0000 | [diff] [blame] | 665 | def expectedFailureAll(bugnumber=None, oslist=None, compiler=None, compiler_version=None, archs=None, triple=None, debug_info=None, swig_version=None, py_version=None): |
Ying Chen | 7091c2c | 2015-04-21 01:15:47 +0000 | [diff] [blame] | 666 | def fn(self): |
Siva Chandra | 7dcad31 | 2015-11-20 20:30:36 +0000 | [diff] [blame] | 667 | oslist_passes = check_list_or_lambda(oslist, self.getPlatform()) |
| 668 | compiler_passes = check_list_or_lambda(self.getCompiler(), compiler) and self.expectedCompilerVersion(compiler_version) |
Zachary Turner | abdb839 | 2015-11-16 22:40:30 +0000 | [diff] [blame] | 669 | arch_passes = self.expectedArch(archs) |
| 670 | triple_passes = triple is None or re.match(triple, lldb.DBG.GetSelectedPlatform().GetTriple()) |
Siva Chandra | 7dcad31 | 2015-11-20 20:30:36 +0000 | [diff] [blame] | 671 | debug_info_passes = check_list_or_lambda(debug_info, self.debug_info) |
Zachary Turner | abdb839 | 2015-11-16 22:40:30 +0000 | [diff] [blame] | 672 | swig_version_passes = (swig_version is None) or (not hasattr(lldb, 'swig_version')) or (check_expected_version(swig_version[0], swig_version[1], lldb.swig_version)) |
| 673 | py_version_passes = (py_version is None) or check_expected_version(py_version[0], py_version[1], sys.version_info) |
| 674 | |
| 675 | return (oslist_passes and |
| 676 | compiler_passes and |
| 677 | arch_passes and |
| 678 | triple_passes and |
| 679 | debug_info_passes and |
| 680 | swig_version_passes and |
| 681 | py_version_passes) |
Ying Chen | 7091c2c | 2015-04-21 01:15:47 +0000 | [diff] [blame] | 682 | return expectedFailure(fn, bugnumber) |
| 683 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 684 | def expectedFailureDwarf(bugnumber=None): |
Tamas Berghammer | 4c0c7a7 | 2015-10-07 10:02:17 +0000 | [diff] [blame] | 685 | return expectedFailureAll(bugnumber=bugnumber, debug_info="dwarf") |
| 686 | |
| 687 | def expectedFailureDwo(bugnumber=None): |
| 688 | return expectedFailureAll(bugnumber=bugnumber, debug_info="dwo") |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 689 | |
| 690 | def expectedFailureDsym(bugnumber=None): |
Tamas Berghammer | 4c0c7a7 | 2015-10-07 10:02:17 +0000 | [diff] [blame] | 691 | return expectedFailureAll(bugnumber=bugnumber, debug_info="dsym") |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 692 | |
| 693 | def expectedFailureCompiler(compiler, compiler_version=None, bugnumber=None): |
| 694 | if compiler_version is None: |
| 695 | compiler_version=['=', None] |
| 696 | return expectedFailureAll(bugnumber=bugnumber, compiler=compiler, compiler_version=compiler_version) |
| 697 | |
Vince Harron | 8974ce2 | 2015-03-13 19:54:54 +0000 | [diff] [blame] | 698 | # to XFAIL a specific clang versions, try this |
| 699 | # @expectedFailureClang('bugnumber', ['<=', '3.4']) |
| 700 | def expectedFailureClang(bugnumber=None, compiler_version=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 701 | return expectedFailureCompiler('clang', compiler_version, bugnumber) |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 702 | |
| 703 | def expectedFailureGcc(bugnumber=None, compiler_version=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 704 | return expectedFailureCompiler('gcc', compiler_version, bugnumber) |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 705 | |
Matt Kopec | 0de53f0 | 2013-03-15 19:10:12 +0000 | [diff] [blame] | 706 | def expectedFailureIcc(bugnumber=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 707 | return expectedFailureCompiler('icc', None, bugnumber) |
Matt Kopec | 0de53f0 | 2013-03-15 19:10:12 +0000 | [diff] [blame] | 708 | |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 709 | def expectedFailureArch(arch, bugnumber=None): |
| 710 | def fn(self): |
| 711 | return arch in self.getArchitecture() |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 712 | return expectedFailure(fn, bugnumber) |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 713 | |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 714 | def expectedFailurei386(bugnumber=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 715 | return expectedFailureArch('i386', bugnumber) |
Johnny Chen | a33843f | 2011-12-22 21:14:31 +0000 | [diff] [blame] | 716 | |
Matt Kopec | ee969f9 | 2013-09-26 23:30:59 +0000 | [diff] [blame] | 717 | def expectedFailurex86_64(bugnumber=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 718 | return expectedFailureArch('x86_64', bugnumber) |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 719 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 720 | def expectedFailureOS(oslist, bugnumber=None, compilers=None, debug_info=None): |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 721 | def fn(self): |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 722 | return (self.getPlatform() in oslist and |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 723 | self.expectedCompiler(compilers) and |
| 724 | (debug_info is None or self.debug_info in debug_info)) |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 725 | return expectedFailure(fn, bugnumber) |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 726 | |
Chaoren Lin | f7160f3 | 2015-06-09 17:39:27 +0000 | [diff] [blame] | 727 | def expectedFailureHostOS(oslist, bugnumber=None, compilers=None): |
| 728 | def fn(self): |
| 729 | return (getHostPlatform() in oslist and |
| 730 | self.expectedCompiler(compilers)) |
| 731 | return expectedFailure(fn, bugnumber) |
| 732 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 733 | def expectedFailureDarwin(bugnumber=None, compilers=None, debug_info=None): |
Robert Flack | efa49c2 | 2015-03-26 19:34:26 +0000 | [diff] [blame] | 734 | # For legacy reasons, we support both "darwin" and "macosx" as OS X triples. |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 735 | return expectedFailureOS(getDarwinOSTriples(), bugnumber, compilers, debug_info=debug_info) |
Matt Kopec | ee969f9 | 2013-09-26 23:30:59 +0000 | [diff] [blame] | 736 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 737 | def expectedFailureFreeBSD(bugnumber=None, compilers=None, debug_info=None): |
| 738 | return expectedFailureOS(['freebsd'], bugnumber, compilers, debug_info=debug_info) |
Ed Maste | 24a7f7d | 2013-07-24 19:47:08 +0000 | [diff] [blame] | 739 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 740 | def expectedFailureLinux(bugnumber=None, compilers=None, debug_info=None): |
| 741 | return expectedFailureOS(['linux'], bugnumber, compilers, debug_info=debug_info) |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 742 | |
Kamil Rytarowski | 49f9fb8 | 2015-12-07 21:25:57 +0000 | [diff] [blame] | 743 | def expectedFailureNetBSD(bugnumber=None, compilers=None, debug_info=None): |
| 744 | return expectedFailureOS(['netbsd'], bugnumber, compilers, debug_info=debug_info) |
| 745 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 746 | def expectedFailureWindows(bugnumber=None, compilers=None, debug_info=None): |
| 747 | return expectedFailureOS(['windows'], bugnumber, compilers, debug_info=debug_info) |
Zachary Turner | 80c2c60 | 2014-12-09 19:28:00 +0000 | [diff] [blame] | 748 | |
Chaoren Lin | f7160f3 | 2015-06-09 17:39:27 +0000 | [diff] [blame] | 749 | def expectedFailureHostWindows(bugnumber=None, compilers=None): |
| 750 | return expectedFailureHostOS(['windows'], bugnumber, compilers) |
| 751 | |
Pavel Labath | 090152b | 2015-08-20 11:37:19 +0000 | [diff] [blame] | 752 | def matchAndroid(api_levels=None, archs=None): |
| 753 | def match(self): |
| 754 | if not target_is_android(): |
| 755 | return False |
| 756 | if archs is not None and self.getArchitecture() not in archs: |
| 757 | return False |
| 758 | if api_levels is not None and android_device_api() not in api_levels: |
| 759 | return False |
| 760 | return True |
| 761 | return match |
| 762 | |
| 763 | |
Tamas Berghammer | 050d1e8 | 2015-07-22 11:00:06 +0000 | [diff] [blame] | 764 | def expectedFailureAndroid(bugnumber=None, api_levels=None, archs=None): |
Siva Chandra | 8af9166 | 2015-06-05 00:22:49 +0000 | [diff] [blame] | 765 | """ Mark a test as xfail for Android. |
| 766 | |
| 767 | Arguments: |
| 768 | bugnumber - The LLVM pr associated with the problem. |
| 769 | api_levels - A sequence of numbers specifying the Android API levels |
Tamas Berghammer | 050d1e8 | 2015-07-22 11:00:06 +0000 | [diff] [blame] | 770 | for which a test is expected to fail. None means all API level. |
| 771 | arch - A sequence of architecture names specifying the architectures |
| 772 | for which a test is expected to fail. None means all architectures. |
Siva Chandra | 8af9166 | 2015-06-05 00:22:49 +0000 | [diff] [blame] | 773 | """ |
Pavel Labath | 090152b | 2015-08-20 11:37:19 +0000 | [diff] [blame] | 774 | return expectedFailure(matchAndroid(api_levels, archs), bugnumber) |
Pavel Labath | 674bc7b | 2015-05-29 14:54:46 +0000 | [diff] [blame] | 775 | |
Vince Harron | 7ac3ea4 | 2015-06-26 15:13:21 +0000 | [diff] [blame] | 776 | # if the test passes on the first try, we're done (success) |
| 777 | # if the test fails once, then passes on the second try, raise an ExpectedFailure |
| 778 | # if the test fails twice in a row, re-throw the exception from the second test run |
| 779 | def expectedFlakey(expected_fn, bugnumber=None): |
| 780 | def expectedFailure_impl(func): |
| 781 | @wraps(func) |
| 782 | def wrapper(*args, **kwargs): |
| 783 | from unittest2 import case |
| 784 | self = args[0] |
| 785 | try: |
| 786 | func(*args, **kwargs) |
Ying Chen | 0a7202b | 2015-07-01 22:44:27 +0000 | [diff] [blame] | 787 | # don't retry if the test case is already decorated with xfail or skip |
| 788 | except (case._ExpectedFailure, case.SkipTest, case._UnexpectedSuccess): |
| 789 | raise |
Vince Harron | 7ac3ea4 | 2015-06-26 15:13:21 +0000 | [diff] [blame] | 790 | except Exception: |
| 791 | if expected_fn(self): |
Ying Chen | 0a7202b | 2015-07-01 22:44:27 +0000 | [diff] [blame] | 792 | # before retry, run tearDown for previous run and setup for next |
Vince Harron | 7ac3ea4 | 2015-06-26 15:13:21 +0000 | [diff] [blame] | 793 | try: |
Ying Chen | 0a7202b | 2015-07-01 22:44:27 +0000 | [diff] [blame] | 794 | self.tearDown() |
| 795 | self.setUp() |
Vince Harron | 7ac3ea4 | 2015-06-26 15:13:21 +0000 | [diff] [blame] | 796 | func(*args, **kwargs) |
| 797 | except Exception: |
| 798 | # oh snap! two failures in a row, record a failure/error |
| 799 | raise |
| 800 | # record the expected failure |
| 801 | raise case._ExpectedFailure(sys.exc_info(), bugnumber) |
| 802 | else: |
| 803 | raise |
| 804 | return wrapper |
| 805 | # if bugnumber is not-callable(incluing None), that means decorator function is called with optional arguments |
| 806 | # return decorator in this case, so it will be used to decorating original method |
Zachary Turner | cd236b8 | 2015-10-26 18:48:24 +0000 | [diff] [blame] | 807 | if six.callable(bugnumber): |
Vince Harron | 7ac3ea4 | 2015-06-26 15:13:21 +0000 | [diff] [blame] | 808 | return expectedFailure_impl(bugnumber) |
| 809 | else: |
| 810 | return expectedFailure_impl |
| 811 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 812 | def expectedFlakeyDwarf(bugnumber=None): |
| 813 | def fn(self): |
| 814 | return self.debug_info == "dwarf" |
| 815 | return expectedFlakey(fn, bugnumber) |
| 816 | |
| 817 | def expectedFlakeyDsym(bugnumber=None): |
| 818 | def fn(self): |
| 819 | return self.debug_info == "dwarf" |
| 820 | return expectedFlakey(fn, bugnumber) |
| 821 | |
Vince Harron | 7ac3ea4 | 2015-06-26 15:13:21 +0000 | [diff] [blame] | 822 | def expectedFlakeyOS(oslist, bugnumber=None, compilers=None): |
| 823 | def fn(self): |
| 824 | return (self.getPlatform() in oslist and |
| 825 | self.expectedCompiler(compilers)) |
| 826 | return expectedFlakey(fn, bugnumber) |
| 827 | |
| 828 | def expectedFlakeyDarwin(bugnumber=None, compilers=None): |
| 829 | # For legacy reasons, we support both "darwin" and "macosx" as OS X triples. |
| 830 | return expectedFlakeyOS(getDarwinOSTriples(), bugnumber, compilers) |
| 831 | |
Kamil Rytarowski | 49f9fb8 | 2015-12-07 21:25:57 +0000 | [diff] [blame] | 832 | def expectedFlakeyFreeBSD(bugnumber=None, compilers=None): |
| 833 | return expectedFlakeyOS(['freebsd'], bugnumber, compilers) |
| 834 | |
Vince Harron | 7ac3ea4 | 2015-06-26 15:13:21 +0000 | [diff] [blame] | 835 | def expectedFlakeyLinux(bugnumber=None, compilers=None): |
| 836 | return expectedFlakeyOS(['linux'], bugnumber, compilers) |
| 837 | |
Kamil Rytarowski | 49f9fb8 | 2015-12-07 21:25:57 +0000 | [diff] [blame] | 838 | def expectedFlakeyNetBSD(bugnumber=None, compilers=None): |
| 839 | return expectedFlakeyOS(['netbsd'], bugnumber, compilers) |
Vince Harron | 7ac3ea4 | 2015-06-26 15:13:21 +0000 | [diff] [blame] | 840 | |
| 841 | def expectedFlakeyCompiler(compiler, compiler_version=None, bugnumber=None): |
| 842 | if compiler_version is None: |
| 843 | compiler_version=['=', None] |
| 844 | def fn(self): |
| 845 | return compiler in self.getCompiler() and self.expectedCompilerVersion(compiler_version) |
| 846 | return expectedFlakey(fn, bugnumber) |
| 847 | |
| 848 | # @expectedFlakeyClang('bugnumber', ['<=', '3.4']) |
| 849 | def expectedFlakeyClang(bugnumber=None, compiler_version=None): |
| 850 | return expectedFlakeyCompiler('clang', compiler_version, bugnumber) |
| 851 | |
| 852 | # @expectedFlakeyGcc('bugnumber', ['<=', '3.4']) |
| 853 | def expectedFlakeyGcc(bugnumber=None, compiler_version=None): |
| 854 | return expectedFlakeyCompiler('gcc', compiler_version, bugnumber) |
| 855 | |
Pavel Labath | 63a579c | 2015-09-07 12:15:27 +0000 | [diff] [blame] | 856 | def expectedFlakeyAndroid(bugnumber=None, api_levels=None, archs=None): |
| 857 | return expectedFlakey(matchAndroid(api_levels, archs), bugnumber) |
| 858 | |
Greg Clayton | 1251456 | 2013-12-05 22:22:32 +0000 | [diff] [blame] | 859 | def skipIfRemote(func): |
| 860 | """Decorate the item to skip tests if testing remotely.""" |
| 861 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 862 | raise Exception("@skipIfRemote can only be used to decorate a test method") |
| 863 | @wraps(func) |
| 864 | def wrapper(*args, **kwargs): |
| 865 | from unittest2 import case |
| 866 | if lldb.remote_platform: |
| 867 | self = args[0] |
| 868 | self.skipTest("skip on remote platform") |
| 869 | else: |
| 870 | func(*args, **kwargs) |
| 871 | return wrapper |
| 872 | |
Siva Chandra | 4470f38 | 2015-06-17 22:32:27 +0000 | [diff] [blame] | 873 | def skipUnlessListedRemote(remote_list=None): |
| 874 | def myImpl(func): |
| 875 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 876 | raise Exception("@skipIfRemote can only be used to decorate a " |
| 877 | "test method") |
| 878 | |
| 879 | @wraps(func) |
| 880 | def wrapper(*args, **kwargs): |
| 881 | if remote_list and lldb.remote_platform: |
| 882 | self = args[0] |
| 883 | triple = self.dbg.GetSelectedPlatform().GetTriple() |
| 884 | for r in remote_list: |
| 885 | if r in triple: |
| 886 | func(*args, **kwargs) |
| 887 | return |
| 888 | self.skipTest("skip on remote platform %s" % str(triple)) |
| 889 | else: |
| 890 | func(*args, **kwargs) |
| 891 | return wrapper |
| 892 | |
| 893 | return myImpl |
| 894 | |
Greg Clayton | 1251456 | 2013-12-05 22:22:32 +0000 | [diff] [blame] | 895 | def skipIfRemoteDueToDeadlock(func): |
| 896 | """Decorate the item to skip tests if testing remotely due to the test deadlocking.""" |
| 897 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 898 | raise Exception("@skipIfRemote can only be used to decorate a test method") |
| 899 | @wraps(func) |
| 900 | def wrapper(*args, **kwargs): |
| 901 | from unittest2 import case |
| 902 | if lldb.remote_platform: |
| 903 | self = args[0] |
| 904 | self.skipTest("skip on remote platform (deadlocks)") |
| 905 | else: |
| 906 | func(*args, **kwargs) |
| 907 | return wrapper |
| 908 | |
Enrico Granata | b633e43 | 2014-10-06 21:37:06 +0000 | [diff] [blame] | 909 | def skipIfNoSBHeaders(func): |
| 910 | """Decorate the item to mark tests that should be skipped when LLDB is built with no SB API headers.""" |
| 911 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
Ed Maste | 59cca5d | 2014-10-07 01:57:52 +0000 | [diff] [blame] | 912 | raise Exception("@skipIfNoSBHeaders can only be used to decorate a test method") |
Enrico Granata | b633e43 | 2014-10-06 21:37:06 +0000 | [diff] [blame] | 913 | @wraps(func) |
| 914 | def wrapper(*args, **kwargs): |
| 915 | from unittest2 import case |
| 916 | self = args[0] |
Shawn Best | 181b09b | 2014-11-08 00:04:04 +0000 | [diff] [blame] | 917 | if sys.platform.startswith("darwin"): |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 918 | header = os.path.join(os.environ["LLDB_LIB_DIR"], 'LLDB.framework', 'Versions','Current','Headers','LLDB.h') |
Shawn Best | 181b09b | 2014-11-08 00:04:04 +0000 | [diff] [blame] | 919 | else: |
| 920 | header = os.path.join(os.environ["LLDB_SRC"], "include", "lldb", "API", "LLDB.h") |
Enrico Granata | b633e43 | 2014-10-06 21:37:06 +0000 | [diff] [blame] | 921 | platform = sys.platform |
Enrico Granata | b633e43 | 2014-10-06 21:37:06 +0000 | [diff] [blame] | 922 | if not os.path.exists(header): |
| 923 | self.skipTest("skip because LLDB.h header not found") |
| 924 | else: |
| 925 | func(*args, **kwargs) |
| 926 | return wrapper |
| 927 | |
Enrico Granata | 5f92a13 | 2015-11-05 00:46:25 +0000 | [diff] [blame] | 928 | def skipIfiOSSimulator(func): |
| 929 | """Decorate the item to skip tests that should be skipped on the iOS Simulator.""" |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 930 | return unittest2.skipIf(configuration.lldb_platform_name == 'ios-simulator', 'skip on the iOS Simulator')(func) |
Enrico Granata | 5f92a13 | 2015-11-05 00:46:25 +0000 | [diff] [blame] | 931 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 932 | def skipIfFreeBSD(func): |
| 933 | """Decorate the item to skip tests that should be skipped on FreeBSD.""" |
| 934 | return skipIfPlatform(["freebsd"])(func) |
Zachary Turner | c782652 | 2014-08-13 17:44:53 +0000 | [diff] [blame] | 935 | |
Kamil Rytarowski | 49f9fb8 | 2015-12-07 21:25:57 +0000 | [diff] [blame] | 936 | def skipIfNetBSD(func): |
| 937 | """Decorate the item to skip tests that should be skipped on NetBSD.""" |
| 938 | return skipIfPlatform(["netbsd"])(func) |
| 939 | |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 940 | def getDarwinOSTriples(): |
| 941 | return ['darwin', 'macosx', 'ios'] |
| 942 | |
Daniel Malea | b3d41a2 | 2013-07-09 00:08:01 +0000 | [diff] [blame] | 943 | def skipIfDarwin(func): |
| 944 | """Decorate the item to skip tests that should be skipped on Darwin.""" |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 945 | return skipIfPlatform(getDarwinOSTriples())(func) |
Daniel Malea | b3d41a2 | 2013-07-09 00:08:01 +0000 | [diff] [blame] | 946 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 947 | def skipIfLinux(func): |
| 948 | """Decorate the item to skip tests that should be skipped on Linux.""" |
| 949 | return skipIfPlatform(["linux"])(func) |
| 950 | |
Oleksiy Vyalov | abb5a35 | 2015-07-29 22:18:16 +0000 | [diff] [blame] | 951 | def skipUnlessHostLinux(func): |
| 952 | """Decorate the item to skip tests that should be skipped on any non Linux host.""" |
| 953 | return skipUnlessHostPlatform(["linux"])(func) |
| 954 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 955 | def skipIfWindows(func): |
| 956 | """Decorate the item to skip tests that should be skipped on Windows.""" |
| 957 | return skipIfPlatform(["windows"])(func) |
| 958 | |
Chaoren Lin | e6eea5d | 2015-06-08 22:13:28 +0000 | [diff] [blame] | 959 | def skipIfHostWindows(func): |
| 960 | """Decorate the item to skip tests that should be skipped on Windows.""" |
| 961 | return skipIfHostPlatform(["windows"])(func) |
| 962 | |
Adrian McCarthy | d9dbae5 | 2015-09-16 18:17:11 +0000 | [diff] [blame] | 963 | def skipUnlessWindows(func): |
| 964 | """Decorate the item to skip tests that should be skipped on any non-Windows platform.""" |
| 965 | return skipUnlessPlatform(["windows"])(func) |
| 966 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 967 | def skipUnlessDarwin(func): |
| 968 | """Decorate the item to skip tests that should be skipped on any non Darwin platform.""" |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 969 | return skipUnlessPlatform(getDarwinOSTriples())(func) |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 970 | |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 971 | def skipUnlessGoInstalled(func): |
| 972 | """Decorate the item to skip tests when no Go compiler is available.""" |
| 973 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 974 | raise Exception("@skipIfGcc can only be used to decorate a test method") |
| 975 | @wraps(func) |
| 976 | def wrapper(*args, **kwargs): |
| 977 | from unittest2 import case |
| 978 | self = args[0] |
| 979 | compiler = self.getGoCompilerVersion() |
| 980 | if not compiler: |
| 981 | self.skipTest("skipping because go compiler not found") |
| 982 | else: |
Todd Fiala | be5dfc5 | 2015-10-06 19:15:56 +0000 | [diff] [blame] | 983 | # Ensure the version is the minimum version supported by |
Todd Fiala | 02c08d0 | 2015-10-06 22:14:33 +0000 | [diff] [blame] | 984 | # the LLDB go support. |
Todd Fiala | be5dfc5 | 2015-10-06 19:15:56 +0000 | [diff] [blame] | 985 | match_version = re.search(r"(\d+\.\d+(\.\d+)?)", compiler) |
| 986 | if not match_version: |
| 987 | # Couldn't determine version. |
| 988 | self.skipTest( |
| 989 | "skipping because go version could not be parsed " |
| 990 | "out of {}".format(compiler)) |
| 991 | else: |
| 992 | from distutils.version import StrictVersion |
Todd Fiala | 02c08d0 | 2015-10-06 22:14:33 +0000 | [diff] [blame] | 993 | min_strict_version = StrictVersion("1.4.0") |
Todd Fiala | be5dfc5 | 2015-10-06 19:15:56 +0000 | [diff] [blame] | 994 | compiler_strict_version = StrictVersion(match_version.group(1)) |
| 995 | if compiler_strict_version < min_strict_version: |
| 996 | self.skipTest( |
| 997 | "skipping because available go version ({}) does " |
Todd Fiala | 02c08d0 | 2015-10-06 22:14:33 +0000 | [diff] [blame] | 998 | "not meet minimum required go version ({})".format( |
Todd Fiala | be5dfc5 | 2015-10-06 19:15:56 +0000 | [diff] [blame] | 999 | compiler_strict_version, |
| 1000 | min_strict_version)) |
Todd Fiala | 9f23680 | 2015-10-06 19:23:22 +0000 | [diff] [blame] | 1001 | func(*args, **kwargs) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1002 | return wrapper |
| 1003 | |
Robert Flack | 068898c | 2015-04-09 18:07:58 +0000 | [diff] [blame] | 1004 | def getPlatform(): |
Robert Flack | 6e1fd35 | 2015-05-15 12:39:33 +0000 | [diff] [blame] | 1005 | """Returns the target platform which the tests are running on.""" |
Robert Flack | 068898c | 2015-04-09 18:07:58 +0000 | [diff] [blame] | 1006 | platform = lldb.DBG.GetSelectedPlatform().GetTriple().split('-')[2] |
| 1007 | if platform.startswith('freebsd'): |
| 1008 | platform = 'freebsd' |
Kamil Rytarowski | 49f9fb8 | 2015-12-07 21:25:57 +0000 | [diff] [blame] | 1009 | elif platform.startswith('netbsd'): |
| 1010 | platform = 'netbsd' |
Robert Flack | 068898c | 2015-04-09 18:07:58 +0000 | [diff] [blame] | 1011 | return platform |
| 1012 | |
Robert Flack | 6e1fd35 | 2015-05-15 12:39:33 +0000 | [diff] [blame] | 1013 | def getHostPlatform(): |
| 1014 | """Returns the host platform running the test suite.""" |
| 1015 | # Attempts to return a platform name matching a target Triple platform. |
| 1016 | if sys.platform.startswith('linux'): |
| 1017 | return 'linux' |
| 1018 | elif sys.platform.startswith('win32'): |
| 1019 | return 'windows' |
| 1020 | elif sys.platform.startswith('darwin'): |
| 1021 | return 'darwin' |
| 1022 | elif sys.platform.startswith('freebsd'): |
| 1023 | return 'freebsd' |
Kamil Rytarowski | 49f9fb8 | 2015-12-07 21:25:57 +0000 | [diff] [blame] | 1024 | elif sys.platform.startswith('netbsd'): |
| 1025 | return 'netbsd' |
Robert Flack | 6e1fd35 | 2015-05-15 12:39:33 +0000 | [diff] [blame] | 1026 | else: |
| 1027 | return sys.platform |
| 1028 | |
Robert Flack | fb2f6c6 | 2015-04-17 08:02:18 +0000 | [diff] [blame] | 1029 | def platformIsDarwin(): |
| 1030 | """Returns true if the OS triple for the selected platform is any valid apple OS""" |
| 1031 | return getPlatform() in getDarwinOSTriples() |
| 1032 | |
Robert Flack | 6e1fd35 | 2015-05-15 12:39:33 +0000 | [diff] [blame] | 1033 | def skipIfHostIncompatibleWithRemote(func): |
| 1034 | """Decorate the item to skip tests if binaries built on this host are incompatible.""" |
| 1035 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 1036 | raise Exception("@skipIfHostIncompatibleWithRemote can only be used to decorate a test method") |
| 1037 | @wraps(func) |
| 1038 | def wrapper(*args, **kwargs): |
| 1039 | from unittest2 import case |
| 1040 | self = args[0] |
| 1041 | host_arch = self.getLldbArchitecture() |
| 1042 | host_platform = getHostPlatform() |
| 1043 | target_arch = self.getArchitecture() |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 1044 | target_platform = 'darwin' if self.platformIsDarwin() else self.getPlatform() |
Robert Flack | 6e1fd35 | 2015-05-15 12:39:33 +0000 | [diff] [blame] | 1045 | if not (target_arch == 'x86_64' and host_arch == 'i386') and host_arch != target_arch: |
| 1046 | self.skipTest("skipping because target %s is not compatible with host architecture %s" % (target_arch, host_arch)) |
| 1047 | elif target_platform != host_platform: |
| 1048 | self.skipTest("skipping because target is %s but host is %s" % (target_platform, host_platform)) |
| 1049 | else: |
| 1050 | func(*args, **kwargs) |
| 1051 | return wrapper |
| 1052 | |
Chaoren Lin | e6eea5d | 2015-06-08 22:13:28 +0000 | [diff] [blame] | 1053 | def skipIfHostPlatform(oslist): |
| 1054 | """Decorate the item to skip tests if running on one of the listed host platforms.""" |
| 1055 | return unittest2.skipIf(getHostPlatform() in oslist, |
| 1056 | "skip on %s" % (", ".join(oslist))) |
| 1057 | |
| 1058 | def skipUnlessHostPlatform(oslist): |
| 1059 | """Decorate the item to skip tests unless running on one of the listed host platforms.""" |
| 1060 | return unittest2.skipUnless(getHostPlatform() in oslist, |
| 1061 | "requires on of %s" % (", ".join(oslist))) |
| 1062 | |
Zachary Turner | 793d997 | 2015-08-14 23:29:24 +0000 | [diff] [blame] | 1063 | def skipUnlessArch(archlist): |
| 1064 | """Decorate the item to skip tests unless running on one of the listed architectures.""" |
| 1065 | def myImpl(func): |
| 1066 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 1067 | raise Exception("@skipUnlessArch can only be used to decorate a test method") |
| 1068 | |
| 1069 | @wraps(func) |
| 1070 | def wrapper(*args, **kwargs): |
| 1071 | self = args[0] |
| 1072 | if self.getArchitecture() not in archlist: |
| 1073 | self.skipTest("skipping for architecture %s (requires one of %s)" % |
| 1074 | (self.getArchitecture(), ", ".join(archlist))) |
| 1075 | else: |
| 1076 | func(*args, **kwargs) |
| 1077 | return wrapper |
| 1078 | |
| 1079 | return myImpl |
| 1080 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 1081 | def skipIfPlatform(oslist): |
| 1082 | """Decorate the item to skip tests if running on one of the listed platforms.""" |
Robert Flack | 068898c | 2015-04-09 18:07:58 +0000 | [diff] [blame] | 1083 | return unittest2.skipIf(getPlatform() in oslist, |
| 1084 | "skip on %s" % (", ".join(oslist))) |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 1085 | |
| 1086 | def skipUnlessPlatform(oslist): |
| 1087 | """Decorate the item to skip tests unless running on one of the listed platforms.""" |
Robert Flack | 068898c | 2015-04-09 18:07:58 +0000 | [diff] [blame] | 1088 | return unittest2.skipUnless(getPlatform() in oslist, |
| 1089 | "requires on of %s" % (", ".join(oslist))) |
Daniel Malea | b3d41a2 | 2013-07-09 00:08:01 +0000 | [diff] [blame] | 1090 | |
Daniel Malea | 4835990 | 2013-05-14 20:48:54 +0000 | [diff] [blame] | 1091 | def skipIfLinuxClang(func): |
| 1092 | """Decorate the item to skip tests that should be skipped if building on |
| 1093 | Linux with clang. |
| 1094 | """ |
| 1095 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 1096 | raise Exception("@skipIfLinuxClang can only be used to decorate a test method") |
| 1097 | @wraps(func) |
| 1098 | def wrapper(*args, **kwargs): |
| 1099 | from unittest2 import case |
| 1100 | self = args[0] |
| 1101 | compiler = self.getCompiler() |
Vince Harron | c849267 | 2015-05-04 02:59:19 +0000 | [diff] [blame] | 1102 | platform = self.getPlatform() |
| 1103 | if "clang" in compiler and platform == "linux": |
Daniel Malea | 4835990 | 2013-05-14 20:48:54 +0000 | [diff] [blame] | 1104 | self.skipTest("skipping because Clang is used on Linux") |
| 1105 | else: |
| 1106 | func(*args, **kwargs) |
| 1107 | return wrapper |
| 1108 | |
Ying Chen | 7091c2c | 2015-04-21 01:15:47 +0000 | [diff] [blame] | 1109 | # provide a function to skip on defined oslist, compiler version, and archs |
| 1110 | # if none is specified for any argument, that argument won't be checked and thus means for all |
| 1111 | # for example, |
| 1112 | # @skipIf, skip for all platform/compiler/arch, |
| 1113 | # @skipIf(compiler='gcc'), skip for gcc on all platform/architecture |
| 1114 | # @skipIf(bugnumber, ["linux"], "gcc", ['>=', '4.9'], ['i386']), skip for gcc>=4.9 on linux with i386 |
| 1115 | |
| 1116 | # TODO: refactor current code, to make skipIfxxx functions to call this function |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 1117 | def skipIf(bugnumber=None, oslist=None, compiler=None, compiler_version=None, archs=None, debug_info=None, swig_version=None, py_version=None, remote=None): |
Ying Chen | 7091c2c | 2015-04-21 01:15:47 +0000 | [diff] [blame] | 1118 | def fn(self): |
Siva Chandra | 7dcad31 | 2015-11-20 20:30:36 +0000 | [diff] [blame] | 1119 | oslist_passes = oslist is None or self.getPlatform() in oslist |
| 1120 | compiler_passes = compiler is None or (compiler in self.getCompiler() and self.expectedCompilerVersion(compiler_version)) |
Zachary Turner | abdb839 | 2015-11-16 22:40:30 +0000 | [diff] [blame] | 1121 | arch_passes = self.expectedArch(archs) |
Siva Chandra | 7dcad31 | 2015-11-20 20:30:36 +0000 | [diff] [blame] | 1122 | debug_info_passes = debug_info is None or self.debug_info in debug_info |
Zachary Turner | abdb839 | 2015-11-16 22:40:30 +0000 | [diff] [blame] | 1123 | swig_version_passes = (swig_version is None) or (not hasattr(lldb, 'swig_version')) or (check_expected_version(swig_version[0], swig_version[1], lldb.swig_version)) |
| 1124 | py_version_passes = (py_version is None) or check_expected_version(py_version[0], py_version[1], sys.version_info) |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 1125 | remote_passes = (remote is None) or (remote == (lldb.remote_platform is not None)) |
Zachary Turner | abdb839 | 2015-11-16 22:40:30 +0000 | [diff] [blame] | 1126 | |
| 1127 | return (oslist_passes and |
| 1128 | compiler_passes and |
| 1129 | arch_passes and |
| 1130 | debug_info_passes and |
| 1131 | swig_version_passes and |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 1132 | py_version_passes and |
| 1133 | remote_passes) |
Zachary Turner | ba10570 | 2015-11-16 23:58:20 +0000 | [diff] [blame] | 1134 | |
| 1135 | local_vars = locals() |
| 1136 | args = [x for x in inspect.getargspec(skipIf).args] |
| 1137 | arg_vals = [eval(x, globals(), local_vars) for x in args] |
| 1138 | args = [x for x in zip(args, arg_vals) if x[1] is not None] |
| 1139 | reasons = ['%s=%s' % (x, str(y)) for (x,y) in args] |
| 1140 | return skipTestIfFn(fn, bugnumber, skipReason='skipping because ' + ' && '.join(reasons)) |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 1141 | |
| 1142 | def skipIfDebugInfo(bugnumber=None, debug_info=None): |
| 1143 | return skipIf(bugnumber=bugnumber, debug_info=debug_info) |
| 1144 | |
Greg Clayton | edea237 | 2015-10-07 20:01:13 +0000 | [diff] [blame] | 1145 | def skipIfDWO(bugnumber=None): |
| 1146 | return skipIfDebugInfo(bugnumber, ["dwo"]) |
| 1147 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 1148 | def skipIfDwarf(bugnumber=None): |
| 1149 | return skipIfDebugInfo(bugnumber, ["dwarf"]) |
| 1150 | |
| 1151 | def skipIfDsym(bugnumber=None): |
| 1152 | return skipIfDebugInfo(bugnumber, ["dsym"]) |
Ying Chen | 7091c2c | 2015-04-21 01:15:47 +0000 | [diff] [blame] | 1153 | |
| 1154 | def skipTestIfFn(expected_fn, bugnumber=None, skipReason=None): |
| 1155 | def skipTestIfFn_impl(func): |
| 1156 | @wraps(func) |
| 1157 | def wrapper(*args, **kwargs): |
| 1158 | from unittest2 import case |
| 1159 | self = args[0] |
| 1160 | if expected_fn(self): |
| 1161 | self.skipTest(skipReason) |
| 1162 | else: |
| 1163 | func(*args, **kwargs) |
| 1164 | return wrapper |
Zachary Turner | cd236b8 | 2015-10-26 18:48:24 +0000 | [diff] [blame] | 1165 | if six.callable(bugnumber): |
Ying Chen | 7091c2c | 2015-04-21 01:15:47 +0000 | [diff] [blame] | 1166 | return skipTestIfFn_impl(bugnumber) |
| 1167 | else: |
| 1168 | return skipTestIfFn_impl |
| 1169 | |
Daniel Malea | be23079 | 2013-01-24 23:52:09 +0000 | [diff] [blame] | 1170 | def skipIfGcc(func): |
| 1171 | """Decorate the item to skip tests that should be skipped if building with gcc .""" |
| 1172 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 1173 | raise Exception("@skipIfGcc can only be used to decorate a test method") |
Daniel Malea | be23079 | 2013-01-24 23:52:09 +0000 | [diff] [blame] | 1174 | @wraps(func) |
| 1175 | def wrapper(*args, **kwargs): |
| 1176 | from unittest2 import case |
| 1177 | self = args[0] |
| 1178 | compiler = self.getCompiler() |
| 1179 | if "gcc" in compiler: |
| 1180 | self.skipTest("skipping because gcc is the test compiler") |
| 1181 | else: |
| 1182 | func(*args, **kwargs) |
| 1183 | return wrapper |
| 1184 | |
Matt Kopec | 0de53f0 | 2013-03-15 19:10:12 +0000 | [diff] [blame] | 1185 | def skipIfIcc(func): |
| 1186 | """Decorate the item to skip tests that should be skipped if building with icc .""" |
| 1187 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 1188 | raise Exception("@skipIfIcc can only be used to decorate a test method") |
| 1189 | @wraps(func) |
| 1190 | def wrapper(*args, **kwargs): |
| 1191 | from unittest2 import case |
| 1192 | self = args[0] |
| 1193 | compiler = self.getCompiler() |
| 1194 | if "icc" in compiler: |
| 1195 | self.skipTest("skipping because icc is the test compiler") |
| 1196 | else: |
| 1197 | func(*args, **kwargs) |
| 1198 | return wrapper |
| 1199 | |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1200 | def skipIfi386(func): |
| 1201 | """Decorate the item to skip tests that should be skipped if building 32-bit.""" |
| 1202 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 1203 | raise Exception("@skipIfi386 can only be used to decorate a test method") |
| 1204 | @wraps(func) |
| 1205 | def wrapper(*args, **kwargs): |
| 1206 | from unittest2 import case |
| 1207 | self = args[0] |
| 1208 | if "i386" == self.getArchitecture(): |
| 1209 | self.skipTest("skipping because i386 is not a supported architecture") |
| 1210 | else: |
| 1211 | func(*args, **kwargs) |
| 1212 | return wrapper |
| 1213 | |
Pavel Labath | 090152b | 2015-08-20 11:37:19 +0000 | [diff] [blame] | 1214 | def skipIfTargetAndroid(api_levels=None, archs=None): |
Siva Chandra | 77f20fc | 2015-06-05 19:54:49 +0000 | [diff] [blame] | 1215 | """Decorator to skip tests when the target is Android. |
| 1216 | |
| 1217 | Arguments: |
| 1218 | api_levels - The API levels for which the test should be skipped. If |
| 1219 | it is None, then the test will be skipped for all API levels. |
Pavel Labath | 090152b | 2015-08-20 11:37:19 +0000 | [diff] [blame] | 1220 | arch - A sequence of architecture names specifying the architectures |
| 1221 | for which a test is skipped. None means all architectures. |
Siva Chandra | 77f20fc | 2015-06-05 19:54:49 +0000 | [diff] [blame] | 1222 | """ |
| 1223 | def myImpl(func): |
| 1224 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 1225 | raise Exception("@skipIfTargetAndroid can only be used to " |
| 1226 | "decorate a test method") |
| 1227 | @wraps(func) |
| 1228 | def wrapper(*args, **kwargs): |
| 1229 | from unittest2 import case |
| 1230 | self = args[0] |
Pavel Labath | 090152b | 2015-08-20 11:37:19 +0000 | [diff] [blame] | 1231 | if matchAndroid(api_levels, archs)(self): |
| 1232 | self.skipTest("skiped on Android target with API %d and architecture %s" % |
| 1233 | (android_device_api(), self.getArchitecture())) |
Tamas Berghammer | 1253a81 | 2015-03-13 10:12:25 +0000 | [diff] [blame] | 1234 | func(*args, **kwargs) |
Siva Chandra | 77f20fc | 2015-06-05 19:54:49 +0000 | [diff] [blame] | 1235 | return wrapper |
| 1236 | return myImpl |
Tamas Berghammer | 1253a81 | 2015-03-13 10:12:25 +0000 | [diff] [blame] | 1237 | |
Ilia K | d995305 | 2015-03-12 07:19:41 +0000 | [diff] [blame] | 1238 | def skipUnlessCompilerRt(func): |
| 1239 | """Decorate the item to skip tests if testing remotely.""" |
| 1240 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 1241 | raise Exception("@skipUnless can only be used to decorate a test method") |
| 1242 | @wraps(func) |
| 1243 | def wrapper(*args, **kwargs): |
| 1244 | from unittest2 import case |
| 1245 | import os.path |
Enrico Granata | 55d99f0 | 2015-11-19 21:45:07 +0000 | [diff] [blame] | 1246 | compilerRtPath = os.path.join(os.path.dirname(__file__), "..", "..", "..", "..", "llvm","projects","compiler-rt") |
| 1247 | print(compilerRtPath) |
Ilia K | d995305 | 2015-03-12 07:19:41 +0000 | [diff] [blame] | 1248 | if not os.path.exists(compilerRtPath): |
| 1249 | self = args[0] |
| 1250 | self.skipTest("skip if compiler-rt not found") |
| 1251 | else: |
| 1252 | func(*args, **kwargs) |
| 1253 | return wrapper |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1254 | |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1255 | class _PlatformContext(object): |
| 1256 | """Value object class which contains platform-specific options.""" |
| 1257 | |
| 1258 | def __init__(self, shlib_environment_var, shlib_prefix, shlib_extension): |
| 1259 | self.shlib_environment_var = shlib_environment_var |
| 1260 | self.shlib_prefix = shlib_prefix |
| 1261 | self.shlib_extension = shlib_extension |
| 1262 | |
| 1263 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 1264 | class Base(unittest2.TestCase): |
Johnny Chen | 8334dad | 2010-10-22 23:15:46 +0000 | [diff] [blame] | 1265 | """ |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 1266 | Abstract base for performing lldb (see TestBase) or other generic tests (see |
| 1267 | BenchBase for one example). lldbtest.Base works with the test driver to |
| 1268 | accomplish things. |
| 1269 | |
Johnny Chen | 8334dad | 2010-10-22 23:15:46 +0000 | [diff] [blame] | 1270 | """ |
Enrico Granata | 5020f95 | 2012-10-24 21:42:49 +0000 | [diff] [blame] | 1271 | |
Enrico Granata | 1918627 | 2012-10-24 21:44:48 +0000 | [diff] [blame] | 1272 | # The concrete subclass should override this attribute. |
| 1273 | mydir = None |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1274 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1275 | # Keep track of the old current working directory. |
| 1276 | oldcwd = None |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1277 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 1278 | @staticmethod |
| 1279 | def compute_mydir(test_file): |
| 1280 | '''Subclasses should call this function to correctly calculate the required "mydir" attribute as follows: |
| 1281 | |
| 1282 | mydir = TestBase.compute_mydir(__file__)''' |
| 1283 | test_dir = os.path.dirname(test_file) |
| 1284 | return test_dir[len(os.environ["LLDB_TEST"])+1:] |
| 1285 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1286 | def TraceOn(self): |
| 1287 | """Returns True if we are in trace mode (tracing detailed test execution).""" |
| 1288 | return traceAlways |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 1289 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1290 | @classmethod |
| 1291 | def setUpClass(cls): |
Johnny Chen | da88434 | 2010-10-01 22:59:49 +0000 | [diff] [blame] | 1292 | """ |
| 1293 | Python unittest framework class setup fixture. |
| 1294 | Do current directory manipulation. |
| 1295 | """ |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 1296 | # Fail fast if 'mydir' attribute is not overridden. |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1297 | if not cls.mydir or len(cls.mydir) == 0: |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 1298 | raise Exception("Subclasses must override the 'mydir' attribute.") |
Enrico Granata | 7e137e3 | 2012-10-24 18:14:21 +0000 | [diff] [blame] | 1299 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1300 | # Save old working directory. |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1301 | cls.oldcwd = os.getcwd() |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1302 | |
| 1303 | # Change current working directory if ${LLDB_TEST} is defined. |
| 1304 | # See also dotest.py which sets up ${LLDB_TEST}. |
| 1305 | if ("LLDB_TEST" in os.environ): |
Vince Harron | 85d1965 | 2015-05-21 19:09:29 +0000 | [diff] [blame] | 1306 | full_dir = os.path.join(os.environ["LLDB_TEST"], cls.mydir) |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1307 | if traceAlways: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1308 | print("Change dir to:", full_dir, file=sys.stderr) |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1309 | os.chdir(os.path.join(os.environ["LLDB_TEST"], cls.mydir)) |
| 1310 | |
Vince Harron | 85d1965 | 2015-05-21 19:09:29 +0000 | [diff] [blame] | 1311 | if debug_confirm_directory_exclusivity: |
Zachary Turner | b48b404 | 2015-05-21 20:16:02 +0000 | [diff] [blame] | 1312 | import lock |
Vince Harron | 85d1965 | 2015-05-21 19:09:29 +0000 | [diff] [blame] | 1313 | cls.dir_lock = lock.Lock(os.path.join(full_dir, ".dirlock")) |
| 1314 | try: |
| 1315 | cls.dir_lock.try_acquire() |
| 1316 | # write the class that owns the lock into the lock file |
| 1317 | cls.dir_lock.handle.write(cls.__name__) |
| 1318 | except IOError as ioerror: |
| 1319 | # nothing else should have this directory lock |
| 1320 | # wait here until we get a lock |
| 1321 | cls.dir_lock.acquire() |
| 1322 | # read the previous owner from the lock file |
| 1323 | lock_id = cls.dir_lock.handle.read() |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1324 | print("LOCK ERROR: {} wants to lock '{}' but it is already locked by '{}'".format(cls.__name__, full_dir, lock_id), file=sys.stderr) |
Vince Harron | 85d1965 | 2015-05-21 19:09:29 +0000 | [diff] [blame] | 1325 | raise ioerror |
| 1326 | |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1327 | # Set platform context. |
Robert Flack | fb2f6c6 | 2015-04-17 08:02:18 +0000 | [diff] [blame] | 1328 | if platformIsDarwin(): |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1329 | cls.platformContext = _PlatformContext('DYLD_LIBRARY_PATH', 'lib', 'dylib') |
Kamil Rytarowski | 49f9fb8 | 2015-12-07 21:25:57 +0000 | [diff] [blame] | 1330 | elif getPlatform() in ("freebsd", "linux", "netbsd"): |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1331 | cls.platformContext = _PlatformContext('LD_LIBRARY_PATH', 'lib', 'so') |
Zachary Turner | be40b2f | 2014-12-02 21:32:44 +0000 | [diff] [blame] | 1332 | else: |
| 1333 | cls.platformContext = None |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1334 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1335 | @classmethod |
| 1336 | def tearDownClass(cls): |
Johnny Chen | da88434 | 2010-10-01 22:59:49 +0000 | [diff] [blame] | 1337 | """ |
| 1338 | Python unittest framework class teardown fixture. |
| 1339 | Do class-wide cleanup. |
| 1340 | """ |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1341 | |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 1342 | if doCleanup and not configuration.skip_build_and_cleanup: |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 1343 | # First, let's do the platform-specific cleanup. |
Peter Collingbourne | 19f48d5 | 2011-06-20 19:06:20 +0000 | [diff] [blame] | 1344 | module = builder_module() |
Zachary Turner | b1490b6 | 2015-08-26 19:44:56 +0000 | [diff] [blame] | 1345 | module.cleanup() |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1346 | |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 1347 | # Subclass might have specific cleanup function defined. |
| 1348 | if getattr(cls, "classCleanup", None): |
| 1349 | if traceAlways: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1350 | print("Call class-specific cleanup function for class:", cls, file=sys.stderr) |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 1351 | try: |
| 1352 | cls.classCleanup() |
| 1353 | except: |
| 1354 | exc_type, exc_value, exc_tb = sys.exc_info() |
| 1355 | traceback.print_exception(exc_type, exc_value, exc_tb) |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1356 | |
Vince Harron | 85d1965 | 2015-05-21 19:09:29 +0000 | [diff] [blame] | 1357 | if debug_confirm_directory_exclusivity: |
| 1358 | cls.dir_lock.release() |
| 1359 | del cls.dir_lock |
| 1360 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1361 | # Restore old working directory. |
| 1362 | if traceAlways: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1363 | print("Restore dir to:", cls.oldcwd, file=sys.stderr) |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1364 | os.chdir(cls.oldcwd) |
| 1365 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 1366 | @classmethod |
| 1367 | def skipLongRunningTest(cls): |
| 1368 | """ |
| 1369 | By default, we skip long running test case. |
| 1370 | This can be overridden by passing '-l' to the test driver (dotest.py). |
| 1371 | """ |
| 1372 | if "LLDB_SKIP_LONG_RUNNING_TEST" in os.environ and "NO" == os.environ["LLDB_SKIP_LONG_RUNNING_TEST"]: |
| 1373 | return False |
| 1374 | else: |
| 1375 | return True |
Johnny Chen | ed49202 | 2011-06-21 00:53:00 +0000 | [diff] [blame] | 1376 | |
Vince Harron | 6d3d0f1 | 2015-05-10 22:01:59 +0000 | [diff] [blame] | 1377 | def enableLogChannelsForCurrentTest(self): |
| 1378 | if len(lldbtest_config.channels) == 0: |
| 1379 | return |
| 1380 | |
| 1381 | # if debug channels are specified in lldbtest_config.channels, |
| 1382 | # create a new set of log files for every test |
| 1383 | log_basename = self.getLogBasenameForCurrentTest() |
| 1384 | |
| 1385 | # confirm that the file is writeable |
| 1386 | host_log_path = "{}-host.log".format(log_basename) |
| 1387 | open(host_log_path, 'w').close() |
| 1388 | |
| 1389 | log_enable = "log enable -Tpn -f {} ".format(host_log_path) |
| 1390 | for channel_with_categories in lldbtest_config.channels: |
| 1391 | channel_then_categories = channel_with_categories.split(' ', 1) |
| 1392 | channel = channel_then_categories[0] |
| 1393 | if len(channel_then_categories) > 1: |
| 1394 | categories = channel_then_categories[1] |
| 1395 | else: |
| 1396 | categories = "default" |
| 1397 | |
| 1398 | if channel == "gdb-remote": |
| 1399 | # communicate gdb-remote categories to debugserver |
| 1400 | os.environ["LLDB_DEBUGSERVER_LOG_FLAGS"] = categories |
| 1401 | |
| 1402 | self.ci.HandleCommand(log_enable + channel_with_categories, self.res) |
| 1403 | if not self.res.Succeeded(): |
| 1404 | raise Exception('log enable failed (check LLDB_LOG_OPTION env variable)') |
| 1405 | |
| 1406 | # Communicate log path name to debugserver & lldb-server |
| 1407 | server_log_path = "{}-server.log".format(log_basename) |
| 1408 | open(server_log_path, 'w').close() |
| 1409 | os.environ["LLDB_DEBUGSERVER_LOG_FILE"] = server_log_path |
| 1410 | |
| 1411 | # Communicate channels to lldb-server |
| 1412 | os.environ["LLDB_SERVER_LOG_CHANNELS"] = ":".join(lldbtest_config.channels) |
| 1413 | |
| 1414 | if len(lldbtest_config.channels) == 0: |
| 1415 | return |
| 1416 | |
| 1417 | def disableLogChannelsForCurrentTest(self): |
| 1418 | # close all log files that we opened |
| 1419 | for channel_and_categories in lldbtest_config.channels: |
| 1420 | # channel format - <channel-name> [<category0> [<category1> ...]] |
| 1421 | channel = channel_and_categories.split(' ', 1)[0] |
| 1422 | self.ci.HandleCommand("log disable " + channel, self.res) |
| 1423 | if not self.res.Succeeded(): |
| 1424 | raise Exception('log disable failed (check LLDB_LOG_OPTION env variable)') |
| 1425 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1426 | def setUp(self): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1427 | """Fixture for unittest test case setup. |
| 1428 | |
| 1429 | It works with the test driver to conditionally skip tests and does other |
| 1430 | initializations.""" |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 1431 | #import traceback |
| 1432 | #traceback.print_stack() |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1433 | |
Daniel Malea | 9115f07 | 2013-08-06 15:02:32 +0000 | [diff] [blame] | 1434 | if "LIBCXX_PATH" in os.environ: |
| 1435 | self.libcxxPath = os.environ["LIBCXX_PATH"] |
| 1436 | else: |
| 1437 | self.libcxxPath = None |
| 1438 | |
Hafiz Abid Qadeer | 1cbac4e | 2014-11-25 10:41:57 +0000 | [diff] [blame] | 1439 | if "LLDBMI_EXEC" in os.environ: |
| 1440 | self.lldbMiExec = os.environ["LLDBMI_EXEC"] |
| 1441 | else: |
| 1442 | self.lldbMiExec = None |
Vince Harron | 790d95c | 2015-05-18 19:39:03 +0000 | [diff] [blame] | 1443 | |
Johnny Chen | ebe5172 | 2011-10-07 19:21:09 +0000 | [diff] [blame] | 1444 | # If we spawn an lldb process for test (via pexpect), do not load the |
| 1445 | # init file unless told otherwise. |
| 1446 | if "NO_LLDBINIT" in os.environ and "NO" == os.environ["NO_LLDBINIT"]: |
| 1447 | self.lldbOption = "" |
| 1448 | else: |
| 1449 | self.lldbOption = "--no-lldbinit" |
Johnny Chen | aaa82ff | 2011-08-02 22:54:37 +0000 | [diff] [blame] | 1450 | |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1451 | # Assign the test method name to self.testMethodName. |
| 1452 | # |
| 1453 | # For an example of the use of this attribute, look at test/types dir. |
| 1454 | # There are a bunch of test cases under test/types and we don't want the |
| 1455 | # module cacheing subsystem to be confused with executable name "a.out" |
| 1456 | # used for all the test cases. |
| 1457 | self.testMethodName = self._testMethodName |
| 1458 | |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1459 | # This is for the case of directly spawning 'lldb'/'gdb' and interacting |
| 1460 | # with it using pexpect. |
| 1461 | self.child = None |
| 1462 | self.child_prompt = "(lldb) " |
| 1463 | # If the child is interacting with the embedded script interpreter, |
| 1464 | # there are two exits required during tear down, first to quit the |
| 1465 | # embedded script interpreter and second to quit the lldb command |
| 1466 | # interpreter. |
| 1467 | self.child_in_script_interpreter = False |
| 1468 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1469 | # These are for customized teardown cleanup. |
| 1470 | self.dict = None |
| 1471 | self.doTearDownCleanup = False |
| 1472 | # And in rare cases where there are multiple teardown cleanups. |
| 1473 | self.dicts = [] |
| 1474 | self.doTearDownCleanups = False |
| 1475 | |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1476 | # List of spawned subproces.Popen objects |
| 1477 | self.subprocesses = [] |
| 1478 | |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 1479 | # List of forked process PIDs |
| 1480 | self.forkedProcessPids = [] |
| 1481 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1482 | # Create a string buffer to record the session info, to be dumped into a |
| 1483 | # test case specific file if test failure is encountered. |
Vince Harron | 1f16037 | 2015-05-21 18:51:20 +0000 | [diff] [blame] | 1484 | self.log_basename = self.getLogBasenameForCurrentTest() |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1485 | |
Vince Harron | 1f16037 | 2015-05-21 18:51:20 +0000 | [diff] [blame] | 1486 | session_file = "{}.log".format(self.log_basename) |
Zachary Turner | 8d13fab | 2015-11-07 01:08:15 +0000 | [diff] [blame] | 1487 | # Python 3 doesn't support unbuffered I/O in text mode. Open buffered. |
| 1488 | self.session = open(session_file, "w") |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1489 | |
| 1490 | # Optimistically set __errored__, __failed__, __expected__ to False |
| 1491 | # initially. If the test errored/failed, the session info |
| 1492 | # (self.session) is then dumped into a session specific file for |
| 1493 | # diagnosis. |
Zachary Turner | b1490b6 | 2015-08-26 19:44:56 +0000 | [diff] [blame] | 1494 | self.__cleanup_errored__ = False |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1495 | self.__errored__ = False |
| 1496 | self.__failed__ = False |
| 1497 | self.__expected__ = False |
| 1498 | # We are also interested in unexpected success. |
| 1499 | self.__unexpected__ = False |
Johnny Chen | f79b076 | 2011-08-16 00:48:58 +0000 | [diff] [blame] | 1500 | # And skipped tests. |
| 1501 | self.__skipped__ = False |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1502 | |
| 1503 | # See addTearDownHook(self, hook) which allows the client to add a hook |
| 1504 | # function to be run during tearDown() time. |
| 1505 | self.hooks = [] |
| 1506 | |
| 1507 | # See HideStdout(self). |
| 1508 | self.sys_stdout_hidden = False |
| 1509 | |
Zachary Turner | be40b2f | 2014-12-02 21:32:44 +0000 | [diff] [blame] | 1510 | if self.platformContext: |
| 1511 | # set environment variable names for finding shared libraries |
| 1512 | self.dylibPath = self.platformContext.shlib_environment_var |
Daniel Malea | 179ff29 | 2012-11-26 21:21:11 +0000 | [diff] [blame] | 1513 | |
Vince Harron | 6d3d0f1 | 2015-05-10 22:01:59 +0000 | [diff] [blame] | 1514 | # Create the debugger instance if necessary. |
| 1515 | try: |
| 1516 | self.dbg = lldb.DBG |
| 1517 | except AttributeError: |
| 1518 | self.dbg = lldb.SBDebugger.Create() |
| 1519 | |
| 1520 | if not self.dbg: |
| 1521 | raise Exception('Invalid debugger instance') |
| 1522 | |
| 1523 | # Retrieve the associated command interpreter instance. |
| 1524 | self.ci = self.dbg.GetCommandInterpreter() |
| 1525 | if not self.ci: |
| 1526 | raise Exception('Could not get the command interpreter') |
| 1527 | |
| 1528 | # And the result object. |
| 1529 | self.res = lldb.SBCommandReturnObject() |
| 1530 | |
| 1531 | self.enableLogChannelsForCurrentTest() |
| 1532 | |
Ying Chen | 0c35282 | 2015-11-16 23:41:02 +0000 | [diff] [blame] | 1533 | #Initialize debug_info |
| 1534 | self.debug_info = None |
| 1535 | |
Johnny Chen | 2a80858 | 2011-10-19 16:48:07 +0000 | [diff] [blame] | 1536 | def runHooks(self, child=None, child_prompt=None, use_cmd_api=False): |
Johnny Chen | a737ba5 | 2011-10-19 01:06:21 +0000 | [diff] [blame] | 1537 | """Perform the run hooks to bring lldb debugger to the desired state. |
| 1538 | |
Johnny Chen | 2a80858 | 2011-10-19 16:48:07 +0000 | [diff] [blame] | 1539 | By default, expect a pexpect spawned child and child prompt to be |
| 1540 | supplied (use_cmd_api=False). If use_cmd_api is true, ignore the child |
| 1541 | and child prompt and use self.runCmd() to run the hooks one by one. |
| 1542 | |
Johnny Chen | a737ba5 | 2011-10-19 01:06:21 +0000 | [diff] [blame] | 1543 | Note that child is a process spawned by pexpect.spawn(). If not, your |
| 1544 | test case is mostly likely going to fail. |
| 1545 | |
| 1546 | See also dotest.py where lldb.runHooks are processed/populated. |
| 1547 | """ |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 1548 | if not configuration.runHooks: |
Johnny Chen | a737ba5 | 2011-10-19 01:06:21 +0000 | [diff] [blame] | 1549 | self.skipTest("No runhooks specified for lldb, skip the test") |
Johnny Chen | 2a80858 | 2011-10-19 16:48:07 +0000 | [diff] [blame] | 1550 | if use_cmd_api: |
| 1551 | for hook in lldb.runhooks: |
| 1552 | self.runCmd(hook) |
| 1553 | else: |
| 1554 | if not child or not child_prompt: |
| 1555 | self.fail("Both child and child_prompt need to be defined.") |
| 1556 | for hook in lldb.runHooks: |
| 1557 | child.sendline(hook) |
| 1558 | child.expect_exact(child_prompt) |
Johnny Chen | a737ba5 | 2011-10-19 01:06:21 +0000 | [diff] [blame] | 1559 | |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 1560 | def setAsync(self, value): |
| 1561 | """ Sets async mode to True/False and ensures it is reset after the testcase completes.""" |
| 1562 | old_async = self.dbg.GetAsync() |
| 1563 | self.dbg.SetAsync(value) |
| 1564 | self.addTearDownHook(lambda: self.dbg.SetAsync(old_async)) |
| 1565 | |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1566 | def cleanupSubprocesses(self): |
| 1567 | # Ensure any subprocesses are cleaned up |
| 1568 | for p in self.subprocesses: |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1569 | p.terminate() |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1570 | del p |
| 1571 | del self.subprocesses[:] |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 1572 | # Ensure any forked processes are cleaned up |
| 1573 | for pid in self.forkedProcessPids: |
| 1574 | if os.path.exists("/proc/" + str(pid)): |
| 1575 | os.kill(pid, signal.SIGTERM) |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1576 | |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 1577 | def spawnSubprocess(self, executable, args=[], install_remote=True): |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1578 | """ Creates a subprocess.Popen object with the specified executable and arguments, |
| 1579 | saves it in self.subprocesses, and returns the object. |
| 1580 | NOTE: if using this function, ensure you also call: |
| 1581 | |
| 1582 | self.addTearDownHook(self.cleanupSubprocesses) |
| 1583 | |
| 1584 | otherwise the test suite will leak processes. |
| 1585 | """ |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 1586 | proc = _RemoteProcess(install_remote) if lldb.remote_platform else _LocalProcess(self.TraceOn()) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1587 | proc.launch(executable, args) |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1588 | self.subprocesses.append(proc) |
| 1589 | return proc |
| 1590 | |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 1591 | def forkSubprocess(self, executable, args=[]): |
| 1592 | """ Fork a subprocess with its own group ID. |
| 1593 | NOTE: if using this function, ensure you also call: |
| 1594 | |
| 1595 | self.addTearDownHook(self.cleanupSubprocesses) |
| 1596 | |
| 1597 | otherwise the test suite will leak processes. |
| 1598 | """ |
| 1599 | child_pid = os.fork() |
| 1600 | if child_pid == 0: |
| 1601 | # If more I/O support is required, this can be beefed up. |
| 1602 | fd = os.open(os.devnull, os.O_RDWR) |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 1603 | os.dup2(fd, 1) |
| 1604 | os.dup2(fd, 2) |
| 1605 | # This call causes the child to have its of group ID |
| 1606 | os.setpgid(0,0) |
| 1607 | os.execvp(executable, [executable] + args) |
| 1608 | # Give the child time to get through the execvp() call |
| 1609 | time.sleep(0.1) |
| 1610 | self.forkedProcessPids.append(child_pid) |
| 1611 | return child_pid |
| 1612 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1613 | def HideStdout(self): |
| 1614 | """Hide output to stdout from the user. |
| 1615 | |
| 1616 | During test execution, there might be cases where we don't want to show the |
| 1617 | standard output to the user. For example, |
| 1618 | |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 1619 | self.runCmd(r'''sc print("\n\n\tHello!\n")''') |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1620 | |
| 1621 | tests whether command abbreviation for 'script' works or not. There is no |
| 1622 | need to show the 'Hello' output to the user as long as the 'script' command |
| 1623 | succeeds and we are not in TraceOn() mode (see the '-t' option). |
| 1624 | |
| 1625 | In this case, the test method calls self.HideStdout(self) to redirect the |
| 1626 | sys.stdout to a null device, and restores the sys.stdout upon teardown. |
| 1627 | |
| 1628 | Note that you should only call this method at most once during a test case |
| 1629 | execution. Any subsequent call has no effect at all.""" |
| 1630 | if self.sys_stdout_hidden: |
| 1631 | return |
| 1632 | |
| 1633 | self.sys_stdout_hidden = True |
| 1634 | old_stdout = sys.stdout |
| 1635 | sys.stdout = open(os.devnull, 'w') |
| 1636 | def restore_stdout(): |
| 1637 | sys.stdout = old_stdout |
| 1638 | self.addTearDownHook(restore_stdout) |
| 1639 | |
| 1640 | # ======================================================================= |
| 1641 | # Methods for customized teardown cleanups as well as execution of hooks. |
| 1642 | # ======================================================================= |
| 1643 | |
| 1644 | def setTearDownCleanup(self, dictionary=None): |
| 1645 | """Register a cleanup action at tearDown() time with a dictinary""" |
| 1646 | self.dict = dictionary |
| 1647 | self.doTearDownCleanup = True |
| 1648 | |
| 1649 | def addTearDownCleanup(self, dictionary): |
| 1650 | """Add a cleanup action at tearDown() time with a dictinary""" |
| 1651 | self.dicts.append(dictionary) |
| 1652 | self.doTearDownCleanups = True |
| 1653 | |
| 1654 | def addTearDownHook(self, hook): |
| 1655 | """ |
| 1656 | Add a function to be run during tearDown() time. |
| 1657 | |
| 1658 | Hooks are executed in a first come first serve manner. |
| 1659 | """ |
Zachary Turner | cd236b8 | 2015-10-26 18:48:24 +0000 | [diff] [blame] | 1660 | if six.callable(hook): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1661 | with recording(self, traceAlways) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1662 | print("Adding tearDown hook:", getsource_if_available(hook), file=sbuf) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1663 | self.hooks.append(hook) |
Enrico Granata | ab0e831 | 2014-11-05 21:31:57 +0000 | [diff] [blame] | 1664 | |
| 1665 | return self |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1666 | |
Jim Ingham | da3a386 | 2014-10-16 23:02:14 +0000 | [diff] [blame] | 1667 | def deletePexpectChild(self): |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1668 | # This is for the case of directly spawning 'lldb' and interacting with it |
| 1669 | # using pexpect. |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1670 | if self.child and self.child.isalive(): |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 1671 | import pexpect |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1672 | with recording(self, traceAlways) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1673 | print("tearing down the child process....", file=sbuf) |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1674 | try: |
Daniel Malea | c9a0ec3 | 2013-02-22 00:41:26 +0000 | [diff] [blame] | 1675 | if self.child_in_script_interpreter: |
| 1676 | self.child.sendline('quit()') |
| 1677 | self.child.expect_exact(self.child_prompt) |
| 1678 | self.child.sendline('settings set interpreter.prompt-on-quit false') |
| 1679 | self.child.sendline('quit') |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1680 | self.child.expect(pexpect.EOF) |
Ilia K | 47448c2 | 2015-02-11 21:41:58 +0000 | [diff] [blame] | 1681 | except (ValueError, pexpect.ExceptionPexpect): |
| 1682 | # child is already terminated |
| 1683 | pass |
| 1684 | except OSError as exception: |
| 1685 | import errno |
| 1686 | if exception.errno != errno.EIO: |
| 1687 | # unexpected error |
| 1688 | raise |
Daniel Malea | c9a0ec3 | 2013-02-22 00:41:26 +0000 | [diff] [blame] | 1689 | # child is already terminated |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1690 | pass |
Shawn Best | eb3e905 | 2014-11-06 17:52:15 +0000 | [diff] [blame] | 1691 | finally: |
| 1692 | # Give it one final blow to make sure the child is terminated. |
| 1693 | self.child.close() |
Jim Ingham | da3a386 | 2014-10-16 23:02:14 +0000 | [diff] [blame] | 1694 | |
| 1695 | def tearDown(self): |
| 1696 | """Fixture for unittest test case teardown.""" |
| 1697 | #import traceback |
| 1698 | #traceback.print_stack() |
| 1699 | |
| 1700 | self.deletePexpectChild() |
| 1701 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1702 | # Check and run any hook functions. |
| 1703 | for hook in reversed(self.hooks): |
| 1704 | with recording(self, traceAlways) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1705 | print("Executing tearDown hook:", getsource_if_available(hook), file=sbuf) |
Enrico Granata | ab0e831 | 2014-11-05 21:31:57 +0000 | [diff] [blame] | 1706 | import inspect |
| 1707 | hook_argc = len(inspect.getargspec(hook).args) |
Enrico Granata | 6e0566c | 2014-11-17 19:00:20 +0000 | [diff] [blame] | 1708 | if hook_argc == 0 or getattr(hook,'im_self',None): |
Enrico Granata | ab0e831 | 2014-11-05 21:31:57 +0000 | [diff] [blame] | 1709 | hook() |
| 1710 | elif hook_argc == 1: |
| 1711 | hook(self) |
| 1712 | else: |
| 1713 | hook() # try the plain call and hope it works |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1714 | |
| 1715 | del self.hooks |
| 1716 | |
| 1717 | # Perform registered teardown cleanup. |
| 1718 | if doCleanup and self.doTearDownCleanup: |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 1719 | self.cleanup(dictionary=self.dict) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1720 | |
| 1721 | # In rare cases where there are multiple teardown cleanups added. |
| 1722 | if doCleanup and self.doTearDownCleanups: |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1723 | if self.dicts: |
| 1724 | for dict in reversed(self.dicts): |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 1725 | self.cleanup(dictionary=dict) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1726 | |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1727 | self.disableLogChannelsForCurrentTest() |
| 1728 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1729 | # ========================================================= |
| 1730 | # Various callbacks to allow introspection of test progress |
| 1731 | # ========================================================= |
| 1732 | |
| 1733 | def markError(self): |
| 1734 | """Callback invoked when an error (unexpected exception) errored.""" |
| 1735 | self.__errored__ = True |
| 1736 | with recording(self, False) as sbuf: |
| 1737 | # False because there's no need to write "ERROR" to the stderr twice. |
| 1738 | # Once by the Python unittest framework, and a second time by us. |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1739 | print("ERROR", file=sbuf) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1740 | |
Zachary Turner | b1490b6 | 2015-08-26 19:44:56 +0000 | [diff] [blame] | 1741 | def markCleanupError(self): |
| 1742 | """Callback invoked when an error occurs while a test is cleaning up.""" |
| 1743 | self.__cleanup_errored__ = True |
| 1744 | with recording(self, False) as sbuf: |
| 1745 | # False because there's no need to write "CLEANUP_ERROR" to the stderr twice. |
| 1746 | # Once by the Python unittest framework, and a second time by us. |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1747 | print("CLEANUP_ERROR", file=sbuf) |
Zachary Turner | b1490b6 | 2015-08-26 19:44:56 +0000 | [diff] [blame] | 1748 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1749 | def markFailure(self): |
| 1750 | """Callback invoked when a failure (test assertion failure) occurred.""" |
| 1751 | self.__failed__ = True |
| 1752 | with recording(self, False) as sbuf: |
| 1753 | # False because there's no need to write "FAIL" to the stderr twice. |
| 1754 | # Once by the Python unittest framework, and a second time by us. |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1755 | print("FAIL", file=sbuf) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1756 | |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1757 | def markExpectedFailure(self,err,bugnumber): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1758 | """Callback invoked when an expected failure/error occurred.""" |
| 1759 | self.__expected__ = True |
| 1760 | with recording(self, False) as sbuf: |
| 1761 | # False because there's no need to write "expected failure" to the |
| 1762 | # stderr twice. |
| 1763 | # Once by the Python unittest framework, and a second time by us. |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1764 | if bugnumber == None: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1765 | print("expected failure", file=sbuf) |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1766 | else: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1767 | print("expected failure (problem id:" + str(bugnumber) + ")", file=sbuf) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1768 | |
Johnny Chen | c5cc625 | 2011-08-15 23:09:08 +0000 | [diff] [blame] | 1769 | def markSkippedTest(self): |
| 1770 | """Callback invoked when a test is skipped.""" |
| 1771 | self.__skipped__ = True |
| 1772 | with recording(self, False) as sbuf: |
| 1773 | # False because there's no need to write "skipped test" to the |
| 1774 | # stderr twice. |
| 1775 | # Once by the Python unittest framework, and a second time by us. |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1776 | print("skipped test", file=sbuf) |
Johnny Chen | c5cc625 | 2011-08-15 23:09:08 +0000 | [diff] [blame] | 1777 | |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1778 | def markUnexpectedSuccess(self, bugnumber): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1779 | """Callback invoked when an unexpected success occurred.""" |
| 1780 | self.__unexpected__ = True |
| 1781 | with recording(self, False) as sbuf: |
| 1782 | # False because there's no need to write "unexpected success" to the |
| 1783 | # stderr twice. |
| 1784 | # Once by the Python unittest framework, and a second time by us. |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1785 | if bugnumber == None: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1786 | print("unexpected success", file=sbuf) |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1787 | else: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1788 | print("unexpected success (problem id:" + str(bugnumber) + ")", file=sbuf) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1789 | |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 1790 | def getRerunArgs(self): |
| 1791 | return " -f %s.%s" % (self.__class__.__name__, self._testMethodName) |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1792 | |
| 1793 | def getLogBasenameForCurrentTest(self, prefix=None): |
| 1794 | """ |
| 1795 | returns a partial path that can be used as the beginning of the name of multiple |
| 1796 | log files pertaining to this test |
| 1797 | |
| 1798 | <session-dir>/<arch>-<compiler>-<test-file>.<test-class>.<test-method> |
| 1799 | """ |
| 1800 | dname = os.path.join(os.environ["LLDB_TEST"], |
| 1801 | os.environ["LLDB_SESSION_DIRNAME"]) |
| 1802 | if not os.path.isdir(dname): |
| 1803 | os.mkdir(dname) |
| 1804 | |
| 1805 | compiler = self.getCompiler() |
| 1806 | |
| 1807 | if compiler[1] == ':': |
| 1808 | compiler = compiler[2:] |
Chaoren Lin | 636a0e3 | 2015-07-17 21:40:11 +0000 | [diff] [blame] | 1809 | if os.path.altsep is not None: |
| 1810 | compiler = compiler.replace(os.path.altsep, os.path.sep) |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1811 | |
Vince Harron | 19e300f | 2015-05-12 00:50:54 +0000 | [diff] [blame] | 1812 | fname = "{}-{}-{}".format(self.id(), self.getArchitecture(), "_".join(compiler.split(os.path.sep))) |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1813 | if len(fname) > 200: |
Vince Harron | 19e300f | 2015-05-12 00:50:54 +0000 | [diff] [blame] | 1814 | fname = "{}-{}-{}".format(self.id(), self.getArchitecture(), compiler.split(os.path.sep)[-1]) |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1815 | |
| 1816 | if prefix is not None: |
| 1817 | fname = "{}-{}".format(prefix, fname) |
| 1818 | |
| 1819 | return os.path.join(dname, fname) |
| 1820 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1821 | def dumpSessionInfo(self): |
| 1822 | """ |
| 1823 | Dump the debugger interactions leading to a test error/failure. This |
| 1824 | allows for more convenient postmortem analysis. |
| 1825 | |
| 1826 | See also LLDBTestResult (dotest.py) which is a singlton class derived |
| 1827 | from TextTestResult and overwrites addError, addFailure, and |
| 1828 | addExpectedFailure methods to allow us to to mark the test instance as |
| 1829 | such. |
| 1830 | """ |
| 1831 | |
| 1832 | # We are here because self.tearDown() detected that this test instance |
| 1833 | # either errored or failed. The lldb.test_result singleton contains |
| 1834 | # two lists (erros and failures) which get populated by the unittest |
| 1835 | # framework. Look over there for stack trace information. |
| 1836 | # |
| 1837 | # The lists contain 2-tuples of TestCase instances and strings holding |
| 1838 | # formatted tracebacks. |
| 1839 | # |
| 1840 | # See http://docs.python.org/library/unittest.html#unittest.TestResult. |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1841 | |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1842 | # output tracebacks into session |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1843 | pairs = [] |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1844 | if self.__errored__: |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 1845 | pairs = configuration.test_result.errors |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1846 | prefix = 'Error' |
Zachary Turner | 14181db | 2015-09-11 21:27:37 +0000 | [diff] [blame] | 1847 | elif self.__cleanup_errored__: |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 1848 | pairs = configuration.test_result.cleanup_errors |
Zachary Turner | b1490b6 | 2015-08-26 19:44:56 +0000 | [diff] [blame] | 1849 | prefix = 'CleanupError' |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1850 | elif self.__failed__: |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 1851 | pairs = configuration.test_result.failures |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1852 | prefix = 'Failure' |
| 1853 | elif self.__expected__: |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 1854 | pairs = configuration.test_result.expectedFailures |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1855 | prefix = 'ExpectedFailure' |
Johnny Chen | c5cc625 | 2011-08-15 23:09:08 +0000 | [diff] [blame] | 1856 | elif self.__skipped__: |
| 1857 | prefix = 'SkippedTest' |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1858 | elif self.__unexpected__: |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1859 | prefix = 'UnexpectedSuccess' |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1860 | else: |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1861 | prefix = 'Success' |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1862 | |
Johnny Chen | c5cc625 | 2011-08-15 23:09:08 +0000 | [diff] [blame] | 1863 | if not self.__unexpected__ and not self.__skipped__: |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1864 | for test, traceback in pairs: |
| 1865 | if test is self: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1866 | print(traceback, file=self.session) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1867 | |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1868 | # put footer (timestamp/rerun instructions) into session |
Johnny Chen | 8082a00 | 2011-08-11 00:16:28 +0000 | [diff] [blame] | 1869 | testMethod = getattr(self, self._testMethodName) |
| 1870 | if getattr(testMethod, "__benchmarks_test__", False): |
| 1871 | benchmarks = True |
| 1872 | else: |
| 1873 | benchmarks = False |
| 1874 | |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1875 | import datetime |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1876 | print("Session info generated @", datetime.datetime.now().ctime(), file=self.session) |
| 1877 | print("To rerun this test, issue the following command from the 'test' directory:\n", file=self.session) |
| 1878 | print("./dotest.py %s -v %s %s" % (self.getRunOptions(), |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1879 | ('+b' if benchmarks else '-t'), |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1880 | self.getRerunArgs()), file=self.session) |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1881 | self.session.close() |
| 1882 | del self.session |
| 1883 | |
| 1884 | # process the log files |
Vince Harron | 1f16037 | 2015-05-21 18:51:20 +0000 | [diff] [blame] | 1885 | log_files_for_this_test = glob.glob(self.log_basename + "*") |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1886 | |
| 1887 | if prefix != 'Success' or lldbtest_config.log_success: |
| 1888 | # keep all log files, rename them to include prefix |
| 1889 | dst_log_basename = self.getLogBasenameForCurrentTest(prefix) |
| 1890 | for src in log_files_for_this_test: |
Zachary Turner | 306278f | 2015-05-26 20:26:29 +0000 | [diff] [blame] | 1891 | if os.path.isfile(src): |
| 1892 | dst = src.replace(self.log_basename, dst_log_basename) |
| 1893 | if os.name == "nt" and os.path.isfile(dst): |
| 1894 | # On Windows, renaming a -> b will throw an exception if b exists. On non-Windows platforms |
| 1895 | # it silently replaces the destination. Ultimately this means that atomic renames are not |
| 1896 | # guaranteed to be possible on Windows, but we need this to work anyway, so just remove the |
| 1897 | # destination first if it already exists. |
| 1898 | os.remove(dst) |
Zachary Turner | 5de068b | 2015-05-26 19:52:24 +0000 | [diff] [blame] | 1899 | |
Zachary Turner | 306278f | 2015-05-26 20:26:29 +0000 | [diff] [blame] | 1900 | os.rename(src, dst) |
Vince Harron | 35b17dc | 2015-05-21 18:20:21 +0000 | [diff] [blame] | 1901 | else: |
| 1902 | # success! (and we don't want log files) delete log files |
| 1903 | for log_file in log_files_for_this_test: |
Adrian McCarthy | a729204 | 2015-09-04 20:48:48 +0000 | [diff] [blame] | 1904 | try: |
| 1905 | os.unlink(log_file) |
| 1906 | except: |
| 1907 | # We've seen consistent unlink failures on Windows, perhaps because the |
| 1908 | # just-created log file is being scanned by anti-virus. Empirically, this |
| 1909 | # sleep-and-retry approach allows tests to succeed much more reliably. |
| 1910 | # Attempts to figure out exactly what process was still holding a file handle |
| 1911 | # have failed because running instrumentation like Process Monitor seems to |
| 1912 | # slow things down enough that the problem becomes much less consistent. |
| 1913 | time.sleep(0.5) |
| 1914 | os.unlink(log_file) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1915 | |
| 1916 | # ==================================================== |
| 1917 | # Config. methods supported through a plugin interface |
| 1918 | # (enables reading of the current test configuration) |
| 1919 | # ==================================================== |
| 1920 | |
| 1921 | def getArchitecture(self): |
| 1922 | """Returns the architecture in effect the test suite is running with.""" |
| 1923 | module = builder_module() |
Ed Maste | 0f434e6 | 2015-04-06 15:50:48 +0000 | [diff] [blame] | 1924 | arch = module.getArchitecture() |
| 1925 | if arch == 'amd64': |
| 1926 | arch = 'x86_64' |
| 1927 | return arch |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1928 | |
Vince Harron | 0261376 | 2015-05-04 00:17:53 +0000 | [diff] [blame] | 1929 | def getLldbArchitecture(self): |
| 1930 | """Returns the architecture of the lldb binary.""" |
| 1931 | if not hasattr(self, 'lldbArchitecture'): |
| 1932 | |
| 1933 | # spawn local process |
| 1934 | command = [ |
Vince Harron | 790d95c | 2015-05-18 19:39:03 +0000 | [diff] [blame] | 1935 | lldbtest_config.lldbExec, |
Vince Harron | 0261376 | 2015-05-04 00:17:53 +0000 | [diff] [blame] | 1936 | "-o", |
Vince Harron | 790d95c | 2015-05-18 19:39:03 +0000 | [diff] [blame] | 1937 | "file " + lldbtest_config.lldbExec, |
Vince Harron | 0261376 | 2015-05-04 00:17:53 +0000 | [diff] [blame] | 1938 | "-o", |
| 1939 | "quit" |
| 1940 | ] |
| 1941 | |
| 1942 | output = check_output(command) |
| 1943 | str = output.decode("utf-8"); |
| 1944 | |
| 1945 | for line in str.splitlines(): |
| 1946 | m = re.search("Current executable set to '.*' \\((.*)\\)\\.", line) |
| 1947 | if m: |
| 1948 | self.lldbArchitecture = m.group(1) |
| 1949 | break |
| 1950 | |
| 1951 | return self.lldbArchitecture |
| 1952 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1953 | def getCompiler(self): |
| 1954 | """Returns the compiler in effect the test suite is running with.""" |
| 1955 | module = builder_module() |
| 1956 | return module.getCompiler() |
| 1957 | |
Oleksiy Vyalov | dc4067c | 2014-11-26 18:30:04 +0000 | [diff] [blame] | 1958 | def getCompilerBinary(self): |
| 1959 | """Returns the compiler binary the test suite is running with.""" |
| 1960 | return self.getCompiler().split()[0] |
| 1961 | |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 1962 | def getCompilerVersion(self): |
| 1963 | """ Returns a string that represents the compiler version. |
| 1964 | Supports: llvm, clang. |
| 1965 | """ |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 1966 | from .lldbutil import which |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 1967 | version = 'unknown' |
| 1968 | |
Oleksiy Vyalov | dc4067c | 2014-11-26 18:30:04 +0000 | [diff] [blame] | 1969 | compiler = self.getCompilerBinary() |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 1970 | version_output = system([[which(compiler), "-v"]])[1] |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 1971 | for line in version_output.split(os.linesep): |
Greg Clayton | 2a844b7 | 2013-03-06 02:34:51 +0000 | [diff] [blame] | 1972 | m = re.search('version ([0-9\.]+)', line) |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 1973 | if m: |
| 1974 | version = m.group(1) |
| 1975 | return version |
| 1976 | |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1977 | def getGoCompilerVersion(self): |
| 1978 | """ Returns a string that represents the go compiler version, or None if go is not found. |
| 1979 | """ |
| 1980 | compiler = which("go") |
| 1981 | if compiler: |
| 1982 | version_output = system([[compiler, "version"]])[0] |
| 1983 | for line in version_output.split(os.linesep): |
| 1984 | m = re.search('go version (devel|go\\S+)', line) |
| 1985 | if m: |
| 1986 | return m.group(1) |
| 1987 | return None |
| 1988 | |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 1989 | def platformIsDarwin(self): |
| 1990 | """Returns true if the OS triple for the selected platform is any valid apple OS""" |
Robert Flack | fb2f6c6 | 2015-04-17 08:02:18 +0000 | [diff] [blame] | 1991 | return platformIsDarwin() |
Vince Harron | 20952cc | 2015-04-03 01:00:06 +0000 | [diff] [blame] | 1992 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 1993 | def getPlatform(self): |
Robert Flack | fb2f6c6 | 2015-04-17 08:02:18 +0000 | [diff] [blame] | 1994 | """Returns the target platform the test suite is running on.""" |
Robert Flack | 068898c | 2015-04-09 18:07:58 +0000 | [diff] [blame] | 1995 | return getPlatform() |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 1996 | |
Daniel Malea | adaaec9 | 2013-08-06 20:51:41 +0000 | [diff] [blame] | 1997 | def isIntelCompiler(self): |
| 1998 | """ Returns true if using an Intel (ICC) compiler, false otherwise. """ |
| 1999 | return any([x in self.getCompiler() for x in ["icc", "icpc", "icl"]]) |
| 2000 | |
Ashok Thirumurthi | 3b03728 | 2013-06-06 14:23:31 +0000 | [diff] [blame] | 2001 | def expectedCompilerVersion(self, compiler_version): |
| 2002 | """Returns True iff compiler_version[1] matches the current compiler version. |
| 2003 | Use compiler_version[0] to specify the operator used to determine if a match has occurred. |
| 2004 | Any operator other than the following defaults to an equality test: |
| 2005 | '>', '>=', "=>", '<', '<=', '=<', '!=', "!" or 'not' |
| 2006 | """ |
Ashok Thirumurthi | c97a608 | 2013-05-17 20:15:07 +0000 | [diff] [blame] | 2007 | if (compiler_version == None): |
| 2008 | return True |
| 2009 | operator = str(compiler_version[0]) |
| 2010 | version = compiler_version[1] |
| 2011 | |
| 2012 | if (version == None): |
| 2013 | return True |
| 2014 | if (operator == '>'): |
| 2015 | return self.getCompilerVersion() > version |
| 2016 | if (operator == '>=' or operator == '=>'): |
| 2017 | return self.getCompilerVersion() >= version |
| 2018 | if (operator == '<'): |
| 2019 | return self.getCompilerVersion() < version |
| 2020 | if (operator == '<=' or operator == '=<'): |
| 2021 | return self.getCompilerVersion() <= version |
| 2022 | if (operator == '!=' or operator == '!' or operator == 'not'): |
| 2023 | return str(version) not in str(self.getCompilerVersion()) |
| 2024 | return str(version) in str(self.getCompilerVersion()) |
| 2025 | |
| 2026 | def expectedCompiler(self, compilers): |
Ashok Thirumurthi | 3b03728 | 2013-06-06 14:23:31 +0000 | [diff] [blame] | 2027 | """Returns True iff any element of compilers is a sub-string of the current compiler.""" |
Ashok Thirumurthi | c97a608 | 2013-05-17 20:15:07 +0000 | [diff] [blame] | 2028 | if (compilers == None): |
| 2029 | return True |
Ashok Thirumurthi | 3b03728 | 2013-06-06 14:23:31 +0000 | [diff] [blame] | 2030 | |
| 2031 | for compiler in compilers: |
| 2032 | if compiler in self.getCompiler(): |
| 2033 | return True |
| 2034 | |
| 2035 | return False |
Ashok Thirumurthi | c97a608 | 2013-05-17 20:15:07 +0000 | [diff] [blame] | 2036 | |
Ying Chen | 7091c2c | 2015-04-21 01:15:47 +0000 | [diff] [blame] | 2037 | def expectedArch(self, archs): |
| 2038 | """Returns True iff any element of archs is a sub-string of the current architecture.""" |
| 2039 | if (archs == None): |
| 2040 | return True |
| 2041 | |
| 2042 | for arch in archs: |
| 2043 | if arch in self.getArchitecture(): |
| 2044 | return True |
| 2045 | |
| 2046 | return False |
| 2047 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2048 | def getRunOptions(self): |
| 2049 | """Command line option for -A and -C to run this test again, called from |
| 2050 | self.dumpSessionInfo().""" |
| 2051 | arch = self.getArchitecture() |
| 2052 | comp = self.getCompiler() |
Johnny Chen | b7bdd10 | 2011-08-24 19:48:51 +0000 | [diff] [blame] | 2053 | if arch: |
| 2054 | option_str = "-A " + arch |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2055 | else: |
Johnny Chen | b7bdd10 | 2011-08-24 19:48:51 +0000 | [diff] [blame] | 2056 | option_str = "" |
| 2057 | if comp: |
Johnny Chen | 531c085 | 2012-03-16 20:44:00 +0000 | [diff] [blame] | 2058 | option_str += " -C " + comp |
Johnny Chen | b7bdd10 | 2011-08-24 19:48:51 +0000 | [diff] [blame] | 2059 | return option_str |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2060 | |
| 2061 | # ================================================== |
| 2062 | # Build methods supported through a plugin interface |
| 2063 | # ================================================== |
| 2064 | |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 2065 | def getstdlibFlag(self): |
| 2066 | """ Returns the proper -stdlib flag, or empty if not required.""" |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 2067 | if self.platformIsDarwin() or self.getPlatform() == "freebsd": |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 2068 | stdlibflag = "-stdlib=libc++" |
Kamil Rytarowski | 49f9fb8 | 2015-12-07 21:25:57 +0000 | [diff] [blame] | 2069 | else: # this includes NetBSD |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 2070 | stdlibflag = "" |
| 2071 | return stdlibflag |
| 2072 | |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 2073 | def getstdFlag(self): |
| 2074 | """ Returns the proper stdflag. """ |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2075 | if "gcc" in self.getCompiler() and "4.6" in self.getCompilerVersion(): |
Daniel Malea | 0b7c611 | 2013-05-06 19:31:31 +0000 | [diff] [blame] | 2076 | stdflag = "-std=c++0x" |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2077 | else: |
| 2078 | stdflag = "-std=c++11" |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 2079 | return stdflag |
| 2080 | |
| 2081 | def buildDriver(self, sources, exe_name): |
| 2082 | """ Platform-specific way to build a program that links with LLDB (via the liblldb.so |
| 2083 | or LLDB.framework). |
| 2084 | """ |
| 2085 | |
| 2086 | stdflag = self.getstdFlag() |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 2087 | stdlibflag = self.getstdlibFlag() |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2088 | |
| 2089 | lib_dir = os.environ["LLDB_LIB_DIR"] |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2090 | if sys.platform.startswith("darwin"): |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2091 | dsym = os.path.join(lib_dir, 'LLDB.framework', 'LLDB') |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2092 | d = {'CXX_SOURCES' : sources, |
| 2093 | 'EXE' : exe_name, |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 2094 | 'CFLAGS_EXTRAS' : "%s %s" % (stdflag, stdlibflag), |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2095 | 'FRAMEWORK_INCLUDES' : "-F%s" % lib_dir, |
| 2096 | 'LD_EXTRAS' : "%s -Wl,-rpath,%s" % (dsym, lib_dir), |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2097 | } |
Ed Maste | 372c24d | 2013-07-25 21:02:34 +0000 | [diff] [blame] | 2098 | elif sys.platform.startswith('freebsd') or sys.platform.startswith("linux") or os.environ.get('LLDB_BUILD_TYPE') == 'Makefile': |
Adrian McCarthy | b016b3c | 2015-03-27 20:47:35 +0000 | [diff] [blame] | 2099 | d = {'CXX_SOURCES' : sources, |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2100 | 'EXE' : exe_name, |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 2101 | 'CFLAGS_EXTRAS' : "%s %s -I%s" % (stdflag, stdlibflag, os.path.join(os.environ["LLDB_SRC"], "include")), |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2102 | 'LD_EXTRAS' : "-L%s -llldb" % lib_dir} |
Adrian McCarthy | b016b3c | 2015-03-27 20:47:35 +0000 | [diff] [blame] | 2103 | elif sys.platform.startswith('win'): |
| 2104 | d = {'CXX_SOURCES' : sources, |
| 2105 | 'EXE' : exe_name, |
| 2106 | 'CFLAGS_EXTRAS' : "%s %s -I%s" % (stdflag, stdlibflag, os.path.join(os.environ["LLDB_SRC"], "include")), |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2107 | 'LD_EXTRAS' : "-L%s -lliblldb" % os.environ["LLDB_IMPLIB_DIR"]} |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2108 | if self.TraceOn(): |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2109 | print("Building LLDB Driver (%s) from sources %s" % (exe_name, sources)) |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2110 | |
| 2111 | self.buildDefault(dictionary=d) |
| 2112 | |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 2113 | def buildLibrary(self, sources, lib_name): |
| 2114 | """Platform specific way to build a default library. """ |
| 2115 | |
| 2116 | stdflag = self.getstdFlag() |
| 2117 | |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2118 | lib_dir = os.environ["LLDB_LIB_DIR"] |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 2119 | if self.platformIsDarwin(): |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2120 | dsym = os.path.join(lib_dir, 'LLDB.framework', 'LLDB') |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 2121 | d = {'DYLIB_CXX_SOURCES' : sources, |
| 2122 | 'DYLIB_NAME' : lib_name, |
| 2123 | 'CFLAGS_EXTRAS' : "%s -stdlib=libc++" % stdflag, |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2124 | 'FRAMEWORK_INCLUDES' : "-F%s" % lib_dir, |
| 2125 | 'LD_EXTRAS' : "%s -Wl,-rpath,%s -dynamiclib" % (dsym, lib_dir), |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 2126 | } |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 2127 | elif self.getPlatform() == 'freebsd' or self.getPlatform() == 'linux' or os.environ.get('LLDB_BUILD_TYPE') == 'Makefile': |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 2128 | d = {'DYLIB_CXX_SOURCES' : sources, |
| 2129 | 'DYLIB_NAME' : lib_name, |
| 2130 | 'CFLAGS_EXTRAS' : "%s -I%s -fPIC" % (stdflag, os.path.join(os.environ["LLDB_SRC"], "include")), |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2131 | 'LD_EXTRAS' : "-shared -L%s -llldb" % lib_dir} |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 2132 | elif self.getPlatform() == 'windows': |
Adrian McCarthy | b016b3c | 2015-03-27 20:47:35 +0000 | [diff] [blame] | 2133 | d = {'DYLIB_CXX_SOURCES' : sources, |
| 2134 | 'DYLIB_NAME' : lib_name, |
| 2135 | 'CFLAGS_EXTRAS' : "%s -I%s -fPIC" % (stdflag, os.path.join(os.environ["LLDB_SRC"], "include")), |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2136 | 'LD_EXTRAS' : "-shared -l%s\liblldb.lib" % self.os.environ["LLDB_IMPLIB_DIR"]} |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 2137 | if self.TraceOn(): |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2138 | print("Building LLDB Library (%s) from sources %s" % (lib_name, sources)) |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 2139 | |
| 2140 | self.buildDefault(dictionary=d) |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 2141 | |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2142 | def buildProgram(self, sources, exe_name): |
| 2143 | """ Platform specific way to build an executable from C/C++ sources. """ |
| 2144 | d = {'CXX_SOURCES' : sources, |
| 2145 | 'EXE' : exe_name} |
| 2146 | self.buildDefault(dictionary=d) |
| 2147 | |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 2148 | def buildDefault(self, architecture=None, compiler=None, dictionary=None, clean=True): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2149 | """Platform specific way to build the default binaries.""" |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 2150 | if configuration.skip_build_and_cleanup: |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 2151 | return |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2152 | module = builder_module() |
Chaoren Lin | e9bbabc | 2015-07-18 00:37:55 +0000 | [diff] [blame] | 2153 | if target_is_android(): |
| 2154 | dictionary = append_android_envs(dictionary) |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 2155 | if not module.buildDefault(self, architecture, compiler, dictionary, clean): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2156 | raise Exception("Don't know how to build default binary") |
| 2157 | |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 2158 | def buildDsym(self, architecture=None, compiler=None, dictionary=None, clean=True): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2159 | """Platform specific way to build binaries with dsym info.""" |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 2160 | if configuration.skip_build_and_cleanup: |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 2161 | return |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2162 | module = builder_module() |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 2163 | if not module.buildDsym(self, architecture, compiler, dictionary, clean): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2164 | raise Exception("Don't know how to build binary with dsym") |
| 2165 | |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 2166 | def buildDwarf(self, architecture=None, compiler=None, dictionary=None, clean=True): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2167 | """Platform specific way to build binaries with dwarf maps.""" |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 2168 | if configuration.skip_build_and_cleanup: |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 2169 | return |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2170 | module = builder_module() |
Chaoren Lin | 9070f53 | 2015-07-17 22:13:29 +0000 | [diff] [blame] | 2171 | if target_is_android(): |
Chaoren Lin | e9bbabc | 2015-07-18 00:37:55 +0000 | [diff] [blame] | 2172 | dictionary = append_android_envs(dictionary) |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 2173 | if not module.buildDwarf(self, architecture, compiler, dictionary, clean): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 2174 | raise Exception("Don't know how to build binary with dwarf") |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 2175 | |
Tamas Berghammer | 4c0c7a7 | 2015-10-07 10:02:17 +0000 | [diff] [blame] | 2176 | def buildDwo(self, architecture=None, compiler=None, dictionary=None, clean=True): |
| 2177 | """Platform specific way to build binaries with dwarf maps.""" |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 2178 | if configuration.skip_build_and_cleanup: |
Tamas Berghammer | 4c0c7a7 | 2015-10-07 10:02:17 +0000 | [diff] [blame] | 2179 | return |
| 2180 | module = builder_module() |
| 2181 | if target_is_android(): |
| 2182 | dictionary = append_android_envs(dictionary) |
| 2183 | if not module.buildDwo(self, architecture, compiler, dictionary, clean): |
| 2184 | raise Exception("Don't know how to build binary with dwo") |
| 2185 | |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 2186 | def buildGo(self): |
| 2187 | """Build the default go binary. |
| 2188 | """ |
| 2189 | system([[which('go'), 'build -gcflags "-N -l" -o a.out main.go']]) |
| 2190 | |
Oleksiy Vyalov | 49b71c6 | 2015-01-22 20:03:21 +0000 | [diff] [blame] | 2191 | def signBinary(self, binary_path): |
| 2192 | if sys.platform.startswith("darwin"): |
| 2193 | codesign_cmd = "codesign --force --sign lldb_codesign %s" % (binary_path) |
| 2194 | call(codesign_cmd, shell=True) |
| 2195 | |
Kuba Brecka | beed821 | 2014-09-04 01:03:18 +0000 | [diff] [blame] | 2196 | def findBuiltClang(self): |
| 2197 | """Tries to find and use Clang from the build directory as the compiler (instead of the system compiler).""" |
| 2198 | paths_to_try = [ |
| 2199 | "llvm-build/Release+Asserts/x86_64/Release+Asserts/bin/clang", |
| 2200 | "llvm-build/Debug+Asserts/x86_64/Debug+Asserts/bin/clang", |
| 2201 | "llvm-build/Release/x86_64/Release/bin/clang", |
| 2202 | "llvm-build/Debug/x86_64/Debug/bin/clang", |
| 2203 | ] |
Enrico Granata | 55d99f0 | 2015-11-19 21:45:07 +0000 | [diff] [blame] | 2204 | lldb_root_path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "..") |
Kuba Brecka | beed821 | 2014-09-04 01:03:18 +0000 | [diff] [blame] | 2205 | for p in paths_to_try: |
| 2206 | path = os.path.join(lldb_root_path, p) |
| 2207 | if os.path.exists(path): |
| 2208 | return path |
Ilia K | d995305 | 2015-03-12 07:19:41 +0000 | [diff] [blame] | 2209 | |
| 2210 | # Tries to find clang at the same folder as the lldb |
Vince Harron | 790d95c | 2015-05-18 19:39:03 +0000 | [diff] [blame] | 2211 | path = os.path.join(os.path.dirname(lldbtest_config.lldbExec), "clang") |
Ilia K | d995305 | 2015-03-12 07:19:41 +0000 | [diff] [blame] | 2212 | if os.path.exists(path): |
| 2213 | return path |
Kuba Brecka | beed821 | 2014-09-04 01:03:18 +0000 | [diff] [blame] | 2214 | |
| 2215 | return os.environ["CC"] |
| 2216 | |
Tamas Berghammer | 765b5e5 | 2015-02-25 13:26:28 +0000 | [diff] [blame] | 2217 | def getBuildFlags(self, use_cpp11=True, use_libcxx=False, use_libstdcxx=False): |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 2218 | """ Returns a dictionary (which can be provided to build* functions above) which |
| 2219 | contains OS-specific build flags. |
| 2220 | """ |
| 2221 | cflags = "" |
Tamas Berghammer | 765b5e5 | 2015-02-25 13:26:28 +0000 | [diff] [blame] | 2222 | ldflags = "" |
Daniel Malea | 9115f07 | 2013-08-06 15:02:32 +0000 | [diff] [blame] | 2223 | |
| 2224 | # On Mac OS X, unless specifically requested to use libstdc++, use libc++ |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 2225 | if not use_libstdcxx and self.platformIsDarwin(): |
Daniel Malea | 9115f07 | 2013-08-06 15:02:32 +0000 | [diff] [blame] | 2226 | use_libcxx = True |
| 2227 | |
| 2228 | if use_libcxx and self.libcxxPath: |
| 2229 | cflags += "-stdlib=libc++ " |
| 2230 | if self.libcxxPath: |
| 2231 | libcxxInclude = os.path.join(self.libcxxPath, "include") |
| 2232 | libcxxLib = os.path.join(self.libcxxPath, "lib") |
| 2233 | if os.path.isdir(libcxxInclude) and os.path.isdir(libcxxLib): |
| 2234 | cflags += "-nostdinc++ -I%s -L%s -Wl,-rpath,%s " % (libcxxInclude, libcxxLib, libcxxLib) |
| 2235 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 2236 | if use_cpp11: |
| 2237 | cflags += "-std=" |
| 2238 | if "gcc" in self.getCompiler() and "4.6" in self.getCompilerVersion(): |
| 2239 | cflags += "c++0x" |
| 2240 | else: |
| 2241 | cflags += "c++11" |
Robert Flack | 4629c4b | 2015-05-15 18:54:32 +0000 | [diff] [blame] | 2242 | if self.platformIsDarwin() or self.getPlatform() == "freebsd": |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 2243 | cflags += " -stdlib=libc++" |
Kamil Rytarowski | 49f9fb8 | 2015-12-07 21:25:57 +0000 | [diff] [blame] | 2244 | elif self.getPlatform() == "netbsd": |
| 2245 | cflags += " -stdlib=libstdc++" |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 2246 | elif "clang" in self.getCompiler(): |
| 2247 | cflags += " -stdlib=libstdc++" |
| 2248 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 2249 | return {'CFLAGS_EXTRAS' : cflags, |
| 2250 | 'LD_EXTRAS' : ldflags, |
| 2251 | } |
| 2252 | |
Johnny Chen | 9f4f5d9 | 2011-08-12 20:19:22 +0000 | [diff] [blame] | 2253 | def cleanup(self, dictionary=None): |
| 2254 | """Platform specific way to do cleanup after build.""" |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 2255 | if configuration.skip_build_and_cleanup: |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 2256 | return |
Johnny Chen | 9f4f5d9 | 2011-08-12 20:19:22 +0000 | [diff] [blame] | 2257 | module = builder_module() |
| 2258 | if not module.cleanup(self, dictionary): |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 2259 | raise Exception("Don't know how to do cleanup with dictionary: "+dictionary) |
Johnny Chen | 9f4f5d9 | 2011-08-12 20:19:22 +0000 | [diff] [blame] | 2260 | |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2261 | def getLLDBLibraryEnvVal(self): |
| 2262 | """ Returns the path that the OS-specific library search environment variable |
| 2263 | (self.dylibPath) should be set to in order for a program to find the LLDB |
| 2264 | library. If an environment variable named self.dylibPath is already set, |
| 2265 | the new path is appended to it and returned. |
| 2266 | """ |
| 2267 | existing_library_path = os.environ[self.dylibPath] if self.dylibPath in os.environ else None |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2268 | lib_dir = os.environ["LLDB_LIB_DIR"] |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2269 | if existing_library_path: |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2270 | return "%s:%s" % (existing_library_path, lib_dir) |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2271 | elif sys.platform.startswith("darwin"): |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2272 | return os.path.join(lib_dir, 'LLDB.framework') |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 2273 | else: |
Greg Clayton | 22fd3b1 | 2015-10-26 17:52:16 +0000 | [diff] [blame] | 2274 | return lib_dir |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 2275 | |
Ed Maste | 437f8f6 | 2013-09-09 14:04:04 +0000 | [diff] [blame] | 2276 | def getLibcPlusPlusLibs(self): |
Kamil Rytarowski | 49f9fb8 | 2015-12-07 21:25:57 +0000 | [diff] [blame] | 2277 | if self.getPlatform() in ('freebsd', 'linux', 'netbsd'): |
Ed Maste | 437f8f6 | 2013-09-09 14:04:04 +0000 | [diff] [blame] | 2278 | return ['libc++.so.1'] |
| 2279 | else: |
| 2280 | return ['libc++.1.dylib','libc++abi.dylib'] |
| 2281 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 2282 | # Metaclass for TestBase to change the list of test metods when a new TestCase is loaded. |
| 2283 | # We change the test methods to create a new test method for each test for each debug info we are |
| 2284 | # testing. The name of the new test method will be '<original-name>_<debug-info>' and with adding |
| 2285 | # the new test method we remove the old method at the same time. |
| 2286 | class LLDBTestCaseFactory(type): |
| 2287 | def __new__(cls, name, bases, attrs): |
| 2288 | newattrs = {} |
Zachary Turner | 606e1e3 | 2015-10-23 17:53:51 +0000 | [diff] [blame] | 2289 | for attrname, attrvalue in attrs.items(): |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 2290 | if attrname.startswith("test") and not getattr(attrvalue, "__no_debug_info_test__", False): |
| 2291 | @dsym_test |
Pavel Labath | dc8b2d3 | 2015-10-26 09:28:32 +0000 | [diff] [blame] | 2292 | @wraps(attrvalue) |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 2293 | def dsym_test_method(self, attrvalue=attrvalue): |
| 2294 | self.debug_info = "dsym" |
| 2295 | return attrvalue(self) |
| 2296 | dsym_method_name = attrname + "_dsym" |
| 2297 | dsym_test_method.__name__ = dsym_method_name |
| 2298 | newattrs[dsym_method_name] = dsym_test_method |
| 2299 | |
| 2300 | @dwarf_test |
Pavel Labath | dc8b2d3 | 2015-10-26 09:28:32 +0000 | [diff] [blame] | 2301 | @wraps(attrvalue) |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 2302 | def dwarf_test_method(self, attrvalue=attrvalue): |
| 2303 | self.debug_info = "dwarf" |
| 2304 | return attrvalue(self) |
| 2305 | dwarf_method_name = attrname + "_dwarf" |
| 2306 | dwarf_test_method.__name__ = dwarf_method_name |
| 2307 | newattrs[dwarf_method_name] = dwarf_test_method |
Tamas Berghammer | 4c0c7a7 | 2015-10-07 10:02:17 +0000 | [diff] [blame] | 2308 | |
| 2309 | @dwo_test |
Pavel Labath | dc8b2d3 | 2015-10-26 09:28:32 +0000 | [diff] [blame] | 2310 | @wraps(attrvalue) |
Tamas Berghammer | 4c0c7a7 | 2015-10-07 10:02:17 +0000 | [diff] [blame] | 2311 | def dwo_test_method(self, attrvalue=attrvalue): |
| 2312 | self.debug_info = "dwo" |
| 2313 | return attrvalue(self) |
| 2314 | dwo_method_name = attrname + "_dwo" |
| 2315 | dwo_test_method.__name__ = dwo_method_name |
| 2316 | newattrs[dwo_method_name] = dwo_test_method |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 2317 | else: |
| 2318 | newattrs[attrname] = attrvalue |
| 2319 | return super(LLDBTestCaseFactory, cls).__new__(cls, name, bases, newattrs) |
| 2320 | |
Zachary Turner | 43a01e4 | 2015-10-20 21:06:05 +0000 | [diff] [blame] | 2321 | # Setup the metaclass for this class to change the list of the test methods when a new class is loaded |
| 2322 | @add_metaclass(LLDBTestCaseFactory) |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 2323 | class TestBase(Base): |
| 2324 | """ |
| 2325 | This abstract base class is meant to be subclassed. It provides default |
| 2326 | implementations for setUpClass(), tearDownClass(), setUp(), and tearDown(), |
| 2327 | among other things. |
| 2328 | |
| 2329 | Important things for test class writers: |
| 2330 | |
| 2331 | - Overwrite the mydir class attribute, otherwise your test class won't |
| 2332 | run. It specifies the relative directory to the top level 'test' so |
| 2333 | the test harness can change to the correct working directory before |
| 2334 | running your test. |
| 2335 | |
| 2336 | - The setUp method sets up things to facilitate subsequent interactions |
| 2337 | with the debugger as part of the test. These include: |
| 2338 | - populate the test method name |
| 2339 | - create/get a debugger set with synchronous mode (self.dbg) |
| 2340 | - get the command interpreter from with the debugger (self.ci) |
| 2341 | - create a result object for use with the command interpreter |
| 2342 | (self.res) |
| 2343 | - plus other stuffs |
| 2344 | |
| 2345 | - The tearDown method tries to perform some necessary cleanup on behalf |
| 2346 | of the test to return the debugger to a good state for the next test. |
| 2347 | These include: |
| 2348 | - execute any tearDown hooks registered by the test method with |
| 2349 | TestBase.addTearDownHook(); examples can be found in |
| 2350 | settings/TestSettings.py |
| 2351 | - kill the inferior process associated with each target, if any, |
| 2352 | and, then delete the target from the debugger's target list |
| 2353 | - perform build cleanup before running the next test method in the |
| 2354 | same test class; examples of registering for this service can be |
| 2355 | found in types/TestIntegerTypes.py with the call: |
| 2356 | - self.setTearDownCleanup(dictionary=d) |
| 2357 | |
| 2358 | - Similarly setUpClass and tearDownClass perform classwise setup and |
| 2359 | teardown fixtures. The tearDownClass method invokes a default build |
| 2360 | cleanup for the entire test class; also, subclasses can implement the |
| 2361 | classmethod classCleanup(cls) to perform special class cleanup action. |
| 2362 | |
| 2363 | - The instance methods runCmd and expect are used heavily by existing |
| 2364 | test cases to send a command to the command interpreter and to perform |
| 2365 | string/pattern matching on the output of such command execution. The |
| 2366 | expect method also provides a mode to peform string/pattern matching |
| 2367 | without running a command. |
| 2368 | |
| 2369 | - The build methods buildDefault, buildDsym, and buildDwarf are used to |
| 2370 | build the binaries used during a particular test scenario. A plugin |
| 2371 | should be provided for the sys.platform running the test suite. The |
| 2372 | Mac OS X implementation is located in plugins/darwin.py. |
| 2373 | """ |
| 2374 | |
| 2375 | # Maximum allowed attempts when launching the inferior process. |
| 2376 | # Can be overridden by the LLDB_MAX_LAUNCH_COUNT environment variable. |
| 2377 | maxLaunchCount = 3; |
| 2378 | |
| 2379 | # Time to wait before the next launching attempt in second(s). |
| 2380 | # Can be overridden by the LLDB_TIME_WAIT_NEXT_LAUNCH environment variable. |
| 2381 | timeWaitNextLaunch = 1.0; |
| 2382 | |
| 2383 | def doDelay(self): |
| 2384 | """See option -w of dotest.py.""" |
| 2385 | if ("LLDB_WAIT_BETWEEN_TEST_CASES" in os.environ and |
| 2386 | os.environ["LLDB_WAIT_BETWEEN_TEST_CASES"] == 'YES'): |
| 2387 | waitTime = 1.0 |
| 2388 | if "LLDB_TIME_WAIT_BETWEEN_TEST_CASES" in os.environ: |
| 2389 | waitTime = float(os.environ["LLDB_TIME_WAIT_BETWEEN_TEST_CASES"]) |
| 2390 | time.sleep(waitTime) |
| 2391 | |
Enrico Granata | 165f8af | 2012-09-21 19:10:53 +0000 | [diff] [blame] | 2392 | # Returns the list of categories to which this test case belongs |
| 2393 | # by default, look for a ".categories" file, and read its contents |
| 2394 | # if no such file exists, traverse the hierarchy - we guarantee |
| 2395 | # a .categories to exist at the top level directory so we do not end up |
| 2396 | # looping endlessly - subclasses are free to define their own categories |
| 2397 | # in whatever way makes sense to them |
| 2398 | def getCategories(self): |
| 2399 | import inspect |
| 2400 | import os.path |
| 2401 | folder = inspect.getfile(self.__class__) |
| 2402 | folder = os.path.dirname(folder) |
| 2403 | while folder != '/': |
| 2404 | categories_file_name = os.path.join(folder,".categories") |
| 2405 | if os.path.exists(categories_file_name): |
| 2406 | categories_file = open(categories_file_name,'r') |
| 2407 | categories = categories_file.readline() |
| 2408 | categories_file.close() |
| 2409 | categories = str.replace(categories,'\n','') |
| 2410 | categories = str.replace(categories,'\r','') |
| 2411 | return categories.split(',') |
| 2412 | else: |
| 2413 | folder = os.path.dirname(folder) |
| 2414 | continue |
| 2415 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 2416 | def setUp(self): |
| 2417 | #import traceback |
| 2418 | #traceback.print_stack() |
| 2419 | |
| 2420 | # Works with the test driver to conditionally skip tests via decorators. |
| 2421 | Base.setUp(self) |
| 2422 | |
Johnny Chen | ed49202 | 2011-06-21 00:53:00 +0000 | [diff] [blame] | 2423 | # Insert some delay between successive test cases if specified. |
| 2424 | self.doDelay() |
Johnny Chen | 0ed37c9 | 2010-10-07 02:04:14 +0000 | [diff] [blame] | 2425 | |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2426 | if "LLDB_MAX_LAUNCH_COUNT" in os.environ: |
| 2427 | self.maxLaunchCount = int(os.environ["LLDB_MAX_LAUNCH_COUNT"]) |
| 2428 | |
Johnny Chen | 430eb76 | 2010-10-19 16:00:42 +0000 | [diff] [blame] | 2429 | if "LLDB_TIME_WAIT_NEXT_LAUNCH" in os.environ: |
Johnny Chen | 4921b11 | 2010-11-29 20:20:34 +0000 | [diff] [blame] | 2430 | self.timeWaitNextLaunch = float(os.environ["LLDB_TIME_WAIT_NEXT_LAUNCH"]) |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2431 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 2432 | # We want our debugger to be synchronous. |
| 2433 | self.dbg.SetAsync(False) |
| 2434 | |
| 2435 | # Retrieve the associated command interpreter instance. |
| 2436 | self.ci = self.dbg.GetCommandInterpreter() |
| 2437 | if not self.ci: |
| 2438 | raise Exception('Could not get the command interpreter') |
| 2439 | |
| 2440 | # And the result object. |
| 2441 | self.res = lldb.SBCommandReturnObject() |
| 2442 | |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 2443 | if lldb.remote_platform and configuration.lldb_platform_working_dir: |
Chaoren Lin | 3e2bdb4 | 2015-05-11 17:53:39 +0000 | [diff] [blame] | 2444 | remote_test_dir = lldbutil.join_remote_paths( |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 2445 | configuration.lldb_platform_working_dir, |
Chaoren Lin | 3e2bdb4 | 2015-05-11 17:53:39 +0000 | [diff] [blame] | 2446 | self.getArchitecture(), |
| 2447 | str(self.test_number), |
| 2448 | self.mydir) |
Zachary Turner | 1411668 | 2015-10-26 18:48:14 +0000 | [diff] [blame] | 2449 | error = lldb.remote_platform.MakeDirectory(remote_test_dir, 448) # 448 = 0o700 |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2450 | if error.Success(): |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2451 | lldb.remote_platform.SetWorkingDirectory(remote_test_dir) |
Tamas Berghammer | f2addf8 | 2015-10-07 12:38:29 +0000 | [diff] [blame] | 2452 | |
Tamas Berghammer | 11db2d3 | 2015-10-07 14:52:16 +0000 | [diff] [blame] | 2453 | # This function removes all files from the current working directory while leaving |
| 2454 | # the directories in place. The cleaup is required to reduce the disk space required |
| 2455 | # by the test suit while leaving the directories untached is neccessary because |
| 2456 | # sub-directories might belong to an other test |
| 2457 | def clean_working_directory(): |
Tamas Berghammer | f2addf8 | 2015-10-07 12:38:29 +0000 | [diff] [blame] | 2458 | # TODO: Make it working on Windows when we need it for remote debugging support |
Tamas Berghammer | 11db2d3 | 2015-10-07 14:52:16 +0000 | [diff] [blame] | 2459 | # TODO: Replace the heuristic to remove the files with a logic what collects the |
| 2460 | # list of files we have to remove during test runs. |
| 2461 | shell_cmd = lldb.SBPlatformShellCommand("rm %s/*" % remote_test_dir) |
Tamas Berghammer | f2addf8 | 2015-10-07 12:38:29 +0000 | [diff] [blame] | 2462 | lldb.remote_platform.Run(shell_cmd) |
Tamas Berghammer | 11db2d3 | 2015-10-07 14:52:16 +0000 | [diff] [blame] | 2463 | self.addTearDownHook(clean_working_directory) |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2464 | else: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2465 | print("error: making remote directory '%s': %s" % (remote_test_dir, error)) |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 2466 | |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 2467 | def registerSharedLibrariesWithTarget(self, target, shlibs): |
| 2468 | '''If we are remotely running the test suite, register the shared libraries with the target so they get uploaded, otherwise do nothing |
| 2469 | |
| 2470 | Any modules in the target that have their remote install file specification set will |
| 2471 | get uploaded to the remote host. This function registers the local copies of the |
| 2472 | shared libraries with the target and sets their remote install locations so they will |
| 2473 | be uploaded when the target is run. |
| 2474 | ''' |
Zachary Turner | be40b2f | 2014-12-02 21:32:44 +0000 | [diff] [blame] | 2475 | if not shlibs or not self.platformContext: |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 2476 | return None |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 2477 | |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 2478 | shlib_environment_var = self.platformContext.shlib_environment_var |
| 2479 | shlib_prefix = self.platformContext.shlib_prefix |
| 2480 | shlib_extension = '.' + self.platformContext.shlib_extension |
| 2481 | |
| 2482 | working_dir = self.get_process_working_directory() |
| 2483 | environment = ['%s=%s' % (shlib_environment_var, working_dir)] |
| 2484 | # Add any shared libraries to our target if remote so they get |
| 2485 | # uploaded into the working directory on the remote side |
| 2486 | for name in shlibs: |
| 2487 | # The path can be a full path to a shared library, or a make file name like "Foo" for |
| 2488 | # "libFoo.dylib" or "libFoo.so", or "Foo.so" for "Foo.so" or "libFoo.so", or just a |
| 2489 | # basename like "libFoo.so". So figure out which one it is and resolve the local copy |
| 2490 | # of the shared library accordingly |
| 2491 | if os.path.exists(name): |
| 2492 | local_shlib_path = name # name is the full path to the local shared library |
| 2493 | else: |
| 2494 | # Check relative names |
| 2495 | local_shlib_path = os.path.join(os.getcwd(), shlib_prefix + name + shlib_extension) |
| 2496 | if not os.path.exists(local_shlib_path): |
| 2497 | local_shlib_path = os.path.join(os.getcwd(), name + shlib_extension) |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 2498 | if not os.path.exists(local_shlib_path): |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 2499 | local_shlib_path = os.path.join(os.getcwd(), name) |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 2500 | |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 2501 | # Make sure we found the local shared library in the above code |
| 2502 | self.assertTrue(os.path.exists(local_shlib_path)) |
| 2503 | |
| 2504 | # Add the shared library to our target |
| 2505 | shlib_module = target.AddModule(local_shlib_path, None, None, None) |
| 2506 | if lldb.remote_platform: |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 2507 | # We must set the remote install location if we want the shared library |
| 2508 | # to get uploaded to the remote target |
Chaoren Lin | 5d76b1b | 2015-06-06 00:25:50 +0000 | [diff] [blame] | 2509 | remote_shlib_path = lldbutil.append_to_process_working_directory(os.path.basename(local_shlib_path)) |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 2510 | shlib_module.SetRemoteInstallFileSpec(lldb.SBFileSpec(remote_shlib_path, False)) |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 2511 | |
| 2512 | return environment |
| 2513 | |
Enrico Granata | 4481816 | 2012-10-24 01:23:57 +0000 | [diff] [blame] | 2514 | # utility methods that tests can use to access the current objects |
| 2515 | def target(self): |
| 2516 | if not self.dbg: |
| 2517 | raise Exception('Invalid debugger instance') |
| 2518 | return self.dbg.GetSelectedTarget() |
| 2519 | |
| 2520 | def process(self): |
| 2521 | if not self.dbg: |
| 2522 | raise Exception('Invalid debugger instance') |
| 2523 | return self.dbg.GetSelectedTarget().GetProcess() |
| 2524 | |
| 2525 | def thread(self): |
| 2526 | if not self.dbg: |
| 2527 | raise Exception('Invalid debugger instance') |
| 2528 | return self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread() |
| 2529 | |
| 2530 | def frame(self): |
| 2531 | if not self.dbg: |
| 2532 | raise Exception('Invalid debugger instance') |
| 2533 | return self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame() |
| 2534 | |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 2535 | def get_process_working_directory(self): |
| 2536 | '''Get the working directory that should be used when launching processes for local or remote processes.''' |
| 2537 | if lldb.remote_platform: |
| 2538 | # Remote tests set the platform working directory up in TestBase.setUp() |
| 2539 | return lldb.remote_platform.GetWorkingDirectory() |
| 2540 | else: |
| 2541 | # local tests change directory into each test subdirectory |
| 2542 | return os.getcwd() |
| 2543 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 2544 | def tearDown(self): |
Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame] | 2545 | #import traceback |
| 2546 | #traceback.print_stack() |
| 2547 | |
Adrian McCarthy | 6ecdbc8 | 2015-10-15 22:39:55 +0000 | [diff] [blame] | 2548 | # Ensure all the references to SB objects have gone away so that we can |
| 2549 | # be sure that all test-specific resources have been freed before we |
| 2550 | # attempt to delete the targets. |
| 2551 | gc.collect() |
| 2552 | |
Johnny Chen | 3794ad9 | 2011-06-15 21:24:24 +0000 | [diff] [blame] | 2553 | # Delete the target(s) from the debugger as a general cleanup step. |
| 2554 | # This includes terminating the process for each target, if any. |
| 2555 | # We'd like to reuse the debugger for our next test without incurring |
| 2556 | # the initialization overhead. |
| 2557 | targets = [] |
| 2558 | for target in self.dbg: |
| 2559 | if target: |
| 2560 | targets.append(target) |
| 2561 | process = target.GetProcess() |
| 2562 | if process: |
| 2563 | rc = self.invoke(process, "Kill") |
| 2564 | self.assertTrue(rc.Success(), PROCESS_KILLED) |
| 2565 | for target in targets: |
| 2566 | self.dbg.DeleteTarget(target) |
Johnny Chen | 6ca006c | 2010-08-16 21:28:10 +0000 | [diff] [blame] | 2567 | |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 2568 | # Do this last, to make sure it's in reverse order from how we setup. |
| 2569 | Base.tearDown(self) |
| 2570 | |
Zachary Turner | 9581204 | 2015-03-26 18:54:21 +0000 | [diff] [blame] | 2571 | # This must be the last statement, otherwise teardown hooks or other |
| 2572 | # lines might depend on this still being active. |
| 2573 | del self.dbg |
| 2574 | |
Johnny Chen | 86268e4 | 2011-09-30 21:48:35 +0000 | [diff] [blame] | 2575 | def switch_to_thread_with_stop_reason(self, stop_reason): |
| 2576 | """ |
| 2577 | Run the 'thread list' command, and select the thread with stop reason as |
| 2578 | 'stop_reason'. If no such thread exists, no select action is done. |
| 2579 | """ |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 2580 | from .lldbutil import stop_reason_to_str |
Johnny Chen | 86268e4 | 2011-09-30 21:48:35 +0000 | [diff] [blame] | 2581 | self.runCmd('thread list') |
| 2582 | output = self.res.GetOutput() |
| 2583 | thread_line_pattern = re.compile("^[ *] thread #([0-9]+):.*stop reason = %s" % |
| 2584 | stop_reason_to_str(stop_reason)) |
| 2585 | for line in output.splitlines(): |
| 2586 | matched = thread_line_pattern.match(line) |
| 2587 | if matched: |
| 2588 | self.runCmd('thread select %s' % matched.group(1)) |
| 2589 | |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 2590 | def runCmd(self, cmd, msg=None, check=True, trace=False, inHistory=False): |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2591 | """ |
| 2592 | Ask the command interpreter to handle the command and then check its |
| 2593 | return status. |
| 2594 | """ |
| 2595 | # Fail fast if 'cmd' is not meaningful. |
| 2596 | if not cmd or len(cmd) == 0: |
| 2597 | raise Exception("Bad 'cmd' parameter encountered") |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2598 | |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2599 | trace = (True if traceAlways else trace) |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 2600 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2601 | if cmd.startswith("target create "): |
| 2602 | cmd = cmd.replace("target create ", "file ") |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 2603 | |
Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 2604 | running = (cmd.startswith("run") or cmd.startswith("process launch")) |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2605 | |
Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 2606 | for i in range(self.maxLaunchCount if running else 1): |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 2607 | self.ci.HandleCommand(cmd, self.res, inHistory) |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2608 | |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2609 | with recording(self, trace) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2610 | print("runCmd:", cmd, file=sbuf) |
Johnny Chen | ab254f5 | 2010-10-15 16:13:00 +0000 | [diff] [blame] | 2611 | if not check: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2612 | print("check of return status not required", file=sbuf) |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2613 | if self.res.Succeeded(): |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2614 | print("output:", self.res.GetOutput(), file=sbuf) |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2615 | else: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2616 | print("runCmd failed!", file=sbuf) |
| 2617 | print(self.res.GetError(), file=sbuf) |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2618 | |
Johnny Chen | ff3d01d | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 2619 | if self.res.Succeeded(): |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2620 | break |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2621 | elif running: |
Johnny Chen | cf7f74e | 2011-01-19 02:02:08 +0000 | [diff] [blame] | 2622 | # For process launch, wait some time before possible next try. |
| 2623 | time.sleep(self.timeWaitNextLaunch) |
Johnny Chen | 552d671 | 2012-08-01 19:56:04 +0000 | [diff] [blame] | 2624 | with recording(self, trace) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2625 | print("Command '" + cmd + "' failed!", file=sbuf) |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2626 | |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2627 | if check: |
Sean Callanan | 05834cd | 2015-07-01 23:56:30 +0000 | [diff] [blame] | 2628 | self.assertTrue(self.res.Succeeded(), |
| 2629 | msg if msg else CMD_MSG(cmd)) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2630 | |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 2631 | def match (self, str, patterns, msg=None, trace=False, error=False, matching=True, exe=True): |
| 2632 | """run command in str, and match the result against regexp in patterns returning the match object for the first matching pattern |
| 2633 | |
| 2634 | Otherwise, all the arguments have the same meanings as for the expect function""" |
| 2635 | |
| 2636 | trace = (True if traceAlways else trace) |
| 2637 | |
| 2638 | if exe: |
| 2639 | # First run the command. If we are expecting error, set check=False. |
| 2640 | # Pass the assert message along since it provides more semantic info. |
| 2641 | self.runCmd(str, msg=msg, trace = (True if trace else False), check = not error) |
| 2642 | |
| 2643 | # Then compare the output against expected strings. |
| 2644 | output = self.res.GetError() if error else self.res.GetOutput() |
| 2645 | |
| 2646 | # If error is True, the API client expects the command to fail! |
| 2647 | if error: |
| 2648 | self.assertFalse(self.res.Succeeded(), |
| 2649 | "Command '" + str + "' is expected to fail!") |
| 2650 | else: |
| 2651 | # No execution required, just compare str against the golden input. |
| 2652 | output = str |
| 2653 | with recording(self, trace) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2654 | print("looking at:", output, file=sbuf) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 2655 | |
| 2656 | # The heading says either "Expecting" or "Not expecting". |
| 2657 | heading = "Expecting" if matching else "Not expecting" |
| 2658 | |
| 2659 | for pattern in patterns: |
| 2660 | # Match Objects always have a boolean value of True. |
| 2661 | match_object = re.search(pattern, output) |
| 2662 | matched = bool(match_object) |
| 2663 | with recording(self, trace) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2664 | print("%s pattern: %s" % (heading, pattern), file=sbuf) |
| 2665 | print("Matched" if matched else "Not matched", file=sbuf) |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 2666 | if matched: |
| 2667 | break |
| 2668 | |
| 2669 | self.assertTrue(matched if matching else not matched, |
| 2670 | msg if msg else EXP_MSG(str, exe)) |
| 2671 | |
| 2672 | return match_object |
| 2673 | |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 2674 | def expect(self, str, msg=None, patterns=None, startstr=None, endstr=None, substrs=None, trace=False, error=False, matching=True, exe=True, inHistory=False): |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2675 | """ |
| 2676 | Similar to runCmd; with additional expect style output matching ability. |
| 2677 | |
| 2678 | Ask the command interpreter to handle the command and then check its |
| 2679 | return status. The 'msg' parameter specifies an informational assert |
| 2680 | message. We expect the output from running the command to start with |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2681 | 'startstr', matches the substrings contained in 'substrs', and regexp |
| 2682 | matches the patterns contained in 'patterns'. |
Johnny Chen | b330786 | 2010-09-17 22:28:51 +0000 | [diff] [blame] | 2683 | |
| 2684 | If the keyword argument error is set to True, it signifies that the API |
| 2685 | client is expecting the command to fail. In this case, the error stream |
Johnny Chen | aa90292 | 2010-09-17 22:45:27 +0000 | [diff] [blame] | 2686 | from running the command is retrieved and compared against the golden |
Johnny Chen | b330786 | 2010-09-17 22:28:51 +0000 | [diff] [blame] | 2687 | input, instead. |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2688 | |
| 2689 | If the keyword argument matching is set to False, it signifies that the API |
| 2690 | client is expecting the output of the command not to match the golden |
| 2691 | input. |
Johnny Chen | 9c48b8d | 2010-09-21 23:33:30 +0000 | [diff] [blame] | 2692 | |
| 2693 | Finally, the required argument 'str' represents the lldb command to be |
| 2694 | sent to the command interpreter. In case the keyword argument 'exe' is |
| 2695 | set to False, the 'str' is treated as a string to be matched/not-matched |
| 2696 | against the golden input. |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2697 | """ |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2698 | trace = (True if traceAlways else trace) |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 2699 | |
Johnny Chen | 9c48b8d | 2010-09-21 23:33:30 +0000 | [diff] [blame] | 2700 | if exe: |
| 2701 | # First run the command. If we are expecting error, set check=False. |
Johnny Chen | 62d4f86 | 2010-10-28 21:10:32 +0000 | [diff] [blame] | 2702 | # Pass the assert message along since it provides more semantic info. |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 2703 | self.runCmd(str, msg=msg, trace = (True if trace else False), check = not error, inHistory=inHistory) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2704 | |
Johnny Chen | 9c48b8d | 2010-09-21 23:33:30 +0000 | [diff] [blame] | 2705 | # Then compare the output against expected strings. |
| 2706 | output = self.res.GetError() if error else self.res.GetOutput() |
Johnny Chen | b330786 | 2010-09-17 22:28:51 +0000 | [diff] [blame] | 2707 | |
Johnny Chen | 9c48b8d | 2010-09-21 23:33:30 +0000 | [diff] [blame] | 2708 | # If error is True, the API client expects the command to fail! |
| 2709 | if error: |
| 2710 | self.assertFalse(self.res.Succeeded(), |
| 2711 | "Command '" + str + "' is expected to fail!") |
| 2712 | else: |
| 2713 | # No execution required, just compare str against the golden input. |
Enrico Granata | bc08ab4 | 2012-10-23 00:09:02 +0000 | [diff] [blame] | 2714 | if isinstance(str,lldb.SBCommandReturnObject): |
| 2715 | output = str.GetOutput() |
| 2716 | else: |
| 2717 | output = str |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2718 | with recording(self, trace) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2719 | print("looking at:", output, file=sbuf) |
Johnny Chen | b330786 | 2010-09-17 22:28:51 +0000 | [diff] [blame] | 2720 | |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2721 | # The heading says either "Expecting" or "Not expecting". |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2722 | heading = "Expecting" if matching else "Not expecting" |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2723 | |
| 2724 | # Start from the startstr, if specified. |
| 2725 | # If there's no startstr, set the initial state appropriately. |
| 2726 | matched = output.startswith(startstr) if startstr else (True if matching else False) |
Johnny Chen | b145bba | 2010-08-20 18:25:15 +0000 | [diff] [blame] | 2727 | |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2728 | if startstr: |
| 2729 | with recording(self, trace) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2730 | print("%s start string: %s" % (heading, startstr), file=sbuf) |
| 2731 | print("Matched" if matched else "Not matched", file=sbuf) |
Johnny Chen | b145bba | 2010-08-20 18:25:15 +0000 | [diff] [blame] | 2732 | |
Johnny Chen | 86268e4 | 2011-09-30 21:48:35 +0000 | [diff] [blame] | 2733 | # Look for endstr, if specified. |
| 2734 | keepgoing = matched if matching else not matched |
| 2735 | if endstr: |
| 2736 | matched = output.endswith(endstr) |
| 2737 | with recording(self, trace) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2738 | print("%s end string: %s" % (heading, endstr), file=sbuf) |
| 2739 | print("Matched" if matched else "Not matched", file=sbuf) |
Johnny Chen | 86268e4 | 2011-09-30 21:48:35 +0000 | [diff] [blame] | 2740 | |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2741 | # Look for sub strings, if specified. |
| 2742 | keepgoing = matched if matching else not matched |
| 2743 | if substrs and keepgoing: |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2744 | for str in substrs: |
Johnny Chen | b052f6c | 2010-09-23 23:35:28 +0000 | [diff] [blame] | 2745 | matched = output.find(str) != -1 |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2746 | with recording(self, trace) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2747 | print("%s sub string: %s" % (heading, str), file=sbuf) |
| 2748 | print("Matched" if matched else "Not matched", file=sbuf) |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2749 | keepgoing = matched if matching else not matched |
| 2750 | if not keepgoing: |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2751 | break |
| 2752 | |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2753 | # Search for regular expression patterns, if specified. |
| 2754 | keepgoing = matched if matching else not matched |
| 2755 | if patterns and keepgoing: |
| 2756 | for pattern in patterns: |
| 2757 | # Match Objects always have a boolean value of True. |
| 2758 | matched = bool(re.search(pattern, output)) |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2759 | with recording(self, trace) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2760 | print("%s pattern: %s" % (heading, pattern), file=sbuf) |
| 2761 | print("Matched" if matched else "Not matched", file=sbuf) |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2762 | keepgoing = matched if matching else not matched |
| 2763 | if not keepgoing: |
| 2764 | break |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2765 | |
| 2766 | self.assertTrue(matched if matching else not matched, |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 2767 | msg if msg else EXP_MSG(str, exe)) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2768 | |
Johnny Chen | f3c5923 | 2010-08-25 22:52:45 +0000 | [diff] [blame] | 2769 | def invoke(self, obj, name, trace=False): |
Johnny Chen | 61703c9 | 2010-08-25 22:56:10 +0000 | [diff] [blame] | 2770 | """Use reflection to call a method dynamically with no argument.""" |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2771 | trace = (True if traceAlways else trace) |
Johnny Chen | f3c5923 | 2010-08-25 22:52:45 +0000 | [diff] [blame] | 2772 | |
| 2773 | method = getattr(obj, name) |
| 2774 | import inspect |
| 2775 | self.assertTrue(inspect.ismethod(method), |
| 2776 | name + "is a method name of object: " + str(obj)) |
| 2777 | result = method() |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2778 | with recording(self, trace) as sbuf: |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2779 | print(str(method) + ":", result, file=sbuf) |
Johnny Chen | f3c5923 | 2010-08-25 22:52:45 +0000 | [diff] [blame] | 2780 | return result |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 2781 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 2782 | def build(self, architecture=None, compiler=None, dictionary=None, clean=True): |
| 2783 | """Platform specific way to build the default binaries.""" |
Zachary Turner | 606e3a5 | 2015-12-08 01:15:30 +0000 | [diff] [blame] | 2784 | if configuration.skip_build_and_cleanup: |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 2785 | return |
| 2786 | module = builder_module() |
| 2787 | if target_is_android(): |
| 2788 | dictionary = append_android_envs(dictionary) |
| 2789 | if self.debug_info is None: |
| 2790 | return self.buildDefault(architecture, compiler, dictionary, clean) |
| 2791 | elif self.debug_info == "dsym": |
| 2792 | return self.buildDsym(architecture, compiler, dictionary, clean) |
| 2793 | elif self.debug_info == "dwarf": |
| 2794 | return self.buildDwarf(architecture, compiler, dictionary, clean) |
Tamas Berghammer | 4c0c7a7 | 2015-10-07 10:02:17 +0000 | [diff] [blame] | 2795 | elif self.debug_info == "dwo": |
| 2796 | return self.buildDwo(architecture, compiler, dictionary, clean) |
| 2797 | else: |
| 2798 | self.fail("Can't build for debug info: %s" % self.debug_info) |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 2799 | |
Johnny Chen | f359cf2 | 2011-05-27 23:36:52 +0000 | [diff] [blame] | 2800 | # ================================================= |
| 2801 | # Misc. helper methods for debugging test execution |
| 2802 | # ================================================= |
| 2803 | |
Johnny Chen | 56b92a7 | 2011-07-11 19:15:11 +0000 | [diff] [blame] | 2804 | def DebugSBValue(self, val): |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2805 | """Debug print a SBValue object, if traceAlways is True.""" |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 2806 | from .lldbutil import value_type_to_str |
Johnny Chen | 87bb589 | 2010-11-03 21:37:58 +0000 | [diff] [blame] | 2807 | |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2808 | if not traceAlways: |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 2809 | return |
| 2810 | |
| 2811 | err = sys.stderr |
| 2812 | err.write(val.GetName() + ":\n") |
Johnny Chen | 86268e4 | 2011-09-30 21:48:35 +0000 | [diff] [blame] | 2813 | err.write('\t' + "TypeName -> " + val.GetTypeName() + '\n') |
| 2814 | err.write('\t' + "ByteSize -> " + str(val.GetByteSize()) + '\n') |
| 2815 | err.write('\t' + "NumChildren -> " + str(val.GetNumChildren()) + '\n') |
| 2816 | err.write('\t' + "Value -> " + str(val.GetValue()) + '\n') |
| 2817 | err.write('\t' + "ValueAsUnsigned -> " + str(val.GetValueAsUnsigned())+ '\n') |
| 2818 | err.write('\t' + "ValueType -> " + value_type_to_str(val.GetValueType()) + '\n') |
| 2819 | err.write('\t' + "Summary -> " + str(val.GetSummary()) + '\n') |
| 2820 | err.write('\t' + "IsPointerType -> " + str(val.TypeIsPointerType()) + '\n') |
| 2821 | err.write('\t' + "Location -> " + val.GetLocation() + '\n') |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 2822 | |
Johnny Chen | 36c5eb1 | 2011-08-05 20:17:27 +0000 | [diff] [blame] | 2823 | def DebugSBType(self, type): |
| 2824 | """Debug print a SBType object, if traceAlways is True.""" |
| 2825 | if not traceAlways: |
| 2826 | return |
| 2827 | |
| 2828 | err = sys.stderr |
| 2829 | err.write(type.GetName() + ":\n") |
| 2830 | err.write('\t' + "ByteSize -> " + str(type.GetByteSize()) + '\n') |
| 2831 | err.write('\t' + "IsPointerType -> " + str(type.IsPointerType()) + '\n') |
| 2832 | err.write('\t' + "IsReferenceType -> " + str(type.IsReferenceType()) + '\n') |
| 2833 | |
Johnny Chen | b877f1e | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 2834 | def DebugPExpect(self, child): |
| 2835 | """Debug the spwaned pexpect object.""" |
| 2836 | if not traceAlways: |
| 2837 | return |
| 2838 | |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 2839 | print(child) |
Filipe Cabecinhas | 0eec15a | 2012-06-20 10:13:40 +0000 | [diff] [blame] | 2840 | |
| 2841 | @classmethod |
| 2842 | def RemoveTempFile(cls, file): |
| 2843 | if os.path.exists(file): |
| 2844 | os.remove(file) |