| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 1 | """Test that lldb functions correctly after the inferior has asserted.""" |
| 2 | |
| 3 | import os, time |
| 4 | import unittest2 |
| 5 | import lldb, lldbutil |
| 6 | from lldbtest import * |
| 7 | |
| 8 | class AssertingInferiorTestCase(TestBase): |
| 9 | |
| Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 10 | mydir = TestBase.compute_mydir(__file__) |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 11 | |
| 12 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| Enrico Granata | 4a2dc3b | 2013-10-31 23:05:35 +0000 | [diff] [blame] | 13 | @unittest2.expectedFailure("rdar://15367233") |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 14 | 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 Granata | 4a2dc3b | 2013-10-31 23:05:35 +0000 | [diff] [blame] | 19 | @expectedFailurei386 # llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly' |
| Ed Maste | ec4f47e | 2014-04-22 13:42:05 +0000 | [diff] [blame] | 20 | @expectedFailureDarwin("rdar://15367233") |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 21 | 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 Kopec | ee969f9 | 2013-09-26 23:30:59 +0000 | [diff] [blame] | 37 | @expectedFailurei386 # llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly |
| Ed Maste | f170add | 2014-01-18 16:23:30 +0000 | [diff] [blame] | 38 | @expectedFailureFreeBSD('llvm.org/pr18533') # PC in __assert frame is outside of function |
| Todd Fiala | 31cb474 | 2014-02-01 00:48:34 +0000 | [diff] [blame] | 39 | @expectedFailureLinux('') # PC in __GI___assert_fail frame is just after the function (this is a no-return so there is no epilogue afterwards) |
| Ashok Thirumurthi | cd20ee8 | 2013-07-23 17:20:17 +0000 | [diff] [blame] | 40 | def test_inferior_asserting_disassemble(self): |
| Ed Maste | 34bdbbd | 2013-09-13 15:34:59 +0000 | [diff] [blame] | 41 | """Test that lldb reliably disassembles frames after asserting (command).""" |
| Ashok Thirumurthi | cd20ee8 | 2013-07-23 17:20:17 +0000 | [diff] [blame] | 42 | self.buildDefault() |
| 43 | self.inferior_asserting_disassemble() |
| 44 | |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 45 | @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 Granata | 4510a15 | 2013-10-31 23:06:54 +0000 | [diff] [blame] | 52 | @unittest2.expectedFailure("rdar://15367233") |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 53 | 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 Kopec | ee969f9 | 2013-09-26 23:30:59 +0000 | [diff] [blame] | 58 | @expectedFailurei386 # llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly |
| Enrico Granata | 4510a15 | 2013-10-31 23:06:54 +0000 | [diff] [blame] | 59 | @unittest2.expectedFailure("rdar://15367233") |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 60 | 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 Granata | 7037b3f | 2013-10-31 23:07:41 +0000 | [diff] [blame] | 66 | @unittest2.expectedFailure("rdar://15367233") |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 67 | 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 Kopec | ee969f9 | 2013-09-26 23:30:59 +0000 | [diff] [blame] | 72 | @expectedFailurei386 # llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly |
| Ed Maste | ec4f47e | 2014-04-22 13:42:05 +0000 | [diff] [blame] | 73 | @expectedFailureDarwin("rdar://15367233") |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 74 | 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 Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 124 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 125 | |
| 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 Thirumurthi | cd20ee8 | 2013-07-23 17:20:17 +0000 | [diff] [blame] | 150 | 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 Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 159 | target.LaunchSimple (None, None, self.get_process_working_directory()) |
| Ashok Thirumurthi | cd20ee8 | 2013-07-23 17:20:17 +0000 | [diff] [blame] | 160 | 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 Clayton | 9485dcf | 2014-01-10 22:22:44 +0000 | [diff] [blame] | 174 | # 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 Molenda | aff1b35 | 2014-10-10 23:07:36 +0000 | [diff] [blame^] | 179 | 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 Thirumurthi | cd20ee8 | 2013-07-23 17:20:17 +0000 | [diff] [blame] | 184 | |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 185 | 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 Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 212 | target.LaunchSimple (None, None, self.get_process_working_directory()) |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 213 | 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 Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 234 | target.LaunchSimple (None, None, self.get_process_working_directory()) |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 235 | |
| 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 | |
| 263 | if __name__ == '__main__': |
| 264 | import atexit |
| 265 | lldb.SBDebugger.Initialize() |
| 266 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 267 | unittest2.main() |