]> git.sur5r.net Git - i3/i3/blob - src/sighandler.c
sighandler: provide gdb with pipe stdin/stdout fds (necessary for gdb < 7.5)
[i3/i3] / src / sighandler.c
1 #undef I3__FILE__
2 #define I3__FILE__ "sighandler.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  * © 2009-2010 Jan-Erik Rediger
9  *
10  * sighandler.c: Interactive crash dialog upon SIGSEGV/SIGABRT/SIGFPE (offers
11  *               to restart inplace).
12  *
13  */
14 #include "all.h"
15
16 #include <ev.h>
17 #include <iconv.h>
18 #include <signal.h>
19 #include <sys/wait.h>
20
21 #include <xcb/xcb_event.h>
22
23 #include <X11/keysym.h>
24
25 static void open_popups(void);
26
27 static xcb_gcontext_t pixmap_gc;
28 static xcb_pixmap_t pixmap;
29 static int raised_signal;
30
31 static char *crash_text[] = {
32     "i3 just crashed.",
33     "To debug this problem, either attach gdb now",
34     "or press",
35     "- 'b' to save a backtrace (needs GDB),",
36     "- 'r' to restart i3 in-place or",
37     "- 'f' to forget the current layout and restart"
38 };
39 static int crash_text_longest = 5;
40 static int backtrace_string_index = 3;
41 static int backtrace_done = 0;
42
43 /*
44  * Attach gdb to pid_parent and dump a backtrace to i3-backtrace.$pid in the
45  * tmpdir
46  */
47 static int backtrace(void) {
48     char *tmpdir = getenv("TMPDIR");
49     if (tmpdir == NULL)
50         tmpdir = "/tmp";
51
52     pid_t pid_parent = getpid();
53
54     char *filename = NULL;
55     if (sasprintf(&filename, "%s/i3-backtrace.%d.txt", tmpdir, pid_parent) == -1
56             || filename == NULL)
57         filename = "i3-backtrace.txt";
58
59     pid_t pid_gdb = fork();
60     if (pid_gdb < 0) {
61         DLOG("Failed to fork for GDB\n");
62         return -1;
63     } else if (pid_gdb == 0) {
64         /* child */
65         int stdin_pipe[2],
66             stdout_pipe[2];
67
68         pipe(stdin_pipe);
69         pipe(stdout_pipe);
70
71         /* close standard streams in case i3 is started from a terminal; gdb
72          * needs to run without controlling terminal for it to work properly in
73          * this situation */
74         close(STDIN_FILENO);
75         close(STDOUT_FILENO);
76         close(STDERR_FILENO);
77
78         /* We provide pipe file descriptors for stdin/stdout because gdb < 7.5
79          * crashes otherwise, see
80          * http://sourceware.org/bugzilla/show_bug.cgi?id=14114 */
81         dup2(stdin_pipe[0], STDIN_FILENO);
82         dup2(stdout_pipe[1], STDOUT_FILENO);
83
84         char *pid_s = NULL;
85         sasprintf(&pid_s, "%d", pid_parent);
86
87         char *gdb_log_cmd = NULL;
88         if (sasprintf(&gdb_log_cmd, "set logging file %s", filename) == -1
89                 || gdb_log_cmd == NULL)
90             gdb_log_cmd = "set logging file i3-backtrace.txt";
91
92         char *args[] = {
93             "gdb",
94             start_argv[0],
95             "-p",
96             pid_s,
97             "-batch",
98             "-nx",
99             "-ex", gdb_log_cmd,
100             "-ex", "set logging on",
101             "-ex", "bt full",
102             "-ex", "quit",
103             NULL
104         };
105         execvp(args[0], args);
106         DLOG("Failed to exec GDB\n");
107         exit(1);
108     }
109     int status = 0;
110     struct stat bt;
111
112     waitpid(pid_gdb, &status, 0);
113
114     /* see if the backtrace was succesful or not */
115     if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
116         DLOG("GDB did not run properly\n");
117         return -1;
118     } else if (stat(filename, &bt) == -1) {
119         DLOG("GDB executed succesfully, but no backtrace was generated\n");
120         return -1;
121     }
122     return 1;
123 }
124
125 /*
126  * Draw the window containing the info text
127  *
128  */
129 static int sig_draw_window(xcb_window_t win, int width, int height, int font_height, i3String **crash_text_i3strings) {
130     /* re-draw the background */
131     xcb_rectangle_t border = { 0, 0, width, height},
132                     inner = { 2, 2, width - 4, height - 4};
133     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#FF0000") });
134     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
135     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#000000") });
136     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
137
138     /* restore font color */
139     set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
140
141     char *bt_colour = "#FFFFFF";
142     if (backtrace_done < 0)
143         bt_colour = "#AA0000";
144     else if (backtrace_done > 0)
145         bt_colour = "#00AA00";
146
147     for (int i = 0; crash_text_i3strings[i] != NULL; ++i) {
148         /* fix the colour for the backtrace line when it finished */
149         if (i == backtrace_string_index)
150             set_font_colors(pixmap_gc, get_colorpixel(bt_colour), get_colorpixel("#000000"));
151
152         draw_text(crash_text_i3strings[i], pixmap, pixmap_gc,
153                 8, 5 + i * font_height, width - 16);
154
155         /* and reset the colour again for other lines */
156         if (i == backtrace_string_index)
157             set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
158     }
159
160     /* Copy the contents of the pixmap to the real window */
161     xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, width, height);
162     xcb_flush(conn);
163
164     return 1;
165 }
166
167 /*
168  * Handles keypresses of 'b', 'r' and 'f' to get a backtrace or restart i3
169  *
170  */
171 static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
172     uint16_t state = event->state;
173
174     /* Apparantly, after activating numlock once, the numlock modifier
175      * stays turned on (use xev(1) to verify). So, to resolve useful
176      * keysyms, we remove the numlock flag from the event state */
177     state &= ~xcb_numlock_mask;
178
179     xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
180
181     if (sym == 'b') {
182         DLOG("User issued core-dump command.\n");
183
184         /* fork and exec/attach GDB to the parent to get a backtrace in the
185          * tmpdir */
186         backtrace_done = backtrace();
187
188         /* re-open the windows to indicate that it's finished */
189         open_popups();
190     }
191
192     if (sym == 'r')
193         i3_restart(false);
194
195     if (sym == 'f')
196         i3_restart(true);
197
198     return 1;
199 }
200
201 /*
202  * Opens the window we use for input/output and maps it
203  *
204  */
205 static xcb_window_t open_input_window(xcb_connection_t *conn, Rect screen_rect, uint32_t width, uint32_t height) {
206     xcb_window_t win = xcb_generate_id(conn);
207
208     uint32_t mask = 0;
209     uint32_t values[2];
210
211     mask |= XCB_CW_BACK_PIXEL;
212     values[0] = 0;
213
214     mask |= XCB_CW_OVERRIDE_REDIRECT;
215     values[1] = 1;
216
217     /* center each popup on the specified screen */
218     uint32_t x = screen_rect.x + ((screen_rect.width / 2) - (width / 2)),
219              y = screen_rect.y + ((screen_rect.height / 2) - (height / 2));
220
221     xcb_create_window(conn,
222                       XCB_COPY_FROM_PARENT,
223                       win, /* the window id */
224                       root, /* parent == root */
225                       x, y, width, height, /* dimensions */
226                       0, /* border = 0, we draw our own */
227                       XCB_WINDOW_CLASS_INPUT_OUTPUT,
228                       XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
229                       mask,
230                       values);
231
232     /* Map the window (= make it visible) */
233     xcb_map_window(conn, win);
234
235     return win;
236 }
237
238 static void open_popups() {
239     /* width and height of the popup window, so that the text fits in */
240     int crash_text_num = sizeof(crash_text) / sizeof(char*);
241     int height = 13 + (crash_text_num * config.font.height);
242
243     int crash_text_length = sizeof(crash_text) / sizeof(char*);
244     i3String **crash_text_i3strings = smalloc(sizeof(i3String *) * (crash_text_length + 1));
245     /* Pre-compute i3Strings for our text */
246     for (int i = 0; i < crash_text_length; ++i) {
247         crash_text_i3strings[i] = i3string_from_utf8(crash_text[i]);
248     }
249     crash_text_i3strings[crash_text_length] = NULL;
250     /* calculate width for longest text */
251     int font_width = predict_text_width(crash_text_i3strings[crash_text_longest]);
252     int width = font_width + 20;
253
254     /* Open a popup window on each virtual screen */
255     Output *screen;
256     xcb_window_t win;
257     TAILQ_FOREACH(screen, &outputs, outputs) {
258         if (!screen->active)
259             continue;
260         win = open_input_window(conn, screen->rect, width, height);
261
262         /* Create pixmap */
263         pixmap = xcb_generate_id(conn);
264         pixmap_gc = xcb_generate_id(conn);
265         xcb_create_pixmap(conn, root_depth, pixmap, win, width, height);
266         xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
267
268         /* Grab the keyboard to get all input */
269         xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
270
271         /* Grab the cursor inside the popup */
272         xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
273                          XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
274
275         sig_draw_window(win, width, height, config.font.height, crash_text_i3strings);
276         xcb_flush(conn);
277     }
278 }
279
280 /*
281  * Handle signals
282  * It creates a window asking the user to restart in-place
283  * or exit to generate a core dump
284  *
285  */
286 void handle_signal(int sig, siginfo_t *info, void *data) {
287     DLOG("i3 crashed. SIG: %d\n", sig);
288
289     struct sigaction action;
290     action.sa_handler = SIG_DFL;
291     sigaction(sig, &action, NULL);
292     raised_signal = sig;
293
294     open_popups();
295
296     xcb_generic_event_t *event;
297     /* Yay, more own eventhandlers… */
298     while ((event = xcb_wait_for_event(conn))) {
299         /* Strip off the highest bit (set if the event is generated) */
300         int type = (event->response_type & 0x7F);
301         if (type == XCB_KEY_PRESS) {
302             sig_handle_key_press(NULL, conn, (xcb_key_press_event_t*)event);
303         }
304         free(event);
305     }
306 }
307
308 /*
309  * Setup signal handlers to safely handle SIGSEGV and SIGFPE
310  *
311  */
312 void setup_signal_handler(void) {
313     struct sigaction action;
314
315     action.sa_sigaction = handle_signal;
316     action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
317     sigemptyset(&action.sa_mask);
318
319     /* Catch all signals with default action "Core", see signal(7) */
320     if (sigaction(SIGQUIT, &action, NULL) == -1 ||
321         sigaction(SIGILL, &action, NULL) == -1 ||
322         sigaction(SIGABRT, &action, NULL) == -1 ||
323         sigaction(SIGFPE, &action, NULL) == -1 ||
324         sigaction(SIGSEGV, &action, NULL) == -1)
325         ELOG("Could not setup signal handler");
326 }