Skip to content

Commit

Permalink
[pcustom] allow custom color for the command via settings
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Apr 9, 2021
1 parent 3102749 commit d554c4a
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -4886,24 +4886,30 @@ def __init__(self):
super().__init__(prefix=True)
self.add_setting("struct_path", os.sep.join([get_gef_setting("gef.tempdir"), "structs"]), "Path to store/load the structure ctypes files")
self.add_setting("max_depth", 4, "Maximum level of recursion supported")
self.add_setting("structure_name", "bold blue", "Color of the structure name")
self.add_setting("structure_type", "bold red", "Color of the attribute type")
self.add_setting("structure_size", "green", "Color of the attribute size")
return

@only_if_gdb_running

def do_invoke(self, argv):
argc = len(argv)
if argc == 0:
self.usage()
gdb.execute("pcustom list")
return

modname, structname = self.get_modulename_structname_from_arg(argv[0])

try:
address = int(gdb.parse_and_eval(argv[1]))
except gdb.error:
err("Failed to parse '{:s}'".format(argv[1]))
return
if argc == 1:
gdb.execute("pcustom show {}".format(structname))
else:
try:
address = int(gdb.parse_and_eval(argv[1]))
except gdb.error:
err("Failed to parse '{:s}'".format(argv[1]))
return

self.apply_structure_to_address(modname, structname, address)
self.apply_structure_to_address(modname, structname, address)
return

def get_pcustom_absolute_root_path(self):
Expand Down Expand Up @@ -4950,6 +4956,8 @@ def get_structure_class(self, modname, classname):
_class = getattr(_mod, classname)
return _class, _class()


@only_if_gdb_running
def apply_structure_to_address(self, mod_name, struct_name, addr, depth=0):
if not self.is_valid_struct(mod_name):
err("Invalid structure name '{:s}'".format(struct_name))
Expand Down Expand Up @@ -5077,9 +5085,11 @@ def __list_custom_structures(self):
path = self.get_pcustom_absolute_root_path()
info("Listing custom structures from '{:s}'".format(path))
structures = self.enumerate_structures()
struct_color = get_gef_setting("pcustom.structure_type")
filename_color = get_gef_setting("pcustom.structure_name")
for filename in structures:
__modules = ", ".join([Color.greenify(x) for x in structures[filename]])
__filename = Color.blueify(filename)
__modules = ", ".join([Color.colorify(x, struct_color) for x in structures[filename]])
__filename = Color.colorify(filename, filename_color)
gef_print("{:s} {:s} ({:s})".format(RIGHT_ARROW, __filename, __modules))
return

Expand Down Expand Up @@ -5124,11 +5134,10 @@ def __dump_custom_structure(self, mod_name, struct_name):
_class, _struct = self.get_structure_class(mod_name, struct_name)

for _name, _type in _struct._fields_:
# todo: use theme to get the colors
_size = ctypes.sizeof(_type)
__name = Color.blueify(_name)
__type = Color.redify(_type.__name__)
__size = Color.greenify(hex(_size))
__name = Color.colorify(_name, get_gef_setting("pcustom.structure_name"))
__type = Color.colorify(_type.__name__, get_gef_setting("pcustom.structure_type"))
__size = Color.colorify(hex(_size), get_gef_setting("pcustom.structure_size"))
__offset = Color.boldify("{:04x}".format(getattr(_class, _name).offset))
gef_print("{:s} {:32s} {:16s} /* size={:s} */".format(__offset, __name, __type, __size))
return
Expand Down

0 comments on commit d554c4a

Please sign in to comment.