blob: 0f0d32420107f53d3a2295dd0c394c0bccd96810 [file] [log] [blame]
Johnny Chen37f99fd2011-03-01 02:20:14 +00001"""
Johnny Chen90aa5942011-03-01 18:51:47 +00002Test SBProcess APIs, including ReadMemory(), WriteMemory(), and others.
Johnny Chen37f99fd2011-03-01 02:20:14 +00003"""
4
Zachary Turner35d017f2015-10-23 17:04:29 +00005from __future__ import print_function
6
Zachary Turner77db4a82015-10-22 20:06:20 +00007import lldb_shared
8
Johnny Chen37f99fd2011-03-01 02:20:14 +00009import os, time
Johnny Chen37f99fd2011-03-01 02:20:14 +000010import lldb
Johnny Chende90f1d2011-04-27 17:43:07 +000011from lldbutil import get_stopped_thread, state_type_to_str
Johnny Chen37f99fd2011-03-01 02:20:14 +000012from lldbtest import *
13
Johnny Chen37f99fd2011-03-01 02:20:14 +000014class ProcessAPITestCase(TestBase):
15
Greg Clayton4570d3e2013-12-10 23:19:29 +000016 mydir = TestBase.compute_mydir(__file__)
Johnny Chen37f99fd2011-03-01 02:20:14 +000017
Johnny Chen37f99fd2011-03-01 02:20:14 +000018 def setUp(self):
19 # Call super's setUp().
20 TestBase.setUp(self)
21 # Find the line number to break inside main().
22 self.line = line_number("main.cpp", "// Set break point at this line and check variable 'my_char'.")
23
Pavel Labathdc8b2d32015-10-26 09:28:32 +000024 @add_test_categories(['pyapi'])
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000025 def test_read_memory(self):
Johnny Chen90aa5942011-03-01 18:51:47 +000026 """Test Python SBProcess.ReadMemory() API."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000027 self.build()
Johnny Chen37f99fd2011-03-01 02:20:14 +000028 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chen37f99fd2011-03-01 02:20:14 +000029
30 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000031 self.assertTrue(target, VALID_TARGET)
Johnny Chen37f99fd2011-03-01 02:20:14 +000032
33 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000034 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen37f99fd2011-03-01 02:20:14 +000035
36 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +000037 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen37f99fd2011-03-01 02:20:14 +000038
Johnny Chen5a0bee72011-06-15 22:14:12 +000039 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +000040 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
Johnny Chend61816b2011-03-03 01:41:57 +000041 frame = thread.GetFrameAtIndex(0)
Johnny Chen37f99fd2011-03-01 02:20:14 +000042
43 # Get the SBValue for the global variable 'my_char'.
44 val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal)
Johnny Chen9a07aba2011-07-11 20:06:28 +000045 self.DebugSBValue(val)
Johnny Chen37f99fd2011-03-01 02:20:14 +000046
47 # Due to the typemap magic (see lldb.swig), we pass in 1 to ReadMemory and
48 # expect to get a Python string as the result object!
Johnny Chen1d3e8802011-07-11 23:38:23 +000049 error = lldb.SBError()
Johnny Chen7cc3d312011-12-16 01:56:27 +000050 self.assertFalse(val.TypeIsPointerType())
51 content = process.ReadMemory(val.AddressOf().GetValueAsUnsigned(), 1, error)
Johnny Chen90aa5942011-03-01 18:51:47 +000052 if not error.Success():
53 self.fail("SBProcess.ReadMemory() failed")
Johnny Chen90da3cc2011-04-19 19:49:09 +000054 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +000055 print("memory content:", content)
Johnny Chen37f99fd2011-03-01 02:20:14 +000056
57 self.expect(content, "Result from SBProcess.ReadMemory() matches our expected output: 'x'",
58 exe=False,
59 startstr = 'x')
60
Johnny Chene7e8af82011-12-16 00:25:30 +000061 # Read (char *)my_char_ptr.
62 val = frame.FindValue("my_char_ptr", lldb.eValueTypeVariableGlobal)
63 self.DebugSBValue(val)
64 cstring = process.ReadCStringFromMemory(val.GetValueAsUnsigned(), 256, error)
65 if not error.Success():
66 self.fail("SBProcess.ReadCStringFromMemory() failed")
67 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +000068 print("cstring read is:", cstring)
Johnny Chene7e8af82011-12-16 00:25:30 +000069
70 self.expect(cstring, "Result from SBProcess.ReadCStringFromMemory() matches our expected output",
71 exe=False,
72 startstr = 'Does it work?')
73
Johnny Chen6e55cd52011-12-15 23:30:05 +000074 # Get the SBValue for the global variable 'my_cstring'.
75 val = frame.FindValue("my_cstring", lldb.eValueTypeVariableGlobal)
76 self.DebugSBValue(val)
77
Johnny Chen6e55cd52011-12-15 23:30:05 +000078 # Due to the typemap magic (see lldb.swig), we pass in 256 to read at most 256 bytes
79 # from the address, and expect to get a Python string as the result object!
Johnny Chen7cc3d312011-12-16 01:56:27 +000080 self.assertFalse(val.TypeIsPointerType())
81 cstring = process.ReadCStringFromMemory(val.AddressOf().GetValueAsUnsigned(), 256, error)
Johnny Chen6e55cd52011-12-15 23:30:05 +000082 if not error.Success():
83 self.fail("SBProcess.ReadCStringFromMemory() failed")
84 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +000085 print("cstring read is:", cstring)
Johnny Chen6e55cd52011-12-15 23:30:05 +000086
87 self.expect(cstring, "Result from SBProcess.ReadCStringFromMemory() matches our expected output",
88 exe=False,
89 startstr = 'lldb.SBProcess.ReadCStringFromMemory() works!')
90
91 # Get the SBValue for the global variable 'my_uint32'.
92 val = frame.FindValue("my_uint32", lldb.eValueTypeVariableGlobal)
93 self.DebugSBValue(val)
94
Johnny Chen7cc3d312011-12-16 01:56:27 +000095 # Due to the typemap magic (see lldb.swig), we pass in 4 to read 4 bytes
Johnny Chen6e55cd52011-12-15 23:30:05 +000096 # from the address, and expect to get an int as the result!
Johnny Chen7cc3d312011-12-16 01:56:27 +000097 self.assertFalse(val.TypeIsPointerType())
98 my_uint32 = process.ReadUnsignedFromMemory(val.AddressOf().GetValueAsUnsigned(), 4, error)
Johnny Chen6e55cd52011-12-15 23:30:05 +000099 if not error.Success():
100 self.fail("SBProcess.ReadCStringFromMemory() failed")
101 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +0000102 print("uint32 read is:", my_uint32)
Johnny Chen6e55cd52011-12-15 23:30:05 +0000103
104 if my_uint32 != 12345:
105 self.fail("Result from SBProcess.ReadUnsignedFromMemory() does not match our expected output")
106
Pavel Labathdc8b2d32015-10-26 09:28:32 +0000107 @add_test_categories(['pyapi'])
Tamas Berghammerc8fd1302015-09-30 10:12:40 +0000108 def test_write_memory(self):
Johnny Chen90aa5942011-03-01 18:51:47 +0000109 """Test Python SBProcess.WriteMemory() API."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +0000110 self.build()
Johnny Chen90aa5942011-03-01 18:51:47 +0000111 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chen37f99fd2011-03-01 02:20:14 +0000112
Johnny Chen90aa5942011-03-01 18:51:47 +0000113 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000114 self.assertTrue(target, VALID_TARGET)
Johnny Chen37f99fd2011-03-01 02:20:14 +0000115
Johnny Chen90aa5942011-03-01 18:51:47 +0000116 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000117 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen37f99fd2011-03-01 02:20:14 +0000118
Johnny Chen90aa5942011-03-01 18:51:47 +0000119 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000120 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen37f99fd2011-03-01 02:20:14 +0000121
Johnny Chen5a0bee72011-06-15 22:14:12 +0000122 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +0000123 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
Johnny Chend61816b2011-03-03 01:41:57 +0000124 frame = thread.GetFrameAtIndex(0)
Johnny Chen90aa5942011-03-01 18:51:47 +0000125
126 # Get the SBValue for the global variable 'my_char'.
127 val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000128 self.DebugSBValue(val)
Johnny Chen90aa5942011-03-01 18:51:47 +0000129
130 # If the variable does not have a load address, there's no sense continuing.
Greg Claytonfe42ac42011-08-03 22:57:10 +0000131 if not val.GetLocation().startswith("0x"):
Johnny Chen90aa5942011-03-01 18:51:47 +0000132 return
133
134 # OK, let's get the hex location of the variable.
Greg Claytonfe42ac42011-08-03 22:57:10 +0000135 location = int(val.GetLocation(), 16)
Johnny Chen90aa5942011-03-01 18:51:47 +0000136
137 # The program logic makes the 'my_char' variable to have memory content as 'x'.
138 # But we want to use the WriteMemory() API to assign 'a' to the variable.
139
140 # Now use WriteMemory() API to write 'a' into the global variable.
Johnny Chen1d3e8802011-07-11 23:38:23 +0000141 error = lldb.SBError()
Johnny Chen5a0bee72011-06-15 22:14:12 +0000142 result = process.WriteMemory(location, 'a', error)
Johnny Chen90aa5942011-03-01 18:51:47 +0000143 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!
Johnny Chen5a0bee72011-06-15 22:14:12 +0000149 content = process.ReadMemory(location, 1, error)
Johnny Chen90aa5942011-03-01 18:51:47 +0000150 if not error.Success():
151 self.fail("SBProcess.ReadMemory() failed")
Johnny Chen90da3cc2011-04-19 19:49:09 +0000152 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +0000153 print("memory content:", content)
Johnny Chen90aa5942011-03-01 18:51:47 +0000154
155 self.expect(content, "Result from SBProcess.ReadMemory() matches our expected output: 'a'",
156 exe=False,
157 startstr = 'a')
Johnny Chen37f99fd2011-03-01 02:20:14 +0000158
Pavel Labathdc8b2d32015-10-26 09:28:32 +0000159 @add_test_categories(['pyapi'])
Tamas Berghammerc8fd1302015-09-30 10:12:40 +0000160 def test_access_my_int(self):
Johnny Chencf386e22011-03-01 22:56:31 +0000161 """Test access 'my_int' using Python SBProcess.GetByteOrder() and other APIs."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +0000162 self.build()
Johnny Chencf386e22011-03-01 22:56:31 +0000163 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chencf386e22011-03-01 22:56:31 +0000164
165 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000166 self.assertTrue(target, VALID_TARGET)
Johnny Chencf386e22011-03-01 22:56:31 +0000167
168 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000169 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chencf386e22011-03-01 22:56:31 +0000170
171 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000172 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chencf386e22011-03-01 22:56:31 +0000173
Johnny Chen5a0bee72011-06-15 22:14:12 +0000174 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +0000175 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
Johnny Chend61816b2011-03-03 01:41:57 +0000176 frame = thread.GetFrameAtIndex(0)
Johnny Chencf386e22011-03-01 22:56:31 +0000177
178 # Get the SBValue for the global variable 'my_int'.
179 val = frame.FindValue("my_int", lldb.eValueTypeVariableGlobal)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000180 self.DebugSBValue(val)
Johnny Chencf386e22011-03-01 22:56:31 +0000181
182 # If the variable does not have a load address, there's no sense continuing.
Greg Claytonfe42ac42011-08-03 22:57:10 +0000183 if not val.GetLocation().startswith("0x"):
Johnny Chencf386e22011-03-01 22:56:31 +0000184 return
185
186 # OK, let's get the hex location of the variable.
Greg Claytonfe42ac42011-08-03 22:57:10 +0000187 location = int(val.GetLocation(), 16)
Johnny Chencf386e22011-03-01 22:56:31 +0000188
Johnny Chen4e90a7e2011-03-02 19:49:27 +0000189 # Note that the canonical from of the bytearray is little endian.
Johnny Chen43766d62011-03-02 01:36:45 +0000190 from lldbutil import int_to_bytearray, bytearray_to_int
Johnny Chen4e90a7e2011-03-02 19:49:27 +0000191
Johnny Chencf386e22011-03-01 22:56:31 +0000192 byteSize = val.GetByteSize()
Johnny Chen43766d62011-03-02 01:36:45 +0000193 bytes = int_to_bytearray(256, byteSize)
Johnny Chencf386e22011-03-01 22:56:31 +0000194
Johnny Chen5a0bee72011-06-15 22:14:12 +0000195 byteOrder = process.GetByteOrder()
Johnny Chencf386e22011-03-01 22:56:31 +0000196 if byteOrder == lldb.eByteOrderBig:
Johnny Chen43766d62011-03-02 01:36:45 +0000197 bytes.reverse()
Johnny Chencf386e22011-03-01 22:56:31 +0000198 elif byteOrder == lldb.eByteOrderLittle:
Johnny Chen43766d62011-03-02 01:36:45 +0000199 pass
Johnny Chencf386e22011-03-01 22:56:31 +0000200 else:
201 # Neither big endian nor little endian? Return for now.
Johnny Chen43766d62011-03-02 01:36:45 +0000202 # Add more logic here if we want to handle other types.
Johnny Chencf386e22011-03-01 22:56:31 +0000203 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)
Johnny Chen1d3e8802011-07-11 23:38:23 +0000210 error = lldb.SBError()
Johnny Chen5a0bee72011-06-15 22:14:12 +0000211 result = process.WriteMemory(location, new_value, error)
Johnny Chencf386e22011-03-01 22:56:31 +0000212 if not error.Success() or result != byteSize:
213 self.fail("SBProcess.WriteMemory() failed")
214
Jim Ingham78a685a2011-04-16 00:01:13 +0000215 # Make sure that the val we got originally updates itself to notice the change:
Greg Claytonfe42ac42011-08-03 22:57:10 +0000216 self.expect(val.GetValue(),
Jim Ingham78a685a2011-04-16 00:01:13 +0000217 "SBProcess.ReadMemory() successfully writes (int)256 to the memory location for 'my_int'",
218 exe=False,
219 startstr = '256')
220
221 # And for grins, get the SBValue for the global variable 'my_int' again, to make sure that also tracks the new value:
Johnny Chencf386e22011-03-01 22:56:31 +0000222 val = frame.FindValue("my_int", lldb.eValueTypeVariableGlobal)
Greg Claytonfe42ac42011-08-03 22:57:10 +0000223 self.expect(val.GetValue(),
Johnny Chencf386e22011-03-01 22:56:31 +0000224 "SBProcess.ReadMemory() successfully writes (int)256 to the memory location for 'my_int'",
225 exe=False,
226 startstr = '256')
227
228 # Now read the memory content. The bytearray should have (byte)1 as the second element.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000229 content = process.ReadMemory(location, byteSize, error)
Johnny Chencf386e22011-03-01 22:56:31 +0000230 if not error.Success():
231 self.fail("SBProcess.ReadMemory() failed")
Johnny Chen4e90a7e2011-03-02 19:49:27 +0000232
233 # Use "ascii" as the encoding because each element of 'content' is in the range [0..255].
Johnny Chencf386e22011-03-01 22:56:31 +0000234 new_bytes = bytearray(content, "ascii")
Johnny Chen43766d62011-03-02 01:36:45 +0000235
236 # The bytearray_to_int utility function expects a little endian bytearray.
Johnny Chencf386e22011-03-01 22:56:31 +0000237 if byteOrder == lldb.eByteOrderBig:
Johnny Chen43766d62011-03-02 01:36:45 +0000238 new_bytes.reverse()
239
240 new_value = bytearray_to_int(new_bytes, byteSize)
241 if new_value != 256:
242 self.fail("Memory content read from 'my_int' does not match (int)256")
Johnny Chencf386e22011-03-01 22:56:31 +0000243
244 # Dump the memory content....
Johnny Chen90da3cc2011-04-19 19:49:09 +0000245 if self.TraceOn():
246 for i in new_bytes:
Zachary Turner35d017f2015-10-23 17:04:29 +0000247 print("byte:", i)
Johnny Chencf386e22011-03-01 22:56:31 +0000248
Pavel Labathdc8b2d32015-10-26 09:28:32 +0000249 @add_test_categories(['pyapi'])
Tamas Berghammerc8fd1302015-09-30 10:12:40 +0000250 def test_remote_launch(self):
Johnny Chen930e3ad2011-03-05 01:20:11 +0000251 """Test SBProcess.RemoteLaunch() API with a process not in eStateConnected, and it should fail."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +0000252 self.build()
Johnny Chen930e3ad2011-03-05 01:20:11 +0000253 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chen930e3ad2011-03-05 01:20:11 +0000254
255 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000256 self.assertTrue(target, VALID_TARGET)
Johnny Chen930e3ad2011-03-05 01:20:11 +0000257
258 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000259 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen930e3ad2011-03-05 01:20:11 +0000260
Johnny Chen90da3cc2011-04-19 19:49:09 +0000261 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +0000262 print("process state:", state_type_to_str(process.GetState()))
Johnny Chen930e3ad2011-03-05 01:20:11 +0000263 self.assertTrue(process.GetState() != lldb.eStateConnected)
264
Johnny Chen1d3e8802011-07-11 23:38:23 +0000265 error = lldb.SBError()
Johnny Chen930e3ad2011-03-05 01:20:11 +0000266 success = process.RemoteLaunch(None, None, None, None, None, None, 0, False, error)
267 self.assertTrue(not success, "RemoteLaunch() should fail for process state != eStateConnected")
268
Pavel Labathdc8b2d32015-10-26 09:28:32 +0000269 @add_test_categories(['pyapi'])
Tamas Berghammerc8fd1302015-09-30 10:12:40 +0000270 def test_get_num_supported_hardware_watchpoints(self):
Johnny Chenf9ef60d2012-05-23 22:34:34 +0000271 """Test SBProcess.GetNumSupportedHardwareWatchpoints() API with a process."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +0000272 self.build()
Johnny Chenf9ef60d2012-05-23 22:34:34 +0000273 exe = os.path.join(os.getcwd(), "a.out")
274 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
275
276 target = self.dbg.CreateTarget(exe)
277 self.assertTrue(target, VALID_TARGET)
278
279 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
280 self.assertTrue(breakpoint, VALID_BREAKPOINT)
281
282 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000283 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chenf9ef60d2012-05-23 22:34:34 +0000284
285 error = lldb.SBError();
286 num = process.GetNumSupportedHardwareWatchpoints(error)
287 if self.TraceOn() and error.Success():
Zachary Turner35d017f2015-10-23 17:04:29 +0000288 print("Number of supported hardware watchpoints: %d" % num)
289