5 from __future__ import print_function
10 from lldbsuite.test.decorators import *
11 from lldbsuite.test.lldbtest import *
12 from lldbsuite.test import lldbutil
15 class AvoidBreakpointInDelaySlotAPITestCase(TestBase):
17 mydir = TestBase.compute_mydir(__file__)
19 @skipIf(archs=no_match(re.compile('mips*')))
22 exe = self.getBuildArtifact("a.out")
23 self.expect("file " + exe,
24 patterns=["Current executable set to .*a.out.*"])
26 # Create a target by the debugger.
27 target = self.dbg.CreateTarget(exe)
28 self.assertTrue(target, VALID_TARGET)
30 breakpoint = target.BreakpointCreateByName('main', 'a.out')
31 self.assertTrue(breakpoint and
32 breakpoint.GetNumLocations() == 1,
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)
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)
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)
55 if (inst.HasDelaySlot()):
56 # Remember the address of branch instruction.
57 branchinstaddress = inst.GetAddress().GetLoadAddress(target)
59 # Get next instruction i.e delay slot instruction.
60 delayinst = insts.GetInstructionAtIndex(i + 1)
61 delayinstaddr = delayinst.GetAddress().GetLoadAddress(target)
63 # Set breakpoint on delay slot instruction
64 breakpoint = target.BreakpointCreateByAddress(delayinstaddr)
66 # Verify the breakpoint.
67 self.assertTrue(breakpoint and
68 breakpoint.GetNumLocations() == 1,
70 # Get the location from breakpoint
71 location = breakpoint.GetLocationAtIndex(0)
73 # Get the address where breakpoint is actually set.
74 bpaddr = location.GetLoadAddress()
76 # Breakpoint address should be adjusted to the address of
78 self.assertTrue(branchinstaddress == bpaddr)
83 if __name__ == '__main__':
85 lldb.SBDebugger.Initialize()
86 atexit.register(lambda: lldb.SBDebugger.Terminate())