src/crashCatcher.cpp

Sat, 29 Aug 2015 16:30:56 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 29 Aug 2015 16:30:56 +0300
changeset 951
54d90af8542d
parent 941
f895379d7fab
permissions
-rw-r--r--

Closed old branch

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
927
409b82a4765e - bump copyright year
Teemu Piippo <crimsondusk64@gmail.com>
parents: 882
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
855
e16f1587ef44 - fixed compilation on windows systems
Santeri Piippo <crimsondusk64@gmail.com>
parents: 848
diff changeset
19 #ifdef __unix__
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 931
diff changeset
20 # include <QProcess>
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 931
diff changeset
21 # include <QTemporaryFile>
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 931
diff changeset
22 # include <unistd.h>
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 931
diff changeset
23 # include <signal.h>
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 931
diff changeset
24 # include "crashCatcher.h"
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 931
diff changeset
25 # include "dialogs.h"
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 931
diff changeset
26
855
e16f1587ef44 - fixed compilation on windows systems
Santeri Piippo <crimsondusk64@gmail.com>
parents: 848
diff changeset
27 # ifdef Q_OS_LINUX
e16f1587ef44 - fixed compilation on windows systems
Santeri Piippo <crimsondusk64@gmail.com>
parents: 848
diff changeset
28 # include <sys/prctl.h>
e16f1587ef44 - fixed compilation on windows systems
Santeri Piippo <crimsondusk64@gmail.com>
parents: 848
diff changeset
29 # endif
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 // Is the crash catcher active now?
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
32 static bool IsCrashCatcherActive = false;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 // List of signals to catch and crash on
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
35 static QList<int> SignalsToCatch ({
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 SIGSEGV, // segmentation fault
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 SIGABRT, // abort() calls
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 SIGFPE, // floating point exceptions (e.g. division by zero)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 SIGILL, // illegal instructions
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 });
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
42 // -------------------------------------------------------------------------------------------------
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
43 //
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
44 // Removes the signal handler from SIGABRT and then aborts.
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
45 //
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
46 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
47 {
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
48 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
49 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
50 sighandler.sa_flags = 0;
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
51 sigaction (SIGABRT, &sighandler, 0);
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
52 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
53 }
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
54
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
55 // -------------------------------------------------------------------------------------------------
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 //
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
57 static void HandleCrash (int sig)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 {
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
59 printf ("!! Caught signal %d, launching gdb\n", sig);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
61 if (IsCrashCatcherActive)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 {
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
63 printf ("Caught signal while crash catcher is active! Execution cannot continue.\n");
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
64 FinalAbort();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
67 pid_t const pid (getpid());
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 QProcess proc;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 QTemporaryFile commandsFile;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
71 IsCrashCatcherActive = true;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 if (commandsFile.open())
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 commandsFile.write (format ("attach %1\n", pid).toLocal8Bit());
789
4b7306f52bb5 - String -> QString
Santeri Piippo <crimsondusk64@gmail.com>
parents: 779
diff changeset
76 commandsFile.write (QString ("backtrace full\n").toLocal8Bit());
4b7306f52bb5 - String -> QString
Santeri Piippo <crimsondusk64@gmail.com>
parents: 779
diff changeset
77 commandsFile.write (QString ("detach\n").toLocal8Bit());
4b7306f52bb5 - String -> QString
Santeri Piippo <crimsondusk64@gmail.com>
parents: 779
diff changeset
78 commandsFile.write (QString ("quit").toLocal8Bit());
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 commandsFile.close();
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81
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
82 proc.start ("gdb", {"-x", commandsFile.fileName()});
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 // 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
85 // 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
86 // We need to do this now and no earlier because only now we actually know GDB's PID.
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 931
diff changeset
87 # ifdef Q_OS_LINUX
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 prctl (PR_SET_PTRACER, proc.pid(), 0, 0, 0);
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 931
diff changeset
89 # endif
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 proc.waitForFinished (1000);
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
92 QString output (proc.readAllStandardOutput());
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
93 QString err (proc.readAllStandardError());
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
94 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
95
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
96 if (f.open (QIODevice::WriteOnly))
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
97 {
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 931
diff changeset
98 fprint (f, format ("=== Program crashed with signal %1 ===\n\n"
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 931
diff changeset
99 "GDB stdout:\n%3\nGDB stderr:\n%4\n", sig, 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
100 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
101 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
103 printf ("Backtrace written to " UNIXNAME "-crash.log. Aborting.\n");
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
104 FinalAbort();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
107 // -------------------------------------------------------------------------------------------------
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
108 //
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
109 // Initializes the crash catcher.
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 //
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
111 void InitCrashCatcher()
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 struct sigaction sighandler;
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
114 sighandler.sa_handler = &HandleCrash;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 sighandler.sa_flags = 0;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 sigemptyset (&sighandler.sa_mask);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
118 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
119 {
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 if (sigaction (sig, &sighandler, null) == -1)
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
121 {
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
122 fprint (stderr, "Couldn't set signal handler %1: %2", sig, strerror (errno));
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
123 SignalsToCatch.removeOne (sig);
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
124 }
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
125 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126
848
68410477c8bb - refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
127 print ("Crash catcher hooked to signals: %1\n", SignalsToCatch);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 }
941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 931
diff changeset
129 #endif // #ifdef __unix__

mercurial