大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
#ifndef __DUMP_H__
#define __DUMP_H__
#include <stdlib.h>
#include <stdio.h>
#include <ostream>
#ifdef _MSC_VER
#include <windows.h>
#include <DbgHelp.h>
#pragma comment(lib, "dbghelp.lib")
#ifndef _M_IX86
#error "The following code only works for x86!"
#endif
inline BOOL IsDataSectionNeeded(const WCHAR* pModuleName)
{
if (pModuleName == 0)
{
return FALSE;
}
WCHAR szFileName[_MAX_FNAME] = L"";
_wsplitpath_s(pModuleName, NULL, 0, NULL, 0, szFileName, _MAX_FNAME, NULL, 0);
if (_wcsicmp(szFileName, L"ntdll") == 0)
return TRUE;
return FALSE;
}
inline BOOL CALLBACK MiniDumpCallback(PVOID pParam,
const PMINIDUMP_CALLBACK_INPUT pInput,
PMINIDUMP_CALLBACK_OUTPUT pOutput)
{
if (pInput == 0 || pOutput == 0)
return FALSE;
switch (pInput->CallbackType)
{
case ModuleCallback:
if (pOutput->ModuleWriteFlags & ModuleWriteDataSeg)
if (!IsDataSectionNeeded(pInput->Module.FullPath))
pOutput->ModuleWriteFlags &= (~ModuleWriteDataSeg);
case IncludeModuleCallback:
case IncludeThreadCallback:
case ThreadCallback:
case ThreadExCallback:
return TRUE;
default:;
}
return FALSE;
}
inline void CreateMiniDump(EXCEPTION_POINTERS* pep, LPCTSTR strFileName)
{
HANDLE hFile = CreateFile(strFileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
{
MINIDUMP_EXCEPTION_INFORMATION mdei;
mdei.ThreadId = GetCurrentThreadId();
mdei.ExceptionPointers = pep;
mdei.ClientPointers = NULL;
MINIDUMP_CALLBACK_INFORMATION mci;
mci.CallbackRoutine = (MINIDUMP_CALLBACK_ROUTINE)MiniDumpCallback;
mci.CallbackParam = 0;
//dump信息较多
MINIDUMP_TYPE mdt = (MINIDUMP_TYPE)(
MiniDumpWithPrivateReadWriteMemory |
MiniDumpWithFullMemory |
MiniDumpWithDataSegs |
MiniDumpWithHandleData |
MiniDumpWithFullMemoryInfo |
MiniDumpWithThreadInfo |
MiniDumpWithUnloadedModules |
MiniDumpWithIndirectlyReferencedMemory |
MiniDumpWithFullAuxiliaryState |
MiniDumpWithPrivateWriteCopyMemory |
MiniDumpIgnoreInaccessibleMemory |
MiniDumpWithTokenInformation |
MiniDumpFilterMemory
);
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
hFile, mdt, (pep != 0) ? &mdei : 0, 0, &mci);
//MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, (pep != 0) ? &mdei : 0, NULL, &mci); //普通dump,小
CloseHandle(hFile);
}
}
std::ostream& operator<<(std::ostream& os, const EXCEPTION_RECORD& red)
{
return os << " Thread ID:" << GetCurrentThreadId()
<< " ExceptionCode: " << red.ExceptionCode << "/n"
<< " ExceptionFlags: " << red.ExceptionFlags << "/n"
<< " ExceptionAddress: " << red.ExceptionAddress << "/n"
<< " NumberParameters: " << red.NumberParameters;
}
LONG WINAPI GPTUnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
{
FILE *fp;
fopen_s(&fp, "DumpInfo.txt", "a+");
char buffer[256];
sprintf_s(buffer, "%s", " Server Dead,CreatDump!!\n");
static char __time[1024];
SYSTEMTIME s;
GetLocalTime(&s);
sprintf_s(__time, "[%d-%d-%d %d:%d:%d]", s.wYear, s.wMonth, s.wDay, s.wHour, s.wMinute, s.wSecond);
fwrite(__time, strlen(__time), 1, fp);
fwrite(buffer, strlen(buffer), 1, fp);
fclose(fp);
//StackWalker sw;
//sw.ShowCallstack();//actionlog.txt
CreateMiniDump(pExceptionInfo, "Exception.dmp");
//std::cerr << "未知错误:" << (*pExceptionInfo->ExceptionRecord) << std::endl;
//exit(pExceptionInfo->ExceptionRecord->ExceptionCode);
return EXCEPTION_EXECUTE_HANDLER; // 程序停止运行
}
// 此函数一旦成功调用,之后对 SetUnhandledExceptionFilter 的调用将无效
void DisableSetUnhandledExceptionFilter()
{
void* addr = (void*)GetProcAddress(LoadLibrary("kernel32.dll"), "SetUnhandledExceptionFilter");
if (addr && !IsBadReadPtr(addr, sizeof(void*)))
{
unsigned char code[16];
int size = 0;
code[size++] = 0x33;
code[size++] = 0xC0;
code[size++] = 0xC2;
code[size++] = 0x04;
code[size++] = 0x00;
DWORD dwOldFlag, dwTempFlag;
if (VirtualProtectEx(GetCurrentProcess(), addr, size, PAGE_EXECUTE_READWRITE, &dwOldFlag) == 0)
return;
WriteProcessMemory(GetCurrentProcess(), addr, code, size, NULL);
VirtualProtectEx(GetCurrentProcess(), addr, size, dwOldFlag, &dwTempFlag);
}
}
void InitMinDump()
{
//注册异常处理函数
SetUnhandledExceptionFilter(GPTUnhandledExceptionFilter);
//使SetUnhandledExceptionFilter
DisableSetUnhandledExceptionFilter();
}
#endif
#endif
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/194571.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...