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