Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "postgres.h"
00040
00041 #define WIN32_LEAN_AND_MEAN
00042 #include <windows.h>
00043 #include <string.h>
00044 #include <dbghelp.h>
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 typedef BOOL (WINAPI * MINIDUMPWRITEDUMP) (HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType,
00061 CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
00062 CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
00063 CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam
00064 );
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 static LONG WINAPI
00079 crashDumpHandler(struct _EXCEPTION_POINTERS * pExceptionInfo)
00080 {
00081
00082
00083
00084
00085 DWORD attribs = GetFileAttributesA("crashdumps");
00086
00087 if (attribs != INVALID_FILE_ATTRIBUTES && (attribs & FILE_ATTRIBUTE_DIRECTORY))
00088 {
00089
00090 HMODULE hDll = NULL;
00091 MINIDUMPWRITEDUMP pDump = NULL;
00092 MINIDUMP_TYPE dumpType;
00093 char dumpPath[_MAX_PATH];
00094 HANDLE selfProcHandle = GetCurrentProcess();
00095 DWORD selfPid = GetProcessId(selfProcHandle);
00096 HANDLE dumpFile;
00097 DWORD systemTicks;
00098 struct _MINIDUMP_EXCEPTION_INFORMATION ExInfo;
00099
00100 ExInfo.ThreadId = GetCurrentThreadId();
00101 ExInfo.ExceptionPointers = pExceptionInfo;
00102 ExInfo.ClientPointers = FALSE;
00103
00104
00105 hDll = LoadLibrary("dbghelp.dll");
00106 if (hDll == NULL)
00107 {
00108 write_stderr("could not load dbghelp.dll, cannot write crash dump\n");
00109 return EXCEPTION_CONTINUE_SEARCH;
00110 }
00111
00112 pDump = (MINIDUMPWRITEDUMP) GetProcAddress(hDll, "MiniDumpWriteDump");
00113
00114 if (pDump == NULL)
00115 {
00116 write_stderr("could not load required functions in dbghelp.dll, cannot write crash dump\n");
00117 return EXCEPTION_CONTINUE_SEARCH;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126 dumpType = MiniDumpNormal | MiniDumpWithHandleData |
00127 MiniDumpWithDataSegs;
00128
00129 if (GetProcAddress(hDll, "EnumDirTree") != NULL)
00130 {
00131
00132 dumpType |= MiniDumpWithIndirectlyReferencedMemory |
00133 MiniDumpWithPrivateReadWriteMemory;
00134 }
00135
00136 systemTicks = GetTickCount();
00137 snprintf(dumpPath, _MAX_PATH,
00138 "crashdumps\\postgres-pid%0i-%0i.mdmp",
00139 (int) selfPid, (int) systemTicks);
00140 dumpPath[_MAX_PATH - 1] = '\0';
00141
00142 dumpFile = CreateFile(dumpPath, GENERIC_WRITE, FILE_SHARE_WRITE,
00143 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
00144 NULL);
00145 if (dumpFile == INVALID_HANDLE_VALUE)
00146 {
00147 write_stderr("could not open crash dump file \"%s\" for writing: error code %lu\n",
00148 dumpPath, GetLastError());
00149 return EXCEPTION_CONTINUE_SEARCH;
00150 }
00151
00152 if ((*pDump) (selfProcHandle, selfPid, dumpFile, dumpType, &ExInfo,
00153 NULL, NULL))
00154 write_stderr("wrote crash dump to file \"%s\"\n", dumpPath);
00155 else
00156 write_stderr("could not write crash dump to file \"%s\": error code %lu\n",
00157 dumpPath, GetLastError());
00158
00159 CloseHandle(dumpFile);
00160 }
00161
00162 return EXCEPTION_CONTINUE_SEARCH;
00163 }
00164
00165
00166 void
00167 pgwin32_install_crashdump_handler(void)
00168 {
00169 SetUnhandledExceptionFilter(crashDumpHandler);
00170 }