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) |
| 72 | self.assertTrue(target.IsValid(), VALID_TARGET) |
| 73 | |
| 74 | breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) |
| 75 | self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT) |
| 76 | |
| 77 | # Launch the process, and do not stop at the entry point. |
| 78 | error = lldb.SBError() |
| 79 | self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error) |
| 80 | |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 81 | thread = get_stopped_thread(self.process, lldb.eStopReasonBreakpoint) |
| 82 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint") |
| 83 | frame = thread.GetFrameAtIndex(0) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 84 | |
| 85 | # Get the SBValue for the global variable 'my_char'. |
| 86 | val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 87 | self.DebugSBValue(frame, val) |
| 88 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 89 | # If the variable does not have a load address, there's no sense continuing. |
| 90 | if not val.GetLocation(frame).startswith("0x"): |
| 91 | return |
| 92 | |
| 93 | # OK, let's get the hex location of the variable. |
| 94 | location = int(val.GetLocation(frame), 16) |
| 95 | |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 96 | # Due to the typemap magic (see lldb.swig), we pass in 1 to ReadMemory and |
| 97 | # expect to get a Python string as the result object! |
| 98 | content = self.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 | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 108 | def write_memory(self): |
| 109 | """Test Python SBProcess.WriteMemory() API.""" |
| 110 | exe = os.path.join(os.getcwd(), "a.out") |
| 111 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 112 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 113 | target = self.dbg.CreateTarget(exe) |
| 114 | self.assertTrue(target.IsValid(), VALID_TARGET) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 115 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 116 | breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) |
| 117 | self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 118 | |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 119 | # Launch the process, and do not stop at the entry point. |
| 120 | error = lldb.SBError() |
| 121 | self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 122 | |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 123 | thread = get_stopped_thread(self.process, lldb.eStopReasonBreakpoint) |
| 124 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint") |
| 125 | frame = thread.GetFrameAtIndex(0) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 126 | |
| 127 | # Get the SBValue for the global variable 'my_char'. |
| 128 | val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal) |
| 129 | self.DebugSBValue(frame, val) |
| 130 | |
| 131 | # If the variable does not have a load address, there's no sense continuing. |
| 132 | if not val.GetLocation(frame).startswith("0x"): |
| 133 | return |
| 134 | |
| 135 | # OK, let's get the hex location of the variable. |
| 136 | location = int(val.GetLocation(frame), 16) |
| 137 | |
| 138 | # The program logic makes the 'my_char' variable to have memory content as 'x'. |
| 139 | # But we want to use the WriteMemory() API to assign 'a' to the variable. |
| 140 | |
| 141 | # Now use WriteMemory() API to write 'a' into the global variable. |
| 142 | result = self.process.WriteMemory(location, 'a', error) |
| 143 | if not error.Success() or result != 1: |
| 144 | self.fail("SBProcess.WriteMemory() failed") |
| 145 | |
| 146 | # Read from the memory location. This time it should be 'a'. |
| 147 | # Due to the typemap magic (see lldb.swig), we pass in 1 to ReadMemory and |
| 148 | # expect to get a Python string as the result object! |
| 149 | content = self.process.ReadMemory(location, 1, error) |
| 150 | if not error.Success(): |
| 151 | self.fail("SBProcess.ReadMemory() failed") |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 152 | if self.TraceOn(): |
| 153 | print "memory content:", content |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 154 | |
| 155 | self.expect(content, "Result from SBProcess.ReadMemory() matches our expected output: 'a'", |
| 156 | exe=False, |
| 157 | startstr = 'a') |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 158 | |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 159 | def access_my_int(self): |
| 160 | """Test access 'my_int' using Python SBProcess.GetByteOrder() and other APIs.""" |
| 161 | exe = os.path.join(os.getcwd(), "a.out") |
| 162 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 163 | |
| 164 | target = self.dbg.CreateTarget(exe) |
| 165 | self.assertTrue(target.IsValid(), VALID_TARGET) |
| 166 | |
| 167 | breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) |
| 168 | self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT) |
| 169 | |
| 170 | # Launch the process, and do not stop at the entry point. |
| 171 | error = lldb.SBError() |
| 172 | self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error) |
| 173 | |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 174 | thread = get_stopped_thread(self.process, lldb.eStopReasonBreakpoint) |
| 175 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint") |
| 176 | frame = thread.GetFrameAtIndex(0) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 177 | |
| 178 | # Get the SBValue for the global variable 'my_int'. |
| 179 | val = frame.FindValue("my_int", lldb.eValueTypeVariableGlobal) |
| 180 | self.DebugSBValue(frame, val) |
| 181 | |
| 182 | # If the variable does not have a load address, there's no sense continuing. |
| 183 | if not val.GetLocation(frame).startswith("0x"): |
| 184 | return |
| 185 | |
| 186 | # OK, let's get the hex location of the variable. |
| 187 | location = int(val.GetLocation(frame), 16) |
| 188 | |
Johnny Chen | 4e90a7e | 2011-03-02 19:49:27 +0000 | [diff] [blame] | 189 | # Note that the canonical from of the bytearray is little endian. |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 190 | from lldbutil import int_to_bytearray, bytearray_to_int |
Johnny Chen | 4e90a7e | 2011-03-02 19:49:27 +0000 | [diff] [blame] | 191 | |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 192 | byteSize = val.GetByteSize() |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 193 | bytes = int_to_bytearray(256, byteSize) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 194 | |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 195 | byteOrder = self.process.GetByteOrder() |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 196 | if byteOrder == lldb.eByteOrderBig: |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 197 | bytes.reverse() |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 198 | elif byteOrder == lldb.eByteOrderLittle: |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 199 | pass |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 200 | else: |
| 201 | # Neither big endian nor little endian? Return for now. |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 202 | # Add more logic here if we want to handle other types. |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 203 | return |
| 204 | |
| 205 | # The program logic makes the 'my_int' variable to have int type and value of 0. |
| 206 | # But we want to use the WriteMemory() API to assign 256 to the variable. |
| 207 | |
| 208 | # Now use WriteMemory() API to write 256 into the global variable. |
| 209 | new_value = str(bytes) |
| 210 | result = self.process.WriteMemory(location, new_value, error) |
| 211 | if not error.Success() or result != byteSize: |
| 212 | self.fail("SBProcess.WriteMemory() failed") |
| 213 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 214 | # Make sure that the val we got originally updates itself to notice the change: |
| 215 | self.expect(val.GetValue(frame), |
| 216 | "SBProcess.ReadMemory() successfully writes (int)256 to the memory location for 'my_int'", |
| 217 | exe=False, |
| 218 | startstr = '256') |
| 219 | |
| 220 | # 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] | 221 | val = frame.FindValue("my_int", lldb.eValueTypeVariableGlobal) |
| 222 | self.expect(val.GetValue(frame), |
| 223 | "SBProcess.ReadMemory() successfully writes (int)256 to the memory location for 'my_int'", |
| 224 | exe=False, |
| 225 | startstr = '256') |
| 226 | |
| 227 | # Now read the memory content. The bytearray should have (byte)1 as the second element. |
| 228 | content = self.process.ReadMemory(location, byteSize, error) |
| 229 | if not error.Success(): |
| 230 | self.fail("SBProcess.ReadMemory() failed") |
Johnny Chen | 4e90a7e | 2011-03-02 19:49:27 +0000 | [diff] [blame] | 231 | |
| 232 | # 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] | 233 | new_bytes = bytearray(content, "ascii") |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 234 | |
| 235 | # The bytearray_to_int utility function expects a little endian bytearray. |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 236 | if byteOrder == lldb.eByteOrderBig: |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 237 | new_bytes.reverse() |
| 238 | |
| 239 | new_value = bytearray_to_int(new_bytes, byteSize) |
| 240 | if new_value != 256: |
| 241 | 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] | 242 | |
| 243 | # Dump the memory content.... |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 244 | if self.TraceOn(): |
| 245 | for i in new_bytes: |
| 246 | print "byte:", i |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 247 | |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 248 | def remote_launch_should_fail(self): |
| 249 | """Test SBProcess.RemoteLaunch() API with a process not in eStateConnected, and it should fail.""" |
| 250 | exe = os.path.join(os.getcwd(), "a.out") |
| 251 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 252 | |
| 253 | target = self.dbg.CreateTarget(exe) |
| 254 | self.assertTrue(target.IsValid(), VALID_TARGET) |
| 255 | |
| 256 | # Launch the process, and do not stop at the entry point. |
| 257 | error = lldb.SBError() |
| 258 | process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error) |
| 259 | |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 260 | if self.TraceOn(): |
Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame^] | 261 | print "process state:", state_type_to_str(process.GetState()) |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 262 | self.assertTrue(process.GetState() != lldb.eStateConnected) |
| 263 | |
| 264 | success = process.RemoteLaunch(None, None, None, None, None, None, 0, False, error) |
| 265 | self.assertTrue(not success, "RemoteLaunch() should fail for process state != eStateConnected") |
| 266 | |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 267 | |
| 268 | if __name__ == '__main__': |
| 269 | import atexit |
| 270 | lldb.SBDebugger.Initialize() |
| 271 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 272 | unittest2.main() |