TestArrayTypes.py revision b794017ee4b085b28249af1511b3bdcaef8924ce
1"""Test breakpoint by file/line number; and list variables with array types."""
2
3import os, time
4import unittest2
5import lldb
6from lldbtest import *
7
8class ArrayTypesTestCase(TestBase):
9
10    mydir = os.path.join("lang", "c", "array_types")
11
12    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
13    def test_with_dsym_and_run_command(self):
14        """Test 'frame variable var_name' on some variables with array types."""
15        self.buildDsym()
16        self.array_types()
17
18    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
19    @python_api_test
20    def test_with_dsym_and_python_api(self):
21        """Use Python APIs to inspect variables with array types."""
22        self.buildDsym()
23        self.array_types_python()
24
25    def test_with_dwarf_and_run_command(self):
26        """Test 'frame variable var_name' on some variables with array types."""
27        self.buildDwarf()
28        self.array_types()
29
30    @python_api_test
31    def test_with_dwarf_and_python_api(self):
32        """Use Python APIs to inspect variables with array types."""
33        self.buildDwarf()
34        self.array_types_python()
35
36    def setUp(self):
37        # Call super's setUp().
38        TestBase.setUp(self)
39        # Find the line number to break inside main().
40        self.line = line_number('main.c', '// Set break point at this line.')
41
42    def array_types(self):
43        """Test 'frame variable var_name' on some variables with array types."""
44        exe = os.path.join(os.getcwd(), "a.out")
45        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
46
47        self.expect("breakpoint set -f main.c -l %d" % self.line,
48                    BREAKPOINT_CREATED,
49            startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
50                        self.line)
51
52        self.runCmd("run", RUN_SUCCEEDED)
53
54        # The test suite sometimes shows that the process has exited without stopping.
55        #
56        # CC=clang ./dotest.py -v -t array_types
57        # ...
58        # Process 76604 exited with status = 0 (0x00000000)
59        self.runCmd("process status")
60
61        # The stop reason of the thread should be breakpoint.
62        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
63            substrs = ['stopped',
64                       'stop reason = breakpoint'])
65
66        # The breakpoint should have a hit count of 1.
67        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
68            substrs = ['resolved, hit count = 1'])
69
70        # Issue 'variable list' command on several array-type variables.
71
72        self.expect("frame variable -T strings", VARIABLES_DISPLAYED_CORRECTLY,
73            startstr = '(char *[4])',
74            substrs = ['(char *) [0]',
75                       '(char *) [1]',
76                       '(char *) [2]',
77                       '(char *) [3]',
78                       'Hello',
79                       'Hola',
80                       'Bonjour',
81                       'Guten Tag'])
82
83        self.expect("frame variable -T char_16", VARIABLES_DISPLAYED_CORRECTLY,
84            substrs = ['(char) [0]',
85                       '(char) [15]'])
86
87        self.expect("frame variable -T ushort_matrix", VARIABLES_DISPLAYED_CORRECTLY,
88            startstr = '(unsigned short [2][3])')
89
90        self.expect("frame variable -T long_6", VARIABLES_DISPLAYED_CORRECTLY,
91            startstr = '(long [6])')
92
93    def array_types_python(self):
94        """Use Python APIs to inspect variables with array types."""
95        exe = os.path.join(os.getcwd(), "a.out")
96
97        target = self.dbg.CreateTarget(exe)
98        self.assertTrue(target, VALID_TARGET)
99
100        breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
101        self.assertTrue(breakpoint, VALID_BREAKPOINT)
102
103        # Sanity check the print representation of breakpoint.
104        bp = repr(breakpoint)
105        self.expect(bp, msg="Breakpoint looks good", exe=False,
106            substrs = ["file ='main.c'",
107                       "line = %d" % self.line,
108                       "locations = 1"])
109        self.expect(bp, msg="Breakpoint is not resolved as yet", exe=False, matching=False,
110            substrs = ["resolved = 1"])
111
112        # Now launch the process, and do not stop at entry point.
113        process = target.LaunchSimple(None, None, os.getcwd())
114        self.assertTrue(process, PROCESS_IS_VALID)
115
116        # Sanity check the print representation of process.
117        proc = repr(process)
118        self.expect(proc, msg="Process looks good", exe=False,
119            substrs = ["state = stopped",
120                       "executable = a.out"])
121
122        # The stop reason of the thread should be breakpoint.
123        thread = process.GetThreadAtIndex(0)
124        if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
125            from lldbutil import stop_reason_to_str
126            self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
127                      stop_reason_to_str(thread.GetStopReason()))
128
129        # Sanity check the print representation of thread.
130        thr = repr(thread)
131        self.expect(thr, "Thread looks good with stop reason = breakpoint", exe=False,
132            substrs = ["tid = 0x%4.4x" % thread.GetThreadID()])
133
134        # The breakpoint should have a hit count of 1.
135        self.assertTrue(breakpoint.GetHitCount() == 1, BREAKPOINT_HIT_ONCE)
136
137        # The breakpoint should be resolved by now.
138        bp = repr(breakpoint)
139        self.expect(bp, "Breakpoint looks good and is resolved", exe=False,
140            substrs = ["file ='main.c'",
141                       "line = %d" % self.line,
142                       "locations = 1"])
143
144        # Sanity check the print representation of frame.
145        frame = thread.GetFrameAtIndex(0)
146        frm = repr(frame)
147        self.expect(frm,
148                    "Frame looks good with correct index %d" % frame.GetFrameID(),
149                    exe=False,
150            substrs = ["#%d" % frame.GetFrameID()])
151
152        # Lookup the "strings" string array variable and sanity check its print
153        # representation.
154        variable = frame.FindVariable("strings")
155        var = repr(variable)
156        self.expect(var, "Variable for 'strings' looks good with correct name", exe=False,
157            substrs = ["name: '%s'" % variable.GetName()])
158        self.DebugSBValue(frame, variable)
159        self.assertTrue(variable.GetNumChildren() == 4,
160                        "Variable 'strings' should have 4 children")
161
162        child3 = variable.GetChildAtIndex(3)
163        self.DebugSBValue(frame, child3)
164        self.assertTrue(child3.GetSummary(frame) == '"Guten Tag"',
165                        'strings[3] == "Guten Tag"')
166
167        # Lookup the "char_16" char array variable.
168        variable = frame.FindVariable("char_16")
169        self.DebugSBValue(frame, variable)
170        self.assertTrue(variable.GetNumChildren() == 16,
171                        "Variable 'char_16' should have 16 children")
172
173        # Lookup the "ushort_matrix" ushort[] array variable.
174        # Notice the pattern of int(child0_2.GetValue(frame), 0).  We pass a
175        # base of 0 so that the proper radix is determined based on the contents
176        # of the string.  Same applies to long().
177        variable = frame.FindVariable("ushort_matrix")
178        self.DebugSBValue(frame, variable)
179        self.assertTrue(variable.GetNumChildren() == 2,
180                        "Variable 'ushort_matrix' should have 2 children")
181        child0 = variable.GetChildAtIndex(0)
182        self.DebugSBValue(frame, child0)
183        self.assertTrue(child0.GetNumChildren() == 3,
184                        "Variable 'ushort_matrix[0]' should have 3 children")
185        child0_2 = child0.GetChildAtIndex(2)
186        self.DebugSBValue(frame, child0_2)
187        self.assertTrue(int(child0_2.GetValue(frame), 0) == 3,
188                        "ushort_matrix[0][2] == 3")
189
190        # Lookup the "long_6" char array variable.
191        variable = frame.FindVariable("long_6")
192        self.DebugSBValue(frame, variable)
193        self.assertTrue(variable.GetNumChildren() == 6,
194                        "Variable 'long_6' should have 6 children")
195        child5 = variable.GetChildAtIndex(5)
196        self.DebugSBValue(frame, child5)
197        self.assertTrue(long(child5.GetValue(frame), 0) == 6,
198                        "long_6[5] == 6")
199
200        # Last, check that "long_6" has a value type of eValueTypeVariableLocal
201        # and "argc" has eValueTypeVariableArgument.
202        from lldbutil import value_type_to_str
203        self.assertTrue(variable.GetValueType() == lldb.eValueTypeVariableLocal,
204                        "Variable 'long_6' should have '%s' value type." %
205                        value_type_to_str(lldb.eValueTypeVariableLocal))
206        argc = frame.FindVariable("argc")
207        self.DebugSBValue(frame, argc)
208        self.assertTrue(argc.GetValueType() == lldb.eValueTypeVariableArgument,
209                        "Variable 'argc' should have '%s' value type." %
210                        value_type_to_str(lldb.eValueTypeVariableArgument))
211
212
213if __name__ == '__main__':
214    import atexit
215    lldb.SBDebugger.Initialize()
216    atexit.register(lambda: lldb.SBDebugger.Terminate())
217    unittest2.main()
218