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

Allow subclassing Type/Object and registering those classes #472

Open
septatrix opened this issue Mar 6, 2025 · 2 comments
Open

Allow subclassing Type/Object and registering those classes #472

septatrix opened this issue Mar 6, 2025 · 2 comments

Comments

@septatrix
Copy link
Contributor

septatrix commented Mar 6, 2025

It would be great if one could implement custom formatters for types. There are a few scenarios where this would be helpful:

  • Data which is stored in a struct but has a better, human-readable representation. One simple example would be timestamp where unixtime or struct tm (from C++ but similar things are done in C) have more digestible/compact representations. Or converting magic values (flags, enums, etc) to their textual representation
  • Printing nested data structures or large structs quickly fills the screen making it hard to spot the necessary information. Adding custom formatters one could specify e.g. the depth, or elide some content after certain thresholds have been reached.
  • Highlight unexpected values. If one wants to get very fancy they could use ANSI colors to highlight unexpectedly high values or other things they want to be able to quickly spot. (Doing the ANSI stuff would of course not be the responsibility of this library).

Being able to register custom callback for each data type which are invoked when their string should be printed would allow more powerful visualization. Or maybe as some kind of "transform" function which takes a struct and returns a JSON such that drgn would still handle indentation and such?

Currently one could achieve a similar result by invoking such handlers manually but that is more involved for nested structures and is quite cumbersome

@septatrix
Copy link
Contributor Author

An even more powerful solution would be to register classes for types. This way one could register a subclass of drgn.Type and customize str, repr, but more importantly could also add custom methods on that type. The API could look something like this:

@drgn.register_type("struct my_custom_struct")
class MyCustomStruct(drgn.Type):
    def __str__(self, <maybe some context>) -> str:
        dt = date.fromtimestamp(self.time)
        return f"{self.id} {self.maybe_large_string[:16]} {dt.isoformat()}"

    def find_child(self, id: id) -> MyOtherCustomType: ...

    def to_graphviz_dot(self) -> str: ...

@septatrix septatrix changed the title Custom __str__ implementation for types Allow subclassing drgn.Type and registering those classes Mar 6, 2025
@septatrix
Copy link
Contributor Author

As a nice side effect this would also allow people to utilize python type hints to a greater extend instead of passing drgn.Type/drgn.Object everywhere

@septatrix septatrix changed the title Allow subclassing drgn.Type and registering those classes Allow subclassing Type/Object and registering those classes Mar 6, 2025
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

No branches or pull requests

1 participant