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