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