xref: /llvm-project/lldb/test/API/tools/lldb-dap/instruction-breakpoint/TestDAP_instruction_breakpoint.py (revision 7d7d2d2b54172f97300c02ec80bb568d35403cce)
1import dap_server
2import shutil
3from lldbsuite.test.decorators import *
4from lldbsuite.test.lldbtest import *
5from lldbsuite.test import lldbutil
6import lldbdap_testcase
7import os
8import lldb
9
10
11class TestDAP_InstructionBreakpointTestCase(lldbdap_testcase.DAPTestCaseBase):
12    NO_DEBUG_INFO_TESTCASE = True
13
14    def setUp(self):
15        lldbdap_testcase.DAPTestCaseBase.setUp(self)
16
17        self.main_basename = "main-copy.cpp"
18        self.main_path = os.path.realpath(self.getBuildArtifact(self.main_basename))
19
20    @skipIfWindows
21    def test_instruction_breakpoint(self):
22        self.build()
23        self.instruction_breakpoint_test()
24
25    def instruction_breakpoint_test(self):
26        """Sample test to ensure SBFrame::Disassemble produces SOME output"""
27        # Create a target by the debugger.
28        target = self.createTestTarget()
29
30        main_line = line_number("main.cpp", "breakpoint 1")
31
32        program = self.getBuildArtifact("a.out")
33        self.build_and_launch(program)
34
35        # Set source breakpoint 1
36        response = self.dap_server.request_setBreakpoints(self.main_path, [main_line])
37        breakpoints = response["body"]["breakpoints"]
38        self.assertEqual(len(breakpoints), 1)
39        breakpoint = breakpoints[0]
40        self.assertEqual(
41            breakpoint["line"], main_line, "incorrect breakpoint source line"
42        )
43        self.assertTrue(breakpoint["verified"], "breakpoint is not verified")
44        self.assertEqual(
45            self.main_basename, breakpoint["source"]["name"], "incorrect source name"
46        )
47        self.assertEqual(
48            self.main_path, breakpoint["source"]["path"], "incorrect source file path"
49        )
50        other_breakpoint_id = breakpoint["id"]
51
52        # Continue and then verifiy the breakpoint
53        self.dap_server.request_continue()
54        self.verify_breakpoint_hit([other_breakpoint_id])
55
56        # now we check the stack trace making sure that we got mapped source paths
57        frames = self.dap_server.request_stackTrace()["body"]["stackFrames"]
58        intstructionPointerReference = []
59        setIntstructionBreakpoints = []
60        intstructionPointerReference.append(frames[0]["instructionPointerReference"])
61        self.assertEqual(
62            frames[0]["source"]["name"], self.main_basename, "incorrect source name"
63        )
64        self.assertEqual(
65            frames[0]["source"]["path"], self.main_path, "incorrect source file path"
66        )
67
68        # Check disassembly view
69        instruction = self.disassemble(frameIndex=0)
70        self.assertEqual(
71            instruction["address"],
72            intstructionPointerReference[0],
73            "current breakpoint reference is not in the disaasembly view",
74        )
75
76        # Get next instruction address to set instruction breakpoint
77        disassembled_instruction_list = self.dap_server.disassembled_instructions
78        instruction_addr_list = list(disassembled_instruction_list.keys())
79        index = instruction_addr_list.index(intstructionPointerReference[0])
80        if len(instruction_addr_list) >= (index + 1):
81            next_inst_addr = instruction_addr_list[index + 1]
82            if len(next_inst_addr) > 2:
83                setIntstructionBreakpoints.append(next_inst_addr)
84                instruction_breakpoint_response = (
85                    self.dap_server.request_setInstructionBreakpoints(
86                        setIntstructionBreakpoints
87                    )
88                )
89                inst_breakpoints = instruction_breakpoint_response["body"][
90                    "breakpoints"
91                ]
92                self.assertEqual(
93                    inst_breakpoints[0]["instructionReference"],
94                    next_inst_addr,
95                    "Instruction breakpoint has not been resolved or failed to relocate the instruction breakpoint",
96                )
97                self.dap_server.request_continue()
98                self.verify_breakpoint_hit([inst_breakpoints[0]["id"]])
99