From ee038d445b3d44d81d53d20570c23657175ed0da Mon Sep 17 00:00:00 2001 From: niansa Date: Wed, 29 Apr 2020 16:41:50 +0200 Subject: [PATCH] Catch segmentation fault to print stacktrace --- main.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index e41e332..f8abdb9 100644 --- a/main.cpp +++ b/main.cpp @@ -26,6 +26,8 @@ along with pilang. If not, see . #include #include #include +#include +#include #include #include @@ -41,9 +43,19 @@ static std::string room; void panic(std::string message) { + // Print message std::cerr << std::endl << "An unhandled exception occured:" << std::endl << message << std::endl; + // Print stacktrace + void *array[10]; + int size = backtrace(array, 10); + std::cerr << std::endl << "Stacktrace:" << std::endl; + backtrace_symbols_fd(array, size, STDERR_FILENO); + // Abort abort(); } +void sigsegv_panic(int) { + panic("Segmentation fault received!"); +} std::string ltrim(const std::string &s) { @@ -303,6 +315,8 @@ std::string get_rss_url(GumboNode *node) { // Currently unused int main(int argc, char *argv[]) { + // Catch SIGSEGV + signal(SIGSEGV, sigsegv_panic); // Show license note if --license was given as first argument if (argc > 1 and !strncmp(argv[1], "--license", 9)) { std::cout << "CommSyFuse Copyright (C) 2020 niansa" << std::endl; @@ -379,8 +393,10 @@ int main(int argc, char *argv[]) { // Await user input std::cout << "? " << std::flush; std::getline (std::cin,userinstr); - // Check string input options first5 - if (userinstr == "r") { + // Check string input options first + if (userinstr == "") { + continue; + } else if (userinstr == "r") { execve(argv[0], argv, nullptr); } // Try to convert input to unsigned long