How to write a sample code that will crash and produce dump file?

后端 未结 9 1418
礼貌的吻别
礼貌的吻别 2020-12-12 23:15

I started learned windbg and I found this good post How to use WinDbg to analyze the crash dump for VC++ application?

Now I want to follow the instructions and do it

9条回答
  •  庸人自扰
    2020-12-12 23:32

    I used the code below when testing out WinDbg some time ago.

    • The code below works and will generate a crash dump
    • There are two functions so that you can see a stack trace with an obvious chain of functions.
    • To find the crash dumps, search for *.dmp or *.mdmp in C:\Users
    • It's probably best to let the OS generate the dump for you. This is probably how most of the real crash dumps you see will be generated.
    • The code works by first allocating 1 KiB of memory, then writing both it and the following 1 KiB with a recognizable hexadecimal value. This usually hits a page of memory marked by the OS as non-writeable, which will trigger the crash.

    #include "stdafx.h"
    #include "stdio.h"
    #include "malloc.h"
    
    void Function2(int * ptr2)
    {
        for(int i=0; i < (2 * 1024); i++)
        {
            *ptr2++ = 0xCAFECAFE;
        }
    }
    
    void Function1()
    {
        int * ptr1 = (int *)malloc(1024 * sizeof(int));
    
        Function2(ptr1);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        printf("Press enter to allocate and corrupt.\r\n");
        getc(stdin);
    
        printf("Allocating and corrupting...\r\n");
        Function1();
    
        printf("Done.  Press enter to exit process.\r\n");
        getc(stdin);
    
        return 0;
    }
    

提交回复
热议问题