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 |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 16 | $ export PYTHONPATH=/Volumes/data/lldb/svn/trunk/build/Debug/LLDB.framework/Resources/Python:$LLDB_TEST:$LLDB_TEST/plugins |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 17 | $ echo $LLDB_TEST |
| 18 | /Volumes/data/lldb/svn/trunk/test |
| 19 | $ echo $PYTHONPATH |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 20 | /Volumes/data/lldb/svn/trunk/build/Debug/LLDB.framework/Resources/Python:/Volumes/data/lldb/svn/trunk/test:/Volumes/data/lldb/svn/trunk/test/plugins |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 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 | |
Johnny Chen | 8952a2d | 2010-08-30 21:35:00 +0000 | [diff] [blame] | 108 | import os, sys |
| 109 | from subprocess import * |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 110 | import time |
Johnny Chen | a33a93c | 2010-08-30 23:08:52 +0000 | [diff] [blame] | 111 | import types |
Johnny Chen | 7325883 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 112 | import unittest2 |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 113 | import lldb |
| 114 | |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 115 | if "LLDB_COMMAND_TRACE" in os.environ and os.environ["LLDB_COMMAND_TRACE"]=="YES": |
| 116 | traceAlways = True |
| 117 | else: |
| 118 | traceAlways = False |
| 119 | |
| 120 | |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 121 | # |
| 122 | # Some commonly used assert messages. |
| 123 | # |
| 124 | |
| 125 | CURRENT_EXECUTABLE_SET = "Current executable set successfully" |
| 126 | |
Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame^] | 127 | PROCESS_IS_VALID = "Process is valid" |
| 128 | |
| 129 | PROCESS_KILLED = "Process is killed successfully" |
| 130 | |
Johnny Chen | 5ee8819 | 2010-08-27 23:47:36 +0000 | [diff] [blame] | 131 | RUN_SUCCEEDED = "Process is launched successfully" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 132 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 133 | RUN_COMPLETED = "Process exited successfully" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 134 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 135 | BREAKPOINT_CREATED = "Breakpoint created successfully" |
| 136 | |
Johnny Chen | e76896c | 2010-08-17 21:33:31 +0000 | [diff] [blame] | 137 | BREAKPOINT_PENDING_CREATED = "Pending breakpoint created successfully" |
| 138 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 139 | BREAKPOINT_HIT_ONCE = "Breakpoint resolved with hit cout = 1" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 140 | |
| 141 | STOPPED_DUE_TO_BREAKPOINT = "Process state is stopped due to breakpoint" |
| 142 | |
| 143 | STOPPED_DUE_TO_STEP_IN = "Process state is stopped due to step in" |
| 144 | |
Johnny Chen | 3c884a0 | 2010-08-24 22:07:56 +0000 | [diff] [blame] | 145 | DATA_TYPES_DISPLAYED_CORRECTLY = "Data type(s) displayed correctly" |
| 146 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 147 | VALID_BREAKPOINT = "Got a valid breakpoint" |
| 148 | |
Johnny Chen | 5ee8819 | 2010-08-27 23:47:36 +0000 | [diff] [blame] | 149 | VALID_FILESPEC = "Got a valid filespec" |
| 150 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 151 | VALID_PROCESS = "Got a valid process" |
| 152 | |
| 153 | VALID_TARGET = "Got a valid target" |
| 154 | |
Johnny Chen | 981463d | 2010-08-25 19:00:04 +0000 | [diff] [blame] | 155 | VARIABLES_DISPLAYED_CORRECTLY = "Variable(s) displayed correctly" |
Johnny Chen | 0077809 | 2010-08-09 22:01:17 +0000 | [diff] [blame] | 156 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 157 | |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 158 | # |
| 159 | # And a generic "Command '%s' returns successfully" message generator. |
| 160 | # |
| 161 | def CMD_MSG(command): |
| 162 | return "Command '%s' returns successfully" % (command) |
| 163 | |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 164 | # |
Johnny Chen | 82d404c8 | 2010-08-27 18:08:58 +0000 | [diff] [blame] | 165 | # Returns the enum from the input string. |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 166 | # |
Johnny Chen | 82d404c8 | 2010-08-27 18:08:58 +0000 | [diff] [blame] | 167 | def StopReasonEnum(string): |
| 168 | if string == "Invalid": |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 169 | return 0 |
Johnny Chen | 82d404c8 | 2010-08-27 18:08:58 +0000 | [diff] [blame] | 170 | elif string == "None": |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 171 | return 1 |
Johnny Chen | 82d404c8 | 2010-08-27 18:08:58 +0000 | [diff] [blame] | 172 | elif string == "Trace": |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 173 | return 2 |
Johnny Chen | 82d404c8 | 2010-08-27 18:08:58 +0000 | [diff] [blame] | 174 | elif string == "Breakpoint": |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 175 | return 3 |
Johnny Chen | 82d404c8 | 2010-08-27 18:08:58 +0000 | [diff] [blame] | 176 | elif string == "Watchpoint": |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 177 | return 4 |
Johnny Chen | 82d404c8 | 2010-08-27 18:08:58 +0000 | [diff] [blame] | 178 | elif string == "Signal": |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 179 | return 5 |
Johnny Chen | 82d404c8 | 2010-08-27 18:08:58 +0000 | [diff] [blame] | 180 | elif string == "Exception": |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 181 | return 6 |
Johnny Chen | 82d404c8 | 2010-08-27 18:08:58 +0000 | [diff] [blame] | 182 | elif string == "PlanComplete": |
Johnny Chen | 5fca8ca | 2010-08-26 20:04:17 +0000 | [diff] [blame] | 183 | return 7 |
| 184 | else: |
| 185 | raise Exception("Unknown stopReason string") |
Johnny Chen | 1794184 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 186 | |
Johnny Chen | 27c4123 | 2010-08-26 21:49:29 +0000 | [diff] [blame] | 187 | # |
Johnny Chen | 82d404c8 | 2010-08-27 18:08:58 +0000 | [diff] [blame] | 188 | # Returns the stopReason string given an enum. |
| 189 | # |
| 190 | def StopReasonString(enum): |
| 191 | if enum == 0: |
| 192 | return "Invalid" |
| 193 | elif enum == 1: |
| 194 | return "None" |
| 195 | elif enum == 2: |
| 196 | return "Trace" |
| 197 | elif enum == 3: |
| 198 | return "Breakpoint" |
| 199 | elif enum == 4: |
| 200 | return "Watchpoint" |
| 201 | elif enum == 5: |
| 202 | return "Signal" |
| 203 | elif enum == 6: |
| 204 | return "Exception" |
| 205 | elif enum == 7: |
| 206 | return "PlanComplete" |
| 207 | else: |
| 208 | raise Exception("Unknown stopReason enum") |
| 209 | |
| 210 | # |
Johnny Chen | 27c4123 | 2010-08-26 21:49:29 +0000 | [diff] [blame] | 211 | # Returns an env variable array from the os.environ map object. |
| 212 | # |
| 213 | def EnvArray(): |
| 214 | return map(lambda k,v: k+"="+v, os.environ.keys(), os.environ.values()) |
| 215 | |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 216 | # From 2.7's subprocess.check_output() convenience function. |
| 217 | def system(*popenargs, **kwargs): |
| 218 | r"""Run command with arguments and return its output as a byte string. |
| 219 | |
| 220 | If the exit code was non-zero it raises a CalledProcessError. The |
| 221 | CalledProcessError object will have the return code in the returncode |
| 222 | attribute and output in the output attribute. |
| 223 | |
| 224 | The arguments are the same as for the Popen constructor. Example: |
| 225 | |
| 226 | >>> check_output(["ls", "-l", "/dev/null"]) |
| 227 | 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' |
| 228 | |
| 229 | The stdout argument is not allowed as it is used internally. |
| 230 | To capture standard error in the result, use stderr=STDOUT. |
| 231 | |
| 232 | >>> check_output(["/bin/sh", "-c", |
| 233 | ... "ls -l non_existent_file ; exit 0"], |
| 234 | ... stderr=STDOUT) |
| 235 | 'ls: non_existent_file: No such file or directory\n' |
| 236 | """ |
| 237 | if 'stdout' in kwargs: |
| 238 | raise ValueError('stdout argument not allowed, it will be overridden.') |
| 239 | process = Popen(stdout=PIPE, *popenargs, **kwargs) |
| 240 | output, unused_err = process.communicate() |
| 241 | retcode = process.poll() |
| 242 | |
| 243 | if traceAlways: |
| 244 | if isinstance(popenargs, types.StringTypes): |
| 245 | args = [popenargs] |
| 246 | else: |
| 247 | args = list(popenargs) |
| 248 | print >> sys.stderr |
| 249 | print >> sys.stderr, "os command:", args |
| 250 | print >> sys.stderr, "output:", output |
| 251 | print >> sys.stderr, "error:", unused_err |
| 252 | print >> sys.stderr, "retcode:", retcode |
| 253 | |
| 254 | if retcode: |
| 255 | cmd = kwargs.get("args") |
| 256 | if cmd is None: |
| 257 | cmd = popenargs[0] |
| 258 | raise CalledProcessError(retcode, cmd, output=output) |
| 259 | return output |
| 260 | |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 261 | |
Johnny Chen | 7325883 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 262 | class TestBase(unittest2.TestCase): |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 263 | """This LLDB abstract base class is meant to be subclassed.""" |
| 264 | |
| 265 | # The concrete subclass should override this attribute. |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 266 | mydir = None |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 267 | |
Johnny Chen | 6ca006c | 2010-08-16 21:28:10 +0000 | [diff] [blame] | 268 | # State pertaining to the inferior process, if any. |
| 269 | runStarted = False |
| 270 | |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 271 | # Maximum allowed attempts when launching the inferior process. |
| 272 | # Can be overridden by the LLDB_MAX_LAUNCH_COUNT environment variable. |
| 273 | maxLaunchCount = 3; |
| 274 | |
| 275 | # Time to wait before the next launching attempt in second(s). |
| 276 | # Can be overridden by the LLDB_TIME_WAIT environment variable. |
| 277 | timeWait = 1.0; |
| 278 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 279 | def setUp(self): |
Johnny Chen | 9289a65 | 2010-08-07 01:13:18 +0000 | [diff] [blame] | 280 | #import traceback |
Johnny Chen | a212495 | 2010-08-05 21:23:45 +0000 | [diff] [blame] | 281 | #traceback.print_stack() |
| 282 | |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 283 | # Fail fast if 'mydir' attribute is not overridden. |
| 284 | if not self.mydir or len(self.mydir) == 0: |
| 285 | raise Exception("Subclasses must override the 'mydir' attribute.") |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 286 | # Save old working directory. |
| 287 | self.oldcwd = os.getcwd() |
| 288 | |
| 289 | # Change current working directory if ${LLDB_TEST} is defined. |
| 290 | # See also dotest.py which sets up ${LLDB_TEST}. |
| 291 | if ("LLDB_TEST" in os.environ): |
| 292 | os.chdir(os.path.join(os.environ["LLDB_TEST"], self.mydir)); |
| 293 | |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 294 | if "LLDB_MAX_LAUNCH_COUNT" in os.environ: |
| 295 | self.maxLaunchCount = int(os.environ["LLDB_MAX_LAUNCH_COUNT"]) |
| 296 | |
| 297 | if "LLDB_TIME_WAIT" in os.environ: |
| 298 | self.timeWait = float(os.environ["LLDB_TIME_WAIT"]) |
| 299 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 300 | # Create the debugger instance if necessary. |
| 301 | try: |
| 302 | self.dbg = lldb.DBG |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 303 | except AttributeError: |
| 304 | self.dbg = lldb.SBDebugger.Create() |
Johnny Chen | f02ec12 | 2010-07-03 20:41:42 +0000 | [diff] [blame] | 305 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 306 | if not self.dbg.IsValid(): |
| 307 | raise Exception('Invalid debugger instance') |
| 308 | |
| 309 | # We want our debugger to be synchronous. |
| 310 | self.dbg.SetAsync(False) |
| 311 | |
Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame^] | 312 | # There is no process associated with the debugger as yet. |
| 313 | self.process = None |
| 314 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 315 | # Retrieve the associated command interpreter instance. |
| 316 | self.ci = self.dbg.GetCommandInterpreter() |
| 317 | if not self.ci: |
| 318 | raise Exception('Could not get the command interpreter') |
| 319 | |
| 320 | # And the result object. |
| 321 | self.res = lldb.SBCommandReturnObject() |
| 322 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 323 | def tearDown(self): |
Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame^] | 324 | #import traceback |
| 325 | #traceback.print_stack() |
| 326 | |
| 327 | # Terminate the current process being debugged, if any. |
Johnny Chen | 6ca006c | 2010-08-16 21:28:10 +0000 | [diff] [blame] | 328 | if self.runStarted: |
Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame^] | 329 | self.runCmd("process kill", PROCESS_KILLED, check=False) |
| 330 | elif self.process and self.process.IsValid(): |
| 331 | rc = self.process.Kill() |
| 332 | self.assertTrue(rc.Success(), PROCESS_KILLED) |
Johnny Chen | 6ca006c | 2010-08-16 21:28:10 +0000 | [diff] [blame] | 333 | |
Johnny Chen | bf6ffa3 | 2010-07-03 03:41:59 +0000 | [diff] [blame] | 334 | del self.dbg |
| 335 | |
| 336 | # Restore old working directory. |
| 337 | os.chdir(self.oldcwd) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 338 | |
Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 339 | def runCmd(self, cmd, msg=None, check=True, trace=False, setCookie=True): |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 340 | """ |
| 341 | Ask the command interpreter to handle the command and then check its |
| 342 | return status. |
| 343 | """ |
| 344 | # Fail fast if 'cmd' is not meaningful. |
| 345 | if not cmd or len(cmd) == 0: |
| 346 | raise Exception("Bad 'cmd' parameter encountered") |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 347 | |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 348 | trace = (True if traceAlways else trace) |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 349 | |
Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 350 | running = (cmd.startswith("run") or cmd.startswith("process launch")) |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 351 | |
Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 352 | for i in range(self.maxLaunchCount if running else 1): |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 353 | self.ci.HandleCommand(cmd, self.res) |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 354 | |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 355 | if trace: |
| 356 | print >> sys.stderr, "runCmd:", cmd |
| 357 | if self.res.Succeeded(): |
| 358 | print >> sys.stderr, "output:", self.res.GetOutput() |
| 359 | else: |
| 360 | print >> sys.stderr, self.res.GetError() |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 361 | |
Johnny Chen | ff3d01d | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 362 | if self.res.Succeeded(): |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 363 | break |
Johnny Chen | ff3d01d | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 364 | else: |
Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 365 | if running: |
Johnny Chen | f2b7023 | 2010-08-25 18:49:48 +0000 | [diff] [blame] | 366 | # Process launch failed, wait some time before the next try. |
| 367 | time.sleep(self.timeWait) |
Johnny Chen | 5bbb88f | 2010-08-20 17:57:32 +0000 | [diff] [blame] | 368 | |
Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame^] | 369 | # Modify runStarted only if "run" or "process launch" was encountered. |
| 370 | if running: |
| 371 | self.runStarted = running and setCookie |
Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 372 | |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 373 | if check: |
| 374 | self.assertTrue(self.res.Succeeded(), |
| 375 | msg if msg else CMD_MSG(cmd)) |
| 376 | |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 377 | def expect(self, cmd, msg=None, startstr=None, substrs=None, trace=False): |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 378 | """ |
| 379 | Similar to runCmd; with additional expect style output matching ability. |
| 380 | |
| 381 | Ask the command interpreter to handle the command and then check its |
| 382 | return status. The 'msg' parameter specifies an informational assert |
| 383 | message. We expect the output from running the command to start with |
| 384 | 'startstr' and matches the substrings contained in 'substrs'. |
| 385 | """ |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 386 | trace = (True if traceAlways else trace) |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 387 | |
Johnny Chen | 74f26b8 | 2010-08-20 19:17:39 +0000 | [diff] [blame] | 388 | # First run the command. |
Johnny Chen | d0190a6 | 2010-08-23 17:10:44 +0000 | [diff] [blame] | 389 | self.runCmd(cmd, trace = (True if trace else False)) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 390 | |
Johnny Chen | 74f26b8 | 2010-08-20 19:17:39 +0000 | [diff] [blame] | 391 | # Then compare the output against expected strings. |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 392 | output = self.res.GetOutput() |
| 393 | matched = output.startswith(startstr) if startstr else True |
Johnny Chen | b145bba | 2010-08-20 18:25:15 +0000 | [diff] [blame] | 394 | |
Johnny Chen | c7c9fcf | 2010-08-24 23:48:10 +0000 | [diff] [blame] | 395 | if startstr and trace: |
| 396 | print >> sys.stderr, "Expecting start string:", startstr |
| 397 | print >> sys.stderr, "Matched" if matched else "Not matched" |
| 398 | print >> sys.stderr |
Johnny Chen | b145bba | 2010-08-20 18:25:15 +0000 | [diff] [blame] | 399 | |
Johnny Chen | 981463d | 2010-08-25 19:00:04 +0000 | [diff] [blame] | 400 | if substrs and matched: |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 401 | for str in substrs: |
| 402 | matched = output.find(str) > 0 |
Johnny Chen | c7c9fcf | 2010-08-24 23:48:10 +0000 | [diff] [blame] | 403 | if trace: |
| 404 | print >> sys.stderr, "Expecting sub string:", str |
| 405 | print >> sys.stderr, "Matched" if matched else "Not matched" |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 406 | if not matched: |
| 407 | break |
Johnny Chen | c7c9fcf | 2010-08-24 23:48:10 +0000 | [diff] [blame] | 408 | if trace: |
| 409 | print >> sys.stderr |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 410 | |
Johnny Chen | 74f26b8 | 2010-08-20 19:17:39 +0000 | [diff] [blame] | 411 | self.assertTrue(matched, msg if msg else CMD_MSG(cmd)) |
Johnny Chen | 27f212d | 2010-08-19 23:26:59 +0000 | [diff] [blame] | 412 | |
Johnny Chen | f3c5923 | 2010-08-25 22:52:45 +0000 | [diff] [blame] | 413 | def invoke(self, obj, name, trace=False): |
Johnny Chen | 61703c9 | 2010-08-25 22:56:10 +0000 | [diff] [blame] | 414 | """Use reflection to call a method dynamically with no argument.""" |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 415 | trace = (True if traceAlways else trace) |
Johnny Chen | f3c5923 | 2010-08-25 22:52:45 +0000 | [diff] [blame] | 416 | |
| 417 | method = getattr(obj, name) |
| 418 | import inspect |
| 419 | self.assertTrue(inspect.ismethod(method), |
| 420 | name + "is a method name of object: " + str(obj)) |
| 421 | result = method() |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 422 | if trace: |
Johnny Chen | f3c5923 | 2010-08-25 22:52:45 +0000 | [diff] [blame] | 423 | print str(method) + ":", result |
| 424 | return result |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 425 | |
Johnny Chen | 1363908 | 2010-09-01 22:08:51 +0000 | [diff] [blame] | 426 | def breakAfterLaunch(self, process, func, trace=False): |
| 427 | """ |
| 428 | Perform some dancees after LaunchProcess() to break at func name. |
| 429 | |
| 430 | Return True if we can successfully break at the func name in due time. |
| 431 | """ |
| 432 | trace = (True if traceAlways else trace) |
| 433 | |
| 434 | count = 0 |
| 435 | while True: |
| 436 | # The stop reason of the thread should be breakpoint. |
| 437 | thread = process.GetThreadAtIndex(0) |
| 438 | SR = thread.GetStopReason() |
| 439 | if trace: |
| 440 | print >> sys.stderr, "StopReason =", StopReasonString(SR) |
| 441 | |
| 442 | if SR == StopReasonEnum("Breakpoint"): |
| 443 | frame = thread.GetFrameAtIndex(0) |
| 444 | name = frame.GetFunction().GetName() |
| 445 | if (name == func): |
| 446 | # We got what we want; now break out of the loop. |
| 447 | return True |
| 448 | |
| 449 | # The inferior is in a transient state; continue the process. |
| 450 | time.sleep(1.0) |
| 451 | if trace: |
| 452 | print >> sys.stderr, "Continuing the process:", process |
| 453 | process.Continue() |
| 454 | |
| 455 | count = count + 1 |
| 456 | if count == 10: |
| 457 | if trace: |
| 458 | print >> sys.stderr, "Reached 10 iterations, giving up..." |
| 459 | # Enough iterations already, break out of the loop. |
| 460 | return False |
| 461 | |
| 462 | # End of while loop. |
| 463 | |
| 464 | |
Johnny Chen | 2f1ad5e | 2010-08-30 22:26:48 +0000 | [diff] [blame] | 465 | def buildDsym(self): |
| 466 | """Platform specific way to build binaries with dsym info.""" |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 467 | module = __import__(sys.platform) |
| 468 | if not module.buildDsym(): |
Johnny Chen | 2f1ad5e | 2010-08-30 22:26:48 +0000 | [diff] [blame] | 469 | raise Exception("Don't know how to build binary with dsym") |
| 470 | |
| 471 | def buildDwarf(self): |
| 472 | """Platform specific way to build binaries with dwarf maps.""" |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 473 | module = __import__(sys.platform) |
| 474 | if not module.buildDwarf(): |
Johnny Chen | 2f1ad5e | 2010-08-30 22:26:48 +0000 | [diff] [blame] | 475 | raise Exception("Don't know how to build binary with dwarf") |
| 476 | |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 477 | def DebugSBValue(self, frame, val): |
Johnny Chen | 8d55a34 | 2010-08-31 17:42:54 +0000 | [diff] [blame] | 478 | """Debug print a SBValue object, if traceAlways is True.""" |
| 479 | if not traceAlways: |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 480 | return |
| 481 | |
| 482 | err = sys.stderr |
| 483 | err.write(val.GetName() + ":\n") |
| 484 | err.write('\t' + "TypeName -> " + val.GetTypeName() + '\n') |
| 485 | err.write('\t' + "ByteSize -> " + str(val.GetByteSize()) + '\n') |
| 486 | err.write('\t' + "NumChildren -> " + str(val.GetNumChildren()) + '\n') |
| 487 | err.write('\t' + "Value -> " + str(val.GetValue(frame)) + '\n') |
| 488 | err.write('\t' + "Summary -> " + str(val.GetSummary(frame)) + '\n') |
| 489 | err.write('\t' + "IsPtrType -> " + str(val.TypeIsPtrType()) + '\n') |
| 490 | err.write('\t' + "Location -> " + val.GetLocation(frame) + '\n') |
| 491 | |