src/crashcatcher.cpp

Fri, 18 Oct 2013 21:52:09 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 18 Oct 2013 21:52:09 +0300
changeset 513
29eb671b34f6
child 514
d78fea0f664c
permissions
-rw-r--r--

Added a crash catcher which trigger under Linux. It calls GDB and tries to get a backtrace. Also integrated assertion failure handling to this new system. Removed the print() function in the process (because the new bomb box uses a text edit and QTextDocuments's print method clashes with the macro and I figured it was a good idea to rid it anyway) and replaced all calls with log().

#ifdef __unix__

#include <QString>
#include <QProcess>
#include <QTemporaryFile>
#include <QMessageBox>
#include <unistd.h>
#include <signal.h>
#include <sys/prctl.h>
#include "crashcatcher.h"
#include "types.h"
#include "dialogs.h"

// Is the crash catcher active now?
static bool g_crashCatcherActive = false;

// If an assertion failed, what was it?
static str g_assertionFailure;

// List of signals to catch and crash on
static QList<int> g_signalsToCatch ({
	SIGSEGV, // segmentation fault
	SIGABRT, // abort() calls
	SIGFPE, // floating point exceptions (e.g. division by zero)
	SIGILL, // illegal instructions
});

// =============================================================================
// -----------------------------------------------------------------------------
static void handleSignal (int sig)
{	printf ("caught signal %d\n", sig);

	if (g_crashCatcherActive)
	{	printf ("caught signal while crash catcher is active!\n");
		return;
	}

	if (g_signalsToCatch.indexOf (sig) != -1)
	{	const pid_t pid = getpid();
		QProcess proc;
		QTemporaryFile commandsFile;

		g_crashCatcherActive = true;
		printf ("%s: crashed with signal %d, launching gdb\n", __func__, sig);

		if (commandsFile.open())
		{	commandsFile.write (fmt ("attach %1\n", pid).toLocal8Bit());
			commandsFile.write (str ("backtrace full\n").toLocal8Bit());
			commandsFile.write (str ("detach\n").toLocal8Bit());
			commandsFile.write (str ("quit").toLocal8Bit());
			commandsFile.flush();
			commandsFile.close();
		}

		QStringList args ({"-x", commandsFile.fileName()});

		proc.start ("gdb", args);

		// Linux doesn't allow ptrace to be used on anything but direct child processes
		// so we need to use prctl to register an exception to this to allow GDB attach to us.
		// We need to do this now and no earlier because only now we actually know GDB's PID.
		prctl (PR_SET_PTRACER, proc.pid(), 0, 0, 0);

		proc.waitForFinished (5000);
		str output = QString (proc.readAllStandardOutput());
		str err = QString (proc.readAllStandardError());

		bombBox (fmt ("<h3>Program crashed with signal %1</h3>\n\n"
			"%2"
			"<p><b>GDB <tt>stdout</tt>:</b></p><pre>%3</pre>\n"
			"<p><b>GDB <tt>stderr</tt>:</b></p><pre>%4</pre>",
			sig, (!g_assertionFailure.isEmpty()) ? g_assertionFailure : "", output, err));
		exit (137);
	}
}

// =============================================================================
// -----------------------------------------------------------------------------
void initCrashCatcher()
{	struct sigaction sighandler;
	sighandler.sa_handler = &handleSignal;
	sighandler.sa_flags = 0;
	sigemptyset (&sighandler.sa_mask);

	for (int sig : g_signalsToCatch)
		sigaction (sig, &sighandler, null);

	log ("%1: crash catcher hooked to signals: %2\n", __func__, g_signalsToCatch);
}
#endif // #ifdef __unix__

// =============================================================================
// This function must be readily available in both Windows and Linux. We display
// the bomb box straight in Windows while in Linux we let abort() trigger the
// signal handler, which will cause the usual bomb box with GDB diagnostics.
// Said prompt will embed the assertion failure information.
// -----------------------------------------------------------------------------
void assertionFailure (const char* file, int line, const char* funcname, const char* expr)
{	str errmsg = fmt (
		"<p><b>File</b>: <tt>%1</tt><br />"
		"<b>Line</b>: <tt>%2</tt><br />"
		"<b>Function:</b> <tt>%3</tt></p>"
		"<p>Assertion <b><tt>`%4'</tt></b> failed.</p>",
		file, line, funcname, expr);

	g_assertionFailure = errmsg;

#ifndef __unix__
	bombBox (errmsg);
#endif

	abort();
}

mercurial