1"""Test that lldb functions correctly after the inferior has crashed.""" 2 3import os, time 4import unittest2 5import lldb, lldbutil 6from lldbtest import * 7 8class CrashingInferiorTestCase(TestBase): 9 10 mydir = os.path.join("functionalities", "inferior-crashing") 11 12 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 13 def test_inferior_crashing_dsym(self): 14 """Test that lldb reliably catches the inferior crashing (command).""" 15 self.buildDsym() 16 self.inferior_crashing() 17 18 def test_inferior_crashing_dwarf(self): 19 """Test that lldb reliably catches the inferior crashing (command).""" 20 self.buildDwarf() 21 self.inferior_crashing() 22 23 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 24 def test_inferior_crashing_registers_dsym(self): 25 """Test that lldb reliably reads registers from the inferior after crashing (command).""" 26 self.buildDsym() 27 self.inferior_crashing_registers() 28 29 def test_inferior_crashing_register_dwarf(self): 30 """Test that lldb reliably reads registers from the inferior after crashing (command).""" 31 self.buildDwarf() 32 self.inferior_crashing_registers() 33 34 @python_api_test 35 def test_inferior_crashing_python(self): 36 """Test that lldb reliably catches the inferior crashing (Python API).""" 37 self.buildDefault() 38 self.inferior_crashing_python() 39 40 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 41 def test_inferior_crashing_expr_dsym(self): 42 """Test that the lldb expression interpreter can read from the inferior after crashing (command).""" 43 self.buildDsym() 44 self.inferior_crashing_expr() 45 46 def test_inferior_crashing_expr_dwarf(self): 47 """Test that the lldb expression interpreter can read from the inferior after crashing (command).""" 48 self.buildDwarf() 49 self.inferior_crashing_expr() 50 51 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 52 def test_inferior_crashing_step_dsym(self): 53 """Test that lldb functions correctly after stepping through a crash.""" 54 self.buildDsym() 55 self.inferior_crashing_step() 56 57 def test_inferior_crashing_step_dwarf(self): 58 """Test that stepping after a crash behaves correctly.""" 59 self.buildDwarf() 60 self.inferior_crashing_step() 61 62 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 63 def test_inferior_crashing_step_after_break_dsym(self): 64 """Test that stepping after a crash behaves correctly.""" 65 self.buildDsym() 66 self.inferior_crashing_step_after_break() 67 68 @expectedFailureLinux # due to llvm.org/pr15988 -- step over misbehaves after crash 69 def test_inferior_crashing_step_after_break_dwarf(self): 70 """Test that lldb functions correctly after stepping through a crash.""" 71 self.buildDwarf() 72 self.inferior_crashing_step_after_break() 73 74 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 75 def test_inferior_crashing_expr_step_and_expr_dsym(self): 76 """Test that lldb expressions work before and after stepping after a crash.""" 77 self.buildDsym() 78 self.inferior_crashing_expr_step_expr() 79 80 @expectedFailureLinux # due to llvm.org/pr15989 -- expression fails after crash and step 81 def test_inferior_crashing_expr_step_and_expr_dwarf(self): 82 """Test that lldb expressions work before and after stepping after a crash.""" 83 self.buildDwarf() 84 self.inferior_crashing_expr_step_expr() 85 86 def set_breakpoint(self, line): 87 lldbutil.run_break_set_by_file_and_line (self, "main.c", line, num_expected_locations=1, loc_exact=True) 88 89 def check_stop_reason(self): 90 if sys.platform.startswith("darwin"): 91 stop_reason = 'stop reason = EXC_BAD_ACCESS' 92 else: 93 stop_reason = 'stop reason = invalid address' 94 95 # The stop reason of the thread should be a bad access exception. 96 self.expect("thread list", STOPPED_DUE_TO_EXC_BAD_ACCESS, 97 substrs = ['stopped', 98 stop_reason]) 99 100 return stop_reason 101 102 def setUp(self): 103 # Call super's setUp(). 104 TestBase.setUp(self) 105 # Find the line number of the crash. 106 self.line = line_number('main.c', '// Crash here.') 107 108 def inferior_crashing(self): 109 """Inferior crashes upon launching; lldb should catch the event and stop.""" 110 exe = os.path.join(os.getcwd(), "a.out") 111 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 112 113 self.runCmd("run", RUN_SUCCEEDED) 114 stop_reason = self.check_stop_reason() 115 116 # And it should report the correct line number. 117 self.expect("thread backtrace all", 118 substrs = [stop_reason, 119 'main.c:%d' % self.line]) 120 121 def inferior_crashing_python(self): 122 """Inferior crashes upon launching; lldb should catch the event and stop.""" 123 exe = os.path.join(os.getcwd(), "a.out") 124 125 target = self.dbg.CreateTarget(exe) 126 self.assertTrue(target, VALID_TARGET) 127 128 # Now launch the process, and do not stop at entry point. 129 # Both argv and envp are null. 130 process = target.LaunchSimple(None, None, os.getcwd()) 131 132 if process.GetState() != lldb.eStateStopped: 133 self.fail("Process should be in the 'stopped' state, " 134 "instead the actual state is: '%s'" % 135 lldbutil.state_type_to_str(process.GetState())) 136 137 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonException) 138 if not thread: 139 self.fail("Fail to stop the thread upon bad access exception") 140 141 if self.TraceOn(): 142 lldbutil.print_stacktrace(thread) 143 144 def inferior_crashing_registers(self): 145 """Test that lldb can read registers after crashing.""" 146 exe = os.path.join(os.getcwd(), "a.out") 147 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 148 149 self.runCmd("run", RUN_SUCCEEDED) 150 self.check_stop_reason() 151 152 # lldb should be able to read from registers from the inferior after crashing. 153 self.expect("register read eax", 154 substrs = ['eax = 0x']) 155 156 def inferior_crashing_expr(self): 157 """Test that the lldb expression interpreter can read symbols after crashing.""" 158 exe = os.path.join(os.getcwd(), "a.out") 159 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 160 161 self.runCmd("run", RUN_SUCCEEDED) 162 self.check_stop_reason() 163 164 # The lldb expression interpreter should be able to read from addresses of the inferior after a crash. 165 self.expect("p argc", 166 startstr = '(int) $0 = 1') 167 168 self.expect("p hello_world", 169 substrs = ['Hello']) 170 171 def inferior_crashing_step(self): 172 """Test that lldb functions correctly after stepping through a crash.""" 173 exe = os.path.join(os.getcwd(), "a.out") 174 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 175 176 self.set_breakpoint(self.line) 177 self.runCmd("run", RUN_SUCCEEDED) 178 179 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 180 substrs = ['main.c:%d' % self.line, 181 'stop reason = breakpoint']) 182 183 self.runCmd("next") 184 stop_reason = self.check_stop_reason() 185 186 # The lldb expression interpreter should be able to read from addresses of the inferior after a crash. 187 self.expect("p argv[0]", 188 substrs = ['a.out']) 189 self.expect("p null_ptr", 190 substrs = ['= 0x0']) 191 192 # lldb should be able to read from registers from the inferior after crashing. 193 self.expect("register read eax", 194 substrs = ['eax = 0x']) 195 196 # And it should report the correct line number. 197 self.expect("thread backtrace all", 198 substrs = [stop_reason, 199 'main.c:%d' % self.line]) 200 201 def inferior_crashing_step_after_break(self): 202 """Test that lldb behaves correctly when stepping after a crash.""" 203 exe = os.path.join(os.getcwd(), "a.out") 204 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 205 206 self.runCmd("run", RUN_SUCCEEDED) 207 self.check_stop_reason() 208 209 self.runCmd("next") 210 self.check_stop_reason() 211 212 def inferior_crashing_expr_step_expr(self): 213 """Test that lldb expressions work before and after stepping after a crash.""" 214 exe = os.path.join(os.getcwd(), "a.out") 215 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 216 217 self.runCmd("run", RUN_SUCCEEDED) 218 self.check_stop_reason() 219 220 # The lldb expression interpreter should be able to read from addresses of the inferior after a crash. 221 self.expect("p argv[0]", 222 substrs = ['a.out']) 223 224 self.runCmd("next") 225 self.check_stop_reason() 226 227 # The lldb expression interpreter should be able to read from addresses of the inferior after a crash. 228 self.expect("p argv[0]", 229 substrs = ['a.out']) 230 231 232if __name__ == '__main__': 233 import atexit 234 lldb.SBDebugger.Initialize() 235 atexit.register(lambda: lldb.SBDebugger.Terminate()) 236 unittest2.main() 237