Skip to content

Commit 0d0c08f

Browse files
committed
pretty printing for btreemap
1 parent fa23350 commit 0d0c08f

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/etc/debugger_pretty_printers_common.py

+21
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
TYPE_KIND_OS_STRING = 18
5050
TYPE_KIND_STD_VECDEQUE = 19
5151
TYPE_KIND_STD_BTREESET = 20
52+
TYPE_KIND_STD_BTREEMAP = 21
5253

5354
ENCODED_ENUM_PREFIX = "RUST$ENCODED$ENUM$"
5455
ENUM_DISR_FIELD_NAME = "RUST$ENUM$DISR"
@@ -75,6 +76,9 @@
7576
# std::collections::BTreeSet<> related constants
7677
STD_BTREESET_FIELD_NAMES = ["map"]
7778

79+
# std::collections::BTreeMap<> related constants
80+
STD_BTREEMAP_FIELD_NAMES = ["root", "length"]
81+
7882
# std::String related constants
7983
STD_STRING_FIELD_NAMES = ["vec"]
8084

@@ -184,6 +188,11 @@ def __classify_struct(self):
184188
self.__conforms_to_field_layout(STD_BTREESET_FIELD_NAMES)):
185189
return TYPE_KIND_STD_BTREESET
186190

191+
# STD COLLECTION BTREEMAP
192+
if (unqualified_type_name.startswith("BTreeMap<") and
193+
self.__conforms_to_field_layout(STD_BTREEMAP_FIELD_NAMES)):
194+
return TYPE_KIND_STD_BTREEMAP
195+
187196
# STD STRING
188197
if (unqualified_type_name.startswith("String") and
189198
self.__conforms_to_field_layout(STD_STRING_FIELD_NAMES)):
@@ -380,6 +389,18 @@ def extract_length_and_ptr_from_std_btreeset(vec_val):
380389
return (length, data_ptr)
381390

382391

392+
def extract_length_and_ptr_from_std_btreemap(vec_val):
393+
assert vec_val.type.get_type_kind() == TYPE_KIND_STD_BTREEMAP
394+
root = vec_val.get_child_at_index(0)
395+
length = vec_val.get_child_at_index(1).as_integer()
396+
node = root.get_child_at_index(0)
397+
ptr = node.get_child_at_index(0)
398+
unique_ptr_val = ptr.get_child_at_index(0)
399+
data_ptr = unique_ptr_val.get_child_at_index(0)
400+
assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR
401+
return (length, data_ptr)
402+
403+
383404
def extract_length_and_ptr_from_slice(slice_val):
384405
assert (slice_val.type.get_type_kind() == TYPE_KIND_SLICE or
385406
slice_val.type.get_type_kind() == TYPE_KIND_STR_SLICE)

src/etc/gdb_rust_pretty_printing.py

+30
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ def rust_pretty_printer_lookup_function(gdb_val):
130130
if type_kind == rustpp.TYPE_KIND_STD_BTREESET:
131131
return RustStdBTreeSetPrinter(val)
132132

133+
if type_kind == rustpp.TYPE_KIND_STD_BTREEMAP:
134+
return RustStdBTreeMapPrinter(val)
135+
133136
if type_kind == rustpp.TYPE_KIND_STD_STRING:
134137
return RustStdStringPrinter(val)
135138

@@ -325,6 +328,32 @@ def children(self):
325328
yield (str(index), gdb_ptr[index])
326329

327330

331+
class RustStdBTreeMapPrinter(object):
332+
def __init__(self, val):
333+
self.__val = val
334+
335+
@staticmethod
336+
def display_hint():
337+
return "map"
338+
339+
def to_string(self):
340+
(length, data_ptr) = \
341+
rustpp.extract_length_and_ptr_from_std_btreemap(self.__val)
342+
return (self.__val.type.get_unqualified_type_name() +
343+
("(len: %i)" % length))
344+
345+
def children(self):
346+
(length, data_ptr) = \
347+
rustpp.extract_length_and_ptr_from_std_btreemap(self.__val)
348+
keys = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3)
349+
keys_ptr = keys.get_wrapped_value()
350+
vals = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(4)
351+
vals_ptr = vals.get_wrapped_value()
352+
for index in xrange(length):
353+
yield (str(index), keys_ptr[index])
354+
yield (str(index), vals_ptr[index])
355+
356+
328357
class RustStdStringPrinter(object):
329358
def __init__(self, val):
330359
self.__val = val
@@ -338,6 +367,7 @@ def to_string(self):
338367
def display_hint(self):
339368
return "string"
340369

370+
341371
class RustOsStringPrinter(object):
342372
def __init__(self, val):
343373
self.__val = val

src/test/debuginfo/pretty-std-collections.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
// gdb-command: print btree_set
2323
// gdb-check:$1 = BTreeSet<i32>(len: 3) = {3, 5, 7}
2424

25+
// gdb-command: print btree_map
26+
// gdb-check:$2 = BTreeMap<i32, i32>(len: 3) = {[3] = 3, [5] = 7, [7] = 4}
27+
2528
// gdb-command: print vec_deque
26-
// gdb-check:$2 = VecDeque<i32>(len: 3, cap: 8) = {5, 3, 7}
29+
// gdb-check:$3 = VecDeque<i32>(len: 3, cap: 8) = {5, 3, 7}
2730

2831
#![allow(unused_variables)]
2932
use std::collections::BTreeSet;
33+
use std::collections::BTreeMap;
3034
use std::collections::VecDeque;
3135

3236

@@ -38,6 +42,12 @@ fn main() {
3842
btree_set.insert(3);
3943
btree_set.insert(7);
4044

45+
// BTreeMap
46+
let mut btree_map = BTreeMap::new();
47+
btree_map.insert(5, 7);
48+
btree_map.insert(3, 3);
49+
btree_map.insert(7, 4);
50+
4151
// VecDeque
4252
let mut vec_deque = VecDeque::new();
4353
vec_deque.push_back(5);

0 commit comments

Comments
 (0)