2014-11-29 17:06:10 -05:00
|
|
|
|
|
|
|
// StackTrace.cpp
|
|
|
|
|
|
|
|
// Implements the functions to print current stack traces
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "StackTrace.h"
|
|
|
|
#ifdef _WIN32
|
2017-06-19 05:05:19 -04:00
|
|
|
#include "WinStackWalker.h"
|
2020-05-10 12:18:28 -04:00
|
|
|
#else
|
2015-11-19 16:34:32 -05:00
|
|
|
#ifdef __GLIBC__
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
2014-11-30 08:23:51 -05:00
|
|
|
#include <unistd.h>
|
2014-11-29 17:06:10 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PrintStackTrace(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2017-06-19 05:05:19 -04:00
|
|
|
class PrintingStackWalker:
|
|
|
|
public WinStackWalker
|
2014-11-29 17:06:10 -05:00
|
|
|
{
|
|
|
|
virtual void OnOutput(LPCSTR szText) override
|
|
|
|
{
|
|
|
|
puts(szText);
|
|
|
|
}
|
|
|
|
} sw;
|
|
|
|
sw.ShowCallstack();
|
2020-05-10 12:18:28 -04:00
|
|
|
#else
|
2015-11-19 16:26:39 -05:00
|
|
|
#ifdef __GLIBC__
|
|
|
|
// Use the backtrace() function to get and output the stackTrace:
|
2015-12-19 09:30:32 -05:00
|
|
|
// Code adapted from https://stackoverflow.com/questions/77005/how-to-generate-a-stacktrace-when-my-gcc-c-app-crashes
|
2015-11-19 16:26:39 -05:00
|
|
|
void * stackTrace[30];
|
2015-12-03 07:52:35 -05:00
|
|
|
auto numItems = backtrace(stackTrace, ARRAYCOUNT(stackTrace));
|
2015-11-19 16:26:39 -05:00
|
|
|
backtrace_symbols_fd(stackTrace, numItems, STDERR_FILENO);
|
|
|
|
#endif
|
2014-11-29 17:06:10 -05:00
|
|
|
#endif
|
|
|
|
}
|