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 successfully, 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");
static void open_popups(void)
i3String * i3string_from_utf8(const char *from_utf8)
Build an i3String from an UTF-8 encoded string.
Stores a rectangle, for example the size of a window, the child window etc.
static int sig_draw_window(xcb_window_t win, int width, int height, int font_height, i3String **crash_text_i3strings)
struct _i3String i3String
Opaque data structure for storing strings.
static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event)
static int backtrace(void)
uint32_t get_colorpixel(const char *hex) __attribute__((const ))
Returns the colorpixel to use for the given hex color (think of HTML).
static xcb_pixmap_t pixmap
static xcb_gcontext_t pixmap_gc
static int crash_text_longest
struct outputs_head outputs
Rect rect
x, y, width, height
static int backtrace_string_index
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
void handle_signal(int sig, siginfo_t *info, void *data)
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
static char * crash_text[]
bool active
Whether the output is currently active (has a CRTC attached with a valid mode)
An Output is a physical output on your graphics driver.
#define TAILQ_FOREACH(var, head, field)
void setup_signal_handler(void)
Setup signal handlers to safely handle SIGSEGV and SIGFPE.
static int backtrace_done
void draw_text(i3String *text, xcb_drawable_t drawable, xcb_gcontext_t gc, int x, int y, int max_width)
Draws text onto the specified X drawable (normally a pixmap) at the specified coordinates (from the t...
static xcb_window_t open_input_window(xcb_connection_t *conn, Rect screen_rect, uint32_t width, uint32_t height)
void i3_restart(bool forget_layout)
Restart i3 in-place appends -a to argument list to disable autostart.
xcb_key_symbols_t * keysyms
int height
The height of the font, built from font_ascent + font_descent.
unsigned int xcb_numlock_mask
int predict_text_width(i3String *text)
Predict the text width in pixels for the given text.
void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background)
Defines the colors to be used for the forthcoming draw_text calls.