8

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.

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

Earlz
  • 62,085
  • 98
  • 303
  • 499
  • 1
    That's not possible. C++ code is too heavily optimized to permit reliable stackwalks. You diagnose C++ crashes with minidumps. Hard to come by in a Store app. – Hans Passant Jan 31 '13 at 19:10
  • 2
    Users have an option to enable Windows to send the crash dump when an app crashes. Then you can see the crash dump in your dashboard so you can download it and open it in a debugger. – Filip Skakun Jan 31 '13 at 21:42
  • It's not possible in C# code either. Inlining & tail-calls will make your stack walk completely unreliable. GetCallingAssembly and it's associated C# calls have been removed from the Windows 8 .NET Profile for this reason. dbghelp.dll is where the functionality exists, and yes, it's not supported under Windows 8 Store apps. Go ahead & request it if you'd like, but there's a long line of stuff that is way higher priority in my book :-) – Kevin Frei Feb 12 '13 at 05:51
  • @HansPassant actually under Windows CE (C++ app) I am able to get very good stack traces for .exe-s build in release. I use map files and GetThreadCallStack function. – marcinj Jun 22 '15 at 11:20

2 Answers2

1

The only way I have been able to debug complex WINRT issues is to use ETW to track causality chains. While kind of tedious to setup This article (while referring to c#) highlights the method:

Here are a couple of decent introduction to ETW for C/C++.

Using this method you should be able to create ETW events then listen for them in the app and include them as a serialized string for analysis later.

Community
  • 1
  • 1
Matt Johnson
  • 1,913
  • 16
  • 23
1

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.

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);
    }
  }
marcinj
  • 48,511
  • 9
  • 79
  • 100