PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
crashdump.c File Reference
#include "postgres.h"
#include <dbghelp.h>
Include dependency graph for crashdump.c:

Go to the source code of this file.

Macros

#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)
 

Macro Definition 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 73 of file crashdump.c.

typedef DWORD HANDLE MINIDUMP_TYPE DumpType

Definition at line 73 of file crashdump.c.

typedef DWORD dwPid

Definition at line 73 of file crashdump.c.

typedef DWORD HANDLE MINIDUMP_TYPE CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam

Definition at line 73 of file crashdump.c.

typedef DWORD HANDLE hFile

Definition at line 73 of file crashdump.c.

typedef DWORD HANDLE MINIDUMP_TYPE CONST PMINIDUMP_EXCEPTION_INFORMATION CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam

Definition at line 73 of file crashdump.c.

Function Documentation

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

Definition at line 92 of file crashdump.c.

References dumpType(), FALSE, NULL, snprintf(), and write_stderr.

Referenced by pgwin32_install_crashdump_handler().

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

Definition at line 180 of file crashdump.c.

References crashDumpHandler().

Referenced by main().

181 {
182  SetUnhandledExceptionFilter(crashDumpHandler);
183 }
static LONG WINAPI crashDumpHandler(struct _EXCEPTION_POINTERS *pExceptionInfo)
Definition: crashdump.c:92