Skip to content

Commit 0deb536

Browse files
committed
Auto merge of #85363 - EFanZh:gdb-pretty-print-slices, r=michaelwoerister
Support pretty printing slices using GDB Support pretty printing `&[T]`, `&mut [T]` and `&mut str` types using GDB. Support pretty printing `&mut [T]` and `&mut str` types using LLDB. Fixes #85219.
2 parents 0cd0709 + 1b0998c commit 0deb536

File tree

7 files changed

+95
-26
lines changed

7 files changed

+95
-26
lines changed

src/etc/gdb_lookup.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from rust_types import *
66

77

8-
rust_enabled = 'set language rust' in gdb.execute('complete set language ru', to_string=True)
98
_gdb_version_matched = re.search('([0-9]+)\\.([0-9]+)', gdb.VERSION)
109
gdb_version = [int(num) for num in _gdb_version_matched.groups()] if _gdb_version_matched else []
1110

@@ -52,9 +51,10 @@ def lookup(valobj):
5251
return StdStringProvider(valobj)
5352
if rust_type == RustType.STD_OS_STRING:
5453
return StdOsStringProvider(valobj)
55-
if rust_type == RustType.STD_STR and not rust_enabled:
54+
if rust_type == RustType.STD_STR:
5655
return StdStrProvider(valobj)
57-
56+
if rust_type == RustType.STD_SLICE:
57+
return StdSliceProvider(valobj)
5858
if rust_type == RustType.STD_VEC:
5959
return StdVecProvider(valobj)
6060
if rust_type == RustType.STD_VEC_DEQUE:

src/etc/gdb_providers.py

+39-16
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,39 @@ def to_string(self):
8585
def display_hint():
8686
return "string"
8787

88+
def _enumerate_array_elements(element_ptrs):
89+
for (i, element_ptr) in enumerate(element_ptrs):
90+
key = "[{}]".format(i)
91+
element = element_ptr.dereference()
92+
93+
try:
94+
# rust-lang/rust#64343: passing deref expr to `str` allows
95+
# catching exception on garbage pointer
96+
str(element)
97+
except RuntimeError:
98+
yield key, "inaccessible"
99+
100+
break
101+
102+
yield key, element
103+
104+
class StdSliceProvider:
105+
def __init__(self, valobj):
106+
self.valobj = valobj
107+
self.length = int(valobj["length"])
108+
self.data_ptr = valobj["data_ptr"]
109+
110+
def to_string(self):
111+
return "{}(size={})".format(self.valobj.type, self.length)
112+
113+
def children(self):
114+
return _enumerate_array_elements(
115+
self.data_ptr + index for index in xrange(self.length)
116+
)
117+
118+
@staticmethod
119+
def display_hint():
120+
return "array"
88121

89122
class StdVecProvider:
90123
def __init__(self, valobj):
@@ -96,19 +129,9 @@ def to_string(self):
96129
return "Vec(size={})".format(self.length)
97130

98131
def children(self):
99-
saw_inaccessible = False
100-
for index in xrange(self.length):
101-
element_ptr = self.data_ptr + index
102-
if saw_inaccessible:
103-
return
104-
try:
105-
# rust-lang/rust#64343: passing deref expr to `str` allows
106-
# catching exception on garbage pointer
107-
str(element_ptr.dereference())
108-
yield "[{}]".format(index), element_ptr.dereference()
109-
except RuntimeError:
110-
saw_inaccessible = True
111-
yield str(index), "inaccessible"
132+
return _enumerate_array_elements(
133+
self.data_ptr + index for index in xrange(self.length)
134+
)
112135

113136
@staticmethod
114137
def display_hint():
@@ -131,9 +154,9 @@ def to_string(self):
131154
return "VecDeque(size={})".format(self.size)
132155

133156
def children(self):
134-
for index in xrange(0, self.size):
135-
value = (self.data_ptr + ((self.tail + index) % self.cap)).dereference()
136-
yield "[{}]".format(index), value
157+
return _enumerate_array_elements(
158+
(self.data_ptr + ((self.tail + index) % self.cap)) for index in xrange(self.size)
159+
)
137160

138161
@staticmethod
139162
def display_hint():

src/etc/lldb_commands

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
type synthetic add -l lldb_lookup.synthetic_lookup -x ".*" --category Rust
22
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)String$" --category Rust
3-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&str$" --category Rust
4-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&\\[.+\\]$" --category Rust
3+
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?str$" --category Rust
4+
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?\\[.+\\]$" --category Rust
55
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::ffi::([a-z_]+::)+)OsString$" --category Rust
66
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Vec<.+>$" --category Rust
77
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)VecDeque<.+>$" --category Rust

src/etc/rust_types.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class RustType(object):
3434

3535

3636
STD_STRING_REGEX = re.compile(r"^(alloc::(\w+::)+)String$")
37-
STD_STR_REGEX = re.compile(r"^&str$")
38-
STD_SLICE_REGEX = re.compile(r"^&\[.+\]$")
37+
STD_STR_REGEX = re.compile(r"^&(mut )?str$")
38+
STD_SLICE_REGEX = re.compile(r"^&(mut )?\[.+\]$")
3939
STD_OS_STRING_REGEX = re.compile(r"^(std::ffi::(\w+::)+)OsString$")
4040
STD_VEC_REGEX = re.compile(r"^(alloc::(\w+::)+)Vec<.+>$")
4141
STD_VEC_DEQUE_REGEX = re.compile(r"^(alloc::(\w+::)+)VecDeque<.+>$")

src/test/debuginfo/pretty-huge-vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// gdb-check:$1 = Vec(size=1000000000) = {[...]...}
1414

1515
// gdb-command: print slice
16-
// gdb-check:$2 = &[u8] {data_ptr: [...], length: 1000000000}
16+
// gdb-check:$2 = &[u8](size=1000000000) = {[...]...}
1717

1818
#![allow(unused_variables)]
1919

src/test/debuginfo/pretty-slices.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// ignore-android: FIXME(#10381)
2+
// ignore-windows
3+
// compile-flags:-g
4+
5+
// gdb-command: run
6+
7+
// gdb-command: print slice
8+
// gdbg-check: $1 = struct &[i32](size=3) = {0, 1, 2}
9+
// gdbr-check: $1 = &[i32](size=3) = {0, 1, 2}
10+
11+
// gdb-command: print mut_slice
12+
// gdbg-check: $2 = struct &mut [i32](size=4) = {2, 3, 5, 7}
13+
// gdbr-check: $2 = &mut [i32](size=4) = {2, 3, 5, 7}
14+
15+
// gdb-command: print str_slice
16+
// gdb-check: $3 = "string slice"
17+
18+
// gdb-command: print mut_str_slice
19+
// gdb-check: $4 = "mutable string slice"
20+
21+
// lldb-command: run
22+
23+
// lldb-command: print slice
24+
// lldb-check: (&[i32]) $0 = size=3 { [0] = 0 [1] = 1 [2] = 2 }
25+
26+
// lldb-command: print mut_slice
27+
// lldb-check: (&mut [i32]) $1 = size=4 { [0] = 2 [1] = 3 [2] = 5 [3] = 7 }
28+
29+
// lldb-command: print str_slice
30+
// lldb-check: (&str) $2 = "string slice" { data_ptr = [...] length = 12 }
31+
32+
// lldb-command: print mut_str_slice
33+
// lldb-check: (&mut str) $3 = "mutable string slice" { data_ptr = [...] length = 20 }
34+
35+
fn b() {}
36+
37+
fn main() {
38+
let slice: &[i32] = &[0, 1, 2];
39+
let mut_slice: &mut [i32] = &mut [2, 3, 5, 7];
40+
41+
let str_slice: &str = "string slice";
42+
let mut mut_str_slice_buffer = String::from("mutable string slice");
43+
let mut_str_slice: &mut str = mut_str_slice_buffer.as_mut_str();
44+
45+
b(); // #break
46+
}

src/tools/compiletest/src/runtest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,8 @@ impl<'test> TestCx<'test> {
11331133

11341134
let rust_type_regexes = vec![
11351135
"^(alloc::([a-z_]+::)+)String$",
1136-
"^&str$",
1137-
"^&\\[.+\\]$",
1136+
"^&(mut )?str$",
1137+
"^&(mut )?\\[.+\\]$",
11381138
"^(std::ffi::([a-z_]+::)+)OsString$",
11391139
"^(alloc::([a-z_]+::)+)Vec<.+>$",
11401140
"^(alloc::([a-z_]+::)+)VecDeque<.+>$",

0 commit comments

Comments
 (0)