Skip to content

Commit 90fc28d

Browse files
debuginfo: Add GDB pretty printers for slices, Vec<>, and String.
1 parent b2f09c1 commit 90fc28d

File tree

2 files changed

+148
-1
lines changed

2 files changed

+148
-1
lines changed

src/etc/gdb_rust_pretty_printing.py

+88-1
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,18 @@ def rust_pretty_printer_lookup_function(val):
2727
if type_code == gdb.TYPE_CODE_STRUCT:
2828
struct_kind = classify_struct(val.type)
2929

30+
if struct_kind == STRUCT_KIND_SLICE:
31+
return RustSlicePrinter(val)
32+
3033
if struct_kind == STRUCT_KIND_STR_SLICE:
3134
return RustStringSlicePrinter(val)
3235

36+
if struct_kind == STRUCT_KIND_STD_VEC:
37+
return RustStdVecPrinter(val)
38+
39+
if struct_kind == STRUCT_KIND_STD_STRING:
40+
return RustStdStringPrinter(val)
41+
3342
if struct_kind == STRUCT_KIND_TUPLE:
3443
return RustTuplePrinter(val)
3544

@@ -172,6 +181,28 @@ def children(self):
172181
def display_hint(self):
173182
return "array"
174183

184+
class RustSlicePrinter:
185+
def __init__(self, val):
186+
self.val = val
187+
188+
def display_hint(self):
189+
return "array"
190+
191+
def to_string(self):
192+
length = int(self.val["length"])
193+
return self.val.type.tag + ("(len: %i)" % length)
194+
195+
def children(self):
196+
cs = []
197+
length = int(self.val["length"])
198+
data_ptr = self.val["data_ptr"]
199+
assert data_ptr.type.code == gdb.TYPE_CODE_PTR
200+
pointee_type = data_ptr.type.target()
201+
202+
for index in range(0, length):
203+
cs.append((str(index), (data_ptr + index).dereference()))
204+
205+
return cs
175206

176207
class RustStringSlicePrinter:
177208
def __init__(self, val):
@@ -181,6 +212,35 @@ def to_string(self):
181212
slice_byte_len = self.val["length"]
182213
return '"%s"' % self.val["data_ptr"].string(encoding="utf-8", length=slice_byte_len)
183214

215+
class RustStdVecPrinter:
216+
def __init__(self, val):
217+
self.val = val
218+
219+
def display_hint(self):
220+
return "array"
221+
222+
def to_string(self):
223+
length = int(self.val["len"])
224+
cap = int(self.val["cap"])
225+
return self.val.type.tag + ("(len: %i, cap: %i)" % (length, cap))
226+
227+
def children(self):
228+
cs = []
229+
(length, data_ptr) = extract_length_and_data_ptr_from_std_vec(self.val)
230+
pointee_type = data_ptr.type.target()
231+
232+
for index in range(0, length):
233+
cs.append((str(index), (data_ptr + index).dereference()))
234+
return cs
235+
236+
class RustStdStringPrinter:
237+
def __init__(self, val):
238+
self.val = val
239+
240+
def to_string(self):
241+
(length, data_ptr) = extract_length_and_data_ptr_from_std_vec(self.val["vec"])
242+
return '"%s"' % data_ptr.string(encoding="utf-8", length=length)
243+
184244

185245
class RustCStyleEnumPrinter:
186246
def __init__(self, val):
@@ -204,19 +264,38 @@ def to_string(self):
204264
STRUCT_KIND_TUPLE_VARIANT = 3
205265
STRUCT_KIND_STRUCT_VARIANT = 4
206266
STRUCT_KIND_CSTYLE_VARIANT = 5
207-
STRUCT_KIND_STR_SLICE = 6
267+
STRUCT_KIND_SLICE = 6
268+
STRUCT_KIND_STR_SLICE = 7
269+
STRUCT_KIND_STD_VEC = 8
270+
STRUCT_KIND_STD_STRING = 9
208271

209272

210273
def classify_struct(type):
274+
# print("\nclassify_struct: tag=%s\n" % type.tag)
211275
if type.tag == "&str":
212276
return STRUCT_KIND_STR_SLICE
213277

278+
if type.tag.startswith("&[") and type.tag.endswith("]"):
279+
return STRUCT_KIND_SLICE
280+
214281
fields = list(type.fields())
215282
field_count = len(fields)
216283

217284
if field_count == 0:
218285
return STRUCT_KIND_REGULAR_STRUCT
219286

287+
if (field_count == 3 and
288+
fields[0].name == "ptr" and
289+
fields[1].name == "len" and
290+
fields[2].name == "cap" and
291+
type.tag.startswith("Vec<")):
292+
return STRUCT_KIND_STD_VEC
293+
294+
if (field_count == 1 and
295+
fields[0].name == "vec" and
296+
type.tag == "String"):
297+
return STRUCT_KIND_STD_STRING
298+
220299
if fields[0].name == "RUST$ENUM$DISR":
221300
if field_count == 1:
222301
return STRUCT_KIND_CSTYLE_VARIANT
@@ -254,3 +333,11 @@ def get_field_at_index(val, index):
254333
return field
255334
i += 1
256335
return None
336+
337+
def extract_length_and_data_ptr_from_std_vec(vec_val):
338+
length = int(vec_val["len"])
339+
vec_ptr_val = vec_val["ptr"]
340+
unique_ptr_val = vec_ptr_val[first_field(vec_ptr_val)]
341+
data_ptr = unique_ptr_val[first_field(unique_ptr_val)]
342+
assert data_ptr.type.code == gdb.TYPE_CODE_PTR
343+
return (length, data_ptr)

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

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-windows failing on win32 bot
12+
// ignore-freebsd: gdb package too new
13+
// ignore-tidy-linelength
14+
// ignore-lldb
15+
// ignore-android: FIXME(#10381)
16+
// compile-flags:-g
17+
// min-gdb-version 7.7
18+
19+
// gdb-command: run
20+
21+
// gdb-command: print slice
22+
// gdb-check:$1 = &[i32](len: 4) = {0, 1, 2, 3}
23+
24+
// gdb-command: print vec
25+
// gdb-check:$2 = Vec<u64>(len: 4, cap: [...]) = {4, 5, 6, 7}
26+
27+
// gdb-command: print str_slice
28+
// gdb-check:$3 = "IAMA string slice!"
29+
30+
// gdb-command: print string
31+
// gdb-check:$4 = "IAMA string!"
32+
33+
// gdb-command: print some
34+
// gdb-check:$5 = Some = {8}
35+
36+
// gdb-command: print none
37+
// gdb-check:$6 = None
38+
39+
fn main() {
40+
41+
// &[]
42+
let slice: &[i32] = &[0, 1, 2, 3];
43+
44+
// Vec
45+
let vec = vec![4u64, 5, 6, 7];
46+
47+
// &str
48+
let str_slice = "IAMA string slice!";
49+
50+
// String
51+
let string = "IAMA string!".to_string();
52+
53+
// Option
54+
let some = Some(8i16);
55+
let none: Option<i64> = None;
56+
57+
zzz(); // #break
58+
}
59+
60+
fn zzz() { () }

0 commit comments

Comments
 (0)