Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create mini dump file programmatically without a crash?

Assume a remote site gets a rare error but doesn't crash the application. I would still like to create a mini dump file when this happens so I have some information to work with, mainly the call stack.

Pseudo code is following:

try
{
    doStuff();
}
catch(_com_error &e)
{
    make_minidump(); // is this possible?

    dump_com_error(e);
    return FALSE;
}

All the examples I see requires that I will have to cause the application to crash (for demo purpose at least) to produce dump file but I don't want to do that. Is it possible to create dump file like this?

I know I can go to task manager and create dump file of a running process and likewise I can use ProcessExplorer to achieve the same, so it seems like it should be possible.

At the same time in all examples I see, dump file is generated only when controls comes to SetUnhandledExceptionFilter which is called when an application crashes!

As a last resort, is the only way to get dump file generated is to deliberately crash the application with something like below: Will that produce anything useful beyond the crash? because I know what caused the crash in this case.

LONG CALLBACK unhandled_handler(EXCEPTION_POINTERS* e)
{
    make_minidump(e);
    return EXCEPTION_CONTINUE_SEARCH;
}

int main()
{
    SetUnhandledExceptionFilter(unhandled_handler);

    return *(int*)0;
}
like image 991
zar Avatar asked Apr 03 '17 20:04

zar


1 Answers

Yes, sure. Similar like Windows task manager can create a crash dump of a running / hanging application without any exception, you can use MiniDumpWriteDump() to create a crash dump. Just pass NULL for ExceptionParam.

Here's some code that might help:

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
                                        );

const wchar_t * DBGHELP = L"DbgHelp.dll";

bool Dump(const std::wstring & dumpFile)
{
    bool success = false;
    DllLoader loader;

    // Load dbghelp.dll. Try first to find it in the application directory.
    loader.Load(::GetModuleHandle(NULL), DBGHELP);
    if (!loader.IsLoaded())
    {
        loader.Load(DBGHELP);
    }

    if (loader.IsLoaded())
    {
        MINIDUMPWRITEDUMP pDump = MINIDUMPWRITEDUMP(loader.GetProcAddress("MiniDumpWriteDump"));

        if (pDump)
        {
            // Create dump file
            HANDLE fileHandle = ::CreateFileW(dumpFile.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, CREATE_ALWAYS,
                                              FILE_ATTRIBUTE_NORMAL, nullptr);

            if (fileHandle != INVALID_HANDLE_VALUE)
            {
                BOOL bOK = pDump(GetCurrentProcess(), GetCurrentProcessId(), fileHandle, MiniDumpWithFullMemory, nullptr, nullptr, nullptr);
                if (bOK)
                {
                    success = true;
                }

                ::CloseHandle(fileHandle);
            }
        }
    }

    return success;
}

Due to optimization, I can't see the correct stack in k, but dds ebpshow it:

0029f8d0  01302029 GetCrashWithDLL!MethodB+0x99 [f:\...\getcrashwithdll.cpp @ 12]
[...]
0029f914  0130209c GetCrashWithDLL!wmain+0x3c [f:\...\getcrashwithdll.cpp @ 31]
[...]
0029f920  01302cff GetCrashWithDLL!__tmainCRTStartup+0xfd [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c @ 623]
like image 116
Thomas Weller Avatar answered Nov 14 '22 20:11

Thomas Weller