src/CrashCatcher.cc

changeset 629
b75c6cce02e2
child 639
851634b85893
equal deleted inserted replaced
628:6b13e4c2e97b 629:b75c6cce02e2
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013, 2014 Santeri Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifdef __unix__
20
21 #include <QString>
22 #include <QProcess>
23 #include <QTemporaryFile>
24 #include <QMessageBox>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <sys/prctl.h>
28 #include "CrashCatcher.h"
29 #include "Types.h"
30 #include "Dialogs.h"
31
32 // Is the crash catcher active now?
33 static bool g_crashCatcherActive = false;
34
35 // If an assertion failed, what was it?
36 static QString g_assertionFailure;
37
38 // List of signals to catch and crash on
39 static QList<int> g_signalsToCatch ({
40 SIGSEGV, // segmentation fault
41 SIGABRT, // abort() calls
42 SIGFPE, // floating point exceptions (e.g. division by zero)
43 SIGILL, // illegal instructions
44 });
45
46 // =============================================================================
47 // -----------------------------------------------------------------------------
48 static void handleCrash (int sig)
49 {
50 printf ("%s: crashed with signal %d, launching gdb\n", __func__, sig);
51
52 if (g_crashCatcherActive)
53 {
54 printf ("caught signal while crash catcher is active!\n");
55 exit (149);
56 }
57
58 const pid_t pid = getpid();
59 QProcess proc;
60 QTemporaryFile commandsFile;
61
62 g_crashCatcherActive = true;
63
64 if (commandsFile.open())
65 {
66 commandsFile.write (fmt ("attach %1\n", pid).toLocal8Bit());
67 commandsFile.write (QString ("backtrace full\n").toLocal8Bit());
68 commandsFile.write (QString ("detach\n").toLocal8Bit());
69 commandsFile.write (QString ("quit").toLocal8Bit());
70 commandsFile.flush();
71 commandsFile.close();
72 }
73
74 QStringList args ({"-x", commandsFile.fileName()});
75
76 proc.start ("gdb", args);
77
78 // Linux doesn't allow ptrace to be used on anything but direct child processes
79 // so we need to use prctl to register an exception to this to allow GDB attach to us.
80 // We need to do this now and no earlier because only now we actually know GDB's PID.
81 prctl (PR_SET_PTRACER, proc.pid(), 0, 0, 0);
82
83 proc.waitForFinished (1000);
84 QString output = QString (proc.readAllStandardOutput());
85 QString err = QString (proc.readAllStandardError());
86
87 bombBox (fmt ("<h3>Program crashed with signal %1</h3>\n\n"
88 "%2"
89 "<p><b>GDB <tt>stdout</tt>:</b></p><pre>%3</pre>\n"
90 "<p><b>GDB <tt>stderr</tt>:</b></p><pre>%4</pre>",
91 sig, (!g_assertionFailure.isEmpty()) ? g_assertionFailure : "", output, err));
92 }
93
94 // =============================================================================
95 // -----------------------------------------------------------------------------
96 void initCrashCatcher()
97 {
98 struct sigaction sighandler;
99 sighandler.sa_handler = &handleCrash;
100 sighandler.sa_flags = 0;
101 sigemptyset (&sighandler.sa_mask);
102
103 for (int sig : g_signalsToCatch)
104 sigaction (sig, &sighandler, null);
105
106 log ("%1: crash catcher hooked to signals: %2\n", __func__, g_signalsToCatch);
107 }
108 #endif // #ifdef __unix__
109
110 // =============================================================================
111 // This function must be readily available in both Windows and Linux. We display
112 // the bomb box straight in Windows while in Linux we let abort() trigger the
113 // signal handler, which will cause the usual bomb box with GDB diagnostics.
114 // Said prompt will embed the assertion failure information.
115 // -----------------------------------------------------------------------------
116 void assertionFailure (const char* file, int line, const char* funcname, const char* expr)
117 {
118 QString errmsg = fmt (
119 "<p><b>File</b>: <tt>%1</tt><br />"
120 "<b>Line</b>: <tt>%2</tt><br />"
121 "<b>Function:</b> <tt>%3</tt></p>"
122 "<p>Assertion <b><tt>`%4'</tt></b> failed.</p>",
123 file, line, funcname, expr);
124
125 g_assertionFailure = errmsg;
126
127 #ifndef __unix__
128 bombBox (errmsg);
129 #endif
130
131 abort();
132 }

mercurial