blob: dc2eed3b7e2acaaaf3bc9384b5576980dce78380 [file] [log] [blame]
Johnny Chen73041472011-03-12 01:18:19 +00001"""
Johnny Chenec80b9d2011-05-03 18:40:19 +00002Test lldb target stop-hook mechanism to see whether it fires off correctly .
Johnny Chen73041472011-03-12 01:18:19 +00003"""
4
5import os
6import unittest2
7import lldb
8import pexpect
9from lldbtest import *
10
Johnny Chenec80b9d2011-05-03 18:40:19 +000011class StopHookMechanismTestCase(TestBase):
Johnny Chen73041472011-03-12 01:18:19 +000012
Johnny Chene2ac6de2011-06-27 22:10:42 +000013 mydir = os.path.join("functionalities", "stop-hook")
Johnny Chen73041472011-03-12 01:18:19 +000014
15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chena3ed7d82012-04-06 00:56:05 +000016 @dsym_test
Johnny Chen73041472011-03-12 01:18:19 +000017 def test_with_dsym(self):
Johnny Chenec80b9d2011-05-03 18:40:19 +000018 """Test the stop-hook mechanism."""
Johnny Chen73041472011-03-12 01:18:19 +000019 self.buildDsym()
Johnny Chenec80b9d2011-05-03 18:40:19 +000020 self.stop_hook_firing()
Johnny Chen73041472011-03-12 01:18:19 +000021
Daniel Malea6bc4dcd2013-05-15 18:48:32 +000022 @skipIfLinux # llvm.org/pr15037: stop-hooks sometimes fail to fire on Linux (disabled to avoid needless noise)
Johnny Chena3ed7d82012-04-06 00:56:05 +000023 @dwarf_test
Johnny Chen73041472011-03-12 01:18:19 +000024 def test_with_dwarf(self):
Johnny Chenec80b9d2011-05-03 18:40:19 +000025 """Test the stop-hook mechanism."""
Johnny Chen73041472011-03-12 01:18:19 +000026 self.buildDwarf()
Johnny Chenec80b9d2011-05-03 18:40:19 +000027 self.stop_hook_firing()
Johnny Chen73041472011-03-12 01:18:19 +000028
29 def setUp(self):
30 # Call super's setUp().
31 TestBase.setUp(self)
32 # Find the line numbers inside main.cpp.
33 self.begl = line_number('main.cpp', '// Set breakpoint here to test target stop-hook.')
34 self.endl = line_number('main.cpp', '// End of the line range for which stop-hook is to be run.')
Jim Inghame031d3e2011-09-23 21:24:57 +000035 self.correct_step_line = line_number ('main.cpp', '// We should stop here after stepping.')
Johnny Chen73041472011-03-12 01:18:19 +000036 self.line = line_number('main.cpp', '// Another breakpoint which is outside of the stop-hook range.')
37
Johnny Chenec80b9d2011-05-03 18:40:19 +000038 def stop_hook_firing(self):
39 """Test the stop-hook mechanism."""
Johnny Chen73041472011-03-12 01:18:19 +000040 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chen22ae9672011-05-03 17:38:06 +000041 prompt = "(lldb) "
42 add_prompt = "Enter your stop hook command(s). Type 'DONE' to end.\r\n> "
Johnny Chen73041472011-03-12 01:18:19 +000043 add_prompt1 = "\r\n> "
44
Johnny Chen22ae9672011-05-03 17:38:06 +000045 # So that the child gets torn down after the test.
Johnny Chen7d7f4472011-10-07 19:21:09 +000046 self.child = pexpect.spawn('%s %s %s' % (self.lldbHere, self.lldbOption, exe))
Johnny Chen22ae9672011-05-03 17:38:06 +000047 child = self.child
Johnny Chen73041472011-03-12 01:18:19 +000048 # Turn on logging for what the child sends back.
Johnny Chenda414c32011-04-20 22:01:48 +000049 if self.TraceOn():
Johnny Chen73041472011-03-12 01:18:19 +000050 child.logfile_read = sys.stdout
51
52 # Set the breakpoint, followed by the target stop-hook commands.
Johnny Chen22ae9672011-05-03 17:38:06 +000053 child.expect_exact(prompt)
Johnny Chen73041472011-03-12 01:18:19 +000054 child.sendline('breakpoint set -f main.cpp -l %d' % self.begl)
Johnny Chen22ae9672011-05-03 17:38:06 +000055 child.expect_exact(prompt)
Johnny Chen73041472011-03-12 01:18:19 +000056 child.sendline('breakpoint set -f main.cpp -l %d' % self.line)
Johnny Chen22ae9672011-05-03 17:38:06 +000057 child.expect_exact(prompt)
Johnny Chen73041472011-03-12 01:18:19 +000058 child.sendline('target stop-hook add -f main.cpp -l %d -e %d' % (self.begl, self.endl))
Johnny Chen22ae9672011-05-03 17:38:06 +000059 child.expect_exact(add_prompt)
Johnny Chen73041472011-03-12 01:18:19 +000060 child.sendline('expr ptr')
Johnny Chen22ae9672011-05-03 17:38:06 +000061 child.expect_exact(add_prompt1)
Johnny Chen73041472011-03-12 01:18:19 +000062 child.sendline('DONE')
Johnny Chen22ae9672011-05-03 17:38:06 +000063 child.expect_exact(prompt)
Johnny Chen73041472011-03-12 01:18:19 +000064 child.sendline('target stop-hook list')
65
66 # Now run the program, expect to stop at the the first breakpoint which is within the stop-hook range.
Johnny Chen22ae9672011-05-03 17:38:06 +000067 child.expect_exact(prompt)
Johnny Chen73041472011-03-12 01:18:19 +000068 child.sendline('run')
Johnny Chen22ae9672011-05-03 17:38:06 +000069 child.expect_exact(prompt)
Johnny Chen73041472011-03-12 01:18:19 +000070 child.sendline('thread step-over')
Johnny Chen0f223042011-09-23 18:25:02 +000071 # Expecting to find the output emitted by the firing of our stop hook.
72 child.expect_exact('(void *) $')
Jim Inghame031d3e2011-09-23 21:24:57 +000073 # This is orthogonal to the main stop hook test, but this example shows a bug in
74 # CLANG where the line table entry for the "return -1" actually includes some code
75 # from the other branch of the if/else, so we incorrectly stop at the "return -1" line.
76 # I fixed that in lldb and I'm sticking in a test here because I don't want to have to
77 # make up a whole nother test case for it.
78 child.sendline('frame info')
79 child.expect_exact('at main.cpp:%d'%self.correct_step_line)
Johnny Chen73041472011-03-12 01:18:19 +000080
81 # Now continue the inferior, we'll stop at another breakpoint which is outside the stop-hook range.
82 child.sendline('process continue')
Johnny Chencf607682011-05-04 00:44:20 +000083 child.expect_exact('// Another breakpoint which is outside of the stop-hook range.')
Johnny Chen22ae9672011-05-03 17:38:06 +000084 #self.DebugPExpect(child)
Johnny Chen73041472011-03-12 01:18:19 +000085 child.sendline('thread step-over')
Johnny Chencf607682011-05-04 00:44:20 +000086 child.expect_exact('// Another breakpoint which is outside of the stop-hook range.')
Johnny Chen22ae9672011-05-03 17:38:06 +000087 #self.DebugPExpect(child)
Johnny Chen73041472011-03-12 01:18:19 +000088 # Verify that the 'Stop Hooks' mechanism is NOT BEING fired off.
89 self.expect(child.before, exe=False, matching=False,
Johnny Chenb656f4e2011-05-12 21:58:22 +000090 substrs = ['(void *) $'])
Johnny Chen73041472011-03-12 01:18:19 +000091
92
93if __name__ == '__main__':
94 import atexit
95 lldb.SBDebugger.Initialize()
96 atexit.register(lambda: lldb.SBDebugger.Terminate())
97 unittest2.main()