如何在WinRT中从C ++获取堆栈跟踪? [英] How can I get a stacktrace from C++ in WinRT?

查看:115
本文介绍了如何在WinRT中从C ++获取堆栈跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从C ++应用程序获取stacktrace,并将其序列化为字符串,以便以后可以对其进行解析。我在Windows上听说过的唯一API是StackWalk64,似乎不受支持。

I need to get a stacktrace from a C++ application, and serialize it into a string so it can be parsed later. The only API I've heard of for this on Windows is StackWalk64, which doesn't appear to be supported.

如何在Windows Store应用程序中从C ++获取堆栈跟踪?

How can I get a stacktrace from C++ in a Windows Store app?

推荐答案

对我有用的是以下asm代码。这仅在x86平台上有效,因此仅在调试器上调试时有用。返回的帧指针可在反汇编窗口中使用,以跳入源代码。我认为应该可以使用地图文件来获取确切的源代码位置。

What worked for me is asm code as below. This only works on x86 platform, so it is usefull only during debugging on emulator. Returned frame pointers can be used in dissassembly window to jump into source code. I think it should be possible to use map file to get exact source code location.

我使用此代码查找内存泄漏,结合使用crtdbg,它在具有大量分配的大型应用程序中非常有效。 VS 2013内存分析器最多可以处理1分钟的数据记录。

I use this code to find memory leaks, combined with crtdbg it works very well in very large application with lots of allocations. VS 2013 memory profiler could handle at most 1 minute of data recording.

  FINLINE static DWORD GetCallerFrameNum(int index) {
#if defined(_DEBUG) && defined(_MSC_VER) && defined(_M_IX86)

    DWORD caller = 0;
    __asm
    {
      mov ebx, ebp
        mov ecx, index
        inc ecx
        xor eax, eax
      StackTrace_getCaller_next :
      mov eax, [ebx + 4]
        mov ebx, [ebx]
        dec ecx
        jnz StackTrace_getCaller_next
        mov caller, eax
    }
    return caller;

#else

    return 0;

#endif
  }

  template<class T>
  void RecordStackTrace(T& vecOut) {
    vecOut.clear();
    vecOut.reserve(32);
    for (INT iInitLevel = 1; iInitLevel < 32; ++iInitLevel) {
      DWORD dwFrameNum = GetCallerFrameNum(iInitLevel);
      if (!dwFrameNum)
        return;
      vecOut.push_back(dwFrameNum);
    }
  }

这篇关于如何在WinRT中从C ++获取堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆