From 0d0c08f9bb75002138ed0b69b0058099ec41ccd1 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 15 Aug 2018 01:38:00 +0900 Subject: [PATCH] pretty printing for btreemap --- src/etc/debugger_pretty_printers_common.py | 21 ++++++++++++++ src/etc/gdb_rust_pretty_printing.py | 30 ++++++++++++++++++++ src/test/debuginfo/pretty-std-collections.rs | 12 +++++++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/etc/debugger_pretty_printers_common.py b/src/etc/debugger_pretty_printers_common.py index e64d863717d..1797f6708ac 100644 --- a/src/etc/debugger_pretty_printers_common.py +++ b/src/etc/debugger_pretty_printers_common.py @@ -49,6 +49,7 @@ TYPE_KIND_REGULAR_UNION = 17 TYPE_KIND_OS_STRING = 18 TYPE_KIND_STD_VECDEQUE = 19 TYPE_KIND_STD_BTREESET = 20 +TYPE_KIND_STD_BTREEMAP = 21 ENCODED_ENUM_PREFIX = "RUST$ENCODED$ENUM$" ENUM_DISR_FIELD_NAME = "RUST$ENUM$DISR" @@ -75,6 +76,9 @@ STD_VECDEQUE_FIELD_NAMES = [STD_VECDEQUE_FIELD_NAME_TAIL, # std::collections::BTreeSet<> related constants STD_BTREESET_FIELD_NAMES = ["map"] +# std::collections::BTreeMap<> related constants +STD_BTREEMAP_FIELD_NAMES = ["root", "length"] + # std::String related constants STD_STRING_FIELD_NAMES = ["vec"] @@ -184,6 +188,11 @@ class Type(object): self.__conforms_to_field_layout(STD_BTREESET_FIELD_NAMES)): return TYPE_KIND_STD_BTREESET + # STD COLLECTION BTREEMAP + if (unqualified_type_name.startswith("BTreeMap<") and + self.__conforms_to_field_layout(STD_BTREEMAP_FIELD_NAMES)): + return TYPE_KIND_STD_BTREEMAP + # STD STRING if (unqualified_type_name.startswith("String") and self.__conforms_to_field_layout(STD_STRING_FIELD_NAMES)): @@ -380,6 +389,18 @@ def extract_length_and_ptr_from_std_btreeset(vec_val): return (length, data_ptr) +def extract_length_and_ptr_from_std_btreemap(vec_val): + assert vec_val.type.get_type_kind() == TYPE_KIND_STD_BTREEMAP + root = vec_val.get_child_at_index(0) + length = vec_val.get_child_at_index(1).as_integer() + node = root.get_child_at_index(0) + ptr = node.get_child_at_index(0) + unique_ptr_val = ptr.get_child_at_index(0) + data_ptr = unique_ptr_val.get_child_at_index(0) + assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR + return (length, data_ptr) + + def extract_length_and_ptr_from_slice(slice_val): assert (slice_val.type.get_type_kind() == TYPE_KIND_SLICE or slice_val.type.get_type_kind() == TYPE_KIND_STR_SLICE) diff --git a/src/etc/gdb_rust_pretty_printing.py b/src/etc/gdb_rust_pretty_printing.py index fae1fd0cac3..216915dba5f 100755 --- a/src/etc/gdb_rust_pretty_printing.py +++ b/src/etc/gdb_rust_pretty_printing.py @@ -130,6 +130,9 @@ def rust_pretty_printer_lookup_function(gdb_val): if type_kind == rustpp.TYPE_KIND_STD_BTREESET: return RustStdBTreeSetPrinter(val) + if type_kind == rustpp.TYPE_KIND_STD_BTREEMAP: + return RustStdBTreeMapPrinter(val) + if type_kind == rustpp.TYPE_KIND_STD_STRING: return RustStdStringPrinter(val) @@ -325,6 +328,32 @@ class RustStdBTreeSetPrinter(object): yield (str(index), gdb_ptr[index]) +class RustStdBTreeMapPrinter(object): + def __init__(self, val): + self.__val = val + + @staticmethod + def display_hint(): + return "map" + + def to_string(self): + (length, data_ptr) = \ + rustpp.extract_length_and_ptr_from_std_btreemap(self.__val) + return (self.__val.type.get_unqualified_type_name() + + ("(len: %i)" % length)) + + def children(self): + (length, data_ptr) = \ + rustpp.extract_length_and_ptr_from_std_btreemap(self.__val) + keys = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3) + keys_ptr = keys.get_wrapped_value() + vals = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(4) + vals_ptr = vals.get_wrapped_value() + for index in xrange(length): + yield (str(index), keys_ptr[index]) + yield (str(index), vals_ptr[index]) + + class RustStdStringPrinter(object): def __init__(self, val): self.__val = val @@ -338,6 +367,7 @@ class RustStdStringPrinter(object): def display_hint(self): return "string" + class RustOsStringPrinter(object): def __init__(self, val): self.__val = val diff --git a/src/test/debuginfo/pretty-std-collections.rs b/src/test/debuginfo/pretty-std-collections.rs index 18d73bf5677..8e37a884b34 100644 --- a/src/test/debuginfo/pretty-std-collections.rs +++ b/src/test/debuginfo/pretty-std-collections.rs @@ -22,11 +22,15 @@ // gdb-command: print btree_set // gdb-check:$1 = BTreeSet(len: 3) = {3, 5, 7} +// gdb-command: print btree_map +// gdb-check:$2 = BTreeMap(len: 3) = {[3] = 3, [5] = 7, [7] = 4} + // gdb-command: print vec_deque -// gdb-check:$2 = VecDeque(len: 3, cap: 8) = {5, 3, 7} +// gdb-check:$3 = VecDeque(len: 3, cap: 8) = {5, 3, 7} #![allow(unused_variables)] use std::collections::BTreeSet; +use std::collections::BTreeMap; use std::collections::VecDeque; @@ -38,6 +42,12 @@ fn main() { btree_set.insert(3); btree_set.insert(7); + // BTreeMap + let mut btree_map = BTreeMap::new(); + btree_map.insert(5, 7); + btree_map.insert(3, 3); + btree_map.insert(7, 4); + // VecDeque let mut vec_deque = VecDeque::new(); vec_deque.push_back(5);