Cuberite
A lightweight, fast and extensible game server for Minecraft
StackTrace.cpp
Go to the documentation of this file.
1 
2 // StackTrace.cpp
3 
4 // Implements the functions to print current stack traces
5 
6 #include "Globals.h"
7 #include "StackTrace.h"
8 #ifdef _WIN32
9  #include "WinStackWalker.h"
10 #else
11  #ifdef __GLIBC__
12  #include <execinfo.h>
13  #endif
14  #include <unistd.h>
15 #endif
16 
17 
18 
19 
20 
21 void PrintStackTrace(void)
22 {
23  #ifdef _WIN32
24  class PrintingStackWalker:
25  public WinStackWalker
26  {
27  virtual void OnOutput(LPCSTR szText) override
28  {
29  std::fputs(szText, stdout);
30  }
31  } sw;
32  sw.ShowCallstack();
33  #else
34  #ifdef __GLIBC__
35  // Use the backtrace() function to get and output the stackTrace:
36  // Code adapted from https://stackoverflow.com/questions/77005/how-to-generate-a-stacktrace-when-my-gcc-c-app-crashes
37  void * stackTrace[30];
38  auto numItems = backtrace(stackTrace, ARRAYCOUNT(stackTrace));
39  backtrace_symbols_fd(stackTrace, numItems, STDERR_FILENO);
40  #endif
41  #endif
42 }
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:231
void PrintStackTrace(void)
Prints the stacktrace for the current thread.
Definition: StackTrace.cpp:21