Created
April 24, 2012 13:29
-
-
Save lutoma/2479667 to your computer and use it in GitHub Desktop.
Xelix memory inspector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdbool.h> | |
#include <string.h> | |
#define BUFFERSIZE 256 | |
// Should be a multiple of 4 | |
#define COLUMN 4 * 3 | |
#define is_digit(C) ((C) >= '0' && (C) <= '9') | |
uint64_t atoi(const char* s) { | |
uint64_t n = 0; | |
while(*s != 0) | |
{ | |
if(!is_digit(*s)) | |
return -1; | |
n = 10 * n + *s++ - '0'; | |
} | |
return n; | |
} | |
int main() | |
{ | |
printf( \ | |
"Memory inspector for Xelix.\n" \ | |
"Please enter a pointer (In decimal format) or 'quit' to quit.\n" \ | |
); | |
char* buffer = malloc(BUFFERSIZE); | |
while(true) | |
{ | |
printf("meminsp> "); | |
memset(buffer, 0, 256); | |
fgets(buffer, 256, stdin); | |
if(buffer[0] == '\n') | |
continue; | |
// Cut off the \n | |
buffer[strlen(buffer) - 1] = 0; | |
if(!strcmp(buffer, "quit")) | |
break; | |
uint64_t pointer = atoi(buffer); | |
if(pointer == -1) | |
{ | |
printf("Sorry, but that doesn't look like a valid pointer (Hexadecimal doesn't work!).\n"); | |
continue; | |
} | |
printf("Showing memory at 0x%x (%d)\n", pointer, pointer); | |
for(int i = 0; i < 23; i++) | |
{ | |
printf("0x%x\t", pointer + i * COLUMN); | |
for(int j = 0; j < COLUMN; j += 4) | |
{ | |
uint32_t c = *((uint32_t*)pointer + i * COLUMN + j); | |
//printf("%d\t", (uint32_t*)pointer + i * COLUMN + j); | |
int padding = 10 - intlen(c); | |
if(padding < 0) padding = 0; | |
//printf("p: %d\n", padding); | |
for(int k = 0; k < padding; k++) | |
printf("0"); | |
printf("%d", c); | |
printf(" "); | |
} | |
printf("\t"); | |
for(int j = 0; j < COLUMN; j++) | |
{ | |
char c = *((char*)pointer + i * COLUMN + j); | |
// All printable ascii chars are between ' ' (32) and '~' (126). | |
if(c >= ' ' && c <= '~') | |
// Show printable chars in white (versus gray for normal text) | |
printf("\e[0;37m%c\e[0m", c); | |
else | |
printf("."); | |
} | |
printf("\n"); | |
} | |
} | |
}% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment