Home | History | Annotate | Download | only in bitfields
      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 import lldbutil
      8 
      9 class BitfieldsTestCase(TestBase):
     10 
     11     mydir = os.path.join("lang", "c", "bitfields")
     12 
     13     @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
     14     @dsym_test
     15     def test_with_dsym_and_run_command(self):
     16         """Test 'frame variable ...' on a variable with bitfields."""
     17         self.buildDsym()
     18         self.bitfields_variable()
     19 
     20     @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
     21     @python_api_test
     22     @dsym_test
     23     def test_with_dsym_and_python_api(self):
     24         """Use Python APIs to inspect a bitfields variable."""
     25         self.buildDsym()
     26         self.bitfields_variable_python()
     27 
     28     @dwarf_test
     29     def test_with_dwarf_and_run_command(self):
     30         """Test 'frame variable ...' on a variable with bitfields."""
     31         self.buildDwarf()
     32         self.bitfields_variable()
     33 
     34     @python_api_test
     35     @dwarf_test
     36     @expectedFailureGcc # GCC (4.6/4.7) generates incorrect code with unnamed bitfields.
     37     def test_with_dwarf_and_python_api(self):
     38         """Use Python APIs to inspect a bitfields variable."""
     39         self.buildDwarf()
     40         self.bitfields_variable_python()
     41 
     42     def setUp(self):
     43         # Call super's setUp().
     44         TestBase.setUp(self)
     45         # Find the line number to break inside main().
     46         self.line = line_number('main.c', '// Set break point at this line.')
     47 
     48     def bitfields_variable(self):
     49         """Test 'frame variable ...' on a variable with bitfields."""
     50         exe = os.path.join(os.getcwd(), "a.out")
     51         self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
     52 
     53         # Break inside the main.
     54         lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
     55 
     56         self.runCmd("run", RUN_SUCCEEDED)
     57 
     58         # The stop reason of the thread should be breakpoint.
     59         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
     60             substrs = ['stopped',
     61                        'stop reason = breakpoint'])
     62 
     63         # The breakpoint should have a hit count of 1.
     64         self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
     65             substrs = [' resolved, hit count = 1'])
     66 
     67         # This should display correctly.
     68         self.expect("frame variable --show-types bits", VARIABLES_DISPLAYED_CORRECTLY,
     69             substrs = ['(uint32_t:1) b1 = 1',
     70                        '(uint32_t:2) b2 = 3',
     71                        '(uint32_t:3) b3 = 7',
     72                        '(uint32_t) b4 = 15',
     73                        '(uint32_t:5) b5 = 31',
     74                        '(uint32_t:6) b6 = 63',
     75                        '(uint32_t:7) b7 = 127',
     76                        '(uint32_t:4) four = 15'])
     77 
     78         # And so should this.
     79         # rdar://problem/8348251
     80         self.expect("frame variable --show-types", VARIABLES_DISPLAYED_CORRECTLY,
     81             substrs = ['(uint32_t:1) b1 = 1',
     82                        '(uint32_t:2) b2 = 3',
     83                        '(uint32_t:3) b3 = 7',
     84                        '(uint32_t) b4 = 15',
     85                        '(uint32_t:5) b5 = 31',
     86                        '(uint32_t:6) b6 = 63',
     87                        '(uint32_t:7) b7 = 127',
     88                        '(uint32_t:4) four = 15'])
     89 
     90         self.expect("expr (bits.b1)", VARIABLES_DISPLAYED_CORRECTLY,
     91             substrs = ['uint32_t', '1'])
     92         self.expect("expr (bits.b2)", VARIABLES_DISPLAYED_CORRECTLY,
     93             substrs = ['uint32_t', '3'])
     94         self.expect("expr (bits.b3)", VARIABLES_DISPLAYED_CORRECTLY,
     95             substrs = ['uint32_t', '7'])
     96         self.expect("expr (bits.b4)", VARIABLES_DISPLAYED_CORRECTLY,
     97             substrs = ['uint32_t', '15'])
     98         self.expect("expr (bits.b5)", VARIABLES_DISPLAYED_CORRECTLY,
     99             substrs = ['uint32_t', '31'])
    100         self.expect("expr (bits.b6)", VARIABLES_DISPLAYED_CORRECTLY,
    101             substrs = ['uint32_t', '63'])
    102         self.expect("expr (bits.b7)", VARIABLES_DISPLAYED_CORRECTLY,
    103             substrs = ['uint32_t', '127'])
    104         self.expect("expr (bits.four)", VARIABLES_DISPLAYED_CORRECTLY,
    105             substrs = ['uint32_t', '15'])
    106 
    107         self.expect("frame variable --show-types more_bits", VARIABLES_DISPLAYED_CORRECTLY,
    108             substrs = ['(uint32_t:3) a = 3',
    109                        '(uint8_t:1) b = \'\\0\'',
    110                        '(uint8_t:1) c = \'\\x01\'',
    111                        '(uint8_t:1) d = \'\\0\''])
    112 
    113         self.expect("expr (more_bits.a)", VARIABLES_DISPLAYED_CORRECTLY,
    114             substrs = ['uint32_t', '3'])
    115         self.expect("expr (more_bits.b)", VARIABLES_DISPLAYED_CORRECTLY,
    116             substrs = ['uint8_t', '\\0'])
    117         self.expect("expr (more_bits.c)", VARIABLES_DISPLAYED_CORRECTLY,
    118             substrs = ['uint8_t', '\\x01'])
    119         self.expect("expr (more_bits.d)", VARIABLES_DISPLAYED_CORRECTLY,
    120             substrs = ['uint8_t', '\\0'])
    121 
    122         self.expect("target modules dump symfile a.out", VARIABLES_DISPLAYED_CORRECTLY,
    123             substrs = ['Bits', 'uint32_t b3 : 3',
    124                        'MoreBits', 'uint32_t a : 3'])
    125 
    126     def bitfields_variable_python(self):
    127         """Use Python APIs to inspect a bitfields variable."""
    128         exe = os.path.join(os.getcwd(), "a.out")
    129 
    130         target = self.dbg.CreateTarget(exe)
    131         self.assertTrue(target, VALID_TARGET)
    132 
    133         breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
    134         self.assertTrue(breakpoint, VALID_BREAKPOINT)
    135 
    136         process = target.LaunchSimple(None, None, os.getcwd())
    137         self.assertTrue(process, PROCESS_IS_VALID)
    138 
    139         # The stop reason of the thread should be breakpoint.
    140         thread = target.GetProcess().GetThreadAtIndex(0)
    141         if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
    142             from lldbutil import stop_reason_to_str
    143             self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
    144                       stop_reason_to_str(thread.GetStopReason()))
    145 
    146         # The breakpoint should have a hit count of 1.
    147         self.assertTrue(breakpoint.GetHitCount() == 1, BREAKPOINT_HIT_ONCE)
    148 
    149         # Lookup the "bits" variable which contains 8 bitfields.
    150         frame = thread.GetFrameAtIndex(0)
    151         bits = frame.FindVariable("bits")
    152         self.DebugSBValue(bits)
    153         self.assertTrue(bits.GetTypeName() == 'Bits', "bits.GetTypeName() == 'Bits'");
    154         self.assertTrue(bits.GetNumChildren() == 10, "bits.GetNumChildren() == 10");
    155         self.assertTrue(bits.GetByteSize() == 32, "bits.GetByteSize() == 32");
    156 
    157         # Notice the pattern of int(b1.GetValue(), 0).  We pass a base of 0
    158         # so that the proper radix is determined based on the contents of the
    159         # string.
    160         b1 = bits.GetChildMemberWithName("b1")
    161         self.DebugSBValue(b1)
    162         self.assertTrue(b1.GetName() == "b1" and
    163                         b1.GetTypeName() == "uint32_t:1" and
    164                         b1.IsInScope() and
    165                         int(b1.GetValue(), 0) == 1,
    166                         'bits.b1 has type uint32_t:1, is in scope, and == 1')
    167 
    168         b7 = bits.GetChildMemberWithName("b7")
    169         self.DebugSBValue(b7)
    170         self.assertTrue(b7.GetName() == "b7" and
    171                         b7.GetTypeName() == "uint32_t:7" and
    172                         b7.IsInScope() and
    173                         int(b7.GetValue(), 0) == 127,
    174                         'bits.b7 has type uint32_t:7, is in scope, and == 127')
    175 
    176         four = bits.GetChildMemberWithName("four")
    177         self.DebugSBValue(four)
    178         self.assertTrue(four.GetName() == "four" and
    179                         four.GetTypeName() == "uint32_t:4" and
    180                         four.IsInScope() and
    181                         int(four.GetValue(), 0) == 15,
    182                         'bits.four has type uint32_t:4, is in scope, and == 15')
    183 
    184         # Now kill the process, and we are done.
    185         rc = target.GetProcess().Kill()
    186         self.assertTrue(rc.Success())
    187 
    188 
    189 if __name__ == '__main__':
    190     import atexit
    191     lldb.SBDebugger.Initialize()
    192     atexit.register(lambda: lldb.SBDebugger.Terminate())
    193     unittest2.main()
    194