blob: 600f6097afa464b9f9591d532b68633327180711 [file] [log] [blame]
Johnny Chen2d899752010-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 Chendee30472010-10-08 00:50:36 +000016 system(["/bin/sh", "-c", "rm -f output.txt"])
Johnny Chen2d899752010-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 Chen83626d42010-10-12 21:52:29 +000029 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 Chen2d899752010-09-21 21:08:53 +000034 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 Chen83626d42010-10-12 21:52:29 +000040 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 Chen2d899752010-09-21 21:08:53 +000048
49 # Now add callbacks for the breakpoints just created.
Johnny Chenc401e372010-10-13 19:22:50 +000050 self.runCmd("breakpoint command add -c -o 'frame variable -t -s' 1")
Johnny Chen2d899752010-09-21 21:08:53 +000051 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 Chen83626d42010-10-12 21:52:29 +000057 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 Chen2d899752010-09-21 21:08:53 +000061
62 self.expect("breakpoint command list 1", "Breakpoint 1 command ok",
63 substrs = ["Breakpoint commands:",
Johnny Chenc401e372010-10-13 19:22:50 +000064 "frame variable -t -s"])
Johnny Chen2d899752010-09-21 21:08:53 +000065 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 Chen584f9da2010-10-08 22:10:42 +000077 with open('output.txt', 'r') as f:
78 output = f.read()
Johnny Chen2d899752010-09-21 21:08:53 +000079
Johnny Chen584f9da2010-10-08 22:10:42 +000080 self.expect(output, "File 'output.txt' and the content matches", exe=False,
81 startstr = "lldb")
Johnny Chen2d899752010-09-21 21:08:53 +000082
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 Chen83626d42010-10-12 21:52:29 +000099 substrs = ["1: file ='main.c', line = %d, locations = 1, resolved = 1" %
100 self.line,
Johnny Chen2d899752010-09-21 21:08:53 +0000101 "hit count = 1"])
102
103 # Not breakpoint 2.
104 self.expect("breakpoint list", "No more breakpoint 2", matching=False,
Johnny Chen83626d42010-10-12 21:52:29 +0000105 substrs = ["2: file ='main.c', line = %d, locations = 1, resolved = 1" %
106 self.line])
Johnny Chen2d899752010-09-21 21:08:53 +0000107
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 Chencc03f682010-09-30 17:06:27 +0000119 self.expect("breakpoint list", BREAKPOINT_HIT_TWICE,
Johnny Chen2d899752010-09-21 21:08:53 +0000120 substrs = ['resolved, hit count = 2'])
121
122
123if __name__ == '__main__':
124 import atexit
125 lldb.SBDebugger.Initialize()
126 atexit.register(lambda: lldb.SBDebugger.Terminate())
127 unittest2.main()