Skip to content

Commit

Permalink
Test program for explaining gdb's "pretty print" feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
epasveer committed Nov 23, 2023
1 parent 1a244d1 commit c595f0c
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/hellopp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hellopp
core*
42 changes: 42 additions & 0 deletions tests/hellopp/LocationStruct_pp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import gdb

# The PrettyPrinter object.
class LocationPrinter:

# Save a copy of the Location.
def __init__(self, val):
self.__val = val

# Convert it to a string. gdb will call this function.
def to_string(self):
ret = ""

# Construct a python string with the values of the Location struct.
# It's good to handle any errors. Hence the try/catch.
try:
ret = "Location=(" + str(self.__val['city']) + "," + str(self.__val['state']) + "," + str(self.__val['zip']) + "," + str(self.__val['cell']) + ")"

except Exception as e:
# Any exception will be saved in the return string.
ret = str(e)

return ret

# A hint for gdb on the return type.
def display_hint (self):
return 'string'


# Specify a function to detect a Location struct type and will call the PrettyPrinter object for it.
#
# gdb maintains a large list of PrettyPrinters for lots of data/class types. It will go through the
# list looking for one that matches the type. If one is found, it uses it. Otherwise, gdb will
# default to the regular way of printing data types.
def LocationPrinter_func(val):
if str(val.type) == 'Location':
return LocationPrinter(val)

# Add the function to gdb's list of PrettyPrinter functions.
gdb.pretty_printers.append(LocationPrinter_func)


10 changes: 10 additions & 0 deletions tests/hellopp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: all
all: hellopp

hellopp: hellopp.cpp
g++ -g -o hellopp hellopp.cpp

.PHONY: clean
clean:
rm -f hellopp hellopp.o

42 changes: 42 additions & 0 deletions tests/hellopp/PersonStruct_pp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import gdb

# The PrettyPrinter object.
class PersonPrinter:

# Save a copy of the Person.
def __init__(self, val):
self.__val = val

# Convert it to a string. gdb will call this function.
def to_string(self):
ret = ""

# Construct a python string with the values of the Person struct.
# It's good to handle any errors. Hence the try/catch.
try:
ret = "Person=(" + str(self.__val['name']) + "," + str(self.__val['age']) + "," + str(self.__val['salary']) + "," + str(self.__val['location']) + ")"

except Exception as e:
# Any exception will be saved in the return string.
ret = str(e)

return ret

# A hint for gdb on the return type.
def display_hint (self):
return 'string'


# Specify a function to detect a Person struct type and will call the PrettyPrinter object for it.
#
# gdb maintains a large list of PrettyPrinters for lots of data/class types. It will go through the
# list looking for one that matches the type. If one is found, it uses it. Otherwise, gdb will
# default to the regular way of printing data types.
def PersonPrinter_func(val):
if str(val.type) == 'Person':
return PersonPrinter(val)

# Add the function to gdb's list of PrettyPrinter functions.
gdb.pretty_printers.append(PersonPrinter_func)


7 changes: 7 additions & 0 deletions tests/hellopp/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

Simple program to show gdb's "pretty print" feature on custom data types.
In this case, two structs. One struct contains the second.

Gdb's "pretty print" will use the custom printers instead of gdb's default.


67 changes: 67 additions & 0 deletions tests/hellopp/hellopp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <string>
#include <iostream>
#include <string.h>


struct Cell {
unsigned int number;
};

struct Location {
std::string city;
std::string state;
int zip;
struct Cell* cell;
};

struct Person {
std::string name;
int age;
float salary;
struct Location location;
};

void print(const Person& person, const Location& where) {

std::cout << "'" << person.name << "' from '" << where.city << "'." << std::endl;
std::cout << "'" << person.name << "' from '" << where.city << "'." << std::endl;
std::cout << "'" << person.name << "' from '" << where.city << "'." << std::endl;
std::cout << "'" << person.name << "' from '" << where.city << "'." << std::endl;
std::cout << "'" << person.name << "' from '" << where.city << "'." << std::endl;
std::cout << "'" << person.name << "' from '" << where.city << "'." << std::endl;
}


int main (int argc, char** argv) {

Person me;
Location where;

me.name = "Pasveer, Ernie";
me.age = 60;
me.salary = 0.25;
me.location.city = "Houston";
me.location.state = "Texas";
me.location.zip = 77063;
me.location.cell = (Cell*)malloc(sizeof(Cell));
me.location.cell->number = 2226669999;

where.city = "Houston";
where.state = "Texas";
where.zip = 77063;
where.cell = (Cell*)malloc(sizeof(Cell));
where.cell->number = 2226669999;

std::cout << "'" << me.name << "', from '" << me.location.city << "', is " << me.age << " years old and makes " << me.salary << " per year." << std::endl;

print(me, where);

free(me.location.cell);
free(where.cell);

me.location.cell = 0;
where.cell = 0;

return 0;
}

0 comments on commit c595f0c

Please sign in to comment.