From 90aa594c5e2138dbaa330dfc08cc21218cda5af4 Mon Sep 17 00:00:00 2001 From: Johnny Chen Date: Tue, 1 Mar 2011 18:51:47 +0000 Subject: Add test methods for SBProcess.WriteMemory() API to the TestProcessAPI.py file. This makes the number of total tests equal to 201. llvm-svn: 126769 --- lldb/test/python_api/process/TestProcessAPI.py | 121 ++++++++++++++++--------- 1 file changed, 80 insertions(+), 41 deletions(-) (limited to 'lldb/test/python_api/process/TestProcessAPI.py') diff --git a/lldb/test/python_api/process/TestProcessAPI.py b/lldb/test/python_api/process/TestProcessAPI.py index be3c9669d83..48f530c67f1 100644 --- a/lldb/test/python_api/process/TestProcessAPI.py +++ b/lldb/test/python_api/process/TestProcessAPI.py @@ -1,5 +1,5 @@ """ -Test symbol table access for main.m. +Test SBProcess APIs, including ReadMemory(), WriteMemory(), and others. """ import os, time @@ -12,21 +12,31 @@ class ProcessAPITestCase(TestBase): mydir = os.path.join("python_api", "process") - symbols_list = ['main', - 'my_char' - ] + @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") + @python_api_test + def test_read_memory_with_dsym(self): + """Test Python SBProcess.ReadMemory() API.""" + self.buildDsym() + self.read_memory() + + @python_api_test + def test_read_memory_with_dwarf(self): + """Test Python SBProcess.ReadMemory() API.""" + self.buildDwarf() + self.read_memory() + @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") @python_api_test - def test_with_dsym_and_python_api(self): - """Test Python process APIs.""" + def test_write_memory_with_dsym(self): + """Test Python SBProcess.WriteMemory() API.""" self.buildDsym() - self.process_api() + self.write_memory() @python_api_test - def test_with_dwarf_and_python_api(self): - """Test Python process APIs.""" + def test_write_memory_with_dwarf(self): + """Test Python SBProcess.WriteMemory() API.""" self.buildDwarf() - self.process_api() + self.write_memory() def setUp(self): # Call super's setUp(). @@ -34,8 +44,8 @@ class ProcessAPITestCase(TestBase): # Find the line number to break inside main(). self.line = line_number("main.cpp", "// Set break point at this line and check variable 'my_char'.") - def process_api(self): - """Test Python process APIs.""" + def read_memory(self): + """Test Python SBProcess.ReadMemory() API.""" exe = os.path.join(os.getcwd(), "a.out") self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) @@ -54,45 +64,74 @@ class ProcessAPITestCase(TestBase): # Get the SBValue for the global variable 'my_char'. val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal) - location = int(val.GetLocation(frame), 16) self.DebugSBValue(frame, val) + # If the variable does not have a load address, there's no sense continuing. + if not val.GetLocation(frame).startswith("0x"): + return + + # OK, let's get the hex location of the variable. + location = int(val.GetLocation(frame), 16) + # Due to the typemap magic (see lldb.swig), we pass in 1 to ReadMemory and # expect to get a Python string as the result object! content = self.process.ReadMemory(location, 1, error) - print "content:", content + if not error.Success(): + self.fail("SBProcess.ReadMemory() failed") + print "memory content:", content self.expect(content, "Result from SBProcess.ReadMemory() matches our expected output: 'x'", exe=False, startstr = 'x') - # - # Exercise Python APIs to access the symbol table entries. - # - - # Create the filespec by which to locate our a.out module. - filespec = lldb.SBFileSpec(exe, False) - - module = target.FindModule(filespec) - self.assertTrue(module.IsValid(), VALID_MODULE) - - # Create the set of known symbols. As we iterate through the symbol - # table, remove the symbol from the set if it is a known symbol. - expected_symbols = set(self.symbols_list) - from lldbutil import lldb_iter - for symbol in lldb_iter(module, 'GetNumSymbols', 'GetSymbolAtIndex'): - self.assertTrue(symbol.IsValid(), VALID_SYMBOL) - #print "symbol:", symbol - name = symbol.GetName() - if name in expected_symbols: - #print "Removing %s from known_symbols %s" % (name, expected_symbols) - expected_symbols.remove(name) - - # At this point, the known_symbols set should have become an empty set. - # If not, raise an error. - #print "symbols unaccounted for:", expected_symbols - self.assertTrue(len(expected_symbols) == 0, - "All the known symbols are accounted for") + def write_memory(self): + """Test Python SBProcess.WriteMemory() API.""" + exe = os.path.join(os.getcwd(), "a.out") + self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) + + target = self.dbg.CreateTarget(exe) + self.assertTrue(target.IsValid(), VALID_TARGET) + + breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) + self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT) + + # Launch the process, and do not stop at the entry point. + error = lldb.SBError() + self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error) + + thread = self.process.GetThreadAtIndex(0); + frame = thread.GetFrameAtIndex(0); + + # Get the SBValue for the global variable 'my_char'. + val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal) + self.DebugSBValue(frame, val) + + # If the variable does not have a load address, there's no sense continuing. + if not val.GetLocation(frame).startswith("0x"): + return + + # OK, let's get the hex location of the variable. + location = int(val.GetLocation(frame), 16) + + # The program logic makes the 'my_char' variable to have memory content as 'x'. + # But we want to use the WriteMemory() API to assign 'a' to the variable. + + # Now use WriteMemory() API to write 'a' into the global variable. + result = self.process.WriteMemory(location, 'a', error) + if not error.Success() or result != 1: + self.fail("SBProcess.WriteMemory() failed") + + # Read from the memory location. This time it should be 'a'. + # Due to the typemap magic (see lldb.swig), we pass in 1 to ReadMemory and + # expect to get a Python string as the result object! + content = self.process.ReadMemory(location, 1, error) + if not error.Success(): + self.fail("SBProcess.ReadMemory() failed") + print "memory content:", content + + self.expect(content, "Result from SBProcess.ReadMemory() matches our expected output: 'a'", + exe=False, + startstr = 'a') if __name__ == '__main__': -- cgit v1.2.3