2 #define I3__FILE__ "sighandler.c"
21 #include <xcb/xcb_event.h>
23 #include <X11/keysym.h>
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"
48 char *tmpdir = getenv(
"TMPDIR");
52 pid_t pid_parent = getpid();
54 char *filename = NULL;
61 sasprintf(&filename,
"%s/i3-backtrace.%d.%d.txt", tmpdir, pid_parent, suffix);
63 }
while (stat(filename, &bt) == 0);
65 pid_t pid_gdb = fork();
67 DLOG(
"Failed to fork for GDB\n");
69 }
else if (pid_gdb == 0) {
87 dup2(stdin_pipe[0], STDIN_FILENO);
88 dup2(stdout_pipe[1], STDOUT_FILENO);
90 char *pid_s, *gdb_log_cmd;
92 sasprintf(&gdb_log_cmd,
"set logging file %s", filename);
102 "-ex",
"set logging on",
107 execvp(args[0], args);
108 DLOG(
"Failed to exec GDB\n");
113 waitpid(pid_gdb, &status, 0);
116 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
117 DLOG(
"GDB did not run properly\n");
119 }
else if (stat(filename, &bt) == -1) {
120 DLOG(
"GDB executed succesfully, but no backtrace was generated\n");
132 xcb_rectangle_t border = { 0, 0,
width, height},
133 inner = { 2, 2, width - 4, height - 4};
142 char *bt_colour =
"#FFFFFF";
144 bt_colour =
"#AA0000";
146 bt_colour =
"#00AA00";
148 for (
int i = 0; crash_text_i3strings[i] != NULL; ++i) {
154 8, 5 + i * font_height, width - 16);
173 uint16_t
state =
event->state;
180 xcb_keysym_t sym = xcb_key_press_lookup_keysym(
keysyms, event, state);
183 DLOG(
"User issued core-dump command.\n");
207 xcb_window_t win = xcb_generate_id(conn);
212 mask |= XCB_CW_BACK_PIXEL;
215 mask |= XCB_CW_OVERRIDE_REDIRECT;
219 uint32_t
x = screen_rect.
x + ((screen_rect.
width / 2) - (width / 2)),
220 y = screen_rect.
y + ((screen_rect.
height / 2) - (height / 2));
222 xcb_create_window(conn,
223 XCB_COPY_FROM_PARENT,
228 XCB_WINDOW_CLASS_INPUT_OUTPUT,
229 XCB_WINDOW_CLASS_COPY_FROM_PARENT,
234 xcb_map_window(conn, win);
241 int crash_text_num =
sizeof(
crash_text) /
sizeof(
char*);
244 int crash_text_length =
sizeof(
crash_text) /
sizeof(
char*);
247 for (
int i = 0; i < crash_text_length; ++i) {
250 crash_text_i3strings[crash_text_length] = NULL;
253 int width = font_width + 20;
270 xcb_grab_keyboard(
conn,
false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
273 xcb_grab_pointer(
conn,
false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
274 XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
288 DLOG(
"i3 crashed. SIG: %d\n", sig);
290 struct sigaction action;
291 action.sa_handler = SIG_DFL;
292 sigaction(sig, &action, NULL);
297 xcb_generic_event_t *event;
299 while ((event = xcb_wait_for_event(
conn))) {
301 int type = (
event->response_type & 0x7F);
302 if (type == XCB_KEY_PRESS) {
314 struct sigaction action;
317 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
318 sigemptyset(&action.sa_mask);
321 if (sigaction(SIGQUIT, &action, NULL) == -1 ||
322 sigaction(SIGILL, &action, NULL) == -1 ||
323 sigaction(SIGABRT, &action, NULL) == -1 ||
324 sigaction(SIGFPE, &action, NULL) == -1 ||
325 sigaction(SIGSEGV, &action, NULL) == -1)
326 ELOG(
"Could not setup signal handler");