| 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 | |
| 10 | mydir = os.path.join("functionalities", "inferior-assert") |
| 11 | |
| 12 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 13 | def test_inferior_asserting_dsym(self): |
| 14 | """Test that lldb reliably catches the inferior asserting (command).""" |
| 15 | self.buildDsym() |
| 16 | self.inferior_asserting() |
| 17 | |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 18 | def test_inferior_asserting_dwarf(self): |
| 19 | """Test that lldb reliably catches the inferior asserting (command).""" |
| 20 | self.buildDwarf() |
| 21 | self.inferior_asserting() |
| 22 | |
| 23 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 24 | def test_inferior_asserting_registers_dsym(self): |
| 25 | """Test that lldb reliably reads registers from the inferior after asserting (command).""" |
| 26 | self.buildDsym() |
| 27 | self.inferior_asserting_registers() |
| 28 | |
| Ed Maste | 0afb78a | 2013-09-10 17:15:05 +0000 | [diff] [blame] | 29 | @expectedFailureFreeBSD('llvm.org/pr17184') |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 30 | def test_inferior_asserting_register_dwarf(self): |
| 31 | """Test that lldb reliably reads registers from the inferior after asserting (command).""" |
| 32 | self.buildDwarf() |
| 33 | self.inferior_asserting_registers() |
| 34 | |
| Ashok Thirumurthi | cd20ee8 | 2013-07-23 17:20:17 +0000 | [diff] [blame] | 35 | @skipIfGcc # Avoid xpasses as the verion of libc used on the gcc buildbot has the required function symbols. |
| Ed Maste | 34bdbbd | 2013-09-13 15:34:59 +0000 | [diff] [blame^] | 36 | @expectedFailureFreeBSD # ResolveSymbolContextForAddress can fail using ELF with stripped function symbols. |
| Ashok Thirumurthi | cd20ee8 | 2013-07-23 17:20:17 +0000 | [diff] [blame] | 37 | @expectedFailureLinux # ResolveSymbolContextForAddress can fail using ELF with stripped function symbols. |
| 38 | def test_inferior_asserting_disassemble(self): |
| Ed Maste | 34bdbbd | 2013-09-13 15:34:59 +0000 | [diff] [blame^] | 39 | """Test that lldb reliably disassembles frames after asserting (command).""" |
| Ashok Thirumurthi | cd20ee8 | 2013-07-23 17:20:17 +0000 | [diff] [blame] | 40 | self.buildDefault() |
| 41 | self.inferior_asserting_disassemble() |
| 42 | |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 43 | @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") |
| 50 | def test_inferior_asserting_expr(self): |
| 51 | """Test that the lldb expression interpreter can read from the inferior after asserting (command).""" |
| 52 | self.buildDsym() |
| 53 | self.inferior_asserting_expr() |
| 54 | |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 55 | def test_inferior_asserting_expr(self): |
| 56 | """Test that the lldb expression interpreter can read from the inferior after asserting (command).""" |
| 57 | self.buildDwarf() |
| 58 | self.inferior_asserting_expr() |
| 59 | |
| 60 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 61 | def test_inferior_asserting_step(self): |
| 62 | """Test that lldb functions correctly after stepping through a call to assert().""" |
| 63 | self.buildDsym() |
| 64 | self.inferior_asserting_step() |
| 65 | |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 66 | def test_inferior_asserting_step(self): |
| 67 | """Test that lldb functions correctly after stepping through a call to assert().""" |
| 68 | self.buildDwarf() |
| 69 | self.inferior_asserting_step() |
| 70 | |
| 71 | def set_breakpoint(self, line): |
| 72 | lldbutil.run_break_set_by_file_and_line (self, "main.c", line, num_expected_locations=1, loc_exact=True) |
| 73 | |
| 74 | def check_stop_reason(self): |
| 75 | stop_reason = 'stop reason = signal SIGABRT' |
| 76 | |
| 77 | # The stop reason of the thread should be an abort signal or exception. |
| 78 | self.expect("thread list", STOPPED_DUE_TO_ASSERT, |
| 79 | substrs = ['stopped', |
| 80 | stop_reason]) |
| 81 | |
| 82 | return stop_reason |
| 83 | |
| 84 | def setUp(self): |
| 85 | # Call super's setUp(). |
| 86 | TestBase.setUp(self) |
| 87 | # Find the line number of the call to assert. |
| 88 | self.line = line_number('main.c', '// Assert here.') |
| 89 | |
| 90 | def inferior_asserting(self): |
| 91 | """Inferior asserts upon launching; lldb should catch the event and stop.""" |
| 92 | exe = os.path.join(os.getcwd(), "a.out") |
| 93 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 94 | |
| 95 | self.runCmd("run", RUN_SUCCEEDED) |
| 96 | stop_reason = self.check_stop_reason() |
| 97 | |
| 98 | # And it should report a backtrace that includes the assert site. |
| 99 | self.expect("thread backtrace all", |
| 100 | substrs = [stop_reason, 'main', 'argc', 'argv']) |
| 101 | |
| 102 | # And it should report the correct line number. |
| 103 | self.expect("thread backtrace all", |
| 104 | substrs = [stop_reason, |
| 105 | 'main.c:%d' % self.line]) |
| 106 | |
| 107 | def inferior_asserting_python(self): |
| 108 | """Inferior asserts upon launching; lldb should catch the event and stop.""" |
| 109 | exe = os.path.join(os.getcwd(), "a.out") |
| 110 | |
| 111 | target = self.dbg.CreateTarget(exe) |
| 112 | self.assertTrue(target, VALID_TARGET) |
| 113 | |
| 114 | # Now launch the process, and do not stop at entry point. |
| 115 | # Both argv and envp are null. |
| 116 | process = target.LaunchSimple(None, None, os.getcwd()) |
| 117 | |
| 118 | if process.GetState() != lldb.eStateStopped: |
| 119 | self.fail("Process should be in the 'stopped' state, " |
| 120 | "instead the actual state is: '%s'" % |
| 121 | lldbutil.state_type_to_str(process.GetState())) |
| 122 | |
| 123 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal) |
| 124 | if not thread: |
| 125 | self.fail("Fail to stop the thread upon assert") |
| 126 | |
| 127 | if self.TraceOn(): |
| 128 | lldbutil.print_stacktrace(thread) |
| 129 | |
| 130 | def inferior_asserting_registers(self): |
| 131 | """Test that lldb can read registers after asserting.""" |
| 132 | exe = os.path.join(os.getcwd(), "a.out") |
| 133 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 134 | |
| 135 | self.runCmd("run", RUN_SUCCEEDED) |
| 136 | self.check_stop_reason() |
| 137 | |
| 138 | # lldb should be able to read from registers from the inferior after asserting. |
| 139 | self.expect("register read eax", |
| 140 | substrs = ['eax = 0x']) |
| 141 | |
| Ashok Thirumurthi | cd20ee8 | 2013-07-23 17:20:17 +0000 | [diff] [blame] | 142 | def inferior_asserting_disassemble(self): |
| 143 | """Test that lldb can disassemble frames after asserting.""" |
| 144 | exe = os.path.join(os.getcwd(), "a.out") |
| 145 | |
| 146 | # Create a target by the debugger. |
| 147 | target = self.dbg.CreateTarget(exe) |
| 148 | self.assertTrue(target, VALID_TARGET) |
| 149 | |
| 150 | # Launch the process, and do not stop at the entry point. |
| 151 | target.LaunchSimple(None, None, os.getcwd()) |
| 152 | self.check_stop_reason() |
| 153 | |
| 154 | process = target.GetProcess() |
| 155 | self.assertTrue(process.IsValid(), "current process is valid") |
| 156 | |
| 157 | thread = process.GetThreadAtIndex(0) |
| 158 | self.assertTrue(thread.IsValid(), "current thread is valid") |
| 159 | |
| 160 | # lldb should be able to disassemble frames from the inferior after asserting. |
| 161 | for frame in thread: |
| 162 | self.assertTrue(frame.IsValid(), "current frame is valid") |
| 163 | |
| 164 | self.runCmd("frame select " + str(frame.GetFrameID()), RUN_SUCCEEDED) |
| 165 | |
| 166 | self.expect("disassemble -a %s" % frame.GetPC(), |
| 167 | substrs = ['->', frame.GetFunctionName()]) |
| 168 | |
| Ashok Thirumurthi | b4e5134 | 2013-05-17 15:35:15 +0000 | [diff] [blame] | 169 | def check_expr_in_main(self, thread): |
| 170 | depth = thread.GetNumFrames() |
| 171 | for i in range(depth): |
| 172 | frame = thread.GetFrameAtIndex(i) |
| 173 | self.assertTrue(frame.IsValid(), "current frame is valid") |
| 174 | if self.TraceOn(): |
| 175 | print "Checking if function %s is main" % frame.GetFunctionName() |
| 176 | |
| 177 | if 'main' == frame.GetFunctionName(): |
| 178 | frame_id = frame.GetFrameID() |
| 179 | self.runCmd("frame select " + str(frame_id), RUN_SUCCEEDED) |
| 180 | self.expect("p argc", substrs = ['(int)', ' = 1']) |
| 181 | self.expect("p hello_world", substrs = ['Hello']) |
| 182 | self.expect("p argv[0]", substrs = ['a.out']) |
| 183 | self.expect("p null_ptr", substrs = ['= 0x0']) |
| 184 | return True |
| 185 | return False |
| 186 | |
| 187 | def inferior_asserting_expr(self): |
| 188 | """Test that the lldb expression interpreter can read symbols after asserting.""" |
| 189 | exe = os.path.join(os.getcwd(), "a.out") |
| 190 | |
| 191 | # Create a target by the debugger. |
| 192 | target = self.dbg.CreateTarget(exe) |
| 193 | self.assertTrue(target, VALID_TARGET) |
| 194 | |
| 195 | # Launch the process, and do not stop at the entry point. |
| 196 | target.LaunchSimple(None, None, os.getcwd()) |
| 197 | self.check_stop_reason() |
| 198 | |
| 199 | process = target.GetProcess() |
| 200 | self.assertTrue(process.IsValid(), "current process is valid") |
| 201 | |
| 202 | thread = process.GetThreadAtIndex(0) |
| 203 | self.assertTrue(thread.IsValid(), "current thread is valid") |
| 204 | |
| 205 | # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert(). |
| 206 | self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace") |
| 207 | |
| 208 | def inferior_asserting_step(self): |
| 209 | """Test that lldb functions correctly after stepping through a call to assert().""" |
| 210 | exe = os.path.join(os.getcwd(), "a.out") |
| 211 | |
| 212 | # Create a target by the debugger. |
| 213 | target = self.dbg.CreateTarget(exe) |
| 214 | self.assertTrue(target, VALID_TARGET) |
| 215 | |
| 216 | # Launch the process, and do not stop at the entry point. |
| 217 | self.set_breakpoint(self.line) |
| 218 | target.LaunchSimple(None, None, os.getcwd()) |
| 219 | |
| 220 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
| 221 | substrs = ['main.c:%d' % self.line, |
| 222 | 'stop reason = breakpoint']) |
| 223 | |
| 224 | self.runCmd("next") |
| 225 | stop_reason = self.check_stop_reason() |
| 226 | |
| 227 | # lldb should be able to read from registers from the inferior after asserting. |
| 228 | if "x86_64" in self.getArchitecture(): |
| 229 | self.expect("register read rbp", substrs = ['rbp = 0x']) |
| 230 | if "i386" in self.getArchitecture(): |
| 231 | self.expect("register read ebp", substrs = ['ebp = 0x']) |
| 232 | |
| 233 | process = target.GetProcess() |
| 234 | self.assertTrue(process.IsValid(), "current process is valid") |
| 235 | |
| 236 | thread = process.GetThreadAtIndex(0) |
| 237 | self.assertTrue(thread.IsValid(), "current thread is valid") |
| 238 | |
| 239 | # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert(). |
| 240 | self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace") |
| 241 | |
| 242 | # And it should report the correct line number. |
| 243 | self.expect("thread backtrace all", |
| 244 | substrs = [stop_reason, |
| 245 | 'main.c:%d' % self.line]) |
| 246 | |
| 247 | if __name__ == '__main__': |
| 248 | import atexit |
| 249 | lldb.SBDebugger.Initialize() |
| 250 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 251 | unittest2.main() |