📄 myexceptiondump.h
字号:
//MyExceptionDump.h
#ifndef _MyExceptionDump_
#define _MyExceptionDump_
//#ifndef PULONG_PTR
typedef PULONG* PULONG_PTR;
//#endif
//#ifndef ULONG_PTR
typedef ULONG* ULONG_PTR;
//#endif
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#include <windef.h>
#include "dbghelp.h"
#pragma comment(lib,"dbghelp.lib")
#define BUFFERSIZE 1024
// Unicode safe char* -> TCHAR* conversion
void PCSTR2LPTSTR( PCSTR lpszIn, LPTSTR lpszOut )
{
#if defined(UNICODE)||defined(_UNICODE)
ULONG index = 0;
PCSTR lpAct = lpszIn;
for( ; ; lpAct++ )
{
lpszOut[index++] = (TCHAR)(*lpAct);
if ( *lpAct == 0 )
break;
}
#else
// This is trivial :)
strcpy( lpszOut, lpszIn );
#endif
}
// Let's figure out the path for the symbol files
// Search path= ".;<CurrentExe>;<CurrentExe\pdb>;%_NT_SYMBOL_PATH%;%_NT_ALTERNATE_SYMBOL_PATH%;%SYSTEMROOT%;%SYSTEMROOT%\System32;" + lpszInitialSymbolPath
// Note: There is no size check for lpszSymbolPath!
static void InitSymbolPath(
PSTR lpszSymbolPath,
DWORD dwSymbolPathSize,
PCSTR lpszInitialSymbolPath
)
{
memset(lpszSymbolPath,0,dwSymbolPathSize);
CHAR lpszPath[BUFFERSIZE]={0,};
// ".;<CurrentExe>;%_NT_SYMBOL_PATH%;%_NT_ALTERNATE_SYMBOL_PATH%;%SYSTEMROOT%;%SYSTEMROOT%\System32;"
strcpy( lpszSymbolPath, ".;");
CHAR* pchToken=0;
/*
//<CurrentModue>
CHAR szMod[MAX_PATH]={0,};
::GetModuleFileName(ModuleFromAddress(InitSymbolPath),szMod,sizeof(szMod));
pchToken=strrchr(szMod,'\\');
if(pchToken)
{
*pchToken='\0';
}
strncat( lpszSymbolPath, ";",dwSymbolPathSize-strlen(lpszSymbolPath));
strncat( lpszSymbolPath, szMod,dwSymbolPathSize-strlen(lpszSymbolPath));
*/
//<CurrentExe dir>
CHAR szExeMod[MAX_PATH]={0,};
::GetModuleFileName(0,szExeMod,sizeof(szExeMod));
pchToken=strrchr(szExeMod,'\\');
if(pchToken)
{
*pchToken='\0';
}
strncat( lpszSymbolPath, ";",dwSymbolPathSize-strlen(lpszSymbolPath) );
strncat( lpszSymbolPath, szExeMod,dwSymbolPathSize-strlen(lpszSymbolPath) );
//<CurrentExe dir\pdb>
strncat( lpszSymbolPath, ";",dwSymbolPathSize-strlen(lpszSymbolPath) );
strncat( lpszSymbolPath, szExeMod,dwSymbolPathSize-strlen(lpszSymbolPath) );
strncat( lpszSymbolPath, "\\pdb",dwSymbolPathSize-strlen(lpszSymbolPath) );
// environment variable _NT_SYMBOL_PATH
if ( GetEnvironmentVariableA( "_NT_SYMBOL_PATH", lpszPath, BUFFERSIZE ) )
{
strncat( lpszSymbolPath, ";",dwSymbolPathSize-strlen(lpszSymbolPath) );
strncat( lpszSymbolPath, lpszPath ,dwSymbolPathSize-strlen(lpszSymbolPath));
}
// environment variable _NT_ALTERNATE_SYMBOL_PATH
if ( GetEnvironmentVariableA( "_NT_ALTERNATE_SYMBOL_PATH", lpszPath, BUFFERSIZE ) )
{
strncat( lpszSymbolPath, ";" ,dwSymbolPathSize-strlen(lpszSymbolPath));
strncat( lpszSymbolPath, lpszPath ,dwSymbolPathSize-strlen(lpszSymbolPath));
}
// environment variable SYSTEMROOT
if ( GetEnvironmentVariableA( "SYSTEMROOT", lpszPath, BUFFERSIZE ) )
{
strncat( lpszSymbolPath, ";" ,dwSymbolPathSize-strlen(lpszSymbolPath));
strncat( lpszSymbolPath, lpszPath ,dwSymbolPathSize-strlen(lpszSymbolPath));
strncat( lpszSymbolPath, ";" ,dwSymbolPathSize-strlen(lpszSymbolPath));
// SYSTEMROOT\System32
strncat( lpszSymbolPath, lpszPath ,dwSymbolPathSize-strlen(lpszSymbolPath));
strncat( lpszSymbolPath, "\\System32" ,dwSymbolPathSize-strlen(lpszSymbolPath));
}
// Add user defined path
if ( lpszInitialSymbolPath != NULL )
{
if ( lpszInitialSymbolPath[0] != '\0' )
{
strncat( lpszSymbolPath, ";" ,dwSymbolPathSize-strlen(lpszSymbolPath));
strncat( lpszSymbolPath, lpszInitialSymbolPath ,dwSymbolPathSize-strlen(lpszSymbolPath));
}
}
}
// Uninitialize the loaded symbol files
bool UninitSymInfo()
{
return SymCleanup( GetCurrentProcess() )?true:false;
}
typedef struct tagGetLogicalAddressParms
{
PVOID pvVirtualAddress;
DWORD dwModuleFilePathLength;
char szModuleFilePath[MAX_PATH];
char szCodeSrcFilePath[MAX_PATH];
DWORD dwCodeSrcFileLine;
char szSymName[512];
DWORD dwSectionId;
DWORD dwOffsetInSection;
DWORD dwAllocationBase;
}GET_LOGICAL_ADDRESS_PARMS;
static BOOL WINAPI GetLogicalAddress(
void* pvVirtualAddress,
GET_LOGICAL_ADDRESS_PARMS & glap
)
{
ZeroMemory(&glap,sizeof(glap));
glap.pvVirtualAddress=(PVOID)pvVirtualAddress;
////////////////////////////////////////////////////////
MEMORY_BASIC_INFORMATION mbi={0,};
if ( !VirtualQuery( pvVirtualAddress, &mbi, sizeof(mbi) ) )
return FALSE;
//必须检查这一项
if(mbi.AllocationBase<=0)
{
return FALSE;
}
glap.dwAllocationBase=(DWORD)mbi.AllocationBase;
glap.dwModuleFilePathLength=GetModuleFileName(
(HMODULE)mbi.AllocationBase,
glap.szModuleFilePath,
sizeof(glap.szModuleFilePath) -1
);
if (0== glap.dwModuleFilePathLength )
{
return FALSE;
}
// Point to the DOS header in memory
PIMAGE_DOS_HEADER pDosHdr = (PIMAGE_DOS_HEADER)mbi.AllocationBase;
// From the DOS header, find the NT (PE) header
PIMAGE_NT_HEADERS pNtHdr = (PIMAGE_NT_HEADERS)((DWORD)mbi.AllocationBase + pDosHdr->e_lfanew);
PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION( pNtHdr );
DWORD rva = (DWORD)pvVirtualAddress - (DWORD)mbi.AllocationBase; // RVA is offset from module load address
// Iterate through the section table, looking for the one that encompasses
// the linear address.
for ( int i = 0;
i < pNtHdr->FileHeader.NumberOfSections;
i++, pSection++ )
{
DWORD sectionStart = pSection->VirtualAddress;
DWORD sectionEnd = sectionStart
+ max(pSection->SizeOfRawData, pSection->Misc.VirtualSize);
// Is the address in this section???
if ( (rva >= sectionStart) && (rva <= sectionEnd) )
{
// Yes, address is in the section. Calculate section and offset,
// and store in the "section" & "offset" params, which were
// passed by reference.
glap.dwSectionId=i+1;
glap.dwOffsetInSection=rva - sectionStart;
return TRUE;
}
}
return FALSE; // Should never get here!
}
#define APPEND_SEARCHPATH(dest,iBufSize,src) {\
if(iBufSize>0) \
{ \
strncat(dest,src,iBufSize); \
iBufSize-=strlen(src); \
} \
if(iBufSize>0) \
{ \
strncat(dest,";",iBufSize);\
--iBufSize;\
}\
}
void DumpExceptionEx(
PEXCEPTION_POINTERS pExceptionInfo
)
{
const HANDLE hProcess=::GetCurrentProcess();
const DWORD dwThreadId=::GetCurrentThreadId();
const HANDLE hThread=::GetCurrentThread();
char szTemp[1024]={0,};
const unsigned int uiTempBufSize=sizeof(szTemp)-1;
// 1、Setup search path for pdb (symbol information) files:
char szSymbolSearchPath[64*1024]={0,};
int iSymbolSearchPatBufSize=sizeof(szSymbolSearchPath)-1;
int iBufSpaceLeft=iSymbolSearchPatBufSize;
if ( ::GetCurrentDirectoryA( uiTempBufSize, szTemp ) )
{
APPEND_SEARCHPATH(szSymbolSearchPath,iBufSpaceLeft,szTemp);
}
// dir with executable
if ( ::GetModuleFileNameA( NULL, szTemp, uiTempBufSize ) )
{
char *pchToken=strrchr(szTemp,'\\');
if(pchToken)
{
*pchToken='\0';
}
APPEND_SEARCHPATH(szSymbolSearchPath,iBufSpaceLeft,szTemp);
}
// environment variable _NT_SYMBOL_PATH
if ( ::GetEnvironmentVariableA( "_NT_SYMBOL_PATH", szTemp, uiTempBufSize ) )
{
APPEND_SEARCHPATH(szSymbolSearchPath,iBufSpaceLeft,szTemp);
}
// environment variable _NT_ALTERNATE_SYMBOL_PATH
if ( ::GetEnvironmentVariableA( "_NT_ALTERNATE_SYMBOL_PATH", szTemp, uiTempBufSize ) )
{
APPEND_SEARCHPATH(szSymbolSearchPath,iBufSpaceLeft,szTemp);
}
// environment variable SYSTEMROOT
if ( ::GetEnvironmentVariableA( "SYSTEMROOT", szTemp, uiTempBufSize ) )
{
APPEND_SEARCHPATH(szSymbolSearchPath,iBufSpaceLeft,szTemp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -