Skip to content

Commit 3d5b8c6

Browse files
committed
Auto merge of #42278 - gentoo90:gdb-pretty-printers, r=michaelwoerister
Fix GDB pretty-printer for tuples and pointers Names of children should not be the same, because GDB uses them to distinguish the children. |Before|After| |---|---| |![tuples_before](https://cloud.githubusercontent.com/assets/1297574/26527639/5d6cf10e-43a0-11e7-9498-abfcddb08055.png)|![tuples_after](https://cloud.githubusercontent.com/assets/1297574/26527655/9699233a-43a0-11e7-83c6-f58f713b51a0.png)| `main.rs` ```rust enum Test { Zero, One(i32), Two(i32, String), Three(i32, String, Vec<String>), } fn main() { let tuple = (1, 2, "Asdfgh"); let zero = Test::Zero; let one = Test::One(10); let two = Test::Two(42, "Qwerty".to_owned()); let three = Test::Three(9000, "Zxcvbn".to_owned(), vec!["lorem".to_owned(), "ipsum".to_owned(), "dolor".to_owned()]); println!(""); // breakpoint here } ``` `launch.json` ```json { "version": "0.2.0", "configurations": [ { "type": "gdb", "request": "launch", "gdbpath": "rust-gdb", "name": "Launch Program", "valuesFormatting": "prettyPrinters", //this requires plugin Native Debug >= 0.20.0 "target": "./target/debug/test_pretty_printers", "cwd": "${workspaceRoot}" } ] } ```
2 parents 5fe923d + 63076dd commit 3d5b8c6

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

src/etc/debugger_pretty_printers_common.py

+14
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
TYPE_KIND_PTR = 15
4747
TYPE_KIND_FIXED_SIZE_VEC = 16
4848
TYPE_KIND_REGULAR_UNION = 17
49+
TYPE_KIND_OS_STRING = 18
4950

5051
ENCODED_ENUM_PREFIX = "RUST$ENCODED$ENUM$"
5152
ENUM_DISR_FIELD_NAME = "RUST$ENUM$DISR"
@@ -64,6 +65,9 @@
6465
# std::String related constants
6566
STD_STRING_FIELD_NAMES = ["vec"]
6667

68+
# std::ffi::OsString related constants
69+
OS_STRING_FIELD_NAMES = ["inner"]
70+
6771

6872
class Type(object):
6973
"""
@@ -162,6 +166,11 @@ def __classify_struct(self):
162166
self.__conforms_to_field_layout(STD_STRING_FIELD_NAMES)):
163167
return TYPE_KIND_STD_STRING
164168

169+
# OS STRING
170+
if (unqualified_type_name == "OsString" and
171+
self.__conforms_to_field_layout(OS_STRING_FIELD_NAMES)):
172+
return TYPE_KIND_OS_STRING
173+
165174
# ENUM VARIANTS
166175
if fields[0].name == ENUM_DISR_FIELD_NAME:
167176
if field_count == 1:
@@ -345,3 +354,8 @@ def extract_type_name(qualified_type_name):
345354
return qualified_type_name
346355
else:
347356
return qualified_type_name[index + 2:]
357+
358+
try:
359+
compat_str = unicode # Python 2
360+
except NameError:
361+
compat_str = str

src/etc/gdb_rust_pretty_printing.py

+34-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def get_child_at_index(self, index):
7878

7979
def as_integer(self):
8080
if self.gdb_val.type.code == gdb.TYPE_CODE_PTR:
81-
return int(str(self.gdb_val), 0)
81+
as_str = rustpp.compat_str(self.gdb_val).split()[0]
82+
return int(as_str, 0)
8283
return int(self.gdb_val)
8384

8485
def get_wrapped_value(self):
@@ -99,8 +100,10 @@ def rust_pretty_printer_lookup_function(gdb_val):
99100
val = GdbValue(gdb_val)
100101
type_kind = val.type.get_type_kind()
101102

102-
if (type_kind == rustpp.TYPE_KIND_REGULAR_STRUCT or
103-
type_kind == rustpp.TYPE_KIND_EMPTY):
103+
if type_kind == rustpp.TYPE_KIND_EMPTY:
104+
return RustEmptyPrinter(val)
105+
106+
if type_kind == rustpp.TYPE_KIND_REGULAR_STRUCT:
104107
return RustStructPrinter(val,
105108
omit_first_field = False,
106109
omit_type_name = False,
@@ -124,6 +127,9 @@ def rust_pretty_printer_lookup_function(gdb_val):
124127
if type_kind == rustpp.TYPE_KIND_STD_STRING:
125128
return RustStdStringPrinter(val)
126129

130+
if type_kind == rustpp.TYPE_KIND_OS_STRING:
131+
return RustOsStringPrinter(val)
132+
127133
if type_kind == rustpp.TYPE_KIND_TUPLE:
128134
return RustStructPrinter(val,
129135
omit_first_field = False,
@@ -170,6 +176,14 @@ def rust_pretty_printer_lookup_function(gdb_val):
170176
#=------------------------------------------------------------------------------
171177
# Pretty Printer Classes
172178
#=------------------------------------------------------------------------------
179+
class RustEmptyPrinter(object):
180+
def __init__(self, val):
181+
self.__val = val
182+
183+
def to_string(self):
184+
return self.__val.type.get_unqualified_type_name()
185+
186+
173187
class RustStructPrinter(object):
174188
def __init__(self, val, omit_first_field, omit_type_name, is_tuple_like):
175189
self.__val = val
@@ -186,10 +200,10 @@ def children(self):
186200
cs = []
187201
wrapped_value = self.__val.get_wrapped_value()
188202

189-
for field in self.__val.type.get_fields():
203+
for number, field in enumerate(self.__val.type.get_fields()):
190204
field_value = wrapped_value[field.name]
191205
if self.__is_tuple_like:
192-
cs.append(("", field_value))
206+
cs.append((str(number), field_value))
193207
else:
194208
cs.append((field.name, field_value))
195209

@@ -268,6 +282,21 @@ def to_string(self):
268282
length=length)
269283

270284

285+
class RustOsStringPrinter(object):
286+
def __init__(self, val):
287+
self.__val = val
288+
289+
def to_string(self):
290+
buf = self.__val.get_child_at_index(0)
291+
vec = buf.get_child_at_index(0)
292+
if vec.type.get_unqualified_type_name() == "Wtf8Buf":
293+
vec = vec.get_child_at_index(0)
294+
295+
(length, data_ptr, cap) = rustpp.extract_length_ptr_and_cap_from_std_vec(
296+
vec)
297+
return '"%s"' % data_ptr.get_wrapped_value().string(length=length)
298+
299+
271300
class RustCStyleVariantPrinter(object):
272301
def __init__(self, val):
273302
assert val.type.get_dwarf_type_kind() == rustpp.DWARF_TYPE_CODE_ENUM

src/test/debuginfo/pretty-std.rs

+13
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
// gdbg-check:$6 = None
3939
// gdbr-check:$6 = core::option::Option::None
4040

41+
// gdb-command: print os_string
42+
// gdb-check:$7 = "IAMA OS string 😃"
43+
44+
// gdb-command: print some_string
45+
// gdb-check:$8 = Some = {"IAMA optional string!"}
46+
4147

4248
// === LLDB TESTS ==================================================================================
4349

@@ -63,6 +69,8 @@
6369

6470

6571
#![allow(unused_variables)]
72+
use std::ffi::OsString;
73+
6674

6775
fn main() {
6876

@@ -78,10 +86,15 @@ fn main() {
7886
// String
7987
let string = "IAMA string!".to_string();
8088

89+
// OsString
90+
let os_string = OsString::from("IAMA OS string \u{1F603}");
91+
8192
// Option
8293
let some = Some(8i16);
8394
let none: Option<i64> = None;
8495

96+
let some_string = Some("IAMA optional string!".to_owned());
97+
8598
zzz(); // #break
8699
}
87100

0 commit comments

Comments
 (0)