blob: 076a7501176e3a91653e8d14a212541d5e611825 [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
10 mydir = "bitfields"
11
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")
19 def test_with_dsym_and_python_api(self):
20 """Use Python APIs to inspect a bitfields variable."""
21 self.buildDsym()
22 self.bitfields_variable_python()
23
Johnny Chen65a65242010-08-31 21:49:24 +000024 def test_with_dwarf_and_run_command(self):
Johnny Chen979e7962010-09-02 15:59:20 +000025 """Test 'frame variable ...' on a variable with bitfields."""
Johnny Chen65a65242010-08-31 21:49:24 +000026 self.buildDwarf()
27 self.bitfields_variable()
28
29 def test_with_dwarf_and_python_api(self):
30 """Use Python APIs to inspect a bitfields variable."""
31 self.buildDwarf()
32 self.bitfields_variable_python()
33
Johnny Chencd9b7772010-10-12 00:09:25 +000034 def setUp(self):
Johnny Chenaadcef52010-10-14 17:31:24 +000035 # Call super's setUp().
36 TestBase.setUp(self)
Johnny Chencd9b7772010-10-12 00:09:25 +000037 # Find the line number to break inside main().
38 self.line = line_number('main.c', '// Set break point at this line.')
39
Johnny Chen65a65242010-08-31 21:49:24 +000040 def bitfields_variable(self):
Johnny Chen979e7962010-09-02 15:59:20 +000041 """Test 'frame variable ...' on a variable with bitfields."""
Johnny Chenea920fe2010-08-24 18:21:23 +000042 exe = os.path.join(os.getcwd(), "a.out")
43 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
44
45 # Break inside the main.
Johnny Chencd9b7772010-10-12 00:09:25 +000046 self.expect("breakpoint set -f main.c -l %d" % self.line,
47 BREAKPOINT_CREATED,
48 startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
49 self.line)
Johnny Chenea920fe2010-08-24 18:21:23 +000050
Johnny Chen5ee88192010-08-27 23:47:36 +000051 self.runCmd("run", RUN_SUCCEEDED)
Johnny Chenea920fe2010-08-24 18:21:23 +000052
53 # The stop reason of the thread should be breakpoint.
54 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Johnny Chen0c724ef2010-10-18 15:44:42 +000055 substrs = ['state is stopped',
Johnny Chenea920fe2010-08-24 18:21:23 +000056 'stop reason = breakpoint'])
57
58 # The breakpoint should have a hit count of 1.
59 self.expect("breakpoint list", BREAKPOINT_HIT_ONCE,
60 substrs = [' resolved, hit count = 1'])
61
62 # This should display correctly.
Johnny Chen456c9c32010-10-13 19:22:50 +000063 self.expect("frame variable -t bits", VARIABLES_DISPLAYED_CORRECTLY,
Greg Clayton8f92f0a2010-10-14 22:52:14 +000064 substrs = ['(uint32_t:1) b1 = 1',
65 '(uint32_t:2) b2 = 3',
66 '(uint32_t:3) b3 = 7',
67 '(uint32_t:4) b4 = 15',
68 '(uint32_t:5) b5 = 31',
69 '(uint32_t:6) b6 = 63',
70 '(uint32_t:7) b7 = 127',
Johnny Chen61464fa2010-09-24 17:33:29 +000071 '(uint32_t:4) four = 15'])
Johnny Chenea920fe2010-08-24 18:21:23 +000072
73 # And so should this.
74 # rdar://problem/8348251
Johnny Chen456c9c32010-10-13 19:22:50 +000075 self.expect("frame variable -t", VARIABLES_DISPLAYED_CORRECTLY,
Greg Clayton8f92f0a2010-10-14 22:52:14 +000076 substrs = ['(uint32_t:1) b1 = 1',
77 '(uint32_t:2) b2 = 3',
78 '(uint32_t:3) b3 = 7',
79 '(uint32_t:4) b4 = 15',
80 '(uint32_t:5) b5 = 31',
81 '(uint32_t:6) b6 = 63',
82 '(uint32_t:7) b7 = 127',
Johnny Chen61464fa2010-09-24 17:33:29 +000083 '(uint32_t:4) four = 15'])
Johnny Chenea920fe2010-08-24 18:21:23 +000084
Johnny Chen65a65242010-08-31 21:49:24 +000085 def bitfields_variable_python(self):
Johnny Chen827edff2010-08-27 00:15:48 +000086 """Use Python APIs to inspect a bitfields variable."""
87 exe = os.path.join(os.getcwd(), "a.out")
88
89 target = self.dbg.CreateTarget(exe)
90 self.assertTrue(target.IsValid(), VALID_TARGET)
91
Johnny Chencd9b7772010-10-12 00:09:25 +000092 breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
Johnny Chen827edff2010-08-27 00:15:48 +000093 self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT)
94
Johnny Chen63dfb272010-09-01 00:15:19 +000095 self.runCmd("run", RUN_SUCCEEDED, setCookie=False)
Johnny Chen827edff2010-08-27 00:15:48 +000096 # This does not work, and results in the process stopped at dyld_start?
97 #process = target.LaunchProcess([''], [''], os.ctermid(), False)
98
Johnny Chen7d1d7532010-09-02 21:23:12 +000099 self.process = target.GetProcess()
100 self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID)
101
Johnny Chen827edff2010-08-27 00:15:48 +0000102 # The stop reason of the thread should be breakpoint.
103 thread = target.GetProcess().GetThreadAtIndex(0)
Johnny Chenc5b06eb2010-10-07 16:51:56 +0000104 self.assertTrue(thread.GetStopReason() == lldb.eStopReasonBreakpoint,
Johnny Chen827edff2010-08-27 00:15:48 +0000105 STOPPED_DUE_TO_BREAKPOINT)
106
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)
112 bits = frame.LookupVar("bits")
113 self.DebugSBValue(frame, bits)
114 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)
123 self.DebugSBValue(frame, b1)
124 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)
131 self.DebugSBValue(frame, b7)
132 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)
139 self.DebugSBValue(frame, four)
140 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()