Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Typedefs | Functions

crashdump.c File Reference

#include "postgres.h"
#include <windows.h>
#include <string.h>
#include <dbghelp.h>
Include dependency graph for crashdump.c:

Go to the source code of this file.

Defines

#define WIN32_LEAN_AND_MEAN

Typedefs

typedef DWORD dwPid
typedef DWORD HANDLE hFile
typedef DWORD HANDLE MINIDUMP_TYPE DumpType
typedef DWORD HANDLE
MINIDUMP_TYPE CONST
PMINIDUMP_EXCEPTION_INFORMATION 
ExceptionParam
typedef DWORD HANDLE
MINIDUMP_TYPE CONST
PMINIDUMP_EXCEPTION_INFORMATION
CONST
PMINIDUMP_USER_STREAM_INFORMATION 
UserStreamParam
typedef DWORD HANDLE
MINIDUMP_TYPE CONST
PMINIDUMP_EXCEPTION_INFORMATION
CONST
PMINIDUMP_USER_STREAM_INFORMATION
CONST
PMINIDUMP_CALLBACK_INFORMATION 
CallbackParam

Functions

typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess
static LONG WINAPI crashDumpHandler (struct _EXCEPTION_POINTERS *pExceptionInfo)
void pgwin32_install_crashdump_handler (void)

Define Documentation

#define WIN32_LEAN_AND_MEAN

Definition at line 41 of file crashdump.c.


Typedef Documentation

typedef DWORD HANDLE MINIDUMP_TYPE CONST PMINIDUMP_EXCEPTION_INFORMATION CONST PMINIDUMP_USER_STREAM_INFORMATION CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam

Definition at line 60 of file crashdump.c.

typedef DWORD HANDLE MINIDUMP_TYPE DumpType

Definition at line 60 of file crashdump.c.

typedef DWORD dwPid

Definition at line 60 of file crashdump.c.

typedef DWORD HANDLE MINIDUMP_TYPE CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam

Definition at line 60 of file crashdump.c.

typedef DWORD HANDLE hFile

Definition at line 60 of file crashdump.c.

typedef DWORD HANDLE MINIDUMP_TYPE CONST PMINIDUMP_EXCEPTION_INFORMATION CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam

Definition at line 60 of file crashdump.c.


Function Documentation

typedef BOOL ( WINAPI *  MINIDUMPWRITEDUMP  ) 
static LONG WINAPI crashDumpHandler ( struct _EXCEPTION_POINTERS *  pExceptionInfo  )  [static]

Definition at line 79 of file crashdump.c.

References NULL, snprintf(), and write_stderr.

Referenced by pgwin32_install_crashdump_handler().

{
    /*
     * We only write crash dumps if the "crashdumps" directory within the
     * postgres data directory exists.
     */
    DWORD       attribs = GetFileAttributesA("crashdumps");

    if (attribs != INVALID_FILE_ATTRIBUTES && (attribs & FILE_ATTRIBUTE_DIRECTORY))
    {
        /* 'crashdumps' exists and is a directory. Try to write a dump' */
        HMODULE     hDll = NULL;
        MINIDUMPWRITEDUMP pDump = NULL;
        MINIDUMP_TYPE dumpType;
        char        dumpPath[_MAX_PATH];
        HANDLE      selfProcHandle = GetCurrentProcess();
        DWORD       selfPid = GetProcessId(selfProcHandle);
        HANDLE      dumpFile;
        DWORD       systemTicks;
        struct _MINIDUMP_EXCEPTION_INFORMATION ExInfo;

        ExInfo.ThreadId = GetCurrentThreadId();
        ExInfo.ExceptionPointers = pExceptionInfo;
        ExInfo.ClientPointers = FALSE;

        /* Load the dbghelp.dll library and functions */
        hDll = LoadLibrary("dbghelp.dll");
        if (hDll == NULL)
        {
            write_stderr("could not load dbghelp.dll, cannot write crash dump\n");
            return EXCEPTION_CONTINUE_SEARCH;
        }

        pDump = (MINIDUMPWRITEDUMP) GetProcAddress(hDll, "MiniDumpWriteDump");

        if (pDump == NULL)
        {
            write_stderr("could not load required functions in dbghelp.dll, cannot write crash dump\n");
            return EXCEPTION_CONTINUE_SEARCH;
        }

        /*
         * Dump as much as we can, except shared memory, code segments, and
         * memory mapped files. Exactly what we can dump depends on the
         * version of dbghelp.dll, see:
         * http://msdn.microsoft.com/en-us/library/ms680519(v=VS.85).aspx
         */
        dumpType = MiniDumpNormal | MiniDumpWithHandleData |
            MiniDumpWithDataSegs;

        if (GetProcAddress(hDll, "EnumDirTree") != NULL)
        {
            /* If this function exists, we have version 5.2 or newer */
            dumpType |= MiniDumpWithIndirectlyReferencedMemory |
                MiniDumpWithPrivateReadWriteMemory;
        }

        systemTicks = GetTickCount();
        snprintf(dumpPath, _MAX_PATH,
                 "crashdumps\\postgres-pid%0i-%0i.mdmp",
                 (int) selfPid, (int) systemTicks);
        dumpPath[_MAX_PATH - 1] = '\0';

        dumpFile = CreateFile(dumpPath, GENERIC_WRITE, FILE_SHARE_WRITE,
                              NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
                              NULL);
        if (dumpFile == INVALID_HANDLE_VALUE)
        {
            write_stderr("could not open crash dump file \"%s\" for writing: error code %lu\n",
                         dumpPath, GetLastError());
            return EXCEPTION_CONTINUE_SEARCH;
        }

        if ((*pDump) (selfProcHandle, selfPid, dumpFile, dumpType, &ExInfo,
                      NULL, NULL))
            write_stderr("wrote crash dump to file \"%s\"\n", dumpPath);
        else
            write_stderr("could not write crash dump to file \"%s\": error code %lu\n",
                         dumpPath, GetLastError());

        CloseHandle(dumpFile);
    }

    return EXCEPTION_CONTINUE_SEARCH;
}

void pgwin32_install_crashdump_handler ( void   ) 

Definition at line 167 of file crashdump.c.

References crashDumpHandler().

Referenced by main().

{
    SetUnhandledExceptionFilter(crashDumpHandler);
}