概述
根据网上大佬的写法魔改了一下,把自己的debug系统忽略掉,然后只输出到main就结束。同时传入了log文件stream来记录log
void TraceStack(std::ofstream & ofs)
{
static const int MAX_STACK_FRAMES = 7;
void* pStack[MAX_STACK_FRAMES];
HANDLE process = GetCurrentProcess();
SymInitialize(process, NULL, TRUE);
WORD frames = CaptureStackBackTrace(0, MAX_STACK_FRAMES, pStack, NULL);
ofs << "tstack traceback: " << std::endl;
for (WORD i = 0; i < frames; ++i) {
if (i < 2)
{
continue;
}
DWORD64 address = (DWORD64)(pStack[i]);
DWORD64 displacementSym = 0;
char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = MAX_SYM_NAME;
DWORD displacementLine = 0;
IMAGEHLP_LINE64 line;
//SymSetOptions(SYMOPT_LOAD_LINES);
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
if (SymFromAddr(process, address, &displacementSym, pSymbol)
&& SymGetLineFromAddr64(process, address, &displacementLine, &line)) {
ofs << "t" << pSymbol->Name << " at " << line.FileName << ": " << line.LineNumber << "(0x" << std::hex << pSymbol->Address << std::dec << ")" << std::endl;
std::string name(pSymbol->Name);
if (name == "main")
{
break;
}
}
else {
ofs << "terror: " << GetLastError() << std::endl;
}
}
return;
}
找了半天实在没找到之前参考的文章,不好意思。
最后
以上就是落寞心锁为你收集整理的c++ 堆栈信息输出,简单实用的全部内容,希望文章能够帮你解决c++ 堆栈信息输出,简单实用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复