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 | |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 5 | from __future__ import print_function |
| 6 | |
Zachary Turner | 77db4a8 | 2015-10-22 20:06:20 +0000 | [diff] [blame] | 7 | import lldb_shared |
| 8 | |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 9 | import os, time |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 10 | import lldb |
Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 11 | from lldbutil import get_stopped_thread, state_type_to_str |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 12 | from lldbtest import * |
| 13 | |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 14 | class ProcessAPITestCase(TestBase): |
| 15 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 16 | mydir = TestBase.compute_mydir(__file__) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 17 | |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 18 | 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 Labath | dc8b2d3 | 2015-10-26 09:28:32 +0000 | [diff] [blame] | 24 | @add_test_categories(['pyapi']) |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 25 | def test_read_memory(self): |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 26 | """Test Python SBProcess.ReadMemory() API.""" |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 27 | self.build() |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 28 | exe = os.path.join(os.getcwd(), "a.out") |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 29 | |
| 30 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 31 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 32 | |
| 33 | breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 34 | self.assertTrue(breakpoint, VALID_BREAKPOINT) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 35 | |
| 36 | # Launch the process, and do not stop at the entry point. |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 37 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 38 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 39 | thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Greg Clayton | 53c5ddf | 2013-03-19 17:59:30 +0000 | [diff] [blame] | 40 | self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint") |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 41 | frame = thread.GetFrameAtIndex(0) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 42 | |
| 43 | # Get the SBValue for the global variable 'my_char'. |
| 44 | val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal) |
Johnny Chen | 9a07aba | 2011-07-11 20:06:28 +0000 | [diff] [blame] | 45 | self.DebugSBValue(val) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 46 | |
| 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 Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 49 | error = lldb.SBError() |
Johnny Chen | 7cc3d31 | 2011-12-16 01:56:27 +0000 | [diff] [blame] | 50 | self.assertFalse(val.TypeIsPointerType()) |
| 51 | content = process.ReadMemory(val.AddressOf().GetValueAsUnsigned(), 1, error) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 52 | if not error.Success(): |
| 53 | self.fail("SBProcess.ReadMemory() failed") |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 54 | if self.TraceOn(): |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 55 | print("memory content:", content) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 56 | |
| 57 | self.expect(content, "Result from SBProcess.ReadMemory() matches our expected output: 'x'", |
| 58 | exe=False, |
| 59 | startstr = 'x') |
| 60 | |
Johnny Chen | e7e8af8 | 2011-12-16 00:25:30 +0000 | [diff] [blame] | 61 | # 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 Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 68 | print("cstring read is:", cstring) |
Johnny Chen | e7e8af8 | 2011-12-16 00:25:30 +0000 | [diff] [blame] | 69 | |
| 70 | self.expect(cstring, "Result from SBProcess.ReadCStringFromMemory() matches our expected output", |
| 71 | exe=False, |
| 72 | startstr = 'Does it work?') |
| 73 | |
Johnny Chen | 6e55cd5 | 2011-12-15 23:30:05 +0000 | [diff] [blame] | 74 | # Get the SBValue for the global variable 'my_cstring'. |
| 75 | val = frame.FindValue("my_cstring", lldb.eValueTypeVariableGlobal) |
| 76 | self.DebugSBValue(val) |
| 77 | |
Johnny Chen | 6e55cd5 | 2011-12-15 23:30:05 +0000 | [diff] [blame] | 78 | # 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 Chen | 7cc3d31 | 2011-12-16 01:56:27 +0000 | [diff] [blame] | 80 | self.assertFalse(val.TypeIsPointerType()) |
| 81 | cstring = process.ReadCStringFromMemory(val.AddressOf().GetValueAsUnsigned(), 256, error) |
Johnny Chen | 6e55cd5 | 2011-12-15 23:30:05 +0000 | [diff] [blame] | 82 | if not error.Success(): |
| 83 | self.fail("SBProcess.ReadCStringFromMemory() failed") |
| 84 | if self.TraceOn(): |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 85 | print("cstring read is:", cstring) |
Johnny Chen | 6e55cd5 | 2011-12-15 23:30:05 +0000 | [diff] [blame] | 86 | |
| 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 Chen | 7cc3d31 | 2011-12-16 01:56:27 +0000 | [diff] [blame] | 95 | # Due to the typemap magic (see lldb.swig), we pass in 4 to read 4 bytes |
Johnny Chen | 6e55cd5 | 2011-12-15 23:30:05 +0000 | [diff] [blame] | 96 | # from the address, and expect to get an int as the result! |
Johnny Chen | 7cc3d31 | 2011-12-16 01:56:27 +0000 | [diff] [blame] | 97 | self.assertFalse(val.TypeIsPointerType()) |
| 98 | my_uint32 = process.ReadUnsignedFromMemory(val.AddressOf().GetValueAsUnsigned(), 4, error) |
Johnny Chen | 6e55cd5 | 2011-12-15 23:30:05 +0000 | [diff] [blame] | 99 | if not error.Success(): |
| 100 | self.fail("SBProcess.ReadCStringFromMemory() failed") |
| 101 | if self.TraceOn(): |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 102 | print("uint32 read is:", my_uint32) |
Johnny Chen | 6e55cd5 | 2011-12-15 23:30:05 +0000 | [diff] [blame] | 103 | |
| 104 | if my_uint32 != 12345: |
| 105 | self.fail("Result from SBProcess.ReadUnsignedFromMemory() does not match our expected output") |
| 106 | |
Pavel Labath | dc8b2d3 | 2015-10-26 09:28:32 +0000 | [diff] [blame] | 107 | @add_test_categories(['pyapi']) |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 108 | def test_write_memory(self): |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 109 | """Test Python SBProcess.WriteMemory() API.""" |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 110 | self.build() |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 111 | exe = os.path.join(os.getcwd(), "a.out") |
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) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 114 | self.assertTrue(target, 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) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 117 | self.assertTrue(breakpoint, 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. |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 120 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
Johnny Chen | 37f99fd | 2011-03-01 02:20:14 +0000 | [diff] [blame] | 121 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 122 | thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Greg Clayton | 53c5ddf | 2013-03-19 17:59:30 +0000 | [diff] [blame] | 123 | self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint") |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 124 | frame = thread.GetFrameAtIndex(0) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 125 | |
| 126 | # Get the SBValue for the global variable 'my_char'. |
| 127 | val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal) |
Johnny Chen | 9a07aba | 2011-07-11 20:06:28 +0000 | [diff] [blame] | 128 | self.DebugSBValue(val) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 129 | |
| 130 | # 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] | 131 | if not val.GetLocation().startswith("0x"): |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 132 | return |
| 133 | |
| 134 | # OK, let's get the hex location of the variable. |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 135 | location = int(val.GetLocation(), 16) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 136 | |
| 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 Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 141 | error = lldb.SBError() |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 142 | result = process.WriteMemory(location, 'a', error) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 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! |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 149 | content = process.ReadMemory(location, 1, error) |
Johnny Chen | 90aa594 | 2011-03-01 18:51:47 +0000 | [diff] [blame] | 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(): |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 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 | |
Pavel Labath | dc8b2d3 | 2015-10-26 09:28:32 +0000 | [diff] [blame] | 159 | @add_test_categories(['pyapi']) |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 160 | def test_access_my_int(self): |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 161 | """Test access 'my_int' using Python SBProcess.GetByteOrder() and other APIs.""" |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 162 | self.build() |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 163 | exe = os.path.join(os.getcwd(), "a.out") |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 164 | |
| 165 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 166 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 167 | |
| 168 | breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 169 | self.assertTrue(breakpoint, VALID_BREAKPOINT) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 170 | |
| 171 | # Launch the process, and do not stop at the entry point. |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 172 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 173 | |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 174 | thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Greg Clayton | 53c5ddf | 2013-03-19 17:59:30 +0000 | [diff] [blame] | 175 | self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint") |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 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) |
Johnny Chen | 9a07aba | 2011-07-11 20:06:28 +0000 | [diff] [blame] | 180 | self.DebugSBValue(val) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 181 | |
| 182 | # 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] | 183 | if not val.GetLocation().startswith("0x"): |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 184 | return |
| 185 | |
| 186 | # OK, let's get the hex location of the variable. |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 187 | location = int(val.GetLocation(), 16) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 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 | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 195 | byteOrder = 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) |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 210 | error = lldb.SBError() |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 211 | result = process.WriteMemory(location, new_value, error) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 212 | if not error.Success() or result != byteSize: |
| 213 | self.fail("SBProcess.WriteMemory() failed") |
| 214 | |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 215 | # 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] | 216 | self.expect(val.GetValue(), |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 217 | "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 Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 222 | val = frame.FindValue("my_int", lldb.eValueTypeVariableGlobal) |
Greg Clayton | fe42ac4 | 2011-08-03 22:57:10 +0000 | [diff] [blame] | 223 | self.expect(val.GetValue(), |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 224 | "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 Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 229 | content = process.ReadMemory(location, byteSize, error) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 230 | if not error.Success(): |
| 231 | self.fail("SBProcess.ReadMemory() failed") |
Johnny Chen | 4e90a7e | 2011-03-02 19:49:27 +0000 | [diff] [blame] | 232 | |
| 233 | # 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] | 234 | new_bytes = bytearray(content, "ascii") |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 235 | |
| 236 | # The bytearray_to_int utility function expects a little endian bytearray. |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 237 | if byteOrder == lldb.eByteOrderBig: |
Johnny Chen | 43766d6 | 2011-03-02 01:36:45 +0000 | [diff] [blame] | 238 | 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 Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 243 | |
| 244 | # Dump the memory content.... |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 245 | if self.TraceOn(): |
| 246 | for i in new_bytes: |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 247 | print("byte:", i) |
Johnny Chen | cf386e2 | 2011-03-01 22:56:31 +0000 | [diff] [blame] | 248 | |
Pavel Labath | dc8b2d3 | 2015-10-26 09:28:32 +0000 | [diff] [blame] | 249 | @add_test_categories(['pyapi']) |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 250 | def test_remote_launch(self): |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 251 | """Test SBProcess.RemoteLaunch() API with a process not in eStateConnected, and it should fail.""" |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 252 | self.build() |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 253 | exe = os.path.join(os.getcwd(), "a.out") |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 254 | |
| 255 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 256 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 257 | |
| 258 | # Launch the process, and do not stop at the entry point. |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 259 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 260 | |
Johnny Chen | 90da3cc | 2011-04-19 19:49:09 +0000 | [diff] [blame] | 261 | if self.TraceOn(): |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 262 | print("process state:", state_type_to_str(process.GetState())) |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 263 | self.assertTrue(process.GetState() != lldb.eStateConnected) |
| 264 | |
Johnny Chen | 1d3e880 | 2011-07-11 23:38:23 +0000 | [diff] [blame] | 265 | error = lldb.SBError() |
Johnny Chen | 930e3ad | 2011-03-05 01:20:11 +0000 | [diff] [blame] | 266 | 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 Labath | dc8b2d3 | 2015-10-26 09:28:32 +0000 | [diff] [blame] | 269 | @add_test_categories(['pyapi']) |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 270 | def test_get_num_supported_hardware_watchpoints(self): |
Johnny Chen | f9ef60d | 2012-05-23 22:34:34 +0000 | [diff] [blame] | 271 | """Test SBProcess.GetNumSupportedHardwareWatchpoints() API with a process.""" |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 272 | self.build() |
Johnny Chen | f9ef60d | 2012-05-23 22:34:34 +0000 | [diff] [blame] | 273 | 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 Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 283 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
Johnny Chen | f9ef60d | 2012-05-23 22:34:34 +0000 | [diff] [blame] | 284 | |
| 285 | error = lldb.SBError(); |
| 286 | num = process.GetNumSupportedHardwareWatchpoints(error) |
| 287 | if self.TraceOn() and error.Success(): |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 288 | print("Number of supported hardware watchpoints: %d" % num) |
| 289 | |