xref: /llvm-project/lldb/test/API/lang/cpp/exceptions/TestCPPExceptionBreakpoints.py (revision 99562332e3de19da9da1714a58b5cc2d24724ef5)
1"""
2Test lldb exception breakpoint command for CPP.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class CPPBreakpointTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        self.source = 'exceptions.cpp'
21        self.catch_line = line_number(
22            self.source, '// This is the line you should stop at for catch')
23
24    @expectedFailureAll(
25        oslist=["windows"],
26        bugnumber="llvm.org/pr24538, clang-cl does not support throw or catch")
27    def test(self):
28        """Test lldb exception breakpoint command for CPP."""
29        self.build()
30        exe = self.getBuildArtifact("a.out")
31
32        # Create a target from the debugger.
33
34        target = self.dbg.CreateTarget(exe)
35        self.assertTrue(target, VALID_TARGET)
36
37        exception_bkpt = target.BreakpointCreateForException(
38            lldb.eLanguageTypeC_plus_plus, True, True)
39        self.assertTrue(exception_bkpt, "Made an exception breakpoint")
40
41        # Now run, and make sure we hit our breakpoint:
42        process = target.LaunchSimple(
43            None, None, self.get_process_working_directory())
44        self.assertTrue(process, "Got a valid process")
45
46        stopped_threads = []
47        stopped_threads = lldbutil.get_threads_stopped_at_breakpoint(
48            process, exception_bkpt)
49        self.assertTrue(
50            len(stopped_threads) == 1,
51            "Stopped at our exception breakpoint.")
52        thread = stopped_threads[0]
53        # Make sure our throw function is still above us on the stack:
54
55        frame_functions = lldbutil.get_function_names(thread)
56        self.assertTrue(
57            frame_functions.count("throws_exception_on_even(int)") == 1,
58            "Our throw function is still on the stack.")
59
60        # Okay we hit our exception throw breakpoint, now make sure we get our catch breakpoint.
61        # One potential complication is that we might hit a couple of the exception breakpoints in getting out of the throw.
62        # so loop till we don't see the throws function on the stack.  We should stop one more time for our exception breakpoint
63        # and that should be the catch...
64
65        while frame_functions.count("throws_exception_on_even(int)") == 1:
66            stopped_threads = lldbutil.continue_to_breakpoint(
67                process, exception_bkpt)
68            self.assertEquals(len(stopped_threads), 1)
69
70            thread = stopped_threads[0]
71            frame_functions = lldbutil.get_function_names(thread)
72
73        self.assertTrue(
74            frame_functions.count("throws_exception_on_even(int)") == 0,
75            "At catch our throw function is off the stack")
76        self.assertTrue(
77            frame_functions.count("intervening_function(int)") == 0,
78            "At catch our intervening function is off the stack")
79        self.assertTrue(
80            frame_functions.count("catches_exception(int)") == 1,
81            "At catch our catch function is on the stack")
82