blob: f8ed37249b9985452aa06239e02fc76e16b2acde [file] [log] [blame]
Ashok Thirumurthib4e51342013-05-17 15:35:15 +00001"""Test that lldb functions correctly after the inferior has asserted."""
2
3import os, time
4import unittest2
5import lldb, lldbutil
6from lldbtest import *
7
8class AssertingInferiorTestCase(TestBase):
9
Greg Clayton4570d3e2013-12-10 23:19:29 +000010 mydir = TestBase.compute_mydir(__file__)
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000011
12 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Enrico Granata4a2dc3b2013-10-31 23:05:35 +000013 @unittest2.expectedFailure("rdar://15367233")
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000014 def test_inferior_asserting_dsym(self):
15 """Test that lldb reliably catches the inferior asserting (command)."""
16 self.buildDsym()
17 self.inferior_asserting()
18
Enrico Granata4a2dc3b2013-10-31 23:05:35 +000019 @expectedFailurei386 # llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly'
Ed Masteec4f47e2014-04-22 13:42:05 +000020 @expectedFailureDarwin("rdar://15367233")
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000021 def test_inferior_asserting_dwarf(self):
22 """Test that lldb reliably catches the inferior asserting (command)."""
23 self.buildDwarf()
24 self.inferior_asserting()
25
26 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
27 def test_inferior_asserting_registers_dsym(self):
28 """Test that lldb reliably reads registers from the inferior after asserting (command)."""
29 self.buildDsym()
30 self.inferior_asserting_registers()
31
32 def test_inferior_asserting_register_dwarf(self):
33 """Test that lldb reliably reads registers from the inferior after asserting (command)."""
34 self.buildDwarf()
35 self.inferior_asserting_registers()
36
Matt Kopecee969f92013-09-26 23:30:59 +000037 @expectedFailurei386 # llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly
Ed Mastef170add2014-01-18 16:23:30 +000038 @expectedFailureFreeBSD('llvm.org/pr18533') # PC in __assert frame is outside of function
Todd Fiala31cb4742014-02-01 00:48:34 +000039 @expectedFailureLinux('') # PC in __GI___assert_fail frame is just after the function (this is a no-return so there is no epilogue afterwards)
Ashok Thirumurthicd20ee82013-07-23 17:20:17 +000040 def test_inferior_asserting_disassemble(self):
Ed Maste34bdbbd2013-09-13 15:34:59 +000041 """Test that lldb reliably disassembles frames after asserting (command)."""
Ashok Thirumurthicd20ee82013-07-23 17:20:17 +000042 self.buildDefault()
43 self.inferior_asserting_disassemble()
44
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000045 @python_api_test
46 def test_inferior_asserting_python(self):
47 """Test that lldb reliably catches the inferior asserting (Python API)."""
48 self.buildDefault()
49 self.inferior_asserting_python()
50
51 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Enrico Granata4510a152013-10-31 23:06:54 +000052 @unittest2.expectedFailure("rdar://15367233")
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000053 def test_inferior_asserting_expr(self):
54 """Test that the lldb expression interpreter can read from the inferior after asserting (command)."""
55 self.buildDsym()
56 self.inferior_asserting_expr()
57
Matt Kopecee969f92013-09-26 23:30:59 +000058 @expectedFailurei386 # llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly
Enrico Granata4510a152013-10-31 23:06:54 +000059 @unittest2.expectedFailure("rdar://15367233")
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000060 def test_inferior_asserting_expr(self):
61 """Test that the lldb expression interpreter can read from the inferior after asserting (command)."""
62 self.buildDwarf()
63 self.inferior_asserting_expr()
64
65 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Enrico Granata7037b3f2013-10-31 23:07:41 +000066 @unittest2.expectedFailure("rdar://15367233")
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000067 def test_inferior_asserting_step(self):
68 """Test that lldb functions correctly after stepping through a call to assert()."""
69 self.buildDsym()
70 self.inferior_asserting_step()
71
Matt Kopecee969f92013-09-26 23:30:59 +000072 @expectedFailurei386 # llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly
Ed Masteec4f47e2014-04-22 13:42:05 +000073 @expectedFailureDarwin("rdar://15367233")
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000074 def test_inferior_asserting_step(self):
75 """Test that lldb functions correctly after stepping through a call to assert()."""
76 self.buildDwarf()
77 self.inferior_asserting_step()
78
79 def set_breakpoint(self, line):
80 lldbutil.run_break_set_by_file_and_line (self, "main.c", line, num_expected_locations=1, loc_exact=True)
81
82 def check_stop_reason(self):
83 stop_reason = 'stop reason = signal SIGABRT'
84
85 # The stop reason of the thread should be an abort signal or exception.
86 self.expect("thread list", STOPPED_DUE_TO_ASSERT,
87 substrs = ['stopped',
88 stop_reason])
89
90 return stop_reason
91
92 def setUp(self):
93 # Call super's setUp().
94 TestBase.setUp(self)
95 # Find the line number of the call to assert.
96 self.line = line_number('main.c', '// Assert here.')
97
98 def inferior_asserting(self):
99 """Inferior asserts upon launching; lldb should catch the event and stop."""
100 exe = os.path.join(os.getcwd(), "a.out")
101 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
102
103 self.runCmd("run", RUN_SUCCEEDED)
104 stop_reason = self.check_stop_reason()
105
106 # And it should report a backtrace that includes the assert site.
107 self.expect("thread backtrace all",
108 substrs = [stop_reason, 'main', 'argc', 'argv'])
109
110 # And it should report the correct line number.
111 self.expect("thread backtrace all",
112 substrs = [stop_reason,
113 'main.c:%d' % self.line])
114
115 def inferior_asserting_python(self):
116 """Inferior asserts upon launching; lldb should catch the event and stop."""
117 exe = os.path.join(os.getcwd(), "a.out")
118
119 target = self.dbg.CreateTarget(exe)
120 self.assertTrue(target, VALID_TARGET)
121
122 # Now launch the process, and do not stop at entry point.
123 # Both argv and envp are null.
Greg Claytonc6947512013-12-13 19:18:59 +0000124 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Ashok Thirumurthib4e51342013-05-17 15:35:15 +0000125
126 if process.GetState() != lldb.eStateStopped:
127 self.fail("Process should be in the 'stopped' state, "
128 "instead the actual state is: '%s'" %
129 lldbutil.state_type_to_str(process.GetState()))
130
131 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal)
132 if not thread:
133 self.fail("Fail to stop the thread upon assert")
134
135 if self.TraceOn():
136 lldbutil.print_stacktrace(thread)
137
138 def inferior_asserting_registers(self):
139 """Test that lldb can read registers after asserting."""
140 exe = os.path.join(os.getcwd(), "a.out")
141 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
142
143 self.runCmd("run", RUN_SUCCEEDED)
144 self.check_stop_reason()
145
146 # lldb should be able to read from registers from the inferior after asserting.
147 self.expect("register read eax",
148 substrs = ['eax = 0x'])
149
Ashok Thirumurthicd20ee82013-07-23 17:20:17 +0000150 def inferior_asserting_disassemble(self):
151 """Test that lldb can disassemble frames after asserting."""
152 exe = os.path.join(os.getcwd(), "a.out")
153
154 # Create a target by the debugger.
155 target = self.dbg.CreateTarget(exe)
156 self.assertTrue(target, VALID_TARGET)
157
158 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000159 target.LaunchSimple (None, None, self.get_process_working_directory())
Ashok Thirumurthicd20ee82013-07-23 17:20:17 +0000160 self.check_stop_reason()
161
162 process = target.GetProcess()
163 self.assertTrue(process.IsValid(), "current process is valid")
164
165 thread = process.GetThreadAtIndex(0)
166 self.assertTrue(thread.IsValid(), "current thread is valid")
167
168 # lldb should be able to disassemble frames from the inferior after asserting.
169 for frame in thread:
170 self.assertTrue(frame.IsValid(), "current frame is valid")
171
172 self.runCmd("frame select " + str(frame.GetFrameID()), RUN_SUCCEEDED)
173
Greg Clayton9485dcf2014-01-10 22:22:44 +0000174 # Don't expect the function name to be in the disassembly as the assert
175 # function might be a no-return function where the PC is past the end
176 # of the function and in the next function. We also can't back the PC up
177 # because we don't know how much to back it up by on targets with opcodes
178 # that have differing sizes
Jason Molendaaff1b352014-10-10 23:07:36 +0000179 pc_backup_offset = 1
180 if frame.GetFrameID() == 0:
181 pc_backup_offset = 0
182 self.expect("disassemble -a %s" % (frame.GetPC() - pc_backup_offset),
183 substrs = ['<%s>:' % frame.GetFunctionName()])
Ashok Thirumurthicd20ee82013-07-23 17:20:17 +0000184
Ashok Thirumurthib4e51342013-05-17 15:35:15 +0000185 def check_expr_in_main(self, thread):
186 depth = thread.GetNumFrames()
187 for i in range(depth):
188 frame = thread.GetFrameAtIndex(i)
189 self.assertTrue(frame.IsValid(), "current frame is valid")
190 if self.TraceOn():
191 print "Checking if function %s is main" % frame.GetFunctionName()
192
193 if 'main' == frame.GetFunctionName():
194 frame_id = frame.GetFrameID()
195 self.runCmd("frame select " + str(frame_id), RUN_SUCCEEDED)
196 self.expect("p argc", substrs = ['(int)', ' = 1'])
197 self.expect("p hello_world", substrs = ['Hello'])
198 self.expect("p argv[0]", substrs = ['a.out'])
199 self.expect("p null_ptr", substrs = ['= 0x0'])
200 return True
201 return False
202
203 def inferior_asserting_expr(self):
204 """Test that the lldb expression interpreter can read symbols after asserting."""
205 exe = os.path.join(os.getcwd(), "a.out")
206
207 # Create a target by the debugger.
208 target = self.dbg.CreateTarget(exe)
209 self.assertTrue(target, VALID_TARGET)
210
211 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000212 target.LaunchSimple (None, None, self.get_process_working_directory())
Ashok Thirumurthib4e51342013-05-17 15:35:15 +0000213 self.check_stop_reason()
214
215 process = target.GetProcess()
216 self.assertTrue(process.IsValid(), "current process is valid")
217
218 thread = process.GetThreadAtIndex(0)
219 self.assertTrue(thread.IsValid(), "current thread is valid")
220
221 # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert().
222 self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace")
223
224 def inferior_asserting_step(self):
225 """Test that lldb functions correctly after stepping through a call to assert()."""
226 exe = os.path.join(os.getcwd(), "a.out")
227
228 # Create a target by the debugger.
229 target = self.dbg.CreateTarget(exe)
230 self.assertTrue(target, VALID_TARGET)
231
232 # Launch the process, and do not stop at the entry point.
233 self.set_breakpoint(self.line)
Greg Claytonc6947512013-12-13 19:18:59 +0000234 target.LaunchSimple (None, None, self.get_process_working_directory())
Ashok Thirumurthib4e51342013-05-17 15:35:15 +0000235
236 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
237 substrs = ['main.c:%d' % self.line,
238 'stop reason = breakpoint'])
239
240 self.runCmd("next")
241 stop_reason = self.check_stop_reason()
242
243 # lldb should be able to read from registers from the inferior after asserting.
244 if "x86_64" in self.getArchitecture():
245 self.expect("register read rbp", substrs = ['rbp = 0x'])
246 if "i386" in self.getArchitecture():
247 self.expect("register read ebp", substrs = ['ebp = 0x'])
248
249 process = target.GetProcess()
250 self.assertTrue(process.IsValid(), "current process is valid")
251
252 thread = process.GetThreadAtIndex(0)
253 self.assertTrue(thread.IsValid(), "current thread is valid")
254
255 # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert().
256 self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace")
257
258 # And it should report the correct line number.
259 self.expect("thread backtrace all",
260 substrs = [stop_reason,
261 'main.c:%d' % self.line])
262
263if __name__ == '__main__':
264 import atexit
265 lldb.SBDebugger.Initialize()
266 atexit.register(lambda: lldb.SBDebugger.Terminate())
267 unittest2.main()