Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a dump file for an application whenever it crashes

I am trying to create a dump file for my application whenever it crashes. I am currently using procdump.exe with -e flag in order to do it, so if I have unhandled exception in my application procdump creates a dump file for me.

I thought that I was done, but then I found out that my application crashes and procdump doesn’t create a dump file. After some investigations I have found out that invalid use of vector::front causes runtime error. I turned on the _SECURE_SCL_THROWS flag and after that procdump.exe -e did catch the crash and created a dump file.

Now to my question: Is now procdump.exe -e will always create a dump file when my application crashes? How can I guarantee that I don’t have any other scenarios where procdump -e is not good for me?

like image 377
Lior Ashkenazi Avatar asked Jan 26 '12 15:01

Lior Ashkenazi


People also ask

What is application crash dump?

Crash dump file are stored in %LOCALAPPDATA%\CrashDumps . This is a subfolder of the user profile. For user helge it resolves to C:\Users\helge\AppData\Local\CrashDumps . Note: if the crashing application runs under the SYSTEM account, that resolves to C:\Windows\System32\config\systemprofile\AppData\Local\CrashDumps .

Why would you create a dump file?

You can use a dump file from a customer's machine when you can't reproduce a crash or unresponsive program on your own machine. Testers also create dumps to save crash or unresponsive program data to use for more testing. The Visual Studio debugger can save dump files for managed or native code.


2 Answers

/* WinDump.cpp */

#ifdef WIN32

#include <windows.h>
#include <Dbghelp.h>
#include <tchar.h>


typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType,CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);

void create_minidump(struct _EXCEPTION_POINTERS* apExceptionInfo)
{
    HMODULE mhLib = ::LoadLibrary(_T("dbghelp.dll"));
    MINIDUMPWRITEDUMP pDump = (MINIDUMPWRITEDUMP)::GetProcAddress(mhLib, "MiniDumpWriteDump");

    HANDLE  hFile = ::CreateFile(_T("core.dmp"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);

    _MINIDUMP_EXCEPTION_INFORMATION ExInfo;
    ExInfo.ThreadId = ::GetCurrentThreadId();
    ExInfo.ExceptionPointers = apExceptionInfo;
    ExInfo.ClientPointers = FALSE;

    pDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL);
    ::CloseHandle(hFile);
}

LONG WINAPI unhandled_handler(struct _EXCEPTION_POINTERS* apExceptionInfo)
{
    create_minidump(apExceptionInfo);
    return EXCEPTION_CONTINUE_SEARCH;
}

#endif // WIN32

/* WinDump.h */

#ifdef WIN32

LONG WINAPI unhandled_handler(struct _EXCEPTION_POINTERS* apExceptionInfo);

#endif // WIN32

/* main.cpp */

#include "WinDump.h"

int main(int argc, char **argv)
{

    // Create a dump file whenever the gateway crashes only on windows
    SetUnhandledExceptionFilter(unhandled_handler);
    return 0;
}
like image 191
Praveen Avatar answered Sep 29 '22 09:09

Praveen


Or use Windows' built in error reporting, WER, by adding a key to registry, to enable it for your app. More details here: https://docs.microsoft.com/en-us/windows/win32/wer/collecting-user-mode-dumps

like image 38
mBardos Avatar answered Sep 29 '22 11:09

mBardos