1""" 2Test 'watchpoint command'. 3""" 4 5 6 7import os 8import lldb 9from lldbsuite.test.decorators import * 10from lldbsuite.test.lldbtest import * 11from lldbsuite.test import lldbutil 12 13 14class WatchpointPythonCommandTestCase(TestBase): 15 16 mydir = TestBase.compute_mydir(__file__) 17 NO_DEBUG_INFO_TESTCASE = True 18 19 def setUp(self): 20 # Call super's setUp(). 21 TestBase.setUp(self) 22 # Our simple source filename. 23 self.source = 'main.cpp' 24 # Find the line number to break inside main(). 25 self.line = line_number( 26 self.source, '// Set break point at this line.') 27 # And the watchpoint variable declaration line number. 28 self.decl = line_number(self.source, 29 '// Watchpoint variable declaration.') 30 # Build dictionary to have unique executable names for each test 31 # method. 32 self.exe_name = self.testMethodName 33 self.d = {'CXX_SOURCES': self.source, 'EXE': self.exe_name} 34 35 def test_watchpoint_command(self): 36 """Test 'watchpoint command'.""" 37 self.build(dictionary=self.d) 38 self.setTearDownCleanup(dictionary=self.d) 39 40 exe = self.getBuildArtifact(self.exe_name) 41 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 42 43 # Add a breakpoint to set a watchpoint when stopped on the breakpoint. 44 lldbutil.run_break_set_by_file_and_line( 45 self, None, self.line, num_expected_locations=1) 46 47 # Run the program. 48 self.runCmd("run", RUN_SUCCEEDED) 49 50 # We should be stopped again due to the breakpoint. 51 # The stop reason of the thread should be breakpoint. 52 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 53 substrs=['stopped', 54 'stop reason = breakpoint']) 55 56 # Now let's set a write-type watchpoint for 'global'. 57 self.expect( 58 "watchpoint set variable -w write global", 59 WATCHPOINT_CREATED, 60 substrs=[ 61 'Watchpoint created', 62 'size = 4', 63 'type = w', 64 '%s:%d' % 65 (self.source, 66 self.decl)]) 67 68 self.runCmd( 69 'watchpoint command add -s python 1 -o \'frame.EvaluateExpression("cookie = 777")\'') 70 71 # List the watchpoint command we just added. 72 self.expect("watchpoint command list 1", 73 substrs=['frame.EvaluateExpression', 'cookie = 777']) 74 75 # Use the '-v' option to do verbose listing of the watchpoint. 76 # The hit count should be 0 initially. 77 self.expect("watchpoint list -v", 78 substrs=['hit_count = 0']) 79 80 self.runCmd("process continue") 81 82 # We should be stopped again due to the watchpoint (write type). 83 # The stop reason of the thread should be watchpoint. 84 self.expect("thread backtrace", STOPPED_DUE_TO_WATCHPOINT, 85 substrs=['stop reason = watchpoint']) 86 87 # Check that the watchpoint snapshoting mechanism is working. 88 self.expect("watchpoint list -v", 89 substrs=['old value: 0', 90 'new value: 1']) 91 92 # The watchpoint command "forced" our global variable 'cookie' to 93 # become 777. 94 self.expect("frame variable --show-globals cookie", 95 substrs=['(int32_t)', 'cookie = 777']) 96 97 @skipIfReproducer 98 def test_continue_in_watchpoint_command(self): 99 """Test continue in a watchpoint command.""" 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 cmd_script_file = os.path.join(self.getSourceDir(), 132 "watchpoint_command.py") 133 self.runCmd("command script import '%s'" % (cmd_script_file)) 134 135 self.runCmd( 136 'watchpoint command add -F watchpoint_command.watchpoint_command') 137 138 # List the watchpoint command we just added. 139 self.expect("watchpoint command list 1", 140 substrs=['watchpoint_command.watchpoint_command']) 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 # We should have hit the watchpoint once, set cookie to 888, then continued to the 150 # second hit and set it to 999 151 self.expect("frame variable --show-globals cookie", 152 substrs=['(int32_t)', 'cookie = 999']) 153 154