A little but interesting memory dump tool written with c

Memory dump tool in Visual Studio 2015

Visual Studio has a memory dump tool.At the first time I use this tool,I thought it is arcane.But in the process of reading The C Programming Language(Second Edition),I found I can write a similar tool with few lines of codes.

void myputchar(char ch){
    switch(ch){
        case '\n':
            myputchar('\\');
            myputchar('n');
            break;
        case '\r':
            myputchar('\\');
            myputchar('r');
            break;            
        default:
            putchar(ch);
            break;
    }
}


void memory_dump(unsigned char* po,int len){
    for (int i = 0; i < len ; i++)
        if ( i % 16 == 15){
      
            printf("%.2X     ",*(i+po));
            for (int j = i - 15;j < i + 1;j++)
                myputchar(*(j+po)); //myputchar() is used to handle '\r' ,'\n'  in case of unexpected new line
            putchar('\n');
            }
        else if ( i % 16 == 0){
            printf("%X: ",(i+po));
            printf("%.2X ",*(i+po));
            }            
        else
            printf("%.2X ",*(i+po));
}

po is the address of the begining byte of a memory area you want to dump,len is the length of this memory area.

Here is an example of how to use this funtion:

#include <stdio.h>
#include <string.h>

void myputchar(char ch){
    switch(ch){
        case '\n':
            myputchar('\\');
            myputchar('n');
            break;
        case '\r':
            myputchar('\\');
            myputchar('r');
            break;            
        default:
            putchar(ch);
            break;
    }
}


void memory_dump(unsigned char* po,int len){
    for (int i = 0; i < len ; i++)
        if ( i % 16 == 15){
      
            printf("%.2X     ",*(i+po));
            for (int j = i - 15;j < i + 1;j++)
                myputchar(*(j+po));
            putchar('\n');
            }
        else if ( i % 16 == 0){
            printf("%X: ",(i+po));
            printf("%.2X ",*(i+po));
            }            
        else
            printf("%.2X ",*(i+po));
}


int main(){
           
    char amessage1[] = "My name is Vincent,";
    char amessage2[] = "I love the c programing language!";
    unsigned char* po1 = (unsigned char*) amessage1;
    unsigned char* po2 = (unsigned char*) amessage2;
    unsigned char* po;
    if (po1 < po2)
        po = po1;
    else
        po = po2;        
        
    memory_dump(po,192);
    
    putchar('\n');    
    
    strcat(amessage1,amessage2);
    
    memory_dump(po,192);
            
}
Result in Windows
Result in Ubuntu

In this example, the program on Windows stopped working,the program on Ubuntu worked properly.We can see the memory change after evaluating the expression strcat(amessage1,amessage2);.

In Windows,the address of string "My name is Vincent," is higher than that of string "I love the c programing language!",which is the opposite on Ubuntu.The program tends to append the string "I love the c programing language!" to the string "My name is Vincent,".So the program in Windows overrided some unknown bytes which have higher address.These bytes may contains some important imformation like the return address of the function main (I am not very sure) according to my previous article An experiment on buffer overflow .Another string function strcpy() will have the same problem.They don't check if the elements they manipulate are out of the range of the array. So we can see string functions in <string.h> like strcpy(),strcat() are not safe.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,552評(píng)論 5 6
  • 我在開(kāi)合里理解 世界 視線(xiàn)所及 時(shí)間的空洞 潛藏生命的微光 渴望光 在黑夜的衣裳上點(diǎn)綴 死亡 靜坐,在沉默里思索 ...
    車(chē)前慕閱讀 264評(píng)論 0 1
  • 大狗子今天重新出現(xiàn)在了大家的視野里,在社區(qū)活動(dòng)室打了一冬天的麻將的大狗子通紅著臉,“天暖了!里邊悶得慌。”大狗子說(shuō)...
    simon_ot閱讀 156評(píng)論 0 0
  • 所有事物的表象背后,都必然有一個(gè)造成這個(gè)表象的深刻原因。就好像每個(gè)成功男人的背后,都有個(gè)賢內(nèi)助女人。就好像每個(gè)熊孩...
    露十七閱讀 4,747評(píng)論 39 75
  • 我是一株草 我很矮小 我就長(zhǎng)在一棵大樹(shù)下面 我把大樹(shù)當(dāng)成我唯一的依靠 我是一株草 我很矮小 然而大樹(shù)卻非常繁茂 所...
    孔家月兒閱讀 282評(píng)論 0 2