xref: /llvm-project/lldb/test/API/functionalities/breakpoint/breakpoint_options/TestBreakpointOptions.py (revision ece2c25ab9f572f719fd18f1ced4fa80f3e5ed1c)
1"""
2Test breakpoint command for different options.
3"""
4
5
6import lldb
7from lldbsuite.test.lldbtest import *
8import lldbsuite.test.lldbutil as lldbutil
9
10
11class BreakpointOptionsTestCase(TestBase):
12    def test(self):
13        """Test breakpoint command for different options."""
14        self.build()
15        self.breakpoint_options_test()
16        self.breakpoint_options_language_test()
17
18    def setUp(self):
19        # Call super's setUp().
20        TestBase.setUp(self)
21        # Find the line number to break inside main().
22        self.line = line_number("main.cpp", "// Set break point at this line.")
23
24    def breakpoint_options_test(self):
25        """Test breakpoint command for different options."""
26        exe = self.getBuildArtifact("a.out")
27        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
28
29        # This should create a breakpoint with 1 locations.
30        lldbutil.run_break_set_by_file_and_line(
31            self, "main.cpp", self.line, extra_options="-K 1", num_expected_locations=1
32        )
33        lldbutil.run_break_set_by_file_and_line(
34            self, "main.cpp", self.line, extra_options="-K 0", num_expected_locations=1
35        )
36
37        # Run the program.
38        self.runCmd("run", RUN_SUCCEEDED)
39
40        # Stopped once.
41        self.expect(
42            "thread backtrace",
43            STOPPED_DUE_TO_BREAKPOINT,
44            substrs=["stop reason = breakpoint 2."],
45        )
46
47        # Check the list of breakpoint.
48        self.expect(
49            "breakpoint list -f",
50            "Breakpoint locations shown correctly",
51            substrs=[
52                "1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1"
53                % self.line,
54                "2: file = 'main.cpp', line = %d, exact_match = 0, locations = 1"
55                % self.line,
56            ],
57        )
58
59        # Continue the program, there should be another stop.
60        self.runCmd("process continue")
61
62        # Stopped again.
63        self.expect(
64            "thread backtrace",
65            STOPPED_DUE_TO_BREAKPOINT,
66            substrs=["stop reason = breakpoint 1."],
67        )
68
69        # Continue the program, we should exit.
70        self.runCmd("process continue")
71
72        # We should exit.
73        self.expect(
74            "process status",
75            "Process exited successfully",
76            patterns=["^Process [0-9]+ exited with status = 0"],
77        )
78
79    def breakpoint_options_language_test(self):
80        """Test breakpoint command for language option."""
81        exe = self.getBuildArtifact("a.out")
82        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
83
84        # This should create a breakpoint with 1 locations.
85        bp_id = lldbutil.run_break_set_by_symbol(
86            self,
87            "ns::func",
88            sym_exact=False,
89            extra_options="-L c++",
90            num_expected_locations=1,
91        )
92
93        location = self.target().FindBreakpointByID(bp_id).GetLocationAtIndex(0)
94        function = location.GetAddress().GetFunction()
95        self.expect(
96            "breakpoint list -v",
97            "Verbose breakpoint list contains mangled names",
98            # The demangled function name is system-dependent, e.g.
99            # 'int ns::func(void)' on Windows and 'ns::func()' on Linux.
100            substrs=[
101                f"function = {function.GetName()}",
102                f"mangled function = {function.GetMangledName()}",
103            ],
104        )
105
106        # This should create a breakpoint with 0 locations.
107        lldbutil.run_break_set_by_symbol(
108            self,
109            "ns::func",
110            sym_exact=False,
111            extra_options="-L c",
112            num_expected_locations=0,
113        )
114        self.runCmd("settings set target.language c")
115        lldbutil.run_break_set_by_symbol(
116            self, "ns::func", sym_exact=False, num_expected_locations=0
117        )
118
119        # Run the program.
120        self.runCmd("run", RUN_SUCCEEDED)
121
122        # Stopped once.
123        self.expect(
124            "thread backtrace",
125            STOPPED_DUE_TO_BREAKPOINT,
126            substrs=["stop reason = breakpoint 1."],
127        )
128
129        # Continue the program, we should exit.
130        self.runCmd("process continue")
131
132        # We should exit.
133        self.expect(
134            "process status",
135            "Process exited successfully",
136            patterns=["^Process [0-9]+ exited with status = 0"],
137        )
138