1"""
2Test specific to MIPS
3"""
4
5from __future__ import print_function
6
7import re
8import unittest2
9import lldb
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14
15class AvoidBreakpointInDelaySlotAPITestCase(TestBase):
16
17    mydir = TestBase.compute_mydir(__file__)
18
19    @skipIf(archs=no_match(re.compile('mips*')))
20    def test(self):
21        self.build()
22        exe = self.getBuildArtifact("a.out")
23        self.expect("file " + exe,
24                    patterns=["Current executable set to .*a.out.*"])
25
26        # Create a target by the debugger.
27        target = self.dbg.CreateTarget(exe)
28        self.assertTrue(target, VALID_TARGET)
29
30        breakpoint = target.BreakpointCreateByName('main', 'a.out')
31        self.assertTrue(breakpoint and
32                        breakpoint.GetNumLocations() == 1,
33                        VALID_BREAKPOINT)
34
35        # Now launch the process, and do not stop at entry point.
36        process = target.LaunchSimple(
37            None, None, self.get_process_working_directory())
38        self.assertTrue(process, PROCESS_IS_VALID)
39
40        list = target.FindFunctions('foo', lldb.eFunctionNameTypeAuto)
41        self.assertTrue(list.GetSize() == 1)
42        sc = list.GetContextAtIndex(0)
43        self.assertTrue(sc.GetSymbol().GetName() == "foo")
44        function = sc.GetFunction()
45        self.assertTrue(function)
46        self.function(function, target)
47
48    def function(self, function, target):
49        """Iterate over instructions in function and place a breakpoint on delay slot instruction"""
50        # Get the list of all instructions in the function
51        insts = function.GetInstructions(target)
52        print(insts)
53        i = 0
54        for inst in insts:
55            if (inst.HasDelaySlot()):
56                # Remember the address of branch instruction.
57                branchinstaddress = inst.GetAddress().GetLoadAddress(target)
58
59                # Get next instruction i.e delay slot instruction.
60                delayinst = insts.GetInstructionAtIndex(i + 1)
61                delayinstaddr = delayinst.GetAddress().GetLoadAddress(target)
62
63                # Set breakpoint on delay slot instruction
64                breakpoint = target.BreakpointCreateByAddress(delayinstaddr)
65
66                # Verify the breakpoint.
67                self.assertTrue(breakpoint and
68                                breakpoint.GetNumLocations() == 1,
69                                VALID_BREAKPOINT)
70                # Get the location from breakpoint
71                location = breakpoint.GetLocationAtIndex(0)
72
73                # Get the address where breakpoint is actually set.
74                bpaddr = location.GetLoadAddress()
75
76                # Breakpoint address should be adjusted to the address of
77                # branch instruction.
78                self.assertTrue(branchinstaddress == bpaddr)
79                i += 1
80            else:
81                i += 1
82
83if __name__ == '__main__':
84    import atexit
85    lldb.SBDebugger.Initialize()
86    atexit.register(lambda: lldb.SBDebugger.Terminate())
87    unittest2.main()
88