blob: 4a1b2e0eb2f9435bab1be938b7722f886c62447b [file] [log] [blame]
Johnny Chen377a8ed2010-07-28 21:24:31 +00001"""
Johnny Chen5fede4b2011-10-31 23:35:33 +00002Test some expressions involving STL data types.
Johnny Chen377a8ed2010-07-28 21:24:31 +00003"""
4
5import os, time
Johnny Chen73258832010-08-05 23:42:46 +00006import unittest2
Johnny Chen377a8ed2010-07-28 21:24:31 +00007import lldb
Johnny Chen15f247a2012-02-03 20:43:00 +00008import lldbutil
Johnny Chen17941842010-08-09 23:44:24 +00009from lldbtest import *
Johnny Chen377a8ed2010-07-28 21:24:31 +000010
Johnny Chencbb4be02010-09-01 19:59:58 +000011class STLTestCase(TestBase):
Johnny Chen377a8ed2010-07-28 21:24:31 +000012
Greg Clayton4570d3e2013-12-10 23:19:29 +000013 mydir = TestBase.compute_mydir(__file__)
Johnny Chen377a8ed2010-07-28 21:24:31 +000014
Johnny Chen9b547242011-11-14 18:33:39 +000015 # rdar://problem/10400981
Johnny Chen5fede4b2011-10-31 23:35:33 +000016 @unittest2.expectedFailure
Robert Flack13c7ad92015-03-30 14:12:17 +000017 @skipUnlessDarwin
Johnny Chen24086bc2012-04-06 19:54:10 +000018 @dsym_test
Johnny Chen165a0792010-09-07 18:27:35 +000019 def test_with_dsym(self):
Johnny Chen5fede4b2011-10-31 23:35:33 +000020 """Test some expressions involving STL data types."""
Johnny Chen165a0792010-09-07 18:27:35 +000021 self.buildDsym()
Johnny Chen5fede4b2011-10-31 23:35:33 +000022 self.step_stl_exprs()
Johnny Chen165a0792010-09-07 18:27:35 +000023
Johnny Chen9b547242011-11-14 18:33:39 +000024 # rdar://problem/10400981
Johnny Chen5fede4b2011-10-31 23:35:33 +000025 @unittest2.expectedFailure
Johnny Chen24086bc2012-04-06 19:54:10 +000026 @dwarf_test
Johnny Chen165a0792010-09-07 18:27:35 +000027 def test_with_dwarf(self):
Johnny Chen5fede4b2011-10-31 23:35:33 +000028 """Test some expressions involving STL data types."""
Johnny Chen165a0792010-09-07 18:27:35 +000029 self.buildDwarf()
Johnny Chen5fede4b2011-10-31 23:35:33 +000030 self.step_stl_exprs()
Johnny Chen165a0792010-09-07 18:27:35 +000031
Johnny Chen15f247a2012-02-03 20:43:00 +000032 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000033 @dsym_test
Siva Chandrac077b3d2015-04-06 21:52:10 +000034 @skipUnlessDarwin
Johnny Chen15f247a2012-02-03 20:43:00 +000035 def test_SBType_template_aspects_with_dsym(self):
Johnny Chen9dad8ae2012-02-03 20:50:56 +000036 """Test APIs for getting template arguments from an SBType."""
Johnny Chen15f247a2012-02-03 20:43:00 +000037 self.buildDsym()
38 self.sbtype_template_apis()
39
Ashok Thirumurthi1db10892013-08-01 18:52:01 +000040 @expectedFailureIcc # icc 13.1 and 14-beta do not emit DW_TAG_template_type_parameter
Johnny Chen15f247a2012-02-03 20:43:00 +000041 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000042 @dwarf_test
Johnny Chen15f247a2012-02-03 20:43:00 +000043 def test_SBType_template_aspects_with_dwarf(self):
Johnny Chen9dad8ae2012-02-03 20:50:56 +000044 """Test APIs for getting template arguments from an SBType."""
Johnny Chen15f247a2012-02-03 20:43:00 +000045 self.buildDwarf()
46 self.sbtype_template_apis()
47
Johnny Chen77ca1a42010-10-12 22:53:02 +000048 def setUp(self):
Johnny Chenaadcef52010-10-14 17:31:24 +000049 # Call super's setUp().
50 TestBase.setUp(self)
Johnny Chen77ca1a42010-10-12 22:53:02 +000051 # Find the line number to break inside main().
Johnny Chen15f247a2012-02-03 20:43:00 +000052 self.source = 'main.cpp'
53 self.line = line_number(self.source, '// Set break point at this line.')
Johnny Chen77ca1a42010-10-12 22:53:02 +000054
Johnny Chen5fede4b2011-10-31 23:35:33 +000055 def step_stl_exprs(self):
56 """Test some expressions involving STL data types."""
Johnny Chen377a8ed2010-07-28 21:24:31 +000057 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chenff3d01d2010-08-20 21:03:09 +000058
Johnny Chen377a8ed2010-07-28 21:24:31 +000059 # The following two lines, if uncommented, will enable loggings.
60 #self.ci.HandleCommand("log enable -f /tmp/lldb.log lldb default", res)
61 #self.assertTrue(res.Succeeded())
Johnny Chenff3d01d2010-08-20 21:03:09 +000062
63 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
Johnny Chen377a8ed2010-07-28 21:24:31 +000064
Johnny Chen78c0aeb2010-10-12 23:01:50 +000065 # rdar://problem/8543077
66 # test/stl: clang built binaries results in the breakpoint locations = 3,
67 # is this a problem with clang generated debug info?
Jim Ingham63dfc722012-09-22 00:05:11 +000068 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
Johnny Chen377a8ed2010-07-28 21:24:31 +000069
Johnny Chen5ee88192010-08-27 23:47:36 +000070 self.runCmd("run", RUN_SUCCEEDED)
Johnny Chen377a8ed2010-07-28 21:24:31 +000071
72 # Stop at 'std::string hello_world ("Hello World!");'.
Johnny Chenff3d01d2010-08-20 21:03:09 +000073 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Johnny Chen77ca1a42010-10-12 22:53:02 +000074 substrs = ['main.cpp:%d' % self.line,
Johnny Chenff3d01d2010-08-20 21:03:09 +000075 'stop reason = breakpoint'])
Johnny Chen377a8ed2010-07-28 21:24:31 +000076
77 # The breakpoint should have a hit count of 1.
Caroline Tice79042b32011-02-04 22:59:41 +000078 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
Johnny Chenff3d01d2010-08-20 21:03:09 +000079 substrs = [' resolved, hit count = 1'])
Johnny Chen377a8ed2010-07-28 21:24:31 +000080
Johnny Chen9a1e9af2011-10-31 23:28:52 +000081 # Now try some expressions....
Johnny Chen377a8ed2010-07-28 21:24:31 +000082
Johnny Chen9a1e9af2011-10-31 23:28:52 +000083 self.runCmd('expr for (int i = 0; i < hello_world.length(); ++i) { (void)printf("%c\\n", hello_world[i]); }')
84
Johnny Chen5fede4b2011-10-31 23:35:33 +000085 # rdar://problem/10373783
Johnny Chen9b547242011-11-14 18:33:39 +000086 # rdar://problem/10400981
Johnny Chen9a1e9af2011-10-31 23:28:52 +000087 self.expect('expr associative_array.size()',
88 substrs = [' = 3'])
89 self.expect('expr associative_array.count(hello_world)',
90 substrs = [' = 1'])
91 self.expect('expr associative_array[hello_world]',
92 substrs = [' = 1'])
93 self.expect('expr associative_array["hello"]',
94 substrs = [' = 2'])
Johnny Chen377a8ed2010-07-28 21:24:31 +000095
Johnny Chen15f247a2012-02-03 20:43:00 +000096 def sbtype_template_apis(self):
97 """Test APIs for getting template arguments from an SBType."""
98 exe = os.path.join(os.getcwd(), 'a.out')
99
100 # Create a target by the debugger.
101 target = self.dbg.CreateTarget(exe)
102 self.assertTrue(target, VALID_TARGET)
103
104 # Create the breakpoint inside function 'main'.
105 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
106 self.assertTrue(breakpoint, VALID_BREAKPOINT)
107
108 # Now launch the process, and do not stop at entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000109 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen15f247a2012-02-03 20:43:00 +0000110 self.assertTrue(process, PROCESS_IS_VALID)
111
112 # Get Frame #0.
113 self.assertTrue(process.GetState() == lldb.eStateStopped)
114 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +0000115 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition")
Johnny Chen15f247a2012-02-03 20:43:00 +0000116 frame0 = thread.GetFrameAtIndex(0)
117
118 # Get the type for variable 'associative_array'.
119 associative_array = frame0.FindVariable('associative_array')
120 self.DebugSBValue(associative_array)
121 self.assertTrue(associative_array, VALID_VARIABLE)
122 map_type = associative_array.GetType()
123 self.DebugSBType(map_type)
124 self.assertTrue(map_type, VALID_TYPE)
125 num_template_args = map_type.GetNumberOfTemplateArguments()
126 self.assertTrue(num_template_args > 0)
127
128 # We expect the template arguments to contain at least 'string' and 'int'.
129 expected_types = { 'string': False, 'int': False }
130 for i in range(num_template_args):
131 t = map_type.GetTemplateArgumentType(i)
132 self.DebugSBType(t)
133 self.assertTrue(t, VALID_TYPE)
134 name = t.GetName()
135 if 'string' in name:
136 expected_types['string'] = True
137 elif 'int' == name:
138 expected_types['int'] = True
139
140 # Check that both entries of the dictionary have 'True' as the value.
141 self.assertTrue(all(expected_types.values()))
142
Johnny Chen377a8ed2010-07-28 21:24:31 +0000143
144if __name__ == '__main__':
Johnny Chena2124952010-08-05 21:23:45 +0000145 import atexit
Johnny Chen377a8ed2010-07-28 21:24:31 +0000146 lldb.SBDebugger.Initialize()
Johnny Chena2124952010-08-05 21:23:45 +0000147 atexit.register(lambda: lldb.SBDebugger.Terminate())
Johnny Chen73258832010-08-05 23:42:46 +0000148 unittest2.main()