summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2018-08-14 20:23:27 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2018-08-14 20:23:27 +0200
commite25e2bb20e826d215f08328f22ad0e8d7c9a0bb5 (patch)
tree0e82adcc0ab6ffb1483a51a4ad58ca62abab47f6
parent919b7cd8a40a42cf03e823efd6d4db1294c2a6ef (diff)
downloadkube-e25e2bb20e826d215f08328f22ad0e8d7c9a0bb5.tar.gz
kube-e25e2bb20e826d215f08328f22ad0e8d7c9a0bb5.zip
Moved the backtrace function to a separate file
-rw-r--r--applications/kube/CMakeLists.txt2
-rw-r--r--applications/kube/backtrace.cpp74
-rw-r--r--applications/kube/backtrace.h22
-rw-r--r--applications/kube/main.cpp49
4 files changed, 98 insertions, 49 deletions
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)
5find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Quick) 5find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Quick)
6 6
7# install executable 7# install executable
8set(SRCS main.cpp) 8set(SRCS main.cpp backtrace.cpp)
9 9
10if(APPLE OR WIN32) 10if(APPLE OR WIN32)
11 # Sets the icon on Windows and OSX 11 # 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 @@
1/*
2 Copyright (c) 2017 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#include "backtrace.h"
20
21#include <QtGlobal>
22#include <iostream>
23#include <cstdlib>
24#include <ostream>
25#include <sstream>
26#ifndef Q_OS_WIN
27#include <execinfo.h>
28#include <unistd.h>
29#include <cxxabi.h>
30#include <dlfcn.h>
31#else
32#include <io.h>
33#include <process.h>
34#endif
35
36void printStacktrace()
37{
38#ifndef Q_OS_WIN
39 int skip = 1;
40 void *callstack[128];
41 const int nMaxFrames = sizeof(callstack) / sizeof(callstack[0]);
42 char buf[1024];
43 int nFrames = backtrace(callstack, nMaxFrames);
44 char **symbols = backtrace_symbols(callstack, nFrames);
45
46 std::ostringstream trace_buf;
47 for (int i = skip; i < nFrames; i++) {
48 // printf("%s\n", symbols[i]);
49 Dl_info info;
50 if (dladdr(callstack[i], &info) && info.dli_sname) {
51 char *demangled = NULL;
52 int status = -1;
53 if (info.dli_sname[0] == '_') {
54 demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status);
55 }
56 snprintf(buf, sizeof(buf), "%-3d %*p %s + %zd\n",
57 i, int(2 + sizeof(void*) * 2), callstack[i],
58 status == 0 ? demangled :
59 info.dli_sname == 0 ? symbols[i] : info.dli_sname,
60 (char *)callstack[i] - (char *)info.dli_saddr);
61 free(demangled);
62 } else {
63 snprintf(buf, sizeof(buf), "%-3d %*p %s\n",
64 i, int(2 + sizeof(void*) * 2), callstack[i], symbols[i]);
65 }
66 trace_buf << buf;
67 }
68 free(symbols);
69 if (nFrames == nMaxFrames) {
70 trace_buf << "[truncated]\n";
71 }
72 std::cerr << trace_buf.str();
73#endif
74}
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 @@
1/*
2 Copyright (c) 2017 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20
21//Print a demangled stacktrace
22void 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 @@
21#include <signal.h> 21#include <signal.h>
22#include <csignal> 22#include <csignal>
23#include <iostream> 23#include <iostream>
24#include <cstdlib>
25#include <ostream>
26#include <sstream>
27#include <thread> 24#include <thread>
28#include <chrono> 25#include <chrono>
29#ifndef Q_OS_WIN 26#ifndef Q_OS_WIN
30#include <execinfo.h>
31#include <unistd.h> 27#include <unistd.h>
32#include <cxxabi.h>
33#include <dlfcn.h>
34#else 28#else
35#include <io.h>
36#include <process.h> 29#include <process.h>
37#endif 30#endif
38 31
@@ -51,50 +44,10 @@
51#include <QResource> 44#include <QResource>
52#include <sink/store.h> 45#include <sink/store.h>
53 46
47#include "backtrace.h"
54#include "framework/src/keyring.h" 48#include "framework/src/keyring.h"
55#include "kube_version.h" 49#include "kube_version.h"
56 50
57//Print a demangled stacktrace
58void printStacktrace()
59{
60#ifndef Q_OS_WIN
61 int skip = 1;
62 void *callstack[128];
63 const int nMaxFrames = sizeof(callstack) / sizeof(callstack[0]);
64 char buf[1024];
65 int nFrames = backtrace(callstack, nMaxFrames);
66 char **symbols = backtrace_symbols(callstack, nFrames);
67
68 std::ostringstream trace_buf;
69 for (int i = skip; i < nFrames; i++) {
70 // printf("%s\n", symbols[i]);
71 Dl_info info;
72 if (dladdr(callstack[i], &info) && info.dli_sname) {
73 char *demangled = NULL;
74 int status = -1;
75 if (info.dli_sname[0] == '_') {
76 demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status);
77 }
78 snprintf(buf, sizeof(buf), "%-3d %*p %s + %zd\n",
79 i, int(2 + sizeof(void*) * 2), callstack[i],
80 status == 0 ? demangled :
81 info.dli_sname == 0 ? symbols[i] : info.dli_sname,
82 (char *)callstack[i] - (char *)info.dli_saddr);
83 free(demangled);
84 } else {
85 snprintf(buf, sizeof(buf), "%-3d %*p %s\n",
86 i, int(2 + sizeof(void*) * 2), callstack[i], symbols[i]);
87 }
88 trace_buf << buf;
89 }
90 free(symbols);
91 if (nFrames == nMaxFrames) {
92 trace_buf << "[truncated]\n";
93 }
94 std::cerr << trace_buf.str();
95#endif
96}
97
98static int sCounter = 0; 51static int sCounter = 0;
99 52
100void crashHandler(int signal) 53void crashHandler(int signal)