资源简介
#include "Stdafx.h"
#include "DebugUtility.h"
// 安全拷贝字符串函数
void SafeStrCpy0(char* szDest, size_t nMaxDestSize, const char* szSrc)
{
if (nMaxDestSize <= 0) return;
if (strlen(szSrc) < nMaxDestSize)
{
strcpy_s(szDest, nMaxDestSize, szSrc);
}
else
{
strncpy_s(szDest, nMaxDestSize, szSrc, nMaxDestSize);
szDest[nMaxDestSize - 1] = '\0';
}
}
// 得到CallStack信息
vector<CallStackInfo> GetCallStack0()
{
CONTEXT _ctx;
RtlCaptureContext(&_ctx);
const CONTEXT* pContext = &_ctx;
HANDLE hProcess = GetCurrentProcess();
SymInitialize(hProcess, NULL, TRUE);
vector<CallStackInfo> arrCallStackInfo;
CONTEXT c = *pContext;
STACKFRAME64 sf;
memset(&sf, 0, sizeof(STACKFRAME64));
DWORD dwImageType = IMAGE_FILE_MACHINE_I386;
// 不同的CPU类型,具体信息可查询MSDN
//
#ifdef _M_IX86
sf.AddrPC.Offset = c.Eip;
sf.AddrPC.Mode = AddrModeFlat;
sf.AddrStack.Offset = c.Esp;
sf.AddrStack.Mode = AddrModeFlat;
sf.AddrFrame.Offset = c.Ebp;
sf.AddrFrame.Mode = AddrModeFlat;
#elif _M_X64
dwImageType = IMAGE_FILE_MACHINE_AMD64;
sf.AddrPC.Offset = c.Rip;
sf.AddrPC.Mode = AddrModeFlat;
sf.AddrFrame.Offset = c.Rsp;
sf.AddrFrame.Mode = AddrModeFlat;
sf.AddrStack.Offset = c.Rsp;
sf.AddrStack.Mode = AddrModeFlat;
#elif _M_IA64
dwImageType = IMAGE_FILE_MACHINE_IA64;
sf.AddrPC.Offset = c.StIIP;
sf.AddrPC.Mode = AddrModeFlat;
sf.AddrFrame.Offset = c.IntSp;
sf.AddrFrame.Mode = AddrModeFlat;
sf.AddrBStore.Offset = c.RsBSP;
sf.AddrBStore.Mode = AddrModeFlat;
sf.AddrStack.Offset = c.IntSp;
sf.AddrStack.Mode = AddrModeFlat;
#else
#error "Platform not supported!"
#endif
HANDLE hThread = GetCurrentThread();
while (true)
{
// 该函数是实现这个功能的最重要的一个函数
// 函数的用法以及参数和返回值的具体解释可以查询MSDN
if (!StackWalk64(dwImageType, hProcess, hThread, &sf, &c, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL))
{
break;
}
if (sf.AddrFrame.Offset == 0)
{
break;
}
CallStackInfo callstackinfo;
SafeStrCpy0(callstackinfo.MethodName, MAX_NAME_LENGTH, "N/A");
SafeStrCpy0(callstackinfo.FileName, MAX_NAME_LENGTH, "N/A");
SafeStrCpy0(callstackinfo.ModuleName, MAX_NAME_LENGTH, "N/A");
SafeStrCpy0(callstackinfo.LineNumber, MAX_NAME_LENGTH, "N/A");
BYTE symbolBuffer[sizeof(IMAGEHLP_SYMBOL64) MAX_NAME_LENGTH];
IMAGEHLP_SYMBOL64 *pSymbol = (IMAGEHLP_SYMBOL64*)symbolBuffer;
memset(pSymbol, 0, sizeof(IMAGEHLP_SYMBOL64) MAX_NAME_LENGTH);
pSymbol->SizeOfStruct = sizeof(symbolBuffer);
pSymbol->MaxNameLength = MAX_NAME_LENGTH;
DWORD symDisplacement = 0;
// 得到函数名
if (SymGetSymFromAddr64(hProcess, sf.AddrPC.Offset, NULL, pSymbol))
{
SafeStrCpy0(callstackinfo.MethodName, MAX_NAME_LENGTH, pSymbol->Name);
}
IMAGEHLP_LINE64 lineInfo;
memset(&lineInfo, 0, sizeof(IMAGEHLP_LINE64));
lineInfo.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
DWORD dwLineDisplacement;
// 得到文件名和所在的代码行
if (SymGetLineFromAddr64(hProcess, sf.AddrPC.Offset, &dwLineDisplacement, &lineInfo))
{
SafeStrCpy0(callstackinfo.FileName, MAX_NAME_LENGTH, lineInfo.FileName);
sprintf_s(callstackinfo.LineNumber, "%d", lineInfo.LineNumber);
}
IMAGEHLP_MODULE64 moduleInfo;
memset(&moduleInfo, 0, sizeof(IMAGEHLP_MODULE64));
moduleInfo.SizeOfStruct = sizeof(IMAGEHLP_MODULE64);
// 得到模块名
if (SymGetModuleInfo64(hProcess, sf.AddrPC.Offset, &moduleInfo))
{
SafeStrCpy0(callstackinfo.ModuleName, MAX_NAME_LENGTH, moduleInfo.ModuleName);
}
arrCallStackInfo.push_back(callstackinfo);
}
SymCleanup(hProcess);
return arrCallStackInfo;
}
#include "DebugUtility.h"
// 安全拷贝字符串函数
void SafeStrCpy0(char* szDest, size_t nMaxDestSize, const char* szSrc)
{
if (nMaxDestSize <= 0) return;
if (strlen(szSrc) < nMaxDestSize)
{
strcpy_s(szDest, nMaxDestSize, szSrc);
}
else
{
strncpy_s(szDest, nMaxDestSize, szSrc, nMaxDestSize);
szDest[nMaxDestSize - 1] = '\0';
}
}
// 得到CallStack信息
vector<CallStackInfo> GetCallStack0()
{
CONTEXT _ctx;
RtlCaptureContext(&_ctx);
const CONTEXT* pContext = &_ctx;
HANDLE hProcess = GetCurrentProcess();
SymInitialize(hProcess, NULL, TRUE);
vector<CallStackInfo> arrCallStackInfo;
CONTEXT c = *pContext;
STACKFRAME64 sf;
memset(&sf, 0, sizeof(STACKFRAME64));
DWORD dwImageType = IMAGE_FILE_MACHINE_I386;
// 不同的CPU类型,具体信息可查询MSDN
//
#ifdef _M_IX86
sf.AddrPC.Offset = c.Eip;
sf.AddrPC.Mode = AddrModeFlat;
sf.AddrStack.Offset = c.Esp;
sf.AddrStack.Mode = AddrModeFlat;
sf.AddrFrame.Offset = c.Ebp;
sf.AddrFrame.Mode = AddrModeFlat;
#elif _M_X64
dwImageType = IMAGE_FILE_MACHINE_AMD64;
sf.AddrPC.Offset = c.Rip;
sf.AddrPC.Mode = AddrModeFlat;
sf.AddrFrame.Offset = c.Rsp;
sf.AddrFrame.Mode = AddrModeFlat;
sf.AddrStack.Offset = c.Rsp;
sf.AddrStack.Mode = AddrModeFlat;
#elif _M_IA64
dwImageType = IMAGE_FILE_MACHINE_IA64;
sf.AddrPC.Offset = c.StIIP;
sf.AddrPC.Mode = AddrModeFlat;
sf.AddrFrame.Offset = c.IntSp;
sf.AddrFrame.Mode = AddrModeFlat;
sf.AddrBStore.Offset = c.RsBSP;
sf.AddrBStore.Mode = AddrModeFlat;
sf.AddrStack.Offset = c.IntSp;
sf.AddrStack.Mode = AddrModeFlat;
#else
#error "Platform not supported!"
#endif
HANDLE hThread = GetCurrentThread();
while (true)
{
// 该函数是实现这个功能的最重要的一个函数
// 函数的用法以及参数和返回值的具体解释可以查询MSDN
if (!StackWalk64(dwImageType, hProcess, hThread, &sf, &c, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL))
{
break;
}
if (sf.AddrFrame.Offset == 0)
{
break;
}
CallStackInfo callstackinfo;
SafeStrCpy0(callstackinfo.MethodName, MAX_NAME_LENGTH, "N/A");
SafeStrCpy0(callstackinfo.FileName, MAX_NAME_LENGTH, "N/A");
SafeStrCpy0(callstackinfo.ModuleName, MAX_NAME_LENGTH, "N/A");
SafeStrCpy0(callstackinfo.LineNumber, MAX_NAME_LENGTH, "N/A");
BYTE symbolBuffer[sizeof(IMAGEHLP_SYMBOL64) MAX_NAME_LENGTH];
IMAGEHLP_SYMBOL64 *pSymbol = (IMAGEHLP_SYMBOL64*)symbolBuffer;
memset(pSymbol, 0, sizeof(IMAGEHLP_SYMBOL64) MAX_NAME_LENGTH);
pSymbol->SizeOfStruct = sizeof(symbolBuffer);
pSymbol->MaxNameLength = MAX_NAME_LENGTH;
DWORD symDisplacement = 0;
// 得到函数名
if (SymGetSymFromAddr64(hProcess, sf.AddrPC.Offset, NULL, pSymbol))
{
SafeStrCpy0(callstackinfo.MethodName, MAX_NAME_LENGTH, pSymbol->Name);
}
IMAGEHLP_LINE64 lineInfo;
memset(&lineInfo, 0, sizeof(IMAGEHLP_LINE64));
lineInfo.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
DWORD dwLineDisplacement;
// 得到文件名和所在的代码行
if (SymGetLineFromAddr64(hProcess, sf.AddrPC.Offset, &dwLineDisplacement, &lineInfo))
{
SafeStrCpy0(callstackinfo.FileName, MAX_NAME_LENGTH, lineInfo.FileName);
sprintf_s(callstackinfo.LineNumber, "%d", lineInfo.LineNumber);
}
IMAGEHLP_MODULE64 moduleInfo;
memset(&moduleInfo, 0, sizeof(IMAGEHLP_MODULE64));
moduleInfo.SizeOfStruct = sizeof(IMAGEHLP_MODULE64);
// 得到模块名
if (SymGetModuleInfo64(hProcess, sf.AddrPC.Offset, &moduleInfo))
{
SafeStrCpy0(callstackinfo.ModuleName, MAX_NAME_LENGTH, moduleInfo.ModuleName);
}
arrCallStackInfo.push_back(callstackinfo);
}
SymCleanup(hProcess);
return arrCallStackInfo;
}
代码片段和文件信息
#include “Stdafx.h“
#include “DebugUtility.h“
// 安全拷贝字符串函数
void SafeStrCpy0(char* szDest size_t nMaxDestSize const char* szSrc)
{
if (nMaxDestSize <= 0) return;
if (strlen(szSrc) < nMaxDestSize)
{
strcpy_s(szDest nMaxDestSize szSrc);
}
else
{
strncpy_s(szDest nMaxDestSize szSrc nMaxDestSize);
szDest[nMaxDestSize - 1] = ‘\0‘;
}
}
// 得到CallStack信息
vector GetCallStack0()
{
CONTEXT _ctx;
RtlCaptureContext(&_ctx);
const CONTEXT* pContext = &_ctx;
HANDLE hProcess = GetCurrentProcess();
SymInitialize(hProcess NULL TRUE);
vector arrCallStackInfo;
CONTEXT c = *pContext;
STACKframe64 sf;
memset(&sf 0 sizeof(STACKframe64));
DWORD dwImageType = IMAGE_FILE_MACHINE_I386;
// 不同的CPU类型,具体信息可查询MSDN
//
#ifdef _M_IX86
sf.AddrPC.Offset = c.Eip;
sf.AddrPC.Mode = AddrModeFlat;
sf.AddrStack.Offset = c.Esp;
sf.AddrStack.Mode = AddrModeFlat;
sf.Addrframe.Offset = c.Ebp;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3772 2017-12-20 12:21 DebugUtility.cpp
文件 717 2017-12-21 15:41 DebugUtility.h
相关资源
- windows文件夹监控(c++源码)
- C++ tts开发 可调节语速,音量,切换语
- CSA算法MATLAB代码
- zprotect 1.6 脱壳工具()
- DDC/CI Tool
- PLCWin10ActiveXTest(三菱PLC通讯控件)
- FTP客户端源代码C语言
- c++(3_MSGS.C)
- 实现图像实时采集(使用BaslerSDK)-
- directx实现正方体图片映射
- c++实现P2PDemo(点对点聊天)
- CVR100UD身份证阅读器二次开发SDK开发说
- windows 串口升级工具(c++源码)
- ntmedsys.sys(c++源码)
- 一款MFC关机小程序
- 自制操作系统.docx(附源码地址)
- 操作系统原理及设计原则(Operating
- 基于51单片机的音乐播放器设计.doc
- 基于C的图书管理系统 毕业设计
- com 原理应用源码
- DSS中的RTSPclientLib程序
- 完成端口开发包iocpmfc
- c 常用的数据结构 (Errata for Ford/Top
- RTSPRTP C++ 源代码
-
c++ xm
lRPC - tftpd32 源代码VC
- 《Learning WebRTC》pdf 高清
- Windows Sockets 规范及应用-Windows网络编
- httpserver(SuperCookie)
- C语言基数排序——顺序队列实现
评论
共有 条评论