blob: f3a35807ebff40169c302cb437293cb737690dd1 [file] [log] [blame]
Johnny Chenea88e942010-09-21 21:08:53 +00001"""
2Test lldb breakpoint command add/list/remove.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9
10class BreakpointCommandTestCase(TestBase):
11
12 mydir = "breakpoint_command"
13
14 @classmethod
15 def classCleanup(cls):
Johnny Chen06b89fe2010-10-08 00:50:36 +000016 system(["/bin/sh", "-c", "rm -f output.txt"])
Johnny Chenea88e942010-09-21 21:08:53 +000017
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.
Johnny Chen277c8f02010-10-08 22:10:42 +000068 with open('output.txt', 'r') as f:
69 output = f.read()
Johnny Chenea88e942010-09-21 21:08:53 +000070
Johnny Chen277c8f02010-10-08 22:10:42 +000071 self.expect(output, "File 'output.txt' and the content matches", exe=False,
72 startstr = "lldb")
Johnny Chenea88e942010-09-21 21:08:53 +000073
74 # Finish the program.
75 self.runCmd("process continue")
76
77 # Remove the breakpoint command associated with breakpoint 1.
78 self.runCmd("breakpoint command remove 1")
79
80 # Remove breakpoint 2.
81 self.runCmd("breakpoint delete 2")
82
83 self.expect("breakpoint command list 1",
84 startstr = "Breakpoint 1 does not have an associated command.")
85 self.expect("breakpoint command list 2", error=True,
86 startstr = "error: '2' is not a currently valid breakpoint id.")
87
88 # The breakpoint list now only contains breakpoint 1.
89 self.expect("breakpoint list", "Breakpoint 1 exists",
90 substrs = ["1: file ='main.c', line = 12, locations = 1, resolved = 1",
91 "hit count = 1"])
92
93 # Not breakpoint 2.
94 self.expect("breakpoint list", "No more breakpoint 2", matching=False,
95 substrs = ["2: file ='main.c', line = 12, locations = 1, resolved = 1"])
96
97 # Run the program again, with breakpoint 1 remaining.
98 self.runCmd("run", RUN_SUCCEEDED)
99
100 # We should be stopped again due to breakpoint 1.
101
102 # The stop reason of the thread should be breakpoint.
103 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
104 substrs = ['state is Stopped',
105 'stop reason = breakpoint'])
106
107 # The breakpoint should have a hit count of 2.
Johnny Chencf11e6062010-09-30 17:06:27 +0000108 self.expect("breakpoint list", BREAKPOINT_HIT_TWICE,
Johnny Chenea88e942010-09-21 21:08:53 +0000109 substrs = ['resolved, hit count = 2'])
110
111
112if __name__ == '__main__':
113 import atexit
114 lldb.SBDebugger.Initialize()
115 atexit.register(lambda: lldb.SBDebugger.Terminate())
116 unittest2.main()