Skip to content

Add GDB pretty-printers for zend_string, zend_type #13463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2024

Conversation

arnaud-lb
Copy link
Member

@arnaud-lb arnaud-lb commented Feb 21, 2024

This adds GDB pretty-printers for zend_string, zend_type.

This can be used by loading .gdb.py with

(gdb) source .gdb.py

and then printing any zend_string or zend_type.

(gdb) p op_array.function_name
$1 = (zend_string *) 0x7ffff7a01900

(gdb) p *op_array.function_name
$2 = "test" = {gc = {refcount = 4, u = {type_info = 22}}, h = 9223372043240499301, len = 4, val = "test"}

(gdb) p arg_infos->type
$3 = [union,arena] "Foo"|"Bar" = {ptr = 0x7ffff7a02108, type_mask = 5505024}

Note that this activates when printing values, and not pointers, so we need to dereference pointers (e.g. p *str).

I initially tried to implement a GDB macro for printing types in .gdbinit, but the lack of variable scope makes it difficult.

Printing zend_string was already possible with the print_zstr macro.

Borrowed some ideas from https://github.com/chromium/chromium/blob/8cd1e07328b9f30fb369f40d754ce3c4e7eeeb0c/tools/gdb/gdb_chrome.py.

@Girgias
Copy link
Member

Girgias commented Feb 21, 2024

Thank you! One small question, does this work to print DNF types?

@arnaud-lb
Copy link
Member Author

Yes it does. Here is what it looks like for null|A|(B&C):

(gdb) p arg_infos->type
$4 = [union,arena] null|"A"|([intersection,arena] "B"&"C") = {ptr = 0x7ffff7a02130, type_mask = 5505026}

@arnaud-lb arnaud-lb marked this pull request as ready for review February 22, 2024 12:34
@iluuu1994
Copy link
Member

Thank you! Would be nice to add support for ASTs and op_arrays at some point.

@arnaud-lb
Copy link
Member Author

That would be super useful indeed. I've added one for zend_ast here: #13520

@bwoebi
Copy link
Member

bwoebi commented Feb 27, 2024

Probably redundant to print the string twice for zend_string (once at start and once for val)
Otherwise, nice addition :-)

Also question: Can this sourcing be added directly to .gdbinit, so that sourcing the .gdbinit also loads this python file? (I don't know anything about the details of this)

@arnaud-lb
Copy link
Member Author

arnaud-lb commented Mar 5, 2024

I've updated the string pretty printer to not print the string twice.

Can this sourcing be added directly to .gdbinit, so that sourcing the .gdbinit also loads this python file?

We can add source .gdb.py in .gdbinit, but this will only work when the script is either in the current directory, or in directories except that $cdir is ignored. I didn't found any way to search relatively to the .gdbinit file.

Python autoloading searches in the same way.

The only reliable way to load .gdb.py by default may be to embed it in the php binary as described in the second section of the python autoloading page.

@bwoebi
Copy link
Member

bwoebi commented Mar 5, 2024

Embedding .gdbinit / .gdb.py sounds very interesting to me. The files are relatively small ... and it saves me to always either manually load it (to the point that it annoys me) or set it up in the env ... which is annoying if these are various production envs sometimes.

@arnaud-lb
Copy link
Member Author

arnaud-lb commented Mar 5, 2024

It will look into this, then :) (in a separate PR)

@arnaud-lb
Copy link
Member Author

I'm going to merge this in a few days unless there are objections

@arnaud-lb arnaud-lb merged commit 4058082 into php:master Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants