1""" 2Test 'watchpoint command'. 3""" 4 5 6 7import lldb 8from lldbsuite.test.decorators import * 9from lldbsuite.test.lldbtest import * 10from lldbsuite.test import lldbutil 11 12 13class WatchpointLLDBCommandTestCase(TestBase): 14 15 mydir = TestBase.compute_mydir(__file__) 16 NO_DEBUG_INFO_TESTCASE = True 17 18 def setUp(self): 19 # Call super's setUp(). 20 TestBase.setUp(self) 21 # Our simple source filename. 22 self.source = 'main.cpp' 23 # Find the line number to break inside main(). 24 self.line = line_number( 25 self.source, '// Set break point at this line.') 26 # And the watchpoint variable declaration line number. 27 self.decl = line_number(self.source, 28 '// Watchpoint variable declaration.') 29 # Build dictionary to have unique executable names for each test 30 # method. 31 self.exe_name = 'a%d.out' % self.test_number 32 self.d = {'CXX_SOURCES': self.source, 'EXE': self.exe_name} 33 34 def test_watchpoint_command(self): 35 """Test 'watchpoint command'.""" 36 self.build(dictionary=self.d) 37 self.setTearDownCleanup(dictionary=self.d) 38 39 exe = self.getBuildArtifact(self.exe_name) 40 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 41 42 # Add a breakpoint to set a watchpoint when stopped on the breakpoint. 43 lldbutil.run_break_set_by_file_and_line( 44 self, None, self.line, num_expected_locations=1) 45 46 # Run the program. 47 self.runCmd("run", RUN_SUCCEEDED) 48 49 # We should be stopped again due to the breakpoint. 50 # The stop reason of the thread should be breakpoint. 51 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 52 substrs=['stopped', 53 'stop reason = breakpoint']) 54 55 # Now let's set a write-type watchpoint for 'global'. 56 self.expect( 57 "watchpoint set variable -w write global", 58 WATCHPOINT_CREATED, 59 substrs=[ 60 'Watchpoint created', 61 'size = 4', 62 'type = w', 63 '%s:%d' % 64 (self.source, 65 self.decl)]) 66 67 self.runCmd('watchpoint command add 1 -o "expr -- cookie = 777"') 68 69 # List the watchpoint command we just added. 70 self.expect("watchpoint command list 1", 71 substrs=['expr -- cookie = 777']) 72 73 # Use the '-v' option to do verbose listing of the watchpoint. 74 # The hit count should be 0 initially. 75 self.expect("watchpoint list -v", 76 substrs=['hit_count = 0']) 77 78 self.runCmd("process continue") 79 80 # We should be stopped again due to the watchpoint (write type). 81 # The stop reason of the thread should be watchpoint. 82 self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT, 83 substrs=['stop reason = watchpoint']) 84 85 # Check that the watchpoint snapshoting mechanism is working. 86 self.expect( 87 "watchpoint list -v", 88 substrs=[ 89 'old value: 0', 'new value: 1', 'hit_count = 1', 90 'ignore_count = 0' 91 ]) 92 93 # The watchpoint command "forced" our global variable 'cookie' to 94 # become 777. 95 self.expect("frame variable --show-globals cookie", 96 substrs=['(int32_t)', 'cookie = 777']) 97 98 def test_watchpoint_command_can_disable_a_watchpoint(self): 99 """Test that 'watchpoint command' action can disable a watchpoint after it is triggered.""" 100 self.build(dictionary=self.d) 101 self.setTearDownCleanup(dictionary=self.d) 102 103 exe = self.getBuildArtifact(self.exe_name) 104 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 105 106 # Add a breakpoint to set a watchpoint when stopped on the breakpoint. 107 lldbutil.run_break_set_by_file_and_line( 108 self, None, self.line, num_expected_locations=1) 109 110 # Run the program. 111 self.runCmd("run", RUN_SUCCEEDED) 112 113 # We should be stopped again due to the breakpoint. 114 # The stop reason of the thread should be breakpoint. 115 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 116 substrs=['stopped', 117 'stop reason = breakpoint']) 118 119 # Now let's set a write-type watchpoint for 'global'. 120 self.expect( 121 "watchpoint set variable -w write global", 122 WATCHPOINT_CREATED, 123 substrs=[ 124 'Watchpoint created', 125 'size = 4', 126 'type = w', 127 '%s:%d' % 128 (self.source, 129 self.decl)]) 130 131 self.runCmd('watchpoint command add 1 -o "watchpoint disable 1"') 132 133 # List the watchpoint command we just added. 134 self.expect("watchpoint command list 1", 135 substrs=['watchpoint disable 1']) 136 137 # Use the '-v' option to do verbose listing of the watchpoint. 138 # The hit count should be 0 initially. 139 self.expect("watchpoint list -v", 140 substrs=['hit_count = 0']) 141 142 self.runCmd("process continue") 143 144 # We should be stopped again due to the watchpoint (write type). 145 # The stop reason of the thread should be watchpoint. 146 self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT, 147 substrs=['stop reason = watchpoint']) 148 149 # Check that the watchpoint has been disabled. 150 self.expect("watchpoint list -v", 151 substrs=['disabled']) 152 153 self.runCmd("process continue") 154 155 # There should be no more watchpoint hit and the process status should 156 # be 'exited'. 157 self.expect("process status", 158 substrs=['exited']) 159