131cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen""" 231cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny ChenTest breakpoint conditions with 'breakpoint modify -c <expr> id'. 331cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen""" 431cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 531cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chenimport os, time 631cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chenimport re 731cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chenimport unittest2 831cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chenimport lldb, lldbutil 931cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chenfrom lldbtest import * 1031cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 1131cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chenclass BreakpointConditionsTestCase(TestBase): 1231cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 13083e4a9b6c13eb737ca6279bbd7359e674a729a4Johnny Chen mydir = os.path.join("functionalities", "breakpoint", "breakpoint_conditions") 1431cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 1531cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 16a3ed7d834b0e0c6924ac95629e740682bbcd15baJohnny Chen @dsym_test 17e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen def test_breakpoint_condition_with_dsym_and_run_command(self): 1831cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen """Exercise breakpoint condition with 'breakpoint modify -c <expr> id'.""" 1931cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen self.buildDsym() 2031cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen self.breakpoint_conditions() 2131cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 220601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 23e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen @dsym_test 24e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen def test_breakpoint_condition_inline_with_dsym_and_run_command(self): 25e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen """Exercise breakpoint condition inline with 'breakpoint set'.""" 26e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen self.buildDsym() 27e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen self.breakpoint_conditions(inline=True) 28e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen 29e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 300678f8e583d79475c574cfc89f77e616fa2468c5Johnny Chen @python_api_test 31a3ed7d834b0e0c6924ac95629e740682bbcd15baJohnny Chen @dsym_test 32e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen def test_breakpoint_condition_with_dsym_and_python_api(self): 330601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen """Use Python APIs to set breakpoint conditions.""" 340601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen self.buildDsym() 350601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen self.breakpoint_conditions_python() 360601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen 37a3ed7d834b0e0c6924ac95629e740682bbcd15baJohnny Chen @dwarf_test 38e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen def test_breakpoint_condition_with_dwarf_and_run_command(self): 3931cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen """Exercise breakpoint condition with 'breakpoint modify -c <expr> id'.""" 4031cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen self.buildDwarf() 4131cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen self.breakpoint_conditions() 4231cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 43e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen @dwarf_test 44e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen def test_breakpoint_condition_inline_with_dwarf_and_run_command(self): 45e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen """Exercise breakpoint condition inline with 'breakpoint set'.""" 46e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen self.buildDwarf() 47e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen self.breakpoint_conditions(inline=True) 48e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen 490678f8e583d79475c574cfc89f77e616fa2468c5Johnny Chen @python_api_test 50a3ed7d834b0e0c6924ac95629e740682bbcd15baJohnny Chen @dwarf_test 51e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen def test_breakpoint_condition_with_dwarf_and_python_api(self): 520601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen """Use Python APIs to set breakpoint conditions.""" 530601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen self.buildDwarf() 540601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen self.breakpoint_conditions_python() 550601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen 5631cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen def setUp(self): 5731cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen # Call super's setUp(). 5831cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen TestBase.setUp(self) 5931cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen # Find the line number to of function 'c'. 60f6bdb19538f490f754a5c052f6553b411c378ebbJohnny Chen self.line1 = line_number('main.c', '// Find the line number of function "c" here.') 61f6bdb19538f490f754a5c052f6553b411c378ebbJohnny Chen self.line2 = line_number('main.c', "// Find the line number of c's parent call here.") 6231cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 63e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen def breakpoint_conditions(self, inline=False): 6431cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen """Exercise breakpoint condition with 'breakpoint modify -c <expr> id'.""" 6531cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen exe = os.path.join(os.getcwd(), "a.out") 6631cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 6731cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 68e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen if inline: 69e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen # Create a breakpoint by function name 'c' and set the condition. 70431d839a33e9a274e705f7a268a1c9de2ffc2da2Jim Ingham lldbutil.run_break_set_by_symbol (self, "c", extra_options="-c 'val == 3'", num_expected_locations=1, sym_exact=True) 71e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen else: 72e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen # Create a breakpoint by function name 'c'. 73431d839a33e9a274e705f7a268a1c9de2ffc2da2Jim Ingham lldbutil.run_break_set_by_symbol (self, "c", num_expected_locations=1, sym_exact=True) 7431cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 75e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen # And set a condition on the breakpoint to stop on when 'val == 3'. 76e4f3cd7fb037f28484198787a465a5e94d12d2e9Johnny Chen self.runCmd("breakpoint modify -c 'val == 3' 1") 7731cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 7831cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen # Now run the program. 7931cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen self.runCmd("run", RUN_SUCCEEDED) 8031cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 81b1349b626c8df3e6b832629ed46a7eba9da35760Johnny Chen # The process should be stopped at this point. 8224f43bef1f1b7ef23d32d36f47f3dd51b1241876Johnny Chen self.expect("process status", PROCESS_STOPPED, 8324f43bef1f1b7ef23d32d36f47f3dd51b1241876Johnny Chen patterns = ['Process .* stopped']) 8424f43bef1f1b7ef23d32d36f47f3dd51b1241876Johnny Chen 856475c42148a8ea1ca86e5db465db7eca742d897dGreg Clayton # 'frame variable --show-types val' should return 3 due to breakpoint condition. 866475c42148a8ea1ca86e5db465db7eca742d897dGreg Clayton self.expect("frame variable --show-types val", VARIABLES_DISPLAYED_CORRECTLY, 8731cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen startstr = '(int) val = 3') 8831cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 8931cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen # Also check the hit count, which should be 3, by design. 9041950cc77813637e6c67b069e4ad2faf8c5f6fa7Caroline Tice self.expect("breakpoint list -f", BREAKPOINT_HIT_THRICE, 9131cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen substrs = ["resolved = 1", 9231cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen "Condition: val == 3", 9331cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen "hit count = 3"]) 9431cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 9531cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen # The frame #0 should correspond to main.c:36, the executable statement 96f6bdb19538f490f754a5c052f6553b411c378ebbJohnny Chen # in function name 'c'. And the parent frame should point to main.c:24. 97f6bdb19538f490f754a5c052f6553b411c378ebbJohnny Chen self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT_CONDITION, 98f6bdb19538f490f754a5c052f6553b411c378ebbJohnny Chen #substrs = ["stop reason = breakpoint"], 99f6bdb19538f490f754a5c052f6553b411c378ebbJohnny Chen patterns = ["frame #0.*main.c:%d" % self.line1, 100f6bdb19538f490f754a5c052f6553b411c378ebbJohnny Chen "frame #1.*main.c:%d" % self.line2]) 10131cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 1027c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen # Test that "breakpoint modify -c ''" clears the condition for the last 1037c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen # created breakpoint, so that when the breakpoint hits, val == 1. 1047c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen self.runCmd("process kill") 1057c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen self.runCmd("breakpoint modify -c ''") 10641950cc77813637e6c67b069e4ad2faf8c5f6fa7Caroline Tice self.expect("breakpoint list -f", BREAKPOINT_STATE_CORRECT, matching=False, 1077c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen substrs = ["Condition:"]) 1087c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen 1097c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen # Now run the program again. 1107c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen self.runCmd("run", RUN_SUCCEEDED) 1117c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen 1127c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen # The process should be stopped at this point. 1137c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen self.expect("process status", PROCESS_STOPPED, 1147c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen patterns = ['Process .* stopped']) 1157c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen 1166475c42148a8ea1ca86e5db465db7eca742d897dGreg Clayton # 'frame variable --show-types val' should return 1 since it is the first breakpoint hit. 1176475c42148a8ea1ca86e5db465db7eca742d897dGreg Clayton self.expect("frame variable --show-types val", VARIABLES_DISPLAYED_CORRECTLY, 1187c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen startstr = '(int) val = 1') 1197c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen 120d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan self.runCmd("process kill") 121d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan self.runCmd("breakpoint disable") 122d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan 123d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan self.runCmd("breakpoint set -p Loop") 12403e33528b9d0218542fc3ec994fdc22808c3809fMatt Kopec self.runCmd("breakpoint modify -c ($eax&&i)") 125d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan self.runCmd("run") 126d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan 127d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan self.expect("process status", PROCESS_STOPPED, 128d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan patterns = ['Process .* stopped']) 129d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan 130d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan self.runCmd("continue") 131d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan 132d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan self.expect("process status", PROCESS_EXITED, 133d0f064d4e5ccef341ecbd6a462bbff5f085a3f77Sean Callanan patterns = ['Process .* exited']) 1347c9a8b57ba1b3ba36061dba07d3d2b440162f0c4Johnny Chen 1350601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen def breakpoint_conditions_python(self): 1360601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen """Use Python APIs to set breakpoint conditions.""" 1370601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen exe = os.path.join(os.getcwd(), "a.out") 1380601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen 1390601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen # Create a target by the debugger. 1400601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen target = self.dbg.CreateTarget(exe) 141805fec7ec90a09b94bc4515fcac9b2b03bae153bJohnny Chen self.assertTrue(target, VALID_TARGET) 1420601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen 1430601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen # Now create a breakpoint on main.c by name 'c'. 1440601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen breakpoint = target.BreakpointCreateByName('c', 'a.out') 1455492fc7f35d503f160ed4df251152d4e6a6d4816Johnny Chen #print "breakpoint:", breakpoint 146805fec7ec90a09b94bc4515fcac9b2b03bae153bJohnny Chen self.assertTrue(breakpoint and 1470601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen breakpoint.GetNumLocations() == 1, 1480601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen VALID_BREAKPOINT) 1490601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen 150b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen # We didn't associate a thread index with the breakpoint, so it should be invalid. 151b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen self.assertTrue(breakpoint.GetThreadIndex() == lldb.UINT32_MAX, 152b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen "The thread index should be invalid") 153b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen # The thread name should be invalid, too. 154b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen self.assertTrue(breakpoint.GetThreadName() is None, 155b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen "The thread name should be invalid") 156b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen 157b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen # Let's set the thread index for this breakpoint and verify that it is, 158b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen # indeed, being set correctly. 159b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen breakpoint.SetThreadIndex(1) # There's only one thread for the process. 160b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen self.assertTrue(breakpoint.GetThreadIndex() == 1, 161b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen "The thread index has been set correctly") 162b3e7181e269e8d26958efc47e362cb6189ecaacaJohnny Chen 1630601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen # Get the breakpoint location from breakpoint after we verified that, 1640601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen # indeed, it has one location. 1650601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen location = breakpoint.GetLocationAtIndex(0) 166805fec7ec90a09b94bc4515fcac9b2b03bae153bJohnny Chen self.assertTrue(location and 1670601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen location.IsEnabled(), 1680601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen VALID_BREAKPOINT_LOCATION) 1690601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen 1700601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen # Set the condition on the breakpoint location. 1710601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen location.SetCondition('val == 3') 1720601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen self.expect(location.GetCondition(), exe=False, 1730601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen startstr = 'val == 3') 1740601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen 1750601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen # Now launch the process, and do not stop at entry point. 1763d8ae4681b81b42a404ad77a231c6f743040e644Johnny Chen process = target.LaunchSimple(None, None, os.getcwd()) 1773d8ae4681b81b42a404ad77a231c6f743040e644Johnny Chen self.assertTrue(process, PROCESS_IS_VALID) 1780601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen 1790601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen # Frame #0 should be on self.line1 and the break condition should hold. 18077356a0e9bec77c6ed7eb23151bcd8de27fef902Johnny Chen from lldbutil import get_stopped_thread 18121f37ad875d4f50d1b4b3d307e120f6d27103730Jim Ingham thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) 182166b89f089d6bec5bb9dd40470a4dc951ffc9daaGreg Clayton self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition") 18377356a0e9bec77c6ed7eb23151bcd8de27fef902Johnny Chen frame0 = thread.GetFrameAtIndex(0) 184a8e2ea75519c7c8c19741e7ac016a924cc46962dJohnny Chen var = frame0.FindValue('val', lldb.eValueTypeVariableArgument) 1850601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen self.assertTrue(frame0.GetLineEntry().GetLine() == self.line1 and 1860fb0bcc9d4e951145e1b8c783652224c09b23af4Greg Clayton var.GetValue() == '3') 1870601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen 1880601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen # The hit count for the breakpoint should be 3. 1890601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen self.assertTrue(breakpoint.GetHitCount() == 3) 1900601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen 1913d8ae4681b81b42a404ad77a231c6f743040e644Johnny Chen process.Continue() 19231cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen 1930601a295d9df20410ba1ff1edcf670aee67c030aJohnny Chen 19431cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chenif __name__ == '__main__': 19531cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen import atexit 19631cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen lldb.SBDebugger.Initialize() 19731cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen atexit.register(lambda: lldb.SBDebugger.Terminate()) 19831cf8e24815c4a99d74b5bea50900ac8dfa2f40dJohnny Chen unittest2.main() 199