""" Check that register fields found in target XML are properly processed. These tests make XML out of string substitution. This can lead to some strange failures. Check that the final XML is valid and each child is indented more than the parent tag. """ from textwrap import dedent import lldb from lldbsuite.test.lldbtest import * from lldbsuite.test.decorators import * from lldbsuite.test.gdbclientutils import * from lldbsuite.test.lldbgdbclient import GDBRemoteTestBase class MultiDocResponder(MockGDBServerResponder): # docs is a dictionary of filename -> file content. def __init__(self, docs): super().__init__() self.docs = docs def qXferRead(self, obj, annex, offset, length): try: return self.docs[annex], False except KeyError: return (None,) def readRegister(self, regnum): return "E01" def readRegisters(self): return "".join( [ # Data for all registers requested by the tests below. # 0x7 and 0xE are used because their lsb and msb are opposites, which # is needed for a byte order test. "77777777EEEEEEEE", # 64 bit x0/r0 "7777EEEE", # 32 bit cpsr/fpc "0000000000000000", # 64 bit pc/pswa ] ) class TestXMLRegisterFlags(GDBRemoteTestBase): def setup_multidoc_test(self, docs): self.server.responder = MultiDocResponder(docs) target = self.dbg.CreateTarget("") if self.TraceOn(): self.runCmd("log enable gdb-remote packets process") self.addTearDownHook( lambda: self.runCmd("log disable gdb-remote packets process") ) process = self.connect(target) lldbutil.expect_state_changes( self, self.dbg.GetListener(), process, [lldb.eStateStopped] ) def setup_register_test(self, registers): self.setup_multidoc_test( # This *must* begin with the opening tag, leading whitespace is not allowed. { "target.xml": dedent( """\ aarch64 {} """ ).format(registers) } ) def setup_flags_test(self, flags): # pc is required here though we don't look at it in the tests. # x0 is only used by some tests but always including it keeps the data ordering # the same throughout. self.setup_register_test( """\ {} """.format( flags ) ) @skipIfXmlSupportMissing @skipIfRemote def test_no_flags(self): self.setup_flags_test("") self.expect("register read cpsr", substrs=["= 0xeeee7777"]) @skipIfXmlSupportMissing @skipIfRemote def test_single_field_pad_msb(self): self.setup_flags_test("""""") # Pads from 31 to 1. self.expect("register read cpsr", substrs=["(SP = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_single_field_pad_lsb(self): self.setup_flags_test("""""") self.expect("register read cpsr", substrs=["(SP = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_multiple_fields_sorted(self): self.setup_flags_test( """ """ ) # Fields should be sorted with MSB on the left. self.expect("register read cpsr", substrs=["(EL = 3, SP = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_ignore_invalid_start_end(self): self.setup_flags_test( # Is valid so is used. '' # Start/end cannot be negative, ignored. '' '' # Start is not <= end, ignored. '' # Start cannot be >= (size of register in bits) '' # End cannot be >= (size of register in bits) '' ) self.expect("register read cpsr", substrs=["(EL = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_field_overlap(self): self.setup_flags_test( '' # A overlaps B '' '' ) # Ignore the whole flags set, it is unlikely to be valid. self.expect("register read cpsr", substrs=["("], matching=False) @skipIfXmlSupportMissing @skipIfRemote def test_field_required_attributes(self): # Fields must have a name, start and end. Any without are ignored. self.setup_flags_test( # Missing name '' # Missing start '' # Missing end '' # Valid '' ) self.expect("register read cpsr", substrs=["(C = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_little_endian_target_order(self): # We are using little endian AArch64 here. self.setup_register_test( """\ """ ) # If lldb used the wrong byte ordering for the value for printing fields, # these field values would flip. Since the top and bottom bits of 0x7 and 0xE # are different. self.expect( "register read cpsr x0", substrs=[ " cpsr = 0xeeee7777\n" " = (msb = 1, lsb = 1)\n" " x0 = 0xeeeeeeee77777777\n" " = (msb = 1, lsb = 1)" ], ) @skipIfXmlSupportMissing @skipIfRemote # Unlike AArch64, we do need the backend present for this test to work. @skipIfLLVMTargetMissing("SystemZ") def test_big_endian_target_order(self): # s390x/SystemZ is big endian. self.setup_multidoc_test( { "target.xml": dedent( """\ s390x """ ) } ) # If we did not swap correctly, these fields would show as 1s when run on # a little endian host. self.expect( "register read r0 fpc", substrs=[ " r0 = 0x77777777eeeeeeee\n" " = (msb = 0, lsb = 0)\n" " fpc = 0x7777eeee\n" " = (msb = 0, lsb = 0)\n" ], ) @skipIfXmlSupportMissing @skipIfRemote def test_many_flag_sets(self): self.setup_register_test( """\ """ ) self.expect( "register read cpsr x0", substrs=[ " cpsr = 0xeeee7777\n" " = (correct = 1)\n" " x0 = 0xeeeeeeee77777777\n" " = (foo = 1)" ], ) @skipIfXmlSupportMissing @skipIfRemote def test_repeated_flag_set(self): # The second definition of "cpsr_flags" should be ignored. # This is because we assign the types to registers as we go. If we allowed # the later flag set, it would destroy the first definition, making the # pointer to the flags invalid. self.setup_register_test( """\ """ ) self.expect("register read cpsr", substrs=["(correct = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_missing_flags(self): self.setup_register_test( """\ """ ) # Register prints with default formatting only if we can't find the # flags type. self.expect("register read cpsr", substrs=["cpsr = 0xeeee7777"]) self.expect("register read cpsr", substrs=["("], matching=False) @skipIfXmlSupportMissing @skipIfRemote def test_flags_invalid_size(self): # We're not using the size for anything yet so just check that we handle # it not being a positive integer. self.setup_register_test( """\ """ ) # Only the final set has a valid size, use that. self.expect("register read cpsr", substrs=["(C = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_flags_unknown_attribute(self): # Unknown attributes on flags or field are ignored. self.setup_register_test( """\ """ ) self.expect("register read cpsr", substrs=["(A = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_flags_required_attributes(self): # flags must have an id and size so the flags with "C" is the only valid one # here. self.setup_register_test( """\ """ ) self.expect("register read cpsr", substrs=["(C = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_flags_register_size_mismatch(self): # If the size of the flag set found does not match the size of the # register, we discard the flags. self.setup_register_test( """\ """ ) self.expect("register read cpsr", substrs=["(C = 1)"], matching=False) @skipIfXmlSupportMissing @skipIfRemote def test_flags_set_even_if_format_set(self): # lldb also sends "format". If that is set, we should still read the # flags type. self.setup_register_test( """\ """ ) self.expect("register read cpsr", substrs=["(B = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_flags_set_even_if_encoding_set(self): # lldb also sends "encoding". If that is set, we should still read the # flags type. self.setup_register_test( """\ """ ) self.expect("register read cpsr", substrs=["(B = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_flags_set_even_if_encoding_and_format_set(self): # As above but both encoding and format are set. self.setup_register_test( """\ """ ) self.expect("register read cpsr", substrs=["(B = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_flags_multiple_lines(self): # Since we use C types they follow lldb's usual decisions as to whether # to print them on one line or many. Long field names will usually mean # many lines. self.setup_flags_test( '' '' '' '' ) self.expect( "register read cpsr", substrs=[ " cpsr = 0xeeee7777\n" " = {\n" " this_is_a_long_field_3 = 0\n" " this_is_a_long_field_2 = 1\n" " this_is_a_long_field_1 = 1\n" " this_is_a_long_field_0 = 1\n" " }" ], ) @skipIfXmlSupportMissing @skipIfRemote def test_flags_child_limit(self): # Flags print like C types so they should follow the child limit setting. self.runCmd("settings set target.max-children-count 3") self.setup_flags_test( '' '' '' ) self.expect("register read cpsr", substrs=["= (field_2 = 1, field_1 = 1, ...)"]) @skipIfXmlSupportMissing @skipIfRemote def test_format_disables_flags(self): # If asked for a specific format, don't print flags after it. self.setup_flags_test('') self.expect("register read cpsr --format X", substrs=["cpsr = 0xEEEE7777"]) self.expect( "register read cpsr --format X", substrs=["field_0"], matching=False ) @skipIfXmlSupportMissing @skipIfRemote def test_xml_includes(self): # Certain targets e.g. s390x QEMU split their defintions over multiple # files that are included into target.xml. self.setup_multidoc_test( { # The formatting is very specific here. lldb doesn't like leading # spaces, and nested tags must be indented more than their parent. "target.xml": dedent( """\ aarch64 """ ), "core.xml": dedent( """\ """ ), } ) self.expect("register read cpsr", substrs=["(B = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_xml_includes_multiple(self): self.setup_multidoc_test( { "target.xml": dedent( """\ aarch64 """ ), "core.xml": dedent( """\ """ ), "core-2.xml": dedent( """\ """ ), } ) self.expect("register read x0 cpsr", substrs=["(B = 1)", "(C = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_xml_includes_flags_redefined(self): self.setup_multidoc_test( { "target.xml": dedent( """\ aarch64 """ ), # Treating xi:include as a textual include, my_flags is first defined # in core.xml. The second definition in core-2.xml # is ignored. "core.xml": dedent( """\ """ ), # The my_flags here is ignored, so x1 will use the my_flags from above. "core-2.xml": dedent( """\ """ ), } ) self.expect("register read x0", substrs=["(correct = 1)"]) self.expect("register read x1", substrs=["(correct = 1)"]) @skipIfXmlSupportMissing @skipIfRemote def test_flags_in_register_info(self): # See RegisterFlags for comprehensive formatting tests. self.setup_flags_test( '' '' '' '' ) # The table should split according to terminal width. self.runCmd("settings set term-width 17") self.expect( "register info cpsr", substrs=[ " Name: cpsr\n" " Size: 4 bytes (32 bits)\n" " In sets: general (index 0)\n" "\n" "| 31-24 | 23-16 |\n" "|-------|-------|\n" "| A | B |\n" "\n" "| 15-8 | 7-0 |\n" "|------|-----|\n" "| C | D |" ], ) @skipIfXmlSupportMissing @skipIfRemote def test_flags_name_xml_reserved_characters(self): """Check that lldb converts reserved character replacements like & when found in field names.""" self.setup_flags_test( '' '' '' '' '' ) self.expect( "register info cpsr", substrs=["| A< | B> | C' | D\" | E& |"], ) @skipIfXmlSupportMissing @skipIfRemote def test_no_enum(self): """Check that lldb does not try to print an enum when there isn't one.""" self.setup_flags_test('' "") self.expect("register info cpsr", patterns=["E:.*$"], matching=False) @skipIfXmlSupportMissing @skipIfRemote def test_enum_type_not_found(self): """Check that lldb uses the default format if we don't find the enum type.""" self.setup_register_test( """\ """ ) self.expect("register read cpsr", patterns=["\(E = 1\)$"]) @skipIfXmlSupportMissing @skipIfRemote def test_enum_duplicated_evalue(self): """Check that lldb only uses the last instance of a evalue for each value.""" self.setup_register_test( """\ """ ) self.expect("register info cpsr", patterns=["E: 1 = def, 2 = geh$"]) self.expect("register read cpsr", patterns=["\(E = def \| geh\)$"]) @skipIfXmlSupportMissing @skipIfRemote def test_enum_duplicated(self): """Check that lldb only uses the last instance of enums with the same id.""" self.setup_register_test( """\ """ ) self.expect("register info cpsr", patterns=["E: 1 = def$"]) self.expect("register read cpsr", patterns=["\(E = def\)$"]) @skipIfXmlSupportMissing @skipIfRemote def test_enum_use_first_valid(self): """Check that lldb uses the first enum that parses correctly and ignores the rest.""" self.setup_register_test( """\ """ ) self.expect("register info cpsr", patterns=["E: 1 = valid$"]) @skipIfXmlSupportMissing @skipIfRemote def test_evalue_empty_name(self): """Check that lldb ignores evalues with an empty name.""" # The only potential use case for empty names is to shadow an evalue # declared later so that it's name is hidden should the debugger only # pick one of them. This behaviour would be debugger specific so the protocol # would probably not care or leave it up to us, and I think it's not a # useful thing to allow. self.setup_register_test( """\ """ ) self.expect("register info cpsr", patterns=["E: 2 = valid$"]) @skipIfXmlSupportMissing @skipIfRemote def test_evalue_invalid_value(self): """Check that lldb ignores evalues with an invalid value.""" self.setup_register_test( """\ """ ) self.expect( "register info cpsr", patterns=["E: 1 = dec, 2 = hex, 3 = octal, 4 = bin$"] ) @skipIfXmlSupportMissing @skipIfRemote def test_evalue_out_of_range(self): """Check that lldb will not use an enum type if one of its evalues exceeds the size of the field it is applied to.""" self.setup_register_test( """\ """ ) # The whole eunm is rejected even if just 1 value is out of range. self.expect("register info cpsr", patterns=["E: 0 = "], matching=False) @skipIfXmlSupportMissing @skipIfRemote def test_enum_ignore_unknown_attributes(self): """Check that lldb ignores unknown attributes on an enum or evalue.""" self.setup_register_test( """\ """ ) self.expect("register info cpsr", patterns=["E: 1 = valid$"]) @skipIfXmlSupportMissing @skipIfRemote def test_evalue_required_attributes(self): """Check that lldb rejects any evalue missing a name and/or value.""" self.setup_register_test( """\ """ ) self.expect("register info cpsr", patterns=["E: 1 = valid$"]) @skipIfXmlSupportMissing @skipIfRemote def test_evalue_name_xml_reserved_characters(self): """Check that lldb converts reserved character replacements like & when found in evalue names.""" self.setup_register_test( """\ """ ) self.expect( "register info cpsr", patterns=["E: 0 = A&, 1 = B\", 2 = C', 3 = D>, 4 = E<$"], ) @skipIfXmlSupportMissing @skipIfRemote def test_enum_value_range(self): """Check that lldb ignores enums whose values would not fit into their field.""" self.setup_register_test( """\ """ ) # some_enum can apply to foo self.expect( "register info cpsr", patterns=["bar: 0 = A, 1 = B, 2 = C, 3 = D, 4 = E$"] ) # but not to bar self.expect("register info cpsr", patterns=["foo: "], matching=False) @skipIfXmlSupportMissing @skipIfRemote def test_evalue_value_limits(self): """Check that lldb can handle an evalue for a field up to 64 bits in size and anything greater is ignored.""" self.setup_register_test( """\ """ ) self.expect( "register info x0", patterns=["foo: 0 = min, 18446744073709551615 = max$"] ) @skipIfXmlSupportMissing @skipIfRemote def test_field_size_limit(self): """Check that lldb ignores any field > 64 bits. We can't handle those correctly.""" self.setup_register_test( """\ """ ) self.expect( "register info x0", substrs=["| 63-0 |\n" "|-------|\n" "| valid |"] ) @skipIfXmlSupportMissing @skipIfRemote def test_many_fields_same_enum(self): """Check that an enum can be reused by many fields, and fields of many registers.""" self.setup_register_test( """\ """ ) expected_info = [ dedent( """\ f2: 1 = valid f1: 1 = valid$""" ) ] self.expect("register info x0", patterns=expected_info) self.expect("register info cpsr", patterns=expected_info) expected_read = ["\(f2 = valid, f1 = valid\)$"] self.expect("register read x0", patterns=expected_read) self.expect("register read cpsr", patterns=expected_read) @skipIfXmlSupportMissing @skipIfRemote def test_fields_same_name_different_enum(self): """Check that lldb does something sensible when there are two fields with the same name, but their enum types differ.""" # It's unlikely anyone would do this intentionally but it is allowed by # the protocol spec so we have to cope with it. self.setup_register_test( """\ """ ) self.expect( "register info x0", patterns=[ dedent( """\ foo: 1 = foo_1 foo: 1 = foo_0$""" ) ], ) self.expect("register read x0", patterns=["\(foo = foo_1, foo = foo_0\)$"])