blob: 451c3444c84f552de658530e6f428690adb6fd31 [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
Johnny Chen48daa1f2010-10-12 21:52:29 +000029 def setUp(self):
Johnny Chenaadcef52010-10-14 17:31:24 +000030 # Call super's setUp().
31 TestBase.setUp(self)
Johnny Chen48daa1f2010-10-12 21:52:29 +000032 # Find the line number to break inside main().
33 self.line = line_number('main.c', '// Set break point at this line.')
34
Johnny Chenea88e942010-09-21 21:08:53 +000035 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 Chen48daa1f2010-10-12 21:52:29 +000041 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 Chenea88e942010-09-21 21:08:53 +000049
50 # Now add callbacks for the breakpoints just created.
Johnny Chen456c9c32010-10-13 19:22:50 +000051 self.runCmd("breakpoint command add -c -o 'frame variable -t -s' 1")
Johnny Chenea88e942010-09-21 21:08:53 +000052 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 Chen48daa1f2010-10-12 21:52:29 +000058 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 Chenea88e942010-09-21 21:08:53 +000062
63 self.expect("breakpoint command list 1", "Breakpoint 1 command ok",
64 substrs = ["Breakpoint commands:",
Johnny Chen456c9c32010-10-13 19:22:50 +000065 "frame variable -t -s"])
Johnny Chenea88e942010-09-21 21:08:53 +000066 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 Chen277c8f02010-10-08 22:10:42 +000078 with open('output.txt', 'r') as f:
79 output = f.read()
Johnny Chenea88e942010-09-21 21:08:53 +000080
Johnny Chen277c8f02010-10-08 22:10:42 +000081 self.expect(output, "File 'output.txt' and the content matches", exe=False,
82 startstr = "lldb")
Johnny Chenea88e942010-09-21 21:08:53 +000083
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 Chen48daa1f2010-10-12 21:52:29 +0000100 substrs = ["1: file ='main.c', line = %d, locations = 1, resolved = 1" %
101 self.line,
Johnny Chenea88e942010-09-21 21:08:53 +0000102 "hit count = 1"])
103
104 # Not breakpoint 2.
105 self.expect("breakpoint list", "No more breakpoint 2", matching=False,
Johnny Chen48daa1f2010-10-12 21:52:29 +0000106 substrs = ["2: file ='main.c', line = %d, locations = 1, resolved = 1" %
107 self.line])
Johnny Chenea88e942010-09-21 21:08:53 +0000108
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 Chencf11e6062010-09-30 17:06:27 +0000120 self.expect("breakpoint list", BREAKPOINT_HIT_TWICE,
Johnny Chenea88e942010-09-21 21:08:53 +0000121 substrs = ['resolved, hit count = 2'])
122
123
124if __name__ == '__main__':
125 import atexit
126 lldb.SBDebugger.Initialize()
127 atexit.register(lambda: lldb.SBDebugger.Terminate())
128 unittest2.main()