Skip to content

Commit b5cf892

Browse files
shafikzmodem
authored andcommitted
[LLDB] Fix the handling of unnamed bit-fields when parsing DWARF
We ran into an assert when debugging clang and performing an expression on a class derived from DeclContext. The assert was indicating we were getting the offsets wrong for RecordDeclBitfields. We were getting both the size and offset of unnamed bit-field members wrong. We could fix this case with a quick change but as I extended the test suite to include more combinations we kept finding more cases that were being handled incorrectly. A fix that handled all the new cases as well as the cases already covered required a refactor of the existing technique. Differential Revision: https://reviews.llvm.org/D72953 (cherry picked from commit fcaf5f6)
1 parent 2c9cb89 commit b5cf892

File tree

5 files changed

+255
-135
lines changed

5 files changed

+255
-135
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""Show bitfields and check that they display correctly."""
2+
3+
import lldb
4+
from lldbsuite.test.decorators import *
5+
from lldbsuite.test.lldbtest import *
6+
from lldbsuite.test import lldbutil
7+
8+
9+
class CppBitfieldsTestCase(TestBase):
10+
11+
mydir = TestBase.compute_mydir(__file__)
12+
13+
def setUp(self):
14+
# Call super's setUp().
15+
TestBase.setUp(self)
16+
# Find the line number to break inside main().
17+
self.line = line_number('main.cpp', '// Set break point at this line.')
18+
19+
# BitFields exhibit crashes in record layout on Windows
20+
# (http://llvm.org/pr21800)
21+
@skipIfWindows
22+
def test_and_run_command(self):
23+
"""Test 'frame variable ...' on a variable with bitfields."""
24+
self.build()
25+
26+
lldbutil.run_to_source_breakpoint(self, '// Set break point at this line.',
27+
lldb.SBFileSpec("main.cpp", False))
28+
29+
# The stop reason of the thread should be breakpoint.
30+
self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
31+
substrs=['stopped',
32+
'stop reason = breakpoint'])
33+
34+
# The breakpoint should have a hit count of 1.
35+
self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
36+
substrs=[' resolved, hit count = 1'])
37+
38+
self.expect("expr (lba.a)", VARIABLES_DISPLAYED_CORRECTLY,
39+
substrs=['unsigned int', '2'])
40+
self.expect("expr (lbb.b)", VARIABLES_DISPLAYED_CORRECTLY,
41+
substrs=['unsigned int', '3'])
42+
self.expect("expr (lbc.c)", VARIABLES_DISPLAYED_CORRECTLY,
43+
substrs=['unsigned int', '4'])
44+
self.expect("expr (lbd.a)", VARIABLES_DISPLAYED_CORRECTLY,
45+
substrs=['unsigned int', '5'])
46+
self.expect("expr (clang_example.f.a)", VARIABLES_DISPLAYED_CORRECTLY,
47+
substrs=['uint64_t', '1'])
48+
49+
self.expect(
50+
"frame variable --show-types lba",
51+
VARIABLES_DISPLAYED_CORRECTLY,
52+
substrs=[
53+
'(int:32) = ',
54+
'(unsigned int:20) a = 2',
55+
])
56+
57+
self.expect(
58+
"frame variable --show-types lbb",
59+
VARIABLES_DISPLAYED_CORRECTLY,
60+
substrs=[
61+
'(unsigned int:1) a = 1',
62+
'(int:31) =',
63+
'(unsigned int:20) b = 3',
64+
])
65+
66+
self.expect(
67+
"frame variable --show-types lbc",
68+
VARIABLES_DISPLAYED_CORRECTLY,
69+
substrs=[
70+
'(int:22) =',
71+
'(unsigned int:1) a = 1',
72+
'(unsigned int:1) b = 0',
73+
'(unsigned int:5) c = 4',
74+
'(unsigned int:1) d = 1',
75+
'(int:2) =',
76+
'(unsigned int:20) e = 20',
77+
])
78+
79+
self.expect(
80+
"frame variable --show-types lbd",
81+
VARIABLES_DISPLAYED_CORRECTLY,
82+
substrs=[
83+
'(char [3]) arr = "abc"',
84+
'(int:32) =',
85+
'(unsigned int:20) a = 5',
86+
])
87+
88+
self.expect(
89+
"frame variable --show-types clang_example",
90+
VARIABLES_DISPLAYED_CORRECTLY,
91+
substrs=[
92+
'(int:22) =',
93+
'(uint64_t:1) a = 1',
94+
'(uint64_t:1) b = 0',
95+
'(uint64_t:1) c = 1',
96+
'(uint64_t:1) d = 0',
97+
'(uint64_t:1) e = 1',
98+
'(uint64_t:1) f = 0',
99+
'(uint64_t:1) g = 1',
100+
'(uint64_t:1) h = 0',
101+
'(uint64_t:1) i = 1',
102+
'(uint64_t:1) j = 0',
103+
'(uint64_t:1) k = 1',
104+
])
105+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <stdint.h>
2+
3+
int main(int argc, char const *argv[]) {
4+
struct LargeBitsA {
5+
unsigned int : 30, a : 20;
6+
} lba;
7+
8+
struct LargeBitsB {
9+
unsigned int a : 1, : 11, : 12, b : 20;
10+
} lbb;
11+
12+
struct LargeBitsC {
13+
unsigned int : 13, : 9, a : 1, b : 1, c : 5, d : 1, e : 20;
14+
} lbc;
15+
16+
struct LargeBitsD {
17+
char arr[3];
18+
unsigned int : 30, a : 20;
19+
} lbd;
20+
21+
// This case came up when debugging clang and models RecordDeclBits
22+
struct BitExampleFromClangDeclContext {
23+
class fields {
24+
uint64_t : 13;
25+
uint64_t : 9;
26+
27+
uint64_t a: 1;
28+
uint64_t b: 1;
29+
uint64_t c: 1;
30+
uint64_t d: 1;
31+
uint64_t e: 1;
32+
uint64_t f: 1;
33+
uint64_t g: 1;
34+
uint64_t h: 1;
35+
uint64_t i: 1;
36+
uint64_t j: 1;
37+
uint64_t k: 1;
38+
39+
// In order to reproduce the crash for this case we need the
40+
// members of fields to stay private :-(
41+
friend struct BitExampleFromClangDeclContext;
42+
};
43+
44+
union {
45+
struct fields f;
46+
};
47+
48+
BitExampleFromClangDeclContext() {
49+
f.a = 1;
50+
f.b = 0;
51+
f.c = 1;
52+
f.d = 0;
53+
f.e = 1;
54+
f.f = 0;
55+
f.g = 1;
56+
f.h = 0;
57+
f.i = 1;
58+
f.j = 0;
59+
f.k = 1;
60+
}
61+
} clang_example;
62+
63+
lba.a = 2;
64+
65+
lbb.a = 1;
66+
lbb.b = 3;
67+
68+
lbc.a = 1;
69+
lbc.b = 0;
70+
lbc.c = 4;
71+
lbc.d = 1;
72+
lbc.e = 20;
73+
74+
lbd.arr[0] = 'a';
75+
lbd.arr[1] = 'b';
76+
lbd.arr[2] = 'c';
77+
lbd.a = 5;
78+
79+
80+
return 0; // Set break point at this line.
81+
}

0 commit comments

Comments
 (0)