| 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 | |
| 18 | @expectedFailureLinux # bugzilla 15671 - backtrace does not include the assert site |
| 19 | def test_inferior_asserting_dwarf(self): |
| 20 | """Test that lldb reliably catches the inferior asserting (command).""" |
| 21 | self.buildDwarf() |
| 22 | self.inferior_asserting() |
| 23 | |
| 24 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 25 | def test_inferior_asserting_registers_dsym(self): |
| 26 | """Test that lldb reliably reads registers from the inferior after asserting (command).""" |
| 27 | self.buildDsym() |
| 28 | self.inferior_asserting_registers() |
| 29 | |
| 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 | |
| 35 | @python_api_test |
| 36 | def test_inferior_asserting_python(self): |
| 37 | """Test that lldb reliably catches the inferior asserting (Python API).""" |
| 38 | self.buildDefault() |
| 39 | self.inferior_asserting_python() |
| 40 | |
| 41 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 42 | def test_inferior_asserting_expr(self): |
| 43 | """Test that the lldb expression interpreter can read from the inferior after asserting (command).""" |
| 44 | self.buildDsym() |
| 45 | self.inferior_asserting_expr() |
| 46 | |
| 47 | @expectedFailureLinux # bugzilla 15671 - backtrace does not include the assert site |
| 48 | def test_inferior_asserting_expr(self): |
| 49 | """Test that the lldb expression interpreter can read from the inferior after asserting (command).""" |
| 50 | self.buildDwarf() |
| 51 | self.inferior_asserting_expr() |
| 52 | |
| 53 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 54 | def test_inferior_asserting_step(self): |
| 55 | """Test that lldb functions correctly after stepping through a call to assert().""" |
| 56 | self.buildDsym() |
| 57 | self.inferior_asserting_step() |
| 58 | |
| 59 | @expectedFailureLinux # bugzilla 15671 - backtrace does not include the assert site |
| 60 | def test_inferior_asserting_step(self): |
| 61 | """Test that lldb functions correctly after stepping through a call to assert().""" |
| 62 | self.buildDwarf() |
| 63 | self.inferior_asserting_step() |
| 64 | |
| 65 | def set_breakpoint(self, line): |
| 66 | lldbutil.run_break_set_by_file_and_line (self, "main.c", line, num_expected_locations=1, loc_exact=True) |
| 67 | |
| 68 | def check_stop_reason(self): |
| 69 | stop_reason = 'stop reason = signal SIGABRT' |
| 70 | |
| 71 | # The stop reason of the thread should be an abort signal or exception. |
| 72 | self.expect("thread list", STOPPED_DUE_TO_ASSERT, |
| 73 | substrs = ['stopped', |
| 74 | stop_reason]) |
| 75 | |
| 76 | return stop_reason |
| 77 | |
| 78 | def setUp(self): |
| 79 | # Call super's setUp(). |
| 80 | TestBase.setUp(self) |
| 81 | # Find the line number of the call to assert. |
| 82 | self.line = line_number('main.c', '// Assert here.') |
| 83 | |
| 84 | def inferior_asserting(self): |
| 85 | """Inferior asserts upon launching; lldb should catch the event and stop.""" |
| 86 | exe = os.path.join(os.getcwd(), "a.out") |
| 87 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 88 | |
| 89 | self.runCmd("run", RUN_SUCCEEDED) |
| 90 | stop_reason = self.check_stop_reason() |
| 91 | |
| 92 | # And it should report a backtrace that includes the assert site. |
| 93 | self.expect("thread backtrace all", |
| 94 | substrs = [stop_reason, 'main', 'argc', 'argv']) |
| 95 | |
| 96 | # And it should report the correct line number. |
| 97 | self.expect("thread backtrace all", |
| 98 | substrs = [stop_reason, |
| 99 | 'main.c:%d' % self.line]) |
| 100 | |
| 101 | def inferior_asserting_python(self): |
| 102 | """Inferior asserts upon launching; lldb should catch the event and stop.""" |
| 103 | exe = os.path.join(os.getcwd(), "a.out") |
| 104 | |
| 105 | target = self.dbg.CreateTarget(exe) |
| 106 | self.assertTrue(target, VALID_TARGET) |
| 107 | |
| 108 | # Now launch the process, and do not stop at entry point. |
| 109 | # Both argv and envp are null. |
| 110 | process = target.LaunchSimple(None, None, os.getcwd()) |
| 111 | |
| 112 | if process.GetState() != lldb.eStateStopped: |
| 113 | self.fail("Process should be in the 'stopped' state, " |
| 114 | "instead the actual state is: '%s'" % |
| 115 | lldbutil.state_type_to_str(process.GetState())) |
| 116 | |
| 117 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal) |
| 118 | if not thread: |
| 119 | self.fail("Fail to stop the thread upon assert") |
| 120 | |
| 121 | if self.TraceOn(): |
| 122 | lldbutil.print_stacktrace(thread) |
| 123 | |
| 124 | def inferior_asserting_registers(self): |
| 125 | """Test that lldb can read registers after asserting.""" |
| 126 | exe = os.path.join(os.getcwd(), "a.out") |
| 127 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 128 | |
| 129 | self.runCmd("run", RUN_SUCCEEDED) |
| 130 | self.check_stop_reason() |
| 131 | |
| 132 | # lldb should be able to read from registers from the inferior after asserting. |
| 133 | self.expect("register read eax", |
| 134 | substrs = ['eax = 0x']) |
| 135 | |
| 136 | def check_expr_in_main(self, thread): |
| 137 | depth = thread.GetNumFrames() |
| 138 | for i in range(depth): |
| 139 | frame = thread.GetFrameAtIndex(i) |
| 140 | self.assertTrue(frame.IsValid(), "current frame is valid") |
| 141 | if self.TraceOn(): |
| 142 | print "Checking if function %s is main" % frame.GetFunctionName() |
| 143 | |
| 144 | if 'main' == frame.GetFunctionName(): |
| 145 | frame_id = frame.GetFrameID() |
| 146 | self.runCmd("frame select " + str(frame_id), RUN_SUCCEEDED) |
| 147 | self.expect("p argc", substrs = ['(int)', ' = 1']) |
| 148 | self.expect("p hello_world", substrs = ['Hello']) |
| 149 | self.expect("p argv[0]", substrs = ['a.out']) |
| 150 | self.expect("p null_ptr", substrs = ['= 0x0']) |
| 151 | return True |
| 152 | return False |
| 153 | |
| 154 | def inferior_asserting_expr(self): |
| 155 | """Test that the lldb expression interpreter can read symbols after asserting.""" |
| 156 | exe = os.path.join(os.getcwd(), "a.out") |
| 157 | |
| 158 | # Create a target by the debugger. |
| 159 | target = self.dbg.CreateTarget(exe) |
| 160 | self.assertTrue(target, VALID_TARGET) |
| 161 | |
| 162 | # Launch the process, and do not stop at the entry point. |
| 163 | target.LaunchSimple(None, None, os.getcwd()) |
| 164 | self.check_stop_reason() |
| 165 | |
| 166 | process = target.GetProcess() |
| 167 | self.assertTrue(process.IsValid(), "current process is valid") |
| 168 | |
| 169 | thread = process.GetThreadAtIndex(0) |
| 170 | self.assertTrue(thread.IsValid(), "current thread is valid") |
| 171 | |
| 172 | # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert(). |
| 173 | self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace") |
| 174 | |
| 175 | def inferior_asserting_step(self): |
| 176 | """Test that lldb functions correctly after stepping through a call to assert().""" |
| 177 | exe = os.path.join(os.getcwd(), "a.out") |
| 178 | |
| 179 | # Create a target by the debugger. |
| 180 | target = self.dbg.CreateTarget(exe) |
| 181 | self.assertTrue(target, VALID_TARGET) |
| 182 | |
| 183 | # Launch the process, and do not stop at the entry point. |
| 184 | self.set_breakpoint(self.line) |
| 185 | target.LaunchSimple(None, None, os.getcwd()) |
| 186 | |
| 187 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
| 188 | substrs = ['main.c:%d' % self.line, |
| 189 | 'stop reason = breakpoint']) |
| 190 | |
| 191 | self.runCmd("next") |
| 192 | stop_reason = self.check_stop_reason() |
| 193 | |
| 194 | # lldb should be able to read from registers from the inferior after asserting. |
| 195 | if "x86_64" in self.getArchitecture(): |
| 196 | self.expect("register read rbp", substrs = ['rbp = 0x']) |
| 197 | if "i386" in self.getArchitecture(): |
| 198 | self.expect("register read ebp", substrs = ['ebp = 0x']) |
| 199 | |
| 200 | process = target.GetProcess() |
| 201 | self.assertTrue(process.IsValid(), "current process is valid") |
| 202 | |
| 203 | thread = process.GetThreadAtIndex(0) |
| 204 | self.assertTrue(thread.IsValid(), "current thread is valid") |
| 205 | |
| 206 | # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert(). |
| 207 | self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace") |
| 208 | |
| 209 | # And it should report the correct line number. |
| 210 | self.expect("thread backtrace all", |
| 211 | substrs = [stop_reason, |
| 212 | 'main.c:%d' % self.line]) |
| 213 | |
| 214 | if __name__ == '__main__': |
| 215 | import atexit |
| 216 | lldb.SBDebugger.Initialize() |
| 217 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 218 | unittest2.main() |