A better hex dump for gdb

Ángel Ortega

Let's say it clear: gdb lacks an acceptable hexadecimal view for memory blocks. Commands like x/32c get near, but it shows a confusing, misaligned mix of ASCII characters and octal garbage. I wish it was something like the x command in lldb.

BUT: you can get that (or at least something very similar) by defining a new command using an external call to the hd external tool, that is probably already installed on your system.

The magic is done by adding the following snippet to your ~/.gdbinit config file:

define hd
  dump binary memory dump.bin $arg0 ((void *)$arg0)+128
  shell hexdump -C dump.bin
  shell rm dump.bin

Now you have a new hd command that accepts as its unique argument a pointer to your data and shows 128 bytes of pretty hexadecimal dump.

2023-08-20 Update: rewritten to make it compatible with OpenBSD, that doesn't have the hd program and which gdb lacks the eval command (or has it disabled, I don't know nor care).