-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test program for explaining gdb's "pretty print" feature.
- Loading branch information
Showing
6 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
hellopp | ||
core* |
This file contains 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
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) | ||
|
||
|
This file contains 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
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 | ||
|
This file contains 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
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) | ||
|
||
|
This file contains 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
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. | ||
|
||
|
This file contains 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
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; | ||
} | ||
|