blob: fb2730a0957a86b7e4a485aed247fb6833885643 [file] [log] [blame]
Johnny Chenb3307862010-09-17 22:28:51 +00001"""
2Set breakpoints on objective-c class and instance methods in foundation.
3Also lookup objective-c data types and evaluate expressions.
4"""
Johnny Chen6c17c082010-09-16 20:54:06 +00005
6import os, time
7import unittest2
8import lldb
9from lldbtest import *
10
11@unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
12class FoundationTestCase(TestBase):
13
14 mydir = "foundation"
15
Johnny Chenb219d282010-09-17 21:14:02 +000016 def test_break_with_dsym(self):
Johnny Chen2756d602010-09-16 23:57:33 +000017 """Test setting objc breakpoints using 'regexp-break' and 'breakpoint set'."""
Johnny Chen6c17c082010-09-16 20:54:06 +000018 self.buildDsym()
19 self.break_on_objc_methods()
20
Johnny Chenb219d282010-09-17 21:14:02 +000021 def test_break_with_dwarf(self):
Johnny Chen2756d602010-09-16 23:57:33 +000022 """Test setting objc breakpoints using 'regexp-break' and 'breakpoint set'."""
Johnny Chen6c17c082010-09-16 20:54:06 +000023 self.buildDwarf()
24 self.break_on_objc_methods()
25
Johnny Chen2769a422010-10-15 23:35:32 +000026 #@unittest2.expectedFailure
Johnny Chenf8afe892010-10-12 21:20:11 +000027 # rdar://problem/8542091
Johnny Chen8eb38502010-09-29 17:58:12 +000028 # rdar://problem/8492646
Johnny Chenb3307862010-09-17 22:28:51 +000029 def test_data_type_and_expr_with_dsym(self):
30 """Lookup objective-c data types and evaluate expressions."""
31 self.buildDsym()
32 self.data_type_and_expr_objc()
33
Johnny Chen2769a422010-10-15 23:35:32 +000034 #@unittest2.expectedFailure
Johnny Chenf8afe892010-10-12 21:20:11 +000035 # rdar://problem/8542091
Johnny Chen8eb38502010-09-29 17:58:12 +000036 # rdar://problem/8492646
Johnny Chenb3307862010-09-17 22:28:51 +000037 def test_data_type_and_expr_with_dwarf(self):
38 """Lookup objective-c data types and evaluate expressions."""
39 self.buildDwarf()
40 self.data_type_and_expr_objc()
41
Johnny Chen6c17c082010-09-16 20:54:06 +000042 def break_on_objc_methods(self):
Johnny Chen2756d602010-09-16 23:57:33 +000043 """Test setting objc breakpoints using 'regexp-break' and 'breakpoint set'."""
Johnny Chen6c17c082010-09-16 20:54:06 +000044 exe = os.path.join(os.getcwd(), "a.out")
45 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
46
47 # Stop at +[NSString stringWithFormat:].
48 self.expect("regexp-break +[NSString stringWithFormat:]", BREAKPOINT_CREATED,
Johnny Chenf062c2f2010-10-12 19:29:49 +000049 substrs = ["Breakpoint created: 1: name = '+[NSString stringWithFormat:]', locations = 1"])
Johnny Chen6c17c082010-09-16 20:54:06 +000050
Johnny Chen2756d602010-09-16 23:57:33 +000051 # Stop at -[MyString initWithNSString:].
52 self.expect("breakpoint set -n '-[MyString initWithNSString:]'", BREAKPOINT_CREATED,
53 startstr = "Breakpoint created: 2: name = '-[MyString initWithNSString:]', locations = 1")
54
55 # Stop at the "description" selector.
56 self.expect("breakpoint set -S description", BREAKPOINT_CREATED,
57 startstr = "Breakpoint created: 3: name = 'description', locations = 1")
58
Johnny Chen6c17c082010-09-16 20:54:06 +000059 # Stop at -[NSAutoreleasePool release].
60 self.expect("regexp-break -[NSAutoreleasePool release]", BREAKPOINT_CREATED,
Johnny Chenf062c2f2010-10-12 19:29:49 +000061 substrs = ["Breakpoint created: 4: name = '-[NSAutoreleasePool release]', locations = 1"])
Johnny Chen6c17c082010-09-16 20:54:06 +000062
63 self.runCmd("run", RUN_SUCCEEDED)
64
65 # First stop is +[NSString stringWithFormat:].
66 self.expect("thread backtrace", "Stop at +[NSString stringWithFormat:]",
67 substrs = ["Foundation`+[NSString stringWithFormat:]"])
68
69 self.runCmd("process continue")
70
Johnny Chen2756d602010-09-16 23:57:33 +000071 # Followed by a.out`-[MyString initWithNSString:].
72 self.expect("thread backtrace", "Stop at a.out`-[MyString initWithNSString:]",
73 substrs = ["a.out`-[MyString initWithNSString:]"])
74
75 self.runCmd("process continue")
76
77 # Followed by -[MyString description].
78 self.expect("thread backtrace", "Stop at -[MyString description]",
79 substrs = ["a.out`-[MyString description]"])
80
81 self.runCmd("process continue")
82
Johnny Chen6c17c082010-09-16 20:54:06 +000083 # Followed by -[NSAutoreleasePool release].
84 self.expect("thread backtrace", "Stop at -[NSAutoreleasePool release]",
85 substrs = ["Foundation`-[NSAutoreleasePool release]"])
86
Johnny Chenf8afe892010-10-12 21:20:11 +000087 def setUp(self):
Johnny Chenaadcef52010-10-14 17:31:24 +000088 # Call super's setUp().
89 TestBase.setUp(self)
Johnny Chenf8afe892010-10-12 21:20:11 +000090 # Find the line number to break inside main().
91 self.line = line_number('main.m', '// Set break point at this line.')
92
Johnny Chenb3307862010-09-17 22:28:51 +000093 def data_type_and_expr_objc(self):
94 """Lookup objective-c data types and evaluate expressions."""
95 exe = os.path.join(os.getcwd(), "a.out")
96 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
97
98 # Stop at -[MyString description].
99 self.expect("breakpoint set -n '-[MyString description]", BREAKPOINT_CREATED,
100 startstr = "Breakpoint created: 1: name = '-[MyString description]', locations = 1")
101
102 self.runCmd("run", RUN_SUCCEEDED)
103
104 # The backtrace should show we stop at -[MyString description].
105 self.expect("thread backtrace", "Stop at -[MyString description]",
106 substrs = ["a.out`-[MyString description]"])
107
108 # Lookup objc data type MyString and evaluate some expressions.
109
110 self.expect("image lookup -t NSString", DATA_TYPES_DISPLAYED_CORRECTLY,
Johnny Chen620f9d12010-09-29 17:50:35 +0000111 substrs = ['name = "NSString"',
Johnny Chenf8afe892010-10-12 21:20:11 +0000112 'clang_type = "@interface NSString'])
Johnny Chenb3307862010-09-17 22:28:51 +0000113
114 self.expect("image lookup -t MyString", DATA_TYPES_DISPLAYED_CORRECTLY,
Johnny Chen620f9d12010-09-29 17:50:35 +0000115 substrs = ['name = "MyString"',
Johnny Chenf8afe892010-10-12 21:20:11 +0000116 'clang_type = "@interface MyString',
117 'NSString * str;',
118 'NSDate * date;'])
Johnny Chenb3307862010-09-17 22:28:51 +0000119
Johnny Chen456c9c32010-10-13 19:22:50 +0000120 self.expect("frame variable -t -s", VARIABLES_DISPLAYED_CORRECTLY,
Johnny Chen7fce8772010-10-01 23:34:28 +0000121 substrs = ["ARG: (MyString *) self"],
122 patterns = ["ARG: \(.*\) _cmd",
123 "(struct objc_selector *)|(SEL)"])
Johnny Chenb3307862010-09-17 22:28:51 +0000124
Johnny Chen07f06c42010-11-11 19:15:04 +0000125 # rdar://problem/8651752
126 # don't crash trying to ask clang how many children an empty record has
127 self.runCmd("frame variable *_cmd")
128
Johnny Chen8eb38502010-09-29 17:58:12 +0000129 # rdar://problem/8492646
130 # test/foundation fails after updating to tot r115023
131 # self->str displays nothing as output
Johnny Chen456c9c32010-10-13 19:22:50 +0000132 self.expect("frame variable -t self->str", VARIABLES_DISPLAYED_CORRECTLY,
Johnny Chenb3307862010-09-17 22:28:51 +0000133 startstr = "(NSString *) self->str")
134
135 # rdar://problem/8447030
136 # 'frame variable self->date' displays the wrong data member
Johnny Chen456c9c32010-10-13 19:22:50 +0000137 self.expect("frame variable -t self->date", VARIABLES_DISPLAYED_CORRECTLY,
Johnny Chenb3307862010-09-17 22:28:51 +0000138 startstr = "(NSDate *) self->date")
139
Johnny Chenf8afe892010-10-12 21:20:11 +0000140 # This should display the str and date member fields as well.
Johnny Chen456c9c32010-10-13 19:22:50 +0000141 self.expect("frame variable -t *self", VARIABLES_DISPLAYED_CORRECTLY,
Johnny Chenf8afe892010-10-12 21:20:11 +0000142 substrs = ["(MyString *) self",
143 "(NSString *) str",
144 "(NSDate *) date"])
145
146 # This should fail expectedly.
Johnny Chena8807f92010-10-19 00:52:07 +0000147 self.expect("expression self->non_existent_member",
148 COMMAND_FAILED_AS_EXPECTED, error=True,
Johnny Chenf8afe892010-10-12 21:20:11 +0000149 startstr = "error: 'MyString' does not have a member named 'non_existent_member'")
150
151 # This currently fails.
152 # rdar://problem/8492646
153 #
154 # Use expression parser.
Johnny Chena8807f92010-10-19 00:52:07 +0000155 #self.runCmd("expression self->str")
156 #self.runCmd("expression self->date")
Johnny Chen2e566e02010-09-29 18:05:03 +0000157
Johnny Chena8807f92010-10-19 00:52:07 +0000158 # (lldb) expression self->str
Johnny Chenf8afe892010-10-12 21:20:11 +0000159 # error: instance variable 'str' is protected
Johnny Chenb3307862010-09-17 22:28:51 +0000160 # error: 1 errors parsing expression
Johnny Chenf8afe892010-10-12 21:20:11 +0000161 #
Johnny Chena8807f92010-10-19 00:52:07 +0000162 # (lldb) expression self->date
Johnny Chenf8afe892010-10-12 21:20:11 +0000163 # error: instance variable 'date' is protected
Johnny Chenb3307862010-09-17 22:28:51 +0000164 # error: 1 errors parsing expression
Johnny Chenf8afe892010-10-12 21:20:11 +0000165 #
166
Johnny Chen7987c612010-10-15 19:29:15 +0000167 self.runCmd("breakpoint delete 1")
Johnny Chenf8afe892010-10-12 21:20:11 +0000168 self.expect("breakpoint set -f main.m -l %d" % self.line,
169 BREAKPOINT_CREATED,
170 startstr = "Breakpoint created: 2: file ='main.m', line = %d, locations = 1" %
171 self.line)
172 self.runCmd("process continue")
173
174 # This currently fails.
175 # rdar://problem/8542091
176 # test/foundation: expr -o -- my not working?
177 #
178 # Test new feature with r115115:
179 # Add "-o" option to "expression" which prints the object description if available.
Johnny Chena8807f92010-10-19 00:52:07 +0000180 self.expect("expression -o -- my", "Object description displayed correctly",
Johnny Chen2769a422010-10-15 23:35:32 +0000181 patterns = ["Hello from.*a.out.*with timestamp: "])
Johnny Chenb3307862010-09-17 22:28:51 +0000182
Johnny Chen6c17c082010-09-16 20:54:06 +0000183
184if __name__ == '__main__':
185 import atexit
186 lldb.SBDebugger.Initialize()
187 atexit.register(lambda: lldb.SBDebugger.Terminate())
188 unittest2.main()