@@ -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
176207class 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
185245class RustCStyleEnumPrinter :
186246 def __init__ (self , val ):
@@ -204,19 +264,38 @@ def to_string(self):
204264STRUCT_KIND_TUPLE_VARIANT = 3
205265STRUCT_KIND_STRUCT_VARIANT = 4
206266STRUCT_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
210273def 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 )
0 commit comments