|
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 |
|
28 #ifdef Q_OS_LINUX |
|
29 # include <sys/prctl.h> |
|
30 #endif |
|
31 |
|
32 #include "crashCatcher.h" |
|
33 #include "basics.h" |
|
34 #include "dialogs.h" |
|
35 |
|
36 // Is the crash catcher active now? |
|
37 static bool g_crashCatcherActive = false; |
|
38 |
|
39 // If an assertion failed, what was it? |
|
40 static QString g_assertionFailure; |
|
41 |
|
42 // List of signals to catch and crash on |
|
43 static QList<int> g_signalsToCatch ({ |
|
44 SIGSEGV, // segmentation fault |
|
45 SIGABRT, // abort() calls |
|
46 SIGFPE, // floating point exceptions (e.g. division by zero) |
|
47 SIGILL, // illegal instructions |
|
48 }); |
|
49 |
|
50 // ============================================================================= |
|
51 // |
|
52 static void handleCrash (int sig) |
|
53 { |
|
54 printf ("%s: crashed with signal %d, launching gdb\n", __func__, sig); |
|
55 |
|
56 if (g_crashCatcherActive) |
|
57 { |
|
58 printf ("caught signal while crash catcher is active!\n"); |
|
59 exit (149); |
|
60 } |
|
61 |
|
62 const pid_t pid = getpid(); |
|
63 QProcess proc; |
|
64 QTemporaryFile commandsFile; |
|
65 |
|
66 g_crashCatcherActive = true; |
|
67 |
|
68 if (commandsFile.open()) |
|
69 { |
|
70 commandsFile.write (format ("attach %1\n", pid).toLocal8Bit()); |
|
71 commandsFile.write (QString ("backtrace full\n").toLocal8Bit()); |
|
72 commandsFile.write (QString ("detach\n").toLocal8Bit()); |
|
73 commandsFile.write (QString ("quit").toLocal8Bit()); |
|
74 commandsFile.flush(); |
|
75 commandsFile.close(); |
|
76 } |
|
77 |
|
78 QStringList args ({"-x", commandsFile.fileName()}); |
|
79 |
|
80 proc.start ("gdb", args); |
|
81 |
|
82 // Linux doesn't allow ptrace to be used on anything but direct child processes |
|
83 // so we need to use prctl to register an exception to this to allow GDB attach to us. |
|
84 // We need to do this now and no earlier because only now we actually know GDB's PID. |
|
85 #ifdef Q_OS_LINUX |
|
86 prctl (PR_SET_PTRACER, proc.pid(), 0, 0, 0); |
|
87 #endif |
|
88 |
|
89 proc.waitForFinished (1000); |
|
90 QString output = QString (proc.readAllStandardOutput()); |
|
91 QString err = QString (proc.readAllStandardError()); |
|
92 |
|
93 bombBox (format ("<h3>Program crashed with signal %1</h3>\n\n" |
|
94 "%2" |
|
95 "<p><b>GDB <tt>stdout</tt>:</b></p><pre>%3</pre>\n" |
|
96 "<p><b>GDB <tt>stderr</tt>:</b></p><pre>%4</pre>", |
|
97 sig, (!g_assertionFailure.isEmpty()) ? g_assertionFailure : "", output, err)); |
|
98 } |
|
99 |
|
100 // ============================================================================= |
|
101 // |
|
102 void initCrashCatcher() |
|
103 { |
|
104 struct sigaction sighandler; |
|
105 sighandler.sa_handler = &handleCrash; |
|
106 sighandler.sa_flags = 0; |
|
107 sigemptyset (&sighandler.sa_mask); |
|
108 |
|
109 for (int sig : g_signalsToCatch) |
|
110 sigaction (sig, &sighandler, null); |
|
111 |
|
112 print ("%1: crash catcher hooked to signals: %2\n", __func__, g_signalsToCatch); |
|
113 } |
|
114 #endif // #ifdef __unix__ |
|
115 |
|
116 // ============================================================================= |
|
117 // |
|
118 // This function must be readily available in both Windows and Linux. We display |
|
119 // the bomb box straight in Windows while in Linux we let abort() trigger the |
|
120 // signal handler, which will cause the usual bomb box with GDB diagnostics. |
|
121 // Said prompt will embed the assertion failure information. |
|
122 // |
|
123 void assertionFailure (const char* file, int line, const char* funcname, const char* expr) |
|
124 { |
|
125 QString errmsg = format ( |
|
126 "<p><b>File</b>: <tt>%1</tt><br />" |
|
127 "<b>Line</b>: <tt>%2</tt><br />" |
|
128 "<b>Function:</b> <tt>%3</tt></p>" |
|
129 "<p>Assertion <b><tt>`%4'</tt></b> failed.</p>", |
|
130 file, line, funcname, expr); |
|
131 |
|
132 g_assertionFailure = errmsg; |
|
133 |
|
134 #ifndef __unix__ |
|
135 bombBox (errmsg); |
|
136 #endif |
|
137 |
|
138 abort(); |
|
139 } |