Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 1 | """ |
| 2 | LLDB module which provides the abstract base class of lldb test case. |
| 3 | |
| 4 | The concrete subclass can override lldbtest.TesBase in order to inherit the |
| 5 | common behavior for unitest.TestCase.setUp/tearDown implemented in this file. |
| 6 | |
| 7 | The subclass should override the attribute mydir in order for the python runtime |
| 8 | to locate the individual test cases when running as part of a large test suite |
| 9 | or when running each test case as a separate python invocation. |
| 10 | |
| 11 | ./dotest.py provides a test driver which sets up the environment to run the |
| 12 | entire test suite. Users who want to run a test case on its own can specify the |
| 13 | LLDB_TEST and PYTHONPATH environment variables, for example: |
| 14 | |
| 15 | $ export LLDB_TEST=$PWD |
| 16 | $ export PYTHONPATH=/Volumes/data/lldb/svn/trunk/build/Debug/LLDB.framework/Resources/Python:$LLDB_TEST |
| 17 | $ echo $LLDB_TEST |
| 18 | /Volumes/data/lldb/svn/trunk/test |
| 19 | $ echo $PYTHONPATH |
| 20 | /Volumes/data/lldb/svn/trunk/build/Debug/LLDB.framework/Resources/Python:/Volumes/data/lldb/svn/trunk/test |
| 21 | $ python function_types/TestFunctionTypes.py |
| 22 | . |
| 23 | ---------------------------------------------------------------------- |
| 24 | Ran 1 test in 0.363s |
| 25 | |
| 26 | OK |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame^] | 27 | $ LLDB_COMMAND_TRACE=YES python array_types/TestArrayTypes.py |
| 28 | LLDB_COMMAND_TRACE=YES python array_types/TestArrayTypes.py |
| 29 | runCmd: file /Volumes/data/lldb/svn/trunk/test/array_types/a.out |
| 30 | output: Current executable set to '/Volumes/data/lldb/svn/trunk/test/array_types/a.out' (x86_64). |
| 31 | |
| 32 | runCmd: breakpoint set -f main.c -l 42 |
| 33 | output: Breakpoint created: 1: file ='main.c', line = 42, locations = 1 |
| 34 | |
| 35 | runCmd: run |
| 36 | output: Launching '/Volumes/data/lldb/svn/trunk/test/array_types/a.out' (x86_64) |
| 37 | |
| 38 | runCmd: thread list |
| 39 | output: Process 24987 state is Stopped |
| 40 | thread #1: tid = 0x2e03, pc = 0x0000000100000df4, where = a.out`main + 612 at /Volumes/data/lldb/svn/trunk/test/array_types/main.c:45, stop reason = breakpoint 1.1, queue = com.apple.main-thread |
| 41 | |
| 42 | runCmd: breakpoint list |
| 43 | output: Current breakpoints: |
| 44 | 1: file ='main.c', line = 42, locations = 1, resolved = 1 |
| 45 | 1.1: where = a.out`main + 612 at /Volumes/data/lldb/svn/trunk/test/array_types/main.c:45, address = 0x0000000100000df4, resolved, hit count = 1 |
| 46 | |
| 47 | |
| 48 | runCmd: variable list strings |
| 49 | output: (char *[4]) strings = { |
| 50 | (char *) strings[0] = 0x0000000100000f0c "Hello", |
| 51 | (char *) strings[1] = 0x0000000100000f12 "Hola", |
| 52 | (char *) strings[2] = 0x0000000100000f17 "Bonjour", |
| 53 | (char *) strings[3] = 0x0000000100000f1f "Guten Tag" |
| 54 | } |
| 55 | |
| 56 | runCmd: variable list char_16 |
| 57 | output: (char [16]) char_16 = { |
| 58 | (char) char_16[0] = 'H', |
| 59 | (char) char_16[1] = 'e', |
| 60 | (char) char_16[2] = 'l', |
| 61 | (char) char_16[3] = 'l', |
| 62 | (char) char_16[4] = 'o', |
| 63 | (char) char_16[5] = ' ', |
| 64 | (char) char_16[6] = 'W', |
| 65 | (char) char_16[7] = 'o', |
| 66 | (char) char_16[8] = 'r', |
| 67 | (char) char_16[9] = 'l', |
| 68 | (char) char_16[10] = 'd', |
| 69 | (char) char_16[11] = '\n', |
| 70 | (char) char_16[12] = '\0', |
| 71 | (char) char_16[13] = '\0', |
| 72 | (char) char_16[14] = '\0', |
| 73 | (char) char_16[15] = '\0' |
| 74 | } |
| 75 | |
| 76 | runCmd: variable list ushort_matrix |
| 77 | output: (unsigned short [2][3]) ushort_matrix = { |
| 78 | (unsigned short [3]) ushort_matrix[0] = { |
| 79 | (unsigned short) ushort_matrix[0][0] = 0x0001, |
| 80 | (unsigned short) ushort_matrix[0][1] = 0x0002, |
| 81 | (unsigned short) ushort_matrix[0][2] = 0x0003 |
| 82 | }, |
| 83 | (unsigned short [3]) ushort_matrix[1] = { |
| 84 | (unsigned short) ushort_matrix[1][0] = 0x000b, |
| 85 | (unsigned short) ushort_matrix[1][1] = 0x0016, |
| 86 | (unsigned short) ushort_matrix[1][2] = 0x0021 |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | runCmd: variable list long_6 |
| 91 | output: (long [6]) long_6 = { |
| 92 | (long) long_6[0] = 1, |
| 93 | (long) long_6[1] = 2, |
| 94 | (long) long_6[2] = 3, |
| 95 | (long) long_6[3] = 4, |
| 96 | (long) long_6[4] = 5, |
| 97 | (long) long_6[5] = 6 |
| 98 | } |
| 99 | |
| 100 | . |
| 101 | ---------------------------------------------------------------------- |
| 102 | Ran 1 test in 0.349s |
| 103 | |
| 104 | OK |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 105 | $ |
| 106 | """ |
| 107 | |
| 108 | import os |
Johnny Chen | ff3d01d | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 109 | import sys |
Johnny Chen | 7325883 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 110 | import unittest2 |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 111 | import lldb |
| 112 | |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 113 | # |
| 114 | # Some commonly used assert messages. |
| 115 | # |
| 116 | |
| 117 | CURRENT_EXECUTABLE_SET = "Current executable set successfully" |
| 118 | |
Johnny Chen | ff3d01d | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 119 | RUN_STOPPED = "Process is launched and then stopped successfully" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 120 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 121 | RUN_COMPLETED = "Process exited successfully" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 122 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 123 | BREAKPOINT_CREATED = "Breakpoint created successfully" |
| 124 | |
Johnny Chen | e76896c | 2010-08-17 21:33:31 +0000 | [diff] [blame] | 125 | BREAKPOINT_PENDING_CREATED = "Pending breakpoint created successfully" |
| 126 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 127 | BREAKPOINT_HIT_ONCE = "Breakpoint resolved with hit cout = 1" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 128 | |
| 129 | STOPPED_DUE_TO_BREAKPOINT = "Process state is stopped due to breakpoint" |
| 130 | |
| 131 | STOPPED_DUE_TO_STEP_IN = "Process state is stopped due to step in" |
| 132 | |
| 133 | VARIABLES_DISPLAYED_CORRECTLY = "Show specified variable(s) correctly" |
| 134 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 135 | # |
| 136 | # And a generic "Command '%s' returns successfully" message generator. |
| 137 | # |
| 138 | def CMD_MSG(command): |
| 139 | return "Command '%s' returns successfully" % (command) |
| 140 | |
| 141 | |
Johnny Chen | 7325883 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 142 | class TestBase(unittest2.TestCase): |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 143 | """This LLDB abstract base class is meant to be subclassed.""" |
| 144 | |
| 145 | # The concrete subclass should override this attribute. |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 146 | mydir = None |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 147 | |
Johnny Chen | 6ca006c | 2010-08-16 21:28:10 +0000 | [diff] [blame] | 148 | # State pertaining to the inferior process, if any. |
| 149 | runStarted = False |
| 150 | |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame^] | 151 | # os.environ["LLDB_COMMAND_TRACE"], if set to "YES", will turn on this flag. |
| 152 | traceAlways = False; |
| 153 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 154 | def setUp(self): |
Johnny Chen | 9289a65 | 2010-08-07 01:13:18 +0000 | [diff] [blame] | 155 | #import traceback |
Johnny Chen | a212495 | 2010-08-05 21:23:45 +0000 | [diff] [blame] | 156 | #traceback.print_stack() |
| 157 | |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 158 | # Fail fast if 'mydir' attribute is not overridden. |
| 159 | if not self.mydir or len(self.mydir) == 0: |
| 160 | raise Exception("Subclasses must override the 'mydir' attribute.") |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 161 | # Save old working directory. |
| 162 | self.oldcwd = os.getcwd() |
| 163 | |
| 164 | # Change current working directory if ${LLDB_TEST} is defined. |
| 165 | # See also dotest.py which sets up ${LLDB_TEST}. |
| 166 | if ("LLDB_TEST" in os.environ): |
| 167 | os.chdir(os.path.join(os.environ["LLDB_TEST"], self.mydir)); |
| 168 | |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame^] | 169 | if ("LLDB_COMMAND_TRACE" in os.environ and |
| 170 | os.environ["LLDB_COMMAND_TRACE"] == "YES"): |
| 171 | self.traceAlways = True |
| 172 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 173 | # Create the debugger instance if necessary. |
| 174 | try: |
| 175 | self.dbg = lldb.DBG |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 176 | except AttributeError: |
| 177 | self.dbg = lldb.SBDebugger.Create() |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 178 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 179 | if not self.dbg.IsValid(): |
| 180 | raise Exception('Invalid debugger instance') |
| 181 | |
| 182 | # We want our debugger to be synchronous. |
| 183 | self.dbg.SetAsync(False) |
| 184 | |
| 185 | # Retrieve the associated command interpreter instance. |
| 186 | self.ci = self.dbg.GetCommandInterpreter() |
| 187 | if not self.ci: |
| 188 | raise Exception('Could not get the command interpreter') |
| 189 | |
| 190 | # And the result object. |
| 191 | self.res = lldb.SBCommandReturnObject() |
| 192 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 193 | def tearDown(self): |
Johnny Chen | 6ca006c | 2010-08-16 21:28:10 +0000 | [diff] [blame] | 194 | # Finish the inferior process, if it was "run" previously. |
| 195 | if self.runStarted: |
| 196 | self.ci.HandleCommand("continue", self.res) |
| 197 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 198 | del self.dbg |
| 199 | |
| 200 | # Restore old working directory. |
| 201 | os.chdir(self.oldcwd) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 202 | |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame^] | 203 | def runCmd(self, cmd, msg=None, check=True, trace=False): |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 204 | """ |
| 205 | Ask the command interpreter to handle the command and then check its |
| 206 | return status. |
| 207 | """ |
| 208 | # Fail fast if 'cmd' is not meaningful. |
| 209 | if not cmd or len(cmd) == 0: |
| 210 | raise Exception("Bad 'cmd' parameter encountered") |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 211 | |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame^] | 212 | trace = (True if self.traceAlways else trace) |
| 213 | |
| 214 | if trace: |
Johnny Chen | ff3d01d | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 215 | print >> sys.stderr, "runCmd:", cmd |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 216 | |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 217 | self.ci.HandleCommand(cmd, self.res) |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 218 | |
Johnny Chen | a6480c1 | 2010-08-19 23:53:55 +0000 | [diff] [blame] | 219 | if cmd.startswith("run"): |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 220 | self.runStarted = True |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 221 | |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame^] | 222 | if trace: |
Johnny Chen | ff3d01d | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 223 | if self.res.Succeeded(): |
| 224 | print >> sys.stderr, "output:", self.res.GetOutput() |
| 225 | else: |
| 226 | print >> sys.stderr, self.res.GetError() |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 227 | |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 228 | if check: |
| 229 | self.assertTrue(self.res.Succeeded(), |
| 230 | msg if msg else CMD_MSG(cmd)) |
| 231 | |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame^] | 232 | def expect(self, cmd, msg=None, startstr=None, substrs=None, trace=False): |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 233 | """ |
| 234 | Similar to runCmd; with additional expect style output matching ability. |
| 235 | |
| 236 | Ask the command interpreter to handle the command and then check its |
| 237 | return status. The 'msg' parameter specifies an informational assert |
| 238 | message. We expect the output from running the command to start with |
| 239 | 'startstr' and matches the substrings contained in 'substrs'. |
| 240 | """ |
Johnny Chen | 74f26b8 | 2010-08-20 19:17:39 +0000 | [diff] [blame] | 241 | |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame^] | 242 | trace = (True if self.traceAlways else trace) |
| 243 | |
Johnny Chen | 74f26b8 | 2010-08-20 19:17:39 +0000 | [diff] [blame] | 244 | # First run the command. |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame^] | 245 | self.runCmd(cmd, trace = (True if trace else False)) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 246 | |
Johnny Chen | 74f26b8 | 2010-08-20 19:17:39 +0000 | [diff] [blame] | 247 | # Then compare the output against expected strings. |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 248 | output = self.res.GetOutput() |
| 249 | matched = output.startswith(startstr) if startstr else True |
Johnny Chen | b145bba | 2010-08-20 18:25:15 +0000 | [diff] [blame] | 250 | |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame^] | 251 | if not matched and startstr and trace: |
Johnny Chen | ff3d01d | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 252 | print >> sys.stderr, "Startstr not matched:", startstr |
Johnny Chen | b145bba | 2010-08-20 18:25:15 +0000 | [diff] [blame] | 253 | |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 254 | if substrs: |
| 255 | for str in substrs: |
| 256 | matched = output.find(str) > 0 |
| 257 | if not matched: |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame^] | 258 | if trace: |
Johnny Chen | ff3d01d | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 259 | print >> sys.stderr, "Substring not matched:", str |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 260 | break |
| 261 | |
Johnny Chen | 74f26b8 | 2010-08-20 19:17:39 +0000 | [diff] [blame] | 262 | self.assertTrue(matched, msg if msg else CMD_MSG(cmd)) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 263 | |