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 | |
| 10 | mydir = "bitfields" |
| 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") |
| 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 Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 24 | def test_with_dwarf_and_run_command(self): |
Johnny Chen | 979e796 | 2010-09-02 15:59:20 +0000 | [diff] [blame] | 25 | """Test 'frame variable ...' on a variable with bitfields.""" |
Johnny Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 26 | 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 Chen | cd9b777 | 2010-10-12 00:09:25 +0000 | [diff] [blame] | 34 | def setUp(self): |
Johnny Chen | aadcef5 | 2010-10-14 17:31:24 +0000 | [diff] [blame] | 35 | # Call super's setUp(). |
| 36 | TestBase.setUp(self) |
Johnny Chen | cd9b777 | 2010-10-12 00:09:25 +0000 | [diff] [blame] | 37 | # Find the line number to break inside main(). |
| 38 | self.line = line_number('main.c', '// Set break point at this line.') |
| 39 | |
Johnny Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 40 | def bitfields_variable(self): |
Johnny Chen | 979e796 | 2010-09-02 15:59:20 +0000 | [diff] [blame] | 41 | """Test 'frame variable ...' on a variable with bitfields.""" |
Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 42 | exe = os.path.join(os.getcwd(), "a.out") |
| 43 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
| 44 | |
| 45 | # Break inside the main. |
Johnny Chen | cd9b777 | 2010-10-12 00:09:25 +0000 | [diff] [blame] | 46 | 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 Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 50 | |
Johnny Chen | 5ee8819 | 2010-08-27 23:47:36 +0000 | [diff] [blame] | 51 | self.runCmd("run", RUN_SUCCEEDED) |
Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 52 | |
| 53 | # The stop reason of the thread should be breakpoint. |
| 54 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
Johnny Chen | 0c724ef | 2010-10-18 15:44:42 +0000 | [diff] [blame^] | 55 | substrs = ['state is stopped', |
Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 56 | '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 Chen | 456c9c3 | 2010-10-13 19:22:50 +0000 | [diff] [blame] | 63 | self.expect("frame variable -t bits", VARIABLES_DISPLAYED_CORRECTLY, |
Greg Clayton | 8f92f0a | 2010-10-14 22:52:14 +0000 | [diff] [blame] | 64 | 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 Chen | 61464fa | 2010-09-24 17:33:29 +0000 | [diff] [blame] | 71 | '(uint32_t:4) four = 15']) |
Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 72 | |
| 73 | # And so should this. |
| 74 | # rdar://problem/8348251 |
Johnny Chen | 456c9c3 | 2010-10-13 19:22:50 +0000 | [diff] [blame] | 75 | self.expect("frame variable -t", VARIABLES_DISPLAYED_CORRECTLY, |
Greg Clayton | 8f92f0a | 2010-10-14 22:52:14 +0000 | [diff] [blame] | 76 | 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 Chen | 61464fa | 2010-09-24 17:33:29 +0000 | [diff] [blame] | 83 | '(uint32_t:4) four = 15']) |
Johnny Chen | ea920fe | 2010-08-24 18:21:23 +0000 | [diff] [blame] | 84 | |
Johnny Chen | 65a6524 | 2010-08-31 21:49:24 +0000 | [diff] [blame] | 85 | def bitfields_variable_python(self): |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 86 | """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 Chen | cd9b777 | 2010-10-12 00:09:25 +0000 | [diff] [blame] | 92 | breakpoint = target.BreakpointCreateByLocation("main.c", self.line) |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 93 | self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT) |
| 94 | |
Johnny Chen | 63dfb27 | 2010-09-01 00:15:19 +0000 | [diff] [blame] | 95 | self.runCmd("run", RUN_SUCCEEDED, setCookie=False) |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 96 | # This does not work, and results in the process stopped at dyld_start? |
| 97 | #process = target.LaunchProcess([''], [''], os.ctermid(), False) |
| 98 | |
Johnny Chen | 7d1d753 | 2010-09-02 21:23:12 +0000 | [diff] [blame] | 99 | self.process = target.GetProcess() |
| 100 | self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID) |
| 101 | |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 102 | # The stop reason of the thread should be breakpoint. |
| 103 | thread = target.GetProcess().GetThreadAtIndex(0) |
Johnny Chen | c5b06eb | 2010-10-07 16:51:56 +0000 | [diff] [blame] | 104 | self.assertTrue(thread.GetStopReason() == lldb.eStopReasonBreakpoint, |
Johnny Chen | 827edff | 2010-08-27 00:15:48 +0000 | [diff] [blame] | 105 | 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 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) |
| 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 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) |
| 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 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) |
| 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 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() |