PostgreSQL Source Code git master
Loading...
Searching...
No Matches
crashdump.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * 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-2026, PostgreSQL Global Development Group
32 *
33 * IDENTIFICATION
34 * src/backend/port/win32/crashdump.c
35 *
36 *-------------------------------------------------------------------------
37 */
38
39#include "postgres.h"
40
41#include <dbghelp.h>
42
43/*
44 * Much of the following code is based on CodeProject and MSDN examples,
45 * particularly
46 * http://www.codeproject.com/KB/debug/postmortemdebug_standalone1.aspx
47 *
48 * Useful MSDN articles:
49 *
50 * http://msdn.microsoft.com/en-us/library/ff805116(v=VS.85).aspx
51 * http://msdn.microsoft.com/en-us/library/ms679294(VS.85).aspx
52 *
53 * Other useful articles on working with minidumps:
54 * http://www.debuginfo.com/articles/effminidumps.html
55 */
56
61);
62
63
64/*
65 * This function is the exception handler passed to SetUnhandledExceptionFilter.
66 * It's invoked only if there's an unhandled exception. The handler will use
67 * dbghelp.dll to generate a crash dump, then resume the normal unhandled
68 * exception process, which will generally exit with an error message from
69 * the runtime.
70 *
71 * This function is run under the unhandled exception handler, effectively
72 * in a crash context, so it should be careful with memory and avoid using
73 * any PostgreSQL functions.
74 */
75static LONG WINAPI
77{
78 /*
79 * We only write crash dumps if the "crashdumps" directory within the
80 * postgres data directory exists.
81 */
82 DWORD attribs = GetFileAttributesA("crashdumps");
83
85 {
86 /* 'crashdumps' exists and is a directory. Try to write a dump' */
90 char dumpPath[_MAX_PATH];
96
97 ExInfo.ThreadId = GetCurrentThreadId();
98 ExInfo.ExceptionPointers = pExceptionInfo;
99 ExInfo.ClientPointers = FALSE;
100
101 /* Load the dbghelp.dll library and functions */
102 hDll = LoadLibrary("dbghelp.dll");
103 if (hDll == NULL)
104 {
105 write_stderr("could not load dbghelp.dll, cannot write crash dump\n");
107 }
108
109 pDump = (MINIDUMPWRITEDUMP) (pg_funcptr_t) GetProcAddress(hDll, "MiniDumpWriteDump");
110
111 if (pDump == NULL)
112 {
113 write_stderr("could not load required functions in dbghelp.dll, cannot write crash dump\n");
115 }
116
117 /*
118 * Dump as much as we can, except shared memory, code segments, and
119 * memory mapped files. Exactly what we can dump depends on the
120 * version of dbghelp.dll, see:
121 * http://msdn.microsoft.com/en-us/library/ms680519(v=VS.85).aspx
122 */
125
126 if (GetProcAddress(hDll, "EnumDirTree") != NULL)
127 {
128 /* If this function exists, we have version 5.2 or newer */
131 }
132
135 "crashdumps\\postgres-pid%0i-%0i.mdmp",
136 (int) selfPid, (int) systemTicks);
137 dumpPath[_MAX_PATH - 1] = '\0';
138
141 NULL);
143 {
144 write_stderr("could not open crash dump file \"%s\" for writing: error code %lu\n",
147 }
148
150 NULL, NULL))
151 write_stderr("wrote crash dump to file \"%s\"\n", dumpPath);
152 else
153 write_stderr("could not write crash dump to file \"%s\": error code %lu\n",
155
157 }
158
160}
161
162
163void
#define write_stderr(str)
Definition parallel.c:186
void(* pg_funcptr_t)(void)
Definition c.h:548
static LONG WINAPI crashDumpHandler(struct _EXCEPTION_POINTERS *pExceptionInfo)
Definition crashdump.c:76
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:57
void pgwin32_install_crashdump_handler(void)
Definition crashdump.c:164
static void dumpType(Archive *fout, const TypeInfo *tyinfo)
Definition pg_dump.c:12217
#define snprintf
Definition port.h:260
static int fb(int x)