5 from __future__ import print_function
11 from lldbsuite.test.decorators import *
12 from lldbsuite.test.lldbtest import *
13 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 = os.path.join(os.getcwd(), "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 (None, None, self.get_process_working_directory())
37 self.assertTrue(process, PROCESS_IS_VALID)
39 list = target.FindFunctions('foo', lldb.eFunctionNameTypeAuto)
40 self.assertTrue(list.GetSize() == 1)
41 sc = list.GetContextAtIndex(0)
42 self.assertTrue(sc.GetSymbol().GetName() == "foo")
43 function = sc.GetFunction()
44 self.assertTrue(function)
45 self.function(function, target)
47 def function (self, function, target):
48 """Iterate over instructions in function and place a breakpoint on delay slot instruction"""
49 # Get the list of all instructions in the function
50 insts = function.GetInstructions(target)
54 if (inst.HasDelaySlot()):
55 # Remember the address of branch instruction.
56 branchinstaddress = inst.GetAddress().GetLoadAddress(target)
58 # Get next instruction i.e delay slot instruction.
59 delayinst = insts.GetInstructionAtIndex(i+1)
60 delayinstaddr = delayinst.GetAddress().GetLoadAddress(target)
62 # Set breakpoint on delay slot instruction
63 breakpoint = target.BreakpointCreateByAddress(delayinstaddr)
65 # Verify the breakpoint.
66 self.assertTrue(breakpoint and
67 breakpoint.GetNumLocations() == 1,
69 # Get the location from breakpoint
70 location = breakpoint.GetLocationAtIndex(0)
72 # Get the address where breakpoint is actually set.
73 bpaddr = location.GetLoadAddress()
75 # Breakpoint address should be adjusted to the address of branch instruction.
76 self.assertTrue(branchinstaddress == bpaddr)
81 if __name__ == '__main__':
83 lldb.SBDebugger.Initialize()
84 atexit.register(lambda: lldb.SBDebugger.Terminate())