| Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 1 | """Show bitfields and check that they display correctly.""" |
| 2 | |
| 3 | import os, time |
| 4 | import unittest2 |
| 5 | import lldb |
| 6 | from lldbtest import * |
| 7 | |
| Johnny Chen | cbb4be0 | 2010-09-01 19:59:58 +0000 | [diff] [blame] | 8 | class BitfieldsTestCase(TestBase): |
| Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 9 | |
| Johnny Chen | fb8cd37 | 2011-06-25 20:43:57 +0000 | [diff] [blame] | 10 | mydir = os.path.join("lang", "c", "bitfields") |
| Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 11 | |
| Johnny Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 12 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| Johnny Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 13 | def test_with_dsym_and_run_command(self): |
| Johnny Chen | 979e796 | 2010-09-02 15:59:20 +0000 | [diff] [blame] | 14 | """Test 'frame variable ...' on a variable with bitfields.""" |
| Johnny Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 15 | self.buildDsym() |
| 16 | self.bitfields_variable() |
| 17 | |
| 18 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| Johnny Chen | a47d7cb | 2010-12-10 01:21:27 +0000 | [diff] [blame] | 19 | @python_api_test |
| Johnny Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 20 | 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 Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 25 | def test_with_dwarf_and_run_command(self): |
| Johnny Chen | 979e796 | 2010-09-02 15:59:20 +0000 | [diff] [blame] | 26 | """Test 'frame variable ...' on a variable with bitfields.""" |
| Johnny Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 27 | self.buildDwarf() |
| 28 | self.bitfields_variable() |
| 29 | |
| Johnny Chen | a47d7cb | 2010-12-10 01:21:27 +0000 | [diff] [blame] | 30 | @python_api_test |
| Johnny Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 31 | 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 Chen | cd9b777 | 2010-10-12 00:09:25 +0000 | [diff] [blame] | 36 | def setUp(self): |
| Johnny Chen | aadcef5 | 2010-10-14 17:31:24 +0000 | [diff] [blame] | 37 | # Call super's setUp(). |
| 38 | TestBase.setUp(self) |
| Johnny Chen | cd9b777 | 2010-10-12 00:09:25 +0000 | [diff] [blame] | 39 | # Find the line number to break inside main(). |
| 40 | self.line = line_number('main.c', '// Set break point at this line.') |
| 41 | |
| Johnny Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 42 | def bitfields_variable(self): |
| Johnny Chen | 979e796 | 2010-09-02 15:59:20 +0000 | [diff] [blame] | 43 | """Test 'frame variable ...' on a variable with bitfields.""" |
| Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 44 | exe = os.path.join(os.getcwd(), "a.out") |
| 45 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 46 | |
| 47 | # Break inside the main. |
| Johnny Chen | cd9b777 | 2010-10-12 00:09:25 +0000 | [diff] [blame] | 48 | 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 Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 52 | |
| Johnny Chen | 5ee8819 | 2010-08-27 23:47:36 +0000 | [diff] [blame] | 53 | self.runCmd("run", RUN_SUCCEEDED) |
| Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 54 | |
| 55 | # The stop reason of the thread should be breakpoint. |
| 56 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
| Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 57 | substrs = ['stopped', |
| Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 58 | 'stop reason = breakpoint']) |
| 59 | |
| 60 | # The breakpoint should have a hit count of 1. |
| Caroline Tice | 79042b3 | 2011-02-04 22:59:41 +0000 | [diff] [blame] | 61 | self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE, |
| Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 62 | substrs = [' resolved, hit count = 1']) |
| 63 | |
| 64 | # This should display correctly. |
| Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 65 | self.expect("frame variable -T bits", VARIABLES_DISPLAYED_CORRECTLY, |
| Greg Clayton | 8f92f0a | 2010-10-14 22:52:14 +0000 | [diff] [blame] | 66 | 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 Chen | 61464fa | 2010-09-24 17:33:29 +0000 | [diff] [blame] | 73 | '(uint32_t:4) four = 15']) |
| Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 74 | |
| 75 | # And so should this. |
| 76 | # rdar://problem/8348251 |
| Jim Ingham | 2837b76 | 2011-05-04 03:43:18 +0000 | [diff] [blame] | 77 | self.expect("frame variable -T", VARIABLES_DISPLAYED_CORRECTLY, |
| Greg Clayton | 8f92f0a | 2010-10-14 22:52:14 +0000 | [diff] [blame] | 78 | 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 Chen | 61464fa | 2010-09-24 17:33:29 +0000 | [diff] [blame] | 85 | '(uint32_t:4) four = 15']) |
| Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 86 | |
| Johnny Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 87 | def bitfields_variable_python(self): |
| Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 88 | """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 Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 92 | self.assertTrue(target, VALID_TARGET) |
| Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 93 | |
| Johnny Chen | cd9b777 | 2010-10-12 00:09:25 +0000 | [diff] [blame] | 94 | breakpoint = target.BreakpointCreateByLocation("main.c", self.line) |
| Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 95 | self.assertTrue(breakpoint, VALID_BREAKPOINT) |
| Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 96 | |
| Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 97 | process = target.LaunchSimple(None, None, os.getcwd()) |
| 98 | self.assertTrue(process, PROCESS_IS_VALID) |
| Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame] | 99 | |
| Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 100 | # The stop reason of the thread should be breakpoint. |
| 101 | thread = target.GetProcess().GetThreadAtIndex(0) |
| Johnny Chen | 378ed7f | 2010-11-10 23:14:41 +0000 | [diff] [blame] | 102 | if thread.GetStopReason() != lldb.eStopReasonBreakpoint: |
| Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 103 | from lldbutil import stop_reason_to_str |
| Johnny Chen | 378ed7f | 2010-11-10 23:14:41 +0000 | [diff] [blame] | 104 | self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS % |
| Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 105 | stop_reason_to_str(thread.GetStopReason())) |
| Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 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) |
| Johnny Chen | 94f928b | 2010-12-14 18:59:15 +0000 | [diff] [blame] | 112 | bits = frame.FindVariable("bits") |
| Johnny Chen | 9a07aba | 2011-07-11 20:06:28 +0000 | [diff] [blame^] | 113 | self.DebugSBValue(bits) |
| Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 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 Chen | 264d659 | 2010-09-24 21:52:37 +0000 | [diff] [blame] | 119 | # 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 Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 122 | b1 = bits.GetChildAtIndex(0) |
| Johnny Chen | 9a07aba | 2011-07-11 20:06:28 +0000 | [diff] [blame^] | 123 | self.DebugSBValue(b1) |
| Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 124 | self.assertTrue(b1.GetName() == "b1" and |
| 125 | b1.GetTypeName() == "uint32_t:1" and |
| 126 | b1.IsInScope(frame) and |
| Johnny Chen | 61464fa | 2010-09-24 17:33:29 +0000 | [diff] [blame] | 127 | int(b1.GetValue(frame), 0) == 1, |
| 128 | 'bits.b1 has type uint32_t:1, is in scope, and == 1') |
| Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 129 | |
| 130 | b7 = bits.GetChildAtIndex(6) |
| Johnny Chen | 9a07aba | 2011-07-11 20:06:28 +0000 | [diff] [blame^] | 131 | self.DebugSBValue(b7) |
| Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 132 | self.assertTrue(b7.GetName() == "b7" and |
| 133 | b7.GetTypeName() == "uint32_t:7" and |
| 134 | b7.IsInScope(frame) and |
| Johnny Chen | 61464fa | 2010-09-24 17:33:29 +0000 | [diff] [blame] | 135 | int(b7.GetValue(frame), 0) == 127, |
| 136 | 'bits.b7 has type uint32_t:7, is in scope, and == 127') |
| Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 137 | |
| 138 | four = bits.GetChildAtIndex(7) |
| Johnny Chen | 9a07aba | 2011-07-11 20:06:28 +0000 | [diff] [blame^] | 139 | self.DebugSBValue(four) |
| Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 140 | self.assertTrue(four.GetName() == "four" and |
| 141 | four.GetTypeName() == "uint32_t:4" and |
| 142 | four.IsInScope(frame) and |
| Johnny Chen | 61464fa | 2010-09-24 17:33:29 +0000 | [diff] [blame] | 143 | int(four.GetValue(frame), 0) == 15, |
| 144 | 'bits.four has type uint32_t:4, is in scope, and == 15') |
| Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 145 | |
| Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 146 | # Now kill the process, and we are done. |
| 147 | rc = target.GetProcess().Kill() |
| 148 | self.assertTrue(rc.Success()) |
| 149 | |
| Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 150 | |
| 151 | if __name__ == '__main__': |
| 152 | import atexit |
| 153 | lldb.SBDebugger.Initialize() |
| 154 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 155 | unittest2.main() |