1"""Test that lldb steps correctly after the inferior has crashed.""" 2 3 4import lldb 5from lldbsuite.test import lldbutil 6from lldbsuite.test import lldbplatformutil 7from lldbsuite.test.decorators import * 8from lldbsuite.test.lldbtest import * 9 10 11class CrashingInferiorStepTestCase(TestBase): 12 13 @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778") 14 @expectedFailureNetBSD 15 def test_inferior_crashing(self): 16 """Test that lldb reliably catches the inferior crashing (command).""" 17 self.build() 18 self.inferior_crashing() 19 20 def test_inferior_crashing_register(self): 21 """Test that lldb reliably reads registers from the inferior after crashing (command).""" 22 self.build() 23 self.inferior_crashing_registers() 24 25 @add_test_categories(['pyapi']) 26 def test_inferior_crashing_python(self): 27 """Test that lldb reliably catches the inferior crashing (Python API).""" 28 self.build() 29 self.inferior_crashing_python() 30 31 def test_inferior_crashing_expr(self): 32 """Test that the lldb expression interpreter can read from the inferior after crashing (command).""" 33 self.build() 34 self.inferior_crashing_expr() 35 36 def test_inferior_crashing_step(self): 37 """Test that stepping after a crash behaves correctly.""" 38 self.build() 39 self.inferior_crashing_step() 40 41 @skipIfTargetAndroid() # debuggerd interferes with this test on Android 42 @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778") 43 def test_inferior_crashing_step_after_break(self): 44 """Test that lldb functions correctly after stepping through a crash.""" 45 self.build() 46 self.inferior_crashing_step_after_break() 47 48 # Inferior exits after stepping after a segfault. This is working as 49 # intended IMHO. 50 @skipIf(oslist=["freebsd", "linux", "netbsd"]) 51 def test_inferior_crashing_expr_step_and_expr(self): 52 """Test that lldb expressions work before and after stepping after a crash.""" 53 self.build() 54 self.inferior_crashing_expr_step_expr() 55 56 def set_breakpoint(self, line): 57 lldbutil.run_break_set_by_file_and_line( 58 self, "main.c", line, num_expected_locations=1, loc_exact=True) 59 60 def check_stop_reason(self): 61 # We should have one crashing thread 62 self.assertEqual( 63 len( 64 lldbutil.get_crashed_threads( 65 self, 66 self.dbg.GetSelectedTarget().GetProcess())), 1, 67 STOPPED_DUE_TO_EXC_BAD_ACCESS) 68 69 def get_api_stop_reason(self): 70 return lldb.eStopReasonException 71 72 def setUp(self): 73 # Call super's setUp(). 74 TestBase.setUp(self) 75 # Find the line number of the crash. 76 self.line = line_number('main.c', '// Crash here.') 77 78 def inferior_crashing(self): 79 """Inferior crashes upon launching; lldb should catch the event and stop.""" 80 exe = self.getBuildArtifact("a.out") 81 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 82 83 self.runCmd("run", RUN_SUCCEEDED) 84 # The exact stop reason depends on the platform 85 if self.platformIsDarwin(): 86 stop_reason = 'stop reason = EXC_BAD_ACCESS' 87 elif self.getPlatform() == "linux" or self.getPlatform() == "freebsd": 88 stop_reason = 'stop reason = signal SIGSEGV' 89 else: 90 stop_reason = 'stop reason = invalid address' 91 self.expect( 92 "thread list", 93 STOPPED_DUE_TO_EXC_BAD_ACCESS, 94 substrs=['stopped', stop_reason]) 95 96 # And it should report the correct line number. 97 self.expect( 98 "thread backtrace all", 99 substrs=[stop_reason, 'main.c:%d' % self.line]) 100 101 def inferior_crashing_python(self): 102 """Inferior crashes upon launching; lldb should catch the event and stop.""" 103 exe = self.getBuildArtifact("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, 111 self.get_process_working_directory()) 112 113 if process.GetState() != lldb.eStateStopped: 114 self.fail("Process should be in the 'stopped' state, " 115 "instead the actual state is: '%s'" % 116 lldbutil.state_type_to_str(process.GetState())) 117 118 threads = lldbutil.get_crashed_threads(self, process) 119 self.assertEqual( 120 len(threads), 1, 121 "Failed to stop the thread upon bad access exception") 122 123 if self.TraceOn(): 124 lldbutil.print_stacktrace(threads[0]) 125 126 def inferior_crashing_registers(self): 127 """Test that lldb can read registers after crashing.""" 128 exe = self.getBuildArtifact("a.out") 129 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 130 131 self.runCmd("run", RUN_SUCCEEDED) 132 self.check_stop_reason() 133 134 # lldb should be able to read from registers from the inferior after 135 # crashing. 136 lldbplatformutil.check_first_register_readable(self) 137 138 def inferior_crashing_expr(self): 139 """Test that the lldb expression interpreter can read symbols after crashing.""" 140 exe = self.getBuildArtifact("a.out") 141 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 142 143 self.runCmd("run", RUN_SUCCEEDED) 144 self.check_stop_reason() 145 146 # The lldb expression interpreter should be able to read from addresses 147 # of the inferior after a crash. 148 self.expect("p argc", startstr='(int) $0 = 1') 149 150 self.expect("p hello_world", substrs=['Hello']) 151 152 def inferior_crashing_step(self): 153 """Test that lldb functions correctly after stepping through a crash.""" 154 exe = self.getBuildArtifact("a.out") 155 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 156 157 self.set_breakpoint(self.line) 158 self.runCmd("run", RUN_SUCCEEDED) 159 160 self.expect( 161 "thread list", 162 STOPPED_DUE_TO_BREAKPOINT, 163 substrs=['main.c:%d' % self.line, 'stop reason = breakpoint']) 164 165 self.runCmd("next") 166 self.check_stop_reason() 167 168 # The lldb expression interpreter should be able to read from addresses 169 # of the inferior after a crash. 170 self.expect("p argv[0]", substrs=['a.out']) 171 self.expect("p null_ptr", substrs=['= 0x0']) 172 173 # lldb should be able to read from registers from the inferior after 174 # crashing. 175 lldbplatformutil.check_first_register_readable(self) 176 177 # And it should report the correct line number. 178 self.expect("thread backtrace all", substrs=['main.c:%d' % self.line]) 179 180 @expectedFailureNetBSD 181 def inferior_crashing_step_after_break(self): 182 """Test that lldb behaves correctly when stepping after a crash.""" 183 exe = self.getBuildArtifact("a.out") 184 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 185 186 self.runCmd("run", RUN_SUCCEEDED) 187 self.check_stop_reason() 188 189 expected_state = 'exited' # Provide the exit code. 190 if self.platformIsDarwin(): 191 # TODO: Determine why 'next' and 'continue' have no effect after a 192 # crash. 193 expected_state = 'stopped' 194 195 self.expect("next", substrs=['Process', expected_state]) 196 197 if expected_state == 'exited': 198 self.expect( 199 "thread list", 200 error=True, 201 substrs=['Process must be launched']) 202 else: 203 self.check_stop_reason() 204 205 def inferior_crashing_expr_step_expr(self): 206 """Test that lldb expressions work before and after stepping after a crash.""" 207 exe = self.getBuildArtifact("a.out") 208 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) 209 210 self.runCmd("run", RUN_SUCCEEDED) 211 self.check_stop_reason() 212 213 # The lldb expression interpreter should be able to read from addresses 214 # of the inferior after a crash. 215 self.expect("p argv[0]", substrs=['a.out']) 216 217 self.runCmd("next") 218 self.check_stop_reason() 219 220 # The lldb expression interpreter should be able to read from addresses 221 # of the inferior after a crash. 222 self.expect("p argv[0]", substrs=['a.out']) 223