blob: 9c6dd6f21ab8396f4ca5df7f8aba9bc933c3f263 [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'
20 @unittest2.expectedFailure("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
Ashok Thirumurthicd20ee82013-07-23 17:20:17 +000038 def test_inferior_asserting_disassemble(self):
Ed Maste34bdbbd2013-09-13 15:34:59 +000039 """Test that lldb reliably disassembles frames after asserting (command)."""
Ashok Thirumurthicd20ee82013-07-23 17:20:17 +000040 self.buildDefault()
41 self.inferior_asserting_disassemble()
42
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000043 @python_api_test
44 def test_inferior_asserting_python(self):
45 """Test that lldb reliably catches the inferior asserting (Python API)."""
46 self.buildDefault()
47 self.inferior_asserting_python()
48
49 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Enrico Granata4510a152013-10-31 23:06:54 +000050 @unittest2.expectedFailure("rdar://15367233")
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000051 def test_inferior_asserting_expr(self):
52 """Test that the lldb expression interpreter can read from the inferior after asserting (command)."""
53 self.buildDsym()
54 self.inferior_asserting_expr()
55
Matt Kopecee969f92013-09-26 23:30:59 +000056 @expectedFailurei386 # llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly
Enrico Granata4510a152013-10-31 23:06:54 +000057 @unittest2.expectedFailure("rdar://15367233")
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000058 def test_inferior_asserting_expr(self):
59 """Test that the lldb expression interpreter can read from the inferior after asserting (command)."""
60 self.buildDwarf()
61 self.inferior_asserting_expr()
62
63 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Enrico Granata7037b3f2013-10-31 23:07:41 +000064 @unittest2.expectedFailure("rdar://15367233")
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000065 def test_inferior_asserting_step(self):
66 """Test that lldb functions correctly after stepping through a call to assert()."""
67 self.buildDsym()
68 self.inferior_asserting_step()
69
Matt Kopecee969f92013-09-26 23:30:59 +000070 @expectedFailurei386 # llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly
Enrico Granata7037b3f2013-10-31 23:07:41 +000071 @unittest2.expectedFailure("rdar://15367233")
Ashok Thirumurthib4e51342013-05-17 15:35:15 +000072 def test_inferior_asserting_step(self):
73 """Test that lldb functions correctly after stepping through a call to assert()."""
74 self.buildDwarf()
75 self.inferior_asserting_step()
76
77 def set_breakpoint(self, line):
78 lldbutil.run_break_set_by_file_and_line (self, "main.c", line, num_expected_locations=1, loc_exact=True)
79
80 def check_stop_reason(self):
81 stop_reason = 'stop reason = signal SIGABRT'
82
83 # The stop reason of the thread should be an abort signal or exception.
84 self.expect("thread list", STOPPED_DUE_TO_ASSERT,
85 substrs = ['stopped',
86 stop_reason])
87
88 return stop_reason
89
90 def setUp(self):
91 # Call super's setUp().
92 TestBase.setUp(self)
93 # Find the line number of the call to assert.
94 self.line = line_number('main.c', '// Assert here.')
95
96 def inferior_asserting(self):
97 """Inferior asserts upon launching; lldb should catch the event and stop."""
98 exe = os.path.join(os.getcwd(), "a.out")
99 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
100
101 self.runCmd("run", RUN_SUCCEEDED)
102 stop_reason = self.check_stop_reason()
103
104 # And it should report a backtrace that includes the assert site.
105 self.expect("thread backtrace all",
106 substrs = [stop_reason, 'main', 'argc', 'argv'])
107
108 # And it should report the correct line number.
109 self.expect("thread backtrace all",
110 substrs = [stop_reason,
111 'main.c:%d' % self.line])
112
113 def inferior_asserting_python(self):
114 """Inferior asserts upon launching; lldb should catch the event and stop."""
115 exe = os.path.join(os.getcwd(), "a.out")
116
117 target = self.dbg.CreateTarget(exe)
118 self.assertTrue(target, VALID_TARGET)
119
120 # Now launch the process, and do not stop at entry point.
121 # Both argv and envp are null.
122 process = target.LaunchSimple(None, None, os.getcwd())
123
124 if process.GetState() != lldb.eStateStopped:
125 self.fail("Process should be in the 'stopped' state, "
126 "instead the actual state is: '%s'" %
127 lldbutil.state_type_to_str(process.GetState()))
128
129 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal)
130 if not thread:
131 self.fail("Fail to stop the thread upon assert")
132
133 if self.TraceOn():
134 lldbutil.print_stacktrace(thread)
135
136 def inferior_asserting_registers(self):
137 """Test that lldb can read registers after asserting."""
138 exe = os.path.join(os.getcwd(), "a.out")
139 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
140
141 self.runCmd("run", RUN_SUCCEEDED)
142 self.check_stop_reason()
143
144 # lldb should be able to read from registers from the inferior after asserting.
145 self.expect("register read eax",
146 substrs = ['eax = 0x'])
147
Ashok Thirumurthicd20ee82013-07-23 17:20:17 +0000148 def inferior_asserting_disassemble(self):
149 """Test that lldb can disassemble frames after asserting."""
150 exe = os.path.join(os.getcwd(), "a.out")
151
152 # Create a target by the debugger.
153 target = self.dbg.CreateTarget(exe)
154 self.assertTrue(target, VALID_TARGET)
155
156 # Launch the process, and do not stop at the entry point.
157 target.LaunchSimple(None, None, os.getcwd())
158 self.check_stop_reason()
159
160 process = target.GetProcess()
161 self.assertTrue(process.IsValid(), "current process is valid")
162
163 thread = process.GetThreadAtIndex(0)
164 self.assertTrue(thread.IsValid(), "current thread is valid")
165
166 # lldb should be able to disassemble frames from the inferior after asserting.
167 for frame in thread:
168 self.assertTrue(frame.IsValid(), "current frame is valid")
169
170 self.runCmd("frame select " + str(frame.GetFrameID()), RUN_SUCCEEDED)
171
Ashok Thirumurthi5e644d92013-09-24 18:03:57 +0000172 # TODO: Disassembly does not specify '->' for the PC for a non-terminal frame for a function with a tail call.
Ashok Thirumurthicd20ee82013-07-23 17:20:17 +0000173 self.expect("disassemble -a %s" % frame.GetPC(),
Ashok Thirumurthi5e644d92013-09-24 18:03:57 +0000174 substrs = [frame.GetFunctionName()])
Ashok Thirumurthicd20ee82013-07-23 17:20:17 +0000175
Ashok Thirumurthib4e51342013-05-17 15:35:15 +0000176 def check_expr_in_main(self, thread):
177 depth = thread.GetNumFrames()
178 for i in range(depth):
179 frame = thread.GetFrameAtIndex(i)
180 self.assertTrue(frame.IsValid(), "current frame is valid")
181 if self.TraceOn():
182 print "Checking if function %s is main" % frame.GetFunctionName()
183
184 if 'main' == frame.GetFunctionName():
185 frame_id = frame.GetFrameID()
186 self.runCmd("frame select " + str(frame_id), RUN_SUCCEEDED)
187 self.expect("p argc", substrs = ['(int)', ' = 1'])
188 self.expect("p hello_world", substrs = ['Hello'])
189 self.expect("p argv[0]", substrs = ['a.out'])
190 self.expect("p null_ptr", substrs = ['= 0x0'])
191 return True
192 return False
193
194 def inferior_asserting_expr(self):
195 """Test that the lldb expression interpreter can read symbols after asserting."""
196 exe = os.path.join(os.getcwd(), "a.out")
197
198 # Create a target by the debugger.
199 target = self.dbg.CreateTarget(exe)
200 self.assertTrue(target, VALID_TARGET)
201
202 # Launch the process, and do not stop at the entry point.
203 target.LaunchSimple(None, None, os.getcwd())
204 self.check_stop_reason()
205
206 process = target.GetProcess()
207 self.assertTrue(process.IsValid(), "current process is valid")
208
209 thread = process.GetThreadAtIndex(0)
210 self.assertTrue(thread.IsValid(), "current thread is valid")
211
212 # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert().
213 self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace")
214
215 def inferior_asserting_step(self):
216 """Test that lldb functions correctly after stepping through a call to assert()."""
217 exe = os.path.join(os.getcwd(), "a.out")
218
219 # Create a target by the debugger.
220 target = self.dbg.CreateTarget(exe)
221 self.assertTrue(target, VALID_TARGET)
222
223 # Launch the process, and do not stop at the entry point.
224 self.set_breakpoint(self.line)
225 target.LaunchSimple(None, None, os.getcwd())
226
227 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
228 substrs = ['main.c:%d' % self.line,
229 'stop reason = breakpoint'])
230
231 self.runCmd("next")
232 stop_reason = self.check_stop_reason()
233
234 # lldb should be able to read from registers from the inferior after asserting.
235 if "x86_64" in self.getArchitecture():
236 self.expect("register read rbp", substrs = ['rbp = 0x'])
237 if "i386" in self.getArchitecture():
238 self.expect("register read ebp", substrs = ['ebp = 0x'])
239
240 process = target.GetProcess()
241 self.assertTrue(process.IsValid(), "current process is valid")
242
243 thread = process.GetThreadAtIndex(0)
244 self.assertTrue(thread.IsValid(), "current thread is valid")
245
246 # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert().
247 self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace")
248
249 # And it should report the correct line number.
250 self.expect("thread backtrace all",
251 substrs = [stop_reason,
252 'main.c:%d' % self.line])
253
254if __name__ == '__main__':
255 import atexit
256 lldb.SBDebugger.Initialize()
257 atexit.register(lambda: lldb.SBDebugger.Terminate())
258 unittest2.main()