Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 1 | """ |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 2 | Test SBProcess APIs, including ReadMemory(), WriteMemory(), and others. |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import unittest2 |
| 7 | import lldb |
Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 8 | from lldbutil import get_stopped_thread, state_type_to_str |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 9 | from lldbtest import * |
| 10 | |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 11 | class ProcessAPITestCase(TestBase): |
| 12 | |
| 13 | mydir = os.path.join("python_api", "process") |
| 14 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 15 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 16 | @python_api_test |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 17 | def test_read_memory_with_dsym(self): |
| 18 | """Test Python SBProcess.ReadMemory() API.""" |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 19 | self.buildDsym() |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 20 | self.read_memory() |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 21 | |
| 22 | @python_api_test |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 23 | def test_read_memory_with_dwarf(self): |
| 24 | """Test Python SBProcess.ReadMemory() API.""" |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 25 | self.buildDwarf() |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 26 | self.read_memory() |
| 27 | |
| 28 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 29 | @python_api_test |
| 30 | def test_write_memory_with_dsym(self): |
| 31 | """Test Python SBProcess.WriteMemory() API.""" |
| 32 | self.buildDsym() |
| 33 | self.write_memory() |
| 34 | |
| 35 | @python_api_test |
| 36 | def test_write_memory_with_dwarf(self): |
| 37 | """Test Python SBProcess.WriteMemory() API.""" |
| 38 | self.buildDwarf() |
| 39 | self.write_memory() |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 40 | |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 41 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 42 | @python_api_test |
| 43 | def test_access_my_int_with_dsym(self): |
| 44 | """Test access 'my_int' using Python SBProcess.GetByteOrder() and other APIs.""" |
| 45 | self.buildDsym() |
| 46 | self.access_my_int() |
| 47 | |
| 48 | @python_api_test |
| 49 | def test_access_my_int_with_dwarf(self): |
| 50 | """Test access 'my_int' using Python SBProcess.GetByteOrder() and other APIs.""" |
| 51 | self.buildDwarf() |
| 52 | self.access_my_int() |
| 53 | |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 54 | @python_api_test |
| 55 | def test_remote_launch(self): |
| 56 | """Test SBProcess.RemoteLaunch() API with a process not in eStateConnected, and it should fail.""" |
| 57 | self.buildDefault() |
| 58 | self.remote_launch_should_fail() |
| 59 | |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 60 | def setUp(self): |
| 61 | # Call super's setUp(). |
| 62 | TestBase.setUp(self) |
| 63 | # Find the line number to break inside main(). |
| 64 | self.line = line_number("main.cpp", "// Set break point at this line and check variable 'my_char'.") |
| 65 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 66 | def read_memory(self): |
| 67 | """Test Python SBProcess.ReadMemory() API.""" |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 68 | exe = os.path.join(os.getcwd(), "a.out") |
| 69 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 70 | |
| 71 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 72 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 73 | |
| 74 | breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 75 | self.assertTrue(breakpoint, VALID_BREAKPOINT) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 76 | |
| 77 | # Launch the process, and do not stop at the entry point. |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 78 | process = target.LaunchSimple(None, None, os.getcwd()) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 79 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 80 | thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 81 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint") |
| 82 | frame = thread.GetFrameAtIndex(0) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 83 | |
| 84 | # Get the SBValue for the global variable 'my_char'. |
| 85 | val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal) |
Johnny Chen | 9a07aba | 2011-07-11 20:06:28 +0000 | [diff] [blame] | 86 | self.DebugSBValue(val) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 87 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 88 | # If the variable does not have a load address, there's no sense continuing. |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 89 | if not val.GetLocation().startswith("0x"): |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 90 | return |
| 91 | |
| 92 | # OK, let's get the hex location of the variable. |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 93 | location = int(val.GetLocation(), 16) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 94 | |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 95 | # Due to the typemap magic (see lldb.swig), we pass in 1 to ReadMemory and |
| 96 | # expect to get a Python string as the result object! |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 97 | error = lldb.SBError() |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 98 | content = process.ReadMemory(location, 1, error) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 99 | if not error.Success(): |
| 100 | self.fail("SBProcess.ReadMemory() failed") |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 101 | if self.TraceOn(): |
| 102 | print "memory content:", content |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 103 | |
| 104 | self.expect(content, "Result from SBProcess.ReadMemory() matches our expected output: 'x'", |
| 105 | exe=False, |
| 106 | startstr = 'x') |
| 107 | |
Johnny Chen | 6e55cd5 | 2011-12-15 23:30:05 +0000 | [diff] [blame] | 108 | # Get the SBValue for the global variable 'my_cstring'. |
| 109 | val = frame.FindValue("my_cstring", lldb.eValueTypeVariableGlobal) |
| 110 | self.DebugSBValue(val) |
| 111 | |
| 112 | # If the variable does not have a load address, there's no sense continuing. |
| 113 | if not val.GetLocation().startswith("0x"): |
| 114 | return |
| 115 | |
| 116 | # OK, let's get the hex location of the variable. |
| 117 | location = int(val.GetLocation(), 16) |
| 118 | |
| 119 | # Due to the typemap magic (see lldb.swig), we pass in 256 to read at most 256 bytes |
| 120 | # from the address, and expect to get a Python string as the result object! |
| 121 | cstring = process.ReadCStringFromMemory(location, 256, error) |
| 122 | if not error.Success(): |
| 123 | self.fail("SBProcess.ReadCStringFromMemory() failed") |
| 124 | if self.TraceOn(): |
| 125 | print "cstring read is:", cstring |
| 126 | |
| 127 | self.expect(cstring, "Result from SBProcess.ReadCStringFromMemory() matches our expected output", |
| 128 | exe=False, |
| 129 | startstr = 'lldb.SBProcess.ReadCStringFromMemory() works!') |
| 130 | |
| 131 | # Get the SBValue for the global variable 'my_uint32'. |
| 132 | val = frame.FindValue("my_uint32", lldb.eValueTypeVariableGlobal) |
| 133 | self.DebugSBValue(val) |
| 134 | |
| 135 | # If the variable does not have a load address, there's no sense continuing. |
| 136 | if not val.GetLocation().startswith("0x"): |
| 137 | return |
| 138 | |
| 139 | # OK, let's get the hex location of the variable. |
| 140 | location = int(val.GetLocation(), 16) |
| 141 | |
| 142 | # Due to the typemap magic (see lldb.swig), we pass in 4 to read at 4 bytes |
| 143 | # from the address, and expect to get an int as the result! |
| 144 | my_uint32 = process.ReadUnsignedFromMemory(location, 4, error) |
| 145 | if not error.Success(): |
| 146 | self.fail("SBProcess.ReadCStringFromMemory() failed") |
| 147 | if self.TraceOn(): |
| 148 | print "uint32 read is:", my_uint32 |
| 149 | |
| 150 | if my_uint32 != 12345: |
| 151 | self.fail("Result from SBProcess.ReadUnsignedFromMemory() does not match our expected output") |
| 152 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 153 | def write_memory(self): |
| 154 | """Test Python SBProcess.WriteMemory() API.""" |
| 155 | exe = os.path.join(os.getcwd(), "a.out") |
| 156 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 157 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 158 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 159 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 160 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 161 | breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 162 | self.assertTrue(breakpoint, VALID_BREAKPOINT) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 163 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 164 | # Launch the process, and do not stop at the entry point. |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 165 | process = target.LaunchSimple(None, None, os.getcwd()) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 166 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 167 | thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 168 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint") |
| 169 | frame = thread.GetFrameAtIndex(0) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 170 | |
| 171 | # Get the SBValue for the global variable 'my_char'. |
| 172 | val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal) |
Johnny Chen | 9a07aba | 2011-07-11 20:06:28 +0000 | [diff] [blame] | 173 | self.DebugSBValue(val) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 174 | |
| 175 | # If the variable does not have a load address, there's no sense continuing. |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 176 | if not val.GetLocation().startswith("0x"): |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 177 | return |
| 178 | |
| 179 | # OK, let's get the hex location of the variable. |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 180 | location = int(val.GetLocation(), 16) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 181 | |
| 182 | # The program logic makes the 'my_char' variable to have memory content as 'x'. |
| 183 | # But we want to use the WriteMemory() API to assign 'a' to the variable. |
| 184 | |
| 185 | # Now use WriteMemory() API to write 'a' into the global variable. |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 186 | error = lldb.SBError() |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 187 | result = process.WriteMemory(location, 'a', error) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 188 | if not error.Success() or result != 1: |
| 189 | self.fail("SBProcess.WriteMemory() failed") |
| 190 | |
| 191 | # Read from the memory location. This time it should be 'a'. |
| 192 | # Due to the typemap magic (see lldb.swig), we pass in 1 to ReadMemory and |
| 193 | # expect to get a Python string as the result object! |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 194 | content = process.ReadMemory(location, 1, error) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 195 | if not error.Success(): |
| 196 | self.fail("SBProcess.ReadMemory() failed") |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 197 | if self.TraceOn(): |
| 198 | print "memory content:", content |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 199 | |
| 200 | self.expect(content, "Result from SBProcess.ReadMemory() matches our expected output: 'a'", |
| 201 | exe=False, |
| 202 | startstr = 'a') |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 203 | |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 204 | def access_my_int(self): |
| 205 | """Test access 'my_int' using Python SBProcess.GetByteOrder() and other APIs.""" |
| 206 | exe = os.path.join(os.getcwd(), "a.out") |
| 207 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 208 | |
| 209 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 210 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 211 | |
| 212 | breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 213 | self.assertTrue(breakpoint, VALID_BREAKPOINT) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 214 | |
| 215 | # Launch the process, and do not stop at the entry point. |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 216 | process = target.LaunchSimple(None, None, os.getcwd()) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 217 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 218 | thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 219 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint") |
| 220 | frame = thread.GetFrameAtIndex(0) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 221 | |
| 222 | # Get the SBValue for the global variable 'my_int'. |
| 223 | val = frame.FindValue("my_int", lldb.eValueTypeVariableGlobal) |
Johnny Chen | 9a07aba | 2011-07-11 20:06:28 +0000 | [diff] [blame] | 224 | self.DebugSBValue(val) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 225 | |
| 226 | # If the variable does not have a load address, there's no sense continuing. |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 227 | if not val.GetLocation().startswith("0x"): |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 228 | return |
| 229 | |
| 230 | # OK, let's get the hex location of the variable. |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 231 | location = int(val.GetLocation(), 16) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 232 | |
Johnny Chen | 4e90a7e | 2011-03-02 19:49:27 +0000 | [diff] [blame] | 233 | # Note that the canonical from of the bytearray is little endian. |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 234 | from lldbutil import int_to_bytearray, bytearray_to_int |
Johnny Chen | 4e90a7e | 2011-03-02 19:49:27 +0000 | [diff] [blame] | 235 | |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 236 | byteSize = val.GetByteSize() |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 237 | bytes = int_to_bytearray(256, byteSize) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 238 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 239 | byteOrder = process.GetByteOrder() |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 240 | if byteOrder == lldb.eByteOrderBig: |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 241 | bytes.reverse() |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 242 | elif byteOrder == lldb.eByteOrderLittle: |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 243 | pass |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 244 | else: |
| 245 | # Neither big endian nor little endian? Return for now. |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 246 | # Add more logic here if we want to handle other types. |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 247 | return |
| 248 | |
| 249 | # The program logic makes the 'my_int' variable to have int type and value of 0. |
| 250 | # But we want to use the WriteMemory() API to assign 256 to the variable. |
| 251 | |
| 252 | # Now use WriteMemory() API to write 256 into the global variable. |
| 253 | new_value = str(bytes) |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 254 | error = lldb.SBError() |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 255 | result = process.WriteMemory(location, new_value, error) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 256 | if not error.Success() or result != byteSize: |
| 257 | self.fail("SBProcess.WriteMemory() failed") |
| 258 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 259 | # Make sure that the val we got originally updates itself to notice the change: |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 260 | self.expect(val.GetValue(), |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 261 | "SBProcess.ReadMemory() successfully writes (int)256 to the memory location for 'my_int'", |
| 262 | exe=False, |
| 263 | startstr = '256') |
| 264 | |
| 265 | # And for grins, get the SBValue for the global variable 'my_int' again, to make sure that also tracks the new value: |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 266 | val = frame.FindValue("my_int", lldb.eValueTypeVariableGlobal) |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 267 | self.expect(val.GetValue(), |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 268 | "SBProcess.ReadMemory() successfully writes (int)256 to the memory location for 'my_int'", |
| 269 | exe=False, |
| 270 | startstr = '256') |
| 271 | |
| 272 | # Now read the memory content. The bytearray should have (byte)1 as the second element. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 273 | content = process.ReadMemory(location, byteSize, error) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 274 | if not error.Success(): |
| 275 | self.fail("SBProcess.ReadMemory() failed") |
Johnny Chen | 4e90a7e | 2011-03-02 19:49:27 +0000 | [diff] [blame] | 276 | |
| 277 | # Use "ascii" as the encoding because each element of 'content' is in the range [0..255]. |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 278 | new_bytes = bytearray(content, "ascii") |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 279 | |
| 280 | # The bytearray_to_int utility function expects a little endian bytearray. |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 281 | if byteOrder == lldb.eByteOrderBig: |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 282 | new_bytes.reverse() |
| 283 | |
| 284 | new_value = bytearray_to_int(new_bytes, byteSize) |
| 285 | if new_value != 256: |
| 286 | self.fail("Memory content read from 'my_int' does not match (int)256") |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 287 | |
| 288 | # Dump the memory content.... |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 289 | if self.TraceOn(): |
| 290 | for i in new_bytes: |
| 291 | print "byte:", i |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 292 | |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 293 | def remote_launch_should_fail(self): |
| 294 | """Test SBProcess.RemoteLaunch() API with a process not in eStateConnected, and it should fail.""" |
| 295 | exe = os.path.join(os.getcwd(), "a.out") |
| 296 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 297 | |
| 298 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 299 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 300 | |
| 301 | # Launch the process, and do not stop at the entry point. |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 302 | process = target.LaunchSimple(None, None, os.getcwd()) |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 303 | |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 304 | if self.TraceOn(): |
Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 305 | print "process state:", state_type_to_str(process.GetState()) |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 306 | self.assertTrue(process.GetState() != lldb.eStateConnected) |
| 307 | |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 308 | error = lldb.SBError() |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 309 | success = process.RemoteLaunch(None, None, None, None, None, None, 0, False, error) |
| 310 | self.assertTrue(not success, "RemoteLaunch() should fail for process state != eStateConnected") |
| 311 | |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 312 | |
| 313 | if __name__ == '__main__': |
| 314 | import atexit |
| 315 | lldb.SBDebugger.Initialize() |
| 316 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 317 | unittest2.main() |