]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/lang/cpp/class_types/TestClassTypes.py
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / lang / cpp / class_types / TestClassTypes.py
1 """Test breakpoint on a class constructor; and variable list the this object."""
2
3 from __future__ import print_function
4
5
6
7 import os, time
8 import lldb
9 import lldbsuite.test.lldbutil as lldbutil
10 from lldbsuite.test.lldbtest import *
11 import lldbsuite.test.lldbutil as lldbutil
12
13 class ClassTypesTestCase(TestBase):
14
15     mydir = TestBase.compute_mydir(__file__)
16
17     def setUp(self):
18         # Call super's setUp().
19         TestBase.setUp(self)
20         # Find the line number to break for main.cpp.
21         self.line = line_number('main.cpp', '// Set break point at this line.')
22
23     def test_with_run_command(self):
24         """Test 'frame variable this' when stopped on a class constructor."""
25         self.build()
26         exe = os.path.join(os.getcwd(), "a.out")
27         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
28
29         # Break on the ctor function of class C.
30         lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=-1)
31
32         self.runCmd("run", RUN_SUCCEEDED)
33
34         # The test suite sometimes shows that the process has exited without stopping.
35         #
36         # CC=clang ./dotest.py -v -t class_types
37         # ...
38         # Process 76604 exited with status = 0 (0x00000000)
39         self.runCmd("process status")
40
41         # The stop reason of the thread should be breakpoint.
42         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
43             substrs = ['stopped',
44                        'stop reason = breakpoint'])
45
46         # The breakpoint should have a hit count of 1.
47         self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
48             substrs = [' resolved, hit count = 1'])
49
50         # We should be stopped on the ctor function of class C.
51         self.expect("frame variable --show-types this", VARIABLES_DISPLAYED_CORRECTLY,
52             substrs = ['C *',
53                        ' this = '])
54
55     @add_test_categories(['pyapi'])
56     def test_with_python_api(self):
57         """Use Python APIs to create a breakpoint by (filespec, line)."""
58         self.build()
59         exe = os.path.join(os.getcwd(), "a.out")
60
61         target = self.dbg.CreateTarget(exe)
62         self.assertTrue(target, VALID_TARGET)
63
64         filespec = target.GetExecutable()
65         self.assertTrue(filespec, VALID_FILESPEC)
66
67         fsDir = os.path.normpath(filespec.GetDirectory())
68         fsFile = filespec.GetFilename()
69
70         self.assertTrue(fsDir == os.getcwd() and fsFile == "a.out",
71                         "FileSpec matches the executable")
72
73         bpfilespec = lldb.SBFileSpec("main.cpp", False)
74
75         breakpoint = target.BreakpointCreateByLocation(bpfilespec, self.line)
76         self.assertTrue(breakpoint, VALID_BREAKPOINT)
77
78         # Verify the breakpoint just created.
79         self.expect(str(breakpoint), BREAKPOINT_CREATED, exe=False,
80             substrs = ['main.cpp',
81                        str(self.line)])
82
83         # Now launch the process, and do not stop at entry point.
84         process = target.LaunchSimple (None, None, self.get_process_working_directory())
85
86         if not process:
87             self.fail("SBTarget.Launch() failed")
88
89         if process.GetState() != lldb.eStateStopped:
90             self.fail("Process should be in the 'stopped' state, "
91                       "instead the actual state is: '%s'" %
92                       lldbutil.state_type_to_str(process.GetState()))
93
94         # The stop reason of the thread should be breakpoint.
95         thread = process.GetThreadAtIndex(0)
96         if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
97             from lldbsuite.test.lldbutil import stop_reason_to_str
98             self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
99                       stop_reason_to_str(thread.GetStopReason()))
100
101         # The filename of frame #0 should be 'main.cpp' and the line number
102         # should be 93.
103         self.expect("%s:%d" % (lldbutil.get_filenames(thread)[0],
104                                lldbutil.get_line_numbers(thread)[0]),
105                     "Break correctly at main.cpp:%d" % self.line, exe=False,
106             startstr = "main.cpp:")
107             ### clang compiled code reported main.cpp:94?
108             ### startstr = "main.cpp:93")
109
110         # We should be stopped on the breakpoint with a hit count of 1.
111         self.assertTrue(breakpoint.GetHitCount() == 1, BREAKPOINT_HIT_ONCE)
112
113         process.Continue()
114
115     def test_with_expr_parser(self):
116         """Test 'frame variable this' and 'expr this' when stopped inside a constructor."""
117         self.build()
118         exe = os.path.join(os.getcwd(), "a.out")
119         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
120
121         # rdar://problem/8516141
122         # Is this a case of clang (116.1) generating bad debug info?
123         #
124         # Break on the ctor function of class C.
125         #self.expect("breakpoint set -M C", BREAKPOINT_CREATED,
126         #    startstr = "Breakpoint created: 1: name = 'C'")
127
128         # Make the test case more robust by using line number to break, instead.
129         lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=-1)
130
131         self.runCmd("run", RUN_SUCCEEDED)
132
133         # The stop reason of the thread should be breakpoint.
134         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
135             substrs = ['stopped',
136                        'stop reason = breakpoint'])
137
138         # The breakpoint should have a hit count of 1.
139         self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
140             substrs = [' resolved, hit count = 1'])
141
142         # Continue on inside the ctor() body...
143         self.runCmd("register read pc")
144         self.runCmd("thread step-over")
145
146         # Verify that 'frame variable this' gets the data type correct.
147         self.expect("frame variable this",VARIABLES_DISPLAYED_CORRECTLY,
148             substrs = ['C *'])
149
150         # Verify that frame variable --show-types this->m_c_int behaves correctly.
151         self.runCmd("register read pc")
152         self.runCmd("expr m_c_int")
153         self.expect("frame variable --show-types this->m_c_int", VARIABLES_DISPLAYED_CORRECTLY,
154             startstr = '(int) this->m_c_int = 66')
155
156         # Verify that 'expression this' gets the data type correct.
157         self.expect("expression this", VARIABLES_DISPLAYED_CORRECTLY,
158             substrs = ['C *'])
159
160         # rdar://problem/8430916
161         # expr this->m_c_int returns an incorrect value
162         #
163         # Verify that expr this->m_c_int behaves correctly.
164         self.expect("expression this->m_c_int", VARIABLES_DISPLAYED_CORRECTLY,
165             patterns = ['\(int\) \$[0-9]+ = 66'])
166
167     def test_with_constructor_name (self):
168         """Test 'frame variable this' and 'expr this' when stopped inside a constructor."""
169         self.build()
170         exe = os.path.join(os.getcwd(), "a.out")
171
172         target = self.dbg.CreateTarget(exe)
173         self.assertTrue(target, VALID_TARGET)
174
175         filespec = target.GetExecutable()
176         self.assertTrue(filespec, VALID_FILESPEC)
177
178         fsDir = os.path.normpath(filespec.GetDirectory())
179         fsFile = filespec.GetFilename()
180
181         self.assertTrue(fsDir == os.getcwd() and fsFile == "a.out",
182                         "FileSpec matches the executable")
183
184         bpfilespec = lldb.SBFileSpec("main.cpp", False)
185
186         breakpoint = target.BreakpointCreateByLocation(bpfilespec, self.line)
187         self.assertTrue(breakpoint, VALID_BREAKPOINT)
188
189         # Verify the breakpoint just created.
190         self.expect(str(breakpoint), BREAKPOINT_CREATED, exe=False,
191             substrs = ['main.cpp',
192                        str(self.line)])
193
194         # Now launch the process, and do not stop at entry point.
195         process = target.LaunchSimple (None, None, self.get_process_working_directory())
196
197         if not process:
198             self.fail("SBTarget.Launch() failed")
199
200         if process.GetState() != lldb.eStateStopped:
201             self.fail("Process should be in the 'stopped' state, "
202                       "instead the actual state is: '%s'" %
203                       lldbutil.state_type_to_str(process.GetState()))
204
205         # The stop reason of the thread should be breakpoint.
206         thread = process.GetThreadAtIndex(0)
207         if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
208             from lldbsuite.test.lldbutil import stop_reason_to_str
209             self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
210                       stop_reason_to_str(thread.GetStopReason()))
211
212         frame = thread.frames[0]
213         self.assertTrue (frame.IsValid(), "Got a valid frame.")
214
215         self.assertTrue ("C::C" in frame.name, "Constructor name includes class name.")