src/crashCatcher.cpp

Thu, 04 Jan 2018 19:44:26 +0200

author
Santeri Piippo
date
Thu, 04 Jan 2018 19:44:26 +0200
changeset 1217
314e12e23c3a
parent 1011
4fdd74a04790
child 1222
34def2630300
permissions
-rw-r--r--

fix paren style

655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
968
4b93b7963456 Happy new year 2015
Teemu Piippo <crimsondusk64@gmail.com>
parents: 958
diff changeset
3 * Copyright (C) 2013 - 2015 Teemu Piippo
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
19 #include <QtGlobal>
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
20
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
21 #ifdef Q_OS_UNIX
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
22 # include <QProcess>
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
23 # include <QTemporaryFile>
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
24 # include <unistd.h>
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
25 # include <signal.h>
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
26 # include "crashCatcher.h"
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
27 # include "dialogs.h"
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
28
855
e16f1587ef44 - fixed compilation on windows systems
Santeri Piippo <crimsondusk64@gmail.com>
parents: 848
diff changeset
29 # ifdef Q_OS_LINUX
e16f1587ef44 - fixed compilation on windows systems
Santeri Piippo <crimsondusk64@gmail.com>
parents: 848
diff changeset
30 # include <sys/prctl.h>
e16f1587ef44 - fixed compilation on windows systems
Santeri Piippo <crimsondusk64@gmail.com>
parents: 848
diff changeset
31 # endif
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32
1011
4fdd74a04790 Rename LDSubfile to LDSubfileReference
Teemu Piippo <crimsondusk64@gmail.com>
parents: 984
diff changeset
33
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
34 // Removes the signal handler from SIGABRT and then aborts.
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
35 static void finalAbort()
778
0ed96169b8c0 - crashcatcher: don't use the bomb box at all, instead just write the crashlog and then abort for good
Santeri Piippo <crimsondusk64@gmail.com>
parents: 765
diff changeset
36 {
0ed96169b8c0 - crashcatcher: don't use the bomb box at all, instead just write the crashlog and then abort for good
Santeri Piippo <crimsondusk64@gmail.com>
parents: 765
diff changeset
37 struct sigaction sighandler;
0ed96169b8c0 - crashcatcher: don't use the bomb box at all, instead just write the crashlog and then abort for good
Santeri Piippo <crimsondusk64@gmail.com>
parents: 765
diff changeset
38 sighandler.sa_handler = SIG_DFL;
0ed96169b8c0 - crashcatcher: don't use the bomb box at all, instead just write the crashlog and then abort for good
Santeri Piippo <crimsondusk64@gmail.com>
parents: 765
diff changeset
39 sighandler.sa_flags = 0;
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
40 sigaction(SIGABRT, &sighandler, 0);
778
0ed96169b8c0 - crashcatcher: don't use the bomb box at all, instead just write the crashlog and then abort for good
Santeri Piippo <crimsondusk64@gmail.com>
parents: 765
diff changeset
41 abort();
0ed96169b8c0 - crashcatcher: don't use the bomb box at all, instead just write the crashlog and then abort for good
Santeri Piippo <crimsondusk64@gmail.com>
parents: 765
diff changeset
42 }
0ed96169b8c0 - crashcatcher: don't use the bomb box at all, instead just write the crashlog and then abort for good
Santeri Piippo <crimsondusk64@gmail.com>
parents: 765
diff changeset
43
1011
4fdd74a04790 Rename LDSubfile to LDSubfileReference
Teemu Piippo <crimsondusk64@gmail.com>
parents: 984
diff changeset
44
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
45 static void handleCrash(int sig)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 {
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
47 static bool isActive = false;
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
48 printf("!! Caught signal %d, launching gdb\n", sig);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
50 if (isActive)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
52 printf("Caught signal while crash catcher is active! Execution cannot continue.\n");
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
53 finalAbort();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
56 pid_t const pid(getpid());
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 QProcess proc;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 QTemporaryFile commandsFile;
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
59 isActive = true;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 if (commandsFile.open())
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
63 commandsFile.write(format("attach %1\n", pid).toLocal8Bit());
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
64 commandsFile.write(QString("backtrace full\n").toLocal8Bit());
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
65 commandsFile.write(QString("detach\n").toLocal8Bit());
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
66 commandsFile.write(QString("quit").toLocal8Bit());
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 commandsFile.close();
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
70 proc.start("gdb", {"-x", commandsFile.fileName()});
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 // Linux doesn't allow ptrace to be used on anything but direct child processes
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 // so we need to use prctl to register an exception to this to allow GDB attach to us.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 // We need to do this now and no earlier because only now we actually know GDB's PID.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 #ifdef Q_OS_LINUX
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
76 prctl(PR_SET_PTRACER, proc.pid(), 0, 0, 0);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 #endif
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
79 proc.waitForFinished(1000);
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
80 QString output(proc.readAllStandardOutput());
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
81 QString err(proc.readAllStandardError());
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
82 QFile f(UNIXNAME "-crash.log");
765
6d8ca5219a57 - crashcatcher: hide the main window before doing anything and also write the crashlog into a file
Santeri Piippo <crimsondusk64@gmail.com>
parents: 739
diff changeset
83
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
84 if (f.open(QIODevice::WriteOnly))
765
6d8ca5219a57 - crashcatcher: hide the main window before doing anything and also write the crashlog into a file
Santeri Piippo <crimsondusk64@gmail.com>
parents: 739
diff changeset
85 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
86 fprint(f, format("=== Program crashed with signal %1 ===\n\n"
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
87 "GDB stdout:\n%3\nGDB stderr:\n%4\n", sig,
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
88 output, err));
765
6d8ca5219a57 - crashcatcher: hide the main window before doing anything and also write the crashlog into a file
Santeri Piippo <crimsondusk64@gmail.com>
parents: 739
diff changeset
89 f.close();
6d8ca5219a57 - crashcatcher: hide the main window before doing anything and also write the crashlog into a file
Santeri Piippo <crimsondusk64@gmail.com>
parents: 739
diff changeset
90 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
92 printf("Backtrace written to " UNIXNAME "-crash.log. Aborting.\n");
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
93 finalAbort();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95
1011
4fdd74a04790 Rename LDSubfile to LDSubfileReference
Teemu Piippo <crimsondusk64@gmail.com>
parents: 984
diff changeset
96
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
97 void initCrashCatcher()
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 {
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
99 // List of signals to catch and crash on
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
100 static const int signalsToCatch[] = {
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
101 SIGSEGV, // segmentation fault
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
102 SIGABRT, // abort() calls
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
103 SIGFPE, // floating point exceptions(e.g. division by zero)
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
104 SIGILL, // illegal instructions
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
105 };
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
106
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 struct sigaction sighandler;
1011
4fdd74a04790 Rename LDSubfile to LDSubfileReference
Teemu Piippo <crimsondusk64@gmail.com>
parents: 984
diff changeset
108 sighandler.sa_handler = &handleCrash;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 sighandler.sa_flags = 0;
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
110 sigemptyset(&sighandler.sa_mask);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
112 for (int sig : signalsToCatch)
778
0ed96169b8c0 - crashcatcher: don't use the bomb box at all, instead just write the crashlog and then abort for good
Santeri Piippo <crimsondusk64@gmail.com>
parents: 765
diff changeset
113 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
114 if (sigaction(sig, &sighandler, nullptr) == -1)
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
115 fprint(stderr, "Couldn't set signal handler %1: %2", sig, strerror(errno));
778
0ed96169b8c0 - crashcatcher: don't use the bomb box at all, instead just write the crashlog and then abort for good
Santeri Piippo <crimsondusk64@gmail.com>
parents: 765
diff changeset
116 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 1011
diff changeset
118 print("Crash catcher hooked to signals: %1\n", signalsToCatch);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 }
778
0ed96169b8c0 - crashcatcher: don't use the bomb box at all, instead just write the crashlog and then abort for good
Santeri Piippo <crimsondusk64@gmail.com>
parents: 765
diff changeset
120
958
1dc890c73e01 Removed all asserts from the code. Removed assert handling and the bomb box dialog.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
121 #endif // Q_OS_UNIX

mercurial