Skip to content

Commit 1aa6307

Browse files
committed
Support pretty printing slices using GDB
1 parent 6d525d5 commit 1aa6307

File tree

7 files changed

+78
-10
lines changed

7 files changed

+78
-10
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

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

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

89114
class StdVecProvider:
90115
def __init__(self, valobj):

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

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

src/tools/compiletest/src/runtest.rs

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

11261126
let rust_type_regexes = vec![
11271127
"^(alloc::([a-z_]+::)+)String$",
1128-
"^&str$",
1129-
"^&\\[.+\\]$",
1128+
"^&(mut )?str$",
1129+
"^&(mut )?\\[.+\\]$",
11301130
"^(std::ffi::([a-z_]+::)+)OsString$",
11311131
"^(alloc::([a-z_]+::)+)Vec<.+>$",
11321132
"^(alloc::([a-z_]+::)+)VecDeque<.+>$",

0 commit comments

Comments
 (0)