I wrote a script for gdb to print out the contents of my custom data structure using printf.
I can use it on gdb command line like this:
print_custom_structure <variable_name>
Now using the display command I can have buil-in data types printed after each command, like print <variable_name>
I would like to achieve the same with the output of my gdb script, unfortunately the command disp print_custom_structure <variable_name>
will return:
No symbol "print_custom_structure" in current context.
How can I achieve what I want?
Kind regards Bastian
the easiest way is to write a gdb pretty printer via a python script, which will allow you to make the gdb internal print command print your custom datatype as you want (it will also work with the display command).
save the following code to a file named "SplitPrinter.py", which is made to print a NULL-terminated array of char* (char **), and adjust it to your needs:
# Class to handle the actual printing
class SplitPrinter():
def __init__(self, val):
self.val = val
def to_string(self):
#print(self.val)
i = self.val
while (i.dereference() != 0):
print (i.dereference())
i = i + 1
# function to detect the datatype and associate it with the printer class
def register_split_printer(val):
if str(val.type)=="char **":
return SplitPrinter(val)
#remove older versions of the Printer
for printer in gdb.pretty_printers:
gdb.pretty_printers = [item for item in gdb.pretty_printers \
if not ( type(item).__name__ == "function" and item.__name__ == "register_split_printer" )]
# add new Printer to pretty printers
gdb.pretty_printers.append(register_split_printer)
You will probably want to modify the function to_string to customize the output of your structure and the function register_split_printer to detect your custom datatype.
then start gdb and load it via the command source SplitPrinter.py