Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GDB pretty-printer for tuples and pointers #42278

Merged
merged 8 commits into from
Jun 9, 2017
14 changes: 14 additions & 0 deletions src/etc/debugger_pretty_printers_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
TYPE_KIND_PTR = 15
TYPE_KIND_FIXED_SIZE_VEC = 16
TYPE_KIND_REGULAR_UNION = 17
TYPE_KIND_OS_STRING = 18

ENCODED_ENUM_PREFIX = "RUST$ENCODED$ENUM$"
ENUM_DISR_FIELD_NAME = "RUST$ENUM$DISR"
Expand All @@ -64,6 +65,9 @@
# std::String related constants
STD_STRING_FIELD_NAMES = ["vec"]

# std::ffi::OsString related constants
OS_STRING_FIELD_NAMES = ["inner"]


class Type(object):
"""
Expand Down Expand Up @@ -162,6 +166,11 @@ def __classify_struct(self):
self.__conforms_to_field_layout(STD_STRING_FIELD_NAMES)):
return TYPE_KIND_STD_STRING

# OS STRING
if (unqualified_type_name == "OsString" and
self.__conforms_to_field_layout(OS_STRING_FIELD_NAMES)):
return TYPE_KIND_OS_STRING

# ENUM VARIANTS
if fields[0].name == ENUM_DISR_FIELD_NAME:
if field_count == 1:
Expand Down Expand Up @@ -345,3 +354,8 @@ def extract_type_name(qualified_type_name):
return qualified_type_name
else:
return qualified_type_name[index + 2:]

try:
compat_str = unicode # Python 2
except NameError:
compat_str = str
39 changes: 34 additions & 5 deletions src/etc/gdb_rust_pretty_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def get_child_at_index(self, index):

def as_integer(self):
if self.gdb_val.type.code == gdb.TYPE_CODE_PTR:
return int(str(self.gdb_val), 0)
as_str = rustpp.compat_str(self.gdb_val).split()[0]
return int(as_str, 0)
return int(self.gdb_val)

def get_wrapped_value(self):
Expand All @@ -99,8 +100,10 @@ def rust_pretty_printer_lookup_function(gdb_val):
val = GdbValue(gdb_val)
type_kind = val.type.get_type_kind()

if (type_kind == rustpp.TYPE_KIND_REGULAR_STRUCT or
type_kind == rustpp.TYPE_KIND_EMPTY):
if type_kind == rustpp.TYPE_KIND_EMPTY:
return RustEmptyPrinter(val)

if type_kind == rustpp.TYPE_KIND_REGULAR_STRUCT:
return RustStructPrinter(val,
omit_first_field = False,
omit_type_name = False,
Expand All @@ -124,6 +127,9 @@ def rust_pretty_printer_lookup_function(gdb_val):
if type_kind == rustpp.TYPE_KIND_STD_STRING:
return RustStdStringPrinter(val)

if type_kind == rustpp.TYPE_KIND_OS_STRING:
return RustOsStringPrinter(val)

if type_kind == rustpp.TYPE_KIND_TUPLE:
return RustStructPrinter(val,
omit_first_field = False,
Expand Down Expand Up @@ -170,6 +176,14 @@ def rust_pretty_printer_lookup_function(gdb_val):
#=------------------------------------------------------------------------------
# Pretty Printer Classes
#=------------------------------------------------------------------------------
class RustEmptyPrinter(object):
def __init__(self, val):
self.__val = val

def to_string(self):
return self.__val.type.get_unqualified_type_name()


class RustStructPrinter(object):
def __init__(self, val, omit_first_field, omit_type_name, is_tuple_like):
self.__val = val
Expand All @@ -186,10 +200,10 @@ def children(self):
cs = []
wrapped_value = self.__val.get_wrapped_value()

for field in self.__val.type.get_fields():
for number, field in enumerate(self.__val.type.get_fields()):
field_value = wrapped_value[field.name]
if self.__is_tuple_like:
cs.append(("", field_value))
cs.append((str(number), field_value))
else:
cs.append((field.name, field_value))

Expand Down Expand Up @@ -268,6 +282,21 @@ def to_string(self):
length=length)


class RustOsStringPrinter(object):
def __init__(self, val):
self.__val = val

def to_string(self):
buf = self.__val.get_child_at_index(0)
vec = buf.get_child_at_index(0)
if vec.type.get_unqualified_type_name() == "Wtf8Buf":
vec = vec.get_child_at_index(0)

(length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(
vec)
return '"%s"' % data_ptr.get_wrapped_value().string(length=length)


class RustCStyleVariantPrinter(object):
def __init__(self, val):
assert val.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_ENUM
Expand Down
13 changes: 13 additions & 0 deletions src/test/debuginfo/pretty-std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
// gdbg-check:$6 = None
// gdbr-check:$6 = core::option::Option::None

// gdb-command: print os_string
// gdb-check:$7 = "IAMA OS string 😃"

// gdb-command: print some_string
// gdb-check:$8 = Some = {"IAMA optional string!"}


// === LLDB TESTS ==================================================================================

Expand All @@ -63,6 +69,8 @@


#![allow(unused_variables)]
use std::ffi::OsString;


fn main() {

Expand All @@ -78,10 +86,15 @@ fn main() {
// String
let string = "IAMA string!".to_string();

// OsString
let os_string = OsString::from("IAMA OS string \u{1F603}");

// Option
let some = Some(8i16);
let none: Option<i64> = None;

let some_string = Some("IAMA optional string!".to_owned());

zzz(); // #break
}

Expand Down