Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test lldb breakpoint command add/list/remove. |
| 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import unittest2 |
| 7 | import lldb |
| 8 | from lldbtest import * |
| 9 | |
| 10 | class BreakpointCommandTestCase(TestBase): |
| 11 | |
| 12 | mydir = "breakpoint_command" |
| 13 | |
| 14 | @classmethod |
| 15 | def classCleanup(cls): |
Johnny Chen | 06b89fe | 2010-10-08 00:50:36 +0000 | [diff] [blame] | 16 | system(["/bin/sh", "-c", "rm -f output.txt"]) |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 17 | |
| 18 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 19 | def test_with_dsym(self): |
| 20 | """Test a sequence of breakpoint command add, list, and remove.""" |
| 21 | self.buildDsym() |
| 22 | self.breakpoint_command_sequence() |
| 23 | |
| 24 | def test_with_dwarf(self): |
| 25 | """Test a sequence of breakpoint command add, list, and remove.""" |
| 26 | self.buildDwarf() |
| 27 | self.breakpoint_command_sequence() |
| 28 | |
Johnny Chen | 48daa1f | 2010-10-12 21:52:29 +0000 | [diff] [blame] | 29 | def setUp(self): |
Johnny Chen | aadcef5 | 2010-10-14 17:31:24 +0000 | [diff] [blame] | 30 | # Call super's setUp(). |
| 31 | TestBase.setUp(self) |
Johnny Chen | 48daa1f | 2010-10-12 21:52:29 +0000 | [diff] [blame] | 32 | # Find the line number to break inside main(). |
| 33 | self.line = line_number('main.c', '// Set break point at this line.') |
| 34 | |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 35 | def breakpoint_command_sequence(self): |
| 36 | """Test a sequence of breakpoint command add, list, and remove.""" |
| 37 | exe = os.path.join(os.getcwd(), "a.out") |
| 38 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 39 | |
| 40 | # Add two breakpoints on the same line. |
Johnny Chen | 48daa1f | 2010-10-12 21:52:29 +0000 | [diff] [blame] | 41 | self.expect("breakpoint set -f main.c -l %d" % self.line, |
| 42 | BREAKPOINT_CREATED, |
| 43 | startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" % |
| 44 | self.line) |
| 45 | self.expect("breakpoint set -f main.c -l %d" % self.line, |
| 46 | BREAKPOINT_CREATED, |
| 47 | startstr = "Breakpoint created: 2: file ='main.c', line = %d, locations = 1" % |
| 48 | self.line) |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 49 | |
| 50 | # Now add callbacks for the breakpoints just created. |
Johnny Chen | 456c9c3 | 2010-10-13 19:22:50 +0000 | [diff] [blame] | 51 | self.runCmd("breakpoint command add -c -o 'frame variable -t -s' 1") |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 52 | self.runCmd("breakpoint command add -p -o 'here = open(\"output.txt\", \"w\"); print >> here, \"lldb\"; here.close()' 2") |
| 53 | |
| 54 | # Check that the breakpoint commands are correctly set. |
| 55 | |
| 56 | # The breakpoint list now only contains breakpoint 1. |
| 57 | self.expect("breakpoint list", "Breakpoints 1 & 2 created", |
Johnny Chen | 48daa1f | 2010-10-12 21:52:29 +0000 | [diff] [blame] | 58 | substrs = ["1: file ='main.c', line = %d, locations = 1" % self.line, |
| 59 | "2: file ='main.c', line = %d, locations = 1" % self.line], |
| 60 | patterns = ["1.1: .+at main.c:%d, .+unresolved, hit count = 0" % self.line, |
| 61 | "2.1: .+at main.c:%d, .+unresolved, hit count = 0" % self.line]) |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 62 | |
| 63 | self.expect("breakpoint command list 1", "Breakpoint 1 command ok", |
| 64 | substrs = ["Breakpoint commands:", |
Johnny Chen | 456c9c3 | 2010-10-13 19:22:50 +0000 | [diff] [blame] | 65 | "frame variable -t -s"]) |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 66 | self.expect("breakpoint command list 2", "Breakpoint 2 command ok", |
| 67 | substrs = ["Breakpoint commands:", |
| 68 | "here = open", |
| 69 | "print >> here", |
| 70 | "here.close()"]) |
| 71 | |
| 72 | # Run the program. |
| 73 | self.runCmd("run", RUN_SUCCEEDED) |
| 74 | |
| 75 | # Check that the file 'output.txt' exists and contains the string "lldb". |
| 76 | |
| 77 | # Read the output file produced by running the program. |
Johnny Chen | 277c8f0 | 2010-10-08 22:10:42 +0000 | [diff] [blame] | 78 | with open('output.txt', 'r') as f: |
| 79 | output = f.read() |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 80 | |
Johnny Chen | 277c8f0 | 2010-10-08 22:10:42 +0000 | [diff] [blame] | 81 | self.expect(output, "File 'output.txt' and the content matches", exe=False, |
| 82 | startstr = "lldb") |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 83 | |
| 84 | # Finish the program. |
| 85 | self.runCmd("process continue") |
| 86 | |
| 87 | # Remove the breakpoint command associated with breakpoint 1. |
| 88 | self.runCmd("breakpoint command remove 1") |
| 89 | |
| 90 | # Remove breakpoint 2. |
| 91 | self.runCmd("breakpoint delete 2") |
| 92 | |
| 93 | self.expect("breakpoint command list 1", |
| 94 | startstr = "Breakpoint 1 does not have an associated command.") |
| 95 | self.expect("breakpoint command list 2", error=True, |
| 96 | startstr = "error: '2' is not a currently valid breakpoint id.") |
| 97 | |
| 98 | # The breakpoint list now only contains breakpoint 1. |
| 99 | self.expect("breakpoint list", "Breakpoint 1 exists", |
Johnny Chen | 48daa1f | 2010-10-12 21:52:29 +0000 | [diff] [blame] | 100 | substrs = ["1: file ='main.c', line = %d, locations = 1, resolved = 1" % |
| 101 | self.line, |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 102 | "hit count = 1"]) |
| 103 | |
| 104 | # Not breakpoint 2. |
| 105 | self.expect("breakpoint list", "No more breakpoint 2", matching=False, |
Johnny Chen | 48daa1f | 2010-10-12 21:52:29 +0000 | [diff] [blame] | 106 | substrs = ["2: file ='main.c', line = %d, locations = 1, resolved = 1" % |
| 107 | self.line]) |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 108 | |
| 109 | # Run the program again, with breakpoint 1 remaining. |
| 110 | self.runCmd("run", RUN_SUCCEEDED) |
| 111 | |
| 112 | # We should be stopped again due to breakpoint 1. |
| 113 | |
| 114 | # The stop reason of the thread should be breakpoint. |
| 115 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
| 116 | substrs = ['state is Stopped', |
| 117 | 'stop reason = breakpoint']) |
| 118 | |
| 119 | # The breakpoint should have a hit count of 2. |
Johnny Chen | cf11e606 | 2010-09-30 17:06:27 +0000 | [diff] [blame] | 120 | self.expect("breakpoint list", BREAKPOINT_HIT_TWICE, |
Johnny Chen | ea88e94 | 2010-09-21 21:08:53 +0000 | [diff] [blame] | 121 | substrs = ['resolved, hit count = 2']) |
| 122 | |
| 123 | |
| 124 | if __name__ == '__main__': |
| 125 | import atexit |
| 126 | lldb.SBDebugger.Initialize() |
| 127 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 128 | unittest2.main() |