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    @skipIf(archs=no_match(re.compile('mips*')))
18    def test(self):
19        self.build()
20        exe = self.getBuildArtifact("a.out")
21        self.expect("file " + exe,
22                    patterns=["Current executable set to .*a.out.*"])
23
24        # Create a target by the debugger.
25        target = self.dbg.CreateTarget(exe)
26        self.assertTrue(target, VALID_TARGET)
27
28        breakpoint = target.BreakpointCreateByName('main', 'a.out')
29        self.assertTrue(breakpoint and
30                        breakpoint.GetNumLocations() == 1,
31                        VALID_BREAKPOINT)
32
33        # Now launch the process, and do not stop at entry point.
34        process = target.LaunchSimple(
35            None, None, self.get_process_working_directory())
36        self.assertTrue(process, PROCESS_IS_VALID)
37
38        list = target.FindFunctions('foo', lldb.eFunctionNameTypeAuto)
39        self.assertEqual(list.GetSize(), 1)
40        sc = list.GetContextAtIndex(0)
41        self.assertEqual(sc.GetSymbol().GetName(), "foo")
42        function = sc.GetFunction()
43        self.assertTrue(function)
44        self.function(function, target)
45
46    def function(self, function, target):
47        """Iterate over instructions in function and place a breakpoint on delay slot instruction"""
48        # Get the list of all instructions in the function
49        insts = function.GetInstructions(target)
50        print(insts)
51        i = 0
52        for inst in insts:
53            if (inst.HasDelaySlot()):
54                # Remember the address of branch instruction.
55                branchinstaddress = inst.GetAddress().GetLoadAddress(target)
56
57                # Get next instruction i.e delay slot instruction.
58                delayinst = insts.GetInstructionAtIndex(i + 1)
59                delayinstaddr = delayinst.GetAddress().GetLoadAddress(target)
60
61                # Set breakpoint on delay slot instruction
62                breakpoint = target.BreakpointCreateByAddress(delayinstaddr)
63
64                # Verify the breakpoint.
65                self.assertTrue(breakpoint and
66                                breakpoint.GetNumLocations() == 1,
67                                VALID_BREAKPOINT)
68                # Get the location from breakpoint
69                location = breakpoint.GetLocationAtIndex(0)
70
71                # Get the address where breakpoint is actually set.
72                bpaddr = location.GetLoadAddress()
73
74                # Breakpoint address should be adjusted to the address of
75                # branch instruction.
76                self.assertEqual(branchinstaddress, bpaddr)
77                i += 1
78            else:
79                i += 1
80