compositor: Use a minimal restore handler for crash clean up

When we hit a segv, it's often the case that we might crash again in
the attempt to clean up.  Instead we introduce a minimal restore callback
in the backend abstraction, that shuts down as simply as possible.  Then
we can call that from the segv handler, and then to aid debugging, we
raise SIGTRAP in the segv handler.  This lets us run gdb on weston from
a different vt, and if we tell gdb

  (gdb) handle SIGSEGV nostop

gdb won't stop when the segv happens but let weston clean up and switch vt,
and then stop when SIGTRAP is raised.

It's also possible to just let gdb catch the segv, and then use sysrq+k
followed by manual vt switch to get back.
diff --git a/src/compositor.c b/src/compositor.c
index 62ae79b..bd2a0d2 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -57,7 +57,7 @@
 #include "git-version.h"
 
 static struct wl_list child_process_list;
-static jmp_buf segv_jmp_buf;
+static struct weston_compositor *segv_compositor;
 
 static int
 sigchld_handler(int signal_number, void *data)
@@ -3309,6 +3309,14 @@
 	int i, count;
 	Dl_info info;
 
+	/* This SIGSEGV handler will do a best-effort backtrace, and
+	 * then call the backend restore function, which will switch
+	 * back to the vt we launched from or ungrab X etc and then
+	 * raise SIGTRAP.  If we run weston under gdb from X or a
+	 * different vt, and tell gdb "handle SIGSEGV nostop", this
+	 * will allow weston to switch back to gdb on crash and then
+	 * gdb will catch the crash with SIGTRAP. */
+
 	weston_log("caught segv\n");
 
 	count = backtrace(buffer, ARRAY_LENGTH(buffer));
@@ -3320,7 +3328,9 @@
 			info.dli_fname);
 	}
 
-	longjmp(segv_jmp_buf, 1);
+	segv_compositor->restore(segv_compositor);
+
+	raise(SIGTRAP);
 }
 
 
@@ -3513,11 +3523,6 @@
 	signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
 					      NULL);
 
-	segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
-	segv_action.sa_sigaction = on_segv_signal;
-	sigemptyset(&segv_action.sa_mask);
-	sigaction(SIGSEGV, &segv_action, NULL);
-
 	if (!backend) {
 		if (getenv("WAYLAND_DISPLAY"))
 			backend = "wayland-backend.so";
@@ -3542,6 +3547,12 @@
 		exit(EXIT_FAILURE);
 	}
 
+	segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
+	segv_action.sa_sigaction = on_segv_signal;
+	sigemptyset(&segv_action.sa_mask);
+	sigaction(SIGSEGV, &segv_action, NULL);
+	segv_compositor = ec;
+
 	for (i = 1; argv[i]; i++)
 		weston_log("fatal: unhandled option: %s\n", argv[i]);
 	if (argv[1]) {
@@ -3589,12 +3600,10 @@
 
 	weston_compositor_dpms_on(ec);
 	weston_compositor_wake(ec);
-	if (setjmp(segv_jmp_buf) == 0)
-		wl_display_run(display);
-	else
-		ret = EXIT_FAILURE;
 
-out:
+	wl_display_run(display);
+
+ out:
 	/* prevent further rendering while shutting down */
 	ec->state = WESTON_COMPOSITOR_SLEEPING;