@@ -27,9 +27,18 @@ def rust_pretty_printer_lookup_function(val):
27
27
if type_code == gdb .TYPE_CODE_STRUCT :
28
28
struct_kind = classify_struct (val .type )
29
29
30
+ if struct_kind == STRUCT_KIND_SLICE :
31
+ return RustSlicePrinter (val )
32
+
30
33
if struct_kind == STRUCT_KIND_STR_SLICE :
31
34
return RustStringSlicePrinter (val )
32
35
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
+
33
42
if struct_kind == STRUCT_KIND_TUPLE :
34
43
return RustTuplePrinter (val )
35
44
@@ -172,6 +181,28 @@ def children(self):
172
181
def display_hint (self ):
173
182
return "array"
174
183
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
175
206
176
207
class RustStringSlicePrinter :
177
208
def __init__ (self , val ):
@@ -181,6 +212,35 @@ def to_string(self):
181
212
slice_byte_len = self .val ["length" ]
182
213
return '"%s"' % self .val ["data_ptr" ].string (encoding = "utf-8" , length = slice_byte_len )
183
214
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
+
184
244
185
245
class RustCStyleEnumPrinter :
186
246
def __init__ (self , val ):
@@ -204,19 +264,38 @@ def to_string(self):
204
264
STRUCT_KIND_TUPLE_VARIANT = 3
205
265
STRUCT_KIND_STRUCT_VARIANT = 4
206
266
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
208
271
209
272
210
273
def classify_struct (type ):
274
+ # print("\nclassify_struct: tag=%s\n" % type.tag)
211
275
if type .tag == "&str" :
212
276
return STRUCT_KIND_STR_SLICE
213
277
278
+ if type .tag .startswith ("&[" ) and type .tag .endswith ("]" ):
279
+ return STRUCT_KIND_SLICE
280
+
214
281
fields = list (type .fields ())
215
282
field_count = len (fields )
216
283
217
284
if field_count == 0 :
218
285
return STRUCT_KIND_REGULAR_STRUCT
219
286
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
+
220
299
if fields [0 ].name == "RUST$ENUM$DISR" :
221
300
if field_count == 1 :
222
301
return STRUCT_KIND_CSTYLE_VARIANT
@@ -254,3 +333,11 @@ def get_field_at_index(val, index):
254
333
return field
255
334
i += 1
256
335
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 )
0 commit comments