Skip to content
Advertisement

Memory dump formatted like xxd from gdb

I’m trying to inspect a buffer which contains a binary formatted message, but also contains string data. As an example, I’m using this C code:

JavaScript

I’d like to get a hex dump of what’s in buf, of a format similar to xxd (I don’t care if it’s an exact match, what I’m really looking for is a hex dump side by side with printable chars).

Inside GDB I can use something like:

JavaScript

which is fine, but it’s hard to pick out strings that way… or I can use

JavaScript

which makes it hard to read the binary part… the actual messages I’m dealing with have plenty of ascii nul’s in them, too, so really it just looks like a mess.

The best I can come up with is to do this:

JavaScript

and then

JavaScript

but that’s a pain to do that every time. I figured somebody out there has wanted this before, so wondering if anybody has found a way to do it inside gdb. Plus you lose the addresses from the original memory this way.

I’m using GDB 7.4 with python support built in, so I’m open to the idea of using a pretty printer or similar, but I don’t know how to set that up.

Advertisement

Answer

JavaScript

Seems easy enough ;-)

You could likely write a Python script (modern GDB versions have embedded Python interpreter) to do the same, and get rid of the need to “shell out”.

Update:

Here is a possible Python implementation (save this into xxd.py):

JavaScript

Use it like so:

JavaScript
Advertisement