From e25e2bb20e826d215f08328f22ad0e8d7c9a0bb5 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 14 Aug 2018 20:23:27 +0200 Subject: Moved the backtrace function to a separate file --- applications/kube/CMakeLists.txt | 2 +- applications/kube/backtrace.cpp | 74 ++++++++++++++++++++++++++++++++++++++++ applications/kube/backtrace.h | 22 ++++++++++++ applications/kube/main.cpp | 49 +------------------------- 4 files changed, 98 insertions(+), 49 deletions(-) create mode 100644 applications/kube/backtrace.cpp create mode 100644 applications/kube/backtrace.h diff --git a/applications/kube/CMakeLists.txt b/applications/kube/CMakeLists.txt index fa145816..b2d5d6c4 100644 --- a/applications/kube/CMakeLists.txt +++ b/applications/kube/CMakeLists.txt @@ -5,7 +5,7 @@ include(ECMAddAppIcon) find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Quick) # install executable -set(SRCS main.cpp) +set(SRCS main.cpp backtrace.cpp) if(APPLE OR WIN32) # Sets the icon on Windows and OSX diff --git a/applications/kube/backtrace.cpp b/applications/kube/backtrace.cpp new file mode 100644 index 00000000..327c46dd --- /dev/null +++ b/applications/kube/backtrace.cpp @@ -0,0 +1,74 @@ +/* + Copyright (c) 2017 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +#include "backtrace.h" + +#include +#include +#include +#include +#include +#ifndef Q_OS_WIN +#include +#include +#include +#include +#else +#include +#include +#endif + +void printStacktrace() +{ +#ifndef Q_OS_WIN + int skip = 1; + void *callstack[128]; + const int nMaxFrames = sizeof(callstack) / sizeof(callstack[0]); + char buf[1024]; + int nFrames = backtrace(callstack, nMaxFrames); + char **symbols = backtrace_symbols(callstack, nFrames); + + std::ostringstream trace_buf; + for (int i = skip; i < nFrames; i++) { + // printf("%s\n", symbols[i]); + Dl_info info; + if (dladdr(callstack[i], &info) && info.dli_sname) { + char *demangled = NULL; + int status = -1; + if (info.dli_sname[0] == '_') { + demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status); + } + snprintf(buf, sizeof(buf), "%-3d %*p %s + %zd\n", + i, int(2 + sizeof(void*) * 2), callstack[i], + status == 0 ? demangled : + info.dli_sname == 0 ? symbols[i] : info.dli_sname, + (char *)callstack[i] - (char *)info.dli_saddr); + free(demangled); + } else { + snprintf(buf, sizeof(buf), "%-3d %*p %s\n", + i, int(2 + sizeof(void*) * 2), callstack[i], symbols[i]); + } + trace_buf << buf; + } + free(symbols); + if (nFrames == nMaxFrames) { + trace_buf << "[truncated]\n"; + } + std::cerr << trace_buf.str(); +#endif +} diff --git a/applications/kube/backtrace.h b/applications/kube/backtrace.h new file mode 100644 index 00000000..d200aa26 --- /dev/null +++ b/applications/kube/backtrace.h @@ -0,0 +1,22 @@ +/* + Copyright (c) 2017 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + + +//Print a demangled stacktrace +void printStacktrace(); diff --git a/applications/kube/main.cpp b/applications/kube/main.cpp index a146a8a7..20b033cb 100644 --- a/applications/kube/main.cpp +++ b/applications/kube/main.cpp @@ -21,18 +21,11 @@ #include #include #include -#include -#include -#include #include #include #ifndef Q_OS_WIN -#include #include -#include -#include #else -#include #include #endif @@ -51,50 +44,10 @@ #include #include +#include "backtrace.h" #include "framework/src/keyring.h" #include "kube_version.h" -//Print a demangled stacktrace -void printStacktrace() -{ -#ifndef Q_OS_WIN - int skip = 1; - void *callstack[128]; - const int nMaxFrames = sizeof(callstack) / sizeof(callstack[0]); - char buf[1024]; - int nFrames = backtrace(callstack, nMaxFrames); - char **symbols = backtrace_symbols(callstack, nFrames); - - std::ostringstream trace_buf; - for (int i = skip; i < nFrames; i++) { - // printf("%s\n", symbols[i]); - Dl_info info; - if (dladdr(callstack[i], &info) && info.dli_sname) { - char *demangled = NULL; - int status = -1; - if (info.dli_sname[0] == '_') { - demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status); - } - snprintf(buf, sizeof(buf), "%-3d %*p %s + %zd\n", - i, int(2 + sizeof(void*) * 2), callstack[i], - status == 0 ? demangled : - info.dli_sname == 0 ? symbols[i] : info.dli_sname, - (char *)callstack[i] - (char *)info.dli_saddr); - free(demangled); - } else { - snprintf(buf, sizeof(buf), "%-3d %*p %s\n", - i, int(2 + sizeof(void*) * 2), callstack[i], symbols[i]); - } - trace_buf << buf; - } - free(symbols); - if (nFrames == nMaxFrames) { - trace_buf << "[truncated]\n"; - } - std::cerr << trace_buf.str(); -#endif -} - static int sCounter = 0; void crashHandler(int signal) -- cgit v1.2.3