1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
"""Test that lldb functions correctly after the inferior has asserted."""
import lldb
from lldbsuite.test import lldbutil
from lldbsuite.test import lldbplatformutil
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
class AssertingInferiorTestCase(TestBase):
@expectedFailureAll(
oslist=["windows"],
bugnumber="llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows",
)
@expectedFailureAll(oslist=["linux"], archs=["arm"], bugnumber="llvm.org/pr25338")
@expectedFailureAll(bugnumber="llvm.org/pr26592", triple="^mips")
def test_inferior_asserting(self):
"""Test that lldb reliably catches the inferior asserting (command)."""
self.build()
self.inferior_asserting()
@expectedFailureAll(
oslist=["windows"],
bugnumber="llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows",
)
@expectedFailureAndroid(api_levels=list(range(16 + 1))) # b.android.com/179836
def test_inferior_asserting_register(self):
"""Test that lldb reliably reads registers from the inferior after asserting (command)."""
self.build()
self.inferior_asserting_registers()
@expectedFailureAll(
oslist=["windows"],
bugnumber="llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows",
)
@expectedFailureAll(
oslist=["linux"],
archs=["arm"],
triple=no_match(".*-android"),
bugnumber="llvm.org/pr25338",
)
@expectedFailureAll(bugnumber="llvm.org/pr26592", triple="^mips")
def test_inferior_asserting_disassemble(self):
"""Test that lldb reliably disassembles frames after asserting (command)."""
self.build()
self.inferior_asserting_disassemble()
@add_test_categories(["pyapi"])
@expectedFailureAll(
oslist=["windows"],
bugnumber="llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows",
)
def test_inferior_asserting_python(self):
"""Test that lldb reliably catches the inferior asserting (Python API)."""
self.build()
self.inferior_asserting_python()
@expectedFailureAll(
oslist=["windows"],
bugnumber="llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows",
)
@expectedFailureAll(
oslist=["linux"],
archs=["arm"],
triple=no_match(".*-android"),
bugnumber="llvm.org/pr25338",
)
@expectedFailureAll(bugnumber="llvm.org/pr26592", triple="^mips")
def test_inferior_asserting_expr(self):
"""Test that the lldb expression interpreter can read from the inferior after asserting (command)."""
self.build()
self.inferior_asserting_expr()
@expectedFailureAll(
oslist=["windows"],
bugnumber="llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows",
)
@expectedFailureAll(
oslist=["linux"],
archs=["arm"],
triple=no_match(".*-android"),
bugnumber="llvm.org/pr25338",
)
@expectedFailureAll(bugnumber="llvm.org/pr26592", triple="^mips")
def test_inferior_asserting_step(self):
"""Test that lldb functions correctly after stepping through a call to assert()."""
self.build()
self.inferior_asserting_step()
def set_breakpoint(self, line):
lldbutil.run_break_set_by_file_and_line(
self, "main.c", line, num_expected_locations=1, loc_exact=True
)
def check_stop_reason(self):
matched = lldbplatformutil.match_android_device(
self.getArchitecture(), valid_api_levels=list(range(1, 16 + 1))
)
if matched:
# On android until API-16 the abort() call ended in a sigsegv
# instead of in a sigabrt
stop_reason = "signal SIGSEGV"
else:
stop_reason = "signal SIGABRT"
target = self.dbg.GetTargetAtIndex(0)
process = target.GetProcess()
thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal)
pattern = "stop reason = (" + stop_reason + "|hit program assert)"
# The stop reason of the thread should be an abort signal or exception.
self.expect(
"thread list",
STOPPED_DUE_TO_ASSERT,
patterns=[pattern],
substrs=["stopped"],
)
return pattern
def setUp(self):
# Call super's setUp().
TestBase.setUp(self)
# Find the line number of the call to assert.
self.line = line_number("main.c", "// Assert here.")
def inferior_asserting(self):
"""Inferior asserts upon launching; lldb should catch the event and stop."""
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
self.runCmd("run", RUN_SUCCEEDED)
stop_reason = self.check_stop_reason()
# And it should report a backtrace that includes the assert site.
self.expect(
"thread backtrace all",
patterns=[stop_reason],
substrs=["main", "argc", "argv"],
)
# And it should report the correct line number.
self.expect(
"thread backtrace all",
patterns=[stop_reason],
substrs=["main.c:%d" % self.line],
)
def inferior_asserting_python(self):
"""Inferior asserts upon launching; lldb should catch the event and stop."""
exe = self.getBuildArtifact("a.out")
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)
# Now launch the process, and do not stop at entry point.
# Both argv and envp are null.
process = target.LaunchSimple(None, None, self.get_process_working_directory())
if process.GetState() != lldb.eStateStopped:
self.fail(
"Process should be in the 'stopped' state, "
"instead the actual state is: '%s'"
% lldbutil.state_type_to_str(process.GetState())
)
thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal)
if not thread:
self.fail("Fail to stop the thread upon assert")
if self.TraceOn():
lldbutil.print_stacktrace(thread)
def inferior_asserting_registers(self):
"""Test that lldb can read registers after asserting."""
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
self.runCmd("run", RUN_SUCCEEDED)
self.check_stop_reason()
# change current frame to frame 0, since recognizer might have selected
# different frame.
self.runCmd("frame select 0", RUN_SUCCEEDED)
# lldb should be able to read from registers from the inferior after
# asserting.
lldbplatformutil.check_first_register_readable(self)
def inferior_asserting_disassemble(self):
"""Test that lldb can disassemble frames after asserting."""
exe = self.getBuildArtifact("a.out")
# Create a target by the debugger.
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)
# Launch the process, and do not stop at the entry point.
target.LaunchSimple(None, None, self.get_process_working_directory())
self.check_stop_reason()
process = target.GetProcess()
self.assertTrue(process.IsValid(), "current process is valid")
thread = process.GetThreadAtIndex(0)
self.assertTrue(thread.IsValid(), "current thread is valid")
lastframeID = thread.GetFrameAtIndex(thread.GetNumFrames() - 1).GetFrameID()
isi386Arch = False
if "i386" in self.getArchitecture():
isi386Arch = True
# lldb should be able to disassemble frames from the inferior after
# asserting.
for frame in thread:
self.assertTrue(frame.IsValid(), "current frame is valid")
self.runCmd("frame select " + str(frame.GetFrameID()), RUN_SUCCEEDED)
# Don't expect the function name to be in the disassembly as the assert
# function might be a no-return function where the PC is past the end
# of the function and in the next function. We also can't back the PC up
# because we don't know how much to back it up by on targets with opcodes
# that have differing sizes
pc_backup_offset = 1
if frame.GetFrameID() == 0:
pc_backup_offset = 0
if isi386Arch:
if lastframeID == frame.GetFrameID():
pc_backup_offset = 0
self.expect(
"disassemble -a %s" % (frame.GetPC() - pc_backup_offset),
substrs=["<+0>: "],
)
def check_expr_in_main(self, thread):
depth = thread.GetNumFrames()
for i in range(depth):
frame = thread.GetFrameAtIndex(i)
self.assertTrue(frame.IsValid(), "current frame is valid")
if self.TraceOn():
print("Checking if function %s is main" % frame.GetFunctionName())
if "main" == frame.GetFunctionName():
frame_id = frame.GetFrameID()
self.runCmd("frame select " + str(frame_id), RUN_SUCCEEDED)
self.expect("expression argc", substrs=["(int)", " = 1"])
self.expect("expression hello_world", substrs=["Hello"])
self.expect("expression argv[0]", substrs=["a.out"])
self.expect("expression null_ptr", substrs=["= 0x0"])
return True
return False
def inferior_asserting_expr(self):
"""Test that the lldb expression interpreter can read symbols after asserting."""
exe = self.getBuildArtifact("a.out")
# Create a target by the debugger.
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)
# Launch the process, and do not stop at the entry point.
target.LaunchSimple(None, None, self.get_process_working_directory())
self.check_stop_reason()
process = target.GetProcess()
self.assertTrue(process.IsValid(), "current process is valid")
thread = process.GetThreadAtIndex(0)
self.assertTrue(thread.IsValid(), "current thread is valid")
# The lldb expression interpreter should be able to read from addresses
# of the inferior after a call to assert().
self.assertTrue(
self.check_expr_in_main(thread), "cannot find 'main' in the backtrace"
)
def inferior_asserting_step(self):
"""Test that lldb functions correctly after stepping through a call to assert()."""
exe = self.getBuildArtifact("a.out")
# Create a target by the debugger.
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)
# Launch the process, and do not stop at the entry point.
self.set_breakpoint(self.line)
target.LaunchSimple(None, None, self.get_process_working_directory())
self.expect(
"thread list",
STOPPED_DUE_TO_BREAKPOINT,
substrs=["main.c:%d" % self.line, "stop reason = breakpoint"],
)
self.runCmd("next")
stop_reason = self.check_stop_reason()
# lldb should be able to read from registers from the inferior after
# asserting.
if "x86_64" in self.getArchitecture():
self.expect("register read rbp", substrs=["rbp = 0x"])
if "i386" in self.getArchitecture():
self.expect("register read ebp", substrs=["ebp = 0x"])
process = target.GetProcess()
self.assertTrue(process.IsValid(), "current process is valid")
thread = process.GetThreadAtIndex(0)
self.assertTrue(thread.IsValid(), "current thread is valid")
# The lldb expression interpreter should be able to read from addresses
# of the inferior after a call to assert().
self.assertTrue(
self.check_expr_in_main(thread), "cannot find 'main' in the backtrace"
)
# And it should report the correct line number.
self.expect(
"thread backtrace all",
patterns=[stop_reason],
substrs=["main.c:%d" % self.line],
)
|