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