]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/lang/c/bitfields/TestBitfields.py
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / lang / c / bitfields / TestBitfields.py
1 """Show bitfields and check that they display correctly."""
2
3 from __future__ import print_function
4
5
6
7 import os, time
8 import lldb
9 from lldbsuite.test.lldbtest import *
10 import lldbsuite.test.lldbutil as lldbutil
11
12 class BitfieldsTestCase(TestBase):
13
14     mydir = TestBase.compute_mydir(__file__)
15     
16     def setUp(self):
17         # Call super's setUp().
18         TestBase.setUp(self)
19         # Find the line number to break inside main().
20         self.line = line_number('main.c', '// Set break point at this line.')
21
22     @skipIfWindows # BitFields exhibit crashes in record layout on Windows (http://llvm.org/pr21800)
23     def test_and_run_command(self):
24         """Test 'frame variable ...' on a variable with bitfields."""
25         self.build()
26         exe = os.path.join(os.getcwd(), "a.out")
27         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
28
29         # Break inside the main.
30         lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
31
32         self.runCmd("run", RUN_SUCCEEDED)
33
34         # The stop reason of the thread should be breakpoint.
35         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
36             substrs = ['stopped',
37                        'stop reason = breakpoint'])
38
39         # The breakpoint should have a hit count of 1.
40         self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
41             substrs = [' resolved, hit count = 1'])
42
43         # This should display correctly.
44         self.expect("frame variable --show-types bits", VARIABLES_DISPLAYED_CORRECTLY,
45             substrs = ['(uint32_t:1) b1 = 1',
46                        '(uint32_t:2) b2 = 3',
47                        '(uint32_t:3) b3 = 7',
48                        '(uint32_t) b4 = 15',
49                        '(uint32_t:5) b5 = 31',
50                        '(uint32_t:6) b6 = 63',
51                        '(uint32_t:7) b7 = 127',
52                        '(uint32_t:4) four = 15'])
53
54         # And so should this.
55         # rdar://problem/8348251
56         self.expect("frame variable --show-types", VARIABLES_DISPLAYED_CORRECTLY,
57             substrs = ['(uint32_t:1) b1 = 1',
58                        '(uint32_t:2) b2 = 3',
59                        '(uint32_t:3) b3 = 7',
60                        '(uint32_t) b4 = 15',
61                        '(uint32_t:5) b5 = 31',
62                        '(uint32_t:6) b6 = 63',
63                        '(uint32_t:7) b7 = 127',
64                        '(uint32_t:4) four = 15'])
65
66         self.expect("expr (bits.b1)", VARIABLES_DISPLAYED_CORRECTLY,
67             substrs = ['uint32_t', '1'])
68         self.expect("expr (bits.b2)", VARIABLES_DISPLAYED_CORRECTLY,
69             substrs = ['uint32_t', '3'])
70         self.expect("expr (bits.b3)", VARIABLES_DISPLAYED_CORRECTLY,
71             substrs = ['uint32_t', '7'])
72         self.expect("expr (bits.b4)", VARIABLES_DISPLAYED_CORRECTLY,
73             substrs = ['uint32_t', '15'])
74         self.expect("expr (bits.b5)", VARIABLES_DISPLAYED_CORRECTLY,
75             substrs = ['uint32_t', '31'])
76         self.expect("expr (bits.b6)", VARIABLES_DISPLAYED_CORRECTLY,
77             substrs = ['uint32_t', '63'])
78         self.expect("expr (bits.b7)", VARIABLES_DISPLAYED_CORRECTLY,
79             substrs = ['uint32_t', '127'])
80         self.expect("expr (bits.four)", VARIABLES_DISPLAYED_CORRECTLY,
81             substrs = ['uint32_t', '15'])
82
83         self.expect("frame variable --show-types more_bits", VARIABLES_DISPLAYED_CORRECTLY,
84             substrs = ['(uint32_t:3) a = 3',
85                        '(uint8_t:1) b = \'\\0\'',
86                        '(uint8_t:1) c = \'\\x01\'',
87                        '(uint8_t:1) d = \'\\0\''])
88
89         self.expect("expr (more_bits.a)", VARIABLES_DISPLAYED_CORRECTLY,
90             substrs = ['uint32_t', '3'])
91         self.expect("expr (more_bits.b)", VARIABLES_DISPLAYED_CORRECTLY,
92             substrs = ['uint8_t', '\\0'])
93         self.expect("expr (more_bits.c)", VARIABLES_DISPLAYED_CORRECTLY,
94             substrs = ['uint8_t', '\\x01'])
95         self.expect("expr (more_bits.d)", VARIABLES_DISPLAYED_CORRECTLY,
96             substrs = ['uint8_t', '\\0'])
97
98     @add_test_categories(['pyapi'])
99     @skipIfWindows # BitFields exhibit crashes in record layout on Windows (http://llvm.org/pr21800)
100     def test_and_python_api(self):
101         """Use Python APIs to inspect a bitfields variable."""
102         self.build()
103         exe = os.path.join(os.getcwd(), "a.out")
104
105         target = self.dbg.CreateTarget(exe)
106         self.assertTrue(target, VALID_TARGET)
107
108         breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
109         self.assertTrue(breakpoint, VALID_BREAKPOINT)
110
111         process = target.LaunchSimple (None, None, self.get_process_working_directory())
112         self.assertTrue(process, PROCESS_IS_VALID)
113
114         # The stop reason of the thread should be breakpoint.
115         thread = target.GetProcess().GetThreadAtIndex(0)
116         if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
117             from lldbsuite.test.lldbutil import stop_reason_to_str
118             self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
119                       stop_reason_to_str(thread.GetStopReason()))
120
121         # The breakpoint should have a hit count of 1.
122         self.assertTrue(breakpoint.GetHitCount() == 1, BREAKPOINT_HIT_ONCE)
123
124         # Lookup the "bits" variable which contains 8 bitfields.
125         frame = thread.GetFrameAtIndex(0)
126         bits = frame.FindVariable("bits")
127         self.DebugSBValue(bits)
128         self.assertTrue(bits.GetTypeName() == 'Bits', "bits.GetTypeName() == 'Bits'");
129         self.assertTrue(bits.GetNumChildren() == 10, "bits.GetNumChildren() == 10");
130         test_compiler = self.getCompiler()
131         self.assertTrue(bits.GetByteSize() == 32, "bits.GetByteSize() == 32");
132
133         # Notice the pattern of int(b1.GetValue(), 0).  We pass a base of 0
134         # so that the proper radix is determined based on the contents of the
135         # string.
136         b1 = bits.GetChildMemberWithName("b1")
137         self.DebugSBValue(b1)
138         self.assertTrue(b1.GetName() == "b1" and
139                         b1.GetTypeName() == "uint32_t:1" and
140                         b1.IsInScope() and
141                         int(b1.GetValue(), 0) == 1,
142                         'bits.b1 has type uint32_t:1, is in scope, and == 1')
143
144         b7 = bits.GetChildMemberWithName("b7")
145         self.DebugSBValue(b7)
146         self.assertTrue(b7.GetName() == "b7" and
147                         b7.GetTypeName() == "uint32_t:7" and
148                         b7.IsInScope() and
149                         int(b7.GetValue(), 0) == 127,
150                         'bits.b7 has type uint32_t:7, is in scope, and == 127')
151
152         four = bits.GetChildMemberWithName("four")
153         self.DebugSBValue(four)
154         self.assertTrue(four.GetName() == "four" and
155                         four.GetTypeName() == "uint32_t:4" and
156                         four.IsInScope() and
157                         int(four.GetValue(), 0) == 15,
158                         'bits.four has type uint32_t:4, is in scope, and == 15')
159
160         # Now kill the process, and we are done.
161         rc = target.GetProcess().Kill()
162         self.assertTrue(rc.Success())