Skip to content

Commit

Permalink
Dump the stacktrace on SIGSEGV to std::cerr
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Apr 23, 2021
1 parent 8056e60 commit 1c4df72
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions radiantcore/Radiant.cpp
Expand Up @@ -13,12 +13,43 @@
#include "messagebus/MessageBus.h"
#include "settings/LanguageManager.h"

#ifdef POSIX
#include <execinfo.h>
#include <signal.h>
#endif

namespace radiant
{

namespace
{
const char* const TIME_FMT = "%Y-%m-%d %H:%M:%S";

void sigsegv_handler(int sig)
{
rError() << "SIGSEGV signal caught: " << sig << std::endl;
std::cerr << "SIGSEGV signal caught: " << sig << std::endl;

void* buffer[100];
int numAddresses = backtrace(buffer, sizeof(buffer));
printf("backtrace() returned %d addresses\n", numAddresses);

char** strings = backtrace_symbols(buffer, numAddresses);

if (strings == nullptr)
{
std::cerr << "backtrace() returned nullptr" << std::endl;
exit(EXIT_FAILURE);
}

for (int j = 0; j < numAddresses; j++)
{
std::cerr << j << "j: " << strings[j] << std::endl;
}

free(strings);
exit(1);
}
}

Radiant::Radiant(IApplicationContext& context) :
Expand All @@ -35,6 +66,10 @@ Radiant::Radiant(IApplicationContext& context) :
// Attach the logfile to the logwriter
createLogFile();

#ifdef POSIX
signal(SIGSEGV, sigsegv_handler);
#endif

_moduleRegistry.reset(new module::ModuleRegistry(_context));

_languageManager.reset(new language::LanguageManager);
Expand Down

0 comments on commit 1c4df72

Please sign in to comment.