I actually have some code which may do what you want. It uses the preprocessor to stringize the variable name to allow you to print it out. It dumps both the variable name and value (based on the type) and the memory layout for that variable. The following program shows how it's done:
#include <stdio.h>
#include <stdlib.h>
static void dumpMem (unsigned char *p, unsigned int s) {
int i;
unsigned char c[0x10];
printf (">> ");
for (i = 0; i < 0x10; i++) printf (" +%x",i);
printf (" +");
for (i = 0; i < 0x10; i++) printf ("%x",i);
printf ("\n");
for (i = 0; i < ((s + 15) & 0xfff0); i++) {
if ((i % 0x10) == 0) {
if (i != 0) printf (" %*.*s\n", 0x10, 0x10, c);
printf (">> %04x ",i);
}
if (i < s) {
printf (" %02x", p[i]);
c[i & 0xf] = ((p[i] < 0x20) || (p[i] > 0x7e)) ? '.' : p[i];
} else {
printf (" ");
c[i & 0xf] = ' ';
}
}
printf (" %*.*s\n", 0x10, 0x10, c);
}
#define DUMPINT(x) do{printf("%s: %d\n",#x,x);dumpMem((char*)(&x),sizeof(int));}while(0)
#define DUMPSTR(x) do{printf("%s: %s\n",#x,x);dumpMem(x,strlen(x));}while(0)
#define DUMPMEM(x,s) do{printf("%s:\n",#x);dumpMem((char*)(&x),s);}while(0)
typedef struct {
char c;
int i;
char c2[6];
} tStruct;
int main (void) {
int i = 42;
char *s = "Hello there, my name is Pax!";
tStruct z;
z.c = 'a'; z.i = 42; strcpy (z.c2,"Hello");
DUMPINT (i);
DUMPSTR (s);
DUMPMEM (z,sizeof(z));
return 0;
}
This outputs:
i: 42
>> +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f +0123456789abcdef
>> 0000 2a 00 00 00 *...
s: Hello there, my name is Pax!
>> +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f +0123456789abcdef
>> 0000 48 65 6c 6c 6f 20 74 68 65 72 65 2c 20 6d 79 20 Hello there, my
>> 0010 6e 61 6d 65 20 69 73 20 50 61 78 21 name is Pax!
z:
>> +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f +0123456789abcdef
>> 0000 61 b6 16 61 2a 00 00 00 48 65 6c 6c 6f 00 0d 61 a..a*...Hello..a
And, if you're wondering about the sanity of do {...} while (0)
in the macros, that's to enable it to be placed anywhere in the code without having to worry about whether you have enough braces surrounding it.