Created
January 29, 2025 13:09
-
-
Save agvxov/db55cab112f003b7be4db6761b1790d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1. Assume the following type in C | |
| { | |
| typedef struct { | |
| int i; | |
| } hello_world_t; | |
| // and an exemplary instance | |
| hello_world_t my_hw; | |
| } | |
| >NOTE: compiling with debugging information is required (-ggdb under gcc) | |
| 2. We create a pretty printer in python | |
| {@begin=python@ | |
| # myPrettyPrinter.py | |
| class mybsPrinter: | |
| def to_string: | |
| return "Hello World" | |
| def mylookup(val): | |
| if str(val.type) == "hello_world_t": | |
| return mybsPrinter(val) | |
| return None | |
| gdb.pretty_printers.append(mylookup) | |
| @end=python@} | |
| 3. Source the pretty printer code from inside gdb like so: | |
| $ python execfile('myPrettyPrinter.py') # python 2 | |
| $ python exec(open(('myPrettyPrinter.py').read()) # python 3 | |
| or copy paste the whole thing into your .gdbinit after the keyword "python" | |
| python | |
| <...> | |
| 4. Invoke our pretty printer by printing an appropriate instance | |
| $ p my_hw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment