blob: a9bdec546c81cb0f6f949a2eeac337417f4569d6 [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
5import os, time
6import unittest2
7import lldb
Johnny Chende90f1d2011-04-27 17:43:07 +00008from lldbutil import get_stopped_thread, state_type_to_str
Johnny Chen37f99fd2011-03-01 02:20:14 +00009from lldbtest import *
10
Johnny Chen37f99fd2011-03-01 02:20:14 +000011class ProcessAPITestCase(TestBase):
12
13 mydir = os.path.join("python_api", "process")
14
Johnny Chen90aa5942011-03-01 18:51:47 +000015 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chen37f99fd2011-03-01 02:20:14 +000016 @python_api_test
Johnny Chen90aa5942011-03-01 18:51:47 +000017 def test_read_memory_with_dsym(self):
18 """Test Python SBProcess.ReadMemory() API."""
Johnny Chen37f99fd2011-03-01 02:20:14 +000019 self.buildDsym()
Johnny Chen90aa5942011-03-01 18:51:47 +000020 self.read_memory()
Johnny Chen37f99fd2011-03-01 02:20:14 +000021
22 @python_api_test
Johnny Chen90aa5942011-03-01 18:51:47 +000023 def test_read_memory_with_dwarf(self):
24 """Test Python SBProcess.ReadMemory() API."""
Johnny Chen37f99fd2011-03-01 02:20:14 +000025 self.buildDwarf()
Johnny Chen90aa5942011-03-01 18:51:47 +000026 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 Chen37f99fd2011-03-01 02:20:14 +000040
Johnny Chencf386e22011-03-01 22:56:31 +000041 @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 Chen930e3ad2011-03-05 01:20:11 +000054 @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 Chen37f99fd2011-03-01 02:20:14 +000060 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 Chen90aa5942011-03-01 18:51:47 +000066 def read_memory(self):
67 """Test Python SBProcess.ReadMemory() API."""
Johnny Chen37f99fd2011-03-01 02:20:14 +000068 exe = os.path.join(os.getcwd(), "a.out")
69 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
70
71 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000072 self.assertTrue(target, VALID_TARGET)
Johnny Chen37f99fd2011-03-01 02:20:14 +000073
74 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000075 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen37f99fd2011-03-01 02:20:14 +000076
77 # Launch the process, and do not stop at the entry point.
78 error = lldb.SBError()
Johnny Chen5a0bee72011-06-15 22:14:12 +000079 process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error)
Johnny Chen37f99fd2011-03-01 02:20:14 +000080
Johnny Chen5a0bee72011-06-15 22:14:12 +000081 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chend61816b2011-03-03 01:41:57 +000082 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint")
83 frame = thread.GetFrameAtIndex(0)
Johnny Chen37f99fd2011-03-01 02:20:14 +000084
85 # Get the SBValue for the global variable 'my_char'.
86 val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal)
Johnny Chen37f99fd2011-03-01 02:20:14 +000087 self.DebugSBValue(frame, val)
88
Johnny Chen90aa5942011-03-01 18:51:47 +000089 # 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 Chen37f99fd2011-03-01 02:20:14 +000096 # 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!
Johnny Chen5a0bee72011-06-15 22:14:12 +000098 content = process.ReadMemory(location, 1, error)
Johnny Chen90aa5942011-03-01 18:51:47 +000099 if not error.Success():
100 self.fail("SBProcess.ReadMemory() failed")
Johnny Chen90da3cc2011-04-19 19:49:09 +0000101 if self.TraceOn():
102 print "memory content:", content
Johnny Chen37f99fd2011-03-01 02:20:14 +0000103
104 self.expect(content, "Result from SBProcess.ReadMemory() matches our expected output: 'x'",
105 exe=False,
106 startstr = 'x')
107
Johnny Chen90aa5942011-03-01 18:51:47 +0000108 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 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.
120 error = lldb.SBError()
Johnny Chen5a0bee72011-06-15 22:14:12 +0000121 process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error)
Johnny Chen37f99fd2011-03-01 02:20:14 +0000122
Johnny Chen5a0bee72011-06-15 22:14:12 +0000123 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chend61816b2011-03-03 01:41:57 +0000124 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint")
125 frame = thread.GetFrameAtIndex(0)
Johnny Chen90aa5942011-03-01 18:51:47 +0000126
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.
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():
153 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
Johnny Chencf386e22011-03-01 22:56:31 +0000159 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)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000165 self.assertTrue(target, VALID_TARGET)
Johnny Chencf386e22011-03-01 22:56:31 +0000166
167 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000168 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chencf386e22011-03-01 22:56:31 +0000169
170 # Launch the process, and do not stop at the entry point.
171 error = lldb.SBError()
Johnny Chen5a0bee72011-06-15 22:14:12 +0000172 process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error)
Johnny Chencf386e22011-03-01 22:56:31 +0000173
Johnny Chen5a0bee72011-06-15 22:14:12 +0000174 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chend61816b2011-03-03 01:41:57 +0000175 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint")
176 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)
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 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 Chen5a0bee72011-06-15 22:14:12 +0000210 result = process.WriteMemory(location, new_value, error)
Johnny Chencf386e22011-03-01 22:56:31 +0000211 if not error.Success() or result != byteSize:
212 self.fail("SBProcess.WriteMemory() failed")
213
Jim Ingham78a685a2011-04-16 00:01:13 +0000214 # 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 Chencf386e22011-03-01 22:56:31 +0000221 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.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000228 content = process.ReadMemory(location, byteSize, error)
Johnny Chencf386e22011-03-01 22:56:31 +0000229 if not error.Success():
230 self.fail("SBProcess.ReadMemory() failed")
Johnny Chen4e90a7e2011-03-02 19:49:27 +0000231
232 # Use "ascii" as the encoding because each element of 'content' is in the range [0..255].
Johnny Chencf386e22011-03-01 22:56:31 +0000233 new_bytes = bytearray(content, "ascii")
Johnny Chen43766d62011-03-02 01:36:45 +0000234
235 # The bytearray_to_int utility function expects a little endian bytearray.
Johnny Chencf386e22011-03-01 22:56:31 +0000236 if byteOrder == lldb.eByteOrderBig:
Johnny Chen43766d62011-03-02 01:36:45 +0000237 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 Chencf386e22011-03-01 22:56:31 +0000242
243 # Dump the memory content....
Johnny Chen90da3cc2011-04-19 19:49:09 +0000244 if self.TraceOn():
245 for i in new_bytes:
246 print "byte:", i
Johnny Chencf386e22011-03-01 22:56:31 +0000247
Johnny Chen930e3ad2011-03-05 01:20:11 +0000248 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)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000254 self.assertTrue(target, VALID_TARGET)
Johnny Chen930e3ad2011-03-05 01:20:11 +0000255
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 Chen90da3cc2011-04-19 19:49:09 +0000260 if self.TraceOn():
Johnny Chende90f1d2011-04-27 17:43:07 +0000261 print "process state:", state_type_to_str(process.GetState())
Johnny Chen930e3ad2011-03-05 01:20:11 +0000262 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 Chen37f99fd2011-03-01 02:20:14 +0000267
268if __name__ == '__main__':
269 import atexit
270 lldb.SBDebugger.Initialize()
271 atexit.register(lambda: lldb.SBDebugger.Terminate())
272 unittest2.main()