]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/functionalities/breakpoint/breakpoint_command/TestBreakpointCommand.py
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / functionalities / breakpoint / breakpoint_command / TestBreakpointCommand.py
1 """
2 Test lldb breakpoint command add/list/delete.
3 """
4
5 from __future__ import print_function
6
7
8
9 import os, time
10 import lldb
11 from lldbsuite.test.lldbtest import *
12 import lldbsuite.test.lldbutil as lldbutil
13
14 class BreakpointCommandTestCase(TestBase):
15
16     mydir = TestBase.compute_mydir(__file__)
17
18     @classmethod
19     def classCleanup(cls):
20         """Cleanup the test byproduct of breakpoint_command_sequence(self)."""
21         cls.RemoveTempFile("output.txt")
22         cls.RemoveTempFile("output2.txt")
23
24     @expectedFailureWindows("llvm.org/pr24528")
25     def test(self):
26         """Test a sequence of breakpoint command add, list, and delete."""
27         self.build()
28         self.breakpoint_command_sequence()
29         self.breakpoint_command_script_parameters ()
30
31     def setUp(self):
32         # Call super's setUp().
33         TestBase.setUp(self)
34         # Find the line number to break inside main().
35         self.line = line_number('main.c', '// Set break point at this line.')
36         # disable "There is a running process, kill it and restart?" prompt
37         self.runCmd("settings set auto-confirm true")
38         self.addTearDownHook(lambda: self.runCmd("settings clear auto-confirm"))
39
40     def breakpoint_command_sequence(self):
41         """Test a sequence of breakpoint command add, list, and delete."""
42         exe = os.path.join(os.getcwd(), "a.out")
43         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
44
45         # Add three breakpoints on the same line.  The first time we don't specify the file,
46         # since the default file is the one containing main:
47         lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1, loc_exact=True)
48         lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
49         lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
50         # Breakpoint 4 - set at the same location as breakpoint 1 to test setting breakpoint commands on two breakpoints at a time
51         lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=1, loc_exact=True)
52
53         # Now add callbacks for the breakpoints just created.
54         self.runCmd("breakpoint command add -s command -o 'frame variable --show-types --scope' 1 4")
55         self.runCmd("breakpoint command add -s python -o 'here = open(\"output.txt\", \"w\"); here.write(\"lldb\\n\"); here.close()' 2")
56         self.runCmd("breakpoint command add --python-function bktptcmd.function 3")
57
58         # Check that the breakpoint commands are correctly set.
59
60         # The breakpoint list now only contains breakpoint 1.
61         self.expect("breakpoint list", "Breakpoints 1 & 2 created",
62             substrs = ["2: file = 'main.c', line = %d, exact_match = 0, locations = 1" % self.line],
63             patterns = ["1: file = '.*main.c', line = %d, exact_match = 0, locations = 1" % self.line] )
64
65         self.expect("breakpoint list -f", "Breakpoints 1 & 2 created",
66             substrs = ["2: file = 'main.c', line = %d, exact_match = 0, locations = 1" % self.line],
67             patterns = ["1: file = '.*main.c', line = %d, exact_match = 0, locations = 1" % self.line,
68                         "1.1: .+at main.c:%d, .+unresolved, hit count = 0" % self.line,
69                         "2.1: .+at main.c:%d, .+unresolved, hit count = 0" % self.line])
70
71         self.expect("breakpoint command list 1", "Breakpoint 1 command ok",
72             substrs = ["Breakpoint commands:",
73                           "frame variable --show-types --scope"])
74         self.expect("breakpoint command list 2", "Breakpoint 2 command ok",
75             substrs = ["Breakpoint commands:",
76                           "here = open",
77                           "here.write",
78                           "here.close()"])
79         self.expect("breakpoint command list 3", "Breakpoint 3 command ok",
80             substrs = ["Breakpoint commands:",
81                           "bktptcmd.function(frame, bp_loc, internal_dict)"])
82
83         self.expect("breakpoint command list 4", "Breakpoint 4 command ok",
84             substrs = ["Breakpoint commands:",
85                           "frame variable --show-types --scope"])
86
87         self.runCmd("breakpoint delete 4")
88
89         self.runCmd("command script import --allow-reload ./bktptcmd.py")
90
91         # Next lets try some other breakpoint kinds.  First break with a regular expression
92         # and then specify only one file.  The first time we should get two locations,
93         # the second time only one:
94
95         lldbutil.run_break_set_by_regexp (self, r"._MyFunction", num_expected_locations=2)
96         
97         lldbutil.run_break_set_by_regexp (self, r"._MyFunction", extra_options="-f a.c", num_expected_locations=1)
98       
99         lldbutil.run_break_set_by_regexp (self, r"._MyFunction", extra_options="-f a.c -f b.c", num_expected_locations=2)
100
101         # Now try a source regex breakpoint:
102         lldbutil.run_break_set_by_source_regexp (self, r"is about to return [12]0", extra_options="-f a.c -f b.c", num_expected_locations=2)
103       
104         lldbutil.run_break_set_by_source_regexp (self, r"is about to return [12]0", extra_options="-f a.c", num_expected_locations=1)
105       
106         # Run the program.  Remove 'output.txt' if it exists.
107         self.RemoveTempFile("output.txt")
108         self.RemoveTempFile("output2.txt")
109         self.runCmd("run", RUN_SUCCEEDED)
110
111         # Check that the file 'output.txt' exists and contains the string "lldb".
112
113         # The 'output.txt' file should now exist.
114         self.assertTrue(os.path.isfile("output.txt"),
115                         "'output.txt' exists due to breakpoint command for breakpoint 2.")
116         self.assertTrue(os.path.isfile("output2.txt"),
117                         "'output2.txt' exists due to breakpoint command for breakpoint 3.")
118
119         # Read the output file produced by running the program.
120         with open('output.txt', 'r') as f:
121             output = f.read()
122
123         self.expect(output, "File 'output.txt' and the content matches", exe=False,
124             startstr = "lldb")
125
126         with open('output2.txt', 'r') as f:
127             output = f.read()
128
129         self.expect(output, "File 'output2.txt' and the content matches", exe=False,
130             startstr = "lldb")
131
132
133         # Finish the program.
134         self.runCmd("process continue")
135
136         # Remove the breakpoint command associated with breakpoint 1.
137         self.runCmd("breakpoint command delete 1")
138
139         # Remove breakpoint 2.
140         self.runCmd("breakpoint delete 2")
141
142         self.expect("breakpoint command list 1",
143             startstr = "Breakpoint 1 does not have an associated command.")
144         self.expect("breakpoint command list 2", error=True,
145             startstr = "error: '2' is not a currently valid breakpoint id.")
146
147         # The breakpoint list now only contains breakpoint 1.
148         self.expect("breakpoint list -f", "Breakpoint 1 exists",
149             patterns = ["1: file = '.*main.c', line = %d, exact_match = 0, locations = 1, resolved = 1" %
150                         self.line,
151                        "hit count = 1"])
152
153         # Not breakpoint 2.
154         self.expect("breakpoint list -f", "No more breakpoint 2", matching=False,
155             substrs = ["2: file = 'main.c', line = %d, exact_match = 0, locations = 1, resolved = 1" %
156                         self.line])
157
158         # Run the program again, with breakpoint 1 remaining.
159         self.runCmd("run", RUN_SUCCEEDED)
160
161         # We should be stopped again due to breakpoint 1.
162
163         # The stop reason of the thread should be breakpoint.
164         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
165             substrs = ['stopped',
166                        'stop reason = breakpoint'])
167
168         # The breakpoint should have a hit count of 2.
169         self.expect("breakpoint list -f", BREAKPOINT_HIT_TWICE,
170             substrs = ['resolved, hit count = 2'])
171
172     def breakpoint_command_script_parameters (self):
173         """Test that the frame and breakpoint location are being properly passed to the script breakpoint command function."""
174         exe = os.path.join(os.getcwd(), "a.out")
175         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
176
177         # Add a breakpoint.
178         lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
179
180         # Now add callbacks for the breakpoints just created.
181         self.runCmd("breakpoint command add -s python -o 'here = open(\"output-2.txt\", \"w\"); here.write(str(frame) + \"\\n\"); here.write(str(bp_loc) + \"\\n\"); here.close()' 1")
182
183         # Remove 'output-2.txt' if it already exists.
184
185         if (os.path.exists('output-2.txt')):
186             os.remove ('output-2.txt')
187
188         # Run program, hit breakpoint, and hopefully write out new version of 'output-2.txt'
189         self.runCmd ("run", RUN_SUCCEEDED)
190
191         # Check that the file 'output.txt' exists and contains the string "lldb".
192
193         # The 'output-2.txt' file should now exist.
194         self.assertTrue(os.path.isfile("output-2.txt"),
195                         "'output-2.txt' exists due to breakpoint command for breakpoint 1.")
196
197         # Read the output file produced by running the program.
198         with open('output-2.txt', 'r') as f:
199             output = f.read()
200
201         self.expect (output, "File 'output-2.txt' and the content matches", exe=False,
202                      startstr = "frame #0:",
203                      patterns = ["1.* where = .*main .* resolved, hit count = 1" ])
204
205         # Now remove 'output-2.txt'
206         os.remove ('output-2.txt')