199451b44SJordan Rupprecht""" 299451b44SJordan RupprechtTest lldb exception breakpoint command for CPP. 399451b44SJordan Rupprecht""" 499451b44SJordan Rupprecht 599451b44SJordan Rupprecht 699451b44SJordan Rupprechtimport lldb 799451b44SJordan Rupprechtfrom lldbsuite.test.decorators import * 899451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import * 999451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil 1099451b44SJordan Rupprecht 1199451b44SJordan Rupprecht 1299451b44SJordan Rupprechtclass CPPBreakpointTestCase(TestBase): 1399451b44SJordan Rupprecht def setUp(self): 1499451b44SJordan Rupprecht # Call super's setUp(). 1599451b44SJordan Rupprecht TestBase.setUp(self) 162238dcc3SJonas Devlieghere self.source = "exceptions.cpp" 1799451b44SJordan Rupprecht self.catch_line = line_number( 182238dcc3SJonas Devlieghere self.source, "// This is the line you should stop at for catch" 192238dcc3SJonas Devlieghere ) 2099451b44SJordan Rupprecht 2199451b44SJordan Rupprecht @expectedFailureAll( 2299451b44SJordan Rupprecht oslist=["windows"], 232238dcc3SJonas Devlieghere bugnumber="llvm.org/pr24538, clang-cl does not support throw or catch", 242238dcc3SJonas Devlieghere ) 2599451b44SJordan Rupprecht def test(self): 2699451b44SJordan Rupprecht """Test lldb exception breakpoint command for CPP.""" 2799451b44SJordan Rupprecht self.build() 2899451b44SJordan Rupprecht exe = self.getBuildArtifact("a.out") 2999451b44SJordan Rupprecht 3099451b44SJordan Rupprecht # Create a target from the debugger. 3199451b44SJordan Rupprecht 3299451b44SJordan Rupprecht target = self.dbg.CreateTarget(exe) 3399451b44SJordan Rupprecht self.assertTrue(target, VALID_TARGET) 3499451b44SJordan Rupprecht 3599451b44SJordan Rupprecht exception_bkpt = target.BreakpointCreateForException( 362238dcc3SJonas Devlieghere lldb.eLanguageTypeC_plus_plus, True, True 372238dcc3SJonas Devlieghere ) 3899451b44SJordan Rupprecht self.assertTrue(exception_bkpt, "Made an exception breakpoint") 3999451b44SJordan Rupprecht 4099451b44SJordan Rupprecht # Now run, and make sure we hit our breakpoint: 412238dcc3SJonas Devlieghere process = target.LaunchSimple(None, None, self.get_process_working_directory()) 4299451b44SJordan Rupprecht self.assertTrue(process, "Got a valid process") 4399451b44SJordan Rupprecht 4499451b44SJordan Rupprecht stopped_threads = [] 4599451b44SJordan Rupprecht stopped_threads = lldbutil.get_threads_stopped_at_breakpoint( 462238dcc3SJonas Devlieghere process, exception_bkpt 472238dcc3SJonas Devlieghere ) 480ed758b2SDave Lee self.assertEqual( 492238dcc3SJonas Devlieghere len(stopped_threads), 1, "Stopped at our exception breakpoint." 502238dcc3SJonas Devlieghere ) 5199451b44SJordan Rupprecht thread = stopped_threads[0] 5299451b44SJordan Rupprecht # Make sure our throw function is still above us on the stack: 5399451b44SJordan Rupprecht 5499451b44SJordan Rupprecht frame_functions = lldbutil.get_function_names(thread) 550ed758b2SDave Lee self.assertEqual( 562238dcc3SJonas Devlieghere frame_functions.count("throws_exception_on_even(int)"), 572238dcc3SJonas Devlieghere 1, 582238dcc3SJonas Devlieghere "Our throw function is still on the stack.", 592238dcc3SJonas Devlieghere ) 6099451b44SJordan Rupprecht 6199451b44SJordan Rupprecht # Okay we hit our exception throw breakpoint, now make sure we get our catch breakpoint. 6299451b44SJordan Rupprecht # One potential complication is that we might hit a couple of the exception breakpoints in getting out of the throw. 6399451b44SJordan Rupprecht # so loop till we don't see the throws function on the stack. We should stop one more time for our exception breakpoint 6499451b44SJordan Rupprecht # and that should be the catch... 6599451b44SJordan Rupprecht 6699451b44SJordan Rupprecht while frame_functions.count("throws_exception_on_even(int)") == 1: 672238dcc3SJonas Devlieghere stopped_threads = lldbutil.continue_to_breakpoint(process, exception_bkpt) 68*80fcecb1SJonas Devlieghere self.assertEqual(len(stopped_threads), 1) 6999451b44SJordan Rupprecht 7099451b44SJordan Rupprecht thread = stopped_threads[0] 7199451b44SJordan Rupprecht frame_functions = lldbutil.get_function_names(thread) 7299451b44SJordan Rupprecht 730ed758b2SDave Lee self.assertEqual( 742238dcc3SJonas Devlieghere frame_functions.count("throws_exception_on_even(int)"), 752238dcc3SJonas Devlieghere 0, 762238dcc3SJonas Devlieghere "At catch our throw function is off the stack", 772238dcc3SJonas Devlieghere ) 780ed758b2SDave Lee self.assertEqual( 792238dcc3SJonas Devlieghere frame_functions.count("intervening_function(int)"), 802238dcc3SJonas Devlieghere 0, 812238dcc3SJonas Devlieghere "At catch our intervening function is off the stack", 822238dcc3SJonas Devlieghere ) 830ed758b2SDave Lee self.assertEqual( 842238dcc3SJonas Devlieghere frame_functions.count("catches_exception(int)"), 852238dcc3SJonas Devlieghere 1, 862238dcc3SJonas Devlieghere "At catch our catch function is on the stack", 872238dcc3SJonas Devlieghere ) 88