Skip to content

Commit

Permalink
pretty printing for btreemap
Browse files Browse the repository at this point in the history
  • Loading branch information
fukatani committed Aug 15, 2018
1 parent fa23350 commit 0d0c08f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/etc/debugger_pretty_printers_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
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"
Expand All @@ -75,6 +76,9 @@
# 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"]

Expand Down Expand Up @@ -184,6 +188,11 @@ def __classify_struct(self):
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)):
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions src/etc/gdb_rust_pretty_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -325,6 +328,32 @@ def children(self):
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
Expand All @@ -338,6 +367,7 @@ def to_string(self):
def display_hint(self):
return "string"


class RustOsStringPrinter(object):
def __init__(self, val):
self.__val = val
Expand Down
12 changes: 11 additions & 1 deletion src/test/debuginfo/pretty-std-collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
// gdb-command: print btree_set
// gdb-check:$1 = BTreeSet<i32>(len: 3) = {3, 5, 7}

// gdb-command: print btree_map
// gdb-check:$2 = BTreeMap<i32, i32>(len: 3) = {[3] = 3, [5] = 7, [7] = 4}

// gdb-command: print vec_deque
// gdb-check:$2 = VecDeque<i32>(len: 3, cap: 8) = {5, 3, 7}
// gdb-check:$3 = VecDeque<i32>(len: 3, cap: 8) = {5, 3, 7}

#![allow(unused_variables)]
use std::collections::BTreeSet;
use std::collections::BTreeMap;
use std::collections::VecDeque;


Expand All @@ -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);
Expand Down

0 comments on commit 0d0c08f

Please sign in to comment.