blob: ab52b30b9c0d02aaaf62076a8f2340c64bc28cfe [file] [log] [blame]
Johnny Chenbf6ffa32010-07-03 03:41:59 +00001"""
2LLDB module which provides the abstract base class of lldb test case.
3
4The concrete subclass can override lldbtest.TesBase in order to inherit the
5common behavior for unitest.TestCase.setUp/tearDown implemented in this file.
6
7The subclass should override the attribute mydir in order for the python runtime
8to locate the individual test cases when running as part of a large test suite
9or when running each test case as a separate python invocation.
10
11./dotest.py provides a test driver which sets up the environment to run the
Johnny Chenc98892e2012-05-16 20:41:28 +000012entire of part of the test suite . Example:
Johnny Chenbf6ffa32010-07-03 03:41:59 +000013
Johnny Chenc98892e2012-05-16 20:41:28 +000014# Exercises the test suite in the types directory....
15/Volumes/data/lldb/svn/ToT/test $ ./dotest.py -A x86_64 types
Johnny Chen57b47382010-09-02 22:25:47 +000016...
Johnny Chend0190a62010-08-23 17:10:44 +000017
Johnny Chenc98892e2012-05-16 20:41:28 +000018Session logs for test failures/errors/unexpected successes will go into directory '2012-05-16-13_35_42'
19Command invoked: python ./dotest.py -A x86_64 types
20compilers=['clang']
Johnny Chend0190a62010-08-23 17:10:44 +000021
Johnny Chenc98892e2012-05-16 20:41:28 +000022Configuration: arch=x86_64 compiler=clang
Johnny Chend0190a62010-08-23 17:10:44 +000023----------------------------------------------------------------------
Johnny Chenc98892e2012-05-16 20:41:28 +000024Collected 72 tests
25
26........................................................................
27----------------------------------------------------------------------
28Ran 72 tests in 135.468s
Johnny Chend0190a62010-08-23 17:10:44 +000029
30OK
Johnny Chenbf6ffa32010-07-03 03:41:59 +000031$
32"""
33
Johnny Chen90312a82010-09-21 22:34:45 +000034import os, sys, traceback
Enrico Granata7e137e32012-10-24 18:14:21 +000035import os.path
Johnny Chenea88e942010-09-21 21:08:53 +000036import re
Daniel Malea69207462013-06-05 21:07:02 +000037import signal
Johnny Chen8952a2d2010-08-30 21:35:00 +000038from subprocess import *
Johnny Chen150c3cc2010-10-15 01:18:29 +000039import StringIO
Johnny Chenf2b70232010-08-25 18:49:48 +000040import time
Johnny Chena33a93c2010-08-30 23:08:52 +000041import types
Johnny Chen73258832010-08-05 23:42:46 +000042import unittest2
Johnny Chenbf6ffa32010-07-03 03:41:59 +000043import lldb
44
Johnny Chen707b3c92010-10-11 22:25:46 +000045# See also dotest.parseOptionsAndInitTestdirs(), where the environment variables
Johnny Chend2047fa2011-01-19 18:18:47 +000046# LLDB_COMMAND_TRACE and LLDB_DO_CLEANUP are set from '-t' and '-r dir' options.
Johnny Chen707b3c92010-10-11 22:25:46 +000047
48# By default, traceAlways is False.
Johnny Chen8d55a342010-08-31 17:42:54 +000049if "LLDB_COMMAND_TRACE" in os.environ and os.environ["LLDB_COMMAND_TRACE"]=="YES":
50 traceAlways = True
51else:
52 traceAlways = False
53
Johnny Chen707b3c92010-10-11 22:25:46 +000054# By default, doCleanup is True.
55if "LLDB_DO_CLEANUP" in os.environ and os.environ["LLDB_DO_CLEANUP"]=="NO":
56 doCleanup = False
57else:
58 doCleanup = True
59
Johnny Chen8d55a342010-08-31 17:42:54 +000060
Johnny Chen00778092010-08-09 22:01:17 +000061#
62# Some commonly used assert messages.
63#
64
Johnny Chenaa902922010-09-17 22:45:27 +000065COMMAND_FAILED_AS_EXPECTED = "Command has failed as expected"
66
Johnny Chen00778092010-08-09 22:01:17 +000067CURRENT_EXECUTABLE_SET = "Current executable set successfully"
68
Johnny Chen7d1d7532010-09-02 21:23:12 +000069PROCESS_IS_VALID = "Process is valid"
70
71PROCESS_KILLED = "Process is killed successfully"
72
Johnny Chend5f66fc2010-12-23 01:12:19 +000073PROCESS_EXITED = "Process exited successfully"
74
75PROCESS_STOPPED = "Process status should be stopped"
76
Johnny Chen5ee88192010-08-27 23:47:36 +000077RUN_SUCCEEDED = "Process is launched successfully"
Johnny Chen00778092010-08-09 22:01:17 +000078
Johnny Chen17941842010-08-09 23:44:24 +000079RUN_COMPLETED = "Process exited successfully"
Johnny Chen00778092010-08-09 22:01:17 +000080
Johnny Chen67af43f2010-10-05 19:27:32 +000081BACKTRACE_DISPLAYED_CORRECTLY = "Backtrace displayed correctly"
82
Johnny Chen17941842010-08-09 23:44:24 +000083BREAKPOINT_CREATED = "Breakpoint created successfully"
84
Johnny Chenf10af382010-12-04 00:07:24 +000085BREAKPOINT_STATE_CORRECT = "Breakpoint state is correct"
86
Johnny Chene76896c2010-08-17 21:33:31 +000087BREAKPOINT_PENDING_CREATED = "Pending breakpoint created successfully"
88
Johnny Chen17941842010-08-09 23:44:24 +000089BREAKPOINT_HIT_ONCE = "Breakpoint resolved with hit cout = 1"
Johnny Chen00778092010-08-09 22:01:17 +000090
Johnny Chen703dbd02010-09-30 17:06:24 +000091BREAKPOINT_HIT_TWICE = "Breakpoint resolved with hit cout = 2"
92
Johnny Chen164f1e12010-10-15 18:07:09 +000093BREAKPOINT_HIT_THRICE = "Breakpoint resolved with hit cout = 3"
94
Greg Clayton5db6b792012-10-24 18:24:14 +000095MISSING_EXPECTED_REGISTERS = "At least one expected register is unavailable."
96
Johnny Chen89109ed12011-06-27 20:05:23 +000097OBJECT_PRINTED_CORRECTLY = "Object printed correctly"
98
Johnny Chen5b3a3572010-12-09 18:22:12 +000099SOURCE_DISPLAYED_CORRECTLY = "Source code displayed correctly"
100
Johnny Chenc70b02a2010-09-22 23:00:20 +0000101STEP_OUT_SUCCEEDED = "Thread step-out succeeded"
102
Johnny Chen1691a162011-04-15 16:44:48 +0000103STOPPED_DUE_TO_EXC_BAD_ACCESS = "Process should be stopped due to bad access exception"
104
Ashok Thirumurthib4e51342013-05-17 15:35:15 +0000105STOPPED_DUE_TO_ASSERT = "Process should be stopped due to an assertion"
106
Johnny Chen5d6c4642010-11-10 23:46:38 +0000107STOPPED_DUE_TO_BREAKPOINT = "Process should be stopped due to breakpoint"
Johnny Chende0338b2010-11-10 20:20:06 +0000108
Johnny Chen5d6c4642010-11-10 23:46:38 +0000109STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS = "%s, %s" % (
110 STOPPED_DUE_TO_BREAKPOINT, "instead, the actual stop reason is: '%s'")
Johnny Chen00778092010-08-09 22:01:17 +0000111
Johnny Chen2e431ce2010-10-20 18:38:48 +0000112STOPPED_DUE_TO_BREAKPOINT_CONDITION = "Stopped due to breakpoint condition"
113
Johnny Chen0a3d1ca2010-12-13 21:49:58 +0000114STOPPED_DUE_TO_BREAKPOINT_IGNORE_COUNT = "Stopped due to breakpoint and ignore count"
115
Johnny Chenc066ab42010-10-14 01:22:03 +0000116STOPPED_DUE_TO_SIGNAL = "Process state is stopped due to signal"
117
Johnny Chen00778092010-08-09 22:01:17 +0000118STOPPED_DUE_TO_STEP_IN = "Process state is stopped due to step in"
119
Johnny Chenf68cc122011-09-15 21:09:59 +0000120STOPPED_DUE_TO_WATCHPOINT = "Process should be stopped due to watchpoint"
121
Johnny Chen3c884a02010-08-24 22:07:56 +0000122DATA_TYPES_DISPLAYED_CORRECTLY = "Data type(s) displayed correctly"
123
Johnny Chen5fca8ca2010-08-26 20:04:17 +0000124VALID_BREAKPOINT = "Got a valid breakpoint"
125
Johnny Chen5bfb8ee2010-10-22 18:10:25 +0000126VALID_BREAKPOINT_LOCATION = "Got a valid breakpoint location"
127
Johnny Chen7209d84f2011-05-06 23:26:12 +0000128VALID_COMMAND_INTERPRETER = "Got a valid command interpreter"
129
Johnny Chen5ee88192010-08-27 23:47:36 +0000130VALID_FILESPEC = "Got a valid filespec"
131
Johnny Chen025d1b82010-12-08 01:25:21 +0000132VALID_MODULE = "Got a valid module"
133
Johnny Chen5fca8ca2010-08-26 20:04:17 +0000134VALID_PROCESS = "Got a valid process"
135
Johnny Chen025d1b82010-12-08 01:25:21 +0000136VALID_SYMBOL = "Got a valid symbol"
137
Johnny Chen5fca8ca2010-08-26 20:04:17 +0000138VALID_TARGET = "Got a valid target"
139
Johnny Chen15f247a2012-02-03 20:43:00 +0000140VALID_TYPE = "Got a valid type"
141
Johnny Chen5819ab42011-07-15 22:28:10 +0000142VALID_VARIABLE = "Got a valid variable"
143
Johnny Chen981463d2010-08-25 19:00:04 +0000144VARIABLES_DISPLAYED_CORRECTLY = "Variable(s) displayed correctly"
Johnny Chen00778092010-08-09 22:01:17 +0000145
Johnny Chenf68cc122011-09-15 21:09:59 +0000146WATCHPOINT_CREATED = "Watchpoint created successfully"
Johnny Chen5fca8ca2010-08-26 20:04:17 +0000147
Johnny Chenc0c67f22010-11-09 18:42:22 +0000148def CMD_MSG(str):
Johnny Chenaacf92e2011-05-31 22:16:51 +0000149 '''A generic "Command '%s' returns successfully" message generator.'''
Johnny Chenc0c67f22010-11-09 18:42:22 +0000150 return "Command '%s' returns successfully" % str
151
Johnny Chen3bc8ae42012-03-15 19:10:00 +0000152def COMPLETION_MSG(str_before, str_after):
Johnny Chen98aceb02012-01-20 23:02:51 +0000153 '''A generic message generator for the completion mechanism.'''
154 return "'%s' successfully completes to '%s'" % (str_before, str_after)
155
Johnny Chenc0c67f22010-11-09 18:42:22 +0000156def EXP_MSG(str, exe):
Johnny Chenaacf92e2011-05-31 22:16:51 +0000157 '''A generic "'%s' returns expected result" message generator if exe.
158 Otherwise, it generates "'%s' matches expected result" message.'''
Johnny Chenc0c67f22010-11-09 18:42:22 +0000159 return "'%s' %s expected result" % (str, 'returns' if exe else 'matches')
Johnny Chen17941842010-08-09 23:44:24 +0000160
Johnny Chen3343f042010-10-19 19:11:38 +0000161def SETTING_MSG(setting):
Johnny Chenaacf92e2011-05-31 22:16:51 +0000162 '''A generic "Value of setting '%s' is correct" message generator.'''
Johnny Chen3343f042010-10-19 19:11:38 +0000163 return "Value of setting '%s' is correct" % setting
164
Johnny Chen27c41232010-08-26 21:49:29 +0000165def EnvArray():
Johnny Chenaacf92e2011-05-31 22:16:51 +0000166 """Returns an env variable array from the os.environ map object."""
Johnny Chen27c41232010-08-26 21:49:29 +0000167 return map(lambda k,v: k+"="+v, os.environ.keys(), os.environ.values())
168
Johnny Chen47ceb032010-10-11 23:52:19 +0000169def line_number(filename, string_to_match):
170 """Helper function to return the line number of the first matched string."""
171 with open(filename, 'r') as f:
172 for i, line in enumerate(f):
173 if line.find(string_to_match) != -1:
174 # Found our match.
Johnny Chencd9b7772010-10-12 00:09:25 +0000175 return i+1
Johnny Chen1691a162011-04-15 16:44:48 +0000176 raise Exception("Unable to find '%s' within file %s" % (string_to_match, filename))
Johnny Chen47ceb032010-10-11 23:52:19 +0000177
Johnny Chen67af43f2010-10-05 19:27:32 +0000178def pointer_size():
179 """Return the pointer size of the host system."""
180 import ctypes
181 a_pointer = ctypes.c_void_p(0xffff)
182 return 8 * ctypes.sizeof(a_pointer)
183
Johnny Chen57816732012-02-09 02:01:59 +0000184def is_exe(fpath):
185 """Returns true if fpath is an executable."""
186 return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
187
188def which(program):
189 """Returns the full path to a program; None otherwise."""
190 fpath, fname = os.path.split(program)
191 if fpath:
192 if is_exe(program):
193 return program
194 else:
195 for path in os.environ["PATH"].split(os.pathsep):
196 exe_file = os.path.join(path, program)
197 if is_exe(exe_file):
198 return exe_file
199 return None
200
Johnny Chen150c3cc2010-10-15 01:18:29 +0000201class recording(StringIO.StringIO):
202 """
203 A nice little context manager for recording the debugger interactions into
204 our session object. If trace flag is ON, it also emits the interactions
205 into the stderr.
206 """
207 def __init__(self, test, trace):
Johnny Chen690fcef2010-10-15 23:55:05 +0000208 """Create a StringIO instance; record the session obj and trace flag."""
Johnny Chen150c3cc2010-10-15 01:18:29 +0000209 StringIO.StringIO.__init__(self)
Johnny Chen0241f142011-08-16 22:06:17 +0000210 # The test might not have undergone the 'setUp(self)' phase yet, so that
211 # the attribute 'session' might not even exist yet.
Johnny Chenbfcf37f2011-08-16 17:06:45 +0000212 self.session = getattr(test, "session", None) if test else None
Johnny Chen150c3cc2010-10-15 01:18:29 +0000213 self.trace = trace
214
215 def __enter__(self):
216 """
217 Context management protocol on entry to the body of the with statement.
218 Just return the StringIO object.
219 """
220 return self
221
222 def __exit__(self, type, value, tb):
223 """
224 Context management protocol on exit from the body of the with statement.
225 If trace is ON, it emits the recordings into stderr. Always add the
226 recordings to our session object. And close the StringIO object, too.
227 """
228 if self.trace:
Johnny Chen690fcef2010-10-15 23:55:05 +0000229 print >> sys.stderr, self.getvalue()
230 if self.session:
231 print >> self.session, self.getvalue()
Johnny Chen150c3cc2010-10-15 01:18:29 +0000232 self.close()
233
Johnny Chen690fcef2010-10-15 23:55:05 +0000234# From 2.7's subprocess.check_output() convenience function.
Johnny Chenac77f3b2011-03-23 20:28:59 +0000235# Return a tuple (stdoutdata, stderrdata).
Zachary Turner9ef307b2014-07-22 16:19:29 +0000236def system(commands, **kwargs):
Johnny Chen8eb14a92011-11-16 22:44:28 +0000237 r"""Run an os command with arguments and return its output as a byte string.
Johnny Chen690fcef2010-10-15 23:55:05 +0000238
239 If the exit code was non-zero it raises a CalledProcessError. The
240 CalledProcessError object will have the return code in the returncode
241 attribute and output in the output attribute.
242
243 The arguments are the same as for the Popen constructor. Example:
244
245 >>> check_output(["ls", "-l", "/dev/null"])
246 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
247
248 The stdout argument is not allowed as it is used internally.
249 To capture standard error in the result, use stderr=STDOUT.
250
251 >>> check_output(["/bin/sh", "-c",
252 ... "ls -l non_existent_file ; exit 0"],
253 ... stderr=STDOUT)
254 'ls: non_existent_file: No such file or directory\n'
255 """
256
257 # Assign the sender object to variable 'test' and remove it from kwargs.
258 test = kwargs.pop('sender', None)
259
Zachary Turner9ef307b2014-07-22 16:19:29 +0000260 separator = None
261 separator = " && " if os.name == "nt" else "; "
262 # [['make', 'clean', 'foo'], ['make', 'foo']] -> ['make clean foo', 'make foo']
263 commandList = [' '.join(x) for x in commands]
264 # ['make clean foo', 'make foo'] -> 'make clean foo; make foo'
265 shellCommand = separator.join(commandList)
266
Johnny Chen690fcef2010-10-15 23:55:05 +0000267 if 'stdout' in kwargs:
268 raise ValueError('stdout argument not allowed, it will be overridden.')
Zachary Turner9ef307b2014-07-22 16:19:29 +0000269 if 'shell' in kwargs and kwargs['shell']==False:
270 raise ValueError('shell=False not allowed')
271 process = Popen(shellCommand, stdout=PIPE, stderr=PIPE, shell=True, **kwargs)
Johnny Chen0bd8c312011-11-16 22:41:53 +0000272 pid = process.pid
Johnny Chen690fcef2010-10-15 23:55:05 +0000273 output, error = process.communicate()
274 retcode = process.poll()
275
Ed Maste6e496332014-08-05 20:33:17 +0000276 # Enable trace on failure return while tracking down FreeBSD buildbot issues
277 trace = traceAlways
278 if not trace and retcode and sys.platform.startswith("freebsd"):
279 trace = True
280
281 with recording(test, trace) as sbuf:
Johnny Chen690fcef2010-10-15 23:55:05 +0000282 print >> sbuf
Zachary Turner9ef307b2014-07-22 16:19:29 +0000283 print >> sbuf, "os command:", shellCommand
Johnny Chen0bd8c312011-11-16 22:41:53 +0000284 print >> sbuf, "with pid:", pid
Johnny Chen690fcef2010-10-15 23:55:05 +0000285 print >> sbuf, "stdout:", output
286 print >> sbuf, "stderr:", error
287 print >> sbuf, "retcode:", retcode
288 print >> sbuf
289
290 if retcode:
291 cmd = kwargs.get("args")
292 if cmd is None:
Zachary Turner9ef307b2014-07-22 16:19:29 +0000293 cmd = shellCommand
Johnny Chen690fcef2010-10-15 23:55:05 +0000294 raise CalledProcessError(retcode, cmd)
Johnny Chenac77f3b2011-03-23 20:28:59 +0000295 return (output, error)
Johnny Chen690fcef2010-10-15 23:55:05 +0000296
Johnny Chenab9c1dd2010-11-01 20:35:01 +0000297def getsource_if_available(obj):
298 """
299 Return the text of the source code for an object if available. Otherwise,
300 a print representation is returned.
301 """
302 import inspect
303 try:
304 return inspect.getsource(obj)
305 except:
306 return repr(obj)
307
Peter Collingbourne19f48d52011-06-20 19:06:20 +0000308def builder_module():
Ed Maste4d90f0f2013-07-25 13:24:34 +0000309 if sys.platform.startswith("freebsd"):
310 return __import__("builder_freebsd")
Peter Collingbourne19f48d52011-06-20 19:06:20 +0000311 return __import__("builder_" + sys.platform)
312
Johnny Chena74bb0a2011-08-01 18:46:13 +0000313#
314# Decorators for categorizing test cases.
315#
316
317from functools import wraps
318def python_api_test(func):
319 """Decorate the item as a Python API only test."""
320 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
321 raise Exception("@python_api_test can only be used to decorate a test method")
322 @wraps(func)
323 def wrapper(self, *args, **kwargs):
324 try:
325 if lldb.dont_do_python_api_test:
326 self.skipTest("python api tests")
327 except AttributeError:
328 pass
329 return func(self, *args, **kwargs)
330
331 # Mark this function as such to separate them from lldb command line tests.
332 wrapper.__python_api_test__ = True
333 return wrapper
334
Johnny Chena74bb0a2011-08-01 18:46:13 +0000335def benchmarks_test(func):
336 """Decorate the item as a benchmarks test."""
337 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
338 raise Exception("@benchmarks_test can only be used to decorate a test method")
339 @wraps(func)
340 def wrapper(self, *args, **kwargs):
341 try:
342 if not lldb.just_do_benchmarks_test:
343 self.skipTest("benchmarks tests")
344 except AttributeError:
345 pass
346 return func(self, *args, **kwargs)
347
348 # Mark this function as such to separate them from the regular tests.
349 wrapper.__benchmarks_test__ = True
350 return wrapper
351
Johnny Chenf1548d42012-04-06 00:56:05 +0000352def dsym_test(func):
353 """Decorate the item as a dsym test."""
354 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
355 raise Exception("@dsym_test can only be used to decorate a test method")
356 @wraps(func)
357 def wrapper(self, *args, **kwargs):
358 try:
359 if lldb.dont_do_dsym_test:
360 self.skipTest("dsym tests")
361 except AttributeError:
362 pass
363 return func(self, *args, **kwargs)
364
365 # Mark this function as such to separate them from the regular tests.
366 wrapper.__dsym_test__ = True
367 return wrapper
368
369def dwarf_test(func):
370 """Decorate the item as a dwarf test."""
371 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
372 raise Exception("@dwarf_test can only be used to decorate a test method")
373 @wraps(func)
374 def wrapper(self, *args, **kwargs):
375 try:
376 if lldb.dont_do_dwarf_test:
377 self.skipTest("dwarf tests")
378 except AttributeError:
379 pass
380 return func(self, *args, **kwargs)
381
382 # Mark this function as such to separate them from the regular tests.
383 wrapper.__dwarf_test__ = True
384 return wrapper
385
Todd Fialaa41d48c2014-04-28 04:49:40 +0000386def debugserver_test(func):
387 """Decorate the item as a debugserver test."""
388 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
389 raise Exception("@debugserver_test can only be used to decorate a test method")
390 @wraps(func)
391 def wrapper(self, *args, **kwargs):
392 try:
393 if lldb.dont_do_debugserver_test:
394 self.skipTest("debugserver tests")
395 except AttributeError:
396 pass
397 return func(self, *args, **kwargs)
398
399 # Mark this function as such to separate them from the regular tests.
400 wrapper.__debugserver_test__ = True
401 return wrapper
402
403def llgs_test(func):
404 """Decorate the item as a lldb-gdbserver test."""
405 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
406 raise Exception("@llgs_test can only be used to decorate a test method")
407 @wraps(func)
408 def wrapper(self, *args, **kwargs):
409 try:
410 if lldb.dont_do_llgs_test:
411 self.skipTest("llgs tests")
412 except AttributeError:
413 pass
414 return func(self, *args, **kwargs)
415
416 # Mark this function as such to separate them from the regular tests.
417 wrapper.__llgs_test__ = True
418 return wrapper
419
Daniel Maleae0f8f572013-08-26 23:57:52 +0000420def not_remote_testsuite_ready(func):
421 """Decorate the item as a test which is not ready yet for remote testsuite."""
422 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
423 raise Exception("@not_remote_testsuite_ready can only be used to decorate a test method")
424 @wraps(func)
425 def wrapper(self, *args, **kwargs):
426 try:
427 if lldb.lldbtest_remote_sandbox:
428 self.skipTest("not ready for remote testsuite")
429 except AttributeError:
430 pass
431 return func(self, *args, **kwargs)
432
433 # Mark this function as such to separate them from the regular tests.
434 wrapper.__not_ready_for_remote_testsuite_test__ = True
435 return wrapper
436
Ed Maste433790a2014-04-23 12:55:41 +0000437def expectedFailure(expected_fn, bugnumber=None):
438 def expectedFailure_impl(func):
439 @wraps(func)
440 def wrapper(*args, **kwargs):
Enrico Granata43f62132013-02-23 01:28:30 +0000441 from unittest2 import case
442 self = args[0]
Enrico Granata43f62132013-02-23 01:28:30 +0000443 try:
Ed Maste433790a2014-04-23 12:55:41 +0000444 func(*args, **kwargs)
Enrico Granata43f62132013-02-23 01:28:30 +0000445 except Exception:
Ed Maste433790a2014-04-23 12:55:41 +0000446 if expected_fn(self):
447 raise case._ExpectedFailure(sys.exc_info(), bugnumber)
Enrico Granata43f62132013-02-23 01:28:30 +0000448 else:
449 raise
Ed Maste433790a2014-04-23 12:55:41 +0000450 if expected_fn(self):
451 raise case._UnexpectedSuccess(sys.exc_info(), bugnumber)
452 return wrapper
453 if callable(bugnumber):
454 return expectedFailure_impl(bugnumber)
455 else:
456 return expectedFailure_impl
457
458def expectedFailureCompiler(compiler, compiler_version=None, bugnumber=None):
459 if compiler_version is None:
460 compiler_version=['=', None]
461 def fn(self):
462 return compiler in self.getCompiler() and self.expectedCompilerVersion(compiler_version)
463 return expectedFailure(fn, bugnumber)
Daniel Malea249287a2013-02-19 16:08:57 +0000464
Enrico Granata2b3a0c42013-02-23 01:35:21 +0000465def expectedFailureClang(bugnumber=None):
Ed Maste433790a2014-04-23 12:55:41 +0000466 return expectedFailureCompiler('clang', None, bugnumber)
467
468def expectedFailureGcc(bugnumber=None, compiler_version=None):
469 return expectedFailureCompiler('gcc', compiler_version, bugnumber)
Daniel Malea249287a2013-02-19 16:08:57 +0000470
Matt Kopec0de53f02013-03-15 19:10:12 +0000471def expectedFailureIcc(bugnumber=None):
Ed Maste433790a2014-04-23 12:55:41 +0000472 return expectedFailureCompiler('icc', None, bugnumber)
Matt Kopec0de53f02013-03-15 19:10:12 +0000473
Ed Maste433790a2014-04-23 12:55:41 +0000474def expectedFailureArch(arch, bugnumber=None):
475 def fn(self):
476 return arch in self.getArchitecture()
477 return expectedFailure(fn, bugnumber)
Daniel Malea249287a2013-02-19 16:08:57 +0000478
Enrico Granatae6cedc12013-02-23 01:05:23 +0000479def expectedFailurei386(bugnumber=None):
Ed Maste433790a2014-04-23 12:55:41 +0000480 return expectedFailureArch('i386', bugnumber)
Johnny Chena33843f2011-12-22 21:14:31 +0000481
Matt Kopecee969f92013-09-26 23:30:59 +0000482def expectedFailurex86_64(bugnumber=None):
Ed Maste433790a2014-04-23 12:55:41 +0000483 return expectedFailureArch('x86_64', bugnumber)
484
485def expectedFailureOS(os, bugnumber=None, compilers=None):
486 def fn(self):
487 return os in sys.platform and self.expectedCompiler(compilers)
488 return expectedFailure(fn, bugnumber)
489
490def expectedFailureDarwin(bugnumber=None, compilers=None):
491 return expectedFailureOS('darwin', bugnumber, compilers)
Matt Kopecee969f92013-09-26 23:30:59 +0000492
Ed Maste24a7f7d2013-07-24 19:47:08 +0000493def expectedFailureFreeBSD(bugnumber=None, compilers=None):
Ed Maste433790a2014-04-23 12:55:41 +0000494 return expectedFailureOS('freebsd', bugnumber, compilers)
Ed Maste24a7f7d2013-07-24 19:47:08 +0000495
Ashok Thirumurthic97a6082013-05-17 20:15:07 +0000496def expectedFailureLinux(bugnumber=None, compilers=None):
Ed Maste433790a2014-04-23 12:55:41 +0000497 return expectedFailureOS('linux', bugnumber, compilers)
Matt Kopece9ea0da2013-05-07 19:29:28 +0000498
Greg Clayton12514562013-12-05 22:22:32 +0000499def skipIfRemote(func):
500 """Decorate the item to skip tests if testing remotely."""
501 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
502 raise Exception("@skipIfRemote can only be used to decorate a test method")
503 @wraps(func)
504 def wrapper(*args, **kwargs):
505 from unittest2 import case
506 if lldb.remote_platform:
507 self = args[0]
508 self.skipTest("skip on remote platform")
509 else:
510 func(*args, **kwargs)
511 return wrapper
512
513def skipIfRemoteDueToDeadlock(func):
514 """Decorate the item to skip tests if testing remotely due to the test deadlocking."""
515 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
516 raise Exception("@skipIfRemote can only be used to decorate a test method")
517 @wraps(func)
518 def wrapper(*args, **kwargs):
519 from unittest2 import case
520 if lldb.remote_platform:
521 self = args[0]
522 self.skipTest("skip on remote platform (deadlocks)")
523 else:
524 func(*args, **kwargs)
525 return wrapper
526
Ed Maste09617a52013-06-25 19:11:36 +0000527def skipIfFreeBSD(func):
528 """Decorate the item to skip tests that should be skipped on FreeBSD."""
529 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
530 raise Exception("@skipIfFreeBSD can only be used to decorate a test method")
531 @wraps(func)
532 def wrapper(*args, **kwargs):
533 from unittest2 import case
534 self = args[0]
535 platform = sys.platform
536 if "freebsd" in platform:
537 self.skipTest("skip on FreeBSD")
538 else:
539 func(*args, **kwargs)
540 return wrapper
541
Daniel Maleae8bdd1f2013-05-15 18:48:32 +0000542def skipIfLinux(func):
Daniel Malea93aec0f2012-11-23 21:59:29 +0000543 """Decorate the item to skip tests that should be skipped on Linux."""
544 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
Daniel Maleae8bdd1f2013-05-15 18:48:32 +0000545 raise Exception("@skipIfLinux can only be used to decorate a test method")
Daniel Malea93aec0f2012-11-23 21:59:29 +0000546 @wraps(func)
547 def wrapper(*args, **kwargs):
548 from unittest2 import case
549 self = args[0]
550 platform = sys.platform
551 if "linux" in platform:
552 self.skipTest("skip on linux")
553 else:
Jim Ingham9732e082012-11-27 01:21:28 +0000554 func(*args, **kwargs)
Daniel Malea93aec0f2012-11-23 21:59:29 +0000555 return wrapper
556
Zachary Turnerc7826522014-08-13 17:44:53 +0000557def skipIfWindows(func):
558 """Decorate the item to skip tests that should be skipped on Windows."""
559 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
560 raise Exception("@skipIfWindows can only be used to decorate a test method")
561 @wraps(func)
562 def wrapper(*args, **kwargs):
563 from unittest2 import case
564 self = args[0]
565 platform = sys.platform
566 if "win32" in platform:
567 self.skipTest("skip on Windows")
568 else:
569 func(*args, **kwargs)
570 return wrapper
571
Daniel Maleab3d41a22013-07-09 00:08:01 +0000572def skipIfDarwin(func):
573 """Decorate the item to skip tests that should be skipped on Darwin."""
574 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
Ed Mastea7f13f02013-07-09 00:24:52 +0000575 raise Exception("@skipIfDarwin can only be used to decorate a test method")
Daniel Maleab3d41a22013-07-09 00:08:01 +0000576 @wraps(func)
577 def wrapper(*args, **kwargs):
578 from unittest2 import case
579 self = args[0]
580 platform = sys.platform
581 if "darwin" in platform:
582 self.skipTest("skip on darwin")
583 else:
584 func(*args, **kwargs)
585 return wrapper
586
587
Daniel Malea48359902013-05-14 20:48:54 +0000588def skipIfLinuxClang(func):
589 """Decorate the item to skip tests that should be skipped if building on
590 Linux with clang.
591 """
592 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
593 raise Exception("@skipIfLinuxClang can only be used to decorate a test method")
594 @wraps(func)
595 def wrapper(*args, **kwargs):
596 from unittest2 import case
597 self = args[0]
598 compiler = self.getCompiler()
599 platform = sys.platform
600 if "clang" in compiler and "linux" in platform:
601 self.skipTest("skipping because Clang is used on Linux")
602 else:
603 func(*args, **kwargs)
604 return wrapper
605
Daniel Maleabe230792013-01-24 23:52:09 +0000606def skipIfGcc(func):
607 """Decorate the item to skip tests that should be skipped if building with gcc ."""
608 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
Daniel Malea0aea0162013-02-27 17:29:46 +0000609 raise Exception("@skipIfGcc can only be used to decorate a test method")
Daniel Maleabe230792013-01-24 23:52:09 +0000610 @wraps(func)
611 def wrapper(*args, **kwargs):
612 from unittest2 import case
613 self = args[0]
614 compiler = self.getCompiler()
615 if "gcc" in compiler:
616 self.skipTest("skipping because gcc is the test compiler")
617 else:
618 func(*args, **kwargs)
619 return wrapper
620
Matt Kopec0de53f02013-03-15 19:10:12 +0000621def skipIfIcc(func):
622 """Decorate the item to skip tests that should be skipped if building with icc ."""
623 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
624 raise Exception("@skipIfIcc can only be used to decorate a test method")
625 @wraps(func)
626 def wrapper(*args, **kwargs):
627 from unittest2 import case
628 self = args[0]
629 compiler = self.getCompiler()
630 if "icc" in compiler:
631 self.skipTest("skipping because icc is the test compiler")
632 else:
633 func(*args, **kwargs)
634 return wrapper
635
Daniel Malea55faa402013-05-02 21:44:31 +0000636def skipIfi386(func):
637 """Decorate the item to skip tests that should be skipped if building 32-bit."""
638 if isinstance(func, type) and issubclass(func, unittest2.TestCase):
639 raise Exception("@skipIfi386 can only be used to decorate a test method")
640 @wraps(func)
641 def wrapper(*args, **kwargs):
642 from unittest2 import case
643 self = args[0]
644 if "i386" == self.getArchitecture():
645 self.skipTest("skipping because i386 is not a supported architecture")
646 else:
647 func(*args, **kwargs)
648 return wrapper
649
650
Johnny Chena74bb0a2011-08-01 18:46:13 +0000651class Base(unittest2.TestCase):
Johnny Chen8334dad2010-10-22 23:15:46 +0000652 """
Johnny Chena74bb0a2011-08-01 18:46:13 +0000653 Abstract base for performing lldb (see TestBase) or other generic tests (see
654 BenchBase for one example). lldbtest.Base works with the test driver to
655 accomplish things.
656
Johnny Chen8334dad2010-10-22 23:15:46 +0000657 """
Enrico Granata5020f952012-10-24 21:42:49 +0000658
Enrico Granata19186272012-10-24 21:44:48 +0000659 # The concrete subclass should override this attribute.
660 mydir = None
Johnny Chenbf6ffa32010-07-03 03:41:59 +0000661
Johnny Chen1a9f4dd2010-09-16 01:53:04 +0000662 # Keep track of the old current working directory.
663 oldcwd = None
Greg Clayton4570d3e2013-12-10 23:19:29 +0000664
665 @staticmethod
666 def compute_mydir(test_file):
667 '''Subclasses should call this function to correctly calculate the required "mydir" attribute as follows:
668
669 mydir = TestBase.compute_mydir(__file__)'''
670 test_dir = os.path.dirname(test_file)
671 return test_dir[len(os.environ["LLDB_TEST"])+1:]
672
Johnny Chenfb4264c2011-08-01 19:50:58 +0000673 def TraceOn(self):
674 """Returns True if we are in trace mode (tracing detailed test execution)."""
675 return traceAlways
Greg Clayton4570d3e2013-12-10 23:19:29 +0000676
Johnny Chen1a9f4dd2010-09-16 01:53:04 +0000677 @classmethod
678 def setUpClass(cls):
Johnny Chenda884342010-10-01 22:59:49 +0000679 """
680 Python unittest framework class setup fixture.
681 Do current directory manipulation.
682 """
683
Johnny Chenf02ec122010-07-03 20:41:42 +0000684 # Fail fast if 'mydir' attribute is not overridden.
Johnny Chen1a9f4dd2010-09-16 01:53:04 +0000685 if not cls.mydir or len(cls.mydir) == 0:
Johnny Chenf02ec122010-07-03 20:41:42 +0000686 raise Exception("Subclasses must override the 'mydir' attribute.")
Enrico Granata7e137e32012-10-24 18:14:21 +0000687
Johnny Chenbf6ffa32010-07-03 03:41:59 +0000688 # Save old working directory.
Johnny Chen1a9f4dd2010-09-16 01:53:04 +0000689 cls.oldcwd = os.getcwd()
Johnny Chenbf6ffa32010-07-03 03:41:59 +0000690
691 # Change current working directory if ${LLDB_TEST} is defined.
692 # See also dotest.py which sets up ${LLDB_TEST}.
693 if ("LLDB_TEST" in os.environ):
Johnny Chen1a9f4dd2010-09-16 01:53:04 +0000694 if traceAlways:
Johnny Chen703dbd02010-09-30 17:06:24 +0000695 print >> sys.stderr, "Change dir to:", os.path.join(os.environ["LLDB_TEST"], cls.mydir)
Johnny Chen1a9f4dd2010-09-16 01:53:04 +0000696 os.chdir(os.path.join(os.environ["LLDB_TEST"], cls.mydir))
697
698 @classmethod
699 def tearDownClass(cls):
Johnny Chenda884342010-10-01 22:59:49 +0000700 """
701 Python unittest framework class teardown fixture.
702 Do class-wide cleanup.
703 """
Johnny Chen1a9f4dd2010-09-16 01:53:04 +0000704
Johnny Chen0fddfb22011-11-17 19:57:27 +0000705 if doCleanup and not lldb.skip_build_and_cleanup:
Johnny Chen707b3c92010-10-11 22:25:46 +0000706 # First, let's do the platform-specific cleanup.
Peter Collingbourne19f48d52011-06-20 19:06:20 +0000707 module = builder_module()
Johnny Chen707b3c92010-10-11 22:25:46 +0000708 if not module.cleanup():
709 raise Exception("Don't know how to do cleanup")
Johnny Chen1a9f4dd2010-09-16 01:53:04 +0000710
Johnny Chen707b3c92010-10-11 22:25:46 +0000711 # Subclass might have specific cleanup function defined.
712 if getattr(cls, "classCleanup", None):
713 if traceAlways:
714 print >> sys.stderr, "Call class-specific cleanup function for class:", cls
715 try:
716 cls.classCleanup()
717 except:
718 exc_type, exc_value, exc_tb = sys.exc_info()
719 traceback.print_exception(exc_type, exc_value, exc_tb)
Johnny Chen1a9f4dd2010-09-16 01:53:04 +0000720
721 # Restore old working directory.
722 if traceAlways:
Johnny Chen703dbd02010-09-30 17:06:24 +0000723 print >> sys.stderr, "Restore dir to:", cls.oldcwd
Johnny Chen1a9f4dd2010-09-16 01:53:04 +0000724 os.chdir(cls.oldcwd)
725
Johnny Chena74bb0a2011-08-01 18:46:13 +0000726 @classmethod
727 def skipLongRunningTest(cls):
728 """
729 By default, we skip long running test case.
730 This can be overridden by passing '-l' to the test driver (dotest.py).
731 """
732 if "LLDB_SKIP_LONG_RUNNING_TEST" in os.environ and "NO" == os.environ["LLDB_SKIP_LONG_RUNNING_TEST"]:
733 return False
734 else:
735 return True
Johnny Chened492022011-06-21 00:53:00 +0000736
Johnny Chen1a9f4dd2010-09-16 01:53:04 +0000737 def setUp(self):
Johnny Chenfb4264c2011-08-01 19:50:58 +0000738 """Fixture for unittest test case setup.
739
740 It works with the test driver to conditionally skip tests and does other
741 initializations."""
Johnny Chen1a9f4dd2010-09-16 01:53:04 +0000742 #import traceback
743 #traceback.print_stack()
Johnny Chenbf6ffa32010-07-03 03:41:59 +0000744
Daniel Malea9115f072013-08-06 15:02:32 +0000745 if "LIBCXX_PATH" in os.environ:
746 self.libcxxPath = os.environ["LIBCXX_PATH"]
747 else:
748 self.libcxxPath = None
749
Johnny Chenaaa82ff2011-08-02 22:54:37 +0000750 if "LLDB_EXEC" in os.environ:
751 self.lldbExec = os.environ["LLDB_EXEC"]
Johnny Chend890bfc2011-08-26 00:00:01 +0000752 else:
753 self.lldbExec = None
754 if "LLDB_HERE" in os.environ:
755 self.lldbHere = os.environ["LLDB_HERE"]
756 else:
757 self.lldbHere = None
Johnny Chenebe51722011-10-07 19:21:09 +0000758 # If we spawn an lldb process for test (via pexpect), do not load the
759 # init file unless told otherwise.
760 if "NO_LLDBINIT" in os.environ and "NO" == os.environ["NO_LLDBINIT"]:
761 self.lldbOption = ""
762 else:
763 self.lldbOption = "--no-lldbinit"
Johnny Chenaaa82ff2011-08-02 22:54:37 +0000764
Johnny Chen985e7402011-08-01 21:13:26 +0000765 # Assign the test method name to self.testMethodName.
766 #
767 # For an example of the use of this attribute, look at test/types dir.
768 # There are a bunch of test cases under test/types and we don't want the
769 # module cacheing subsystem to be confused with executable name "a.out"
770 # used for all the test cases.
771 self.testMethodName = self._testMethodName
772
Johnny Chenf3e22ac2010-12-10 18:52:10 +0000773 # Python API only test is decorated with @python_api_test,
774 # which also sets the "__python_api_test__" attribute of the
775 # function object to True.
Johnny Chen4533dad2011-05-31 23:21:42 +0000776 try:
777 if lldb.just_do_python_api_test:
778 testMethod = getattr(self, self._testMethodName)
779 if getattr(testMethod, "__python_api_test__", False):
780 pass
781 else:
Johnny Chen5ccbccf2011-07-30 01:39:58 +0000782 self.skipTest("non python api test")
783 except AttributeError:
784 pass
785
786 # Benchmarks test is decorated with @benchmarks_test,
787 # which also sets the "__benchmarks_test__" attribute of the
788 # function object to True.
789 try:
790 if lldb.just_do_benchmarks_test:
791 testMethod = getattr(self, self._testMethodName)
792 if getattr(testMethod, "__benchmarks_test__", False):
793 pass
794 else:
795 self.skipTest("non benchmarks test")
Johnny Chen4533dad2011-05-31 23:21:42 +0000796 except AttributeError:
797 pass
Johnny Chenf3e22ac2010-12-10 18:52:10 +0000798
Johnny Chen985e7402011-08-01 21:13:26 +0000799 # This is for the case of directly spawning 'lldb'/'gdb' and interacting
800 # with it using pexpect.
801 self.child = None
802 self.child_prompt = "(lldb) "
803 # If the child is interacting with the embedded script interpreter,
804 # there are two exits required during tear down, first to quit the
805 # embedded script interpreter and second to quit the lldb command
806 # interpreter.
807 self.child_in_script_interpreter = False
808
Johnny Chenfb4264c2011-08-01 19:50:58 +0000809 # These are for customized teardown cleanup.
810 self.dict = None
811 self.doTearDownCleanup = False
812 # And in rare cases where there are multiple teardown cleanups.
813 self.dicts = []
814 self.doTearDownCleanups = False
815
Daniel Malea2dd69bb2013-02-15 21:21:52 +0000816 # List of spawned subproces.Popen objects
817 self.subprocesses = []
818
Daniel Malea69207462013-06-05 21:07:02 +0000819 # List of forked process PIDs
820 self.forkedProcessPids = []
821
Johnny Chenfb4264c2011-08-01 19:50:58 +0000822 # Create a string buffer to record the session info, to be dumped into a
823 # test case specific file if test failure is encountered.
824 self.session = StringIO.StringIO()
825
826 # Optimistically set __errored__, __failed__, __expected__ to False
827 # initially. If the test errored/failed, the session info
828 # (self.session) is then dumped into a session specific file for
829 # diagnosis.
830 self.__errored__ = False
831 self.__failed__ = False
832 self.__expected__ = False
833 # We are also interested in unexpected success.
834 self.__unexpected__ = False
Johnny Chenf79b0762011-08-16 00:48:58 +0000835 # And skipped tests.
836 self.__skipped__ = False
Johnny Chenfb4264c2011-08-01 19:50:58 +0000837
838 # See addTearDownHook(self, hook) which allows the client to add a hook
839 # function to be run during tearDown() time.
840 self.hooks = []
841
842 # See HideStdout(self).
843 self.sys_stdout_hidden = False
844
Daniel Malea179ff292012-11-26 21:21:11 +0000845 # set environment variable names for finding shared libraries
846 if sys.platform.startswith("darwin"):
847 self.dylibPath = 'DYLD_LIBRARY_PATH'
848 elif sys.platform.startswith("linux") or sys.platform.startswith("freebsd"):
849 self.dylibPath = 'LD_LIBRARY_PATH'
850
Johnny Chen2a808582011-10-19 16:48:07 +0000851 def runHooks(self, child=None, child_prompt=None, use_cmd_api=False):
Johnny Chena737ba52011-10-19 01:06:21 +0000852 """Perform the run hooks to bring lldb debugger to the desired state.
853
Johnny Chen2a808582011-10-19 16:48:07 +0000854 By default, expect a pexpect spawned child and child prompt to be
855 supplied (use_cmd_api=False). If use_cmd_api is true, ignore the child
856 and child prompt and use self.runCmd() to run the hooks one by one.
857
Johnny Chena737ba52011-10-19 01:06:21 +0000858 Note that child is a process spawned by pexpect.spawn(). If not, your
859 test case is mostly likely going to fail.
860
861 See also dotest.py where lldb.runHooks are processed/populated.
862 """
863 if not lldb.runHooks:
864 self.skipTest("No runhooks specified for lldb, skip the test")
Johnny Chen2a808582011-10-19 16:48:07 +0000865 if use_cmd_api:
866 for hook in lldb.runhooks:
867 self.runCmd(hook)
868 else:
869 if not child or not child_prompt:
870 self.fail("Both child and child_prompt need to be defined.")
871 for hook in lldb.runHooks:
872 child.sendline(hook)
873 child.expect_exact(child_prompt)
Johnny Chena737ba52011-10-19 01:06:21 +0000874
Daniel Malea249287a2013-02-19 16:08:57 +0000875 def setAsync(self, value):
876 """ Sets async mode to True/False and ensures it is reset after the testcase completes."""
877 old_async = self.dbg.GetAsync()
878 self.dbg.SetAsync(value)
879 self.addTearDownHook(lambda: self.dbg.SetAsync(old_async))
880
Daniel Malea2dd69bb2013-02-15 21:21:52 +0000881 def cleanupSubprocesses(self):
882 # Ensure any subprocesses are cleaned up
883 for p in self.subprocesses:
884 if p.poll() == None:
885 p.terminate()
886 del p
887 del self.subprocesses[:]
Daniel Malea69207462013-06-05 21:07:02 +0000888 # Ensure any forked processes are cleaned up
889 for pid in self.forkedProcessPids:
890 if os.path.exists("/proc/" + str(pid)):
891 os.kill(pid, signal.SIGTERM)
Daniel Malea2dd69bb2013-02-15 21:21:52 +0000892
893 def spawnSubprocess(self, executable, args=[]):
894 """ Creates a subprocess.Popen object with the specified executable and arguments,
895 saves it in self.subprocesses, and returns the object.
896 NOTE: if using this function, ensure you also call:
897
898 self.addTearDownHook(self.cleanupSubprocesses)
899
900 otherwise the test suite will leak processes.
901 """
902
903 # Don't display the stdout if not in TraceOn() mode.
904 proc = Popen([executable] + args,
905 stdout = open(os.devnull) if not self.TraceOn() else None,
906 stdin = PIPE)
907 self.subprocesses.append(proc)
908 return proc
909
Daniel Malea69207462013-06-05 21:07:02 +0000910 def forkSubprocess(self, executable, args=[]):
911 """ Fork a subprocess with its own group ID.
912 NOTE: if using this function, ensure you also call:
913
914 self.addTearDownHook(self.cleanupSubprocesses)
915
916 otherwise the test suite will leak processes.
917 """
918 child_pid = os.fork()
919 if child_pid == 0:
920 # If more I/O support is required, this can be beefed up.
921 fd = os.open(os.devnull, os.O_RDWR)
Daniel Malea69207462013-06-05 21:07:02 +0000922 os.dup2(fd, 1)
923 os.dup2(fd, 2)
924 # This call causes the child to have its of group ID
925 os.setpgid(0,0)
926 os.execvp(executable, [executable] + args)
927 # Give the child time to get through the execvp() call
928 time.sleep(0.1)
929 self.forkedProcessPids.append(child_pid)
930 return child_pid
931
Johnny Chenfb4264c2011-08-01 19:50:58 +0000932 def HideStdout(self):
933 """Hide output to stdout from the user.
934
935 During test execution, there might be cases where we don't want to show the
936 standard output to the user. For example,
937
938 self.runCmd(r'''sc print "\n\n\tHello!\n"''')
939
940 tests whether command abbreviation for 'script' works or not. There is no
941 need to show the 'Hello' output to the user as long as the 'script' command
942 succeeds and we are not in TraceOn() mode (see the '-t' option).
943
944 In this case, the test method calls self.HideStdout(self) to redirect the
945 sys.stdout to a null device, and restores the sys.stdout upon teardown.
946
947 Note that you should only call this method at most once during a test case
948 execution. Any subsequent call has no effect at all."""
949 if self.sys_stdout_hidden:
950 return
951
952 self.sys_stdout_hidden = True
953 old_stdout = sys.stdout
954 sys.stdout = open(os.devnull, 'w')
955 def restore_stdout():
956 sys.stdout = old_stdout
957 self.addTearDownHook(restore_stdout)
958
959 # =======================================================================
960 # Methods for customized teardown cleanups as well as execution of hooks.
961 # =======================================================================
962
963 def setTearDownCleanup(self, dictionary=None):
964 """Register a cleanup action at tearDown() time with a dictinary"""
965 self.dict = dictionary
966 self.doTearDownCleanup = True
967
968 def addTearDownCleanup(self, dictionary):
969 """Add a cleanup action at tearDown() time with a dictinary"""
970 self.dicts.append(dictionary)
971 self.doTearDownCleanups = True
972
973 def addTearDownHook(self, hook):
974 """
975 Add a function to be run during tearDown() time.
976
977 Hooks are executed in a first come first serve manner.
978 """
979 if callable(hook):
980 with recording(self, traceAlways) as sbuf:
981 print >> sbuf, "Adding tearDown hook:", getsource_if_available(hook)
982 self.hooks.append(hook)
983
984 def tearDown(self):
985 """Fixture for unittest test case teardown."""
986 #import traceback
987 #traceback.print_stack()
988
Johnny Chen985e7402011-08-01 21:13:26 +0000989 # This is for the case of directly spawning 'lldb' and interacting with it
990 # using pexpect.
Johnny Chen985e7402011-08-01 21:13:26 +0000991 if self.child and self.child.isalive():
Zachary Turner9ef307b2014-07-22 16:19:29 +0000992 import pexpect
Johnny Chen985e7402011-08-01 21:13:26 +0000993 with recording(self, traceAlways) as sbuf:
994 print >> sbuf, "tearing down the child process...."
Johnny Chen985e7402011-08-01 21:13:26 +0000995 try:
Daniel Maleac9a0ec32013-02-22 00:41:26 +0000996 if self.child_in_script_interpreter:
997 self.child.sendline('quit()')
998 self.child.expect_exact(self.child_prompt)
999 self.child.sendline('settings set interpreter.prompt-on-quit false')
1000 self.child.sendline('quit')
Johnny Chen985e7402011-08-01 21:13:26 +00001001 self.child.expect(pexpect.EOF)
Daniel Maleac9a0ec32013-02-22 00:41:26 +00001002 except ValueError, ExceptionPexpect:
1003 # child is already terminated
Johnny Chen985e7402011-08-01 21:13:26 +00001004 pass
Daniel Maleac9a0ec32013-02-22 00:41:26 +00001005
Johnny Chenac257132012-02-27 23:07:40 +00001006 # Give it one final blow to make sure the child is terminated.
1007 self.child.close()
Johnny Chen985e7402011-08-01 21:13:26 +00001008
Johnny Chenfb4264c2011-08-01 19:50:58 +00001009 # Check and run any hook functions.
1010 for hook in reversed(self.hooks):
1011 with recording(self, traceAlways) as sbuf:
1012 print >> sbuf, "Executing tearDown hook:", getsource_if_available(hook)
1013 hook()
1014
1015 del self.hooks
1016
1017 # Perform registered teardown cleanup.
1018 if doCleanup and self.doTearDownCleanup:
Johnny Chen0fddfb22011-11-17 19:57:27 +00001019 self.cleanup(dictionary=self.dict)
Johnny Chenfb4264c2011-08-01 19:50:58 +00001020
1021 # In rare cases where there are multiple teardown cleanups added.
1022 if doCleanup and self.doTearDownCleanups:
Johnny Chenfb4264c2011-08-01 19:50:58 +00001023 if self.dicts:
1024 for dict in reversed(self.dicts):
Johnny Chen0fddfb22011-11-17 19:57:27 +00001025 self.cleanup(dictionary=dict)
Johnny Chenfb4264c2011-08-01 19:50:58 +00001026
1027 # Decide whether to dump the session info.
1028 self.dumpSessionInfo()
1029
1030 # =========================================================
1031 # Various callbacks to allow introspection of test progress
1032 # =========================================================
1033
1034 def markError(self):
1035 """Callback invoked when an error (unexpected exception) errored."""
1036 self.__errored__ = True
1037 with recording(self, False) as sbuf:
1038 # False because there's no need to write "ERROR" to the stderr twice.
1039 # Once by the Python unittest framework, and a second time by us.
1040 print >> sbuf, "ERROR"
1041
1042 def markFailure(self):
1043 """Callback invoked when a failure (test assertion failure) occurred."""
1044 self.__failed__ = True
1045 with recording(self, False) as sbuf:
1046 # False because there's no need to write "FAIL" to the stderr twice.
1047 # Once by the Python unittest framework, and a second time by us.
1048 print >> sbuf, "FAIL"
1049
Enrico Granatae6cedc12013-02-23 01:05:23 +00001050 def markExpectedFailure(self,err,bugnumber):
Johnny Chenfb4264c2011-08-01 19:50:58 +00001051 """Callback invoked when an expected failure/error occurred."""
1052 self.__expected__ = True
1053 with recording(self, False) as sbuf:
1054 # False because there's no need to write "expected failure" to the
1055 # stderr twice.
1056 # Once by the Python unittest framework, and a second time by us.
Enrico Granatae6cedc12013-02-23 01:05:23 +00001057 if bugnumber == None:
1058 print >> sbuf, "expected failure"
1059 else:
1060 print >> sbuf, "expected failure (problem id:" + str(bugnumber) + ")"
Johnny Chenfb4264c2011-08-01 19:50:58 +00001061
Johnny Chenc5cc6252011-08-15 23:09:08 +00001062 def markSkippedTest(self):
1063 """Callback invoked when a test is skipped."""
1064 self.__skipped__ = True
1065 with recording(self, False) as sbuf:
1066 # False because there's no need to write "skipped test" to the
1067 # stderr twice.
1068 # Once by the Python unittest framework, and a second time by us.
1069 print >> sbuf, "skipped test"
1070
Enrico Granatae6cedc12013-02-23 01:05:23 +00001071 def markUnexpectedSuccess(self, bugnumber):
Johnny Chenfb4264c2011-08-01 19:50:58 +00001072 """Callback invoked when an unexpected success occurred."""
1073 self.__unexpected__ = True
1074 with recording(self, False) as sbuf:
1075 # False because there's no need to write "unexpected success" to the
1076 # stderr twice.
1077 # Once by the Python unittest framework, and a second time by us.
Enrico Granatae6cedc12013-02-23 01:05:23 +00001078 if bugnumber == None:
1079 print >> sbuf, "unexpected success"
1080 else:
1081 print >> sbuf, "unexpected success (problem id:" + str(bugnumber) + ")"
Johnny Chenfb4264c2011-08-01 19:50:58 +00001082
1083 def dumpSessionInfo(self):
1084 """
1085 Dump the debugger interactions leading to a test error/failure. This
1086 allows for more convenient postmortem analysis.
1087
1088 See also LLDBTestResult (dotest.py) which is a singlton class derived
1089 from TextTestResult and overwrites addError, addFailure, and
1090 addExpectedFailure methods to allow us to to mark the test instance as
1091 such.
1092 """
1093
1094 # We are here because self.tearDown() detected that this test instance
1095 # either errored or failed. The lldb.test_result singleton contains
1096 # two lists (erros and failures) which get populated by the unittest
1097 # framework. Look over there for stack trace information.
1098 #
1099 # The lists contain 2-tuples of TestCase instances and strings holding
1100 # formatted tracebacks.
1101 #
1102 # See http://docs.python.org/library/unittest.html#unittest.TestResult.
1103 if self.__errored__:
1104 pairs = lldb.test_result.errors
1105 prefix = 'Error'
1106 elif self.__failed__:
1107 pairs = lldb.test_result.failures
1108 prefix = 'Failure'
1109 elif self.__expected__:
1110 pairs = lldb.test_result.expectedFailures
1111 prefix = 'ExpectedFailure'
Johnny Chenc5cc6252011-08-15 23:09:08 +00001112 elif self.__skipped__:
1113 prefix = 'SkippedTest'
Johnny Chenfb4264c2011-08-01 19:50:58 +00001114 elif self.__unexpected__:
1115 prefix = "UnexpectedSuccess"
1116 else:
1117 # Simply return, there's no session info to dump!
1118 return
1119
Johnny Chenc5cc6252011-08-15 23:09:08 +00001120 if not self.__unexpected__ and not self.__skipped__:
Johnny Chenfb4264c2011-08-01 19:50:58 +00001121 for test, traceback in pairs:
1122 if test is self:
1123 print >> self.session, traceback
1124
Johnny Chen8082a002011-08-11 00:16:28 +00001125 testMethod = getattr(self, self._testMethodName)
1126 if getattr(testMethod, "__benchmarks_test__", False):
1127 benchmarks = True
1128 else:
1129 benchmarks = False
1130
Johnny Chenfb4264c2011-08-01 19:50:58 +00001131 dname = os.path.join(os.environ["LLDB_TEST"],
1132 os.environ["LLDB_SESSION_DIRNAME"])
1133 if not os.path.isdir(dname):
1134 os.mkdir(dname)
Sean Callanan794baf62012-10-16 18:22:04 +00001135 fname = os.path.join(dname, "%s-%s-%s-%s.log" % (prefix, self.getArchitecture(), "_".join(self.getCompiler().split('/')), self.id()))
Johnny Chenfb4264c2011-08-01 19:50:58 +00001136 with open(fname, "w") as f:
1137 import datetime
1138 print >> f, "Session info generated @", datetime.datetime.now().ctime()
1139 print >> f, self.session.getvalue()
1140 print >> f, "To rerun this test, issue the following command from the 'test' directory:\n"
Johnny Chen8082a002011-08-11 00:16:28 +00001141 print >> f, "./dotest.py %s -v %s -f %s.%s" % (self.getRunOptions(),
1142 ('+b' if benchmarks else '-t'),
Johnny Chenfb4264c2011-08-01 19:50:58 +00001143 self.__class__.__name__,
1144 self._testMethodName)
1145
1146 # ====================================================
1147 # Config. methods supported through a plugin interface
1148 # (enables reading of the current test configuration)
1149 # ====================================================
1150
1151 def getArchitecture(self):
1152 """Returns the architecture in effect the test suite is running with."""
1153 module = builder_module()
1154 return module.getArchitecture()
1155
1156 def getCompiler(self):
1157 """Returns the compiler in effect the test suite is running with."""
1158 module = builder_module()
1159 return module.getCompiler()
1160
Daniel Malea0aea0162013-02-27 17:29:46 +00001161 def getCompilerVersion(self):
1162 """ Returns a string that represents the compiler version.
1163 Supports: llvm, clang.
1164 """
1165 from lldbutil import which
1166 version = 'unknown'
1167
1168 compiler = self.getCompiler()
Zachary Turner9ef307b2014-07-22 16:19:29 +00001169 version_output = system([[which(compiler), "-v"]])[1]
Daniel Malea0aea0162013-02-27 17:29:46 +00001170 for line in version_output.split(os.linesep):
Greg Clayton2a844b72013-03-06 02:34:51 +00001171 m = re.search('version ([0-9\.]+)', line)
Daniel Malea0aea0162013-02-27 17:29:46 +00001172 if m:
1173 version = m.group(1)
1174 return version
1175
Daniel Maleaadaaec92013-08-06 20:51:41 +00001176 def isIntelCompiler(self):
1177 """ Returns true if using an Intel (ICC) compiler, false otherwise. """
1178 return any([x in self.getCompiler() for x in ["icc", "icpc", "icl"]])
1179
Ashok Thirumurthi3b037282013-06-06 14:23:31 +00001180 def expectedCompilerVersion(self, compiler_version):
1181 """Returns True iff compiler_version[1] matches the current compiler version.
1182 Use compiler_version[0] to specify the operator used to determine if a match has occurred.
1183 Any operator other than the following defaults to an equality test:
1184 '>', '>=', "=>", '<', '<=', '=<', '!=', "!" or 'not'
1185 """
Ashok Thirumurthic97a6082013-05-17 20:15:07 +00001186 if (compiler_version == None):
1187 return True
1188 operator = str(compiler_version[0])
1189 version = compiler_version[1]
1190
1191 if (version == None):
1192 return True
1193 if (operator == '>'):
1194 return self.getCompilerVersion() > version
1195 if (operator == '>=' or operator == '=>'):
1196 return self.getCompilerVersion() >= version
1197 if (operator == '<'):
1198 return self.getCompilerVersion() < version
1199 if (operator == '<=' or operator == '=<'):
1200 return self.getCompilerVersion() <= version
1201 if (operator == '!=' or operator == '!' or operator == 'not'):
1202 return str(version) not in str(self.getCompilerVersion())
1203 return str(version) in str(self.getCompilerVersion())
1204
1205 def expectedCompiler(self, compilers):
Ashok Thirumurthi3b037282013-06-06 14:23:31 +00001206 """Returns True iff any element of compilers is a sub-string of the current compiler."""
Ashok Thirumurthic97a6082013-05-17 20:15:07 +00001207 if (compilers == None):
1208 return True
Ashok Thirumurthi3b037282013-06-06 14:23:31 +00001209
1210 for compiler in compilers:
1211 if compiler in self.getCompiler():
1212 return True
1213
1214 return False
Ashok Thirumurthic97a6082013-05-17 20:15:07 +00001215
Johnny Chenfb4264c2011-08-01 19:50:58 +00001216 def getRunOptions(self):
1217 """Command line option for -A and -C to run this test again, called from
1218 self.dumpSessionInfo()."""
1219 arch = self.getArchitecture()
1220 comp = self.getCompiler()
Johnny Chenb7bdd102011-08-24 19:48:51 +00001221 if arch:
1222 option_str = "-A " + arch
Johnny Chenfb4264c2011-08-01 19:50:58 +00001223 else:
Johnny Chenb7bdd102011-08-24 19:48:51 +00001224 option_str = ""
1225 if comp:
Johnny Chen531c0852012-03-16 20:44:00 +00001226 option_str += " -C " + comp
Johnny Chenb7bdd102011-08-24 19:48:51 +00001227 return option_str
Johnny Chenfb4264c2011-08-01 19:50:58 +00001228
1229 # ==================================================
1230 # Build methods supported through a plugin interface
1231 # ==================================================
1232
Ed Mastec97323e2014-04-01 18:47:58 +00001233 def getstdlibFlag(self):
1234 """ Returns the proper -stdlib flag, or empty if not required."""
1235 if sys.platform.startswith("darwin") or sys.platform.startswith("freebsd"):
1236 stdlibflag = "-stdlib=libc++"
1237 else:
1238 stdlibflag = ""
1239 return stdlibflag
1240
Matt Kopec7663b3a2013-09-25 17:44:00 +00001241 def getstdFlag(self):
1242 """ Returns the proper stdflag. """
Daniel Malea55faa402013-05-02 21:44:31 +00001243 if "gcc" in self.getCompiler() and "4.6" in self.getCompilerVersion():
Daniel Malea0b7c6112013-05-06 19:31:31 +00001244 stdflag = "-std=c++0x"
Daniel Malea55faa402013-05-02 21:44:31 +00001245 else:
1246 stdflag = "-std=c++11"
Matt Kopec7663b3a2013-09-25 17:44:00 +00001247 return stdflag
1248
1249 def buildDriver(self, sources, exe_name):
1250 """ Platform-specific way to build a program that links with LLDB (via the liblldb.so
1251 or LLDB.framework).
1252 """
1253
1254 stdflag = self.getstdFlag()
Ed Mastec97323e2014-04-01 18:47:58 +00001255 stdlibflag = self.getstdlibFlag()
Daniel Malea55faa402013-05-02 21:44:31 +00001256
1257 if sys.platform.startswith("darwin"):
1258 dsym = os.path.join(self.lib_dir, 'LLDB.framework', 'LLDB')
1259 d = {'CXX_SOURCES' : sources,
1260 'EXE' : exe_name,
Ed Mastec97323e2014-04-01 18:47:58 +00001261 'CFLAGS_EXTRAS' : "%s %s" % (stdflag, stdlibflag),
Daniel Malea55faa402013-05-02 21:44:31 +00001262 'FRAMEWORK_INCLUDES' : "-F%s" % self.lib_dir,
Stefanus Du Toit04004442013-07-30 19:19:49 +00001263 'LD_EXTRAS' : "%s -Wl,-rpath,%s" % (dsym, self.lib_dir),
Daniel Malea55faa402013-05-02 21:44:31 +00001264 }
Ed Maste372c24d2013-07-25 21:02:34 +00001265 elif sys.platform.startswith('freebsd') or sys.platform.startswith("linux") or os.environ.get('LLDB_BUILD_TYPE') == 'Makefile':
Daniel Malea55faa402013-05-02 21:44:31 +00001266 d = {'CXX_SOURCES' : sources,
1267 'EXE' : exe_name,
Ed Mastec97323e2014-04-01 18:47:58 +00001268 'CFLAGS_EXTRAS' : "%s %s -I%s" % (stdflag, stdlibflag, os.path.join(os.environ["LLDB_SRC"], "include")),
Daniel Malea55faa402013-05-02 21:44:31 +00001269 'LD_EXTRAS' : "-L%s -llldb" % self.lib_dir}
1270 if self.TraceOn():
1271 print "Building LLDB Driver (%s) from sources %s" % (exe_name, sources)
1272
1273 self.buildDefault(dictionary=d)
1274
Matt Kopec7663b3a2013-09-25 17:44:00 +00001275 def buildLibrary(self, sources, lib_name):
1276 """Platform specific way to build a default library. """
1277
1278 stdflag = self.getstdFlag()
1279
1280 if sys.platform.startswith("darwin"):
1281 dsym = os.path.join(self.lib_dir, 'LLDB.framework', 'LLDB')
1282 d = {'DYLIB_CXX_SOURCES' : sources,
1283 'DYLIB_NAME' : lib_name,
1284 'CFLAGS_EXTRAS' : "%s -stdlib=libc++" % stdflag,
1285 'FRAMEWORK_INCLUDES' : "-F%s" % self.lib_dir,
1286 'LD_EXTRAS' : "%s -Wl,-rpath,%s -dynamiclib" % (dsym, self.lib_dir),
1287 }
1288 elif sys.platform.startswith('freebsd') or sys.platform.startswith("linux") or os.environ.get('LLDB_BUILD_TYPE') == 'Makefile':
1289 d = {'DYLIB_CXX_SOURCES' : sources,
1290 'DYLIB_NAME' : lib_name,
1291 'CFLAGS_EXTRAS' : "%s -I%s -fPIC" % (stdflag, os.path.join(os.environ["LLDB_SRC"], "include")),
1292 'LD_EXTRAS' : "-shared -L%s -llldb" % self.lib_dir}
1293 if self.TraceOn():
1294 print "Building LLDB Library (%s) from sources %s" % (lib_name, sources)
1295
1296 self.buildDefault(dictionary=d)
1297
Daniel Malea55faa402013-05-02 21:44:31 +00001298 def buildProgram(self, sources, exe_name):
1299 """ Platform specific way to build an executable from C/C++ sources. """
1300 d = {'CXX_SOURCES' : sources,
1301 'EXE' : exe_name}
1302 self.buildDefault(dictionary=d)
1303
Johnny Chenfdc80a5c2012-02-01 01:49:50 +00001304 def buildDefault(self, architecture=None, compiler=None, dictionary=None, clean=True):
Johnny Chenfb4264c2011-08-01 19:50:58 +00001305 """Platform specific way to build the default binaries."""
Johnny Chen0fddfb22011-11-17 19:57:27 +00001306 if lldb.skip_build_and_cleanup:
1307 return
Johnny Chenfb4264c2011-08-01 19:50:58 +00001308 module = builder_module()
Johnny Chenfdc80a5c2012-02-01 01:49:50 +00001309 if not module.buildDefault(self, architecture, compiler, dictionary, clean):
Johnny Chenfb4264c2011-08-01 19:50:58 +00001310 raise Exception("Don't know how to build default binary")
1311
Johnny Chenfdc80a5c2012-02-01 01:49:50 +00001312 def buildDsym(self, architecture=None, compiler=None, dictionary=None, clean=True):
Johnny Chenfb4264c2011-08-01 19:50:58 +00001313 """Platform specific way to build binaries with dsym info."""
Johnny Chen0fddfb22011-11-17 19:57:27 +00001314 if lldb.skip_build_and_cleanup:
1315 return
Johnny Chenfb4264c2011-08-01 19:50:58 +00001316 module = builder_module()
Johnny Chenfdc80a5c2012-02-01 01:49:50 +00001317 if not module.buildDsym(self, architecture, compiler, dictionary, clean):
Johnny Chenfb4264c2011-08-01 19:50:58 +00001318 raise Exception("Don't know how to build binary with dsym")
1319
Johnny Chenfdc80a5c2012-02-01 01:49:50 +00001320 def buildDwarf(self, architecture=None, compiler=None, dictionary=None, clean=True):
Johnny Chenfb4264c2011-08-01 19:50:58 +00001321 """Platform specific way to build binaries with dwarf maps."""
Johnny Chen0fddfb22011-11-17 19:57:27 +00001322 if lldb.skip_build_and_cleanup:
1323 return
Johnny Chenfb4264c2011-08-01 19:50:58 +00001324 module = builder_module()
Johnny Chenfdc80a5c2012-02-01 01:49:50 +00001325 if not module.buildDwarf(self, architecture, compiler, dictionary, clean):
Johnny Chenfb4264c2011-08-01 19:50:58 +00001326 raise Exception("Don't know how to build binary with dwarf")
Johnny Chena74bb0a2011-08-01 18:46:13 +00001327
Kuba Breckabeed8212014-09-04 01:03:18 +00001328 def findBuiltClang(self):
1329 """Tries to find and use Clang from the build directory as the compiler (instead of the system compiler)."""
1330 paths_to_try = [
1331 "llvm-build/Release+Asserts/x86_64/Release+Asserts/bin/clang",
1332 "llvm-build/Debug+Asserts/x86_64/Debug+Asserts/bin/clang",
1333 "llvm-build/Release/x86_64/Release/bin/clang",
1334 "llvm-build/Debug/x86_64/Debug/bin/clang",
1335 ]
1336 lldb_root_path = os.path.join(os.path.dirname(__file__), "..")
1337 for p in paths_to_try:
1338 path = os.path.join(lldb_root_path, p)
1339 if os.path.exists(path):
1340 return path
1341
1342 return os.environ["CC"]
1343
Daniel Malea9115f072013-08-06 15:02:32 +00001344 def getBuildFlags(self, use_cpp11=True, use_libcxx=False, use_libstdcxx=False, use_pthreads=True):
Andrew Kaylor93132f52013-05-28 23:04:25 +00001345 """ Returns a dictionary (which can be provided to build* functions above) which
1346 contains OS-specific build flags.
1347 """
1348 cflags = ""
Daniel Malea9115f072013-08-06 15:02:32 +00001349
1350 # On Mac OS X, unless specifically requested to use libstdc++, use libc++
1351 if not use_libstdcxx and sys.platform.startswith('darwin'):
1352 use_libcxx = True
1353
1354 if use_libcxx and self.libcxxPath:
1355 cflags += "-stdlib=libc++ "
1356 if self.libcxxPath:
1357 libcxxInclude = os.path.join(self.libcxxPath, "include")
1358 libcxxLib = os.path.join(self.libcxxPath, "lib")
1359 if os.path.isdir(libcxxInclude) and os.path.isdir(libcxxLib):
1360 cflags += "-nostdinc++ -I%s -L%s -Wl,-rpath,%s " % (libcxxInclude, libcxxLib, libcxxLib)
1361
Andrew Kaylor93132f52013-05-28 23:04:25 +00001362 if use_cpp11:
1363 cflags += "-std="
1364 if "gcc" in self.getCompiler() and "4.6" in self.getCompilerVersion():
1365 cflags += "c++0x"
1366 else:
1367 cflags += "c++11"
Ed Mastedbd59502014-02-02 19:24:15 +00001368 if sys.platform.startswith("darwin") or sys.platform.startswith("freebsd"):
Andrew Kaylor93132f52013-05-28 23:04:25 +00001369 cflags += " -stdlib=libc++"
1370 elif "clang" in self.getCompiler():
1371 cflags += " -stdlib=libstdc++"
1372
1373 if use_pthreads:
1374 ldflags = "-lpthread"
1375
1376 return {'CFLAGS_EXTRAS' : cflags,
1377 'LD_EXTRAS' : ldflags,
1378 }
1379
Johnny Chen9f4f5d92011-08-12 20:19:22 +00001380 def cleanup(self, dictionary=None):
1381 """Platform specific way to do cleanup after build."""
Johnny Chen0fddfb22011-11-17 19:57:27 +00001382 if lldb.skip_build_and_cleanup:
1383 return
Johnny Chen9f4f5d92011-08-12 20:19:22 +00001384 module = builder_module()
1385 if not module.cleanup(self, dictionary):
Johnny Chen0fddfb22011-11-17 19:57:27 +00001386 raise Exception("Don't know how to do cleanup with dictionary: "+dictionary)
Johnny Chen9f4f5d92011-08-12 20:19:22 +00001387
Daniel Malea55faa402013-05-02 21:44:31 +00001388 def getLLDBLibraryEnvVal(self):
1389 """ Returns the path that the OS-specific library search environment variable
1390 (self.dylibPath) should be set to in order for a program to find the LLDB
1391 library. If an environment variable named self.dylibPath is already set,
1392 the new path is appended to it and returned.
1393 """
1394 existing_library_path = os.environ[self.dylibPath] if self.dylibPath in os.environ else None
1395 if existing_library_path:
1396 return "%s:%s" % (existing_library_path, self.lib_dir)
1397 elif sys.platform.startswith("darwin"):
1398 return os.path.join(self.lib_dir, 'LLDB.framework')
1399 else:
1400 return self.lib_dir
Johnny Chena74bb0a2011-08-01 18:46:13 +00001401
Ed Maste437f8f62013-09-09 14:04:04 +00001402 def getLibcPlusPlusLibs(self):
1403 if sys.platform.startswith('freebsd'):
1404 return ['libc++.so.1']
1405 else:
1406 return ['libc++.1.dylib','libc++abi.dylib']
1407
Johnny Chena74bb0a2011-08-01 18:46:13 +00001408class TestBase(Base):
1409 """
1410 This abstract base class is meant to be subclassed. It provides default
1411 implementations for setUpClass(), tearDownClass(), setUp(), and tearDown(),
1412 among other things.
1413
1414 Important things for test class writers:
1415
1416 - Overwrite the mydir class attribute, otherwise your test class won't
1417 run. It specifies the relative directory to the top level 'test' so
1418 the test harness can change to the correct working directory before
1419 running your test.
1420
1421 - The setUp method sets up things to facilitate subsequent interactions
1422 with the debugger as part of the test. These include:
1423 - populate the test method name
1424 - create/get a debugger set with synchronous mode (self.dbg)
1425 - get the command interpreter from with the debugger (self.ci)
1426 - create a result object for use with the command interpreter
1427 (self.res)
1428 - plus other stuffs
1429
1430 - The tearDown method tries to perform some necessary cleanup on behalf
1431 of the test to return the debugger to a good state for the next test.
1432 These include:
1433 - execute any tearDown hooks registered by the test method with
1434 TestBase.addTearDownHook(); examples can be found in
1435 settings/TestSettings.py
1436 - kill the inferior process associated with each target, if any,
1437 and, then delete the target from the debugger's target list
1438 - perform build cleanup before running the next test method in the
1439 same test class; examples of registering for this service can be
1440 found in types/TestIntegerTypes.py with the call:
1441 - self.setTearDownCleanup(dictionary=d)
1442
1443 - Similarly setUpClass and tearDownClass perform classwise setup and
1444 teardown fixtures. The tearDownClass method invokes a default build
1445 cleanup for the entire test class; also, subclasses can implement the
1446 classmethod classCleanup(cls) to perform special class cleanup action.
1447
1448 - The instance methods runCmd and expect are used heavily by existing
1449 test cases to send a command to the command interpreter and to perform
1450 string/pattern matching on the output of such command execution. The
1451 expect method also provides a mode to peform string/pattern matching
1452 without running a command.
1453
1454 - The build methods buildDefault, buildDsym, and buildDwarf are used to
1455 build the binaries used during a particular test scenario. A plugin
1456 should be provided for the sys.platform running the test suite. The
1457 Mac OS X implementation is located in plugins/darwin.py.
1458 """
1459
1460 # Maximum allowed attempts when launching the inferior process.
1461 # Can be overridden by the LLDB_MAX_LAUNCH_COUNT environment variable.
1462 maxLaunchCount = 3;
1463
1464 # Time to wait before the next launching attempt in second(s).
1465 # Can be overridden by the LLDB_TIME_WAIT_NEXT_LAUNCH environment variable.
1466 timeWaitNextLaunch = 1.0;
1467
1468 def doDelay(self):
1469 """See option -w of dotest.py."""
1470 if ("LLDB_WAIT_BETWEEN_TEST_CASES" in os.environ and
1471 os.environ["LLDB_WAIT_BETWEEN_TEST_CASES"] == 'YES'):
1472 waitTime = 1.0
1473 if "LLDB_TIME_WAIT_BETWEEN_TEST_CASES" in os.environ:
1474 waitTime = float(os.environ["LLDB_TIME_WAIT_BETWEEN_TEST_CASES"])
1475 time.sleep(waitTime)
1476
Enrico Granata165f8af2012-09-21 19:10:53 +00001477 # Returns the list of categories to which this test case belongs
1478 # by default, look for a ".categories" file, and read its contents
1479 # if no such file exists, traverse the hierarchy - we guarantee
1480 # a .categories to exist at the top level directory so we do not end up
1481 # looping endlessly - subclasses are free to define their own categories
1482 # in whatever way makes sense to them
1483 def getCategories(self):
1484 import inspect
1485 import os.path
1486 folder = inspect.getfile(self.__class__)
1487 folder = os.path.dirname(folder)
1488 while folder != '/':
1489 categories_file_name = os.path.join(folder,".categories")
1490 if os.path.exists(categories_file_name):
1491 categories_file = open(categories_file_name,'r')
1492 categories = categories_file.readline()
1493 categories_file.close()
1494 categories = str.replace(categories,'\n','')
1495 categories = str.replace(categories,'\r','')
1496 return categories.split(',')
1497 else:
1498 folder = os.path.dirname(folder)
1499 continue
1500
Johnny Chena74bb0a2011-08-01 18:46:13 +00001501 def setUp(self):
1502 #import traceback
1503 #traceback.print_stack()
1504
1505 # Works with the test driver to conditionally skip tests via decorators.
1506 Base.setUp(self)
1507
Johnny Chena74bb0a2011-08-01 18:46:13 +00001508 try:
1509 if lldb.blacklist:
1510 className = self.__class__.__name__
1511 classAndMethodName = "%s.%s" % (className, self._testMethodName)
1512 if className in lldb.blacklist:
1513 self.skipTest(lldb.blacklist.get(className))
1514 elif classAndMethodName in lldb.blacklist:
1515 self.skipTest(lldb.blacklist.get(classAndMethodName))
1516 except AttributeError:
1517 pass
1518
Johnny Chened492022011-06-21 00:53:00 +00001519 # Insert some delay between successive test cases if specified.
1520 self.doDelay()
Johnny Chen0ed37c92010-10-07 02:04:14 +00001521
Johnny Chenf2b70232010-08-25 18:49:48 +00001522 if "LLDB_MAX_LAUNCH_COUNT" in os.environ:
1523 self.maxLaunchCount = int(os.environ["LLDB_MAX_LAUNCH_COUNT"])
1524
Johnny Chen430eb762010-10-19 16:00:42 +00001525 if "LLDB_TIME_WAIT_NEXT_LAUNCH" in os.environ:
Johnny Chen4921b112010-11-29 20:20:34 +00001526 self.timeWaitNextLaunch = float(os.environ["LLDB_TIME_WAIT_NEXT_LAUNCH"])
Johnny Chenf2b70232010-08-25 18:49:48 +00001527
Johnny Chenbf6ffa32010-07-03 03:41:59 +00001528 # Create the debugger instance if necessary.
1529 try:
1530 self.dbg = lldb.DBG
Johnny Chenbf6ffa32010-07-03 03:41:59 +00001531 except AttributeError:
1532 self.dbg = lldb.SBDebugger.Create()
Johnny Chenf02ec122010-07-03 20:41:42 +00001533
Johnny Chen3cd1e552011-05-25 19:06:18 +00001534 if not self.dbg:
Johnny Chenbf6ffa32010-07-03 03:41:59 +00001535 raise Exception('Invalid debugger instance')
1536
Daniel Maleae0f8f572013-08-26 23:57:52 +00001537 #
1538 # Warning: MAJOR HACK AHEAD!
1539 # If we are running testsuite remotely (by checking lldb.lldbtest_remote_sandbox),
1540 # redefine the self.dbg.CreateTarget(filename) method to execute a "file filename"
1541 # command, instead. See also runCmd() where it decorates the "file filename" call
1542 # with additional functionality when running testsuite remotely.
1543 #
1544 if lldb.lldbtest_remote_sandbox:
1545 def DecoratedCreateTarget(arg):
1546 self.runCmd("file %s" % arg)
1547 target = self.dbg.GetSelectedTarget()
1548 #
Greg Claytonc6947512013-12-13 19:18:59 +00001549 # SBtarget.LaunchSimple () currently not working for remote platform?
Daniel Maleae0f8f572013-08-26 23:57:52 +00001550 # johnny @ 04/23/2012
1551 #
1552 def DecoratedLaunchSimple(argv, envp, wd):
1553 self.runCmd("run")
1554 return target.GetProcess()
1555 target.LaunchSimple = DecoratedLaunchSimple
1556
1557 return target
1558 self.dbg.CreateTarget = DecoratedCreateTarget
1559 if self.TraceOn():
1560 print "self.dbg.Create is redefined to:\n%s" % getsource_if_available(DecoratedCreateTarget)
1561
Johnny Chenbf6ffa32010-07-03 03:41:59 +00001562 # We want our debugger to be synchronous.
1563 self.dbg.SetAsync(False)
1564
1565 # Retrieve the associated command interpreter instance.
1566 self.ci = self.dbg.GetCommandInterpreter()
1567 if not self.ci:
1568 raise Exception('Could not get the command interpreter')
1569
1570 # And the result object.
1571 self.res = lldb.SBCommandReturnObject()
1572
Johnny Chen44d24972012-04-16 18:55:15 +00001573 # Run global pre-flight code, if defined via the config file.
1574 if lldb.pre_flight:
1575 lldb.pre_flight(self)
1576
Greg Claytonfb909312013-11-23 01:58:15 +00001577 if lldb.remote_platform:
1578 #remote_test_dir = os.path.join(lldb.remote_platform_working_dir, self.mydir)
Greg Clayton5fb8f792013-12-02 19:35:49 +00001579 remote_test_dir = os.path.join(lldb.remote_platform_working_dir,
1580 self.getArchitecture(),
1581 str(self.test_number),
1582 self.mydir)
Greg Claytonfb909312013-11-23 01:58:15 +00001583 error = lldb.remote_platform.MakeDirectory(remote_test_dir, 0700)
1584 if error.Success():
Greg Claytonfb909312013-11-23 01:58:15 +00001585 lldb.remote_platform.SetWorkingDirectory(remote_test_dir)
1586 else:
1587 print "error: making remote directory '%s': %s" % (remote_test_dir, error)
1588
Enrico Granata44818162012-10-24 01:23:57 +00001589 # utility methods that tests can use to access the current objects
1590 def target(self):
1591 if not self.dbg:
1592 raise Exception('Invalid debugger instance')
1593 return self.dbg.GetSelectedTarget()
1594
1595 def process(self):
1596 if not self.dbg:
1597 raise Exception('Invalid debugger instance')
1598 return self.dbg.GetSelectedTarget().GetProcess()
1599
1600 def thread(self):
1601 if not self.dbg:
1602 raise Exception('Invalid debugger instance')
1603 return self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread()
1604
1605 def frame(self):
1606 if not self.dbg:
1607 raise Exception('Invalid debugger instance')
1608 return self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame()
1609
Greg Claytonc6947512013-12-13 19:18:59 +00001610 def get_process_working_directory(self):
1611 '''Get the working directory that should be used when launching processes for local or remote processes.'''
1612 if lldb.remote_platform:
1613 # Remote tests set the platform working directory up in TestBase.setUp()
1614 return lldb.remote_platform.GetWorkingDirectory()
1615 else:
1616 # local tests change directory into each test subdirectory
1617 return os.getcwd()
1618
Johnny Chenbf6ffa32010-07-03 03:41:59 +00001619 def tearDown(self):
Johnny Chen7d1d7532010-09-02 21:23:12 +00001620 #import traceback
1621 #traceback.print_stack()
1622
Johnny Chenfb4264c2011-08-01 19:50:58 +00001623 Base.tearDown(self)
Johnny Chen707d8222010-10-19 23:40:13 +00001624
Johnny Chen3794ad92011-06-15 21:24:24 +00001625 # Delete the target(s) from the debugger as a general cleanup step.
1626 # This includes terminating the process for each target, if any.
1627 # We'd like to reuse the debugger for our next test without incurring
1628 # the initialization overhead.
1629 targets = []
1630 for target in self.dbg:
1631 if target:
1632 targets.append(target)
1633 process = target.GetProcess()
1634 if process:
1635 rc = self.invoke(process, "Kill")
1636 self.assertTrue(rc.Success(), PROCESS_KILLED)
1637 for target in targets:
1638 self.dbg.DeleteTarget(target)
Johnny Chen6ca006c2010-08-16 21:28:10 +00001639
Johnny Chen44d24972012-04-16 18:55:15 +00001640 # Run global post-flight code, if defined via the config file.
1641 if lldb.post_flight:
1642 lldb.post_flight(self)
1643
Johnny Chenbf6ffa32010-07-03 03:41:59 +00001644 del self.dbg
Johnny Chen150c3cc2010-10-15 01:18:29 +00001645
Johnny Chen86268e42011-09-30 21:48:35 +00001646 def switch_to_thread_with_stop_reason(self, stop_reason):
1647 """
1648 Run the 'thread list' command, and select the thread with stop reason as
1649 'stop_reason'. If no such thread exists, no select action is done.
1650 """
1651 from lldbutil import stop_reason_to_str
1652 self.runCmd('thread list')
1653 output = self.res.GetOutput()
1654 thread_line_pattern = re.compile("^[ *] thread #([0-9]+):.*stop reason = %s" %
1655 stop_reason_to_str(stop_reason))
1656 for line in output.splitlines():
1657 matched = thread_line_pattern.match(line)
1658 if matched:
1659 self.runCmd('thread select %s' % matched.group(1))
1660
Enrico Granata7594f142013-06-17 22:51:50 +00001661 def runCmd(self, cmd, msg=None, check=True, trace=False, inHistory=False):
Johnny Chen27f212d2010-08-19 23:26:59 +00001662 """
1663 Ask the command interpreter to handle the command and then check its
1664 return status.
1665 """
1666 # Fail fast if 'cmd' is not meaningful.
1667 if not cmd or len(cmd) == 0:
1668 raise Exception("Bad 'cmd' parameter encountered")
Johnny Chen5bbb88f2010-08-20 17:57:32 +00001669
Johnny Chen8d55a342010-08-31 17:42:54 +00001670 trace = (True if traceAlways else trace)
Johnny Chend0190a62010-08-23 17:10:44 +00001671
Daniel Maleae0f8f572013-08-26 23:57:52 +00001672 # This is an opportunity to insert the 'platform target-install' command if we are told so
1673 # via the settig of lldb.lldbtest_remote_sandbox.
1674 if cmd.startswith("target create "):
1675 cmd = cmd.replace("target create ", "file ")
1676 if cmd.startswith("file ") and lldb.lldbtest_remote_sandbox:
1677 with recording(self, trace) as sbuf:
1678 the_rest = cmd.split("file ")[1]
1679 # Split the rest of the command line.
1680 atoms = the_rest.split()
1681 #
1682 # NOTE: This assumes that the options, if any, follow the file command,
1683 # instead of follow the specified target.
1684 #
1685 target = atoms[-1]
1686 # Now let's get the absolute pathname of our target.
1687 abs_target = os.path.abspath(target)
1688 print >> sbuf, "Found a file command, target (with absolute pathname)=%s" % abs_target
1689 fpath, fname = os.path.split(abs_target)
1690 parent_dir = os.path.split(fpath)[0]
1691 platform_target_install_command = 'platform target-install %s %s' % (fpath, lldb.lldbtest_remote_sandbox)
1692 print >> sbuf, "Insert this command to be run first: %s" % platform_target_install_command
1693 self.ci.HandleCommand(platform_target_install_command, self.res)
1694 # And this is the file command we want to execute, instead.
1695 #
1696 # Warning: SIDE EFFECT AHEAD!!!
1697 # Populate the remote executable pathname into the lldb namespace,
1698 # so that test cases can grab this thing out of the namespace.
1699 #
1700 lldb.lldbtest_remote_sandboxed_executable = abs_target.replace(parent_dir, lldb.lldbtest_remote_sandbox)
1701 cmd = "file -P %s %s %s" % (lldb.lldbtest_remote_sandboxed_executable, the_rest.replace(target, ''), abs_target)
1702 print >> sbuf, "And this is the replaced file command: %s" % cmd
1703
Johnny Chen63dfb272010-09-01 00:15:19 +00001704 running = (cmd.startswith("run") or cmd.startswith("process launch"))
Johnny Chen5bbb88f2010-08-20 17:57:32 +00001705
Johnny Chen63dfb272010-09-01 00:15:19 +00001706 for i in range(self.maxLaunchCount if running else 1):
Enrico Granata7594f142013-06-17 22:51:50 +00001707 self.ci.HandleCommand(cmd, self.res, inHistory)
Johnny Chen5bbb88f2010-08-20 17:57:32 +00001708
Johnny Chen150c3cc2010-10-15 01:18:29 +00001709 with recording(self, trace) as sbuf:
1710 print >> sbuf, "runCmd:", cmd
Johnny Chenab254f52010-10-15 16:13:00 +00001711 if not check:
Johnny Chen27b107b2010-10-15 18:52:22 +00001712 print >> sbuf, "check of return status not required"
Johnny Chenf2b70232010-08-25 18:49:48 +00001713 if self.res.Succeeded():
Johnny Chen150c3cc2010-10-15 01:18:29 +00001714 print >> sbuf, "output:", self.res.GetOutput()
Johnny Chenf2b70232010-08-25 18:49:48 +00001715 else:
Johnny Chen150c3cc2010-10-15 01:18:29 +00001716 print >> sbuf, "runCmd failed!"
1717 print >> sbuf, self.res.GetError()
Johnny Chen5bbb88f2010-08-20 17:57:32 +00001718
Johnny Chenff3d01d2010-08-20 21:03:09 +00001719 if self.res.Succeeded():
Johnny Chenf2b70232010-08-25 18:49:48 +00001720 break
Johnny Chen150c3cc2010-10-15 01:18:29 +00001721 elif running:
Johnny Chencf7f74e2011-01-19 02:02:08 +00001722 # For process launch, wait some time before possible next try.
1723 time.sleep(self.timeWaitNextLaunch)
Johnny Chen552d6712012-08-01 19:56:04 +00001724 with recording(self, trace) as sbuf:
Johnny Chen150c3cc2010-10-15 01:18:29 +00001725 print >> sbuf, "Command '" + cmd + "' failed!"
Johnny Chen5bbb88f2010-08-20 17:57:32 +00001726
Johnny Chen27f212d2010-08-19 23:26:59 +00001727 if check:
1728 self.assertTrue(self.res.Succeeded(),
Johnny Chenc0c67f22010-11-09 18:42:22 +00001729 msg if msg else CMD_MSG(cmd))
Johnny Chen27f212d2010-08-19 23:26:59 +00001730
Jim Ingham63dfc722012-09-22 00:05:11 +00001731 def match (self, str, patterns, msg=None, trace=False, error=False, matching=True, exe=True):
1732 """run command in str, and match the result against regexp in patterns returning the match object for the first matching pattern
1733
1734 Otherwise, all the arguments have the same meanings as for the expect function"""
1735
1736 trace = (True if traceAlways else trace)
1737
1738 if exe:
1739 # First run the command. If we are expecting error, set check=False.
1740 # Pass the assert message along since it provides more semantic info.
1741 self.runCmd(str, msg=msg, trace = (True if trace else False), check = not error)
1742
1743 # Then compare the output against expected strings.
1744 output = self.res.GetError() if error else self.res.GetOutput()
1745
1746 # If error is True, the API client expects the command to fail!
1747 if error:
1748 self.assertFalse(self.res.Succeeded(),
1749 "Command '" + str + "' is expected to fail!")
1750 else:
1751 # No execution required, just compare str against the golden input.
1752 output = str
1753 with recording(self, trace) as sbuf:
1754 print >> sbuf, "looking at:", output
1755
1756 # The heading says either "Expecting" or "Not expecting".
1757 heading = "Expecting" if matching else "Not expecting"
1758
1759 for pattern in patterns:
1760 # Match Objects always have a boolean value of True.
1761 match_object = re.search(pattern, output)
1762 matched = bool(match_object)
1763 with recording(self, trace) as sbuf:
1764 print >> sbuf, "%s pattern: %s" % (heading, pattern)
1765 print >> sbuf, "Matched" if matched else "Not matched"
1766 if matched:
1767 break
1768
1769 self.assertTrue(matched if matching else not matched,
1770 msg if msg else EXP_MSG(str, exe))
1771
1772 return match_object
1773
Enrico Granata7594f142013-06-17 22:51:50 +00001774 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 Chen27f212d2010-08-19 23:26:59 +00001775 """
1776 Similar to runCmd; with additional expect style output matching ability.
1777
1778 Ask the command interpreter to handle the command and then check its
1779 return status. The 'msg' parameter specifies an informational assert
1780 message. We expect the output from running the command to start with
Johnny Chenea88e942010-09-21 21:08:53 +00001781 'startstr', matches the substrings contained in 'substrs', and regexp
1782 matches the patterns contained in 'patterns'.
Johnny Chenb3307862010-09-17 22:28:51 +00001783
1784 If the keyword argument error is set to True, it signifies that the API
1785 client is expecting the command to fail. In this case, the error stream
Johnny Chenaa902922010-09-17 22:45:27 +00001786 from running the command is retrieved and compared against the golden
Johnny Chenb3307862010-09-17 22:28:51 +00001787 input, instead.
Johnny Chenea88e942010-09-21 21:08:53 +00001788
1789 If the keyword argument matching is set to False, it signifies that the API
1790 client is expecting the output of the command not to match the golden
1791 input.
Johnny Chen9c48b8d2010-09-21 23:33:30 +00001792
1793 Finally, the required argument 'str' represents the lldb command to be
1794 sent to the command interpreter. In case the keyword argument 'exe' is
1795 set to False, the 'str' is treated as a string to be matched/not-matched
1796 against the golden input.
Johnny Chen27f212d2010-08-19 23:26:59 +00001797 """
Johnny Chen8d55a342010-08-31 17:42:54 +00001798 trace = (True if traceAlways else trace)
Johnny Chend0190a62010-08-23 17:10:44 +00001799
Johnny Chen9c48b8d2010-09-21 23:33:30 +00001800 if exe:
1801 # First run the command. If we are expecting error, set check=False.
Johnny Chen62d4f862010-10-28 21:10:32 +00001802 # Pass the assert message along since it provides more semantic info.
Enrico Granata7594f142013-06-17 22:51:50 +00001803 self.runCmd(str, msg=msg, trace = (True if trace else False), check = not error, inHistory=inHistory)
Johnny Chen27f212d2010-08-19 23:26:59 +00001804
Johnny Chen9c48b8d2010-09-21 23:33:30 +00001805 # Then compare the output against expected strings.
1806 output = self.res.GetError() if error else self.res.GetOutput()
Johnny Chenb3307862010-09-17 22:28:51 +00001807
Johnny Chen9c48b8d2010-09-21 23:33:30 +00001808 # If error is True, the API client expects the command to fail!
1809 if error:
1810 self.assertFalse(self.res.Succeeded(),
1811 "Command '" + str + "' is expected to fail!")
1812 else:
1813 # No execution required, just compare str against the golden input.
Enrico Granatabc08ab42012-10-23 00:09:02 +00001814 if isinstance(str,lldb.SBCommandReturnObject):
1815 output = str.GetOutput()
1816 else:
1817 output = str
Johnny Chen150c3cc2010-10-15 01:18:29 +00001818 with recording(self, trace) as sbuf:
1819 print >> sbuf, "looking at:", output
Johnny Chenb3307862010-09-17 22:28:51 +00001820
Johnny Chenea88e942010-09-21 21:08:53 +00001821 # The heading says either "Expecting" or "Not expecting".
Johnny Chen150c3cc2010-10-15 01:18:29 +00001822 heading = "Expecting" if matching else "Not expecting"
Johnny Chenea88e942010-09-21 21:08:53 +00001823
1824 # Start from the startstr, if specified.
1825 # If there's no startstr, set the initial state appropriately.
1826 matched = output.startswith(startstr) if startstr else (True if matching else False)
Johnny Chenb145bba2010-08-20 18:25:15 +00001827
Johnny Chen150c3cc2010-10-15 01:18:29 +00001828 if startstr:
1829 with recording(self, trace) as sbuf:
1830 print >> sbuf, "%s start string: %s" % (heading, startstr)
1831 print >> sbuf, "Matched" if matched else "Not matched"
Johnny Chenb145bba2010-08-20 18:25:15 +00001832
Johnny Chen86268e42011-09-30 21:48:35 +00001833 # Look for endstr, if specified.
1834 keepgoing = matched if matching else not matched
1835 if endstr:
1836 matched = output.endswith(endstr)
1837 with recording(self, trace) as sbuf:
1838 print >> sbuf, "%s end string: %s" % (heading, endstr)
1839 print >> sbuf, "Matched" if matched else "Not matched"
1840
Johnny Chenea88e942010-09-21 21:08:53 +00001841 # Look for sub strings, if specified.
1842 keepgoing = matched if matching else not matched
1843 if substrs and keepgoing:
Johnny Chen27f212d2010-08-19 23:26:59 +00001844 for str in substrs:
Johnny Chenb052f6c2010-09-23 23:35:28 +00001845 matched = output.find(str) != -1
Johnny Chen150c3cc2010-10-15 01:18:29 +00001846 with recording(self, trace) as sbuf:
1847 print >> sbuf, "%s sub string: %s" % (heading, str)
1848 print >> sbuf, "Matched" if matched else "Not matched"
Johnny Chenea88e942010-09-21 21:08:53 +00001849 keepgoing = matched if matching else not matched
1850 if not keepgoing:
Johnny Chen27f212d2010-08-19 23:26:59 +00001851 break
1852
Johnny Chenea88e942010-09-21 21:08:53 +00001853 # Search for regular expression patterns, if specified.
1854 keepgoing = matched if matching else not matched
1855 if patterns and keepgoing:
1856 for pattern in patterns:
1857 # Match Objects always have a boolean value of True.
1858 matched = bool(re.search(pattern, output))
Johnny Chen150c3cc2010-10-15 01:18:29 +00001859 with recording(self, trace) as sbuf:
1860 print >> sbuf, "%s pattern: %s" % (heading, pattern)
1861 print >> sbuf, "Matched" if matched else "Not matched"
Johnny Chenea88e942010-09-21 21:08:53 +00001862 keepgoing = matched if matching else not matched
1863 if not keepgoing:
1864 break
Johnny Chenea88e942010-09-21 21:08:53 +00001865
1866 self.assertTrue(matched if matching else not matched,
Johnny Chenc0c67f22010-11-09 18:42:22 +00001867 msg if msg else EXP_MSG(str, exe))
Johnny Chen27f212d2010-08-19 23:26:59 +00001868
Johnny Chenf3c59232010-08-25 22:52:45 +00001869 def invoke(self, obj, name, trace=False):
Johnny Chen61703c92010-08-25 22:56:10 +00001870 """Use reflection to call a method dynamically with no argument."""
Johnny Chen8d55a342010-08-31 17:42:54 +00001871 trace = (True if traceAlways else trace)
Johnny Chenf3c59232010-08-25 22:52:45 +00001872
1873 method = getattr(obj, name)
1874 import inspect
1875 self.assertTrue(inspect.ismethod(method),
1876 name + "is a method name of object: " + str(obj))
1877 result = method()
Johnny Chen150c3cc2010-10-15 01:18:29 +00001878 with recording(self, trace) as sbuf:
1879 print >> sbuf, str(method) + ":", result
Johnny Chenf3c59232010-08-25 22:52:45 +00001880 return result
Johnny Chen827edff2010-08-27 00:15:48 +00001881
Johnny Chenf359cf22011-05-27 23:36:52 +00001882 # =================================================
1883 # Misc. helper methods for debugging test execution
1884 # =================================================
1885
Johnny Chen56b92a72011-07-11 19:15:11 +00001886 def DebugSBValue(self, val):
Johnny Chen8d55a342010-08-31 17:42:54 +00001887 """Debug print a SBValue object, if traceAlways is True."""
Johnny Chende90f1d2011-04-27 17:43:07 +00001888 from lldbutil import value_type_to_str
Johnny Chen87bb5892010-11-03 21:37:58 +00001889
Johnny Chen8d55a342010-08-31 17:42:54 +00001890 if not traceAlways:
Johnny Chen827edff2010-08-27 00:15:48 +00001891 return
1892
1893 err = sys.stderr
1894 err.write(val.GetName() + ":\n")
Johnny Chen86268e42011-09-30 21:48:35 +00001895 err.write('\t' + "TypeName -> " + val.GetTypeName() + '\n')
1896 err.write('\t' + "ByteSize -> " + str(val.GetByteSize()) + '\n')
1897 err.write('\t' + "NumChildren -> " + str(val.GetNumChildren()) + '\n')
1898 err.write('\t' + "Value -> " + str(val.GetValue()) + '\n')
1899 err.write('\t' + "ValueAsUnsigned -> " + str(val.GetValueAsUnsigned())+ '\n')
1900 err.write('\t' + "ValueType -> " + value_type_to_str(val.GetValueType()) + '\n')
1901 err.write('\t' + "Summary -> " + str(val.GetSummary()) + '\n')
1902 err.write('\t' + "IsPointerType -> " + str(val.TypeIsPointerType()) + '\n')
1903 err.write('\t' + "Location -> " + val.GetLocation() + '\n')
Johnny Chen827edff2010-08-27 00:15:48 +00001904
Johnny Chen36c5eb12011-08-05 20:17:27 +00001905 def DebugSBType(self, type):
1906 """Debug print a SBType object, if traceAlways is True."""
1907 if not traceAlways:
1908 return
1909
1910 err = sys.stderr
1911 err.write(type.GetName() + ":\n")
1912 err.write('\t' + "ByteSize -> " + str(type.GetByteSize()) + '\n')
1913 err.write('\t' + "IsPointerType -> " + str(type.IsPointerType()) + '\n')
1914 err.write('\t' + "IsReferenceType -> " + str(type.IsReferenceType()) + '\n')
1915
Johnny Chenb877f1e2011-03-12 01:18:19 +00001916 def DebugPExpect(self, child):
1917 """Debug the spwaned pexpect object."""
1918 if not traceAlways:
1919 return
1920
1921 print child
Filipe Cabecinhas0eec15a2012-06-20 10:13:40 +00001922
1923 @classmethod
1924 def RemoveTempFile(cls, file):
1925 if os.path.exists(file):
1926 os.remove(file)