windows 平台下 生成Dump文件例子

///
//
// MiniDump.cpp 
//
// Sample approach to collecting data with MiniDumpWriteDump and MiniDumpCallback 
// 
// Author: Oleg Starodumov (www.debuginfo.com)
//
//




///
// Include files 
//


#include <windows.h>
#include <tchar.h>
#include <dbghelp.h>
#include <stdio.h>
#include <crtdbg.h>




///
// Directives 
//


#pragma comment ( lib, "dbghelp.lib" )




///
// Function declarations 
//


void CreateMiniDump( EXCEPTION_POINTERS* pep ); 


BOOL CALLBACK MyMiniDumpCallback(
PVOID                            pParam, 
const PMINIDUMP_CALLBACK_INPUT   pInput, 
PMINIDUMP_CALLBACK_OUTPUT        pOutput 
); 




///
// Test data and code 
//


struct A 
{
int a; 


A() 
: a( 0 ) {}


void Print() 
{
_tprintf( _T("a: %d\n"), a ); 
}
};


struct B 
{
A* pA; 


B() 
: pA( 0 ) {}


void Print() 
{
_tprintf( _T("pA: %x\n"), pA ); 
pA->Print(); 
}


};


void DoWork() 
{
B* pB = new B(); // but forget to initialize B::pA 


pB->Print(); // here it should crash 
}




///
// main() function 
//


int main( int argc, char* argv[] ) 
{
__try 
{
DoWork(); 
}
__except( CreateMiniDump( GetExceptionInformation() ), EXCEPTION_EXECUTE_HANDLER ) 
{
}


return 0; 
}




///
// Minidump creation function 
//


void CreateMiniDump( EXCEPTION_POINTERS* pep ) 
{
// Open the file 


HANDLE hFile = CreateFile( _T("MiniDump.dmp"), GENERIC_READ | GENERIC_WRITE, 
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); 


if( ( hFile != NULL ) && ( hFile != INVALID_HANDLE_VALUE ) ) 
{
// Create the minidump 


MINIDUMP_EXCEPTION_INFORMATION mdei; 


mdei.ThreadId           = GetCurrentThreadId(); 
mdei.ExceptionPointers  = pep; 
mdei.ClientPointers     = FALSE; 


MINIDUMP_CALLBACK_INFORMATION mci; 


mci.CallbackRoutine     = (MINIDUMP_CALLBACK_ROUTINE)MyMiniDumpCallback; 
mci.CallbackParam       = 0; 


MINIDUMP_TYPE mdt       = (MINIDUMP_TYPE)(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory); 


BOOL rv = MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(), 
hFile, mdt, (pep != 0) ? &mdei : 0, 0, &mci ); 


if( !rv ) 
_tprintf( _T("MiniDumpWriteDump failed. Error: %u \n"), GetLastError() ); 
else 
_tprintf( _T("Minidump created.\n") ); 


// Close the file 


CloseHandle( hFile ); 


}
else 
{
_tprintf( _T("CreateFile failed. Error: %u \n"), GetLastError() ); 
}


}




///
// Custom minidump callback 
//


BOOL CALLBACK MyMiniDumpCallback(
PVOID                            pParam, 
const PMINIDUMP_CALLBACK_INPUT   pInput, 
PMINIDUMP_CALLBACK_OUTPUT        pOutput 

{
BOOL bRet = FALSE; 




// Check parameters 


if( pInput == 0 ) 
return FALSE; 


if( pOutput == 0 ) 
return FALSE; 




// Process the callbacks 


switch( pInput->CallbackType ) 
{
case IncludeModuleCallback: 
{
// Include the module into the dump 
bRet = TRUE; 
}
break; 


case IncludeThreadCallback: 
{
// Include the thread into the dump 
bRet = TRUE; 
}
break; 


case ModuleCallback: 
{
// Does the module have ModuleReferencedByMemory flag set ? 


if( !(pOutput->ModuleWriteFlags & ModuleReferencedByMemory) ) 
{
// No, it does not - exclude it 


wprintf( L"Excluding module: %s \n", pInput->Module.FullPath ); 


pOutput->ModuleWriteFlags &= (~ModuleWriteModule); 
}


bRet = TRUE; 
}
break; 


case ThreadCallback: 
{
// Include all thread information into the minidump 
bRet = TRUE;  
}
break; 


case ThreadExCallback: 
{
// Include this information 
bRet = TRUE;  
}
break; 


case MemoryCallback: 
{
// We do not include any information here -> return FALSE 
bRet = FALSE; 
}
break; 


case CancelCallback: 
break; 
}


return bRet; 


}


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

简化版例子:

#include <windows.h>
#include <tchar.h>
#include <dbghelp.h>
#include <crtdbg.h>




///
// Directives 
//


#pragma comment ( lib, "dbghelp.lib" )




///
// Function declarations 
//


void CreateMiniDump( EXCEPTION_POINTERS* pep ); 


///




int main( int argc, char* argv[] ) 
{
__try 
{
*((int*) 0) = 1;
}
__except( CreateMiniDump( GetExceptionInformation() ), EXCEPTION_EXECUTE_HANDLER ) 
{
}


getch();
return 0; 
}




///
// Minidump creation function 
//


void CreateMiniDump( EXCEPTION_POINTERS* pep ) 
{
// Open the file 


HANDLE hFile = CreateFile( _T("MiniDump.dmp"), GENERIC_READ | GENERIC_WRITE, 
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); 


if( ( hFile != NULL ) && ( hFile != INVALID_HANDLE_VALUE ) ) 
{
// Create the minidump 


MINIDUMP_EXCEPTION_INFORMATION mdei; 


mdei.ThreadId           = GetCurrentThreadId(); 
mdei.ExceptionPointers  = pep; 
mdei.ClientPointers     = FALSE; 


BOOL rv = MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(), 
hFile, MiniDumpNormal, (pep != 0) ? &mdei : NULL,  NULL, NULL ); 


if( !rv ) 
_tprintf( _T("MiniDumpWriteDump failed. Error: %u \n"), GetLastError() ); 
else 
_tprintf( _T("Minidump created.\n") ); 


// Close the file 


CloseHandle( hFile ); 


}
else 
{
_tprintf( _T("CreateFile failed. Error: %u \n"), GetLastError() ); 
}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值