Skip to content
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

contrib: Add ptdrgn.py script to run interactively via ptpython #309

Merged
merged 2 commits into from
Sep 7, 2023

Conversation

brenns10
Copy link
Contributor

I finally got around to getting Drgn working with ptpython and it's pretty glorious. One particular item that's great is that it has a good multi-line code editor, so you can write multi-line loops, etc, and get automatic indentation, and you can go back and fix mistakes prior to the current line. I added in C syntax highlighting for Drgn objects + types, and I'd like to at some point get it to do completion for object members.

The actual code doing it was just going to live in ~/bin/ptdrgn, but I figured I could at least share it. It's full of hacks and implementation details, but this is the contrib directory, so maaaybe it's ok?

ptpython is a really excellent REPL library which provides great
multiline history and editing support, as well as syntax highlighting,
and a nice code completion UI.

Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
@brenns10
Copy link
Contributor Author

image

(also pictured: drgn running with CTF support)

Completion of drgn Objects *is* already possible in ptpython, but
unfortunately the list of completions is alphabetical (except for
underscored attributes, which are last). This means that default Object
fields like "absent_" and "type_" are not distinguished from the Object
member fields.

Resolve this by detecting when the first completion is "absent_" and
then moving all the non-default completions to the beginning of the
list. The result is that Object members are enumerated first, followed
by the default Object fields.

Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
@brenns10
Copy link
Contributor Author

It turns out that the Object completions were already there, but they're not easy to find since the first completion is usually Object.absent_. I added a hacky completion wrapper which reorders Object attribute completions to prioritize the ones which came from the program, rather than the default ones in drgn.

@brenns10
Copy link
Contributor Author

Here it is for a kmem_cache:
image
And here it is for list_head -- the reordering is very important here or you would not be able to see the struct members easily:
image

@osandov
Copy link
Owner

osandov commented Sep 7, 2023

Eventually we can make this less hacky with something like #319, but this is really cool and there's no reason it can't go in contrib.

@osandov osandov merged commit 535563b into osandov:main Sep 7, 2023
34 checks passed
@brenns10 brenns10 deleted the ptdrgn branch September 7, 2023 06:42
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.

2 participants