PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/test/functionalities/inferior-assert/TestInferiorAssert.py

https://gitlab.com/jorjpimm/lldb
Python | 273 lines | 174 code | 58 blank | 41 comment | 11 complexity | 27a7520f4daa2bbd05319853699f1553 MD5 | raw file
  1. """Test that lldb functions correctly after the inferior has asserted."""
  2. import os, time
  3. import unittest2
  4. import lldb, lldbutil
  5. from lldbtest import *
  6. class AssertingInferiorTestCase(TestBase):
  7. mydir = TestBase.compute_mydir(__file__)
  8. @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
  9. @unittest2.expectedFailure("rdar://15367233")
  10. def test_inferior_asserting_dsym(self):
  11. """Test that lldb reliably catches the inferior asserting (command)."""
  12. self.buildDsym()
  13. self.inferior_asserting()
  14. @expectedFailurei386("llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly")
  15. @expectedFailureDarwin("rdar://15367233")
  16. @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
  17. def test_inferior_asserting_dwarf(self):
  18. """Test that lldb reliably catches the inferior asserting (command)."""
  19. self.buildDwarf()
  20. self.inferior_asserting()
  21. @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
  22. def test_inferior_asserting_registers_dsym(self):
  23. """Test that lldb reliably reads registers from the inferior after asserting (command)."""
  24. self.buildDsym()
  25. self.inferior_asserting_registers()
  26. @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
  27. def test_inferior_asserting_register_dwarf(self):
  28. """Test that lldb reliably reads registers from the inferior after asserting (command)."""
  29. self.buildDwarf()
  30. self.inferior_asserting_registers()
  31. @expectedFailurei386("llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly")
  32. @expectedFailureFreeBSD('llvm.org/pr18533 - PC in __assert frame is outside of function')
  33. @expectedFailureLinux("PC in __GI___assert_fail frame is just after the function (this is a no-return so there is no epilogue afterwards)")
  34. @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
  35. def test_inferior_asserting_disassemble(self):
  36. """Test that lldb reliably disassembles frames after asserting (command)."""
  37. self.buildDefault()
  38. self.inferior_asserting_disassemble()
  39. @python_api_test
  40. @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
  41. def test_inferior_asserting_python(self):
  42. """Test that lldb reliably catches the inferior asserting (Python API)."""
  43. self.buildDefault()
  44. self.inferior_asserting_python()
  45. @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
  46. @unittest2.expectedFailure("rdar://15367233")
  47. def test_inferior_asserting_expr(self):
  48. """Test that the lldb expression interpreter can read from the inferior after asserting (command)."""
  49. self.buildDsym()
  50. self.inferior_asserting_expr()
  51. @expectedFailurei386('llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly')
  52. @unittest2.expectedFailure("rdar://15367233")
  53. @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
  54. def test_inferior_asserting_expr(self):
  55. """Test that the lldb expression interpreter can read from the inferior after asserting (command)."""
  56. self.buildDwarf()
  57. self.inferior_asserting_expr()
  58. @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
  59. @unittest2.expectedFailure("rdar://15367233")
  60. def test_inferior_asserting_step(self):
  61. """Test that lldb functions correctly after stepping through a call to assert()."""
  62. self.buildDsym()
  63. self.inferior_asserting_step()
  64. @expectedFailurei386("llvm.org/pr17384: lldb needs to be aware of linux-vdso.so to unwind stacks properly")
  65. @expectedFailureDarwin("rdar://15367233")
  66. @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
  67. def test_inferior_asserting_step(self):
  68. """Test that lldb functions correctly after stepping through a call to assert()."""
  69. self.buildDwarf()
  70. self.inferior_asserting_step()
  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. def check_stop_reason(self):
  74. stop_reason = 'stop reason = signal SIGABRT'
  75. # The stop reason of the thread should be an abort signal or exception.
  76. self.expect("thread list", STOPPED_DUE_TO_ASSERT,
  77. substrs = ['stopped',
  78. stop_reason])
  79. return stop_reason
  80. def setUp(self):
  81. # Call super's setUp().
  82. TestBase.setUp(self)
  83. # Find the line number of the call to assert.
  84. self.line = line_number('main.c', '// Assert here.')
  85. def inferior_asserting(self):
  86. """Inferior asserts upon launching; lldb should catch the event and stop."""
  87. exe = os.path.join(os.getcwd(), "a.out")
  88. self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
  89. self.runCmd("run", RUN_SUCCEEDED)
  90. stop_reason = self.check_stop_reason()
  91. # And it should report a backtrace that includes the assert site.
  92. self.expect("thread backtrace all",
  93. substrs = [stop_reason, 'main', 'argc', 'argv'])
  94. # And it should report the correct line number.
  95. self.expect("thread backtrace all",
  96. substrs = [stop_reason,
  97. 'main.c:%d' % self.line])
  98. def inferior_asserting_python(self):
  99. """Inferior asserts upon launching; lldb should catch the event and stop."""
  100. exe = os.path.join(os.getcwd(), "a.out")
  101. target = self.dbg.CreateTarget(exe)
  102. self.assertTrue(target, VALID_TARGET)
  103. # Now launch the process, and do not stop at entry point.
  104. # Both argv and envp are null.
  105. process = target.LaunchSimple (None, None, self.get_process_working_directory())
  106. if process.GetState() != lldb.eStateStopped:
  107. self.fail("Process should be in the 'stopped' state, "
  108. "instead the actual state is: '%s'" %
  109. lldbutil.state_type_to_str(process.GetState()))
  110. thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal)
  111. if not thread:
  112. self.fail("Fail to stop the thread upon assert")
  113. if self.TraceOn():
  114. lldbutil.print_stacktrace(thread)
  115. def inferior_asserting_registers(self):
  116. """Test that lldb can read registers after asserting."""
  117. exe = os.path.join(os.getcwd(), "a.out")
  118. self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
  119. self.runCmd("run", RUN_SUCCEEDED)
  120. self.check_stop_reason()
  121. # lldb should be able to read from registers from the inferior after asserting.
  122. self.expect("register read eax",
  123. substrs = ['eax = 0x'])
  124. def inferior_asserting_disassemble(self):
  125. """Test that lldb can disassemble frames after asserting."""
  126. exe = os.path.join(os.getcwd(), "a.out")
  127. # Create a target by the debugger.
  128. target = self.dbg.CreateTarget(exe)
  129. self.assertTrue(target, VALID_TARGET)
  130. # Launch the process, and do not stop at the entry point.
  131. target.LaunchSimple (None, None, self.get_process_working_directory())
  132. self.check_stop_reason()
  133. process = target.GetProcess()
  134. self.assertTrue(process.IsValid(), "current process is valid")
  135. thread = process.GetThreadAtIndex(0)
  136. self.assertTrue(thread.IsValid(), "current thread is valid")
  137. # lldb should be able to disassemble frames from the inferior after asserting.
  138. for frame in thread:
  139. self.assertTrue(frame.IsValid(), "current frame is valid")
  140. self.runCmd("frame select " + str(frame.GetFrameID()), RUN_SUCCEEDED)
  141. # Don't expect the function name to be in the disassembly as the assert
  142. # function might be a no-return function where the PC is past the end
  143. # of the function and in the next function. We also can't back the PC up
  144. # because we don't know how much to back it up by on targets with opcodes
  145. # that have differing sizes
  146. pc_backup_offset = 1
  147. if frame.GetFrameID() == 0:
  148. pc_backup_offset = 0
  149. self.expect("disassemble -a %s" % (frame.GetPC() - pc_backup_offset),
  150. substrs = ['<%s>:' % frame.GetFunctionName()])
  151. def check_expr_in_main(self, thread):
  152. depth = thread.GetNumFrames()
  153. for i in range(depth):
  154. frame = thread.GetFrameAtIndex(i)
  155. self.assertTrue(frame.IsValid(), "current frame is valid")
  156. if self.TraceOn():
  157. print "Checking if function %s is main" % frame.GetFunctionName()
  158. if 'main' == frame.GetFunctionName():
  159. frame_id = frame.GetFrameID()
  160. self.runCmd("frame select " + str(frame_id), RUN_SUCCEEDED)
  161. self.expect("p argc", substrs = ['(int)', ' = 1'])
  162. self.expect("p hello_world", substrs = ['Hello'])
  163. self.expect("p argv[0]", substrs = ['a.out'])
  164. self.expect("p null_ptr", substrs = ['= 0x0'])
  165. return True
  166. return False
  167. def inferior_asserting_expr(self):
  168. """Test that the lldb expression interpreter can read symbols after asserting."""
  169. exe = os.path.join(os.getcwd(), "a.out")
  170. # Create a target by the debugger.
  171. target = self.dbg.CreateTarget(exe)
  172. self.assertTrue(target, VALID_TARGET)
  173. # Launch the process, and do not stop at the entry point.
  174. target.LaunchSimple (None, None, self.get_process_working_directory())
  175. self.check_stop_reason()
  176. process = target.GetProcess()
  177. self.assertTrue(process.IsValid(), "current process is valid")
  178. thread = process.GetThreadAtIndex(0)
  179. self.assertTrue(thread.IsValid(), "current thread is valid")
  180. # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert().
  181. self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace")
  182. def inferior_asserting_step(self):
  183. """Test that lldb functions correctly after stepping through a call to assert()."""
  184. exe = os.path.join(os.getcwd(), "a.out")
  185. # Create a target by the debugger.
  186. target = self.dbg.CreateTarget(exe)
  187. self.assertTrue(target, VALID_TARGET)
  188. # Launch the process, and do not stop at the entry point.
  189. self.set_breakpoint(self.line)
  190. target.LaunchSimple (None, None, self.get_process_working_directory())
  191. self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
  192. substrs = ['main.c:%d' % self.line,
  193. 'stop reason = breakpoint'])
  194. self.runCmd("next")
  195. stop_reason = self.check_stop_reason()
  196. # lldb should be able to read from registers from the inferior after asserting.
  197. if "x86_64" in self.getArchitecture():
  198. self.expect("register read rbp", substrs = ['rbp = 0x'])
  199. if "i386" in self.getArchitecture():
  200. self.expect("register read ebp", substrs = ['ebp = 0x'])
  201. process = target.GetProcess()
  202. self.assertTrue(process.IsValid(), "current process is valid")
  203. thread = process.GetThreadAtIndex(0)
  204. self.assertTrue(thread.IsValid(), "current thread is valid")
  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. # And it should report the correct line number.
  208. self.expect("thread backtrace all",
  209. substrs = [stop_reason,
  210. 'main.c:%d' % self.line])
  211. if __name__ == '__main__':
  212. import atexit
  213. lldb.SBDebugger.Initialize()
  214. atexit.register(lambda: lldb.SBDebugger.Terminate())
  215. unittest2.main()