• 大小: 1.98KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-02-22
  • 标签: c++  c  堆栈  

资源简介

#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 “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

评论

共有 条评论