blob: b0d7580122a535a47c40b1b43a6d94daaec6a103 [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
Johnny Chena3ed7d82012-04-06 00:56:05 +000022 @dwarf_test
Johnny Chen73041472011-03-12 01:18:19 +000023 def test_with_dwarf(self):
Johnny Chenec80b9d2011-05-03 18:40:19 +000024 """Test the stop-hook mechanism."""
Johnny Chen73041472011-03-12 01:18:19 +000025 self.buildDwarf()
Johnny Chenec80b9d2011-05-03 18:40:19 +000026 self.stop_hook_firing()
Johnny Chen73041472011-03-12 01:18:19 +000027
28 def setUp(self):
29 # Call super's setUp().
30 TestBase.setUp(self)
31 # Find the line numbers inside main.cpp.
32 self.begl = line_number('main.cpp', '// Set breakpoint here to test target stop-hook.')
33 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 +000034 self.correct_step_line = line_number ('main.cpp', '// We should stop here after stepping.')
Johnny Chen73041472011-03-12 01:18:19 +000035 self.line = line_number('main.cpp', '// Another breakpoint which is outside of the stop-hook range.')
36
Johnny Chenec80b9d2011-05-03 18:40:19 +000037 def stop_hook_firing(self):
38 """Test the stop-hook mechanism."""
Johnny Chen73041472011-03-12 01:18:19 +000039 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chen22ae9672011-05-03 17:38:06 +000040 prompt = "(lldb) "
41 add_prompt = "Enter your stop hook command(s). Type 'DONE' to end.\r\n> "
Johnny Chen73041472011-03-12 01:18:19 +000042 add_prompt1 = "\r\n> "
43
Johnny Chen22ae9672011-05-03 17:38:06 +000044 # So that the child gets torn down after the test.
Johnny Chen7d7f4472011-10-07 19:21:09 +000045 self.child = pexpect.spawn('%s %s %s' % (self.lldbHere, self.lldbOption, exe))
Johnny Chen22ae9672011-05-03 17:38:06 +000046 child = self.child
Johnny Chen73041472011-03-12 01:18:19 +000047 # Turn on logging for what the child sends back.
Johnny Chenda414c32011-04-20 22:01:48 +000048 if self.TraceOn():
Johnny Chen73041472011-03-12 01:18:19 +000049 child.logfile_read = sys.stdout
50
51 # Set the breakpoint, followed by the target stop-hook commands.
Johnny Chen22ae9672011-05-03 17:38:06 +000052 child.expect_exact(prompt)
Johnny Chen73041472011-03-12 01:18:19 +000053 child.sendline('breakpoint set -f main.cpp -l %d' % self.begl)
Johnny Chen22ae9672011-05-03 17:38:06 +000054 child.expect_exact(prompt)
Johnny Chen73041472011-03-12 01:18:19 +000055 child.sendline('breakpoint set -f main.cpp -l %d' % self.line)
Johnny Chen22ae9672011-05-03 17:38:06 +000056 child.expect_exact(prompt)
Johnny Chen73041472011-03-12 01:18:19 +000057 child.sendline('target stop-hook add -f main.cpp -l %d -e %d' % (self.begl, self.endl))
Johnny Chen22ae9672011-05-03 17:38:06 +000058 child.expect_exact(add_prompt)
Johnny Chen73041472011-03-12 01:18:19 +000059 child.sendline('expr ptr')
Johnny Chen22ae9672011-05-03 17:38:06 +000060 child.expect_exact(add_prompt1)
Johnny Chen73041472011-03-12 01:18:19 +000061 child.sendline('DONE')
Johnny Chen22ae9672011-05-03 17:38:06 +000062 child.expect_exact(prompt)
Johnny Chen73041472011-03-12 01:18:19 +000063 child.sendline('target stop-hook list')
64
65 # 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 +000066 child.expect_exact(prompt)
Johnny Chen73041472011-03-12 01:18:19 +000067 child.sendline('run')
Johnny Chen22ae9672011-05-03 17:38:06 +000068 child.expect_exact(prompt)
Johnny Chen73041472011-03-12 01:18:19 +000069 child.sendline('thread step-over')
Johnny Chen0f223042011-09-23 18:25:02 +000070 # Expecting to find the output emitted by the firing of our stop hook.
71 child.expect_exact('(void *) $')
Jim Inghame031d3e2011-09-23 21:24:57 +000072 # This is orthogonal to the main stop hook test, but this example shows a bug in
73 # CLANG where the line table entry for the "return -1" actually includes some code
74 # from the other branch of the if/else, so we incorrectly stop at the "return -1" line.
75 # I fixed that in lldb and I'm sticking in a test here because I don't want to have to
76 # make up a whole nother test case for it.
77 child.sendline('frame info')
78 child.expect_exact('at main.cpp:%d'%self.correct_step_line)
Johnny Chen73041472011-03-12 01:18:19 +000079
80 # Now continue the inferior, we'll stop at another breakpoint which is outside the stop-hook range.
81 child.sendline('process continue')
Johnny Chencf607682011-05-04 00:44:20 +000082 child.expect_exact('// Another breakpoint which is outside of the stop-hook range.')
Johnny Chen22ae9672011-05-03 17:38:06 +000083 #self.DebugPExpect(child)
Johnny Chen73041472011-03-12 01:18:19 +000084 child.sendline('thread step-over')
Johnny Chencf607682011-05-04 00:44:20 +000085 child.expect_exact('// Another breakpoint which is outside of the stop-hook range.')
Johnny Chen22ae9672011-05-03 17:38:06 +000086 #self.DebugPExpect(child)
Johnny Chen73041472011-03-12 01:18:19 +000087 # Verify that the 'Stop Hooks' mechanism is NOT BEING fired off.
88 self.expect(child.before, exe=False, matching=False,
Johnny Chenb656f4e2011-05-12 21:58:22 +000089 substrs = ['(void *) $'])
Johnny Chen73041472011-03-12 01:18:19 +000090
91
92if __name__ == '__main__':
93 import atexit
94 lldb.SBDebugger.Initialize()
95 atexit.register(lambda: lldb.SBDebugger.Terminate())
96 unittest2.main()