SymGetLineFromAddr 无法正常工作

SymGetLineFromAddr not working properly(SymGetLineFromAddr 无法正常工作)
本文介绍了SymGetLineFromAddr 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

#include "stdafx.h"
#include <process.h>
#include <iostream>
#include <Windows.h>
#include "dbghelp.h"

using namespace std;

int LogStackTrace()
{
    void *stack[1024];
    HANDLE process = GetCurrentProcess();
    SymInitialize(process, NULL, TRUE);
    WORD numberOfFrames = CaptureStackBackTrace(0, 1000, stack, NULL);
    SYMBOL_INFO *symbol = (SYMBOL_INFO *)malloc(sizeof(SYMBOL_INFO));
    symbol->MaxNameLen = 1024;
    symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
    IMAGEHLP_LINE *line = (IMAGEHLP_LINE *)malloc(sizeof(IMAGEHLP_LINE));
    line->SizeOfStruct = sizeof(IMAGEHLP_LINE);
    printf("Caught exception ");
    for (int i = 0; i < numberOfFrames; i++)
    {
        SymFromAddr(process, (DWORD64)(stack[i]), NULL, symbol);
        SymGetLineFromAddr(process, (DWORD)(stack[i]), NULL, line);
        printf("at %s in %s, address 0x%0X
", symbol->Name, line->FileName, symbol->Address);
    }
    return 0;
}

void function2()
{
    int a = 0;
    int b = 0;
    throw new exception("Expected exception.");
}

void function1()
{
    int a = 0;
    function2();
}

void function0()
{
    function1();
}

static void threadFunction(void *param)
{
    try
    {
        function0();
    }
    catch (...)
    {
        LogStackTrace();
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    try
    {
        _beginthread(threadFunction, 0, NULL);
    }
    catch (...)
    {
        LogStackTrace();
    }
    printf("Press any key to exit.
");
    cin.get();
    return 0;
}

问题是它总是在这一行出错:printf("at %s in %s, address 0x%0X ", symbol->Name, line->FileName, symbol->地址);

The problem is that it always errors out at this line: printf("at %s in %s, address 0x%0X ", symbol->Name, line->FileName, symbol->Address);

原因是因为 line 的 FileName 似乎为 NULL.实际上,整个线路结构是混乱的.我正在尝试编写一个应用程序来显示错误的堆栈跟踪.但这是为什么呢?它不应该使用上面的代码工作吗?PS 我针对 Win32 编译它,作为一个简单的 MSVC++ 控制台应用程序.

The reason is because line's FileName seems to be NULL. Actually, the entire line structure is messed up. I am trying to write an application to show a stack trace on an error. But why is that? Shouldn't it be working using the above code? PS I compiled it against Win32, as a simple MSVC++ Console application.

推荐答案

你的代码有同样的问题(Windows 7 64b,Unicode 32 位构建,VS2012 Express)

Had the same problem with your code (Windows Seven 64b, Unicode 32 bits build, VS2012 Express)

修复它:

DWORD dwDisplacement;
SymGetLineFromAddr(process, (DWORD)(stack[i]), &dwDisplacement, line);

这篇关于SymGetLineFromAddr 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)