PostgreSQL Source Code git master
crashdump.c File Reference
#include "postgres.h"
#include <dbghelp.h>
Include dependency graph for crashdump.c:

Go to the source code of this file.

Typedefs

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)
 

Functions

static LONG WINAPI crashDumpHandler (struct _EXCEPTION_POINTERS *pExceptionInfo)
 
void pgwin32_install_crashdump_handler (void)
 

Typedef Documentation

◆ MINIDUMPWRITEDUMP

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)

Definition at line 71 of file crashdump.c.

Function Documentation

◆ crashDumpHandler()

static LONG WINAPI crashDumpHandler ( struct _EXCEPTION_POINTERS *  pExceptionInfo)
static

Definition at line 90 of file crashdump.c.

91{
92 /*
93 * We only write crash dumps if the "crashdumps" directory within the
94 * postgres data directory exists.
95 */
96 DWORD attribs = GetFileAttributesA("crashdumps");
97
98 if (attribs != INVALID_FILE_ATTRIBUTES && (attribs & FILE_ATTRIBUTE_DIRECTORY))
99 {
100 /* 'crashdumps' exists and is a directory. Try to write a dump' */
101 HMODULE hDll = NULL;
102 MINIDUMPWRITEDUMP pDump = NULL;
103 MINIDUMP_TYPE dumpType;
104 char dumpPath[_MAX_PATH];
105 HANDLE selfProcHandle = GetCurrentProcess();
106 DWORD selfPid = GetProcessId(selfProcHandle);
107 HANDLE dumpFile;
108 DWORD systemTicks;
109 struct _MINIDUMP_EXCEPTION_INFORMATION ExInfo;
110
111 ExInfo.ThreadId = GetCurrentThreadId();
112 ExInfo.ExceptionPointers = pExceptionInfo;
113 ExInfo.ClientPointers = FALSE;
114
115 /* Load the dbghelp.dll library and functions */
116 hDll = LoadLibrary("dbghelp.dll");
117 if (hDll == NULL)
118 {
119 write_stderr("could not load dbghelp.dll, cannot write crash dump\n");
120 return EXCEPTION_CONTINUE_SEARCH;
121 }
122
123 pDump = (MINIDUMPWRITEDUMP) (pg_funcptr_t) GetProcAddress(hDll, "MiniDumpWriteDump");
124
125 if (pDump == NULL)
126 {
127 write_stderr("could not load required functions in dbghelp.dll, cannot write crash dump\n");
128 return EXCEPTION_CONTINUE_SEARCH;
129 }
130
131 /*
132 * Dump as much as we can, except shared memory, code segments, and
133 * memory mapped files. Exactly what we can dump depends on the
134 * version of dbghelp.dll, see:
135 * http://msdn.microsoft.com/en-us/library/ms680519(v=VS.85).aspx
136 */
137 dumpType = MiniDumpNormal | MiniDumpWithHandleData |
138 MiniDumpWithDataSegs;
139
140 if (GetProcAddress(hDll, "EnumDirTree") != NULL)
141 {
142 /* If this function exists, we have version 5.2 or newer */
143 dumpType |= MiniDumpWithIndirectlyReferencedMemory |
144 MiniDumpWithPrivateReadWriteMemory;
145 }
146
147 systemTicks = GetTickCount();
148 snprintf(dumpPath, _MAX_PATH,
149 "crashdumps\\postgres-pid%0i-%0i.mdmp",
150 (int) selfPid, (int) systemTicks);
151 dumpPath[_MAX_PATH - 1] = '\0';
152
153 dumpFile = CreateFile(dumpPath, GENERIC_WRITE, FILE_SHARE_WRITE,
154 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
155 NULL);
156 if (dumpFile == INVALID_HANDLE_VALUE)
157 {
158 write_stderr("could not open crash dump file \"%s\" for writing: error code %lu\n",
159 dumpPath, GetLastError());
160 return EXCEPTION_CONTINUE_SEARCH;
161 }
162
163 if ((*pDump) (selfProcHandle, selfPid, dumpFile, dumpType, &ExInfo,
164 NULL, NULL))
165 write_stderr("wrote crash dump to file \"%s\"\n", dumpPath);
166 else
167 write_stderr("could not write crash dump to file \"%s\": error code %lu\n",
168 dumpPath, GetLastError());
169
170 CloseHandle(dumpFile);
171 }
172
173 return EXCEPTION_CONTINUE_SEARCH;
174}
#define write_stderr(str)
Definition: parallel.c:186
void(* pg_funcptr_t)(void)
Definition: c.h:410
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)
Definition: crashdump.c:71
static void dumpType(Archive *fout, const TypeInfo *tyinfo)
Definition: pg_dump.c:10964
#define snprintf
Definition: port.h:238

References dumpType(), snprintf, and write_stderr.

Referenced by pgwin32_install_crashdump_handler().

◆ pgwin32_install_crashdump_handler()

void pgwin32_install_crashdump_handler ( void  )

Definition at line 178 of file crashdump.c.

179{
180 SetUnhandledExceptionFilter(crashDumpHandler);
181}
static LONG WINAPI crashDumpHandler(struct _EXCEPTION_POINTERS *pExceptionInfo)
Definition: crashdump.c:90

References crashDumpHandler().

Referenced by main().