#include "ccrashstack.h"
#include <tlhelp32.h>
#include <stdio.h>
#define _WIN32_DCOM
#include <comdef.h>
#include <Wbemidl.h>
//#include<base/constants.h>
#include "qdebug.h"
CCrashStack::CCrashStack(PEXCEPTION_POINTERS pException)
{
m_pException = pException;
}
QString CCrashStack::GetModuleByRetAddr(PBYTE Ret_Addr, PBYTE & Module_Addr)
{
MODULEENTRY32 M = {sizeof(M)};
HANDLE hSnapshot;
wchar_t Module_Name[MAX_PATH] = {0};
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0);
if ((hSnapshot != INVALID_HANDLE_VALUE) &&
Module32First(hSnapshot, &M))
{
do
{
if (DWORD(Ret_Addr - M.modBaseAddr) < M.modBaseSize)
{
lstrcpyn(Module_Name, M.szExePath, MAX_PATH);
Module_Addr = M.modBaseAddr;
break;
}
} while (Module32Next(hSnapshot, &M));
}
CloseHandle(hSnapshot);
QString sRet = QString::fromWCharArray(Module_Name);
return sRet;
}
QString CCrashStack::GetCallStack(PEXCEPTION_POINTERS pException)
{
PBYTE Module_Addr_1;
char bufer[256]={0};
QString sRet;
typedef struct STACK
{
STACK * Ebp;
PBYTE Ret_Addr;
DWORD Param[0];
} STACK, * PSTACK;
STACK Stack = {0, 0};
PSTACK Ebp;
if (pException) //fake frame for exception address
{
Stack.Ebp = (PSTACK)pException->ContextRecord->Ebp;
Stack.Ret_Addr = (PBYTE)pException->ExceptionRecord->ExceptionAddress;
Ebp = &Stack;
}
else
{
Ebp = (PSTACK)&pException - 1; //frame addr of Get_Call_Stack()
// Skip frame of Get_Call_Stack().
if (!IsBadReadPtr(Ebp, sizeof(PSTACK)))
Ebp = Ebp->Ebp; //caller ebp
}
// Break trace on wrong stack frame.
for (; !IsBadReadPtr(Ebp, sizeof(PSTACK)) && !IsBadCodePtr(FARPROC(Ebp->Ret_Addr));
Ebp = Ebp->Ebp)
{
// If module with Ebp->Ret_Addr found.
memset(bufer,0, sizeof(0));
sprintf(bufer, "\n%08X ", (unsigned int)Ebp->Ret_Addr);
sRet.append(bufer);
QString moduleName = this->GetModuleByRetAddr(Ebp->Ret_Addr, Module_Addr_1) ;
if (moduleName.length() > 0)
{
sRet.append(moduleName);
}
}
return sRet;
} //Get_Call_Stack
QString CCrashStack::GetVersionStr()
{
OSVERSIONINFOEX V = {sizeof(OSVERSIONINFOEX)}; //EX for NT 5.0 and later
if (!GetVersionEx((POSVERSIONINFO)&V))
{
ZeroMemory(&V, sizeof(V));
V.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx((POSVERSIONINFO)&V);
}
if (V.dwPlatformId != VER_PLATFORM_WIN32_NT)
V.dwBuildNumber = LOWORD(V.dwBuildNumber); //for 9x HIWORD(dwBuildNumber) = 0x04xx
QString sRet;
sRet.append(QString("Windows: %1.%2.%3, SP %4.%5, Product Type %6\n")
.arg(V.dwMajorVersion).arg(V.dwMinorVersion).arg(V.dwBuildNumber)
.arg(V.wServicePackMajor).arg(V.wServicePackMinor).arg(V.wProductType));
// QString graphics_module = "GraphicsCard: ";
// QString sound_module = "SoundDevice: ";
// GetHardwareInaformation(graphics_module, sound_module);
// sRet.append(graphics_module);
// sRet.append(sound_module);
return sRet;
}
QString CCrashStack::GetExceptionInfo()
{
WCHAR Module_Name[MAX_PATH];
PBYTE Module_Addr;
QString sRet;
char buffer[512]={0};
QString sTmp = GetVersionStr();
sRet.append(sTmp);
sRet.append("Process: ");
GetModuleFileName(NULL, Module_Name, MAX_PATH);
sRet.append(QString::fromWCharArray(Module_Name));
sRet.append("\n");
// If exception occurred.
if (m_pException)
{
EXCEPTION_RECORD & E = *m_pException->ExceptionRecord;
CONTEXT & C = *m_pException->ContextRecord;
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "Exception Addr: %08X ", (int)E.ExceptionAddress);
sRet.append(buffer);
// If module with E.ExceptionAddress found - save its path and date.
QString module = GetModuleByRetAddr((PBYTE)E.ExceptionAddress, Module_Addr);
if (module.length() > 0)
{
sRet.append(" Module: ");
sRet.append(module);
}
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "\nException Code: %08X\n", (int)E.ExceptionCode);
sRet.append(buffer);
if (E.ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
{
// Access violation type - Write/Read.
memset(buffer, 0, sizeof(buffer));
sprintf(buffer,"%s Address: %08X\n",
(E.ExceptionInformation[0]) ? "Write" : "Read", (int)E.ExceptionInformation[1]);
sRet.append(buffer);
}
sRet.append("Instruction: ");
for (int i = 0; i < 16; i++)
{
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, " %02X", PBYTE(E.ExceptionAddress)[i]);
sRet.append(buffer);
}
sRet.append("\nRegisters: ");
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "\nEAX: %08X EBX: %08X ECX: %08X EDX: %08X", (unsigned int)C.Eax,(unsigned int) C.Ebx, (unsigned int)C.Ecx, (unsigned int)C.Edx);
sRet.append(buffer);
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "\nESI: %08X EDI: %08X ESP: %08X EBP: %08X", (unsigned int)C.Esi, (unsigned int)C.Edi, (unsigned int)C.Esp, (unsigned int)C.Ebp);
sRet.append(buffer);
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "\nEIP: %08X EFlags: %08X", (unsigned int)C.Eip,(unsigned int) C.EFlags);
sRet.append(buffer);
} //if (pException)
sRet.append("\nCall Stack:");
QString sCallstack = this->GetCallStack(m_pException);
sRet.append(sCallstack);
return sRet;
}
#if 0
bool CCrashStack::GetHardwareInaformation(QString &graphics_card, QString &sound_deivce)
{
HRESULT hres;
// Initialize COM.
hres = CoInitializeEx(0, COINIT_APARTMENTTHREADED);
if (FAILED(hres))
{
qDebug() << "Failed to initialize COM library. "
<< "Error code = 0x"
<< hex << hres << endl;
return false;
}
// Initialize
hres = CoInitializeSecurity(NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL);
if ((hres != RPC_E_TOO_LATE) && FAILED(hres))
{
qDebug() << "Failed to initialize security. "
<< "Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return false;
}
// Obta
守静向光
- 粉丝: 46
- 资源: 2
最新资源
- 基于C语言的嵌入式软件定时器详细文档+全部资料+高分项目+源码.zip
- 基于ffmpeg的直播推流器,超级稳定,经过长时间稳定性测试,超低延时,可用于手机,电视,嵌入式等直播App及设备。详细文档+全部资料+高分项目+源码.zip
- 基于DCT算法的水印嵌入和提取的移动智能终端数字图像证据系统详细文档+全部资料+高分项目+源码.zip
- 基于FPGA的DDR1控制器,为低端FPGA嵌入式系统提供廉价、大容量的存储详细文档+全部资料+高分项目+源码.zip
- 基于FreeRTOS开发的嵌入式开发框架详细文档+全部资料+高分项目+源码.zip
- 基于FMCW雷达的多天线定位系统详细文档+全部资料+高分项目+源码.zip
- 基于FriendlyARM6410平台的嵌入式Qt程序:实时天气信息,远程vnc控制,远程监视摄像头,语音控制,语音输出TTS详细文档+全部资料+高分项目+源码.zip
- 基于FSMPSTem32的嵌入式音乐播放器、实训作业详细文档+全部资料+高分项目+源码.zip
- 基于GEC6818嵌入式大作业详细文档+全部资料+高分项目+源码.zip
- 基于jetty嵌入式容器的java性能分析工具,内嵌H2 database,以图表形式直观展现应用当前性能数据详细文档+全部资料+高分项目+源码.zip
- 基于jq开发的数学公式插件,可随意嵌入web中详细文档+全部资料+高分项目+源码.zip
- 基于Linux系统的应用程序,旨在搭建一套完整的多进程多线程通讯的消息框架. 支持多SOC的嵌入式APP详细文档+全部资料+高分项目+源码.zip
- 基于mplayer的嵌入式音视频播放器详细文档+全部资料+高分项目+源码.zip
- 基于LSM-Tree的嵌入式数据库详细文档+全部资料+高分项目+源码.zip
- 基于liunx下的一个QT程序,KTV点歌系统嵌入式设备详细文档+全部资料+高分项目+源码.zip
- 基于MySQL的嵌入式Linux智慧农业采集控制系统详细文档+全部资料+高分项目+源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈