Skip to content

Commit

Permalink
debuginfo: Add GDB pretty printers for structs and enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Aug 27, 2014
1 parent ee25b6b commit 6974b4f
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/compiletest/runtest.rs
Expand Up @@ -458,7 +458,8 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
}

_=> {
let rust_src_root = find_rust_src_root(config).expect("Could not find Rust source root");
let rust_src_root = find_rust_src_root(config)
.expect("Could not find Rust source root");
let rust_pp_module_rel_path = Path::new("./src/etc");
let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
.as_str()
Expand Down
225 changes: 225 additions & 0 deletions src/etc/gdb_rust_pretty_printing.py
@@ -0,0 +1,225 @@
# Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

import gdb

#===============================================================================
# GDB Pretty Printing Module for Rust
#===============================================================================

def register_printers(objfile):
"Registers Rust pretty printers for the given objfile"
objfile.pretty_printers.append(rust_pretty_printer_lookup_function)

def rust_pretty_printer_lookup_function(val):
"Returns the correct Rust pretty printer for the given value if there is one"
type_code = val.type.code

if type_code == gdb.TYPE_CODE_STRUCT:
struct_kind = classify_struct(val.type)

if struct_kind == STRUCT_KIND_STR_SLICE:
return RustStringSlicePrinter(val)

if struct_kind == STRUCT_KIND_TUPLE:
return RustTuplePrinter(val)

if struct_kind == STRUCT_KIND_TUPLE_STRUCT:
return RustTupleStructPrinter(val, False)

if struct_kind == STRUCT_KIND_CSTYLE_VARIANT:
return RustCStyleEnumPrinter(val[get_field_at_index(val, 0)])

if struct_kind == STRUCT_KIND_TUPLE_VARIANT:
return RustTupleStructPrinter(val, True)

if struct_kind == STRUCT_KIND_STRUCT_VARIANT:
return RustStructPrinter(val, True)

return RustStructPrinter(val, False)

# Enum handling
if type_code == gdb.TYPE_CODE_UNION:
enum_members = list(val.type.fields())
enum_member_count = len(enum_members)

if enum_member_count == 0:
return RustStructPrinter(val, false)

if enum_member_count == 1:
if enum_members[0].name == None:
# This is a singleton enum
return rust_pretty_printer_lookup_function(val[enum_members[0]])
else:
assert enum_members[0].name.startswith("RUST$ENCODED$ENUM$")
# This is a space-optimized enum
last_separator_index = enum_members[0].name.rfind("$")
second_last_separator_index = first_variant_name.rfind("$", 0, last_separator_index)
disr_field_index = first_variant_name[second_last_separator_index + 1 :
last_separator_index]
disr_field_index = int(disr_field_index)

sole_variant_val = val[enum_members[0]]
disr_field = get_field_at_index(sole_variant_val, disr_field_index)
discriminant = int(sole_variant_val[disr_field])

if discriminant == 0:
null_variant_name = first_variant_name[last_separator_index + 1:]
return IdentityPrinter(null_variant_name)

return rust_pretty_printer_lookup_function(sole_variant_val)

# This is a regular enum, extract the discriminant
discriminant_name, discriminant_val = extract_discriminant_value(val)
return rust_pretty_printer_lookup_function(val[enum_members[discriminant_val]])



return None

#=------------------------------------------------------------------------------
# Pretty Printer Classes
#=------------------------------------------------------------------------------

class RustStructPrinter:
def __init__(self, val, hide_first_field):
self.val = val
self.hide_first_field = hide_first_field

def to_string(self):
return self.val.type.tag

def children(self):
cs = []
for field in self.val.type.fields():
field_name = field.name;
if field_name == None:
field_name = ""
name_value_tuple = ( field_name, self.val[field] )
cs.append( name_value_tuple )

if self.hide_first_field:
cs = cs[1:]

return cs

class RustTuplePrinter:
def __init__(self, val):
self.val = val

def to_string(self):
return None

def children(self):
cs = []
for field in self.val.type.fields():
cs.append( ("", self.val[field]) )

return cs

def display_hint(self):
return "array"

class RustTupleStructPrinter:
def __init__(self, val, hide_first_field):
self.val = val
self.hide_first_field = hide_first_field

def to_string(self):
return self.val.type.tag

def children(self):
cs = []
for field in self.val.type.fields():
cs.append( ("", self.val[field]) )

if self.hide_first_field:
cs = cs[1:]

return cs

def display_hint(self):
return "array"

class RustStringSlicePrinter:
def __init__(self, val):
self.val = val

def to_string(self):
slice_byte_len = self.val["length"]
return '"%s"' % self.val["data_ptr"].string(encoding = "utf-8",
length = slice_byte_len)

class RustCStyleEnumPrinter:
def __init__(self, val):
assert val.type.code == gdb.TYPE_CODE_ENUM
self.val = val

def to_string(self):
return str(self.val)

class IdentityPrinter:
def __init__(self, string):
self.string

def to_string(self):
return self.string

STRUCT_KIND_REGULAR_STRUCT = 0
STRUCT_KIND_TUPLE_STRUCT = 1
STRUCT_KIND_TUPLE = 2
STRUCT_KIND_TUPLE_VARIANT = 3
STRUCT_KIND_STRUCT_VARIANT = 4
STRUCT_KIND_CSTYLE_VARIANT = 5
STRUCT_KIND_STR_SLICE = 6

def classify_struct(type):
if type.tag == "&str":
return STRUCT_KIND_STR_SLICE

fields = list(type.fields())
field_count = len(fields)

if field_count == 0:
return STRUCT_KIND_REGULAR_STRUCT

if fields[0].artificial:
if field_count == 1:
return STRUCT_KIND_CSTYLE_VARIANT
elif fields[1].name == None:
return STRUCT_KIND_TUPLE_VARIANT
else:
return STRUCT_KIND_STRUCT_VARIANT

if fields[0].name == None:
if type.tag.startswith("("):
return STRUCT_KIND_TUPLE
else:
return STRUCT_KIND_TUPLE_STRUCT

return STRUCT_KIND_REGULAR_STRUCT

def extract_discriminant_value(enum_val):
assert enum_val.type.code == gdb.TYPE_CODE_UNION
for variant_descriptor in enum_val.type.fields():
variant_val = enum_val[variant_descriptor]
for field in variant_val.type.fields():
return (field.name, int(variant_val[field]))

def first_field(val):
for field in val.type.fields():
return field

def get_field_at_index(val, index):
i = 0
for field in val.type.fields():
if i == index:
return field
return None
25 changes: 24 additions & 1 deletion src/librustc/middle/trans/debuginfo.rs
Expand Up @@ -235,6 +235,9 @@ static UNKNOWN_COLUMN_NUMBER: c_uint = 0;
static UNKNOWN_FILE_METADATA: DIFile = (0 as DIFile);
static UNKNOWN_SCOPE_METADATA: DIScope = (0 as DIScope);

static FLAGS_NONE: c_uint = 0;
static FLAGS_ARTIFICAL: c_uint = llvm::debuginfo::FlagArtificial as c_uint;

//=-----------------------------------------------------------------------------
// Public Interface of debuginfo module
//=-----------------------------------------------------------------------------
Expand Down Expand Up @@ -1733,6 +1736,7 @@ struct MemberDescription {
llvm_type: Type,
type_metadata: DIType,
offset: MemberOffset,
flags: c_uint
}

// A factory for MemberDescriptions. It produces a list of member descriptions
Expand Down Expand Up @@ -1891,6 +1895,7 @@ impl StructMemberDescriptionFactory {
llvm_type: type_of::type_of(cx, field.mt.ty),
type_metadata: type_metadata(cx, field.mt.ty, self.span),
offset: offset,
flags: FLAGS_NONE,
}
}).collect()
}
Expand Down Expand Up @@ -1951,6 +1956,7 @@ impl TupleMemberDescriptionFactory {
llvm_type: type_of::type_of(cx, component_type),
type_metadata: type_metadata(cx, component_type, self.span),
offset: ComputedMemberOffset,
flags: FLAGS_NONE,
}
}).collect()
}
Expand Down Expand Up @@ -2036,6 +2042,7 @@ impl EnumMemberDescriptionFactory {
llvm_type: variant_llvm_type,
type_metadata: variant_type_metadata,
offset: FixedMemberOffset { bytes: 0 },
flags: FLAGS_NONE
}
}).collect()
},
Expand Down Expand Up @@ -2069,6 +2076,7 @@ impl EnumMemberDescriptionFactory {
llvm_type: variant_llvm_type,
type_metadata: variant_type_metadata,
offset: FixedMemberOffset { bytes: 0 },
flags: FLAGS_NONE
}
]
}
Expand Down Expand Up @@ -2102,6 +2110,7 @@ impl EnumMemberDescriptionFactory {
llvm_type: non_null_llvm_type,
type_metadata: non_null_type_metadata,
offset: FixedMemberOffset { bytes: 0 },
flags: FLAGS_NONE
};

let unique_type_id = debug_context(cx).type_map
Expand Down Expand Up @@ -2139,6 +2148,7 @@ impl EnumMemberDescriptionFactory {
llvm_type: artificial_struct_llvm_type,
type_metadata: artificial_struct_metadata,
offset: FixedMemberOffset { bytes: 0 },
flags: FLAGS_NONE
}
]
},
Expand Down Expand Up @@ -2183,6 +2193,7 @@ impl EnumMemberDescriptionFactory {
llvm_type: variant_llvm_type,
type_metadata: variant_type_metadata,
offset: FixedMemberOffset { bytes: 0 },
flags: FLAGS_NONE
}
]
},
Expand All @@ -2209,6 +2220,11 @@ impl VariantMemberDescriptionFactory {
_ => type_metadata(cx, ty, self.span)
},
offset: ComputedMemberOffset,
flags: if self.discriminant_type_metadata.is_some() && i == 0 {
FLAGS_ARTIFICAL
} else {
FLAGS_NONE
}
}
}).collect()
}
Expand Down Expand Up @@ -2524,7 +2540,7 @@ fn set_members_of_composite_type(cx: &CrateContext,
bytes_to_bits(member_size),
bytes_to_bits(member_align),
bytes_to_bits(member_offset),
0,
member_description.flags,
member_description.type_metadata)
}
})
Expand Down Expand Up @@ -2611,30 +2627,35 @@ fn at_box_metadata(cx: &CrateContext,
llvm_type: *member_llvm_types.get(0),
type_metadata: type_metadata(cx, int_type, codemap::DUMMY_SP),
offset: ComputedMemberOffset,
flags: FLAGS_ARTIFICAL,
},
MemberDescription {
name: "drop_glue".to_string(),
llvm_type: *member_llvm_types.get(1),
type_metadata: nil_pointer_type_metadata,
offset: ComputedMemberOffset,
flags: FLAGS_ARTIFICAL,
},
MemberDescription {
name: "prev".to_string(),
llvm_type: *member_llvm_types.get(2),
type_metadata: nil_pointer_type_metadata,
offset: ComputedMemberOffset,
flags: FLAGS_ARTIFICAL,
},
MemberDescription {
name: "next".to_string(),
llvm_type: *member_llvm_types.get(3),
type_metadata: nil_pointer_type_metadata,
offset: ComputedMemberOffset,
flags: FLAGS_ARTIFICAL,
},
MemberDescription {
name: "val".to_string(),
llvm_type: *member_llvm_types.get(4),
type_metadata: content_type_metadata,
offset: ComputedMemberOffset,
flags: FLAGS_ARTIFICAL,
}
];

Expand Down Expand Up @@ -2735,12 +2756,14 @@ fn vec_slice_metadata(cx: &CrateContext,
llvm_type: *member_llvm_types.get(0),
type_metadata: element_type_metadata,
offset: ComputedMemberOffset,
flags: FLAGS_ARTIFICAL
},
MemberDescription {
name: "length".to_string(),
llvm_type: *member_llvm_types.get(1),
type_metadata: type_metadata(cx, ty::mk_uint(), span),
offset: ComputedMemberOffset,
flags: FLAGS_ARTIFICAL
},
];

Expand Down
5 changes: 4 additions & 1 deletion src/librustc_llvm/lib.rs
Expand Up @@ -428,7 +428,10 @@ pub mod debuginfo {
FlagObjcClassComplete = 1 << 9,
FlagObjectPointer = 1 << 10,
FlagVector = 1 << 11,
FlagStaticMember = 1 << 12
FlagStaticMember = 1 << 12,
FlagIndirectVariable = 1 << 13,
FlagLValueReference = 1 << 14,
FlagRValueReference = 1 << 15
}
}

Expand Down

0 comments on commit 6974b4f

Please sign in to comment.