PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
crashdump.c File Reference
#include "postgres.h"
#include <windows.h>
#include <string.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 74 of file crashdump.c.

typedef DWORD HANDLE MINIDUMP_TYPE DumpType

Definition at line 74 of file crashdump.c.

typedef DWORD dwPid

Definition at line 74 of file crashdump.c.

typedef DWORD HANDLE MINIDUMP_TYPE CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam

Definition at line 74 of file crashdump.c.

typedef DWORD HANDLE hFile

Definition at line 74 of file crashdump.c.

typedef DWORD HANDLE MINIDUMP_TYPE CONST PMINIDUMP_EXCEPTION_INFORMATION CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam

Definition at line 74 of file crashdump.c.

Function Documentation

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

Definition at line 93 of file crashdump.c.

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

Referenced by pgwin32_install_crashdump_handler().

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

Definition at line 181 of file crashdump.c.

References crashDumpHandler().

Referenced by main().

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