Skip to content

Commit 1f97e93

Browse files
Rollup merge of rust-lang#52821 - fukatani:gdb-vecdeque-print, r=michaelwoerister
pretty print for std::collections::vecdeque I want pretty print function for VecDeque like Vec. ```rust use std::collections::VecDeque; fn main() { let mut d: VecDeque<i32> = VecDeque::new(); d.push_back(4); d.push_back(4); d.push_back(6); let mut v: Vec<i32> = Vec::new(); v.push(4); v.push(4); v.push(6); } ``` ``` (gdb) p v $1 = Vec<i32>(len: 3, cap: 4) = {4, 4, 6} (gdb) p d $2 = VecDeque<i32>(len: 3, cap: 8) = {4, 4, 6} ``` Thanks.
2 parents 647d246 + 9845ee0 commit 1f97e93

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Diff for: src/etc/debugger_pretty_printers_common.py

+33
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
TYPE_KIND_FIXED_SIZE_VEC = 16
4848
TYPE_KIND_REGULAR_UNION = 17
4949
TYPE_KIND_OS_STRING = 18
50+
TYPE_KIND_STD_VECDEQUE = 19
5051

5152
ENCODED_ENUM_PREFIX = "RUST$ENCODED$ENUM$"
5253
ENUM_DISR_FIELD_NAME = "RUST$ENUM$DISR"
@@ -62,6 +63,14 @@
6263
STD_VEC_FIELD_NAMES = [STD_VEC_FIELD_NAME_BUF,
6364
STD_VEC_FIELD_NAME_LENGTH]
6465

66+
# std::collections::VecDeque<> related constants
67+
STD_VECDEQUE_FIELD_NAME_TAIL = "tail"
68+
STD_VECDEQUE_FIELD_NAME_HEAD = "head"
69+
STD_VECDEQUE_FIELD_NAME_BUF = "buf"
70+
STD_VECDEQUE_FIELD_NAMES = [STD_VECDEQUE_FIELD_NAME_TAIL,
71+
STD_VECDEQUE_FIELD_NAME_HEAD,
72+
STD_VECDEQUE_FIELD_NAME_BUF]
73+
6574
# std::String related constants
6675
STD_STRING_FIELD_NAMES = ["vec"]
6776

@@ -161,6 +170,11 @@ def __classify_struct(self):
161170
self.__conforms_to_field_layout(STD_VEC_FIELD_NAMES)):
162171
return TYPE_KIND_STD_VEC
163172

173+
# STD COLLECTION VECDEQUE
174+
if (unqualified_type_name.startswith("VecDeque<") and
175+
self.__conforms_to_field_layout(STD_VECDEQUE_FIELD_NAMES)):
176+
return TYPE_KIND_STD_VECDEQUE
177+
164178
# STD STRING
165179
if (unqualified_type_name.startswith("String") and
166180
self.__conforms_to_field_layout(STD_STRING_FIELD_NAMES)):
@@ -325,6 +339,25 @@ def extract_length_ptr_and_cap_from_std_vec(vec_val):
325339
assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR
326340
return (length, data_ptr, capacity)
327341

342+
343+
def extract_tail_head_ptr_and_cap_from_std_vecdeque(vec_val):
344+
assert vec_val.type.get_type_kind() == TYPE_KIND_STD_VECDEQUE
345+
tail_field_index = STD_VECDEQUE_FIELD_NAMES.index(STD_VECDEQUE_FIELD_NAME_TAIL)
346+
head_field_index = STD_VECDEQUE_FIELD_NAMES.index(STD_VECDEQUE_FIELD_NAME_HEAD)
347+
buf_field_index = STD_VECDEQUE_FIELD_NAMES.index(STD_VECDEQUE_FIELD_NAME_BUF)
348+
349+
tail = vec_val.get_child_at_index(tail_field_index).as_integer()
350+
head = vec_val.get_child_at_index(head_field_index).as_integer()
351+
buf = vec_val.get_child_at_index(buf_field_index)
352+
353+
vec_ptr_val = buf.get_child_at_index(0)
354+
capacity = buf.get_child_at_index(1).as_integer()
355+
unique_ptr_val = vec_ptr_val.get_child_at_index(0)
356+
data_ptr = unique_ptr_val.get_child_at_index(0)
357+
assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR
358+
return (tail, head, data_ptr, capacity)
359+
360+
328361
def extract_length_and_ptr_from_slice(slice_val):
329362
assert (slice_val.type.get_type_kind() == TYPE_KIND_SLICE or
330363
slice_val.type.get_type_kind() == TYPE_KIND_STR_SLICE)

Diff for: src/etc/gdb_rust_pretty_printing.py

+25
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ def rust_pretty_printer_lookup_function(gdb_val):
124124
if type_kind == rustpp.TYPE_KIND_STD_VEC:
125125
return RustStdVecPrinter(val)
126126

127+
if type_kind == rustpp.TYPE_KIND_STD_VECDEQUE:
128+
return RustStdVecDequePrinter(val)
129+
127130
if type_kind == rustpp.TYPE_KIND_STD_STRING:
128131
return RustStdStringPrinter(val)
129132

@@ -274,6 +277,28 @@ def children(self):
274277
yield (str(index), (gdb_ptr + index).dereference())
275278

276279

280+
class RustStdVecDequePrinter(object):
281+
def __init__(self, val):
282+
self.__val = val
283+
284+
@staticmethod
285+
def display_hint():
286+
return "array"
287+
288+
def to_string(self):
289+
(tail, head, data_ptr, cap) = \
290+
rustpp.extract_tail_head_ptr_and_cap_from_std_vecdeque(self.__val)
291+
return (self.__val.type.get_unqualified_type_name() +
292+
("(len: %i, cap: %i)" % (head - tail, cap)))
293+
294+
def children(self):
295+
(tail, head, data_ptr, cap) = \
296+
rustpp.extract_tail_head_ptr_and_cap_from_std_vecdeque(self.__val)
297+
gdb_ptr = data_ptr.get_wrapped_value()
298+
for index in xrange(tail, head):
299+
yield (str(index), (gdb_ptr + index).dereference())
300+
301+
277302
class RustStdStringPrinter(object):
278303
def __init__(self, val):
279304
self.__val = val

0 commit comments

Comments
 (0)