1 """Test breakpoint on a class constructor; and variable list the this object."""
3 from __future__ import print_function
9 from lldbsuite.test.decorators import *
10 from lldbsuite.test.lldbtest import *
11 from lldbsuite.test import lldbutil
13 class ClassTypesTestCase(TestBase):
15 mydir = TestBase.compute_mydir(__file__)
18 # Call super's setUp().
20 # Find the line number to break for main.cpp.
21 self.line = line_number('main.cpp', '// Set break point at this line.')
23 def test_with_run_command(self):
24 """Test 'frame variable this' when stopped on a class constructor."""
26 exe = os.path.join(os.getcwd(), "a.out")
27 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
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)
32 self.runCmd("run", RUN_SUCCEEDED)
34 # The test suite sometimes shows that the process has exited without stopping.
36 # CC=clang ./dotest.py -v -t class_types
38 # Process 76604 exited with status = 0 (0x00000000)
39 self.runCmd("process status")
41 # The stop reason of the thread should be breakpoint.
42 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
44 'stop reason = breakpoint'])
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'])
50 # We should be stopped on the ctor function of class C.
51 self.expect("frame variable --show-types this", VARIABLES_DISPLAYED_CORRECTLY,
55 @add_test_categories(['pyapi'])
56 def test_with_python_api(self):
57 """Use Python APIs to create a breakpoint by (filespec, line)."""
59 exe = os.path.join(os.getcwd(), "a.out")
61 target = self.dbg.CreateTarget(exe)
62 self.assertTrue(target, VALID_TARGET)
64 filespec = target.GetExecutable()
65 self.assertTrue(filespec, VALID_FILESPEC)
67 fsDir = os.path.normpath(filespec.GetDirectory())
68 fsFile = filespec.GetFilename()
70 self.assertTrue(fsDir == os.getcwd() and fsFile == "a.out",
71 "FileSpec matches the executable")
73 bpfilespec = lldb.SBFileSpec("main.cpp", False)
75 breakpoint = target.BreakpointCreateByLocation(bpfilespec, self.line)
76 self.assertTrue(breakpoint, VALID_BREAKPOINT)
78 # Verify the breakpoint just created.
79 self.expect(str(breakpoint), BREAKPOINT_CREATED, exe=False,
80 substrs = ['main.cpp',
83 # Now launch the process, and do not stop at entry point.
84 process = target.LaunchSimple (None, None, self.get_process_working_directory())
87 self.fail("SBTarget.Launch() failed")
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()))
94 # The stop reason of the thread should be breakpoint.
95 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
96 self.assertIsNotNone(thread)
98 # The filename of frame #0 should be 'main.cpp' and the line number
100 self.expect("%s:%d" % (lldbutil.get_filenames(thread)[0],
101 lldbutil.get_line_numbers(thread)[0]),
102 "Break correctly at main.cpp:%d" % self.line, exe=False,
103 startstr = "main.cpp:")
104 ### clang compiled code reported main.cpp:94?
105 ### startstr = "main.cpp:93")
107 # We should be stopped on the breakpoint with a hit count of 1.
108 self.assertTrue(breakpoint.GetHitCount() == 1, BREAKPOINT_HIT_ONCE)
112 def test_with_expr_parser(self):
113 """Test 'frame variable this' and 'expr this' when stopped inside a constructor."""
115 exe = os.path.join(os.getcwd(), "a.out")
116 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
118 # rdar://problem/8516141
119 # Is this a case of clang (116.1) generating bad debug info?
121 # Break on the ctor function of class C.
122 #self.expect("breakpoint set -M C", BREAKPOINT_CREATED,
123 # startstr = "Breakpoint created: 1: name = 'C'")
125 # Make the test case more robust by using line number to break, instead.
126 lldbutil.run_break_set_by_file_and_line (self, None, self.line, num_expected_locations=-1)
128 self.runCmd("run", RUN_SUCCEEDED)
130 # The stop reason of the thread should be breakpoint.
131 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
132 substrs = ['stopped',
133 'stop reason = breakpoint'])
135 # The breakpoint should have a hit count of 1.
136 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
137 substrs = [' resolved, hit count = 1'])
139 # Continue on inside the ctor() body...
140 self.runCmd("register read pc")
141 self.runCmd("thread step-over")
143 # Verify that 'frame variable this' gets the data type correct.
144 self.expect("frame variable this",VARIABLES_DISPLAYED_CORRECTLY,
147 # Verify that frame variable --show-types this->m_c_int behaves correctly.
148 self.runCmd("register read pc")
149 self.runCmd("expr m_c_int")
150 self.expect("frame variable --show-types this->m_c_int", VARIABLES_DISPLAYED_CORRECTLY,
151 startstr = '(int) this->m_c_int = 66')
153 # Verify that 'expression this' gets the data type correct.
154 self.expect("expression this", VARIABLES_DISPLAYED_CORRECTLY,
157 # rdar://problem/8430916
158 # expr this->m_c_int returns an incorrect value
160 # Verify that expr this->m_c_int behaves correctly.
161 self.expect("expression this->m_c_int", VARIABLES_DISPLAYED_CORRECTLY,
162 patterns = ['\(int\) \$[0-9]+ = 66'])
164 def test_with_constructor_name (self):
165 """Test 'frame variable this' and 'expr this' when stopped inside a constructor."""
167 exe = os.path.join(os.getcwd(), "a.out")
169 target = self.dbg.CreateTarget(exe)
170 self.assertTrue(target, VALID_TARGET)
172 filespec = target.GetExecutable()
173 self.assertTrue(filespec, VALID_FILESPEC)
175 fsDir = os.path.normpath(filespec.GetDirectory())
176 fsFile = filespec.GetFilename()
178 self.assertTrue(fsDir == os.getcwd() and fsFile == "a.out",
179 "FileSpec matches the executable")
181 bpfilespec = lldb.SBFileSpec("main.cpp", False)
183 breakpoint = target.BreakpointCreateByLocation(bpfilespec, self.line)
184 self.assertTrue(breakpoint, VALID_BREAKPOINT)
186 # Verify the breakpoint just created.
187 self.expect(str(breakpoint), BREAKPOINT_CREATED, exe=False,
188 substrs = ['main.cpp',
191 # Now launch the process, and do not stop at entry point.
192 process = target.LaunchSimple (None, None, self.get_process_working_directory())
195 self.fail("SBTarget.Launch() failed")
197 if process.GetState() != lldb.eStateStopped:
198 self.fail("Process should be in the 'stopped' state, "
199 "instead the actual state is: '%s'" %
200 lldbutil.state_type_to_str(process.GetState()))
202 # The stop reason of the thread should be breakpoint.
203 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
204 self.assertIsNotNone(thread)
206 frame = thread.frames[0]
207 self.assertTrue (frame.IsValid(), "Got a valid frame.")
209 self.assertTrue ("C::C" in frame.name, "Constructor name includes class name.")