Skip to content

Commit e34eebb

Browse files
GDB: format output for class entries and class constants (php#15955)
For `zend_string` pointers in class entries, show the string contents (if not `NULL`). For class constants, the `ce` (class entry pointer) field is shown with the name of the class, and the `doc_comment` field is shown with the string contents if possible.
1 parent b675db4 commit e34eebb

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

main/debug_gdb_scripts.c

+52
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,12 @@ asm(
737737
".ascii \"\\n\"\n"
738738
".ascii \"pp_set.add_printer('zend_string', '^_zend_string$', ZendStringPrettyPrinter)\\n\"\n"
739739
".ascii \"\\n\"\n"
740+
".ascii \"def zendStringPointerPrinter(ptr):\\n\"\n"
741+
".ascii \" \\\"Given a pointer to a zend_string, show the contents (if non-NULL)\\\"\\n\"\n"
742+
".ascii \" if int(ptr) == 0:\\n\"\n"
743+
".ascii \" return '0x0'\\n\"\n"
744+
".ascii \" return ZendStringPrettyPrinter(ptr.dereference()).to_string()\\n\"\n"
745+
".ascii \"\\n\"\n"
740746
".ascii \"class ZendTypePrettyPrinter(gdb.printing.PrettyPrinter):\\n\"\n"
741747
".ascii \" \\\"Print a zend_type\\\"\\n\"\n"
742748
".ascii \"\\n\"\n"
@@ -959,6 +965,52 @@ asm(
959965
".ascii \"\\n\"\n"
960966
".ascii \"pp_set.add_printer('zval', '^_zval_struct$', ZvalPrettyPrinter)\\n\"\n"
961967
".ascii \"\\n\"\n"
968+
".ascii \"class ZendClassEntryPrettyPrinter(gdb.printing.PrettyPrinter):\\n\"\n"
969+
".ascii \" \\\"Print a zend_class_entry\\\"\\n\"\n"
970+
".ascii \"\\n\"\n"
971+
".ascii \" # String pointers, show the string contents if possible\\n\"\n"
972+
".ascii \" STRING_FIELDS = [ 'name', 'doc_comment' ]\\n\"\n"
973+
".ascii \"\\n\"\n"
974+
".ascii \" def __init__(self, val):\\n\"\n"
975+
".ascii \" self.val = val\\n\"\n"
976+
".ascii \"\\n\"\n"
977+
".ascii \" def to_string(self):\\n\"\n"
978+
".ascii \" return zendStringPointerPrinter(self.val['name'])\\n\"\n"
979+
".ascii \"\\n\"\n"
980+
".ascii \" def children(self):\\n\"\n"
981+
".ascii \" for field in self.val.type.fields():\\n\"\n"
982+
".ascii \" if field.name is not None:\\n\"\n"
983+
".ascii \" if field.name in self.STRING_FIELDS:\\n\"\n"
984+
".ascii \" yield (field.name, zendStringPointerPrinter(self.val[field.name]))\\n\"\n"
985+
".ascii \" else:\\n\"\n"
986+
".ascii \" yield (field.name, self.val[field.name])\\n\"\n"
987+
".ascii \" else:\\n\"\n"
988+
".ascii \" # Don't break on the union fields. Unfortunately, pretty\\n\"\n"
989+
".ascii \" # printers done in python cannot match the default formatting of\\n\"\n"
990+
".ascii \" # C anonymous fields, which omit the name entirely, see\\n\"\n"
991+
".ascii \" # binutils-gdb/gdb/cp-valprint.c#248 (as of commit\\n\"\n"
992+
".ascii \" # b6532accdd8e24329cc69bb58bc2883796008776)\\n\"\n"
993+
".ascii \" yield ('<anonymous>', self.val[field])\\n\"\n"
994+
".ascii \"\\n\"\n"
995+
".ascii \"pp_set.add_printer('zend_class_entry', '^_zend_class_entry$', ZendClassEntryPrettyPrinter)\\n\"\n"
996+
".ascii \"\\n\"\n"
997+
".ascii \"class ZendClassConstantPrettyPrinter(gdb.printing.PrettyPrinter):\\n\"\n"
998+
".ascii \" \\\"Print a zend_class_constant\\\"\\n\"\n"
999+
".ascii \"\\n\"\n"
1000+
".ascii \" def __init__(self, val):\\n\"\n"
1001+
".ascii \" self.val = val\\n\"\n"
1002+
".ascii \"\\n\"\n"
1003+
".ascii \" def children(self):\\n\"\n"
1004+
".ascii \" for field in self.val.type.fields():\\n\"\n"
1005+
".ascii \" if field.name == 'doc_comment':\\n\"\n"
1006+
".ascii \" yield ('doc_comment', zendStringPointerPrinter(self.val['doc_comment']))\\n\"\n"
1007+
".ascii \" elif field.name == 'ce':\\n\"\n"
1008+
".ascii \" yield ('ce', zendStringPointerPrinter(self.val['ce']['name']))\\n\"\n"
1009+
".ascii \" else:\\n\"\n"
1010+
".ascii \" yield (field.name, self.val[field.name])\\n\"\n"
1011+
".ascii \"\\n\"\n"
1012+
".ascii \"pp_set.add_printer('zend_class_constant', '^_zend_class_constant$', ZendClassConstantPrettyPrinter)\\n\"\n"
1013+
".ascii \"\\n\"\n"
9621014
".ascii \"type_bit_to_name = None\\n\"\n"
9631015
".ascii \"type_name_to_bit = None\\n\"\n"
9641016
".ascii \"\\n\"\n"

scripts/gdb/php_gdb.py

+52
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ def format_string(self):
6767

6868
pp_set.add_printer('zend_string', '^_zend_string$', ZendStringPrettyPrinter)
6969

70+
def zendStringPointerPrinter(ptr):
71+
"Given a pointer to a zend_string, show the contents (if non-NULL)"
72+
if int(ptr) == 0:
73+
return '0x0'
74+
return ZendStringPrettyPrinter(ptr.dereference()).to_string()
75+
7076
class ZendTypePrettyPrinter(gdb.printing.PrettyPrinter):
7177
"Print a zend_type"
7278

@@ -289,6 +295,52 @@ def children(self):
289295

290296
pp_set.add_printer('zval', '^_zval_struct$', ZvalPrettyPrinter)
291297

298+
class ZendClassEntryPrettyPrinter(gdb.printing.PrettyPrinter):
299+
"Print a zend_class_entry"
300+
301+
# String pointers, show the string contents if possible
302+
STRING_FIELDS = [ 'name', 'doc_comment' ]
303+
304+
def __init__(self, val):
305+
self.val = val
306+
307+
def to_string(self):
308+
return zendStringPointerPrinter(self.val['name'])
309+
310+
def children(self):
311+
for field in self.val.type.fields():
312+
if field.name is not None:
313+
if field.name in self.STRING_FIELDS:
314+
yield (field.name, zendStringPointerPrinter(self.val[field.name]))
315+
else:
316+
yield (field.name, self.val[field.name])
317+
else:
318+
# Don't break on the union fields. Unfortunately, pretty
319+
# printers done in python cannot match the default formatting of
320+
# C anonymous fields, which omit the name entirely, see
321+
# binutils-gdb/gdb/cp-valprint.c#248 (as of commit
322+
# b6532accdd8e24329cc69bb58bc2883796008776)
323+
yield ('<anonymous>', self.val[field])
324+
325+
pp_set.add_printer('zend_class_entry', '^_zend_class_entry$', ZendClassEntryPrettyPrinter)
326+
327+
class ZendClassConstantPrettyPrinter(gdb.printing.PrettyPrinter):
328+
"Print a zend_class_constant"
329+
330+
def __init__(self, val):
331+
self.val = val
332+
333+
def children(self):
334+
for field in self.val.type.fields():
335+
if field.name == 'doc_comment':
336+
yield ('doc_comment', zendStringPointerPrinter(self.val['doc_comment']))
337+
elif field.name == 'ce':
338+
yield ('ce', zendStringPointerPrinter(self.val['ce']['name']))
339+
else:
340+
yield (field.name, self.val[field.name])
341+
342+
pp_set.add_printer('zend_class_constant', '^_zend_class_constant$', ZendClassConstantPrettyPrinter)
343+
292344
type_bit_to_name = None
293345
type_name_to_bit = None
294346

0 commit comments

Comments
 (0)