Little memdump
David L.Davis
SpottedTiger at yahoo.com
Tue Mar 13 14:13:37 PDT 2007
Tom Wrote:
> Hi people,
>
> I just wanted to share with you a *very-humble* memdump procedure
> that has been very helpful to me in my tedious D-debugging nights.
>
> Kind regards,
> --
> Tom;
>
Hi Tom,
Thanks for sharing your useful code...hope you don't mind that I've replaced the 'C' printf()s with 'D' writef()s functions, and added some test data to it.
// To compile the test example: dmd memdump.d -version=memdump
// To compile: dmd MySource.d memdump.d
private import io = std.stdio;
void memdump(void* from, uint length)
{
uint plen = 0;
ubyte* adr(uint i) { return cast(ubyte*)from + i + plen; }
char chr(uint i)
{ return cast(char)*(cast(ubyte*)from + i + plen); }
ubyte byt(uint i)
{ return cast(ubyte)*(cast(ubyte*)from + i + plen); }
char c = '\0';
while (length > 0)
{
io.writef("+-----------+-----------+-----------+-----------+\n|");
for (uint i = 0; i < 16; i += 4)
io.writef("0x%0.8X |", adr(i));
io.writef("\n|");
for (uint i = 0, j = 0; i < 16; i++, j = (j + 1) % 4)
{
if (i < length)
{
c = chr(i);
if (c < 14 || c > 127)
io.writef(" ");
else
io.writef(" %s", chr(i));
}
else
io.writef(" ");
if (j == 3)
io.writef("|");
else
io.writef(" ");
}
io.writef("\n|");
for (uint i = 0, j = 0; i < 16; i++, j = (j + 1) % 4)
{
if (i < length)
io.writef("%0.2X", byt(i));
else
io.writef(" ");
if (j == 3)
io.writef("|");
else
io.writef(" ");
}
io.writef("\n");
if (length > 16)
length -= 16;
else
length = 0;
plen += 16;
}
io.writef("+-----------+-----------+-----------+-----------+\n");
}
version(memdump)
{
void main()
{
char[] s =
"This is a text message for memdump to use as test data.";
ubyte[] b = [0x4D, 0x5A, 0x60, 0x00, 0x01, 0x00, 0x00, 0x00,
0x04, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0x00, 0x00,
0xFE, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x52, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x73,
0x20, 0x57, 0x69, 0x6E, 0x33, 0x32, 0x00, 0x00];
io.writefln("dump string data...");
memdump(cast(void*)s, s.length);
io.writefln();
io.writefln("dump binary data...");
memdump(cast(void*)b, b.length);
}
}
Take Care,
David L.
More information about the Digitalmars-d
mailing list