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 | |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 34 | import abc |
Johnny Chen | 90312a8 | 2010-09-21 22:34:45 +0000 | [diff] [blame] | 35 | import os, sys, traceback |
Enrico Granata | 7e137e3 | 2012-10-24 18:14:21 +0000 | [diff] [blame] | 36 | import os.path |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 37 | import re |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 38 | import signal |
Johnny Chen | 8952a2d | 2010-08-30 21:35:00 +0000 | [diff] [blame] | 39 | from subprocess import * |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 40 | import StringIO |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 41 | import time |
Johnny Chen | a33a93c | 2010-08-30 23:08:52 +0000 | [diff] [blame] | 42 | import types |
Johnny Chen | 7325883 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 43 | import unittest2 |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 44 | import lldb |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 45 | from _pyio import __metaclass__ |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 46 | |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 47 | # See also dotest.parseOptionsAndInitTestdirs(), where the environment variables |
Johnny Chen | d2047fa | 2011-01-19 18:18:47 +0000 | [diff] [blame] | 48 | # 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] | 49 | |
| 50 | # By default, traceAlways is False. |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 51 | if "LLDB_COMMAND_TRACE" in os.environ and os.environ["LLDB_COMMAND_TRACE"]=="YES": |
| 52 | traceAlways = True |
| 53 | else: |
| 54 | traceAlways = False |
| 55 | |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 56 | # By default, doCleanup is True. |
| 57 | if "LLDB_DO_CLEANUP" in os.environ and os.environ["LLDB_DO_CLEANUP"]=="NO": |
| 58 | doCleanup = False |
| 59 | else: |
| 60 | doCleanup = True |
| 61 | |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 62 | |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 63 | # |
| 64 | # Some commonly used assert messages. |
| 65 | # |
| 66 | |
Johnny Chen | aa90292 | 2010-09-17 22:45:27 +0000 | [diff] [blame] | 67 | COMMAND_FAILED_AS_EXPECTED = "Command has failed as expected" |
| 68 | |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 69 | CURRENT_EXECUTABLE_SET = "Current executable set successfully" |
| 70 | |
Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame] | 71 | PROCESS_IS_VALID = "Process is valid" |
| 72 | |
| 73 | PROCESS_KILLED = "Process is killed successfully" |
| 74 | |
Johnny Chen | d5f66fc | 2010-12-23 01:12:19 +0000 | [diff] [blame] | 75 | PROCESS_EXITED = "Process exited successfully" |
| 76 | |
| 77 | PROCESS_STOPPED = "Process status should be stopped" |
| 78 | |
Johnny Chen | 5ee8819 | 2010-08-27 23:47:36 +0000 | [diff] [blame] | 79 | RUN_SUCCEEDED = "Process is launched successfully" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 80 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 81 | RUN_COMPLETED = "Process exited successfully" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 82 | |
Johnny Chen | 67af43f | 2010-10-05 19:27:32 +0000 | [diff] [blame] | 83 | BACKTRACE_DISPLAYED_CORRECTLY = "Backtrace displayed correctly" |
| 84 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 85 | BREAKPOINT_CREATED = "Breakpoint created successfully" |
| 86 | |
Johnny Chen | f10af38 | 2010-12-04 00:07:24 +0000 | [diff] [blame] | 87 | BREAKPOINT_STATE_CORRECT = "Breakpoint state is correct" |
| 88 | |
Johnny Chen | e76896c | 2010-08-17 21:33:31 +0000 | [diff] [blame] | 89 | BREAKPOINT_PENDING_CREATED = "Pending breakpoint created successfully" |
| 90 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 91 | BREAKPOINT_HIT_ONCE = "Breakpoint resolved with hit cout = 1" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 92 | |
Johnny Chen | 703dbd0 | 2010-09-30 17:06:24 +0000 | [diff] [blame] | 93 | BREAKPOINT_HIT_TWICE = "Breakpoint resolved with hit cout = 2" |
| 94 | |
Johnny Chen | 164f1e1 | 2010-10-15 18:07:09 +0000 | [diff] [blame] | 95 | BREAKPOINT_HIT_THRICE = "Breakpoint resolved with hit cout = 3" |
| 96 | |
Greg Clayton | 5db6b79 | 2012-10-24 18:24:14 +0000 | [diff] [blame] | 97 | MISSING_EXPECTED_REGISTERS = "At least one expected register is unavailable." |
| 98 | |
Johnny Chen | 89109ed1 | 2011-06-27 20:05:23 +0000 | [diff] [blame] | 99 | OBJECT_PRINTED_CORRECTLY = "Object printed correctly" |
| 100 | |
Johnny Chen | 5b3a357 | 2010-12-09 18:22:12 +0000 | [diff] [blame] | 101 | SOURCE_DISPLAYED_CORRECTLY = "Source code displayed correctly" |
| 102 | |
Johnny Chen | c70b02a | 2010-09-22 23:00:20 +0000 | [diff] [blame] | 103 | STEP_OUT_SUCCEEDED = "Thread step-out succeeded" |
| 104 | |
Johnny Chen | 1691a16 | 2011-04-15 16:44:48 +0000 | [diff] [blame] | 105 | STOPPED_DUE_TO_EXC_BAD_ACCESS = "Process should be stopped due to bad access exception" |
| 106 | |
Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 107 | STOPPED_DUE_TO_ASSERT = "Process should be stopped due to an assertion" |
| 108 | |
Johnny Chen | 5d6c464 | 2010-11-10 23:46:38 +0000 | [diff] [blame] | 109 | STOPPED_DUE_TO_BREAKPOINT = "Process should be stopped due to breakpoint" |
Johnny Chen | de0338b | 2010-11-10 20:20:06 +0000 | [diff] [blame] | 110 | |
Johnny Chen | 5d6c464 | 2010-11-10 23:46:38 +0000 | [diff] [blame] | 111 | STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS = "%s, %s" % ( |
| 112 | STOPPED_DUE_TO_BREAKPOINT, "instead, the actual stop reason is: '%s'") |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 113 | |
Johnny Chen | 2e431ce | 2010-10-20 18:38:48 +0000 | [diff] [blame] | 114 | STOPPED_DUE_TO_BREAKPOINT_CONDITION = "Stopped due to breakpoint condition" |
| 115 | |
Johnny Chen | 0a3d1ca | 2010-12-13 21:49:58 +0000 | [diff] [blame] | 116 | STOPPED_DUE_TO_BREAKPOINT_IGNORE_COUNT = "Stopped due to breakpoint and ignore count" |
| 117 | |
Johnny Chen | c066ab4 | 2010-10-14 01:22:03 +0000 | [diff] [blame] | 118 | STOPPED_DUE_TO_SIGNAL = "Process state is stopped due to signal" |
| 119 | |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 120 | STOPPED_DUE_TO_STEP_IN = "Process state is stopped due to step in" |
| 121 | |
Johnny Chen | f68cc12 | 2011-09-15 21:09:59 +0000 | [diff] [blame] | 122 | STOPPED_DUE_TO_WATCHPOINT = "Process should be stopped due to watchpoint" |
| 123 | |
Johnny Chen | 3c884a0 | 2010-08-24 22:07:56 +0000 | [diff] [blame] | 124 | DATA_TYPES_DISPLAYED_CORRECTLY = "Data type(s) displayed correctly" |
| 125 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 126 | VALID_BREAKPOINT = "Got a valid breakpoint" |
| 127 | |
Johnny Chen | 5bfb8ee | 2010-10-22 18:10:25 +0000 | [diff] [blame] | 128 | VALID_BREAKPOINT_LOCATION = "Got a valid breakpoint location" |
| 129 | |
Johnny Chen | 7209d84f | 2011-05-06 23:26:12 +0000 | [diff] [blame] | 130 | VALID_COMMAND_INTERPRETER = "Got a valid command interpreter" |
| 131 | |
Johnny Chen | 5ee8819 | 2010-08-27 23:47:36 +0000 | [diff] [blame] | 132 | VALID_FILESPEC = "Got a valid filespec" |
| 133 | |
Johnny Chen | 025d1b8 | 2010-12-08 01:25:21 +0000 | [diff] [blame] | 134 | VALID_MODULE = "Got a valid module" |
| 135 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 136 | VALID_PROCESS = "Got a valid process" |
| 137 | |
Johnny Chen | 025d1b8 | 2010-12-08 01:25:21 +0000 | [diff] [blame] | 138 | VALID_SYMBOL = "Got a valid symbol" |
| 139 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 140 | VALID_TARGET = "Got a valid target" |
| 141 | |
Matthew Gardiner | c928de3 | 2014-10-22 07:22:56 +0000 | [diff] [blame] | 142 | VALID_PLATFORM = "Got a valid platform" |
| 143 | |
Johnny Chen | 15f247a | 2012-02-03 20:43:00 +0000 | [diff] [blame] | 144 | VALID_TYPE = "Got a valid type" |
| 145 | |
Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 146 | VALID_VARIABLE = "Got a valid variable" |
| 147 | |
Johnny Chen | 981463d | 2010-08-25 19:00:04 +0000 | [diff] [blame] | 148 | VARIABLES_DISPLAYED_CORRECTLY = "Variable(s) displayed correctly" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 149 | |
Johnny Chen | f68cc12 | 2011-09-15 21:09:59 +0000 | [diff] [blame] | 150 | WATCHPOINT_CREATED = "Watchpoint created successfully" |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 151 | |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 152 | def CMD_MSG(str): |
Johnny Chen | aacf92e | 2011-05-31 22:16:51 +0000 | [diff] [blame] | 153 | '''A generic "Command '%s' returns successfully" message generator.''' |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 154 | return "Command '%s' returns successfully" % str |
| 155 | |
Johnny Chen | 3bc8ae4 | 2012-03-15 19:10:00 +0000 | [diff] [blame] | 156 | def COMPLETION_MSG(str_before, str_after): |
Johnny Chen | 98aceb0 | 2012-01-20 23:02:51 +0000 | [diff] [blame] | 157 | '''A generic message generator for the completion mechanism.''' |
| 158 | return "'%s' successfully completes to '%s'" % (str_before, str_after) |
| 159 | |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 160 | def EXP_MSG(str, exe): |
Johnny Chen | aacf92e | 2011-05-31 22:16:51 +0000 | [diff] [blame] | 161 | '''A generic "'%s' returns expected result" message generator if exe. |
| 162 | Otherwise, it generates "'%s' matches expected result" message.''' |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 163 | return "'%s' %s expected result" % (str, 'returns' if exe else 'matches') |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 164 | |
Johnny Chen | 3343f04 | 2010-10-19 19:11:38 +0000 | [diff] [blame] | 165 | def SETTING_MSG(setting): |
Johnny Chen | aacf92e | 2011-05-31 22:16:51 +0000 | [diff] [blame] | 166 | '''A generic "Value of setting '%s' is correct" message generator.''' |
Johnny Chen | 3343f04 | 2010-10-19 19:11:38 +0000 | [diff] [blame] | 167 | return "Value of setting '%s' is correct" % setting |
| 168 | |
Johnny Chen | 27c4123 | 2010-08-26 21:49:29 +0000 | [diff] [blame] | 169 | def EnvArray(): |
Johnny Chen | aacf92e | 2011-05-31 22:16:51 +0000 | [diff] [blame] | 170 | """Returns an env variable array from the os.environ map object.""" |
Johnny Chen | 27c4123 | 2010-08-26 21:49:29 +0000 | [diff] [blame] | 171 | return map(lambda k,v: k+"="+v, os.environ.keys(), os.environ.values()) |
| 172 | |
Johnny Chen | 47ceb03 | 2010-10-11 23:52:19 +0000 | [diff] [blame] | 173 | def line_number(filename, string_to_match): |
| 174 | """Helper function to return the line number of the first matched string.""" |
| 175 | with open(filename, 'r') as f: |
| 176 | for i, line in enumerate(f): |
| 177 | if line.find(string_to_match) != -1: |
| 178 | # Found our match. |
Johnny Chen | cd9b777 | 2010-10-12 00:09:25 +0000 | [diff] [blame] | 179 | return i+1 |
Johnny Chen | 1691a16 | 2011-04-15 16:44:48 +0000 | [diff] [blame] | 180 | 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] | 181 | |
Johnny Chen | 67af43f | 2010-10-05 19:27:32 +0000 | [diff] [blame] | 182 | def pointer_size(): |
| 183 | """Return the pointer size of the host system.""" |
| 184 | import ctypes |
| 185 | a_pointer = ctypes.c_void_p(0xffff) |
| 186 | return 8 * ctypes.sizeof(a_pointer) |
| 187 | |
Johnny Chen | 5781673 | 2012-02-09 02:01:59 +0000 | [diff] [blame] | 188 | def is_exe(fpath): |
| 189 | """Returns true if fpath is an executable.""" |
| 190 | return os.path.isfile(fpath) and os.access(fpath, os.X_OK) |
| 191 | |
| 192 | def which(program): |
| 193 | """Returns the full path to a program; None otherwise.""" |
| 194 | fpath, fname = os.path.split(program) |
| 195 | if fpath: |
| 196 | if is_exe(program): |
| 197 | return program |
| 198 | else: |
| 199 | for path in os.environ["PATH"].split(os.pathsep): |
| 200 | exe_file = os.path.join(path, program) |
| 201 | if is_exe(exe_file): |
| 202 | return exe_file |
| 203 | return None |
| 204 | |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 205 | class recording(StringIO.StringIO): |
| 206 | """ |
| 207 | A nice little context manager for recording the debugger interactions into |
| 208 | our session object. If trace flag is ON, it also emits the interactions |
| 209 | into the stderr. |
| 210 | """ |
| 211 | def __init__(self, test, trace): |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 212 | """Create a StringIO instance; record the session obj and trace flag.""" |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 213 | StringIO.StringIO.__init__(self) |
Johnny Chen | 0241f14 | 2011-08-16 22:06:17 +0000 | [diff] [blame] | 214 | # The test might not have undergone the 'setUp(self)' phase yet, so that |
| 215 | # the attribute 'session' might not even exist yet. |
Johnny Chen | bfcf37f | 2011-08-16 17:06:45 +0000 | [diff] [blame] | 216 | self.session = getattr(test, "session", None) if test else None |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 217 | self.trace = trace |
| 218 | |
| 219 | def __enter__(self): |
| 220 | """ |
| 221 | Context management protocol on entry to the body of the with statement. |
| 222 | Just return the StringIO object. |
| 223 | """ |
| 224 | return self |
| 225 | |
| 226 | def __exit__(self, type, value, tb): |
| 227 | """ |
| 228 | Context management protocol on exit from the body of the with statement. |
| 229 | If trace is ON, it emits the recordings into stderr. Always add the |
| 230 | recordings to our session object. And close the StringIO object, too. |
| 231 | """ |
| 232 | if self.trace: |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 233 | print >> sys.stderr, self.getvalue() |
| 234 | if self.session: |
| 235 | print >> self.session, self.getvalue() |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 236 | self.close() |
| 237 | |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 238 | class _BaseProcess(object): |
| 239 | __metaclass__ = abc.ABCMeta |
| 240 | |
| 241 | @abc.abstractproperty |
| 242 | def pid(self): |
| 243 | """Returns process PID if has been launched already.""" |
| 244 | |
| 245 | @abc.abstractmethod |
| 246 | def launch(self, executable, args): |
| 247 | """Launches new process with given executable and args.""" |
| 248 | |
| 249 | @abc.abstractmethod |
| 250 | def terminate(self): |
| 251 | """Terminates previously launched process..""" |
| 252 | |
| 253 | class _LocalProcess(_BaseProcess): |
| 254 | |
| 255 | def __init__(self, trace_on): |
| 256 | self._proc = None |
| 257 | self._trace_on = trace_on |
| 258 | |
| 259 | @property |
| 260 | def pid(self): |
| 261 | return self._proc.pid |
| 262 | |
| 263 | def launch(self, executable, args): |
| 264 | self._proc = Popen([executable] + args, |
| 265 | stdout = open(os.devnull) if not self._trace_on else None, |
| 266 | stdin = PIPE) |
| 267 | |
| 268 | def terminate(self): |
| 269 | if self._proc.poll() == None: |
| 270 | self._proc.terminate() |
| 271 | |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 272 | def poll(self): |
| 273 | return self._proc.poll() |
| 274 | |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 275 | class _RemoteProcess(_BaseProcess): |
| 276 | |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 277 | def __init__(self, install_remote): |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 278 | self._pid = None |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 279 | self._install_remote = install_remote |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 280 | |
| 281 | @property |
| 282 | def pid(self): |
| 283 | return self._pid |
| 284 | |
| 285 | def launch(self, executable, args): |
| 286 | remote_work_dir = lldb.remote_platform.GetWorkingDirectory() |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 287 | |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 288 | if self._install_remote: |
| 289 | src_path = executable |
| 290 | dst_path = os.path.join(remote_work_dir, os.path.basename(executable)) |
| 291 | |
| 292 | dst_file_spec = lldb.SBFileSpec(dst_path, False) |
| 293 | err = lldb.remote_platform.Install(lldb.SBFileSpec(src_path, True), dst_file_spec) |
| 294 | if err.Fail(): |
| 295 | raise Exception("remote_platform.Install('%s', '%s') failed: %s" % (src_path, dst_path, err)) |
| 296 | else: |
| 297 | dst_path = executable |
| 298 | dst_file_spec = lldb.SBFileSpec(executable, False) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 299 | |
| 300 | launch_info = lldb.SBLaunchInfo(args) |
| 301 | launch_info.SetExecutableFile(dst_file_spec, True) |
| 302 | launch_info.SetWorkingDirectory(remote_work_dir) |
| 303 | |
| 304 | # Redirect stdout and stderr to /dev/null |
| 305 | launch_info.AddSuppressFileAction(1, False, True) |
| 306 | launch_info.AddSuppressFileAction(2, False, True) |
| 307 | |
| 308 | err = lldb.remote_platform.Launch(launch_info) |
| 309 | if err.Fail(): |
| 310 | raise Exception("remote_platform.Launch('%s', '%s') failed: %s" % (dst_path, args, err)) |
| 311 | self._pid = launch_info.GetProcessID() |
| 312 | |
| 313 | def terminate(self): |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 314 | lldb.remote_platform.Kill(self._pid) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 315 | |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 316 | # From 2.7's subprocess.check_output() convenience function. |
Johnny Chen | ac77f3b | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 317 | # Return a tuple (stdoutdata, stderrdata). |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 318 | def system(commands, **kwargs): |
Johnny Chen | 8eb14a9 | 2011-11-16 22:44:28 +0000 | [diff] [blame] | 319 | 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] | 320 | |
| 321 | If the exit code was non-zero it raises a CalledProcessError. The |
| 322 | CalledProcessError object will have the return code in the returncode |
| 323 | attribute and output in the output attribute. |
| 324 | |
| 325 | The arguments are the same as for the Popen constructor. Example: |
| 326 | |
| 327 | >>> check_output(["ls", "-l", "/dev/null"]) |
| 328 | 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' |
| 329 | |
| 330 | The stdout argument is not allowed as it is used internally. |
| 331 | To capture standard error in the result, use stderr=STDOUT. |
| 332 | |
| 333 | >>> check_output(["/bin/sh", "-c", |
| 334 | ... "ls -l non_existent_file ; exit 0"], |
| 335 | ... stderr=STDOUT) |
| 336 | 'ls: non_existent_file: No such file or directory\n' |
| 337 | """ |
| 338 | |
| 339 | # Assign the sender object to variable 'test' and remove it from kwargs. |
| 340 | test = kwargs.pop('sender', None) |
| 341 | |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 342 | # [['make', 'clean', 'foo'], ['make', 'foo']] -> ['make clean foo', 'make foo'] |
| 343 | commandList = [' '.join(x) for x in commands] |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 344 | output = "" |
| 345 | error = "" |
| 346 | for shellCommand in commandList: |
| 347 | if 'stdout' in kwargs: |
| 348 | raise ValueError('stdout argument not allowed, it will be overridden.') |
| 349 | if 'shell' in kwargs and kwargs['shell']==False: |
| 350 | raise ValueError('shell=False not allowed') |
| 351 | process = Popen(shellCommand, stdout=PIPE, stderr=PIPE, shell=True, **kwargs) |
| 352 | pid = process.pid |
| 353 | this_output, this_error = process.communicate() |
| 354 | retcode = process.poll() |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 355 | |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 356 | # Enable trace on failure return while tracking down FreeBSD buildbot issues |
| 357 | trace = traceAlways |
| 358 | if not trace and retcode and sys.platform.startswith("freebsd"): |
| 359 | trace = True |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 360 | |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 361 | with recording(test, trace) as sbuf: |
| 362 | print >> sbuf |
| 363 | print >> sbuf, "os command:", shellCommand |
| 364 | print >> sbuf, "with pid:", pid |
| 365 | print >> sbuf, "stdout:", output |
| 366 | print >> sbuf, "stderr:", error |
| 367 | print >> sbuf, "retcode:", retcode |
| 368 | print >> sbuf |
Ed Maste | 6e49633 | 2014-08-05 20:33:17 +0000 | [diff] [blame] | 369 | |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 370 | if retcode: |
| 371 | cmd = kwargs.get("args") |
| 372 | if cmd is None: |
| 373 | cmd = shellCommand |
| 374 | raise CalledProcessError(retcode, cmd) |
| 375 | output = output + this_output |
| 376 | error = error + this_error |
Johnny Chen | ac77f3b | 2011-03-23 20:28:59 +0000 | [diff] [blame] | 377 | return (output, error) |
Johnny Chen | 690fcef | 2010-10-15 23:55:05 +0000 | [diff] [blame] | 378 | |
Johnny Chen | ab9c1dd | 2010-11-01 20:35:01 +0000 | [diff] [blame] | 379 | def getsource_if_available(obj): |
| 380 | """ |
| 381 | Return the text of the source code for an object if available. Otherwise, |
| 382 | a print representation is returned. |
| 383 | """ |
| 384 | import inspect |
| 385 | try: |
| 386 | return inspect.getsource(obj) |
| 387 | except: |
| 388 | return repr(obj) |
| 389 | |
Peter Collingbourne | 19f48d5 | 2011-06-20 19:06:20 +0000 | [diff] [blame] | 390 | def builder_module(): |
Ed Maste | 4d90f0f | 2013-07-25 13:24:34 +0000 | [diff] [blame] | 391 | if sys.platform.startswith("freebsd"): |
| 392 | return __import__("builder_freebsd") |
Peter Collingbourne | 19f48d5 | 2011-06-20 19:06:20 +0000 | [diff] [blame] | 393 | return __import__("builder_" + sys.platform) |
| 394 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 395 | # |
| 396 | # Decorators for categorizing test cases. |
| 397 | # |
| 398 | |
| 399 | from functools import wraps |
| 400 | def python_api_test(func): |
| 401 | """Decorate the item as a Python API only test.""" |
| 402 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 403 | raise Exception("@python_api_test can only be used to decorate a test method") |
| 404 | @wraps(func) |
| 405 | def wrapper(self, *args, **kwargs): |
| 406 | try: |
| 407 | if lldb.dont_do_python_api_test: |
| 408 | self.skipTest("python api tests") |
| 409 | except AttributeError: |
| 410 | pass |
| 411 | return func(self, *args, **kwargs) |
| 412 | |
| 413 | # Mark this function as such to separate them from lldb command line tests. |
| 414 | wrapper.__python_api_test__ = True |
| 415 | return wrapper |
| 416 | |
Hafiz Abid Qadeer | 1cbac4e | 2014-11-25 10:41:57 +0000 | [diff] [blame] | 417 | def lldbmi_test(func): |
| 418 | """Decorate the item as a lldb-mi only test.""" |
| 419 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 420 | raise Exception("@lldbmi_test can only be used to decorate a test method") |
| 421 | @wraps(func) |
| 422 | def wrapper(self, *args, **kwargs): |
| 423 | try: |
| 424 | if lldb.dont_do_lldbmi_test: |
| 425 | self.skipTest("lldb-mi tests") |
| 426 | except AttributeError: |
| 427 | pass |
| 428 | return func(self, *args, **kwargs) |
| 429 | |
| 430 | # Mark this function as such to separate them from lldb command line tests. |
| 431 | wrapper.__lldbmi_test__ = True |
| 432 | return wrapper |
| 433 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 434 | def benchmarks_test(func): |
| 435 | """Decorate the item as a benchmarks test.""" |
| 436 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 437 | raise Exception("@benchmarks_test can only be used to decorate a test method") |
| 438 | @wraps(func) |
| 439 | def wrapper(self, *args, **kwargs): |
| 440 | try: |
| 441 | if not lldb.just_do_benchmarks_test: |
| 442 | self.skipTest("benchmarks tests") |
| 443 | except AttributeError: |
| 444 | pass |
| 445 | return func(self, *args, **kwargs) |
| 446 | |
| 447 | # Mark this function as such to separate them from the regular tests. |
| 448 | wrapper.__benchmarks_test__ = True |
| 449 | return wrapper |
| 450 | |
Johnny Chen | f1548d4 | 2012-04-06 00:56:05 +0000 | [diff] [blame] | 451 | def dsym_test(func): |
| 452 | """Decorate the item as a dsym test.""" |
| 453 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 454 | raise Exception("@dsym_test can only be used to decorate a test method") |
| 455 | @wraps(func) |
| 456 | def wrapper(self, *args, **kwargs): |
| 457 | try: |
| 458 | if lldb.dont_do_dsym_test: |
| 459 | self.skipTest("dsym tests") |
| 460 | except AttributeError: |
| 461 | pass |
| 462 | return func(self, *args, **kwargs) |
| 463 | |
| 464 | # Mark this function as such to separate them from the regular tests. |
| 465 | wrapper.__dsym_test__ = True |
| 466 | return wrapper |
| 467 | |
| 468 | def dwarf_test(func): |
| 469 | """Decorate the item as a dwarf test.""" |
| 470 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 471 | raise Exception("@dwarf_test can only be used to decorate a test method") |
| 472 | @wraps(func) |
| 473 | def wrapper(self, *args, **kwargs): |
| 474 | try: |
| 475 | if lldb.dont_do_dwarf_test: |
| 476 | self.skipTest("dwarf tests") |
| 477 | except AttributeError: |
| 478 | pass |
| 479 | return func(self, *args, **kwargs) |
| 480 | |
| 481 | # Mark this function as such to separate them from the regular tests. |
| 482 | wrapper.__dwarf_test__ = True |
| 483 | return wrapper |
| 484 | |
Todd Fiala | a41d48c | 2014-04-28 04:49:40 +0000 | [diff] [blame] | 485 | def debugserver_test(func): |
| 486 | """Decorate the item as a debugserver test.""" |
| 487 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 488 | raise Exception("@debugserver_test can only be used to decorate a test method") |
| 489 | @wraps(func) |
| 490 | def wrapper(self, *args, **kwargs): |
| 491 | try: |
| 492 | if lldb.dont_do_debugserver_test: |
| 493 | self.skipTest("debugserver tests") |
| 494 | except AttributeError: |
| 495 | pass |
| 496 | return func(self, *args, **kwargs) |
| 497 | |
| 498 | # Mark this function as such to separate them from the regular tests. |
| 499 | wrapper.__debugserver_test__ = True |
| 500 | return wrapper |
| 501 | |
| 502 | def llgs_test(func): |
Robert Flack | 8cc4cf1 | 2015-03-06 14:36:33 +0000 | [diff] [blame] | 503 | """Decorate the item as a lldb-server test.""" |
Todd Fiala | a41d48c | 2014-04-28 04:49:40 +0000 | [diff] [blame] | 504 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 505 | raise Exception("@llgs_test can only be used to decorate a test method") |
| 506 | @wraps(func) |
| 507 | def wrapper(self, *args, **kwargs): |
| 508 | try: |
| 509 | if lldb.dont_do_llgs_test: |
| 510 | self.skipTest("llgs tests") |
| 511 | except AttributeError: |
| 512 | pass |
| 513 | return func(self, *args, **kwargs) |
| 514 | |
| 515 | # Mark this function as such to separate them from the regular tests. |
| 516 | wrapper.__llgs_test__ = True |
| 517 | return wrapper |
| 518 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 519 | def not_remote_testsuite_ready(func): |
| 520 | """Decorate the item as a test which is not ready yet for remote testsuite.""" |
| 521 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 522 | raise Exception("@not_remote_testsuite_ready can only be used to decorate a test method") |
| 523 | @wraps(func) |
| 524 | def wrapper(self, *args, **kwargs): |
| 525 | try: |
Tamas Berghammer | 3e0ecb2 | 2015-03-25 15:13:28 +0000 | [diff] [blame] | 526 | if lldb.lldbtest_remote_sandbox or lldb.remote_platform: |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 527 | self.skipTest("not ready for remote testsuite") |
| 528 | except AttributeError: |
| 529 | pass |
| 530 | return func(self, *args, **kwargs) |
| 531 | |
| 532 | # Mark this function as such to separate them from the regular tests. |
| 533 | wrapper.__not_ready_for_remote_testsuite_test__ = True |
| 534 | return wrapper |
| 535 | |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 536 | def expectedFailure(expected_fn, bugnumber=None): |
| 537 | def expectedFailure_impl(func): |
| 538 | @wraps(func) |
| 539 | def wrapper(*args, **kwargs): |
Enrico Granata | 43f6213 | 2013-02-23 01:28:30 +0000 | [diff] [blame] | 540 | from unittest2 import case |
| 541 | self = args[0] |
Enrico Granata | 43f6213 | 2013-02-23 01:28:30 +0000 | [diff] [blame] | 542 | try: |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 543 | func(*args, **kwargs) |
Enrico Granata | 43f6213 | 2013-02-23 01:28:30 +0000 | [diff] [blame] | 544 | except Exception: |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 545 | if expected_fn(self): |
| 546 | raise case._ExpectedFailure(sys.exc_info(), bugnumber) |
Enrico Granata | 43f6213 | 2013-02-23 01:28:30 +0000 | [diff] [blame] | 547 | else: |
| 548 | raise |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 549 | if expected_fn(self): |
| 550 | raise case._UnexpectedSuccess(sys.exc_info(), bugnumber) |
| 551 | return wrapper |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 552 | # if bugnumber is not-callable(incluing None), that means decorator function is called with optional arguments |
| 553 | # return decorator in this case, so it will be used to decorating original method |
| 554 | if callable(bugnumber): |
| 555 | return expectedFailure_impl(bugnumber) |
| 556 | else: |
| 557 | return expectedFailure_impl |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 558 | |
| 559 | def expectedFailureCompiler(compiler, compiler_version=None, bugnumber=None): |
| 560 | if compiler_version is None: |
| 561 | compiler_version=['=', None] |
| 562 | def fn(self): |
| 563 | return compiler in self.getCompiler() and self.expectedCompilerVersion(compiler_version) |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 564 | return expectedFailure(fn, bugnumber) |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 565 | |
Vince Harron | 8974ce2 | 2015-03-13 19:54:54 +0000 | [diff] [blame] | 566 | # to XFAIL a specific clang versions, try this |
| 567 | # @expectedFailureClang('bugnumber', ['<=', '3.4']) |
| 568 | def expectedFailureClang(bugnumber=None, compiler_version=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 569 | return expectedFailureCompiler('clang', compiler_version, bugnumber) |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 570 | |
| 571 | def expectedFailureGcc(bugnumber=None, compiler_version=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 572 | return expectedFailureCompiler('gcc', compiler_version, bugnumber) |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 573 | |
Matt Kopec | 0de53f0 | 2013-03-15 19:10:12 +0000 | [diff] [blame] | 574 | def expectedFailureIcc(bugnumber=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 575 | return expectedFailureCompiler('icc', None, bugnumber) |
Matt Kopec | 0de53f0 | 2013-03-15 19:10:12 +0000 | [diff] [blame] | 576 | |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 577 | def expectedFailureArch(arch, bugnumber=None): |
| 578 | def fn(self): |
| 579 | return arch in self.getArchitecture() |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 580 | return expectedFailure(fn, bugnumber) |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 581 | |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 582 | def expectedFailurei386(bugnumber=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 583 | return expectedFailureArch('i386', bugnumber) |
Johnny Chen | a33843f | 2011-12-22 21:14:31 +0000 | [diff] [blame] | 584 | |
Matt Kopec | ee969f9 | 2013-09-26 23:30:59 +0000 | [diff] [blame] | 585 | def expectedFailurex86_64(bugnumber=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 586 | return expectedFailureArch('x86_64', bugnumber) |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 587 | |
Robert Flack | efa49c2 | 2015-03-26 19:34:26 +0000 | [diff] [blame] | 588 | def expectedFailureOS(oslist, bugnumber=None, compilers=None): |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 589 | def fn(self): |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 590 | return (self.getPlatform() in oslist and |
Robert Flack | efa49c2 | 2015-03-26 19:34:26 +0000 | [diff] [blame] | 591 | self.expectedCompiler(compilers)) |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 592 | return expectedFailure(fn, bugnumber) |
Ed Maste | 433790a | 2014-04-23 12:55:41 +0000 | [diff] [blame] | 593 | |
| 594 | def expectedFailureDarwin(bugnumber=None, compilers=None): |
Robert Flack | efa49c2 | 2015-03-26 19:34:26 +0000 | [diff] [blame] | 595 | # For legacy reasons, we support both "darwin" and "macosx" as OS X triples. |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 596 | return expectedFailureOS(getDarwinOSTriples(), bugnumber, compilers) |
Matt Kopec | ee969f9 | 2013-09-26 23:30:59 +0000 | [diff] [blame] | 597 | |
Ed Maste | 24a7f7d | 2013-07-24 19:47:08 +0000 | [diff] [blame] | 598 | def expectedFailureFreeBSD(bugnumber=None, compilers=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 599 | return expectedFailureOS(['freebsd'], bugnumber, compilers) |
Ed Maste | 24a7f7d | 2013-07-24 19:47:08 +0000 | [diff] [blame] | 600 | |
Ashok Thirumurthi | c97a608 | 2013-05-17 20:15:07 +0000 | [diff] [blame] | 601 | def expectedFailureLinux(bugnumber=None, compilers=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 602 | return expectedFailureOS(['linux'], bugnumber, compilers) |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 603 | |
Zachary Turner | 80c2c60 | 2014-12-09 19:28:00 +0000 | [diff] [blame] | 604 | def expectedFailureWindows(bugnumber=None, compilers=None): |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 605 | return expectedFailureOS(['windows'], bugnumber, compilers) |
Zachary Turner | 80c2c60 | 2014-12-09 19:28:00 +0000 | [diff] [blame] | 606 | |
Chaoren Lin | 72b8f05 | 2015-02-03 01:51:18 +0000 | [diff] [blame] | 607 | def expectedFailureLLGS(bugnumber=None, compilers=None): |
| 608 | def fn(self): |
Vince Harron | bc477dd | 2015-03-01 23:21:29 +0000 | [diff] [blame] | 609 | # llgs local is only an option on Linux systems |
| 610 | if 'linux' not in sys.platform: |
| 611 | return False |
| 612 | self.runCmd('settings show platform.plugin.linux.use-llgs-for-local') |
| 613 | return 'true' in self.res.GetOutput() and self.expectedCompiler(compilers) |
Ying Chen | 464d1e1 | 2015-03-27 00:26:52 +0000 | [diff] [blame] | 614 | return expectedFailure(fn, bugnumber) |
Chaoren Lin | 72b8f05 | 2015-02-03 01:51:18 +0000 | [diff] [blame] | 615 | |
Greg Clayton | 1251456 | 2013-12-05 22:22:32 +0000 | [diff] [blame] | 616 | def skipIfRemote(func): |
| 617 | """Decorate the item to skip tests if testing remotely.""" |
| 618 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 619 | raise Exception("@skipIfRemote can only be used to decorate a test method") |
| 620 | @wraps(func) |
| 621 | def wrapper(*args, **kwargs): |
| 622 | from unittest2 import case |
| 623 | if lldb.remote_platform: |
| 624 | self = args[0] |
| 625 | self.skipTest("skip on remote platform") |
| 626 | else: |
| 627 | func(*args, **kwargs) |
| 628 | return wrapper |
| 629 | |
| 630 | def skipIfRemoteDueToDeadlock(func): |
| 631 | """Decorate the item to skip tests if testing remotely due to the test deadlocking.""" |
| 632 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 633 | raise Exception("@skipIfRemote can only be used to decorate a test method") |
| 634 | @wraps(func) |
| 635 | def wrapper(*args, **kwargs): |
| 636 | from unittest2 import case |
| 637 | if lldb.remote_platform: |
| 638 | self = args[0] |
| 639 | self.skipTest("skip on remote platform (deadlocks)") |
| 640 | else: |
| 641 | func(*args, **kwargs) |
| 642 | return wrapper |
| 643 | |
Enrico Granata | b633e43 | 2014-10-06 21:37:06 +0000 | [diff] [blame] | 644 | def skipIfNoSBHeaders(func): |
| 645 | """Decorate the item to mark tests that should be skipped when LLDB is built with no SB API headers.""" |
| 646 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
Ed Maste | 59cca5d | 2014-10-07 01:57:52 +0000 | [diff] [blame] | 647 | raise Exception("@skipIfNoSBHeaders can only be used to decorate a test method") |
Enrico Granata | b633e43 | 2014-10-06 21:37:06 +0000 | [diff] [blame] | 648 | @wraps(func) |
| 649 | def wrapper(*args, **kwargs): |
| 650 | from unittest2 import case |
| 651 | self = args[0] |
Shawn Best | 181b09b | 2014-11-08 00:04:04 +0000 | [diff] [blame] | 652 | if sys.platform.startswith("darwin"): |
| 653 | header = os.path.join(self.lib_dir, 'LLDB.framework', 'Versions','Current','Headers','LLDB.h') |
| 654 | else: |
| 655 | 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] | 656 | platform = sys.platform |
Enrico Granata | b633e43 | 2014-10-06 21:37:06 +0000 | [diff] [blame] | 657 | if not os.path.exists(header): |
| 658 | self.skipTest("skip because LLDB.h header not found") |
| 659 | else: |
| 660 | func(*args, **kwargs) |
| 661 | return wrapper |
| 662 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 663 | def skipIfFreeBSD(func): |
| 664 | """Decorate the item to skip tests that should be skipped on FreeBSD.""" |
| 665 | return skipIfPlatform(["freebsd"])(func) |
Zachary Turner | c782652 | 2014-08-13 17:44:53 +0000 | [diff] [blame] | 666 | |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 667 | def getDarwinOSTriples(): |
| 668 | return ['darwin', 'macosx', 'ios'] |
| 669 | |
Daniel Malea | b3d41a2 | 2013-07-09 00:08:01 +0000 | [diff] [blame] | 670 | def skipIfDarwin(func): |
| 671 | """Decorate the item to skip tests that should be skipped on Darwin.""" |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 672 | return skipIfPlatform(getDarwinOSTriples())(func) |
Daniel Malea | b3d41a2 | 2013-07-09 00:08:01 +0000 | [diff] [blame] | 673 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 674 | def skipIfLinux(func): |
| 675 | """Decorate the item to skip tests that should be skipped on Linux.""" |
| 676 | return skipIfPlatform(["linux"])(func) |
| 677 | |
| 678 | def skipIfWindows(func): |
| 679 | """Decorate the item to skip tests that should be skipped on Windows.""" |
| 680 | return skipIfPlatform(["windows"])(func) |
| 681 | |
| 682 | def skipUnlessDarwin(func): |
| 683 | """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] | 684 | return skipUnlessPlatform(getDarwinOSTriples())(func) |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 685 | |
| 686 | def skipIfPlatform(oslist): |
| 687 | """Decorate the item to skip tests if running on one of the listed platforms.""" |
| 688 | def decorator(func): |
| 689 | @wraps(func) |
| 690 | def wrapper(*args, **kwargs): |
| 691 | from unittest2 import case |
| 692 | self = args[0] |
| 693 | if self.getPlatform() in oslist: |
| 694 | self.skipTest("skip on %s" % (", ".join(oslist))) |
| 695 | else: |
| 696 | func(*args, **kwargs) |
| 697 | return wrapper |
| 698 | return decorator |
| 699 | |
| 700 | def skipUnlessPlatform(oslist): |
| 701 | """Decorate the item to skip tests unless running on one of the listed platforms.""" |
| 702 | def decorator(func): |
| 703 | @wraps(func) |
| 704 | def wrapper(*args, **kwargs): |
| 705 | from unittest2 import case |
| 706 | self = args[0] |
| 707 | if not (self.getPlatform() in oslist): |
| 708 | self.skipTest("requires one of %s" % (", ".join(oslist))) |
| 709 | else: |
| 710 | func(*args, **kwargs) |
| 711 | return wrapper |
| 712 | return decorator |
Daniel Malea | b3d41a2 | 2013-07-09 00:08:01 +0000 | [diff] [blame] | 713 | |
Daniel Malea | 4835990 | 2013-05-14 20:48:54 +0000 | [diff] [blame] | 714 | def skipIfLinuxClang(func): |
| 715 | """Decorate the item to skip tests that should be skipped if building on |
| 716 | Linux with clang. |
| 717 | """ |
| 718 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 719 | raise Exception("@skipIfLinuxClang can only be used to decorate a test method") |
| 720 | @wraps(func) |
| 721 | def wrapper(*args, **kwargs): |
| 722 | from unittest2 import case |
| 723 | self = args[0] |
| 724 | compiler = self.getCompiler() |
| 725 | platform = sys.platform |
| 726 | if "clang" in compiler and "linux" in platform: |
| 727 | self.skipTest("skipping because Clang is used on Linux") |
| 728 | else: |
| 729 | func(*args, **kwargs) |
| 730 | return wrapper |
| 731 | |
Daniel Malea | be23079 | 2013-01-24 23:52:09 +0000 | [diff] [blame] | 732 | def skipIfGcc(func): |
| 733 | """Decorate the item to skip tests that should be skipped if building with gcc .""" |
| 734 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 735 | raise Exception("@skipIfGcc can only be used to decorate a test method") |
Daniel Malea | be23079 | 2013-01-24 23:52:09 +0000 | [diff] [blame] | 736 | @wraps(func) |
| 737 | def wrapper(*args, **kwargs): |
| 738 | from unittest2 import case |
| 739 | self = args[0] |
| 740 | compiler = self.getCompiler() |
| 741 | if "gcc" in compiler: |
| 742 | self.skipTest("skipping because gcc is the test compiler") |
| 743 | else: |
| 744 | func(*args, **kwargs) |
| 745 | return wrapper |
| 746 | |
Matt Kopec | 0de53f0 | 2013-03-15 19:10:12 +0000 | [diff] [blame] | 747 | def skipIfIcc(func): |
| 748 | """Decorate the item to skip tests that should be skipped if building with icc .""" |
| 749 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 750 | raise Exception("@skipIfIcc can only be used to decorate a test method") |
| 751 | @wraps(func) |
| 752 | def wrapper(*args, **kwargs): |
| 753 | from unittest2 import case |
| 754 | self = args[0] |
| 755 | compiler = self.getCompiler() |
| 756 | if "icc" in compiler: |
| 757 | self.skipTest("skipping because icc is the test compiler") |
| 758 | else: |
| 759 | func(*args, **kwargs) |
| 760 | return wrapper |
| 761 | |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 762 | def skipIfi386(func): |
| 763 | """Decorate the item to skip tests that should be skipped if building 32-bit.""" |
| 764 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 765 | raise Exception("@skipIfi386 can only be used to decorate a test method") |
| 766 | @wraps(func) |
| 767 | def wrapper(*args, **kwargs): |
| 768 | from unittest2 import case |
| 769 | self = args[0] |
| 770 | if "i386" == self.getArchitecture(): |
| 771 | self.skipTest("skipping because i386 is not a supported architecture") |
| 772 | else: |
| 773 | func(*args, **kwargs) |
| 774 | return wrapper |
| 775 | |
Tamas Berghammer | 1253a81 | 2015-03-13 10:12:25 +0000 | [diff] [blame] | 776 | def skipIfTargetAndroid(func): |
| 777 | """Decorate the item to skip tests that should be skipped when the target is Android.""" |
| 778 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 779 | raise Exception("@skipIfTargetAndroid can only be used to decorate a test method") |
| 780 | @wraps(func) |
| 781 | def wrapper(*args, **kwargs): |
| 782 | from unittest2 import case |
| 783 | self = args[0] |
| 784 | triple = self.dbg.GetSelectedPlatform().GetTriple() |
| 785 | if re.match(".*-.*-.*-android", triple): |
| 786 | self.skipTest("skip on Android target") |
| 787 | else: |
| 788 | func(*args, **kwargs) |
| 789 | return wrapper |
| 790 | |
Ilia K | d995305 | 2015-03-12 07:19:41 +0000 | [diff] [blame] | 791 | def skipUnlessCompilerRt(func): |
| 792 | """Decorate the item to skip tests if testing remotely.""" |
| 793 | if isinstance(func, type) and issubclass(func, unittest2.TestCase): |
| 794 | raise Exception("@skipUnless can only be used to decorate a test method") |
| 795 | @wraps(func) |
| 796 | def wrapper(*args, **kwargs): |
| 797 | from unittest2 import case |
| 798 | import os.path |
| 799 | compilerRtPath = os.path.join(os.path.dirname(__file__), "..", "..", "..", "projects", "compiler-rt") |
| 800 | if not os.path.exists(compilerRtPath): |
| 801 | self = args[0] |
| 802 | self.skipTest("skip if compiler-rt not found") |
| 803 | else: |
| 804 | func(*args, **kwargs) |
| 805 | return wrapper |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 806 | |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 807 | class _PlatformContext(object): |
| 808 | """Value object class which contains platform-specific options.""" |
| 809 | |
| 810 | def __init__(self, shlib_environment_var, shlib_prefix, shlib_extension): |
| 811 | self.shlib_environment_var = shlib_environment_var |
| 812 | self.shlib_prefix = shlib_prefix |
| 813 | self.shlib_extension = shlib_extension |
| 814 | |
| 815 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 816 | class Base(unittest2.TestCase): |
Johnny Chen | 8334dad | 2010-10-22 23:15:46 +0000 | [diff] [blame] | 817 | """ |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 818 | Abstract base for performing lldb (see TestBase) or other generic tests (see |
| 819 | BenchBase for one example). lldbtest.Base works with the test driver to |
| 820 | accomplish things. |
| 821 | |
Johnny Chen | 8334dad | 2010-10-22 23:15:46 +0000 | [diff] [blame] | 822 | """ |
Enrico Granata | 5020f95 | 2012-10-24 21:42:49 +0000 | [diff] [blame] | 823 | |
Enrico Granata | 1918627 | 2012-10-24 21:44:48 +0000 | [diff] [blame] | 824 | # The concrete subclass should override this attribute. |
| 825 | mydir = None |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 826 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 827 | # Keep track of the old current working directory. |
| 828 | oldcwd = None |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 829 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 830 | @staticmethod |
| 831 | def compute_mydir(test_file): |
| 832 | '''Subclasses should call this function to correctly calculate the required "mydir" attribute as follows: |
| 833 | |
| 834 | mydir = TestBase.compute_mydir(__file__)''' |
| 835 | test_dir = os.path.dirname(test_file) |
| 836 | return test_dir[len(os.environ["LLDB_TEST"])+1:] |
| 837 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 838 | def TraceOn(self): |
| 839 | """Returns True if we are in trace mode (tracing detailed test execution).""" |
| 840 | return traceAlways |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 841 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 842 | @classmethod |
| 843 | def setUpClass(cls): |
Johnny Chen | da88434 | 2010-10-01 22:59:49 +0000 | [diff] [blame] | 844 | """ |
| 845 | Python unittest framework class setup fixture. |
| 846 | Do current directory manipulation. |
| 847 | """ |
| 848 | |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 849 | # Fail fast if 'mydir' attribute is not overridden. |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 850 | if not cls.mydir or len(cls.mydir) == 0: |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 851 | raise Exception("Subclasses must override the 'mydir' attribute.") |
Enrico Granata | 7e137e3 | 2012-10-24 18:14:21 +0000 | [diff] [blame] | 852 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 853 | # Save old working directory. |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 854 | cls.oldcwd = os.getcwd() |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 855 | |
| 856 | # Change current working directory if ${LLDB_TEST} is defined. |
| 857 | # See also dotest.py which sets up ${LLDB_TEST}. |
| 858 | if ("LLDB_TEST" in os.environ): |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 859 | if traceAlways: |
Johnny Chen | 703dbd0 | 2010-09-30 17:06:24 +0000 | [diff] [blame] | 860 | print >> sys.stderr, "Change dir to:", os.path.join(os.environ["LLDB_TEST"], cls.mydir) |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 861 | os.chdir(os.path.join(os.environ["LLDB_TEST"], cls.mydir)) |
| 862 | |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 863 | # Set platform context. |
| 864 | if sys.platform.startswith('darwin'): |
| 865 | cls.platformContext = _PlatformContext('DYLD_LIBRARY_PATH', 'lib', 'dylib') |
| 866 | elif sys.platform.startswith('linux') or sys.platform.startswith('freebsd'): |
| 867 | cls.platformContext = _PlatformContext('LD_LIBRARY_PATH', 'lib', 'so') |
Zachary Turner | be40b2f | 2014-12-02 21:32:44 +0000 | [diff] [blame] | 868 | else: |
| 869 | cls.platformContext = None |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 870 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 871 | @classmethod |
| 872 | def tearDownClass(cls): |
Johnny Chen | da88434 | 2010-10-01 22:59:49 +0000 | [diff] [blame] | 873 | """ |
| 874 | Python unittest framework class teardown fixture. |
| 875 | Do class-wide cleanup. |
| 876 | """ |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 877 | |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 878 | if doCleanup and not lldb.skip_build_and_cleanup: |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 879 | # First, let's do the platform-specific cleanup. |
Peter Collingbourne | 19f48d5 | 2011-06-20 19:06:20 +0000 | [diff] [blame] | 880 | module = builder_module() |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 881 | if not module.cleanup(): |
| 882 | raise Exception("Don't know how to do cleanup") |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 883 | |
Johnny Chen | 707b3c9 | 2010-10-11 22:25:46 +0000 | [diff] [blame] | 884 | # Subclass might have specific cleanup function defined. |
| 885 | if getattr(cls, "classCleanup", None): |
| 886 | if traceAlways: |
| 887 | print >> sys.stderr, "Call class-specific cleanup function for class:", cls |
| 888 | try: |
| 889 | cls.classCleanup() |
| 890 | except: |
| 891 | exc_type, exc_value, exc_tb = sys.exc_info() |
| 892 | traceback.print_exception(exc_type, exc_value, exc_tb) |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 893 | |
| 894 | # Restore old working directory. |
| 895 | if traceAlways: |
Johnny Chen | 703dbd0 | 2010-09-30 17:06:24 +0000 | [diff] [blame] | 896 | print >> sys.stderr, "Restore dir to:", cls.oldcwd |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 897 | os.chdir(cls.oldcwd) |
| 898 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 899 | @classmethod |
| 900 | def skipLongRunningTest(cls): |
| 901 | """ |
| 902 | By default, we skip long running test case. |
| 903 | This can be overridden by passing '-l' to the test driver (dotest.py). |
| 904 | """ |
| 905 | if "LLDB_SKIP_LONG_RUNNING_TEST" in os.environ and "NO" == os.environ["LLDB_SKIP_LONG_RUNNING_TEST"]: |
| 906 | return False |
| 907 | else: |
| 908 | return True |
Johnny Chen | ed49202 | 2011-06-21 00:53:00 +0000 | [diff] [blame] | 909 | |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 910 | def setUp(self): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 911 | """Fixture for unittest test case setup. |
| 912 | |
| 913 | It works with the test driver to conditionally skip tests and does other |
| 914 | initializations.""" |
Johnny Chen | 1a9f4dd | 2010-09-16 01:53:04 +0000 | [diff] [blame] | 915 | #import traceback |
| 916 | #traceback.print_stack() |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 917 | |
Daniel Malea | 9115f07 | 2013-08-06 15:02:32 +0000 | [diff] [blame] | 918 | if "LIBCXX_PATH" in os.environ: |
| 919 | self.libcxxPath = os.environ["LIBCXX_PATH"] |
| 920 | else: |
| 921 | self.libcxxPath = None |
| 922 | |
Johnny Chen | aaa82ff | 2011-08-02 22:54:37 +0000 | [diff] [blame] | 923 | if "LLDB_EXEC" in os.environ: |
| 924 | self.lldbExec = os.environ["LLDB_EXEC"] |
Johnny Chen | d890bfc | 2011-08-26 00:00:01 +0000 | [diff] [blame] | 925 | else: |
| 926 | self.lldbExec = None |
Hafiz Abid Qadeer | 1cbac4e | 2014-11-25 10:41:57 +0000 | [diff] [blame] | 927 | if "LLDBMI_EXEC" in os.environ: |
| 928 | self.lldbMiExec = os.environ["LLDBMI_EXEC"] |
| 929 | else: |
| 930 | self.lldbMiExec = None |
| 931 | self.dont_do_lldbmi_test = True |
Johnny Chen | d890bfc | 2011-08-26 00:00:01 +0000 | [diff] [blame] | 932 | if "LLDB_HERE" in os.environ: |
| 933 | self.lldbHere = os.environ["LLDB_HERE"] |
| 934 | else: |
| 935 | self.lldbHere = None |
Johnny Chen | ebe5172 | 2011-10-07 19:21:09 +0000 | [diff] [blame] | 936 | # If we spawn an lldb process for test (via pexpect), do not load the |
| 937 | # init file unless told otherwise. |
| 938 | if "NO_LLDBINIT" in os.environ and "NO" == os.environ["NO_LLDBINIT"]: |
| 939 | self.lldbOption = "" |
| 940 | else: |
| 941 | self.lldbOption = "--no-lldbinit" |
Johnny Chen | aaa82ff | 2011-08-02 22:54:37 +0000 | [diff] [blame] | 942 | |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 943 | # Assign the test method name to self.testMethodName. |
| 944 | # |
| 945 | # For an example of the use of this attribute, look at test/types dir. |
| 946 | # There are a bunch of test cases under test/types and we don't want the |
| 947 | # module cacheing subsystem to be confused with executable name "a.out" |
| 948 | # used for all the test cases. |
| 949 | self.testMethodName = self._testMethodName |
| 950 | |
Johnny Chen | f3e22ac | 2010-12-10 18:52:10 +0000 | [diff] [blame] | 951 | # Python API only test is decorated with @python_api_test, |
| 952 | # which also sets the "__python_api_test__" attribute of the |
| 953 | # function object to True. |
Johnny Chen | 4533dad | 2011-05-31 23:21:42 +0000 | [diff] [blame] | 954 | try: |
| 955 | if lldb.just_do_python_api_test: |
| 956 | testMethod = getattr(self, self._testMethodName) |
| 957 | if getattr(testMethod, "__python_api_test__", False): |
| 958 | pass |
| 959 | else: |
Johnny Chen | 5ccbccf | 2011-07-30 01:39:58 +0000 | [diff] [blame] | 960 | self.skipTest("non python api test") |
| 961 | except AttributeError: |
| 962 | pass |
| 963 | |
Hafiz Abid Qadeer | 1cbac4e | 2014-11-25 10:41:57 +0000 | [diff] [blame] | 964 | # lldb-mi only test is decorated with @lldbmi_test, |
| 965 | # which also sets the "__lldbmi_test__" attribute of the |
| 966 | # function object to True. |
| 967 | try: |
| 968 | if lldb.just_do_lldbmi_test: |
| 969 | testMethod = getattr(self, self._testMethodName) |
| 970 | if getattr(testMethod, "__lldbmi_test__", False): |
| 971 | pass |
| 972 | else: |
| 973 | self.skipTest("non lldb-mi test") |
| 974 | except AttributeError: |
| 975 | pass |
| 976 | |
Johnny Chen | 5ccbccf | 2011-07-30 01:39:58 +0000 | [diff] [blame] | 977 | # Benchmarks test is decorated with @benchmarks_test, |
| 978 | # which also sets the "__benchmarks_test__" attribute of the |
| 979 | # function object to True. |
| 980 | try: |
| 981 | if lldb.just_do_benchmarks_test: |
| 982 | testMethod = getattr(self, self._testMethodName) |
| 983 | if getattr(testMethod, "__benchmarks_test__", False): |
| 984 | pass |
| 985 | else: |
| 986 | self.skipTest("non benchmarks test") |
Johnny Chen | 4533dad | 2011-05-31 23:21:42 +0000 | [diff] [blame] | 987 | except AttributeError: |
| 988 | pass |
Johnny Chen | f3e22ac | 2010-12-10 18:52:10 +0000 | [diff] [blame] | 989 | |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 990 | # This is for the case of directly spawning 'lldb'/'gdb' and interacting |
| 991 | # with it using pexpect. |
| 992 | self.child = None |
| 993 | self.child_prompt = "(lldb) " |
| 994 | # If the child is interacting with the embedded script interpreter, |
| 995 | # there are two exits required during tear down, first to quit the |
| 996 | # embedded script interpreter and second to quit the lldb command |
| 997 | # interpreter. |
| 998 | self.child_in_script_interpreter = False |
| 999 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1000 | # These are for customized teardown cleanup. |
| 1001 | self.dict = None |
| 1002 | self.doTearDownCleanup = False |
| 1003 | # And in rare cases where there are multiple teardown cleanups. |
| 1004 | self.dicts = [] |
| 1005 | self.doTearDownCleanups = False |
| 1006 | |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1007 | # List of spawned subproces.Popen objects |
| 1008 | self.subprocesses = [] |
| 1009 | |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 1010 | # List of forked process PIDs |
| 1011 | self.forkedProcessPids = [] |
| 1012 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1013 | # Create a string buffer to record the session info, to be dumped into a |
| 1014 | # test case specific file if test failure is encountered. |
| 1015 | self.session = StringIO.StringIO() |
| 1016 | |
| 1017 | # Optimistically set __errored__, __failed__, __expected__ to False |
| 1018 | # initially. If the test errored/failed, the session info |
| 1019 | # (self.session) is then dumped into a session specific file for |
| 1020 | # diagnosis. |
| 1021 | self.__errored__ = False |
| 1022 | self.__failed__ = False |
| 1023 | self.__expected__ = False |
| 1024 | # We are also interested in unexpected success. |
| 1025 | self.__unexpected__ = False |
Johnny Chen | f79b076 | 2011-08-16 00:48:58 +0000 | [diff] [blame] | 1026 | # And skipped tests. |
| 1027 | self.__skipped__ = False |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1028 | |
| 1029 | # See addTearDownHook(self, hook) which allows the client to add a hook |
| 1030 | # function to be run during tearDown() time. |
| 1031 | self.hooks = [] |
| 1032 | |
| 1033 | # See HideStdout(self). |
| 1034 | self.sys_stdout_hidden = False |
| 1035 | |
Zachary Turner | be40b2f | 2014-12-02 21:32:44 +0000 | [diff] [blame] | 1036 | if self.platformContext: |
| 1037 | # set environment variable names for finding shared libraries |
| 1038 | self.dylibPath = self.platformContext.shlib_environment_var |
Daniel Malea | 179ff29 | 2012-11-26 21:21:11 +0000 | [diff] [blame] | 1039 | |
Johnny Chen | 2a80858 | 2011-10-19 16:48:07 +0000 | [diff] [blame] | 1040 | def runHooks(self, child=None, child_prompt=None, use_cmd_api=False): |
Johnny Chen | a737ba5 | 2011-10-19 01:06:21 +0000 | [diff] [blame] | 1041 | """Perform the run hooks to bring lldb debugger to the desired state. |
| 1042 | |
Johnny Chen | 2a80858 | 2011-10-19 16:48:07 +0000 | [diff] [blame] | 1043 | By default, expect a pexpect spawned child and child prompt to be |
| 1044 | supplied (use_cmd_api=False). If use_cmd_api is true, ignore the child |
| 1045 | and child prompt and use self.runCmd() to run the hooks one by one. |
| 1046 | |
Johnny Chen | a737ba5 | 2011-10-19 01:06:21 +0000 | [diff] [blame] | 1047 | Note that child is a process spawned by pexpect.spawn(). If not, your |
| 1048 | test case is mostly likely going to fail. |
| 1049 | |
| 1050 | See also dotest.py where lldb.runHooks are processed/populated. |
| 1051 | """ |
| 1052 | if not lldb.runHooks: |
| 1053 | self.skipTest("No runhooks specified for lldb, skip the test") |
Johnny Chen | 2a80858 | 2011-10-19 16:48:07 +0000 | [diff] [blame] | 1054 | if use_cmd_api: |
| 1055 | for hook in lldb.runhooks: |
| 1056 | self.runCmd(hook) |
| 1057 | else: |
| 1058 | if not child or not child_prompt: |
| 1059 | self.fail("Both child and child_prompt need to be defined.") |
| 1060 | for hook in lldb.runHooks: |
| 1061 | child.sendline(hook) |
| 1062 | child.expect_exact(child_prompt) |
Johnny Chen | a737ba5 | 2011-10-19 01:06:21 +0000 | [diff] [blame] | 1063 | |
Daniel Malea | 249287a | 2013-02-19 16:08:57 +0000 | [diff] [blame] | 1064 | def setAsync(self, value): |
| 1065 | """ Sets async mode to True/False and ensures it is reset after the testcase completes.""" |
| 1066 | old_async = self.dbg.GetAsync() |
| 1067 | self.dbg.SetAsync(value) |
| 1068 | self.addTearDownHook(lambda: self.dbg.SetAsync(old_async)) |
| 1069 | |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1070 | def cleanupSubprocesses(self): |
| 1071 | # Ensure any subprocesses are cleaned up |
| 1072 | for p in self.subprocesses: |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1073 | p.terminate() |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1074 | del p |
| 1075 | del self.subprocesses[:] |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 1076 | # Ensure any forked processes are cleaned up |
| 1077 | for pid in self.forkedProcessPids: |
| 1078 | if os.path.exists("/proc/" + str(pid)): |
| 1079 | os.kill(pid, signal.SIGTERM) |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1080 | |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 1081 | def spawnSubprocess(self, executable, args=[], install_remote=True): |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1082 | """ Creates a subprocess.Popen object with the specified executable and arguments, |
| 1083 | saves it in self.subprocesses, and returns the object. |
| 1084 | NOTE: if using this function, ensure you also call: |
| 1085 | |
| 1086 | self.addTearDownHook(self.cleanupSubprocesses) |
| 1087 | |
| 1088 | otherwise the test suite will leak processes. |
| 1089 | """ |
Tamas Berghammer | 04f51d1 | 2015-03-11 13:51:07 +0000 | [diff] [blame] | 1090 | proc = _RemoteProcess(install_remote) if lldb.remote_platform else _LocalProcess(self.TraceOn()) |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1091 | proc.launch(executable, args) |
Daniel Malea | 2dd69bb | 2013-02-15 21:21:52 +0000 | [diff] [blame] | 1092 | self.subprocesses.append(proc) |
| 1093 | return proc |
| 1094 | |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 1095 | def forkSubprocess(self, executable, args=[]): |
| 1096 | """ Fork a subprocess with its own group ID. |
| 1097 | NOTE: if using this function, ensure you also call: |
| 1098 | |
| 1099 | self.addTearDownHook(self.cleanupSubprocesses) |
| 1100 | |
| 1101 | otherwise the test suite will leak processes. |
| 1102 | """ |
| 1103 | child_pid = os.fork() |
| 1104 | if child_pid == 0: |
| 1105 | # If more I/O support is required, this can be beefed up. |
| 1106 | fd = os.open(os.devnull, os.O_RDWR) |
Daniel Malea | 6920746 | 2013-06-05 21:07:02 +0000 | [diff] [blame] | 1107 | os.dup2(fd, 1) |
| 1108 | os.dup2(fd, 2) |
| 1109 | # This call causes the child to have its of group ID |
| 1110 | os.setpgid(0,0) |
| 1111 | os.execvp(executable, [executable] + args) |
| 1112 | # Give the child time to get through the execvp() call |
| 1113 | time.sleep(0.1) |
| 1114 | self.forkedProcessPids.append(child_pid) |
| 1115 | return child_pid |
| 1116 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1117 | def HideStdout(self): |
| 1118 | """Hide output to stdout from the user. |
| 1119 | |
| 1120 | During test execution, there might be cases where we don't want to show the |
| 1121 | standard output to the user. For example, |
| 1122 | |
| 1123 | self.runCmd(r'''sc print "\n\n\tHello!\n"''') |
| 1124 | |
| 1125 | tests whether command abbreviation for 'script' works or not. There is no |
| 1126 | need to show the 'Hello' output to the user as long as the 'script' command |
| 1127 | succeeds and we are not in TraceOn() mode (see the '-t' option). |
| 1128 | |
| 1129 | In this case, the test method calls self.HideStdout(self) to redirect the |
| 1130 | sys.stdout to a null device, and restores the sys.stdout upon teardown. |
| 1131 | |
| 1132 | Note that you should only call this method at most once during a test case |
| 1133 | execution. Any subsequent call has no effect at all.""" |
| 1134 | if self.sys_stdout_hidden: |
| 1135 | return |
| 1136 | |
| 1137 | self.sys_stdout_hidden = True |
| 1138 | old_stdout = sys.stdout |
| 1139 | sys.stdout = open(os.devnull, 'w') |
| 1140 | def restore_stdout(): |
| 1141 | sys.stdout = old_stdout |
| 1142 | self.addTearDownHook(restore_stdout) |
| 1143 | |
| 1144 | # ======================================================================= |
| 1145 | # Methods for customized teardown cleanups as well as execution of hooks. |
| 1146 | # ======================================================================= |
| 1147 | |
| 1148 | def setTearDownCleanup(self, dictionary=None): |
| 1149 | """Register a cleanup action at tearDown() time with a dictinary""" |
| 1150 | self.dict = dictionary |
| 1151 | self.doTearDownCleanup = True |
| 1152 | |
| 1153 | def addTearDownCleanup(self, dictionary): |
| 1154 | """Add a cleanup action at tearDown() time with a dictinary""" |
| 1155 | self.dicts.append(dictionary) |
| 1156 | self.doTearDownCleanups = True |
| 1157 | |
| 1158 | def addTearDownHook(self, hook): |
| 1159 | """ |
| 1160 | Add a function to be run during tearDown() time. |
| 1161 | |
| 1162 | Hooks are executed in a first come first serve manner. |
| 1163 | """ |
| 1164 | if callable(hook): |
| 1165 | with recording(self, traceAlways) as sbuf: |
| 1166 | print >> sbuf, "Adding tearDown hook:", getsource_if_available(hook) |
| 1167 | self.hooks.append(hook) |
Enrico Granata | ab0e831 | 2014-11-05 21:31:57 +0000 | [diff] [blame] | 1168 | |
| 1169 | return self |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1170 | |
Jim Ingham | da3a386 | 2014-10-16 23:02:14 +0000 | [diff] [blame] | 1171 | def deletePexpectChild(self): |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1172 | # This is for the case of directly spawning 'lldb' and interacting with it |
| 1173 | # using pexpect. |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1174 | if self.child and self.child.isalive(): |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 1175 | import pexpect |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1176 | with recording(self, traceAlways) as sbuf: |
| 1177 | print >> sbuf, "tearing down the child process...." |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1178 | try: |
Daniel Malea | c9a0ec3 | 2013-02-22 00:41:26 +0000 | [diff] [blame] | 1179 | if self.child_in_script_interpreter: |
| 1180 | self.child.sendline('quit()') |
| 1181 | self.child.expect_exact(self.child_prompt) |
| 1182 | self.child.sendline('settings set interpreter.prompt-on-quit false') |
| 1183 | self.child.sendline('quit') |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1184 | self.child.expect(pexpect.EOF) |
Ilia K | 47448c2 | 2015-02-11 21:41:58 +0000 | [diff] [blame] | 1185 | except (ValueError, pexpect.ExceptionPexpect): |
| 1186 | # child is already terminated |
| 1187 | pass |
| 1188 | except OSError as exception: |
| 1189 | import errno |
| 1190 | if exception.errno != errno.EIO: |
| 1191 | # unexpected error |
| 1192 | raise |
Daniel Malea | c9a0ec3 | 2013-02-22 00:41:26 +0000 | [diff] [blame] | 1193 | # child is already terminated |
Johnny Chen | 985e740 | 2011-08-01 21:13:26 +0000 | [diff] [blame] | 1194 | pass |
Shawn Best | eb3e905 | 2014-11-06 17:52:15 +0000 | [diff] [blame] | 1195 | finally: |
| 1196 | # Give it one final blow to make sure the child is terminated. |
| 1197 | self.child.close() |
Jim Ingham | da3a386 | 2014-10-16 23:02:14 +0000 | [diff] [blame] | 1198 | |
| 1199 | def tearDown(self): |
| 1200 | """Fixture for unittest test case teardown.""" |
| 1201 | #import traceback |
| 1202 | #traceback.print_stack() |
| 1203 | |
| 1204 | self.deletePexpectChild() |
| 1205 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1206 | # Check and run any hook functions. |
| 1207 | for hook in reversed(self.hooks): |
| 1208 | with recording(self, traceAlways) as sbuf: |
| 1209 | print >> sbuf, "Executing tearDown hook:", getsource_if_available(hook) |
Enrico Granata | ab0e831 | 2014-11-05 21:31:57 +0000 | [diff] [blame] | 1210 | import inspect |
| 1211 | hook_argc = len(inspect.getargspec(hook).args) |
Enrico Granata | 6e0566c | 2014-11-17 19:00:20 +0000 | [diff] [blame] | 1212 | if hook_argc == 0 or getattr(hook,'im_self',None): |
Enrico Granata | ab0e831 | 2014-11-05 21:31:57 +0000 | [diff] [blame] | 1213 | hook() |
| 1214 | elif hook_argc == 1: |
| 1215 | hook(self) |
| 1216 | else: |
| 1217 | hook() # try the plain call and hope it works |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1218 | |
| 1219 | del self.hooks |
| 1220 | |
| 1221 | # Perform registered teardown cleanup. |
| 1222 | if doCleanup and self.doTearDownCleanup: |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 1223 | self.cleanup(dictionary=self.dict) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1224 | |
| 1225 | # In rare cases where there are multiple teardown cleanups added. |
| 1226 | if doCleanup and self.doTearDownCleanups: |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1227 | if self.dicts: |
| 1228 | for dict in reversed(self.dicts): |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 1229 | self.cleanup(dictionary=dict) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1230 | |
| 1231 | # Decide whether to dump the session info. |
| 1232 | self.dumpSessionInfo() |
| 1233 | |
| 1234 | # ========================================================= |
| 1235 | # Various callbacks to allow introspection of test progress |
| 1236 | # ========================================================= |
| 1237 | |
| 1238 | def markError(self): |
| 1239 | """Callback invoked when an error (unexpected exception) errored.""" |
| 1240 | self.__errored__ = True |
| 1241 | with recording(self, False) as sbuf: |
| 1242 | # False because there's no need to write "ERROR" to the stderr twice. |
| 1243 | # Once by the Python unittest framework, and a second time by us. |
| 1244 | print >> sbuf, "ERROR" |
| 1245 | |
| 1246 | def markFailure(self): |
| 1247 | """Callback invoked when a failure (test assertion failure) occurred.""" |
| 1248 | self.__failed__ = True |
| 1249 | with recording(self, False) as sbuf: |
| 1250 | # False because there's no need to write "FAIL" to the stderr twice. |
| 1251 | # Once by the Python unittest framework, and a second time by us. |
| 1252 | print >> sbuf, "FAIL" |
| 1253 | |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1254 | def markExpectedFailure(self,err,bugnumber): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1255 | """Callback invoked when an expected failure/error occurred.""" |
| 1256 | self.__expected__ = True |
| 1257 | with recording(self, False) as sbuf: |
| 1258 | # False because there's no need to write "expected failure" to the |
| 1259 | # stderr twice. |
| 1260 | # Once by the Python unittest framework, and a second time by us. |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1261 | if bugnumber == None: |
| 1262 | print >> sbuf, "expected failure" |
| 1263 | else: |
| 1264 | print >> sbuf, "expected failure (problem id:" + str(bugnumber) + ")" |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1265 | |
Johnny Chen | c5cc625 | 2011-08-15 23:09:08 +0000 | [diff] [blame] | 1266 | def markSkippedTest(self): |
| 1267 | """Callback invoked when a test is skipped.""" |
| 1268 | self.__skipped__ = True |
| 1269 | with recording(self, False) as sbuf: |
| 1270 | # False because there's no need to write "skipped test" to the |
| 1271 | # stderr twice. |
| 1272 | # Once by the Python unittest framework, and a second time by us. |
| 1273 | print >> sbuf, "skipped test" |
| 1274 | |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1275 | def markUnexpectedSuccess(self, bugnumber): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1276 | """Callback invoked when an unexpected success occurred.""" |
| 1277 | self.__unexpected__ = True |
| 1278 | with recording(self, False) as sbuf: |
| 1279 | # False because there's no need to write "unexpected success" to the |
| 1280 | # stderr twice. |
| 1281 | # Once by the Python unittest framework, and a second time by us. |
Enrico Granata | e6cedc1 | 2013-02-23 01:05:23 +0000 | [diff] [blame] | 1282 | if bugnumber == None: |
| 1283 | print >> sbuf, "unexpected success" |
| 1284 | else: |
| 1285 | print >> sbuf, "unexpected success (problem id:" + str(bugnumber) + ")" |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1286 | |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 1287 | def getRerunArgs(self): |
| 1288 | return " -f %s.%s" % (self.__class__.__name__, self._testMethodName) |
| 1289 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1290 | def dumpSessionInfo(self): |
| 1291 | """ |
| 1292 | Dump the debugger interactions leading to a test error/failure. This |
| 1293 | allows for more convenient postmortem analysis. |
| 1294 | |
| 1295 | See also LLDBTestResult (dotest.py) which is a singlton class derived |
| 1296 | from TextTestResult and overwrites addError, addFailure, and |
| 1297 | addExpectedFailure methods to allow us to to mark the test instance as |
| 1298 | such. |
| 1299 | """ |
| 1300 | |
| 1301 | # We are here because self.tearDown() detected that this test instance |
| 1302 | # either errored or failed. The lldb.test_result singleton contains |
| 1303 | # two lists (erros and failures) which get populated by the unittest |
| 1304 | # framework. Look over there for stack trace information. |
| 1305 | # |
| 1306 | # The lists contain 2-tuples of TestCase instances and strings holding |
| 1307 | # formatted tracebacks. |
| 1308 | # |
| 1309 | # See http://docs.python.org/library/unittest.html#unittest.TestResult. |
| 1310 | if self.__errored__: |
| 1311 | pairs = lldb.test_result.errors |
| 1312 | prefix = 'Error' |
| 1313 | elif self.__failed__: |
| 1314 | pairs = lldb.test_result.failures |
| 1315 | prefix = 'Failure' |
| 1316 | elif self.__expected__: |
| 1317 | pairs = lldb.test_result.expectedFailures |
| 1318 | prefix = 'ExpectedFailure' |
Johnny Chen | c5cc625 | 2011-08-15 23:09:08 +0000 | [diff] [blame] | 1319 | elif self.__skipped__: |
| 1320 | prefix = 'SkippedTest' |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1321 | elif self.__unexpected__: |
| 1322 | prefix = "UnexpectedSuccess" |
| 1323 | else: |
| 1324 | # Simply return, there's no session info to dump! |
| 1325 | return |
| 1326 | |
Johnny Chen | c5cc625 | 2011-08-15 23:09:08 +0000 | [diff] [blame] | 1327 | if not self.__unexpected__ and not self.__skipped__: |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1328 | for test, traceback in pairs: |
| 1329 | if test is self: |
| 1330 | print >> self.session, traceback |
| 1331 | |
Johnny Chen | 8082a00 | 2011-08-11 00:16:28 +0000 | [diff] [blame] | 1332 | testMethod = getattr(self, self._testMethodName) |
| 1333 | if getattr(testMethod, "__benchmarks_test__", False): |
| 1334 | benchmarks = True |
| 1335 | else: |
| 1336 | benchmarks = False |
| 1337 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1338 | dname = os.path.join(os.environ["LLDB_TEST"], |
| 1339 | os.environ["LLDB_SESSION_DIRNAME"]) |
| 1340 | if not os.path.isdir(dname): |
| 1341 | os.mkdir(dname) |
Zachary Turner | 756acba | 2014-10-14 21:54:14 +0000 | [diff] [blame] | 1342 | compiler = self.getCompiler() |
| 1343 | if compiler[1] == ':': |
| 1344 | compiler = compiler[2:] |
| 1345 | |
Tamas Berghammer | 6698d84 | 2015-03-12 10:24:11 +0000 | [diff] [blame] | 1346 | fname = "%s-%s-%s-%s.log" % (prefix, self.getArchitecture(), "_".join(compiler.split(os.path.sep)), self.id()) |
| 1347 | if len(fname) > 255: |
| 1348 | fname = "%s-%s-%s-%s.log" % (prefix, self.getArchitecture(), compiler.split(os.path.sep)[-1], self.id()) |
| 1349 | pname = os.path.join(dname, fname) |
| 1350 | with open(pname, "w") as f: |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1351 | import datetime |
| 1352 | print >> f, "Session info generated @", datetime.datetime.now().ctime() |
| 1353 | print >> f, self.session.getvalue() |
| 1354 | print >> f, "To rerun this test, issue the following command from the 'test' directory:\n" |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 1355 | print >> f, "./dotest.py %s -v %s %s" % (self.getRunOptions(), |
| 1356 | ('+b' if benchmarks else '-t'), |
| 1357 | self.getRerunArgs()) |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1358 | |
| 1359 | # ==================================================== |
| 1360 | # Config. methods supported through a plugin interface |
| 1361 | # (enables reading of the current test configuration) |
| 1362 | # ==================================================== |
| 1363 | |
| 1364 | def getArchitecture(self): |
| 1365 | """Returns the architecture in effect the test suite is running with.""" |
| 1366 | module = builder_module() |
| 1367 | return module.getArchitecture() |
| 1368 | |
| 1369 | def getCompiler(self): |
| 1370 | """Returns the compiler in effect the test suite is running with.""" |
| 1371 | module = builder_module() |
| 1372 | return module.getCompiler() |
| 1373 | |
Oleksiy Vyalov | dc4067c | 2014-11-26 18:30:04 +0000 | [diff] [blame] | 1374 | def getCompilerBinary(self): |
| 1375 | """Returns the compiler binary the test suite is running with.""" |
| 1376 | return self.getCompiler().split()[0] |
| 1377 | |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 1378 | def getCompilerVersion(self): |
| 1379 | """ Returns a string that represents the compiler version. |
| 1380 | Supports: llvm, clang. |
| 1381 | """ |
| 1382 | from lldbutil import which |
| 1383 | version = 'unknown' |
| 1384 | |
Oleksiy Vyalov | dc4067c | 2014-11-26 18:30:04 +0000 | [diff] [blame] | 1385 | compiler = self.getCompilerBinary() |
Zachary Turner | 9ef307b | 2014-07-22 16:19:29 +0000 | [diff] [blame] | 1386 | version_output = system([[which(compiler), "-v"]])[1] |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 1387 | for line in version_output.split(os.linesep): |
Greg Clayton | 2a844b7 | 2013-03-06 02:34:51 +0000 | [diff] [blame] | 1388 | m = re.search('version ([0-9\.]+)', line) |
Daniel Malea | 0aea016 | 2013-02-27 17:29:46 +0000 | [diff] [blame] | 1389 | if m: |
| 1390 | version = m.group(1) |
| 1391 | return version |
| 1392 | |
Greg Clayton | e0d0a76 | 2015-04-02 18:24:03 +0000 | [diff] [blame] | 1393 | def platformIsDarwin(self): |
| 1394 | """Returns true if the OS triple for the selected platform is any valid apple OS""" |
| 1395 | platform_name = self.getPlatform() |
| 1396 | return platform_name in getDarwinOSTriples() |
| 1397 | |
Vince Harron | 20952cc | 2015-04-03 01:00:06 +0000 | [diff] [blame^] | 1398 | def platformIsLinux(self): |
| 1399 | """Returns true if the OS triple for the selected platform is any valid apple OS""" |
| 1400 | platform_name = self.getPlatform() |
| 1401 | return platform_name == "linux" |
| 1402 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 1403 | def getPlatform(self): |
| 1404 | """Returns the platform the test suite is running on.""" |
Ed Maste | bf441f4 | 2015-03-31 16:37:10 +0000 | [diff] [blame] | 1405 | platform = lldb.DBG.GetSelectedPlatform().GetTriple().split('-')[2] |
| 1406 | if platform.startswith('freebsd'): |
| 1407 | platform = 'freebsd' |
| 1408 | return platform |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 1409 | |
Daniel Malea | adaaec9 | 2013-08-06 20:51:41 +0000 | [diff] [blame] | 1410 | def isIntelCompiler(self): |
| 1411 | """ Returns true if using an Intel (ICC) compiler, false otherwise. """ |
| 1412 | return any([x in self.getCompiler() for x in ["icc", "icpc", "icl"]]) |
| 1413 | |
Ashok Thirumurthi | 3b03728 | 2013-06-06 14:23:31 +0000 | [diff] [blame] | 1414 | def expectedCompilerVersion(self, compiler_version): |
| 1415 | """Returns True iff compiler_version[1] matches the current compiler version. |
| 1416 | Use compiler_version[0] to specify the operator used to determine if a match has occurred. |
| 1417 | Any operator other than the following defaults to an equality test: |
| 1418 | '>', '>=', "=>", '<', '<=', '=<', '!=', "!" or 'not' |
| 1419 | """ |
Ashok Thirumurthi | c97a608 | 2013-05-17 20:15:07 +0000 | [diff] [blame] | 1420 | if (compiler_version == None): |
| 1421 | return True |
| 1422 | operator = str(compiler_version[0]) |
| 1423 | version = compiler_version[1] |
| 1424 | |
| 1425 | if (version == None): |
| 1426 | return True |
| 1427 | if (operator == '>'): |
| 1428 | return self.getCompilerVersion() > version |
| 1429 | if (operator == '>=' or operator == '=>'): |
| 1430 | return self.getCompilerVersion() >= version |
| 1431 | if (operator == '<'): |
| 1432 | return self.getCompilerVersion() < version |
| 1433 | if (operator == '<=' or operator == '=<'): |
| 1434 | return self.getCompilerVersion() <= version |
| 1435 | if (operator == '!=' or operator == '!' or operator == 'not'): |
| 1436 | return str(version) not in str(self.getCompilerVersion()) |
| 1437 | return str(version) in str(self.getCompilerVersion()) |
| 1438 | |
| 1439 | def expectedCompiler(self, compilers): |
Ashok Thirumurthi | 3b03728 | 2013-06-06 14:23:31 +0000 | [diff] [blame] | 1440 | """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] | 1441 | if (compilers == None): |
| 1442 | return True |
Ashok Thirumurthi | 3b03728 | 2013-06-06 14:23:31 +0000 | [diff] [blame] | 1443 | |
| 1444 | for compiler in compilers: |
| 1445 | if compiler in self.getCompiler(): |
| 1446 | return True |
| 1447 | |
| 1448 | return False |
Ashok Thirumurthi | c97a608 | 2013-05-17 20:15:07 +0000 | [diff] [blame] | 1449 | |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1450 | def getRunOptions(self): |
| 1451 | """Command line option for -A and -C to run this test again, called from |
| 1452 | self.dumpSessionInfo().""" |
| 1453 | arch = self.getArchitecture() |
| 1454 | comp = self.getCompiler() |
Johnny Chen | b7bdd10 | 2011-08-24 19:48:51 +0000 | [diff] [blame] | 1455 | if arch: |
| 1456 | option_str = "-A " + arch |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1457 | else: |
Johnny Chen | b7bdd10 | 2011-08-24 19:48:51 +0000 | [diff] [blame] | 1458 | option_str = "" |
| 1459 | if comp: |
Johnny Chen | 531c085 | 2012-03-16 20:44:00 +0000 | [diff] [blame] | 1460 | option_str += " -C " + comp |
Johnny Chen | b7bdd10 | 2011-08-24 19:48:51 +0000 | [diff] [blame] | 1461 | return option_str |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1462 | |
| 1463 | # ================================================== |
| 1464 | # Build methods supported through a plugin interface |
| 1465 | # ================================================== |
| 1466 | |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 1467 | def getstdlibFlag(self): |
| 1468 | """ Returns the proper -stdlib flag, or empty if not required.""" |
| 1469 | if sys.platform.startswith("darwin") or sys.platform.startswith("freebsd"): |
| 1470 | stdlibflag = "-stdlib=libc++" |
| 1471 | else: |
| 1472 | stdlibflag = "" |
| 1473 | return stdlibflag |
| 1474 | |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 1475 | def getstdFlag(self): |
| 1476 | """ Returns the proper stdflag. """ |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1477 | if "gcc" in self.getCompiler() and "4.6" in self.getCompilerVersion(): |
Daniel Malea | 0b7c611 | 2013-05-06 19:31:31 +0000 | [diff] [blame] | 1478 | stdflag = "-std=c++0x" |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1479 | else: |
| 1480 | stdflag = "-std=c++11" |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 1481 | return stdflag |
| 1482 | |
| 1483 | def buildDriver(self, sources, exe_name): |
| 1484 | """ Platform-specific way to build a program that links with LLDB (via the liblldb.so |
| 1485 | or LLDB.framework). |
| 1486 | """ |
| 1487 | |
| 1488 | stdflag = self.getstdFlag() |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 1489 | stdlibflag = self.getstdlibFlag() |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1490 | |
| 1491 | if sys.platform.startswith("darwin"): |
| 1492 | dsym = os.path.join(self.lib_dir, 'LLDB.framework', 'LLDB') |
| 1493 | d = {'CXX_SOURCES' : sources, |
| 1494 | 'EXE' : exe_name, |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 1495 | 'CFLAGS_EXTRAS' : "%s %s" % (stdflag, stdlibflag), |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1496 | 'FRAMEWORK_INCLUDES' : "-F%s" % self.lib_dir, |
Stefanus Du Toit | 0400444 | 2013-07-30 19:19:49 +0000 | [diff] [blame] | 1497 | 'LD_EXTRAS' : "%s -Wl,-rpath,%s" % (dsym, self.lib_dir), |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1498 | } |
Ed Maste | 372c24d | 2013-07-25 21:02:34 +0000 | [diff] [blame] | 1499 | 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] | 1500 | d = {'CXX_SOURCES' : sources, |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1501 | 'EXE' : exe_name, |
Ed Maste | c97323e | 2014-04-01 18:47:58 +0000 | [diff] [blame] | 1502 | 'CFLAGS_EXTRAS' : "%s %s -I%s" % (stdflag, stdlibflag, os.path.join(os.environ["LLDB_SRC"], "include")), |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1503 | 'LD_EXTRAS' : "-L%s -llldb" % self.lib_dir} |
Adrian McCarthy | b016b3c | 2015-03-27 20:47:35 +0000 | [diff] [blame] | 1504 | elif sys.platform.startswith('win'): |
| 1505 | d = {'CXX_SOURCES' : sources, |
| 1506 | 'EXE' : exe_name, |
| 1507 | 'CFLAGS_EXTRAS' : "%s %s -I%s" % (stdflag, stdlibflag, os.path.join(os.environ["LLDB_SRC"], "include")), |
| 1508 | 'LD_EXTRAS' : "-L%s -lliblldb" % self.implib_dir} |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1509 | if self.TraceOn(): |
| 1510 | print "Building LLDB Driver (%s) from sources %s" % (exe_name, sources) |
| 1511 | |
| 1512 | self.buildDefault(dictionary=d) |
| 1513 | |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 1514 | def buildLibrary(self, sources, lib_name): |
| 1515 | """Platform specific way to build a default library. """ |
| 1516 | |
| 1517 | stdflag = self.getstdFlag() |
| 1518 | |
| 1519 | if sys.platform.startswith("darwin"): |
| 1520 | dsym = os.path.join(self.lib_dir, 'LLDB.framework', 'LLDB') |
| 1521 | d = {'DYLIB_CXX_SOURCES' : sources, |
| 1522 | 'DYLIB_NAME' : lib_name, |
| 1523 | 'CFLAGS_EXTRAS' : "%s -stdlib=libc++" % stdflag, |
| 1524 | 'FRAMEWORK_INCLUDES' : "-F%s" % self.lib_dir, |
| 1525 | 'LD_EXTRAS' : "%s -Wl,-rpath,%s -dynamiclib" % (dsym, self.lib_dir), |
| 1526 | } |
Adrian McCarthy | b016b3c | 2015-03-27 20:47:35 +0000 | [diff] [blame] | 1527 | elif sys.platform.startswith('freebsd') or sys.platform.startswith("linux") or os.environ.get('LLDB_BUILD_TYPE') == 'Makefile': |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 1528 | d = {'DYLIB_CXX_SOURCES' : sources, |
| 1529 | 'DYLIB_NAME' : lib_name, |
| 1530 | 'CFLAGS_EXTRAS' : "%s -I%s -fPIC" % (stdflag, os.path.join(os.environ["LLDB_SRC"], "include")), |
| 1531 | 'LD_EXTRAS' : "-shared -L%s -llldb" % self.lib_dir} |
Adrian McCarthy | b016b3c | 2015-03-27 20:47:35 +0000 | [diff] [blame] | 1532 | elif sys.platform.startswith("win"): |
| 1533 | d = {'DYLIB_CXX_SOURCES' : sources, |
| 1534 | 'DYLIB_NAME' : lib_name, |
| 1535 | 'CFLAGS_EXTRAS' : "%s -I%s -fPIC" % (stdflag, os.path.join(os.environ["LLDB_SRC"], "include")), |
| 1536 | 'LD_EXTRAS' : "-shared -l%s\liblldb.lib" % self.implib_dir} |
Matt Kopec | 7663b3a | 2013-09-25 17:44:00 +0000 | [diff] [blame] | 1537 | if self.TraceOn(): |
| 1538 | print "Building LLDB Library (%s) from sources %s" % (lib_name, sources) |
| 1539 | |
| 1540 | self.buildDefault(dictionary=d) |
| 1541 | |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1542 | def buildProgram(self, sources, exe_name): |
| 1543 | """ Platform specific way to build an executable from C/C++ sources. """ |
| 1544 | d = {'CXX_SOURCES' : sources, |
| 1545 | 'EXE' : exe_name} |
| 1546 | self.buildDefault(dictionary=d) |
| 1547 | |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 1548 | def buildDefault(self, architecture=None, compiler=None, dictionary=None, clean=True): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1549 | """Platform specific way to build the default binaries.""" |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 1550 | if lldb.skip_build_and_cleanup: |
| 1551 | return |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1552 | module = builder_module() |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 1553 | if not module.buildDefault(self, architecture, compiler, dictionary, clean): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1554 | raise Exception("Don't know how to build default binary") |
| 1555 | |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 1556 | def buildDsym(self, architecture=None, compiler=None, dictionary=None, clean=True): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1557 | """Platform specific way to build binaries with dsym info.""" |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 1558 | if lldb.skip_build_and_cleanup: |
| 1559 | return |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1560 | module = builder_module() |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 1561 | if not module.buildDsym(self, architecture, compiler, dictionary, clean): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1562 | raise Exception("Don't know how to build binary with dsym") |
| 1563 | |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 1564 | def buildDwarf(self, architecture=None, compiler=None, dictionary=None, clean=True): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1565 | """Platform specific way to build binaries with dwarf maps.""" |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 1566 | if lldb.skip_build_and_cleanup: |
| 1567 | return |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1568 | module = builder_module() |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 1569 | if not module.buildDwarf(self, architecture, compiler, dictionary, clean): |
Johnny Chen | fb4264c | 2011-08-01 19:50:58 +0000 | [diff] [blame] | 1570 | raise Exception("Don't know how to build binary with dwarf") |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 1571 | |
Oleksiy Vyalov | 49b71c6 | 2015-01-22 20:03:21 +0000 | [diff] [blame] | 1572 | def signBinary(self, binary_path): |
| 1573 | if sys.platform.startswith("darwin"): |
| 1574 | codesign_cmd = "codesign --force --sign lldb_codesign %s" % (binary_path) |
| 1575 | call(codesign_cmd, shell=True) |
| 1576 | |
Kuba Brecka | beed821 | 2014-09-04 01:03:18 +0000 | [diff] [blame] | 1577 | def findBuiltClang(self): |
| 1578 | """Tries to find and use Clang from the build directory as the compiler (instead of the system compiler).""" |
| 1579 | paths_to_try = [ |
| 1580 | "llvm-build/Release+Asserts/x86_64/Release+Asserts/bin/clang", |
| 1581 | "llvm-build/Debug+Asserts/x86_64/Debug+Asserts/bin/clang", |
| 1582 | "llvm-build/Release/x86_64/Release/bin/clang", |
| 1583 | "llvm-build/Debug/x86_64/Debug/bin/clang", |
| 1584 | ] |
| 1585 | lldb_root_path = os.path.join(os.path.dirname(__file__), "..") |
| 1586 | for p in paths_to_try: |
| 1587 | path = os.path.join(lldb_root_path, p) |
| 1588 | if os.path.exists(path): |
| 1589 | return path |
Ilia K | d995305 | 2015-03-12 07:19:41 +0000 | [diff] [blame] | 1590 | |
| 1591 | # Tries to find clang at the same folder as the lldb |
| 1592 | path = os.path.join(os.path.dirname(self.lldbExec), "clang") |
| 1593 | if os.path.exists(path): |
| 1594 | return path |
Kuba Brecka | beed821 | 2014-09-04 01:03:18 +0000 | [diff] [blame] | 1595 | |
| 1596 | return os.environ["CC"] |
| 1597 | |
Tamas Berghammer | 765b5e5 | 2015-02-25 13:26:28 +0000 | [diff] [blame] | 1598 | def getBuildFlags(self, use_cpp11=True, use_libcxx=False, use_libstdcxx=False): |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 1599 | """ Returns a dictionary (which can be provided to build* functions above) which |
| 1600 | contains OS-specific build flags. |
| 1601 | """ |
| 1602 | cflags = "" |
Tamas Berghammer | 765b5e5 | 2015-02-25 13:26:28 +0000 | [diff] [blame] | 1603 | ldflags = "" |
Daniel Malea | 9115f07 | 2013-08-06 15:02:32 +0000 | [diff] [blame] | 1604 | |
| 1605 | # On Mac OS X, unless specifically requested to use libstdc++, use libc++ |
| 1606 | if not use_libstdcxx and sys.platform.startswith('darwin'): |
| 1607 | use_libcxx = True |
| 1608 | |
| 1609 | if use_libcxx and self.libcxxPath: |
| 1610 | cflags += "-stdlib=libc++ " |
| 1611 | if self.libcxxPath: |
| 1612 | libcxxInclude = os.path.join(self.libcxxPath, "include") |
| 1613 | libcxxLib = os.path.join(self.libcxxPath, "lib") |
| 1614 | if os.path.isdir(libcxxInclude) and os.path.isdir(libcxxLib): |
| 1615 | cflags += "-nostdinc++ -I%s -L%s -Wl,-rpath,%s " % (libcxxInclude, libcxxLib, libcxxLib) |
| 1616 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 1617 | if use_cpp11: |
| 1618 | cflags += "-std=" |
| 1619 | if "gcc" in self.getCompiler() and "4.6" in self.getCompilerVersion(): |
| 1620 | cflags += "c++0x" |
| 1621 | else: |
| 1622 | cflags += "c++11" |
Ed Maste | dbd5950 | 2014-02-02 19:24:15 +0000 | [diff] [blame] | 1623 | if sys.platform.startswith("darwin") or sys.platform.startswith("freebsd"): |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 1624 | cflags += " -stdlib=libc++" |
| 1625 | elif "clang" in self.getCompiler(): |
| 1626 | cflags += " -stdlib=libstdc++" |
| 1627 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 1628 | return {'CFLAGS_EXTRAS' : cflags, |
| 1629 | 'LD_EXTRAS' : ldflags, |
| 1630 | } |
| 1631 | |
Johnny Chen | 9f4f5d9 | 2011-08-12 20:19:22 +0000 | [diff] [blame] | 1632 | def cleanup(self, dictionary=None): |
| 1633 | """Platform specific way to do cleanup after build.""" |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 1634 | if lldb.skip_build_and_cleanup: |
| 1635 | return |
Johnny Chen | 9f4f5d9 | 2011-08-12 20:19:22 +0000 | [diff] [blame] | 1636 | module = builder_module() |
| 1637 | if not module.cleanup(self, dictionary): |
Johnny Chen | 0fddfb2 | 2011-11-17 19:57:27 +0000 | [diff] [blame] | 1638 | raise Exception("Don't know how to do cleanup with dictionary: "+dictionary) |
Johnny Chen | 9f4f5d9 | 2011-08-12 20:19:22 +0000 | [diff] [blame] | 1639 | |
Daniel Malea | 55faa40 | 2013-05-02 21:44:31 +0000 | [diff] [blame] | 1640 | def getLLDBLibraryEnvVal(self): |
| 1641 | """ Returns the path that the OS-specific library search environment variable |
| 1642 | (self.dylibPath) should be set to in order for a program to find the LLDB |
| 1643 | library. If an environment variable named self.dylibPath is already set, |
| 1644 | the new path is appended to it and returned. |
| 1645 | """ |
| 1646 | existing_library_path = os.environ[self.dylibPath] if self.dylibPath in os.environ else None |
| 1647 | if existing_library_path: |
| 1648 | return "%s:%s" % (existing_library_path, self.lib_dir) |
| 1649 | elif sys.platform.startswith("darwin"): |
| 1650 | return os.path.join(self.lib_dir, 'LLDB.framework') |
| 1651 | else: |
| 1652 | return self.lib_dir |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 1653 | |
Ed Maste | 437f8f6 | 2013-09-09 14:04:04 +0000 | [diff] [blame] | 1654 | def getLibcPlusPlusLibs(self): |
| 1655 | if sys.platform.startswith('freebsd'): |
| 1656 | return ['libc++.so.1'] |
| 1657 | else: |
| 1658 | return ['libc++.1.dylib','libc++abi.dylib'] |
| 1659 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 1660 | class TestBase(Base): |
| 1661 | """ |
| 1662 | This abstract base class is meant to be subclassed. It provides default |
| 1663 | implementations for setUpClass(), tearDownClass(), setUp(), and tearDown(), |
| 1664 | among other things. |
| 1665 | |
| 1666 | Important things for test class writers: |
| 1667 | |
| 1668 | - Overwrite the mydir class attribute, otherwise your test class won't |
| 1669 | run. It specifies the relative directory to the top level 'test' so |
| 1670 | the test harness can change to the correct working directory before |
| 1671 | running your test. |
| 1672 | |
| 1673 | - The setUp method sets up things to facilitate subsequent interactions |
| 1674 | with the debugger as part of the test. These include: |
| 1675 | - populate the test method name |
| 1676 | - create/get a debugger set with synchronous mode (self.dbg) |
| 1677 | - get the command interpreter from with the debugger (self.ci) |
| 1678 | - create a result object for use with the command interpreter |
| 1679 | (self.res) |
| 1680 | - plus other stuffs |
| 1681 | |
| 1682 | - The tearDown method tries to perform some necessary cleanup on behalf |
| 1683 | of the test to return the debugger to a good state for the next test. |
| 1684 | These include: |
| 1685 | - execute any tearDown hooks registered by the test method with |
| 1686 | TestBase.addTearDownHook(); examples can be found in |
| 1687 | settings/TestSettings.py |
| 1688 | - kill the inferior process associated with each target, if any, |
| 1689 | and, then delete the target from the debugger's target list |
| 1690 | - perform build cleanup before running the next test method in the |
| 1691 | same test class; examples of registering for this service can be |
| 1692 | found in types/TestIntegerTypes.py with the call: |
| 1693 | - self.setTearDownCleanup(dictionary=d) |
| 1694 | |
| 1695 | - Similarly setUpClass and tearDownClass perform classwise setup and |
| 1696 | teardown fixtures. The tearDownClass method invokes a default build |
| 1697 | cleanup for the entire test class; also, subclasses can implement the |
| 1698 | classmethod classCleanup(cls) to perform special class cleanup action. |
| 1699 | |
| 1700 | - The instance methods runCmd and expect are used heavily by existing |
| 1701 | test cases to send a command to the command interpreter and to perform |
| 1702 | string/pattern matching on the output of such command execution. The |
| 1703 | expect method also provides a mode to peform string/pattern matching |
| 1704 | without running a command. |
| 1705 | |
| 1706 | - The build methods buildDefault, buildDsym, and buildDwarf are used to |
| 1707 | build the binaries used during a particular test scenario. A plugin |
| 1708 | should be provided for the sys.platform running the test suite. The |
| 1709 | Mac OS X implementation is located in plugins/darwin.py. |
| 1710 | """ |
| 1711 | |
| 1712 | # Maximum allowed attempts when launching the inferior process. |
| 1713 | # Can be overridden by the LLDB_MAX_LAUNCH_COUNT environment variable. |
| 1714 | maxLaunchCount = 3; |
| 1715 | |
| 1716 | # Time to wait before the next launching attempt in second(s). |
| 1717 | # Can be overridden by the LLDB_TIME_WAIT_NEXT_LAUNCH environment variable. |
| 1718 | timeWaitNextLaunch = 1.0; |
| 1719 | |
| 1720 | def doDelay(self): |
| 1721 | """See option -w of dotest.py.""" |
| 1722 | if ("LLDB_WAIT_BETWEEN_TEST_CASES" in os.environ and |
| 1723 | os.environ["LLDB_WAIT_BETWEEN_TEST_CASES"] == 'YES'): |
| 1724 | waitTime = 1.0 |
| 1725 | if "LLDB_TIME_WAIT_BETWEEN_TEST_CASES" in os.environ: |
| 1726 | waitTime = float(os.environ["LLDB_TIME_WAIT_BETWEEN_TEST_CASES"]) |
| 1727 | time.sleep(waitTime) |
| 1728 | |
Enrico Granata | 165f8af | 2012-09-21 19:10:53 +0000 | [diff] [blame] | 1729 | # Returns the list of categories to which this test case belongs |
| 1730 | # by default, look for a ".categories" file, and read its contents |
| 1731 | # if no such file exists, traverse the hierarchy - we guarantee |
| 1732 | # a .categories to exist at the top level directory so we do not end up |
| 1733 | # looping endlessly - subclasses are free to define their own categories |
| 1734 | # in whatever way makes sense to them |
| 1735 | def getCategories(self): |
| 1736 | import inspect |
| 1737 | import os.path |
| 1738 | folder = inspect.getfile(self.__class__) |
| 1739 | folder = os.path.dirname(folder) |
| 1740 | while folder != '/': |
| 1741 | categories_file_name = os.path.join(folder,".categories") |
| 1742 | if os.path.exists(categories_file_name): |
| 1743 | categories_file = open(categories_file_name,'r') |
| 1744 | categories = categories_file.readline() |
| 1745 | categories_file.close() |
| 1746 | categories = str.replace(categories,'\n','') |
| 1747 | categories = str.replace(categories,'\r','') |
| 1748 | return categories.split(',') |
| 1749 | else: |
| 1750 | folder = os.path.dirname(folder) |
| 1751 | continue |
| 1752 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 1753 | def setUp(self): |
| 1754 | #import traceback |
| 1755 | #traceback.print_stack() |
| 1756 | |
| 1757 | # Works with the test driver to conditionally skip tests via decorators. |
| 1758 | Base.setUp(self) |
| 1759 | |
Johnny Chen | a74bb0a | 2011-08-01 18:46:13 +0000 | [diff] [blame] | 1760 | try: |
| 1761 | if lldb.blacklist: |
| 1762 | className = self.__class__.__name__ |
| 1763 | classAndMethodName = "%s.%s" % (className, self._testMethodName) |
| 1764 | if className in lldb.blacklist: |
| 1765 | self.skipTest(lldb.blacklist.get(className)) |
| 1766 | elif classAndMethodName in lldb.blacklist: |
| 1767 | self.skipTest(lldb.blacklist.get(classAndMethodName)) |
| 1768 | except AttributeError: |
| 1769 | pass |
| 1770 | |
Johnny Chen | ed49202 | 2011-06-21 00:53:00 +0000 | [diff] [blame] | 1771 | # Insert some delay between successive test cases if specified. |
| 1772 | self.doDelay() |
Johnny Chen | 0ed37c9 | 2010-10-07 02:04:14 +0000 | [diff] [blame] | 1773 | |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 1774 | if "LLDB_MAX_LAUNCH_COUNT" in os.environ: |
| 1775 | self.maxLaunchCount = int(os.environ["LLDB_MAX_LAUNCH_COUNT"]) |
| 1776 | |
Johnny Chen | 430eb76 | 2010-10-19 16:00:42 +0000 | [diff] [blame] | 1777 | if "LLDB_TIME_WAIT_NEXT_LAUNCH" in os.environ: |
Johnny Chen | 4921b11 | 2010-11-29 20:20:34 +0000 | [diff] [blame] | 1778 | self.timeWaitNextLaunch = float(os.environ["LLDB_TIME_WAIT_NEXT_LAUNCH"]) |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 1779 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1780 | # Create the debugger instance if necessary. |
| 1781 | try: |
| 1782 | self.dbg = lldb.DBG |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1783 | except AttributeError: |
| 1784 | self.dbg = lldb.SBDebugger.Create() |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 1785 | |
Johnny Chen | 3cd1e55 | 2011-05-25 19:06:18 +0000 | [diff] [blame] | 1786 | if not self.dbg: |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1787 | raise Exception('Invalid debugger instance') |
| 1788 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1789 | # |
| 1790 | # Warning: MAJOR HACK AHEAD! |
| 1791 | # If we are running testsuite remotely (by checking lldb.lldbtest_remote_sandbox), |
| 1792 | # redefine the self.dbg.CreateTarget(filename) method to execute a "file filename" |
| 1793 | # command, instead. See also runCmd() where it decorates the "file filename" call |
| 1794 | # with additional functionality when running testsuite remotely. |
| 1795 | # |
| 1796 | if lldb.lldbtest_remote_sandbox: |
| 1797 | def DecoratedCreateTarget(arg): |
| 1798 | self.runCmd("file %s" % arg) |
| 1799 | target = self.dbg.GetSelectedTarget() |
| 1800 | # |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 1801 | # SBtarget.LaunchSimple () currently not working for remote platform? |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1802 | # johnny @ 04/23/2012 |
| 1803 | # |
| 1804 | def DecoratedLaunchSimple(argv, envp, wd): |
| 1805 | self.runCmd("run") |
| 1806 | return target.GetProcess() |
| 1807 | target.LaunchSimple = DecoratedLaunchSimple |
| 1808 | |
| 1809 | return target |
| 1810 | self.dbg.CreateTarget = DecoratedCreateTarget |
| 1811 | if self.TraceOn(): |
| 1812 | print "self.dbg.Create is redefined to:\n%s" % getsource_if_available(DecoratedCreateTarget) |
| 1813 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1814 | # We want our debugger to be synchronous. |
| 1815 | self.dbg.SetAsync(False) |
| 1816 | |
| 1817 | # Retrieve the associated command interpreter instance. |
| 1818 | self.ci = self.dbg.GetCommandInterpreter() |
| 1819 | if not self.ci: |
| 1820 | raise Exception('Could not get the command interpreter') |
| 1821 | |
| 1822 | # And the result object. |
| 1823 | self.res = lldb.SBCommandReturnObject() |
| 1824 | |
Johnny Chen | 44d2497 | 2012-04-16 18:55:15 +0000 | [diff] [blame] | 1825 | # Run global pre-flight code, if defined via the config file. |
| 1826 | if lldb.pre_flight: |
| 1827 | lldb.pre_flight(self) |
| 1828 | |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 1829 | if lldb.remote_platform: |
| 1830 | #remote_test_dir = os.path.join(lldb.remote_platform_working_dir, self.mydir) |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 1831 | remote_test_dir = os.path.join(lldb.remote_platform_working_dir, |
| 1832 | self.getArchitecture(), |
| 1833 | str(self.test_number), |
| 1834 | self.mydir) |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 1835 | error = lldb.remote_platform.MakeDirectory(remote_test_dir, 0700) |
| 1836 | if error.Success(): |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 1837 | lldb.remote_platform.SetWorkingDirectory(remote_test_dir) |
| 1838 | else: |
| 1839 | print "error: making remote directory '%s': %s" % (remote_test_dir, error) |
| 1840 | |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 1841 | def registerSharedLibrariesWithTarget(self, target, shlibs): |
| 1842 | '''If we are remotely running the test suite, register the shared libraries with the target so they get uploaded, otherwise do nothing |
| 1843 | |
| 1844 | Any modules in the target that have their remote install file specification set will |
| 1845 | get uploaded to the remote host. This function registers the local copies of the |
| 1846 | shared libraries with the target and sets their remote install locations so they will |
| 1847 | be uploaded when the target is run. |
| 1848 | ''' |
Zachary Turner | be40b2f | 2014-12-02 21:32:44 +0000 | [diff] [blame] | 1849 | if not shlibs or not self.platformContext: |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1850 | return None |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 1851 | |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1852 | shlib_environment_var = self.platformContext.shlib_environment_var |
| 1853 | shlib_prefix = self.platformContext.shlib_prefix |
| 1854 | shlib_extension = '.' + self.platformContext.shlib_extension |
| 1855 | |
| 1856 | working_dir = self.get_process_working_directory() |
| 1857 | environment = ['%s=%s' % (shlib_environment_var, working_dir)] |
| 1858 | # Add any shared libraries to our target if remote so they get |
| 1859 | # uploaded into the working directory on the remote side |
| 1860 | for name in shlibs: |
| 1861 | # The path can be a full path to a shared library, or a make file name like "Foo" for |
| 1862 | # "libFoo.dylib" or "libFoo.so", or "Foo.so" for "Foo.so" or "libFoo.so", or just a |
| 1863 | # basename like "libFoo.so". So figure out which one it is and resolve the local copy |
| 1864 | # of the shared library accordingly |
| 1865 | if os.path.exists(name): |
| 1866 | local_shlib_path = name # name is the full path to the local shared library |
| 1867 | else: |
| 1868 | # Check relative names |
| 1869 | local_shlib_path = os.path.join(os.getcwd(), shlib_prefix + name + shlib_extension) |
| 1870 | if not os.path.exists(local_shlib_path): |
| 1871 | local_shlib_path = os.path.join(os.getcwd(), name + shlib_extension) |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 1872 | if not os.path.exists(local_shlib_path): |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1873 | local_shlib_path = os.path.join(os.getcwd(), name) |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 1874 | |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1875 | # Make sure we found the local shared library in the above code |
| 1876 | self.assertTrue(os.path.exists(local_shlib_path)) |
| 1877 | |
| 1878 | # Add the shared library to our target |
| 1879 | shlib_module = target.AddModule(local_shlib_path, None, None, None) |
| 1880 | if lldb.remote_platform: |
Greg Clayton | 35c9134 | 2014-11-17 18:40:27 +0000 | [diff] [blame] | 1881 | # We must set the remote install location if we want the shared library |
| 1882 | # to get uploaded to the remote target |
| 1883 | remote_shlib_path = os.path.join(lldb.remote_platform.GetWorkingDirectory(), os.path.basename(local_shlib_path)) |
| 1884 | shlib_module.SetRemoteInstallFileSpec(lldb.SBFileSpec(remote_shlib_path, False)) |
Oleksiy Vyalov | a3ff6af | 2014-12-01 23:21:18 +0000 | [diff] [blame] | 1885 | |
| 1886 | return environment |
| 1887 | |
Enrico Granata | 4481816 | 2012-10-24 01:23:57 +0000 | [diff] [blame] | 1888 | # utility methods that tests can use to access the current objects |
| 1889 | def target(self): |
| 1890 | if not self.dbg: |
| 1891 | raise Exception('Invalid debugger instance') |
| 1892 | return self.dbg.GetSelectedTarget() |
| 1893 | |
| 1894 | def process(self): |
| 1895 | if not self.dbg: |
| 1896 | raise Exception('Invalid debugger instance') |
| 1897 | return self.dbg.GetSelectedTarget().GetProcess() |
| 1898 | |
| 1899 | def thread(self): |
| 1900 | if not self.dbg: |
| 1901 | raise Exception('Invalid debugger instance') |
| 1902 | return self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread() |
| 1903 | |
| 1904 | def frame(self): |
| 1905 | if not self.dbg: |
| 1906 | raise Exception('Invalid debugger instance') |
| 1907 | return self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame() |
| 1908 | |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 1909 | def get_process_working_directory(self): |
| 1910 | '''Get the working directory that should be used when launching processes for local or remote processes.''' |
| 1911 | if lldb.remote_platform: |
| 1912 | # Remote tests set the platform working directory up in TestBase.setUp() |
| 1913 | return lldb.remote_platform.GetWorkingDirectory() |
| 1914 | else: |
| 1915 | # local tests change directory into each test subdirectory |
| 1916 | return os.getcwd() |
| 1917 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1918 | def tearDown(self): |
Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame] | 1919 | #import traceback |
| 1920 | #traceback.print_stack() |
| 1921 | |
Johnny Chen | 3794ad9 | 2011-06-15 21:24:24 +0000 | [diff] [blame] | 1922 | # Delete the target(s) from the debugger as a general cleanup step. |
| 1923 | # This includes terminating the process for each target, if any. |
| 1924 | # We'd like to reuse the debugger for our next test without incurring |
| 1925 | # the initialization overhead. |
| 1926 | targets = [] |
| 1927 | for target in self.dbg: |
| 1928 | if target: |
| 1929 | targets.append(target) |
| 1930 | process = target.GetProcess() |
| 1931 | if process: |
| 1932 | rc = self.invoke(process, "Kill") |
| 1933 | self.assertTrue(rc.Success(), PROCESS_KILLED) |
| 1934 | for target in targets: |
| 1935 | self.dbg.DeleteTarget(target) |
Johnny Chen | 6ca006c | 2010-08-16 21:28:10 +0000 | [diff] [blame] | 1936 | |
Johnny Chen | 44d2497 | 2012-04-16 18:55:15 +0000 | [diff] [blame] | 1937 | # Run global post-flight code, if defined via the config file. |
| 1938 | if lldb.post_flight: |
| 1939 | lldb.post_flight(self) |
| 1940 | |
Zachary Turner | 65fe1eb | 2015-03-26 16:43:25 +0000 | [diff] [blame] | 1941 | # Do this last, to make sure it's in reverse order from how we setup. |
| 1942 | Base.tearDown(self) |
| 1943 | |
Zachary Turner | 9581204 | 2015-03-26 18:54:21 +0000 | [diff] [blame] | 1944 | # This must be the last statement, otherwise teardown hooks or other |
| 1945 | # lines might depend on this still being active. |
| 1946 | del self.dbg |
| 1947 | |
Johnny Chen | 86268e4 | 2011-09-30 21:48:35 +0000 | [diff] [blame] | 1948 | def switch_to_thread_with_stop_reason(self, stop_reason): |
| 1949 | """ |
| 1950 | Run the 'thread list' command, and select the thread with stop reason as |
| 1951 | 'stop_reason'. If no such thread exists, no select action is done. |
| 1952 | """ |
| 1953 | from lldbutil import stop_reason_to_str |
| 1954 | self.runCmd('thread list') |
| 1955 | output = self.res.GetOutput() |
| 1956 | thread_line_pattern = re.compile("^[ *] thread #([0-9]+):.*stop reason = %s" % |
| 1957 | stop_reason_to_str(stop_reason)) |
| 1958 | for line in output.splitlines(): |
| 1959 | matched = thread_line_pattern.match(line) |
| 1960 | if matched: |
| 1961 | self.runCmd('thread select %s' % matched.group(1)) |
| 1962 | |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 1963 | def runCmd(self, cmd, msg=None, check=True, trace=False, inHistory=False): |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 1964 | """ |
| 1965 | Ask the command interpreter to handle the command and then check its |
| 1966 | return status. |
| 1967 | """ |
| 1968 | # Fail fast if 'cmd' is not meaningful. |
| 1969 | if not cmd or len(cmd) == 0: |
| 1970 | raise Exception("Bad 'cmd' parameter encountered") |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 1971 | |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 1972 | trace = (True if traceAlways else trace) |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 1973 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1974 | # This is an opportunity to insert the 'platform target-install' command if we are told so |
| 1975 | # via the settig of lldb.lldbtest_remote_sandbox. |
| 1976 | if cmd.startswith("target create "): |
| 1977 | cmd = cmd.replace("target create ", "file ") |
| 1978 | if cmd.startswith("file ") and lldb.lldbtest_remote_sandbox: |
| 1979 | with recording(self, trace) as sbuf: |
| 1980 | the_rest = cmd.split("file ")[1] |
| 1981 | # Split the rest of the command line. |
| 1982 | atoms = the_rest.split() |
| 1983 | # |
| 1984 | # NOTE: This assumes that the options, if any, follow the file command, |
| 1985 | # instead of follow the specified target. |
| 1986 | # |
| 1987 | target = atoms[-1] |
| 1988 | # Now let's get the absolute pathname of our target. |
| 1989 | abs_target = os.path.abspath(target) |
| 1990 | print >> sbuf, "Found a file command, target (with absolute pathname)=%s" % abs_target |
| 1991 | fpath, fname = os.path.split(abs_target) |
| 1992 | parent_dir = os.path.split(fpath)[0] |
| 1993 | platform_target_install_command = 'platform target-install %s %s' % (fpath, lldb.lldbtest_remote_sandbox) |
| 1994 | print >> sbuf, "Insert this command to be run first: %s" % platform_target_install_command |
| 1995 | self.ci.HandleCommand(platform_target_install_command, self.res) |
| 1996 | # And this is the file command we want to execute, instead. |
| 1997 | # |
| 1998 | # Warning: SIDE EFFECT AHEAD!!! |
| 1999 | # Populate the remote executable pathname into the lldb namespace, |
| 2000 | # so that test cases can grab this thing out of the namespace. |
| 2001 | # |
| 2002 | lldb.lldbtest_remote_sandboxed_executable = abs_target.replace(parent_dir, lldb.lldbtest_remote_sandbox) |
| 2003 | cmd = "file -P %s %s %s" % (lldb.lldbtest_remote_sandboxed_executable, the_rest.replace(target, ''), abs_target) |
| 2004 | print >> sbuf, "And this is the replaced file command: %s" % cmd |
| 2005 | |
Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 2006 | running = (cmd.startswith("run") or cmd.startswith("process launch")) |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2007 | |
Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 2008 | for i in range(self.maxLaunchCount if running else 1): |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 2009 | self.ci.HandleCommand(cmd, self.res, inHistory) |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2010 | |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2011 | with recording(self, trace) as sbuf: |
| 2012 | print >> sbuf, "runCmd:", cmd |
Johnny Chen | ab254f5 | 2010-10-15 16:13:00 +0000 | [diff] [blame] | 2013 | if not check: |
Johnny Chen | 27b107b | 2010-10-15 18:52:22 +0000 | [diff] [blame] | 2014 | print >> sbuf, "check of return status not required" |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2015 | if self.res.Succeeded(): |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2016 | print >> sbuf, "output:", self.res.GetOutput() |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2017 | else: |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2018 | print >> sbuf, "runCmd failed!" |
| 2019 | print >> sbuf, self.res.GetError() |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2020 | |
Johnny Chen | ff3d01d | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 2021 | if self.res.Succeeded(): |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 2022 | break |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2023 | elif running: |
Johnny Chen | cf7f74e | 2011-01-19 02:02:08 +0000 | [diff] [blame] | 2024 | # For process launch, wait some time before possible next try. |
| 2025 | time.sleep(self.timeWaitNextLaunch) |
Johnny Chen | 552d671 | 2012-08-01 19:56:04 +0000 | [diff] [blame] | 2026 | with recording(self, trace) as sbuf: |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2027 | print >> sbuf, "Command '" + cmd + "' failed!" |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 2028 | |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2029 | if check: |
| 2030 | self.assertTrue(self.res.Succeeded(), |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 2031 | msg if msg else CMD_MSG(cmd)) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2032 | |
Jim Ingham | 63dfc72 | 2012-09-22 00:05:11 +0000 | [diff] [blame] | 2033 | def match (self, str, patterns, msg=None, trace=False, error=False, matching=True, exe=True): |
| 2034 | """run command in str, and match the result against regexp in patterns returning the match object for the first matching pattern |
| 2035 | |
| 2036 | Otherwise, all the arguments have the same meanings as for the expect function""" |
| 2037 | |
| 2038 | trace = (True if traceAlways else trace) |
| 2039 | |
| 2040 | if exe: |
| 2041 | # First run the command. If we are expecting error, set check=False. |
| 2042 | # Pass the assert message along since it provides more semantic info. |
| 2043 | self.runCmd(str, msg=msg, trace = (True if trace else False), check = not error) |
| 2044 | |
| 2045 | # Then compare the output against expected strings. |
| 2046 | output = self.res.GetError() if error else self.res.GetOutput() |
| 2047 | |
| 2048 | # If error is True, the API client expects the command to fail! |
| 2049 | if error: |
| 2050 | self.assertFalse(self.res.Succeeded(), |
| 2051 | "Command '" + str + "' is expected to fail!") |
| 2052 | else: |
| 2053 | # No execution required, just compare str against the golden input. |
| 2054 | output = str |
| 2055 | with recording(self, trace) as sbuf: |
| 2056 | print >> sbuf, "looking at:", output |
| 2057 | |
| 2058 | # The heading says either "Expecting" or "Not expecting". |
| 2059 | heading = "Expecting" if matching else "Not expecting" |
| 2060 | |
| 2061 | for pattern in patterns: |
| 2062 | # Match Objects always have a boolean value of True. |
| 2063 | match_object = re.search(pattern, output) |
| 2064 | matched = bool(match_object) |
| 2065 | with recording(self, trace) as sbuf: |
| 2066 | print >> sbuf, "%s pattern: %s" % (heading, pattern) |
| 2067 | print >> sbuf, "Matched" if matched else "Not matched" |
| 2068 | if matched: |
| 2069 | break |
| 2070 | |
| 2071 | self.assertTrue(matched if matching else not matched, |
| 2072 | msg if msg else EXP_MSG(str, exe)) |
| 2073 | |
| 2074 | return match_object |
| 2075 | |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 2076 | 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] | 2077 | """ |
| 2078 | Similar to runCmd; with additional expect style output matching ability. |
| 2079 | |
| 2080 | Ask the command interpreter to handle the command and then check its |
| 2081 | return status. The 'msg' parameter specifies an informational assert |
| 2082 | message. We expect the output from running the command to start with |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2083 | 'startstr', matches the substrings contained in 'substrs', and regexp |
| 2084 | matches the patterns contained in 'patterns'. |
Johnny Chen | b330786 | 2010-09-17 22:28:51 +0000 | [diff] [blame] | 2085 | |
| 2086 | If the keyword argument error is set to True, it signifies that the API |
| 2087 | 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] | 2088 | from running the command is retrieved and compared against the golden |
Johnny Chen | b330786 | 2010-09-17 22:28:51 +0000 | [diff] [blame] | 2089 | input, instead. |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2090 | |
| 2091 | If the keyword argument matching is set to False, it signifies that the API |
| 2092 | client is expecting the output of the command not to match the golden |
| 2093 | input. |
Johnny Chen | 9c48b8d | 2010-09-21 23:33:30 +0000 | [diff] [blame] | 2094 | |
| 2095 | Finally, the required argument 'str' represents the lldb command to be |
| 2096 | sent to the command interpreter. In case the keyword argument 'exe' is |
| 2097 | set to False, the 'str' is treated as a string to be matched/not-matched |
| 2098 | against the golden input. |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2099 | """ |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2100 | trace = (True if traceAlways else trace) |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 2101 | |
Johnny Chen | 9c48b8d | 2010-09-21 23:33:30 +0000 | [diff] [blame] | 2102 | if exe: |
| 2103 | # First run the command. If we are expecting error, set check=False. |
Johnny Chen | 62d4f86 | 2010-10-28 21:10:32 +0000 | [diff] [blame] | 2104 | # Pass the assert message along since it provides more semantic info. |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 2105 | 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] | 2106 | |
Johnny Chen | 9c48b8d | 2010-09-21 23:33:30 +0000 | [diff] [blame] | 2107 | # Then compare the output against expected strings. |
| 2108 | output = self.res.GetError() if error else self.res.GetOutput() |
Johnny Chen | b330786 | 2010-09-17 22:28:51 +0000 | [diff] [blame] | 2109 | |
Johnny Chen | 9c48b8d | 2010-09-21 23:33:30 +0000 | [diff] [blame] | 2110 | # If error is True, the API client expects the command to fail! |
| 2111 | if error: |
| 2112 | self.assertFalse(self.res.Succeeded(), |
| 2113 | "Command '" + str + "' is expected to fail!") |
| 2114 | else: |
| 2115 | # No execution required, just compare str against the golden input. |
Enrico Granata | bc08ab4 | 2012-10-23 00:09:02 +0000 | [diff] [blame] | 2116 | if isinstance(str,lldb.SBCommandReturnObject): |
| 2117 | output = str.GetOutput() |
| 2118 | else: |
| 2119 | output = str |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2120 | with recording(self, trace) as sbuf: |
| 2121 | print >> sbuf, "looking at:", output |
Johnny Chen | b330786 | 2010-09-17 22:28:51 +0000 | [diff] [blame] | 2122 | |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2123 | # The heading says either "Expecting" or "Not expecting". |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2124 | heading = "Expecting" if matching else "Not expecting" |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2125 | |
| 2126 | # Start from the startstr, if specified. |
| 2127 | # If there's no startstr, set the initial state appropriately. |
| 2128 | matched = output.startswith(startstr) if startstr else (True if matching else False) |
Johnny Chen | b145bba | 2010-08-20 18:25:15 +0000 | [diff] [blame] | 2129 | |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2130 | if startstr: |
| 2131 | with recording(self, trace) as sbuf: |
| 2132 | print >> sbuf, "%s start string: %s" % (heading, startstr) |
| 2133 | print >> sbuf, "Matched" if matched else "Not matched" |
Johnny Chen | b145bba | 2010-08-20 18:25:15 +0000 | [diff] [blame] | 2134 | |
Johnny Chen | 86268e4 | 2011-09-30 21:48:35 +0000 | [diff] [blame] | 2135 | # Look for endstr, if specified. |
| 2136 | keepgoing = matched if matching else not matched |
| 2137 | if endstr: |
| 2138 | matched = output.endswith(endstr) |
| 2139 | with recording(self, trace) as sbuf: |
| 2140 | print >> sbuf, "%s end string: %s" % (heading, endstr) |
| 2141 | print >> sbuf, "Matched" if matched else "Not matched" |
| 2142 | |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2143 | # Look for sub strings, if specified. |
| 2144 | keepgoing = matched if matching else not matched |
| 2145 | if substrs and keepgoing: |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2146 | for str in substrs: |
Johnny Chen | b052f6c | 2010-09-23 23:35:28 +0000 | [diff] [blame] | 2147 | matched = output.find(str) != -1 |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2148 | with recording(self, trace) as sbuf: |
| 2149 | print >> sbuf, "%s sub string: %s" % (heading, str) |
| 2150 | print >> sbuf, "Matched" if matched else "Not matched" |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2151 | keepgoing = matched if matching else not matched |
| 2152 | if not keepgoing: |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2153 | break |
| 2154 | |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2155 | # Search for regular expression patterns, if specified. |
| 2156 | keepgoing = matched if matching else not matched |
| 2157 | if patterns and keepgoing: |
| 2158 | for pattern in patterns: |
| 2159 | # Match Objects always have a boolean value of True. |
| 2160 | matched = bool(re.search(pattern, output)) |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2161 | with recording(self, trace) as sbuf: |
| 2162 | print >> sbuf, "%s pattern: %s" % (heading, pattern) |
| 2163 | print >> sbuf, "Matched" if matched else "Not matched" |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2164 | keepgoing = matched if matching else not matched |
| 2165 | if not keepgoing: |
| 2166 | break |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 2167 | |
| 2168 | self.assertTrue(matched if matching else not matched, |
Johnny Chen | c0c67f2 | 2010-11-09 18:42:22 +0000 | [diff] [blame] | 2169 | msg if msg else EXP_MSG(str, exe)) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 2170 | |
Johnny Chen | f3c5923 | 2010-08-25 22:52:45 +0000 | [diff] [blame] | 2171 | def invoke(self, obj, name, trace=False): |
Johnny Chen | 61703c9 | 2010-08-25 22:56:10 +0000 | [diff] [blame] | 2172 | """Use reflection to call a method dynamically with no argument.""" |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2173 | trace = (True if traceAlways else trace) |
Johnny Chen | f3c5923 | 2010-08-25 22:52:45 +0000 | [diff] [blame] | 2174 | |
| 2175 | method = getattr(obj, name) |
| 2176 | import inspect |
| 2177 | self.assertTrue(inspect.ismethod(method), |
| 2178 | name + "is a method name of object: " + str(obj)) |
| 2179 | result = method() |
Johnny Chen | 150c3cc | 2010-10-15 01:18:29 +0000 | [diff] [blame] | 2180 | with recording(self, trace) as sbuf: |
| 2181 | print >> sbuf, str(method) + ":", result |
Johnny Chen | f3c5923 | 2010-08-25 22:52:45 +0000 | [diff] [blame] | 2182 | return result |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 2183 | |
Johnny Chen | f359cf2 | 2011-05-27 23:36:52 +0000 | [diff] [blame] | 2184 | # ================================================= |
| 2185 | # Misc. helper methods for debugging test execution |
| 2186 | # ================================================= |
| 2187 | |
Johnny Chen | 56b92a7 | 2011-07-11 19:15:11 +0000 | [diff] [blame] | 2188 | def DebugSBValue(self, val): |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2189 | """Debug print a SBValue object, if traceAlways is True.""" |
Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 2190 | from lldbutil import value_type_to_str |
Johnny Chen | 87bb589 | 2010-11-03 21:37:58 +0000 | [diff] [blame] | 2191 | |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 2192 | if not traceAlways: |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 2193 | return |
| 2194 | |
| 2195 | err = sys.stderr |
| 2196 | err.write(val.GetName() + ":\n") |
Johnny Chen | 86268e4 | 2011-09-30 21:48:35 +0000 | [diff] [blame] | 2197 | err.write('\t' + "TypeName -> " + val.GetTypeName() + '\n') |
| 2198 | err.write('\t' + "ByteSize -> " + str(val.GetByteSize()) + '\n') |
| 2199 | err.write('\t' + "NumChildren -> " + str(val.GetNumChildren()) + '\n') |
| 2200 | err.write('\t' + "Value -> " + str(val.GetValue()) + '\n') |
| 2201 | err.write('\t' + "ValueAsUnsigned -> " + str(val.GetValueAsUnsigned())+ '\n') |
| 2202 | err.write('\t' + "ValueType -> " + value_type_to_str(val.GetValueType()) + '\n') |
| 2203 | err.write('\t' + "Summary -> " + str(val.GetSummary()) + '\n') |
| 2204 | err.write('\t' + "IsPointerType -> " + str(val.TypeIsPointerType()) + '\n') |
| 2205 | err.write('\t' + "Location -> " + val.GetLocation() + '\n') |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 2206 | |
Johnny Chen | 36c5eb1 | 2011-08-05 20:17:27 +0000 | [diff] [blame] | 2207 | def DebugSBType(self, type): |
| 2208 | """Debug print a SBType object, if traceAlways is True.""" |
| 2209 | if not traceAlways: |
| 2210 | return |
| 2211 | |
| 2212 | err = sys.stderr |
| 2213 | err.write(type.GetName() + ":\n") |
| 2214 | err.write('\t' + "ByteSize -> " + str(type.GetByteSize()) + '\n') |
| 2215 | err.write('\t' + "IsPointerType -> " + str(type.IsPointerType()) + '\n') |
| 2216 | err.write('\t' + "IsReferenceType -> " + str(type.IsReferenceType()) + '\n') |
| 2217 | |
Johnny Chen | b877f1e | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 2218 | def DebugPExpect(self, child): |
| 2219 | """Debug the spwaned pexpect object.""" |
| 2220 | if not traceAlways: |
| 2221 | return |
| 2222 | |
| 2223 | print child |
Filipe Cabecinhas | 0eec15a | 2012-06-20 10:13:40 +0000 | [diff] [blame] | 2224 | |
| 2225 | @classmethod |
| 2226 | def RemoveTempFile(cls, file): |
| 2227 | if os.path.exists(file): |
| 2228 | os.remove(file) |