Skip to content

Commit 6974b4f

Browse files
debuginfo: Add GDB pretty printers for structs and enums.
1 parent ee25b6b commit 6974b4f

File tree

5 files changed

+422
-3
lines changed

5 files changed

+422
-3
lines changed

src/compiletest/runtest.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,8 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
458458
}
459459

460460
_=> {
461-
let rust_src_root = find_rust_src_root(config).expect("Could not find Rust source root");
461+
let rust_src_root = find_rust_src_root(config)
462+
.expect("Could not find Rust source root");
462463
let rust_pp_module_rel_path = Path::new("./src/etc");
463464
let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
464465
.as_str()

src/etc/gdb_rust_pretty_printing.py

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
# Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2+
# file at the top-level directory of this distribution and at
3+
# http://rust-lang.org/COPYRIGHT.
4+
#
5+
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
# option. This file may not be copied, modified, or distributed
9+
# except according to those terms.
10+
11+
import gdb
12+
13+
#===============================================================================
14+
# GDB Pretty Printing Module for Rust
15+
#===============================================================================
16+
17+
def register_printers(objfile):
18+
"Registers Rust pretty printers for the given objfile"
19+
objfile.pretty_printers.append(rust_pretty_printer_lookup_function)
20+
21+
def rust_pretty_printer_lookup_function(val):
22+
"Returns the correct Rust pretty printer for the given value if there is one"
23+
type_code = val.type.code
24+
25+
if type_code == gdb.TYPE_CODE_STRUCT:
26+
struct_kind = classify_struct(val.type)
27+
28+
if struct_kind == STRUCT_KIND_STR_SLICE:
29+
return RustStringSlicePrinter(val)
30+
31+
if struct_kind == STRUCT_KIND_TUPLE:
32+
return RustTuplePrinter(val)
33+
34+
if struct_kind == STRUCT_KIND_TUPLE_STRUCT:
35+
return RustTupleStructPrinter(val, False)
36+
37+
if struct_kind == STRUCT_KIND_CSTYLE_VARIANT:
38+
return RustCStyleEnumPrinter(val[get_field_at_index(val, 0)])
39+
40+
if struct_kind == STRUCT_KIND_TUPLE_VARIANT:
41+
return RustTupleStructPrinter(val, True)
42+
43+
if struct_kind == STRUCT_KIND_STRUCT_VARIANT:
44+
return RustStructPrinter(val, True)
45+
46+
return RustStructPrinter(val, False)
47+
48+
# Enum handling
49+
if type_code == gdb.TYPE_CODE_UNION:
50+
enum_members = list(val.type.fields())
51+
enum_member_count = len(enum_members)
52+
53+
if enum_member_count == 0:
54+
return RustStructPrinter(val, false)
55+
56+
if enum_member_count == 1:
57+
if enum_members[0].name == None:
58+
# This is a singleton enum
59+
return rust_pretty_printer_lookup_function(val[enum_members[0]])
60+
else:
61+
assert enum_members[0].name.startswith("RUST$ENCODED$ENUM$")
62+
# This is a space-optimized enum
63+
last_separator_index = enum_members[0].name.rfind("$")
64+
second_last_separator_index = first_variant_name.rfind("$", 0, last_separator_index)
65+
disr_field_index = first_variant_name[second_last_separator_index + 1 :
66+
last_separator_index]
67+
disr_field_index = int(disr_field_index)
68+
69+
sole_variant_val = val[enum_members[0]]
70+
disr_field = get_field_at_index(sole_variant_val, disr_field_index)
71+
discriminant = int(sole_variant_val[disr_field])
72+
73+
if discriminant == 0:
74+
null_variant_name = first_variant_name[last_separator_index + 1:]
75+
return IdentityPrinter(null_variant_name)
76+
77+
return rust_pretty_printer_lookup_function(sole_variant_val)
78+
79+
# This is a regular enum, extract the discriminant
80+
discriminant_name, discriminant_val = extract_discriminant_value(val)
81+
return rust_pretty_printer_lookup_function(val[enum_members[discriminant_val]])
82+
83+
84+
85+
return None
86+
87+
#=------------------------------------------------------------------------------
88+
# Pretty Printer Classes
89+
#=------------------------------------------------------------------------------
90+
91+
class RustStructPrinter:
92+
def __init__(self, val, hide_first_field):
93+
self.val = val
94+
self.hide_first_field = hide_first_field
95+
96+
def to_string(self):
97+
return self.val.type.tag
98+
99+
def children(self):
100+
cs = []
101+
for field in self.val.type.fields():
102+
field_name = field.name;
103+
if field_name == None:
104+
field_name = ""
105+
name_value_tuple = ( field_name, self.val[field] )
106+
cs.append( name_value_tuple )
107+
108+
if self.hide_first_field:
109+
cs = cs[1:]
110+
111+
return cs
112+
113+
class RustTuplePrinter:
114+
def __init__(self, val):
115+
self.val = val
116+
117+
def to_string(self):
118+
return None
119+
120+
def children(self):
121+
cs = []
122+
for field in self.val.type.fields():
123+
cs.append( ("", self.val[field]) )
124+
125+
return cs
126+
127+
def display_hint(self):
128+
return "array"
129+
130+
class RustTupleStructPrinter:
131+
def __init__(self, val, hide_first_field):
132+
self.val = val
133+
self.hide_first_field = hide_first_field
134+
135+
def to_string(self):
136+
return self.val.type.tag
137+
138+
def children(self):
139+
cs = []
140+
for field in self.val.type.fields():
141+
cs.append( ("", self.val[field]) )
142+
143+
if self.hide_first_field:
144+
cs = cs[1:]
145+
146+
return cs
147+
148+
def display_hint(self):
149+
return "array"
150+
151+
class RustStringSlicePrinter:
152+
def __init__(self, val):
153+
self.val = val
154+
155+
def to_string(self):
156+
slice_byte_len = self.val["length"]
157+
return '"%s"' % self.val["data_ptr"].string(encoding = "utf-8",
158+
length = slice_byte_len)
159+
160+
class RustCStyleEnumPrinter:
161+
def __init__(self, val):
162+
assert val.type.code == gdb.TYPE_CODE_ENUM
163+
self.val = val
164+
165+
def to_string(self):
166+
return str(self.val)
167+
168+
class IdentityPrinter:
169+
def __init__(self, string):
170+
self.string
171+
172+
def to_string(self):
173+
return self.string
174+
175+
STRUCT_KIND_REGULAR_STRUCT = 0
176+
STRUCT_KIND_TUPLE_STRUCT = 1
177+
STRUCT_KIND_TUPLE = 2
178+
STRUCT_KIND_TUPLE_VARIANT = 3
179+
STRUCT_KIND_STRUCT_VARIANT = 4
180+
STRUCT_KIND_CSTYLE_VARIANT = 5
181+
STRUCT_KIND_STR_SLICE = 6
182+
183+
def classify_struct(type):
184+
if type.tag == "&str":
185+
return STRUCT_KIND_STR_SLICE
186+
187+
fields = list(type.fields())
188+
field_count = len(fields)
189+
190+
if field_count == 0:
191+
return STRUCT_KIND_REGULAR_STRUCT
192+
193+
if fields[0].artificial:
194+
if field_count == 1:
195+
return STRUCT_KIND_CSTYLE_VARIANT
196+
elif fields[1].name == None:
197+
return STRUCT_KIND_TUPLE_VARIANT
198+
else:
199+
return STRUCT_KIND_STRUCT_VARIANT
200+
201+
if fields[0].name == None:
202+
if type.tag.startswith("("):
203+
return STRUCT_KIND_TUPLE
204+
else:
205+
return STRUCT_KIND_TUPLE_STRUCT
206+
207+
return STRUCT_KIND_REGULAR_STRUCT
208+
209+
def extract_discriminant_value(enum_val):
210+
assert enum_val.type.code == gdb.TYPE_CODE_UNION
211+
for variant_descriptor in enum_val.type.fields():
212+
variant_val = enum_val[variant_descriptor]
213+
for field in variant_val.type.fields():
214+
return (field.name, int(variant_val[field]))
215+
216+
def first_field(val):
217+
for field in val.type.fields():
218+
return field
219+
220+
def get_field_at_index(val, index):
221+
i = 0
222+
for field in val.type.fields():
223+
if i == index:
224+
return field
225+
return None

src/librustc/middle/trans/debuginfo.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ static UNKNOWN_COLUMN_NUMBER: c_uint = 0;
235235
static UNKNOWN_FILE_METADATA: DIFile = (0 as DIFile);
236236
static UNKNOWN_SCOPE_METADATA: DIScope = (0 as DIScope);
237237

238+
static FLAGS_NONE: c_uint = 0;
239+
static FLAGS_ARTIFICAL: c_uint = llvm::debuginfo::FlagArtificial as c_uint;
240+
238241
//=-----------------------------------------------------------------------------
239242
// Public Interface of debuginfo module
240243
//=-----------------------------------------------------------------------------
@@ -1733,6 +1736,7 @@ struct MemberDescription {
17331736
llvm_type: Type,
17341737
type_metadata: DIType,
17351738
offset: MemberOffset,
1739+
flags: c_uint
17361740
}
17371741

17381742
// A factory for MemberDescriptions. It produces a list of member descriptions
@@ -1891,6 +1895,7 @@ impl StructMemberDescriptionFactory {
18911895
llvm_type: type_of::type_of(cx, field.mt.ty),
18921896
type_metadata: type_metadata(cx, field.mt.ty, self.span),
18931897
offset: offset,
1898+
flags: FLAGS_NONE,
18941899
}
18951900
}).collect()
18961901
}
@@ -1951,6 +1956,7 @@ impl TupleMemberDescriptionFactory {
19511956
llvm_type: type_of::type_of(cx, component_type),
19521957
type_metadata: type_metadata(cx, component_type, self.span),
19531958
offset: ComputedMemberOffset,
1959+
flags: FLAGS_NONE,
19541960
}
19551961
}).collect()
19561962
}
@@ -2036,6 +2042,7 @@ impl EnumMemberDescriptionFactory {
20362042
llvm_type: variant_llvm_type,
20372043
type_metadata: variant_type_metadata,
20382044
offset: FixedMemberOffset { bytes: 0 },
2045+
flags: FLAGS_NONE
20392046
}
20402047
}).collect()
20412048
},
@@ -2069,6 +2076,7 @@ impl EnumMemberDescriptionFactory {
20692076
llvm_type: variant_llvm_type,
20702077
type_metadata: variant_type_metadata,
20712078
offset: FixedMemberOffset { bytes: 0 },
2079+
flags: FLAGS_NONE
20722080
}
20732081
]
20742082
}
@@ -2102,6 +2110,7 @@ impl EnumMemberDescriptionFactory {
21022110
llvm_type: non_null_llvm_type,
21032111
type_metadata: non_null_type_metadata,
21042112
offset: FixedMemberOffset { bytes: 0 },
2113+
flags: FLAGS_NONE
21052114
};
21062115

21072116
let unique_type_id = debug_context(cx).type_map
@@ -2139,6 +2148,7 @@ impl EnumMemberDescriptionFactory {
21392148
llvm_type: artificial_struct_llvm_type,
21402149
type_metadata: artificial_struct_metadata,
21412150
offset: FixedMemberOffset { bytes: 0 },
2151+
flags: FLAGS_NONE
21422152
}
21432153
]
21442154
},
@@ -2183,6 +2193,7 @@ impl EnumMemberDescriptionFactory {
21832193
llvm_type: variant_llvm_type,
21842194
type_metadata: variant_type_metadata,
21852195
offset: FixedMemberOffset { bytes: 0 },
2196+
flags: FLAGS_NONE
21862197
}
21872198
]
21882199
},
@@ -2209,6 +2220,11 @@ impl VariantMemberDescriptionFactory {
22092220
_ => type_metadata(cx, ty, self.span)
22102221
},
22112222
offset: ComputedMemberOffset,
2223+
flags: if self.discriminant_type_metadata.is_some() && i == 0 {
2224+
FLAGS_ARTIFICAL
2225+
} else {
2226+
FLAGS_NONE
2227+
}
22122228
}
22132229
}).collect()
22142230
}
@@ -2524,7 +2540,7 @@ fn set_members_of_composite_type(cx: &CrateContext,
25242540
bytes_to_bits(member_size),
25252541
bytes_to_bits(member_align),
25262542
bytes_to_bits(member_offset),
2527-
0,
2543+
member_description.flags,
25282544
member_description.type_metadata)
25292545
}
25302546
})
@@ -2611,30 +2627,35 @@ fn at_box_metadata(cx: &CrateContext,
26112627
llvm_type: *member_llvm_types.get(0),
26122628
type_metadata: type_metadata(cx, int_type, codemap::DUMMY_SP),
26132629
offset: ComputedMemberOffset,
2630+
flags: FLAGS_ARTIFICAL,
26142631
},
26152632
MemberDescription {
26162633
name: "drop_glue".to_string(),
26172634
llvm_type: *member_llvm_types.get(1),
26182635
type_metadata: nil_pointer_type_metadata,
26192636
offset: ComputedMemberOffset,
2637+
flags: FLAGS_ARTIFICAL,
26202638
},
26212639
MemberDescription {
26222640
name: "prev".to_string(),
26232641
llvm_type: *member_llvm_types.get(2),
26242642
type_metadata: nil_pointer_type_metadata,
26252643
offset: ComputedMemberOffset,
2644+
flags: FLAGS_ARTIFICAL,
26262645
},
26272646
MemberDescription {
26282647
name: "next".to_string(),
26292648
llvm_type: *member_llvm_types.get(3),
26302649
type_metadata: nil_pointer_type_metadata,
26312650
offset: ComputedMemberOffset,
2651+
flags: FLAGS_ARTIFICAL,
26322652
},
26332653
MemberDescription {
26342654
name: "val".to_string(),
26352655
llvm_type: *member_llvm_types.get(4),
26362656
type_metadata: content_type_metadata,
26372657
offset: ComputedMemberOffset,
2658+
flags: FLAGS_ARTIFICAL,
26382659
}
26392660
];
26402661

@@ -2735,12 +2756,14 @@ fn vec_slice_metadata(cx: &CrateContext,
27352756
llvm_type: *member_llvm_types.get(0),
27362757
type_metadata: element_type_metadata,
27372758
offset: ComputedMemberOffset,
2759+
flags: FLAGS_ARTIFICAL
27382760
},
27392761
MemberDescription {
27402762
name: "length".to_string(),
27412763
llvm_type: *member_llvm_types.get(1),
27422764
type_metadata: type_metadata(cx, ty::mk_uint(), span),
27432765
offset: ComputedMemberOffset,
2766+
flags: FLAGS_ARTIFICAL
27442767
},
27452768
];
27462769

src/librustc_llvm/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,10 @@ pub mod debuginfo {
428428
FlagObjcClassComplete = 1 << 9,
429429
FlagObjectPointer = 1 << 10,
430430
FlagVector = 1 << 11,
431-
FlagStaticMember = 1 << 12
431+
FlagStaticMember = 1 << 12,
432+
FlagIndirectVariable = 1 << 13,
433+
FlagLValueReference = 1 << 14,
434+
FlagRValueReference = 1 << 15
432435
}
433436
}
434437

0 commit comments

Comments
 (0)