xref: /llvm-project/lldb/test/API/tools/lldb-dap/step/TestDAP_step.py (revision 6257a98b258a3f17b78af31bf43009a559c5dd1d)
1"""
2Test lldb-dap setBreakpoints request
3"""
4
5
6import dap_server
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10import lldbdap_testcase
11
12
13class TestDAP_step(lldbdap_testcase.DAPTestCaseBase):
14    @skipIfWindows
15    def test_step(self):
16        """
17        Tests the stepping in/out/over in threads.
18        """
19        program = self.getBuildArtifact("a.out")
20        self.build_and_launch(program)
21        source = "main.cpp"
22        # source_path = os.path.join(os.getcwd(), source)
23        breakpoint1_line = line_number(source, "// breakpoint 1")
24        lines = [breakpoint1_line]
25        # Set breakpoint in the thread function so we can step the threads
26        breakpoint_ids = self.set_source_breakpoints(source, lines)
27        self.assertEqual(
28            len(breakpoint_ids), len(lines), "expect correct number of breakpoints"
29        )
30        self.continue_to_breakpoints(breakpoint_ids)
31        threads = self.dap_server.get_threads()
32        for thread in threads:
33            if "reason" in thread:
34                reason = thread["reason"]
35                if reason == "breakpoint":
36                    # We have a thread that is stopped at our breakpoint.
37                    # Get the value of "x" and get the source file and line.
38                    # These will help us determine if we are stepping
39                    # correctly. If we step a thread correctly we will verify
40                    # the correct falue for x as it progresses through the
41                    # program.
42                    tid = thread["id"]
43                    x1 = self.get_local_as_int("x", threadId=tid)
44                    (src1, line1) = self.get_source_and_line(threadId=tid)
45
46                    # Now step into the "recurse()" function call again and
47                    # verify, using the new value of "x" and the source file
48                    # and line if we stepped correctly
49                    self.stepIn(threadId=tid, waitForStop=True)
50                    x2 = self.get_local_as_int("x", threadId=tid)
51                    (src2, line2) = self.get_source_and_line(threadId=tid)
52                    self.assertEqual(x1, x2 + 1, "verify step in variable")
53                    self.assertLess(line2, line1, "verify step in line")
54                    self.assertEqual(src1, src2, "verify step in source")
55
56                    # Now step out and verify
57                    self.stepOut(threadId=tid, waitForStop=True)
58                    x3 = self.get_local_as_int("x", threadId=tid)
59                    (src3, line3) = self.get_source_and_line(threadId=tid)
60                    self.assertEqual(x1, x3, "verify step out variable")
61                    self.assertGreaterEqual(line3, line1, "verify step out line")
62                    self.assertEqual(src1, src3, "verify step in source")
63
64                    # Step over and verify
65                    self.stepOver(threadId=tid, waitForStop=True)
66                    x4 = self.get_local_as_int("x", threadId=tid)
67                    (src4, line4) = self.get_source_and_line(threadId=tid)
68                    self.assertEqual(x4, x3, "verify step over variable")
69                    self.assertGreater(line4, line3, "verify step over line")
70                    self.assertEqual(src1, src4, "verify step over source")
71
72                    # Step a single assembly instruction.
73                    # Unfortunately, there is no portable way to verify the correct
74                    # stepping behavior here, because the generated assembly code
75                    # depends highly on the compiler, its version, the operating
76                    # system, and many more factors.
77                    self.stepOver(
78                        threadId=tid, waitForStop=True, granularity="instruction"
79                    )
80                    self.stepIn(
81                        threadId=tid, waitForStop=True, granularity="instruction"
82                    )
83
84                    # only step one thread that is at the breakpoint and stop
85                    break
86