You've already forked llvm-project
mirror of
https://github.com/encounter/llvm-project.git
synced 2026-03-30 11:27:19 -07:00
c8fd130a2c
Currently most of the test files have a separate dwarf and a separate dsym test with almost identical content (only the build step is different). With adding dwo symbol file handling to the test suit it would increase this to a 3-way duplication. The purpose of this change is to eliminate this redundancy with generating 2 test case (one dwarf and one dsym) for each test function specified (dwo handling will be added at a later commit). Main design goals: * There should be no boilerplate code in each test file to support the multiple debug info in most of the tests (custom scenarios are acceptable in special cases) so adding a new test case is easier and we can't miss one of the debug info type. * In case of a test failure, the debug symbols used during the test run have to be cleanly visible from the output of dotest.py to make debugging easier both from build bot logs and from local test runs * Each test case should have a unique, fully qualified name so we can run exactly 1 test with "-f <test-case>.<test-function>" syntax * Test output should be grouped based on test files the same way as it happens now (displaying dwarf/dsym results separately isn't preferable) Proposed solution (main logic in lldbtest.py, rest of them are test cases fixed up for the new style): * Have only 1 test fuction in the test files what will run for all debug info separately and this test function should call just "self.build(...)" to build an inferior with the right debug info * When a class is created by python (the class object, not the class instance), we will generate a new test method for each debug info format in the test class with the name "<test-function>_<debug-info>" and remove the original test method. This way unittest2 see multiple test methods (1 for each debug info, pretty much as of now) and will handle the test selection and the failure reporting correctly (the debug info will be visible from the end of the test name) * Add new annotation @no_debug_info_test to disable the generation of multiple tests for each debug info format when the test don't have an inferior Differential revision: http://reviews.llvm.org/D13028 llvm-svn: 248883
293 lines
12 KiB
Python
293 lines
12 KiB
Python
"""
|
|
Test SBProcess APIs, including ReadMemory(), WriteMemory(), and others.
|
|
"""
|
|
|
|
import os, time
|
|
import unittest2
|
|
import lldb
|
|
from lldbutil import get_stopped_thread, state_type_to_str
|
|
from lldbtest import *
|
|
|
|
class ProcessAPITestCase(TestBase):
|
|
|
|
mydir = TestBase.compute_mydir(__file__)
|
|
|
|
def setUp(self):
|
|
# Call super's setUp().
|
|
TestBase.setUp(self)
|
|
# 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'.")
|
|
|
|
@python_api_test
|
|
def test_read_memory(self):
|
|
"""Test Python SBProcess.ReadMemory() API."""
|
|
self.build()
|
|
exe = os.path.join(os.getcwd(), "a.out")
|
|
|
|
target = self.dbg.CreateTarget(exe)
|
|
self.assertTrue(target, VALID_TARGET)
|
|
|
|
breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
|
|
self.assertTrue(breakpoint, VALID_BREAKPOINT)
|
|
|
|
# Launch the process, and do not stop at the entry point.
|
|
process = target.LaunchSimple (None, None, self.get_process_working_directory())
|
|
|
|
thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
|
|
self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
|
|
frame = thread.GetFrameAtIndex(0)
|
|
|
|
# Get the SBValue for the global variable 'my_char'.
|
|
val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal)
|
|
self.DebugSBValue(val)
|
|
|
|
# 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!
|
|
error = lldb.SBError()
|
|
self.assertFalse(val.TypeIsPointerType())
|
|
content = process.ReadMemory(val.AddressOf().GetValueAsUnsigned(), 1, error)
|
|
if not error.Success():
|
|
self.fail("SBProcess.ReadMemory() failed")
|
|
if self.TraceOn():
|
|
print "memory content:", content
|
|
|
|
self.expect(content, "Result from SBProcess.ReadMemory() matches our expected output: 'x'",
|
|
exe=False,
|
|
startstr = 'x')
|
|
|
|
# Read (char *)my_char_ptr.
|
|
val = frame.FindValue("my_char_ptr", lldb.eValueTypeVariableGlobal)
|
|
self.DebugSBValue(val)
|
|
cstring = process.ReadCStringFromMemory(val.GetValueAsUnsigned(), 256, error)
|
|
if not error.Success():
|
|
self.fail("SBProcess.ReadCStringFromMemory() failed")
|
|
if self.TraceOn():
|
|
print "cstring read is:", cstring
|
|
|
|
self.expect(cstring, "Result from SBProcess.ReadCStringFromMemory() matches our expected output",
|
|
exe=False,
|
|
startstr = 'Does it work?')
|
|
|
|
# Get the SBValue for the global variable 'my_cstring'.
|
|
val = frame.FindValue("my_cstring", lldb.eValueTypeVariableGlobal)
|
|
self.DebugSBValue(val)
|
|
|
|
# Due to the typemap magic (see lldb.swig), we pass in 256 to read at most 256 bytes
|
|
# from the address, and expect to get a Python string as the result object!
|
|
self.assertFalse(val.TypeIsPointerType())
|
|
cstring = process.ReadCStringFromMemory(val.AddressOf().GetValueAsUnsigned(), 256, error)
|
|
if not error.Success():
|
|
self.fail("SBProcess.ReadCStringFromMemory() failed")
|
|
if self.TraceOn():
|
|
print "cstring read is:", cstring
|
|
|
|
self.expect(cstring, "Result from SBProcess.ReadCStringFromMemory() matches our expected output",
|
|
exe=False,
|
|
startstr = 'lldb.SBProcess.ReadCStringFromMemory() works!')
|
|
|
|
# Get the SBValue for the global variable 'my_uint32'.
|
|
val = frame.FindValue("my_uint32", lldb.eValueTypeVariableGlobal)
|
|
self.DebugSBValue(val)
|
|
|
|
# Due to the typemap magic (see lldb.swig), we pass in 4 to read 4 bytes
|
|
# from the address, and expect to get an int as the result!
|
|
self.assertFalse(val.TypeIsPointerType())
|
|
my_uint32 = process.ReadUnsignedFromMemory(val.AddressOf().GetValueAsUnsigned(), 4, error)
|
|
if not error.Success():
|
|
self.fail("SBProcess.ReadCStringFromMemory() failed")
|
|
if self.TraceOn():
|
|
print "uint32 read is:", my_uint32
|
|
|
|
if my_uint32 != 12345:
|
|
self.fail("Result from SBProcess.ReadUnsignedFromMemory() does not match our expected output")
|
|
|
|
@python_api_test
|
|
def test_write_memory(self):
|
|
"""Test Python SBProcess.WriteMemory() API."""
|
|
self.build()
|
|
exe = os.path.join(os.getcwd(), "a.out")
|
|
|
|
target = self.dbg.CreateTarget(exe)
|
|
self.assertTrue(target, VALID_TARGET)
|
|
|
|
breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
|
|
self.assertTrue(breakpoint, VALID_BREAKPOINT)
|
|
|
|
# Launch the process, and do not stop at the entry point.
|
|
process = target.LaunchSimple (None, None, self.get_process_working_directory())
|
|
|
|
thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
|
|
self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
|
|
frame = thread.GetFrameAtIndex(0)
|
|
|
|
# Get the SBValue for the global variable 'my_char'.
|
|
val = frame.FindValue("my_char", lldb.eValueTypeVariableGlobal)
|
|
self.DebugSBValue(val)
|
|
|
|
# If the variable does not have a load address, there's no sense continuing.
|
|
if not val.GetLocation().startswith("0x"):
|
|
return
|
|
|
|
# OK, let's get the hex location of the variable.
|
|
location = int(val.GetLocation(), 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.
|
|
error = lldb.SBError()
|
|
result = 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 = process.ReadMemory(location, 1, error)
|
|
if not error.Success():
|
|
self.fail("SBProcess.ReadMemory() failed")
|
|
if self.TraceOn():
|
|
print "memory content:", content
|
|
|
|
self.expect(content, "Result from SBProcess.ReadMemory() matches our expected output: 'a'",
|
|
exe=False,
|
|
startstr = 'a')
|
|
|
|
@python_api_test
|
|
def test_access_my_int(self):
|
|
"""Test access 'my_int' using Python SBProcess.GetByteOrder() and other APIs."""
|
|
self.build()
|
|
exe = os.path.join(os.getcwd(), "a.out")
|
|
|
|
target = self.dbg.CreateTarget(exe)
|
|
self.assertTrue(target, VALID_TARGET)
|
|
|
|
breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
|
|
self.assertTrue(breakpoint, VALID_BREAKPOINT)
|
|
|
|
# Launch the process, and do not stop at the entry point.
|
|
process = target.LaunchSimple (None, None, self.get_process_working_directory())
|
|
|
|
thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
|
|
self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
|
|
frame = thread.GetFrameAtIndex(0)
|
|
|
|
# Get the SBValue for the global variable 'my_int'.
|
|
val = frame.FindValue("my_int", lldb.eValueTypeVariableGlobal)
|
|
self.DebugSBValue(val)
|
|
|
|
# If the variable does not have a load address, there's no sense continuing.
|
|
if not val.GetLocation().startswith("0x"):
|
|
return
|
|
|
|
# OK, let's get the hex location of the variable.
|
|
location = int(val.GetLocation(), 16)
|
|
|
|
# Note that the canonical from of the bytearray is little endian.
|
|
from lldbutil import int_to_bytearray, bytearray_to_int
|
|
|
|
byteSize = val.GetByteSize()
|
|
bytes = int_to_bytearray(256, byteSize)
|
|
|
|
byteOrder = process.GetByteOrder()
|
|
if byteOrder == lldb.eByteOrderBig:
|
|
bytes.reverse()
|
|
elif byteOrder == lldb.eByteOrderLittle:
|
|
pass
|
|
else:
|
|
# Neither big endian nor little endian? Return for now.
|
|
# Add more logic here if we want to handle other types.
|
|
return
|
|
|
|
# The program logic makes the 'my_int' variable to have int type and value of 0.
|
|
# But we want to use the WriteMemory() API to assign 256 to the variable.
|
|
|
|
# Now use WriteMemory() API to write 256 into the global variable.
|
|
new_value = str(bytes)
|
|
error = lldb.SBError()
|
|
result = process.WriteMemory(location, new_value, error)
|
|
if not error.Success() or result != byteSize:
|
|
self.fail("SBProcess.WriteMemory() failed")
|
|
|
|
# Make sure that the val we got originally updates itself to notice the change:
|
|
self.expect(val.GetValue(),
|
|
"SBProcess.ReadMemory() successfully writes (int)256 to the memory location for 'my_int'",
|
|
exe=False,
|
|
startstr = '256')
|
|
|
|
# And for grins, get the SBValue for the global variable 'my_int' again, to make sure that also tracks the new value:
|
|
val = frame.FindValue("my_int", lldb.eValueTypeVariableGlobal)
|
|
self.expect(val.GetValue(),
|
|
"SBProcess.ReadMemory() successfully writes (int)256 to the memory location for 'my_int'",
|
|
exe=False,
|
|
startstr = '256')
|
|
|
|
# Now read the memory content. The bytearray should have (byte)1 as the second element.
|
|
content = process.ReadMemory(location, byteSize, error)
|
|
if not error.Success():
|
|
self.fail("SBProcess.ReadMemory() failed")
|
|
|
|
# Use "ascii" as the encoding because each element of 'content' is in the range [0..255].
|
|
new_bytes = bytearray(content, "ascii")
|
|
|
|
# The bytearray_to_int utility function expects a little endian bytearray.
|
|
if byteOrder == lldb.eByteOrderBig:
|
|
new_bytes.reverse()
|
|
|
|
new_value = bytearray_to_int(new_bytes, byteSize)
|
|
if new_value != 256:
|
|
self.fail("Memory content read from 'my_int' does not match (int)256")
|
|
|
|
# Dump the memory content....
|
|
if self.TraceOn():
|
|
for i in new_bytes:
|
|
print "byte:", i
|
|
|
|
@python_api_test
|
|
def test_remote_launch(self):
|
|
"""Test SBProcess.RemoteLaunch() API with a process not in eStateConnected, and it should fail."""
|
|
self.build()
|
|
exe = os.path.join(os.getcwd(), "a.out")
|
|
|
|
target = self.dbg.CreateTarget(exe)
|
|
self.assertTrue(target, VALID_TARGET)
|
|
|
|
# Launch the process, and do not stop at the entry point.
|
|
process = target.LaunchSimple (None, None, self.get_process_working_directory())
|
|
|
|
if self.TraceOn():
|
|
print "process state:", state_type_to_str(process.GetState())
|
|
self.assertTrue(process.GetState() != lldb.eStateConnected)
|
|
|
|
error = lldb.SBError()
|
|
success = process.RemoteLaunch(None, None, None, None, None, None, 0, False, error)
|
|
self.assertTrue(not success, "RemoteLaunch() should fail for process state != eStateConnected")
|
|
|
|
@python_api_test
|
|
def test_get_num_supported_hardware_watchpoints(self):
|
|
"""Test SBProcess.GetNumSupportedHardwareWatchpoints() API with a process."""
|
|
self.build()
|
|
exe = os.path.join(os.getcwd(), "a.out")
|
|
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
|
|
|
|
target = self.dbg.CreateTarget(exe)
|
|
self.assertTrue(target, VALID_TARGET)
|
|
|
|
breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
|
|
self.assertTrue(breakpoint, VALID_BREAKPOINT)
|
|
|
|
# Launch the process, and do not stop at the entry point.
|
|
process = target.LaunchSimple (None, None, self.get_process_working_directory())
|
|
|
|
error = lldb.SBError();
|
|
num = process.GetNumSupportedHardwareWatchpoints(error)
|
|
if self.TraceOn() and error.Success():
|
|
print "Number of supported hardware watchpoints: %d" % num
|
|
|
|
|
|
if __name__ == '__main__':
|
|
import atexit
|
|
lldb.SBDebugger.Initialize()
|
|
atexit.register(lambda: lldb.SBDebugger.Terminate())
|
|
unittest2.main()
|