windows下生成core dump檔案
阿新 • • 發佈:2019-02-14
http://blog.csdn.net/xiarendeniao/article/details/7306282
下面是從pandion裡面摘取的兩個檔案
MiniDumper.h
- #ifndef MINIDUMPER_H
- #define MINIDUMPER_H
- #include <windows.h>
- class CMiniDumper
- {
- public:
- CMiniDumper(bool bPromptUserForMiniDump);
- ~CMiniDumper(void);
-
private
- staticLONG WINAPI unhandledExceptionHandler(struct_EXCEPTION_POINTERS *pExceptionInfo);
- void setMiniDumpFileName(void);
- bool getImpersonationToken(HANDLE* phToken);
- BOOL enablePrivilege(LPCTSTR pszPriv, HANDLE hToken, TOKEN_PRIVILEGES* ptpOld);
-
BOOL restorePrivilege(
- LONG writeMiniDump(_EXCEPTION_POINTERS *pExceptionInfo );
- _EXCEPTION_POINTERS *m_pExceptionInfo;
- TCHAR m_szMiniDumpPath[MAX_PATH];
- TCHAR m_szAppPath[MAX_PATH];
- TCHAR m_szAppBaseName[MAX_PATH];
-
bool m_bPromptUserForMiniDump;
- static CMiniDumper* s_pMiniDumper;
- static LPCRITICAL_SECTION s_pCriticalSection;
- };
- #endif // MINIDUMPER_H
MiniDumper.cpp
- #include <windows.h>
- #include <stdio.h>
- #include <assert.h>
- #include <time.h>
- #include <stdlib.h>
- #include <strsafe.h>
- //#include <tchar.h>
- #include <dbghelp.h>
- #include "miniDumper.h"
- #ifdef UNICODE
- #define _tcssprintf wsprintf
- #define tcsplitpath _wsplitpath
- #else
- #define _tcssprintf sprintf
- #define tcsplitpath _splitpath
- #endif
- constint USER_DATA_BUFFER_SIZE = 4096;
- //-----------------------------------------------------------------------------
- // GLOBALS
- //-----------------------------------------------------------------------------
- CMiniDumper* CMiniDumper::s_pMiniDumper = NULL;
- LPCRITICAL_SECTION CMiniDumper::s_pCriticalSection = NULL;
- // Based on dbghelp.h
- typedefBOOL (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);
- //-----------------------------------------------------------------------------
- // Name: CMiniDumper()
- // Desc: Constructor
- //-----------------------------------------------------------------------------
- CMiniDumper::CMiniDumper( bool bPromptUserForMiniDump )
- {
- // Our CMiniDumper should act alone as a singleton.
- assert( !s_pMiniDumper );
- s_pMiniDumper = this;
- m_bPromptUserForMiniDump = bPromptUserForMiniDump;
- // The SetUnhandledExceptionFilter function enables an application to
- // supersede the top-level exception handler of each thread and process.
- // After calling this function, if an exception occurs in a process
- // that is not being debugged, and the exception makes it to the
- // unhandled exception filter, that filter will call the exception
- // filter function specified by the lpTopLevelExceptionFilter parameter.
- ::SetUnhandledExceptionFilter( unhandledExceptionHandler );
- // Since DBGHELP.dll is not inherently thread-safe, making calls into it
- // from more than one thread simultaneously may yield undefined behavior.
- // This means that if your application has multiple threads, or is
- // called by multiple threads in a non-synchronized manner, you need to
- // make sure that all calls into DBGHELP.dll are isolated via a global
- // critical section.
- s_pCriticalSection = new CRITICAL_SECTION;
- if( s_pCriticalSection )
- InitializeCriticalSection( s_pCriticalSection );
- }
- //-----------------------------------------------------------------------------
- // Name: ~CMiniDumper()
- // Desc: Destructor
- //-----------------------------------------------------------------------------
- CMiniDumper::~CMiniDumper( void )
- {
- if( s_pCriticalSection )
- {
- DeleteCriticalSection( s_pCriticalSection );
- delete s_pCriticalSection;
- }
- }
- //-----------------------------------------------------------------------------
- // Name: unhandledExceptionHandler()
- // Desc: Call-back filter function for unhandled exceptions
- //-----------------------------------------------------------------------------
- LONG CMiniDumper::unhandledExceptionHandler( _EXCEPTION_POINTERS *pExceptionInfo )
- {
- if( !s_pMiniDumper )
- return EXCEPTION_CONTINUE_SEARCH;
- return s_pMiniDumper->writeMiniDump( pExceptionInfo );
- }
- //-----------------------------------------------------------------------------
- // Name: setMiniDumpFileName()
- // Desc:
- //-----------------------------------------------------------------------------
- void CMiniDumper::setMiniDumpFileName( void )
- {
- time_t currentTime;
- time( ¤tTime );
- wsprintf( m_szMiniDumpPath,
- L"%s%s.%ld.dmp",
- m_szAppPath,
- m_szAppBase