2 #define I3__FILE__ "sighandler.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8 * © 2009-2010 Jan-Erik Rediger
10 * sighandler.c: Interactive crash dialog upon SIGSEGV/SIGABRT/SIGFPE (offers
11 * to restart inplace).
21 #include <xcb/xcb_event.h>
23 #include <X11/keysym.h>
25 static void open_popups(void);
27 static xcb_gcontext_t pixmap_gc;
28 static xcb_pixmap_t pixmap;
29 static int raised_signal;
31 static char *crash_text[] = {
33 "To debug this problem, either attach gdb now",
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"
39 static int crash_text_longest = 5;
40 static int backtrace_string_index = 3;
41 static int backtrace_done = 0;
44 * Attach gdb to pid_parent and dump a backtrace to i3-backtrace.$pid in the
47 static int backtrace(void) {
48 char *tmpdir = getenv("TMPDIR");
52 pid_t pid_parent = getpid();
54 char *filename = NULL;
55 if (sasprintf(&filename, "%s/i3-backtrace.%d.txt", tmpdir, pid_parent) == -1
57 filename = "i3-backtrace.txt";
59 pid_t pid_gdb = fork();
61 DLOG("Failed to fork for GDB\n");
63 } else if (pid_gdb == 0) {
66 /* close standard streams in case i3 is started from a terminal; gdb
67 * needs to run without controlling terminal for it to work properly in
74 sasprintf(&pid_s, "%d", pid_parent);
76 char *gdb_log_cmd = NULL;
77 if (sasprintf(&gdb_log_cmd, "set logging file %s", filename) == -1
78 || gdb_log_cmd == NULL)
79 gdb_log_cmd = "set logging file i3-backtrace.txt";
89 "-ex", "set logging on",
94 execvp(args[0], args);
95 DLOG("Failed to exec GDB\n");
101 waitpid(pid_gdb, &status, 0);
103 /* see if the backtrace was succesful or not */
104 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
105 DLOG("GDB did not run properly\n");
107 } else if (stat(filename, &bt) == -1) {
108 DLOG("GDB executed succesfully, but no backtrace was generated\n");
115 * Draw the window containing the info text
118 static int sig_draw_window(xcb_window_t win, int width, int height, int font_height, i3String **crash_text_i3strings) {
119 /* re-draw the background */
120 xcb_rectangle_t border = { 0, 0, width, height},
121 inner = { 2, 2, width - 4, height - 4};
122 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#FF0000") });
123 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
124 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#000000") });
125 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
127 /* restore font color */
128 set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
130 char *bt_colour = "#FFFFFF";
131 if (backtrace_done < 0)
132 bt_colour = "#AA0000";
133 else if (backtrace_done > 0)
134 bt_colour = "#00AA00";
136 for (int i = 0; crash_text_i3strings[i] != NULL; ++i) {
137 /* fix the colour for the backtrace line when it finished */
138 if (i == backtrace_string_index)
139 set_font_colors(pixmap_gc, get_colorpixel(bt_colour), get_colorpixel("#000000"));
141 draw_text(crash_text_i3strings[i], pixmap, pixmap_gc,
142 8, 5 + i * font_height, width - 16);
144 /* and reset the colour again for other lines */
145 if (i == backtrace_string_index)
146 set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
149 /* Copy the contents of the pixmap to the real window */
150 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, width, height);
157 * Handles keypresses of 'b', 'r' and 'f' to get a backtrace or restart i3
160 static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
161 uint16_t state = event->state;
163 /* Apparantly, after activating numlock once, the numlock modifier
164 * stays turned on (use xev(1) to verify). So, to resolve useful
165 * keysyms, we remove the numlock flag from the event state */
166 state &= ~xcb_numlock_mask;
168 xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
171 DLOG("User issued core-dump command.\n");
173 /* fork and exec/attach GDB to the parent to get a backtrace in the
175 backtrace_done = backtrace();
177 /* re-open the windows to indicate that it's finished */
191 * Opens the window we use for input/output and maps it
194 static xcb_window_t open_input_window(xcb_connection_t *conn, Rect screen_rect, uint32_t width, uint32_t height) {
195 xcb_window_t win = xcb_generate_id(conn);
200 mask |= XCB_CW_BACK_PIXEL;
203 mask |= XCB_CW_OVERRIDE_REDIRECT;
206 /* center each popup on the specified screen */
207 uint32_t x = screen_rect.x + ((screen_rect.width / 2) - (width / 2)),
208 y = screen_rect.y + ((screen_rect.height / 2) - (height / 2));
210 xcb_create_window(conn,
211 XCB_COPY_FROM_PARENT,
212 win, /* the window id */
213 root, /* parent == root */
214 x, y, width, height, /* dimensions */
215 0, /* border = 0, we draw our own */
216 XCB_WINDOW_CLASS_INPUT_OUTPUT,
217 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
221 /* Map the window (= make it visible) */
222 xcb_map_window(conn, win);
227 static void open_popups() {
228 /* width and height of the popup window, so that the text fits in */
229 int crash_text_num = sizeof(crash_text) / sizeof(char*);
230 int height = 13 + (crash_text_num * config.font.height);
232 int crash_text_length = sizeof(crash_text) / sizeof(char*);
233 i3String **crash_text_i3strings = smalloc(sizeof(i3String *) * (crash_text_length + 1));
234 /* Pre-compute i3Strings for our text */
235 for (int i = 0; i < crash_text_length; ++i) {
236 crash_text_i3strings[i] = i3string_from_utf8(crash_text[i]);
238 crash_text_i3strings[crash_text_length] = NULL;
239 /* calculate width for longest text */
240 int font_width = predict_text_width(crash_text_i3strings[crash_text_longest]);
241 int width = font_width + 20;
243 /* Open a popup window on each virtual screen */
246 TAILQ_FOREACH(screen, &outputs, outputs) {
249 win = open_input_window(conn, screen->rect, width, height);
252 pixmap = xcb_generate_id(conn);
253 pixmap_gc = xcb_generate_id(conn);
254 xcb_create_pixmap(conn, root_depth, pixmap, win, width, height);
255 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
257 /* Grab the keyboard to get all input */
258 xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
260 /* Grab the cursor inside the popup */
261 xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
262 XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
264 sig_draw_window(win, width, height, config.font.height, crash_text_i3strings);
271 * It creates a window asking the user to restart in-place
272 * or exit to generate a core dump
275 void handle_signal(int sig, siginfo_t *info, void *data) {
276 DLOG("i3 crashed. SIG: %d\n", sig);
278 struct sigaction action;
279 action.sa_handler = SIG_DFL;
280 sigaction(sig, &action, NULL);
285 xcb_generic_event_t *event;
286 /* Yay, more own eventhandlers… */
287 while ((event = xcb_wait_for_event(conn))) {
288 /* Strip off the highest bit (set if the event is generated) */
289 int type = (event->response_type & 0x7F);
290 if (type == XCB_KEY_PRESS) {
291 sig_handle_key_press(NULL, conn, (xcb_key_press_event_t*)event);
298 * Setup signal handlers to safely handle SIGSEGV and SIGFPE
301 void setup_signal_handler(void) {
302 struct sigaction action;
304 action.sa_sigaction = handle_signal;
305 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
306 sigemptyset(&action.sa_mask);
308 /* Catch all signals with default action "Core", see signal(7) */
309 if (sigaction(SIGQUIT, &action, NULL) == -1 ||
310 sigaction(SIGILL, &action, NULL) == -1 ||
311 sigaction(SIGABRT, &action, NULL) == -1 ||
312 sigaction(SIGFPE, &action, NULL) == -1 ||
313 sigaction(SIGSEGV, &action, NULL) == -1)
314 ELOG("Could not setup signal handler");