xref: /llvm-project/lldb/test/API/functionalities/multiple-slides/TestMultipleSlides.py (revision 1fee25629d9d3f33cc618cb2b61cdf3823bfd092)
1"""
2Test that a binary can be slid to different load addresses correctly
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class MultipleSlidesTestCase(TestBase):
14
15    NO_DEBUG_INFO_TESTCASE = True
16    def test_mulitple_slides(self):
17        """Test that a binary can be slid multiple times correctly."""
18        self.build()
19        exe = self.getBuildArtifact("a.out")
20        err = lldb.SBError()
21        load_dependent_modules = False
22        target = self.dbg.CreateTarget(exe, '', '', load_dependent_modules, err)
23        self.assertTrue(target.IsValid())
24        module = target.GetModuleAtIndex(0)
25        self.assertTrue(module.IsValid())
26
27        first_sym = target.FindSymbols("first").GetContextAtIndex(0).GetSymbol()
28        second_sym = target.FindSymbols("second").GetContextAtIndex(0).GetSymbol()
29        first_size = first_sym.GetEndAddress().GetOffset() - first_sym.GetStartAddress().GetOffset()
30        second_size = second_sym.GetEndAddress().GetOffset() - second_sym.GetStartAddress().GetOffset()
31
32        # View the first element of `first` and `second` while
33        # they have no load address set.
34        self.expect("p/d ((int*)&first)[0]", substrs=['= 5'])
35        self.expect("p/d ((int*)&second)[0]", substrs=['= 6'])
36        self.assertEqual(first_sym.GetStartAddress().GetLoadAddress(target), lldb.LLDB_INVALID_ADDRESS)
37        self.assertEqual(second_sym.GetStartAddress().GetLoadAddress(target), lldb.LLDB_INVALID_ADDRESS)
38
39
40        # View the first element of `first` and `second` with
41        # no slide applied, but with load address set.
42        #
43        # In memory, we have something like
44        #    0x1000 - 0x17ff  first[]
45        #    0x1800 - 0x1fff  second[]
46        target.SetModuleLoadAddress(module, 0)
47        self.expect("p/d ((int*)&first)[0]", substrs=['= 5'])
48        self.expect("p/d ((int*)&second)[0]", substrs=['= 6'])
49        self.assertEqual(first_sym.GetStartAddress().GetLoadAddress(target),
50                         first_sym.GetStartAddress().GetFileAddress())
51        self.assertEqual(second_sym.GetStartAddress().GetLoadAddress(target),
52                         second_sym.GetStartAddress().GetFileAddress())
53
54        # Slide it a little bit less than the size of the first array.
55        #
56        # In memory, we have something like
57        #    0xfc0 - 0x17bf  first[]
58        #    0x17c0 - 0x1fbf second[]
59        #
60        # but if the original entries are still present in lldb,
61        # the beginning address of second[] will get a load address
62        # of 0x1800, instead of 0x17c0 (0x1800-64) as we need to get.
63        target.SetModuleLoadAddress(module, first_size - 64)
64        self.expect("p/d ((int*)&first)[0]", substrs=['= 5'])
65        self.expect("p/d ((int*)&second)[0]", substrs=['= 6'])
66        self.assertNotEqual(first_sym.GetStartAddress().GetLoadAddress(target),
67                         first_sym.GetStartAddress().GetFileAddress())
68        self.assertNotEqual(second_sym.GetStartAddress().GetLoadAddress(target),
69                         second_sym.GetStartAddress().GetFileAddress())
70
71        # Slide it back to the original vmaddr.
72        target.SetModuleLoadAddress(module, 0)
73        self.expect("p/d ((int*)&first)[0]", substrs=['= 5'])
74        self.expect("p/d ((int*)&second)[0]", substrs=['= 6'])
75        self.assertEqual(first_sym.GetStartAddress().GetLoadAddress(target),
76                         first_sym.GetStartAddress().GetFileAddress())
77        self.assertEqual(second_sym.GetStartAddress().GetLoadAddress(target),
78                         second_sym.GetStartAddress().GetFileAddress())
79
80