• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Test 'watchpoint command'.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11class WatchpointPythonCommandTestCase(TestBase):
12
13    mydir = os.path.join("functionalities", "watchpoint", "watchpoint_commands", "command")
14
15    def setUp(self):
16        # Call super's setUp().
17        TestBase.setUp(self)
18        # Our simple source filename.
19        self.source = 'main.cpp'
20        # Find the line number to break inside main().
21        self.line = line_number(self.source, '// Set break point at this line.')
22        # And the watchpoint variable declaration line number.
23        self.decl = line_number(self.source, '// Watchpoint variable declaration.')
24        # Build dictionary to have unique executable names for each test method.
25        self.exe_name = self.testMethodName
26        self.d = {'CXX_SOURCES': self.source, 'EXE': self.exe_name}
27
28    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
29    @dsym_test
30    def test_watchpoint_command_with_dsym(self):
31        """Test 'watchpoint command'."""
32        self.buildDsym(dictionary=self.d)
33        self.setTearDownCleanup(dictionary=self.d)
34        self.watchpoint_command()
35
36    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
37    @dwarf_test
38    def test_watchpoint_command_with_dwarf(self):
39        """Test 'watchpoint command'."""
40        self.buildDwarf(dictionary=self.d)
41        self.setTearDownCleanup(dictionary=self.d)
42        self.watchpoint_command()
43
44    def watchpoint_command(self):
45        """Do 'watchpoint command add'."""
46        exe = os.path.join(os.getcwd(), self.exe_name)
47        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
48
49        # Add a breakpoint to set a watchpoint when stopped on the breakpoint.
50        lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1)
51#        self.expect("breakpoint set -l %d" % self.line, BREAKPOINT_CREATED,
52#            startstr = "Breakpoint created: 1: file ='%s', line = %d, locations = 1" %
53#                       (self.source, self.line))#
54
55        # Run the program.
56        self.runCmd("run", RUN_SUCCEEDED)
57
58        # We should be stopped again due to the breakpoint.
59        # The stop reason of the thread should be breakpoint.
60        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
61            substrs = ['stopped',
62                       'stop reason = breakpoint'])
63
64        # Now let's set a write-type watchpoint for 'global'.
65        self.expect("watchpoint set variable -w write global", WATCHPOINT_CREATED,
66            substrs = ['Watchpoint created', 'size = 4', 'type = w',
67                       '%s:%d' % (self.source, self.decl)])
68
69        self.runCmd('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 become 777.
93        self.expect("frame variable --show-globals cookie",
94            substrs = ['(int32_t)', 'cookie = 777'])
95
96
97if __name__ == '__main__':
98    import atexit
99    lldb.SBDebugger.Initialize()
100    atexit.register(lambda: lldb.SBDebugger.Terminate())
101    unittest2.main()
102