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?
The stack trace shows where the error occurs, namely in the c function. It also shows that the c function was called by b , which was called by a , which was in turn called by the code on line 15 (the last line) of the program.
Get a Stack Trace Using the Thread Class We can obtain a stack trace from a thread by calling the getStackTrace() method on the Thread instance. It returns an array of StackTraceElement, from which details about stack frames of the thread can be found.
C#/WinRT is a NuGet-packaged toolkit that provides Windows Runtime (WinRT) projection support for the C# language. A projection assembly is an interop assembly, which enables programming WinRT APIs in a natural and familiar way for the target language.
WinRT applications are applications that use Windows Runtime framework. More specifically, these are Windows Store and Universal Windows Platform (UWP) applications. Note that dotTrace is unable to profile a UWP application if it uses . NET native tool chain.
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With