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