2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
15 #include <xcb/xcb_event.h>
17 #include <X11/keysym.h>
19 typedef struct dialog_t {
21 xcb_colormap_t colormap;
29 static TAILQ_HEAD(dialogs_head, dialog_t) dialogs = TAILQ_HEAD_INITIALIZER(dialogs);
30 static int raised_signal;
31 static int backtrace_done = 0;
33 static int sighandler_backtrace(void);
34 static void sighandler_setup(void);
35 static void sighandler_create_dialogs(void);
36 static void sighandler_destroy_dialogs(void);
37 static void sighandler_handle_expose(void);
38 static void sighandler_draw_dialog(dialog_t *dialog);
39 static void sighandler_handle_key_press(xcb_key_press_event_t *event);
41 static i3String *message_intro;
42 static i3String *message_intro2;
43 static i3String *message_option_backtrace;
44 static i3String *message_option_restart;
45 static i3String *message_option_forget;
46 static int dialog_width;
47 static int dialog_height;
49 static int border_width = 2;
50 static int margin = 4;
53 * Attach gdb to pid_parent and dump a backtrace to i3-backtrace.$pid in the
56 static int sighandler_backtrace(void) {
57 char *tmpdir = getenv("TMPDIR");
61 pid_t pid_parent = getpid();
63 char *filename = NULL;
65 /* Find a unique filename for the backtrace (since the PID of i3 stays the
66 * same), so that we don’t overwrite earlier backtraces. */
69 sasprintf(&filename, "%s/i3-backtrace.%d.%d.txt", tmpdir, pid_parent, suffix);
71 } while (path_exists(filename));
73 pid_t pid_gdb = fork();
75 DLOG("Failed to fork for GDB\n");
77 } else if (pid_gdb == 0) {
82 if (pipe(stdin_pipe) == -1) {
83 ELOG("Failed to init stdin_pipe\n");
86 if (pipe(stdout_pipe) == -1) {
87 ELOG("Failed to init stdout_pipe\n");
91 /* close standard streams in case i3 is started from a terminal; gdb
92 * needs to run without controlling terminal for it to work properly in
98 /* We provide pipe file descriptors for stdin/stdout because gdb < 7.5
99 * crashes otherwise, see
100 * https://sourceware.org/bugzilla/show_bug.cgi?id=14114 */
101 dup2(stdin_pipe[0], STDIN_FILENO);
102 dup2(stdout_pipe[1], STDOUT_FILENO);
104 char *pid_s, *gdb_log_cmd;
105 sasprintf(&pid_s, "%d", pid_parent);
106 sasprintf(&gdb_log_cmd, "set logging file %s", filename);
116 "-ex", "set logging on",
120 execvp(args[0], args);
121 DLOG("Failed to exec GDB\n");
126 waitpid(pid_gdb, &status, 0);
128 /* see if the backtrace was successful or not */
129 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
130 DLOG("GDB did not run properly\n");
132 } else if (!path_exists(filename)) {
133 DLOG("GDB executed successfully, but no backtrace was generated\n");
139 static void sighandler_setup(void) {
140 border_width = logical_px(border_width);
141 margin = logical_px(margin);
144 message_intro = i3string_from_utf8("i3 has just crashed. Please report a bug for this.");
145 message_intro2 = i3string_from_utf8("To debug this problem, you can either attach gdb or choose from the following options:");
146 message_option_backtrace = i3string_from_utf8("- 'b' to save a backtrace (requires gdb)");
147 message_option_restart = i3string_from_utf8("- 'r' to restart i3 in-place");
148 message_option_forget = i3string_from_utf8("- 'f' to forget the previous layout and restart i3");
150 int width_longest_message = predict_text_width(message_intro2);
152 dialog_width = width_longest_message + 2 * border_width + 2 * margin;
153 dialog_height = num_lines * config.font.height + 2 * border_width + 2 * margin;
156 static void sighandler_create_dialogs(void) {
158 TAILQ_FOREACH(output, &outputs, outputs) {
159 if (!output->active) {
163 dialog_t *dialog = scalloc(1, sizeof(struct dialog_t));
164 TAILQ_INSERT_TAIL(&dialogs, dialog, dialogs);
166 xcb_visualid_t visual = get_visualid_by_depth(root_depth);
167 dialog->colormap = xcb_generate_id(conn);
168 xcb_create_colormap(conn, XCB_COLORMAP_ALLOC_NONE, dialog->colormap, root, visual);
174 /* Needs to be set in the case of a 32-bit root depth. */
175 mask |= XCB_CW_BACK_PIXEL;
176 values[i++] = root_screen->black_pixel;
178 /* Needs to be set in the case of a 32-bit root depth. */
179 mask |= XCB_CW_BORDER_PIXEL;
180 values[i++] = root_screen->black_pixel;
182 mask |= XCB_CW_OVERRIDE_REDIRECT;
185 /* Needs to be set in the case of a 32-bit root depth. */
186 mask |= XCB_CW_COLORMAP;
187 values[i++] = dialog->colormap;
189 dialog->dims.x = output->rect.x + (output->rect.width / 2);
190 dialog->dims.y = output->rect.y + (output->rect.height / 2);
191 dialog->dims.width = dialog_width;
192 dialog->dims.height = dialog_height;
194 /* Make sure the dialog is centered. */
195 dialog->dims.x -= dialog->dims.width / 2;
196 dialog->dims.y -= dialog->dims.height / 2;
198 dialog->id = create_window(conn, dialog->dims, root_depth, visual,
199 XCB_WINDOW_CLASS_INPUT_OUTPUT, XCURSOR_CURSOR_POINTER,
202 draw_util_surface_init(conn, &(dialog->surface), dialog->id, get_visualtype_by_id(visual),
203 dialog->dims.width, dialog->dims.height);
205 xcb_grab_keyboard(conn, false, dialog->id, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
207 /* Confine the pointer to the crash dialog. */
208 xcb_grab_pointer(conn, false, dialog->id, XCB_NONE, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, dialog->id,
209 XCB_NONE, XCB_CURRENT_TIME);
212 sighandler_handle_expose();
216 static void sighandler_destroy_dialogs(void) {
217 while (!TAILQ_EMPTY(&dialogs)) {
218 dialog_t *dialog = TAILQ_FIRST(&dialogs);
220 xcb_free_colormap(conn, dialog->colormap);
221 draw_util_surface_free(conn, &(dialog->surface));
222 xcb_destroy_window(conn, dialog->id);
224 TAILQ_REMOVE(&dialogs, dialog, dialogs);
231 static void sighandler_handle_expose(void) {
233 TAILQ_FOREACH(current, &dialogs, dialogs) {
234 sighandler_draw_dialog(current);
240 static void sighandler_draw_dialog(dialog_t *dialog) {
241 const color_t black = draw_util_hex_to_color("#000000");
242 const color_t white = draw_util_hex_to_color("#FFFFFF");
243 const color_t red = draw_util_hex_to_color("#FF0000");
245 /* Start with a clean slate and draw a red border. */
246 draw_util_clear_surface(&(dialog->surface), red);
247 draw_util_rectangle(&(dialog->surface), black, border_width, border_width,
248 dialog->dims.width - 2 * border_width, dialog->dims.height - 2 * border_width);
250 int y = border_width + margin;
251 const int x = border_width + margin;
252 const int max_width = dialog->dims.width - 2 * x;
254 draw_util_text(message_intro, &(dialog->surface), white, black, x, y, max_width);
255 y += config.font.height;
257 draw_util_text(message_intro2, &(dialog->surface), white, black, x, y, max_width);
258 y += config.font.height;
260 char *bt_color = "#FFFFFF";
261 if (backtrace_done < 0) {
262 bt_color = "#AA0000";
263 } else if (backtrace_done > 0) {
264 bt_color = "#00AA00";
266 draw_util_text(message_option_backtrace, &(dialog->surface), draw_util_hex_to_color(bt_color), black, x, y, max_width);
267 y += config.font.height;
269 draw_util_text(message_option_restart, &(dialog->surface), white, black, x, y, max_width);
270 y += config.font.height;
272 draw_util_text(message_option_forget, &(dialog->surface), white, black, x, y, max_width);
273 y += config.font.height;
276 static void sighandler_handle_key_press(xcb_key_press_event_t *event) {
277 uint16_t state = event->state;
279 /* Apparently, after activating numlock once, the numlock modifier
280 * stays turned on (use xev(1) to verify). So, to resolve useful
281 * keysyms, we remove the numlock flag from the event state */
282 state &= ~xcb_numlock_mask;
284 xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
287 DLOG("User issued core-dump command.\n");
289 /* fork and exec/attach GDB to the parent to get a backtrace in the
291 backtrace_done = sighandler_backtrace();
292 sighandler_handle_expose();
293 } else if (sym == 'r') {
294 sighandler_destroy_dialogs();
296 } else if (sym == 'f') {
297 sighandler_destroy_dialogs();
302 static void handle_signal(int sig, siginfo_t *info, void *data) {
303 DLOG("i3 crashed. SIG: %d\n", sig);
305 struct sigaction action;
306 action.sa_handler = SIG_DFL;
308 sigemptyset(&action.sa_mask);
309 sigaction(sig, &action, NULL);
313 sighandler_create_dialogs();
315 xcb_generic_event_t *event;
316 /* Yay, more own eventhandlers… */
317 while ((event = xcb_wait_for_event(conn))) {
318 /* Strip off the highest bit (set if the event is generated) */
319 int type = (event->response_type & 0x7F);
322 sighandler_handle_key_press((xcb_key_press_event_t *)event);
325 if (((xcb_expose_event_t *)event)->count == 0) {
326 sighandler_handle_expose();
337 * Configured a signal handler to gracefully handle crashes and allow the user
338 * to generate a backtrace and rescue their session.
341 void setup_signal_handler(void) {
342 struct sigaction action;
344 action.sa_sigaction = handle_signal;
345 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
346 sigemptyset(&action.sa_mask);
348 /* Catch all signals with default action "Core", see signal(7) */
349 if (sigaction(SIGQUIT, &action, NULL) == -1 ||
350 sigaction(SIGILL, &action, NULL) == -1 ||
351 sigaction(SIGABRT, &action, NULL) == -1 ||
352 sigaction(SIGFPE, &action, NULL) == -1 ||
353 sigaction(SIGSEGV, &action, NULL) == -1)
354 ELOG("Could not setup signal handler.\n");