blob: 72b5ed2c338698553f84aa531f94c1587d7f7d10 [file] [log] [blame]
Johnny Chenea920fe2010-08-24 18:21:23 +00001"""Show bitfields and check that they display correctly."""
2
3import os, time
4import unittest2
5import lldb
6from lldbtest import *
7
Johnny Chencbb4be02010-09-01 19:59:58 +00008class BitfieldsTestCase(TestBase):
Johnny Chenea920fe2010-08-24 18:21:23 +00009
Johnny Chenfb8cd372011-06-25 20:43:57 +000010 mydir = os.path.join("lang", "c", "bitfields")
Johnny Chenea920fe2010-08-24 18:21:23 +000011
Johnny Chen65a65242010-08-31 21:49:24 +000012 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chen65a65242010-08-31 21:49:24 +000013 def test_with_dsym_and_run_command(self):
Johnny Chen979e7962010-09-02 15:59:20 +000014 """Test 'frame variable ...' on a variable with bitfields."""
Johnny Chen65a65242010-08-31 21:49:24 +000015 self.buildDsym()
16 self.bitfields_variable()
17
18 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chena47d7cb2010-12-10 01:21:27 +000019 @python_api_test
Johnny Chen65a65242010-08-31 21:49:24 +000020 def test_with_dsym_and_python_api(self):
21 """Use Python APIs to inspect a bitfields variable."""
22 self.buildDsym()
23 self.bitfields_variable_python()
24
Johnny Chen65a65242010-08-31 21:49:24 +000025 def test_with_dwarf_and_run_command(self):
Johnny Chen979e7962010-09-02 15:59:20 +000026 """Test 'frame variable ...' on a variable with bitfields."""
Johnny Chen65a65242010-08-31 21:49:24 +000027 self.buildDwarf()
28 self.bitfields_variable()
29
Johnny Chena47d7cb2010-12-10 01:21:27 +000030 @python_api_test
Johnny Chen65a65242010-08-31 21:49:24 +000031 def test_with_dwarf_and_python_api(self):
32 """Use Python APIs to inspect a bitfields variable."""
33 self.buildDwarf()
34 self.bitfields_variable_python()
35
Johnny Chencd9b7772010-10-12 00:09:25 +000036 def setUp(self):
Johnny Chenaadcef52010-10-14 17:31:24 +000037 # Call super's setUp().
38 TestBase.setUp(self)
Johnny Chencd9b7772010-10-12 00:09:25 +000039 # Find the line number to break inside main().
40 self.line = line_number('main.c', '// Set break point at this line.')
41
Johnny Chen65a65242010-08-31 21:49:24 +000042 def bitfields_variable(self):
Johnny Chen979e7962010-09-02 15:59:20 +000043 """Test 'frame variable ...' on a variable with bitfields."""
Johnny Chenea920fe2010-08-24 18:21:23 +000044 exe = os.path.join(os.getcwd(), "a.out")
45 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
46
47 # Break inside the main.
Johnny Chencd9b7772010-10-12 00:09:25 +000048 self.expect("breakpoint set -f main.c -l %d" % self.line,
49 BREAKPOINT_CREATED,
50 startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
51 self.line)
Johnny Chenea920fe2010-08-24 18:21:23 +000052
Johnny Chen5ee88192010-08-27 23:47:36 +000053 self.runCmd("run", RUN_SUCCEEDED)
Johnny Chenea920fe2010-08-24 18:21:23 +000054
55 # The stop reason of the thread should be breakpoint.
56 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Greg Clayton7260f622011-04-18 08:33:37 +000057 substrs = ['stopped',
Johnny Chenea920fe2010-08-24 18:21:23 +000058 'stop reason = breakpoint'])
59
60 # The breakpoint should have a hit count of 1.
Caroline Tice79042b32011-02-04 22:59:41 +000061 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
Johnny Chenea920fe2010-08-24 18:21:23 +000062 substrs = [' resolved, hit count = 1'])
63
64 # This should display correctly.
Jim Ingham2837b762011-05-04 03:43:18 +000065 self.expect("frame variable -T bits", VARIABLES_DISPLAYED_CORRECTLY,
Greg Clayton8f92f0a2010-10-14 22:52:14 +000066 substrs = ['(uint32_t:1) b1 = 1',
67 '(uint32_t:2) b2 = 3',
68 '(uint32_t:3) b3 = 7',
69 '(uint32_t:4) b4 = 15',
70 '(uint32_t:5) b5 = 31',
71 '(uint32_t:6) b6 = 63',
72 '(uint32_t:7) b7 = 127',
Johnny Chen61464fa2010-09-24 17:33:29 +000073 '(uint32_t:4) four = 15'])
Johnny Chenea920fe2010-08-24 18:21:23 +000074
75 # And so should this.
76 # rdar://problem/8348251
Jim Ingham2837b762011-05-04 03:43:18 +000077 self.expect("frame variable -T", VARIABLES_DISPLAYED_CORRECTLY,
Greg Clayton8f92f0a2010-10-14 22:52:14 +000078 substrs = ['(uint32_t:1) b1 = 1',
79 '(uint32_t:2) b2 = 3',
80 '(uint32_t:3) b3 = 7',
81 '(uint32_t:4) b4 = 15',
82 '(uint32_t:5) b5 = 31',
83 '(uint32_t:6) b6 = 63',
84 '(uint32_t:7) b7 = 127',
Johnny Chen61464fa2010-09-24 17:33:29 +000085 '(uint32_t:4) four = 15'])
Johnny Chenea920fe2010-08-24 18:21:23 +000086
Johnny Chen65a65242010-08-31 21:49:24 +000087 def bitfields_variable_python(self):
Johnny Chen827edff2010-08-27 00:15:48 +000088 """Use Python APIs to inspect a bitfields variable."""
89 exe = os.path.join(os.getcwd(), "a.out")
90
91 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000092 self.assertTrue(target, VALID_TARGET)
Johnny Chen827edff2010-08-27 00:15:48 +000093
Johnny Chencd9b7772010-10-12 00:09:25 +000094 breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000095 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen827edff2010-08-27 00:15:48 +000096
Johnny Chen5a0bee72011-06-15 22:14:12 +000097 process = target.LaunchSimple(None, None, os.getcwd())
98 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chen7d1d7532010-09-02 21:23:12 +000099
Johnny Chen827edff2010-08-27 00:15:48 +0000100 # The stop reason of the thread should be breakpoint.
101 thread = target.GetProcess().GetThreadAtIndex(0)
Johnny Chen378ed7f2010-11-10 23:14:41 +0000102 if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
Johnny Chende90f1d2011-04-27 17:43:07 +0000103 from lldbutil import stop_reason_to_str
Johnny Chen378ed7f2010-11-10 23:14:41 +0000104 self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
Johnny Chende90f1d2011-04-27 17:43:07 +0000105 stop_reason_to_str(thread.GetStopReason()))
Johnny Chen827edff2010-08-27 00:15:48 +0000106
107 # The breakpoint should have a hit count of 1.
108 self.assertTrue(breakpoint.GetHitCount() == 1, BREAKPOINT_HIT_ONCE)
109
110 # Lookup the "bits" variable which contains 8 bitfields.
111 frame = thread.GetFrameAtIndex(0)
Johnny Chen94f928b2010-12-14 18:59:15 +0000112 bits = frame.FindVariable("bits")
Johnny Chen9a07aba2011-07-11 20:06:28 +0000113 self.DebugSBValue(bits)
Johnny Chen827edff2010-08-27 00:15:48 +0000114 self.assertTrue(bits.GetTypeName() == "Bits" and
115 bits.GetNumChildren() == 8 and
116 bits.GetByteSize() == 4,
117 "(Bits)bits with byte size of 4 and 8 children")
118
Johnny Chen264d6592010-09-24 21:52:37 +0000119 # Notice the pattern of int(b1.GetValue(frame), 0). We pass a base of 0
120 # so that the proper radix is determined based on the contents of the
121 # string.
Johnny Chen827edff2010-08-27 00:15:48 +0000122 b1 = bits.GetChildAtIndex(0)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000123 self.DebugSBValue(b1)
Johnny Chen827edff2010-08-27 00:15:48 +0000124 self.assertTrue(b1.GetName() == "b1" and
125 b1.GetTypeName() == "uint32_t:1" and
126 b1.IsInScope(frame) and
Johnny Chen61464fa2010-09-24 17:33:29 +0000127 int(b1.GetValue(frame), 0) == 1,
128 'bits.b1 has type uint32_t:1, is in scope, and == 1')
Johnny Chen827edff2010-08-27 00:15:48 +0000129
130 b7 = bits.GetChildAtIndex(6)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000131 self.DebugSBValue(b7)
Johnny Chen827edff2010-08-27 00:15:48 +0000132 self.assertTrue(b7.GetName() == "b7" and
133 b7.GetTypeName() == "uint32_t:7" and
134 b7.IsInScope(frame) and
Johnny Chen61464fa2010-09-24 17:33:29 +0000135 int(b7.GetValue(frame), 0) == 127,
136 'bits.b7 has type uint32_t:7, is in scope, and == 127')
Johnny Chen827edff2010-08-27 00:15:48 +0000137
138 four = bits.GetChildAtIndex(7)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000139 self.DebugSBValue(four)
Johnny Chen827edff2010-08-27 00:15:48 +0000140 self.assertTrue(four.GetName() == "four" and
141 four.GetTypeName() == "uint32_t:4" and
142 four.IsInScope(frame) and
Johnny Chen61464fa2010-09-24 17:33:29 +0000143 int(four.GetValue(frame), 0) == 15,
144 'bits.four has type uint32_t:4, is in scope, and == 15')
Johnny Chen827edff2010-08-27 00:15:48 +0000145
Johnny Chen63dfb272010-09-01 00:15:19 +0000146 # Now kill the process, and we are done.
147 rc = target.GetProcess().Kill()
148 self.assertTrue(rc.Success())
149
Johnny Chenea920fe2010-08-24 18:21:23 +0000150
151if __name__ == '__main__':
152 import atexit
153 lldb.SBDebugger.Initialize()
154 atexit.register(lambda: lldb.SBDebugger.Terminate())
155 unittest2.main()