Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 1 | """ |
Johnny Chen | ec80b9d | 2011-05-03 18:40:19 +0000 | [diff] [blame] | 2 | Test lldb target stop-hook mechanism to see whether it fires off correctly . |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 3 | """ |
| 4 | |
| 5 | import os |
| 6 | import unittest2 |
| 7 | import lldb |
| 8 | import pexpect |
| 9 | from lldbtest import * |
| 10 | |
Johnny Chen | ec80b9d | 2011-05-03 18:40:19 +0000 | [diff] [blame] | 11 | class StopHookMechanismTestCase(TestBase): |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 12 | |
Johnny Chen | e2ac6de | 2011-06-27 22:10:42 +0000 | [diff] [blame] | 13 | mydir = os.path.join("functionalities", "stop-hook") |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 14 | |
| 15 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
Johnny Chen | a3ed7d8 | 2012-04-06 00:56:05 +0000 | [diff] [blame] | 16 | @dsym_test |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 17 | def test_with_dsym(self): |
Johnny Chen | ec80b9d | 2011-05-03 18:40:19 +0000 | [diff] [blame] | 18 | """Test the stop-hook mechanism.""" |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 19 | self.buildDsym() |
Johnny Chen | ec80b9d | 2011-05-03 18:40:19 +0000 | [diff] [blame] | 20 | self.stop_hook_firing() |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 21 | |
Daniel Malea | 6bc4dcd | 2013-05-15 18:48:32 +0000 | [diff] [blame] | 22 | @skipIfLinux # llvm.org/pr15037: stop-hooks sometimes fail to fire on Linux (disabled to avoid needless noise) |
Johnny Chen | a3ed7d8 | 2012-04-06 00:56:05 +0000 | [diff] [blame] | 23 | @dwarf_test |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 24 | def test_with_dwarf(self): |
Johnny Chen | ec80b9d | 2011-05-03 18:40:19 +0000 | [diff] [blame] | 25 | """Test the stop-hook mechanism.""" |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 26 | self.buildDwarf() |
Johnny Chen | ec80b9d | 2011-05-03 18:40:19 +0000 | [diff] [blame] | 27 | self.stop_hook_firing() |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 28 | |
| 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 Ingham | e031d3e | 2011-09-23 21:24:57 +0000 | [diff] [blame] | 35 | self.correct_step_line = line_number ('main.cpp', '// We should stop here after stepping.') |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 36 | self.line = line_number('main.cpp', '// Another breakpoint which is outside of the stop-hook range.') |
| 37 | |
Johnny Chen | ec80b9d | 2011-05-03 18:40:19 +0000 | [diff] [blame] | 38 | def stop_hook_firing(self): |
| 39 | """Test the stop-hook mechanism.""" |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 40 | exe = os.path.join(os.getcwd(), "a.out") |
Johnny Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 41 | prompt = "(lldb) " |
| 42 | add_prompt = "Enter your stop hook command(s). Type 'DONE' to end.\r\n> " |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 43 | add_prompt1 = "\r\n> " |
| 44 | |
Johnny Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 45 | # So that the child gets torn down after the test. |
Johnny Chen | 7d7f447 | 2011-10-07 19:21:09 +0000 | [diff] [blame] | 46 | self.child = pexpect.spawn('%s %s %s' % (self.lldbHere, self.lldbOption, exe)) |
Johnny Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 47 | child = self.child |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 48 | # Turn on logging for what the child sends back. |
Johnny Chen | da414c3 | 2011-04-20 22:01:48 +0000 | [diff] [blame] | 49 | if self.TraceOn(): |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 50 | child.logfile_read = sys.stdout |
| 51 | |
| 52 | # Set the breakpoint, followed by the target stop-hook commands. |
Johnny Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 53 | child.expect_exact(prompt) |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 54 | child.sendline('breakpoint set -f main.cpp -l %d' % self.begl) |
Johnny Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 55 | child.expect_exact(prompt) |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 56 | child.sendline('breakpoint set -f main.cpp -l %d' % self.line) |
Johnny Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 57 | child.expect_exact(prompt) |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 58 | child.sendline('target stop-hook add -f main.cpp -l %d -e %d' % (self.begl, self.endl)) |
Johnny Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 59 | child.expect_exact(add_prompt) |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 60 | child.sendline('expr ptr') |
Johnny Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 61 | child.expect_exact(add_prompt1) |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 62 | child.sendline('DONE') |
Johnny Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 63 | child.expect_exact(prompt) |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 64 | 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 Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 67 | child.expect_exact(prompt) |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 68 | child.sendline('run') |
Johnny Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 69 | child.expect_exact(prompt) |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 70 | child.sendline('thread step-over') |
Johnny Chen | 0f22304 | 2011-09-23 18:25:02 +0000 | [diff] [blame] | 71 | # Expecting to find the output emitted by the firing of our stop hook. |
| 72 | child.expect_exact('(void *) $') |
Jim Ingham | e031d3e | 2011-09-23 21:24:57 +0000 | [diff] [blame] | 73 | # 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 Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 80 | |
| 81 | # Now continue the inferior, we'll stop at another breakpoint which is outside the stop-hook range. |
| 82 | child.sendline('process continue') |
Johnny Chen | cf60768 | 2011-05-04 00:44:20 +0000 | [diff] [blame] | 83 | child.expect_exact('// Another breakpoint which is outside of the stop-hook range.') |
Johnny Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 84 | #self.DebugPExpect(child) |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 85 | child.sendline('thread step-over') |
Johnny Chen | cf60768 | 2011-05-04 00:44:20 +0000 | [diff] [blame] | 86 | child.expect_exact('// Another breakpoint which is outside of the stop-hook range.') |
Johnny Chen | 22ae967 | 2011-05-03 17:38:06 +0000 | [diff] [blame] | 87 | #self.DebugPExpect(child) |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 88 | # Verify that the 'Stop Hooks' mechanism is NOT BEING fired off. |
| 89 | self.expect(child.before, exe=False, matching=False, |
Johnny Chen | b656f4e | 2011-05-12 21:58:22 +0000 | [diff] [blame] | 90 | substrs = ['(void *) $']) |
Johnny Chen | 7304147 | 2011-03-12 01:18:19 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | import atexit |
| 95 | lldb.SBDebugger.Initialize() |
| 96 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 97 | unittest2.main() |