Inspect an image in memory
This article shows how to visualize a data buffer as an image, while debugging the program, without writing a debugging function into the program and without recompiling it.
For this purpose we will use the following simple c code:
Sample code
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
int main()
{
// image width
int w = 0;
// imege height
int h = 0;
// number of channels
int c = 0;
// image data
BYTE* img = stbi_load("Lenna.png", &w, &h, &c, 0);
// release image data
stbi_image_free(img);
return 0;
}
In order to visualize the contents of img
byte buffer above, we are going
to dump its contents to a file and open it with IrfanView
.
dumpmem
Dump memory utility.
------------------------------------------------------------------
Syntax: <dumpmem> <optional flags> <filename> <address> <size>
EX: dumpmem c:\memdump.bin 0x00656789 200
EX: dumpmem -f c:\memdump.bin 0x00656789 200
Flags:
-f - Force file overwrite.
-a - Append to the file.
<filename> - output filename
<address> - read address, must be a hex address / pointer, can be an expression
<size> - size in bytes, can be an expression
------------------------------------------------------------------
Steps to write the image to a file and load it in the viewer:
- Start debugging the program.
- Place a breakpoint at a carefully select point in code, where our data is available.
- Go to VSDebugPro menu and click Settings.
- Specifiy a path to an image viewer that can open raw files. Click OK.
- Go to VSDebugPro menu and open Console.
- Dump image data with the following command:
dumpmem lenna.raw img w * h * c
- Click on the file link in
VSD Console
to open the image in viewer.
The dumpmem utility uses the Visual Studio C++ debugger interface to automatically evaluate
the address of img
and the size from w * h * c
expression.
References
HxD a free hex editor.
IrfanView freeware for non-commercial use image viewer.
Stb Image public domain image loader single file library.