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 | e7e8af8 | 2011-12-16 00:25:30 +0000 | [diff] [blame^] | 108 | # Read (char *)my_char_ptr. |
| 109 | val = frame.FindValue("my_char_ptr", lldb.eValueTypeVariableGlobal) |
| 110 | self.DebugSBValue(val) |
| 111 | cstring = process.ReadCStringFromMemory(val.GetValueAsUnsigned(), 256, error) |
| 112 | if not error.Success(): |
| 113 | self.fail("SBProcess.ReadCStringFromMemory() failed") |
| 114 | if self.TraceOn(): |
| 115 | print "cstring read is:", cstring |
| 116 | |
| 117 | self.expect(cstring, "Result from SBProcess.ReadCStringFromMemory() matches our expected output", |
| 118 | exe=False, |
| 119 | startstr = 'Does it work?') |
| 120 | |
Johnny Chen | 6e55cd5 | 2011-12-15 23:30:05 +0000 | [diff] [blame] | 121 | # Get the SBValue for the global variable 'my_cstring'. |
| 122 | val = frame.FindValue("my_cstring", lldb.eValueTypeVariableGlobal) |
| 123 | self.DebugSBValue(val) |
| 124 | |
| 125 | # If the variable does not have a load address, there's no sense continuing. |
| 126 | if not val.GetLocation().startswith("0x"): |
| 127 | return |
| 128 | |
| 129 | # OK, let's get the hex location of the variable. |
| 130 | location = int(val.GetLocation(), 16) |
| 131 | |
| 132 | # Due to the typemap magic (see lldb.swig), we pass in 256 to read at most 256 bytes |
| 133 | # from the address, and expect to get a Python string as the result object! |
| 134 | cstring = process.ReadCStringFromMemory(location, 256, error) |
| 135 | if not error.Success(): |
| 136 | self.fail("SBProcess.ReadCStringFromMemory() failed") |
| 137 | if self.TraceOn(): |
| 138 | print "cstring read is:", cstring |
| 139 | |
| 140 | self.expect(cstring, "Result from SBProcess.ReadCStringFromMemory() matches our expected output", |
| 141 | exe=False, |
| 142 | startstr = 'lldb.SBProcess.ReadCStringFromMemory() works!') |
| 143 | |
| 144 | # Get the SBValue for the global variable 'my_uint32'. |
| 145 | val = frame.FindValue("my_uint32", lldb.eValueTypeVariableGlobal) |
| 146 | self.DebugSBValue(val) |
| 147 | |
| 148 | # If the variable does not have a load address, there's no sense continuing. |
| 149 | if not val.GetLocation().startswith("0x"): |
| 150 | return |
| 151 | |
| 152 | # OK, let's get the hex location of the variable. |
| 153 | location = int(val.GetLocation(), 16) |
| 154 | |
| 155 | # Due to the typemap magic (see lldb.swig), we pass in 4 to read at 4 bytes |
| 156 | # from the address, and expect to get an int as the result! |
| 157 | my_uint32 = process.ReadUnsignedFromMemory(location, 4, error) |
| 158 | if not error.Success(): |
| 159 | self.fail("SBProcess.ReadCStringFromMemory() failed") |
| 160 | if self.TraceOn(): |
| 161 | print "uint32 read is:", my_uint32 |
| 162 | |
| 163 | if my_uint32 != 12345: |
| 164 | self.fail("Result from SBProcess.ReadUnsignedFromMemory() does not match our expected output") |
| 165 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 166 | def write_memory(self): |
| 167 | """Test Python SBProcess.WriteMemory() API.""" |
| 168 | exe = os.path.join(os.getcwd(), "a.out") |
| 169 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 170 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 171 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 172 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 173 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 174 | breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 175 | self.assertTrue(breakpoint, VALID_BREAKPOINT) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 176 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 177 | # Launch the process, and do not stop at the entry point. |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 178 | process = target.LaunchSimple(None, None, os.getcwd()) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 179 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 180 | thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 181 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint") |
| 182 | frame = thread.GetFrameAtIndex(0) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 183 | |
| 184 | # Get the SBValue for the global variable 'my_char'. |
| 185 | val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal) |
Johnny Chen | 9a07aba | 2011-07-11 20:06:28 +0000 | [diff] [blame] | 186 | self.DebugSBValue(val) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 187 | |
| 188 | # 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] | 189 | if not val.GetLocation().startswith("0x"): |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 190 | return |
| 191 | |
| 192 | # OK, let's get the hex location of the variable. |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 193 | location = int(val.GetLocation(), 16) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 194 | |
| 195 | # The program logic makes the 'my_char' variable to have memory content as 'x'. |
| 196 | # But we want to use the WriteMemory() API to assign 'a' to the variable. |
| 197 | |
| 198 | # Now use WriteMemory() API to write 'a' into the global variable. |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 199 | error = lldb.SBError() |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 200 | result = process.WriteMemory(location, 'a', error) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 201 | if not error.Success() or result != 1: |
| 202 | self.fail("SBProcess.WriteMemory() failed") |
| 203 | |
| 204 | # Read from the memory location. This time it should be 'a'. |
| 205 | # Due to the typemap magic (see lldb.swig), we pass in 1 to ReadMemory and |
| 206 | # expect to get a Python string as the result object! |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 207 | content = process.ReadMemory(location, 1, error) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 208 | if not error.Success(): |
| 209 | self.fail("SBProcess.ReadMemory() failed") |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 210 | if self.TraceOn(): |
| 211 | print "memory content:", content |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 212 | |
| 213 | self.expect(content, "Result from SBProcess.ReadMemory() matches our expected output: 'a'", |
| 214 | exe=False, |
| 215 | startstr = 'a') |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 216 | |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 217 | def access_my_int(self): |
| 218 | """Test access 'my_int' using Python SBProcess.GetByteOrder() and other APIs.""" |
| 219 | exe = os.path.join(os.getcwd(), "a.out") |
| 220 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 221 | |
| 222 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 223 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 224 | |
| 225 | breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 226 | self.assertTrue(breakpoint, VALID_BREAKPOINT) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 227 | |
| 228 | # Launch the process, and do not stop at the entry point. |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 229 | process = target.LaunchSimple(None, None, os.getcwd()) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 230 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 231 | thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 232 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint") |
| 233 | frame = thread.GetFrameAtIndex(0) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 234 | |
| 235 | # Get the SBValue for the global variable 'my_int'. |
| 236 | val = frame.FindValue("my_int", lldb.eValueTypeVariableGlobal) |
Johnny Chen | 9a07aba | 2011-07-11 20:06:28 +0000 | [diff] [blame] | 237 | self.DebugSBValue(val) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 238 | |
| 239 | # 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] | 240 | if not val.GetLocation().startswith("0x"): |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 241 | return |
| 242 | |
| 243 | # OK, let's get the hex location of the variable. |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 244 | location = int(val.GetLocation(), 16) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 245 | |
Johnny Chen | 4e90a7e | 2011-03-02 19:49:27 +0000 | [diff] [blame] | 246 | # Note that the canonical from of the bytearray is little endian. |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 247 | from lldbutil import int_to_bytearray, bytearray_to_int |
Johnny Chen | 4e90a7e | 2011-03-02 19:49:27 +0000 | [diff] [blame] | 248 | |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 249 | byteSize = val.GetByteSize() |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 250 | bytes = int_to_bytearray(256, byteSize) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 251 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 252 | byteOrder = process.GetByteOrder() |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 253 | if byteOrder == lldb.eByteOrderBig: |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 254 | bytes.reverse() |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 255 | elif byteOrder == lldb.eByteOrderLittle: |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 256 | pass |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 257 | else: |
| 258 | # Neither big endian nor little endian? Return for now. |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 259 | # Add more logic here if we want to handle other types. |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 260 | return |
| 261 | |
| 262 | # The program logic makes the 'my_int' variable to have int type and value of 0. |
| 263 | # But we want to use the WriteMemory() API to assign 256 to the variable. |
| 264 | |
| 265 | # Now use WriteMemory() API to write 256 into the global variable. |
| 266 | new_value = str(bytes) |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 267 | error = lldb.SBError() |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 268 | result = process.WriteMemory(location, new_value, error) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 269 | if not error.Success() or result != byteSize: |
| 270 | self.fail("SBProcess.WriteMemory() failed") |
| 271 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 272 | # 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] | 273 | self.expect(val.GetValue(), |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 274 | "SBProcess.ReadMemory() successfully writes (int)256 to the memory location for 'my_int'", |
| 275 | exe=False, |
| 276 | startstr = '256') |
| 277 | |
| 278 | # 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] | 279 | val = frame.FindValue("my_int", lldb.eValueTypeVariableGlobal) |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 280 | self.expect(val.GetValue(), |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 281 | "SBProcess.ReadMemory() successfully writes (int)256 to the memory location for 'my_int'", |
| 282 | exe=False, |
| 283 | startstr = '256') |
| 284 | |
| 285 | # 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] | 286 | content = process.ReadMemory(location, byteSize, error) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 287 | if not error.Success(): |
| 288 | self.fail("SBProcess.ReadMemory() failed") |
Johnny Chen | 4e90a7e | 2011-03-02 19:49:27 +0000 | [diff] [blame] | 289 | |
| 290 | # 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] | 291 | new_bytes = bytearray(content, "ascii") |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 292 | |
| 293 | # The bytearray_to_int utility function expects a little endian bytearray. |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 294 | if byteOrder == lldb.eByteOrderBig: |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 295 | new_bytes.reverse() |
| 296 | |
| 297 | new_value = bytearray_to_int(new_bytes, byteSize) |
| 298 | if new_value != 256: |
| 299 | 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] | 300 | |
| 301 | # Dump the memory content.... |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 302 | if self.TraceOn(): |
| 303 | for i in new_bytes: |
| 304 | print "byte:", i |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 305 | |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 306 | def remote_launch_should_fail(self): |
| 307 | """Test SBProcess.RemoteLaunch() API with a process not in eStateConnected, and it should fail.""" |
| 308 | exe = os.path.join(os.getcwd(), "a.out") |
| 309 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 310 | |
| 311 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 312 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 313 | |
| 314 | # Launch the process, and do not stop at the entry point. |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 315 | process = target.LaunchSimple(None, None, os.getcwd()) |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 316 | |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 317 | if self.TraceOn(): |
Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 318 | print "process state:", state_type_to_str(process.GetState()) |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 319 | self.assertTrue(process.GetState() != lldb.eStateConnected) |
| 320 | |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 321 | error = lldb.SBError() |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 322 | success = process.RemoteLaunch(None, None, None, None, None, None, 0, False, error) |
| 323 | self.assertTrue(not success, "RemoteLaunch() should fail for process state != eStateConnected") |
| 324 | |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 325 | |
| 326 | if __name__ == '__main__': |
| 327 | import atexit |
| 328 | lldb.SBDebugger.Initialize() |
| 329 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 330 | unittest2.main() |