|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * win32_crashdump.c |
| 4 | + * Automatic crash dump creation for PostgreSQL on Windows |
| 5 | + * |
| 6 | + * The crashdump feature traps unhandled win32 exceptions produced by the |
| 7 | + * backend, and tries to produce a Windows MiniDump crash |
| 8 | + * dump for later debugging and analysis. The machine performing the dump |
| 9 | + * doesn't need any special debugging tools; the user only needs to send |
| 10 | + * the dump to somebody who has the same version of PostgreSQL and has debugging |
| 11 | + * tools. |
| 12 | + * |
| 13 | + * crashdump module originally by Craig Ringer <ringerc@ringerc.id.au> |
| 14 | + * |
| 15 | + * LIMITATIONS |
| 16 | + * =========== |
| 17 | + * This *won't* work in hard OOM situations or stack overflows. |
| 18 | + * |
| 19 | + * For those, it'd be necessary to take a much more complicated approach where |
| 20 | + * the handler switches to a new stack (if it can) and forks a helper process |
| 21 | + * to debug it self. |
| 22 | + * |
| 23 | + * POSSIBLE FUTURE WORK |
| 24 | + * ==================== |
| 25 | + * For bonus points, the crash dump format permits embedding of user-supplied |
| 26 | + * data. If there's anything else that should always be supplied with a crash |
| 27 | + * dump (postgresql.conf? Last few lines of a log file?), it could potentially |
| 28 | + * be added, though at the cost of a greater chance of the crash dump failing. |
| 29 | + * |
| 30 | + * |
| 31 | + * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group |
| 32 | + * |
| 33 | + * IDENTIFICATION |
| 34 | + * src/backend/port/win32/crashdump.c |
| 35 | + * |
| 36 | + *------------------------------------------------------------------------- |
| 37 | + */ |
| 38 | + |
| 39 | +#include "postgres.h" |
| 40 | + |
| 41 | +#define WIN32_LEAN_AND_MEAN |
| 42 | +#include <windows.h> |
| 43 | +#include <string.h> |
| 44 | +#include <dbghelp.h> |
| 45 | + |
| 46 | +/* |
| 47 | + * Much of the following code is based on CodeProject and MSDN examples, |
| 48 | + * particularly |
| 49 | + * http://www.codeproject.com/KB/debug/postmortemdebug_standalone1.aspx |
| 50 | + * |
| 51 | + * Useful MSDN articles: |
| 52 | + * |
| 53 | + * http://msdn.microsoft.com/en-us/library/ff805116(v=VS.85).aspx |
| 54 | + * http://msdn.microsoft.com/en-us/library/ms679294(VS.85).aspx |
| 55 | + * |
| 56 | + * Other useful articles on working with minidumps: |
| 57 | + * http://www.debuginfo.com/articles/effminidumps.html |
| 58 | + */ |
| 59 | + |
| 60 | +typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType, |
| 61 | + CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, |
| 62 | + CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, |
| 63 | + CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam |
| 64 | + ); |
| 65 | + |
| 66 | + |
| 67 | +/* |
| 68 | + * This function is the exception handler passed to SetUnhandledExceptionFilter. |
| 69 | + * It's invoked only if there's an unhandled exception. The handler will use |
| 70 | + * dbghelp.dll to generate a crash dump, then resume the normal unhandled |
| 71 | + * exception process, which will generally exit with an error message from |
| 72 | + * the runtime. |
| 73 | + * |
| 74 | + * This function is run under the unhandled exception handler, effectively |
| 75 | + * in a crash context, so it should be careful with memory and avoid using |
| 76 | + * any PostgreSQL functions. |
| 77 | + */ |
| 78 | +static LONG WINAPI |
| 79 | +crashDumpHandler(struct _EXCEPTION_POINTERS *pExceptionInfo) |
| 80 | +{ |
| 81 | + /* |
| 82 | + * We only write crash dumps if the "crashdumps" directory within |
| 83 | + * the postgres data directory exists. |
| 84 | + */ |
| 85 | + DWORD attribs = GetFileAttributesA("crashdumps"); |
| 86 | + if (attribs != INVALID_FILE_ATTRIBUTES && (attribs & FILE_ATTRIBUTE_DIRECTORY) ) |
| 87 | + { |
| 88 | + /* 'crashdumps' exists and is a directory. Try to write a dump' */ |
| 89 | + HMODULE hDll = NULL; |
| 90 | + MINIDUMPWRITEDUMP pDump = NULL; |
| 91 | + MINIDUMP_TYPE dumpType; |
| 92 | + char dumpPath[_MAX_PATH]; |
| 93 | + HANDLE selfProcHandle = GetCurrentProcess(); |
| 94 | + DWORD selfPid = GetProcessId(selfProcHandle); |
| 95 | + HANDLE dumpFile; |
| 96 | + DWORD systemTicks; |
| 97 | + struct _MINIDUMP_EXCEPTION_INFORMATION ExInfo; |
| 98 | + |
| 99 | + ExInfo.ThreadId = GetCurrentThreadId(); |
| 100 | + ExInfo.ExceptionPointers = pExceptionInfo; |
| 101 | + ExInfo.ClientPointers = FALSE; |
| 102 | + |
| 103 | + /* Load the dbghelp.dll library and functions */ |
| 104 | + hDll = LoadLibrary("dbghelp.dll"); |
| 105 | + if (hDll == NULL) |
| 106 | + { |
| 107 | + write_stderr("could not load dbghelp.dll, cannot write crashdump\n"); |
| 108 | + return EXCEPTION_CONTINUE_SEARCH; |
| 109 | + } |
| 110 | + |
| 111 | + pDump = (MINIDUMPWRITEDUMP)GetProcAddress(hDll, "MiniDumpWriteDump"); |
| 112 | + |
| 113 | + if (pDump==NULL) |
| 114 | + { |
| 115 | + write_stderr("could not load required functions in dbghelp.dll, cannot write crashdump\n"); |
| 116 | + return EXCEPTION_CONTINUE_SEARCH; |
| 117 | + } |
| 118 | + |
| 119 | + /* |
| 120 | + * Dump as much as we can, except shared memory, code segments, |
| 121 | + * and memory mapped files. |
| 122 | + * Exactly what we can dump depends on the version of dbghelp.dll, |
| 123 | + * see: |
| 124 | + * http://msdn.microsoft.com/en-us/library/ms680519(v=VS.85).aspx |
| 125 | + */ |
| 126 | + dumpType = MiniDumpNormal | MiniDumpWithHandleData | |
| 127 | + MiniDumpWithDataSegs; |
| 128 | + |
| 129 | + if (GetProcAddress(hDll, "EnumDirTree") != NULL) |
| 130 | + { |
| 131 | + /* If this function exists, we have version 5.2 or newer */ |
| 132 | + dumpType |= MiniDumpWithIndirectlyReferencedMemory | |
| 133 | + MiniDumpWithPrivateReadWriteMemory; |
| 134 | + } |
| 135 | + if (GetProcAddress(hDll, "SymFromIndex") != NULL) |
| 136 | + { |
| 137 | + /* If this function exists, we have version 6.2 or newer */ |
| 138 | + dumpType |= MiniDumpWithThreadInfo; |
| 139 | + } |
| 140 | + |
| 141 | + systemTicks = GetTickCount(); |
| 142 | + snprintf(dumpPath, _MAX_PATH, |
| 143 | + "crashdumps\\postgres-pid%0i-%0i.mdmp", selfPid, systemTicks); |
| 144 | + dumpPath[_MAX_PATH-1] = '\0'; |
| 145 | + |
| 146 | + dumpFile = CreateFile(dumpPath, GENERIC_WRITE, FILE_SHARE_WRITE, |
| 147 | + NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, |
| 148 | + NULL); |
| 149 | + if (dumpFile==INVALID_HANDLE_VALUE) |
| 150 | + { |
| 151 | + write_stderr("could not open crash dump file %s for writing: error code %d\n", |
| 152 | + dumpPath, GetLastError()); |
| 153 | + return EXCEPTION_CONTINUE_SEARCH; |
| 154 | + } |
| 155 | + |
| 156 | + if ((*pDump)(selfProcHandle, selfPid, dumpFile, dumpType, &ExInfo, |
| 157 | + NULL, NULL)) |
| 158 | + write_stderr("wrote crash dump to %s\n", dumpPath); |
| 159 | + else |
| 160 | + write_stderr("could not write crash dump to %s: error code %08x\n", |
| 161 | + dumpPath, GetLastError()); |
| 162 | + |
| 163 | + CloseHandle(dumpFile); |
| 164 | + } |
| 165 | + |
| 166 | + return EXCEPTION_CONTINUE_SEARCH; |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | +void |
| 171 | +pgwin32_install_crashdump_handler(void) |
| 172 | +{ |
| 173 | + SetUnhandledExceptionFilter(crashDumpHandler); |
| 174 | +} |
0 commit comments