Skip to content

Commit 94d43d6

Browse files
committed
Auto merge of #68098 - ssomers:btreemap_gdb_pretty_print, r=Mark-Simulacrum
Test gdb pretty printing more and fix overzealous type substitution Adresses a problem concerning printing BTreeMap / BTreeSet data in gdb: when the key or value type name contains substring "LeafNode", and the map has multiple nodes (e.g. more than 11 elements), printing causes an exception. E.g. ``` rustc -g - <<EOF use std::collections::BTreeMap; struct MyLeafNode(i8); fn main() { let m: BTreeMap<i8, MyLeafNode> = (0..12).map(|i| (i, MyLeafNode(i))).collect(); assert!(!m.is_empty()); } EOF ``` ``` $ rust-gdb rust_out (gdb) b 7 (gdb) r (gdb) p m $1 = BTreeMap<i8, rust_out::MyLeafNode>(len: 12)Python Exception <class 'gdb.error'> No type named alloc::collections::btree::node::InternalNode<i8, rust_out::MyInternalNode>.: use std::collections::BTreeMap; ``` The code was written in #56144 by @tromey (and later touched upon by @RalfJung in #57045, but I think that had nothing to do with the issues in this PR).
2 parents 5574b1d + d8a136f commit 94d43d6

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/etc/gdb_rust_pretty_printing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def children(self):
335335
def children_of_node(boxed_node, height, want_values):
336336
node_ptr = boxed_node['ptr']['pointer']
337337
if height > 0:
338-
type_name = str(node_ptr.type.target()).replace('LeafNode', 'InternalNode')
338+
type_name = str(node_ptr.type.target()).replace('LeafNode', 'InternalNode', 1)
339339
node_type = gdb.lookup_type(type_name)
340340
node_ptr = node_ptr.cast(node_type.pointer())
341341
leaf = node_ptr['data']

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,22 @@
2626
// gdb-command: print empty_btree_map
2727
// gdb-check:$4 = BTreeMap<i32, u32>(len: 0)
2828

29+
// gdb-command: print nasty_btree_map
30+
// gdb-check:$5 = BTreeMap<i32, pretty_std_collections::MyLeafNode>(len: 1) = {[1] = pretty_std_collections::MyLeafNode (11)}
31+
2932
// gdb-command: print vec_deque
30-
// gdb-check:$5 = VecDeque<i32>(len: 3, cap: 8) = {5, 3, 7}
33+
// gdb-check:$6 = VecDeque<i32>(len: 3, cap: 8) = {5, 3, 7}
3134

3235
// gdb-command: print vec_deque2
33-
// gdb-check:$6 = VecDeque<i32>(len: 7, cap: 8) = {2, 3, 4, 5, 6, 7, 8}
36+
// gdb-check:$7 = VecDeque<i32>(len: 7, cap: 8) = {2, 3, 4, 5, 6, 7, 8}
3437

3538
#![allow(unused_variables)]
3639
use std::collections::BTreeMap;
3740
use std::collections::BTreeSet;
3841
use std::collections::VecDeque;
3942

43+
struct MyLeafNode(i32); // helps to ensure we don't blindly replace substring "LeafNode"
44+
4045
fn main() {
4146
// BTreeSet
4247
let mut btree_set = BTreeSet::new();
@@ -54,6 +59,9 @@ fn main() {
5459

5560
let mut empty_btree_map: BTreeMap<i32, u32> = BTreeMap::new();
5661

62+
let mut nasty_btree_map: BTreeMap<i32, MyLeafNode> = BTreeMap::new();
63+
nasty_btree_map.insert(1, MyLeafNode(11));
64+
5765
// VecDeque
5866
let mut vec_deque = VecDeque::new();
5967
vec_deque.push_back(5);

0 commit comments

Comments
 (0)